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

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
// #pragma warning(disable : 4996)

#ifdef _MSC_VER
#include <intrin.h>

#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

// #include"atcoder/all"
// using namespace atcoder;
using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << endl
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
#define int long long
// #define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v)                                                         \\
  REP(i, v.size()) cout << v[i] << " \\n"[i == (int)v.size() - 1]
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;
}
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;

long long nCr(long long n, long long r) {
  if (r > n / 2)
    r = n - r; // because C(n, r) == C(n, n - r)
  long long ans = 1;
  long long i;

  for (i = 1; i <= r; i++) {
    ans *= n - r + i;
    ans /= i;
  }

  return ans;
}

using Bit = bitset<20>;
namespace std {
bool operator<(const Bit &a, const Bit &b) {
  REP(i, 20) if ((!a[i]) && (b[i])) return true;
  return false;
}
} // namespace std

bool ign[20];
int dp[60][60];

void solve() {
  int N, K, S, T;
  cin >> N >> K >> S >> T;
  vector<int> A(N);
  REP(i, N) cin >> A[i];

  Bit s(S), t(T);
  vector<Bit> B(N);
  REP(i, N) { B[i] = Bit(A[i]); }
  // コーナーケース
  if (S == T) {
    bool found = false;
    REP(i, N) {
      if (A[i] == S)
        found = true;
    }
    if (found)
      print(1);
    else
      print(0);
    return;
  }
  bool ng = false;
  REP(i, 18) {
    if (s[i] && (!t[i]))
      ng = true;
  }
  if (ng) {
    print(0);
    return;
  }
  set<int> st;
  REP(i, N) st.insert(A[i]);
  REP(j, 18) {
    if (s[j] && t[j]) {
      ign[j] = true;
      REP(i, N) {
        if (!(A[i] & (1 << j)))
          st.erase(A[i]);
      }
    }
    if ((!s[j]) && (!t[j])) {
      ign[j] = true;
      REP(i, N) if (A[i] & (1 << j)) st.erase(A[i]);
    }
  }
  B.clear();
  for (auto b : st)
    B.push_back(Bit(b));
  N = B.size();
  int sb = 0;
  map<int, int> mp;

  int al = 0; // sum nCk
  FOR(i, 1, K + 1) {
    if (i <= N)
      al += nCr(N, i);
  }

  FOR(bit, 1, (1 << 18)) {
    bool skip = false;
    REP(j, 18) {
      if (ign[j]) {
        if (bit & (1 << j))
          skip = true;
      }
    }
    if (skip)
      continue;
    Bit b(bit);
    // print(b);
    bool found = false;
    REP(j, 18) if (ign[j] && b[j]) continue;
    vector<int> C;
    for (auto x : B) {
      int t = x.to_ullong();
      t &= bit;
      C.push_back(t);
    }
    // for (auto &x : C)x &= b;
    mp.clear();
    for (auto x : C)
      mp[x]++;
    int M = mp.size();
    int tmp = 0;
    for (auto x : mp) {
      int n = x.second;
      FOR(i, 1, min(n + 1, K + 1)) { tmp += nCr(n, i); }
    }
    // REP(i, M + 1)REP(j, K + 1)dp[i][j] = 0;
    // int idx = 0;
    // dp[0][0] = 1;
    // for (auto x : mp) {
    //	REP(j, K + 1) {
    //		dp[idx + 1][j] += dp[idx][j];
    //		dp[idx + 1][j + 1] += dp[idx][j] * x.second;
    //	}
    //	idx++;
    // }
    // int sub = 0;
    // FOR(j, 1, K + 1)sub += dp[M][j];
    int bitcnt = 0;
    REP(i, 18) bitcnt += b[i];
    // int tmp = al - sub;
    if (bitcnt & 1)
      sb += tmp;
    else
      sb -= tmp;
  END:;
  }
  print(al - sb);
}

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

  // int q;
  // cin >> q;
  // while (q--)
  solve();
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#pragma GCC optimize("unroll-loops")
// #pragma warning(disable : 4996)

#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#define __builtin_popcountll __popcnt64
#endif

#include <limits.h>
#include <math.h>
#include <time.h>

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n - 1; i >= 0; --i)
#define FOR(i, m, n) for (int i = m; i < n; ++i)
#define FORR(i, m, n) for (int i = m - 1; i >= n; --i)
#define SORT(v, n) sort(v, v + n);
#define VSORT(v) sort(v.begin(), v.end());
#define REVERSE(v, n) reverse(v, v + n);
#define VREVERSE(v) reverse(v.begin(), v.end())
#define ll long long
#define print(x) cout << (x) << '\\n'
#define pe(x) cout << (x) << " "
#define DEBUG(x) cout << #x << ": " << x << endl
#define lb(v, n) lower_bound(v.begin(), v.end(), (n))
#define ub(v, n) upper_bound(v.begin(), v.end(), (n))
// #define int long long
#define double long double
#define all(x) (x).begin(), (x).end()
#define print_space(v)                                                         \\
  REP(i, v.size()) cout << v[i] << ((i == v.size() - 1) ? "\\n" : " ")
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;
}
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef array<int, 3> arr3;
std::random_device rd;
std::mt19937 mt(rd());
constexpr ll MOD = 1e9 + 7;
constexpr int MAX = 500050;
const double pi = acos(-1);
constexpr double EPS = 1e-8;
constexpr ll LINF = 1e18 + 1;
constexpr int INF = 1e9 + 1;

unordered_map<int, ll> dp[2][52][1 << 18];

void solve() {
  int N, K, S, T;
  cin >> N >> K >> S >> T;
  vector<int> A(N);
  REP(i, N) cin >> A[i];
  dp[0][0][(1 << 18) - 1][0] = 1;
  REP(i, N) {
    int now = i % 2;
    int nxt = (i + 1) % 2;
    REP(k, K + 1) REP(s, 1 << 18) dp[nxt][k][s].clear();
    REP(k, K + 1) {
      REP(s, 1 << 18) {
        if ((s & S) != S)
          continue;
        for (auto x : dp[now][k][s]) {
          int t = x.first;
          ll cnt = x.second;
          dp[nxt][k][s][t] += cnt;
          int ns = s & A[i], nt = t | A[i];
          if (((ns & S) == S) && ((nt | T) == T)) {
            dp[nxt][k + 1][ns][nt] += cnt;
            // dp[nxt][k + 1][ns][nt] += cnt;
          }
        }
      }
    }
  }
  ll ans = 0;
  FOR(k, 1, K + 1) { ans += dp[N % 2][k][S][T]; }
  print(ans);
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // mt.seed((int)(time(0)));
  // REP(_, 10000)test();
  // int q;
  // cin >> q;
  // while (q--)
  solve();
}