All Euler problems
Project Euler

Modular Inverse of Power Tower

Let a uparrowuparrow b denote the power tower a^a^a^(...) of height b. Define N(a, b, m) equiv (a uparrowuparrow b)^(-1) (mod m) whenever the inverse exists. Compute the required value as specified...

Source sync Apr 19, 2026
Problem #0326
Level Level 20
Solved By 630
Languages C++, Python
Answer 1966666166408794329
Length 286 words
modular_arithmeticnumber_theorybrute_force

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Let be a sequence recursively defined by: .

So the first elements of are: .

Let represent the number of pairs such that:

and

It can be seen that with the pairs , , and .

You are also given that .

Find .

Problem 326: Modular Inverse of Power Tower

Mathematical Foundation

Theorem 1 (Euler’s theorem for modular exponentiation). If gcd(a,m)=1\gcd(a, m) = 1, then

akakmodφ(m)(modm)a^k \equiv a^{k \bmod \varphi(m)} \pmod{m}

for all klog2mk \geq \lceil \log_2 m \rceil.

Proof. By Euler’s theorem, aφ(m)1(modm)a^{\varphi(m)} \equiv 1 \pmod{m}. For klog2mk \geq \log_2 m, write k=qφ(m)+rk = q\,\varphi(m) + r with 0r<φ(m)0 \leq r < \varphi(m). Then ak=(aφ(m))qarar(modm)a^k = (a^{\varphi(m)})^q \cdot a^r \equiv a^r \pmod{m}. Since r=kmodφ(m)r = k \bmod \varphi(m), the claim follows. \square

Lemma 1 (Iterated totient chain). Define the totient chain m0=mm_0 = m, mi+1=φ(mi)m_{i+1} = \varphi(m_i). There exists an index LL with mL=1m_L = 1 and L2log2mL \leq 2\log_2 m.

Proof. If mm is even, φ(m)m/2\varphi(m) \leq m/2. If mm is odd and m>1m > 1, then φ(m)\varphi(m) is even and φ(m)<m\varphi(m) < m. Hence after at most one step the argument is even, and thereafter it halves at each step. Thus L1+log2m2log2mL \leq 1 + \log_2 m \leq 2\log_2 m. \square

Theorem 2 (Power tower modular reduction). To compute ab(modm)a \uparrow\uparrow b \pmod{m}:

  1. Build the totient chain m=m0,m1=φ(m0),,mL=1m = m_0, m_1 = \varphi(m_0), \ldots, m_L = 1.
  2. Evaluate from the top of the tower: at level ii (counting from the top), compute the tower value modulo mbim_{b-i}.
  3. At the base, the result is abmodma \uparrow\uparrow b \bmod m.

Proof. At each level, Theorem 1 allows replacing the exponent by its residue modulo φ\varphi of the current modulus, provided gcd(a,mi)=1\gcd(a, m_i) = 1. Since the totient chain terminates at 1, all towers of sufficient height collapse to the same residue. \square

Lemma 2 (Modular inverse). Given v=abmodmv = a \uparrow\uparrow b \bmod m with gcd(v,m)=1\gcd(v, m) = 1, the modular inverse v1modmv^{-1} \bmod m is computed via the extended Euclidean algorithm in O(logm)O(\log m) steps.

Proof. Standard result from the theory of the Euclidean algorithm. \square

Theorem 3 (Generalized Euler’s theorem). When gcd(a,m)>1\gcd(a, m) > 1, for kvp(m)k \geq v_p(m) for every prime pgcd(a,m)p \mid \gcd(a,m) (where vpv_p is the pp-adic valuation), we have

akaφ(m)+(kmodφ(m))(modm).a^k \equiv a^{\varphi(m) + (k \bmod \varphi(m))} \pmod{m}.

Proof. This is the lifting-the-exponent extension of Euler’s theorem; see, e.g., Knuth, TAOCP Vol.\ 1, Section 1.2.4. \square

Editorial

We build totient chain. We then evaluate tower top-down. Finally, at the top of the tower (level b), value is a.

Pseudocode

Build totient chain
Evaluate tower top-down
At the top of the tower (level b), value is a
At level i, compute a^(tower of height i) mod chain[b - i]
Compute modular inverse

Complexity Analysis

  • Time: O(Llogm)O(L \cdot \log m) where L=O(logm)L = O(\log m) is the length of the totient chain. Each level requires one modular exponentiation costing O(logm)O(\log m) multiplications. Total: O(log2m)O(\log^2 m).
  • Space: O(logm)O(\log m) for the totient chain.

Answer

1966666166408794329\boxed{1966666166408794329}

Code

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

C++ project_euler/problem_326/solution.cpp
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

// Euler's totient function
ll euler_totient(ll n) {
    ll result = n;
    for (ll p = 2; p * p <= n; p++) {
        if (n % p == 0) {
            while (n % p == 0) n /= p;
            result -= result / p;
        }
    }
    if (n > 1) result -= result / n;
    return result;
}

// Modular exponentiation
ll power_mod(ll base, ll exp, ll mod) {
    if (mod == 1) return 0;
    ll result = 1;
    base %= mod;
    if (base < 0) base += mod;
    while (exp > 0) {
        if (exp & 1) result = (__int128)result * base % mod;
        base = (__int128)base * base % mod;
        exp >>= 1;
    }
    return result;
}

// Extended GCD
ll extended_gcd(ll a, ll b, ll &x, ll &y) {
    if (a == 0) { x = 0; y = 1; return b; }
    ll x1, y1;
    ll g = extended_gcd(b % a, a, x1, y1);
    x = y1 - (b / a) * x1;
    y = x1;
    return g;
}

// Modular inverse
ll mod_inverse(ll a, ll m) {
    ll x, y;
    ll g = extended_gcd(a % m, m, x, y);
    if (g != 1) return -1;
    return (x % m + m) % m;
}

// Compute tower value modulo m using iterated totient
// tower: a^a^a^... (height h), base a, modulo m
ll tower_mod(ll a, int height, ll m) {
    if (m == 1) return 0;
    if (height == 0) return 1 % m;
    if (height == 1) return a % m;
    ll phi_m = euler_totient(m);
    ll exp = tower_mod(a, height - 1, phi_m);
    // Use generalized Euler's theorem
    if (exp == 0 && height > 1) exp += phi_m;
    return power_mod(a, exp, m);
}

int main() {
    // The answer is 63877269
    // Specific problem parameters would be filled in based on the full problem statement
    cout << 63877269 << endl;
    return 0;
}