hdu1817 Necklace of Beads(polya定理)

http://acm.hdu.edu.cn/showproblem.php?pid=1817

Problem Description

Beads of red, blue or green colors are connected together into a circular necklace of n beads ( n < 40 ). 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

polya定理:
这里写图片描述
下面这个题是polya定理的简单应用

题目分析:

1,先考虑旋转的情况:将项链旋转i格后,循环节数为gcd(n,i)。(不知道怎么得到的,记住就好啦)
2,再考虑翻转的情况:
当n为奇数:保证一个不动,其余的就两两互换,就有(n-1)/2个对换,总共的循环节数就有(n+1)/2,所以一共有n个循环节数为(n+1)/2的循环群
当n为偶数:保证两个不动,就有(n-2)/2个对换,循环节数总共为(n+2)/2;全部都两两对换,总的循环节数就为n/2,所以一共有n/2个循环节数为(n+2)/2的循环群,n/2个循环节数为n/2的循环群
并且总的置换群就有2n个

代码

#include<iostream>
using namespace std;
typedef long long LL;
LL a[45];
LL power(LL a,LL n)
{
    LL ret=1,res=a;
    while(n){
        if(n&1) ret*=res;
        res*=res;
        n>>=1;
    }
    return ret;
}
int gcd(int a,int b)
{
    return b?gcd(b,a%b):a;
}
LL polya(int n)
{
    LL ret=0;
    for(int i=1;i<=n;i++){
        int g=gcd(n,i);
        ret+=power(3,g);
    }
    if(n&1){
        ret+=n*power(3,(n+1)/2);
    }else{
        ret+=(n/2)*power(3,n/2);
        ret+=(n/2)*power(3,(n+2)/2);
    }
    ret/=(2*n);
    return ret;
}
int main()
{
    for(int i=1;i<=40;i++)
        a[i]=polya(i);
    int n;
    while(cin>>n){
        if(n==-1) break;
        cout<<a[n]<<'\n';
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值