?、BSGS(Baby Step Giant Step、小步大步算法)(POJ2417)(没写哈希表):
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <unordered_map>
typedef long long LL;
std::unordered_map<LL,LL> mp;
inline LL qpow(LL x,LL y,LL p){
LL ret=1,tt=x%p;
while(y){
if(y&1) ret=ret*tt%p;
tt=tt*tt%p;
y>>=1;
}
return ret;
}
inline LL bsgs(LL a,LL b,LL p){
mp.clear();
b%=p;
LL siz=(LL)ceil(sqrt(1.0*p));
LL temp=b;
for(int i=0;i<=siz;i++){
mp[temp]=i;
temp=temp*a%p;
}
a=qpow(a,siz,p);
if(a==0) return b==0?1:-1;
temp=1;
for(int i=0;i<=siz;i++){
LL j=(mp.find(temp)==mp.end()?-1:mp[temp]);
if(j>=0&&i*siz-j>=0) return i*siz-j;
temp=temp*a%p;
}
return -1;
}
int main(){
LL a,b,p;
while(~scanf("%lld%lld%lld",&p,&a,&b)){
LL ans=bsgs(a,b,p);
if(ans<0) printf("no solution\n");
else printf("%lld\n",ans);
}
return 0;
}