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)

fn main() {
    let (r, w) = (std::io::stdin(), std::io::stdout());
    let mut sc = IO::new(r.lock(), w.lock());
    let s = sc
        .read::<String>()
        .chars()
        .map(|c| c as usize - '0' as usize)
        .collect::<Vec<_>>();
    let k: usize = sc.read();

    let mut dp: Vec<Vec<u64>> = vec![vec![0; k + 1]; 4];
    dp[0][0] = 1;

    let n = s.len();
    for i in 0..n {
        let mut next = vec![vec![0; k + 1]; 4];
        for mask in 0..4 {
            let is_started = mask & 1 != 0;
            let is_lower = mask & 2 != 0;
            for prev_k in 0..(k + 1) {
                for next_d in 0..10 {
                    if !is_lower && next_d > s[i] {
                        continue;
                    }

                    let next_started = is_started || next_d > 0;
                    let next_lower = is_lower || next_d < s[i];
                    let next_mask =
                        if next_lower { 2 } else { 0 } + if next_started { 1 } else { 0 };
                    let next_k = if next_d != 0 { prev_k + 1 } else { prev_k };
                    if next_k > k {
                        continue;
                    }
                    next[next_mask][next_k] += dp[mask][prev_k];
                }
            }
        }
        dp = next;
    }

    let ans = dp.into_iter().map(|dp| dp[k]).sum::<u64>();
    println!("{}", ans);
}

pub struct IO<R, W: std::io::Write>(R, std::io::BufWriter<W>);

impl<R: std::io::Read, W: std::io::Write> IO<R, W> {
    pub fn new(r: R, w: W) -> IO<R, W> {
        IO(r, std::io::BufWriter::new(w))
    }
    pub fn write<S: std::ops::Deref<Target = str>>(&mut self, s: S) {
        use std::io::Write;
        self.1.write(s.as_bytes()).unwrap();
    }
    pub fn read<T: std::str::FromStr>(&mut self) -> T {
        use std::io::Read;
        let buf = self
            .0
            .by_ref()
            .bytes()
            .map(|b| b.unwrap())
            .skip_while(|&b| b == b' ' || b == b'\n' || b == b'\r' || b == b'\t')
            .take_while(|&b| b != b' ' && b != b'\n' && b != b'\r' && b != b'\t')
            .collect::<Vec<_>>();
        unsafe { std::str::from_utf8_unchecked(&buf) }
            .parse()
            .ok()
            .expect("Parse error.")
    }
    pub fn vec<T: std::str::FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read()).collect()
    }
    pub fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}