最后一点一直WA,应该是越界(可能是其他的),但是感觉无法再处理了
#include <cstdio>
using namespace std;
#define ll long long
int n;
ll a[100005],b[100005];
ll exgcd(ll a,ll b,ll &x,ll &y){
if (b==0){
x=1;
y=0;
return a;
}
ll r=exgcd(b,a%b,x,y);
ll t=x;
x=y;
y=t-a/b*y;
return r;
}
ll exCRT(ll a[],ll m[],int n){
ll mm=m[1],aa=a[1],x,y,d;
for (int i=2;i<=n;i++){
d=exgcd(mm,m[i],x,y);
if ((aa-a[i])%d!=0){
return -1;
}
x=((aa-a[i])/d*x%(m[i]/d)+m[i]/d)%(m[i]/d);
aa-=x*mm;
mm=mm/d*m[i];
aa=(aa%mm+mm)%mm;
}
return aa;
}
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++){
scanf("%lld%lld",&a[i],&b[i]);
}
printf("%lld",exCRT(b,a,n));
return 0;
}