guest@cp-base:~/home/combinatorics$
templates/burnsides-lemma.cpp
compilable
$cat templates/burnsides-lemma

Burnside's Lemma

Count distinct objects under symmetry (rotations) using Burnside's lemma.

#burnside#counting#symmetry#necklace#group-theory
Log in to track progress, save custom versions, and organize into collections.Log In
$cat source_code/
cpp
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <bits/stdc++.h>
using namespace std;
using ll = long long;

const ll MOD = 1e9 + 7;

ll power(ll base, ll exp, ll mod) {
    ll res = 1; base %= mod;
    while (exp > 0) {
        if (exp & 1) res = res * base % mod;
        base = base * base % mod;
        exp >>= 1;
    }
    return res;
}

ll modInv(ll x) { return power(x, MOD - 2, MOD); }

ll necklaceCount(ll n, ll k) {
    ll ans = 0;
    for (ll i = 1; i <= n; i++)
        ans = (ans + power(k, __gcd(i, n), MOD)) % MOD;
    return ans % MOD * modInv(n) % MOD;
}
24 linesutf-8
$cat explanation_notes.md
notes_viewer --renderedmarkdown (math enabled)

Burnside's Lemma

Counts distinct objects under a group action (e.g., rotational symmetry).

Formula

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

where XgX^g is the set of elements fixed by group element gg.

Necklace Application

For nn beads with kk colors, considering only rotational symmetry:

Necklaces=1ni=1nkgcd(i,n)\text{Necklaces} = \frac{1}{n} \sum_{i=1}^{n} k^{\gcd(i, n)}

Rotating by ii positions fixes kgcd(i,n)k^{\gcd(i,n)} colorings.

When to Use

  • Counting distinct colorings under rotation

  • "How many distinct objects up to symmetry?"

  • Grid or polygon coloring with rotational equivalence
  • Notes

  • For reflections too, use Polya enumeration (Burnside + reflection group)

  • Requires modular inverse of nn (so MOD must be prime and gcd(n,MOD)=1\gcd(n, \text{MOD}) = 1)
  • Complexity

  • Time — O(nlogn)O(n \log n)

  • Space — O(1)O(1)