Description
With given integers a,b,c, you are asked to judge whether the following statement is true: "For any x, if a⋅+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
题意:判断命题p→q是否为真。p:a⋅+b⋅x+c=0有解,q:为整数
思路:p q p→q
0 0 1
0 1 1
1 0 0
1 1 1
AC代码:
//F
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
int main()
{
int T,a,b,c;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&a,&b,&c);
if(a==0){
if(b==0){
if(c==0) //a=b=c=0;
printf("NO\n");
else //a=b=0;c!=0;
printf("YES\n");
}
else{
if(c==0) //a=0,b!=0,c=0;
printf("YES\n");
else if(abs(c)%abs(b)==0) //a=0,b!=0,c!=0,且有整数解
printf("YES\n");
else //a=0,b!=0,c!=0,且无整数解
printf("NO\n");
}
}
else{
/* int drt = b*b - 4*a*c;
if(drt < 0) printf("YES\n");
else{
int leap=0;
for(int i=-100;i<=100;++i){
if((2*a*i+b)*(2*a*i+b)==drt){ //这个写法错在了我以为只要有一个整数解就可以
printf("YES\n"); //但是可能存在一个整数解一个非整数解的情况
leap=1;
break;
}
}
if(leap==0) printf("NO\n");
}*/
if(b*b-4*a*c>=0){
int ans=0;
for(int i=-100;i<=100;i++){
if(a*i*i+b*i+c==0) ans++;
}
if(ans==1&&b*b-4*a*c==0) printf("YES\n");
else if(ans==2&&b*b-4*a*c>0) printf("YES\n");
else printf("NO\n");
}
else printf("YES\n");
}
}
return 0;
}