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;
}