poj 2891 Strange Way to Express Integers 扩展CRT

本文介绍了解决一组同余方程的方法,特别是在余数不互质的情况下的处理技巧,并通过具体的数学步骤展示了如何找到满足特定条件的最小非负整数。

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

http://www.elijahqi.win/archives/3177
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 a1, a2, …, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1, a2, …, ak are properly chosen, m can be determined, then the pairs (ai, ri) 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 ai, ri (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

POJ Monthly–2006.07.30, Static
那么考虑当两个余数不互质的时候 我们应该如何处理
假设他们分别是m1 c1 m2 c2
那么转换下形式
x=k1m1+c1 x = k 1 ∗ m 1 + c 1
x=k2m2+c2 x = k 2 ∗ m 2 + c 2
满足翡蜀定理 即 gcd(m1,m2)|(c1c2) g c d ( m 1 , m 2 ) | ( c 1 − c 2 ) 这种情况下才有解
k1m1=k2m2c1+c2 k 1 ∗ m 1 = k 2 ∗ m 2 − c 1 + c 2
两个同除gcd(m1,m2)
m1(m1,m2)k1=k2m2(m1,m2)+c2c1(m1,m2) m 1 ( m 1 , m 2 ) k 1 = k 2 m 2 ( m 1 , m 2 ) + c 2 − c 1 ( m 1 , m 2 )
m1(m1,m2)k1c2c1(m1,m2)(modm2(m1,m2)) m 1 ( m 1 , m 2 ) k 1 ≡ c 2 − c 1 ( m 1 , m 2 ) ( m o d m 2 ( m 1 , m 2 ) )
考虑将左边的系数除到右边
k1inv(m1(m1,m2),m2(m1,m2))×(c2c1(m1,m2))(modm2(m1,m2)) k 1 ≡ i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) × ( c 2 − c 1 ( m 1 , m 2 ) ) ( m o d m 2 ( m 1 , m 2 ) )
再将原来的系数还原回来
k1=inv(m1(m1,m2),m2(m1,m2))×(c2c1(m1,m2))+(y×m2(m1,m2)) k 1 = i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) × ( c 2 − c 1 ( m 1 , m 2 ) ) + ( y × m 2 ( m 1 , m 2 ) )
然后再将k1带回原式可以发现
xinv(m1(m1,m2),m2(m1,m2))×(c2c1(m1,m2))×m1+c1(modm1×m2(m1,m2)) x ≡ i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) × ( c 2 − c 1 ( m 1 , m 2 ) ) × m 1 + c 1 ( m o d m 1 × m 2 ( m 1 , m 2 ) )
然后这题就搞定了 那么本题只需要直接exgcd求出的即是我想要的
inv(m1(m1,m2)) i n v ( m 1 ( m 1 , m 2 ) )

#include<cstdio>
#include<cctype>
#include<algorithm>
#define ll long long
using namespace std;
inline ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)) {if (ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
    return x*f; 
}
inline ll exgcd(ll a,ll b,ll &x,ll &y){
    if (!b) {x=1;y=0;return a;} ll g=exgcd(b,a%b,x,y);
    ll t=x;x=y;y=t-a/b*y;return g;
}
ll k;
int main(){
    freopen("poj2891.in","r",stdin);
    while(~scanf("%lld",&k)){static ll m1,c1,m2,c2;
        m1=read();c1=read();bool flag=0;
        for (int i=2;i<=k;++i){
            m2=read();c2=read();ll t1,t2;if (flag) continue;
            ll g=exgcd(m1,m2,t1,t2);
            if ((c2-c1)%g) {flag=1;}
            t1=t1*(c2-c1)/g;m2/=g;t1=(t1%m2+m2)%m2;
            c1+=t1*m1;m1*=m2;c1%=m1;
        }
        if(flag) {puts("-1");continue;}
        printf("%lld\n",c1);
    }
    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、付费专栏及课程。

余额充值