Problem p02649 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
const int N = 51, MAXN = 387420489, LOG = 18;
int pw[LOG] = {1}, dp[1 << LOG], a[N];
long long C[N][N], F[N], ans;
char cnt[MAXN];

int bit(int a, int i) { return (a >> i) & 1; }
void BT(int i, int mask, int c) {
  if (i == -1) {
    if (c % 2 == 0)
      ans += F[(int)cnt[mask]];
    else
      ans -= F[(int)cnt[mask]];
    return;
  }
  for (int j = 0; j < 3; j++)
    BT(i - 1, mask + j * pw[i], c + (j > 0));
  return;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, k, A, O;
  cin >> n >> k >> A >> O;
  for (int i = 1; i < LOG; i++)
    pw[i] = pw[i - 1] * 3;
  for (int i = 0; i < N; i++)
    C[i][0] = 1;
  for (int i = 1; i < N; i++)
    for (int j = 1; j < N; j++)
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= min(i, k); j++)
      F[i] += C[i][j];
  // --------------------------------------------------------------------
  for (int i = 0; i < LOG; i++)
    if (bit(A, i) && (!bit(O, i)))
      return cout << 0 << "\\n", 0;
  vector<int> vec, B;
  for (int i = 0; i < n; i++) {
    int x, val = 1;
    cin >> x;
    for (int j = 0; j < LOG; j++) {
      if (bit(x, j)) {
        if (!bit(O, j))
          val = 0;
      } else {
        if (bit(A, j))
          val = 0;
      }
    }
    if (val)
      vec.push_back(x);
  }
  n = vec.size();
  for (int i = 0; i < LOG; i++)
    if (bit(O, i) && (!bit(A, i)))
      B.push_back(i);
  int log = B.size();
  if (log > 18)
    assert(0);
  for (int i = 0; i < n; i++) {
    for (int j = log - 1; j >= 0; j--)
      a[i] = (a[i] << 1) | bit(vec[i], B[j]);
    memset(dp, 0, sizeof dp), cnt[0]++;
    for (int mask = 1; mask < (1 << log); mask++) {
      int b = __builtin_ctz(mask);
      dp[mask] = dp[mask ^ (1 << b)] + pw[b] * (bit(a[i], b) + 1);
      cnt[dp[mask]]++;
    }
  }
  BT(log - 1, 0, 0);
  cout << ans << "\\n";
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const int N = 51, MAXN = 387420289, LOG = 18;
int pw[LOG] = {1}, dp[1 << LOG], a[N];
long long C[N][N], F[N], ans;
char cnt[MAXN];

int bit(int a, int i) { return (a >> i) & 1; }
void BT(int i, int mask, int c) {
  if (mask < 0 || mask >= MAXN)
    assert(0);
  if (i == -1) {
    if (c % 2 == 0)
      ans += F[(int)cnt[mask]];
    else
      ans -= F[(int)cnt[mask]];
    return;
  }
  for (int j = 0; j < 3; j++)
    BT(i - 1, mask + j * pw[i], c + (j > 0));
  return;
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n, k, A, O;
  cin >> n >> k >> A >> O;
  for (int i = 1; i < LOG; i++)
    pw[i] = pw[i - 1] * 3;
  for (int i = 0; i < N; i++)
    C[i][0] = 1;
  for (int i = 1; i < N; i++)
    for (int j = 1; j < N; j++)
      C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= min(i, k); j++)
      F[i] += C[i][j];
  // --------------------------------------------------------------------
  for (int i = 0; i < LOG; i++)
    if (bit(A, i) && (!bit(O, i)))
      return cout << 0 << "\\n", 0;
  vector<int> vec, B;
  for (int i = 0; i < n; i++) {
    int x, val = 1;
    cin >> x;
    for (int j = 0; j < LOG; j++) {
      if (bit(x, j)) {
        if (!bit(O, j))
          val = 0;
      } else {
        if (bit(A, j))
          val = 0;
      }
    }
    if (val)
      vec.push_back(x);
  }
  n = vec.size();
  for (int i = 0; i < LOG; i++)
    if (bit(O, i) && (!bit(A, i)))
      B.push_back(i);
  int log = B.size();
  for (int i = 0; i < n; i++) {
    for (int j = log - 1; j >= 0; j--)
      a[i] = (a[i] << 1) | bit(vec[i], B[j]);
    memset(dp, 0, sizeof dp), cnt[0]++;
    for (int mask = 1; mask < (1 << log); mask++) {
      int b = __builtin_ctz(mask);
      dp[mask] = dp[mask ^ (1 << b)] + pw[b] * (bit(a[i], b) + 1);
      cnt[dp[mask]]++;
    }
  }
  BT(log - 1, 0, 0);
  cout << ans << "\\n";
}