Problem:
Score : 800 points

Problem Statement

Given are N pairwise distinct non-negative integers A_1,A_2,\ldots,A_N.
Find the number of ways to choose a set of between 1 and K numbers (inclusive) from the given numbers so that the following two conditions are satisfied:

The bitwise AND of the chosen numbers is S.

The bitwise OR of the chosen numbers is T.

Constraints

1 \leq N \leq 50

1 \leq K \leq N

0 \leq A_i < 2^{18}

0 \leq S < 2^{18}

0 \leq T < 2^{18}

A_i \neq A_j (1 \leq i < j \leq N)

Input

Input is given from Standard Input in the following format:

N K S T
A_1 A_2 ... A_N

Output

Print the answer.

Sample Input 1

3 3 0 3
1 2 3

Sample Output 1

2

The conditions are satisfied when we choose \{1,2\} or \{1,2,3\}.

Sample Input 2

5 3 1 7
3 4 9 1 5

Sample Output 2

2

Sample Input 3

5 4 0 15
3 4 9 1 5

Sample Output 3

3

Solution in C++ (GCC 9.2.1) (judge verdict: Wrong Answer)

#include<bits/stdc++.h>
using namespace std;
 
#define MOD 1000000000000000002
template<typename ty1,typename ty2>
inline int add(ty1 x, ty2 y) {
	if(y>=MOD)y%=MOD;
	if(x>=MOD)x%=MOD;
    x += y; return x < MOD ? x : x - MOD;
}
template<typename ty1,typename ty2>
inline void addto(ty1 &x, ty2 y) {
		  if(y>=MOD)y%=MOD;
		  if(x>=MOD)x%=MOD;
	      x += y; if (x >= MOD) x -= MOD;
}
template<typename ty1,typename ty2>
inline int sub(ty1 x, ty2 y) {
		if(y>=MOD)y%=MOD;
		 if(x>=MOD)x%=MOD;
        x -= y; return x < 0 ? x + MOD : x;
}
template<typename ty1,typename ty2>
inline void subto(ty1 &x, ty2 y) {
	if(y>=MOD)y%=MOD;
	if(x>=MOD)x%=MOD;
    x -= y; if (x < 0) x += MOD;
}
template<typename ty1,typename ty2>
inline int mul(ty1 x, ty2 y) {
        if(y>=MOD)y%=MOD;
		if(x>=MOD)x%=MOD;
        return 1ll * x * y % MOD;
}
template<typename ty1,typename ty2>
void multo(ty1 &x, ty2 y) {
        if(y>=MOD)y%=MOD;
		if(x>=MOD)x%=MOD;
        x=1ll * x * y % MOD;
}
  
long long int gcd(long long int a, long long int b){
	if (a > b){
		swap(a, b);
	}
	while (a){
		swap(a, b);
		a %= b;
	}
	return b;
}
long long int lcm(long long int a, long long int b){
	return a / gcd(a, b)*b;
}
long long int ppow(long long int i, long long int j){
		long long int res = 1LL;
		while (j){
			if ((j & 1LL)){
				res *= i;
				if (res >= MOD){
					res %= MOD;
				}
			}
			j >>= 1;
			i *= i;
			if (i >= MOD){
				i %= MOD;
			}
		}
		return res;
	}
class Combination{
	
public:
	vector<long long int> k;
	vector<long long int> r;
	void resize(int N){
		k.resize(N + 2);
		r.resize(N + 2);
		k[0] = 1;
		for (int i = 1; i < N+2; i++){
			k[i] = k[i - 1];
			k[i] *= i;
			if (k[i] >= MOD)k[i] %= MOD;
		}
		long long int al = k[k.size() - 1];
		long long int iv = ppow(k[k.size() - 1],MOD-2);
		r[k.size() - 1] = iv;
		for (int i = (int)(r.size()) - 2; i >= 0; i--){
			r[i] = r[i + 1] * (i + 1);
			if (r[i] >= MOD){
				r[i] %= MOD;
			}
		}
	}
	long long int C(int a, int b){
		if (a < b)return 0;
		long long int up = k[a];
		long long int dw = k[b] * r[a - b];
		dw %= MOD;
		up *= dw;
		up %= MOD;
		return up;
	}
	long long int H(int a, int b){
		return C(a + b - 1, b);
	}
	long long int catalan_number(int n){
		return (C(2 * n, n) + MOD - C(2 * n, n - 1)) % MOD;
	}
};
Combination C;
int n;
int k;
int s;
int t;
int32_t v[52];
long long int choose[52];
long long int c[52][52];
int main(){
	c[0][0]=1;
	for(int i=0;i<51;i++){
		for(int j=0;j<=i;j++){
			c[i+1][j]+=c[i][j];
			c[i+1][j+1]+=c[i][j];
		}
	}
	C.resize(100);
	cin>>n>>k>>s>>t;
	for(long long int i=0;i<n;i++){
		long long int a;
		scanf("%lld",&a);
		v[i]=a;
	}
	long long int z=(1<<18)-1;
	z^=t;
	vector<long long int> ret;
	for(int i=0;i<n;i++){
		if(v[i]&z){
			continue;
		}
		if((v[i]&s)!=s){
			continue;
		}
		ret.push_back(v[i]);
	}
	if(ret.size()==0){
		puts("0");
		return 0;
	}
	for(long long int i=0;i<ret.size();i++){
		v[i]=ret[i];
	}
	n=ret.size();
	k=min(k,n);
	for(long long int i=1;i<=50;i++){
		for(long long int j=1;j<=i&&j<=k-1;j++){
			choose[i]+=c[i][j];
		}
		choose[i]++;
	}
	long long int ans=0;
	long long int nes=0;
	for(long long int i=0;i<18;i++){
		if((t>>i)&1){
			if((s>>i)&1){
				
			}
			else{
				nes^=(1<<i);
			}
		}
	}
	for(int i=0;i<(1<<18);i++){
		if((i&nes)!=i){
			continue;
		}
		int overall=0;
		for(int j=0;j<n;j++){
			long long int cnt=0;
			long long int filter=0;
			long long int fl=0;
			for(int k=0;k<18;k++){
				if((i>>k)&1){
					filter|=(1<<k);
					if(!((v[j]>>k)&1)){
						fl^=(1<<k);
					}
				}
			}
			for(int k=j+1;k<n;k++){
				if(((v[k]^fl)&filter)==filter){
					cnt++;
				}
			}
			overall+=choose[cnt];
		}
		bool flag=false;
		for(int k=0;k<18;k++){
			if((i>>k)&1){
				flag^=true;
			}
		}
		if(flag){
			ans-=overall;
		}
		else{
		ans+=overall;
	}
	}
	printf("%lld\n",ans);
	return 0;
	return 0;
}