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

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

// int N, M, K, H, W, L, R;
long long int N, M, K, H, W, L, R;

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

  cin >> N >> K;
  cin >> L >> R;
  if ((R & L) != L) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> v;
  for (int i = 0; i < N; i++) {
    cin >> M;
    if ((L & M) != L)
      continue;
    if ((R | M) != R)
      continue;
    v.push_back(M);
  }
  R -= L;
  L = 0;
  for (auto &i : v)
    i -= L;
  vector<vector<long long int>> binom(N + 1, vector<long long int>(N + 1, 0));
  for (int i = 1; i <= N; i++) {
    binom[i][1] = i;
  }
  for (int i = 2; i <= N; i++) {
    for (int j = 2; j <= i; j++) {
      binom[i][j] += binom[i - 1][j] + binom[i - 1][j - 1];
    }
  }
  for (int i = 1; i <= N; i++) {
    for (int j = 2; j <= K; j++) {
      binom[i][j] += binom[i][j - 1];
    }
  }
  long long int ans = 0;
  ans += binom[v.size()][K];
  for (int i = 1; i < (1 << 18); i++) {
    if ((i & R) != i)
      continue;
    map<int, int> mp;
    for (auto j : v) {
      mp[j & i]++;
    }
    int box = i;
    int num = 0;
    while (box) {
      num += box & 1;
      box >>= 1;
    }
    for (auto j : mp) {
      //	cout << i << " " << j.first << " " << j.second << endl;
      if (num & 1) {
        ans -= binom[j.second][K];
        //		ans %= MOD;
      } else {
        ans += binom[j.second][K];
        //		ans %= MOD;
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include "algorithm"
#include "bitset"
#include "cassert"
#include "climits"
#include "cmath"
#include "cstdio"
#include "ctime"
#include "functional"
#include "iomanip"
#include "iostream"
#include "list"
#include "map"
#include "numeric"
#include "queue"
#include "random"
#include "set"
#include "stack"
#include "string"
#include "unordered_map"
#include "unordered_set"

using namespace std;

constexpr long long int MOD = 1000000007;
// constexpr int MOD = 1000000007;
// constexpr int MOD = 998244353;
// constexpr long long int MOD = 998244353;
constexpr double EPS = 1e-9;

int N, M, K, H, W, L, R;
// long long int N, M, K, H, W, L, R;

int dp[51][262144];

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

  cin >> N >> K;
  cin >> L >> R;
  if ((L & R) != L) {
    cout << 0 << endl;
    return 0;
  }
  vector<int> v;
  for (int i = 0; i < N; i++) {
    cin >> M;
    if ((L & M) != L)
      continue;
    if ((R | M) != R)
      continue;
    v.push_back(M);
  }
  N = v.size();
  long long int ans = 0;
  dp[1][0] = 1;
  for (int i = 0; i < N; i++) {
    H = L ^ v[i];
    W = R ^ v[i];
    //	cout << i << " " << W << endl;
    for (int j = i + 1; j < N; j++) {
      int box = (W & v[j]) | ((H) & (262143 ^ v[j]));
      for (int k = K; k > 1; k--) {
        for (int l = 0; l < 262144; l++) {
          dp[k][l | box] += dp[k - 1][l];
          if (dp[k][l | box] >= MOD)
            dp[k][l | box] -= MOD;
        }
      }
    }
    for (int j = 1; j <= K; j++) {
      ans += dp[j][H ^ W];
      if (ans >= MOD)
        ans -= MOD;
      if (j > 1)
        for (int k = 0; k < 262144; k++)
          dp[j][k] = 0;
    }
  }
  cout << ans << endl;
}