alexjercan/bugnet huggingface dataset:
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;
int N;
vector<int> V, W;
unordered_map<int, int> mp;
int dp[1024][100001];
int rec(int v, int L) {
  if (v < 1024)
    return dp[v][L];
  int res = rec(v / 2, L);
  if (L >= W[v]) {
    res = max(res, rec(v / 2, L - W[v]) + V[v]);
  }
  return res;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  V.resize(N + 1);
  W.resize(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> V[i + 1] >> W[i + 1];
    // V[i + 1] = W[i + 1] = 1;
  }
  int Q;
  cin >> Q;
  for (int i = 1; i <= min(N, 1023); i++) {
    int p = i / 2;
    for (int j = 0; j < 100001; j++) {
      dp[i][j] = dp[p][j];
      if (j >= W[i]) {
        dp[i][j] = max(dp[i][j], dp[p][j - W[i]] + V[i]);
      }
    }
  }
  for (int i = 0; i < Q; i++) {
    int v, L;
    cin >> v >> L;
    // v = i + 1;
    // L = i + 1;
    cout << rec(v, L) << "\\n";
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define int long long
const int MOD = 1000000007;

int N;
vector<int> V, W;
unordered_map<int, int> mp;
int rec(int v, int L) {
  if (v == 0 || L <= 0)
    return 0;
  int t = (v << 30) + L;
  auto x = mp.find(t);
  if (mp.find(t) == mp.end()) {
    int res = rec(v / 2, L);
    if (L >= W[v]) {
      res = max(res, rec(v / 2, L - W[v]) + V[v]);
    }
    mp[t] = res;
    return res;
  } else {
    return (*x).second;
  }
  return 0;
}
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> N;
  V.resize(N + 1);
  W.resize(N + 1);
  for (int i = 0; i < N; i++) {
    cin >> V[i + 1] >> W[i + 1];
  }
  int Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    int v, L;
    cin >> v >> L;
    cout << rec(v, L) << "\\n";
  }
  cout << endl;
  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1 << 29;

int main() {
  int n;
  scanf("%d", &n);
  vector<int> v(n), w(n);
  rep(i, n) scanf("%d%d", &v[i], &w[i]);

  vector<vector<int>> dp(min(1 << 9, n), vector<int>(100001, -INF));
  rep(i, min(1 << 9, n)) {
    if (i == 0) {
      dp[i][0] = 0;
    } else {
      dp[i] = dp[(i - 1) / 2];
    }
    for (int a = 100000; a >= w[i]; a--)
      dp[i][a] = max(dp[i][a], dp[i][a - w[i]] + v[i]);
    rep(a, 100000) dp[i][a + 1] = max(dp[i][a + 1], dp[i][a]);
  }

  int q;
  scanf("%d", &q);
  rep(_, q) {
    int u, W;
    scanf("%d%d", &u, &W);
    u--;

    vector<int> V;
    while (u >= min(1 << 9, n)) {
      V.emplace_back(u);
      u = (u - 1) / 2;
    }

    int ans = 0;
    int m = V.size();
    rep(S, 1 << m) {
      int wsum = 0, vsum = 0;
      rep(i, m) if (S >> i & 1) {
        wsum += w[V[i]];
        vsum += v[V[i]];
      }
      if (wsum > W)
        continue;

      ans = max(ans, dp[u][W - wsum] + vsum);
    }
    printf("%d\\n", ans);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1 << 29;

int main() {
  int n;
  scanf("%d", &n);
  vector<int> v(n), w(n);
  rep(i, n) scanf("%d%d", &v[i], &w[i]);

  vector<vector<int>> dp(min(1 << 9, n), vector<int>(10001, -INF));
  rep(i, min(1 << 9, n)) {
    if (i == 0) {
      dp[i][0] = 0;
    } else {
      dp[i] = dp[(i - 1) / 2];
    }
    for (int a = 10000; a >= w[i]; a--)
      dp[i][a] = max(dp[i][a], dp[i][a - w[i]] + v[i]);
    rep(a, 10000) dp[i][a + 1] = max(dp[i][a + 1], dp[i][a]);
  }

  int q;
  scanf("%d", &q);
  rep(_, q) {
    int u, W;
    scanf("%d%d", &u, &W);
    u--;

    vector<int> V;
    while (u >= min(1 << 9, n)) {
      V.emplace_back(u);
      u = (u - 1) / 2;
    }

    int ans = 0;
    int m = V.size();
    rep(S, 1 << m) {
      int wsum = 0, vsum = 0;
      rep(i, m) if (S >> i & 1) {
        wsum += w[V[i]];
        vsum += v[V[i]];
      }
      if (wsum > W)
        continue;

      ans = max(ans, dp[u][W - wsum] + vsum);
    }
    printf("%d\\n", ans);
  }

  return 0;
}
Problem p02648 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
int mae[1024][100001], V[262144], W[262144];

int dp(int i, int w) {
  if (w == 0) {
    return 0;
  }
  if (i < 1024) {
    return mae[i][w];
  }
  int ans = dp(i / 2, w);
  if (W[i] <= w) {
    ans = max(ans, dp(i / 2, w - W[i]) + V[i]);
  }
  return ans;
}

int main() {
  int N, Q;
  cin >> N;
  REP(i, N) { cin >> V[i + 1] >> W[i + 1]; }
  for (int i = 1; i < 1024; i++) {
    REP(j, 100001) {
      if (W[i] <= j) {
        mae[i][j] = max(mae[i / 2][j - W[i]] + V[i], mae[i / 2][j]);
      } else {
        mae[i][j] = mae[i / 2][j];
      }
    }
  }
  cin >> Q;
  int v, L;
  REP(i, Q) {
    cin >> v >> L;
    cout << dp(v, L) << "\\n";
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
typedef map<int, map<int, int>> MIMII;
typedef map<int, map<int, bool>> MIMIB;
int mae[513][100001], V[262144], W[262144];

int dp(int i, int w, MIMII &ans, MIMIB &kst) {
  if (w == 0) {
    return 0;
  }
  if (i <= 512) {
    return mae[i][w];
  }
  if (kst[i][w]) {
    return ans[i][w];
  }
  kst[i][w] = true;
  if (w < W[i]) {
    return ans[i][w] = dp(i / 2, w, ans, kst);
  } else {
    return ans[i][w] = max(dp(i / 2, w - W[i], ans, kst) + V[i],
                           dp(i / 2, w, ans, kst));
  }
}

int main() {
  int N, Q;
  cin >> N;
  REP(i, N) { cin >> V[i + 1] >> W[i + 1]; }
  for (int i = 1; i <= 512; i++) {
    REP(j, 100001) {
      if (W[i] <= j) {
        mae[i][j] = max(mae[i / 2][j - W[i]] + V[i], mae[i / 2][j]);
      } else {
        mae[i][j] = mae[i / 2][j];
      }
    }
  }
  cin >> Q;
  int v, L;
  REP(i, Q) {
    MIMII ans;
    MIMIB kst;
    cin >> v >> L;
    cout << dp(v, L, ans, kst) << "\\n";
  }
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    if ((b & s) == s && (b | t) == t) {
      a.emplace_back(b);
    }
  }
  if ((t & s) != s) {
    cout << 0 << '\\n';
    return 0;
  }
  debug(a);
  ll ans = 0;
  n = a.size();
  vector<vector<ll>> C(n + 1, vector<ll>(n + 1));
  for (int i = 0; i <= n; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
    }
  }
  vector<ll> w(n + 1);
  for (int i = 0; i <= n; i++) {
    for (int j = 1; j <= min(k, i); j++) {
      w[i] += C[i][j];
    }
  }
  for (int i = 0; i < (1 << 18); i++) {
    if ((s & i) != s || (t | i) != t) {
      continue;
    }
    ll sgn = 1;
    if (__builtin_popcount(i) % 2 == 1) {
      sgn = -1;
    }
    vector<int> b(n);
    for (int j = 0; j < n; j++) {
      b[j] = a[j] & i;
    }
    sort(b.begin(), b.end());
    int j = 0;
    while (j < n) {
      int l = j;
      while (l + 1 < n && b[l] == b[l + 1]) {
        l++;
      }
      ans += sgn * w[l - j + 1];
      j = l + 1;
    } /*
    for (int j = 0; j < n; j++) {
        int l = j;
        if (j + 1 < n && b[j] == b[j + 1]) {
            j++;
        }
        ans += sgn * w[j - l + 1];
    }*/
  }
  cout << abs(ans) << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  vector<int> a;
  vector<int> x;
  for (int i = 0; i < 18; i++) {
    if (t & (1 << i) && !(s & (1 << i))) {
      x.emplace_back(i);
    }
  }
  int all = 0;
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    if ((b | s) == b && (b | t) == t) {
      int c = 0;
      for (int j = 0; j < x.size(); j++) {
        c *= 2;
        if (b & (1 << x[j])) {
          c++;
        }
      }
      a.emplace_back(c);
    }
  }
  int m = x.size();
  n = a.size();
  s = 0;
  t = (1 << m) - 1;
  vector<map<pair<int, int>, ll>> mp(k + 1);
  mp[0][{0, (1 << m) - 1}] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = k - 1; j >= 0; j--) {
      for (auto e : mp[j]) {
        int p = e.first.first, q = e.first.second;
        mp[j + 1][{p | a[i], q & a[i]}] += e.second;
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i <= k; i++) {
    ans += mp[i][{t, s}];
  }
  cout << ans << '\\n';
  return 0;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, K, S, T;
  cin >> N >> K >> S >> T;
  if ((S & T) != S) {
    cout << 0;
    return 0;
  }
  vector<int> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  vector<vector<ll>> C(N + 1, vector<ll>(N + 1));
  vector<ll> ways(N + 1);
  for (int i = 0; i <= N; ++i) {
    C[i][0] = 1;
    for (int j = 1; j <= i; ++j) {
      C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
      if (j <= K)
        ways[i] += C[i][j];
    }
  }
  ll ans = 0;
  for (int mask = 0; mask < (1 << 18); ++mask) {
    if ((mask & (S ^ T)) != mask)
      continue;
    map<int, int> cnts;
    for (int i = 0; i < N; ++i) {
      if ((A[i] & S) != S)
        continue;
      if ((A[i] | T) != T)
        continue;
      ++cnts[A[i] & mask];
    }
    int cf = __builtin_popcount(mask) & 1 ? -1 : 1;
    for (auto z : cnts)
      ans += cf * ways[z.second];
  }
  cout << ans << "\\n";
}


Error: TLE

Fixed submission:

#pragma GCC optimize("-O3")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int N, K, S, T;
  cin >> N >> K >> S >> T;
  if ((S & T) != S) {
    cout << 0;
    return 0;
  }
  vector<int> A(N);
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  vector<int> cnt(1 << 18);
  for (int i = 0; i < N; ++i) {
    if ((A[i] & S) != S)
      continue;
    if ((A[i] | T) != T)
      continue;
    ++cnt[A[i]];
  }
  vector<char> state(18);
  for (int i = 0; i < 18; ++i) {
    if (S & (1 << i))
      state[i] = 1;
    else if (T & (1 << i))
      state[i] = 2;
  }
  // cerr << "clgt\\n";
  vector<ll> cf(N + 1);
  function<void(int, int, int, int)> brute = [&](int i, int cnt, int orv,
                                                 int andv) {
    if (i == 18) {
      int q = 0;
      for (int z : A)
        q += ((z & andv) == andv) && ((z | orv) == orv);
      cf[q] += (cnt & 1) ? -1 : +1;
      return;
    }
    if (state[i] == 0) {
      brute(i + 1, cnt, orv, andv);
    } else if (state[i] == 1) {
      brute(i + 1, cnt, orv + (1 << i), andv + (1 << i));
    } else {
      brute(i + 1, cnt + 1, orv, andv);
      brute(i + 1, cnt + 1, orv + (1 << i), andv + (1 << i));
      brute(i + 1, cnt, orv + (1 << i), andv);
    }
  };
  brute(0, 0, 0, 0);
  vector<vector<ll>> C(N + 1, vector<ll>(N + 1));
  for (int i = 0; i <= N; ++i) {
    C[i][0] = 1;
    for (int j = 1; j <= i; ++j)
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
  }
  ll ans = 0;
  for (int q = 1; q <= N; ++q) {
    ll z = cf[q];
    for (int i = 1; i <= K; ++i)
      ans += z * C[q][i];
  }
  cout << ans << "\\n";
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int maxN = 55;
const int maxB = 18;
typedef long long ll;

int A[maxN], as[maxB];
ll C[maxN][maxN], ans;
map<int, int> M;

void init() {
  int i, j;
  for (i = 0; i < maxN; ++i)
    for (C[i][0] = C[i][i] = j = 1; j < i; ++j)
      C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
  for (i = 0; i < maxN; ++i)
    for (j = 2; j < maxN; ++j)
      C[i][j] += C[i][j - 1];
}

int main() {
  int n, k, S, T, i, j, c = 0, b, a;
  scanf("%d%d%d%d", &n, &k, &S, &T);
  for (init(), i = 0; i < n; ++i)
    scanf("%d", A + i);
  for (i = 0; i < maxB; ++i) {
    b = 1 << i;
    if ((S & b) && !(T & b)) {
      puts("0");
      return 0;
    }
    if (!(S & b) && (T & b))
      as[c++] = i;
    else {
      for (j = a = 0; j < n; ++j)
        if ((A[j] & b) + (S & b) != b)
          A[a++] = A[j];
      n = a;
    }
  }
  for (b = 0; b < (1 << c); ++b) {
    int sgn = 1, s = 0;
    for (i = 0; i < c; ++i)
      if (b & (1 << i))
        s |= (1 << as[i]), sgn *= -1;
    for (M.clear(), i = 0; i < n; ++i)
      ++M[A[i] & s];
    for (auto it : M)
      ans += sgn * C[it.second][k];
  }
  printf("%lld\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  const int N = 55;
  vector<vector<long long>> C(N, vector<long long>(N, 0));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= i; j++) {
      if (i == j || j == 0)
        C[i][j] = 1;
      else
        C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
    }
  }
  int n, k, S, T;
  cin >> n >> k >> S >> T;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    int b;
    cin >> b;
    if ((b & S) != S)
      continue;
    if ((b | T) != T)
      continue;
    a.push_back(b);
  }
  n = a.size();
  vector<int> bts;
  bool flag = true;
  for (int i = 0; i < 18; i++) {
    if ((S >> i & 1) && (~T >> i & 1))
      flag = false;
    if ((~S >> i & 1) && (T >> i & 1)) {
      bts.push_back(i);
    }
  }
  int m = bts.size();
  vector<long long> nums(m, 0);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      if (a[i] >> bts[j] & 1)
        nums[j] |= 1ll << i;
    }
  }
  long long ans = 0;
  if (flag)
    for (int c = 1; c <= min(n, k); c++) {
      long long tot = C[n][c];
      vector<vector<long long>> st(1 << m);
      st[0].push_back((1ll << n) - 1);
      for (int i = 1; i < (1 << m); i++) {
        int bc = __builtin_popcount(i);
        for (int j = 0; j < m; j++) {
          if (i >> j & 1) {
            for (auto val : st[i ^ (1 << j)]) {
              if (val & nums[j])
                st[i].push_back(val & nums[j]);
              if (val & (~nums[j]))
                st[i].push_back(val & (~nums[j]));
            }
            break;
          }
        }
        long long sgn = 1;
        if (bc & 1)
          sgn = -1;
        for (auto val : st[i]) {
          bc = __builtin_popcountll(val);
          tot += sgn * C[bc][c];
        }
      }
      ans += tot;
    }
  cout << ans << endl;
  return 0;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

