#include<iostream> using namespace std; int fun(int b,int d){//power int i=1,p=1; for(;i<=d;i++){ p*=b; } return p; } int main(){ int i,j,c,n,k,a[1005]={0},count=1; scanf("%d%d",&k,&n); a[count++]=1; a[count++]=k; a[count++]=k+1; for(i=2;count<=n;i++){ a[count++]=fun(k,i); c=count-1;//index of lastest bit for(j=1;j<c&&count<=n;j++){ a[count++]=a[j]+a[c]; } } printf("%d\n",a[n]); return 0; }
reference
9+1 9+3 9+4
27+1 27+3 27+4 27+9 27+10 27+12 27+13