POJ 1286 Necklace of Beads(Polya原理)

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.
 
题目大意:用3种颜色的珠子串一条项链,旋转或对称后相同的视为同一种方案,问n个珠子有多少种方案。
思路:比较基础的Polya原理题。
对于置换,旋转有n种方案。而对称也有n种方案。
旋转的n种方案有多少循环节暴力一下即可,好像用数论的方法也行反制我不会。
而对称,要分奇数偶数的情况讨论
偶数的情况:关于两点之间连线的对称,有n/2+1个循环节;关于相邻两点之间连线的垂直平分线对称,有n/2个循环节(显然的>_<)
奇数的情况:关于某点和中心所连直线对称,有(n+1)/2个循环节。
PS:在n比较小的时候置换会重复,但答案是对的,这大概只是偶然?
PS:n=0的时候输出0,不要问我为什么我也不知道为什么,果然这种远古级别的题目提交之前应该先看看DISCUSS……
 
代码(0MS):
 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
", solve());
44     }
45 }
View Code
原文地址:https://www.cnblogs.com/oyking/p/3541943.html