https://www.luogu.org/problem/P1495
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#define LL long long
using namespace std;
const int MN=15;
LL d[MN],cycle[MN],ans;
int n;
LL exgcd(LL a,LL b,LL &x,LL &y) {
if(b==0) {
x=1;y=0;
return a;
}
LL gcd=exgcd(b,a%b,x,y);
LL x2=x,y2=y;
x=y2; y=x2-(a/b)*y2;
return gcd;
}
//中国剩余定理 a[]存放余数 w[]存放两两互质的数
LL Chinese_Remainder(LL a[],LL w[],LL len) {
LL x,y,ret=0,m=1;
for(LL i=1;i<=len;i++) m*=w[i];
for(LL i=1;i<=len;i++){
LL mi=m/w[i];
exgcd(w[i],mi,x,y);
ret=(ret+y*mi*a[i])%m;
}
return (m+ret%m)%m;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lld%lld",&cycle[i],&d[i]);
ans=Chinese_Remainder(d,cycle,n);
printf("%lld\n",ans);
return 0;
}