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>
#include <iostream>
#include <ctime>
using namespace std;
#define _LL long long

int* mallocint(int si)
{
    return (int*) malloc(si * sizeof(int));
}

int* inputint(int n)
{
    int *ans = mallocint(n);
    for( int i = 0; i < n; i++ ) cin >> ans[i];
    return ans;
}

char *getbits(_LL a)
{
        char *ans = new char[18];
        for( _LL bit = 0; bit < 18; bit++ )
        {
            ans[bit] = (a & ( (_LL)1 << bit )) ? 1 : 0;
        }    
        return  ans;
}

typedef struct 
{
    char total;
    char c[50];
    bool flag[50];
} BGROUP;


bool comp(BGROUP *p1, BGROUP *p2)
{
    return p1->total < p2->total;
}

int _heaplist[50][50];

_LL countpattern(vector<BGROUP*> groups, int n, int* uselist, int depth, int num, int k)
{
    _LL ans = 0;

    if( k < 0 ) 
    {
        for( int l = 0; l < groups.size(); l++ )
        {
            if( groups[l]->flag[depth - 1] ) return 0;
        }
        return 1;
    }

    for( int i = 0; i < groups.size(); i++ )
    {
        if( depth == 0 || groups[i]->flag[depth - 1] )
        {
            int count = 0;
            for(int j = 0; j < n; j++ )
            {
                if( uselist[j] == 0 && groups[i]->c[j] > 0 )
                {
                    _heaplist[depth][count++] = j;
                    uselist[j] = 1;
                    bool flag = false;
                    {
                        for( int l = 0; l < groups.size(); l++ )
                        {
                            groups[l]->flag[depth] = (( depth == 0 || groups[l]->flag[depth - 1] ) && groups[l]->c[j] == 0);
                        }    
                    }
                    ans += countpattern(groups, n, uselist, depth + 1, num, k - 1);
                    uselist[j] = -1;
                }
            }
            for( int j = 0; j < count; j++ ) uselist[_heaplist[depth][j]] = 0;
            return ans;
        }
    }

    ans = 1;
    int left = 0;
    for(int j = 0; j < n; j++ )
    {
        if( uselist[j] == 0 ) left++;
    }

    _LL tmp = 1;

    for( int i = 0; i < k; i++ )
    {
        tmp *= (left - i);
        tmp /= (i + 1);
        ans += tmp;
    }
    return ans ;
}




int main()
{
    _LL n, k ,s, t;
    cin >> n >> k >> s >> t;
    char **bits = (char**) malloc( sizeof(char*) * n);
    for( _LL i = 0; i < n; i++ )
    {
        _LL a;
        cin >> a;
        bits[i] = getbits(a);
    }
    char *sbit = getbits(s);
    char *tbit = getbits(t);

    bool usable[50];
    bool staybit[18];

    vector<BGROUP*> groups;
    BGROUP *glist = (BGROUP*) malloc( sizeof(BGROUP) * 18 * 2 );
    for( int i = 0; i < n; i++ ) usable[i] = true;

    for( int i = 0; i < 18; i++ )
    {
        if( sbit[i] == 1 && tbit[i] == 0 )
        {
            cout << 0;
            return 0;
        }
        if( sbit[i] == 1 && tbit[i] == 1 )
        {
            staybit[i] = true;
            for( int j = 0; j < n; j++ )
            {
                if( bits[j][i] != 1 ) usable[j] = false;
            }
        }
        else if( sbit[i] == 0 && tbit[i] == 0 )
        {
            staybit[i] = true;
            for( int j = 0; j < n; j++ )
            {
                if( bits[j][i] != 0 ) usable[j] = false;
            }
        }
        else
        {
            staybit[i] = false;
        }
    }

    for( int i = 0; i < 18; i++ )
    {
        if( !staybit[i] )
        {
            BGROUP *g1 = glist + i * 2;
            BGROUP *g2 = glist + i * 2 + 1;
            g1->total = 0;
            g2->total = 0;

            for( int j = 0; j < n; j++ ) 
            {
                g1->c[j] = 0;
                g2->c[j] = 0;
                if( usable[j] )
                {
                    if( bits[j][i] == 0 )
                    {
                        g1->total++;
                        g1->c[j] = 1;
                    }
                    else
                    {
                        g2->total++;
                        g2->c[j] = 1;
                    }
                    
                }
            }
            groups.push_back(g1);
            groups.push_back(g2);
        }
    }

    sort( groups.begin(), groups.end(), comp );

    int uselist[50];
    int list[36];
    int num = 0;
    for( int j = 0; j < n; j++ ) 
    {
        if( usable[j] ) 
        {
            uselist[j] = 0;
            num++;
        }
        else
        {
            uselist[j] = -1;
        }
    }
    cout << countpattern(groups, n, uselist, 0, num, k);

    return 0;    
}