Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

// he's not even trying
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fore(b, c) for (int val0 = b; val0 < c; val0++)
#define forr(k, c, s) for (int k = c; k < s; k++)
#define pb push_back
#define mmp make_pair
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long double ld;
typedef vector<vii> al;
typedef vector<ll> vl;
const int INF = 1e9;
const ll INFL = 1LL << 61;
struct chash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
typedef gp_hash_table<ll, ll, chash> gash;
vl w;
int bru(int u) {
  int res = 0;
  while (u) {
    res += u & 1;
    u >>= 1;
  }
  return res;
}
ll dp[55][55];
ll cho(int n, int k) {
  if (k < 0 || k > n) {
    return 0;
  }
  if (k == 0 || k == n) {
    return 1;
  }
  if (dp[n][k] != -1) {
    return dp[n][k];
  }
  return dp[n][k] = cho(n - 1, k - 1) + cho(n - 1, k);
}
ll calc(ll res, ll ks) {
  if (res <= ks) {
    return (1LL << res) - 1;
  } else {
    ll wa = 0;
    for (int i = 1; i <= ks; i++) {
      wa += cho(res, i);
    }
    return wa;
  }
}
int main() {
  ios::sync_with_stdio(0);
  cout.precision(20);
  cout.tie(0);
  cin.tie(0);
  ll n, ks, as, ot;
  cin >> n >> ks >> as >> ot;
  memset(dp, -1, sizeof(dp));
  if ((as & ot) != as) {
    cout << 0 << '\\n';
    return 0;
  }
  for (int i = 0; i < n; i++) {
    ll t;
    cin >> t;
    w.push_back(t);
  }
  ll tob = 0;
  ll ko = 0;
  for (int i = 0; i < 20; i++) {
    if (((as >> i) & 1) == ((ot >> i) & 1)) {
      int bo = (as >> i) & 1;
      vl we;
      for (int j = 0; j < w.size(); j++) {
        if (((w[j] >> i) & 1) == bo) {
          we.push_back(w[j]);
        }
      }
      w = we;
    } else {
      ko++;
      tob |= (1 << i);
    }
  }
  as = 0;
  ot = (1 << ko) - 1;
  n = w.size();
  for (int i = 0; i < n; i++) {
    ll ru = 0;
    ll rb = 0;
    for (int j = 0; j < 20; j++) {
      if ((tob >> j) & 1) {
        ru |= (((w[i] >> j) & 1) << rb);
        rb++;
      }
    }
    w[i] = ru;
  }
  ll ans = 0;
  for (int i = 0; i <= ot; i++) {
    int sg = 1 - ((bru(i) & 1) * 2);
    gash lol;
    for (int j = 0; j < n; j++) {
      lol[i & w[j]]++;
    }
    ll res = 0;
    for (auto &it : lol) {
      res += calc(it.second, ks);
    }
    ans += sg * res;
  }
  cout << ans << '\\n';
}


Error: TLE

Fixed submission:

// he's not even trying
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define fore(b, c) for (int val0 = b; val0 < c; val0++)
#define forr(k, c, s) for (int k = c; k < s; k++)
#define pb push_back
#define mmp make_pair
using namespace __gnu_pbds;
using namespace std;
template <typename T>
using oset =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef long double ld;
typedef vector<vii> al;
typedef vector<ll> vl;
const int INF = 1e9;
const ll INFL = 1LL << 61;
struct chash {
  static uint64_t splitmix64(uint64_t x) {
    // http://xorshift.di.unimi.it/splitmix64.c
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }

  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
typedef gp_hash_table<ll, ll, chash> gash;
vl w;
int main() {
  ios::sync_with_stdio(0);
  cout.precision(20);
  cout.tie(0);
  cin.tie(0);
  ll n, ks, as, ot;
  cin >> n >> ks >> as >> ot;
  for (int i = 0; i < n; i++) {
    ll t;
    cin >> t;
    w.push_back(t);
  }
  shuffle(w.begin(), w.end(), rng);
  for (int i = 0; i < 20; i++) {
    if (((as >> i) & 1) == ((ot >> i) & 1)) {
      int bo = (as >> i) & 1;
      vl we;
      for (int j = 0; j < w.size(); j++) {
        if (((w[j] >> i) & 1) == bo) {
          we.push_back(w[j]);
        }
      }
      w = we;
    }
  }
  n = w.size();
  ll res = 0;
  for (int st = 0; st < n; st++) {
    int cu = 0;
    gash dp[2];
    dp[cu][(1LL << 40) | (w[st] << 20) | w[st]] = 1;
    for (int j = st + 1; j < n; j++) {
      int nu = 1 - cu;
      dp[nu] = dp[cu];
      for (auto &it : dp[cu]) {
        ll soz = it.first >> 40;
        ll en = (it.first >> 20) & ((1 << 20) - 1);
        ll re = (it.first) & ((1 << 20) - 1);
        en &= w[j];
        re |= w[j];
        if ((en & as) != as) {
          continue;
        }
        if ((re | ot) != ot) {
          continue;
        }
        soz++;
        if (soz <= ks) {
          dp[nu][(soz << 40) | (en << 20) | (re)] += it.second;
        }
      }
      cu = nu;
    }
    for (ll i = 1; i <= ks; i++) {
      res += dp[cu][(i << 40) | (as << 20) | (ot)];
    }
  }
  cout << res << '\\n';
}