1. 简介
孙子定理是中国古代求解一次同余式组(见同余)的方法。是数论中一个重要定理。又称中国余数定理。一元线性同余方程组问题最早可见于中国南北朝时期(公元5世纪)的数学著作《孙子算经》卷下第二十六题,叫做“物不知数”问题,原文如下:
有物不知其数,三三数之剩二,五五数之剩三,七七数之剩二。问物几何?即,一个整数除以三余二,除以五余三,除以七余二,求这个整数。《孙子算经》中首次提到了同余方程组问题,以及以上具体问题的解法,因此在中文数学文献中也会将中国剩余定理称为孙子定理。
2.公式
3. 例子
- 由以下方程组求x
{ x ≡ 1 m o d 2 x ≡ 2 m o d 3 x ≡ 3 m o d 5 x ≡ 5 m o d 7 \begin{cases} x \equiv 1 mod 2 \\ x \equiv 2 mod 3 \\ x \equiv 3 mod 5 \\ x \equiv 5 mod 7 \\ \end{cases} ⎩⎪⎪⎪⎨⎪⎪⎪⎧x≡1mod2x≡2mod3x≡3mod5x≡5mod7
解:
M = ∏ i = 1 k m i = 2 ∗ 3 ∗ 5 ∗ 7 = 210 M = \displaystyle\prod_{i=1}^k m_i = 2*3*5*7 = 210 M=i=1∏kmi=2∗3∗5∗7=210
M 1 = M m 1 = 105 , M 1 = M m 2 = 70 M 1 = M m 3 = 42 , M 1 = M m 4 = 30 M_1 = \frac{M}{m_1} = 105 ,M_1 = \frac{M}{m_2} = 70\\ M_1 = \frac{M}{m_3} = 42,M_1 = \frac{M}{m_4} = 30\\ M1=m1M=105,M1=m2M=70M1=m3M=42,M1=m4M=30
e 1 ≡ M 1 − 1 m o d 2 ≡ 1 , e 2 ≡ M 2 − 1 m o d 3 ≡ 1 e 3 ≡ M 3 − 1 m o d 5 ≡ 3 , e 4 ≡ M 4 − 1 m o d 7 ≡ 4 e_1 \equiv M^{-1}_1 mod 2 \equiv 1,e_2 \equiv M^{-1}_2 mod 3 \equiv 1\\ e_3 \equiv M^{-1}_3 mod 5 \equiv 3,e_4 \equiv M^{-1}_4 mod 7 \equiv 4\\ e1≡M1−1mod2≡1,e2≡M2−1mod3≡1e3≡M3−1mod5≡3,e4≡M4−1mod7≡4
x m o d 210 ≡ ( 105 ∗ 1 ∗ 1 + 70 ∗ 1 ∗ 2 + 42 ∗ 3 ∗ 3 + 30 ∗ 4 ∗ 5 ) m o d 210 ≡ 173 x mod 210 \equiv (105*1*1+70*1*2+42*3*3+30*4*5) mod 210 \equiv 173\\ xmod210≡(105∗1∗1+70∗1∗2+42∗3∗3+30∗4∗5)mod210≡173
4. 代码实现 (Java)
public class crt {
public static void main(String[] args) {
// mod 左边数的集合数组
int[] a = {1,2,3,5};
// mod 右边数的集合数组
int[] m = {2,3,5,7};
int x = crt(a,m);
System.out.println("方程组的解为" + x);
}
/**
* 中国剩余定理(CRT)
* @param a
* @param m
* @return
*/
public static int crt(int[] a,int[] m) {
int M = arrayProduct(m);
int[] e = new int[m.length];
int sum = 0;
for (int i = 0; i < m.length; i++) {
e[i] = gcd(M/m[i],m[i]);
sum += M/m[i]*e[i]*a[i];;
}
return sum % M;
}
/**
* 整数数组的元素之积
* @param array
* @return
*/
public static int arrayProduct(int[] array) {
int product = 1;
for (int i = 0; i < array.length; i++) {
product *= array[i];
}
return product;
}
/**
* 乘法逆元( f^-1 mod d)
* @param f
* @param d
* @return
*/
public static int gcd(int f, int d) {
int x1 = 1, x2 = 0, x3 = d;
int y1 = 0, y2 = 1, y3 = f;
while (true) {
if (y3 == 0) {
return 0;
}
if (y3 == 1) {
return (y2 + d) % d;
}
int q = x3 / y3;
int t1 = x1 - q * y1;
int t2 = x2 - q * y2;
int t3 = x3 - q * y3;
x1 = y1;
x2 = y2;
x3 = y3;
y1 = t1;
y2 = t2;
y3 = t3;
}
}
}