洛谷p4777 【模板】扩展中国剩余定理

被同机房早就1年前就学过的东西我现在才学,wtcl。设要求的数为\(x\)
设当前处理到第\(k\)个同余式,设\(M = LCM ^ {k - 1} _ {i - 1}\)
\(k - 1\)个的通解就是\(x + i * M\)
那么其实第\(k\)个来说,
其实就是求一个\(y\)
使得\(x + y * M ≡ a_k(mod b_k)\)
转化一下就是\(y * M ≡ (a_k - x)(mod b_k)\)
这样\(y\)我们可以用\(exgcd\)求出来。
若有解,
那么前\(k\)个同余式的解就是\(x_k = x_{k - 1} + y * M\)
其实就是求\(k\)次扩展欧几里得。。

#include <bits/stdc++.h>

typedef long long ll;

const int maxn = 100010;

template<class t> inline void read(t& res) {
    res = 0;  char ch = getchar();  bool neg = 0;  
    while(!isdigit(ch))
        neg |= ch == '-', ch = getchar();
    while(isdigit(ch))
        res = (res << 1) + (res << 3) + (ch & 15), ch = getchar();
    if(neg)
        res = -res;
}

ll n;
ll a[maxn], b[maxn];

inline ll mul(ll a,ll b,ll mod) {
    ll res = 0;
    while(b) {
        if(b & 1)
            res = (res + a) % mod;
        a = (a + a) % mod;
        b >>= 1;            
    }
    return res;
}
ll exgcd(ll a,ll b,ll& x,ll& y) {
    if(!b) {
        x = 1;  y = 0;
        return a;
    }
    ll res = exgcd(b,a % b,x,y);
    ll z = x;  x = y;  y = z - a / b * y;
    return res;   
}
inline ll excrt() {
    ll M = b[1], res = a[1], x, y;
    for(int i = 2;i <= n;i++) {
        ll A = M, B = b[i], C = (a[i] - res % B + B) % B;
        ll D = exgcd(A,B,x,y), E = B / D;
        x = mul(x,C / D,E);
        res += x * M;  
        M *= E;  
        res = (res % M + M) % M;  
    }
    return (res % M + M) % M;  
}

int main() {
    scanf("%lld",&n);
    for(int i = 1;i <= n;i++)
        scanf("%lld %lld",b + i,a + i);
    printf("%lld\n",excrt());
    return 0;  
}

转载于:https://www.cnblogs.com/Sai0511/p/11233760.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值