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.
-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):
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL; const int MAXN = ; int n, m = ;
bool vis[MAXN]; LL power(LL x, int p) {
LL ret = ;
while(p) {
if(p & ) ret *= x;
x *= x;
p >>= ;
}
return ret;
} LL solve() {
LL ans = ;
for(int step = ; step < n; ++step) {
memset(vis, , sizeof(vis));
int t = ;
for(int i = ; i < n; ++i) {
if(vis[i]) continue;
for(int j = i; !vis[j]; j = (j + step) % n) vis[j] = true;
++t;
}
ans += power(m, t);
}
if(n & ) ans += n * power(m, (n + ) / );
else ans += (n / ) * power(m, n / + ) + (n / ) * power(m, n / );
return n == ? : ans / ( * n);
} int main() {
while(scanf("%d", &n) != EOF) {
if(n == -) break;
printf("%I64d\n", solve());
}
}