burnside_necklace(n, k): total = 0 for r = 0..n-1: total += k^{gcd(n, r)} return total / n# D_n (회전 + 반사)burnside_dihedral(n, k): total = burnside_necklace(n, k) * n # 회전 기여 # 반사 기여 if n % 2 == 0: total += n/2 * (k^{n/2} + k^{n/2 + 1}) else: total += n * k^{(n+1)/2} return total / (2*n)
구현
// Burnside's lemma: necklace counting#include <bits/stdc++.h>using namespace std;using ll = long long;ll modpow(ll base, ll exp, ll mod) { ll res = 1; while (exp) { if (exp & 1) res = res * base % mod; base = base * base % mod; exp >>= 1; } return res;}int main() { // 회전만: necklace(n, k) int n, k; cin >> n >> k; ll total = 0; for (int r = 0; r < n; r++) total += modpow(k, gcd(n, r), LLONG_MAX); cout << "Necklace (rotation): " << total / n << "\n"; // 반사 포함 (Dihedral group D_n) total = 0; for (int r = 0; r < n; r++) total += modpow(k, gcd(n, r), LLONG_MAX); if (n % 2 == 1) { total += n * modpow(k, (n + 1) / 2, LLONG_MAX); } else { total += n / 2 * modpow(k, n / 2, LLONG_MAX); total += n / 2 * modpow(k, n / 2 + 1, LLONG_MAX); } cout << "Necklace (dihedral): " << total / (2 * n) << "\n"; return 0;}
# Burnside's lemma: necklace countingimport mathdef modpow(base, exp, mod): res = 1 while exp: if exp & 1: res = res * base % mod base = base * base % mod exp >>= 1 return resn, k = map(int, input().split())# rotation onlytotal = sum(k ** math.gcd(n, r) for r in range(n))print(f"Necklace (rotation): {total // n}")# dihedral (rotation + reflection)total = sum(k ** math.gcd(n, r) for r in range(n))if n % 2 == 1: total += n * k ** ((n + 1) // 2)else: total += n // 2 * k ** (n // 2) total += n // 2 * k ** (n // 2 + 1)print(f"Necklace (dihedral): {total // (2 * n)}")
import java.util.*;public class Main { static long modpow(long base, long exp, long mod) { long res = 1; while (exp > 0) { if ((exp & 1) == 1) res = res * base % mod; base = base * base % mod; exp >>= 1; } return res; } static long gcd(long a, long b) { return b == 0 ? a : gcd(b, a % b); } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int k = sc.nextInt(); long total = 0; for (int r = 0; r < n; r++) total += modpow(k, gcd(n, r), Long.MAX_VALUE); System.out.println("Necklace (rotation): " + total / n); total = 0; for (int r = 0; r < n; r++) total += modpow(k, gcd(n, r), Long.MAX_VALUE); if (n % 2 == 1) { total += n * modpow(k, (n + 1) / 2, Long.MAX_VALUE); } else { total += n / 2 * modpow(k, n / 2, Long.MAX_VALUE); total += n / 2 * modpow(k, n / 2 + 1, Long.MAX_VALUE); } System.out.println("Necklace (dihedral): " + total / (2 * n)); }}
💬 댓글