Problem:
Score : 200 points

Problem Statement

Find the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Constraints

1 \leq N \leq 10^4

1 \leq A \leq B \leq 36

All input values are integers.

Input

Input is given from Standard Input in the following format:

N A B

Output

Print the sum of the integers between 1 and N (inclusive), whose sum of digits written in base 10 is between A and B (inclusive).

Sample Input 1

20 2 5

Sample Output 1

84

Among the integers not greater than 20, the ones whose sums of digits are between 2 and 5, are: 2,3,4,5,11,12,13,14 and 20. We should print the sum of these, 84.

Sample Input 2

10 1 2

Sample Output 2

13

Sample Input 3

100 4 16

Sample Output 3

4554

Solution in OCaml (4.02.3) (judge verdict: Accepted)

let fold_int f a n =
  let rec fold f a i n =
    if i >= n then
      a
    else
      fold f (f a i) (i + 1) n
  in
    fold f a 0 n

let int_of_digit c = int_of_char c - 48 (* 48 = int_of_char '0' *)

(* O(log(n)) *)
let sum_digits n =
  let rec loop sofar m =
    if m <= 0 then
      sofar
    else
      loop (sofar + m mod 10) (m / 10)
  in
    loop 0 n

(* O(n * log(n)) *)
let solve n a b =
  fold_int
    (fun sum i ->
      let i = i + 1 in
      let d = sum_digits i in
      sum + if a <= d && d <= b then i else 0)
    0
    n

let read_ints () = read_line () |> Str.split (Str.regexp " ") |> List.map int_of_string

let _ =
  match read_ints () with
  | [n; a; b] -> solve n a b |> string_of_int |> print_endline
  | _ -> ()