首页 > 其他分享 >BSGS&exBSGS

BSGS&exBSGS

时间:2023-01-15 22:22:05浏览次数:61  
标签:return exBSGS sqrt mod BSGS ll equiv

BSGS

baby(boy) step giant(girl) step算法,用于处理\(a^x\equiv b(mod\ p)\),给\(a,b,p(a,p互质)\)求所有的 \(x\) 的问题

本质上有一点点像二分搜索的意味在里面

算法流程

​ 有\(a^x \equiv b(mod\ p)\)

​ 令\(x = A \lceil \sqrt p \rceil - B\),其中\(0\leq A,B \leq \lceil\sqrt p \rceil\)

​ \(\Rightarrow a^{A\lceil \sqrt p \rceil - B} \equiv b(mod p)\)

​ \(\Rightarrow a^{A\lceil \sqrt p \rceil} \equiv b \times a^B(mod p)\)

​ 我们可以把所有的\(b\times a^i,i\in [1,\lceil \sqrt p\rceil]\) 存在一个映射数据结构(hash/map/unordered_map)上

​ 枚举A(\(1\rightarrow n\)),在映射数据结构上查找是否含有\(a^{A\lceil\sqrt p \rceil}\),

时间复杂度

​ 显然地,复杂度为\(\Theta(\sqrt p)\),(因为\(A,B\leq \sqrt p\) )

​ 如果使用map/unordered_map则为\(\Theta(log_p \sqrt p )\)

注:建议做UVA10225,洛谷P3846数据有亿点水

警戒点

  • \(\sqrt p\)一定要上取整
  • \(b\times a^i\) ,i从0->n!
  • \(a^i\) ,i从1->n!

代码

typedef long long ll;
map<ll,ll>  Map;
ll BSGS(ll a,ll b,ll p){
	Map.clear();
	ll t = ceil(sqrt(p));//必须加上ceil
	ll k = 1;
	Map[b%p] = 0;
	for(ll i = 1; i <= t; ++i)Map[(k = k * a % p) * b % p] = i;//计算b*a^i
	a = 1;
	for(ll i = 1; i <= t; ++i){
		a = a * k % p;
		if(Map.count(a))//在映射数组中找a^i
			return i*t-Map[a];
	}
	return -1;
}
int main(){
	ll p,b,n,ans;
	while(~scanf("%lld%lld%lld",&p,&b,&n))
		if((ans = BSGS(b,n,p)) != -1)printf("%lld\n",ans);
		else puts("no solution");
	return 0;
}

exBSGS

​ exBSGS \(a^x\equiv b(mod\ p)\),给\(a,b,p\)\((b,p\)不互质\()\)求所有的 \(x\) 的问题

​ 此时\(a\times a^{x-1} \equiv b(mod\ p)\),令\(d = gcd(a,p)\)

左右同除\(d\)

​ \(\therefore \frac{a}{d}\times a^{x-1} \equiv \frac{b}{d}(mod\ gcd(a,p))\)

代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<ll,ll>  Map;
ll BSGS(ll a,ll b,ll p){
	Map.clear();
	ll t = ceil(sqrt(p));
	ll k = 1;
	Map[b%p] = 0;
	for(ll i = 1; i <= t; ++i)Map[(k = k * a % p) * b % p] = i;
	a = 1;
	for(ll i = 1; i <= t; ++i){
		a = a * k % p;
		if(Map.count(a))
			return i*t-Map[a];
	}
	return -1;
}
ll exgcd(ll a,ll b,ll &x,ll &y){
	if(!b){x = 1;y = 0;return a;}
	ll d = exgcd(b,a%b,x,y);
	ll t = x;
	x = y;
	y = t - (a/b) * y;
	return d;
}
ll inv(ll a,ll b){
	ll x,y;
	exgcd(a,b,x,y);
	return (x % b + b) % b;
}
ll ksm(ll x,ll k,ll p){
	ll res = 0,a = k % p;
	while(k){
		if(k&1)res = res * a % p;
		a = a * a % p;
		k >>= 1;
	} 
}
ll gcd(ll x,ll y){return y ? gcd(y,x%y) : x;}
ll x = 1;
ll exBSGS(ll a,ll b,ll p){
	ll d = gcd(a,p);
	if(b == x)return 0;
	if(d == 1){
		return BSGS(a,b*inv(x,p) % p,p);
	}
	if(b % d != 0)return -1;
	else{
		x = a/d * x % (p/d);
		ll res = exBSGS(a,b/d,p/d); 
		if(res == -1)return -1;
		if(res == -2)return -2;
		return res + 1;
	}
}
ll a,p,b;
int main(){
	while(~scanf("%lld%lld%lld",&a,&p,&b)){
		x = 1;
		if(a == 0 && b == 0 && p == 0)break;
		a%=p;b%=p;
		if(b==1||p==1){
			puts("0");continue;
		}
		ll res = exBSGS(a%p,b%p,p);
		if(res == -1)puts("No Solution");
		else printf("%lld\n",res);
	}
} 