constexpr static int MAXN = 50;
constexpr static int MAXA = (1 << 18);

int n, k, S, T;
int a[MAXN], b[MAXN];
int64_t choose[MAXN + 1][MAXN + 1];
int64_t nsubsets[MAXN + 1];

int64_t calc() {
  if ((S | T) != T)
    return 0;

  int m = 0;
  for (int i = 0; i < n; i++)
    if ((a[i] & S) == S && (a[i] | T) == T)
      b[m++] = a[i];

  // for each mask subset, count subsets that have same bits, use PIE
  int64_t total = 0;
  for (int mask = 0; mask < MAXA; mask++) {
    if ((mask | (S ^ T)) != (S ^ T))
      continue;
    map<int, int> cnt;
    for (int i = 0; i < m; i++)
      cnt[b[i] & mask]++;

    int64_t nsame = 0;
    for (auto kv : cnt) {
      // cout << kv.second << " ";
      nsame += nsubsets[kv.second];
    }
    // cout << endl;

    int bits = __builtin_popcount(mask);
    if (bits % 2 == 0)
      total += nsame;
    else
      total -= nsame;
  }
  return total;
}

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

  cin >> n >> k >> S >> T;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  choose[0][0] = 1;
  for (int i = 1; i <= n; i++) {
    choose[i][0] = choose[i][i] = 1;
    for (int j = 1; j < i; j++)
      choose[i][j] = choose[i - 1][j] + choose[i - 1][j - 1];
  }
  for (int i = 1; i <= n; i++) {
    nsubsets[i] = 0;
    for (int j = 1; j <= k && j <= i; j++)
      nsubsets[i] += choose[i][j];
  }

  cout << calc() << endl;
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

