RT
照着以前的板子打的
就是过不去
#include<bits/stdc++.h>
using namespace std;
#define int long long
int a[100010],b[100010];
void exgcd(int a,int b,int &x,int &y,int &gcd){
if(!b){
x=1;y=0;gcd=a;
return;
}
exgcd(b,a%b,y,x,gcd);
y-=a/b*x;
}
int mul(int x,int y,int mod){
x=(x%mod+mod)%mod;
y=(y%mod+mod)%mod;
int s=0;
while(y){
if(y&1)s=(s+x)%mod;
x<<=1;x%=mod;y>>=1;
}
return s;
}
signed main(){
int n;
scanf("%lld",&n);
for(int i=1;i<=n;++i)
scanf("%lld%lld",&a[i],&b[i]);
int lcm=a[1],ans=b[1];
for(int i=2;i<=n;++i){
int mod=a[i];
int c=(b[i]-ans%mod+mod)%mod;
int x,y,gcd;
exgcd(lcm,mod,x,y,gcd);
if(c%gcd!=0){
printf("No Solution");return 0;
}
x=mul(x,c/gcd,mod/gcd);
int new_lcm=lcm*mod/gcd;
ans+=mul(x,lcm,new_lcm);
lcm=new_lcm;
ans=(ans%lcm+lcm)%lcm;
}
printf("%lld",ans);
return 0;
}