Necklace of Beads

Description

Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < 24 ). If the repetitions that are produced by rotation around the center of the circular necklace or reflection to the axis of symmetry are all neglected, how many different forms of the necklace are there?

Input

The input has several lines, and each line contains the input data n.
-1 denotes the end of the input file.

Output

The output should contain the output data: Number of different forms, in each line correspondent to the input data.

Sample Input

4
5
-1

Sample Output

21
39

Solution

polya定理模板题

\(\overline{G}\)是n个对象的一个置换群, 用m种颜色染图这n个对象,则不同的染色方案数为:
\[L=\frac{1}{|\overline{G}|}[m^{c(\overline{p_1}})+m^{c(\overline{p_2}})+…+m^{c(\overline{p_g}})]\]
其中 \(\overline{G}=\{\overline{p_1},\overline{p_2},…,\overline{p_g}\}\)\(c(\overline{p_k})\)\(\overline{p_k}\) 的循环节数(阶)

如对于n=4:
单位元:仅有(1)(2)(3)(4)一种情况,\((1)^4*1\)
考虑旋转\(\pm90^\circ\),有\((1234)^1\),\((1432)^1\)两种情况,\((4)^1*2\)
考虑旋转\(180^\circ\),有(13)(24)一种情况,\((2)^2*1\)
考虑以两个对立顶点为轴翻转,有(1)(3)(24),(2)(4)(13)两种情况,\((1)^2(2)^1*2\)
考虑以一不经过任一顶点但平分多边形的直径翻转,有(12)(34),(13)(24)两种情况,\((2)^2*2\)
所以答案为\[\frac{3^4+3^1*2+3^2+3^3*2+3^2*2}{1+2+1+2+2}=21\]

#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define LONG_LONG_MAX 9223372036854775807LL
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
int n, m, k;
const int maxn = 1e5 + 10;
ll p[25];
void init()
{
    p[1] = 3;
    for (int i = 2; i < 24; i++)
        p[i] = p[i - 1] * 3;
}
ll solve(int x)
{
    ll t = 1;
    ll res = p[x];
    if (x & 1)
    {
        for (int i = 1; i <= (x - 1) / 2; i++)
        {
            int tmp = 0;
            set<int> s1, s2;
            s1.clear();
            for (int j = 1; j <= x; j++)
                s1.insert(j);
            while (!s1.empty())
            {
                s2.clear();
                int begin = *s1.begin();
                while (!s2.count(begin))
                {
                    // cout << begin << " ";
                    s2.insert(begin);
                    s1.erase(begin);
                    begin = (begin + i) % x;
                    if (!begin)
                        begin = x;
                }
                // cout << "\n";
                ++tmp;
            }
            t += 2;
            res += 2 * p[tmp];
        }
        res += x * p[(x + 1) / 2];
        t += x;
        return res / t;
    }
    else
    {
        int e = x / 2;
        for (int i = 1; i <= e; i++)
        {
            int tmp = 0;
            set<int> s1, s2;
            s1.clear();
            for (int j = 1; j <= x; j++)
                s1.insert(j);
            while (!s1.empty())
            {
                s2.clear();
                int begin = *s1.begin();
                while (!s2.count(begin))
                {
                    // cout << begin << " ";
                    s2.insert(begin);
                    s1.erase(begin);
                    begin = (begin + i) % x;
                    if (!begin)
                        begin = x;
                }
                // cout << "\n";
                ++tmp;
            }
            if (i != e)
            {
                t += 2;
                res += 2 * p[tmp];
            }
            else
            {
                ++t;
                res += p[tmp];
            }
        }
        t += e;
        res += e * p[e + 1];
        t += e;
        res += e * p[e];
        return res / t;
    }
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    init();
    while (~scanf("%d", &n) && n != -1)
    {
        printf("%d\n", solve(n));
    }
    return 0;
}

版权声明:本文为mooleetzi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/mooleetzi/p/11355215.html