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 = input().strip() K = int(input()) digits = list(map(int, list(N))) from functools import lru_cache @lru_cache(maxsize=None) def dp(pos, count, tight, leading_zero): if pos == len(digits): return 1 if (count == K and not leading_zero) else 0 limit = digits[pos] if tight else 9 total = 0 for d in range(0, limit + 1): new_tight = tight and (d == limit) new_leading_zero = leading_zero and (d == 0) if leading_zero: if d == 0: new_count = count else: new_count = count + 1 new_leading_zero = False else: new_count = count + (1 if d != 0 else 0) if new_count > K: continue total += dp(pos + 1, new_count, new_tight, new_leading_zero) return total print(dp(0, 0, True, True))
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.