Files
python/TangDou/CSP-J/J1_2020/3_2.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

43 lines
895 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
long long n, ans;
int k, len;
long long d[1000000];
int main() {
cin >> n >> k;
d[0] = 0;
len = 1;
ans = 0;
for (long long i = 0; i < n; ++i) {
++d[0]; //每次个位 +1
//进位处理到倒数第二高位
for (int j = 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];
// cout << endl;
}
cout << len << endl;
//输出进位次数
// cout << ans << endl;
return 0;
}