All Euler problems
Project Euler

Tours on a 4 x n Playing Board

Let T(n) be the number of tours on a 4 x n playing board such that: The tour visits every square exactly once. The tour starts at the top-left corner and ends at the bottom-left corner. Movement is...

Source sync Apr 19, 2026
Problem #0237
Level Level 11
Solved By 1,849
Languages C++, Python
Answer 15836928
Length 452 words
modular_arithmeticlinear_algebrasequence

Problem Statement

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

Let $T(n)$ be the number of tours over a $4 \times n$ playing board such that:

  • The tour starts in the top left corner.

  • The tour consists of moves that are up, down, left, or right one square.

  • The tour visits each square exactly once.

  • The tour ends in the bottom left corner.

The diagram shows one tour over a $4 \times 10$ board:

Problem illustration

$T(10)$ is $2329$. What is $T(10^{12})$ modulo $10^8$?

Problem 237: Tours on a 4 x n Playing Board

Mathematical Foundation

Theorem (Transfer Matrix Representation). The count T(n)T(n) of Hamiltonian paths from the top-left to the bottom-left corner of a 4×n4 \times n grid can be expressed as

T(n)=estartMn1eendT(n) = \mathbf{e}_{\mathrm{start}}^\top \cdot \mathbf{M}^{n-1} \cdot \mathbf{e}_{\mathrm{end}}

where M\mathbf{M} is a k×kk \times k transfer matrix encoding valid column-to-column transitions and estart,eend\mathbf{e}_{\mathrm{start}}, \mathbf{e}_{\mathrm{end}} are boundary state vectors.

Proof. Decompose the grid into columns. At the boundary between columns ii and i+1i+1, the partial Hamiltonian path induces a connectivity pattern among the 4 row positions, specifying which pairs of boundary crossings are connected through the visited portion. This connectivity pattern forms a finite state. The transfer matrix M\mathbf{M} has entry MijM_{ij} equal to the number of valid fillings of a single column that transition from boundary state ii to boundary state jj while maintaining the Hamiltonian property. The product Mn1\mathbf{M}^{n-1} accounts for n1n-1 column transitions in an nn-column grid. \square

Lemma (Linear Recurrence from Characteristic Polynomial). The sequence T(n)T(n) satisfies a linear recurrence of order kk (the dimension of M\mathbf{M}), with characteristic polynomial equal to det(xIM)\det(xI - \mathbf{M}).

Proof. By the Cayley-Hamilton theorem, M\mathbf{M} satisfies its own characteristic polynomial: p(M)=0p(\mathbf{M}) = 0 where p(x)=det(xIM)p(x) = \det(xI - \mathbf{M}). Since T(n)T(n) is a linear function of Mn1\mathbf{M}^{n-1}, it inherits the same recurrence relation defined by the coefficients of p(x)p(x). \square

For the 4×n4 \times n board, careful enumeration of boundary states yields a transfer matrix of dimension k6k \approx 6. The resulting linear recurrence (verified by the initial values including T(10)=2329T(10) = 2329) allows computation via matrix exponentiation modulo 10810^8.

Theorem (Matrix Exponentiation Modular Arithmetic). For any integer mm and modulus MM, the matrix power MmmodM\mathbf{M}^m \bmod M can be computed in O(k3logm)O(k^3 \log m) operations using repeated squaring, where each entry is reduced modulo MM at every step.

Proof. Write mm in binary: m=jbj2jm = \sum_{j} b_j 2^j. Then Mm=j:bj=1M2j\mathbf{M}^m = \prod_{j: b_j=1} \mathbf{M}^{2^j}. Each squaring requires O(k3)O(k^3) multiplications, and there are log2m+1\lfloor \log_2 m \rfloor + 1 squarings. Modular reduction at each step preserves correctness since (AB)modM=((AmodM)(BmodM))modM(AB) \bmod M = ((A \bmod M)(B \bmod M)) \bmod M for matrix entries. \square

Editorial

T(n) = number of Hamiltonian paths on a 4 x n grid from top-left (0,0) to bottom-left (3,0), visiting every cell exactly once. Given T(10) = 2329, find T(10^12) mod 10^8. The sequence satisfies the linear recurrence: T(n) = 2T(n-1) + 2T(n-2) - 2*T(n-3) + T(n-4) with initial values T(1)=1, T(2)=1, T(3)=4, T(4)=8. This was found by: 1. Computing small values via brute-force backtracking. 2. Finding the minimal recurrence via Gaussian elimination. We use matrix exponentiation to compute T(10^12) mod 10^8. We construct the k x k transfer matrix M for 4 x n Hamiltonian paths. We then (top-left to bottom-left), determined by enumerating boundary states. Finally, compute M^(10^12 - 1) mod 10^8 via repeated squaring.

Pseudocode

Construct the k x k transfer matrix M for 4 x n Hamiltonian paths
(top-left to bottom-left), determined by enumerating boundary states
Compute M^(10^12 - 1) mod 10^8 via repeated squaring
Extract T(10^12) from appropriate entry

Complexity Analysis

  • Time: O(k3logn)O(k^3 \log n) where k6k \approx 6 is the transfer matrix dimension and n=1012n = 10^{12}. This gives roughly O(6340)=O(8640)O(6^3 \cdot 40) = O(8640) arithmetic operations.
  • Space: O(k2)O(k^2) for storing the matrix.

Answer

15836928\boxed{15836928}

Code

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

C++ project_euler/problem_237/solution.cpp
#include <bits/stdc++.h>
using namespace std;

/*
 * Problem 237: Tours on a 4 x n Playing Board
 *
 * T(n) = number of Hamiltonian paths on 4 x n grid from (0,0) to (3,0).
 * Find T(10^12) mod 10^8.
 *
 * Recurrence (found by brute-force + Gaussian elimination):
 *   T(n) = 2*T(n-1) + 2*T(n-2) - 2*T(n-3) + T(n-4)
 *   T(1)=1, T(2)=1, T(3)=4, T(4)=8
 *
 * Verified: T(10) = 2329.
 *
 * Use 4x4 companion matrix exponentiation modulo 10^8.
 */

typedef long long ll;
typedef vector<vector<ll>> Mat;

const ll MOD = 1e8;

Mat mat_mul(const Mat& A, const Mat& B) {
    int n = A.size();
    Mat C(n, vector<ll>(n, 0));
    for (int i = 0; i < n; i++)
        for (int k = 0; k < n; k++) {
            if (A[i][k] == 0) continue;
            for (int j = 0; j < n; j++)
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % MOD;
        }
    return C;
}

Mat mat_pow(Mat M, ll p) {
    int n = M.size();
    Mat R(n, vector<ll>(n, 0));
    for (int i = 0; i < n; i++) R[i][i] = 1;
    while (p > 0) {
        if (p & 1) R = mat_mul(R, M);
        M = mat_mul(M, M);
        p >>= 1;
    }
    return R;
}

int main(){
    // Companion matrix for T(n) = 2T(n-1) + 2T(n-2) - 2T(n-3) + T(n-4)
    Mat M = {
        {2, 2, MOD - 2, 1},
        {1, 0, 0, 0},
        {0, 1, 0, 0},
        {0, 0, 1, 0}
    };

    ll N = 1000000000000LL; // 10^12

    // State at n=4: [T(4), T(3), T(2), T(1)] = [8, 4, 1, 1]
    Mat Mk = mat_pow(M, N - 4);

    vector<ll> state = {8, 4, 1, 1};
    ll ans = 0;
    for (int j = 0; j < 4; j++)
        ans = (ans + Mk[0][j] * state[j]) % MOD;

    cout << ans << endl;

    return 0;
}