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):

import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

N = list(map(int, sr()))
K = ir()
dp_dec = [0] * (K+1)
dp_reserved = [0] * (K+1)
dp_reserved[0] = 1
for x in N:
    prev = dp_dec[:]
    
    for k in range(K):
        dp_dec[k+1] += prev[k] * 9  
    if x > 0:
        for k in range(K+1):
            dp_dec[k] += dp_reserved[k]  
            if k < K:
                dp_dec[k+1] += dp_reserved[k] * (x - 1)  
        dp_reserved[1:] = dp_reserved[:-1]
        dp_reserved[0] = 0

answer = dp_dec[K] + dp_reserved[K]
print(answer)

Solution (Python):
import sys
sys.setrecursionlimit(700000)

def s_in():
    return input()

def n_in():
    return int(input())

def l_in():
    return list(map(int, input().split()))

def print_l(l):
    print(' '.join(map(str, l)))

class Interval():
    def __init__(self, li):
        self.li = li
        self.n = len(li)
        self.sum_li = [li[0]]
        for i in range(1, self.n):
            self.sum_li.append(self.sum_li[i-1] + li[i])

    def sum(self, a, b=None):
        if b is None:
            return self.sum(0, a)

        res = self.sum_li[min(self.n-1, b-1)]
        if a > 0:
            res -= self.sum_li[a-1]
        return res

n = s_in()
k = n_in()

res = 0
d = len(n)

def comb(a,b):
    if b == 0:
        return 1
    if b == 1:
        return a
    if b == 2:
        return a*(a-1)//2
    if b == 3:
        return (a)*(a-1)*(a-2)//6

    return 0
    


def calc(i, l):
    m = int(n[i])

    if i == d-1:
        if l == 0:
            if m == 0:
                return 1
            else:
                return 0
        elif l == 1:
            if m == 0:
                return 0
            else:
                return m
        else:
            return 0
            
        
    if m == 0:
        return calc(i+1, l)
    else:
        tmp = calc(i+1, l-1) 
        tmp += (m-1)*comb(d-i-1, l-1)*int(9**(l-1)) 
        tmp += comb(d-i-1,l)*int(9**l)

        return tmp

print(calc(0,k))

Solution (Python):
def calc(n, k):
    length = len(n)
    dp = [[[0]*(k + 1) for _ in range(2)] for _ in range(length+1)]
    dp[0][0][0] = 1
    for i in range(length):
        max_digit = int(n[i])
        for flag_less in range(2):
            for non_zero in range(4):
                range_digit = 9 if flag_less else max_digit
                for d in range(range_digit+1):
                    flag_less_next = 0
                    non_zero_next = non_zero
                    if flag_less == 1 or d < max_digit:
                        flag_less_next = 1
                    if d != 0:
                        non_zero_next = non_zero + 1
                    if non_zero_next < k + 1:
                        dp[i+1][flag_less_next][non_zero_next] += dp[i][flag_less][non_zero]
    return dp[length][0][k]+dp[length][1][k]

n = input()
k = int(input())

print(calc(n, k))

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

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

@lru_cache(None)
def F(N, K):
    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):

def solve(S, K):
    L = len(S)
    
    dp = [[[0 for _ in range(4)] for _ in range(2)] for _ in range(101)]
    dp[0][0][0] = 1
    for i in range(L):  
        D = int(S[i])
        for j in range(2):  
            for k in range(4):  
                for d in range(10 if j else D + 1):
                    cnt = k
                    if d != 0:
                        cnt += 1
                    if cnt > K:
                        continue
                    dp[i + 1][j or (d < D)][cnt] += dp[i][j][k]

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


def main():
    n = int(input())
    K = int(input())
    print(solve(str(n), K))


if __name__ == '__main__':
    main()

Solution (Python):

S = list(input())
K = int(input())
N = len(S)




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

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


for i in range(1, N + 1):
    for j in range(K + 1):
        for k in range(2):
            for num in range(10):
                if num == 0:
                    inc = 0
                else:
                    inc = 1
                
                if k == 1:
                    dp[i][j+inc][k] += dp[i - 1][j][k]
                else:
                    if num < int(S[i-1]):
                        
                        dp[i][j + inc][1] += dp[i - 1][j][k]
                    elif num == int(S[i-1]):
                        
                        dp[i][j + inc][0] += dp[i - 1][j][k]



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

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

