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

#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
using Int = long long;
const char newl = '\\n';

template <typename T> void drop(const T &x) {
  cout << x << endl;
  exit(0);
}

// INSERT ABOVE HERE
const Int LOG = 18;
const Int SZ = 1 << LOG;
using ll = long long;
ll dp[SZ] = {};
ll binom[55][55];
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

  binom[0][0] = 1;
  for (Int i = 0; i < 52; i++) {
    for (Int j = 0; j < 52; j++) {
      binom[i + 1][j + 0] += binom[i][j];
      binom[i + 1][j + 1] += binom[i][j];
    }
  }

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

  vector<Int> as;
  for (Int i = 0; i < n; i++) {
    Int a;
    cin >> a;
    if ((a & s) != s)
      continue;
    if ((a | t) != t)
      continue;
    as.emplace_back(a);
  }
  if (as.empty())
    drop(0);
  n = as.size();

  ll ans = 0;
  for (Int p = 0; p < n; p++) {
    Int b = as[p];

    memset(dp, 0, sizeof(dp));
    for (Int q = p + 1; q < n; q++) {
      Int a = as[q];

      Int ok = 0;

      for (Int j = 0; j < LOG; j++) {
        if (((s >> j) & 1) == ((t >> j) & 1))
          ok |= 1 << j;
        if (((a >> j) & 1) != ((b >> j) & 1))
          ok |= 1 << j;
      }
      dp[ok]++;
    }

    for (Int i = 1; i < SZ; i <<= 1)
      for (Int j = 0; j < SZ; j++)
        if ((j & i) == 0)
          dp[j | i] += dp[j];

    for (Int bit = 0; bit < SZ; bit++) {
      Int ng = LOG - __builtin_popcount(bit);
      for (Int c = 0; c < k; c++) {
        if (ng & 1)
          ans -= binom[dp[bit]][c];
        else
          ans += binom[dp[bit]][c];
      }
    }
  }
  cout << ans << newl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}
using Int = long long;
const char newl = '\\n';

template <typename T> void drop(const T &x) {
  cout << x << endl;
  exit(0);
}

// INSERT ABOVE HERE
const int LOG = 18;
const int SZ = 1 << LOG;
using ll = long long;
ll dp[55][SZ] = {};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(0);

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

  vector<int> as;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if ((a & s) != s)
      continue;
    if ((a | t) != t)
      continue;
    as.emplace_back(a);
  }
  if (as.empty())
    drop(0);
  n = as.size();

  ll ans = 0;
  for (int p = 0; p < n; p++) {
    int b = as[p];

    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;

    for (int q = p + 1; q < n; q++) {
      int a = as[q];

      int ok = 0;

      for (int j = 0; j < LOG; j++) {
        if (((s >> j) & 1) == ((t >> j) & 1))
          ok |= 1 << j;
        if (((a >> j) & 1) != ((b >> j) & 1))
          ok |= 1 << j;
      }

      // cerr<<a<<' '<<ok<<endl;

      for (int c = q - p - 1; c >= 0; c--)
        for (int i = 0; i < SZ; i++)
          dp[c + 1][i | ok] += dp[c][i];
    }

    for (int c = 0; c < k; c++)
      ans += dp[c][SZ - 1];
  }
  cout << ans << newl;
  return 0;
}