最大公约数与最小公倍数

最大公约数与最小公倍数

一、定义

1. 最大公约数

a 1 , a 2 , a 3 , . . . , a k a_1, a_2, a_3, ... , a_k a1,a2,a3,...,ak k k k 个非 0 整数,如果 ∃ d ∈ Z , d ≠ 0 \exist d \in Z, d \neq 0 dZ,d=0 ,使得 d ∣ a 1 , d ∣ a 2 , d ∣ a 3 , . . . , d ∣ a k d \mid a_1, d \mid a_2, d \mid a_3, ... , d \mid a_k da1,da2,da3,...,dak ,则称 d d d a 1 , a 2 , a 3 , . . . , a k a_1, a_2, a_3, ... , a_k a1,a2,a3,...,ak 的公约数;

公约数中最大的一个称为最大公约数,记为 gcd ⁡ ( a 1 , a 2 , a 3 , . . . , a k ) \gcd(a_1, a_2, a_3, ... , a_k) gcd(a1,a2,a3,...,ak) ,其永远存在,最少为 1 ;

gcd ⁡ = 1 \gcd = 1 gcd=1 时,称这 n n n 个数是互质或互约的,公约数一定是最大公约数的约数;

2. 最小公倍数

a 1 , a 2 , a 3 , . . . , a k a_1, a_2, a_3, ... , a_k a1,a2,a3,...,ak k k k 个非 0 整数,如果 ∃ d ∈ Z , d ≠ 0 \exist d \in Z, d \neq 0 dZ,d=0 ,使得 a 1 ∣ d , a 2 ∣ d , a 3 ∣ d , . . . , a k ∣ d a_1 \mid d, a_2 \mid d, a_3 \mid d, ... , a_k \mid d a1d,a2d,a3d,...,akd ,则称 d d d a 1 , a 2 , a 3 , . . . , a k a_1, a_2, a_3, ... , a_k a1,a2,a3,...,ak 的公倍数;

公倍数中最下的一个称为最小公倍数,记为 l c m ( a 1 , a 2 , a 3 , . . . , a k ) lcm(a_1, a_2, a_3, ... , a_k) lcm(a1,a2,a3,...,ak) ,其永远存在;

公倍数一定是最小公倍数的倍数;

3. 定理

l c m ( a , b ) ∗ gcd ⁡ ( a , b ) = a b lcm(a, b) * \gcd(a, b) = ab lcm(a,b)gcd(a,b)=ab

证明如下,

a , b a, b a,b 质因数分解,设 a , b a, b a,b 质因数集合并集为 { P 1 , P 2 , . . . , P n } \{P_1, P_2, ... , P_n\} {P1,P2,...,Pn}

则设 a = P 1 k 1 ∗ P 2 k 2 ∗ . . . ∗ P n k n ( 0 ≤ k i , k i ∈ Z ) , b = P 1 j 1 ∗ P 2 j 2 ∗ . . . ∗ P n j n ( 0 ≤ j i , j i ∈ Z ) a = P_1^{k_1} * P_2^{k_2} * ... * P_n^{k_n} (0 \leq k_i, k_i \in Z), b = P_1^{j_1} * P_2^{j_2} * ... * P_n^{j_n} (0 \leq j_i, j_i \in Z) a=P1k1P2k2...Pnkn(0ki,kiZ),b=P1j1P2j2...Pnjn(0ji,jiZ)

∴ gcd ⁡ ( a , b ) = P 1 min ⁡ ( k 1 , j 1 ) ∗ P 2 min ⁡ ( k 2 , j 2 ) ∗ P 3 min ⁡ ( k 3 , j 3 ) ∗ . . . ∗ P n min ⁡ ( k n , j n ) \therefore \gcd(a, b) = P_1^{\min(k_1, j_1)} * P_2^{\min(k_2, j_2)} * P_3^{\min(k_3, j_3)} * ... * P_n^{\min(k_n, j_n)} gcd(a,b)=P1min(k1,j1)P2min(k2,j2)P3min(k3,j3)...Pnmin(kn,jn)

