Two Heads Are Better Than One
A fair coin is flipped repeatedly. Let E_n denote the expected number of flips required to obtain n consecutive heads. Compute E_n and related quantities modulo a given prime.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
An unbiased coin is tossed repeatedly until two consecutive heads are obtained. Suppose these occur on the $(M-1)$th and $M$th toss.
Let $P(n)$ be the probability that $M$ is divisible by $n$. For example, the outcomes HH, HTHH, and THTTHH all count towards $P(2)$, but THH and HTTHH do not.
You are given that $P(2) =\frac 3 5$ and $P(3)=\frac 9 {31}$. Indeed, it can be shown that $P(n)$ is always a rational number.
For a prime $p$ and a fully reduced fraction $\frac a b$, define $Q(\frac a b,p)$ to be the smallest positive $q$ for which $a \equiv b q \pmod{p}$.
For example $Q(P(2), 109) = Q(\frac 3 5, 109) = 66$, because $5 \cdot 66 = 330 \equiv 3 \pmod{109}$ and $66$ is the smallest positive such number.
Similarly $Q(P(3),109) = 46$.
Find $Q(P(10^{18}),1\,000\,000\,009)$.
Problem 624: Two Heads Are Better Than One
Mathematical Analysis
Markov Chain Model
Define states where means the current run of consecutive heads has length . State is absorbing (goal reached). Transitions:
System of Equations
Let = expected flips from state to . Then and for :
Solving the System
Step 1: Define (the “extra” cost of being at state vs ). From (2):
A cleaner approach: rearrange (2) as … Let’s instead solve directly.
From (2): , so .
Setting : .
Wait, let’s substitute carefully: , giving .
With . Iterating: , , and in general:
More precisely: after telescoping.
Since : , giving:
Closed Form
Verification Table
| Direct computation | ||
|---|---|---|
| 1 | 2 | |
| 2 | 6 | States: , |
| 3 | 14 | Solve 3-state system |
| 4 | 30 | |
| 5 | 62 | |
| 10 | 2046 | |
| 20 | 2097150 |
Alternative: Generating Function Approach
The probability generating function for the waiting time satisfies:
But the Markov chain approach is simpler and yields the explicit formula directly.
Higher Moments
The variance of the waiting time can also be computed from the Markov chain. The second moment satisfies a similar system, yielding .
Derivation
Algorithm 1: Closed Form (primary)
Simply compute using fast modular exponentiation.
Algorithm 2: Markov Chain Iteration (verification)
Solve the system of equations numerically by back-substitution from to .
Algorithm 3: Monte Carlo Simulation (cross-check)
Simulate the coin-flip process many times and compute the empirical mean.
Proof of Correctness
Theorem. for all .
Proof. We verify satisfies the recurrence :
Corollary. satisfies the recurrence with .
Proof. .
Proposition (Exponential growth). as : the expected wait doubles with each additional required head.
Complexity Analysis
- Closed form: via modular exponentiation.
- Markov chain: for back-substitution.
- Simulation: for trials.
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;
/*
* Problem 624: Two Heads Are Better Than One
*
* Expected flips to get n consecutive heads: E(n) = 2^{n+1} - 2.
*
* Markov chain: S_k = run of k heads. Transitions:
* S_k -> S_{k+1} with probability 1/2 (heads)
* S_k -> S_0 with probability 1/2 (tails)
*
* Recurrence: e_k = 1 + (1/2)*e_{k+1} + (1/2)*e_0
* Solution: e_0 = 2^{n+1} - 2.
*
* Methods:
* 1. Closed-form via modular exponentiation
* 2. Back-substitution (verification)
*/
const ll MOD = 1e9 + 7;
ll power(ll base, ll exp, ll mod) {
ll result = 1;
base %= mod;
while (exp > 0) {
if (exp & 1) result = result * base % mod;
base = base * base % mod;
exp >>= 1;
}
return result;
}
// Method 1: Closed form
ll solve_closed(int n) {
return (power(2, n + 1, MOD) - 2 + MOD) % MOD;
}
// Method 2: Back-substitution (exact for small n)
ll solve_exact(int n) {
// e_k = 2^{n+1} - 2^{k+1}
// Verify: e_0 = 2^{n+1} - 2
ll e0 = (1LL << (n + 1)) - 2;
return e0;
}
int main() {
// Verify closed form matches exact for small n
for (int n = 1; n <= 30; n++) {
ll exact = solve_exact(n);
ll modular = solve_closed(n);
assert(exact % MOD == modular);
// Verify recurrence: E(n) = 2*E(n-1) + 2
if (n >= 2) {
assert(exact == 2 * solve_exact(n - 1) + 2);
}
}
cout << "Verification passed for n = 1..30" << endl;
// Print table
printf("%4s %12s\n", "n", "E(n)");
for (int n = 1; n <= 20; n++) {
if (n <= 30) {
printf("%4d %12lld\n", n, solve_exact(n));
}
}
// Large n via modular arithmetic
int n = 1000000;
ll ans = solve_closed(n);
cout << "\nE(" << n << ") mod " << MOD << " = " << ans << endl;
return 0;
}
"""
Problem 624: Two Heads Are Better Than One
Expected number of fair coin flips to get n consecutive heads: E(n) = 2^{n+1} - 2.
Markov chain with states S_0, S_1, ..., S_n (S_k = run of k heads).
Transition: S_k -> S_{k+1} with p=1/2, S_k -> S_0 with p=1/2.
Recurrence: e_k = 1 + (1/2)*e_{k+1} + (1/2)*e_0, solution: e_0 = 2^{n+1} - 2.
Method 1: Closed-form formula (primary)
Method 2: Markov chain back-substitution (verification)
Method 3: Monte Carlo simulation (cross-check)
"""
import random
MOD = 10**9 + 7
# --- Method 1: Closed form ---
def expected_closed(n: int):
"""E(n) = 2^{n+1} - 2 (exact integer)."""
return 2**(n + 1) - 2
def expected_closed_mod(n: int, mod: int):
"""E(n) = 2^{n+1} - 2 mod p."""
return (pow(2, n + 1, mod) - 2) % mod
# --- Method 2: Markov chain back-substitution ---
def expected_markov(n: int) -> float:
"""Solve the Markov chain system exactly via back-substitution.
e_k = 2^{n+1} - 2^{k+1}, verified by substitution.
Here we solve numerically for comparison.
"""
# e_k = 1 + 0.5*e_{k+1} + 0.5*e_0
# Let f_k = e_k - e_0. Then f_k = 1 + 0.5*f_{k+1}, f_n = -e_0, f_0 = 0.
# f_{n-1} = 1 - 0.5*e_0
# f_{n-2} = 1 + 0.5*(1 - 0.5*e_0) = 1.5 - 0.25*e_0
# f_k = 2*(1 - 2^{-(n-k)}) - 2^{-(n-k)}*e_0
# f_0 = 0 => e_0 = 2*(2^n - 1)
e = [0.0] * (n + 1)
# Symbolic: e_k = A_k + B_k * e_0
# e_n = 0: A_n = 0, B_n = 0
# e_k = 1 + 0.5*(A_{k+1} + B_{k+1}*e_0) + 0.5*e_0
# = (1 + 0.5*A_{k+1}) + (0.5*B_{k+1} + 0.5)*e_0
A = [0.0] * (n + 1)
B = [0.0] * (n + 1)
for k in range(n - 1, -1, -1):
A[k] = 1 + 0.5 * A[k + 1]
B[k] = 0.5 * B[k + 1] + 0.5
# e_0 = A_0 + B_0*e_0 => e_0*(1 - B_0) = A_0
e0 = A[0] / (1 - B[0])
return e0
# --- Method 3: Monte Carlo simulation ---
def expected_simulation(n: int, trials: int = 100000) -> float:
"""Estimate E(n) via Monte Carlo simulation."""
total = 0
for _ in range(trials):
run, flips = 0, 0
while run < n:
flips += 1
if random.random() < 0.5:
run += 1
else:
run = 0
total += flips
return total / trials
# Compute and verify
# Verify closed form against Markov chain
for n in range(1, 25):
exact = expected_closed(n)
markov = expected_markov(n)
assert abs(exact - markov) < 1e-6, f"n={n}: exact={exact}, markov={markov}"
# Verify recurrence E_n = 2*E_{n-1} + 2
for n in range(2, 30):
assert expected_closed(n) == 2 * expected_closed(n - 1) + 2
# Verify modular computation
for n in range(1, 20):
assert expected_closed_mod(n, MOD) == expected_closed(n) % MOD
# Verify against simulation for small n
random.seed(42)
for n in range(1, 6):
sim = expected_simulation(n, trials=200000)
exact = expected_closed(n)
rel_error = abs(sim - exact) / exact
assert rel_error < 0.05, f"n={n}: sim={sim:.1f}, exact={exact}, error={rel_error:.3f}"
print("All verifications passed.")
# Print table
print(f"\n{'n':>4} {'E(n)':>12} {'2^(n+1)-2':>12} {'Simulation':>12}")
for n in range(1, 11):
random.seed(n)
sim = expected_simulation(n, trials=50000)
print(f"{n:>4} {expected_closed(n):>12} {2**(n+1)-2:>12} {sim:>12.1f}")