constexpr static int MAXN = 50;
constexpr static int MAXA = (1 << 18);

int n, k, S, T;
int a[MAXN], b[MAXN];
int64_t cnt[MAXN][MAXA];

int64_t calc() {
  if ((S | T) != T)
    return 0;

  int m = 0;
  for (int i = 0; i < n; i++)
    if ((a[i] & S) == S && (a[i] | T) == T)
      b[m++] = a[i];

  int64_t total = 0;
  for (int i = 0; i < m; i++) {
    // check feasibility
    int allmask = 0;
    for (int j = i + 1; j < m; j++) {
      int mask = b[j] ^ b[i];
      allmask |= mask;
    }
    if (allmask != (T ^ S))
      continue;
    // DP too slow... this is O(n^3 * |MAXA|)
    for (int num = 0; num < k; num++)
      for (int l = 0; l < MAXA; l++)
        cnt[num][l] = 0;
    cnt[0][0] = 1;
    for (int j = i + 1; j < m; j++) {
      int mask = b[j] ^ b[i];
      for (int num = min(k - 1, j - i); num > 0; num--) {
        for (int l = 0; l < MAXA; l++)
          if (cnt[num - 1][l] > 0)
            cnt[num][l | mask] += cnt[num - 1][l];
      }
    }
    for (int num = 0; num < k; num++)
      total += cnt[num][T ^ S];
  }
  return total;
}

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

  cin >> n >> k >> S >> T;
  for (int i = 0; i < n; i++)
    cin >> a[i];

  cout << calc() << endl;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, K, S, T;
  cin >> N >> K >> S >> T;

  vector<map<pair<int, int>, long long>> dp(K + 1);
  dp[0][{(1 << 18) - 1, 0}] = 1;

  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    if ((a & S) != S)
      continue;
    if ((a | T) != T)
      continue;
    for (int k = K - 1; k >= 0; --k) {
      for (auto &p : dp[k]) {
        int prod, sum;
        tie(prod, sum) = p.first;
        dp[k + 1][{prod & a, sum | a}] += p.second;
      }
    }
  }

  long long ans = 0;
  for (int k = 1; k <= K; ++k) {
    ans += dp[k][{S, T}];
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, K, S, T;
  cin >> N >> K >> S >> T;

  vector<map<pair<int, int>, long long>> dp(K + 1);
  dp[0][{(1 << 18) - 1, 0}] = 1;

  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    for (int k = K - 1; k >= 0; --k) {
      for (auto &p : dp[k]) {
        int prod, sum;
        tie(prod, sum) = p.first;
        dp[k + 1][{prod & a, sum | a}] += p.second;
      }
    }
  }

  long long ans = 0;
  for (int k = 1; k <= K; ++k) {
    ans += dp[k][{S, T}];
  }
  cout << ans << endl;

  return 0;
}
Problem p02649 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

