Integer Sequences SGU - 140


Integer Sequences SGU - 140


A sequence A is called an integer sequence of length N if all its elements A1 A2 .. AN are non-negative integers less than2 000 000 000. Consider two integer sequences of length N, A and X. The result of their multiplication (A*X) is an integer numberR=A1*X1 + A2*X2 + .. + AN*XN. Your task is to solve the equation A*X=B (mod P), given the integer sequenceA 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 sequenceA, separated by blanks: A1 A2 .. AN.

Output

You should print one line containing the word "YES" if there exists at least one integer sequenceX 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



  题意:  求多元线性同余方程。 给定n 个数a[i] ,求出 满足(a[i]*x[i]+...a[n]*x[n])== b%p 的x[i];


  思路:  扩欧求解,

  求 a[1]*x[1]+a[2]*x[2]+... +a[n]*x[n]==b%p;其实可以把 p看做 a[n+1]

原式变为  a[1]*x[1]+a[2]*x[2]+...+a[n]*x[n]+a[n+1]*x[n+1]= b  ;  

由扩欧 对于二元一次方程 ax+by=c 有 c% gcd(a,b)==0  有解,

那么对于这个题其实就是一个个 二元方程的组合,即只有当b%(gcd(a1,a2,a3,...,an+1)==0  才有整数解。

就是一个循环了,同时循环过程中我们可以得到 (xi,yi) ;

对于一个二元方程  (x1,y1) 表示满足 ax+by =c 的解,

那么对于这个题,每次我们得到的(Xi,Yi)  表示什么呢?不如用公式表示下来循环过程

a1*X1+a2*Y1= gcd(a1,a2) =g1;

g1*X2+a3*Y2= gcd(a1,a2,a3)=g2;

g2*X3+a4*Y3= gcd(a1,a2,a3,a4)=g3;

...

gn-1*Xn-1+an*Yn-1= gcd(a1...an)=gn;

gn*Xn+an+1*Yn= gcd(a1...an+1)=gn+1;


显然我们可以得到:

 ((a1*X1+a2*Y1)*X2+a3*Y2)*X3+a4*Y4)...

  即我们要求的xi其实变为了 X1*X2*...*Xn*Yn-1

   比如 xn=Xn*Yn-1*b/g;

        ......

        x1=Xn*Xn-1*Xn-2*Xn-3*...*X1*Y0*b/g;




#pragma comment(linker, "/STACK:1024000000,1024000000")
//#include <bits/stdc++.h>
#include<string>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<algorithm>
#define maxn 1001
#define INF 0x3f3f3f3f
#define eps 1e-8
#define MOD 1000000007
#define ll long long
using namespace std;

ll a[maxn];
int x[maxn],y[maxn];
int ans[maxn];
void ex_gcd(int a,int b,int &d,int &x,int &y)
{
    if(!b)
    {
        d=a;
        x=1;
        y=0;
        return ;
    }
    ex_gcd(b,a%b,d,y,x);
    y-=a/b*x;

}
int main()
{
    int n,p,b,g;
    scanf("%d%d%d",&n,&p,&b);
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        g=a[1];
        a[n+1]=p;
        for(int i=1;i<=n;i++)
        {
            ex_gcd(g,a[i+1],g,x[i],y[i]);
        }
        if(b%g!=0)
            printf("NO\n");
        else
        {
            printf("YES\n");
            y[0]=1;
            g=b/g;
            for(int i=n;i>=1;i--)
            {
                g=(g*x[i]%p+p)%p;
                  ans[i]=(g*y[i-1]%p+p)%p;
            }
            for(int i=1;i<=n;i++)
                printf("%d%c",ans[i],i==n?'\n':' ');
        }
    }
    return 0;
}











  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值