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;
}






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