输入文件中仅包含一行两个整数a、b,含义如上所述。
输出文件中包含一行10个整数,分别表示0-9在[a,b]中出现了多少次。
1 99
9 20 20 20 20 20 20 20 20 20
对于30%的数据,保证;
对于100%的数据,保证。
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=' ')