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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
  os << '(' << a.first << ',' << a.second << ')';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (auto x : v)
    os << x << ' ';
  return os;
}
void debug() { cerr << '\\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
  cerr << a;
  if (sizeof...(b))
    cerr << ", ";
  debug(b...);
}
#include <unordered_map>
unordered_map<ll, ll> dp[51];
int main() {
  int n, k;
  int s, t;
  cin >> n >> k >> s >> t;
  dp[0][(1 << 18) - 1] = 1;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if ((s & x) == s and (t | x) == t)
      for (int j = k - 1; j >= 0; j--) {
        for (auto &y : dp[j]) {
          dp[j + 1][(((y.first >> 18) | x) << 18) + (y.first & x)] += y.second;
        }
      }
  }
  ll ans = 0;
  for (int i = 1; i <= k; i++) {
    ans += dp[i][((ll)t << 18) + s];
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
// #define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream &operator<<(ostream &os, pair<T, S> a) {
  os << '(' << a.first << ',' << a.second << ')';
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> v) {
  for (auto x : v)
    os << x << ' ';
  return os;
}
void debug() { cerr << '\\n'; }
template <typename H, typename... T> void debug(H a, T... b) {
  cerr << a;
  if (sizeof...(b))
    cerr << ", ";
  debug(b...);
}
map<ll, ll> dp[51];
int main() {
  int n, k;
  ll s, t;
  cin >> n >> k >> s >> t;
  dp[0][(1 << 18) - 1] = 1;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    for (int j = k - 1; j >= 0; j--) {
      for (auto &y : dp[j]) {
        dp[j + 1][(((y.first >> 18) | x) << 18) + (y.first & x)] += y.second;
      }
    }
  }
  ll ans = 0;
  for (int i = 1; i <= k; i++) {
    ans += dp[i][(t << 18) + s];
  }
  cout << ans << endl;
}