Problem:
 Score : 500 points    Problem Statement 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.        Sample Input 1 100
1      Sample Output 1 19  The following 19 integers satisfy the condition:   1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100        Sample Input 2 25
2      Sample Output 2 14  The following 14 integers satisfy the condition:   11,12,13,14,15,16,17,18,19,21,22,23,24,25        Sample Input 3 314159
2      Sample Output 3 937       Sample Input 4 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
3      Sample Output 4 117879300

Solution (Python):
N=input()
K=int(input())
l=len(N)
dp=[[0]+[1]*l]+[[0]*(l+1) for i in range(K)]
dp2=[[1]+[0]*l]+[[0]*(l+1) for i in range(K)]
for i,n in enumerate(N):
    n=int(n)
    for j in range(K):
        dp[j+1][i+1]=dp[j][i]*9+dp[j+1][i]+(dp2[j][i]*(n-1)+dp2[j+1][i] if n>0 else 0)
        dp2[j+1][i+1]=dp2[j][i] if n>0 else dp2[j+1][i]
print(dp[-1][-1]+dp2[-1][-1])

Solution (Python):

N = int(input())
K = int(input())



dp = [[[0 for _ in range(2)] for _ in range(K+1)] for _ in range(len(str(N))+1)]

dp[0][0][0] = 1


"""
N = 987654として、

（１）3桁目までみたときに、987より小さくなるdp[3][j][1]への派生は
・87から870を作るような、dp[2][j][1]から
・98から980みたいなのがあるから、dp[2][j][0]から。ただし、Nの３桁目が0だと、今作ってる３桁目までの数字98＊が980未満になれないので、その場合はスキップ
↑↑↑ここまではi桁目に0を使うパターン↑↑↑
↓↓↓i桁目に0以外を使うパターン↓↓↓
まだ使える0以外の個数がのこっているとき、
・87から871~879の９パターンに派生する形なので、dp[i][j-1][1] から派生するのが9通りなので * 9 して足す（0以外の値を一つ消化する）
・98から981~986への派生なので、dp[i][j-1][0] * (digit[i] - 1) (例でいうところの７未満の値を使える)　（0以外の値を一つ消化する）

（２）3桁目までみたときに、987と一致するようなdp[3][j][0]への派生は
・もし３桁目が０なら、98から980への派生があるので、dp[i][j][0]から派生できる
・もし３桁目が０ではないのなら、98から987へ派生し、一つ0以外の数字を消化するので、dp[i][j-1][0]から派生


みたいに考えていく。
"""

for i in range(len(str(N))):
    digit = int(str(N)[i])
    for j in range(K+1):
        
        
        dp[i+1][j][1] += dp[i][j][1]

        
        if digit == 0:
            
            dp[i+1][j][0] += dp[i][j][0]
        else:
            """
            314159で、31OXXXかつNの3桁目までより小さいパターンは、31XXXXから派生
            """
            
            dp[i+1][j][1] += dp[i][j][0]
        
        
        if j-1 >= 0:
            
            dp[i+1][j][1] += dp[i][j-1][1] * 9
            if digit != 0:
                
                dp[i+1][j][0] += dp[i][j-1][0]
                
                dp[i+1][j][1] += dp[i][j-1][0] * (digit-1)

print(sum(dp[-1][-1]))

Solution (Python):
N=int(input())
K=int(input())
import math
ans=0
n=str(N)
a=len(n)
def combi(p,q):
    if p<q : return 0
    else: return math.factorial(p)//(math.factorial(q)*math.factorial(p-q))
def mi(i):
    if i==0:
        return 0
    else:
        return i-1
def zero(i):
    if int(n[i])==0:
        return 0
    else:
        return 1
if a<K:
    ans=0
else:
    if a>K:
        ans+=combi(a-1,K)*(9**K)
    if K==1:ans+=int(n[0])
    elif K==2:
        x=0
        i=1
        b=0
        bs=0
        while True:
            if (int(n[i])!=0) and (x==0):
                b=int(n[i])
                bs=a-i
                x+=1
            if (x==1) or (i==len(n)-1):
                break
            i+=1
        ans+=mi(int(n[0]))*(a-1)*9+mi(bs)*9+b
    elif a==3 : ans+=mi(int(n[0]))*81*(combi(a-1,2))+9*mi(int(n[1]))+int(n[2])
    else:
        x=0
        i=1
        b=0
        bs=0
        c=0
        cs=0
        while True:
            if (int(n[i])!=0) and (x==0):
                b=int(n[i])
                bs=a-i
                x+=1
            elif (int(n[i])!=0) and (x==1):
                c=int(n[i])
                cs=a-i
                x+=1
            if (x==2) or (i==len(n)-1):
                break
            i+=1
        ans+=mi(int(n[0]))*81*(combi(a-1,2))+combi(bs-1,2)*81+9*(a-2)*mi(b)+c+mi(cs)*9

