quadratic equation (余数)

quadratic equation

Time Limit: 2000 ms Memory Limit: 131072 KiB
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

1.这个题目就是要清楚,一个蕴含表达式只有前真后假的情况,才会使得整个表达式取得假值,就算前件为假,表达式也是真的。

2.要学会求解x为整数的时候,用取余的方法。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;


int check(int a,int b,int c)
{
if(a==0)
{
if(b==0)
{
if(c==0)// 此时x可以取任何值不成立,可以取任意值,就可以说明可以取到非整数 
{
return 0;
}
return 1;//此时存在一个整数X
}
else if(c%b==0) return 1;//b*x=c;x=c/b;因为题目中要求x为整数,也就是说,(-c)%b==0
return 0;
}
int d=b*b-4*a*c;
int D=(int)sqrt(d);
if(d<0)  return 1;  //因为这是蕴含表达式,所以就算前件不成立,整个表达式确是真的 
    if(fabs(D - sqrt(d)) < 0.00000001)
    {
    if(((-1)*b-D)%(2*a)==0)
       if(((-1)*b+D)%(2*a)==0)
      return 1;
}
return 0;


}
int main()
{
int t;
int a,b,c;
cin>>t;
while(t--)
{
cin>>a>>b>>c;
if(check(a,b,c))
 cout<<"YES"<<endl;
else
 cout<<"NO"<<endl;
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值