标签:return,exBSGS,sqrt,mod,BSGS,ll,equiv
From: https://www.cnblogs.com/rickylin/p/17054359.html

相关文章

  • 大步小步算法(BSGS)
    BSGS是解决\(a^{l}\equivb(\modp)\)已知\(a\)、\(b\)、\(p\)的情况下求最小的非负整数\(l\)的算法。设$m=\left\lceil\sqrt{p}\right\rceil$,\(l=x\timesm-y(0\l......
  • BSGS
    BSGS(大步小步法)BSGS可以在\(O(\sqrt{p})\)的时间内求解满足\(a^x\equivb\pmodp\)的\(x\),其中\(a\perpp,x>0\)。实现原理:尝试进行和整除分块异曲同工的......
  • Shank's Baby-Step-Giant_Step Algorithm(BSGS)
    解模方程(\(n\)为素数)\[a^x\equivb(\bmodn)\]因为欧拉定理\(a^{\phi(n)}\equiv1(\bmodn)\)(\(n\)为素数)。有\[0\lex\len-1\]设\(m=\sqrt{n+......
  • BSGS学习笔记
    对于一个式子\(\quad\quadat\equivb(mod\p)\)我们可以用扩展欧几里得算法求解而对于这个式子\(\quad\quada^t\equivb(mod\p),(a,p)=1\)我们就要用\(BSGS\)求......
  • BSGS算法学习小记(大步小步算法)
    简介先看一个式子xy≡z(modp),z是质数现在只知道x和z,要求y。大步小步算法(BSGS,BabyStepsGiantSteps)就是解决这个问题。算法流程暴搜的枚举范围根据费马小定理:xp−1≡1。......
  • 离散对数&BSGS学习笔记
    离散对数定义求\(k\)使得\(a^k\equivn\pmodp\),称\(n\)在模\(p\)意义下以\(a\)为底的对数是\(k\)。如何求离散对数BSGS(BabyStep,GiantStep)大步小步算......
  • 3125. 扩展BSGS
    题目链接3125.扩展BSGS给定整数\(a,p,b\)。求满足\(a^x≡b\pmodp\)的最小非负整数\(x\)。输入格式每个测试文件中最多包含\(100\)组测试数据。每组数据中,每......
  • bsgs(扩展还没补)
    bsgs,北上广深,拔山盖世,蓝超巨星(bluesupergiantstar)。大概是\(O(\sqrtn)\)求解模意义下离散对数的一个算法。经典的平衡复杂度思想。离散对数,也就是长这样的一个东西:\[......
  • BSGS exBSGS 详解(大步小步算法)
    我放弃挣扎了至少除了这个点,我的BSGSexBSGS都是可以过的,我就姑且当它没有问题了!BSGS:BigStepGiantStep额其实我也不懂这个算法和名字有什么关系,倒是有点根号分......
  • [学习笔记]BSGS
    $\operatorname{BSGS}$,也即$Baby\;step\;Giant\;step$大步小步算法,可以在$\Theta(\sqrt{p})$的时间内求解$$a^x\equivb\pmod{p}$$的问题,其中$a,p$互质(也即$a......