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))) n = len(digits) dp = [{} for _ in range(n + 1)] dp[0][(True, 0, False)] = 1 for pos in range(n): current_digit = digits[pos] for state in list(dp[pos].keys()): tight, current_count, started = state cnt = dp[pos][state] max_d = current_digit if tight else 9 for d in range(0, max_d + 1): new_tight = tight and (d == max_d) new_started = started or (d != 0) if new_started: new_count = current_count + (1 if d != 0 else 0) else: new_count = 0 if new_count > K: continue next_state = (new_tight, new_count, new_started) if next_state in dp[pos + 1]: dp[pos + 1][next_state] += cnt else: dp[pos + 1][next_state] = cnt result = 0 for state in dp[n]: tight, count, started = state if started and count == K: result += dp[n][state] print(result)
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.