题目地址:http://www.sdutacm.org/onlinejudge2/index.php/Home/Index/problemdetail/pid/3898.html
quadratic equation
Time Limit: 2000MS Memory Limit: 131072KB
Problem 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
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
31 4 4 0 0 1 1 3 1
Example Output
YESYES NO
心得:
这题在赛场上费尽心机也没A掉,后悔不已。不禁痛恨,算不算大坑,其实是自己离散数学没学好
今天重见此题,终于解心头之恨
分析:
这是一个离散数学中的蕴含命题。对于任意的x,前提是方程有解x ,结果是所有的x都是整数。
只有当前提成立,但结果不成立的时候,这个命题才是假NO。其余情况全部为真YES
知道了这句话,还怕A不掉这题吗?关键是笨脑子想不到这是个离散数学里的蕴含式
代码:
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
int n;
scanf("%d",&n);
while(n--)
{
scanf("%d%d%d",&a,&b,&c);
int flag;
int d=b*b-4*a*c;
if(a==0)
{
if(b==0)
{
if(c==0)
flag=0;
else
flag=1;
}
else if(c%b==0)
flag=1;
else
flag=0;
}
else if(d<0)
flag=1;
else if(d==0)
{
if(b%(2*a)==0)
flag=1;
else
flag=0;
}
else
{
if((int)sqrt(d)==sqrt(d))
{
if((-b+(int)sqrt(d))%(2*a)==0&&(-b-(int)sqrt(d))%(2*a)==0)
flag=1;
else
flag=0;
}
else
flag=0;
}
if(flag)
puts("YES");
else
puts("NO");
}
return 0;
}