print(ans)

Solution (Python):

N = int(input())
K = int(input())


def nCr(n,r):
  if n < 0 or r < 0 or n < r: return 0
  if n - r < r: r = n - r
  if r == 0: return 1
  if r == 1: return n
  
  
  numerator = [n-r+k+1 for k in range(r)]
  
  denominator = [k+1 for k in range(r)]
  
  
  for p in range(2, r+1):
    
    pivot = denominator[p-1]
    if pivot > 1:
      
      offset = (n-r) % p
      for k in range(p-1, r, p):
        
        numerator[k - offset]  /= pivot
        denominator[k] /= pivot
        
  ret = 1
  for k in range(r):
    if numerator[k] > 1: ret *= int(numerator[k])
      
  return ret


dp = [[[0 for _ in range(2)] for _ in range(K+1)] for _ in range(len(str(N))+1)]

dp[0][0][0] = 1


for i in range(len(str(N))):
    digit = int(str(N)[i])
    for j in range(K+1):
        
        
        dp[i+1][j][1] += dp[i][j][1]

        
        if digit == 0:
            
            dp[i+1][j][0] += dp[i][j][0]
        else:
            """
            314159で、31OXXXかつNの3桁目までより小さいパターンは、31XXXXから派生
            """
            
            dp[i+1][j][1] += dp[i][j][0]
        
        
        if j-1 >= 0:
            
            dp[i+1][j][1] += dp[i][j-1][1] * 9
            if digit != 0:
                
                dp[i+1][j][0] += dp[i][j-1][0]
                
                dp[i+1][j][1] += dp[i][j-1][0] * (digit-1)

print(sum(dp[-1][-1]))

Solution (Python):
N=int(input())
K=int(input())
dp1=[[0 for _ in range(4)] for _ in range(101)]
dp2=[[0 for _ in range(4)] for _ in range(101)]
dp1[0][0]=1
for i in range(1, 101):
    dp1[i][0]=1
    for j in range(1, 4):
        dp1[i][j]=dp1[i-1][j-1]*9+dp1[i-1][j]
dp2[0][1]=1
for i in range(1, 101):
    dp2[i][1]=dp1[i][1]+1
    dp2[i][2]=dp1[i][2]
    dp2[i][3]=dp1[i][3]
ans=0
i=len(str(N))-1
cnt=0
while i>=0:
    k=int(str(N)[len(str(N))-1-i])
    if k==0:
        i-=1
        continue
    ans+=dp2[i][K-cnt]
    cnt+=1
    if K-cnt<0:
        break
    ans+=dp1[i][K-cnt]*(k-1)
    i-=1
print(ans)

Solution (Python):
def main():
    N = list(map(int,input()))
    K = int(input())
    if len(N) < K :
        print(0)
    else:
        print(dfs(N, 0, K))
def test(N,K):
    return dfs(list(map(int,str(N))),0,K)

def comb(N, K):
    if N < K:
        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


def dfs(N, pos, rest):
    if rest == 0:
        return 1
    if pos >= len(N):
        return 0
    ans = 0
    for i in range(10):
        if N[pos] > i:
            if i == 0:
                ans += comb(len(N) - pos - 1, rest) * (9**rest)
            else:
                ans += comb(len(N) - pos - 1, rest - 1) * (9 ** (rest-1))
        elif N[pos] == i :
            if i == 0 :
                ans += dfs(N, pos+1, rest)
            else :
                ans += dfs(N, pos+1, rest - 1)
        else:
            break
    return ans
if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
K = int(input())

N_ = str(N)
n = len(N_)

dp = [[[0]*2 for i in range(5)] for j in range(110)]

dp[0][0][0] = 1
dp[0][0][1] = 0

for i in range(n):
  for j in range(4):
    for k in range(2):
      nd = int(N_[i])
      for d in range(10):
        ni = i+1
        nj = j
        nk = k
        if d != 0:
          nj += 1
        if nj >3:
          continue
        if k == 0:
          if d > nd:
            continue
          elif d < nd:
            nk = 1
          else:
            pass
        dp[ni][nj][nk] += dp[i][j][k]
        
print(sum(dp[n][K]))

Solution (Python):
N = int(input())
K = int(input())

N_ = str(N)
n = len(N_)

dp = [[[0]*2 for i in range(5)] for j in range(110)]

dp[0][0][0] = 1
dp[0][0][1] = 0

for i in range(n):
  for j in range(4):
    for k in range(2):
      nd = int(N_[i])
      for d in range(10):
        ni = i+1
        nj = j
        nk = k
        if d != 0:
          nj += 1
        if nj >3:
          continue
        if k == 0:
          if d > nd:
            continue
          elif d < nd:
            nk = 1
          else:
            pass
        dp[ni][nj][nk] += dp[i][j][k]
        
print(sum(dp[n][K]))

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,pprint,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
K = inp()
s = list(str(n))
l = len(s)

dp = [[[0 for _ in range(2)] for j in range(4)] for i in range(l+5)]
dp[0][0][0] = 1

for i in range(l):
    nd = int(s[i])
    
    for j in range(4):
        for k in range(2):
            for d in range(10):
                ni = i+1; nj = j; nk = k
                if d>0: nj += 1
                if nj >= 4: continue

                if k == 0:
                    if d < nd: nk += 1
                    elif d > nd: continue
                dp[ni][nj][nk] += dp[i][j][k]

print(sum(dp[l][K]))

Solution (Python):
def nCr(n,r):
  ret = 1
  for i in range(r):
    ret *= n-i
    ret /= i+1
  return ret

def top(N,K):
  if K == 0:
    return 1
  L = len(N)
  ans = 0
  if L < K:
    return 0
  else:
    ans += nCr(L-1,K) * 9 ** K

    u = int(N[0])
    z = N.count('0')
    o = N.count('1')
    f = 0
    if z + o == L:
      f = 1
    if f == 0:
      ans += (u - 1) * nCr(L-1,K - 1) * 9 ** (K - 1)
      if L == 1:
        return ans + 1
      else:
        return ans + top(str(int(N[1:])),K - 1)
    else:
      if len(str(int(N[1:]))) != 1:
        return ans + top(str(int(N[1:])),K - 1)
      else:
        if o >= K:
          return ans + 1
        else:
          return ans

N = str(input())
K = int(input())

print(int(top(N,K)))

Solution (Python):
N = int(input())
K = int(input())


def ketadp(X):
    X = '0' + str(X)
    dp = [[[0] * 4 for _ in range(2)] for _ in range(len(X))]
    dp[0][0][0] = 1
    for i in range(len(X) - 1):
        for j in range(2):
            for d in range(10 if j else int(X[i + 1]) + 1):
                if d == 0:
                    for k in range(4):
                        dp[i + 1][j or d < int(X[i + 1])][k] += dp[i][j][k]
                else:
                    for k in range(3):
                        dp[i + 1][j or d < int(X[i + 1])][k + 1] += dp[i][j][k]

    return dp[-1][0][K] + dp[-1][1][K]


print(ketadp(N))

Solution (Python):
N = int(input())
K = int(input())



from functools import lru_cache
import sys
sys.setrecursionlimit(1 << 25)


@lru_cache(None)
def F(N, K):
    '''N以下でちょうど0以外がK回出てくる (0以外を特にカウントする)'''
    
    assert N > -1
    if K < 0:
        return 0
    if N < 10:
        if K == 0:
            return 1  
        if K == 1:
            return N  
        return 0  

    
    q, r = divmod(N, 10)
    ret = 0
    for d in range(10):
        
        if d == 0:
            ret += F(q, K)
        else:  
            
            if d > r:
                ret += F(q - 1, K - 1)
            else:
                ret += F(q, K - 1)
    return ret


print(F(N, K))

Solution (Python):

N=int(input())
K=int(input())

lN=len(str(N))

dp=[[[0 for i in range(K+1)] for j in range(2)] for k in range(lN+1)]

dp[0][0][0]=1

for i in range(lN):
    num=int(str(N)[i])
    for j in range(K+1):
        if num==0:
            dp[i+1][0][j] = dp[i][0][j]
        else:
            if j==0:
                dp[i+1][0][j] = 0
            else:
                dp[i+1][0][j] = dp[i][0][j-1]
        
        if j==0:
            dp[i+1][1][j] = 1
        else:
            if num==0:
                dp[i+1][1][j] = dp[i][1][j-1]*9 + dp[i][1][j]*1
            else:
                dp[i+1][1][j] = dp[i][1][j-1]*9 + dp[i][1][j]*1 + dp[i][0][j-1]*(num-1) + dp[i][0][j]*1

print(dp[lN][1][K] + dp[lN][0][K])

Solution (Python):
def run():
  N = list(map(int, list(input())))
  K = int(input())
  Nlen = len(N)
  dp = [[[0]*(K+2) for _ in range(2)] for n in range(Nlen+1)]
  dp[0][0][0] = 1
  for nlen in range(Nlen):
    dN = N[nlen]
    for d in range(10):
      for j in range(2):
        for k in range(K+2):
          flagj = j or d < dN
          if (flagj == 0) and (d > dN): continue
          flagk = min(K+1, k+(d != 0))
          dp[nlen+1][flagj][flagk] += dp[nlen][j][k]
  print(dp[-1][0][K]+dp[-1][1][K])
  
