We need to count numbers from 1 to N inclusive that have exactly K non-zero digits in decimal representation. N is up to 10^100 - huge length, but K is only up to 3, so we can use digit DP. Classic problem: Count numbers ≤ N with exactly K non-zero digits. Since K ≤ 3, we can also use combinatorial counting: count numbers with exactly K non-zero digits less than or equal to N. But N is huge, digit DP with states: position, count of non-zero digits used so far, and tight flag. Since length up to 100, DP size is O(len * (K+1) * 2). That's fine. However careful: leading zeros? The numbers from 1 to N inclusive, so we should count numbers without leading zeros. Typically digit DP counts numbers from 0 to N inclusive. So we can do DP counting from 0 to N inclusive, and then subtract 1 if 0 is counted? But 0 has 0 non-zero digits, so if K>0 then it won't be counted anyway. But we must exclude 0. However the DP counting from 0 to N with leading zeros allowed can be set to count numbers with exactly K non-zero digits, but includes numbers with leading zeros that effectively represent numbers with fewer digits. That's okay if we treat the entire representation as fixed length with leading zeros, but then we would count numbers like "000123" as 123, which is fine. However the issue: The number 0 (all digits zero) would have 0 non-zero digits, so if K=0 it would be counted; but K≥1, so it's fine. So we can just DP from 0 to N inclusive and treat leading zeros as zeros that do not count as non-zero digits, but careful: At the end we need to count only numbers from 1 to N. Since 0 has 0 non-zero digits, for K>=1, it is not counted. So answer = DP(N) with exactly K non-zero digits, counting from 0 to N inclusive, automatically excludes 0 if K>0. But if K could be 0, we would need to subtract 1, but constraints say K>=1, so fine.
However caution: Should we include numbers with leading zeros that produce numbers like 001? That's same as 1, but we don't want to double count. But in fixed-length DP, we typically process digits from most significant to least significant with a flag "tight" and also a flag "started" (non-leading-zero). We can either incorporate a "started" flag to avoid counting numbers with leading zeros as having non-zero digits incorrectly. Actually typical DP: we want to count numbers ≤ N with exactly K non-zero digits, where numbers are considered without leading zeros. One approach: treat the number as having exactly len(N) digits, with leading zeros allowed; but when counting non-zero digits, we only count digits that are non-zero; however a number like "00123" has 3 non-zero digits, which matches its actual representation "123". So if we treat leading zeros as not counting, it's fine. However the number 0 corresponds to all digits zero, which has 0 non-zero digits. So if we process all digits and count non-zero digits ignoring leading zeros, we get correct counts for numbers in range [0, N] including those with fewer digits. However we must ensure that the DP does not overcount because of different leading-zero patterns? No, because each integer from 0 to N has a unique representation of exactly len(N) digits with leading zeros. So DP over fixed length covers each integer exactly once. So we don't need a separate "started" flag; we only need to ensure that we count non-zero digits correctly (i.e., zeros are zeros; they don't contribute to count regardless of whether they are leading or not). That's fine. So DP state: index pos from 0 to len-1 (0 being most significant?), count (0..K), tight flag (0/1). At each position, we iterate over digit d from 0 to (tight? current digit of N: 9). For each, we update new_count = count + (d != 0? 1:0). If new_count > K, skip. Then dp[pos+1][new_count][tight && d == limit] += dp[pos][count][tight].
At the end, answer = dp[len][K][0] + dp[len][K][1] (both tight states after processing all digits). Actually after last digit, tight flag can be either; sum them.
This DP works, but N can be up to 10^100, i.e., length up to 100. That's small.
However K is up to 3, we can also use combinatorial formulas for speed, but DP is straightforward.
But careful: N can be extremely large, but DP is O(len * K * 2 * 10) ~ 10032*10=6000, trivial. So implement DP.
But we need to handle large N input as a string. So parse string to digits.
Also note: the last example: N is a huge string of 9's and K=3, output is 117879300. We'll test.
Implementation details:
Let s = N as string. Let n = len(s). Convert digits to integer array a[0..n-1] (most significant first). We'll use DP array dp[pos][cnt][tight] where pos from 0 to n, cnt 0..K, tight 0/1. Initially dp[0][0][1] = 1 (starting at position 0, with count 0, tight=1 meaning we are matching N so far). Then for each pos, for cnt, for tight, if dp[pos][cnt][tight] > 0, then loop d from 0 to (tight ? a[pos] : 9). new_cnt = cnt + (d != 0). if new_cnt <= K, then dp[pos+1][new_cnt][tight && (d == a[pos])] += dp[pos][cnt][tight].
After processing all positions, answer = dp[n][K][0] + dp[n][K][1].
Since answer can be large: Up to N which is huge, but N can be 10^100, the count of numbers up to N with K non-zero digits is roughly O( (log N)^(K-1) * something ), actually for K fixed, the number of numbers ≤ N with exactly K non-zero digits is roughly C(len, K) * 9^K (for numbers with exactly K non-zero digits and no leading zeros). That's about O(len^K) which can be huge, but within 64-bit? Let's check: len up to 100, K=3, C(100,3)=161700, times 9^3=729 gives about 117.9 million, as in example. So it's within 32-bit? 117 million fits in 32-bit signed, but could be larger? For K=3, maximum count up to N = 10^100 - 1 (all 9's) is roughly sum_{i=K}^{100} C(i-1, K-1)*9^K, which is like C(100,3)9^3 plus others. Actually the maximum count for exactly K non-zero digits among numbers with up to L digits is sum_{d=K}^{L} C(d-1, K-1) * 9^K, because first digit cannot be zero. That sum = 9^K * sum_{d=K}^{L} C(d-1, K-1) = 9^K * C(L, K) (by combinatorial identity). Actually sum_{d=K}^{L} C(d-1, K-1) = C(L, K). So total numbers with exactly K non-zero digits and up to L digits (including leading digit non-zero) is 9^K * C(L, K). For L=100, K=3, that's 9^3 * C(100,3) = 729 * 161700 = 117,879,300, exactly the example's output. So maximum is about 1.18e8 for K=3, L=100. For K=2, maximum is 9^2 * C(100,2) = 81 * 4950 = 400,950. For K=1, maximum is 9 * C(100,1) = 9100=900. So answer fits comfortably within 32-bit signed. Actually 400k and 900. So we can store answer in int (32-bit). But just to be safe, use long long. In Python, automatically big.
Thus we can implement in Python easily.
But note: The input N is huge, but Python int can handle arbitrary precision, but we treat it as string.
Now, verifying examples:
Example 1: N=100, K=1. Let's compute manually: Numbers from 1 to 100 with exactly one non-zero digit: That includes 1-9 (9 numbers), and also numbers like 10,20,...,90? Actually 10 has digits '1' and '0', one non-zero digit? The digit '1' is non-zero, '0' is zero, so yes exactly one non-zero digit: numbers 10,20,...,90 are 9 numbers. Also 100? Digits: '1','0','0' -> one non-zero digit, so included. So total = 9+9+1 = 19. Also numbers like 100? yes. So DP should yield 19.
Example 2: N=25, K=2. Count numbers from 1 to 25 with exactly two non-zero digits: These are numbers with two non-zero digits, i.e., from 10 to 99: those with two non-zero digits? Actually numbers with two non-zero digits include 11-99 except those with a zero digit. But also numbers with two digits where both digits non-zero: there are 99=81 numbers from 10 to 99 with both digits non-zero? Actually two-digit numbers from 10 to 99: tens digit from 1 to 9, units digit from 0 to 9. To have exactly two non-zero digits, both digits must be non-zero, because if units digit is zero, then only one non-zero digit. So numbers with both digits non-zero: 99=81. But N=25, so up to 25 includes numbers 10-25. Among these, count those with both digits non-zero: 11,12,13,14,15,16,17,18,19,21,22,23,24,25. That's? Let's list: 11,12,13,14,15,16,17,18,19 = 9 numbers. Then 21,22,23,24,25 = 5 numbers. Total = 14. Also number 20 has only one non-zero digit, excluded. Also numbers 1-9 have only one non-zero digit. Also number 100+ not in range. So answer 14 matches.
Example 3: N=314159, K=2. Output 937. We'll trust.
Thus DP should work.
However, we can also consider combinatorial counting: sum over positions of first non-zero etc. But DP is simple.
Edge cases: N may be "0"? Constraints say N ≥ 1, so ignore.
Implementation: We'll read N as string, read K as integer.
Define n = len(N), digits = list(map(int, N)). Use DP array: dp = [[0]*(K+1) for _ in range(2)] perhaps with tight dimensions? Since len is up to 100, we can have 2D arrays for each state? Actually we need position dimension, but we can compress over positions using two rows (current and next). But also we need two tight states. So we can have dp[tight][cnt] for current position. But careful: the tight flag and cnt. We can have dp[ tight ][ cnt ] for current pos. At each step, we generate next_dp[0..1][0..K] initialized to zeros. For each tight (0/1), each cnt (0..K), if dp[tight][cnt] > 0, then limit = digits[pos] if tight else 9. For d in 0..limit: new_cnt = cnt + (1 if d!=0 else 0). if new_cnt <= K: next_tight = 1 if (tight and d==limit) else 0. next_dp[next_tight][new_cnt] += dp[tight][cnt].
Initialize dp[1][0] = 1 at start.
After processing all digits, answer = dp[0][K] + dp[1][K] (but careful: after loop, dp will be the final state? Actually after each digit, we assign next_dp to dp. At the end, dp corresponds to after processing all positions. So answer = dp[0][K] + dp[1][K].
Check: What about numbers like 0? At start we have count 0. During processing, if we always pick d=0 at all positions, we get cnt=0 and tight=1 if N is all zeros? But N is positive, so at the end we have dp[tight][0] includes the number 0. But K≥1, so we don't include that. So fine.
But we must consider: The DP counts numbers from 0 to N inclusive. For numbers less than N that have leading zeros, they are included. So correct.
Time complexity: O(n * K * 10 * 2) ~ 100 * 3 * 10 * 2 = 6000, trivial.
Now, test with large N: all 9's length 100, K=3, answer should be 117879300. Let's quickly compute: 9^3 * C(100,3) = 729 * 161700 = 117879300. Yes.
Thus solution is straightforward.
Potential pitfalls: In DP, we must ensure that we use 64-bit integers for counts, but Python auto big.
Now implement solve() function.
I'll produce final answer.
But note: The problem might be from AtCoder (maybe ABC154 F? but not exactly). Anyway.
Now double-check K values: K up to 3, but our DP works for any K.
Edge: K=0? Constraint says K>=1, but if we allow, we would need to handle 0 but it's not required.
Also, note that we count numbers from 1 to N, but DP includes 0. For K>=1, 0 not counted. So fine.
Now ensure correct handling of large N input: reading line may have spaces, so read N as string, then K as int.
Potential issue: Python int conversion of N not needed.
Now test with examples manually by mental simulation.
Example1:
N=100, digits=[1,0,0], K=1.
Initialize dp[1][0]=1.
pos0: digit=1.
Iterate: tight=1,cnt=0,dp=1.
limit=1. d=0: new_cnt=0, next_tight? d=0 != limit so tight becomes 0? Actually next_tight = tight and d==limit => 1 and (0==1) false => 0. So next_dp[0][0] +=1.
d=1: new_cnt=1 (since d!=0), next_tight=1 and 1==1 =>1. new_cnt=1 <=K, next_dp[1][1] +=1.
Now dp becomes after pos0: dp[0][0]=1, dp[1][1]=1.
pos1: digit=0.
Now we process each state:
State tight=0, cnt=0: dp=1, limit=9. Loop d=0..9:
d=0: new_cnt=0, next_tight=0 (tight=0 so stays 0), next_dp[0][0] +=1
d=1..9: new_cnt=1, next_tight=0, each adds 1, so next_dp[0][1] gets +9.
So after this: next_dp[0][0] gets +1, next_dp[0][1] gets +9.
State tight=1, cnt=1: dp=1, limit=digit[1]=0.
Only d=0 allowed: new_cnt=1 (since d=0? Actually d=0 gives new_cnt=1? cnt was 1, d=0 doesn't add, so new_cnt=1). next_tight = 1 and 0==0 =>1. So next_dp[1][1] +=1.
So after pos1: we have next_dp[0][0]=1, next_dp[0][1]=9, next_dp[1][1]=1.
Now pos2: digit=0.
Process:
State tight=0, cnt=0: dp=1, limit=9.
Loop d=0..9:
d=0: new_cnt=0 -> next_dp[0][0] +=1
d=1..9: new_cnt=1 -> next_dp[0][1] +=9
So next_dp[0][0] +=1, next_dp[0][1] +=9.
State tight=0, cnt=1: dp=9, limit=9.
Loop d=0..9: for each d:
d=0: new_cnt=1 -> next_dp[0][1] +=9
d=1..9: new_cnt=2 -> skip because K=1, so only new_cnt<=1. So only d=0 allowed? Actually if new_cnt=2 > K, skip. So only d=0 (9 times) add to next_dp[0][1]. So next_dp[0][1] gets +9 *9? Wait careful: For cnt=1, we have dp=9 (i.e., 9 ways). For each of those, we iterate d from 0 to 9. For d=0: new_cnt=1 <=K, so add dp to next_dp[0][1] (since tight=0, next_tight=0). For d=1..9: new_cnt=2 >K, skip. So only d=0 adds. So total addition = 9 * 1 = 9 to next_dp[0][1].
State tight=1, cnt=1: dp=1, limit=digit[2]=0.
Only d=0: new_cnt=1, next_tight = 1 (since tight and d==limit). So next_dp[1][1] +=1.
Now after pos2, we compute totals:
Start with next_dp all zeros.
From state (0,0): adds (0,0):+1, (0,1):+9.
From state (0,1): adds (0,1):+9.
From state (1,1): adds (1,1):+1.
So totals: dp[0][0]=1, dp[0][1]=9+9=18, dp[1][1]=1.
Now after processing all digits, answer = dp[0][K] + dp[1][K] = dp[0][1] + dp[1][1] = 18+1 = 19. Good.
So DP works.
Now we need to output as integer.
Edge Cases: If N has length 1, e.g., N=5, K=1. DP should output count of numbers from 0..5 with exactly 1 non-zero digit: those are 1,2,3,4,5 =5. But note 0 excluded. So answer=5. Let's test quickly: digits=[5], init dp[1][0]=1.
pos0: limit=5.
d=0: new_cnt=0 => next_dp[0][0] +=1
d=1-5: new_cnt=1 => for each, next_tight= d==5? only when d=5. So for d=1-4: next_tight=0, for d=5: next_tight=1. So next_dp[0][1] gets 4, next_dp[1][1] gets 1.
Thus final dp[0][1]=4, dp[1][1]=1, total=5. Good.
Now we need to ensure that we treat numbers like 0 correctly when K=0, but not required.
Thus we are done.
Potential improvement: For K <= 3, we could also compute using combinatorial formulas with prefix of N. But DP is simpler.
Now produce final code.
But we need to consider performance: Python loops fine.
Implementation:
def solve():
import sys
input = sys.stdin.readline
N = input().strip()
K = int(input().strip())
digits = list(map(int, N))
n = len(digits)
dp_tight_1 = [0](K+1)
dp_tight_0 = [0](K+1)
# Initial state: tight = 1, cnt = 0
dp_tight_1[0] = 1
for pos in range(n):
next_tight_1 = [0](K+1)
next_tight_0 = [0](K+1)
for tight in (0,1):
if tight == 0:
dp = dp_tight_0
else:
dp = dp_tight_1
limit = digits[pos] if tight else 9
for cnt in range(K+1):
val = dp[cnt]
if val == 0:
continue
for d in range(limit+1):
new_cnt = cnt + (1 if d != 0 else 0)
if new_cnt > K:
continue
if tight and d == limit:
next_tight_1[new_cnt] += val
else:
next_tight_0[new_cnt] += val
dp_tight_0, dp_tight_1 = next_tight_0, next_tight_1
ans = dp_tight_0[K] + dp_tight_1[K]
print(ans)
But we must be careful: In the inner loop for tight, we used dp arrays but after we modify we are writing to next arrays. However we need to ensure we don't inadvertently use updated dp arrays for the other tight state within same iteration? That's fine because we are reading from current dp_tight_0 and dp_tight_1, which are separate arrays. Then we compute next arrays from scratch. So it's correct.
Alternatively we can use a 2D list dp[tight][cnt], but above is fine.
Let's test with sample inputs.
Sample 1:
N=100
K=1
We already did mental and got 19.
tight=1, dp=[1,0] (since only cnt=0 has 1). limit=2.
cnt=0, val=1.
d=0: new_cnt=0, next_tight_0[0] +=1
d=1: new_cnt=1, next_tight_0[1] +=1 (since d=1<2, so tight false)
d=2: new_cnt=1, next_tight_1[1] +=1 (since d==limit)
So after pos0: next_tight_0: cnt0=1, cnt1=1; next_tight_1: cnt1=1.
pos1:
We now have dp_tight_0: [1,1,0], dp_tight_1: [0,1,0].
Process pos1 digit=5.
First tight=0, dp=[1,1,0], limit=9.
cnt=0 val=1:
d=0..9: d=0: new_cnt=0 -> next_tight_0[0] +=1
d=1..9: new_cnt=1 -> next_tight_0[1] +=9
cnt=1 val=1:
d=0: new_cnt=1 -> next_tight_0[1] +=1
d=1..9: new_cnt=2 -> but K=2, so new_cnt=2 allowed! Actually K=2, so new_cnt <=2 allowed. So for d=1..9, new_cnt=2, next_tight_0[2] gets +9? Wait, careful: cnt=1, val=1, d from 0 to 9:
d=0: new_cnt=1 -> next_tight_0[1] +=1
d=1..9: new_cnt=2 -> next_tight_0[2] +=1 for each d, so next_tight_0[2] gets +9
cnt=2 val=0 (none)
So from tight=0: contributions:
next_tight_0[0] +=1
next_tight_0[1] +=9 (from cnt=0) +1 (from cnt=1,d=0) =10
next_tight_0[2] +=9 (from cnt=1,d=1..9)
Now tight=1, dp=[0,1,0], limit=5.
cnt=1 val=1:
d=0..5:
d=0: new_cnt=1, next_tight? tight=1 and d==5? No for d=0, next_tight=0 -> next_tight_0[1] +=1
d=1: new_cnt=2, next_tight=0 -> next_tight_0[2] +=1
d=2: new_cnt=2 -> next_tight_0[2] +=1
d=3: new_cnt=2 -> next_tight_0[2] +=1
d=4: new_cnt=2 -> next_tight_0[2] +=1
d=5: new_cnt=2, next_tight=1 (since d==limit) -> next_tight_1[2] +=1
So from tight=1:
next_tight_0[1] +=1 (from d=0)
next_tight_0[2] +=4 (d=1..4)
next_tight_1[2] +=1 (d=5)
Now sum contributions:
next_tight_0[0] =1
next_tight_0[1] =10 +1 =11
next_tight_0[2] =9 +4 =13
next_tight_1[2] =1
Thus final dp_tight_0[2]=13, dp_tight_1[2]=1, total=14. Correct.
Sample 3: We'll trust.
Now for huge input all 9's length 100 K=3: Should compute 117879300.
We can test in code.
Thus final solution.
But note: The DP currently includes numbers with leading zeros? Yes, but it counts each number exactly once. However, we have to ensure that numbers like 0 are counted if K=0 but not if K>0.
One potential nuance: The range is from 1 to N inclusive. Are we counting 0? For K>0 it's not counted, so fine.
But wait: For numbers with fewer digits, we represent them with leading zeros. For example, for N=100, number 5 is represented as "005"? Actually with 3-digit representation, 5 is "005". That has non-zero digit count = 1. So DP counts it. Good.
But what about number 0? It's "000", count=0, not counted for K>=1.
So correct.
Edge case: N itself is 0? Not allowed.
Thus solution done.
Now consider if we need to mod? No, output exact integer.
Now think about alternative combinatorial approach: We can derive a formula for K=1,2,3. But DP is simpler.
Now let's ensure that we handle input correctly: Could N have leading zeros? The problem says N is given as decimal, no leading zeros likely.
Now I'll craft final answer.
But hold on: The problem constraints: N < 10^{100}, so N can be up to a number with up to 100 digits, possibly exactly 100 digits (since 10^{100} has 101 digits: 1 followed by 100 zeros, but N < 10^{100}, so maximum N is 10^{100} - 1, which is 100 digits all 9's). So length exactly 100 at most.
Now double-check DP initialization: dp_tight_1[0] = 1, dp_tight_0 all zeros.
Now possible bug: In the inner loop for tight, we have two separate arrays. But we need to ensure that when tight=0 we use dp_tight_0, and when tight=1 we use dp_tight_1. That's fine.
Now optimization: We might combine into a 2D list: dp = [ [0]*(K+1) for _ in range(2) ].
Initialize dp[1][0] = 1.
For each digit:
new_dp = [ [0]*(K+1) for _ in range(2) ]
for tight in (0,1):
for cnt...
limit = digits[pos] if tight else 9
for d in range(limit+1):
new_cnt = cnt + (d!=0)
if new_cnt <= K:
new_tight = 1 if (tight and d == limit) else 0
new_dp[new_tight][new_cnt] += dp[tight][cnt]
dp = new_dp
Then answer = dp[0][K] + dp[1][K].
That's simpler. We'll adopt this.
Now test with sample.
Now one more nuance: Since we are using Python, we must ensure to use integers.
Now we should produce final answer.
Now, there is also possibility of using recursion with memoization, but iterative is fine.
Now writing final code.
Edge Cases:
N = 1, K = 1: answer should be 1. DP: digits=[1], K=1. pos0: dp[1][0]=1, limit=1. d=0 -> new_cnt=0 -> new_tight=0 -> new_dp[0][0]=1. d=1 -> new_cnt=1 -> new_tight=1 -> new_dp[1][1]=1. After loop, dp[0][1]=0, dp[1][1]=1 => answer=1. Good.
N = 10, K=1: numbers: 1-9, 10? 10 has one non-zero digit? Actually digits: 1 and 0, so one non-zero digit, yes. So answer=10? Wait, 1-9 is 9, plus 10 = 10. But 10 includes 1 and 0, so one non-zero digit. So answer=10. Let's test DP: N=10, digits=[1,0], K=1.
Initialize: dp[1][0]=1.
pos0 (digit=1):
tight=1 cnt=0 val=1.
d=0: new_cnt=0 -> new_tight=0 -> new_dp[0][0]=1
d=1: new_cnt=1 -> new_tight=1 -> new_dp[1][1]=1
Now dp after pos0: dp[0][0]=1, dp[1][1]=1.
pos1 (digit=0):
Process tight=0 cnt=0 val=1: limit=9.
d=0..9:
d=0: new_cnt=0 -> new_tight=0 -> new_dp[0][0] +=1
d=1..9: new_cnt=1 -> new_tight=0 -> new_dp[0][1] +=9
Process tight=1 cnt=1 val=1: limit=0.
d=0: new_cnt=1 -> new_tight=1 -> new_dp[1][1] +=1
So final dp: new_dp[0][0]=1, new_dp[0][1]=9, new_dp[1][1]=1. So answer = dp[0][1]+dp[1][1] = 9+1=10. Correct.
Now N=10, K=2: Numbers from 1-10 with exactly two non-zero digits: The only numbers with two non-zero digits up to 10? 11? but 11 >10. 10 has one non-zero digit. So only numbers 1-9 have one non-zero digit, none have two. Also 0 not counted. So answer=0. DP: Should yield 0. Let's test: pos0: as before, after pos0 dp[0][0]=1, dp[1][1]=1. pos1: For tight=0 cnt=0 val=1: d=0..9: d=0 gives cnt0, d=1..9 give cnt1. But K=2, so cnt1 allowed. tight=1 cnt=1 val=1: only d=0 gives cnt1. So after pos1: dp[0][0]=1, dp[0][1]=9, dp[1][1]=1. So dp[?][2] all zero. Answer=0. Good.
Thus DP correct.
Now the huge sample: Let's compute quickly with combinatorial reasoning to double-check: For all 9's length L=100, K=3, answer = 9^3 * C(100,3) = 729 * 161700 =? 161700700 = 113,190,000; 16170029 = 4,689,300; sum=117,879,300. Yes.
Now we just need to code.
But one more nuance: The DP approach as described counts numbers with exactly K non-zero digits, but it also counts the number 0 when cnt=0. Since K>=1, it's fine. But if K=0, we would need to subtract 1. Since constraints guarantee K>=1, we ignore.
Now I'll implement solve().
Time to write final answer.
But to be thorough, we should consider if there's any off-by-one with numbers like N exactly having K non-zero digits? DP counts N as well because we are counting ≤ N inclusive. Since we include tight state =1 at the end, that includes N. So correct.
Now produce final solution.
We'll also note that reading input: there might be spaces, but N is on first line, K on second. So we can read with input().strip() twice.
Now complete.
Now for completeness, we can add docstring.
Now final answer in python.
I'll write:
def solve():
import sys
N = sys.stdin.readline().strip()
K = int(sys.stdin.readline().strip())
digits = [int(c) for c in N]
n = len(digits)
# dp[tight][cnt] where tight = 0 or 1
dp = [[0](K+1) for _ in range(2)]
dp[1][0] = 1
for pos in range(n):
new_dp = [[0](K+1) for _ in range(2)]
for tight in range(2):
for cnt in range(K+1):
cur = dp[tight][cnt]
if cur == 0:
continue
limit = digits[pos] if tight else 9
for d in range(limit+1):
new_cnt = cnt + (1 if d != 0 else 0)
if new_cnt > K:
continue
new_tight = 1 if (tight and d == limit) else 0
new_dp[new_tight][new_cnt] += cur
dp = new_dp
ans = dp[0][K] + dp[1][K]
print(ans)
if name == "main":
solve()
Now test with sample 1:
Input:
100
1
Our code: digits = [1,0,0], K=1 => ans computed as earlier: 19.
Sample 2:
25
2 => 14.
Sample 3:
314159
2 => should be 937.
We can maybe test quickly with a small script but trust.
Now for sample 4: huge number of 9's, K=3 => 117879300. Let's simulate DP mentally? Not needed.
Now final answer.
But one more nuance: When N is huge (length 100), the DP loops 100 * 2 * (K+1) * (10) ~ 10024*10=8000, trivial. However we have nested loops: for each digit, for each tight, for each cnt, for each d. For each cnt we also iterate d. That's fine.
Potential improvement: we can early break if cnt > K? Already ensured.
Now the answer is an integer, Python prints it.
Thus final solution.
Now I'll provide final answer.
```python
import sys
def solve() -> None:
N = sys.stdin.readline().strip()
K = int(sys.stdin.readline().strip())
code
digits = [int(c) for c in N]
n = len(digits)
# dp[tight][cnt] tight = 0/1, cnt = number of non-zero digits used
dp = [[0] * (K + 1) for _ in range(2)]
dp[1][0] = 1 # start: tight = 1, no non-zero digit yet
for pos in range(n):
new_dp = [[0] * (K + 1) for _ in range(2)]
limit_digit = digits[pos]
for tight in range(2):
limit = limit_digit if tight else 9
for cnt in range(K + 1):
cur = dp[tight][cnt]
if cur == 0:
continue
for d in range(limit + 1):
new_cnt = cnt + (1 if d != 0 else 0)
if new_cnt > K:
continue
new_tight = 1 if (tight == 1 and d == limit) else 0
new_dp[new_tight][new_cnt] += cur
dp = new_dp
ans = dp[0][K] + dp[1][K]
print(ans)