All Euler problems
Project Euler

Disc Game Prize Fund

A bag contains one red disc and one blue disc. In a game of chance, the player pays $1 to play and the following procedure is repeated each turn: A red disc is added to the bag. A disc is randomly...

Source sync May 21, 2026
Problem #0121
Level Level 04
Solved By 11,059
Languages C++, Python
Answer 2269
Length 557 words
dynamic_programminggame_theoryprobability

Problem Statement

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

A bag contains one red disc and one blue disc. In a game of chance a player takes a disc at random and its colour is noted. After each turn the disc is returned to the bag, an extra red disc is added, and another disc is taken at random.

The player pays £1 to play and wins if they have taken more blue discs than red discs at the end of the game.

If the game is played for four turns, the probability of a player winning is exactly \(11/120\), and so the maximum prize fund the banker should allocate for winning in this game would be \(\pounds 10\) before they would expect to incur a loss. Note that any payout will be a whole number of pounds and also includes the original \(\pounds 1\) paid to play the game, so in the example given the player actually wins \(\pounds 9\).

Problem 121: Disc Game Prize Fund

Mathematical Development

Definition 1. Let nn denote the number of turns. At turn kk (1≤k≤n1 \le k \le n), the bag contains one blue disc and kk red discs (the original red disc, plus the k−1k - 1 previously added red discs, plus the one added at the start of turn kk), for a total of k+1k + 1 discs.

Theorem 1 (Turn probabilities and independence). At turn kk, Pr⁡(blue at turn k)=1k+1\Pr(\text{blue at turn } k) = \frac{1}{k+1} and Pr⁡(red at turn k)=kk+1\Pr(\text{red at turn } k) = \frac{k}{k+1}. The outcomes across distinct turns are mutually independent.

Proof. Consider the equivalent probabilistic model. Before turn kk, one red disc is added to the bag and then one disc is drawn uniformly at random from the k+1k + 1 discs present. Since the drawn disc is set aside and one new red disc replaces it before the next turn, the bag at each turn kk is deterministically composed of 1 blue and kk red discs, regardless of prior history. Consequently, the conditional probability of drawing blue at turn kk given any history equals 1/(k+1)1/(k+1), and by the definition of conditional independence, the turn outcomes are mutually independent. □\square

Theorem 2 (Winning probability). The probability that the player wins (draws at least ⌈(n+1)/2⌉\lceil(n+1)/2\rceil blue discs in nn turns) is

P(win)=N(n+1)!,P(\text{win}) = \frac{N}{(n+1)!},

where

N=∑T⊆{1,…,n}∣T∣≤⌊n/2⌋∏k∈Tk,N = \sum_{\substack{T \subseteq \{1, \ldots, n\} \\ |T| \le \lfloor n/2 \rfloor}} \prod_{k \in T} k,

and TT indexes the set of turns on which red is drawn.

Proof. Let S⊆{1,…,n}S \subseteq \{1, \ldots, n\} be the set of turns on which blue is drawn, and set T={1,…,n}∖ST = \{1,\ldots,n\} \setminus S. By Theorem 1 and the multiplication rule for independent events,

Pr⁡(S)=∏k∈S1k+1⋅∏k∈Tkk+1=∏k∈Tk∏k=1n(k+1).\Pr(S) = \prod_{k \in S} \frac{1}{k+1} \cdot \prod_{k \in T} \frac{k}{k+1} = \frac{\prod_{k \in T} k}{\prod_{k=1}^{n}(k+1)}.

The common denominator is ∏k=1n(k+1)=2⋅3⋯(n+1)=(n+1)!\prod_{k=1}^{n}(k+1) = 2 \cdot 3 \cdots (n+1) = (n+1)!. The player wins when ∣S∣>n/2|S| > n/2, equivalently ∣T∣≤⌊n/2⌋|T| \le \lfloor n/2 \rfloor. Summing the numerators ∏k∈Tk\prod_{k \in T} k over all qualifying TT yields NN, and therefore P(win)=N/(n+1)!P(\text{win}) = N / (n+1)!. □\square

Lemma 1 (Elementary symmetric polynomial recurrence). Define e(i,j)e(i, j) to be the jj-th elementary symmetric polynomial of {1,2,…,i}\{1, 2, \ldots, i\}, i.e., the sum of products of all jj-element subsets. Then

