Uva 10294 Polya
#include <bits/stdc++.h> using namespace std; typedef long long LL; int gcd(int a,int b) { return b ==0 ? a : gcd(b,a%b); } const int maxn = 55; int main() { int t,n; while(scanf("%d%d",&n,&t)!=EOF) { LL pow[maxn]; pow[0] = 1; for(int i=1;i<=n;i++) pow[i] = pow[i-1] * t; LL a = 0; for(int i=0;i<n;i++) a +=pow[gcd(i,n)]; LL b = 0; if(n%2==1) b = n*pow[(n+1)/2]; else b = n/2*pow[n/2] + n/2*pow[n/2+1]; printf("%lld %lld\n",a/n,(a+b)/2/n); } return 0; }