Hilbert's Blackout
Consider a 2^n x 2^n grid of lights indexed by an order- n Hilbert curve. A blackout operation turns off lights at positions along the curve satisfying specific arithmetic conditions. Determine the...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Despite the popularity of Hilbert's infinite hotel, Hilbert decided to try managing extremely large finite hotels, instead.
To cut costs, Hilbert wished to power the new hotel with his own special generator. Each floor would send power to the floor above it, with the top floor sending power back down to the bottom floor. That way, Hilbert could have the generator placed on any given floor (as he likes having the option) and have electricity flow freely throughout the entire hotel.
Unfortunately, the contractors misinterpreted the schematics when they built the hotel. They informed Hilbert that each floor sends power to another floor at random, instead. This may compromise Hilbert's freedom to have the generator placed anywhere, since blackouts could occur on certain floors.
For example, consider a sample flow diagram for a three-story hotel:

If the generator were placed on the first floor, then every floor would receive power. But if it were placed on the second or third floors instead, then there would be a blackout on the first floor. Note that while a given floor can receive power from many other floors at once, it can only send power to one other floor.
To resolve the blackout concerns, Hilbert decided to have a minimal number of floors rewired. To rewire a floor is to change the floor it sends power to. In the sample diagram above, all possible blackouts can be avoided by rewiring the second floor to send power to the first floor instead of the third floor.
Let $F(n)$ be the sum of the minimum number of floor rewirings needed over all possible power-flow arrangements in a hotel of $n$ floors. For example, $F(3) = 6$, $F(8) = 16276736$, and $F(100) \bmod 135707531 = 84326147$.
Find $F(12344321) \bmod 135707531$.
Problem 522: Hilbert’s Blackout
Mathematical Foundation
Theorem (Hilbert Curve Bijection). For every , the order- Hilbert curve defines a bijection
such that consecutive indices map to grid-adjacent cells (sharing an edge).
Proof. We proceed by induction on .
Base case (): The order-1 curve visits the four cells of a grid in the order . Each consecutive pair is adjacent. The map is clearly a bijection on a 4-element set.
Inductive step: Assume is a bijection on the grid with the adjacency property. Partition the grid into four quadrants. Define by traversing:
- Bottom-left quadrant using reflected about the diagonal ( CW rotation),
- Top-left quadrant using (unmodified),
- Top-right quadrant using (unmodified),
- Bottom-right quadrant using reflected about the anti-diagonal ( CCW rotation).
Each quadrant contributes cells, giving total. The junction points between quadrants are adjacent by construction (the exit of one quadrant neighbors the entry of the next). By induction, within each quadrant consecutive indices are adjacent. Hence is a bijection with the adjacency property.
Lemma (Self-Similar Counting). Let denote the count of surviving lights after the blackout operation on an order- grid. If the blackout condition respects the 4-fold recursive structure of the Hilbert curve (i.e., the condition on index depends only on for some fixed ), then satisfies a linear recurrence of the form
for constants determined by the blackout rule applied to the quadrant junctions.
Proof. When the blackout condition depends only on , the number of blackout positions in each quadrant of an order- curve is determined by the number of blackout positions in an order- curve, shifted by a fixed offset modulo . Since the four quadrants tile the index space into four contiguous blocks of size , each block’s count depends on plus a bounded correction from the offset. The correction is constant in for .
Editorial
Grid blackout puzzle on Hilbert curve grids. We base: enumerate all 4^k positions for small k. We then recursive: exploit self-similarity. Finally, determine counts for each quadrant transformation.
Pseudocode
Base: enumerate all 4^k positions for small k
Recursive: exploit self-similarity
Determine counts for each quadrant transformation
rotate quadrant
Complexity Analysis
- Time (direct enumeration): , since all positions are visited.
- Time (recursive, structure-respecting blackout): , since the recurrence has constant cost per level.
- Space: for the recursion stack, or for direct enumeration.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
// Hilbert curve coordinate conversion
pair<int,int> d2xy(int n, int d) {
int x = 0, y = 0;
for (int s = 1; s < (1 << n); s <<= 1) {
int rx = (d & 2) ? 1 : 0;
int ry = ((d & 1) ^ rx) ? 1 : 0;
if (ry == 0) {
if (rx == 1) {
x = s - 1 - x;
y = s - 1 - y;
}
swap(x, y);
}
x += s * rx;
y += s * ry;
d >>= 2;
}
return {x, y};
}
int xy2d(int n, int x, int y) {
int d = 0;
for (int s = (1 << n) >> 1; s > 0; s >>= 1) {
int rx = (x & s) > 0 ? 1 : 0;
int ry = (y & s) > 0 ? 1 : 0;
d += s * s * ((3 * rx) ^ ry);
if (ry == 0) {
if (rx == 1) {
x = s - 1 - x;
y = s - 1 - y;
}
swap(x, y);
}
}
return d;
}
int main() {
int order;
cout << "Enter Hilbert curve order: ";
cin >> order;
long long total = 1LL << (2 * order);
cout << "Total cells: " << total << endl;
// Example: count lights remaining after every k-th blackout
for (int k : {2, 3, 5, 7}) {
long long blacked = total / k;
long long remaining = total - blacked;
cout << "Blackout every " << k << ": " << remaining << " remaining" << endl;
}
// Verify coordinate mapping for small order
if (order <= 4) {
int side = 1 << order;
cout << "\nHilbert curve path:" << endl;
for (int d = 0; d < total; d++) {
auto [x, y] = d2xy(order, d);
int d2 = xy2d(order, x, y);
if (d != d2) {
cout << "ERROR: d=" << d << " -> (" << x << "," << y << ") -> " << d2 << endl;
}
}
cout << "Coordinate mapping verified." << endl;
}
return 0;
}
"""
Problem 522: Hilbert's Blackout
Grid blackout puzzle on Hilbert curve grids.
"""
def hilbert_d2xy(n: int, d: int):
"""Convert Hilbert curve index d to (x, y) coordinates on 2^n x 2^n grid."""
x = y = 0
s = 1
while s < (1 << n):
rx = 1 if (d & 2) else 0
ry = 1 if ((d & 1) ^ rx) else 0
# Rotate
if ry == 0:
if rx == 1:
x = s - 1 - x
y = s - 1 - y
x, y = y, x
x += s * rx
y += s * ry
d >>= 2
s <<= 1
return x, y
def hilbert_xy2d(n: int, x: int, y: int):
"""Convert (x, y) coordinates to Hilbert curve index on 2^n x 2^n grid."""
d = 0
s = (1 << n) >> 1
while s > 0:
rx = 1 if (x & s) > 0 else 0
ry = 1 if (y & s) > 0 else 0
d += s * s * ((3 * rx) ^ ry)
# Rotate
if ry == 0:
if rx == 1:
x = s - 1 - x
y = s - 1 - y
x, y = y, x
s >>= 1
return d
def generate_hilbert_curve(order: int):
"""Generate all points along a Hilbert curve of given order."""
n_points = 4 ** order
points = [hilbert_d2xy(order, d) for d in range(n_points)]
return points
def blackout_count(order: int, k: int):
"""
Count lights remaining after blacking out every k-th position
along the Hilbert curve of given order.
"""
total = 4 ** order
blacked = total // k
return total - blacked
# Compute blackout counts
for order in range(1, 8):
total = 4 ** order
for k in [2, 3, 5]:
remaining = blackout_count(order, k)
print(f"Order {order} ({total} cells), blackout every {k}: {remaining} remaining")