quadratic equation

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+bx+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

Hint

Source

"Inspur Cup" Shandong Province 8th ACM College Student Programming Competition (Thanks to Qingdao University of Science and Technology)


First of all, this question is to determine whether the implication is true or false.

p -> q

If the premise is false, the result is true no matter what (0->1, 0->0 are all true)

If the premise is true, the result is true, true (1->1 is true)

If the premise is true, the result is false and false (1->0 is false)

So it should be noted that if it is a quadratic equation, no solution is true, that is, when the discriminant is less than 0, it is easy to ignore that if ab is all 0

Only the constant c is left. If c=0, it means that the premise is true, and the premise is satisfied for any value of x, so it is false at this time, and when c takes other constants, the premise is false, and the proposition is true.

But do you think you'll be able to pass this way? Not necessarily. I've paid more than 10 times, but I still don't know what else to pay attention to in this question. I can only paste the code on the Internet.

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        double a,b,c;
        scanf("%lf%lf%lf",&a,&b,&c);
        if(a == 0 && b == 0){
            if(c == 0) printf("NO\n");//The precondition is 1, and the result is 0 (x may not be an integer) is false
            else printf("YES\n");//The precondition is 0, both are true
        }
        else if(a == 0){
            if((-c / b) == (int)(-c / b))
                printf("YES\n");
            else
                printf("NO\n");
        }
        else if(b * b - 4 * a* c >= 0){
            int flag = 0;
            for(int i = -5; i <= 5; i++){
                if(a * i * i + b * i + c == 0)
                    flag++;
            }
            if(b * b - 4 * a * c == 0 && flag == 1)
                printf("YES\n");
            else if(b * b - 4 * a * c > 0 && flag == 2)
                printf("YES\n");
            else printf("NO\n");
        }
        else printf("YES\n");
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325872404&siteId=291194637