poj 1286 polya定理

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\]


  • 考虑旋转,对于\(n\)个球的环旋转\(i\)个球的循环节的个数为\(gcd(n,i)\),这一部分快速幂求和

  • 考虑奇数的翻转,每次选择一个顶点作对称轴翻转,循环节数为\(\frac{n+1}{2}\)(自身不动,剩余两两交换),一共n种选择

  • 考虑偶数的翻转,可选对顶的两顶点作为对称轴旋转,循环节数为\(\frac{n+2}{2}\),(两顶点不动,剩余两两交换),\(\frac{n}{2}种选择\),选择无顶点的对称轴翻转,循环节数为\(\frac{n}{2}\)(全部两两交换),\(\frac{n}{2}\)种选择

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#if __cplusplus >= 201103L
#include <unordered_map>
#include <unordered_set>
#endif
#include <vector>
#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r
#define LONG_LONG_MAX 9223372036854775807LL
#define ll LL
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> P;
int n, m, k;
const int maxn = 1e5 + 10;
ll qpow(ll a, ll n)
{
    ll res = 1;
    while (n)
    {
        if (n & 1)
            res *= a;
        a *= a;
        n >>= 1;
    }
    return res;
}
ll gcd(ll a, ll b) { return !b ? a : gcd(b, a % b); }
ll solve()
{
    ll res = 0;
    if (!n)
        return res;
    for (int i = 1; i <= n; i++)
    {
        res += qpow(3, gcd(n, i));
    }
    if (n & 1)
    {
        res += (ll)n * qpow(3, (n + 1) / 2);
    }
    else
    {
        res += (ll)(n / 2) * qpow(3, (n + 2) / 2);
        res += (ll)(n / 2) * qpow(3, n / 2);
    }
    return res / (2 * n);
}
int main(int argc, char const *argv[])
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    while (cin >> n && n != -1)
    {
        cout << solve() << '\n';
    }
    return 0;
}

转载于:https://www.cnblogs.com/mooleetzi/p/11355215.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值