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++ (Clang 10.0.0) (judge verdict: Wrong Answer)

#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
using namespace std;
typedef long long ll;
int n,K,S,T;
#define Maxn (1<<18)
int bit[Maxn];
int seq[18][3][2],L[18];
int ha[18];
int lowbit(int x){return x&(-x);}

map<pair<int,int>,int> mp;
int ans[15000010],cnt=0;
int seq1[15000010],seq2[15000010];
void dfs(int at,int sta1,int sta2){
	if(at==18){
		int d=mp[make_pair(sta1,sta2)];
		if(!mp[make_pair(sta1,sta2)]){
		    d=mp[make_pair(sta1,sta2)]=++cnt;
		    seq1[d]=sta1;seq2[d]=sta2;
		}
		ans[d]++;
		return;
	}
	for(int i=0;i<L[at];++i)
		dfs(at+1,sta1+seq[at][i][0]*(1<<at),sta2+seq[at][i][1]*(1<<at));
}

ll c[52][52];

int main(){
	ha[0]=1;
	for(int i=1;i<18;++i)ha[i]=ha[i-1]*3;
	for(int i=1;i<(1<<18);++i)bit[i]=bit[i-lowbit(i)]+1;
	scanf("%d%d",&n,&K,&S,&T);
	c[0][0]=1;
	for(int i=1;i<=n;++i){
		c[i][0]=1;
		for(int j=1;j<=i;++j){
			c[i][j]=c[i-1][j-1]+c[i-1][j];
		}
	}
	if((S&T)!=S){
		puts("0");
		return 0;
	}
	for(int i=0;i<18;++i)
	if(S&(1<<i)&&T&(1<<i)){
		L[i]=1;
		seq[i][0][0]=1;seq[i][0][1]=1;
	}else if((!(S&(1<<i)))&&(!(T&(1<<i)))){
		L[i]=1;
		seq[i][0][0]=0;seq[i][0][1]=0;
	}else{
		L[i]=3;
		L[i]=3;
		seq[i][0][0]=0;seq[i][0][1]=0;
		seq[i][1][0]=0;seq[i][1][1]=1;
		seq[i][2][0]=1;seq[i][2][1]=1;
	}
	int A;
	for(int i=1;i<=n;++i){
		scanf("%d",&A);
		for(int i=0;i<18;++i)
		    if(A&(1<<i)){
		    	L[i]=2;
		    	seq[i][0][0]=0;seq[i][0][1]=1;
		    	seq[i][1][0]=1;seq[i][1][1]=1;
			}else{
				L[i]=2;
				seq[i][0][0]=0;seq[i][0][1]=0;
				seq[i][1][0]=0;seq[i][1][1]=1;
			}
		dfs(0,0,0);
	}
	ll Ans=0;
	for(int i=1;i<=cnt;++i)
	    if(((S&seq1[i])==S)&&((seq2[i]&T)==seq2[i])){
	    	int at=bit[seq1[i]]-bit[S]+bit[T]-bit[seq2[i]];
	    	if(at&1){
	    		for(int j=1;j<=K;++j){
				    Ans-=c[ans[i]][j];
				}
			}else{
				for(int j=1;j<=K;++j){
					Ans+=c[ans[i]][j];
				}
			}
		}
	printf("%lld\n",Ans);
	return 0;
}