l c m ( a , b ) = P 1 max ⁡ ( k 1 , j 1 ) ∗ P 2 max ⁡ ( k 2 , j 2 ) ∗ P 3 max ⁡ ( k 3 , j 3 ) ∗ . . . ∗ P n max ⁡ ( k n , j n ) \quad lcm(a, b) = P_1^{\max(k_1, j_1)} * P_2^{\max(k_2, j_2)} * P_3^{\max(k_3, j_3)} * ... * P_n^{\max(k_n, j_n)} lcm(a,b)=P1max(k1,j1)P2max(k2,j2)P3max(k3,j3)...Pnmax(kn,jn)

∵ min ⁡ ( k i , j i ) + max ⁡ ( k i , j i ) = k i + j i \because \min(k_i, j_i) + \max(k_i, j_i) = k_i + j_i min(ki,ji)+max(ki,ji)=ki+ji

∴ gcd ⁡ ( a , b ) ∗ l c m ( a , b ) = P 1 k 1 + j 1 ∗ . . . ∗ P n k n + j n = a ∗ b \therefore \gcd(a, b) * lcm(a, b) = P_1^{k_1 + j_1} * ... * P_n^{k_n + j_n} = a * b gcd(a,b)lcm(a,b)=P1k1+j1...Pnkn+jn=ab

二、辗转相除法

1. 说明

辗转相除法用于求两个数的最大公约数,又称欧几里得算法,其原理是 gcd ⁡ ( a , b ) = gcd ⁡ ( b , a    m o d    b ) \gcd(a, b) = \gcd(b, a \; mod \; b) gcd(a,b)=gcd(b,amodb)

2. 证明

gcd ⁡ ( a , b ) = p \gcd(a, b) = p gcd(a,b)=p ,则

a = k p , b = g p , ( k , g ) = 1 , k , g ∈ Z a = kp, b = gp, (k, g) = 1, k, g \in Z a=kp,b=gp,(k,g)=1,k,gZ

由模运算放缩性得,

a    m o d    b = k p    m o d    g p = p ( k    m o d    g ) a \; mod \; b = kp \; mod \; gp = p(k \; mod \; g) amodb=kpmodgp=p(kmodg)

∴ gcd ⁡ ( b , a    m o d    b ) = gcd ⁡ ( g p , ( k    m o d    g ) ∗ p ) \therefore \gcd(b, a \; mod \; b) = \gcd(gp, (k \; mod \; g) * p) gcd(b,amodb)=gcd(gp,(kmodg)p)

∵ ( k , g ) = 1 \because (k, g) = 1 (k,g)=1

∴ gcd ⁡ ( b , a    m o d    b ) = p \therefore \gcd(b, a \; mod \; b) = p gcd(b,amodb)=p

三、扩展欧几里得算法

1. 说明

已知整数 a , b a, b a,b 时,一定 ∃ x , y ∈ Z \exist x, y \in Z x,yZ 使得 a x + b y = gcd ⁡ ( a , b ) ax + by = \gcd(a, b) ax+by=gcd(a,b)

证明如下,

a x 1 + b y 1 = gcd ⁡ ( a , b ) , b x 2 + ( a    m o d    b ) y 2 = gcd ⁡ ( b , a    m o d    b ) ax_1 + by_1 = \gcd(a, b), bx_2 + (a \; mod \; b)y_2 = \gcd(b, a \; mod \; b) ax1+by1=gcd(a,b),bx2+(amodb)y2=gcd(b,amodb)

由欧几里得定理可得, gcd ⁡ ( a , b ) = gcd ⁡ ( b , a    m o d    b ) \gcd(a, b) = \gcd(b, a \; mod \; b) gcd(a,b)=gcd(b,amodb)

∴ a x 1 + b y 1 = b x 2 + ( a    m o d    b ) y 2 \therefore ax_1 + by_1 = bx_2 + (a \; mod \; b) y2 ax1+by1=bx2+(amodb)y2

