中国剩余定理和扩展中国剩余定理代码模版

中国剩余定理和扩展中国剩余定理原理证明:请看这里

自己手撸的,不保证完全正确,仅供参考。

中国剩余定理代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <algorithm>
#define INF 0x3f3f3f3f

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;
int n;
LL a[N], m[N];

LL exgcd(LL a, LL b, LL &x, LL &y)
{
    if(!b)  { x = 1; y = 0; return a; }

    LL d = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

LL CRT(LL a[], LL m[], int n)//a (mod m)
{
    LL M = 1, res = 0;
    for(int i = 1; i <= n; i ++)    M *= m[i];

    for(int i = 1; i <= n; i ++)
    {
        LL Mi = M / m[i], x, y; //x存储Mi的逆元
        LL d = exgcd(Mi, m[i], x, y);
        res = (res + a[i] * Mi * x) % M;
    }
    return (res + M) % M;
}

int main()
{
#ifdef LOCAL
    freopen("in.in", "r", stdin);
    freopen("out.out", "w", stdout);
#endif
    cin >> n;
    for(int i = 1; i <= n; i ++)
        cin >> a[i] >> m[i];
    
    cout << CRT(a, m, n) << endl;

    return 0;
}

扩展中国剩余定理代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <algorithm>
#define INF 0x3f3f3f3f

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

LL exgcd(LL a, LL b, LL &x, LL &y)
{
    if(!b) { x = 1, y = 0; return a; }

    LL d = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

LL EXCRT(LL a[], LL m[], int n)
{
    bool succes = true;
    LL x; //存放解
    for(int i = 2; i <= n; i ++) //将第2~n个方程都合并到第1个方程上面
    {
        LL k1, k2;
        LL d = exgcd(m[1], m[i], k1, k2); //第1个和第i个合并
        if((a[i]- a[1]) % d) //方程无解
        {
            succes = false;
            break;
        }

        k1 *= (a[i] - a[1]) / d; //这里求出来的k1并不是原来方程的k1,需要在乘一下
        //k1 + m[i]/d是k1的通解,在所有可以选择的k1中取一个最小的,防止溢出
        k1 = (k1 % (m[i] / d) + m[i] / d) % (m[i] / d);

        x = k1 * m[1] + a[1]; //存下来该组解

        //接下来将合并后的方程的a和m存放到a[i]和m[1]中.
        a[1] = k1 * m[1] + a[1];
        m[1] = abs(m[1] / d * m[i]); //d可能是负数,所以加上abs
    }

    return succes ? (x % m[1] + m[1]) % m[1] : -1;
}

#include #include using namespace std; typedef int LL; typedef pair PLL; LL inv(LL t, LL p) {//求t关于p的逆元 if (t >= p) t = t%p; return t == 1 ? 1 : (p - p / t) * inv(p % t, p) % p; } LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); } PLL linear(LL A[], LL B[], LL M[], int n) {//求解A[i]x = B[i] (mod M[i]),总共n个线性方程组 LL x = 0, m = 1; for (int i = 0; i < n; i++) { LL a = A[i] * m, b = B[i] - A[i] * x, d =gcd(M[i], a); if (b % d != 0) return PLL(0, -1);//答案不存在,返回-1 LL t = b / d * inv(a / d, M[i] / d) % (M[i] / d); x = x + m*t; m *= M[i] / d; } x = (x % m + m) % m; return PLL(x, m);//返回的x就是答案,m是最后的lcm值 } int main() { int n; scanf_s("%d", &n); LL a[2017], b[2017], m[2017]; for (int i = 0; i<n; i++) { scanf_s("%d%d%d", &a[i], &b[i], &m[i]); } PLL pa = linear(a, b, m, n); printf("%lld\n", pa.first); } 设计思路: 有这样一道算术题:“今有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二,问物几何?” 解这题,先构造一个答案 5*7*inv(5*7, 3) % 3 = 1 3*7*inv(3*7, 5) % 5 = 1 3*5*inv(3*5, 7) % 7 = 1 然后两边同乘你需要的数 2 * 5*7*inv(5*7, 3) % 3 = 2 3 * 3*7*inv(3*7, 5) % 5 = 3 2 * 3*5*inv(3*5, 7) % 7 = 2 令 a = 2 * 5*7*inv(5*7, 3) b = 3 * 3*7*inv(3*7, 5) c = 2 * 3*5*inv(3*5, 7) 那么 a % 3 = 2 b % 5 = 3 c % 7 = 2 其实答案就是a+b+c 因为 a%5 = a%7 = 0 因为a是5的倍数,也是7的倍数 b%3 = b%7 = 0 因为b是3的倍数,也是7的倍数 c%3 = c%5 = 0 因为c是3的倍数,也是5的倍数 所以 (a + b + c) % 3 = (a % 3) + (b % 3) + (c % 3) = 2 + 0 + 0 = 2 (a + b + c) % 5 = (a % 5) + (b % 5) + (c % 5) = 0 + 3 + 0 = 3 (a + b + c) % 7 = (a % 7) + (b % 7) + (c % 7) = 0 + 0 + 2 = 2 答案a+b+c完全满足题意 但是答案,不只一个,有无穷个,每相隔105就是一个答案(105 = 3 * 5 * 7) a=2*5*7*2=140 b=3*3*7*1=63 c=2*3*5*1=30 140+63+30=233 2335 = 23 如果题目问你最小的那个答案,那就是23了。 当 1*x=2(%3) 1*x=3(%5) 1*x=2(%7) 输入: 3 1 2 3 1 3 5 1 2 7 输出: 23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值