e(i,j)=e(i−1,j)+i⋅e(i−1,j−1),e(i, j) = e(i-1, j) + i \cdot e(i-1, j-1),

with boundary conditions e(0,0)=1e(0, 0) = 1 and e(i,j)=0e(i, j) = 0 for j<0j < 0 or j>ij > i. Moreover,

N=∑j=0⌊n/2⌋e(n,j).N = \sum_{j=0}^{\lfloor n/2 \rfloor} e(n, j).

Proof. Partition the jj-element subsets of {1,…,i}\{1, \ldots, i\} by membership of ii:

  • Subsets not containing ii: these are jj-element subsets of {1,…,i−1}\{1, \ldots, i-1\}, contributing e(i−1,j)e(i-1, j).
  • Subsets containing ii: formed by selecting a (j−1)(j-1)-element subset of {1,…,i−1}\{1, \ldots, i-1\} and multiplying by ii, contributing i⋅e(i−1,j−1)i \cdot e(i-1, j-1).

Since each subset TT of {1,…,n}\{1, \ldots, n\} with ∣T∣=j|T| = j contributes ∏k∈Tk\prod_{k \in T} k to e(n,j)e(n, j), summing e(n,j)e(n, j) for 0≤j≤⌊n/2⌋0 \le j \le \lfloor n/2 \rfloor yields NN. □\square

Theorem 3 (Maximum prize fund). The maximum prize fund is ⌊(n+1)!/N⌋\lfloor (n+1)! / N \rfloor.

Proof. The banker charges 1pergame.Foraprizeof1 per game. For a prize of Mdollars,theexpectedpayoutisdollars, the expected payout isM \cdot P(\text{win}) = MN/(n+1)!.Non−negativeexpectedprofitrequires. Non-negative expected profit requires MN/(n+1)! \le 1,hence, hence M \le (n+1)!/N.Themaximumintegerprizeistherefore. The maximum integer prize is therefore \lfloor (n+1)!/N \rfloor.. \square$

Editorial

The winning probability has a common denominator right from the start: each turn contributes a factor of (k+1)(k+1) in the denominator, so every outcome over 15 turns sits over 16!16!. What varies is only the numerator, and for a fixed set of red turns that numerator is the product of the turn indices where red was drawn. Summing those products over all losing-or-drawing red counts produces the numerator NN from the mathematical section.

That structure is exactly an elementary symmetric polynomial, so the implementation computes it with a one-dimensional dynamic program instead of enumerating subsets explicitly. After processing turns 11 through 1515, the relevant DP entries sum to NN, and the maximum safe prize fund is just ⌊16!/N⌋\lfloor 16! / N \rfloor.

Pseudocode

Create an array whose j-th entry stores the j-th elementary symmetric sum of the processed turn indices.
Initialize the zero-th entry to 1 and all others to 0.

For each turn index i from 1 through 15:
    Update the symmetric sums backwards so that each size-j sum either skips i or includes one factor of i.

Add together the entries corresponding to at most seven red draws; this gives the numerator N of the winning probability.
Compute 16! and return the integer part of 16! divided by N.

Complexity Analysis

  • Time: The double loop performs ∑i=1nmin⁡(i,⌊n/2⌋)\sum_{i=1}^{n} \min(i, \lfloor n/2 \rfloor) iterations, which is O(n⋅⌊n/2⌋)=O(n2)O(n \cdot \lfloor n/2 \rfloor) = O(n^2).
  • Space: O(n)O(n) for the one-dimensional DP array, updated in-place by processing the inner index in decreasing order.

Answer

2269\boxed{2269}

Code

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

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

int main() {
    const int n = 15;
    const int half = n / 2; // 7

    // dp[j] = j-th elementary symmetric polynomial of {1,...,i}
    vector<long long> dp(half + 1, 0);
    dp[0] = 1;

    for (int i = 1; i <= n; i++)
        for (int j = min(i, half); j >= 1; j--)
            dp[j] += (long long)i * dp[j - 1];

    long long N = 0;
    for (int j = 0; j <= half; j++)
        N += dp[j];

    // Compute (n+1)! = 16!
    long long fact = 1;
    for (int i = 1; i <= n + 1; i++)
        fact *= i;

    cout << fact / N << endl;
    return 0;
}