alexjercan/bugnet huggingface dataset:
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define N 109
int a[N], n, k, s, t, tot;
int vis[300009];
ll C[N][N], D[N][N];
int main() {
  cin >> n >> k >> s >> t;
  for (int i = 0; i <= n; i++)
    D[i][0] = C[i][0] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= i; j++) {
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
      D[i][j] = D[i][j - 1] + C[i][j];
    }
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &x);
    if ((x & s) == s && (x & t) == x)
      a[++tot] = x;
  }
  t ^= s;
  int cnt = 0;
  for (int j = 0; j < 18; j++)
    if (t >> j & 1)
      cnt++;
  // printf("%d %d\\n",cnt,tot);
  for (int i = 1; i <= tot; i++) {
    int val = 1, sum = 0;
    for (int j = 0; j < 18; j++) {
      if ((a[i] >> j & 1) && (t >> j & 1))
        sum += val;
      if (t >> j & 1)
        val *= 2;
    }
    a[i] = sum;
  }
  ll ans = 0;
  for (int i = 0; i < (1 << cnt); i++)
    vis[i] = 0;
  for (int t = 0; t < (1 << cnt); t++) {
    int s = 1;
    for (int j = 0; j < cnt; j++)
      if (t >> j & 1)
        s *= -1;
    ll sum = 0;
    for (int i = 1; i <= tot; i++) {
      sum -= D[vis[a[i] & t]][min(vis[a[i] & t], k)];
      vis[a[i] & t]++;
      sum += D[vis[a[i] & t]][min(vis[a[i] & t], k)];
    }
    for (int i = 1; i <= tot; i++)
      vis[a[i] & t]--;
    ans += s * sum;
  }
  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

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

int n, k, s, t, ans = 0, a[128], chk[128], hbi[128];

void dfs(int step, int num, int A, int O) {
  if ((s & A) != s)
    return;
  if ((t | O) != t)
    return;
  if (num > k)
    return;
  if (step == n && num <= k && A == s && O == t)
    ans++;
  if (step == n)
    return;
  dfs(step + 1, num, A, O);
  dfs(step + 1, num + 1, A & a[step], O | a[step]);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k >> s >> t;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  bool flg = 1;
  memset(chk, -1, sizeof chk);
  for (int i = 0; i < 18; i++) {
    int A = s >> i & 1, O = t >> i & 1;
    if (A == 0 && O == 0)
      chk[i] = 0;
    if (A == 1 && O == 1)
      chk[i] = 1;
    if (A == 1 && O == 0)
      flg = 0;
  }
  for (int i = 0; i < n; i++) {
    hbi[i] = 1;
    for (int j = 0; j < 18; j++) {
      if (chk[j] == -1)
        continue;
      if ((a[i] >> j & 1) != chk[j])
        hbi[i] = 0;
    }
  }
  vector<int> v;
  for (int i = 0; i < n; i++)
    if (hbi[i])
      v.push_back(a[i]);
  n = v.size();
  for (int i = 0; i < n; i++)
    a[i] = v[i];
  if (flg == 0) {
    cout << 0 << endl;
    return 0;
  }
  dfs(0, 0, (1 << 20) - 1, 0);
  cout << ans << endl;
  return 0;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using i64 = int64_t;
template <class T> using Vec = std::vector<T>;
template <class K, class V> using UM = std::unordered_map<K, V>;

using namespace std;

namespace std {
template <> struct hash<pair<int, int>> {
  size_t operator()(pair<int, int> p) const noexcept {
    return (size_t(p.first) << 18) | (size_t(p.second));
  }
};
} // namespace std

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n, k, s, t;
  cin >> n >> k >> s >> t;

  Vec<int> as(n);
  for (auto &a : as)
    cin >> a;

  if ((s & t) != s || (s | t) != t) {
    cout << 0;
    return 0;
  }

  auto invalid = [&](int a) { return s != (s & a) || t != (t | a); };
  as.erase(remove_if(as.begin(), as.end(), invalid), as.end());

  n = as.size();

  Vec<UM<pair<int, int>, i64>> by_andor(k + 19);
  by_andor[0][{t, s}] = 1;

  for (auto &lvl : by_andor)
    lvl.reserve(1 << 18);
  int aand, oor;

  for (int i = 0; i < as.size(); ++i) {
    int a = as[i];

    for (int j = min(i, k - 1); j >= 0; --j) {
      auto &next = by_andor[j + 1];

      for (auto &[key, value] : by_andor[j]) {
        tie(aand, oor) = key;
        next[{aand & a, oor | a}] += value;
      }
    }
  }

  i64 ans = 0;
  for (int i = 0; i <= k; ++i)
    ans += by_andor[i][{s, t}];
  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using i64 = int64_t;
template <class T> using Vec = std::vector<T>;
template <class K, class V> using UM = std::unordered_map<K, V>;

using namespace std;

namespace std {
template <> struct hash<pair<int, int>> {
  size_t operator()(pair<int, int> p) const noexcept {
    return (size_t(p.first) << 18) | (size_t(p.second));
  }
};
} // namespace std

int main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);

  int n, k, s, t;
  cin >> n >> k >> s >> t;

  Vec<int> as(n);
  for (auto &a : as)
    cin >> a;

  if ((s & t) != s || (s | t) != t) {
    cout << 0;
    return 0;
  }

  auto invalid = [&](int a) { return s != (s & a) || t != (t | a); };
  as.erase(remove_if(as.begin(), as.end(), invalid), as.end());

  n = as.size();

  Vec<UM<pair<int, int>, i64>> by_andor(k + 19);
  by_andor[0][{t, s}] = 1;

  for (auto &lvl : by_andor)
    lvl.reserve(1 << 14);
  int aand, oor;

  for (int i = 0; i < as.size(); ++i) {
    int a = as[i];

    for (int j = min(i, k - 1); j >= 0; --j) {
      auto &next = by_andor[j + 1];

      for (auto &[key, value] : by_andor[j]) {
        tie(aand, oor) = key;
        next[{aand & a, oor | a}] += value;
      }
    }
  }

  i64 ans = 0;
  for (int i = 0; i <= k; ++i)
    ans += by_andor[i][{s, t}];
  cout << ans << '\\n';

  return 0;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define MP make_pair
