数论 线性同余方程的应用 poj2891
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 17321 | Accepted: 5828 |
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
解同于方程组:
其中模数不一定互质。
题解
若模数两两互质,我们可以用中国剩余定理来解。
这里我们先考虑两个方程:
我们可以写成:
相减得:y1a1+y2a2=r1−r2也就是ax+by=m的形式。
这是可以用扩展欧几里德解的。
若gcd(a,b)|m那么方程就无解,直接输出-1。 (如果m%gcd(a,b)!=0无解)
否则我们可以解出上式的y1。回带得到一个特解x0=r1−y1a1。
通解可以写成x=x0+k∗lcm(a1,a2)也就是x≡x0(modlcm(a1,a2))。
这样我们就将两个方程合并为了一个。
重复进行以上操作,我们最终能将n个方程全部合并,再用扩展欧几德得解出来就好了。
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; typedef long long ll; ll a[100005],r[100005]; int n; ll exgcd(ll a,ll b,ll &x,ll &y){ if(b == 0){ x = 1; y = 0; return a; } ll d = exgcd(b,a%b,x,y); ll tmp = x; x = y; y = tmp - a/b*y; return d; } ll solve(){ ll M = a[1],R = r[1],x,y; for(int i=2;i<=n;i++){ ll d = exgcd(M,a[i],x,y); if((R-r[i])%d!=0){//无解 return -1; } x = (R-r[i])/d*x%a[i];//这才是真正的x,记得模a[i] R -= x*M;//特解x0,新的余数 M = M/d*a[i];//新的模数 R %= M; } return (R%M+M)%M;//确保R不为负数 } int main(){ while(cin >> n){ for(int i=1;i<=n;i++){ cin >> a[i] >> r[i]; } ll ans = solve(); cout << ans << endl; } return 0; }