gcd里的类型一定要long long,至今想不明白,不然会一直WA
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
#define ll long long
ll p[100];
ll gcd(ll a,ll b)
{
if(b==0)
return a;
else gcd(b,a%b);
}
int main ()
{
int n;
p[0]=1;
for(int i=1;i<=40;++i)
p[i]=p[i-1]*3;
while(scanf("%d",&n)!=EOF)
{
if(n==-1)
break;
if(n==0)
{
printf("0\n");
continue;
}
ll a=0;
ll b=0;
for(int i=0;i<n;++i)
a+=p[gcd(n,i)];
if(n%2==0)
b+=(n/2*(p[n/2+1]+p[n/2]));
else b+=(n*p[(n+1)/2]);
printf("%lld\n",(a+b)/2/n);
}
//system("pause");
return 0;
}