Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
0msAC#include<stdio.h> #include<string.h> #define ll long long ll look(ll a,ll b, ll &x,ll &y) { if(!b) { x=1,y=0; return a; } ll t=look(b,a%b,x,y); ll k =x; x=y; y=k-(a/b)*y; return t; } int main() { ll n; while(~scanf("%lld",&n)) { ll ans=0; ll lcm=1; ll x; ll y; int flag=0; for(ll i=0;i<n;i++) { ll a,b; scanf("%lld%lld",&a,&b); ll d=look(lcm,a,x,y); if((b-ans)%d) { flag=1; } if(!flag) { x=x*((b-ans)/d); a/=d; x=(x%a+a)%a; ans+=x*lcm; lcm*=a; } } if(flag) { printf("-1\n"); } else { if(ans==0) printf("%lld\n",lcm); else printf("%lld\n",ans); } } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator