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 PyPy3 (2.4.0) (judge verdict: Wrong Answer)

n=int(input())
lenn=len(str(n))

k=int(input())
#k:1~3

result=0

if n <=9:
    if k==1:
        print(n)
    else:
        print(0)
    exit()
elif n <=99:
    if k==1:
        print(n//10+9)
    elif k==2:
        print((n//10-1)*9+n%10)
    else:
        print(0)
    exit()
    """
elif n <=999:
    if k==1:
        print(n//100+9+9)
    elif k==2:
        print((n//100-1)*9+9*9)
    else:
        print(0)
    exit()
    """
else:
    if k==1:
        print(9*(lenn-1)+n//(10**(lenn-1)))
    elif k==2:
        result=0
        #先頭の文字を抜いた状態で
        result+=9*9*(lenn-1)*(lenn-2)//1//2
        #print(result)
        #先頭の文字を1ひいて
        result+=(n//(10**(lenn-1))-1)*9*(lenn-1)
        #print(result)

        for i in str(n)[1:]:
            result+=int(i)
        print(result)
    else:
        if lenn==3:
            result=(n//(10**(lenn-1))-1)*9*9
            temp=1
            for i in str(n)[1:]:
                temp=temp*int(i)
            result+=temp
            print(result)
            exit()
        else:
            result=0
            #先頭の文字を抜いた状態で
            result+=9*9*9*(lenn-1)*(lenn-2)*(lenn-3)//1//2//3
        #先頭の文字を1ひいて
            result+=(n//(10**(lenn-1))-1)*9*9*(lenn-1)*(lenn-2)
            temp=1
            i=1
            while i<lenn:
                j=i+1
                while j<lenn:
                    result+=int(str(n)[i])*int(str(n)[j])
                    j+=1
                i+=1

            print(result)