# 扩展欧几里得算法

扩展欧几里得算法是欧几里得算法的一种变体，它除了给出两个整数的最大公约数之外，还计算出满足贝祖等式 \( \gcd(a,b) = s \cdot a + t \cdot b \) 的整数 s 和 t。<sup>[1](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture14.html)</sup> 系数 s 和 t 称为贝祖系数，而该算法是求得它们的标准计算途径。<sup>[2](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.10%3A_Computing_Coefficients_for_Bezout%27s_Lemma)</sup> 其主要实际用途是计算模乘法逆元，而这正是密码学中关键步骤的基础。<sup>[3](https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=934860)</sup>

| 关键事实 | 详情 |
|---|---|
| 输出 | \( \gcd(a,b) \) 以及满足 \( \gcd(a,b) = s \cdot a + t \cdot b \) 的整数 s、t<sup>[1](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture14.html)</sup> |
| 依据 | 贝祖定理：对于正整数 a 和 b，这样的整数 s 和 t 总是存在<sup>[1](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture14.html)</sup> |
| 演算结果 | \( \gcd(803,154) = 11 = 5 \cdot 803 - 26 \cdot 154 \)<sup>[2](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.10%3A_Computing_Coefficients_for_Bezout%27s_Lemma)</sup> |
| 逆元用途 | 若 \( \gcd(a,m) = 1 \) 且 \( s \cdot a + t \cdot m = 1 \)，则 \( s \bmod m \) 是 a 模 m 的乘法逆元<sup>[1](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture14.html)</sup> |
| 运行时间 | \( O(\log n) \) 个除法步骤；最坏情形是相邻的斐波那契数<sup>[4](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)</sup> |
| 多项式形式 | 在域 \( \mathbf{k} \) 上的 \( R = \mathbf{k}[x] \) 中，贝祖系数的次数线性增长：\( \deg s_i = n_1 - n_{i-1} \) 且 \( \deg t_i = n_0 - n_{i-1} \)<sup>[5](https://www.csd.uwo.ca/~mmorenom/CS424/Lectures/EuclideanMethods.html/node4.html)</sup> |
| 软件 | Sage 将该计算暴露为 `xgcd(a,b)`；GMP 提供 `mpz_gcdext`<sup>[6](https://math-cs.gordon.edu/ntic/ntic2017/section-bezout-id.html)</sup><sup> • </sup><sup>[7](https://gmplib.org/gmp-man-6.2.1.pdf)</sup> |

## 扩展增加了什么

普通的欧几里得算法只计算最大公约数：它反复将数对 \((a, b)\) 替换为 \((b, a \bmod b)\)，直到余数为零，此时最后一个非零除数即为最大公约数。<sup>[3](https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=934860)</sup> 扩展版本在相同的步骤中保留了额外的记录，使得最大公约数也可以写成两个原始数的整系数线性组合。<sup>[8](https://www.math.cmu.edu/~bkell/21110-2010s/extended-euclidean.html)</sup>

这种组合的存在性正是贝祖定理的内容：若 a 和 b 是正整数，则存在整数 s 和 t 使得 \( \gcd(a,b) = s \cdot a + t \cdot b \)。<sup>[1](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture14.html)</sup> 由于最大公约数通常小于 a 和 b 两者，两个系数中通常有一个为负。<sup>[8](https://www.math.cmu.edu/~bkell/21110-2010s/extended-euclidean.html)</sup> 这种表示并不唯一。<sup>[6](https://math-cs.gordon.edu/ntic/ntic2017/section-bezout-id.html)</sup>

## 如何计算

**回代法。** 获得系数的一种方法是运行标准算法，同时记录每个除法步骤，然后从记录下的等式中解出余数，并反向代入。<sup>[1](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture14.html)</sup> 除法步骤中的每个余数 \( r \) 都满足 \( r = n - q \cdot d \)，其中 n 是被除数，q 是商，d 是除数，因此每个余数都可以用更早的余数重新表示。<sup>[8](https://www.math.cmu.edu/~bkell/21110-2010s/extended-euclidean.html)</sup>

LibreTexts 中 a = 803、b = 154 的例子展示了这一方法。欧几里得算法给出 \( 803 = 154 \cdot 5 + 33 \)、\( 154 = 33 \cdot 4 + 22 \)、\( 33 = 22 \cdot 1 + 11 \) 以及 \( 22 = 11 \cdot 2 + 0 \)，因此最大公约数为 11。对除最后一个之外的所有等式进行移项并反向代入，得到 \( 11 = 5 \cdot 33 - 1 \cdot 154 \)，进而 \( 11 = 5 \cdot 803 - 26 \cdot 154 \)，所以 s = 5，t = −26。<sup>[2](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.10%3A_Computing_Coefficients_for_Bezout%27s_Lemma)</sup>

**正向记账法。** 算法也可以改为在每一步维护当前余数关于原始 a 和 b 的表达式，随着除法进行不断更新系数对。按照西安大略大学（UWO）讲义中的记号，每个余数满足 \( r_i = s_i \cdot a + t_i \cdot b \)，系数通过递推式 \( s_k = s_{k-2} - q_k \cdot s_{k-1} \) 和 \( t_k = t_{k-2} - q_k \cdot t_{k-1} \) 更新，其中 \( q_k \) 是第 k 步的商；最终的 \( s_k \) 和 \( t_k \) 即为贝祖系数。<sup>[5](https://www.csd.uwo.ca/~mmorenom/CS424/Lectures/EuclideanMethods.html/node4.html)</sup> 一种避免回代过程的表格化变体被称为布兰金希普方法（Blankinship's method）。<sup>[2](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.10%3A_Computing_Coefficients_for_Bezout%27s_Lemma)</sup>

## 应用

**模逆元。** 当 \( \gcd(a, m) = 1 \) 时，贝祖定理给出整数 s 和 t 使得 \( s \cdot a + t \cdot m = 1 \)。对 m 取模可知 \( s \bmod m \) 是 a 模 m 的乘法逆元，因为 \( (s \bmod m) \cdot a \equiv 1 \pmod{m} \)。<sup>[1](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture14.html)</sup> 这些逆元反过来又使得模方程得以求解。<sup>[1](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture14.html)</sup> 一本密码学教材指出，求 b 模 a 的乘法逆元正是欧几里得算法在该领域的主要用途，而这样使用它有时本身就被称为扩展欧几里得算法。<sup>[3](https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=934860)</sup>

**线性丢番图方程。** 该扩展可用于求解线性丢番图方程，即形如 \( s \cdot a + t \cdot b = c \) 的整数方程。<sup>[8](https://www.math.cmu.edu/~bkell/21110-2010s/extended-euclidean.html)</sup>

**多项式与域。** 该算法不仅适用于整数，也适用于其他欧几里得整环，如多项式环。在 \( R = \mathbf{k}[x] \) 上（其中 \( \mathbf{k} \) 是域），贝祖系数的次数随输入次数线性增长，且只要其次数足够小，系数本质上唯一。<sup>[5](https://www.csd.uwo.ca/~mmorenom/CS424/Lectures/EuclideanMethods.html/node4.html)</sup> 扩展欧几里得算法的多项式版本被用于计算有限域（如 \( \mathrm{GF}(2^m) \)）中的逆元。<sup>[9](https://arxiv.org/abs/quant-ph/0407095)</sup>

## 效率

最坏情形出现在相邻的斐波那契数上：由于 \( f_n \bmod f_{n-1} = f_{n-2} \)，计算 \( \gcd(f_n, f_{n-1}) \) 会在终止前回溯整个斐波那契序列。<sup>[4](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)</sup>

对这一运行时间的历史分析本身就是一个课题。滑铁卢大学计算机科学家杰弗里·沙利特（Jeffrey Shallit）的研究于1994年发表在《数学史》（*Historia Mathematica*）上，其中记载：首次分析的功劳传统上归于加布里埃尔·拉梅（Gabriel Lamé）1844年的论文，但安托万-安德烈-路易·雷诺（Antoine-André-Louis Reynaud）早在1811年就给出了一个较弱的界，埃米尔·莱热（Émile Léger）在1837年已知道拉梅的基本结果，而皮埃尔-约瑟夫-艾蒂安·芬克（Pierre-Joseph-Étienne Finck）在1841年沿不同思路给出了完整而有效的证明。<sup>[10](https://www.sciencedirect.com/science/article/pii/S0315086084710317)</sup> 关于相邻斐波那契数会导致特别长的运行时间这一观察，至少可以追溯到16世纪。<sup>[11](https://arxiv.org/abs/1409.0729)</sup>

## 历史与软件

欧几里得算法与求解余数问题之间的联系很早就被看到了：巴歇·德·梅齐里亚克（Bachet de Méziriac）在1624年首次将这些方法与欧几里得算法以及丢番图分析联系起来。<sup>[12](https://www.sciencedirect.com/science/article/pii/S0315086008000670)</sup> 高德纳（D. E. Knuth）曾评论说，欧几里得算法是“留存至今最古老的非平凡算法”。<sup>[10](https://www.sciencedirect.com/science/article/pii/S0315086084710317)</sup>

计算机代数系统和库直接提供扩展计算。Sage 将其实现为 `xgcd(a,b)`，这个名称体现了“扩展欧几里得算法”（eXtended Euclidean algorithm）。<sup>[6](https://math-cs.gordon.edu/ntic/ntic2017/section-bezout-id.html)</sup> GMP 库的 `mpz_gcdext` 将 g 设为 \( \gcd(a,b) \)，将 s、t 设为满足 \( a \cdot s + b \cdot t = g \) 的系数，其选取方式使得通常 \( |s| < |b|/(2g) \) 且 \( |t| < |a|/(2g) \)，这在少数例外情形之外唯一确定了 s 和 t。<sup>[7](https://gmplib.org/gmp-man-6.2.1.pdf)</sup>

## References

1. [CSE 311 Lecture 14: Euclidean Algorithm and Modular Equations, University of Washington](https://courses.cs.washington.edu/courses/cse311/20sp/lectures/lecture14.html)
2. [1.10: Computing Coefficients for Bezout's Lemma, Mathematics LibreTexts](https://math.libretexts.org/Bookshelves/Combinatorics_and_Discrete_Mathematics/Elementary_Number_Theory_%28Barrus_and_Clark%29/01%3A_Chapters/1.10%3A_Computing_Coefficients_for_Bezout%27s_Lemma)
3. [Network Security: Private Communication in a Public World, 3rd Edition (NIST copy)](https://tsapps.nist.gov/publication/get_pdf.cfm?pub_id=934860)
4. [SI 335, Unit 3: Number-Theoretic Computations, United States Naval Academy](https://www.usna.edu/Users/cs/wcbrown/courses/S17SI335/notes/03/notes.html)
5. [The Extended Euclidean Algorithm, University of Western Ontario CS 424 lecture notes](https://www.csd.uwo.ca/~mmorenom/CS424/Lectures/EuclideanMethods.html/node4.html)
6. [NTIC: The Bezout Identity, Gordon College](https://math-cs.gordon.edu/ntic/ntic2017/section-bezout-id.html)
7. [GNU MP 6.2.1 Manual, Chapter 5: Integer Functions](https://gmplib.org/gmp-man-6.2.1.pdf)
8. [21-110: The extended Euclidean algorithm, Carnegie Mellon University](https://www.math.cmu.edu/~bkell/21110-2010s/extended-euclidean.html)
9. [Optimized quantum implementation of elliptic curve arithmetic over binary fields, arXiv](https://arxiv.org/abs/quant-ph/0407095)
10. [Jeffrey Shallit (1994). Origins of the analysis of the Euclidean algorithm. Historia Mathematica 21(4), 401–419.](https://www.sciencedirect.com/science/article/pii/S0315086084710317)
11. [A rigorous version of R. P. Brent's model for the binary Euclidean algorithm, arXiv](https://arxiv.org/abs/1409.0729)
12. [Modular arithmetic before C.F. Gauss, Historia Mathematica](https://www.sciencedirect.com/science/article/pii/S0315086008000670)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 物质世界与数学 › 数学与统计学 › 数与代数 › Number theory › Elementary number theory › Divisibility, GCD, and the integers

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/extended-euclidean-algorithm

English original: https://www.edgepedia.org/extended-euclidean-algorithm
