poj2409 http://poj.org/problem?id=2409

基础的polya题目:建议看一下博客http://blog.csdn.net/shiren_Bod/article/details/5664934//

#include<stdio.h>
int gcd (int a, int b)
{
    return b? gcd(b,a%b) : a;
}
__int64 Polya(int c,int s)
{
    int k;
    __int64 p[100];
    p[0]=1;
    for(k=0; k<s; k++)
        p[k+1]=p[k]*c;
    __int64 ans=s%2?s*p[s/2+1]:(s/2)*(p[s/2]+p[s/2+1]);
    for(k=1; k<=s; k++)
        ans+=p[gcd(k,s)];
    ans/=2*s;
    return ans;
}
int main()
{
    int c,s;
    while(scanf("%d%d",&c,&s),c||s)
    {
        printf("%I64d\n",Polya(c,s));
    }
    return 0;
}

你可能感兴趣的:(poj2409 http://poj.org/problem?id=2409)