#include<iostream>
#include <unordered_map>
#include<cmath>
using namespace std;
#define int long long
int bsgs(int a,int b,int p)
{
if(1%p==b%p) return 0;
unordered_map<int,int>mp;
int k=sqrt(p)+1;
int ak=1;
for(int i=0,j=b;i<k;i++)
{
mp[j]=i;
ak=ak*a%p;
j=j*a%p;
}
for(int i=1,j=ak%p;i<=k;i++)
{
if(mp.count(j)) return i*k-mp[j];
j=j*ak%p;
}
return -1;
}
signed main()
{
int a,p,b;
while( cin>>a>>p>>b,a||p||b)
{
int res=bsgs(a,b,p);
if(res==-1) cout<<"No Solution"<<endl;
else cout<<res<<endl;
}
}
BSGS
最新推荐文章于 2022-04-30 17:17:42 发布