sgu140: Integer Sequences

一道扩展欧几里得的好题。
由于有对 P 取模,于是可以表示为

A1X1+A2X2+A3X3+...+AnXn+PQ=BQZ

对于 A1X1+A2X2 ,我们可以求出 A1X1+A2X2=gcd(A1,A2) X1 X2 ,存入X数组中,再把 gcd(A1,A2) 当作新的元素,于是方程变为:
kgcd(A1,A2)+A3X3+...+AnXn+PQ=B ,我们再对 gcd(A1,A2),A3 同样进行上述操作,求出 k X3,把之前求出的所有的 Xi 乘上 k ,再将X3加入 X 数组中,不断重复,直到算到An为止。
再把 gcd(A1,A2,A3...An) P 进行同样的操作,同时把所有解乘上B/gcd(A1,A2...An,P),如果除不尽则无解,否则输出。(记得取模!!!)

代码如下:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

inline int read()
{
    int x = 0, f = 1, t = getchar();
    while(t < '0' || t > '9') {
        if(t == '-') f = -1;
        t = getchar();
    }
    while(t >= '0' && t <= '9') {
        x = (x<<1) + (x<<3) + t - '0';
        t = getchar();
    }
    return x * f;
}

const int maxn = 105;

int n, P, B, A[maxn], x[maxn];

void gcd(int a, int b, int &d, int &x, int &y)
{
    if(b == 0) d = a, x = 1, y = 0;
    else gcd(b, a % b, d, y, x), y -= x*(a/b);
}
void init()
{
    n = read(), P = read(), B = read();
    for(int i = 1; i <= n; ++i)
        A[i] = read() % P; //remember '%' !
}
void work()
{
    register int i, j, a, b, GCD = A[1];
    x[1] = 1;
    for(i = 2; i <= n; ++i) {
        gcd(GCD, A[i], GCD, a, b);
        for(j = 1; j < i; ++j) x[j] = x[j] * a % P;
        x[i] = b;
    }
    gcd(GCD, P, GCD, a, b);
    if(B % GCD == 0) {
        puts("YES");
        for(i = 1; i <= n; ++i) {
            x[i] = x[i] * a * B / GCD % P;
            x[i] = (x[i] + P) % P;
        }
        for(i = 1; i <= n; ++i)
            printf("%d ", x[i]);
    }
    else puts("NO");
}
int main()
{
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif

    init();
    work();

    #ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
    #endif
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值