前言:以前这两个定理我一直都没有搞懂,最近我刷题时遇到了,又去学了一遍,竟然就懂了。。。
中国剩余定理
设
m
1
,
m
2
,
.
.
.
,
m
n
m_1,m_2,...,mn
m1,m2,...,mn是两两互质的整数,
m
=
∏
i
=
1
n
m
i
m=\begin{matrix} \prod_{i=1}^n m_i \end{matrix}
m=∏i=1nmi,
M
i
=
m
/
m
i
M_i=m/mi
Mi=m/mi,
t
i
t_i
ti时线性同余方程
M
i
t
i
≡
1
(
m
o
d
m
i
)
M_it_i\equiv1\pmod{m_i}
Miti≡1(modmi)的一个解。对于任意的n个整数
a
1
,
a
2
,
.
.
.
,
a
n
a_1,a_2,...,a_n
a1,a2,...,an,方程组
{
x
≡
a
1
(
m
o
d
m
1
)
x
≡
a
2
(
m
o
d
m
2
)
.
.
.
x
≡
a
n
(
m
o
d
m
n
)
\begin{cases} x\equiv a_1\pmod{m_1}\\ x\equiv a_2\pmod{m_2}\\ ...\\ x\equiv a_n\pmod{m_n} \end{cases}
⎩⎪⎪⎪⎨⎪⎪⎪⎧x≡a1(modm1)x≡a2(modm2)...x≡an(modmn)
有整数解,则解为
x
=
∑
i
=
1
n
a
i
M
i
t
i
x=\sum_{i=1}^n a_iM_it_i
x=∑i=1naiMiti
证明:
因为
M
i
=
m
/
m
i
M_i=m/m_i
Mi=m/mi是除
m
i
m_i
mi之外所有模数的倍数,所以
∀
k
≠
i
,
a
i
M
i
t
i
≡
0
(
m
o
d
m
k
)
\forall k\ne i,a_iM_it_i\equiv 0\pmod{m_k}
∀k=i,aiMiti≡0(modmk)。又因为
a
i
M
i
t
i
≡
a
i
(
m
o
d
m
i
)
a_iM_it_i\equiv a_i \pmod{m_i}
aiMiti≡ai(modmi),所以带入
x
=
∑
i
=
1
n
a
i
M
i
t
i
x=\sum_{i=1}^n a_iM_it_i
x=∑i=1naiMiti,原方程组成立。
证毕。
中国剩余定理给出了模数两两互质的线性同余方程组的一个特解。方程组的通解可以表示为 x + k m ( k ∈ Z ) x+km(k\in Z) x+km(k∈Z)。有些题目求出最小的非负整数解,只需要把 x x x对 m m m取模,并让 x x x落在0~m-1的范围内即可。原因就是 k m km km对于任意的 m i m_i mi取模的结果都是0。
扩展中国剩余定理
请大家看看POJ2891
Strange Way to Express Integers
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 10787 Accepted: 3291
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.
对于这一道题,
m
i
m_i
mi没有两两互质,中国剩余定理就行不通了。于是我们尝试使用数学归纳法,假设已经求出了前
k
−
1
k-1
k−1个方程构成的方程组的一个解
x
x
x。记为
m
=
∏
i
=
1
k
−
1
m
i
m=\begin{matrix} \prod_{i=1}^{k-1} m_i \end{matrix}
m=∏i=1k−1mi则
x
+
i
∗
m
(
i
∈
Z
)
x+i*m(i\in Z)
x+i∗m(i∈Z)是前
k
−
1
k-1
k−1个方程的通解。
所以我们尝试将第k个方程合并进去。考虑第k个方程,求出一个整数t,使得
x
+
t
∗
m
≡
a
k
(
m
o
d
m
k
)
x+t*m\equiv a_k\pmod{m_k}
x+t∗m≡ak(modmk)。该方程等价于
m
∗
t
≡
a
k
−
x
(
m
o
d
m
k
)
m*t\equiv a_k-x\pmod{m_k}
m∗t≡ak−x(modmk),其中t是未知量。这就是一个线性同余方程,可以用扩展欧几里得算法判断是否有解,并求出它的解,则
x
′
=
x
+
t
∗
m
x'=x+t*m
x′=x+t∗m就是前k个方程构成的方程组的一个解。
综上所述,我们使用n次扩展欧几里得算法,就求出了整个方程组的解。
参考代码:
//Author:XuHt
#include <iostream>
#define ll long long
using namespace std;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1;
y = 0;
return a;
}
ll d = exgcd(b, a % b, x, y);
ll z = x;
x = y;
y = z - y * (a / b);
return d;
}
int main() {
int n;
while (cin >> n) {
ll a, b, x, y, k;
cin >> a >> b;
ll lcm = a, ans = b;//lcm对应m,ans对应x
bool flag = 1;
--n;
while (n--) {
cin >> a >> b;
if(!flag)continue;
b = (b - ans % a + a) % a;//对应m*t≡a[k]-x(mod m[k])中的a[k]-x
ll d = exgcd(lcm, a, x, y);
if (b % d) flag = 0;//如果无法满足,就记录
else k = x * (b / d) % a;//求出m*t≡a[k]-x(mod m[k])中的t,并用k表示
ans += k * lcm;//对应x'=x+t*m
lcm = lcm / d * a;//更新
ans = (ans % lcm + lcm) % lcm;
}
if (flag) cout << ans << endl;
else cout << "-1" << endl;
}
return 0;
}