Poj 2891 Strange Way to Express Integers【crt】

本文介绍了一种利用中国剩余定理解决特定整数表达问题的方法。通过给出多个除数和余数组合,找到能够同时满足这些条件的最小非负整数。文章提供了一个C++实现示例,包括扩展欧几里得算法和中国剩余定理的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Strange Way to Express Integers
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 12923 Accepted: 4126

Description

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 m from 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.

Source


题意:

题目说的是,给出若干组除数和余数,求能确定满足所有组的最小的数,如果不存在,输出-1


题解:

中国剩余定理,不是特别懂,无解的情况是基于拓展欧几里得...

/*
http://blog.youkuaiyun.com/liuke19950717
*/

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const ll maxn=5005;
void extgcd(ll a,ll b,ll &d,ll &x,ll &y)
{
	if(!b)
	{
		d=a;x=1;y=0;
		return;
	}
	extgcd(b,a%b,d,y,x);
	y-=(a/b)*x;
}
ll crt(ll n,ll a[],ll r[])
{
	ll M=a[0],R=r[0],x,y,d;
	for(int i=1;i<n;++i)
	{
		extgcd(M,a[i],d,x,y);
		if((r[i]-R)%d)
		{
			return -1;
		}
		x=(r[i]-R)/d*x%(a[i]/d);
		R+=x*M;
		M=M/d*a[i];
		R%=M;
	}
	return R>0?R:R+M;
}
int main()
{
	ll n;
	while(~scanf("%lld",&n))
	{
		ll a[maxn]={0},r[maxn]={0};
		for(ll i=0;i<n;++i)
		{
			scanf("%lld%lld",&a[i],&r[i]);
		}
		printf("%lld\n",crt(n,a,r));
	}
	return 0;
} 




### POJ2891 Strange Way to Express Integers 的算法解析 此问题的核心在于通过扩展中国剩余定理来解决一组模线性同余方程组。当模数不一定两两互质时,可以通过逐步合并的方式解决问题。 #### 扩展中国剩余定理的应用 对于两个同余方程 \( N \equiv r_1 \ (\text{mod} \ m_1) \) 和 \( N \equiv r_2 \ (\text{mod} \ m_2) \),我们需要找到满足这两个条件的最小非负整数解。设 \( d = \gcd(m_1, m_2) \),则存在整数解的前提是 \( (r_2 - r_1) \% d == 0 \)[^4]。如果该条件成立,则可通过扩展欧几里得算法求出特解并进一步计算通解。 具体步骤如下: 1. **初始化参数** 设初始状态为 \( M = m_1 \) 和 \( R = r_1 \)。 2. **逐对处理每一对模数和余数** 对于当前的状态 \( M \) 和 \( R \),以及新的模数 \( m_i \) 和余数 \( r_i \),我们尝试将其合并成一个新的同余关系: \[ xM + ym_i = r_i - R \] 如果 \( (r_i - R) \% gcd(M, m_i) != 0 \),说明无解;否则继续下一步。 3. **利用扩展欧几里得算法求解系数** 使用扩展欧几里得算法求解上述不定方程的一组特解 \( x_0 \) 和 \( y_0 \)。然后调整这些系数使得最终的结果落在有效范围内。 4. **更新全局变量** 更新后的模数为 \( lcm(M, m_i) \),而对应的余数则是新算出来的值加上原来的偏移量。 5. **重复直到完成所有输入数据** 最后得到的 \( R \) 即为目标答案。 以下是基于 Python 实现的一个版本: ```python from math import gcd def ex_gcd(a, b): """扩展欧几里得算法""" if b == 0: return a, 1, 0 g, x, y = ex_gcd(b, a % b) return g, y, x - (a // b) * y def solve(): k = int(input()) mods = [] rems = [] for _ in range(k): mi, ri = map(int, input().split()) mods.append(mi) rems.append(ri) M = mods[0] R = rems[0] for i in range(1, len(mods)): Mi = mods[i] Ri = rems[i] g, p, q = ex_gcd(M, Mi) if (Ri - R) % g != 0: print(-1) # No solution exists. return tmp = ((Ri - R) // g) * p % (Mi // g) R += M * tmp M *= Mi // g while R < 0: R += M print(R % M) if __name__ == "__main__": T = int(input()) # Number of test cases results = [] for t in range(T): solve() ``` 以上程序实现了扩展中国剩余定理的方法,并能够正确处理多组测试样例的情况。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值