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: Wrong Answer)

fn keta(x: u64) -> u64 {
    let mut y = x;
    let mut keta = 1;
    while y >= 10 {
        keta += 1;
        y /= 10;
    }
    keta
}

fn next_num(n: &String, x: usize) -> (u64, u64) {
    let keta = n.len();

    for i in x..keta {
        let a = n[i..i + 1].parse::<u64>().unwrap();
        if a != 0 {
            return (a, i as u64);
        }
    }
    (0, 0)
}

fn main() {
    let n = read::<String>();
    let k = read::<u64>();
    let keta = n.len() as u64;
    let n_first = n[0..1].parse::<u64>().unwrap();

    let mut ans = 0;
    if k == 1 {
        ans = 9 * (keta - 1) + n_first;
    } else if k == 2 {
        let (n_second, i2) = next_num(&n, 1);

        ans += 9 * 9 * ((keta - 2) * (keta - 2 + 1) / 2); // keta-1桁までの数
        ans += (n_first - 1) * 9 * (keta - 1); // keta桁の先頭1つ目の数字より1つ少ないケース
        if n_second > 0 {
            ans += 9 * (keta - i2 - 1); // 先頭1つ目が上限値と同じケース、かつ二桁めが1少ないケース
            ans += n_second;
        }
    } else if k == 3 {
        let (n_second, i2) = next_num(&n, 1);
        let (n_third, i3) = next_num(&n, (i2 + 1) as usize);

        for i in 2..keta - 1 {
            ans += 9 * 9 * 9 * i * (i - 1) / 2;
        }
        ans += (n_first - 1) * 9 * 9 * (keta - 1) * (keta - 2) / 2;
        if n_second > 0 {
            // 先頭1桁が固定のケース
            ans += (n_second - 1) * 9 * (keta - i2 - 1);
        }
        if n_third > 0 {
            // 先頭2桁までが固定のケース
            ans += 9 * (keta - i3 - 1);
            ans += n_third;
        }
    }
    println!("{}", ans);
}

#[allow(dead_code)]
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()
}
#[allow(dead_code)]
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}
#[allow(dead_code)]
fn read_vec2<T: std::str::FromStr>(n: u32) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}
#[allow(dead_code)]
fn yn(result: bool) {
    if result {
        println!("Yes");
    } else {
        println!("No");
    }
}