Project Euler Lab - Problem 502

#502 - Counting Castles

● ResearchOfficial difficulty: 93%Two-player gamesTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

We define a block to be a rectangle with a height of \(1\) and an integer-valued length. Let a castle be a configuration of stacked blocks.

Given a game grid that is \(w\) units wide and \(h\) units tall, a castle is generated according to the following rules:

  1. Blocks can be placed on top of other blocks as long as nothing sticks out past the edges or hangs out over open space.
  2. All blocks are aligned/snapped to the grid.
  3. Any two neighboring blocks on the same row have at least one unit of space between them.
  4. The bottom row is occupied by a block of length \(w\).
  5. The maximum achieved height of the entire castle is exactly \(h\).
  6. The castle is made from an even number of blocks.

The following is a sample castle for \(w=8\) and \(h=5\):

0502_castles.png

Let \(F(w,h)\) represent the number of valid castles, given grid parameters \(w\) and \(h\).

For example, \(F(4,2) = 10\), \(F(13,10) = 3729050610636\), \(F(10,13) = 37959702514\), and \(F(100,100) \bmod 1\,000\,000\,007 = 841913936\).

Find \((F(10^{12},100) + F(10000,10000) + F(100,10^{12})) \bmod 1\,000\,000\,007\).

This problem is taken from Project Euler, Problem 502.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=502. Published Saturday, 7th February 2015, 04:00 pm. Solved by 370 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 C - reduced scale in browser; full scale in notebook
Browser runs a reduced, clearly-labelled educational scale; the original scale is provided 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.