Open Source · 7 Languages · 900+ Problems

Project Euler Solutions

Complete solutions in C++, Python & Java — with step-by-step mathematical explanations

All Problems

Problem 605: Pairwise Coin-Tossing Game

View on Project Euler

Project Euler Problem 605 Solution

EulerSolve provides an optimized solution for Project Euler Problem 605, Pairwise Coin-Tossing Game, with C++, Python, Java, and a step-by-step mathematical explanation.

Problem Summary In this cyclic pairwise coin-tossing game, the players are visited in a fixed cycle, each round is decided by a fair coin, and a player becomes champion as soon as that same player wins two consecutive rounds in which the player appears. Let \(P_n(k)\) be the probability that player \(k\) wins when there are \(n\) players, and let \(M_n(k)\) be the product of numerator and denominator after \(P_n(k)\) is reduced to lowest terms. The required value is the last eight digits of $M_{10^8+7}(10^4+7).$ Mathematical Approach The implementations use a standard reduction of the game to a pattern-matching problem in a fair coin sequence. Once that reduction is made, the rest is an exercise in counting first occurrences and simplifying an arithmetico-geometric series. Step 1: Reduce the Game to the First Occurrence of \(RL\) Let \(T\) be the position where the pattern \(RL\) appears for the first time in an infinite fair-coin sequence. In the standard reformulation of the game, the decisive moment that produces the champion is exactly this first \(RL\), and the winning label depends only on the round number modulo \(n\). Therefore player \(k\) wins exactly when $T=k+jn$ for some integer \(j\ge 0\). So the whole problem becomes: compute the distribution of \(T\), then sum the probabilities over one residue class modulo \(n\)....

Detailed mathematical approach

Problem Summary

In this cyclic pairwise coin-tossing game, the players are visited in a fixed cycle, each round is decided by a fair coin, and a player becomes champion as soon as that same player wins two consecutive rounds in which the player appears. Let \(P_n(k)\) be the probability that player \(k\) wins when there are \(n\) players, and let \(M_n(k)\) be the product of numerator and denominator after \(P_n(k)\) is reduced to lowest terms. The required value is the last eight digits of

$M_{10^8+7}(10^4+7).$

Mathematical Approach

The implementations use a standard reduction of the game to a pattern-matching problem in a fair coin sequence. Once that reduction is made, the rest is an exercise in counting first occurrences and simplifying an arithmetico-geometric series.

Step 1: Reduce the Game to the First Occurrence of \(RL\)

Let \(T\) be the position where the pattern \(RL\) appears for the first time in an infinite fair-coin sequence. In the standard reformulation of the game, the decisive moment that produces the champion is exactly this first \(RL\), and the winning label depends only on the round number modulo \(n\).

Therefore player \(k\) wins exactly when

$T=k+jn$

for some integer \(j\ge 0\). So the whole problem becomes: compute the distribution of \(T\), then sum the probabilities over one residue class modulo \(n\).

Step 2: Count the Probability That the First \(RL\) Ends at Round \(t\)

Fix \(t\ge 2\). For the first \(RL\) to end exactly at round \(t\), the first \(t-1\) symbols must contain no earlier \(RL\), the symbol at position \(t-1\) must be \(R\), and the symbol at position \(t\) must be \(L\).

A binary word with no \(RL\) must have all \(L\)'s first and all \(R\)'s afterward, so every admissible prefix of length \(t-1\) has the form

$L^aR^{\,t-1-a}\qquad (0\le a\le t-2).$

There are exactly \(t-1\) such prefixes. Since all length-\(t\) coin strings have probability \(2^{-t}\), we get

$\Pr(T=t)=\frac{t-1}{2^t}\qquad (t\ge 2).$

Step 3: Sum Over the Correct Congruence Class

Because player labels repeat every \(n\) rounds, player \(k\) collects exactly the terms with \(t\equiv k\pmod n\). Hence

$P_n(k)=\sum_{j\ge 0}\Pr(T=k+jn)=\sum_{j\ge 0}\frac{k+jn-1}{2^{k+jn}}.$

This is the key series used by all three implementations.

Step 4: Convert the Series to an Arithmetico-Geometric Sum

Set

$q=2^{-n}.$

Then factor out \(2^{-k}\):

$P_n(k)=2^{-k}\sum_{j\ge 0}(k-1+jn)q^j.$

Now use the standard identities

