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 C# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Globalization;
using System.IO;
using System.Runtime.ExceptionServices;

class Program
{
    static void Main(string[] args) {
        var scan = new Scanner();
        var writer = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
        Console.SetOut(writer);
        Console.WriteLine(Solve(scan));
        Console.Out.Flush();
    }
    static object Solve(Scanner scan) {
        var N = Console.ReadLine();
        var K = scan.NextInt();
        var digits = N.Length;
        var dp = new long[digits + 1, 2, K + 1];
        dp[0, 0, 0] = 1;
        for (int d = 0; d < digits; d++) {
            var cur = N[d] - '0';
            for (int s = 0; s <= 1; s++) {
                for (int c = 0; c <= K; c++) {
                    var m = c == K ? 0 :
                            s == 1 ? 9 : cur;
                    for (int n = 0; n <= m; n++) {
                        var isSmall = s == 1 || n < cur ? 1 : 0;
                        var count = c == K ? K : 
                                    n != 0 ? c + 1 : c;
                        dp[d + 1, isSmall, count] += dp[d, s, c];
                    }
                }
            }
        }
        return dp[digits, 0, K] + dp[digits, 1, K];
    }
}

class Scanner
{
    string[] s;
    int i;
    public Scanner() {
        s = new string[0];
        i = 0;
    }
    public string Next() {
        if (i < s.Length) return s[i++];
        string str = Console.ReadLine();
        while (str == "") str = Console.ReadLine();
        s = str.Split(' ', StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return Next();
        i = 0;
        return s[i++];
    }
    public int NextInt() => int.Parse(Next());
}