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 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 k2(n: &String, n_first: u64) -> u64 {
    let mut ans = 0;
    let (n_second, i2) = next_num(n, 1);
    let keta = n.len() as u64;

    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;
    }
    ans
}

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 {
        ans += k2(&n, n_first);
    } else if k == 3 {
        let (n_second, i2) = next_num(&n, 1);

        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桁が固定(n_first)のケース
            ans += k2(&n[i2 as usize..].to_string(), n_second);
        }
    }
    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");
    }
}