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 OCaml (4.02.3) (judge verdict: Runtime Error)

open Batteries
open BatPrintf
open BatBigarray

let scan fmt f =
  BatScanf.sscanf (read_line ()) fmt f

let scan_list cnv =
  read_line ()
  |> BatString.split_on_char ' '
  |> List.map cnv

let rec zip xs ys =
  match xs, ys with
  | [], _ -> []
  | _, [] -> []
  | x::xs, y::ys ->
    (x, y) :: zip xs ys

let n = scan "%s" identity
let k = scan "%d" identity

let () =
  let len = String.length n in
  let dp = Array3.create Int C_layout 2 (len+1) len in
  Array3.set dp 0 0 0 1;
  let open BatEnum in
  iter (fun i ->
      let d = int_of_string @@ String.of_char @@ String.get n (i-1) in
      iter (fun j ->
          Array3.set dp 1 i j @@ (Array3.get dp 1 (i - 1) j) * 9 +
                                 (if j > 0 then
                                    Array3.get dp 1 (i - 1) (j - 1)
                                  else 0) +
                                 if d > 0 then
                                   (d - 1) * (Array3.get dp 0 (i - 1) j) + (if j > 0 then Array3.get dp 0 (i - 1) (j - 1) else 0)
                                 else
                                   0;
          Array3.set dp 0 i j @@
          if d > 0 then
            Array3.get dp 0 (i-1) j
          else if j > 0 then
            Array3.get dp 0 (i-1) (j-1)
          else
            0
        ) (0 --^ len);
    ) (1 -- len);
  printf "%d\n" @@ Array3.get dp 1 len (len - k) + Array3.get dp 0 len (len - k)