Buggy python submission (verdict: Runtime Error):
def nCk(n, k):
    if n < k:
        return 0
    elif k == 0:
        return 1
    elif k == 1:
        return n
    elif k == 2:
        return n * (n - 1) // 2
    else:
        return n * (n - 1) * (n - 2) // 6


def solve(N, K):
    if K == 0:
        return 1

    M = len(N)
    res = solve(N[1:], K - 1)
    res += (int(N[0]) - 1) * nCk(M - 1, K - 1) * (9 ** (K - 1))
    res += nCk(M - 1, K) * (9 ** K)

    return res


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

print(solve(N, K))

Fixed submission:
def nCk(n, k):
    if k == 0:
        return 1
    elif k == 1:
        return n
    elif k == 2:
        return n * (n - 1) // 2
    else:
        return n * (n - 1) * (n - 2) // 6


def solve(N, K):
    if N == 0 and K > 0:
        return 0

    if K == 0:
        return 1

    M = len(str(N))
    hd, tl = divmod(N, 10 ** (M - 1))

    res = solve(tl, K - 1)
    res += (hd - 1) * nCk(M - 1, K - 1) * (9 ** (K - 1))

    if M > K:
        res += nCk(M - 1, K) * (9 ** K)

    return res


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

print(solve(N, K))

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


M = len(N)

if K == 1:
    ans = (M - 1) * 9 + int(N[0])
    print(ans)

if K == 2:
    ans = int(N[1])
    ans += (M - 2) * 9
    ans += (int(N[0]) - 1) * (M - 1) * 9
    ans += (M - 1) * (M - 2) // 2 * 9 * 9
    print(ans)

if K == 3:
    ans = int(N[2])
    ans += (M - 3) * 9
    ans += (int(N[1]) - 1) * (M - 2) * 9
    ans += (M - 2) * (M - 3) // 2 * 9 * 9
    ans += (int(N[0]) - 1) * (M - 1) * (M - 2) // 2 * 9 * 9
    ans += (M - 1) * (M - 2) * (M - 3) // 6 * 9 * 9 * 9
    print(ans)


Fixed submission:
def nCk(n, k):
    if k == 0:
        return 1
    elif k == 1:
        return n
    elif k == 2:
        return n * (n - 1) // 2
    else:
        return n * (n - 1) * (n - 2) // 6


def solve(N, K):
    if N == 0 and K > 0:
        return 0

    if K == 0:
        return 1

    M = len(str(N))
    hd, tl = divmod(N, 10 ** (M - 1))

    res = solve(tl, K - 1)
    res += (hd - 1) * nCk(M - 1, K - 1) * (9 ** (K - 1))

    if M > K:
        res += nCk(M - 1, K) * (9 ** K)

    return res


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

print(solve(N, K))

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


M = len(N)

if K == 1:
    ans = (M - 1) * 9 + int(N[0])
    print(ans)

if K == 2:
    ans = int(N[1])
    ans += (int(N[1]) - 1) * (M - 2) * 9
    ans += (int(N[0]) - 1) * (M - 1) * 9
    ans += (M - 1) * (M - 2) // 2 * 9 * 9
    print(ans)

if K == 3:
    ans = int(N[2])
    ans += (M - 3) * 9
    ans += (int(N[1]) - 1) * (M - 2) * 9
    ans += (M - 2) * (M - 3) // 2 * 9 * 9
    ans += (int(N[0]) - 1) * (M - 1) * (M - 2) // 2 * 9 * 9
    ans += (M - 1) * (M - 2) * (M - 3) // 6 * 9 * 9 * 9
    print(ans)


Fixed submission:
def nCk(n, k):
    if k == 0:
        return 1
    elif k == 1:
        return n
    elif k == 2:
        return n * (n - 1) // 2
    else:
        return n * (n - 1) * (n - 2) // 6


def solve(N, K):
    if N == 0 and K > 0:
        return 0

    if K == 0:
        return 1

    M = len(str(N))
    hd, tl = divmod(N, 10 ** (M - 1))

    res = solve(tl, K - 1)
    res += (hd - 1) * nCk(M - 1, K - 1) * (9 ** (K - 1))

    if M > K:
        res += nCk(M - 1, K) * (9 ** K)

    return res


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

print(solve(N, K))