All Euler problems
Project Euler

$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).

Source sync Apr 19, 2026
Problem #0801
Level Level 28
Solved By 337
Languages C++, Python
Answer 638129754
Length 236 words
number_theorymodular_arithmeticbrute_force

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: xyyx(modp)x^y \equiv y^x \pmod{p}

Mathematical Foundation

Theorem 1 (Discrete Logarithm Reduction). Let pp be an odd prime and gg a primitive root modulo pp. For x,y{1,,p1}x, y \in \{1, \ldots, p-1\}, write x=gax = g^a and y=gby = g^b where a=indg(x)a = \operatorname{ind}_g(x) and b=indg(y)b = \operatorname{ind}_g(y). Then

xyyx(modp)    aybx(modp1).x^y \equiv y^x \pmod{p} \iff a \cdot y \equiv b \cdot x \pmod{p-1}.

Proof. Since gg is a primitive root, the map kgkk \mapsto g^k is a bijection Z/(p1)Z(Z/pZ)\mathbb{Z}/(p-1)\mathbb{Z} \to (\mathbb{Z}/p\mathbb{Z})^*. We have xy=gayx^y = g^{ay} and yx=gbxy^x = g^{bx}, so xyyx(modp)x^y \equiv y^x \pmod{p} if and only if gaygbx(modp)g^{ay} \equiv g^{bx} \pmod{p}, which holds if and only if aybx(modp1)ay \equiv bx \pmod{p-1} by the order of gg. \square

Lemma 1 (Collision via Hash Function). Define h:{x{1,,p1}:gcd(x,p1)=1}Z/(p1)Zh : \{x \in \{1, \ldots, p-1\} : \gcd(x, p-1) = 1\} \to \mathbb{Z}/(p-1)\mathbb{Z} by

h(x)=indg(x)x1(modp1).h(x) = \operatorname{ind}_g(x) \cdot x^{-1} \pmod{p-1}.

Then for x,yx, y both in the domain of hh, we have xyyx(modp)x^y \equiv y^x \pmod{p} if and only if h(x)=h(y)h(x) = h(y).

Proof. The condition aybx(modp1)ay \equiv bx \pmod{p-1} can be rewritten (when gcd(x,p1)=1\gcd(x, p-1) = 1 and gcd(y,p1)=1\gcd(y, p-1) = 1) as ax1by1(modp1)a \cdot x^{-1} \equiv b \cdot y^{-1} \pmod{p-1}, i.e., h(x)=h(y)h(x) = h(y). \square

Lemma 2 (Handling gcd(x,p1)>1\gcd(x, p-1) > 1). When gcd(x,p1)>1\gcd(x, p-1) > 1, the inverse x1(modp1)x^{-1} \pmod{p-1} does not exist. For such xx, the congruence aybx(modp1)ay \equiv bx \pmod{p-1} must be checked directly: it holds iff gcd(x,p1)(aybx)\gcd(x, p-1) \mid (ay - bx), which requires case-by-case enumeration grouped by the residue class of a/gcd(a,p1)a/\gcd(a, p-1).

Proof. This follows from the solvability condition for linear congruences: aybx(modp1)ay \equiv bx \pmod{p-1} has solutions iff gcd(y,p1)bxay\gcd(y, p-1) \mid bx - ay, but here x,yx, y are both given. The condition is simply p1aybxp - 1 \mid ay - bx. \square

Editorial

For a prime pp, let f(p)=(x+y)f(p) = \sum (x+y) where the sum is over all pairs (x,y)(x,y) with 1x<y<p1 \le x < y < p such that xyyx(modp)x^y \equiv y^x \pmod{p}. Find p106f(p)mod1000000007\sum_{p \le 10^6} f(p) \bmod 1\,000\,000\,007. 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 pp, computing the discrete logarithm table takes O(p)O(p). Building the hash map and counting collisions is O(p)O(p). Summing over all primes pNp \le N: O ⁣(pNp)=O(N2/lnN)O\!\left(\sum_{p \le N} p\right) = O(N^2 / \ln N). With BSGS for discrete logs: O(plogp)O(\sqrt{p} \log p) per prime, giving O(N3/2/lnN)O(N^{3/2}/\ln N) total.
  • Space: O(N)O(N) for the sieve and the largest discrete log table.

Answer

638129754\boxed{638129754}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_801/solution.cpp
#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;
}