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: Accepted)

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

ll comb[53][53];
ll combsum[53][53];
void make_table(){
  for(ll i=0;i<=51;i++){
    comb[i][0]=1;
    comb[i][i]=1;
  }
  for(ll i=1;i<=51;i++)for(ll j=1;j<i;j++){
    comb[i][j]=(comb[i-1][j-1]+comb[i-1][j]);
  }
}

ll cnt0[(1<<18)];
ll cnt1[(1<<18)];
ll pcnt[(1<<18)],sgn[(1<<18)];

inline ll bitv(ll a, ll n){
  return (a>>n)&1LL;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  make_table();
  rep(i,52){
    combsum[i][0]=comb[i][0];
    rep(j,51){
      combsum[i][j+1]=combsum[i][j]+comb[i][j+1];
    }
  }

  const ll B=18;
  rep(i,1<<B){
    pcnt[i]=bcnt(i);
    if(pcnt[i]%2==1) sgn[i]=-1;
    else sgn[i]=+1;
  }

  ll N,K,S,T;
  cin>>N>>K>>S>>T;
  vector<ll> A(N);
  rep(i,N)cin>>A[i];
  vector<ll> tmp;
  ll mask=(1<<B)-1;
  rep(j,B){
    if(bitv(S,j)==1&&bitv(T,j)==0){
      cout<<0<<endl;
      return 0;
    }
  }
  rep(j,B){
    if(bitv(S,j)==1||bitv(T,j)==0){
      mask^=(1LL<<j);
    }
  }
  //dbg(mask);
  rep(i,N){
    bool ok=true;
    rep(j,B){
      if(bitv(S,j)==1&&bitv(A[i],j)==0){
        ok=false;
      }
      if(bitv(T,j)==0&&bitv(A[i],j)==1){
        ok=false;
      }
    }
    if(ok){
      tmp.push_back(A[i]);
    }
  }
  A=tmp;
  N=A.size();
  //dbg(N);

  rep(i,N)A[i]=(A[i]&mask);

  rep(S0,1<<B){
    if((S0|mask)!=mask)continue;
    rep(i,N){
      ll ax=A[i]^mask;
      if((S0|ax)==ax)cnt0[S0]|=(1LL<<i);
    }
  }

  rep(S1,1<<B){
    if((S1|mask)!=mask)continue;
    rep(i,N){
      if((S1|A[i])==A[i])cnt1[S1]|=(1LL<<i);
    }
  }

  ll res=0;
  rep(U,1<<B){
    if((U|mask)!=mask)continue;
    for(ll U1=U;U1>=0;U1--){
      U1&=U;

      //dbg(U);
      ll U2=U^U1;
      //dbg(U1); dbg(U2);
      //ll cpcnt=pcnt[cnt0[U1]&cnt1[U2]];
      ll cpcnt=bcnt(cnt0[U1]&cnt1[U2]);
      //dbg(cnt0[U1]); dbg(cnt1[U2]);
      ll add=combsum[cpcnt][K]-1;
      //dbg(cpcnt);
      //dbg(add);
      res+=add*sgn[U];
    }
  }
  cout<<res<<endl;

  return 0;
}