首页 > 其他 > 详细

POJ 1286 Necklace of Beads

时间:2018-02-06 11:26:30      阅读:222      评论:0      收藏:0      [点我收藏+]

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 }

 

POJ 1286 Necklace of Beads

原文:https://www.cnblogs.com/Y-E-T-I/p/8421398.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!