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 in Python (3.4.3) (judge verdict: Runtime Error)

N = int(input())
K = int(input())
from math import log10
from operator import mul
from functools import reduce

def comb(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


lg = int(log10(N))

def dp(n:int,k:int,K:int):
    lg = int(log10(N))
    if n > N:
        return 0
    if k > K:
        return 0
    if k == K:
        tmp = lg - int(log10(n))
        return tmp if n*10**tmp > N else tmp + 1
    sm = 0
    for i in range(1,10):
        sm += dp(n*10 + i,k + 1,K)
    return sm + dp(n*10,k,K)

memo = {}
def dp2(string:str,k:int):
    if (string,k) in memo:
        return memo[(string,k)]
    if k == 1:
        ans = 0
        for i in range(int( log10( N ) ) + 2):
            digit = 10**i
            for j in range(1,10):
                if j*digit <= N:
                    ans += 1
        memo[(string,k)] = ans
        return memo[(string,k)]

    if string[0] == "0":
        memo[(string,k)] =  dp2(string[1:],k)
        return memo[(string,k)]

    if len(string) > 3:
        ln = len(string)
        tmp = (comb(ln - 1,k)*9**k)*(string[0]) + dp2(string[1:],k)
        memo[(string,k)] = tmp
        return tmp
    memo[(string,k)] = dp(int(string),0,K)
    return memo[(string,k)]

def main():
    if K == 1:
        ans = 0
        for i in range(int( log10( N ) ) + 2):
            digit = 10**i
            for j in range(1,10):
                if j*digit <= N:
                    ans += 1
        print(ans)
        return
    if N < 10**10:
        ans = 0
        for i in range(1,10):
            ans += dp(i,1,K)
        print(ans)
    print(dp2(str(N),K))
    return

if __name__ == "__main__":
    main()