1年前に長い同じ部屋を研究してきたことは、どのような私は今奨学金wtclを持っていました。リセット要求の数\(X \) 。
第二に、現在のプロセスを設定する\(K \)に設けられ、合同式番目( - _ {1。K} {I - 1} MはLCM ^を= \)\、
前\(k - 1 \)一般解がすることである(\私はX + * M \を)。
だから、実際には、最初の\(k個\)数ヶ月、それが
実際に求めている\(のy \)に
なるように\(X + Yの* M≡
a_k(MOD b_k)\) であるものを変換する\(Y * Mは≡は(a_k - X )(モッズ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;
}