string = str(N)
digits = len(string)



dp = [[[0]*2 for _ in range(K+1)] for __ in range(digits+1)]

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

for i in range(digits):
    d = int(string[i])
    ni = i+1
    for j in range(K+1):
        for k in range(2):
            for nd in range(10):
                nk = 1 if k == 1 and nd == d else 0
                if k == 1 and nd > d: break

                nj = j + (1 if nd != 0 else 0)
                if nj > K: continue

                dp[ni][nj][nk] += dp[i][j][k]

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

Solution (Python):
from functools import lru_cache

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

@lru_cache(maxsize=None)
def search(d, cnt, isEq):
    if d == len(N):
        return 1 if cnt == K else 0

    n = int(N[d])

    if isEq and n == 0:
        return search(d + 1, cnt, isEq)

    if isEq:
        ret = 0
        ret += search(d + 1, cnt, False)
        for _ in range(1, n):
            ret += search(d + 1, cnt + 1, False)
        ret += search(d + 1, cnt + 1, True)
        return ret

    return search(d + 1, cnt, False) + search(d + 1, cnt + 1, False) * 9

print(search(0, 0, True))

Solution (Python):
s = str(input())
K = int(input())

n = len(s)

dp = [[[0]*2 for i in range(4)] for j in range(105)]
dp[0][0][0] = 1

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

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

Solution (Python):
import sys

def solve():
    input = sys.stdin.readline
    N = input().strip("\n")
    K = int(input())
    dig = len(N)
    DP = [[0] * (K + 1) for _ in range(dig)]
    DP[0][0] = 1
    DP[0][1] = int(N[0]) - 1
    for i in range(1, dig):
        DP[i][1] = 9
        if N[i] != "0": 
            DP[i][0] = DP[i-1][0] + 1
            if DP[i-1][0] <= K: DP[i][DP[i-1][0]] += 1
            if DP[i-1][0] <= K - 1: DP[i][DP[i-1][0] + 1] += int(N[i]) - 1     
        else: DP[i][0] = DP[i-1][0]
        for j in range(1, K): DP[i][j+1] += DP[i-1][j] * 9
        for j in range(1, K + 1): DP[i][j] += DP[i - 1][j]
    if DP[dig - 1][0] == K: DP[dig - 1][K] += 1
    print(DP[dig - 1][K])
    

            
    return 0

if __name__ == "__main__":
    solve()

Solution (Python):
def main():
    S = input()
    N = len(S)
    K = int(input())
    dp = [[[0]*5 for j in range(2)] for i in range(N+1)]
    dp[0][0][0] = 1
    for i in range(N):
        for j in range(2):
            for k in range(4):
                lim = (9 if j else int(S[i]))
                for d in range(lim + 1):
                    
                    dp[i+1][j or d < lim][k + int(d != 0)] += dp[i][j][k]
    print((dp[N][0][K]+dp[N][1][K]))


if __name__ == '__main__':
    main()

Solution (Python):
n = input()
k = int(input())
dp = [[0] * (3 + 1) for i in range(len(n))]
dp[0][1] = int(n[0])
cnt = 1
for i in range(1, len(n)):
  m = int(n[i])
  if cnt == 1:
    dp[i][1] = 9 + dp[i - 1][1]
    dp[i][2] = (dp[i - 1][1] - 1) * 9 + dp[i - 1][2] + m
    dp[i][3] = dp[i - 1][2] * 9 + dp[i - 1][3]
  elif cnt == 2:
    dp[i][1] = 9 + dp[i - 1][1]
    dp[i][2] = dp[i - 1][1] * 9 + dp[i - 1][2]
    dp[i][3] = (dp[i - 1][2] - 1) * 9 + dp[i - 1][3] + m
  else:
    dp[i][1] = 9 + dp[i - 1][1]
    dp[i][2] = dp[i - 1][1] * 9 + dp[i - 1][2]
    dp[i][3] = dp[i - 1][2] * 9 + dp[i - 1][3]
  if m != 0:
    cnt += 1
print(dp[len(n) - 1][k])

