/*
m = a1*m1+r1
m = a2*m2+r2
m = a3*m3+r3
通过不断的合并方程求出最后的解,过程是这样的,以前两个方程为例
m === r1 mod a1
m === r2 mod a2
m = r1+a1*x
r1+a1*x === r2 mod a2
r1+a1*x-r2 = a2*y
a1*x-a2*y = r2-r1;
r2 - r1 - a1*x = a2*y
a1*x+a2*y = r2-r1
则可用扩展欧几里得算法求出x,y。则
m求出,不过这里只是对这两个方程而言,合并为新的方程,就有下式
m === (r1+a1*x) mod lcm(a1,a2)
*/
// include file
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <ctime>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <bitset>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <set>
#include <list>
#include <functional>
using namespace std;
// typedef
typedef __int64 LL;
//
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
#define Z(a,b) ((a)<<(b))
#define Y(a,b) ((a)>>(b))
const double eps = 1e-6;
const double INFf = 1e100;
const int INFi = 1000000000;
const LL INFll = (LL)1<<62;
const double Pi = acos(-1.0);
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T TMAX(T x,T y)
{
if(x>y) return x;
return y;
}
template<class T> inline T TMIN(T x,T y)
{
if(x<y) return x;
return y;
}
template<class T> inline T MMAX(T x,T y,T z)
{
return TMAX(TMAX(x,y),z);
}
template<class T> inline T MMIN(T x,T y,T z)
{
return TMIN(TMIN(x,y),z);
}
template<class T> inline void SWAP(T &x,T &y)
{
T t = x;
x = y;
y = t;
}
// code begin
LL N,a1,r1,a2,r2,m,a,b,c,d,x,y;
LL gcd(LL aa,LL bb)
{
if(bb==0) return aa;
return gcd(bb,aa%bb);
}
LL extGcd(LL aa,LL bb,LL &x,LL &y)
{
if(bb==0)
{
x = 1;
y = 0;
return aa;
}
LL r = extGcd(bb,aa%bb,x,y);
LL t = x;
x = y;
y = t-aa/bb*y;
return r;
}
int main()
{
read;
write;
while(scanf("%I64d",&N)!=-1)
{
scanf("%I64d %I64d",&a1,&r1);
int i;
for(i=1;i<N;i++)
{
scanf("%I64d %I64d",&a2,&r2);
a = a1;
b = a2;
c = r2-r1;
d = gcd(a,b);
if(c%d!=0)
{
printf("-1\n");
break;
}
else
{
a/=d;
b/=d;
c/=d;
extGcd(a,b,x,y);
x = (x*c)%a2;
if(x<0) x+=a2;
r1 = r1+a1*x;
a1 = (a1*a2)/d;
}
}
if(i<N)
{
for(i++;i<N;i++)
{
scanf("%I64d %I64d",&a2,&r2);
}
}
else
{
printf("%I64d\n",r1%a1);
}
}
return 0;
}
转载于:https://www.cnblogs.com/ac2012/archive/2011/04/30/2033426.html