if __name__ == '__main__':
    run()

Solution (Python):
N = input()
K = int(input())
m = len(N)
dp = [[[0] * (K + 1) for _ in range(2)] for _ in range(m + 1)]
dp[0][0][0] = 1

for i in range(1, m + 1):
    l = int(N[i - 1])
    for k in range(K + 1):
        dp[i][1][k] += dp[i - 1][1][k]
        if l != 0:
            dp[i][1][k] += dp[i - 1][0][k]
        else:
            dp[i][0][k] += dp[i - 1][0][k]
        if k - 1 >= 0:
            dp[i][1][k] += 9 * dp[i - 1][1][k - 1]
            if l != 0:
                dp[i][0][k] += dp[i - 1][0][k - 1]
                dp[i][1][k] += (l - 1) * dp[i - 1][0][k - 1]

print(dp[m][0][K] + dp[m][1][K])

Solution (Python):
import itertools

def read():
    N = str(input().strip())
    K = int(input().strip())
    return N, K

def solve(N, K):
    L = len(N)
    D = [int(N[i]) for i in range(L)]
    dp0 = [[0 for k in range(K+1)] for i in range(L+1)]
    dp1 = [[0 for k in range(K+1)] for i in range(L+1)]
    
    dp0[0][0] = 1
    for i in range(L):
        for k in range(K+1):
            if k < K and D[i] > 0:
                dp0[i+1][k+1] += dp0[i][k]
                for d in range(1, D[i]):
                    dp1[i+1][k+1] += dp0[i][k]
            if D[i] == 0:
                dp0[i+1][k] += dp0[i][k]
            else:
                dp1[i+1][k] += dp0[i][k]
            if k < K:
                for d in range(1, 10):
                    dp1[i+1][k+1] += dp1[i][k]
            dp1[i+1][k] += dp1[i][k]
    return dp0[L][K] + dp1[L][K]


if __name__ == '__main__':
    inputs = read()
    print("%d" % solve(*inputs))

Solution (Python):
N = list(map(int,list(input())))
K = int(input())


def main(k,p):
    l = len(k)
    x = 9 ** p
    for i in range(p):
        x *= max(l - 1 - i,0)
    for i in range(p):
        x //= (i+1)
    y = (k[0] - 1)*(9**(p-1))
    for i in range(p-1):
        y *= max(l - 1 - i,0)
    for i in range(p-1):
        y //= (i+1)
    
    i = 1
    while i < l-1 and k[i] == 0 :
        i += 1
    
    if p == 1:
        z = 1
    elif i == l-1 and k[i]==0:
        z = 0
    else:
        z = main(k[i:],p-1)
    
    return (x+y)+z

if len(N) < K:
    print(0)
else:
    print(main(N,K))

Solution (Python):
import sys
import math
from collections import defaultdict

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def II(): return map(int, input().split())
def III(): return list(map(int, input().split()))
def Line(N,num):
    if N<=0:
        return [[] for _ in range(num)]
    elif num==1:
        return [I() for _ in range(N)]
    else:
        read_all = [tuple(II()) for _ in range(N)]
        return map(list, zip(*read_all))



N = I()
K = I()

S = str(N)
L = len(S)



dp = [[[0]*2 for _ in range(K+1)] for _ in range(L)]
dp[0][0][1] = 1
dp[0][1][0] = 1
dp[0][1][1] = int(S[0])-1

for i in range(1,L):
    for x in range(K+1):
        if S[i] == '0':
            dp[i][x][0] = dp[i-1][x][0]
            dp[i][x][1] = dp[i-1][x][1]
            if x >= 1:
                dp[i][x][1] += 9*dp[i-1][x-1][1]
        else:
            dp[i][x][1] = dp[i-1][x][0] + dp[i-1][x][1]
            if x >= 1:
                dp[i][x][0] = dp[i-1][x-1][0]
                dp[i][x][1] += 9*dp[i-1][x-1][1] + (int(S[i])-1)*dp[i-1][x-1][0]

print(dp[L-1][K][0]+dp[L-1][K][1])

Solution (Python):
x = int(input())
X = [int(i) for i in str(x)]
y = int(input())
dp = [[[0] * 2 for i in range(4)] for j in range(len(str(x))+1)]
dp[0][0][0] = 1

