首页 > 其他 > 详细

POJ 1286 Necklace of Beads(Polya原理)

时间:2014-02-09 23:15:30      阅读:434      评论: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? 
bubuko.com,布布扣

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.
 
题目大意:用3种颜色的珠子串一条项链,旋转或对称后相同的视为同一种方案,问n个珠子有多少种方案。
思路:暂缺。
PS:n=0的时候输出0,不要问我为什么我也不知道为什么,果然这种远古级别的题目提交之前应该先看看DISCUSS……
 
代码(0MS):
bubuko.com,布布扣
 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <iostream>
 4 #include <cstring>
 5 using namespace std;
 6 typedef long long LL;
 7 
 8 const int MAXN = 25;
 9 
10 int n, m = 3;
11 bool vis[MAXN];
12 
13 LL power(LL x, int p) {
14     LL ret = 1;
15     while(p) {
16         if(p & 1) ret *= x;
17         x *= x;
18         p >>= 1;
19     }
20     return ret;
21 }
22 
23 LL solve() {
24     LL ans = 0;
25     for(int step = 0; step < n; ++step) {
26         memset(vis, 0, sizeof(vis));
27         int t = 0;
28         for(int i = 0; i < n; ++i) {
29             if(vis[i]) continue;
30             for(int j = i; !vis[j]; j = (j + step) % n) vis[j] = true;
31             ++t;
32         }
33         ans += power(m, t);
34     }
35     if(n & 1) ans += n * power(m, (n + 1) / 2);
36     else ans += (n / 2) * power(m, n / 2 + 1) + (n / 2) * power(m, n / 2);
37     return n == 0 ? 0 : ans / (2 * n);
38 }
39 
40 int main() {
41     while(scanf("%d", &n) != EOF) {
42         if(n == -1) break;
43         printf("%I64d\n", solve());
44     }
45 }
View Code

 

POJ 1286 Necklace of Beads(Polya原理)

原文:http://www.cnblogs.com/oyking/p/3541943.html

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