Project Euler Lab - Problem 325

#325 - Stone Game II

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

A game is played with two piles of stones and two players.
On each player's turn, the player may remove a number of stones from the larger pile.
The number of stones removed must be a positive multiple of the number of stones in the smaller pile.

E.g. Let the ordered pair \((6,14)\) describe a configuration with \(6\) stones in the smaller pile and \(14\) stones in the larger pile, then the first player can remove \(6\) or \(12\) stones from the larger pile.

The player taking all the stones from a pile wins the game.

A winning configuration is one where the first player can force a win. For example, \((1,5)\), \((2,6)\), and \((3,12)\) are winning configurations because the first player can immediately remove all stones in the second pile.

A losing configuration is one where the second player can force a win, no matter what the first player does. For example, \((2,3)\) and \((3,4)\) are losing configurations: any legal move leaves a winning configuration for the second player.

Define \(S(N)\) as the sum of \((x_i + y_i)\) for all losing configurations \((x_i, y_i), 0 \lt x_i \lt y_i \le N\).
We can verify that \(S(10) = 211\) and \(S(10^4) = 230312207313\).

Find \(S(10^{16}) \bmod 7^{10}\).

This problem is taken from Project Euler, Problem 325.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=325. Published Saturday, 19th February 2011, 01:00 pm. Solved by 724 members at time of mirroring.

Why this is useful

Algorithmic Development. Optimal substructure and state-space reasoning are exactly how American-option pricing and optimal execution are solved (Phases 13, 16).

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.