for i in range(len(str(x))):
  for j in range(4):
    for m in range(2):
      for n in range(10):
        nj = j + 1 if n != 0 else j
        if nj == 4: continue
        nm = m        
        if X[i] > n and m == 0:
          nm = 1
        if X[i] < n and m == 0:
          continue
        dp[i+1][nj][nm] += dp[i][j][m]

print(dp[len(X)][y][0]+dp[len(X)][y][1])

Solution (Python):
import sys

def comb(n, r):
    if r > n or r < 0: return 0
    r = min(r, n - r)
    if r == 0: return 1
    if r == 1: return n
    numerator = list(range(n-r+1, n+1))
    denominator = list(range(1, r+1))
    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot
    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])
    return result

def solve(n, k):
    if k == 0:
        return 1

    p = int(str(n)[0])
    l = len(str(n))
    if l < k:
        return 0
    res = 0
    for r in range(k-1, l-1):
        res += comb(r, k-1) * 9 ** (k - 1) * 9
    res += comb(l-1, k-1) * 9 ** (k - 1) * (p - 1)
    if k == 1:
        res += 1
    else:
        res += solve(int(str(n)[1:]), k-1)
    return res

n, k = map(int, sys.stdin.read().split())
def main():
    return solve(n, k)

if __name__ == '__main__':
    ans = main()
    print(ans)

Solution (Python):
import sys
from collections import *
import heapq
import math
import bisect
from itertools import permutations,accumulate,combinations,product
from fractions import gcd
def input():
    return sys.stdin.readline()[:-1]
mod=10**9+7

n=int(input())
k=int(input())
keta=len(str(n))
sn=str(n)
dp=[[[0]*(k+1) for j in range(keta)] for i in range(2)]

for i in range(keta):
    if i==0:
        dp[0][0][0]=1
        dp[0][0][1]=int(sn[i])-1
        dp[1][0][1]=1
    else:
        for j in range(k+1):
            if j!=k:
                dp[0][i][j]+=dp[0][i-1][j]
                dp[0][i][j+1]+=dp[0][i-1][j]*9
                if int(sn[i])==0:
                    dp[1][i][j]+=dp[1][i-1][j]
                else:
                    dp[1][i][j+1]+=dp[1][i-1][j]
                    dp[0][i][j]+=dp[1][i-1][j]
                    dp[0][i][j+1]+=dp[1][i-1][j]*(int(sn[i])-1)
            else:
                dp[0][i][j]+=dp[0][i-1][j]
                if int(sn[i])==0:
                    dp[1][i][j]+=dp[1][i-1][j]
                else:
                    dp[0][i][j]+=dp[1][i-1][j]

print(dp[0][-1][-1]+dp[1][-1][-1])

Solution (Python):
n = str(input())
k = int(input())
m = len(n)
dp = [[[0,0] for __ in range(k+2)] for _ in range(m+1)]
dp[0][0][0] = 1
for i in range(m):
    d = int(n[i])
    for j in range(k+1):
        for s in range(2):
            for c in range(10):
                if (not s ) and c>d:
                    continue
                dp[i+1][j+(c!=0)][s or (c<d)] += dp[i][j][s]
print(dp[m][k][0]+dp[m][k][1])

Solution (Python):
digits = [int(c) for c in input()]
n = len(digits)
m = int(input())

dp = [[[0 for _ in range(2)] for _ in range(m + 1)] for _ in range(n + 1)]
dp[0][0][0] = 1

for i, digit in enumerate(digits):
    for j in range(m + 1):
        for k in range(2):
            for d in range(10 if k else digit + 1):
                if j + (d > 0) <= m:
                    dp[i + 1][j + (d > 0)][k or (d < digit)] += dp[i][j][k]

print(sum(dp[n][m]))

Solution (Python):
S = input()
K = int(input())

l = len(S)

dp0 = [[0 for j in range(K + 1)] for i in range(l + 1)] 
dp1 = [[0 for j in range(K + 1)] for i in range(l + 1)] 

dp0[0][0] = 1
for i in range(l):
    n = int(S[i])
    for j in range(K + 1):
        for d in range(10):
            nk = j
            if j > K: 
                continue

            if d > 0:
                nk += 1
                if nk > K:
                    continue
            
            dp1[i + 1][nk] += dp1[i][j]

            
            if d > n:
                continue
            elif d == n:
                dp0[i + 1][nk] += dp0[i][j]
            elif d < n:
                dp1[i + 1][nk] += dp0[i][j]

print(dp0[-1][-1] + dp1[-1][-1])

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

from functools import lru_cache

N, K = map(int,read().split())

