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.Text;
using static Scanner;

class Program
{
    static void Main(string[] args) {
        var writer = new StreamWriter(Console.OpenStandardOutput()) {AutoFlush = false};
        Console.SetOut(writer);
        Solve();
        Console.Out.Flush();
    }
    static void Solve() {
        var N = Scan();
        var K = ScanInt();
        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];
                    }
                }
            }
        }
        Console.WriteLine(dp[digits, 0, K] + dp[digits, 1, K]);
    }
}

public static class Scanner
{
    static readonly byte[] buffer = new byte[1024];
    static int length, p;
    static bool isEof;

    private static byte ConsoleIn() {
        var stream = Console.OpenStandardInput();
        if (isEof) return 0;
        if (p < length) return buffer[p++];
        p = 0;
        if ((length = stream.Read(buffer, 0, 1024)) > 0) return buffer[p++];
        isEof = true;
        return 0;
    }
    private static char Char() {
        var let = ConsoleIn();
        while ((let < '!' || '~' < let) && !isEof) let = ConsoleIn();
        return (char) let;
    }
    public static string Scan() {
        var sb = new StringBuilder();
        for (var let = Char(); '!' <= let && let <= '~'; let = (char) ConsoleIn()) {
            sb.Append(let);
        }
        return sb.ToString();
    }
    public static int ScanInt() {
        var temp = 0;
        for (var let = Char(); '0' <= let && let <= '9'; let = (char) ConsoleIn()) {
            temp = temp * 10 + let - '0';
        }
        return temp;
    }
}