poj 1286 Necklace of Beads(polya 定理)

来源:互联网 发布:linux squid 4.0 安装 编辑:程序博客网 时间:2024/06/05 01:58

Necklace of Beads
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 7571 Accepted: 3152

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

45-1

Sample Output

2139

Source

Xi'an 2002

[Submit]   [Go Back]   [Status]   [Discuss]


题解:与poj 2409类似。

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#define LL long long using namespace std;LL ans,n,m;LL gcd(LL x,LL y){LL r;while (y){r=x%y;x=y;y=r;}return x;}LL pow(LL num,LL x){LL base=num; LL ans=1;while (x){if (x&1)  ans=ans*base;x>>=1;base=base*base;}return ans;}int main(){n=3;while (scanf("%lld",&m)!=EOF){if (m==0){printf("0\n");continue;}if (m==-1)  break;ans=0;for (LL i=1;i<=m;i++) ans+=pow(n,gcd(m,i));if (m%2)  ans+=pow(n,m/2+1)*m;else ans+=pow(n,m/2+1)*(m/2),     ans+=pow(n,m/2)*(m/2);printf("%lld\n",ans/(m*2));}}



0 0
原创粉丝点击