您的位置:首页 > 其它

2017年ACM第八届山东省赛F题:quadratic equation(离散数学蕴含式)

2017-10-06 15:00 471 查看

F题:quadratic equation

时间限制: 2 秒 内存限制: 128 MB | 提交: 277 解决: 44

题目描述

With given integers a,b,c, you are asked to judge whether the following statement is true: "For any x, if a⋅x2+b⋅x+c=0, then x is an integer."

输入

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).

输出

or each test case, output “YES” if the
statement is true, or “NO” if not.

样例输入

3
1 4 4
0 0 1
1 3 1

样例输出

YES
YES
NO
题意:判断"For any x, if a⋅x2+b⋅x+c=0, then x is an integer."是否成立
   前件:For any x, if a⋅x2+b⋅x+c=0成立 (方程有解)
   后件:then x is an integer.成立 (解是整数)
   离散数学 p 为前件 q 为后件 的蕴含式 只有前件为 T 后件 为 F 时事件才为 F
#include <cstdio>
#include <cmath>

bool judge(int a , int b , int c){

if(a==0){
if(b==0){
if(c==0){
return false ; // 此时x可以取 任何值 不成立
}
return true ;
} else if(c%b==0){
return true ;
}
return false ; //
}
double d = b*b - 4 * a * c ;
int D = (int)sqrt(d) ;
if(d < 0){
return true ;
}

if(fabs(D - sqrt(d)) < 0.00000001){

if((0-b+D)%(2*a)==0 ){

if((0-b-D)%(2*a)==0)
return true ;
}

}

return false ;
}

int main(){
int t ;
int a , b , c ;
scanf("%d" , &t) ;
while(t--){
scanf("%d%d%d" , &a , &b , &c) ;

if(judge(a , b , c)){
printf("YES\n") ;
} else {
printf("NO\n") ;
}

}

return 0 ;
}
#include<stdio.h>
#include<math.h>
int main(void)
{
int T, a, b, c;
double x;
scanf("%d", &T);
while(T--)
{
scanf("%d%d%d", &a, &b, &c);
if(a==0 && b==0 && c==0)
printf("NO\n");
else if(a==0 && b==0)
printf("YES\n");
else if(a==0)
{
if(c%b==0)
printf("YES\n");
else
printf("NO\n");
}
else if(b*b-4*a*c<0)
printf("YES\n");
else
{
x = sqrt(1.0*b*b-4*a*c);
if((-b+x)/(2*a)-(int)(-b+x)/(2*a)==0 && (-b-x)/(2*a)-(int)(-b-x)/(2*a)==0)
printf("YES\n");
else
printf("NO\n");
}
}
return 0;
}

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: