poj2409 Let it Bead
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
int poww(int a,int b)
{
int res=1;
int i;
for(i=0;i<b;i++)
{
res*=a;
}
return res;
}
int main()
{
int c,l;
while(scanf("%d %d",&c,&l),c||l)
{
int res=0;
int i;
for(i=1;i<=l;i++)
{
res+=poww(c,gcd(i,l));
}
if(l%2==0)
{
res+=(poww(c,l/2)*(l/2));
res+=(poww(c,(l-2)/2+2)*l/2);
res/=(l+l);
}
if(l%2==1)
{
res+=(poww(c,(l+1)/2)*l);
res/=(l+l);
}
printf("%d\n",res);
}
return 0;
}