BSGS模板题:
#include <iostream> #include <cstring> #include <string> #include <map> #include <cmath> #include <cstdio> using namespace std; typedef long long ll; ll b, l, n, p; ll a; ll quick_mod(ll a, ll b, ll n) { ll ans = 1; while(b) { if(b&1) { ans = ans*a%n; } a = a*a % n; b >>= 1; } return ans%n; } ll anss; bool BSGS() { map<ll, ll> mp; ll m = ceil(sqrt(p+0.5)); ll cns = 1; cns = b % p; for (int j = 1; j <= m; j++) { cns = cns * a % p; mp[cns] = j; } ll cnss = quick_mod(a, m, p); ll ress = 1; for (int i = 1; i <= m; i++) { ress = ress * cnss % p; if(mp[ress]) { anss = i * m - mp[ress]; return true; } } return false; } int main() { ios::sync_with_stdio(false); while(cin >> p >> a >> b) { bool flag = BSGS(); if(!flag) { cout << "no solution" << endl; } else { cout << (anss % p + p) % p << endl; } } }