算法引入
在学习数论时,我们会学习一种算法,叫做 扩展欧几里得算法(以下简称 扩欧 ),
其思路可以参考这篇博客,时间复杂度为 O(log(max(a,b))
不过,我们还可以找到另一种算法来计算EXGCD。
是谁呢? 更相减损法(Phase reduction method,PRM)。
基础更相减损法
本体
式子如下(想看证明的看这):
gcd(a,b)=⎩⎨⎧gcd(b,a)gcd(b,a−b)a(a<b)(a≥b且b=0)(b=0)
举个例子 求 114 和 514 的最大公因数:
| a | b |
|---|
| 114 | 514 |
| 514 | 114 |
| 114 | 514−114=400 |
| 400 | 114 |
| 114 | 400−114=286 |
| 286 | 114 |
| 114 | 286−114=172 |
| 172 | 114 |
| 114 | 172−114=58 |
| 58 | 114−58=56 |
| 56 | 58−56=2 |
| ⋯ | ⋯ |
| 4 | 2 |
| 2 | 2 |
| 2 | 0 |
所以,114 和 514 的最大公因数是 2 。
到省略号部分时,我们可以发现,当 a , b 差距过大时,该算法效率会大幅降低。如果构造 {n,1} 这样的数据,复杂度会退化回 O(n) 。
所以, 需要优化 。
优化·式子
显然, 当 a , b 都是 2 的倍数时
gcd(a,b)=2×gcd(a÷2,b÷2)
否则,当 a 是 2 的倍数时
gcd(a,b)=gcd(a÷2,b)
否则,当 b 是 2 的倍数时
gcd(a,b)=gcd(a,b÷2)
我们,就可以就此,利用移位运算(不会见此)除以 2 优化。
则最终式子为(其中 x∣y 表示 y 是 x 的因数,x∤y 表示 y 不是 x 的因数):
gcd(a,b)=⎩⎨⎧(1)a(2)2×gcd(a÷2,b÷2)(3)gcd(a÷2,b)(4)gcd(a,b÷2)(5)gcd(b,a)(6)gcd(b,a−b)(b=0)(2∣a且2∣b且b=0)(2∣a且2∤b且b=0)(2∤a且2∣b且b=0)(a<b且2∤a且2∤b且b=0)(a≥b且2∤a且2∤b且b=0)
2024-4-21更新至此
他喵的,写了后 面时间复杂度,在新网页上保存上了。
老网页没刷新,又在老网页上保存了, 后面的全没了……没了o(╥﹏╥)o
优化·时间复杂度
先说结论:稳定为 O(log(max(a,b))) 。
为什么呢?
证明如下:
命题 1
内容:当递归不在 (2) 式时,以后也不可能进入 (2) 式。
证明:对于 (1)(5) 式,显然。
对于 (3) 式,已经 2∤b ,在运行完之后依旧如此,不会进入 (2) 式。
对于 (4) 式,同上。
对于 (6) 式,因为 2∤a , 2∤b 。
即
a≡1(mod2)
b≡1(mod2)
所以
a−b≡0(mod2)
即 2∣(a−b) 。
因为 2∤b , 2∣(a−b) ,
所以,在 (6) 式之后的下一个式子,一定是 (4)(5) 式。
综上,命题成立。
命题 2
内容: (1) 式只能出现一次。
证明:略。
命题 3
内容:(5)(6) 式不能连续 4 次出现。
证明:对于 (5) 式,显然。
对于 (6) 式,见命题 1(6) ,成立。
对于 (5)(6) 式一起,由命题 1(6) ,(6)式以后为 (4)(5) 式。
在此时 (5) 式以后,只能为 (4) 式。
综上,命题成立。
-------命题结束-----
现在开始分析。
(1)(5)(6) 式不会连续出现,且附着在 (3)(4) 式后,可以忽略。
(2)(3)(4) 式最多运行 log 级别的次数。
所以,时间复杂度为稳定的 O(log(max(a,b))) 。
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更新至此
真的开始:扩展·更相减损法
推导
我们所要解的,是以下的方程:
ax+by=gcd(a,b)
我们所要利用的,式刚刚推出来的式子:
gcd(a,b)=⎩⎨⎧(1)a(2)2×gcd(a÷2,b÷2)(3)gcd(a÷2,b)(4)gcd(a,b÷2)(5)gcd(b,a)(6)gcd(b,a−b)(b=0)(2∣a且2∣b且b=0)(2∣a且2∤b且b=0)(2∤a且2∣b且b=0)(a<b且2∤a且2∤b且b=0)(a≥b且2∤a且2∤b且b=0)
为了方便,设 a′,b′ 为进入式子后的 a,b 值。
设 x′,y′ 满足
a′x′+b′y′=gcd(a′,b′)
下面,开始解!方!程!
(1) 式
ax+by=gcd(a,b)=a=a×1+b×0
在其中, x=1,y=0 显然是一组解。
(2) 式
ax+by=2×gcd(a÷2,b÷2)
则 a′=a÷2,b′=b÷2 。
由于
gcd(a′,b′)=a′x′+b′y′=2ax′+2by′=a×2x′+b×2y′
所以
ax+by=2×gcd(a′,b′)=2(a×2x′+b×2y′)=ax′+by′
所以,x=x′,y=y′ 是一组特解。
(3) 式
ax+by=gcd(a÷2,b)
则 a′=a÷2,b′=b 。
由于
gcd(a′,b′)=a′x′+b′y′=2ax′+by′=a×2x′+by′
所以
ax+by=gcd(a′,b′)=a×2x′+by′
所以,x=2x′,y=y′ 是一组特解。
(4) 式
ax+by=gcd(a,b÷2)
则 a′=a,b′=b÷2 。
由于
gcd(a′,b′)=a′x′+b′y′=ax′+2by′=ax′+b×2y′
所以
ax+by=gcd(a′,b′)=ax′+b×2y′
所以,x=x′,y=2y′ 是一组特解。
(5) 式
ax+by=gcd(b,a)
则 a′=b,b′=a 。
由于
gcd(a′,b′)=a′x′+b′y′=bx′+ay′=ay′+bx′
所以
ax+by=gcd(a′,b′)=ay′+bx′
所以,x=y′,y=x′ 是一组特解。
(6) 式
ax+by=gcd(b,a−b)
则 a′=b,b′=a−b 。
由于
gcd(a′,b′)=a′x′+b′y′=bx′+(a−b)y′=ay′+b(x′−y′)
所以
ax+by=gcd(a′,b′)=ay′+b(x′−y′)
所以,x=y′,y=x′−y′ 是一组特解。
漏洞与解决
刚刚的推导看似无懈可击,但对于 (3)(4) 式,有致命的漏洞。
x,y都必须要是整数!
2024-4-26更新至此
而在 (3)(4) 式中,出现了形如 2x′ 这样的式子。
例如,在 (3) 式中,当 2∣x′ 时, 2x′ 是整数,无事发生。
但是 2∤x′ 时,2x′ 不为整数,算法崩溃了
……吗?
在 (3) 式内,显然
2∣a,2∤b
设 r1,r2 满足
a(2x′+2r1)+b(y′+r2)=gcd(a,b)
因为
a×2x′+by′=gcd(a,b)
所以
a×2r1+br2=0
r2=−2ba×r1
r2=−gcd(a,2b)2b换gcd(a,2b)a换×r1
此时右式中的分式部分已经是最简分数,所以
r1=gcd(a,2b)2b×n=gcd(a÷2,b)b×n
r2=−gcd(a,2b)a×n=−gcd(a÷2,b)a÷2×n
(其中, n 是一个整数)
我们想要 2x′+2r1 为整数,所以
r1+x′≡0(mod2)
gcd(a÷2,b)b×n≡−x′(mod2)
因为 2∤x′,2∤b,2∤gcd(a÷2,b) ,
所以
gcd(a÷2,b)b≡1(mod2)
−x′≡1(mod2)
所以
n≡1(mod2)
为了 卡常 方便,设
n=gcd(a÷2,b)
所以
r1=b
r2=−2a
那么 (4) 式又如何呢?
因为推导过程和 (3) 式相同,所以过程省略,结果如下:
r1=−2b
r2=a
那么,针对 (3)(4) 式的“女娲补天”——
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见祖宗
提交如下:你,什么,都看不到!!!哈哈哈哈!但是,运行时间 34ms ,远高于其它大佬(在同语言同环境下)的 33ms 。
那么,卡常,启动!!!
卡常 I
通过用全局变量来代替返回值,可以大大减少常数。
以下的代码可以实现 33 到 36ms 的耗时(看人品和评测机心情)
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更新至此
卡常 II
我们注意到,(5) 式后面一定递归 (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。你有没有注意到真正的记录在这句话蓝色字体的最后?
后记
你好,这是我耗尽了三周来的所有星期天写的,点赞、收藏一下呗。
这篇文章是在卡常中诞生的,但最后依旧未能超出 33 ms的瓶颈,我十分伤心。
希望各位大佬可以让它突破瓶颈。
谢谢观看。