Project Euler Lab - Problem 984

#984 - Knights and Horses

● ResearchOfficial difficulty: 70%PointsTier D - conceptual / notebook executionNot viewed
↖ Euler Lab

In Western chess, a knight is a piece that moves either two squares horizontally and one square vertically, or one square horizontally and two squares vertically, and is capable of jumping over any intervening pieces.

Chinese chess has a similar piece called the horse, whose moves have an identical displacement as a knight's move; however, a horse, unlike a knight, is unable to jump over intervening pieces.

More specifically, a horse's move consists of two steps: An orthogonal move of one square, followed by a diagonal move by one square in the same direction as the orthogonal move. If the orthogonal square is occupied by another piece, the horse is unable to move in that direction. 0984_KnightHorsesDiag1.jpg Specifically the horse in the centre of the above board can move to the squares \(b_{11},b_{12},b_{21},b_{22},b_{31},b_{32},b_{41},b_{42}\) providing the squares \(a_{1},a_{2},a_{3},a_{4}\) are unoccupied. For example, if \(a_2\) was occupied then it could not move to \(b_{21}\) or \(b_{22}\).

A set of squares on a chessboard is called knight-connected if a knight can travel between any two squares in the set using only legal moves without using any squares not in the set. A set of squares on a chessboard is called horse-disjoint if, when a horse is placed on every square in the set (and no other square), no horse can attack any other. 0984_KnightHorsesDiag2.jpg Let \(f(N)\) be the number of knight-connected, horse-disjoint non-empty subsets of an \(N\times N\) chessboard. For example, \(f(3) = 9\), consisting of the nine singleton sets. You are also given that \(f(5) = 903, f(100) = 8658918531876\), and \(f(10000) \equiv 377956308 \bmod 10^9+7\).

Find \(f(10^{18})\). Give your answer modulo \(10^9+7\).

This problem is taken from Project Euler, Problem 984.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=984. Published Saturday, 14th February 2026, 07:00 pm. Solved by 103 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.