题目:
题解:
同余方程
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
ll gcd;
gcd=exgcd(b,a%b,y,x);
y-=a/b*x;
return gcd;
}
int main()
{
ll n;
cin>>n;
ll a1,m1;
cin>>a1>>m1;
for(int i=1;i<n;i++)
{
ll a2,m2;
cin>>a2>>m2;
ll k1,k2;
ll d=exgcd(a1,a2,k1,k2);
if((m2-m1)%d)
{
cout<<"-1"<<endl;
return 0;
}
k1*=(m2-m1)/d;
ll t=abs(a2/d);
k1=(k1%t+t)%t;
m1=a1*k1+m1;
a1=abs(a1*a2/d);
}
cout<<(m1%a1+a1)%a1<<endl;
return 0;
}