网络安全-数论入门-孙子定理(CRT)

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} x1mod2x2mod3x3mod5x5mod7
    解:
    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=1kmi=2357=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=105M1=m2M=70M1=m3M=42M1=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\\ e1M11mod21e2M21mod31e3M31mod53e4M41mod74
    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(10511+7012+4233+3045)mod210173

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;
		}
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王文聪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值