整理得 a x 1 + b y 1 = a y 2 + b ( x 2 − ⌊ a b ⌋ y 2 ) ax_1 + by_1 = ay_2 + b(x_2 - \lfloor\frac{a}{b}\rfloor y_2) ax1+by1=ay2+b(x2bay2)

∴ ∃ x 1 = y 2 , y 1 = x 2 − ⌊ a b ⌋ y 2 \therefore \exist x_1 = y_2, y_1 = x_2 - \lfloor\frac{a}{b}\rfloor y_2 x1=y2,y1=x2bay2

x 1 , y 1 x_1, y_1 x1,y1 这组解由 x 2 , y 2 x_2, y_2 x2,y2 得来,

x n , y n x_n, y_n xn,yn 关于 gcd ⁡ ( a , 0 ) \gcd(a, 0) gcd(a,0) 时,有

a x n + b y n = gcd ⁡ ( a , 0 ) = a ax_n + by_n = \gcd(a, 0) = a axn+byn=gcd(a,0)=a

∴ x n = 1 , y n = 0 \therefore x_n = 1, y_n = 0 xn=1,yn=0

∴ ∃ x n , y n \therefore \exist x_n, y_n xn,yn

∴ ∃ x , y \therefore \exist x, y x,y

2. 代码

思路

则模拟每次证明的思路进行辗转相除后更新 x , y x, y x,y 即可;

代码
int ex_gcd(int a, int b, int &x, int &y) {
	int tot1, tot2;
	if (!b) {
		x = 1, y = 0;
		return a;
	}
	tot1 = ex_gcd(b, a % b, x, y);
	tot2 = x;
	x = y;
	y = tot2 - a / b * y;
	return tot1;
}

四、裴蜀定理

1. 定理

对于不定方程 a x + b y = m ax + by = m ax+by=m ,其有解的充要条件为 gcd ⁡ ( a , b ) ∣ m \gcd(a, b) \mid m gcd(a,b)m

证明如下,

  1. 必要性,即 a x + b y = m ax + by = m ax+by=m 有解,可得 gcd ⁡ ( a , b ) ∣ m \gcd(a, b) \mid m gcd(a,b)m

    ∵ a x + b y = m \because ax + by = m ax+by=m 有解

    ∴ ∃ x , y ∈ Z \therefore \exist x, y \in Z x,yZ 使得 a x + b y = m ax + by = m ax+by=m

    ∵ gcd ⁡ ∣ a , g c d ∣ b \because \gcd \mid a, gcd \mid b gcda,gcdb

    ∴ g c d ( a , b ) ∣ a x + b y \therefore gcd(a, b) \mid ax + by gcd(a,b)ax+by

    ∴ g c d ( a , b ) ∣ m \therefore gcd(a, b) \mid m gcd(a,b)m

  2. 充分性,即 g c d ( a , b ) ∣ m gcd(a, b) \mid m gcd(a,b)m ,可得 a x + b y = m ax + by = m ax+by=m 有解;

    由扩展欧拉定理得, a x + b y = gcd ⁡ ( a , b ) ax + by = \gcd(a, b) ax+by=gcd(a,b) 一定有解

    ∵ g c d ( a , b ) ∣ m \because gcd(a, b) \mid m gcd(a,b)m

    ∴ a x + b y = m \therefore ax + by = m ax+by=m 有解

2. 引理1

a , b ∈ Z + a, b \in Z^+ a,bZ+ ,且 ( a , b ) = 1 (a, b) = 1 (a,b)=1 ,则 ∄ k < b ∈ Z + \not\exist k < b \in Z^+ k<bZ+ ,使得 b ∣ k a b \mid ka bka

使用反证法,证明如下,

∃ 0 < k < b ∈ Z \exist 0 < k < b \in Z ∃0<k<bZ ,使得 b ∣ k a b \mid ka bka ,则 k a ka ka 中一定包含 b b b 的所有质因子

∵ ( a , b ) = 1 \because (a, b) = 1 (a,b)=1

∴ a \therefore a a 中不可能包含 b b b 的质因子

∴ k \therefore k k 定为 b b b 的倍数

0 < k < b 0 < k < b 0<k<b 矛盾,假设不成立;

3. 推论