@lru_cache(None)
def F(N, K):
    """N以下で0でないものがちょうどK個。0を含める"""
    assert N >= 0
    if N < 10:
        if K == 0:
            return 1
        if K == 1:
            return N
        return 0
    q,r = divmod(N,10)
    ret = 0
    if K >= 1:
        
        ret += F(q, K-1) * r
        ret += F(q-1, K-1) * (9-r)
    
    ret += F(q, K)
    return ret



print(F(N, K))

Solution (Python):
import sys
import math
sys.setrecursionlimit(10 ** 7)




N = input()
K = int(input())
lenN = len(N)



def comb(n, k):
    return math.factorial(n) // (math.factorial(k) * math.factorial(n-k))






def CntAlmostZero(i, k, smaller):
    
    if i == lenN:
        if k == 0:
            return 1
        else:
            return 0

    if k == 0:
        return 1

    
    if smaller:
        
        if (lenN - i) >= k:
            return comb(lenN - i, k) * (9 ** k)
        
        else:
            return 0
    
    else:
        
        if N[i] == '0':
            return CntAlmostZero(i + 1, k, False)
        
        else:
            
            zero = CntAlmostZero(i+1, k, True)
            
            same = CntAlmostZero(i+1, k-1, False)
            
            aida = CntAlmostZero(i+1, k-1, True) * (int(N[i]) - 1)

            return zero + same + aida

    return 1


ans = CntAlmostZero(0, K, False)
print(ans)

Solution (Python):


N=input()
K=int(input())














dp = [[[0 for i in range(5)] for j in range(11)] for k in range(104)]
for j in range(10):
    if j==0:
        dp[0][j][0]=1
    else:
        dp[0][j][1]=1

for k in range(K+1):
    for i in range(len(N)):
        if i==0:
            continue
        for j in range(10):
            if j==0:
                for l in range(10):
                    dp[i][j][k]+=dp[i-1][l][k]
            elif (k != 0):
                for l in range(10):
                    dp[i][j][k]+=dp[i-1][l][k-1]








Ans=0
k=K
for i in range(len(N)):
    D=int(N[i])
    if i==len(N)-1:
        D+=1
    for j in range(D):
        Ans+=dp[len(N)-i-1][j][k]
    if D!=0:
        k-=1
    if k<0:
        break

print(Ans)

Solution (Python):
N=input()
K=int(input())
dp_0 = [[0]*(len(N)) for i in range(K+1)]
dp_1 = [[0]*(len(N)) for i in range(K+1)]
dp_0[0][0]=1
dp_0[1][0]=int(N[0])-1
dp_1[1][0]=1
for i in range(len(N)-1):
    for j in range(K+1):
        if j==K:
            dp_0[j][i+1] += dp_0[j][i]
        elif j==0:
            dp_0[j][i+1] =dp_0[j][i]
            dp_0[j+1][i+1] += dp_0[j][i]*9
        else:
            dp_0[j][i+1] += dp_0[j][i]
            dp_0[j+1][i+1] += dp_0[j][i]*9
        if j != K and N[i+1] != '0':
            dp_0[j+1][i+1] += (int(N[i+1])-1)*dp_1[j][i]
            dp_0[j][i+1] += dp_1[j][i]
            dp_1[j+1][i+1] = dp_1[j][i]
        elif N[i+1] =='0':
            dp_1[j][i+1] = dp_1[j][i]
        else:

            dp_0[j][i+1] += dp_1[j][i]

print(dp_0[K][len(N)-1]+dp_1[K][len(N)-1])

Solution (Python):
sn = input()
k = int(input())
d = len(sn)

dp1 = [[0 for j in range(k + 1)] for i in range(d + 1)] 
dp2 = [[0 for j in range(k + 1)] for i in range(d + 1)] 
dp2[0][0] = 1
for i in range(d):
    for j in range(k):
        if sn[i] == '0':
            dp2[i + 1][j] += dp2[i][j]
        else:
            dp2[i + 1][j + 1] += dp2[i][j] 
            dp1[i + 1][j + 1] += (int(sn[i]) - 1) * dp2[i][j] 
            dp1[i + 1][j] += dp2[i][j] 

        dp1[i + 1][j] += dp1[i][j]
        dp1[i + 1][j + 1] += dp1[i][j] * 9
    dp1[i + 1][k] += dp1[i][k]
    dp2[i + 1][k] += dp2[i][k]
print(dp1[-1][-1] + dp2[-1][-1])

Solution (Python):
import numpy as np

