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 Java (OpenJDK 11.0.6) (judge verdict: Time Limit Exceeded)

import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        Scanner sc = new Scanner(System.in);
        String n = sc.next();
        int k = sc.nextInt();
        
        // que: ルンルン数(次の桁に-1～+1で行ける)
        // ex. 334, 2123, 3234566667
        Queue<String[]> que = new ArrayDeque<String[]>();
        
        // 1～9からBFSスタート
        for (int i = 1; i <= 9; i++) {
            String tmp = String.valueOf(i);
            que.add(new String[]{tmp, "1"});
        }
        
        // count: キューから出した回数
        int count = 0;
        while (!que.isEmpty()) {
            String[] curtmp = que.poll();
            String cur = curtmp[0];
            int nonzero = Integer.parseInt(curtmp[1]);
            
            // System.out.println(cur);
            
            if (nonzero > k) continue;
            
            if (cur.length() < n.length()) {
                if (nonzero == k) {
                    count++;
                    // System.out.println(cur);
                }
            } else if (cur.length() == n.length()) {
                if (nonzero != k) continue;
                
                // n以下かどうか
                boolean under = true;
                for (int i = 0; i < n.length(); i++) {
                    if (cur.charAt(i) < n.charAt(i)) {
                        break;
                    } else if (cur.charAt(i) > n.charAt(i)) {
                        under = false;
                        break;
                    }
                }
                
                if (under) {
                    count++;
                    // System.out.println(cur);
                }
                continue;
            }
            
            // 次の桁のパターンをキューに入れる
            for (int i = 0; i <= 9; i++) {
                String tmp = String.valueOf(i);
                
                if (i == 0) {
                    que.add(new String[]{cur + tmp, String.valueOf(nonzero)});
                } else {
                    que.add(new String[]{cur + tmp, String.valueOf(nonzero + 1)});
                }
            }
        }
        System.out.println(count);
    }
}