Project Euler Lab - Problem 837

#837 - Amidakuji

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

Amidakuji (Japanese: 阿弥陀籤) is a method for producing a random permutation of a set of objects.

In the beginning, a number of parallel vertical lines are drawn, one for each object. Then a specified number of horizontal rungs are added, each lower than any previous rungs. Each rung is drawn as a line segment spanning a randomly select pair of adjacent vertical lines.

For example, the following diagram depicts an Amidakuji with three objects (\(A\), \(B\), \(C\)) and six rungs:

0837_amidakuji.png

The coloured lines in the diagram illustrate how to form the permutation. For each object, starting from the top of its vertical line, trace downwards but follow any rung encountered along the way, and record which vertical we end up on. In this example, the resulting permutation happens to be the identity: \(A\mapsto A\), \(B\mapsto B\), \(C\mapsto C\).

Let \(a(m, n)\) be the number of different three-object Amidakujis that have \(m\) rungs between \(A\) and \(B\), and \(n\) rungs between \(B\) and \(C\), and whose outcome is the identity permutation. For example, \(a(3, 3) = 2\), because the Amidakuji shown above and its mirror image are the only ones with the required property.

You are also given that \(a(123, 321) \equiv 172633303 \pmod{1234567891}\).

Find \(a(123456789, 987654321)\). Give your answer modulo \(1234567891\).

This problem is taken from Project Euler, Problem 837.
Problem text © Project Euler, licensed under CC BY-NC-SA 4.0. Original: projecteuler.net/problem=837. Published Saturday, 1st April 2023, 05:00 pm. Solved by 251 members at time of mirroring.

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.