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 Rust (1.15.1) (judge verdict: Accepted)

use std::io::*;
use std::str::FromStr;

pub fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.expect("failed to read char") as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().expect("failed to parse token")
}

pub fn read_n<T: FromStr>(n: usize) -> Vec<T> {
    read_n_logic::<T>(n, vec![])
}

pub fn read_n_logic<T: FromStr>(n: usize, mut a: Vec<T>) -> Vec<T> {
    match n {
        0 => a,
        _ => {
            a.push(read::<T>());
            read_n_logic(n - 1, a)
        }
    }
}

use std::cmp::{max, min};

// watch
// ref https://www.youtube.com/watch?v=JQmQjJd-sLA&feature=youtu.be

fn main() {
    let s: String = read();
    let sv: Vec<char> = s.chars().collect();
    let K: usize = read();
    let n: usize = s.len();

    let mut dp: Vec<Vec<Vec<usize>>> = vec![vec![vec![0; 2]; K + 1]; n + 1];

    dp[0][0][0] = 1;

    for i in 0..n {
        for j in 0..K + 1 {
            // println!("j: {}", j);
            for k in 0..2 {
                let nd = sv[i] as u32 - '0' as u32;
                // println!("nd: {}", nd);
                for d in 0..10 {
                    let mut ni = i + 1;
                    let mut nj = j;
                    let mut nk = k;
                    if d != 0 {
                        nj += 1;
                    }

                    if nj > K {
                        continue;
                    }

                    if nk == 0 {
                        if d > nd {
                            continue;
                        }
                        if d < nd {
                            nk = 1
                        }
                    }
                    // println!(
                    //     "ni:{} nj:{} nk:{} d:{} i:{} j{} k{} v:{}",
                    //     ni, nj, nk, d, i, j, k, dp[i][j][k]
                    // );
                    dp[ni][nj][nk] += dp[i][j][k];
                }
            }
        }
    }

    let ans = dp[n][K][1] + dp[n][K][0];
    println!("{}", ans);
}