2891

/*
	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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值