POJ2891 Strange Way to Express Integers 同余式的合并操作(中国剩余定理两两不一定非素的替代)


Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:

Choose k different positive integers a1a2…, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) can be used to express m.

“It is easy to calculate the pairs from m, ” said Elina. “But how can I find mfrom the pairs?”

Since Elina is new to programming, this problem is too difficult for her. Can you help her?

Input

The input contains multiple test cases. Each test cases consists of some lines.

  • Line 1: Contains the integer k.
  • Lines 2 ~ k + 1: Each contains a pair of integers airi (1 ≤ i ≤ k).

Output

Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.

Sample Input
2
8 7
11 9
Sample Output
31
Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.



注意中间乘法爆long long的情况,特别是中间 K变量。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<map>
#include<string>

using namespace std;


__int64 gcd(__int64 a, __int64 b)
{
	return b == 0 ? a : gcd(b, a%b);
}

__int64 extendgcd(__int64 a, __int64 b, __int64 &x, __int64 &y)
{

	if (b == 0)
	{
		x = 1;y = 0;
		return a;
	}

	__int64 ret = extendgcd(b, a%b, x, y);
	__int64 t = x;
	x = y;
	y = t - a / b*y;
	return ret;
}

__int64 ni(__int64 a, __int64 b)
{
	__int64 x;
	__int64 y;
	extendgcd(a, b, x, y);
	//逆元求解(a*a^(-1))%b=1,其中a^(-1)是a关于b的逆元
//a*a^(-1) = 1+b*k.这实际上就是拓展gcd方程
//实际上extendgcd算出来的x就是a关于b的逆元,而y是b关于a的逆元
	//x = (x%b+b)%b;
	x = x%b;
	return x;
}

__int64 n;

__int64 a;
__int64 r;
__int64 newr;
__int64 newa;

bool merge(__int64 &a1, __int64 &m1, __int64 a2, __int64 m2)
{
	__int64 d = gcd(m1, m2);

	//cout << "d" << d << endl;

	__int64 c = a2 - a1;
	
	//c = c < 0 ? -c : c;

	if (c%d != 0)//有解的条件
		return 0;
	/*
设两个方程X = a1 mod m1
	X = a2 mod m2
问题实际上是求解最小的X
转化成:a1+m1*k1 = a2+m2*k2
如果上面的方程有整数解,那么就存在这样的X(多个解,包括一个最小的X)
所以问题就转化成拓展GCD了,即(a1-a2)%gcd(m1,m2)==0时才有解
*/
	__int64 nnn = ni(m1 / d, m2 / d);//求解m1/关于m2/d的逆元

//主要的思想是将同余方程进行合并
//思想是将k1表示出来,然后带到x = a1 + k1*n1中就将a n更新
	__int64 rec = m1;
	m1 = m1 / d*m2;
	__int64 k = c / d*nnn;
	k %= m2/d;//k的范围注意
	a1 = a1+((rec%m1)*k)%m1;
	a1=(a1%m1+m1)%m1;

	return 1;
}


int main()
{
	while (scanf("%lld", &n) != EOF)
	{
		scanf("%lld %lld", &r, &a);//先收入一组数据
		bool flag = 1;
		if (r <= a)
			a = a%r;
			//flag = 0;
		for (int i = 1;i < n;i++)
		{
			scanf("%lld %lld", &newr, &newa);
			if (newr <= newa)//防止题目的余数可能比模大
				newa=newa%newr;
			if (flag == 1)
				 flag = merge(a, r, newa, newr);//用过求解线性同余方程组合
//并余数和模

			//	cout << i <<" "<<a<<" "<<r<<endl;
		}

	//	cout << a << r << endl;
		if (flag == 1)
			printf("%lld\n", (a%r + r) % r);
		else
			printf("-1\n");
	}
	

	return 0;
}






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章