hdu3892 Common Roots 最大取模公因式
2014-06-30 19:41
106 查看
We have many polynomials modulo p (p is a prime number). An interesting issue would be to determine whether they have some roots in common. Notice roots we mention here are integers in modulo p system (0 <= root < p). Moreover, if
the given polynomial is of order r, we will guarantee that it has r roots.
For example, we have
x^2 + 13x + 36 (mod 37)
x^3 + 14x^2 + 49x + 36 (mod 37)
If x = 33 or x = 28, both of them would give the value of 0. So 33 and 28 are the roots in common.
[align=left]Input[/align]
There are many test cases (less than1000).
In each case, the integer in the first line is n (the number of polynomials in this case). Then n lines followed. Each of them starts with an integer r (order of polynomials, r <= 50), and r + 1 integers (a(r), a(r-1) ,..., a(0)), which means the polynomial
goes like:
a(r) * x^r + a(r-1) * x^(r-1) + … +a(1) * x + a(0) (mod 999983).
To make it easier, p is set to be 999983, as you see.
[align=left]Output[/align]
For each case, just output “YES” if they have common roots, otherwise “NO” in a single line.
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
是否存在解,满足每个式子取模后都为0。
求出这些式子的公因式,如果最大系数大于0的话肯定有解。
the given polynomial is of order r, we will guarantee that it has r roots.
For example, we have
x^2 + 13x + 36 (mod 37)
x^3 + 14x^2 + 49x + 36 (mod 37)
If x = 33 or x = 28, both of them would give the value of 0. So 33 and 28 are the roots in common.
[align=left]Input[/align]
There are many test cases (less than1000).
In each case, the integer in the first line is n (the number of polynomials in this case). Then n lines followed. Each of them starts with an integer r (order of polynomials, r <= 50), and r + 1 integers (a(r), a(r-1) ,..., a(0)), which means the polynomial
goes like:
a(r) * x^r + a(r-1) * x^(r-1) + … +a(1) * x + a(0) (mod 999983).
To make it easier, p is set to be 999983, as you see.
[align=left]Output[/align]
For each case, just output “YES” if they have common roots, otherwise “NO” in a single line.
[align=left]Sample Input[/align]
2 2 1 13 36 3 1 14 49 36
[align=left]Sample Output[/align]
YES
是否存在解,满足每个式子取模后都为0。
求出这些式子的公因式,如果最大系数大于0的话肯定有解。
#include<iostream> #include<queue> #include<cstring> #include<cstdio> #include<cmath> #include<set> #include<map> #include<vector> #include<stack> #include<algorithm> #define eps 1e-9 #define MAXN 1010 #define MAXM 110 #define MOD 999983 typedef long long LL; using namespace std; LL N,M; vector<LL> ans,g[MAXN]; void gcd(LL a,LL b,LL& d,LL& x,LL& y){ if(!b){ d=a; x=1; y=0; } else{ gcd(b,a%b,d,y,x); y-=x*(a/b); } } LL inv(LL a,LL n){ LL d,x,y; gcd(a,n,d,x,y); return d==1?(x+n)%n:-1; } vector<LL> poly_gcd(vector<LL> a,vector<LL> b){ if(!b.size()) return a; int L=a.size()-b.size()+1,bs=b.size(),as=a.size(); for(LL i=0;i<L;i++){ LL t=(a[i]*inv(b[0]%MOD,MOD))%MOD; for(LL j=0;j<bs;j++) a[i+j]=(a[i+j]-b[j]*t%MOD+MOD)%MOD; } vector<LL> c; LL flag=0; for(LL i=0;i<as;i++){ if(a[i]){ flag=1; for(LL j=i;j<as;j++) c.push_back(a[j]); } if(flag) break; } return poly_gcd(b,c); } int main(){ freopen("in.txt","r",stdin); LL cas=0; while(scanf("%I64d",&N)!=EOF){ for(LL i=0;i<N;i++) g[i].clear(); for(LL i=0;i<N;i++){ scanf("%I64d",&M); LL t; for(LL j=0;j<M+1;j++){ scanf("%I64d",&t); g[i].push_back(t); } } if(N==1) ans=g[0]; else{ ans=poly_gcd(g[0],g[1]); for(LL i=2;i<N;i++) ans=poly_gcd(ans,g[i]); } if(ans.size()>1) puts("YES"); else puts("NO"); } return 0; }
相关文章推荐
- HDU3892 Common Roots 多项式欧几里德求最大公共多项式
- Guru of the Week 条款16:具有最大可复用性的通用Containers
- 网络流最大流最小割算法
- HDU 1257 (最大上升子序列)
- ubuntu13.10解决开机自动调节为最大亮度问题
- mysql 自定义函数 计算 月份的最大天数
- 【算法学习笔记】19.算法设计初步 最大子列和问题的三种方法
- poj 1032 拆分数字使乘积最大
- D. 连续最大积吗 为什么要留代码呢 因为我缝缝补补的就过了 也不知道咋回事
- 算法设计例题:最大团(回溯、分枝限界)
- hdu 3416 Marriage Match IV 【图论-网络流-最短路+最大流(spfa + Dinic)】
- 最大熵模型
- windows远程桌面如果超出最大连接数, 使用命令行mstsc /console登录即可
- 求三个数中最大数和最小数的差值
- 算法入门--堆排序(最大堆,从小到大排序)
- 求最小公倍数与最大公约数的函数
- 矩阵最大值
- [编程之美] PSet2.7 最大公约数问题
- (hdu step 4.1.8)猜数字(求n次所能猜到的最大数字)
- HDU 5137 How Many Maos Does the Guanxi Worth (最短路径最大值)