sgu 140 分类: sgu 2015-03...

解模意义下的方程

可以视为解不定方程
A1x1+A2x2+A3x3++Anxn+Pxn+1=B

用欧几里得扩展算法

二元情形:
a>b

1,显然当 b=0 时,gcd(ab)=a 。此时 x=1y=0
2,a , b <> 0 时,

ax1+by1=gcd(a,b)
bx2+(a mod b)y2=gcd(b,a mod b)

根据朴素的欧几里德原理有 gcd(a,b)=gcd(b,amodb)
则:ax1+by1=bx2+(a mod b)y2
即:ax1+by1=bx2+(a[a/b]b)y2=ay2+bx2[a/b]by2

也就是ax1+by1==ay2+b(x2[a/b]y2)

根据恒等定理得:x1=y2y1=x2[a/b]y2;
这样我们就得到了求解 x1,y1 的方法:x1y1 的值基于 x2y2.

多元情形:

gcd(a1,a2,ai1)x+aiy=gcd(a1,a2,ai1,ai)
可以用二元方法解出x,y
原方程中Xi=y,Xj=Xjx,j<i

最后要注意把X都变成非负整数



#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<iostream>
#include<algorithm>

const int MAXN = 105;

int ax[MAXN] = {0};
int A[MAXN] = {0};
int N,P,B;

int super_gcd(int a,int b,int &x,int &y)
{
    if(!b)
    {
        x = 1;
        y = 0;
        return a;
    }
    else
    {
        int ret = super_gcd(b,a%b,x,y);

        int tmp = x;
        x = y;
        y = tmp - a/b*y;

        return ret;
    }   
}


int main()
{
    int gcd;

#ifndef ONLINE_JUDGE    
    freopen("sgu140.in","r",stdin);
    freopen("sgu140.out","w",stdout);
#endif

    scanf("%d%d%d",&N,&P,&B);

    for(int i = 1; i <= N; i++)
      {scanf("%d",&A[i]);A[i] %= P;}

    ax[1] = 1 , gcd = A[1]; A[N+1] = P;
     for(int i = 2; i <= N+1; i++)
    {
        int x , y;

        gcd = super_gcd(gcd , A[i] , x , y);

        ax[i] = y;
        for(int j = 1 ; j < i; j++)
          ax[j] *= x,ax[j] %= P;
    }

    if(B%gcd)
      puts("NO");
    else
    {   
        int tmp = B/gcd;

        for(int i = 1; i <= N; i++)
          {ax[i] *= tmp; ax[i] = (ax[i]%P + P)%P;}

        puts("YES");
        for(int i = 1; i <= N; i++) 
           printf("%d ",ax[i]);
    }


#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif      
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/dashgua/p/4723078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值