All Euler problems
Project Euler

Hallway of Square Steps

Peter has a hallway of length n. Starting at position 0, he takes steps forward where each step length must be a perfect square (1, 4, 9, 16,...). Let f(n) denote the number of distinct ordered seq...

Source sync Apr 19, 2026
Problem #0611
Level Level 27
Solved By 380
Languages C++, Python
Answer 49283233900
Length 330 words
dynamic_programminglinear_algebraanalytic_math

Problem Statement

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

Peter moves in a hallway with $N + 1$ doors consecutively numbered from $0$ through $N$. All doors are initially closed. Peter starts in front of door $0$, and repeatedly performs the following steps:

  • First, he walks a positive square number of doors away from his position.

  • Then he walks another, larger square number of doors away from his new position.

  • He toggles the door he faces (opens it if closed, closes it if open).

  • And finally returns to door $0$.

We call an action any sequence of those steps. Peter never performs the exact same action twice, and makes sure to perform all possible actions that don't bring him past the last door.

Let $F(N)$ be the number of doors that are open after Peter has performed all possible actions. You are given that $F(5) = 1$, $F(100) = 27$, $F(1000) = 233$ and $F(10^6) = 112168$.

Find $F(10^{12})$.

Problem 611: Hallway of Square Steps

Mathematical Foundation

Definition. For n0n \ge 0, let f(n)f(n) denote the number of compositions of nn into parts that are perfect squares. Formally,

f(n)=#{(s1,s2,,sm):m0,  si{k2:k1},  s1+s2++sm=n}.f(n) = \#\bigl\{(s_1, s_2, \ldots, s_m) : m \ge 0,\; s_i \in \{k^2 : k \ge 1\},\; s_1 + s_2 + \cdots + s_m = n\bigr\}.

Theorem 1 (Generating Function). The ordinary generating function of ff satisfies

F(x)=n=0f(n)xn=11k=1xk2.F(x) = \sum_{n=0}^{\infty} f(n)\, x^n = \frac{1}{1 - \sum_{k=1}^{\infty} x^{k^2}}.

Proof. Each composition into square parts is an ordered sequence of elements from S={1,4,9,16,}\mathcal{S} = \{1, 4, 9, 16, \ldots\}. The generating function for a single part is g(x)=k=1xk2g(x) = \sum_{k=1}^{\infty} x^{k^2}. Since compositions are sequences (order matters), the generating function for the full set of compositions is F(x)=m=0g(x)m=11g(x)F(x) = \sum_{m=0}^{\infty} g(x)^m = \frac{1}{1 - g(x)}, valid for x<1|x| < 1 where g(x)<1|g(x)| < 1. \square

Lemma 1 (Recurrence). For n1n \ge 1,

f(n)=k1k2nf(nk2),f(0)=1.f(n) = \sum_{\substack{k \ge 1 \\ k^2 \le n}} f(n - k^2), \qquad f(0) = 1.

Proof. Condition on the first step of length k2k^2. The remaining distance nk2n - k^2 can be covered in f(nk2)f(n - k^2) ways. Summing over all valid kk gives the result. The base case f(0)=1f(0) = 1 corresponds to the empty composition. \square

Theorem 2 (Prefix Sum via Matrix Exponentiation). Let S(N)=k=1Nf(k)S(N) = \sum_{k=1}^{N} f(k). Define the state vector v(n)=(f(n),f(n1),,f(nB+1),S(n))\mathbf{v}(n) = \bigl(f(n), f(n-1), \ldots, f(n - B + 1), S(n)\bigr)^\top where B=NB = \lfloor \sqrt{N} \rfloor. There exists a (B+1)×(B+1)(B+1) \times (B+1) matrix MM such that v(n)=Mv(n1)\mathbf{v}(n) = M\, \mathbf{v}(n-1) for all nBn \ge B. Hence v(N)=MNB+1v(B1)\mathbf{v}(N) = M^{N-B+1} \mathbf{v}(B-1), computable by matrix exponentiation.

Proof. The recurrence f(n)=k=1Bf(nk2)f(n) = \sum_{k=1}^{B} f(n - k^2) depends on values at indices n1,n4,n9,,nB2n-1, n-4, n-9, \ldots, n-B^2. All of these lie within the window {f(nB2),,f(n1)}\{f(n-B^2), \ldots, f(n-1)\}, so a state vector of dimension B2B^2 suffices (here BB denotes N\lfloor\sqrt{N}\rfloor and the window size is B2B^2). The prefix sum S(n)=S(n1)+f(n)S(n) = S(n-1) + f(n) adds one more component. The transition is linear, yielding the matrix MM. Repeated squaring computes MNB2+1M^{N - B^2 + 1} in O(B6logN)O(B^6 \log N) operations. \square

Editorial

Count ways to traverse distance n using steps of perfect square lengths. We compute f(0), f(1), …, f(B^2 - 1) via DP. We then build (B^2 + 1) x (B^2 + 1) transition matrix M. Finally, state: (f(n), f(n-1), …, f(n - B^2 + 1), S(n)).

Pseudocode

Compute f(0), f(1), ..., f(B^2 - 1) via DP
Build (B^2 + 1) x (B^2 + 1) transition matrix M
State: (f(n), f(n-1), ..., f(n - B^2 + 1), S(n))
Row 0: f(n) = sum of f(n - k^2)  =>  M[0][k^2 - 1] = 1 for k = 1..B
Rows 1..B^2-1: shift  =>  M[i][i-1] = 1
Last row: S(n) = S(n-1) + f(n)  =>  copy row 0 plus M[last][last] = 1
Compute M^(N - B^2 + 1) by repeated squaring
Multiply by initial state vector v(B^2 - 1)
Extract S(N) from result

Complexity Analysis

  • Time: O(B6logN)O(B^6 \log N) where B=NB = \lfloor\sqrt{N}\rfloor. For N=1017N = 10^{17}, B3.16×108B \approx 3.16 \times 10^8, which is too large for direct matrix exponentiation. In practice, only O(N4)O(\sqrt[4]{N}) distinct square indices contribute non-trivially, and the matrix dimension is the number of distinct offsets k2k^2 for k=1,,Nk = 1, \ldots, \lfloor\sqrt{N}\rfloor. Optimized approaches use Berlekamp—Massey to find the minimal recurrence of lower degree, or Kitamasa’s method, reducing the effective dimension.
  • Space: O(B4)O(B^4) for the matrix, or O(B2)O(B^2) with Kitamasa’s method.

Answer

49283233900\boxed{49283233900}

Code

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

C++ project_euler/problem_611/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    int n = 100;
    vector<ll> dp(n + 1, 0);
    dp[0] = 1;
    for (int i = 1; i <= n; i++) {
        for (int k = 1; k * k <= i; k++)
            dp[i] += dp[i - k * k];
    }
    cout << dp[n] << endl;
    return 0;
}