记录编号 |
363135 |
评测结果 |
AAAAAAAAAA |
题目名称 |
丧心病狂的韩信大点兵 |
最终得分 |
100 |
用户昵称 |
_Itachi |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.002 s |
提交时间 |
2017-01-10 10:35:01 |
内存使用 |
0.29 MiB |
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define LL long long
LL Rabit_mul(LL a,LL b,LL c){
return (a*b-(LL)(a/(long double)c*b+1e-3)*c+c)%c;
}
LL a1,a2,b1,b2;
LL Rabit_gcd(LL a,LL b){return b?Rabit_gcd(b,a%b):a;}
void Rabit_gcd(LL a,LL b,LL &x,LL &y){
if(!b){x=1,y=0;return;}
Rabit_gcd(b,a%b,y,x),y-=x*(a/b);
}
bool Rabit_run(){
LL g=Rabit_gcd(a1,a2),c=b2-b1;
if(c%g)return false;
a1/=g,a2/=g,c/=g;
LL k,tmp,lcm,x;Rabit_gcd(a1,a2,k,tmp);
k=(k%a2+a2)%a2;
k=((k*1ll*c)%a2+a2)%a2;
lcm=a1*a2*g;
x=Rabit_mul(k,a1*g,lcm);
x=(x+b1)%lcm;
a1=lcm,b1=x;
return true;
}
int main(){
freopen("weakhanxin.in","r",stdin);freopen("weakhanxin.out","w",stdout);
int n;scanf("%d",&n);
scanf("%lld%lld",&a1,&b1);
for(int i=1;i<n;i++){
scanf("%lld%lld",&a2,&b2);
if(!Rabit_run()){puts("-1");return 0;}
}
printf("%lld\n",b1);
}