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

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
const int MAXN = 55;
ll binom[MAXN][MAXN];
int main() {
  for (int i = 0; i < MAXN; ++i)
    for (int j = 0; j <= i; ++j)
      if (!j || j == i)
        binom[i][j] = 1;
      else
        binom[i][j] = binom[i - 1][j - 1] + binom[i - 1][j];
  int n, k, S, T;
  scanf("%d%d%d%d", &n, &k, &S, &T);
  vector<int> A(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  if ((T & S) != S) {
    printf("0\\n");
    return 0;
  }
  vector<int> bits;
  for (int j = 0; j < 18; ++j) {
    if ((T & (1 << j)) && !(S & (1 << j)))
      bits.push_back(j);
  }
  int s = bits.size();
  vector<int> B;
  for (int i = 0; i < n; ++i) {
    if (~T & A[i])
      continue;
    if ((S & A[i]) != S)
      continue;
    int b = 0;
    for (int j = 0; j < s; ++j) {
      if (A[i] & (1 << bits[j]))
        b |= 1 << j;
    }
    B.push_back(b);
  }
  int req = (1 << s);
  int m = B.size();
  ll ans = 0;
  for (int i = 0; i < m; ++i) {
    vector<int> V;
    for (int j = i + 1; j < m; ++j) {
      int d = B[j] ^ B[i];
      V.push_back(d);
    }
    // 0..K-1 things ORing to req-1.
    for (int b = 0; b < req; ++b) {
      int cnt = 0;
      for (int x : V) {
        if (x & b)
          continue;
        ++cnt;
      }
      ll t = 0;
      for (int i = 0; i < k; ++i)
        t += binom[cnt][i];
      if (__builtin_popcount(b) & 1)
        ans -= t;
      else
        ans += t;
    }
  }
  printf("%lld\\n", ans);
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// #define cerr if (false) cerr
#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z)                                                           \\
  cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z     \\
       << endl
#define dbv(v)                                                                 \\
  cerr << #v << "=";                                                           \\
  for (auto _x : v)                                                            \\
    cerr << _x << ", ";                                                        \\
  cerr << endl
#define dba(a, n)                                                              \\
  cerr << #a << "=";                                                           \\
  for (int _i = 0; _i < (n); ++_i)                                             \\
    cerr << a[_i] << ", ";                                                     \\
  cerr << endl
template <typename A, typename B>
ostream &operator<<(ostream &os, const pair<A, B> &x) {
  return os << "(" << x.first << "," << x.second << ")";
}
typedef long long ll;
typedef long double ld;
int main() {
  int n, k, S, T;
  scanf("%d%d%d%d", &n, &k, &S, &T);
  vector<int> A(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &A[i]);
  }
  if ((T & S) != S) {
    printf("0\\n");
    return 0;
  }
  vector<int> bits;
  for (int j = 0; j < 18; ++j) {
    if ((T & (1 << j)) && !(S & (1 << j)))
      bits.push_back(j);
  }
  int s = bits.size();
  vector<int> B;
  for (int i = 0; i < n; ++i) {
    if (~T & A[i])
      continue;
    if ((S & A[i]) != S)
      continue;
    int b = 0;
    for (int j = 0; j < s; ++j) {
      if (A[i] & (1 << bits[j]))
        b |= 1 << j;
    }
    B.push_back(b);
  }
  int req = (1 << s);
  int m = B.size();
  ll ans = 0;
  for (int i = 0; i < m; ++i) {
    vector<int> V;
    for (int j = i + 1; j < m; ++j) {
      int d = B[j] ^ B[i];
      V.push_back(d);
    }
    // 0..K-1 things ORing to req-1.
    vector<vector<ll>> dp(k, vector<ll>(req));
    dp[0][0] = 1;
    for (int x : V) {
      for (int c = k - 1; c; --c) {
        for (int j = 0; j < req; ++j) {
          dp[c][j | x] += dp[c - 1][j];
        }
      }
    }
    for (int c = 0; c < k; ++c)
      ans += dp[c][req - 1];
  }
  printf("%lld\\n", ans);
}