hdu5616 Jam's math problem

来源:互联网 发布:软件开发技术员招聘 编辑:程序博客网 时间:2024/06/05 17:53

Jam’s math problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 990 Accepted Submission(s): 474

Problem Description

Jam has a math problem. He just learned factorization.
He is trying to factorize ax2+bx+c into the form of pqx2+(qk+mp)x+km=(px+k)(qx+m).
He could only solve the problem in which p,q,m,k are positive numbers.
Please help him determine whether the expression could be factorized with p,q,m,k being postive.

Input

The first line is a number T, means there are T(1≤T≤100) cases

Each case has one line,the line has 3 numbers a,b,c(1≤a,b,c≤100000000)
分析:题意就化成多项式形式化成十字相乘的形式。直接想到两个for()枚举a,c的四个因子。只要到sqrt(a)就行了。

#include<iostream>#include<string>#include<cstring>#include<algorithm> #include<cmath>using namespace std;int main(){    long long a,b,c;    int t,flag;    scanf("%d",&t);    while(t--)    {        flag=0;        long long p,k;      scanf("%lld %lld %lld",&a,&b,&c);      for(long long i=1;i<=sqrt(a);i++)      {         if(a%i)         continue;for(long long j=1;j<=sqrt(c);j++)//枚举由两个因子乘的一个,这样快些        {            if(c%j)            continue;                p=a/i,k=c/j;                if((i*j+p*k==b)||(i*k+j*p==b))//有两个形式,别忘了                {                    printf("YES\n");                    flag=1;                    break;                }        }          if(flag)          break;      }      if(!flag)       printf("NO\n");    }    return 0;}
0 0