#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long ll;
ll qpow(ll a, ll b, ll p) {
ll res = 1;
while (b) {
if (b & 1) {
res = (res * a) % p;
}
a = (a * a) % p;
b >>= 1;
}
return res;
}
ll bsgs(ll a, ll b, ll p) {
unordered_map<ll, ll> mp;
ll m = sqrt(p) + 1, val = (b * a) % p;
for (int j = 1; j <= m; j++) {
mp[val] = j;
val = (val * a) % p;
}
ll am = qpow(a, m, p), key = am;
for (int i = 1; i <= m; i++) {
if (mp[key]) {
return i * m - mp[key];
}
key = (key * am) % p;
}
return -1;
}
int main() {
ll p, b, n;
cin >> p >> b >> n;
ll ans = bsgs(b, n, p);
if (ans == -1) cout << "no solution" << endl;
else cout << ans << endl;
return 0;
}
模板——BSGS
最新推荐文章于 2022-03-07 13:46:01 发布