您的位置:首页 > 其它

HDU5615:Jam's math problem

2016-01-31 16:23 344 查看
[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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)
 

[align=left]Output[/align]
You should output the "YES" or "NO".
 

[align=left]Sample Input[/align]

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm HDU BastCoder math