Project Euler Lab - Problem 366

#366 - Stone Game III

● ResearchOfficial difficulty: 64%Two-player gamesTier D - conceptual / notebook executionNot viewed
↖ Euler Lab

Two players, Anton and Bernhard, are playing the following game.
There is one pile of \(n\) stones.
The first player may remove any positive number of stones, but not the whole pile.
Thereafter, each player may remove at most twice the number of stones his opponent took on the previous move.
The player who removes the last stone wins.

E.g. \(n=5\).
If the first player takes anything more than one stone the next player will be able to take all remaining stones.
If the first player takes one stone, leaving four, his opponent will take also one stone, leaving three stones.
The first player cannot take all three because he may take at most \(2 \times 1=2\) stones. So let's say he takes also one stone, leaving \(2\). The second player can take the two remaining stones and wins.
So \(5\) is a losing position for the first player.
For some winning positions there is more than one possible move for the first player.
E.g. when \(n=17\) the first player can remove one or four stones.

Let \(M(n)\) be the maximum number of stones the first player can take from a winning position at his first turn and \(M(n)=0\) for any other position.

\(\sum M(n)\) for \(n \le 100\) is \(728\).

Find \(\sum M(n)\) for \(n \le 10^{18}\). Give your answer modulo \(10^8\).

This problem is taken from Project Euler, Problem 366.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=366. Published Saturday, 7th January 2012, 07:00 pm. Solved by 804 members at time of mirroring.

Why this is useful

Mathematical Foundation. Exact integer reasoning and algorithmic efficiency. Foundational rigour and computational discipline rather than a direct trading application.

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 D - conceptual / notebook execution
Too heavy for browser Pyodide at original scale: the browser is used for planning, small cases and reasoning; full scale runs in a local notebook.

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.