Solution (Python):
N=str(input())
n=len(N)
K=int(input())
dp0=[[0]*(K+1) for _ in range(n+1)]
dp1=[[0]*(K+1) for _ in range(n+1)]
for l in range(1,n+1):
  dp0[l][0]=1
dp1[0][0]=1
for i in range(n):
  for j in range(K):
    if N[i]=='0':
      dp0[i+1][j+1]=9*dp0[i][j]+dp0[i][j+1]
      dp1[i+1][j+1]=dp1[i][j+1]
    else:
      dp0[i+1][j+1]=dp0[i][j]*9+dp0[i][j+1]+dp1[i][j]*(int(N[i])-1)+dp1[i][j+1]
      dp1[i+1][j+1]=dp1[i][j]
print(dp0[n][K]+dp1[n][K])

Solution (Python):

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


N_bin = str(N)
digit_len_N = len(N_bin)
DP = [[[0]*(K+2) for _ in range(2)] for _ in range(digit_len_N+1)]




DP[0][0][0] = 1


for digit in range(digit_len_N):
    max_digit = int(N_bin[digit])
    for smaller in range(2):  
        for k in range(K+1):
            cand_digits = max_digit+1 if smaller == 0 else 10  
            for next in range(cand_digits):
                if next == max_digit and smaller == 0:  
                    if next == 0:  
                        DP[digit+1][0][k] += DP[digit][0][k]
                    else:
                        DP[digit+1][0][k+1] += DP[digit][0][k]
                else:  
                    if next == 0:  
                        DP[digit+1][1][k] += DP[digit][smaller][k]
                    else:
                        DP[digit+1][1][k+1] += DP[digit][smaller][k]
print(DP[-1][0][K]+DP[-1][1][K])

Solution (Python):

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


N_bin = str(N)
bit_len_N = len(N_bin)
DP = [[[0]*(K+2) for _ in range(2)] for _ in range(bit_len_N+1)]



DP[0][0][0] = 1


for bit in range(bit_len_N):
    max_digit = int(N_bin[bit])
    for smaller in range(2):
        for k in range(K+1):
            cand_digits = max_digit+1 if smaller == 0 else 10
            for next in range(cand_digits):
                if next == max_digit and smaller == 0:
                    if next == 0:
                        DP[bit+1][0][k] += DP[bit][0][k]
                    else:
                        DP[bit+1][0][k+1] += DP[bit][0][k]
                else:
                    if next == 0:
                        DP[bit+1][1][k] += DP[bit][smaller][k]
                    else:
                        DP[bit+1][1][k+1] += DP[bit][smaller][k]
print(DP[-1][0][K]+DP[-1][1][K])

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

Solution (Python):

def f(i,k):
    if i == 0:
        return 0
    num = int(s[-i])
    if k == 0:
        if num ==0:return 0
        else: return 1
    if num==0:
        return 0
    return (num-1)* ncr(i-1,k-1)*9**(k-1)+ncr(i-1,k)*9**k


def g(i,k):
    if i == 0:
        if k==0:return 1
        else:return 0
    num = int(s[-i])
    if k == 0:
        if num ==0:return 1
        else:return 0

    if num==0:
        return f(i-1,k) +g(i-1,k)
    else:
        return f(i-1,k-1) + g(i-1,k-1)



def ncr(n,r):
    x = 1
    y = 1
    for i in range(r):
        x*=(n-i)
        y*=(i+1)
    return x//y

s = input()
k = int(input())
l= len(s)
print(f(l,k)+g(l,k))

