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

#include <bits/stdc++.h>
using namespace std;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int n, k, S, T;
// S: AND
// T: OR

bool compat(int x) {
  if ((x | T) != T)
    return false;
  if ((x & S) != S)
    return false;

  return true;
}

bool ok(ll mask) {
  // check S
  for (int i = 0; i < 18; i++) {
    if (!(S >> i & 1)) {
      if (mask >> i & 1) {
        if (!((mask >> (i + 18)) & 1)) {
          return false;
        }
      }
    }
  }

  // check T
  for (int i = 0; i < 18; i++) {
    if (T >> i & 1) {
      if (!(mask >> i & 1)) {
        return false;
      }
    } else {
      if (mask >> i & 1) {
        return false;
      }
    }
  }

  return true;
}

ll make(ll x, ll mask) {
  mask |= x;
  x = (~x) & ((1 << 18) - 1);
  mask |= (x << 18);

  return mask;
}

unordered_map<ll, ll> dp[55]; // dp[size][have]

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

  cin >> n >> k >> S >> T;
  if ((S & T) != S)
    out(0);

  vector<int> a;
  while (n--) {
    int x;
    cin >> x;
    if (compat(x)) {
      a.push_back(x);
    }
  }

  dp[0][0] = 1;

  for (int x : a) {
    for (int sz = k - 1; sz >= 0; sz--) {
      for (auto p : dp[sz]) {
        ll nmask = make(x, p.first);
        dp[sz + 1][nmask] += p.second;
      }
    }
  }

  ll res = 0;
  for (int i = 1; i <= k; i++) {
    for (auto p : dp[i]) {
      if (ok(p.first)) {
        res += p.second;
      }
    }
  }

  cout << res << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

template <typename T> void out(T x) {
  cout << x << endl;
  exit(0);
}
#define watch(x) cout << (#x) << " is " << (x) << endl

using ll = long long;

const ll mod = 1e9 + 7;
const int maxn = 1e6 + 5;

int n, k, S, T;
// S: AND
// T: OR

bool compat(int x) {
  if ((x | T) != T)
    return false;
  if ((x & S) != S)
    return false;

  return true;
}

bool ok(ll mask) {
  // check S
  for (int i = 0; i < 18; i++) {
    if (!(S >> i & 1)) {
      if (mask >> i & 1) {
        if (!((mask >> (i + 18)) & 1)) {
          return false;
        }
      }
    }
  }

  // check T
  for (int i = 0; i < 18; i++) {
    if (T >> i & 1) {
      if (!(mask >> i & 1)) {
        return false;
      }
    } else {
      if (mask >> i & 1) {
        return false;
      }
    }
  }

  return true;
}

ll make(int x, ll mask) {
  for (int j = 0; j < 18; j++) {
    if (x >> j & 1) {
      mask |= (1ll << j);
    } else {
      mask |= (1ll << (18 + j));
    }
  }

  return mask;
}

unordered_map<ll, ll> dp[55]; // dp[size][have]

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

  cin >> n >> k >> S >> T;
  if ((S & T) != S)
    out(0);

  vector<int> a;
  while (n--) {
    int x;
    cin >> x;
    if (compat(x)) {
      a.push_back(x);
    }
  }

  dp[0][0] = 1;

  for (int x : a) {
    for (int sz = k - 1; sz >= 0; sz--) {
      for (auto p : dp[sz]) {
        ll nmask = make(x, p.first);
        dp[sz + 1][nmask] += p.second;
      }
    }
  }

  ll res = 0;
  for (int i = 1; i <= k; i++) {
    for (auto p : dp[i]) {
      if (ok(p.first)) {
        res += p.second;
      }
    }
  }

  cout << res << endl;

  return 0;
}