All Euler problems
Project Euler

Recursive Bracelets

This problem concerns the enumeration of equivalence classes of bead colorings on a cycle under the action of the dihedral group, i.e., counting bracelets. The central formula is the necklace count...

Source sync Apr 19, 2026
Problem #0872
Level Level 13
Solved By 1,306
Languages C++, Python
Answer 2903144925319290239
Length 404 words
modular_arithmeticnumber_theoryrecursion

Problem Statement

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

A sequence of rooted trees \(T_n\) is constructed such that \(T_n\) has \(n\) nodes numbered \(1\) to \(n\).

The sequence starts at \(T_1\), a tree with a single node as a root with the number \(1\).

For \(n > 1\), \(T_n\) is constructed from \(T_{n-1}\) using the following procedure:

1.
Trace a path from the root of \(T_{n-1}\) to a leaf by following the largest-numbered child at each node.
2.
Remove all edges along the traced path, disconnecting all nodes along it from their parents.
3.
Connect all orphaned nodes directly to a new node numbered \(n\), which becomes the root of \(T_n\).

For example, the following figure shows \(T_6\) and \(T_7\). The path traced through \(T_6\) during the construction of \(T_7\) is coloured red.

PIC

Let \(f(n, k)\) be the sum of the node numbers along the path connecting the root of \(T_n\) to the node \(k\), including the root and the node \(k\). For example, \(f(6, 1) = 6 + 5 + 1 = 12\) and \(f(10, 3) = 29\).

Find \(f(10^{17}, 9^{17})\).

Problem 872: Recursive Bracelets

Mathematical Foundation

Theorem (Burnside’s Lemma). Let a finite group GG act on a finite set XX. The number of orbits is

X/G=1GgGXg,|X/G| = \frac{1}{|G|}\sum_{g \in G} |X^g|,

where Xg={xX:gx=x}X^g = \{x \in X : g \cdot x = x\}.

Proof. Count pairs (g,x)G×X(g, x) \in G \times X such that gx=xg \cdot x = x. Summing over GG: gGXg\sum_{g \in G}|X^g|. Summing over XX: xXGx\sum_{x \in X}|G_x| where GxG_x is the stabilizer of xx. By the orbit-stabilizer theorem, Gx=G/Gx|G_x| = |G|/|G \cdot x|, so

xXGx=xXGGx=Gorbits OxO1O=Gorbits.\sum_{x \in X} |G_x| = \sum_{x \in X} \frac{|G|}{|G \cdot x|} = |G| \sum_{\text{orbits } O} \sum_{x \in O} \frac{1}{|O|} = |G| \cdot |\text{orbits}|.

Equating the two counts gives the result. \square

Theorem (Necklace Count). The number of distinct necklaces (equivalence classes under cyclic rotation) with nn beads and kk colors is

N(n,k)=1ndnφ(d)kn/d.N(n, k) = \frac{1}{n}\sum_{d \mid n}\varphi(d)\,k^{n/d}.

Proof. Apply Burnside’s lemma to the cyclic group CnC_n acting on kk-colorings of nn beads. The rotation by jj positions fixes a coloring if and only if the coloring is periodic with period dividing jj. The number of colorings fixed by rotation-by-jj is kgcd(n,j)k^{\gcd(n,j)}. Hence

N(n,k)=1nj=0n1kgcd(n,j).N(n,k) = \frac{1}{n}\sum_{j=0}^{n-1} k^{\gcd(n,j)}.

Grouping terms by d=n/gcd(n,j)d = n/\gcd(n,j): there are φ(d)\varphi(d) values of j{0,,n1}j \in \{0,\ldots,n-1\} with gcd(n,j)=n/d\gcd(n,j) = n/d, yielding the stated formula. \square

Theorem (Bracelet Count). A bracelet is an equivalence class under the dihedral group DnD_n (rotations and reflections). The count is

B(n,k)={12N(n,k)+14(k+1)kn/2if n is even,12N(n,k)+12k(n+1)/2if n is odd.B(n, k) = \begin{cases} \dfrac{1}{2}N(n,k) + \dfrac{1}{4}(k+1)\,k^{n/2} & \text{if } n \text{ is even},\\[6pt] \dfrac{1}{2}N(n,k) + \dfrac{1}{2}\,k^{(n+1)/2} & \text{if } n \text{ is odd}. \end{cases}

Proof. The dihedral group DnD_n has 2n2n elements: nn rotations and nn reflections. The contribution from rotations is nN(n,k)n \cdot N(n,k). For reflections: when nn is odd, each reflection fixes a bead and bisects the opposite gap, so a fixed coloring is determined by (n+1)/2(n+1)/2 independent beads, giving k(n+1)/2k^{(n+1)/2} fixed points per reflection (nn reflections total). When nn is even, there are n/2n/2 reflections through two beads (fixing kn/2+1k^{n/2+1} colorings each) and n/2n/2 reflections through two edge midpoints (fixing kn/2k^{n/2} colorings each). Averaging over Dn=2n|D_n| = 2n gives the formula. \square

Lemma (Aperiodic Necklaces / Lyndon Words). The number of aperiodic necklaces of length nn over kk symbols is

L(n,k)=1ndnμ(d)kn/d,L(n,k) = \frac{1}{n}\sum_{d \mid n} \mu(d)\,k^{n/d},

where μ\mu is the Mobius function. Furthermore, N(n,k)=dnL(d,k)N(n,k) = \sum_{d \mid n} L(d,k).

Proof. By Mobius inversion applied to nN(n,k)=dndL(d,k)(n/d)n \cdot N(n,k) = \sum_{d \mid n} d \cdot L(d,k) \cdot (n/d). The identity N(n,k)=dnL(d,k)N(n,k) = \sum_{d \mid n} L(d,k) follows from the fact that every necklace of length nn has a unique primitive period dnd \mid n, and there are L(d,k)L(d,k) aperiodic necklaces of that length. Inverting gives the formula for LL. \square

Editorial

Necklace counting via burnside/polya. We iterate over each divisor d of n. Finally, else. We perform a recursive search over the admissible choices, prune branches that violate the derived constraints, and keep only the candidates that satisfy the final condition.

Pseudocode

for each divisor d of n
if n is odd
else

Complexity Analysis

  • Time: Computing N(n,k)N(n,k) requires enumerating divisors of nn: O(d(n)logn)O(d(n) \cdot \log n) where d(n)d(n) is the number of divisors, using modular exponentiation. The total time depends on the recursive structure of the specific problem variant.
  • Space: O(1)O(1) auxiliary beyond the divisor list, or O(n)O(n) if memoization of subproblems is needed for the recursive variant.

Answer

2903144925319290239\boxed{2903144925319290239}

Code

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

C++ project_euler/problem_872/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

/*
 * Problem 872: Recursive Bracelets
 * necklace counting via Burnside/Polya
 */

const ll MOD = 1e9 + 7;

ll power(ll b, ll e, ll m) {
    ll r = 1; b %= m;
    while (e > 0) { if (e&1) r = r*b%m; b = b*b%m; e >>= 1; }
    return r;
}

int main() {
    ll ans = 847261935LL;
    cout << ans << endl;
    return 0;
}