All Euler problems
Project Euler

Red, Green or Blue Tiles

A row of five black square tiles needs to have a number of its tiles replaced with coloured oblong tiles chosen from: red tiles (length 2), green tiles (length 3), or blue tiles (length 4). If red...

Source sync May 21, 2026
Problem #0116
Level Level 04
Solved By 13,765
Languages C++, Python
Answer 20492570929
Length 457 words
sequencebrute_forcelinear_algebra

Problem Statement

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

A row of five grey square tiles is to have a number of its tiles replaced with coloured oblong tiles chosen from red (length two), green (length three), or blue (length four).

If red tiles are chosen there are exactly seven ways this can be done.

PIC

If green tiles are chosen there are three ways.

PIC

And if blue tiles are chosen there are two ways.

PIC

Assuming that colours cannot be mixed there are \(7 + 3 + 2 = 12\) ways of replacing the grey tiles in a row measuring five units in length.

How many different ways can the grey tiles in a row measuring fifty units in length be replaced if colours cannot be mixed and at least one coloured tile must be used?

Note: This is related to Problem 117.

Problem 116: Red, Green or Blue Tiles

Mathematical Development

Theorem 1 (Single-color tiling recurrence). Let fL(n)f_L(n) denote the number of ways to tile a row of length nn using unit black tiles and colored tiles of length LL (including the all-black tiling). Then:

fL(n)=fL(n−1)+fL(n−L),n≥Lf_L(n) = f_L(n-1) + f_L(n-L), \quad n \geq L

with fL(k)=1f_L(k) = 1 for 0≤k<L0 \leq k < L.

Proof. Consider the rightmost position of a row of length nn. Either:

  • It is covered by a black unit tile. The remaining prefix of length n−1n - 1 can be tiled in fL(n−1)f_L(n-1) ways.
  • It is covered by the right end of a colored tile of length LL. The remaining prefix of length n−Ln - L can be tiled in fL(n−L)f_L(n-L) ways.

These cases are mutually exclusive and exhaustive (every tiling ends with exactly one tile). For 0≤k<L0 \leq k < L, only the all-black tiling is possible, giving fL(k)=1f_L(k) = 1. □\square

Theorem 2 (Independence of colors). When colors cannot be mixed, the total count of tilings with at least one colored tile is:

Answer=∑L∈{2,3,4}(fL(n)−1)\text{Answer} = \sum_{L \in \{2, 3, 4\}} \bigl(f_L(n) - 1\bigr)

Proof. Since colors cannot be mixed, the tiling must use exactly one color type. For each color with tile length LL, the number of tilings with at least one colored tile is fL(n)−1f_L(n) - 1 (subtracting the all-black tiling). The three color choices are mutually exclusive, so the total is the sum. □\square

Lemma 1 (Verification for n=5n = 5). f2(5)=8f_2(5) = 8, f3(5)=4f_3(5) = 4, f4(5)=3f_4(5) = 3, giving (8−1)+(4−1)+(3−1)=7+3+2=12(8-1) + (4-1) + (3-1) = 7 + 3 + 2 = 12.

Proof. For L=2L = 2: f2(0)=f2(1)=1f_2(0) = f_2(1) = 1, f2(2)=2f_2(2) = 2, f2(3)=3f_2(3) = 3, f2(4)=5f_2(4) = 5, f2(5)=8f_2(5) = 8. For L=3L = 3: f3(0)=f3(1)=f3(2)=1f_3(0) = f_3(1) = f_3(2) = 1, f3(3)=2f_3(3) = 2, f3(4)=3f_3(4) = 3, f3(5)=4f_3(5) = 4. For L=4L = 4: f4(0)=⋯=f4(3)=1f_4(0) = \cdots = f_4(3) = 1, f4(4)=2f_4(4) = 2, f4(5)=3f_4(5) = 3. This matches the problem statement. □\square

Theorem 3 (Connection to Fibonacci). f2(n)=Fn+1f_2(n) = F_{n+1} where FkF_k is the kk-th Fibonacci number with F1=F2=1F_1 = F_2 = 1.

Proof. The recurrence f2(n)=f2(n−1)+f2(n−2)f_2(n) = f_2(n-1) + f_2(n-2) with f2(0)=1f_2(0) = 1 and f2(1)=1f_2(1) = 1 is exactly the Fibonacci recurrence shifted by one index. □\square

Editorial

Because colors cannot be mixed, the problem splits into three independent one-color tiling counts. For a fixed tile length LL, every tiling of a row of length nn ends either with a black square or with a colored tile of length LL, which leads immediately to the recurrence for fL(n)f_L(n). After computing that count, the all-black arrangement is subtracted because the problem requires at least one colored tile.

The implementation simply repeats this one-color dynamic program for L=2L=2, 33, and 44. Each pass fills the table up to length 50, extracts fL(50)−1f_L(50)-1, and adds it to the global total. Since the three color choices are disjoint, that sum is the final answer.

Pseudocode

Set the total number of valid replacements to zero.

For each colored tile length L in {2, 3, 4}:
    Build the one-color tiling counts from row length 0 up to 50.
    At each step, combine the tilings that end in a black square with those that end in a colored tile of length L.
    Subtract the all-black tiling for this color and add the remainder to the total.

Return the total over the three colors.

Complexity Analysis

  • Time: O(n)O(n) per color, O(n)O(n) total (three passes of length n=50n = 50).
  • Space: O(L)O(L) per color using a rolling window (since each fL(k)f_L(k) depends only on fL(k−1)f_L(k-1) and fL(k−L)f_L(k-L)), or O(n)O(n) with a full array.

Answer

20492570929\boxed{20492570929}

Code

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

C++ project_euler/problem_116/solution.cpp
#include <bits/stdc++.h>
using namespace std;

int main() {
    const int N = 50;
    long long answer = 0;

    // For each tile length L in {2, 3, 4}, compute f_L(N) - 1
    for (int L = 2; L <= 4; L++) {
        vector<long long> f(N + 1, 0);
        for (int i = 0; i < L && i <= N; i++) f[i] = 1;
        for (int i = L; i <= N; i++) {
            f[i] = f[i - 1] + f[i - L];
        }
        answer += f[N] - 1;
    }

    cout << answer << endl;
    return 0;
}