方法一:由十字相乘相关理论我们能知道,如果要有p,k,q,m,那么首先要有解,所以b*b-4*a*c要>0,然而因为p,k,q,m是正整数,所以代表x1,x2都是有理数,有理数是什么鬼呢?就是解不带根号,我们知道有求根公式,其中2*a,-b都保证是自然数了,如果根号下b*b-4*a*c也保证是有理数我们就就能保证解是自然数,那么如何保证根号下b*b-4*a*c是有理数呢?那么b*b-4*a*c就是平方数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 #include<cmath> 6 #include<math.h> 7 #include<algorithm> 8 #include<queue> 9 #include<set> 10 #include<bitset> 11 #include<map> 12 #include<vector> 13 #include<stdlib.h> 14 using namespace std; 15 #define ll long long 16 #define eps 1e-10 17 #define MOD 1000000007 18 #define N 1000000 19 #define inf 1e12 20 ll a,b,c; 21 int main() 22 { 23 int t; 24 scanf("%d",&t); 25 while(t--){ 26 scanf("%I64d%I64d%I64d",&a,&b,&c); 27 double bet = b*b-4*a*c; 28 if(bet>eps && (int)(sqrt(bet))==sqrt(bet)){ 29 printf("YES "); 30 }else{ 31 printf("NO "); 32 } 33 } 34 return 0; 35 }
方法二:暴力枚举数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<stdio.h> 3 #include<math.h> 4 using namespace std; 5 6 int a,b,c; 7 int p[20000],cnt1;//p 8 int k[20000],cnt2;//k 9 10 bool f(){ 11 cnt1=cnt2=0; 12 13 int sqrt1=(int)sqrt(a); 14 for(int i=1;i<=sqrt1;++i){ 15 if(a%i==0){ 16 p[cnt1++]=i; 17 p[cnt1++]=a/i; 18 } 19 } 20 int sqrt2=(int)sqrt(c); 21 for(int i=1;i<=sqrt2;++i){ 22 if(c%i==0){ 23 k[cnt2++]=i; 24 k[cnt2++]=c/i; 25 } 26 } 27 28 int q,m; 29 for(int i=0;i<cnt1;++i){ 30 for(int j=0;j<cnt2;++j){ 31 q=a/p[i]; 32 m=c/k[j]; 33 if( q*k[j]+m*p[i]==b ){ 34 return true; 35 } 36 } 37 } 38 return false; 39 } 40 41 int main(){ 42 43 int T; 44 45 scanf("%d",&T); 46 47 while(T--){ 48 scanf("%d%d%d",&a,&b,&c); 49 50 if(f()){ 51 printf("YES "); 52 } 53 else{ 54 printf("NO "); 55 } 56 } 57 58 return 0; 59 }