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.42.0) (judge verdict: Accepted)

#![allow(unused_imports)]
use std::io;
use std::cmp;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::f64::consts::PI;
use std::str::FromStr;

type Error = Box<dyn std::error::Error>;

fn read_line() -> String {
    let mut buf = String::new();
    io::stdin().read_line(&mut buf).ok();
    buf
}

struct Scanner {
    buff: Vec<String>,
    pos: usize,
}


#[allow(dead_code)]
impl Scanner {
    pub fn new() -> Self {
        Scanner {
            buff: Vec::new(),
            pos: 0,
        }
    }

    pub fn next<T: FromStr>(&mut self) -> T {
        while self.buff.len() <= self.pos {
            self.pos = 0;
            self.buff = read_line().split_whitespace().map(|s| s.to_owned()).collect();
        }

        let result = self.buff[self.pos].parse::<T>();
        self.pos += 1;
        result.ok().unwrap()
    }

    pub fn next_line(&mut self) -> String {
        let result  = read_line().trim().to_owned();
        self.buff.clear();
        self.pos = 0;

        result
    }

    pub fn next_vec<T: FromStr>(&mut self) -> Vec<T> {
        self.buff.clear();
        self.pos = 0;
        read_line().trim().split_whitespace().map(|s| s.parse().ok().unwrap()).collect()
    }
    
    pub fn next_line_chars(&mut self) -> Vec<char> {
        let result  = read_line().trim().chars().collect();
        self.buff.clear();
        self.pos = 0;

        result
    }
}

fn func(n: &Vec<char>, idx: usize, k: usize) -> u64 {
    if k==0 {
        return 1;
    }
    if idx >= n.len() {
        return 0;
    }

    let m = n.len();
    let p = (n[idx] as u8 - '0' as u8) as u64;
    let mut tmp = 0;
    if p > 0 {
        tmp += dbg!((p-1) * func2(m-idx, k-1));
        tmp += func(n, idx+1, k-1);
        tmp += func2(m-idx, k);
    } else {
        tmp += func(n, idx+1, k);
    }
    
    dbg!(tmp)
}

fn func2(m: usize, k: usize) -> u64 {
    if m < k || m == 0 {
        return 0;
    }
    let mut tmp = 1;

    for _i in 0..k {
        tmp *= 9 as u64;
    }

    for i in 0..k {
        tmp *= (m-i-1) as u64;
    }
    for i in 0..k {
        tmp /= (i+1) as u64;
    }
    dbg!(tmp)
}

fn func3(n: &Vec<char>, k:usize) -> u64 {
    let mut ans = 0;
    let mut tmp = 0;
    for &c in n.iter() {
        tmp *= 10;
        tmp += (c as u8 - '0' as u8) as u64;
    }
    let n = tmp;
    for i in 0..n+1 {
        let mut tmp = i;
        let mut count = 0;
        while tmp > 0 {
            let p = tmp %10;
            if p > 0 {
                count += 1;
            }
            tmp /= 10;
        }
        if count == k {
            ans += 1;
        }
    }
    ans
}


fn main() {
    let mut sc = Scanner::new();

    let n: Vec<char> = sc.next_line_chars();
    let k: usize = sc.next();
    let mut m = n.len();

    let ans: u64 = func(&n, 0, k);
    // for i in 1..1000000 {
    //     for k in 1..4 {
    //         let n: Vec<char> = i.to_string().chars().collect();
    //         println!("check: {} {}", i, k);
    //         debug_assert_eq!(func(&n, 0, k), func3(&n, k));
    //     }
    // }


    println!("{}", ans);
}