2 1 6 5 1 6 4
分解a、c因子,然后遍历。数据比较小,不会超时。
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> #include<set> #include<cmath> #include<vector> #define ll long long #define MAXN 1000000 struct node{ int m, k; }km[MAXN]; struct node1{ int p, q; }pq[MAXN]; int main(){ int t; int a, b, c, cnta, cntb, flag; scanf("%d", &t); while(t--){ scanf("%d%d%d",&a, &b, &c); cnta= 0; cntb = flag = 0; for(int i = 1; i <= (int)sqrt(a); i ++){ if(a%i==0) { pq[cnta].p = i; pq[cnta++].q = a/i; } } for(int i = 1; i <= (int)sqrt(c); i ++){ if(c%i==0){ km[cntb].k = i; km[cntb++].m = c/i; } } for(int i = 0; i < cnta; i++){ int p, q; p = pq[i].p; q = pq[i].q; for(int j = 0; j < cntb; j++){ int k, m; k = km[j].k; m = km[j].m; if(p*m+q*k==b||p*k+q*m==b){ printf("YES\n"); flag = 1; i = cnta; j = cntb; } } } if(flag==0) printf("NO\n"); } return 0; }
判别式法:x = (-b+cnt_1)/2*a,即(2*a*x+(b-cnt_1))(2*a*x+(b+cnt_1))==0
下面问题就是 b-cnt_1 是否大于 0 ? 因为cnt_1*cnt_1==b*b-4*a*c; a c不小于0 ,所以cnt_1 <= b 恒成立
.
#include <cstdio> #include <cstring> #include <queue> #include <vector> #include <algorithm> #include <set> #include <cmath> #include <iostream> #define MAXN 1000000+10 #define ll long long using namespace std; int main(){ ll t,a,b,c,cnt; scanf("%I64d",&t); while(t--){ scanf("%I64d%I64d%I64d",&a,&b,&c); cnt = b*b - 4*a*c; if(cnt<0) printf("NO\n"); //判别式小于0,方程无解 else if(cnt==0) printf("YES\n"); //判别式为0,方程有唯一解 else{ int flag = 0; ll cnt_1 = sqrt(cnt); if(cnt_1*cnt_1==cnt) flag = 1; //判别式可开方为整数 if(flag == 0) printf("NO\n"); else printf("YES\n"); } } return 0; }