跳到正文

算法·理论

扩展·更相减损法在数论中的应用

watermouthhang约 22 分钟
原文

这是 2026年1月27日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年1月27日
更新
2026年1月27日
篇幅
约 22 分钟

版本

  1. v2当前
  2. v1

算法引入

在学习数论时,我们会学习一种算法,叫做 扩展欧几里得算法(以下简称 扩欧 ), 其思路可以参考这篇博客,时间复杂度为 O⁡(log⁡(max⁡(a,b))\operatorname O(\log(\max(a,b))

不过,我们还可以找到另一种算法来计算EXGCD。
是谁呢? 更相减损法(Phase reduction method,PRM)。

基础更相减损法

本体

式子如下(想看证明的看这):

gcd⁡(a,b)={gcd⁡(b,a)(a<b)gcd⁡(b,a−b)(a≥b且b≠0)a(b=0)\gcd(a,b)=\begin{cases} \gcd(b,a) & (a<b) \\ \gcd(b,a-b) & (a\ge b \text{且}b\ne0) \\ a & (b=0) \end{cases}

举个例子 求 114114 和 514514 的最大公因数:

aabb
114114514514
514514114114
114114514−114=400514-114=400
400400114114
114114400−114=286400-114=286
286286114114
114114286−114=172286-114=172
172172114114
114114172−114=58172-114=58
5858114−58=56114-58=56
565658−56=258-56=2
⋯\cdots⋯\cdots
4422
2222
2200

所以,114114 和 514514 的最大公因数是 22 。

到省略号部分时,我们可以发现,当 aa , bb 差距过大时,该算法效率会大幅降低。如果构造 {n,1}\{n,1\} 这样的数据,复杂度会退化回 O⁡(n)\operatorname O(n) 。

所以, 需要优化\text{\color{red}需要优化} 。

优化·式子

显然, 当 aa , bb 都是 22 的倍数时

gcd⁡(a,b)=2×gcd⁡(a÷2,b÷2)\gcd(a,b)=2 \times \gcd(a \div 2,b \div 2 )

否则,当 aa 是 22 的倍数时

gcd⁡(a,b)=gcd⁡(a÷2,b)\gcd(a,b)=\gcd(a \div 2,b)

否则,当 bb 是 22 的倍数时

gcd⁡(a,b)=gcd⁡(a,b÷2)\gcd(a,b)=\gcd(a,b \div 2)

我们,就可以就此,利用移位运算(不会见此)除以 22 优化。

则最终式子为(其中 x∣yx \mid y 表示 yy 是 xx 的因数,x∤yx \nmid y 表示 yy 不是 xx 的因数):

gcd⁡(a,b)={(1)a(b=0)(2)2×gcd⁡(a÷2,b÷2)(2∣a且2∣b且b≠0)(3)gcd⁡(a÷2,b)(2∣a且2∤b且b≠0)(4)gcd⁡(a,b÷2)(2∤a且2∣b且b≠0)(5)gcd⁡(b,a)(a<b且2∤a且2∤b且b≠0)(6)gcd⁡(b,a−b)(a≥b且2∤a且2∤b且b≠0)\gcd(a,b)=\begin{cases} (1)a & (b=0) \\ (2)2 \times \gcd(a \div 2,b \div 2) & (2\mid a \text{且} 2\mid b \text{且}b\ne0) \\ (3)\gcd(a \div 2,b) & (2\mid a \text{且} 2\nmid b \text{且}b\ne0) \\ (4)\gcd(a,b \div 2) & (2\nmid a \text{且} 2\mid b \text{且}b\ne0) \\ (5)\gcd(b,a) & (a<b \text{且} 2\nmid a \text{且} 2\nmid b \text{且}b\ne0) \\ (6)\gcd(b,a-b) & (a\ge b \text{且} 2\nmid a \text{且} 2\nmid b \text{且}b\ne0) \\ \end{cases}

2024-4-21更新至此\text{\color{white}2024-4-21更新至此}

他喵的,写了后 面时间复杂度,在新网页上保存上了。\text{\color{white}他喵的,写了后 面时间复杂度,在新网页上保存上了。}

老网页没刷新,又在老网页上保存了, 后面的全没了……没了o(╥﹏╥)o\text{\color{white}老网页没刷新,又在老网页上保存了, 后面的全没了……没了o(╥﹏╥)o}

优化·时间复杂度

先说结论:稳定为 O⁡(log⁡(max⁡(a,b)))\operatorname O(\log(\max(a,b))) 。
为什么呢?

证明如下:

命题 11

内容:当递归不在 (2)(2) 式时,以后也不可能进入 (2)(2) 式。

证明:对于 (1)(5)(1)(5) 式,显然。
对于 (3)(3) 式,已经 2∤b2 \nmid b ,在运行完之后依旧如此,不会进入 (2)(2) 式。
对于 (4)(4) 式,同上。
对于 (6)(6) 式,因为 2∤a2 \nmid a , 2∤b2 \nmid b 。
即

a≡1(mod2)a \equiv 1 \pmod 2 b≡1(mod2)b \equiv 1 \pmod 2

所以

a−b≡0(mod2)a-b \equiv 0 \pmod 2

即 2∣(a−b)2 \mid (a-b) 。
因为 2∤b2 \nmid b , 2∣(a−b)2 \mid (a-b) ,
所以,在 (6)(6) 式之后的下一个式子,一定是 (4)(5)(4)(5) 式。

综上,命题成立。

命题 22

内容: (1)(1) 式只能出现一次。

证明:略。

命题 33

内容:(5)(6)(5)(6) 式不能连续 44 次出现。

证明:对于 (5)(5) 式,显然。
对于 (6)(6) 式,见命题 1(6)1(6) ,成立。
对于 (5)(6)(5)(6) 式一起,由命题 1(6)1(6) ,(6)式以后为 (4)(5)(4)(5) 式。
在此时 (5)(5) 式以后,只能为 (4)(4) 式。

综上,命题成立。

-------命题结束-----

现在开始分析。

(1)(5)(6)(1)(5)(6) 式不会连续出现,且附着在 (3)(4)(3)(4) 式后,可以忽略。
(2)(3)(4)(2)(3)(4) 式最多运行 log⁡\log 级别的次数。

所以,时间复杂度为稳定的 O⁡(log⁡(max⁡(a,b)))\operatorname O(\log(\max(a,b))) 。

Q.E.D.\mathbb {Q.E.D.}

优化·代码部分

现在,我们根据这个函数,写下它的代码。

C++
int gcdt(int a,int b)
{
	if(b==0)//(1)式 
	{
		return a;
	}
	while(!(a&1))//(3)式 
	{
		a>>=1;
	}
	while(!(b&1))//(4)式 
	{
		b>>=1;
	}
	/*
	注意,根据证明,(2)式不会出现,(3)(4)式
	显然不会同时出现 
	*/
	if(a<b)//(5)式 
	{
		return gcdt(a,b-a);
	}
	return gcdt(b,a-b);//(6)式 
}

int gcd2(int a,int b)//真正的函数入口
{
	int ans=1;
	while(!(a&1||b&1))//先行处理(2)式 
	{
		ans<<=1;
		a>>=1;
		b>>=1;
	}
	return ans*gcdt(a,b);//不要忘了乘回去 
}

2024-4-25更新至此\text{\color{white}2024-4-25更新至此}

真的开始:扩展·更相减损法

推导

我们所要解的,是以下的方程:

ax+by=gcd⁡(a,b)ax+by =\gcd(a,b)

我们所要利用的,式刚刚推出来的式子:

gcd⁡(a,b)={(1)a(b=0)(2)2×gcd⁡(a÷2,b÷2)(2∣a且2∣b且b≠0)(3)gcd⁡(a÷2,b)(2∣a且2∤b且b≠0)(4)gcd⁡(a,b÷2)(2∤a且2∣b且b≠0)(5)gcd⁡(b,a)(a<b且2∤a且2∤b且b≠0)(6)gcd⁡(b,a−b)(a≥b且2∤a且2∤b且b≠0)\gcd(a,b)=\begin{cases} (1)a & (b=0) \\ (2)2 \times \gcd(a \div 2,b \div 2) & (2\mid a \text{且} 2\mid b \text{且}b\ne0) \\ (3)\gcd(a \div 2,b) & (2\mid a \text{且} 2\nmid b \text{且}b\ne0) \\ (4)\gcd(a,b \div 2) & (2\nmid a \text{且} 2\mid b \text{且}b\ne0) \\ (5)\gcd(b,a) & (a<b \text{且} 2\nmid a \text{且} 2\nmid b \text{且}b\ne0) \\ (6)\gcd(b,a-b) & (a\ge b \text{且} 2\nmid a \text{且} 2\nmid b \text{且}b\ne0) \\ \end{cases}

为了方便,设 a′,b′a',b' 为进入式子后的 a,ba,b 值。

设 x′,y′x',y' 满足

a′x′+b′y′=gcd(a′,b′)a'x'+b'y'=gcd(a',b')

下面,开始解!方!程!

(1)(1) 式

ax+by=gcd⁡(a,b)=a=a×1+b×0\begin{aligned} ax+by &= \gcd(a,b)\\ &=a\\ &=a \times 1 + b \times 0 \end{aligned}

在其中, x=1,y=0x=1,y=0 显然是一组解。

(2)(2) 式

ax+by=2×gcd⁡(a÷2,b÷2)\begin{aligned} ax+by &= 2 \times\gcd(a \div 2,b \div 2)\\ \end{aligned}

则 a′=a÷2,b′=b÷2a'=a \div 2,b'=b \div 2 。
由于

gcd⁡(a′,b′)=a′x′+b′y′=ax′2+by′2=a×x′2+b×y′2\begin{aligned} \gcd(a',b')&=a'x'+b'y'\\ &=\frac{ax'}{2} + \frac{by'}{2}\\ &=a \times \frac{x'}{2}+ b \times \frac{y'}{2} \end{aligned}

所以

ax+by=2×gcd⁡(a′,b′)=2(a×x′2+b×y′2)=ax′+by′\begin{aligned} ax+by&=2\times\gcd(a',b')\\ &=2(a\times\frac{x'}{2}+b\times\frac{y'}{2})\\ &=ax'+by' \end{aligned}

所以,x=x′,y=y′x=x',y=y' 是一组特解。

(3)(3) 式

ax+by=gcd⁡(a÷2,b)\begin{aligned} ax+by &=\gcd(a \div 2,b)\\ \end{aligned}

则 a′=a÷2,b′=ba'=a \div 2,b'=b 。
由于

gcd⁡(a′,b′)=a′x′+b′y′=ax′2+by′=a×x′2+by′\begin{aligned} \gcd(a',b')&=a'x'+b'y'\\ &=\frac{ax'}{2} + by'\\ &=a \times \frac{x'}{2}+ by' \end{aligned}

所以

ax+by=gcd⁡(a′,b′)=a×x′2+by′\begin{aligned} ax+by&=\gcd(a',b')\\ &=a\times\frac{x'}{2}+by' \end{aligned}

所以,x=x′2,y=y′x=\frac{x'}{2},y=y' 是一组特解。

(4)(4) 式

ax+by=gcd⁡(a,b÷2)\begin{aligned} ax+by &=\gcd(a,b \div 2)\\ \end{aligned}

则 a′=a,b′=b÷2a'=a,b'=b \div 2 。
由于

gcd⁡(a′,b′)=a′x′+b′y′=ax′+by′2=ax′+b×y′2\begin{aligned} \gcd(a',b')&=a'x'+b'y'\\ &=ax' + \frac{by'}{2}\\ &=ax'+b \times \frac{y'}{2} \end{aligned}

所以

ax+by=gcd⁡(a′,b′)=ax′+b×y′2\begin{aligned} ax+by&=\gcd(a',b')\\ &=ax'+b \times \frac{y'}{2} \end{aligned}

所以,x=x′,y=y′2x=x',y=\frac{y'}{2} 是一组特解。

(5)(5) 式

ax+by=gcd⁡(b,a)\begin{aligned} ax+by &=\gcd(b,a)\\ \end{aligned}

则 a′=b,b′=aa'=b,b'=a 。
由于

gcd⁡(a′,b′)=a′x′+b′y′=bx′+ay′=ay′+bx′\begin{aligned} \gcd(a',b')&=a'x'+b'y'\\ &=bx'+ay'\\ &=ay'+bx' \end{aligned}

所以

ax+by=gcd⁡(a′,b′)=ay′+bx′\begin{aligned} ax+by&=\gcd(a',b')\\ &=ay'+bx' \end{aligned}

所以,x=y′,y=x′x=y',y=x' 是一组特解。

(6)(6) 式

ax+by=gcd⁡(b,a−b)\begin{aligned} ax+by &=\gcd(b,a-b)\\ \end{aligned}

则 a′=b,b′=a−ba'=b,b'=a-b 。
由于

gcd⁡(a′,b′)=a′x′+b′y′=bx′+(a−b)y′=ay′+b(x′−y′)\begin{aligned} \gcd(a',b')&=a'x'+b'y'\\ &=bx'+(a-b)y'\\ &=ay'+b(x'-y') \end{aligned}

所以

ax+by=gcd⁡(a′,b′)=ay′+b(x′−y′)\begin{aligned} ax+by&=\gcd(a',b')\\ &=ay'+b(x'-y') \end{aligned}

所以,x=y′,y=x′−y′x=y',y=x'-y' 是一组特解。

漏洞与解决

刚刚的推导看似无懈可击,但对于 (3)(4)(3)(4) 式,有致命的漏洞。

x,yx,y都必须要是整数!

2024-4-26更新至此\text{\color{white}2024-4-26更新至此}

而在 (3)(4)(3)(4) 式中,出现了形如 x′2\frac{x'}{2} 这样的式子。

例如,在 (3)(3) 式中,当 2∣x′2 \mid x' 时, x′2\frac{x'}{2} 是整数,无事发生。

但是 2∤x′2 \nmid x' 时,x′2\frac{x'}{2} 不为整数,算法崩溃了
……吗?

 \text{\color{white} }
 \text{\color{white} }
 \text{\color{white} }
 \text{\color{white} }
 \text{\color{white} }
 \text{\color{white} }
 \text{\color{white} }
 \text{\color{white} }
 \text{\color{white} }
 \text{\color{white} }
 \text{\color{white} }
 \text{\color{white} }
check\text{\color{white}check}
 \text{\color{white} }
 \text{\color{white} }
 \text{\color{white} }

在 (3)(3) 式内,显然

2∣a,2∤b2\mid a,2 \nmid b

设 r1,r2r_1,r_2 满足

a(x′2+r12)+b(y′+r2)=gcd⁡(a,b)a(\frac{x'}{2}+\frac{r_1}{2})+b(y'+r_2)=\gcd(a,b)

因为

a×x′2+by′=gcd⁡(a,b)a\times\frac{x'}{2}+by'=\gcd(a,b)

所以

a×r12+br2=0a\times\frac{r_1}{2}+br_2=0 r2=−a2b×r1r_2=-\frac{a}{2b}\times r_1 r2=−换agcd⁡(a,2b)换2bgcd⁡(a,2b)×r1r_2=-\frac{\text{\color{white}换}\frac{a}{\gcd(a,2b)}\text{\color{white}换}}{\frac{2b}{\gcd(a,2b)}}\times r_1

此时右式中的分式部分已经是最简分数,所以

r1=2bgcd⁡(a,2b)×n=bgcd⁡(a÷2,b)×n\begin{aligned}r_1&=\frac{2b}{\gcd(a,2b)}\times n\\ &=\frac{b}{\gcd(a\div 2,b)}\times n \end{aligned} r2=−agcd⁡(a,2b)×n=−a÷2gcd⁡(a÷2,b)×n\begin{aligned} r_2&=-\frac{a}{\gcd(a,2b)}\times n \\ &=-\frac{a \div 2}{\gcd(a\div 2,b)}\times n \end{aligned}

(其中, nn 是一个整数)

我们想要 x′2+r12\frac{x'}{2}+\frac{r_1}{2} 为整数,所以

r1+x′≡0(mod2)r_1+x'\equiv 0 \pmod 2 bgcd⁡(a÷2,b)×n≡−x′(mod2)\frac{b}{\gcd(a\div 2,b)}\times n\equiv -x' \pmod 2

因为 2∤x′,2∤b,2∤gcd⁡(a÷2,b)2\nmid x',2\nmid b,2\nmid\gcd(a\div 2,b) ,
所以

bgcd⁡(a÷2,b)≡1(mod2)\frac{b}{\gcd(a\div 2,b)}\equiv 1 \pmod 2 −x′≡1(mod2)-x'\equiv 1 \pmod 2

所以

n≡1(mod2)n\equiv1\pmod 2

为了 卡常 方便,设

n=gcd⁡(a÷2,b)n=\gcd(a\div 2,b)

所以

r1=br_1=b r2=−a2r_2=-\frac{a}{2}

那么 (4)(4) 式又如何呢?

因为推导过程和 (3)(3) 式相同,所以过程省略,结果如下:

r1=−b2r_1=-\frac{b}{2} r2=ar_2=a

那么,针对 (3)(4)(3)(4) 式的“女娲补天”——
Q.E.D.\mathbb {Q.E.D.}

至此,公式已成。


代码……卡常?

我们以扩欧模板题之一 P1082为例

依照扩展·更相减损法,在 C++14(GCC9) 情况下,代码如下:

C++
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <iomanip>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
#include <cstdint>
#include <bitset>
#include <set>
#include <unordered_map>
#include <unordered_set>

using namespace std;
#define int long long

pair<int,int> exgcd_prm(int a,int b)
{
	if(b==0)
	{
		return make_pair(1,0);
	}
	if(!(a&1))
	{
		pair<int,int> ans=exgcd_prm(a>>=1,b);
		if(ans.first&1)
		{
			ans.first+=b;
			ans.second-=a;
		}
		ans.first>>=1;
		return ans;
	}
	if(!(b&1))
	{
		pair<int,int> ans=exgcd_prm(a,b>>=1);
		if(ans.second&1)
		{
			ans.first-=b;
			ans.second+=a;
		}
		ans.second>>=1;
		
		return ans;
	}
	if(a<b)
	{
		pair<int,int> ans=exgcd_prm(b,a);
		int temp=ans.first;
		ans.first=ans.second;
		ans.second=temp;
		return ans;
	}
	pair<int,int> ans=exgcd_prm(b,a-b);
	int temp=ans.first;
	ans.first=ans.second;
	ans.second=temp-ans.first;
	return ans;
}
pair<int,int> exprm(int a,int b)
{
	while(!(a&1||b&1))
	{
		a>>=1,b>>=1;
	}
	return exgcd_prm(a,b);
}

signed main()
{
	int a,b;
	scanf("%lld%lld",&a,&b);
	auto s=exprm(a,b);
	printf("%lld\n",(s.first%b+b)%b);
	return 0;
}//十年OI一场空,不开longlong见祖宗

提交如下:你,什么,都看不到!!!哈哈哈哈!\text{\color{white}你,什么,都看不到!!!哈哈哈哈!}但是,运行时间 34ms34ms ,远高于其它大佬(在同语言同环境下)的 33ms33ms 。
那么,卡常,启动!!!

卡常 II

通过用全局变量来代替返回值,可以大大减少常数。
以下的代码可以实现 3333 到 36ms36 ms 的耗时(看人品和评测机心情)

C++
#include <cstdio>

using namespace std;
#define int long long
int ans1,ans2;

void exgcd_prm(int a,int b)
{
	if(b==0)
	{
		ans1=1,ans2=0;return;
	}
	if(!(a&1))
	{
		exgcd_prm(a>>=1,b);
		if(ans1&1)
		{
			ans1+=b;
			ans2-=a;
		}
		ans1>>=1;return;
	}
	if(!(b&1))
	{
		exgcd_prm(a,b>>=1);
		if(ans2&1)
		{
			ans1-=b;
			ans2+=a;
		}
		ans2>>=1;return;
	}
	if(a<b)
	{
		exgcd_prm(b,a);
		int temp=ans1;
		ans1=ans2;
		ans2=temp;return;
	}
	exgcd_prm(b,a-b);
	int temp=ans1;
	ans1=ans2;
	ans2=temp-ans1;
	return;
}
void exprm(int a,int b)
{
	while(!(a&1||b&1))
	{
		a>>=1,b>>=1;
	}
	exgcd_prm(a,b);
}

signed main()
{
	int a,b;
	scanf("%lld%lld",&a,&b);
	exprm(a,b);
	printf("%lld\n",(ans1%b+b)%b);
	return 0;
}

2024-4-30更新至此\text{\color{white}2024-4-30更新至此}

卡常 IIII

我们注意到,(5)(5) 式后面一定递归 (6)(6) 式。
所以,我们可以通过此种方式减少递归。

C++
#include <cstdio>

using namespace std;
#define int long long
int ans1,ans2;

void exgcd_prm(int a,int b)
{
	if(b==0)
	{
		ans1=1,ans2=0;return;
	}
	if(!(a&1))
	{
		exgcd_prm(a>>=1,b);
		if(ans1&1)
		{
			ans1+=b;
			ans2-=a;
		}
		ans1>>=1;return;
	}
	if(!(b&1))
	{
		exgcd_prm(a,b>>=1);
		if(ans2&1)
		{
			ans1-=b;
			ans2+=a;
		}
		ans2>>=1;return;
	}
	if(a<b)
	{
		exgcd_prm(a,b-a);
		int temp=ans2;
		ans1=ans1-temp;return;
	}
	exgcd_prm(b,a-b);
	int temp=ans1;
	ans1=ans2;
	ans2=temp-ans1;
	return;
}
void exprm(int a,int b)
{
	while(!(a&1||b&1))
	{
		a>>=1,b>>=1;
	}
	exgcd_prm(a,b);
}

signed main()
{
	int a,b;
	scanf("%lld%lld",&a,&b);
	exprm(a,b);
	printf("%lld\n",(ans1%b+b)%b);
	return 0;
}

评测记录 is there。你有没有注意到真正的记录在这句话蓝色字体的最后?\text{\color{white}你有没有注意到真正的记录在这句话蓝色字体的最后?}

后记

你好,这是我耗尽了三周来的所有星期天写的,点赞、收藏一下呗。\text{\color{white}你好,这是我耗尽了三周来的所有星期天写的,点赞、收藏一下呗。}

这篇文章是在卡常中诞生的,但最后依旧未能超出 33 ms的瓶颈,我十分伤心。\text{\color{white}这篇文章是在卡常中诞生的,但最后依旧未能超出 33 ms的瓶颈,我十分伤心。}

希望各位大佬可以让它突破瓶颈。\text{\color{white}希望各位大佬可以让它突破瓶颈。}

谢谢观看。\text{\color{black}谢谢观看。}