a , b ∈ Z + a, b \in Z^+ a,bZ+ ( a , b ) = 1 (a, b) = 1 (a,b)=1 ,则 0 , a , 2 a , 3 a , . . . , ( b − 1 ) a 0, a, 2a, 3a, ... , (b - 1)a 0,a,2a,3a,...,(b1)a 分别 m o d    b mod \; b modb 的余数互不相等;

证明使用反证法,如下,

如果存在 i a , j a , ( 0 < j < i < b ) ia, ja, (0 < j < i < b) ia,ja,(0<j<i<b) ,使得它们分别 m o d    b mod \; b modb 的余数相等,则 ( i − j ) ∗ a    m o d    b = = 0 (i - j) * a \; mod \; b == 0 (ij)amodb==0 0 < i − j < b 0 < i - j < b 0<ij<b 与引理 1 矛盾,假设不成立;

4. 引理2

( a , b ) = 1 (a, b) = 1 (a,b)=1 ,则 ∃ k ∈ Z \exist k \in Z kZ 使 k ∗ a    m o d    b = 1 k * a \; mod \; b = 1 kamodb=1

证明如下,

根据推论, 0 , a , 2 a , 3 a , . . . , ( b − 1 ) a 0, a, 2a, 3a, ... , (b - 1)a 0,a,2a,3a,...,(b1)a 分别 m o d    b mod \; b modb 的余数互不相等,且有 b b b 个,在 [ 0 , b − 1 ] [0, b - 1] [0,b1] 之间

∴ ∃ k ∈ Z \therefore \exist k \in Z kZ 使得 k ∗ a    m o d    b = 1 k * a \; mod \; b = 1 kamodb=1

五、费马小定理

如果 p p p 为质数,且 a    m o d    p ≠ 0 a \; mod \; p \neq 0 amodp=0 ,则 a p − 1    m o d    1 a^{p - 1} \; mod \; 1 ap1mod1

证明如下,

( a ∗ 2 a ∗ 3 a ∗ . . . ∗ ( p − 1 ) a )    m o d    p = ( a p − 1 ∗ ( p − 1 ) ! )    m o d    p (a * 2a * 3a * ... * (p - 1)a) \; mod \; p = (a^{p - 1} * (p - 1)!) \; mod \; p (a2a3a...(p1)a)modp=(ap1(p1)!)modp

∵ p \because p p 为质数,且 a a a 不为 p p p 的倍数,根据裴蜀定理推论有,

a    m o d    p , 2 a    m o d    p , 3 a    m o d    p , . . . , ( p − 1 ) a    m o d    p a \; mod \; p, 2a \; mod \; p, 3a \; mod \; p, ... , (p - 1)a \; mod \; p amodp,2amodp,3amodp,...,(p1)amodp 互不相等,且在 [ 1 , p − 1 ] [1, p - 1] [1,p1] 之间

∴ ( a ∗ 2 a ∗ . . . ∗ ( p − 1 ) a )    m o d    p \therefore (a * 2a * ... * (p - 1)a) \; mod \; p (a2a...(p1)a)modp

= a    m o d    p ∗ 2 a    m o d    p ∗ . . . ∗ ( p − 1 ) a    m o d    p = a \; mod \; p * 2a \; mod \; p * ... * (p - 1)a \; mod \; p =amodp2amodp...(p1)amodp

= ( p − 1 ) !    m o d    p = (p - 1)! \; mod \; p =(p1)!modp

∴ a p − 1 ∗ ( p − 1 ) !    m o d    p = ( p − 1 ) !    m o d    p \therefore a^{p - 1} * (p - 1)! \; mod \; p = (p - 1)! \; mod \; p ap1(p1)!modp=(p1)!modp

∵ p \because p p 为质数

∴ ( p , ( p − 1 ) ! ) = 1 \therefore (p, (p - 1)!) = 1 (p,(p1)!)=1

∴ a p − 1    m o d    1 \therefore a^{p - 1} \; mod \; 1 ap1mod1

你可能感兴趣的:(数学,数学,扩展欧几里得,费马小定理)