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

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

const int N = 55, L = 18, K = 14000000;

int n, k, len, s, t, a[N], popcount[1 << L], cnt = 0, tot = 0;
pair<int, int> vec[K];
unsigned long long coef[N], c[N][N], ans = 0ull;

int lowbit(int x) { return x & -x; }

int main() {
  scanf("%d%d%d%d", &n, &k, &s, &t);
  len = 0;
  if ((s & t) != s) {
    puts("0");
    return 0;
  }
  for (int i = (t ^ s); i; i ^= lowbit(i))
    len++;

  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if ((x & s) != s)
      continue;
    if ((x & t) != x)
      continue;
    a[cnt] = 0;
    for (int j = (t ^ s), k = 0; j; j ^= lowbit(j), k++) {
      if (x & lowbit(j))
        a[cnt] |= (1 << k);
    }
    cnt++;
  }

  int U = (1 << len) - 1;
  popcount[0] = 0;
  for (int i = 1; i <= U; i++)
    popcount[i] = popcount[i >> 1] + (i & 1);
  for (int i = 0; i < cnt; i++) {
    int b = U ^ a[i];
    for (int j = a[i];; j = (j - 1) & a[i]) {
      for (int k = b;; k = (k - 1) & b) {
        vec[tot++] = make_pair(j, a[i] | k);
        if (!k)
          break;
      }
      if (!j)
        break;
    }
  }
  for (int i = 0; i <= n; i++)
    coef[i] = 0ull, c[i][0] = c[i][i] = 1ull;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++)
      c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
  }
  sort(vec, vec + tot);
  for (int i = 0, j = 0; i < tot; i++) {
    if (i && vec[i] != vec[i - 1])
      j = 0;
    j++;
    if (i == tot - 1 || vec[i] != vec[i + 1]) {
      if ((popcount[vec[i].first] + popcount[vec[i].second] + len) & 1)
        coef[j]--;
      else
        coef[j]++;
    }
  }

  for (int i = 1; i <= k; i++) {
    for (int j = i; j <= n; j++)
      ans += coef[j] * c[j][i];
  }
  printf("%llu\\n", ans);
  return 0;
}

Error: 0

Fixed submission:

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

const int N = 55, L = 18, K = 1400000;

int n, k, len, s, t, a[N], popcount[1 << L], cnt = 0, tot = 0;
pair<int, int> vec[K];
unsigned long long coef[N], c[N][N], ans = 0ull;

int lowbit(int x) { return x & -x; }

int main() {
  clock_t t_s = clock();
  scanf("%d%d%d%d", &n, &k, &s, &t);
  len = 0;
  if ((s & t) != s) {
    puts("0");
    return 0;
  }
  for (int i = (t ^ s); i; i ^= lowbit(i))
    len++;

  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    if ((x & s) != s)
      continue;
    if ((x & t) != x)
      continue;
    a[cnt] = 0;
    for (int j = (t ^ s), k = 0; j; j ^= lowbit(j), k++) {
      if (x & lowbit(j))
        a[cnt] |= (1 << k);
    }
    cnt++;
  }

  int U = (1 << len) - 1;
  popcount[0] = 0;
  for (int i = 1; i <= U; i++)
    popcount[i] = popcount[i >> 1] + (i & 1);
  for (int i = 0; i < cnt; i++) {
    int b = U ^ a[i];
    for (int j = a[i];; j = (j - 1) & a[i]) {
      for (int k = b;; k = (k - 1) & b) {
        vec[tot++] = make_pair(j, a[i] | k);
        if (!k)
          break;
      }
      if (!j)
        break;
    }
  }
  for (int i = 0; i <= n; i++)
    coef[i] = 0ull, c[i][0] = c[i][i] = 1ull;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++)
      c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
  }
  sort(vec, vec + tot);
  for (int i = 0, j = 0; i < tot; i++) {
    if (i && vec[i] != vec[i - 1])
      j = 0;
    j++;
    if (i == tot - 1 || vec[i] != vec[i + 1]) {
      if ((popcount[vec[i].first] + popcount[vec[i].second] + len) & 1)
        coef[j]--;
      else
        coef[j]++;
    }
  }

  for (int i = 1; i <= k; i++) {
    for (int j = i; j <= n; j++)
      ans += coef[j] * c[j][i];
  }
  printf("%llu\\n", ans);
  return 0;
}