All Euler problems
Project Euler

Lights Out

Consider a w x h grid where each cell is either ON or OFF. Selecting a cell toggles it and all its edge-adjacent neighbors. A state is solvable if there exists a sequence of selections that turns a...

Source sync Apr 19, 2026
Problem #0707
Level Level 31
Solved By 272
Languages C++, Python
Answer 652907799
Length 422 words
modular_arithmeticsequencelinear_algebra

Problem Statement

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

Consider a $w\times h$ grid. A cell is either ON or OFF. When a cell is selected, that cell and all cells connected to that cell by an edge are toggled on-off, off-on. See the diagram for the 3 cases of selecting a corner cell, an edge cell or central cell in a grid that has all cells on (white).

Problem illustration

The goal is to get every cell to be off simultaneously. This is not possible for all starting states. A state is solvable if, by a process of selecting cells, the goal can be achieved.

Let $F(w,h)$ be the number of solvable states for a $w\times h$ grid. You are given $F(1,2)=2$, $F(3,3) = 512$, $F(4,4) = 4096$ and $F(7,11) \equiv 270016253 \pmod{1\,000\,000\,007}$.

Let $f_1=f_2 = 1$ and $f_n=f_{n-1}+f_{n-2}, n \ge 3$ be the Fibonacci sequence and define $$ S(w,n) = \displaystyle \sum_{k=1}^n F(w,f_k)$$ You are given $S(3,3) = 32$, $S(4,5) = 1052960$ and $S(5,7) \equiv 346547294 \pmod{1\,000\,000\,007}$.

Find $S(199,199)$. Give your answer modulo $1\,000\,000\,007$.

Problem 707: Lights Out

Mathematical Foundation

Theorem 1 (Linear Algebra over F2\mathbb{F}_2). The Lights Out puzzle on a w×hw \times h grid is equivalent to a system of linear equations over F2={0,1}\mathbb{F}_2 = \{0, 1\}. The toggle matrix AF2wh×whA \in \mathbb{F}_2^{wh \times wh} has Aij=1A_{ij} = 1 if pressing cell jj toggles cell ii. The number of solvable states is

F(w,h)=2rank(A).F(w, h) = 2^{\operatorname{rank}(A)}.

Proof. A state sF2wh\mathbf{s} \in \mathbb{F}_2^{wh} is solvable iff scol(A)\mathbf{s} \in \operatorname{col}(A), the column space of AA. The number of elements in col(A)\operatorname{col}(A) is 2rank(A)2^{\operatorname{rank}(A)}. \square

Lemma 1 (Block Tridiagonal Structure). Labeling cells row by row, AA has block tridiagonal form:

A=(BIIBIIB)A = \begin{pmatrix} B & I & & \\ I & B & I & \\ & \ddots & \ddots & \ddots \\ & & I & B \end{pmatrix}

where BF2w×wB \in \mathbb{F}_2^{w \times w} is the tridiagonal matrix with 1s on the main diagonal and the sub/super-diagonals, and II is the w×ww \times w identity matrix.

Proof. Pressing cell (r,c)(r, c) toggles cells (r,c)(r, c), (r±1,c)(r \pm 1, c), (r,c±1)(r, c \pm 1) (within bounds). The within-row toggles produce BB on the diagonal blocks; the between-row toggles produce II on the off-diagonal blocks. \square

Theorem 2 (Rank via Characteristic Polynomial Recurrence). Define the sequence of w×ww \times w matrices over F2\mathbb{F}_2: P0=IP_0 = I, P1=BP_1 = B, Pk=BPk1+Pk2P_k = B P_{k-1} + P_{k-2} (where addition is over F2\mathbb{F}_2). Then

nullity(A)=nullity(Ph),\operatorname{nullity}(A) = \operatorname{nullity}(P_h),

and therefore rank(A)=whnullity(Ph)\operatorname{rank}(A) = wh - \operatorname{nullity}(P_h).

Proof. By performing block Gaussian elimination on the block tridiagonal matrix AA using the recurrence Pk=BPk1+Pk2P_k = B P_{k-1} + P_{k-2}, the Schur complement at the last block row is PhP_h. The nullity of AA equals the nullity of PhP_h since the elimination preserves rank. \square

Lemma 2 (Periodicity of PhP_h over F2\mathbb{F}_2). The sequence Phmod2P_h \bmod 2 is periodic with some period πw\pi_w depending on ww. Since PhP_h takes values in F2w×w\mathbb{F}_2^{w \times w}, the period divides (2w21)!(2^{w^2} - 1)! but in practice is much smaller.

Proof. The pair (Ph,Ph1)(P_h, P_{h-1}) lives in the finite set (F2w×w)2(\mathbb{F}_2^{w \times w})^2, which has 22w22^{2w^2} elements. By the pigeonhole principle, the sequence is eventually periodic. Since the recurrence is invertible (Pk2=BPk1+PkP_{k-2} = B P_{k-1} + P_k), it is purely periodic. \square

Theorem 3 (Fibonacci Heights and Pisano Periods). To evaluate S(w,n)=k=1nF(w,fk)S(w, n) = \sum_{k=1}^{n} F(w, f_k), we exploit:

  1. nullity(Ph)\operatorname{nullity}(P_h) depends only on hmodπwh \bmod \pi_w.
  2. fkmodπwf_k \bmod \pi_w is periodic with Pisano period π(πw)\pi(\pi_w).
  3. Therefore F(w,fk)F(w, f_k) is periodic in kk with period dividing π(πw)\pi(\pi_w), and the sum S(w,n)S(w, n) can be computed by finding one period and multiplying.

Proof. Composition of periodic functions: hnullity(Ph)h \mapsto \operatorname{nullity}(P_h) is periodic in hh with period πw\pi_w; the Fibonacci sequence modulo πw\pi_w is periodic with Pisano period π(πw)\pi(\pi_w). The composition knullity(Pfk)k \mapsto \operatorname{nullity}(P_{f_k}) is periodic with period π(πw)\pi(\pi_w). \square

Editorial

Restored canonical Python entry generated from local archive metadata. We compute B (w x w tridiagonal matrix over F_2). We then find the period pi_w of the sequence P_h mod 2. Finally, iterate over each h in {0, 1, …, pi_w - 1}, compute nullity(P_h).

Pseudocode

Compute B (w x w tridiagonal matrix over F_2)
Find the period pi_w of the sequence P_h mod 2
For each h in {0, 1, ..., pi_w - 1}, compute nullity(P_h)
Compute Fibonacci numbers mod pi_w for k = 1..n
Using Pisano period pi(pi_w)
Build the periodic sequence F(w, f_k) mod mod for one period
Actually: F(w, f_k) = 2^{rank(A)} = 2^{w*f_k - nullity(P_{f_k})}
nullity(P_{f_k}) = null[f_k mod pi_w]
But 2^{w*f_k} mod mod requires f_k, not f_k mod pi_w
Use: 2^{w*f_k} mod mod via Fermat: exponent mod (mod-1)
Sum over n terms using periodicity

Complexity Analysis

  • Time: O(w3πw)O(w^3 \cdot \pi_w) to find the period and compute nullities, plus O(π(πw)log(max Fibonacci))O(\pi(\pi_w) \cdot \log(\text{max Fibonacci})) for the summation phase. For w=199w = 199, πw\pi_w is manageable.
  • Space: O(w2πw)O(w^2 \cdot \pi_w) to store the period of matrix pairs, or O(w2)O(w^2) if computed on-the-fly.

Answer

652907799\boxed{652907799}

Code

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

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

// Problem 707: Lights Out
// Restored canonical C++ entry generated from local archive metadata.

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