山东省第八届ACM省赛 quadratic equation(简单数学)

Problem Description

With given integers a,b,c, you are asked to judge whether the following statement is true: “For any x, if a x 2 + b x + c = 0 , then x is an integer.”

Input

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).

Output

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

Sample Input

3
1 4 4
0 0 1
1 3 1

Sample Output

YES
YES
NO

解题思路

命题的理解:
如果 a x 2 + b x + c = 0 成立,且解x均为正数,输出”YES”;
如果 a x 2 + b x + c = 0 不成立即不存在x使得这个等式为0,输出“YES”.
注意考虑a,b,c为 0 时的特殊情况.

代码实现

#include<bits/stdc++.h>
using namespace std;
#define IO ios::sync_with_stdio(false);\
cin.tie(0);\
cout.tie(0);
typedef long long ll;
const ll mod = 1e9+7;
#define INF 0x3f3f3f3f
const int maxn = 1e5+7;
int main()
{
    IO;
    int T;
    int a,b,c;
    cin>>T;
    while(T--)
    {
        bool flag=false;
        cin>>a>>b>>c;
        if(a!=0)
        {
            int t=b*b-4*a*c;
            if(t>=0)
            {
                int sqr=sqrt(t);
                if(sqr*sqr==t)
                {
                    if((-b+sqr)%(2*a)==0&&(-b-sqr)%(2*a)==0)
                    {
                        flag=true;
                    }
                }
            }
            else
                flag=true;
        }
        else if(a==0&&b!=0)
        {
            if(-c%b==0) flag=true;
        }
        else if(a==0&&b==0&&c!=0) flag=true;
        if(flag) cout<<"YES"<<endl;
        else cout<<"NO"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/so_so_y/article/details/80067607