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)

// This code is generated by [cargo-atcoder](https://github.com/tanakh/cargo-atcoder)
// Original source code:
/*
use lib_competition::prelude::*;

// DP 解
#[allow(dead_code)]
fn solve1(n: String, k: usize) -> u64 {
    if n.len() < k {
        return 0;
    }

    let mut dp = vec![vec![vec![0u64; 2]; 5]; n.len() + 1];
    // dp[i][m][a] = i 桁の数値で 1~9 が m-1 個含まれ，
    //               かつ n の上部 i 桁と{ if a == 1 一致する else 一致しない }数の総数

    dp[0][1][1] = 1;

    for (i, c) in n.char_indices() {
        let c = c.to_digit(10).unwrap() as u64;
        for m in 1..5 {
            dp[i + 1][m][0] = dp[i][m][0]
                + dp[i][m - 1][0] * 9
                + if 0 < c { dp[i][m][1] } else { 0 }
                + if 1 < c { dp[i][m - 1][1] * (c - 1) } else { 0 };
            dp[i + 1][m][1] = if c == 0 { dp[i][m][1] } else { dp[i][m - 1][1] };
        }
    }

    // for i in 0..2 {
    //     for j in 1..5 {
    //         for k in 0..(n.len() + 1) {
    //             print!("{:>5} ", dp[k][j][i]);
    //         }
    //         println!("");
    //     }
    //     println!("--");
    // }

    return dp[n.len()][k + 1][0] + dp[n.len()][k + 1][1];
}

// 逆向き DP 解
#[allow(dead_code)]
fn solve2(n: String, k: usize) -> u64 {
    if n.len() < k {
        return 0;
    }

    let mut dp = vec![vec![vec![0u64; 2]; 5]; n.len() + 1];
    // dp[i][m][a] = i 桁の数値で 1~9 が m 個含まれ，
    //               かつ n の上部 i 桁と{ if a == 1 一致する else 一致しない }数の総数

    dp[0][0][1] = 1;

    for (i, c) in n.char_indices() {
        let c = c.to_digit(10).unwrap() as u64;
        for m in 0..4 {
            dp[i + 1][m][0] += dp[i][m][0];
            dp[i + 1][m + 1][0] += dp[i][m][0] * 9;
            dp[i + 1][m + 1][0] += dp[i][m][1] * (std::cmp::max(c, 1) - 1);
            if c == 0 {
                dp[i + 1][m][1] += dp[i][m][1];
            } else {
                dp[i + 1][m + 1][1] += dp[i][m][1];
                dp[i + 1][m][0] += dp[i][m][1];
            };
        }
    }

    // for i in 0..2 {
    //     for j in 0..4 {
    //         for k in 0..(n.len() + 1) {
    //             print!("{:>5} ", dp[k][j][i]);
    //         }
    //         println!("");
    //     }
    //     println!("--");
    // }

    return dp[n.len()][k][0] + dp[n.len()][k][1];
}

// リファクタリングした DP 解
#[allow(dead_code)]
fn solve3(n: String, k: usize) -> u64 {
    if n.len() < k {
        return 0;
    }

    let mut dp = vec![vec![vec![0u64; 2]; 5]; n.len() + 1];
    // dp[i][m][a] = i 桁の数値で 1~9 が m 個含まれ，
    //               かつ n の上部 i 桁と{ if a == 1 一致する else 一致しない }数の総数

    dp[0][0][1] = 1;

    for (i, c) in n.char_indices() {
        let c = c.to_digit(10).unwrap() as u64;
        for m in 0..4 {
            for a in 0..2 {
                for cc in 0..(if a == 0 { 10 } else { c + 1 }) {
                    dp[i + 1][m + (cc > 0) as usize][(a == 1 && cc == c) as usize] += dp[i][m][a];
                }
            }
        }
    }

    // for i in 0..2 {
    //     for j in 0..4 {
    //         for k in 0..(n.len() + 1) {
    //             print!("{:>5} ", dp[k][j][i]);
    //         }
    //         println!("");
    //     }
    //     println!("--");
    // }

    return dp[n.len()][k][0] + dp[n.len()][k][1];
}


// メモ化再帰
#[allow(dead_code)]
fn solve4(n: String, k: usize) -> u64 {
    let mut cache = vec![vec![vec![-1i64; 2]; 5]; n.len() + 1];

    fn rec(n: String, k: usize, less: bool, mut cache: &mut Vec<Vec<Vec<i64>>>) -> u64 {
        if n.len() < k {
            return 0;
        }
        if k == 0 {
            return 1;
        }

        if cache[n.len()][k][less as usize] >= 0 {
            return cache[n.len()][k][less as usize] as u64;
        }

        let mut r = 0;
        let n_tail = n[1..].to_string();
        if less {
            r += rec(n_tail.clone(), k, true, &mut cache)
                + rec(n_tail.clone(), k - 1, true, &mut cache) * 9;
        } else {
            let c = n.chars().nth(0).unwrap().to_digit(10).unwrap() as i64;
            r += rec(n_tail.clone(), k, c != 0, &mut cache)
                + rec(n_tail.clone(), k - 1, true, &mut cache) * std::cmp::max(c - 1, 0) as u64
                + rec(n_tail.clone(), k - 1, false, &mut cache) * (c != 0) as u64;
        }

        cache[n.len()][k][less as usize] = r as i64;
        return r;
    };

    let r = rec(n, k, false, &mut cache);

    // for i in 0..2 {
    //     for j in 0..4 {
    //         for k in 0..(nl + 1) {
    //             print!("{:>5} ", cache[k][j][i]);
    //         }
    //         println!("");
    //     }
    //     println!("--");
    // }

    return r;
}

fn main() {
    input! {
        n: String,
        k: usize
    };

    println!("{}", solve4(n, k));
}
*/

fn main() {
    let exe = "/tmp/bin654B28D4";
    std::io::Write::write_all(&mut std::fs::File::create(exe).unwrap(), &decode(BIN)).unwrap();
    std::fs::set_permissions(exe, std::os::unix::fs::PermissionsExt::from_mode(0o755)).unwrap();
    std::process::exit(std::process::Command::new(exe).status().unwrap().code().unwrap())
}

fn decode(v: &str) -> Vec<u8> {
    let mut ret = vec![];
    let mut buf = 0;
    let mut tbl = vec![64; 256];
    for i in 0..64 { tbl[TBL[i] as usize] = i as u8; }
    for (i, c) in v.bytes().filter_map(|c| { let c = tbl[c as usize]; if c < 64 { Some(c) } else { None } }).enumerate() {
        match i % 4 {
            0 => buf = c << 2,
            1 => { ret.push(buf | c >> 4); buf = c << 4; }
            2 => { ret.push(buf | c >> 2); buf = c << 6; }
            3 => ret.push(buf | c),
            _ => unreachable!(),
        }
    }
    ret
}

const TBL: &'static [u8] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const BIN: &'static str = "<base64-encoded ELF executable removed>";