This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
#include<bits/stdc++.h>
usingnamespacestd;
longlongn,ans;
intk,len;
longlongd[1000000];
intmain(){
cin>>n>>k;
d[0]=0;
len=1;
ans=0;
for(longlongi=0;i<n;++i){
++d[0];//每次个位 +1
//进位处理到倒数第二高位
for(intj=0;j+1<len;++j){
if(d[j]==k){//如果本位到达了k进制的k这个数,本位置0,向前进1
d[j]=0;
d[j+1]+=1;
++ans;//记录进位次数
}
}
//最高位单独处理 涉及到len的调整
if(d[len-1]==k){
d[len-1]=0;
d[len]=1;
++len;
++ans;
}
//输出k进制的表示法
// for (int i = len - 1; i >= 0; i--) cout << d[i];