Codeforces 919E
求有多少个$n \in [1,x]$,满足
$$na^n \equiv b \pmod p$$
$$2 \leq p \leq 10^6 + 3,1 \leq a,b <p,1 \leq n \leq 10^{12}$$
保证$p$为质数。
将$b$移到等式左边,再将$n \in [1,p]$时的$b^{-1}n$存到哈希表中,枚举$a^1$到$a^{p-1}$,在哈希表中查有没有值是它的逆元,查到了后用CRT计算答案。
const int MAXN = 1000000 + 10; int MOD; IL int add(int a, int b) { a += b; return a >= MOD ? a - MOD : a; } IL int mul(int a, int b) { return 1LL * a * b % MOD; } IL int FastPow(int a, int p) { int result = 1; for (int base = a; p; p >>= 1, base = mul(base, base)) if (p & 1) result = mul(result, base); return result; } int a[MAXN]; std::unordered_multimap < int, int > mp; int main() { int a, b; LL x; std::cin >> a >> b >> MOD >> x; int base = FastPow(b, MOD - 2); For(i, 1, mymin(x, 1LL * (MOD - 1))) mp.insert(std::make_pair(mul(base, i), i)); int cur = 1, inv = FastPow(MOD - 1, MOD - 2); LL answer = 0; For(i, 1, mymin(1LL * (MOD - 1), x)) { cur = mul(cur, a); auto cnt = mp.equal_range(FastPow(cur, MOD - 2)); for (auto it = cnt.F; it != cnt.S; ++ it) { LL tot= (1LL * it->S * inv * (MOD - 1) + 1LL * (i == MOD - 1 ? 0 : i) * MOD) % (1LL * MOD * (MOD - 1)); answer += x >= tot ? (x - tot) / (1LL * MOD * (MOD - 1)) + 1 : 0; } } std::cout << answer << '\n'; return 0; }