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: Runtime Error)

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 50;
const int LOG = 20;

int A[MAXN];
vector< int >B;
int n, k, s, t;
int l;

void filter() {
    for (int i = 0; i < n; i++) {
        bool okay = true;
        int x = 0;
        for (int j = 0, l = 0; j < LOG; j++) {
            int _i = (A[i]>>j)&1;
            int _s = (s>>j)&1;
            int _t = (t>>j)&1;

            if (_s==1 && _i==0) {
                okay = false;
                break;
            }
            if (_t==0 && _i==1) {
                okay = false;
                break;
            }

            if (_s == 0 && _t == 1) {
                x |= _i<<l;
                l++;
            }
        }

        if (okay) {
//            cout << "take " << i << " as " << x << endl;
            B.push_back(x);
        }
    }
}

int p3[LOG+1];
//int getIndex(const vector< int >&w) {
//    int ans = 0;
//    for (int i = w.size()-1; i >= 0; i--) {
//        ans = ans*3+w[i];
//    }
//    return ans;
//}

//int replaceIndex(vector< int >&w, int idx, int p, int r) {
//    return idx+(r-w[p])*p3[p];
//}

int target;
LL dp[MAXN+1][MAXN+1][20000];

LL solve(int i, int taken, vector< int >w, int idx) {
    if (taken == k) return idx == target;
    if (i == B.size()) return idx == target;
    LL &ans = dp[i][taken][idx];
    if (ans != -1) return ans;
    ans = solve(i+1, taken, w, idx);

    int newidx = idx;
    for (int j = 0; j < l; j++) {
        if (B[i]&(1<<j)) {
            if (w[j]==0) {
                w[j] = 2;
                newidx += 2*p3[j];
            }
        } else {
            if (w[j]==1) {
                w[j] = 2;
                newidx += p3[j];
            }
        }
    }

    ans += solve(i+1, taken+1, w, newidx);

    return ans;
}

void calcVariable() {
    target = 0;
    l = 0;
    for (int j = 0; j < LOG; j++) {
        int _s = (s>>j)&1;
        int _t = (t>>j)&1;
        if (_s == 0 && _t == 1) {
            target += 2*p3[l];
            l++;
        }
    }
}

int main() {
    p3[0] = 1;
    for (int i = 1; i <= LOG; i++) {
        p3[i] = p3[i-1]*3;
    }

    cin >> n >> k >> s >> t;
    for (int i = 0; i < n; i++) cin >> A[i];

    calcVariable();
    if (l > 9)
    {
        int msk = (1<<LOG)-1;
        for (int i = 0; i < n; i++) A[i] ^= msk;
        s ^= msk, t ^= msk;
        swap(s, t);
        calcVariable();
        assert(l <= 9);
    }

    filter();

    if (B.empty()) {
        cout << 0 << endl;
        return 0;
    }

    int m = B.size();
    memset(dp, -1, sizeof dp);

    LL ans = 0;
    for (int i = 0; i < m; i++) {
        vector< int >w(l, 0);
        int idx = 0;
        for (int j = 0; j < l; j++) {
            w[j] = (B[i]>>j)&1;
            idx += w[j]*p3[j];
        }
        ans += solve(i+1, 1, w, idx);
    }

    cout << ans << endl;


    return 0;
}