#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
long long exgcd(long long a,long long b,long long &x,long long &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
long long r=exgcd(b,a%b,x,y);
long long t=x;
x=y;
y=t-a/b*y;
return r;
}
int main()
{
int n;
long long x,y;
long long a1,a2,r1,r2;
while(scanf("%d",&n)!=EOF)
{
scanf("%lld%lld",&a1,&r1);
bool flag=0;
for(int i=1; i<n; i++)
{
scanf("%lld%lld",&a2,&r2);
if(flag)continue;
long long p=exgcd(a1,a2,x,y);
if((r1-r2)%p!=0)
{
flag=1;
continue;
}
long long q=a2/p;
long long z=((r2-r1)/p*x%a2+a2)%q;
r1=z*a1+r1;
a1=a1*a2/p;
}
if(flag)
{
cout<<-1<<endl;
}
else cout<<r1<<endl;
}
return 0;
}
中国剩余定理模版
最新推荐文章于 2022-11-01 22:50:30 发布