POJ 1286 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

1.旋转的置换,有n种

对于旋转i-1个的置换循环数为gcd(i,n)

2.翻转的置换,对于n为奇偶分情况讨论

n为奇数:

有n中置换循环数为n/2+1

n为偶数:

有2种情况:对称轴在点上,不在点上

第一种:循环数为(n-2)/2+2=n/2+1,有n/2种

第二种:循环数为n/2,有n/2种

然后运用置换群的polya定理

$ans=\frac{1}{2*n}*(3^{c(a_1)}+3^{c(a_2)}.....+3^{c(a_{2*n})})$

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 typedef long long lol;
 8 int tot;
 9 int n;
10 lol ans;
11 lol qpow(lol x,lol y)
12 {
13   lol res=1;
14   while (y)
15     {
16       if (y&1) res=res*x;
17       x=x*x;
18       y/=2;
19     }
20   return res;
21 }
22 int gcd(int a,int b)
23 {
24   if (!b) return a;
25   return gcd(b,a%b);
26 }
27 int main()
28 {int i,j;
29   while (cin>>n&&n!=-1)
30     {
31       ans=0;
32       for (i=1;i<=n;i++)
33     {
34       tot=gcd(i,n);
35       ans+=qpow(3,tot);
36     }
37       if (n%2==0&&n)
38     {
39       ans+=qpow(3,n/2)*(n/2);
40       ans+=qpow(3,n/2+1)*(n/2);
41     }
42       else
43     ans+=qpow(3,n/2+1)*n;
44       if (n)
45       ans/=2*n;
46       printf("%lld\n",ans);
47     }
48 }

 

posted @ 2018-02-06 11:18  Z-Y-Y-S  阅读(243)  评论(0编辑  收藏  举报