[模板]BSGS

最近被数论全家桶搞emo了...

证明就不说了,给一份debug的代码吧...

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll BSGS(ll a,ll b,ll m)
{
	unordered_map<ll,ll> hs;//map会比这个多一个log的复杂度 
	hs.clear();
	ll cur=1,t=sqrt(m)+1;
	for(int B=1;B<=t;B++)
	{ 
		(cur*=a)%=m;
		hs[b*cur%m]=B;
	}
	ll now=cur;
	for(int A=1;A<=t;A++)
	{
		auto it=hs.find(now);
		if(it!=hs.end()) return A*t-it->second;
		(now*=cur)%=m;
	}
	return -1;
}
ll m,a,b; 
int main()
{
	ios::sync_with_stdio(0);
	cin>>m>>a>>b;
	int p=BSGS(a,b,m);
	if(p==-1) cout<<"no solution";
	else cout<<p;
	return 0;
}
posted @ 2023-08-22 16:03  inlinexhx  阅读(22)  评论(0编辑  收藏  举报