I - Jam’s math problem
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u
Submit
Status
Description
Jam has a math problem. He just learned factorization.
He is trying to factorize ax^2+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m).
He could only solve the problem in which p,q,m,k are positive numbers.
Please help him determine whether the expression could be factorized with p,q,m,k being postive.
Input
The first line is a number T, means there are T(1 \leq T \leq 100 ) cases
Each case has one line,the line has 3 numbers a,b,c (1 \leq a,b,c \leq 100000000)
Output
You should output the “YES” or “NO”.
Sample Input
2
1 6 5
1 6 4
Sample Output
YES
NO
Hint
The first case turn x2+6∗x+5 into (x+1)(x+5)
- 题意:求二元一次方程根的个数。
- 思路:用图像法:当x=0时,f(x)=ax^2+bx+c=c>0,又a>0,可以简单画出图像,要满足题意需图像与x轴有交点,根据根的判别式判断,题中还有一要求,就是p,q,k,m都是整数,根据求根公式可得sqrt(b*b-4*a*c)一定要为整数。(数学公式不好打呀)
- 失误:第一次没有用数学方法,超时;第二次没考虑到根式必须开尽。看来数学题还是都有规律,都是特殊情况的,应该推出公式。
- 代码如下:
正确代码:
#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
__int64 t,a,b,c,s,ans;
cin>>t;
while(t--)
{
cin>>a>>b>>c;
s=b*b-4*a*c;//有根
ans=sqrt(s);//根式开的尽
if(s>=0&&ans*ans==s)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
超时代码:
#include<cstdio>
#include<iostream>
using namespace std;
int main()
{
__int64 t,p,q,a,b,c,flag,k,m;
cin>>t;
while(t--)
{
cin>>a>>b>>c;
flag=0;
for(p=1;p<=a;++p)
{
if(a%p!=0)
continue;
for(k=1;k<=c;++k)
{
if(c%k!=0)
continue;
q=a/p;
m=c/k;
if(q*k+m*p==b)
{
flag=1;
break;
}
}
if(flag==1)
break;
}
if(flag)
{
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
return 0;
}