SGU 140. Integer Sequences 线性同余,数论 难度:2
2014-10-22 00:49
399 查看
140. Integer Sequences
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
A sequence A is called an integer sequence of length N if all its elements A1 A2 .. AN are non-negative integers less than 2 000 000 000. Consider two integer sequences of length N, A and X. The result of their multiplication (A*X) is an integer number R=A1*X1 + A2*X2 + .. + AN*XN. Your task is to solve the equation A*X=B (mod P), given the integer sequence A and the integer numbers B and P.
Input
The first line contains the integer numbers N (1<=N<=100) - the length of the integer sequences - P (1<=P<=10 000) and B (0<=B<=P-1). The second line contains the elements of the sequence A, separated by blanks: A1 A2 .. AN.
Output
You should print one line containing the word "YES" if there exists at least one integer sequence X which is a solution to the equation, or print "NO" otherwise. If the answer is "YES", the next line should contain N non-negative integers separated by blanks: X1 X2 .. XN.
Sample Input #1
Sample Output #1
Sample Input #2
Sample Output #2
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
A sequence A is called an integer sequence of length N if all its elements A1 A2 .. AN are non-negative integers less than 2 000 000 000. Consider two integer sequences of length N, A and X. The result of their multiplication (A*X) is an integer number R=A1*X1 + A2*X2 + .. + AN*XN. Your task is to solve the equation A*X=B (mod P), given the integer sequence A and the integer numbers B and P.
Input
The first line contains the integer numbers N (1<=N<=100) - the length of the integer sequences - P (1<=P<=10 000) and B (0<=B<=P-1). The second line contains the elements of the sequence A, separated by blanks: A1 A2 .. AN.
Output
You should print one line containing the word "YES" if there exists at least one integer sequence X which is a solution to the equation, or print "NO" otherwise. If the answer is "YES", the next line should contain N non-negative integers separated by blanks: X1 X2 .. XN.
Sample Input #1
2 7 4 7 3
Sample Output #1
YES 0 6
Sample Input #2
3 10 1 2 4 6
Sample Output #2
NO 线性同余方程,不断使前k个项余p得到最大公约数,同除去最大公约数,逆推即可
#include <cstdio> using namespace std; int extgcd(int a,int b,int& x,int& y){ if(a==0){ x=0;y=1; return b; } int t=extgcd(b%a,a,x,y); int temp=x; x=y-b/a*x; y=temp; return t; } int num[110],x[110],y[110]; int main(){ int cgcd,n,p,b; scanf("%d%d%d",&n,&p,&b); for(int i=0;i<n;i++){ scanf("%d",num+i); num[i]%=p; } cgcd=num[0]; for(int i=1;i<n;i++){ cgcd=extgcd(cgcd,num[i],x[i],y[i]); } cgcd=extgcd(cgcd,p,x ,y ); if(b%cgcd!=0)puts("NO"); else { puts("YES"); b/=cgcd; y[0]=1; for(int i=n-1;i>=0;i--){ while(x[i+1]<0)x[i+1]+=p; b*=x[i+1]; b%=p; while(y[i]<0)y[i]+=p; y[i]=y[i]*b%p; } for(int i=0;i<n;i++){ printf("%d%c",y[i],i==n-1?'\n':' '); } } return 0; }
相关文章推荐
- sgu 137. Funny Strings 线性同余,数论,构造 难度:3
- SGU 140 Integer Sequences 多元一次同余线性方程
- poj 1061 青蛙的约会 数论 线性同余
- SGU 141.Jumping Joe 数论,拓展欧几里得,二元不等式 难度:3
- POJ 1426 Find the Multiple 思路,线性同余,搜索 难度:2
- POJ 3087 Shuffle'm Up 线性同余,暴力 难度:2
- java语言实现:数论经典问题 除法表达 ,无平方因子数 ,直线上的点,同余与模算术 大整数取模 幂取模,模线性方程
- HDU 4686 Arc of Dream 矩阵快速幂,线性同余 难度:1
- luogu 1865 数论 线性素数筛法
- 线性同余生成随机数的一点思考
- sgu114. Telecasting station 难度:1
- SGU 124. Broken line 射线法 eps的精准运用,计算几何 难度:3
- SGU 119 数论
- SGU 132. Another Chocolate Maniac 状压dp 难度:1
- SGU193 Chinese Girls' Amusement(数论,约瑟夫环问题)
- (数论) SGU 105
- 【数论】【中国剩余定理】解线性模方程
- [数论]线性求所有逆元的方法
- poj 1061(线性同余)
- SGU 119 Magic Pairs(数论)