$\sum_{j\ge 0}q^j=\frac{1}{1-q},\qquad \sum_{j\ge 0}jq^j=\frac{q}{(1-q)^2}.$

Substituting them gives

$P_n(k)=2^{-k}\left(\frac{k-1}{1-q}+\frac{nq}{(1-q)^2}\right).$

Step 5: Simplify to a Closed Rational Formula

Let

$X=2^n-1.$

Since \(q=2^{-n}\), we have

$\frac{1}{1-q}=\frac{2^n}{X},\qquad \frac{q}{(1-q)^2}=\frac{2^n}{X^2}.$

Therefore

$P_n(k)=2^{-k}\left(\frac{(k-1)2^n}{X}+\frac{n2^n}{X^2}\right)=\frac{2^{n-k}\big((k-1)X+n\big)}{X^2}.$

Replacing \(X\) by \(2^n-1\) yields the compact form

$P_n(k)=\frac{2^{n-k}\big((k-1)2^n+(n-k+1)\big)}{(2^n-1)^2}.$

Step 6: Pass from \(P_n(k)\) to \(M_n(k)\)

Write

$Y=(k-1)2^n+(n-k+1).$

The unreduced fraction is

$P_n(k)=\frac{2^{n-k}Y}{X^2},\qquad X=2^n-1.$

Because \(X\) is odd, the factor \(2^{n-k}\) never cancels with the denominator. Any reduction can only come from \(\gcd(Y,X^2)\). Modulo \(X\), we have

$Y\equiv (k-1)+(n-k+1)\equiv n\pmod X,$

so

$\gcd(Y,X)=\gcd(n,X).$

For the target value \(n=10^8+7\), \(n\) is prime and Fermat's little theorem gives \(2^n\equiv 2\pmod n\). Hence

$2^n-1\equiv 1\pmod n,$

which implies \(\gcd(n,2^n-1)=1\), so no reduction is needed for the target instance. Therefore

$M_n(k)\equiv 2^{n-k}\big((k-1)2^n+(n-k+1)\big)(2^n-1)^2\pmod{10^8}.$

Worked Example: \(n=6,\ k=2\)

This example shows why the reduction step matters in general, even though the target instance is already coprime.

From the closed form,

$P_6(2)=\frac{2^{6-2}\big((2-1)2^6+(6-2+1)\big)}{(2^6-1)^2}=\frac{16\cdot 69}{63^2}=\frac{1104}{3969}.$

Now divide numerator and denominator by \(3\):

$P_6(2)=\frac{368}{1323}.$

So

$M_6(2)=368\cdot 1323=486864,$

which matches the small checkpoint used by the exact validation logic.

How the Code Works

The C++, Python, and Java implementations do not simulate the game. Instead, they evaluate the closed formula directly modulo \(10^8\), because only the last eight digits are required.

First they compute \(2^n\bmod 10^8\) and \(2^{n-k}\bmod 10^8\) using binary modular exponentiation. Next they build the two closed-form factors \(2^n-1\) and \((k-1)2^n+(n-k+1)\) modulo \(10^8\). Then they multiply

$2^{n-k}\cdot \big((k-1)2^n+(n-k+1)\big)\cdot (2^n-1)\cdot (2^n-1)\pmod{10^8}.$

One implementation also verifies small exact examples and checks the coprimality condition that justifies skipping reduction for the target input, while the streamlined implementations rely on the fixed target parameters. The final value is formatted as an eight-digit string, including leading zeros if necessary.

Complexity Analysis

The dominant work is modular exponentiation, which takes \(O(\log n)\) time. All other arithmetic is constant-time modular multiplication and addition on machine-size integers, so the total memory usage is \(O(1)\).

Footnotes and References

  1. Problem page: https://projecteuler.net/problem=605
  2. Geometric series: Wikipedia - Geometric series
  3. Arithmetico-geometric sequence: Wikipedia - Arithmetico-geometric sequence
  4. Modular exponentiation: Wikipedia - Modular exponentiation
  5. Fermat's little theorem: Wikipedia - Fermat's little theorem

Mathematical approach · C++ solution · Python solution · Java solution

Previous: Problem 604 · All Project Euler solutions · Next: Problem 606

Need help with a problem? Ask me! 💡
e
✦ Euler GLM 5.2
Hi! I'm Euler. Ask me anything about Project Euler problems, math concepts, or solution approaches! 🧮