All Euler problems
Project Euler

Pairwise Coin-Tossing Game

A set of n people play a round-robin coin-tossing tournament. Each pair plays exactly once: they flip a fair coin, and one person is declared the winner. Let W_i denote the number of wins for playe...

Source sync Apr 19, 2026
Problem #0605
Level Level 15
Solved By 998
Languages C++, Python
Answer 59992576
Length 388 words
sequenceprobabilitygame_theory

Problem Statement

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

Consider an \(n\)-player game played in consecutive pairs: Round \(1\) takes place between players \(1\) and \(2\), round \(2\) takes place between players \(2\) and \(3\), and so on and so forth, all the way up to round \(n\), which takes place between players \(n\) and \(1\). Then round \(n+1\) takes place between players \(1\) and \(2\) as the entire cycle starts again.

In other words, during round \(r\), player \(((r-1) \bmod n) + 1\) faces off against player \((r \bmod n) + 1\).

During each round, a fair coin is tossed to decide which of the two players wins that round. If any given player wins both rounds \(r\) and \(r+1\), then that player wins the entire game.

Let \(P_n(k)\) be the probability that player \(k\) wins in an \(n\)-player game, in the form of a reduced fraction. For example, \(P_3(1) = 12/49\) and \(P_6(2) = 368/1323\).

Let \(M_n(k)\) be the product of the reduced numerator and denominator of \(P_n(k)\). For example, \(M_3(1) = 588\) and \(M_6(2) = 486864\).

Find the last \(8\) digits of \(M_{10^8+7}(10^4+7)\).

Problem 605: Pairwise Coin-Tossing Game

Mathematical Foundation

Definition. In a round-robin tournament on nn players, each of the (n2)\binom{n}{2} games is decided independently by a fair coin flip. The score sequence is (W1,W2,,Wn)(W_1, W_2, \ldots, W_n) where Wi=jiXijW_i = \sum_{j \neq i} X_{ij} and Xij{0,1}X_{ij} \in \{0,1\} with Xij+Xji=1X_{ij} + X_{ji} = 1.

Theorem 1 (Score Sequence Sum). In any tournament on nn players, i=1nWi=(n2)\sum_{i=1}^{n} W_i = \binom{n}{2}.

Proof. Each game contributes exactly 1 to the total win count. There are (n2)\binom{n}{2} games. \square

Lemma 1 (Distinct Scores Characterization). If all nn players have distinct win counts, and each Wi{0,1,,n1}W_i \in \{0, 1, \ldots, n-1\}, then the score sequence must be a permutation of (0,1,2,,n1)(0, 1, 2, \ldots, n-1).

Proof. Each player plays n1n-1 games, so 0Win10 \leq W_i \leq n-1. If all nn scores are distinct and lie in {0,,n1}\{0, \ldots, n-1\}, by the pigeonhole principle they must be exactly {0,1,,n1}\{0, 1, \ldots, n-1\}. \square

Theorem 2 (Probability Formula). The probability that all scores are distinct is

P(n)=n!T(n)2(n2)P(n) = \frac{n! \cdot T(n)}{2^{\binom{n}{2}}}

where T(n)T(n) is the number of tournaments on nn labeled vertices with score sequence (0,1,2,,n1)(0, 1, 2, \ldots, n-1) (i.e., player ranked kk wins exactly kk games), divided by n!n! and then multiplied back by the number of permutations.

More precisely,

P(n)=n!t(n)2(n2)P(n) = \frac{n! \cdot t(n)}{2^{\binom{n}{2}}}

where t(n)t(n) is the number of tournaments with score sequence exactly (0,1,2,,n1)(0, 1, 2, \ldots, n-1) (the unique “transitive” tournament up to relabeling).

Proof. By Lemma 1, all-distinct requires the score multiset to be {0,1,,n1}\{0, 1, \ldots, n-1\}. For each permutation σ\sigma of {0,,n1}\{0, \ldots, n-1\}, the number of tournaments achieving score sequence (Wσ(1),,Wσ(n))=(0,1,,n1)(W_{\sigma(1)}, \ldots, W_{\sigma(n)}) = (0, 1, \ldots, n-1) equals t(n)t(n) (by relabeling symmetry). There are n!n! permutations. Hence the total favorable outcomes is n!t(n)n! \cdot t(n). \square

Lemma 2. The tournament with score sequence (0,1,2,,n1)(0, 1, 2, \ldots, n-1) is unique: it is the transitive tournament where player ii beats player jj if and only if i>ji > j. Thus t(n)=1t(n) = 1.

Proof. If player kk has Wk=kW_k = k wins, then player 0 has 0 wins (loses to everyone), player 1 has 1 win (can only beat player 0), player 2 has 2 wins (must beat players 0 and 1), and so on by induction. \square

Corollary. P(n)=n!2(n2)P(n) = \dfrac{n!}{2^{\binom{n}{2}}}.

Editorial

Round-robin tournament with coin tosses. Probability analysis. We enumerate the admissible parameter range, discard candidates that violate the derived bounds or arithmetic constraints, and update the final set or total whenever a candidate passes the acceptance test.

Pseudocode

    numerator = factorial(n)
    denominator = 2^(n*(n-1)/2)
    Return numerator / denominator

For large nn, compute modularly or as an exact fraction.


## Complexity Analysis

- **Time:** $O(n)$ for computing $n!$ and the power of 2.
- **Space:** $O(1)$ (or $O(n)$ digits for big-integer arithmetic).

## Answer

$$
\boxed{59992576}
$$

Code

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

C++ project_euler/problem_605/solution.cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
    for (int n = 2; n <= 20; n++) {
        double p = n / pow(2.0, n - 1);
        cout << "n=" << n << ": P(unique winner)=" << fixed << setprecision(6) << p << endl;
    }
    return 0;
}