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
pythonimport math from functools import lru_cache N = input().strip() K = int(input()) sum_part = 0 length = len(N) # Calculate sum for numbers with length less than len(N) for l in range(1, length): if K > l: continue comb = math.comb(l-1, K-1) sum_part += comb * (9 ** K) digits = list(map(int, N)) @lru_cache(maxsize=None) def dp(pos, count, tight, started): if pos == len(digits): return 1 if (count == K and started) else 0 res = 0 max_digit = digits[pos] if tight else 9 for d in range(0, max_digit + 1): # Skip leading zero for the first position if pos == 0 and d == 0: continue new_tight = tight and (d == max_digit) new_started = started or (d != 0) new_count = count if new_started and d != 0: new_count += 1 if new_count > K: continue res += dp(pos + 1, new_count, new_tight, new_started) return res dp_result = dp(0, 0, True, False) print(sum_part + dp_result)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.