Even Stevens
Even Stevens flips a fair coin repeatedly. Starting with score 0, each flip adds 1 (heads, probability (1)/(2)) or 2 (tails, probability (1)/(2)) to his score. He stops when his score reaches or ex...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Every day for the past $n$ days Even Stevens brings home his groceries in a plastic bag. He stores these plastic bags in a cupboard. He either puts the plastic bag into the cupboard with the rest, or else he takes an even number of the existing bags (which may either be empty or previously filled with other bags themselves) and places these into the current bag.
After 4 days there are 5 possible packings and if the bags are numbered 1 (oldest), 2, 3, 4, they are:
Four empty bags,
1 and 2 inside 3, 4 empty,
1 and 3 inside 4, 2 empty,
1 and 2 inside 4, 3 empty,
2 and 3 inside 4, 1 empty.
Note that 1, 2, 3 inside 4 is invalid because every bag must contain an even number of bags.
Define $f(n)$ to be the number of possible packings of $n$ bags. Hence $f(4)=5$. You are also given $f(8)=1\,385$.
Find $f(24\,680)$ giving your answer modulo $1\,020\,202\,009$.
Problem 709: Even Stevens
Mathematical Foundation
Theorem 1 (Recurrence for ). The probability satisfies
with initial conditions and .
Proof. To land exactly on , the last step must arrive at from either (via heads) or (via tails). If the player is at score , flipping heads (probability ) reaches exactly. If at score , flipping tails (probability ) reaches exactly. Thus .
For the base cases: (the player starts at 0 with certainty). (the only way to reach exactly 1 is to flip heads from 0).
Theorem 2 (Closed Form). For ,
Proof. The characteristic equation of is , which factors as . The roots are and .
The general solution is .
Applying initial conditions:
- : .
- : .
From the first equation, . Substituting into the second: , so , giving and .
Theorem 3 (Closed Form for the Sum). Define . Then
Proof.
The geometric sum is with :
Therefore .
Lemma 1 (Modular Arithmetic). To compute where :
- : compute .
- : compute .
- : compute .
Proof. Since is prime and , all modular inverses exist by Fermat’s little theorem.
Editorial
We 2N/3 mod p. We then (-1/2)^N mod p. Finally, else. We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.
Pseudocode
p = 10^9 + 7
2N/3 mod p
(-1/2)^N mod p
if N is even
else
1/9 * (1 - (-1/2)^N) mod p
Note: be careful with sign: we subtract term2
Sigma = term1 - term2
But (-1/2)^N for modular: pow(-inv2, N, p) = pow(p - inv2, N, p)
Complexity Analysis
- Time: for modular exponentiation. All other operations are .
- Space: .
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 long long MOD = 1e9 + 7;
long long power(long long a, long long b, long long mod) {
long long res = 1; a %= mod;
while (b > 0) {
if (b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
return res;
}
int main() {
long long N = 10000000;
long long inv2 = power(2, MOD - 2, MOD);
long long inv3 = power(3, MOD - 2, MOD);
long long two_N_3 = 2 * N % MOD * inv3 % MOD;
long long neg_half = (MOD - inv2) % MOD;
long long neg_half_N = power(neg_half, N, MOD);
long long geo = (MOD - 1 + MOD) % MOD * ((1 - neg_half_N + MOD) % MOD) % MOD * inv3 % MOD;
long long ans = (two_N_3 + inv3 % MOD * geo % MOD) % MOD;
cout << ans << endl;
return 0;
}
"""
Problem 709: Even Stevens
"""
def solve():
MOD = 10**9 + 7
N = 10**7
inv2 = pow(2, MOD - 2, MOD)
inv3 = pow(3, MOD - 2, MOD)
# p(n) = 2/3 + (1/3)(-1/2)^n
# sum_{n=1}^{N} p(n) = 2N/3 + (1/3) * sum_{n=1}^{N} (-1/2)^n
# Geometric sum: sum = (-1/2)(1 - (-1/2)^N) / (1 - (-1/2)) = (-1/2)(1-(-1/2)^N)/(3/2)
# = -(1-(-1/2)^N)/3
two_N_over_3 = 2 * N % MOD * inv3 % MOD
# (-1/2)^N mod MOD
neg_half = MOD - inv2 # -1/2 mod MOD
neg_half_N = pow(neg_half, N, MOD)
geo_sum = (MOD - 1) * (1 - neg_half_N + MOD) % MOD * inv3 % MOD
result = (two_N_over_3 + inv3 * geo_sum) % MOD
return result
answer = solve()
print(answer)