Problem:
Score : 300 points

Problem Statement

Tak has N cards. On the i-th (1 \leq i \leq N) card is written an integer x_i.
He is selecting one or more cards from these N cards, so that the average of the integers written on the selected cards is exactly A.
In how many ways can he make his selection?

Constraints

1 \leq N \leq 50

1 \leq A \leq 50

1 \leq x_i \leq 50

N,\,A,\,x_i are integers.

Partial Score

200 points will be awarded for passing the test set satisfying 1 \leq N \leq 16.

Input

The input is given from Standard Input in the following format:

N A
x_1 x_2 ... x_N

Output

Print the number of ways to select cards such that the average of the written integers is exactly A.

Sample Input 1

4 8
7 9 8 9

Sample Output 1

5

The following are the 5 ways to select cards such that the average is 8:

Select the 3-rd card.

Select the 1-st and 2-nd cards.

Select the 1-st and 4-th cards.

Select the 1-st, 2-nd and 3-rd cards.

Select the 1-st, 3-rd and 4-th cards.

Sample Input 2

3 8
6 6 9

Sample Output 2

0

Sample Input 3

8 5
3 6 2 8 7 6 5 9

Sample Output 3

19

Sample Input 4

33 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3

Sample Output 4

8589934591

The answer may not fit into a 32-bit integer.

Solution in OCaml (4.02.3) (judge verdict: Runtime Error)

let rec loop x fn =
  match fn x with
  | `Break x -> x
  | `Continue x -> loop x fn

module List = struct
  include List
  let take n lst =
    loop (0, lst, []) begin fun (i, rest, r_acc) ->
      match i < n, rest with
      | true, []  | false, _ -> `Break (rev r_acc)
      | true, x :: rest -> `Continue (i + 1, rest, x :: r_acc) end
  let drop n lst =
    loop (0, lst) begin fun (i, rest) ->
      match i < n, rest with
      | true, _ :: rest -> `Continue (i + 1, rest)
      | true, []  | false, _ -> `Break rest end
end

let () =
  let open Scanf in
  let read_ints () = let open Buffer in
    let buf = create 10 in
    let add = add_bytes buf and get () = contents buf in
    loop () begin fun () ->
      try read_line () ^ " " |> add; `Continue ()
      with _ -> `Break (get ()) end
    |> Str.(split (regexp " ")) |> List.map int_of_string in

  let rest = read_ints () in
  let n :: rest = rest in
  let xs = List.take n rest in
  let l :: q :: qs = List.drop n rest in
  let qs =
    let qs = qs |> List.map (fun q -> q - 1) in
    loop (qs, []) begin fun (rest, r_acc) ->
      match rest with
      | x:: y:: rest -> `Continue (rest, (x, y):: r_acc)
      | []  | _:: [] -> `Break (List.rev r_acc) end in
  (* input stage is finally finished *)

  let xs = Array.of_list xs in
  let bisect fn_shift fn_idx = fun l r x ->
    loop (l, r) begin fun (l, r) ->
      if r - l - 1 = 0 then `Break (fn_idx l r)
      else let m = (l + r) / 2 in
        match fn_shift x xs.(m) with
        | `Left -> `Continue (l, m)
        | `Right -> `Continue (m, r) end in
  let bisect_l = bisect
      (fun x m -> if x < m then `Left else `Right) (fun l r -> l)
  and bisect_r = bisect
      (fun x m -> if x > m then `Right else `Left) (fun l r -> r) in

  let next = let open Hashtbl in
    let memo = create 10 in
    let cache = replace memo and get = find memo in
    let calc init fn_bound direction fn_step fn_shift fn_bisect =
      loop init begin fun here ->
        match fn_bound here with
        | false ->
          cache (here, direction) (fn_step here); `Break ()
        | true ->
          let x' = fn_shift xs.(here) l in
          let next = fn_bisect here x' in
          cache (here, direction) next;
          `Continue (fn_step here) end in
    calc 0 (fun x -> x < n - 1) `Right succ ( + )
      (fun here x -> bisect_l here n x);
    calc (n - 1) (fun x -> x > 0) `Left pred ( - )
      (fun here x -> bisect_r (-1) here x);
    (* let fold init fn t = fold fn t init in *)
    (* memo |> fold () begin fun k v () -> *)
    (*   match k with *)
    (*   | here, `Left -> *)
    (*     Printf.printf "`L, %d = %d;" (succ here) (succ v) *)
    (*   | here, `Right -> *)
    (*     Printf.printf "`R, %d = %d;" (succ here) (succ v) end; *)
    get in
  
  let add, get = let open Buffer in
    let buf = create (q * 2) in
    let add = add_string buf and get () = contents buf in
    add, get in

  let solve (a, b) =
    let reached, next =
      if a < b
      then (fun x -> b <= x), (fun x -> next (x, `Right))
      else (fun x -> b >= x), (fun x -> next (x, `Left)) in
    loop (a, 0) begin fun (a, cnt) ->
      let a' = next a in
      (* add (Printf.sprintf "(%d,%d), " (a + 1) (a' + 1)); *)
      if reached a' then `Break (cnt + 1)
      else `Continue (a', cnt + 1) end in

  qs |> List.map begin fun q ->
    let open Printf in
    (* printf "q:(%d,%d)" (fst q) (snd q); flush_all (); *)
    add (sprintf "%d\n" (solve q));
  end |> ignore;
  Printf.printf "%s" (get ());