/* ID: neverchanje PROG: LANG: C++11 */ #include<vector> #include<iostream> #include<cstring> #include<string> #include<algorithm> #include<cstdio> #include<set> #include<queue> #include<map> #define INF 0Xfffffffff #define st_size (1<<18)-1 #define maxn typedef long long ll; using namespace std; int c,s; ll pow[36]; int gcd(int a,int b){ if(b==0) return a; return gcd(b,a%b); } int main(){ // freopen("a.txt","r",stdin); // freopen(".out","w",stdout); while(cin>>c>>s){ if(c==0 && s==0) break; pow[0]=1; for(int i=1;i<=s;i++) pow[i]=pow[i-1]*c; int res=0; for(int i=1;i<=s;i++) res+=pow[gcd(s,i)]; if(s&1) res += s*pow[s/2+1]; else res += s/2*pow[s/2]+s/2*pow[s/2+1]; printf("%d ",res/(s*2)); } return 0; } /* DESCRIPTION: 项链的旋转是polya定理的基本问题 polya定理: sum(C(f))/|f| //burnside引理 C(f)=k^m(f) //k为颜色数,m(f)为循环数 置换是旋转: 给项链编号0,1,2,...s-1 可连续旋转0,1,2,3,....,s-1个珠子,对应|f|=s个置换 每个置换都有k=c 对s=6 转6(即不转) m(f)=6 转1 m(f)=1 转2 m(f)=2 转3 m(f)=3 转4 m(f)=2 转5 m(f)=1 可发现m(f)=gcd(i,s) //i表示转几个 m(f)<=s 置换是翻转: 分奇偶判断 */