poj 1286 Necklace of Beads (polya)

Necklace of Beads
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6701   Accepted: 2806

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? 
poj 1286 Necklace of Beads (polya)_第1张图片

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

4
5
-1

Sample Output

21
39


本题tricks;

1,n==0,ans=0

2,long long


//poj 1286

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<cmath>
using namespace std;
typedef long long LL;
const int MAX=0xfffffff;
int n,m;
LL ans;
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
void polya()
{
    ans=0;
    for(int i=0;i<n;i++)
    {
        int c=gcd(n,i);
        ans+=(LL)pow(double(m),(double)c);
    }
    if(n&1)
    {
        int c=(n+1)/2;
        int t=(int)pow(double(m),double(c));
        ans+=LL(t*n);
    }
    else
    {
        int c=n/2;
        int t=(int)pow(double(m),double(c));
        ans+=LL((n/2)*t);
        t*=m;
        ans+=LL(n/2)*t;
    }
    ans/=LL(2*n);
}
int main( )
{
    //freopen("1.txt","r",stdin);
    while(scanf("%d",&n)!=EOF&&n!=-1)
    {
        if(n==0)  ans=0;
        else
        {
            m=3;
            polya();
        }


        printf("%lld\n",ans);
    }
    return 0;
}

你可能感兴趣的:(Polya)