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 Rust (1.15.1) (judge verdict: Time Limit Exceeded)

use std::str::FromStr;
use std::io::*;
use std::collections::*;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = AtRead::from(i);
    let (n, a) = i.read2::<usize, usize>();
    let x = i.read_vec::<usize>(n);

    writeln!(o, "{}", solve(a, &x)).unwrap();
}
fn solve(a: usize, x: &[usize]) -> usize {
    let lower: Vec<_> = x.iter().filter(|&&x| x < a).map(|&x| a - x).collect();
    let upper: Vec<_> = x.iter().filter(|&&x| x > a).map(|&x| x - a).collect();
    let mids = x.iter().filter(|&&x| x == a).count();
    let mut result: usize = 0;

    let mut comb_lower = HashMap::<usize, usize>::new();
    for_each_comb(lower.len(), |idx| {
        let sum = idx.iter().map(|&i| lower[i]).sum();
        *comb_lower.entry(sum).or_insert(0) += 1;
    });
    for_each_comb(upper.len(), |idx| {
        let sum = idx.iter().map(|&i| upper[i]).sum();
        result += comb_lower.get(&sum).cloned().unwrap_or(0);
    });
    result * (2 as usize).pow(mids as u32) - 1
}
fn for_each_comb<F: FnMut(&[usize])>(n: usize, mut f: F) {
    let mut idx = Vec::new();
    f(&idx);
    for _ in 0..n {
        idx.push(0);
        for i in 0..idx.len() {
            idx[i] = i;
        }
        let c = idx.len();
        'outer: loop {
            f(&idx);
            for j in 0.. {
                if j == c {
                    break 'outer;
                }
                if idx[c - j - 1] < n - 1 - j {
                    idx[c - j - 1] += 1;
                    for k in 0..j {
                        idx[c - j + k] = idx[c - j + k - 1] + 1;
                    }
                    break;
                }
            }
        }
    }
}

mod utils {
    use super::*;

    pub struct AtRead<R: BufRead> {
        r: R,
        s: String,
    }

    macro_rules! fn_read {
        {$f:ident($($v:ident: $t:ident),*)} => {
            pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.read_line().split(' ');
                $(
                    let $v = next(i);
                )*
                ($($v),*)
            }
        };
    }

    impl<R: BufRead> AtRead<R> {
        pub fn from(r: R) -> Self {
            AtRead {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        fn_read! { read2(v1: T1, v2: T2) }

        pub fn read_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            self.read_line()
                .split(' ')
                .take(n)
                .map(|x| x.parse().ok().unwrap())
                .collect()
        }
    }

    fn next<'a, T: FromStr, I: std::iter::Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

}