被同机房早就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
",excrt());
return 0;
}