Buggy python submission (verdict: Runtime Error):
N=int(input())
K=int(input())

d=len(str(N)) #桁数
a=int(str(N)[0]) #最上位桁の数
if K==1:
  l=9*(d-1)
  m=a-1
  n=1
  print(int(l+m+n))
  
if K==2:
  if d==1:
    print(0)
  else:
    l=9**2*(d-1)*(d-2)/2 #最上位0
    m=(a-1)*9*(d-1) #最上位1~a-1 
    for i in range(1,d):
      if int(str(N)[i])!=0:
        n=(int(str(N)[i]))+9*(d-i-1)
        break
    print(int(l+m+n))
  
if K==3:
  if d<=2:
    print(0)
  else:
    l=9**3*(d-1)*(d-2)*(d-3)/6
    m=(a-1)*9**2*(d-1)*(d-2)/2
    for i in range(1,d):
      if int(str(N)[i])!=0:
        n=(int(str(N)[i]))*9*(d-i-1)+9**2*(d-i-1)*(d-i-2)/2
        break
    print(int(l+m+n))

Fixed submission:
N=input()
K=int(input())

dpsm=[[0]*(K+1) for _ in range(len(N))] #dpsm[i][k]
dp=[[0]*(K+1) for _ in range(len(N))]

dpsm[0][0]=1
dpsm[0][1]=int(N[0])-1
dp[0][1]=1

for i in range(1,len(N)):
    for k in range(K-1,-1,-1):
        dpsm[i][k+1]+=dpsm[i-1][k]*9

    
    if N[i]=="0":
        for k in range(K,-1,-1):
            dpsm[i][k]+=dpsm[i-1][k]
            dp[i][k]+=dp[i-1][k]
    else:
        for k in range(K-1,-1,-1):
            dpsm[i][k+1]+=dp[i-1][k]*(int(N[i])-1)
            dp[i][k+1]+=dp[i-1][k]

        for k in range(K,-1,-1):
            dpsm[i][k]+=dpsm[i-1][k]
            dpsm[i][k]+=dp[i-1][k]
          
print(dp[-1][-1]+dpsm[-1][-1])

Buggy python submission (verdict: Runtime Error):
N=int(input())
K=int(input())

d=len(str(N)) #桁数
a=int(str(N)[0]) #最上位桁の数
if K==1:
  l=9*(d-1)
  m=a-1
  n=1
  print(int(l+m+n))
  
if K==2:
  if d=1:
    print(0)
  else:
    l=9**2*(d-1)*(d-2)/2 #最上位0
    m=(a-1)*9*(d-1) #最上位1~a-1 
    for i in range(1,d):
      if int(str(N)[i])!=0:
        n=(int(str(N)[i]))+9*(d-i-1)
        break
    print(int(l+m+n))
  
if K==3:
  if d<=2:
    print(0)
  else:
    l=9**3*(d-1)*(d-2)*(d-3)/6
    m=(a-1)*9**2*(d-1)*(d-2)/2
    for i in range(1,d):
      if int(str(N)[i])!=0:
        n=(int(str(N)[i]))*9*(d-i-1)+9**2*(d-i-1)*(d-i-2)/2
        break
    print(int(l+m+n))

Fixed submission:
N=input()
K=int(input())

dpsm=[[0]*(K+1) for _ in range(len(N))] #dpsm[i][k]
dp=[[0]*(K+1) for _ in range(len(N))]

dpsm[0][0]=1
dpsm[0][1]=int(N[0])-1
dp[0][1]=1

for i in range(1,len(N)):
    for k in range(K-1,-1,-1):
        dpsm[i][k+1]+=dpsm[i-1][k]*9

    
    if N[i]=="0":
        for k in range(K,-1,-1):
            dpsm[i][k]+=dpsm[i-1][k]
            dp[i][k]+=dp[i-1][k]
    else:
        for k in range(K-1,-1,-1):
            dpsm[i][k+1]+=dp[i-1][k]*(int(N[i])-1)
            dp[i][k+1]+=dp[i-1][k]

        for k in range(K,-1,-1):
            dpsm[i][k]+=dpsm[i-1][k]
            dpsm[i][k]+=dp[i-1][k]
          
print(dp[-1][-1]+dpsm[-1][-1])

Buggy python submission (verdict: Runtime Error):
N=int(input())
K=int(input())

d=len(str(N)) #桁数
a=int(str(N)[0]) #最上位桁の数
if K==1:
  l=9*(d-1)
  m=a-1
  n=1
  print(int(l+m+n))
  
if K==2:
  l=9**2*(d-1)*(d-2)/2 #最上位0
  m=(a-1)*9*(d-1) #最上位1~a-1 
  for i in range(1,d):
    if int(str(N)[i])!=0:
      n=(int(str(N)[i]))+9*(d-i-1)
      break
  print(int(l+m+n))
  
if K==3:
  l=9**3*(d-1)*(d-2)*(d-3)/6
  m=(a-1)*9**2*(d-1)*(d-2)/2
  for i in range(1,d):
    if int(str(N)[i])!=0:
      n=(int(str(N)[i]))*9*(d-i-1)+9**2*(d-i-1)*(d-i-2)/2
      break
  print(int(l+m+n))

