Project Euler Lab - Problem 364

#364 - Comfortable Distance

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

There are \(N\) seats in a row. \(N\) people come after each other to fill the seats according to the following rules:

  1. If there is any seat whose adjacent seat(s) are not occupied take such a seat.
  2. If there is no such seat and there is any seat for which only one adjacent seat is occupied take such a seat.
  3. Otherwise take one of the remaining available seats.
Let \(T(N)\) be the number of possibilities that \(N\) seats are occupied by \(N\) people with the given rules.
The following figure shows \(T(4)=8\).
0364_comf_dist.gif

We can verify that \(T(10) = 61632\) and \(T(1\,000) \bmod 100\,000\,007 = 47255094\).

Find \(T(1\,000\,000) \bmod 100\,000\,007\).

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

Why this is useful

Algorithmic Development. Graph/state-space search transfers to routing, dependency resolution, and execution-path optimisation (Phases 16, 17).

We classify relevance honestly - not every Euler problem is a trading application.

Prerequisites

Lessons that prepare you:
19.14 Computational Complexity, Feasibility Estimation, and Proving Algorithms Correct · 19.9 Graph Algorithms: BFS, DFS, Dijkstra, and Minimum Spanning Trees

Recommended stepping-stone problems: #575 · #189 · #155

Concepts: graph-theory brute-force-reduction

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.