裸的polya定理
#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 %d",&m, &n) != EOF && (n + m))
{
ans = 0;
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;
}