Disc Game Prize Fund
· Project Euler 121
The prize and the probability of winning
The banker collects £1 on every play and pays the prize only on a win. If the prize is F pounds and the probability of winning is p, the expected payout is pF pounds. The banker's expected profit is therefore 1−pF. Requiring 1−pF≥0 gives the largest whole-pound prize
Fmax=⌊p1⌋.In n draws, a player who sees blue k times sees red n−k times. Winning requires k>n−k, so for 15 turns we need the probability of drawing at least eight blues.
Count the blues after each turn
Returning the disc makes the bag on the next turn predictable. Whatever the previous colours were, on turn i the bag holds one blue disc and i red discs. To decide whether the player has won, the only part of the history we need to keep is the number of blues drawn so far.
Let Ai(k) be the probability of exactly k blues after i turns. A red disc on the last turn leaves the count at k, so it contributes Ai−1(k)i/(i+1). A blue disc brings the count up from k−1 and contributes Ai−1(k−1)/(i+1). Every history ending with k blues falls into exactly one of these cases. Adding them gives
Ai(k)=i+1iAi−1(k)+Ai−1(k−1).Initially A0(0)=1. A count outside 0≤k≤i has probability zero, which also covers the boundary cases of the recurrence.
To take the floor of 1/p exactly, keep the numerators as integers. Every history through turn i has the same denominator Di=2⋅3⋯(i+1)=(i+1)!, with D0=1. Set Wi(k)=DiAi(k). Since Di=(i+1)Di−1, the recurrence becomes
Wi(k)=iWi−1(k)+Wi−1(k−1),W0(0)=1.Four turns by hand
With four draws the player needs at least three blues. Four blues have weight 1. If there is exactly one red, its turn determines the weight, since red on turn j contributes a factor of j and every blue contributes a factor of 1. The winning weight is therefore 1+(1+2+3+4)=11. The denominator is 5!=120, so the maximum prize is ⌊120/11⌋=10 pounds. A winner keeps £9 after accounting for the stake.
For 15 turns the recurrence gives ∑k=815W15(k)=9219406943. The denominator is 16!=20922789888000, so the largest prize is £2,269.
Time and space
The table has one row per turn and one column per possible blue count.
Filling it takes O(n2) arithmetic operations and O(n2) storage for
n turns. Through turn 15 every row sums to at most 16!, so all the
weights and the final denominator fit in an unsigned long long.
C++ implementation
#include <array>
#include <iostream>
int main() {
constexpr unsigned turns = 15;
std::array<std::array<unsigned long long, turns + 1>, turns + 1> weights{};
weights[0][0] = 1;
unsigned long long denominator = 1;
for (unsigned turn = 1; turn <= turns; ++turn) {
denominator *= turn + 1;
for (unsigned blues = 0; blues <= turn; ++blues) {
weights[turn][blues] = turn * weights[turn - 1][blues];
if (blues > 0) {
weights[turn][blues] += weights[turn - 1][blues - 1];
}
}
}
unsigned long long winning_weight = 0;
for (unsigned blues = turns / 2 + 1; blues <= turns; ++blues) {
winning_weight += weights[turns][blues];
}
std::cout << denominator / winning_weight << '\n';
}