扩展·更相减损法在数论中的应用
watermouthhang · · 算法·理论
算法引入
在学习数论时,我们会学习一种算法,叫做 扩展欧几里得算法(以下简称 扩欧 ),
其思路可以参考这篇博客,时间复杂度为
不过,我们还可以找到另一种算法来计算EXGCD。
是谁呢? 更相减损法(Phase reduction method,PRM)。
基础更相减损法
本体
式子如下(想看证明的看这):
举个例子 求
所以,
到省略号部分时,我们可以发现,当
所以,
优化·式子
显然, 当
否则,当
否则,当
我们,就可以就此,利用移位运算(不会见此)除以
则最终式子为(其中
优化·时间复杂度
先说结论:稳定为
为什么呢?
证明如下:
命题 1
内容:当递归不在
证明:对于
对于
对于
对于
即
所以
即
因为
所以,在
综上,命题成立。
命题 2
内容:
证明:略。
命题 3
内容:
证明:对于
对于
对于
在此时
综上,命题成立。
-------命题结束-----
现在开始分析。
真的开始:扩展·更相减损法
推导
我们所要解的,是以下的方程:
我们所要利用的,式刚刚推出来的式子:
为了方便,设
设
下面,开始解!方!程!
(1) 式
在其中,
(2) 式
则
由于
所以
所以,
(3) 式
则
由于
所以
所以,
(4) 式
则
由于
所以
所以,
(5) 式
则
由于
所以
所以,
(6) 式
则
由于
所以
所以,
漏洞与解决
刚刚的推导看似无懈可击,但对于
而在
例如,在
但是
……吗?
\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} }
\text{\color{white}check}
\text{\color{white} }
\text{\color{white} }
\text{\color{white} }
在
设
因为
所以
此时右式中的分式部分已经是最简分数,所以
(其中,
我们想要
因为
所以
所以
为了 卡常 方便,设
所以
那么
因为推导过程和
那么,针对
至此,公式已成。
代码……卡常?
我们以扩欧模板题之一 P1082为例
依照扩展·更相减损法,在 C++14(GCC9) 情况下,代码如下:
#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见祖宗
提交如下:
那么,卡常,启动!!!
卡常 I
通过用全局变量来代替返回值,可以大大减少常数。
以下的代码可以实现
#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;
}
卡常 II
我们注意到,
所以,我们可以通过此种方式减少递归。
#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。[
后记
[
[
[