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

$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 of Hamiltonian paths from the top-left to the bottom-left corner of a grid can be expressed as
where is a transfer matrix encoding valid column-to-column transitions and are boundary state vectors.
Proof. Decompose the grid into columns. At the boundary between columns and , 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 has entry equal to the number of valid fillings of a single column that transition from boundary state to boundary state while maintaining the Hamiltonian property. The product accounts for column transitions in an -column grid.
Lemma (Linear Recurrence from Characteristic Polynomial). The sequence satisfies a linear recurrence of order (the dimension of ), with characteristic polynomial equal to .
Proof. By the Cayley-Hamilton theorem, satisfies its own characteristic polynomial: where . Since is a linear function of , it inherits the same recurrence relation defined by the coefficients of .
For the board, careful enumeration of boundary states yields a transfer matrix of dimension . The resulting linear recurrence (verified by the initial values including ) allows computation via matrix exponentiation modulo .
Theorem (Matrix Exponentiation Modular Arithmetic). For any integer and modulus , the matrix power can be computed in operations using repeated squaring, where each entry is reduced modulo at every step.
Proof. Write in binary: . Then . Each squaring requires multiplications, and there are squarings. Modular reduction at each step preserves correctness since for matrix entries.
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: where is the transfer matrix dimension and . This gives roughly arithmetic operations.
- Space: for storing the matrix.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
"""
Problem 237: Tours on a 4 x n Playing Board
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) = 2*T(n-1) + 2*T(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.
"""
MOD = 10**8
def mat_mul(A, B, mod):
"""Multiply two square matrices modulo mod."""
n = len(A)
C = [[0]*n for _ in range(n)]
for i in range(n):
for k in range(n):
if A[i][k] == 0:
continue
for j in range(n):
C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % mod
return C
def mat_pow(M, p, mod):
"""Compute M^p modulo mod using binary exponentiation."""
n = len(M)
R = [[1 if i == j else 0 for j in range(n)] for i in range(n)]
while p > 0:
if p & 1:
R = mat_mul(R, M, mod)
M = mat_mul(M, M, mod)
p >>= 1
return R
# Recurrence: T(n) = 2*T(n-1) + 2*T(n-2) - 2*T(n-3) + T(n-4)
# Initial: T(1)=1, T(2)=1, T(3)=4, T(4)=8
# Verify small values
T = [0, 1, 1, 4, 8]
for i in range(5, 20):
T.append(2*T[-1] + 2*T[-2] - 2*T[-3] + T[-4])
assert T[10] == 2329, f"T(10) = {T[10]}, expected 2329"
# Companion matrix:
# [T(n) ] [2 2 -2 1] [T(n-1)]
# [T(n-1)] = [1 0 0 0] [T(n-2)]
# [T(n-2)] [0 1 0 0] [T(n-3)]
# [T(n-3)] [0 0 1 0] [T(n-4)]
M = [
[2, 2, MOD - 2, 1],
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0]
]
N = 10**12
# State at n=4: [T(4), T(3), T(2), T(1)] = [8, 4, 1, 1]
Mk = mat_pow(M, N - 4, MOD)
state = [8, 4, 1, 1]
ans = 0
for j in range(4):
ans = (ans + Mk[0][j] * state[j]) % MOD
print(ans)