Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero…
- User
Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten.
Constraints
- 1 \leq N < 10^{100}
- 1 \leq K \leq 3 Input
Input is given from Standard Input in the following format:
N K
Output
Print the count.
Examples
Input
100 1
Output
19
Input
25 2
Output
14
Input
314159 2
Output
937
Input
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999 3
Output
117879300
Assistant
pythonn_str = input().strip() K = int(input()) m = len(n_str) def comb(n, k): if k < 0 or k > n: return 0 if k == 0: return 1 elif k == 1: return n elif k == 2: return n * (n - 1) // 2 elif k == 3: return n * (n - 1) * (n - 2) // 6 else: return 0 part1 = 0 for L in range(K, m): part1 += comb(L - 1, K - 1) * (9 ** K) digits = [int(c) for c in n_str] from functools import lru_cache def calculate_part2(digits, K): m = len(digits) @lru_cache(maxsize=None) def dp(pos, count, tight): if pos == m: return 1 if count == K else 0 res = 0 max_d = digits[pos] if tight else 9 for d in range(0, max_d + 1): new_tight = tight and (d == max_d) new_count = count + (1 if d != 0 else 0) if pos == 0 and d == 0: continue # Skip leading zero res += dp(pos + 1, new_count, new_tight) return res return dp(0, 0, True) part2 = calculate_part2(digits, K) print(part1 + part2)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.