输入文件中仅包含一行两个整数a、b,含义如上所述。
输出文件中包含一行10个整数,分别表示0-9在[a,b]中出现了多少次。
1 99
9 20 20 20 20 20 20 20 20 20
对于30%的数据,保证;
对于100%的数据,保证。
#include <bits/stdc++.h> // C++万能头文件
using namespace std;
static const auto io_sync_off = [](){ // lambda表达式
std::ios::sync_with_stdio(false); // 解除与scanf()等函数的同步
std::cin.tie(nullptr); // 解除cin和cout的绑定
return nullptr;
}();
long count(long n, int k) { // 1~n中数字k出现的次数
long low = 0, cur = n % 10, high = n / 10;
long digit = 1, res = 0;
while (high || cur) {
if (k == 0) { // 0单独处理
if (cur == 0)
res += (high - 1) * digit + low + 1;
else
res += high * digit;
}
else {
if (cur < k)
res += high * digit;
else if (cur == k)
res += high * digit + low + 1;
else
res += (high + 1) * digit;
}
low += cur * digit;
cur = high % 10;
high /= 10;
digit *= 10;
}
return res;
}
int main() {
long a, b;
cin >> a >> b;
for (int k = 0; k < 10; k++)
cout << count(b, k) - count(a - 1, k) << " ";
cout << endl;
return 0;
} from functools import lru_cache a, b = map(int, input().split()) @lru_cache(None) def dfs(i: int, tot: int, is_limit: bool, is_num: bool) -> int: if i == n: return tot cnt = dfs(i + 1, tot, False, is_num) if not is_num else 0 ub = int(s[i]) if is_limit else 9 # 当前位置的数可以是[1-is_num, ub] for d in range(1 - int(is_num), ub + 1): cnt += dfs(i + 1, tot + int(digit == d), is_limit and d == int(s[i]), True) return cnt for digit in range(0, 10): s = str(a - 1) n = len(s) lb = dfs(0, 0, True, False) dfs.cache_clear() s = str(b) n = len(s) ub = dfs(0, 0, True, False) dfs.cache_clear() print(ub - lb, end=' ')