n = input()
k = int(input()) + 1
d = len(n)
dp0 = np.zeros((d, k), dtype=int)
dp1 = np.zeros((d, k), dtype=int)
dp0[0][0] = 1
dp0[0][1] = int(n[0]) - 1
dp1[0][1] = 1
for i in range(d - 1):
    for j in range(k):
        dp0[i + 1][j] = dp0[i][j]
        if j > 0:
            dp0[i + 1][j] += 9 * dp0[i][j - 1]
        m = int(n[i + 1])
        if m > 0:
            dp0[i + 1][j] += dp1[i][j]
            if j > 0:
                dp0[i + 1][j] += (m - 1) * dp1[i][j - 1]
                dp1[i + 1][j] = dp1[i][j - 1]
            else:
                dp1[i + 1][j] = 0
        else:
            dp1[i + 1][j] = dp1[i][j]
print(dp0[d - 1][k - 1] + dp1[d - 1][k - 1])

Solution (Python):
from math import factorial
N = int(input())
K = int(input())
strN = str(N)
lenN = len(strN)

dp = [[[0]*(K+1) for i in range(2)] for j in range(lenN+1)]
dp[0][0][0] = 1
for i in range(lenN):
    for j in range(2):
        for k in range(K+1):
            if j == 0:
                if k != K:
                    if strN[i] == "0":
                        dp[i+1][j][k] += dp[i][j][k]
                    else:
                        dp[i+1][j][k+1] +=  dp[i][j][k]
                        dp[i+1][1][k+1] +=  (int(strN[i])-1) * dp[i][j][k]
                        dp[i+1][1][k] += dp[i][j][k]
                else:
                    dp[i+1][j][k] += dp[i][j][k]
            if j == 1:
                if k != K:
                    dp[i+1][j][k+1] += 9*dp[i][j][k]
                    dp[i+1][j][k] += dp[i][j][k]
                else:
                    dp[i+1][j][k] += dp[i][j][k]
print(dp[lenN][0][K] + dp[lenN][1][K])

Solution (Python):
n = input()
k = int(input())
 
dp = [[0] * (k + 2) for _ in range(2)]
dp[0][0] = 1
 
for d in n:
    ndp = [[0] * (k + 2) for _ in range(2)]
    for i in range(k + 1):
        ndp[1][i] += dp[1][i]
        ndp[1][i + 1] += dp[1][i] * 9
        if d == '0':
            ndp[0][i] += dp[0][i]
        else:
            ndp[1][i] += dp[0][i]
            ndp[1][i + 1] += dp[0][i] * (int(d) - 1)
            ndp[0][i + 1] += dp[0][i]
    dp = ndp
print(dp[0][k] + dp[1][k])

Solution (Python):
N=input()
K=int(input())
S="0"*(100-len(N))+N
DP=[[[0]*2 for _ in range(K+2)] for _ in range(101)]

DP[0][0][0] = 1
for i in range(100):
  for j in range(K+1):
    
    if S[i]=='0':
      DP[i+1][j][0]+=DP[i][j][0]
      
    
    else:
      DP[i+1][j+1][0]+=DP[i][j][0]
    
    
    DP[i+1][j][1]+=sum(DP[i][j])
    if int(S[i])==0:
      
      DP[i+1][j][1]-=DP[i][j][0]
      
    
    DP[i+1][j+1][1] += DP[i][j][1]*9+DP[i][j][0]*(max(0, int(S[i])-1))
    
print(DP[-1][K][0]+DP[-1][K][1])

Solution (Python):
N = input()
K = int(input())
m = len(N)
dp = [[[0] * (K + 1) for _ in range(2)] for _ in range(m + 1)]
dp[0][0][0] = 1

for i in range(1, m + 1):
    l = int(N[i - 1])
    for k in range(K + 1):
        dp[i][1][k] += dp[i - 1][1][k]
        if l != 0:
            dp[i][1][k] += dp[i - 1][0][k]
        else:
            dp[i][0][k] += dp[i - 1][0][k]
        if k - 1 >= 0:
            dp[i][1][k] += 9 * dp[i - 1][1][k - 1]
            if l != 0:
                dp[i][0][k] += dp[i - 1][0][k - 1]
                dp[i][1][k] += (l - 1) * dp[i - 1][0][k - 1]

print(dp[m][0][K] + dp[m][1][K])

Solution (Python):
N = input()
n = len(N)
K = int(input())
a = [0] * n
for i in range(n):
    a[i] = int(N[i])
a.insert(0,0)
dp = [[[0,0,0,0],[0,0,0,0]] for _ in range(n+1)]
dp[0][0][0] = 1
for i in range(1,n+1):
    for f in range(2):
        if f == 0:
            for k in range(4):
                if a[i] != 0:
                    if k !=0:
                        dp[i][0][k] = dp[i-1][0][k-1]
                    dp[i][1][k] += dp[i-1][0][k]
                    if k == 3:
                        continue
                    dp[i][1][k+1] += dp[i-1][0][k] * (a[i] - 1)
                if a[i] == 0:
                    dp[i][0][k] = dp[i-1][0][k]
        if f == 1:
            for k in range(4):
                if k!=0:
                    dp[i][1][k] += dp[i-1][1][k] + dp[i-1][1][k-1] * 9
                if k == 0:
                    dp[i][1][k] += dp[i-1][1][k]
