普通 BSGS 可以通过本题。。。
代码:
#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <unordered_map>
#define int unsigned long long
typedef __int128 _;
struct hash {
static int nxt(int x) {
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
int operator()(int x) const {
static const int rnd =
std::chrono::steady_clock::now().time_since_epoch().count();
return nxt(x + rnd);
}
};
int pw(int x, int y, int p) {
int r = 1;
for (; y; y >>= 1, x = x * x % p)
if (y & 1) r = r * x % p;
return r;
}
int bsgs(int a, int b, int p) {
std::unordered_map<int, int, hash> mp;
int m = ceil(sqrtl(p)), t = 1;
for (int i = 0; i < m; i++, (t *= a) %= p) mp[t * b % p] = i;
for (int i = 1, j = t; i <= m; i++, (j *= t) %= p)
if (mp.count(j)) return i * m - mp[j];
return -1ull;
}
int a, p, b;
signed main() {
while (scanf("%llu%llu%llu", &a, &p, &b) && (a || p || b)) {
int ans = bsgs(a, b, p);
if (ans == -1ull || pw(a, ans, p) != b)
puts("No Solution");
else
printf("%llu\n", ans);
}
}