Solution (Python):
n=list(input())
N=len(n)
k=int(input())
dp1=[[0 for i in range(k+1)] for j in range(N+1)]
dp2=[[0 for i in range(k+1)] for j in range(N+1)]
dp1[0][0]=1
for i in range(1,N+1):
  x=int(n[i-1])
  
  if i!=N and x!=0:
    for j in range(k+1):
      if j==0:
        dp2[i][j]=dp1[i-1][j]+dp2[i-1][j]
      else:
        dp1[i][j]=dp1[i-1][j-1]
        dp2[i][j]=dp1[i-1][j]+dp1[i-1][j-1]*(x-1)+dp2[i-1][j]+dp2[i-1][j-1]*9
  
  elif i!=N and x==0:
    for j in range(k+1):
      if j==0:
        dp1[i][j]=dp1[i-1][j]
        dp2[i][j]=dp2[i-1][j]
      else:
        dp1[i][j]=dp1[i-1][j]
        dp2[i][j]=dp2[i-1][j]+dp2[i-1][j-1]*9
  
  elif i==N and x!=0:
    for j in range(k+1):
      if j==0:
        dp2[i][j]=dp1[i-1][j]+dp2[i-1][j]
      else:
        dp2[i][j]=dp1[i-1][j]+dp1[i-1][j-1]*x+dp2[i-1][j]+dp2[i-1][j-1]*9
  
  else:
    for j in range(k+1):
      if j==0:
        dp2[i][j]=dp2[i-1][j]
      else:
        dp2[i][j]=dp1[i-1][j]+dp2[i-1][j]+dp2[i-1][j-1]*9

print(dp2[N][k])

Solution (Python):
n = input()
K = int(input())
L = len(n)

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

for i in range(L):
    d = int(n[i])
    for j in [0, 1]:
        for k in range(K + 1):
            if j == 0:
                dp[i + 1][j][k] += dp[i][j][k]
                dp[i + 1][j][k + 1] += dp[i][j][k] * 9
            else:
                if d > 0:
                    dp[i + 1][0][k] += dp[i][j][k]
                    dp[i + 1][0][k + 1] += dp[i][j][k] * (d - 1)
                    dp[i + 1][1][k + 1] += dp[i][j][k]
                else:
                    dp[i + 1][1][k] += dp[i][j][k]


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

Solution (Python):

from functools import lru_cache

