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 Apr 19, 2026
Problem #0116
Level Level 04
Solved By 13,765
Languages C++, Python
Answer 20492570929
Length 384 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 Foundation

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(n1)+fL(nL),nLf_L(n) = f_L(n-1) + f_L(n-L), \quad n \geq L

with fL(k)=1f_L(k) = 1 for 0k<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 n1n - 1 can be tiled in fL(n1)f_L(n-1) ways.
  • It is covered by the right end of a colored tile of length LL. The remaining prefix of length nLn - L can be tiled in fL(nL)f_L(n-L) ways.

These cases are mutually exclusive and exhaustive (every tiling ends with exactly one tile). For 0k<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 (81)+(41)+(31)=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(n1)+f2(n2)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

Count ways to tile a row of 50 black squares using tiles of a single color (red=2, green=3, blue=4) without mixing colors, with at least one colored tile. We compute f_L(n).

Pseudocode

    total = 0
    For each L in {2, 3, 4}:
        Compute f_L(n)
        f = array of size n+1
        For k from 0 to L-1:
            f[k] = 1
        For k from L to n:
            f[k] = f[k-1] + f[k-L]
        total += f[n] - 1
    Return total

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(k1)f_L(k-1) and fL(kL)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;
}