虽然我不懂...
#include <cstdio>
#include <cstring>
#include <map>
#include <cmath>
using namespace std;
#define LL __int64
LL p[],o[];
LL x,y;
LL ext_eulid(LL a,LL b)
{
LL t,d;
if(b == )
{
x = ;
y = ;
return a;
}
d = ext_eulid(b,a%b);
t = x;
x = y;
y = t - (a/b)*y;
return d;
}
LL gcd(LL a,LL b)
{
return b == ?a:gcd(b,a%b);
}
int main()
{
int i,n,z;
LL p1,o1,d,c,mod;
while(scanf("%d",&n)!=EOF)
{
for(i = ;i < n;i ++)
{
scanf("%I64d%I64d",&p[i],&o[i]);
}
p1 = p[];
o1 = o[];
z = ;
for(i = ;i < n&&z;i ++)
{
d = ext_eulid(p1,p[i]);
c = o[i] - o1;
if(c%d) z = ;
mod = p[i]/d;
x = ((c/d*x)%mod+mod)%mod;
o1 = p1*x + o1;
p1 = p1*mod;
}
if(!z)
printf("-1\n");
else
printf("%I64d\n",o1);
}
return ;
}