https://oi-wiki.org/math/bsgs/所以这个应该也是个优化暴力
map<LL, int>mp;
LL BSGS(LL a, LL b, LL p){
LL lim = ceil(sqrt(p));
if(!p % a){
return -1;
}
for(int i = 0; i <= lim; i++){
mp[FMul(b, QPow(a, i, p), p)] = i;
}
LL bp = QPow(a, lim, p);
LL rNow = 1;
for(int i = 1; i <= lim; i++){
rNow = FMul(rNow, bp, p);
if(rNow == b % p){
return i*lim;
}
if(mp[rNow]){
return i*lim - mp[rNow];
} // 等式右边出现过该数
}
return -1;
}
FMul -> 快速乘
QPow -> 快速幂
在这里