Regardless of the value of k, the sequence trend is the same.
Take k=2
Idea: first convert to binary,
#include<iostream>
using namespace std;
int main()
{
int k,n;
cin>>k>>n;
int ans=0;
int t=1;
while(n)
{
if(n&1)
{
ans+=t;
}
n=n>>1;
t*=k;
}
cout<<ans;
return 0;
}