Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

0msAC

Posted by yuzhihao at 2019-08-08 15:42:18 on Problem 2891
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator