您的位置:首页 > 产品设计 > UI/UE

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

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;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: