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 C (GCC 5.4.1) (judge verdict: Accepted)

#include<stdio.h>
#include<stdlib.h>

#define NEW(p,n){p=malloc((n)*sizeof(p[0]));if(p==NULL){printf("not enough memory\n");exit(1);};}
//pの型の変数n個の要素分のメモリを確保し、そのアドレスをpに代入するマクロ

#define INFTY 999999

typedef long* vector;
typedef vector* matrix;
typedef matrix* tmatrix;

//0-c-1の添字のものを作る(初期値0)
vector create_vector(int c){
    vector v;
    NEW(v,c);
    for(int i=0;i<c;i++){
        v[i]=0;
    }
    return v;
}

//0-b-1,0-c-1の添字のものを作る
matrix create_matrix(int b, int c){
    matrix M;
    NEW(M,b);
    for(int i=0;i<b;i++){
        M[i]=create_vector(c);
    }
    return M;
}

//0-a-1,0-b-1,0-c-1の添字のものを作る
tmatrix create_tmatrix(int a, int b, int c){
    tmatrix T;
    NEW(T,a);
    for(int i=0;i<a;i++){
        T[i]=create_matrix(b,c);
    }
    return T;
}

//サイズcのベクトルのメモリ解法
void free_vector(vector v, int c){
    free(v);
    return;
}

//サイズb,cのmatrixのメモリ解法
void free_matrix(matrix M, int b, int c){
    for(int i=0;i<b;i++){
        free_vector(M[i],c);
    }
    free(M);
    return;
}

//サイズa,b,cのtmatrixのメモリ解法
void free_tmatrix(tmatrix T, int a, int b, int c){
    for(int i=0;i<a;i++){
        free_matrix(T[i],b,c);
    }
    free(T);
    return;
}


int main(void){
    int n,a;
    scanf("%d %d",&n,&a);

    vector x=create_vector(n+1);
    for(int i=1;i<=n;i++){
        scanf("%ld", &x[i]);
    }

    //T[a][b][c]=xの先頭a個の中から、b枚選んで合計cにする組み合わせ総数
    //0~a~n-1,1~b~n,0~c~na
    tmatrix T=create_tmatrix(n+1,n+1,n*50+1);
    //long T[n+1][n+1][n*a+1];
    T[0][0][0]=1;
    for(int i=1;i<=n;i++){
        for(int j=0;j<=n;j++){
            for(int k=0;k<=n*50;k++){
                if(x[i]>k){
                    T[i][j][k]=T[i-1][j][k];
                }
                else{
                    if((k-x[i])>=0&&(j-1)>=0){
                        T[i][j][k]=T[i-1][j][k]+T[i-1][j-1][k-x[i]];
                    }
                }
            }
        }
    }

    long ans=0;
    for(int i=1;i<=n;i++){
        ans+=T[n][i][i*a];
    }

    printf("%ld\n",ans);
    
    return 0;
    
}