Project Euler Lab - Problem 605

#605 - Pairwise Coin-Tossing Game

● AppliedOfficial difficulty: 28%Two-player gamesTier B - browser, with the efficient algorithmNot viewed
↖ Euler Lab

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)\).

This problem is taken from Project Euler, Problem 605.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=605. Published Sunday, 28th May 2017, 07:00 am. Solved by 1,019 members at time of mirroring.

Why this is useful

Probability Statistics. Expectation and state-based probability reasoning underpin pricing, risk, and statistical inference (Phases 7, 11, 13).

We classify relevance honestly - not every Euler problem is a trading application.

Learning mode

Pick how much scaffolding you want. Your choice is remembered per problem.

Scratchpad

Mathematical notes, formulas, pseudocode, hypotheses, complexity notes. Saved automatically with your progress.

Python workbench

Tier B - browser, with the efficient algorithm
Runs in the browser only with the intended efficient algorithm; a naive loop will hit the timeout.

Real Python (Pyodide) in a sandboxed Web Worker - no network, no filesystem, no DOM access. Ctrl/Cmd+Enter runs. Escape leaves the editor. Stop terminates the worker.

Python runtime not loaded (it boots on first run - a one-time local load).

Check your answer

Answers are checked against a salted hash held in a separate file - not printed in this page. This prevents accidental spoilers; it is not cryptographic protection (see the build notes).

Progressive hints

Confidence

Low confidence schedules this problem for spaced review, even if you solved it.

Reference solution

Spoiler
The complete original explanation (interpretation, naive approach, insight, proof, complexity, Python implementation, tests, common mistakes, alternatives) is hidden and lazy-loaded. Reveal it only after a meaningful attempt - the struggle is where the learning happens.