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 Apr 19, 2026
Problem #0121
Level Level 04
Solved By 11,059
Languages C++, Python
Answer 2269
Length 476 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 (1kn1 \le k \le n), the bag contains one blue disc and kk red discs (the original red disc, plus the k1k - 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}Tn/2kTk,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)=kS1k+1kTkk+1=kTkk=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)=23(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 Tn/2|T| \le \lfloor n/2 \rfloor. Summing the numerators kTk\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(i1,j)+ie(i1,j1),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=0n/2e(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,,i1}\{1, \ldots, i-1\}, contributing e(i1,j)e(i-1, j).
  • Subsets containing ii: formed by selecting a (j1)(j-1)-element subset of {1,,i1}\{1, \ldots, i-1\} and multiplying by ii, contributing ie(i1,j1)i \cdot e(i-1, j-1).

Since each subset TT of {1,,n}\{1, \ldots, n\} with T=j|T| = j contributes kTk\prod_{k \in T} k to e(n,j)e(n, j), summing e(n,j)e(n, j) for 0jn/20 \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)!.Nonnegativeexpectedprofitrequires. 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

At turn k, P(blue) = 1/(k+1), P(red) = k/(k+1). Player wins with >= 8 blue out of 15 turns. Prize = floor((n+1)! / N) where N sums elementary symmetric polynomials.

Pseudocode

    dp[0..floor(n/2)] initialized to 0
    dp[0] = 1

    For i from 1 to n:
        For j from min(i, floor(n/2)) down to 1:
            dp[j] = dp[j] + i * dp[j-1]

    N = sum(dp[0..floor(n/2)])
    D = (n+1)!
    Return floor(D / 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(nn/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;
}