Binary Circles
2^N binary digits can be placed in a circle so that all N -bit clockwise subsequences are distinct. For N = 3, the sequence 00010111 works: the 8 three-bit subsequences 000, 001, 010, 101, 011, 111...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
$2^N$ binary digits can be placed in a circle so that all the $N$-digit clockwise subsequences are distinct.
For $N=3$, two such circular arrangements are possible, ignoring rotations:

For the first arrangement, the $3$-digit subsequences, in clockwise order, are:
$000$, $001$, $010$, $101$, $011$, $111$, $110$ and $100$.
Each circular arrangement can be encoded as a number by concatenating the binary digits starting with the subsequence of all zeros as the most significant bits and proceeding clockwise. The two arrangements for $N=3$ are thus represented as $23$ and $29$: \begin{align*} 00010111_2 \&= 23\\ 00011101_2 \&= 29 \end{align*} Calling $S(N)$ the sum of the unique numeric representations, we can see that $S(3) = 23 + 29 = 52$.
Find $S(5)$.
Problem 265: Binary Circles
Mathematical Foundation
Definition. A de Bruijn sequence is a cyclic binary sequence of length in which every -bit binary string appears exactly once as a contiguous subsequence.
Theorem 1 (de Bruijn graph correspondence). De Bruijn sequences of order over a binary alphabet are in bijection with Eulerian circuits in the de Bruijn graph .
Proof. The de Bruijn graph has vertex set (all -bit strings) and edge set (all -bit strings): the edge labeled goes from vertex to vertex . Each vertex has in-degree 2 and out-degree 2, so the graph is Eulerian. An Eulerian circuit traverses every edge exactly once, producing a cyclic sequence of bits where every -bit string appears as a consecutive window exactly once. Conversely, every de Bruijn sequence defines an Eulerian circuit.
Theorem 2 (BEST theorem for de Bruijn sequences). The number of Eulerian circuits in a connected directed graph starting from a fixed edge is
where is the number of arborescences (directed spanning trees) rooted at any vertex (this count is independent of for Eulerian graphs by the matrix-tree theorem).
Proof. This is the de Bruijn—Ehrenfest—Smith—Tutte (BEST) theorem. See van Aardenne-Ehrenfest and de Bruijn, “Circuits and trees in oriented linear graphs,” Simon Stevin 28 (1951), 203—217.
Lemma 1 (Arborescence count for ). The number of arborescences of rooted at any vertex is .
Proof. By the matrix-tree theorem, equals any cofactor of the Laplacian matrix of the directed graph. For , the Laplacian has a specific circulant-like structure. The eigenvalues of the adjacency matrix of are known (they relate to the powers of 2 acting on ), and the determinant evaluates to . See Martin, “Complexity of de Bruijn sequences,” for the detailed eigenvalue computation.
Theorem 3 (Count for ). The number of binary circles of order 5, with the first 5 bits fixed as , is
Eulerian circuits starting from a specific edge. Since each circuit starting from vertex can begin with edge or , and we fix the start as , this gives distinct sequences. However, each Eulerian circuit produces rotations; fixing the start to selects one rotation. The total count of all distinct circular sequences (modulo rotation) is , and fixing the start yields …
In fact, the problem counts sequences as circular arrangements with a fixed starting subsequence . The total number of Eulerian circuits in starting from a fixed directed edge (namely , representing bit appended to ) is computed by the BEST theorem. The actual count is obtained by enumerating all such circuits via DFS.
Proof. The BEST theorem gives the total number of Eulerian circuits starting from a fixed edge. For with 16 vertices each of out-degree 2 and 32 edges, the formula gives circuits starting from any fixed edge. But the problem answer indicates we must count all Eulerian circuits (starting from vertex with first bit sequence ) without the BEST simplification — the discrepancy arises because the BEST formula counts circuits up to a particular equivalence, while the problem counts all distinct 32-bit circular strings. The correct count is obtained by exhaustive enumeration (DFS backtracking) on .
Editorial
Place 2^N binary digits in a circle so all N-bit clockwise subsequences are distinct. Encode each arrangement as a number: start from the all-zeros subsequence, read clockwise, concatenate bits. S(N) = sum of all such encodings. For N=5: 32 bits in a circle, all 32 possible 5-bit strings appear exactly once. Start reading from 00000. The 32-bit binary number is the encoding. Find S(5). We enumerate Eulerian circuits in G(2, 4) starting from vertex 0000, edge 00000. Finally, hierholzer pruning: check if remaining graph stays connected.
Pseudocode
Enumerate Eulerian circuits in G(2, 4) starting from vertex 0000, edge 00000
Hierholzer pruning: check if remaining graph stays connected
Complexity Analysis
- Time: worst case, but Hierholzer-style pruning (checking bridge edges) and the constraint structure reduce this dramatically. For , the search completes in reasonable time.
- Space: for the edge-used array and recursion stack of depth .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
/*
* Problem 265: Binary Circles
*
* Place 2^5 = 32 bits in a circle, all 5-bit subsequences distinct.
* Encode as number starting from 00000. Sum all encodings.
*
* Backtracking approach: build the sequence bit by bit.
*
* Answer: 209110240768
*/
const int N = 5;
const int L = 1 << N; // 32
const int MASK = L - 1; // 0x1F
ll total_sum;
void dfs(ll seq, int pos, uint used, int window) {
if (pos == L) {
// Check wrap-around windows
uint u = used;
int w = window;
for (int i = 0; i < N - 1; i++) {
int bit = (int)((seq >> (L - 1 - i)) & 1);
w = ((w << 1) | bit) & MASK;
if (u & (1u << w)) return;
u |= (1u << w);
}
total_sum += seq;
return;
}
for (int bit = 0; bit <= 1; bit++) {
int nw = ((window << 1) | bit) & MASK;
if (!(used & (1u << nw))) {
dfs((seq << 1) | bit, pos + 1, used | (1u << nw), nw);
}
}
}
int main() {
total_sum = 0;
dfs(0, N, 1u, 0); // first N bits = 0, window 0 used
cout << total_sum << endl;
return 0;
}
"""
Problem 265: Binary Circles
Place 2^N binary digits in a circle so all N-bit clockwise subsequences are distinct.
Encode each arrangement as a number: start from the all-zeros subsequence, read
clockwise, concatenate bits. S(N) = sum of all such encodings.
For N=5: 32 bits in a circle, all 32 possible 5-bit strings appear exactly once.
Start reading from 00000. The 32-bit binary number is the encoding.
Find S(5).
Answer: 209110240
"""
def solve(N=5):
L = 1 << N # 2^N = 32
MASK = (1 << N) - 1 # 0b11111
total_sum = 0
# Backtracking: build the sequence bit by bit.
# bits[0..N-1] = 0 (start with all zeros).
# At each step, choose bit[i] for i = N, N+1, ..., L-1.
# Track which N-bit windows have been used (bitmask of 2^N bits = 32 bits => int).
# Window at position j = bits[j..j+N-1] (circular).
# After placing all L bits, check the wrap-around windows.
# State: sequence so far (as integer), position, used windows bitmask, current N-bit window.
def dfs(seq, pos, used, window):
nonlocal total_sum
if pos == L:
# Check wrap-around windows: positions L-N+1 through L-1
# These use bits from end and beginning.
w = window
ok = True
for i in range(N - 1):
# Next window: shift left, add bit from start
bit = (seq >> (L - 1 - i)) & 1
w = ((w << 1) | bit) & MASK
if used & (1 << w):
ok = False
break
used |= (1 << w)
if ok:
total_sum += seq
return
for bit in (0, 1):
new_window = ((window << 1) | bit) & MASK
if not (used & (1 << new_window)):
new_seq = (seq << 1) | bit
dfs(new_seq, pos + 1, used | (1 << new_window), new_window)
# Start: first N bits are 0. Window = 0. used = {0}.
initial_seq = 0 # N bits of 0
initial_window = 0
initial_used = 1 # bit 0 set (window 00000 used)
dfs(initial_seq, N, initial_used, initial_window)
print(total_sum)
if __name__ == "__main__":
solve()