水题...
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
ll Gcd(ll a,ll b){
return b?Gcd(b,a%b):a;
}
inline ll Pow(ll a,ll b){
ll ret=1;
for (;b;b>>=1,a*=a)
if (b&1)
ret*=a;
return ret;
}
int main()
{
ll c,n,ans;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
while (1)
{
scanf("%lld%lld",&c,&n);
if (!n && !c) break;
ans=0;
for (int i=1;i<=n;i++)
ans+=Pow(c,Gcd(n,(ll)i));
if (n&1)
ans+=n*Pow(c,(n+1)>>1);
else
ans+=n/2*Pow(c,n/2)+n/2*Pow(c,n/2+1);
printf("%lld\n",ans/2/n);
}
return 0;
}