ans = dp[n][0][K] + dp[n][1][K]
print(ans)

Solution (Python):
N = input()
K = int(input())

DP = [[[0 for i in range(K+1)] for i in range(2)] for i in range(len(N))]

for d in range(int(N[0])+1):
    DP[0][d<int(N[0])][d!=0] += 1

for i in range(len(N)-1):
    for j in range(2):
        for k in range(K+1):
            for d in range(10 if j else int(N[i+1])+1):
                if k+(d!=0) <= K:
                    DP[i+1][j|(d<int(N[i+1]))][k+(d!=0)] += DP[i][j][k]

print(DP[len(N)-1][0][K]+DP[len(N)-1][1][K])

Solution (Python):
n = input()
k = int(input())


dp_0 = [[0 for i in range(k+1)] for j in range(len(n))]


dp_1 = [[0 for i in range(k+1)] for j in range(len(n))]
dp_0[0][0] = 1
dp_0[0][1] = int(n[0]) - 1
dp_1[0][1] = 1
for i in range(1,len(n)):
    dp_0[i][0] = 1
    for j in range(1,k+1):
    
        if n[i] != '0':
            
            
            dp_0[i][j] += dp_0[i-1][j-1] * 9 + dp_1[i-1][j-1] * (int(n[i]) - 1)
            dp_1[i][j] += dp_1[i-1][j-1]
            
            
            
            dp_0[i][j] += dp_0[i-1][j] + dp_1[i-1][j]
        else:
            
            
            
            
            dp_0[i][j] = dp_0[i-1][j-1] * 9 + dp_0[i-1][j]
            dp_1[i][j] = dp_1[i-1][j]
print(dp_0[-1][k] + dp_1[-1][k])

Solution (Python):
n = input()
k = int(input())


dp_0 = [[0 for i in range(k+1)] for j in range(len(n))]


dp_1 = [[0 for i in range(k+1)] for j in range(len(n))]
dp_0[0][0] = 1
dp_0[0][1] = int(n[0]) - 1
dp_1[0][1] = 1
for i in range(1,len(n)):
    dp_0[i][0] = 1
    for j in range(1,k+1):
    
        if n[i] != '0':
            
            
            dp_0[i][j] = dp_0[i-1][j-1] * 9 + dp_1[i-1][j-1] * (int(n[i]) - 1) + dp_0[i-1][j] + dp_1[i-1][j]
            dp_1[i][j] = dp_1[i-1][j-1]
            
            
            
        else:
            
            
            
            
            dp_0[i][j] = dp_0[i-1][j-1] * 9 + dp_0[i-1][j]
            dp_1[i][j] = dp_1[i-1][j]


print(dp_0[-1][k] + dp_1[-1][k])

Solution (Python):
N = input()
K = int(input())
m = len(N)
dp = [[[0] * (K + 1) for _ in range(2)] for _ in range(m + 1)]
dp[0][0][0] = 1

for i in range(1, m + 1):
    l = int(N[i - 1])
    for k in range(K + 1):
        dp[i][1][k] += dp[i - 1][1][k]
        if l != 0:
            dp[i][1][k] += dp[i - 1][0][k]
        else:
            dp[i][0][k] += dp[i - 1][0][k]
        if k - 1 >= 0:
            dp[i][1][k] += 9 * dp[i - 1][1][k - 1]
            if l != 0:
                dp[i][0][k] += dp[i - 1][0][k - 1]
                dp[i][1][k] += (l - 1) * dp[i - 1][0][k - 1]

print(dp[m][0][K] + dp[m][1][K])

Solution (Python):
from functools import lru_cache
from operator import mul
from functools import reduce


def cmb(n, r):
    r = min(n - r, r)
    if r == 0:
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under


N = list(input())
N = list(map(int, N))
K = int(input())
n = len(N)


@lru_cache(None)
def rec(i, k, smaller):
    if k == 0:
        return 1
    if n - i < k:
        return 0

    if smaller:
        
        return cmb(n - i, k) * pow(9, k)
    else:
        if N[i] == 0:
            return rec(i + 1, k, False)
        else:
            a = rec(i + 1, k, True)
            b = rec(i + 1, k - 1, True) * (N[i] - 1)
            c = rec(i + 1, k - 1, False)
            return a + b + c


answer = rec(0, K, False)
print(answer)