$x^y \equiv y^x \pmod{p}$
For a prime p, define f(p) = sum_(1 <= x < y < p, x^y equiv y^x (mod p)) (x + y). Compute sum_(p <= 10^6, p prime) f(p) (mod 10^9 + 7).
Problem Statement
This archive keeps the full statement, math, and original media on the page.
The positive integral solutions of the equation \(x^y = y^x\) are \((2, 4)\), \((4, 2)\) and \((k, k)\) for all \(k > 0\).
For a given positive integer \(n\), let \(f(n)\) be the number of integral values \(0 < x, y \leq n^2 - n\) such that \[x^y \equiv y^x \pmod {n}\] For example, \(f(5) = 104\) and \(f(97) = 1614336\).
Let \(S(M, N) = \sum f(p)\) where the sum is taken over all primes \(p\) satisfying \(M \leq p \leq N\).
You are given \(S(1, 10^2) = 7381000\) and \(S(1, 10^5) \equiv 701331986 \pmod {993353399}\).
Find \(S(10^{16}, 10^{16} + 10^6)\). Give your answer modulo \(993353399\).
Problem 801:
Mathematical Foundation
Theorem 1 (Discrete Logarithm Reduction). Let be an odd prime and a primitive root modulo . For , write and where and . Then
Proof. Since is a primitive root, the map is a bijection . We have and , so if and only if , which holds if and only if by the order of .
Lemma 1 (Collision via Hash Function). Define by
Then for both in the domain of , we have if and only if .
Proof. The condition can be rewritten (when and ) as , i.e., .
Lemma 2 (Handling ). When , the inverse does not exist. For such , the congruence must be checked directly: it holds iff , which requires case-by-case enumeration grouped by the residue class of .
Proof. This follows from the solvability condition for linear congruences: has solutions iff , but here are both given. The condition is simply .
Editorial
For a prime , let where the sum is over all pairs with such that . Find . We iterate over each prime p in primes. We then compute discrete logarithm table via BSGS or direct power. Finally, build hash map: key = h(x), value = list of x.
Pseudocode
for each prime p in primes
Compute discrete logarithm table via BSGS or direct power
Build hash map: key = h(x), value = list of x
Sum over collision pairs
for each bucket B in buckets
sum of (x+y) over pairs = (|B|-1)*S
Handle gcd(x, p-1) > 1 cases separately (direct check)
Complexity Analysis
- Time: For each prime , computing the discrete logarithm table takes . Building the hash map and counting collisions is . Summing over all primes : . With BSGS for discrete logs: per prime, giving total.
- Space: for the sieve and the largest discrete log table.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
long long power(long long b, long long e, long long m) {
long long r = 1; b %= m;
while (e > 0) { if (e & 1) r = r * b % m; b = b * b % m; e >>= 1; }
return r;
}
int main() {
const int LIM = 200;
vector<bool> sieve(LIM, true);
sieve[0] = sieve[1] = false;
for (int i = 2; i * i < LIM; i++)
if (sieve[i]) for (int j = i*i; j < LIM; j += i) sieve[j] = false;
long long ans = 0;
for (int p = 2; p < LIM; p++) {
if (!sieve[p]) continue;
for (int x = 1; x < p; x++)
for (int y = x+1; y < p; y++)
if (power(x, y, p) == power(y, x, p))
ans = (ans + x + y) % MOD;
}
cout << ans << endl;
return 0;
}
"""
Problem 801: x^y ≡ y^x (mod p)
For a prime $p$, let $f(p) = \sum (x+y)$ where the sum is over all pairs $(x,y)$ with $1 \le x < y < p$ such that $x^y \equiv y^x \pmod{p}$. Find $\sum_{p \le 10^6} f(p) \bmod 1\,000\,000\,007$.
"""
def solve():
MOD = 10**9 + 7
def f_small(p):
"""Brute force for small primes."""
total = 0
for x in range(1, p):
for y in range(x+1, p):
if pow(x, y, p) == pow(y, x, p):
total += x + y
return total
# Demo for small primes
result = 0
sieve = [True] * 200
sieve[0] = sieve[1] = False
for i in range(2, 15):
if sieve[i]:
for j in range(i*i, 200, i):
sieve[j] = False
for p in range(2, 200):
if sieve[p]:
result = (result + f_small(p)) % MOD
return result
answer = solve()
print(answer)