Power Sequence
Tetration (iterated exponentiation) is defined recursively: ^0 a = 1 and ^(n+1) a = a^^n a. Compute ^n a mod m for given a, n, m.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Given two positive integers \(a,b\), Alex and Bianca play a game in \(ab\) rounds. They begin with a square piece of paper of side length \(1\).
In each round Alex divides the current rectangular piece of paper into \(a \times b\) pieces using \(a-1\) horizontal cuts and \(b-1\) vertical ones. The cuts do not need to be evenly spaced. Moreover, a piece can have zero width/height when a cut coincides with another cut or the edge of the paper. The pieces are then numbered \(1, 2, ..., ab\) starting from the left top corner, moving from left to right and starting from the left of the next row when a row is finished.
Then Bianca chooses one of the pieces for the game to continue on. However, Bianca must not choose a piece with a number she has already chosen during the game.
Bianca wants to minimize the area of the final piece of paper while Alex wants to maximize it. Let \(S(a,b)\) be the area of the final piece assuming optimal play.
For example, \(S(2,2) = 1/36\) and \(S(2, 3) = 1/1800 \approx 5.5555555556\mathrm {e}{-4}\).
Find \(S(5,8)\). Give your answer in scientific notation rounded to ten significant digits after the decimal point. Use a lowercase e to separate the mantissa and the exponent.
Problem 855: Power Sequence
Mathematical Analysis
Euler’s Theorem for Modular Tetration
Theorem (Euler). If , then .
Corollary. when and .
Recursive Reduction
To compute :
- If , return 0.
- Compute recursively.
- Return .
The recursion terminates because after steps.
Theorem. The tower after at most iterations, since for .
Lifting the Exponent Lemma
When , we need more care. If for some small , and the tower height exceeds , the answer is 0.
Concrete Examples
| 2 | 1 | 10 | 2 |
| 2 | 2 | 10 | 4 |
| 2 | 3 | 10 | 6 () |
| 2 | 4 | 10 | 6 () |
| 3 | 2 | 7 | 6 () |
| 2 | 3 | 100 | 36 () |
Verification for : . Correct.
Verification for : .
Convergence of Tetration Mod
Theorem. For fixed and with , the sequence eventually stabilizes (becomes constant) for .
Complexity Analysis
- Recursive -reduction: for the recursion depth times modular exponentiation.
- Computing : per level via trial division.
- Total: .
Knuth’s Up-Arrow Notation
Tetration is . In Knuth’s notation: , ( copies). This extends to higher hyperoperations: (pentation), etc.
Why the Algorithm Terminates
Theorem. The -reduction chain reaches 1 after at most steps.
Proof. For even : . For odd : is even (since is even for ), so the next step halves. Every two steps, the value at least halves.
Handling
When , Euler’s theorem doesn’t directly apply. The correct formula is:
The "" ensures we’re in the periodic regime of .
Infinite Tetration
Theorem. The infinite tower converges iff , i.e., approximately .
For : since .
For : .
Connection to Ackermann Function
Tetration is equivalent to the Ackermann function at level 3: grows as tetration of 2.
Fixed Points of Modular Tetration
Theorem. For fixed and , the sequence is eventually constant. The stabilization index is at most where .
| Stable value | |||
|---|---|---|---|
| 2 | 10 | 4 | 6 |
| 3 | 7 | 3 | 6 |
| 2 | 100 | 5 | 36 |
| 5 | 13 | 4 | 5 |
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll euler_totient(ll n) {
ll result = n, temp = n;
for (ll p = 2; p * p <= temp; p++) {
if (temp % p == 0) {
while (temp % p == 0) temp /= p;
result -= result / p;
}
}
if (temp > 1) result -= result / temp;
return result;
}
ll power(ll b, ll e, ll m) {
ll r = 1; b %= m;
while (e > 0) { if (e&1) r = r*b%m; b = b*b%m; e >>= 1; }
return r;
}
ll tetration_mod(ll a, ll n, ll m) {
if (m == 1) return 0;
if (n == 0) return 1 % m;
if (n == 1) return a % m;
ll phi = euler_totient(m);
ll exp = tetration_mod(a, n-1, phi);
if (__gcd(a, m) == 1) return power(a, exp, m);
return power(a, exp + phi, m);
}
int main() {
assert(tetration_mod(2, 3, 10) == 6);
assert(tetration_mod(2, 4, 10) == 6);
cout << tetration_mod(2, 100, 1000000007) << endl;
return 0;
}
"""
Problem 855: Power Sequence - Modular Tetration
Compute {}^n a mod m using iterated Euler's theorem.
"""
from math import gcd
def euler_totient(n):
result = n
p = 2
temp = n
while p * p <= temp:
if temp % p == 0:
while temp % p == 0: temp //= p
result -= result // p
p += 1
if temp > 1:
result -= result // temp
return result
def tetration_mod(a, n, m):
"""Compute {}^n a mod m."""
if m == 1: return 0
if n == 0: return 1 % m
if n == 1: return a % m
phi_m = euler_totient(m)
if gcd(a, m) == 1:
exp = tetration_mod(a, n - 1, phi_m)
return pow(a, exp, m)
else:
exp = tetration_mod(a, n - 1, phi_m)
# Need to handle case where a and m share factors
# For large enough towers, a^tower is divisible by high powers of shared primes
return pow(a, exp + phi_m, m) # add phi_m to ensure correct residue
# Direct computation for small cases
def tetration_exact(a, n):
if n == 0: return 1
return a ** tetration_exact(a, n - 1)
# Verify
assert tetration_mod(2, 1, 10) == 2
assert tetration_mod(2, 2, 10) == 4
assert tetration_mod(2, 3, 10) == 6
assert tetration_mod(2, 4, 10) == 6
assert tetration_mod(3, 2, 7) == 6
for a in range(2, 6):
for n in range(0, 5):
exact = tetration_exact(a, n)
for m in [7, 10, 13, 100]:
assert tetration_mod(a, n, m) == exact % m, \
f"Mismatch: a={a}, n={n}, m={m}"
print("Verification passed!")
print(f"Answer: {tetration_mod(2, 100, 10**9 + 7)}")