1 条题解

  • 0
    @ 2025-10-8 16:49:47

    G20 扩展中国剩余定理

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    void exgcd(LL a, LL b, LL &d, LL &x, LL &y) 
    {
        if(b == 0) d = a, x = 1, y = 0;
        else
        {
            exgcd(b, a % b, d, y, x);
            y -= (a / b) * x;
        }
    }
    int main()
    {
        LL a1, b1, a2, b2, A, B, X, Y, K, d;
        int n; scanf("%d", &n);
        bool bk = true;
        scanf("%lld%lld", &a1, &b1);
        for(int i = 2; i <= n; i++) 
        {
            scanf("%lld%lld", &a2, &b2);
            A = a1; B = a2; K = b2 - b1;
            exgcd(A, B, d, X, Y);
            if(K % d != 0) bk = false;
            LL dx = abs(B / d), dy = abs(A / d);
            X = X * (K / d);
            X = (X % dx + dx) % dx;
            b1 = a1 * X + b1;
            a1 = a1 / d * a2;
        }
        if(bk == false) printf("no solution!\n");
        else printf("%lld\n", b1);
        return 0;
    }
    

    中国剩余定理 求同余方程组

    • 1

    G20*【模板】扩展中国剩余定理(EXCRT)

    信息

    ID
    352
    时间
    1000ms
    内存
    128MiB
    难度
    7
    标签
    递交数
    306
    已通过
    61
    上传者