UVA10225 Discrete Logging
题意
BSGS 。
思路
BSGS 。
代码
ll a,b,mod;
ll block;
unordered_map<ll,ll>m;
ll bsgs(ll x,ll y){
// if(y==1)return 0;
ll bas=1;
for(ll i=0;i<block;i++){
m[bas*y%mod]=i;
bas=bas*x%mod;
}
x=bas;
bas=1;
if(!x)return y==0?1:-1;
for(ll i=1;i<=block;i++){
bas=bas*x%mod;
if(m.count(bas)){
ll res=i*block-m[bas];
if(res>=0)return res;
}
}
return -1;
}
void solve(){
//reading had finished in the main fuc.
m.clear();
block=sqrt(mod);
if(b==1){
cout<<0<<endl;
return ;
}
ll res=bsgs(a,b);
if(res==-1)cout<<"no solution"<<endl;
else cout<<res<<endl;
return ;
}