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)




struct DFS {
    n: Vec<char>,
    k: usize,
}

impl DFS {
    fn new() -> DFS {
        let n = read::<String>().chars().collect();
        let k = read();
        DFS {
            n: n,
            k: k,
        }
    }

    fn dfs(&self, i: usize, buf0: Vec<u64>, buf1: Vec<u64>) -> u64{
        if i >= self.n.len() {
            buf0[self.k]+ buf1[self.k]
        } else {
            let ni = self.n[i].to_digit(10).expect("Failed to Convert char to digit") as u64;
            let mut next0 = vec![0; self.k + 1];
            let mut next1 = vec![0; self.k + 1];

            next0[0] = buf0[0] + if ni != 0 { buf1[0] } else { 0 };
            next1[0] = if ni != 0 { 0 } else { buf1[0] };
            for j in 1..(self.k + 1) {
                next0[j] = buf0[j-1] * 9 + buf0[j];
                if ni != 0 {
                    next0[j] += buf1[j] + buf1[j-1] * (ni - 1);
                }

                next1[j] = if ni > 0 { buf1[j-1] } else { buf1[j] };
            }

            self.dfs(i + 1, next0, next1)
        }
    }
}



fn main() {
    let d = DFS::new();    
    let mut buf1 = vec![0; d.k + 1];
    buf1[0] = 1;
    println!("{:?}", d.dfs(0, vec![0; d.k + 1], buf1));
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}