题目
quadratic equation
Problem Description
With given integers a,b,c, you are asked to judge whether the following statement is true: "For any x, ifa⋅+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.
Example Input
3 1 4 4 0 0 1 1 3 1
Example Output
YES YES NO
题目要求我们来判断一句话是否为真,“对于任何x,如果a⋅+b⋅x+c=0,那么x是一个整数"
解题思路
看到这里我们基本上就可以判断的出来这是个离散数学中最基本的蕴含式,也就是大家所熟知的P->Q
F代表False,T代表True
所以我们可以把a⋅+b⋅x+c=0设为P,把x是一个整数设为Q
通过图片我们可以获知如果P为false,那么这句话一定为真,P为false的时候是a⋅+b⋅x+c=0无解的时候
如果P为true,那么只有Q也为true的时候这句话才为真,这种情况是等式有解且两个解都是整数的时候
这里有一个坑就是a,b,c都是零的时候是一个假命题,当时疏忽了倒是WA了好几次
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int T;
scanf("%d",&T);
int ans[20]={0,1,4,9,16,25,36,49,64,81,100,121};
while(T--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
int dt=b*b-4*a*c;
if(a==0)
{
if(b==0)
{
if(c==0)
printf("NO\n");
else
printf("YES\n");
}
else if(b!=0)
{
if(c%b==0)
printf("YES\n");
else
printf("NO\n");
}
}
else
{
if(dt>=0)//有解的时候
{
int flag=0;
int sq_dt;
for(int i=0;i<12;i++)
{
if(dt==ans[i])
{
flag=1;
sq_dt=i;
break;
}
}
if(flag)
{
if(((-b+sq_dt)%(2*a)==0)&&((-b-sq_dt)%(2*a)==0))
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
else
{
printf("NO\n");
}
}
else//无解的时候
{
printf("YES\n");
}
}
}
return 0;
}