裸的Polya定理, 不过要注意一点就是N = 0 的情况
#include <iostream>
#include <string.h>#include <stdio.h>
#include <stdlib.h>
#include <cmath>
using namespace std;
int gcd( int a, int b)
{
if(b == 0)
return a;
return gcd(b, a%b);
}
int n, m;
int main()
{
double ans = 0;
while(scanf("%d",&n) != EOF && n != -1)
{
ans = 0;
m = 3;
if(n == 0)
{
printf("0\n");
continue;
}
if(n%2)
{
ans += n* pow(m*1.0, (n+1.0)/2.0);
}
else
{
ans += n/2 * pow(m*1.0, (n+2.0)/2.0);
ans += n/2 * pow(m*1.0, n/2.0);
}
for( int i = 1; i <= n ; i++)
{
int res = gcd(n, i);
ans += pow(m*1.0, res);
}
ans = ans/(2*n);
printf("%d\n",(int)ans);
}
return 0;
}