@lru_cache(maxsize=10000)
def calc(n, k):
    

    if n < 10:
        if k > 1:
            
            return 0
        elif k == 1:
            
            return n
        else:
            return 1

    else:
        if k == 0:
            
            return 1
        else:
            
            return calc(n // 10, k - 1) * (n % 10) + calc(n // 10 - 1, k - 1) * (9 - n % 10) + calc(n // 10, k)

n = int(input())
k = int(input())

print(calc(n, k))

Solution (Python):
def main():
    mod = 10**9+7
    fact = [1, 1]
    for i in range(2, 10**3):
        fact.append(fact[-1]*i % mod)

    def inv_n(n):
        return pow(n, mod-2, mod)

    def nCr(n, r, mod=10**9+7):
        return inv_n(fact[n-r]*fact[r] % mod)*fact[n] % mod

    n = input()
    k = int(input())

    m = len(n)
    ans2 = 0
    if len(n)-n.count("0") == k:
        ans2 = 1
    for i in range(m):
        s = n[i]
        if s != "0":
            if k >= 0 and m-i-1 >= k:
                ans2 += nCr(m-i-1, k)*pow(9, k)
            k -= 1
            if k >= 0 and m-i-1 >= k:
                ans2 += (int(n[i])-1)*nCr(m-i-1, k)*pow(9, k)
    print(ans2)


main()

Solution (Python):
N = int(input())
K = int(input())
dp = [[[0]*2 for _ in range(4)] for _ in range(105)]





dp[0][0][0] = 1
length = len(str(N))
for i in range(length):
    for j in range(4):
        for k in range(2):
            next_digit = int(str(N)[i])
            for d in range(10):
                ni = i+1
                nj = j
                nk = k
                if d != 0:
                    nj += 1
                if nj > K:
                    continue
                if k == 0:
                    if next_digit < d:
                        continue
                    if next_digit > d:
                        nk = 1
                dp[ni][nj][nk] += dp[i][j][k]
ans = dp[length][K][0] + dp[length][K][1]
print(ans)

Solution (Python):
mod = 10**9 + 7
from collections import deque

def main():
    N = iip(False)
    K = iip(False)
    ret = solve(N, K)
    print(ret)

def solve(N, K):

    strn = str(N)

    A = []
    B = []

    for i in range(len(strn), 0, -1):
        if strn[len(strn)-i] != "0":
            A.append(int(strn[len(strn)-i]))
            B.append(i)


    if K == 1:
        return 9*(B[0]-1) + A[0]


    if K == 2:
        if len(strn) < 2:
            return 0
        ret = 0
        ret += (B[0]-1) * (B[0]-2) // 2 * (9**2) 
        ret += (A[0]-1) * 9 * (B[0]-1) 
        if len(B) >= 2 and len(A) >= 2:
            ret += (B[1]-1) * 9 + A[1] 
        return ret

    ret = 0
    ret += (B[0] - 1) * (B[0] - 2) * (B[0] - 3) // 6 * 9**3 
    ret += (A[0] - 1) * (B[0] - 1) * (B[0] - 2) // 2 * 9**2 

    
    if len(strn) < 3:
        return 0
    if len(B) >= 2:
        ret += (B[1]-1) * (B[1]-2) // 2 * 9**2 
    if len(B) >= 2 and len(A) >= 2:
        ret += (A[1] - 1) * (B[1]-1) * 9 
    if len(B) >= 3 and len(A) >= 3:
        ret += (B[2] - 1) * 9 + A[2] 
    return ret




def split_print_space(s):
    print(" ".join([str(i) for i in s]))

def split_print_enter(s):
    print("\n".join([str(i) for i in s]))


def searchsorted(sorted_list, n, side):
    if side not in ["right", "left"]:
        raise Exception("sideはrightかleftで指定してください")

    l = 0
    r = len(sorted_list)

    if n > sorted_list[-1]:
        
        return len(sorted_list)
    if n < sorted_list[0]:
        return 0

    while r-l > 1:
        x = (l+r)//2
        if sorted_list[x] > n:
            r = x
        elif sorted_list[x] < n:
            l = x
        else:
            if side == "left":
                r = x
            elif side == "right":
                l = x

    if side == "left":
        if sorted_list[l] == n:
            return r - 1
        else:
            return r

    if side == "right":
        if sorted_list[l] == n:
            return l + 1
        else:
            return l


def iip(listed):
    ret = [int(i) for i in input().split()]
    if len(ret) == 1 and not listed:
        return ret[0]
    return ret

def soinsuu_bunkai(n):
    ret = []
    for i in range(2, int(n**0.5)+1):
        while n % i == 0:
            n //= i
            ret.append(i)
        if i > n:
            break
    if n != 1:
        ret.append(n)
    return ret


def conbination(n, r, mod, test=False):
    if n <=0:
        return 0
    if r == 0:
        return 1
    if r < 0:
        return 0
    if r == 1:
        return n
    ret = 1
    for i in range(n-r+1, n+1):
        ret *= i
        ret = ret % mod

    bunbo = 1
    for i in range(1, r+1):
        bunbo *= i
        bunbo = bunbo % mod

    ret = (ret * inv(bunbo, mod)) % mod
    if test:
        
        pass
    return ret


def inv(n, mod):
    return power(n, mod-2)

def power(n, p):
    if p == 0:
        return 1
    if p % 2 == 0:
        return (power(n, p//2) ** 2) % mod
    if p % 2 == 1:
        return (n * power(n, p-1)) % mod


if __name__ == "__main__":
    main()

Solution (Python):
n = list(str(input()))
l = len(n)
for i in range(l):
	n[i] = int(n[i])
k = int(input())
dp0 = [[0 for _ in range(4)] for _ in range(l+1)]
dp1 = [[0 for _ in range(4)] for _ in range(l+1)]
dp0[1] = [1, n[0]-1, 0, 0]
dp1[1] = [0, 1, 0, 0]
for i in range(2,l+1):
	for j in range(4):
		p = n[i-1]
		if j == 0:
			if p == 0:
				dp0[i][j] = dp0[i-1][j]
				dp1[i][j] = dp1[i-1][j]
			else:
				dp0[i][j] = 1
				dp1[i][j] = 0
		elif p == 0:
			dp1[i][j] = dp1[i-1][j]
			dp0[i][j] = dp0[i-1][j] + 9 * dp0[i-1][j-1]
		else:
			dp1[i][j] = dp1[i-1][j-1]
			dp0[i][j] = dp0[i-1][j] + 9 * dp0[i-1][j-1] + dp1[i-1][j] + (p-1) * dp1[i-1][j-1]
print(dp0[l][k] + dp1[l][k])

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

dig=[]
n=N
while n>0:
  dig.append(n%10)
  n//=10
a=dig[-1]
digit=len(dig)

dp=[[0 for _ in range(digit)] for _ in range(3)]

dp[0][0]=dig[0]
for i in range(1,digit):
  if dig[i]==0:
    dp[0][i]=dp[0][i-1]
  else:
    dp[0][i]=dig[i]+9*i

for i in range(1,digit):
  if dig[i]==0:
    dp[1][i]=dp[1][i-1]
  else:
    dp[1][i]=dp[0][i-1]+(dig[i]-1)*9*i+81*i*(i-1)//2
    
for i in range(1,digit):
  if dig[i]==0:
    dp[2][i]=dp[2][i-1]
  else:
    dp[2][i]=(dp[1][i-1]  
              +(dig[i]-1)*81*i*(i-1)//2 
              +729*i*(i-1)*(i-2)//6) 
print(dp[K-1][digit-1])

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

dp = [[[0 for _ in range(2)] for _ in range(4)] for _ in range(105)]
dp[0][0][0] = 1

for i in range(N):
    for j in range(4):
        for k in range(2):
            nd = int(S[i])
            for d in range(10):
                ni = i+1
                nj = j
                nk = k
                if d != 0:
                    nj += 1
                if nj > K:
                    continue
                if k == 0:
                    if d > nd:
                        continue
                    if d < nd:
                        nk = 1
                dp[ni][nj][nk] += dp[i][j][k]

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

Solution (Python):

import sys
sys.setrecursionlimit(10**8)


def solve(N: int, K: int):
    NL = list(map(int, N))

    useMaxDigits = [[0 for i in range(K+1)] for j in range((len(N)+1))]
    noUseMaxDigits = [[0 for i in range(K+1)]
                      for j in range((len(N)+1))]
    useMaxDigits[0][0] = 1
    for digit in range(1, len(N)+1):
        D = NL[digit-1]
        
        if D != 0:
            for i in range(1, K+1):
                useMaxDigits[digit][i] = useMaxDigits[digit-1][i-1]
            for i in range(0, K+1):
                noUseMaxDigits[digit][i] = useMaxDigits[digit-1][i]
        else:
            for i in range(0, K+1):
                useMaxDigits[digit][i] = useMaxDigits[digit-1][i]

        
        for i in range(0, K+1):
            
            noUseMaxDigits[digit][i] += noUseMaxDigits[digit-1][i]
        for i in range(1, K+1):
            noUseMaxDigits[digit][i] += noUseMaxDigits[digit-1][i-1]*9
        for i in range(1, K+1):
            noUseMaxDigits[digit][i] += useMaxDigits[digit -
                                                     1][i-1]*max(0, D-1)
    print(useMaxDigits[-1][-1]+noUseMaxDigits[-1][-1])
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = next(tokens)  
    K = int(next(tokens))  
    solve(N, K)


if __name__ == '__main__':
    main()

Solution (Python):



from math import factorial

def main():
  s = list(map(int, input().strip()))
  k = int(input())
  dp = []
  dp.append([[0]*(len(s)+1) for _ in range(k+1)])
  dp.append([[0]*(len(s)+1) for _ in range(k+1)])
  dp[0][0][0] = 1
  
  for i, c in enumerate(s):
    for j in range(k+1):
        for d in range(10):
          nj = j
          if d != 0:
            nj+=1
          if nj > k:
            continue
            
          if d < c:
            dp[1][nj][i+1] += dp[0][j][i]
          if d == c:
            dp[0][nj][i+1] += dp[0][j][i]
          dp[1][nj][i+1] += dp[1][j][i]
            
  print(dp[0][k][len(s)]+dp[1][k][len(s)])

if __name__=='__main__':
  main()

Solution (Python):
import math
import itertools
n=int(input())
k=int(input())
def comb(n,k):
    return len(list(itertools.combinations(range(n), k)))
x=str(n)
m=len(x)
ans=0
for i in range(1,m):
    ans+=(comb(i-1,k-1)*(9**k))
if k==1:
    for i in range(1,10):
        if i*(10**(m-1))<=n:
            ans+=1
elif k==2:
    for i in range(1,10):
        if i*(10**(m-1))<=n:
            for j in range(m-1):
                for a in range(1,10):
                    if i*(10**(m-1))+a*(10**(j))<=n:
                        ans+=1
                    else:
                        break
        else:
            break
else:
    po=[10**i for i in range(m)]
    for i in range(1,10):
        if i*po[m-1]<=n:
            for j in range(m-1):
                for a in range(1,10):
                    if i*po[m-1]+a*po[j]<=n:
                        for b in range(j):
                            for c in range(1,10):
                                ans+=(i*po[m-1]+a*po[j]+c*po[b]<=n)
                    else:
                        break
        else:
            break
print(ans)

Solution (Python):
def main():
    N = input()
    len_N = len(N)
    K = int(input())
    dp = [[[0] * 2 for _ in range(K + 5)] for _ in range(len_N + 5)]
    dp[0][0][0] = 1
    for i in range(len_N):
        tmp = int(N[i])
        for j in range(K+1):
            
            for l in range(2):
                for x in range(10):
                    if l == 0:
                        if x > tmp:
                            continue
                        elif x == tmp:
                            if x == 0:
                                dp[i+1][j][0] += dp[i][j][0]
                            else:
                                dp[i+1][j+1][0] += dp[i][j][0]
                        else:
                            if x == 0:
                                dp[i+1][j][1] += dp[i][j][0]
                            else:
                                dp[i+1][j+1][1] += dp[i][j][0]
                    if l == 1:
                        if x == 0:
                            dp[i+1][j][1] += dp[i][j][1]
                        else:
                            dp[i+1][j+1][1] += dp[i][j][1]
    
    print(dp[len_N][K][0] + dp[len_N][K][1])


        




if __name__ == "__main__":
    main()

Solution (Python):

n = list(map(int, list(input())))
k = int(input())
l = len(n)
dp0 = [[0]*(k+1) for _ in range(l+1)] 
dp1 = [[0]*(k+1) for _ in range(l+1)]

for i in range(1, l+1):
    dp0[i][0] = 1

dp1[0][0] = 1

for i in range(l):
    for j in range(k):

        if n[i] > 0:

            dp0[i+1][j+1] = \
                9*dp0[i][j] + dp0[i][j+1] + dp1[i][j+1] + (n[i]-1)*dp1[i][j]
            dp1[i + 1][j + 1] = dp1[i][j]
        else:

            dp0[i+1][j+1] = \
                9*dp0[i][j] + dp0[i][j+1]
            dp1[i + 1][j + 1] = dp1[i][j + 1]




print(dp0[l][k] + dp1[l][k])

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

def solve(N, K):
    l = len(N)
    dp = [[[0] * (10) for i in range(2)] for _ in range(l+1)]
    dp[0][0][0] = 1
    for i in range(l):
        D = int(N[i])
        for j in range(2):
            for k in range(K+1):
                for d in range(10):
                    if j == 0 and d > D: break
                    dp[i+1][j or (d < D)][k if d == 0 else k+1] += dp[i][j][k]
    
    return dp[l][0][K] + dp[l][1][K]

print(solve(N,K))

Solution (Python):

n = input()
N = len(n)
k = int(input())
dp = [[[0] * 2 for j in range(k + 1)] for i in range(N + 1)]
dp[0][0][0] = 1
for i in range(N):
    for j in range(k + 1):
        d = int(n[i])
        
        
        if d > 0:
            dp[i + 1][j][1] += dp[i][j][0]
        else:
            dp[i + 1][j][0] += dp[i][j][0]
        dp[i + 1][j][1] += dp[i][j][1]

        
        if j < k:
            if d > 0:
                dp[i + 1][j + 1][0] += dp[i][j][0]
                dp[i + 1][j + 1][1] += dp[i][j][0] * (d - 1)
            dp[i + 1][j + 1][1] += dp[i][j][1] * 9

print(dp[N][k][0] + dp[N][k][1])

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

l = len(str(n))

s = [0] + list(map(int,str(n))) 

if k > l:
  print(0)
  exit()

dp = [[[0]*2 for _ in range(k+1)] for _ in range(l+1)]






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


for dig in range(l): 
  for i in range(k+1): 
    dp[dig+1][i][0] += dp[dig][i][0]
    dp[dig+1][i][1 if s[dig+1] == 0 else 0] += dp[dig][i][1]
    
    
    if i < k:
      dp[dig+1][i+1][0] += dp[dig][i][0] * 9
      if s[dig+1] > 0:
      	dp[dig+1][i+1][1] += dp[dig][i][1]
      	dp[dig+1][i+1][0] += dp[dig][i][1] * (s[dig+1]-1)

print(dp[l][k][0]+dp[l][k][1])

Solution (Python):
n, k = int(input()), int(input())
l = len(str(n))
s = [0] + list(map(int, str(n)))
if k > l: print(0); exit()

dp = [[[0] * 2 for _ in range(k+1)] for _ in range(l+1)]
dp[0][0][1] = 1

for dig in range(l):
    for i in range(k+1):
        dp[dig+1][i][0] += dp[dig][i][0]
        dp[dig+1][i][1 if s[dig+1] == 0 else 0] += dp[dig][i][1]
        if i < k:
            dp[dig+1][i+1][0] += dp[dig][i][0] * 9
            if s[dig+1] > 0:
                dp[dig+1][i+1][1] += dp[dig][i][1]
                dp[dig+1][i+1][0] += dp[dig][i][1] * (s[dig+1]-1)

print(dp[l][k][0] + dp[l][k][1])

Solution (Python):
from collections import deque
n = input()
a = [int(i) for i in list(n)]
k = int(input())
n = len(n)
ans = 0

if k > n:
    ans = 0
elif k == 1:
    
    ans += a[0]
    
    ans += (n - 1)*9
elif k == 2:
    
    for i in range(1, n):
        if a[i] != 0:
            j = i
            break
    else:
        j = n - 1
    ans += a[j] + (n - j - 1)*9
    
    ans += (a[0] - 1)*(n - 1)*9
    
    ans += (n - 1)*(n - 2)*9*9//2
else:
    
    j, l= -1, n - 1
    for i in range(1, n):
        if a[i] != 0:
            if j == -1:
                j = i
            else:
                l = i
                break
    if j != -1 and j != n - 1:
        ans += a[l] + (n - l - 1)*9 + (a[j] - 1)*(n - j - 1)*9 + (n - j - 1)*(n - j - 2)*9*9//2
    
    ans += (a[0] - 1)*(n - 1)*(n - 2)*9*9//2
    
    ans += (n - 1)*(n - 2)*(n - 3)*9*9*9//6
print(ans)

Solution (Python):

n = input()
s = len(n)
x = int(input())


dp = [[[0 for _ in range(4)] for _ in range(2)] for _ in range(s+1)]
dp[0][0][0] = 1

for i in range(1, s+1):
    for k in [0, 1, 2, 3]:
        if n[i-1] == '0':
            dp[i][0][k] += dp[i-1][0][k]
        else:
            dp[i][1][k] += dp[i-1][0][k]
            if k > 0:
                dp[i][0][k] += dp[i-1][0][k-1]
                dp[i][1][k] += dp[i-1][0][k-1] * (int(n[i-1]) - 1)
        
        dp[i][1][k] += dp[i-1][1][k]
        if k > 0:
            dp[i][1][k] += dp[i-1][1][k-1] * 9
   
print(dp[s][0][x] + dp[s][1][x])

Solution (Python):

import sys
input = sys.stdin.readline

n = int(input())
s = str(n)
K = int(input())
l = len(str(n))


dp = [[[0] * l for _ in range(K + 2)] for _ in range(2)]
for i, item in enumerate(s):
    val = int(item)
    
    if i == 0:
        dp[0][0][i] = 1
        for j in range(1, 10):
            if j < val:
                dp[0][1][i] += 1
            if j == val:
                dp[1][1][i] += 1
        continue
    for j in range(10):
        is_not_zero = j != 0
        if val == j:
            for k in range(K+1):
                dp[0][k+is_not_zero][i] += dp[0][k][i-1]
                dp[1][k+is_not_zero][i] += dp[1][k][i-1]
        elif j < val:
            for k in range(K+1):
                dp[0][k+is_not_zero][i] += dp[0][k][i-1] + dp[1][k][i-1]
        else:
            for k in range(K+1):
                dp[0][k+is_not_zero][i] += dp[0][k][i-1]

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