Project Euler Lab - Problem 631

#631 - Constrained Permutations

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

Let \((p_1 p_2 \ldots p_k)\) denote the permutation of the set \({1, ..., k}\) that maps \(p_i\mapsto i\). Define the length of the permutation to be \(k\); note that the empty permutation \(()\) has length zero.

Define an occurrence of a permutation \(p=(p_1 p_2 \cdots p_k)\) in a permutation \(P=(P_1 P_2 \cdots P_n)\) to be a sequence \(1\leq t_1 \lt t_2 \lt \cdots \lt t_k \leq n\) such that \(p_i \lt p_j\) if and only if \(P_{t_i} \lt P_{t_j}\) for all \(i,j \in \{1, \dots, k\}\).

For example, \((1243)\) occurs twice in the permutation \((314625)\): once as the 1st, 3rd, 4th and 6th elements \((3\,\,46\,\,5)\), and once as the 2nd, 3rd, 4th and 6th elements \((\,\,146\,\,5)\).

Let \(f(n, m)\) be the number of permutations \(P\) of length at most \(n\) such that there is no occurrence of the permutation \(1243\) in \(P\) and there are at most \(m\) occurrences of the permutation \(21\) in \(P\).

For example, \(f(2,0) = 3\), with the permutations \(()\), \((1)\), \((1,2)\) but not \((2,1)\).

You are also given that \(f(4, 5) = 32\) and \(f(10, 25) = 294\,400\).

Find \(f(10^{18}, 40)\) modulo \(1\,000\,000\,007\).

This problem is taken from Project Euler, Problem 631.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=631. Published Sunday, 15th July 2018, 10:00 am. Solved by 220 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 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.