POJ 1286 Necklace of Beads

// 3中颜色的n个珠子 问有多少种不同数量 旋转和翻转后相同算一种 
// 先考虑旋转 转i格 循环节 gcd(i,n)
// 翻转的话 n为奇数 每种翻转循环节 n/2+1
// n为偶数 n/2的循环节为 n/2 n/2的循环节为 (n-2)/2 +2
// 最后用 polya定理
#include <iostream> #include <stdio.h> using namespace std; #define LL long long LL Pow(LL a,LL b) { LL t=1; for(;b;b>>=1) { if(b&1) t=t*a; a=a*a; } return t; } int gcd(int a,int b) { int r; while(r=a%b){a=b;b=r;} return b; } int main() { int i,n; LL ans; //printf("%lld ",Pow(2,10)); while(scanf("%d",&n),~n) { if(!n){printf("0\n");continue;} ans=0; for(i=1;i<=n;i++) ans+=Pow(3,gcd(i,n)); // printf("%lld ",ans); if(n&1) ans+=n*Pow(3,n/2+1); else { ans+=n/2*Pow(3,n/2); ans+=n/2*Pow(3,n/2+1); } printf("%lld\n",ans/n/2); } return 0; }

 

posted on 2014-03-30 12:06  江财小子  阅读(148)  评论(0编辑  收藏  举报