poj1286(polya计数 套模版)

来源:互联网 发布:js调用身份证读取器 编辑:程序博客网 时间:2024/06/01 09:53
http://poj.org/problem?id=1286
Necklace ofBeads
Time Limit: 1000MSMemory Limit: 10000KTotal Submissions: 5318Accepted: 2203

Description

Beads of red, blueor green colors are connected together into a circular necklace ofn beads ( n < 24 ). If the repetitions that are produced byrotation around the center of the circular necklace or reflectionto the axis of symmetry are all neglected, how many different formsof the necklace are there?
poj1286(polya计数 <wbr>套模版)

Input

The input hasseveral lines, and each line contains the input data n.
-1 denotes the end of the input file.

Output

The output shouldcontain the output data: Number of different forms, in each linecorrespondent to the input data.

Sample Input

45-1

Sample Output

2139

Source

Xi'an2002
和poj2409一样,直接照搬就行了,具体解题报告参看上一篇文章。。
注意点:他只输入n,而且颜色数量只有3钟,然后又不去模。所以不用考虑逆元什么的
有两点要注意:
1、n=32的时候答案其实已经超过int了,要用int64才能装得下
2、n=0的时候答案不是1而是0(这个地方re了好多次啊)。。。
#include
#include
#include
using namespacestd;
int gcd(int a,intb)
{
   return b?gcd(b,a%b):a;
}
int main()
{
   int c=3,s;
   while(scanf("%d",&s)!=EOF)
   {
      if(s==0)//关键
       {
          printf("0\n");
          continue;
       }
      if(s==-1)
       {
          break;
       }
       intk;
       long longp[64];
      p[0]=1;
      for(k=0;k
       {
          p[k+1]=p[k]*c;
       }
       long longcount=s&1?s*p[s/2+1]:(s/2)*(p[s/2]+p[s/2+1]);
      for(k=1;k<=s;k++)
       {
          count+=p[gcd(k,s)];
       }
      count/=2*s;
      printf("%lld\n",count);
   }
   return 0;
}



原创粉丝点击