All Euler problems
Project Euler

Paper Sheets of Standard Sizes

A printing shop runs 16 batches per job. For each batch, they take one sheet of paper at random from an envelope. At the beginning, the envelope contains a single A1 sheet. Whenever an A n sheet (w...

Source sync Apr 19, 2026
Problem #0151
Level Level 06
Solved By 6,406
Languages C++, Python
Answer 0.464399
Length 617 words
dynamic_programmingprobabilityrecursion

Problem Statement

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

A printing shop runs \(16\) batches (jobs) every week and each batch requires a sheet of special colour-proofing paper of size \(A5\).

Every Monday morning, the supervisor opens a new envelope, containing a large sheet of the special paper with size \(A1\).

The supervisor proceeds to cut it in half, thus getting two sheets of size \(A2\). Then one of the sheets is cut in half to get two sheets of size \(A3\) and so on until an \(A5\)-size sheet is obtained, which is needed for the first batch of the week.

All the unused sheets are placed back in the envelope.

PIC

At the beginning of each subsequent batch, the supervisor takes from the envelope one sheet of paper at random. If it is of size \(A5\), then it is used. If it is larger, then the ’cut-in-half’ procedure is repeated until an \(A5\)-size sheet is obtained, and any remaining sheets are always placed back in the envelope.

Excluding the first and last batch of the week, find the expected number of times (during each week) that the supervisor finds a single sheet of paper in the envelope.

Give your answer rounded to six decimal places using the format \(x.xxxxxx\).

Problem 151: Paper Sheets of Standard Sizes

Mathematical Development

Definition 1. A state is a tuple s=(a2,a3,a4,a5)Z04\mathbf{s} = (a_2, a_3, a_4, a_5) \in \mathbb{Z}_{\ge 0}^4, where aka_k denotes the number of Akk sheets in the envelope for k{2,3,4,5}k \in \{2,3,4,5\}. The A1 sheet is omitted because it is consumed in the first (excluded) batch.

Lemma 1 (State Transition Rules). When an Akk sheet (k{2,3,4,5}k \in \{2,3,4,5\}) is selected from the envelope, the state transition is:

  • k=5k = 5: a5a51a_5 \mapsto a_5 - 1; all other components unchanged.
  • k=4k = 4: a4a41,  a5a5+1a_4 \mapsto a_4 - 1,\; a_5 \mapsto a_5 + 1.
  • k=3k = 3: a3a31,  a4a4+1,  a5a5+1a_3 \mapsto a_3 - 1,\; a_4 \mapsto a_4 + 1,\; a_5 \mapsto a_5 + 1.
  • k=2k = 2: a2a21,  a3a3+1,  a4a4+1,  a5a5+1a_2 \mapsto a_2 - 1,\; a_3 \mapsto a_3 + 1,\; a_4 \mapsto a_4 + 1,\; a_5 \mapsto a_5 + 1.

Proof. An Akk sheet with k<5k < 5 is cut by successive halving into sheets A(k+1)(k{+}1), A(k+2)(k{+}2), …, A55, A55. The final halving of an A44 produces two A55 sheets, so the cut of Akk yields one sheet of each size from A(k+1)(k{+}1) through A44, plus two A55 sheets. One A55 is consumed for the batch. The net effect is: remove one Akk, add one each of A(k+1)(k{+}1), …, A55. For k=5k = 5, the sheet is consumed outright. \square

Lemma 2 (Initial State). The state after the first batch (which deterministically consumes the lone A1 sheet) is (1,1,1,1)(1, 1, 1, 1).

Proof. Cutting A1 yields one each of A2, A3, A4, and two copies of A5. One A5 is consumed, leaving counts (1,1,1,1)(1, 1, 1, 1). \square

Lemma 3 (Finiteness of the State Space). The reachable state space satisfies a21a_2 \le 1, a32a_3 \le 2, a44a_4 \le 4, a58a_5 \le 8, subject to the invariant 8a2+4a3+2a4+a5158a_2 + 4a_3 + 2a_4 + a_5 \le 15. In particular, the number of reachable states S|\mathcal{S}| is at most 2×3×5×9=2702 \times 3 \times 5 \times 9 = 270.

Proof. Define the A5-equivalent count W(s)=8a2+4a3+2a4+a5W(\mathbf{s}) = 8a_2 + 4a_3 + 2a_4 + a_5, reflecting that each Akk sheet can be decomposed into 25k2^{5-k} sheets of A5. The initial state has W=8+4+2+1=15W = 8 + 4 + 2 + 1 = 15. Each batch consumes exactly one A5-equivalent unit (one A5 is used), so WW decreases by 1 per batch. No transition creates additional A5-equivalent mass: when an Akk sheet (k<5k < 5) is cut and one A5 consumed, the net change is 25k+(25(k+1)++20+20)1=01=1-2^{5-k} + (2^{5-(k+1)} + \cdots + 2^0 + 2^0) - 1 = 0 - 1 = -1. Since no A2 is ever created (only removed), a21a_2 \le 1. Similarly, a31+a22a_3 \le 1 + a_2 \le 2, a41+a2+a34a_4 \le 1 + a_2 + a_3 \le 4, and a51+a2+a3+a48a_5 \le 1 + a_2 + a_3 + a_4 \le 8. \square

Theorem 1 (Expectation Recursion). Define E(s)E(\mathbf{s}) as the expected number of single-sheet encounters from state s\mathbf{s} onward, excluding the terminal batch. Let T(s)=a2+a3+a4+a5T(\mathbf{s}) = a_2 + a_3 + a_4 + a_5 be the total sheet count. Then E(0)=0E(\mathbf{0}) = 0 and for s0\mathbf{s} \neq \mathbf{0}:

E(s)=1 ⁣[T(s)=1    s(0,0,0,1)]+k=2ak>05akT(s)E(sk)E(\mathbf{s}) = \mathbf{1}\!\left[T(\mathbf{s}) = 1 \;\wedge\; \mathbf{s} \neq (0,0,0,1)\right] + \sum_{\substack{k=2 \\ a_k > 0}}^{5} \frac{a_k}{T(\mathbf{s})} \cdot E(\mathbf{s}_k)

where sk\mathbf{s}_k denotes the state obtained by selecting an Akk sheet from state s\mathbf{s} (as defined in Lemma 1).

Proof. The process is a finite-state Markov chain on S\mathcal{S} with absorbing state 0\mathbf{0}. At each non-terminal state s\mathbf{s}, the worker observes a single sheet if and only if T(s)=1T(\mathbf{s}) = 1. The problem excludes the first batch (state (1,1,1,1)(1,1,1,1), where T=4T = 4) and the last batch. The last batch occurs at state (0,0,0,1)(0,0,0,1), which is the unique pre-terminal state; it transitions to 0\mathbf{0} with certainty. We exclude this state from the single-sheet count by the indicator condition s(0,0,0,1)\mathbf{s} \neq (0,0,0,1).

Each sheet of size kk is selected with probability ak/T(s)a_k / T(\mathbf{s}) (uniform random draw). After selection, the state transitions to sk\mathbf{s}_k and the process continues. By the tower property of conditional expectation:

E(s)=1[single-sheet event]+kakT(s)E(sk).E(\mathbf{s}) = \mathbf{1}[\text{single-sheet event}] + \sum_{k} \frac{a_k}{T(\mathbf{s})} E(\mathbf{s}_k).

The recursion terminates at 0\mathbf{0} since no further batches occur. Since WW strictly decreases, there are no cycles, ensuring the recursion is well-founded. \square

Corollary. The answer to the problem is E(1,1,1,1)E(1,1,1,1).

Editorial

We single-sheet indicator (exclude terminal state). Finally, recurse over each possible selection. We use dynamic programming over the state space implied by the derivation, apply each admissible transition, and read the answer from the final table entry.

Pseudocode

Single-sheet indicator (exclude terminal state)
Recurse over each possible selection

Complexity Analysis

  • Time: O(S)O(|\mathcal{S}|) where S270|\mathcal{S}| \le 270 is the number of reachable states. Each state is visited once (via memoization) and requires O(1)O(1) work (at most 4 recursive lookups and arithmetic operations). Total: O(270)=O(1)O(270) = O(1).
  • Space: O(S)=O(1)O(|\mathcal{S}|) = O(1) for the memoization table.

Answer

0.464399\boxed{0.464399}

Code

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

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

map<tuple<int,int,int,int>, double> memo;

double E(int a2, int a3, int a4, int a5) {
    if (a2 == 0 && a3 == 0 && a4 == 0 && a5 == 0)
        return 0.0;
    auto key = make_tuple(a2, a3, a4, a5);
    auto it = memo.find(key);
    if (it != memo.end()) return it->second;

    int T = a2 + a3 + a4 + a5;
    double result = 0.0;

    // Single-sheet indicator, excluding terminal state (0,0,0,1)
    if (T == 1 && !(a2 == 0 && a3 == 0 && a4 == 0 && a5 == 1))
        result = 1.0;

    if (a2 > 0)
        result += (double)a2 / T * E(a2 - 1, a3 + 1, a4 + 1, a5 + 1);
    if (a3 > 0)
        result += (double)a3 / T * E(a2, a3 - 1, a4 + 1, a5 + 1);
    if (a4 > 0)
        result += (double)a4 / T * E(a2, a3, a4 - 1, a5 + 1);
    if (a5 > 0)
        result += (double)a5 / T * E(a2, a3, a4, a5 - 1);

    memo[key] = result;
    return result;
}

int main() {
    printf("%.6f\n", E(1, 1, 1, 1));
    return 0;
}