Project Euler Lab - Problem 907

#907 - Stacking Cups

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

An infant's toy consists of \(n\) cups, labelled \(C_1,\dots,C_n\) in increasing order of size.

0907_four_cups.png

The cups may be stacked in various combinations and orientations to form towers. The cups are shaped such that the following means of stacking are possible:

  • Nesting: \(C_k\) may sit snugly inside \(C_{k+1}\).
    0907_nesting.png
  • Base-to-base: \(C_{k+2}\) or \(C_{k-2}\) may sit, right-way-up, on top of an up-side-down \(C_k\), with their bottoms fitting together snugly.
    0907_base_to_base.png
  • Rim-to-rim: \(C_{k+2}\) or \(C_{k-2}\) may sit, up-side-down, on top of a right-way-up \(C_k\), with their tops fitting together snugly.
    0907_rim_to_rim.png
  • For the purposes of this problem, it is not permitted to stack both \(C_{k+2}\) and \(C_{k-2}\) rim-to-rim on top of \(C_k\), despite the schematic diagrams appearing to allow it:
    0907_rim_to_rim_counter_example.png

Define \(S(n)\) to be the number of ways to build a single tower using all \(n\) cups according to the above rules.
You are given \(S(4)=12\), \(S(8)=58\), and \(S(20)=5560\).

Find \(S(10^7)\), giving your answer modulo \(1\,000\,000\,007\).

This problem is taken from Project Euler, Problem 907.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=907. Published Saturday, 14th September 2024, 11:00 pm. Solved by 329 members at time of mirroring.

Why this is useful

Mathematical Foundation. Exact counting underlies discrete pricing lattices, scenario enumeration, and combinatorial probability (Phase 7).

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.