HAUTOJ quadratic equation

来源:互联网 发布:黄金未来走势预测知乎 编辑:程序博客网 时间:2024/06/05 23:46

问题 F: quadratic equation

时间限制: 2 秒  内存限制: 128 MB
提交: 268  解决: 42
 
 

题目描述

With given integers a,b,c, you are asked to judge whether the following statement is true:"For anyx, if ax2+bx+c=0, thenx is an integer."

输入

The first line contains only one integer T(1≤T≤2000), which indicates the number of test cases.
For each test case, there is only one line containing three integers a,b,c(−5≤a,b,c≤5).

输出

or each test case, output “YES” if thestatement is true, or “NO” if not.

样例输入

31 4 40 0 11 3 1

样例输出

YESYESNO

提示

 

题意:

问是否存在这样的命题:对于任意的x, 满足 ax2+bx+c=0,则x一定为整数

即若有解,则所有解都为整数

若无解,则由样例知为YES


注意

某种情况此方程会变成一元一次方程

当a,b,c都为0时为NO,因为x可取任意实数


思路:

枚举加特判


代码:

#include<iostream>#include<cstdio>#include<algorithm>using namespace std;int main(){    int n;    scanf("%d",&n);    while(n--)    {        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        if(b*b-4*a*c<0)printf("YES\n");        //只要有解,其解一定为整数        else if(a==b && b==c && a==0)printf("NO\n");        else        {            int f=0,f1=0;            int p=b*b-4*a*c;            if(a==0 && (b!=0))//一元一次方程            {                int l=0;                for(int i=-10;i<=10;i++)                    if(b*i+c==0)                    {                        l=1;                        break;                    }                if(l)printf("YES\n");                else printf("NO\n");                continue;            }            for(int i=-10;i<=10;i++)            {                if(a*i*i+b*i==0)                {                    f1++;//常函数的时候                }                if(a*i*i+b*i+c==0)                {                    f++;                }                if(p==0 && f)break;                if(f==2)break;            }            if(f1>=20)printf("YES\n");            else if(f==1 && p==0)printf("YES\n");            else if(f==2)printf("YES\n");            else printf("NO\n");        }    }    return 0;}



阅读全文
0 0
原创粉丝点击