Project Euler Lab - Problem 941

#941 - de Bruijn's Combination Lock

● ResearchOfficial difficulty: 86%CountingTier D - conceptual / notebook executionNot viewed
↖ Euler Lab

de Bruijn has a digital combination lock with \(k\) buttons numbered \(0\) to \(k-1\) where \(k \le 10\).
The lock opens when the last \(n\) buttons pressed match the preset combination.

Unfortunately he has forgotten the combination. He creates a sequence of these digits which contains every possible combination of length \(n\). Then by pressing the buttons in this order he is sure to open the lock.

Consider all sequences of shortest possible length that contains every possible combination of the digits.
Denote by \(C(k, n)\) the lexicographically smallest of these.

For example, \(C(3, 2) = \) 0010211220.

Define the sequence \(a_n\) by \(a_0=0\) and
\[a_n=(920461 a_{n-1}+800217387569)\bmod 10^{12} \text{ for }\ n > 0\] Interpret each \(a_n\) as a \(12\)-digit combination, adding leading zeros for any \(a_n\) with less than \(12\) digits.

Given a positive integer \(N\), we are interested in the order the combinations \(a_1,\dots,a_N\) appear in \(C(10,12)\).
Denote by \(p_n\) the place, numbered \(1,\dots,N\), in which \(a_n\) appears out of \(a_1,\dots,a_N\). Define \(\displaystyle F(N)=\sum_{n=1}^Np_na_n\).

For example, the combination \(a_1=800217387569\) is entered before \(a_2=696996536878\). Therefore: \[F(2)=1\cdot800217387569 + 2\cdot696996536878 = 2194210461325\] You are also given \(F(10)=32698850376317\).

Find \(F(10^7)\). Give your answer modulo \(1234567891\).

This problem is taken from Project Euler, Problem 941.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=941. Published Saturday, 19th April 2025, 08:00 pm. Solved by 134 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 D - conceptual / notebook execution
Too heavy for browser Pyodide at original scale: the browser is used for planning, small cases and reasoning; full scale runs 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.