All Euler problems
Project Euler

Lattice Walks with Barriers

Count the number of lattice paths from (0,0) to (m,n) using unit steps right (1,0) and up (0,1), subject to barrier constraints (e.g., the path must not cross certain lines). The solution uses the...

Source sync Apr 19, 2026
Problem #0884
Level Level 19
Solved By 684
Languages C++, Python
Answer 1105985795684653500
Length 366 words
linear_algebraanalytic_mathbrute_force

Problem Statement

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

Starting from a positive integer $n$, at each step we subtract from $n$ the largest perfect cube not exceeding $n$, until $n$ becomes $0$.

For example, with $n = 100$ the procedure ends in $4$ steps: $$100 \xrightarrow{-4^4} 36 \xrightarrow{-3^3} 9 \xrightarrow{-2^3} 1 \xrightarrow{-1^3} 0$$ Let $D(n)$ denote the number of steps of the procedure. Thus $D(100) = 4$.

Let $S(N)$ denote the sum of $D(n)$ for all positive integers $n$ strictly less than $N$.

For example, $S(100) = 512$.

Find $S(10^{17})$.

Problem 884: Lattice Walks with Barriers

Mathematical Analysis

Theorem 1 (Unrestricted Lattice Paths)

The number of paths from (0,0)(0,0) to (m,n)(m,n) using mm right steps and nn up steps is (m+nm)\binom{m+n}{m}.

Proof. Each path is a sequence of m+nm+n steps, choosing mm positions for right steps. \square

Theorem 2 (Reflection Principle / Andre’s Reflection)

The number of paths from (0,0)(0,0) to (m,n)(m,n) that do not touch the line y=x+1y = x + 1 (staying weakly below y=xy = x) is:

(m+nm)(m+nm+1)=mn+1m+1(m+nm)\binom{m+n}{m} - \binom{m+n}{m+1} = \frac{m-n+1}{m+1}\binom{m+n}{m}

This is the ballot problem formula. When m=nm = n, this gives the Catalan number Cn=1n+1(2nn)C_n = \frac{1}{n+1}\binom{2n}{n}.

Proof. By reflection across y=x+1y = x + 1: paths touching the barrier biject with unrestricted paths from (1,1)(-1, 1) to (m,n)(m, n), counted by (m+nm+1)\binom{m+n}{m+1}. \square

Theorem 3 (LGV Lemma)

Let A={a1,,ak}A = \{a_1, \ldots, a_k\} be source points and B={b1,,bk}B = \{b_1, \ldots, b_k\} be destination points. Let e(ai,bj)e(a_i, b_j) be the number of paths from aia_i to bjb_j. Then the number of kk-tuples of non-intersecting paths (path ii goes from aia_i to bib_i) is:

det[e(a1,b1)e(a1,b2)e(a2,b1)e(a2,b2)]\det \begin{bmatrix} e(a_1, b_1) & e(a_1, b_2) & \cdots \\ e(a_2, b_1) & e(a_2, b_2) & \cdots \\ \vdots & & \ddots \end{bmatrix}

Proof. Expand the determinant as σsgn(σ)ie(ai,bσ(i))\sum_\sigma \text{sgn}(\sigma) \prod_i e(a_i, b_{\sigma(i)}). By a sign-reversing involution, all terms with intersecting paths cancel, leaving only non-intersecting configurations. \square

Concrete Numerical Examples

Catalan Numbers (Dyck Paths)

Paths from (0,0)(0,0) to (n,n)(n,n) staying below y=xy = x:

nnCn=1n+1(2nn)C_n = \frac{1}{n+1}\binom{2n}{n}
01
11
22
35
414
542
6132

LGV Example: Two Non-Intersecting Paths

Sources: a1=(0,0)a_1 = (0,0), a2=(0,1)a_2 = (0,1). Destinations: b1=(3,2)b_1 = (3,2), b2=(3,3)b_2 = (3,3).

det((53)(63)(43)(53))=det(1020410)=10080=20\det \begin{pmatrix} \binom{5}{3} & \binom{6}{3} \\ \binom{4}{3} & \binom{5}{3} \end{pmatrix} = \det \begin{pmatrix} 10 & 20 \\ 4 & 10 \end{pmatrix} = 100 - 80 = 20

Verification Table (Ballot Problem)

mmnnUnrestrictedBelow y=xy=xRatio
332050.25
4470140.20
55252420.167
4215100.667

Catalan Number Interpretations

CnC_n counts many combinatorial structures:

  1. Dyck paths from (0,0)(0,0) to (2n,0)(2n,0)
  2. Valid sequences of nn pairs of parentheses
  3. Triangulations of a convex (n+2)(n+2)-gon
  4. Full binary trees with n+1n+1 leaves
  5. Non-crossing partitions of {1,,n}\{1, \ldots, n\}

Generating Function

C(x)=n=0Cnxn=114x2xC(x) = \sum_{n=0}^{\infty} C_n x^n = \frac{1 - \sqrt{1-4x}}{2x}

satisfying C(x)=1+xC(x)2C(x) = 1 + x \cdot C(x)^2.

Asymptotic

Cn4nn3/2πC_n \sim \frac{4^n}{n^{3/2}\sqrt{\pi}}

Complexity Analysis

MethodTimeSpace
Single path countO(m+n)O(m + n)O(1)O(1)
LGV (kk paths)O(k3+k2(m+n))O(k^3 + k^2(m+n))O(k2)O(k^2)
Transfer matrixO(nW3)O(n \cdot W^3)O(W2)O(W^2)

Answer

1105985795684653500\boxed{1105985795684653500}

Code

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

C++ project_euler/problem_884/solution.cpp
/*
 * Problem 884: Lattice Walks with Barriers
 * Catalan numbers, ballot problem, LGV lemma.
 */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

ll comb_val(int n, int r) {
    if (r < 0 || r > n) return 0;
    if (r > n - r) r = n - r;
    ll result = 1;
    for (int i = 0; i < r; i++)
        result = result * (n - i) / (i + 1);
    return result;
}

ll catalan(int n) { return comb_val(2*n, n) / (n + 1); }

ll ballot(int m, int n) {
    if (n > m) return 0;
    return comb_val(m + n, m) - comb_val(m + n, m + 1);
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cout << "=== Catalan Numbers ===" << endl;
    for (int n = 0; n <= 15; n++)
        cout << "C_" << n << " = " << catalan(n) << endl;

    cout << "\n=== Ballot Problem ===" << endl;
    for (auto [m, n] : vector<pair<int,int>>{{3,3},{4,4},{5,5},{4,2},{6,3}}) {
        cout << "ballot(" << m << "," << n << ") = " << ballot(m, n) << endl;
    }

    cout << "\nAnswer: C_10 = " << catalan(10) << endl;
    return 0;
}