离散对数
求最小的r使得a^r=b(mod p)成立
如果在没有p的时候,r=log(a,b),是个对数
因为有mod,所以叫做离散对数。
如何求离散对数
第一种方法是暴力找r,不多讲了。
下面的方法不怎么讲证明,只管照着步骤弄就行了,以后补证明吧。
BSGS
Baby step Giant step,中文名叫做大步小步法,和它的过程很有关联。
如果P是素数:
因为如果有解,则r一定小于p;
令m=sqrt(p),令r=i*m+j(0<=i<=m,0<=j<=m)
则a^(i*m) * a^j=b(mod p)
分别处理a^j以及a^(i*m),用逆元进行查找。
所以时间复杂度是O(sqrt(p)*logsqrt(m))的。
扩展BSGS
如果P不是素数:
在求逆元的时候可能会出现问题,因此 需要先进性一些处理:
可以直接看代码,反正相当于把代码复述了一遍。
扩展BSGS代码(BSGS也在里面了)
BZOJ 2480: Spoj3105 Mod
#include<queue>
#include<cmath>
#include<cctype>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=350000;
int a,p,b,ans;
int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}
int exgcd(int a,int b,int &