Language: C++

#pragma GCC optimize("Ofast,unroll-loops,no-stack-protector,fast-math,inline")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,lzcnt,mmx,abm,avx,avx2,tune=native")

#include <algorithm>
#include <array>
#include <cassert>
#include <iostream>
#include <vector>
using namespace std;

template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }

void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }

#ifdef NEAL_DEBUG
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif


const int BITS = 18;

int N, K, S, T;
int64_t ALL;
vector<int> A;
array<array<int64_t, 2>, BITS> fail;
vector<vector<int64_t>> choose;
vector<int64_t> up_to_K;
int64_t answer;

void recurse(int index, int sign, int64_t mask) {
    if (mask == 0)
        return;

    if (index == BITS) {
        answer += sign * up_to_K[__builtin_popcountll(mask)];
        return;
    }

    recurse(index + 1, sign, mask);
    recurse(index + 1, -sign, mask & fail[index][0]);
    recurse(index + 1, -sign, mask & fail[index][1]);
}

int main() {
    cin >> N >> K >> S >> T;

    if ((S | T) != T) {
        cout << 0 << '\n';
        return 0;
    }

    for (int i = 0; i < N; i++) {
        int a;
        cin >> a;

        if ((a & S) != S || (a | T) != T)
            continue;

        A.push_back(a);
    }

    N = int(A.size());
    K = min(K, N);

    choose.assign(N + 1, vector<int64_t>(N + 1, 0));

    for (int i = 1; i <= N; i++) {
        choose[i][0] = choose[i][i] = 1;

        for (int j = 1; j < i; j++)
            choose[i][j] = choose[i - 1][j] + choose[i - 1][j - 1];
    }

    up_to_K.assign(N + 1, 0);

    for (int i = 1; i <= N; i++)
        for (int j = 1; j <= K; j++)
            up_to_K[i] += choose[i][j];

    ALL = (1LL << N) - 1;

    for (int bit = 0; bit < BITS; bit++) {
        fail[bit] = {ALL, ALL};

        if ((S >> bit & 1) == 0) {
            fail[bit][0] = 0;

            for (int i = 0; i < N; i++)
                if (A[i] >> bit & 1)
                    fail[bit][0] |= 1LL << i;

            if (fail[bit][0] == 0)
                fail[bit][0] = ALL;
        }

        if ((T >> bit & 1) == 1) {
            fail[bit][1] = 0;

            for (int i = 0; i < N; i++)
                if ((A[i] >> bit & 1) == 0)
                    fail[bit][1] |= 1LL << i;

            if (fail[bit][1] == 0)
                fail[bit][1] = ALL;
        }
    }

    dbg(fail);

    answer = 0;
    recurse(0, 1, ALL);
    cout << answer << '\n';
}


Compiler output:
In file included from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bits/locale_classes.h:40,
                 from /usr/include/c++/14/bits/ios_base.h:41,
                 from /usr/include/c++/14/ios:44,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:7:
/usr/include/c++/14/bits/allocator.h: In destructor 'std::_Vector_base<int, std::allocator<int> >::_Vector_impl::~_Vector_impl()':
/usr/include/c++/14/bits/allocator.h:182:7: error: inlining failed in call to 'always_inline' 'std::allocator< <template-parameter-1-1> >::~allocator() noexcept [with _Tp = int]': target specific option mismatch
  182 |       ~allocator() _GLIBCXX_NOTHROW { }
      |       ^
In file included from /usr/include/c++/14/vector:66,
                 from a.cc:8:
/usr/include/c++/14/bits/stl_vector.h:132:14: note: called from here
  132 |       struct _Vector_impl
      |              ^~~~~~~~~~~~