Disc Game Prize Fund
· Project Euler 121
The maximum prize is £2,269, including the player's £1 stake.
The player needs at least eight blue draws to win. On turn i the bag holds one blue disc and i red discs, whatever was drawn earlier. Encode the red choices by i and the blue choice by x. In the product
P(x)=(1+x)(2+x)⋯(15+x),each choice of x records one blue. The coefficient of xk counts the draw sequences with k blues, with the individual red discs counted separately. The program below builds the coefficients one factor at a time. Multiplying by i+x gives the new coefficient of xk as i times its old coefficient plus the old coefficient of xk−1, taking missing coefficients as zero. There are 2⋅3⋯16=16! equally likely sequences. Adding the coefficients from x8 through x15 and dividing by 16! gives the winning probability
p=209227898880009219406943.The banker collects £1 and pays F pounds with probability p, so the prize must satisfy F≤1/p. Dividing the denominator of p by its numerator gives a quotient of 2269 and a remainder of 3,955,534,333. Paying £2,270 would cost more than £1 per play on average.
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';
}