Fixed submission:
N=input()
K=int(input())

dpsm=[[0]*(K+1) for _ in range(len(N))] #dpsm[i][k]
dp=[[0]*(K+1) for _ in range(len(N))]

dpsm[0][0]=1
dpsm[0][1]=int(N[0])-1
dp[0][1]=1

for i in range(1,len(N)):
    for k in range(K-1,-1,-1):
        dpsm[i][k+1]+=dpsm[i-1][k]*9

    
    if N[i]=="0":
        for k in range(K,-1,-1):
            dpsm[i][k]+=dpsm[i-1][k]
            dp[i][k]+=dp[i-1][k]
    else:
        for k in range(K-1,-1,-1):
            dpsm[i][k+1]+=dp[i-1][k]*(int(N[i])-1)
            dp[i][k+1]+=dp[i-1][k]

        for k in range(K,-1,-1):
            dpsm[i][k]+=dpsm[i-1][k]
            dpsm[i][k]+=dp[i-1][k]
          
print(dp[-1][-1]+dpsm[-1][-1])

Buggy python submission (verdict: Runtime Error):
N=int(input())
K=int(input())

d=len(str(N)) #桁数
a=int(str(N)[0]) #最上位桁の数
if K==1:
  l=9*(d-1)
  m=a-1
  n=1
  print(l+m+n)
  
if K==2:
  l=9**2*(d-1)*(d-2)/2 #最上位0
  m=(a-1)*9*(d-1) #最上位1~a-1 
  for i in range(1,d):
    if int(str(N)[i])!=0:
      n=(int(str(N)[i]))+9*(d-i-1)
      break
  print(l+m+n)
  
if K==3:
  l=9**3*(d-1)*(d-2)*(d-3)/6
  m=(a-1)*9**2*(d-1)*(d-2)/2
  for i in range(1,d):
    if int(str(N)[i])!=0:
      n=(int(str(N)[i]))*9*(d-i-1)+9**2*(d-i-1)*(d-i-2)/2
      break
  print(l+m+n)  

Fixed submission:
N=input()
K=int(input())

dpsm=[[0]*(K+1) for _ in range(len(N))] #dpsm[i][k]
dp=[[0]*(K+1) for _ in range(len(N))]

dpsm[0][0]=1
dpsm[0][1]=int(N[0])-1
dp[0][1]=1

for i in range(1,len(N)):
    for k in range(K-1,-1,-1):
        dpsm[i][k+1]+=dpsm[i-1][k]*9

    
    if N[i]=="0":
        for k in range(K,-1,-1):
            dpsm[i][k]+=dpsm[i-1][k]
            dp[i][k]+=dp[i-1][k]
    else:
        for k in range(K-1,-1,-1):
            dpsm[i][k+1]+=dp[i-1][k]*(int(N[i])-1)
            dp[i][k+1]+=dp[i-1][k]

        for k in range(K,-1,-1):
            dpsm[i][k]+=dpsm[i-1][k]
            dpsm[i][k]+=dp[i-1][k]
          
print(dp[-1][-1]+dpsm[-1][-1])

Buggy python submission (verdict: Runtime Error):
N=int(input())
K=int(input())

d=len(str(N)) #桁数
a=int(str(N)[0]) #最上位桁の数
if K==1:
  print(9*(d-1)+a)
  
if K==2:
  l=9**2*(d-1)*(d-2)/2 #最上位0
  m=(a-1)*9*(d-1) #最上位1~a-1 
  for i in range(1,d):
    if int(str(N)[i])!=0:
      n=(int(str(N)[i]))+9*(d-i-1)
      break
  print(l+m+n)
  
if K==3:
  l=9**3*(d-1)*(d-2)*(d-3)/6
  m=(a-1)*9**2*(d-1)*(d-2)/2
  for i in range(1,d):
    if int(str(N)[i])!=0:
      n=(int(str(N)[i]))*9*(d-i-1)+9**2*(d-i-1)*(d-i-2)/2
      break
  print(l+m+n)  

Fixed submission:
N=input()
K=int(input())

dpsm=[[0]*(K+1) for _ in range(len(N))] #dpsm[i][k]
dp=[[0]*(K+1) for _ in range(len(N))]

dpsm[0][0]=1
dpsm[0][1]=int(N[0])-1
dp[0][1]=1

for i in range(1,len(N)):
    for k in range(K-1,-1,-1):
        dpsm[i][k+1]+=dpsm[i-1][k]*9

    
    if N[i]=="0":
        for k in range(K,-1,-1):
            dpsm[i][k]+=dpsm[i-1][k]
            dp[i][k]+=dp[i-1][k]
    else:
        for k in range(K-1,-1,-1):
            dpsm[i][k+1]+=dp[i-1][k]*(int(N[i])-1)
            dp[i][k+1]+=dp[i-1][k]

        for k in range(K,-1,-1):
            dpsm[i][k]+=dpsm[i-1][k]
            dpsm[i][k]+=dp[i-1][k]
          
print(dp[-1][-1]+dpsm[-1][-1])