Project Euler Lab - Problem 726

#726 - Falling Bottles

● ResearchOfficial difficulty: 67%CountingTier C - reduced scale in browser; full scale in notebookNot viewed
↖ Euler Lab

Consider a stack of bottles of wine. There are \(n\) layers in the stack with the top layer containing only one bottle and the bottom layer containing \(n\) bottles. For \(n=4\) the stack looks like the picture below.

The collapsing process happens every time a bottle is taken. A space is created in the stack and that space is filled according to the following recursive steps:

  • No bottle touching from above: nothing happens. For example, taking \(F\).
  • One bottle touching from above: that will drop down to fill the space creating another space. For example, taking \(D\).
  • Two bottles touching from above: one will drop down to fill the space creating another space. For example, taking \(C\).

This process happens recursively; for example, taking bottle \(A\) in the diagram above. Its place can be filled with either \(B\) or \(C\). If it is filled with \(C\) then the space that \(C\) creates can be filled with \(D\) or \(E\). So there are 3 different collapsing processes that can happen if \(A\) is taken, although the final shape (in this case) is the same.

Define \(f(n)\) to be the number of ways that we can take all the bottles from a stack with \(n\) layers. Two ways are considered different if at any step we took a different bottle or the collapsing process went differently.

You are given \(f(1) = 1\), \(f(2) = 6\) and \(f(3) = 1008\).

Also define \[S(n) = \sum_{k=1}^n f(k).\]

Find \(S(10^4)\) and give your answer modulo \(1\,000\,000\,033\).

This problem is taken from Project Euler, Problem 726.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=726. Published Saturday, 19th September 2020, 08:00 pm. Solved by 235 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.