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因子,然后遍历。数据比较小,不会超时。
方法二:
判别式法: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 恒成立
.
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; }
相关文章推荐
- 微软Word 2007数学插件 Microsoft Math 提供下载
- 关于C# Math 处理奇进偶不进的实现代码
- JavaScript中的Math.SQRT1_2属性使用简介
- JavaScript Math.ceil() 函数使用介绍
- 与Math.pow 相反的函数使用介绍
- JavaScript中使用指数方法Math.exp()的简介
- JavaScript使用Math.Min返回两个数中较小数的方法
- Ajax获取页面被缓存的解决方法
- JavaScript高级程序设计 阅读笔记(十二) js内置对象Math
- JavaScript中Math对象使用说明
- javascript对象之内置对象Math使用方法
- 基于java math API 的详细解释说明
- 简介JavaScript中用于处理正切的Math.tan()方法
- 浅谈JavaScript中的Math.atan()方法的使用
- PHP内置的Math函数效率测试
- 在JavaScript中使用对数Math.log()方法的教程
- JavaScript中用于四舍五入的Math.round()方法讲解
- 简介JavaScript中Math.LOG10E属性的使用
- 在javascript中随机数 math random如何生成指定范围数值的随机数
- 在PHP模板引擎smarty生成随机数的方法和math函数详解