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

“浪潮杯”山东省第八届ACM大学生程序设计竞赛(感谢青岛科技大学)

题意:给你一个逻辑公式,给出a,b,c,让你判断这个逻辑公式对错;

思路:

只要有x在a*x*x+b*x+c==0这个式子成立,那么x一定是整数,即:


p   q    p->q

0   0      1
0   1      1
1   0      0

1   1      1

#include<cstdio>
#include<cstring>
#include<algorithm>
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"); //当a=b=c=0时,所有的x都符合,所以是NO 
			else printf("YES\n");//c!=0,前式为0,无论后式为何值,结果都为0 
		}
		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;
}

猜你喜欢

转载自blog.csdn.net/islittlehappy/article/details/80185822
今日推荐