All Euler problems
Project Euler

Window into a Matrix

Consider a 2 x n binary matrix where every 2 x k contiguous submatrix (window) has entry sum exactly k. Let A(k, n) count the number of such matrices. Given: A(3, 9) = 560, A(4, 20) = 1,060,870. Fi...

Source sync Apr 19, 2026
Problem #0743
Level Level 13
Solved By 1,469
Languages C++, Python
Answer 259158998
Length 440 words
linear_algebramodular_arithmeticcombinatorics

Problem Statement

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

A window into a matrix is a contiguous sub matrix.

Consider a \(2\times n\) matrix where every entry is either 0 or 1.

Let \(A(k,n)\) be the total number of these matrices such that the sum of the entries in every \(2\times k\) window is \(k\).

You are given that \(A(3,9) = 560\) and \(A(4,20) = 1060870\).

Find \(A(10^8,10^{16})\). Give your answer modulo \(1\,000\,000\,007\).

Problem 743: Window into a Matrix

Mathematical Foundation

Theorem 1 (Column-Sum Periodicity). Let cjc_j denote the column sum of column jj (so cj{0,1,2}c_j \in \{0, 1, 2\}). The window constraint i=jj+k1ci=k\sum_{i=j}^{j+k-1} c_i = k for all valid jj implies that the column-sum sequence is periodic with period dividing kk, and each period of kk consecutive sums totals kk.

Proof. For any jj with 1jnk1 \le j \le n - k, we have:

i=jj+k1ci=kandi=j+1j+kci=k.\sum_{i=j}^{j+k-1} c_i = k \quad \text{and} \quad \sum_{i=j+1}^{j+k} c_i = k.

Subtracting: cj+kcj=0c_{j+k} - c_j = 0, so cj+k=cjc_{j+k} = c_j for all valid jj. Hence the sequence (cj)(c_j) is periodic with period dividing kk. Within one period, i=1kci=k\sum_{i=1}^{k} c_i = k. \square

Theorem 2 (Decomposition into Column Patterns). A valid 2×n2 \times n matrix is determined by:

  1. A column-sum pattern (c1,,ck)(c_1, \ldots, c_k) with ci{0,1,2}c_i \in \{0, 1, 2\}, ci=k\sum c_i = k, periodic with period dividing kk.
  2. For each column with ci=1c_i = 1: a choice of which of the two cells is 11 (2 choices).
  3. Columns with ci=0c_i = 0 or ci=2c_i = 2 are fully determined.

Proof. The window constraint is equivalent to column-sum periodicity by Theorem 1. Given the column sums, each column with sum 00 is (0,0)T(0,0)^T, each with sum 22 is (1,1)T(1,1)^T, and each with sum 11 has exactly 22 arrangements: (1,0)T(1,0)^T or (0,1)T(0,1)^T. The window constraint does not impose further coupling between the specific cell values beyond the column sums (since the constraint is purely on sums). \square

Lemma 1 (Counting Formula). Let mm denote the number of columns with ci=1c_i = 1 in one period. The total count of matrices for a given column-sum pattern with nn columns is 2mn/k+m2^{m \cdot \lfloor n/k \rfloor + m'} where mm' accounts for partial periods. For knk \mid n, this simplifies to 2m(n/k)2^{m \cdot (n/k)}.

Proof. Each column with sum 11 contributes a factor of 22 independently. In n/kn/k complete periods, there are m(n/k)m \cdot (n/k) such columns. \square

Theorem 3 (Closed-Form for A(k,n)A(k, n)). When knk \mid n:

A(k,n)=(c1,,ck)ci{0,1,2},ci=k2{i:ci=1}n/kA(k, n) = \sum_{\substack{(c_1, \ldots, c_k) \\ c_i \in \{0,1,2\}, \sum c_i = k}} 2^{|\{i : c_i = 1\}| \cdot n/k}

The inner sum counts compositions of kk into parts from {0,1,2}\{0, 1, 2\} of length kk. Let mm denote the number of 11‘s among the cic_i. If there are mm ones, then there are kmk - m positions for 00‘s and 22‘s, with (km)/2(k - m)/2 each (requiring kmk - m even). The number of such patterns is (km)(km(km)/2)\binom{k}{m} \binom{k - m}{(k-m)/2}. Thus:

A(k,n)=m=0km evenk(km)(km(km)/2)2mn/kA(k, n) = \sum_{\substack{m = 0 \\ k - m \text{ even}}}^{k} \binom{k}{m} \binom{k-m}{(k-m)/2} \cdot 2^{mn/k}

Proof. We must choose which mm of the kk positions have ci=1c_i = 1, then distribute the remaining kmk - m sum among kmk - m positions using only 00‘s and 22‘s: this requires (km)/2(k-m)/2 twos and (km)/2(k-m)/2 zeros, giving (km(km)/2)\binom{k-m}{(k-m)/2} arrangements. Each such pattern generates 2mn/k2^{mn/k} matrices over n/kn/k periods. Summing over valid mm yields the formula. \square

Lemma 2 (Modular Evaluation). For k=108k = 10^8 and n=1016n = 10^{16} (so n/k=108n/k = 10^8), the sum involves binomial coefficients mod(109+7)\bmod (10^9+7) and modular exponentials 2m1082^{m \cdot 10^8}, computable via Lucas’ theorem and fast exponentiation.

Proof. Since 109+710^9 + 7 is prime and k=108<109+7k = 10^8 < 10^9 + 7, all binomial coefficients (km)\binom{k}{m} can be computed using precomputed factorials modulo pp. The powers 2mn/k2^{mn/k} are computed via fast modular exponentiation in O(log(mn/k))O(\log(mn/k)) time. \square

Editorial

A 2×n2 \times n binary matrix where every 2×k2 \times k contiguous window sums to exactly kk. A(k,n)A(k, n) counts such matrices. Given A(3,9)=560A(3,9) = 560 and A(4,20)=1060870A(4,20) = 1060870. Find $A(10^8, 10^{16}) \bmod. We precompute factorials mod p up to k. We then binom(k, m) * binom(k-m, half). Finally, 2^(m * ratio).

Pseudocode

Precompute factorials mod p up to k
binom(k, m) * binom(k-m, half)
2^(m * ratio)

Complexity Analysis

  • Time: O(k)O(k) for factorial precomputation and the main summation loop. For k=108k = 10^8, this requires approximately 10810^8 operations.
  • Space: O(k)O(k) for factorial and inverse factorial arrays.

Answer

259158998\boxed{259158998}

Code

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

C++ project_euler/problem_743/solution.cpp
#include <bits/stdc++.h>
using namespace std;
/*
 * Problem 743: Window into a Matrix
 * A $2 \times n$ binary matrix where every $2 \times k$ contiguous window sums to exactly $k$. $A(k, n)$ counts such matri
 */
int main() {
    printf("Problem 743: Window into a Matrix\n");
    // See solution.md for algorithm details
    return 0;
}