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 Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		try (Scanner sc = new Scanner(System.in)) {
			String str = sc.next();
			int num = sc.nextInt();
			String head = str.substring(0, num);
			long ans = 0;
			if (num == 1) {
				ans += Long.parseLong(head);
				ans += (str.length() - 1) * 9;
				System.out.println(ans);
			}
			if (num == 2) {
				int i = Integer.parseInt(head.substring(0, 1));
				int j = Integer.parseInt(head.substring(1, 2));
				ans += j;
				ans += 9 * (str.length() - head.length());
				ans += (i - 1) * 9 * (str.length() + 1 - head.length());
				if (!str.equals(head)) {
					ans += ((str.length() - 1) * (str.length() - 2)) * 81 / 2;
				}
				System.out.println(ans);
			}
			if (num == 3) {
				int i = Integer.parseInt(head.substring(0, 1));
				int j = Integer.parseInt(head.substring(1, 2));
				int k = Integer.parseInt(head.substring(2, 3));
				ans += k;
				ans += (j - 1) * 9 * (str.length() + 1 - head.length());
				ans += 9 * (str.length() - head.length());
				ans += ((str.length() - 2) * (str.length() - 3)) * 81 / 2;
				ans += (i - 1) * ((str.length() - 1) * (str.length() - 2)) * 81 / 2;
				if (!str.equals(head)) {
					ans += ((str.length() - 1) * (str.length() - 2) * (str.length() - 3)) * 729 / 6;
				}
				System.out.println(ans);
			}
		}
	}
}