Project Euler Lab - Problem 472

#472 - Comfortable Distance II

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

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

  1. No person sits beside another.
  2. The first person chooses any seat.
  3. Each subsequent person chooses the seat furthest from anyone else already seated, as long as it does not violate rule 1. If there is more than one choice satisfying this condition, then the person chooses the leftmost choice.

Note that due to rule 1, some seats will surely be left unoccupied, and the maximum number of people that can be seated is less than \(N\) (for \(N \gt 1\)).

Here are the possible seating arrangements for \(N = 15\):

0472_n15.png

We see that if the first person chooses correctly, the \(15\) seats can seat up to \(7\) people.
We can also see that the first person has \(9\) choices to maximize the number of people that may be seated.

Let \(f(N)\) be the number of choices the first person has to maximize the number of occupants for \(N\) seats in a row. Thus, \(f(1) = 1\), \(f(15) = 9\), \(f(20) = 6\), and \(f(500) = 16\).

Also, \(\sum f(N) = 83\) for \(1 \le N \le 20\) and \(\sum f(N) = 13343\) for \(1 \le N \le 500\).

Find \(\sum f(N)\) for \(1 \le N \le 10^{12}\). Give the last \(8\) digits of your answer.

This problem is taken from Project Euler, Problem 472.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=472. Published Sunday, 18th May 2014, 01:00 am. Solved by 299 members at time of mirroring.

Why this is useful

Optimization. A genuine objective is optimised over a choice set - the same shape as calibration and optimal-execution problems (Phases 10, 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.