#define F first
#define S second
typedef long long ll;
using namespace std;
int main() {
  ll n, k, s, t, a[50];
  cin >> n >> k >> s >> t;
  REP(i, n) cin >> a[i];
  map<pair<ll, ll>, ll> dp[50];
  REP(i, n) {
    for (ll j = k - 2; j + 1; j--) {
      for (auto l = dp[j].begin(); l != dp[j].end(); l++) {
        dp[j + 1][MP((l->F.F) & a[i], (l->F.S) | a[i])] += (l->S);
      }
    }
    dp[0][MP(a[i], a[i])] = 1;
  }
  n = 0;
  REP(i, k) n += (dp[i][MP(s, t)]);
  cout << n;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < n; i++)
#define MP make_pair
#define F first
#define S second
typedef long long ll;
using namespace std;
int main() {
  ll n, k, s, t, a[50];
  cin >> n >> k >> s >> t;
  REP(i, n) cin >> a[i];
  vector<map<pair<ll, ll>, ll>> dp(k);
  REP(i, n) {
    for (ll j = k - 2; j + 1; j--) {
      for (auto l = dp[j].begin(); l != dp[j].end(); l++) {
        dp[j + 1][MP((l->F.F) & a[i], (l->F.S) | a[i])] += (l->S);
      }
    }
    dp[0][MP(a[i], a[i])] = 1;
  }
  n = 0;
  REP(i, k) n += (dp[i][MP(s, t)]);
  cout << n;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define R(n) for (L i = 0; i < n; i++)
#define M make_pair
typedef long long L;
using namespace std;
int main() {
  L n, k, s, t, a[50];
  cin >> n >> k >> s >> t;
  R(n) cin >> a[i];
  map<pair<L, L>, L> p[50];
  R(n) {
    for (L j = k - 2; j + 1; j--)
      for (const auto &l : p[j])
        p[j + 1][M(l.first.first & a[i], l.first.second | a[i])] += l.second;
    p[0][M(a[i], a[i])] = 1;
  }
  n = 0;
  R(k) n += p[i][M(s, t)];
  cout << n;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define REP(n) for (L i = 0; i < n; i++)
#define M make_pair
typedef long long L;
using namespace std;
int main() {
  L n, k, s, t, a[50];
  cin >> n >> k >> s >> t;
  REP(n) cin >> a[i];
  map<pair<L, L>, L> p[50];
  REP(n) {
    for (L j = k - 2; j + 1; j--) {
      for (const auto &l : p[j]) {
        p[j + 1][M(l.first.first & a[i], l.first.second | a[i])] += l.second;
      }
    }
    p[0][M(a[i], a[i])] = 1;
  }
  n = 0;
  REP(k) n += p[i][M(s, t)];
  cout << n;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int pop_count(int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

int main() {
  int N, K, S, T;
  cin >> N >> K >> S >> T;
  if ((S & T) != S) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> A;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if ((a & S) == S && (a | T) == T)
      A.push_back(a - S);
  }
  T -= S;
  N = A.size();
  K = min(K, N);

  uint64_t comb[51][51] = {0};
  comb[0][0] = 1;
  for (int i = 1; i <= N; i++) {
    comb[i][0] = 1;
    for (int j = 1; j <= i; j++)
      comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
  }

  uint64_t sub[51] = {0};
  for (int i = 0; i <= N; i++)
    for (int j = 0; j < K; j++)
      sub[i] += comb[i][j];

  uint64_t ans = 0;
  for (int i = 0; i < N; i++) {
    for (int b = T;; b = (b - 1) & T) {
      int cnt = 0;
      int n1 = b & A[i];
      int n0 = b - n1;
      for (int j = i + 1; j < N; j++)
        if ((A[j] & n1) == n1 && (A[j] & n0) == 0)
          cnt++;
      uint64_t res = sub[cnt];
      if (pop_count(b) % 2)
        res *= -1;
      ans += res;
      if (b == 0)
        break;
    }
  }

  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int pop_count(int bits) {
  bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
  bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
  bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
  bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
  return (bits & 0x0000ffff) + (bits >> 16 & 0x0000ffff);
}

int main() {
  int N, K, S, T;
  cin >> N >> K >> S >> T;
  if ((S & T) != S) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> A;
  for (int i = 0; i < N; i++) {
    int a;
    cin >> a;
    if ((a & S) == S && (a | T) == T)
      A.push_back(a - S);
  }
  T -= S;
  N = A.size();
  K = min(K, N);

  uint64_t comb[51][51] = {0};
  comb[0][0] = 1;
  for (int i = 1; i <= N; i++) {
    comb[i][0] = 1;
    for (int j = 1; j <= i; j++)
      comb[i][j] = comb[i - 1][j - 1] + comb[i - 1][j];
  }

  uint64_t sub[51] = {0};
  for (int i = 0; i <= N; i++)
    for (int j = 1; j <= K; j++)
      sub[i] += comb[i][j];

  uint64_t ans = 0;
  for (int b = T;; b = (b - 1) & T) {
    uint64_t res = 0;
    for (int d = b;; d = (d - 1) & b) {
      int c = b - d;
      int cnt = 0;
      for (int a : A)
        if ((a & c) == 0 && (a & d) == d)
          cnt++;
      res += sub[cnt];
      if (d == 0)
        break;
    }
    if (pop_count(b) % 2)
      res *= -1;
    ans += res;
    if (b == 0)
      break;
  }

  cout << ans << endl;
  return 0;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  const int N = 55;
  vector<vector<long long>> C(N, vector<long long>(N, 0));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= i; j++) {
      if (i == j || j == 0)
        C[i][j] = 1;
      else
        C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
    }
  }
  int n, k, S, T;
  cin >> n >> k >> S >> T;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    if ((b & S) != S)
      continue;
    if ((b | T) != T)
      continue;
    a.push_back(b);
  }
  n = a.size();
  vector<int> bts;
  bool flag = true;
  for (int i = 0; i < 18; i++) {
    if ((S >> i & 1) && (~T >> i & 1))
      flag = false;
    if ((~S >> i & 1) && (T >> i & 1)) {
      bts.push_back(i);
    }
  }
  int m = bts.size();
  vector<long long> nums(m, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] >> bts[j] & 1)
        nums[j] |= 1ll << i;
    }
  }
  long long ans = 0;
  if (flag) {
    long long tot = 0;
    vector<int> add(n + 1, 0), del(n + 1, 0);
    vector<vector<long long>> st(1 << m);
    st[0].push_back((1ll << n) - 1);
    for (int i = 1; i < (1 << m); i++) {
      int bc = __builtin_popcount(i);
      for (int j = 0; j < m; j++) {
        if (i >> j & 1) {
          for (auto val : st[i ^ (1 << j)]) {
            if (val & nums[j])
              st[i].push_back(val & nums[j]);
            if (val & (~nums[j]))
              st[i].push_back(val & (~nums[j]));
          }
          break;
        }
      }
      long long sgn = 1;
      if (bc & 1)
        sgn = -1;
      for (auto val : st[i]) {
        bc = __builtin_popcountll(val);
        if (sgn > 0)
          add[bc]++;
        else
          del[bc]++;
        //				tot += sgn * C[bc][c];
      }
    }
    for (int c = 1; c <= min(n, k); c++) {
      tot += C[n][c];
      for (int i = 0; i <= n; i++) {
        tot += C[i][c] * (add[i] - del[i]);
      }
    }
    ans += tot;
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int n, k, s, t, ans = 0, a[128];

void dfs(int step, int num, int A, int O) {
  if ((s & A) != s)
    return;
  if ((t | O) != t)
    return;
  if (num > k)
    return;
  if (step == n && num <= k && A == s && O == t)
    ans++;
  if (step == n)
    return;
  dfs(step + 1, num, A, O);
  dfs(step + 1, num + 1, A & a[step], O | a[step]);
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k >> s >> t;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  dfs(0, 0, (1 << 20) - 1, 0);
  cout << ans << endl;
  return 0;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

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

mt19937 rng(123);

typedef long long i64;
typedef unsigned long long u64;

struct chash {
  u64 operator()(const pair<int, int> &p) const {
    return p.first * 12323123 + p.second;
  }
};

int n, k, s, t, a[50];

int main() {
  cin >> n >> k >> s >> t;
  vector<gp_hash_table<pair<int, int>, i64, chash>> dp(k + 1);
  dp[0][{(1 << 18) - 1, 0}] = 1;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if ((a[i] & s) != s || (a[i] | t) != t) {
      --i;
      --n;
      continue;
    }
  }
  shuffle(a, a + n, rng);
  for (int i = 0; i < n; ++i)
    for (int j = k - 1; j >= 0; --j)
      for (const auto &p : dp[j]) {
        int x = p.first.first & a[i], y = p.first.second | a[i];
        if ((x & s) != s)
          continue;
        if ((y | t) != t)
          continue;
        dp[j + 1][{x, y}] += p.second;
      }
  i64 ans = 0;
  for (int i = 1; i <= k; ++i)
    ans += dp[i][{s, t}];
  cout << ans << endl;
}


Error: TLE

Fixed submission:

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

mt19937 rng(123);

typedef long long i64;
typedef unsigned long long u64;

struct chash {
  u64 operator()(const pair<int, int> &p) const {
    return p.first * 12323123 + p.second;
  }
};

int n, k, s, t, a[50];

int main() {
  cin >> n >> k >> s >> t;
  vector<gp_hash_table<pair<int, int>, i64, chash>> dp(k + 1), pd(k + 1);
  dp[0][{(1 << 18) - 1, 0}] = 1;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    if ((a[i] & s) != s || (a[i] | t) != t) {
      --i;
      --n;
      continue;
    }
  }
  shuffle(a, a + n, rng);
  for (int i = 0; i < n; ++i) {
    pd = dp;
    for (int j = 0; j < k; ++j)
      for (const auto &p : dp[j]) {
        int x = p.first.first & a[i], y = p.first.second | a[i];
        if ((x & s) != s)
          continue;
        if ((y | t) != t)
          continue;
        pd[j + 1][{x, y}] += p.second;
      }
    dp = pd;
  }
  i64 ans = 0;
  for (int i = 1; i <= k; ++i)
    ans += dp[i][{s, t}];
  cout << ans << endl;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 2000007;
const int MOD = 998244353;
//

signed main() {
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  vector<int> a1(n), a2, b;
  for (int i = 0; i < n; i++)
    cin >> a1[i];
  for (int i = 17; i >= 0; i--) {
    if ((s & (1 << i)) > (t & (1 << i))) {
      cout << 0 << endl;
      return 0;
    }
    if ((s & (1 << i)) < (t & (1 << i))) {
      b.push_back(i);
      continue;
    }
    a2.clear();
    for (auto &&j : a1)
      if ((j & (1 << i)) == (s & (1 << i)))
        a2.push_back(((j >> (i + 1)) << i) + (j % (1 << i)));
    swap(a2, a1);
  }
  int ans = 0;
  int dp[a1.size() + 1][k + 1];
  for (int i = 0; i <= a1.size(); i++) {
    dp[i][0] = 1;
    for (int j = 1; j <= k; j++)
      dp[i][j] = (dp[i][j - 1] * (i + 1 - j)) / j;
  }
  for (int i = 0; i <= a1.size(); i++)
    for (int j = 1; j <= k; j++)
      dp[i][j] += dp[i][j - 1];

  for (int i = 0; i < (1 << b.size()); i++) {
    int k2 = 1;
    for (int j = 0; j < b.size(); j++) {
      if (i & (1 << j))
        k2 *= (-1);
    }

    map<int, int> mp;
    for (auto &&j : a1)
      mp[j & i]++;
    for (auto &&j : mp)
      ans += k2 * (dp[j.second][min(k, j.second)] - 1);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int MAX = 2000007;
const int MOD = 998244353;
//

signed main() {
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  vector<int> a1(n), a2, b;
  for (int i = 0; i < n; i++)
    cin >> a1[i];
  for (int i = 17; i >= 0; i--) {
    if ((s & (1 << i)) > (t & (1 << i))) {
      cout << 0 << endl;
      return 0;
    }
    if ((s & (1 << i)) < (t & (1 << i))) {
      b.push_back(i);
      continue;
    }
    a2.clear();
    for (auto &&j : a1)
      if ((j & (1 << i)) == (s & (1 << i)))
        a2.push_back(((j >> (i + 1)) << i) + (j % (1 << i)));
    swap(a2, a1);
  }
  int ans = 0;
  int dp[a1.size() + 1][k + 1];
  for (int i = 0; i <= a1.size(); i++) {
    dp[i][0] = 1;
    for (int j = 1; j <= k; j++)
      dp[i][j] = (dp[i][j - 1] * (i + 1 - j)) / j;
  }
  for (int i = 0; i <= a1.size(); i++)
    for (int j = 1; j <= k; j++)
      dp[i][j] += dp[i][j - 1];

  for (int i = 0; i < (1 << b.size()); i++) {
    int k2 = 1;
    for (int j = 0; j < b.size(); j++) {
      if (i & (1 << j))
        k2 *= (-1);
    }
    for (int i2 = 0; i2 < (1 << b.size()); i2++) {
      if ((i | i2) != i)
        continue;

      int p = 0;
      for (auto &&j : a1) {
        if ((j & i) == i2)
          p++;
      }
      // cerr<<dp[p][min(p,k)]<<' '<<p<<' '<<k<<endl;
      ans += k2 * (dp[p][min(p, k)] - 1);
    }
  }
  cout << ans << endl;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int N = 55, M = (1 << 18) + 5;
int n, m, k, s, t;
int a[N];
bool book[N];
long long C[N][N];
long long sum[N][N];
void init(int n = 50) {
  for (int i = 0; i <= n; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++)
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
  }
  for (int i = 0; i <= n; i++)
    for (int j = 1; j <= n; j++)
      sum[i][j] = sum[i][j - 1] + C[i][j];
  return;
}
int main() {
  scanf("%d%d%d%d", &n, &k, &s, &t);
  init();
  m = 18;
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = m - 1; i >= 0; i--) {
    if ((s & (1 << i)) == 1 && (t & (1 << i)) == 0) {
      printf("0");
      return 0;
    }
    if ((s & (1 << i)) == (t & (1 << i))) {
      for (int j = 1; j <= n; j++)
        if ((s & (1 << i)) != (a[j] & (1 << i)))
          book[j] = true;
    }
  }
  vector<int> v;
  for (int i = 1; i <= n; i++)
    if (!book[i])
      v.push_back(a[i]);
  n = 0;
  for (int u : v)
    a[++n] = u;
  long long ans = 0;
  for (int state = 0; state < (1 << m); state++) {
    bool flag = false;
    for (int i = 0; i < m; i++)
      if ((state & (1 << i)) && (s & (1 << i)) == (t & (1 << i))) {
        flag = true;
        break;
      }
    if (flag)
      continue;
    map<int, int> cnt;
    for (int i = 1; i <= n; i++)
      cnt[a[i] & state]++;
    long long res = 0;
    for (auto [val, num] : cnt)
      res += sum[num][k];
    if (__builtin_popcount(state) & 1)
      ans -= res;
    else
      ans += res;
  }
  printf("%lld", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int N = 55, M = (1 << 18) + 5;
int n, m, k, s, t;
int a[N];
map<int, map<pair<int, int>, long long>> f[N];
int main() {
  scanf("%d%d%d%d", &n, &k, &s, &t);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  reverse(a + 1, a + n + 1);
  m = (1 << 18) - 1;
  f[0][0][make_pair(m, 0)] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= k; j++) {
      map<pair<int, int>, long long> to;
      if (j >= 1) {
        for (auto it : f[i - 1][j - 1]) {
          int u = it.first.first, v = it.first.second;
          int nu = u & a[i], nv = v | a[i];
          if (nu < s || nv > t)
            continue;
          to[make_pair(nu, nv)] += it.second;
        }
      }
      for (auto it : f[i - 1][j]) {
        int u = it.first.first, v = it.first.second;
        to[make_pair(u, v)] += it.second;
      }
      f[i][j] = to;
    }
    for (int j = 0; j <= k; j++)
      f[i - 1][j].clear();
  }

  long long ans = 0;
  for (int j = 0; j <= k; j++)
    for (auto it : f[n][j]) {
      int u = it.first.first, v = it.first.second;
      if (u == s && v == t)
        ans += it.second;
    }
  printf("%lld", ans);
  return 0;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#undef _GLIBCXX_DEBUG

#include <bits/stdc++.h>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  if ((t & s) != s) {
    cout << 0 << '\\n';
    return 0;
  }
  vector<vector<long long>> C(n + 1, vector<long long>(n + 1));
  for (int i = 0; i <= n; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
    }
  }
  vector<long long> ways(n + 1);
  for (int i = 0; i <= n; i++) {
    for (int j = 1; j <= min(i, k); j++) {
      ways[i] += C[i][j];
    }
  }
  vector<int> b;
  long long ans = 0;
  for (int mask = 0; mask < (1 << 18); mask++) {
    if ((mask & (t ^ s)) != mask) {
      continue;
    }
    int sign = (__builtin_popcount(mask) % 2 == 0 ? 1 : -1);
    b.clear();
    for (int i = 0; i < n; i++) {
      if ((a[i] & s) != s) {
        continue;
      }
      if ((a[i] | t) != t) {
        continue;
      }
      b.push_back(a[i] & mask);
    }
    sort(b.begin(), b.end());
    int beg = 0;
    while (beg < (int)b.size()) {
      int end = beg;
      while (end + 1 < (int)b.size() && b[end + 1] == b[end]) {
        ++end;
      }
      ans += sign * ways[end - beg + 1];
      beg = end + 1;
    }
  }
  cout << ans << '\\n';
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

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

  vector<map<pair<int, int>, long long>> dp(K + 1);
  dp[0][{(1 << 18) - 1, 0}] = 1;

  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    if ((a & S) != S)
      continue;
    if ((a | T) != T)
      continue;
    for (int k = K - 1; k >= 0; --k) {
      for (auto &p : dp[k]) {
        int prod, sum;
        tie(prod, sum) = p.first;
        dp[k + 1][{prod & a, sum | a}] += p.second;
      }
    }
  }

  long long ans = 0;
  for (int k = 1; k <= K; ++k) {
    ans += dp[k][{S, T}];
  }
  cout << ans << endl;

  return 0;
}