点击这里查看原题
这里是教程
/*
User:Small
Language:C++
Problem No.:2891
*/
#include<stdio.h>
#include<iostream>
#include<iomanip>
#include<string.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
#define ll long long
#define inf 999999999
using namespace std;
const int M=1005;
ll n,c[M],m[M];
ll gcd(ll a,ll b){
return b==0?a:gcd(b,a%b);
}
void exgcd(ll a,ll b,ll &x,ll &y){
if(b==0){
x=1;
y=0;
return;
}
exgcd(b,a%b,y,x);
y-=a/b*x;
}
ll inv(ll a,ll b){
ll x,y;
exgcd(a,b,x,y);
x=(x%b+b)%b;
if(!x) x+=b;
return x;
}
int main(){
freopen("data.in","r",stdin);//
ios::sync_with_stdio(false);
while(cin>>n){
bool flag=0;
for(int i=1;i<=n;i++) cin>>m[i]>>c[i];
for(int i=2;i<=n;i++){
ll m1=m[i-1],m2=m[i],c1=c[i-1],c2=c[i];
ll t=gcd(m1,m2);
if((c2-c1)%t){
flag=1;
break;
}
m[i]=m1/t*m2;
c[i]=inv(m1/t,m2/t)*((c2-c1)/t)%(m2/t)*m1+c1;
c[i]=(c[i]%m[i]+m[i])%m[i];
}
if(flag) c[n]=-1;
cout<<c[n]<<endl;
}
return 0;
}