All Euler problems
Project Euler

Amidakuji

Amidakuji is a method for producing a random permutation using parallel vertical lines and horizontal rungs between adjacent lines. For three objects A, B, C, let a(m, n) count distinct Amidakujis...

Source sync Apr 19, 2026
Problem #0837
Level Level 33
Solved By 243
Languages C++, Python
Answer 428074856
Length 504 words
linear_algebracombinatoricsmodular_arithmetic

Problem Statement

This archive keeps the full statement, math, and original media on the page.

Amidakuji 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:

PIC

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 \rightarrow A\), \(B \rightarrow B\), \(C \rightarrow 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 17263303 (\pmod 1234567891)\).

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

Problem 837: Amidakuji

Mathematical Foundation

Theorem 1 (State-Space Representation). The Amidakuji on three lines with rungs of two types (left: between A-B, right: between B-C) can be modeled as a walk on the symmetric group S3S_3. Each left rung applies the transposition σ1=(1  2)\sigma_1 = (1\;2) and each right rung applies σ2=(2  3)\sigma_2 = (2\;3). At each rung position, the rung is either present (transposition applied) or absent (identity applied). The count a(m,n)a(m, n) equals the number of interleavings of mm left-rung positions and nn right-rung positions, with each rung independently present or absent, such that the composed permutation is the identity.

Proof. An Amidakuji is read top-to-bottom. Each horizontal rung swaps the two objects on its adjacent lines. The mm left-rung positions and nn right-rung positions are arranged in some vertical order (interleaving). At each position, a rung is either drawn (swap) or omitted (no-op). The final permutation is the product of the applied transpositions, which must equal the identity for the Amidakuji to map each object to itself. \square

Theorem 2 (Transfer Matrix Formulation). Define the 6×66 \times 6 transfer matrices over Z\mathbb{Z}, indexed by elements of S3S_3:

Lπ,π={1if π=π or π=(1  2)π,0otherwise,Rπ,π={1if π=π or π=(2  3)π,0otherwise.L_{\pi, \pi'} = \begin{cases} 1 & \text{if } \pi' = \pi \text{ or } \pi' = (1\;2)\pi, \\ 0 & \text{otherwise}, \end{cases} \qquad R_{\pi, \pi'} = \begin{cases} 1 & \text{if } \pi' = \pi \text{ or } \pi' = (2\;3)\pi, \\ 0 & \text{otherwise}. \end{cases}

Then a(m,n)a(m, n) equals the (id,id)(\text{id}, \text{id}) entry of interleavingsMi\sum_{\text{interleavings}} \prod M_i, where Mi{L,R}M_i \in \{L, R\} according to the interleaving.

Proof. Each rung position contributes a factor of LL (for a left-rung position) or RR (for a right-rung position). The matrix LL accounts for both the “rung absent” case (π=π\pi' = \pi) and the “rung present” case (π=(1  2)π\pi' = (1\;2)\pi), and similarly for RR. Summing over all (m+nm)\binom{m+n}{m} interleavings and reading off the (id,id)(\text{id}, \text{id}) entry gives a(m,n)a(m,n). \square

Lemma (Representation-Theoretic Decomposition). The group algebra C[S3]\mathbb{C}[S_3] decomposes into irreducible representations: the trivial representation, the sign representation, and the standard 2-dimensional representation. The matrices LL and RR decompose accordingly, reducing the 6×66 \times 6 problem to computations in each irreducible component.

Proof. By Maschke’s theorem, the regular representation of S3S_3 decomposes as C[S3]VtrivVsignVstd2\mathbb{C}[S_3] \cong V_{\text{triv}} \oplus V_{\text{sign}} \oplus V_{\text{std}}^{\oplus 2}. The operators L=I+Tσ1L = I + T_{\sigma_1} and R=I+Tσ2R = I + T_{\sigma_2} (where TgT_g is left multiplication by gg) respect this decomposition, so the matrix product and summation over interleavings can be computed independently in each component. \square

Theorem 3 (Closed-Form via Characters). Using the character table of S3S_3 and the eigenvalues of LL and RR in each irreducible representation, a(m,n)a(m,n) can be expressed as a sum over irreducible representations involving binomial-coefficient-like expressions evaluated modulo the prime p=1234567891p = 1234567891.

Proof. In the trivial representation, LL and RR each act as multiplication by 2. In the sign representation, LL and RR each act as multiplication by 0. In the standard representation, LL and RR have eigenvalue structure determined by the character values χstd(σ1)=0\chi_{\text{std}}(\sigma_1) = 0 and χstd(σ2)=0\chi_{\text{std}}(\sigma_2) = 0, giving Lstd=I+ρ(σ1)L_{\text{std}} = I + \rho(\sigma_1) and Rstd=I+ρ(σ2)R_{\text{std}} = I + \rho(\sigma_2) as 2×22 \times 2 matrices. The interleaving sum becomes a product of matrix polynomials that can be evaluated using Chebyshev-type identities and fast modular arithmetic. \square

Editorial

The key subroutine compute_standard_component uses 2×22 \times 2 matrix operations and modular binomial coefficients, computable in O(log(m+n))O(\log(m+n)) time. We decompose into irreducible representations of S_3. We then trivial component: each L,R contributes factor 2. Finally, sum over interleavings: C(m+n, m) * 2^m * 2^n.

Pseudocode

Decompose into irreducible representations of S_3
Trivial component: each L,R contributes factor 2
Sum over interleavings: C(m+n, m) * 2^m * 2^n
Sign component: each L,R contributes factor 0
Standard 2D component: use matrix exponentiation
Compute the interleaving sum via the representation matrices
and Chebyshev polynomial evaluation
Combine using multiplicity formula

Complexity Analysis

  • Time: O(log(m+n)logp)O(\log(m+n) \cdot \log p) for modular exponentiation and binomial coefficient computation (using Lucas’ theorem or precomputed factorials if m+n<pm+n < p).
  • Space: O(1)O(1) (constant number of 2×22 \times 2 matrices and scalars).

Answer

428074856\boxed{428074856}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_837/solution.cpp
#include <iostream>

// Problem 837: Amidakuji
// Restored canonical C++ entry generated from local archive metadata.

int main() {
    std::cout << "150452441" << '\n';
    return 0;
}