All Euler problems
Project Euler

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

Source sync Apr 19, 2026
Problem #0709
Level Level 15
Solved By 1,082
Languages C++, Python
Answer 773479144
Length 257 words
modular_arithmeticprobabilitynumber_theory

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 p(n)p(n)). The probability p(n)p(n) satisfies

p(n)=12p(n1)+12p(n2),n2,p(n) = \frac{1}{2}\,p(n-1) + \frac{1}{2}\,p(n-2), \quad n \ge 2,

with initial conditions p(0)=1p(0) = 1 and p(1)=12p(1) = \frac{1}{2}.

Proof. To land exactly on nn, the last step must arrive at nn from either n1n-1 (via heads) or n2n-2 (via tails). If the player is at score n1n-1, flipping heads (probability 12\frac{1}{2}) reaches nn exactly. If at score n2n-2, flipping tails (probability 12\frac{1}{2}) reaches nn exactly. Thus p(n)=12p(n1)+12p(n2)p(n) = \frac{1}{2}\,p(n-1) + \frac{1}{2}\,p(n-2).

For the base cases: p(0)=1p(0) = 1 (the player starts at 0 with certainty). p(1)=12p(0)=12p(1) = \frac{1}{2}\,p(0) = \frac{1}{2} (the only way to reach exactly 1 is to flip heads from 0). \square

Theorem 2 (Closed Form). For n0n \ge 0,

p(n)=23+13(12)n.p(n) = \frac{2}{3} + \frac{1}{3}\left(-\frac{1}{2}\right)^n.

Proof. The characteristic equation of p(n)=12p(n1)+12p(n2)p(n) = \frac{1}{2}\,p(n-1) + \frac{1}{2}\,p(n-2) is 2x2x1=02x^2 - x - 1 = 0, which factors as (2x+1)(x1)=0(2x + 1)(x - 1) = 0. The roots are x=1x = 1 and x=12x = -\frac{1}{2}.

The general solution is p(n)=A1n+B(12)n=A+B(12)np(n) = A \cdot 1^n + B \cdot (-\frac{1}{2})^n = A + B(-\frac{1}{2})^n.

Applying initial conditions:

  • p(0)=1p(0) = 1: A+B=1A + B = 1.
  • p(1)=12p(1) = \frac{1}{2}: AB2=12A - \frac{B}{2} = \frac{1}{2}.

From the first equation, B=1AB = 1 - A. Substituting into the second: A(1A)/2=1/2A - (1-A)/2 = 1/2, so 3A/2=13A/2 = 1, giving A=2/3A = 2/3 and B=1/3B = 1/3. \square

Theorem 3 (Closed Form for the Sum). Define Σ(N)=n=1Np(n)\Sigma(N) = \sum_{n=1}^{N} p(n). Then

Σ(N)=2N3+1312(1(12)N)1(12)=2N319(1(12)N).\Sigma(N) = \frac{2N}{3} + \frac{1}{3} \cdot \frac{-\frac{1}{2}\bigl(1 - (-\frac{1}{2})^N\bigr)}{1 - (-\frac{1}{2})} = \frac{2N}{3} - \frac{1}{9}\bigl(1 - (-\tfrac{1}{2})^N\bigr).

Proof.

Σ(N)=n=1N(23+13(12)n)=2N3+13n=1N(12)n.\Sigma(N) = \sum_{n=1}^{N}\left(\frac{2}{3} + \frac{1}{3}\left(-\frac{1}{2}\right)^n\right) = \frac{2N}{3} + \frac{1}{3}\sum_{n=1}^{N}\left(-\frac{1}{2}\right)^n.

The geometric sum is n=1Nrn=r(1rN)/(1r)\sum_{n=1}^{N} r^n = r(1 - r^N)/(1 - r) with r=1/2r = -1/2:

n=1N(12)n=12(1(12)N)32=(1(12)N)3.\sum_{n=1}^{N}\left(-\frac{1}{2}\right)^n = \frac{-\frac{1}{2}(1 - (-\frac{1}{2})^N)}{\frac{3}{2}} = \frac{-(1 - (-\frac{1}{2})^N)}{3}.

Therefore Σ(N)=2N319(1(12)N)\Sigma(N) = \frac{2N}{3} - \frac{1}{9}(1 - (-\frac{1}{2})^N). \square

Lemma 1 (Modular Arithmetic). To compute Σ(N)modp\Sigma(N) \bmod p where p=109+7p = 10^9 + 7:

  • 2/3modp2/3 \bmod p: compute 23p2modp2 \cdot 3^{p-2} \bmod p.
  • (1/2)Nmodp(-1/2)^N \bmod p: compute (1)N(2p2)Nmodp=(1)N2N(p2)mod(p1)modp(-1)^N \cdot (2^{p-2})^N \bmod p = (-1)^N \cdot 2^{N(p-2) \bmod (p-1)} \bmod p.
  • 1/9modp1/9 \bmod p: compute 9p2modp9^{p-2} \bmod p.

Proof. Since pp is prime and gcd(2,p)=gcd(3,p)=gcd(9,p)=1\gcd(2, p) = \gcd(3, p) = \gcd(9, p) = 1, all modular inverses exist by Fermat’s little theorem. \square

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: O(logN)O(\log N) for modular exponentiation. All other operations are O(1)O(1).
  • Space: O(1)O(1).

Answer

773479144\boxed{773479144}

Code

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

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