const int N = 51;

int n, k, s, t;
ll C[N][N], ways[N];
vector<int> a;

ll solve(vector<int> &b) {
  ll ret = 0;
  for (int mask = 0; mask <= (t ^ s); mask++) {
    if ((mask & (t ^ s)) != mask)
      continue;
    int sign = (__builtin_popcount(mask) % 2 ? -1 : 1);
    int cnt = 0;
    for (auto &i : b) {
      cnt += (i & mask) == 0;
    }
    ret += sign * ways[cnt];
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  C[0][0] = 1;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < N; j++) {
      C[i][j] = (j == 0 ? 0 : C[i - 1][j - 1]) + C[i - 1][j];
    }
  }
  cin >> n >> k >> s >> t;
  k--;
  if ((s & t) != s)
    finish(0);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if ((x & s) != s)
      continue;
    if ((x | t) != t)
      continue;
    a.push_back(x);
  }
  n = a.size();
  for (int i = 0; i < N; i++) {
    for (int j = 0; j <= k; j++) {
      ways[i] += C[i][j];
    }
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    vector<int> b;
    for (int j = i + 1; j < n; j++) {
      b.push_back(a[i] ^ a[j]);
    }
    ans += solve(b);
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx")

#include <bits/stdc++.h>
using namespace std;
#define finish(x) return cout << x << endl, 0
#define ll long long

#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;

struct chash {
  const int RANDOM =
      (long long)(make_unique<char>().get()) ^
      chrono::high_resolution_clock::now().time_since_epoch().count();
  static unsigned long long hash_f(unsigned long long x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  static unsigned hash_combine(unsigned a, unsigned b) { return a * 31 + b; }
  int operator()(int x) const { return hash_f(x) ^ RANDOM; }
};

int n, k, s, t;
vector<int> a;
gp_hash_table<int, ll, chash> dp[51];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  srand(time(0));
  cin >> n >> k >> s >> t;
  if ((s & t) != s)
    finish(0);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    if ((x & s) != s)
      continue;
    if ((x | t) != t)
      continue;
    a.push_back(x);
  }
  n = a.size();
  vector<int> bs;
  for (int i = 0; i < 18; i++) {
    if (((t >> i) & 1) && (((s >> i) & 1) == 0))
      bs.push_back(i);
  }
  int all = 0;
  for (auto &i : bs)
    all |= (1 << i);
  ll ans = 0;
  random_shuffle(a.begin(), a.end());
  for (int i = 0; i < n; i++) {
    dp[1][0] = 1;
    for (int j = i + 1; j < n; j++) {
      for (int sz = k - 1; sz >= 0; sz--) {
        for (auto &h : dp[sz]) {
          dp[sz + 1][h.first | (a[j] ^ a[i])] += h.second;
        }
      }
    }
    for (int sz = 1; sz <= k; sz++) {
      ans += dp[sz][all];
      dp[sz].clear();
    }
  }
  cout << ans << endl;
}