All Euler problems
Project Euler

Quadtree Encoding

A 2^N x 2^N black-and-white image is encoded via a quadtree: Entirely black region: output 10 (2 bits). Entirely white region: output 11 (2 bits). Mixed region: output 0 followed by encodings of fo...

Source sync Apr 19, 2026
Problem #0287
Level Level 12
Solved By 1,679
Languages C++, Python
Answer 313135496
Length 356 words
geometrycombinatoricsrecursion

Problem Statement

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

The quadtree encoding allows us to describe a $2^N \times 2^N$ black and white image as a sequence of bits (0 and 1). Those sequences are to be read from left to right like this:

  • the first bit deals with the complete $2^N \times 2^N$ region;

  • "0" denotes a split:

    the current $2^n \times 2^n$ region is divided into $4$ sub-regions of dimension $2^{n - 1} \times 2^{n - 1}$,

    the next bits contains the description of the top left, top right, bottom left and bottom right sub-regions - in that order;

  • "10" indicates that the current region contains only black pixels;

  • "11" indicates that the current region contains only white pixels.

Consider the following $4 \times 4$ image (colored marks denote places where a split can occur):

Consider the following $4 \times 4$ image (colored marks denote places where a split can occur):

Problem illustration

This image can be described by several sequences, for example :

$"\textcolor{red}{0}\textcolor{blue}{0}10101010\textcolor{green}{0}1011111011\textcolor{orange}{0}10101010"$, of length $30$, or

$"\textcolor{red}{0}10\textcolor{green}{0}101111101110"$, of length $16$, which is the minimal sequence for this image.

For a positive integer $N$, define $D_N$ as the $2^N \times 2^N$ image with the following coloring scheme:

  • the pixel with coordinates $x = 0, y = 0$ corresponds to the bottom left pixel,

  • if $(x - 2^{N - 1})^2 + (y - 2^{N - 1})^2 \le 2^{2N - 2}$ then the pixel is black,

  • otherwise the pixel is white.

What is the length of the minimal sequence describing $D_{24}$?

Problem 287: Quadtree Encoding

Mathematical Foundation

Theorem (Quadtree Encoding Length Recurrence). Let L(x0,y0,s)L(x_0, y_0, s) be the encoding length of the square region [x0,x0+s)×[y0,y0+s)[x_0, x_0+s) \times [y_0, y_0+s). Then

L(x0,y0,s)={2if the region is uniformly black or white,1+i=14L(Qi)otherwise,L(x_0, y_0, s) = \begin{cases} 2 & \text{if the region is uniformly black or white,} \\ 1 + \sum_{i=1}^{4} L(Q_i) & \text{otherwise,} \end{cases}

where Q1,,Q4Q_1, \ldots, Q_4 are the four quadrants of size s/2s/2.

Proof. By the encoding specification: uniform regions output 2 bits, and mixed regions output 1 bit (the 0 prefix) plus the four sub-encodings. \quad\square

Lemma (Uniform Region Tests). For a square [x0,x0+s)×[y0,y0+s)[x_0, x_0+s) \times [y_0, y_0+s) and the disk {(x,y):(xc)2+(yc)2r2}\{(x,y) : (x-c)^2 + (y-c)^2 \le r^2\} with c=2N1c = 2^{N-1}, r=2N1r = 2^{N-1}:

  1. The region is entirely black if all four corner pixels (x0,y0)(x_0, y_0), (x0+s1,y0)(x_0+s-1, y_0), (x0,y0+s1)(x_0, y_0+s-1), (x0+s1,y0+s1)(x_0+s-1, y_0+s-1) satisfy the disk inequality.
  2. The region is entirely white if the pixel in the region closest to the center (c,c)(c,c) fails the disk inequality. The closest pixel is (clamp(c,x0,x0+s1),clamp(c,y0,y0+s1))(\operatorname{clamp}(c, x_0, x_0+s-1),\, \operatorname{clamp}(c, y_0, y_0+s-1)).

Proof.

(1) The disk (xc)2+(yc)2r2(x-c)^2 + (y-c)^2 \le r^2 is a convex set. Since all pixels in the square lie in the convex hull of the four corners, and the four corners are in the disk, all pixels are in the disk. (Formally: a point in the square can be written as a convex combination of the corners; since the disk is convex, any convex combination of points in the disk lies in the disk.)

(2) If the closest point to the center is outside the disk, then every point in the region is at least as far from the center (by the definition of “closest”), hence outside the disk. \quad\square

Theorem (Quadtree Complexity for a Disk). The quadtree for a disk of radius rr on a 2N×2N2^N \times 2^N grid has O(2N)O(2^N) nodes.

Proof. Only squares that intersect the boundary of the disk need subdivision. At depth dd (squares of side 2Nd2^{N-d}), the circle boundary passes through O(2d)O(2^d) such squares (the boundary has length O(r)=O(2N)O(r) = O(2^N) and each square has side O(2N/2d)O(2^N/2^d), so the number of boundary-touching squares is O(2N/(2N/2d))=O(2d)O(2^N / (2^N/2^d)) = O(2^d)). The total number of internal nodes is d=0NO(2d)=O(2N)\sum_{d=0}^{N} O(2^d) = O(2^N). \quad\square

Editorial

We check all-black: all 4 corners inside disk. We then check all-white: closest point outside disk. Finally, mixed: recurse on 4 quadrants.

Pseudocode

Check all-black: all 4 corners inside disk
Check all-white: closest point outside disk
Mixed: recurse on 4 quadrants

Complexity Analysis

  • Time: O(2N)O(2^N) where N=24N = 24. The quadtree has O(2N)O(2^N) nodes, each requiring O(1)O(1) work.
  • Space: O(N)=O(24)O(N) = O(24) for the recursion stack (depth NN).

Answer

313135496\boxed{313135496}

Code

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

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

const int N = 24;
const long long C = 1LL << (N - 1);  // center = 2^23
const long long R2 = 1LL << (2 * (N - 1));  // radius^2 = 2^46

bool inside(long long x, long long y) {
    long long dx = x - C;
    long long dy = y - C;
    return dx * dx + dy * dy <= R2;
}

long long solve(long long x0, long long y0, long long s) {
    if (s == 1) {
        return 2;  // single pixel: either 10 or 11
    }

    // Check all four corners of the pixel region [x0, x0+s-1] x [y0, y0+s-1]
    long long x1 = x0 + s - 1;
    long long y1 = y0 + s - 1;

    bool c00 = inside(x0, y0);
    bool c10 = inside(x1, y0);
    bool c01 = inside(x0, y1);
    bool c11 = inside(x1, y1);

    if (c00 && c10 && c01 && c11) {
        // All corners inside => all black (since disk is convex, all interior pixels are inside too)
        return 2;
    }

    // Check if entirely outside: find closest point in the square to center
    long long cx = max(x0, min(C, x1));
    long long cy = max(y0, min(C, y1));
    if (!inside(cx, cy)) {
        // Closest point is outside the disk => all white
        return 2;
    }

    // Mixed: subdivide
    long long h = s / 2;
    return 1 + solve(x0, y0, h) + solve(x0 + h, y0, h) +
               solve(x0, y0 + h, h) + solve(x0 + h, y0 + h, h);
}

int main() {
    long long S = 1LL << N;
    cout << solve(0, 0, S) << endl;
    return 0;
}