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

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

const int N = 55;
int a[N], n;
ll c[N][N], sc[N][N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  for (int i = 0; i < 18; i++)
    if ((s & (1 << i)) == 1 && (t & (1 << i)) == 0) {
      cout << "0\\n";
      return 0;
    }
  c[0][0] = 1;
  for (int i = 1; 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];
  }
  for (int i = 0; i <= n; ++i) {
    sc[i][0] = 0;
    for (int j = 1; j <= k; ++j)
      sc[i][j] = sc[i][j - 1] + c[i][j];
  }
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  {
    static int b[N];
    for (int i = 1; i <= n; ++i)
      b[i] = a[i];
    int tot = 0;
    for (int i = 1; i <= n; ++i) {
      bool flg = 1;
      for (int j = 0; j < 18; ++j) {
        if ((s & (1 << j)) == (t & (1 << j)) &&
            (a[i] & (1 << j)) != (s & (1 << j))) {
          flg = false;
          break;
        }
      }
      if (flg)
        b[++tot] = a[i];
    }
    n = tot;
    for (int i = 1; i <= n; ++i)
      a[i] = b[i];
  }
  ll ans = 0;
  for (int mask = s ^ t;; mask = (mask - 1) & (s ^ t)) {
    static int cnt[1 << 19];
    for (int i = 1; i <= n; ++i) {
      ++cnt[a[i] & mask];
    }
    ll tmp = 0;
    for (int i = 1; i <= n; ++i) {
      tmp += sc[cnt[a[i] & mask]][k];
      cnt[a[i] & mask] = 0;
    }
    if (__builtin_popcount(mask) & 1)
      ans -= tmp;
    else
      ans += tmp;
    if (!mask)
      break;
  }
  cout << ans << '\\n';
  return 0;
}

Error: 0

Fixed submission:

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

const int N = 55;
int a[N], n;
ll c[N][N], sc[N][N];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  for (int i = 0; i < 18; i++)
    if ((s & (1 << i)) == 1 && (t & (1 << i)) == 0) {
      cout << "0\\n";
      return 0;
    }
  c[0][0] = 1;
  for (int i = 1; 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];
  }
  for (int i = 0; i <= n; ++i) {
    sc[i][0] = 0;
    for (int j = 1; j <= k; ++j)
      sc[i][j] = sc[i][j - 1] + c[i][j];
  }
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  {
    static int b[N];
    for (int i = 1; i <= n; ++i)
      b[i] = a[i];
    int tot = 0;
    for (int i = 1; i <= n; ++i) {
      bool flg = 1;
      for (int j = 0; j < 18; ++j) {
        if ((s & (1 << j)) == (t & (1 << j)) &&
            (a[i] & (1 << j)) != (s & (1 << j))) {
          flg = false;
          break;
        }
      }
      if (flg)
        b[++tot] = a[i];
    }
    n = tot;
    for (int i = 1; i <= n; ++i)
      a[i] = b[i];
  }
  ll ans = 0;
  for (int mask = s ^ t;; mask = (mask - 1) & (s ^ t)) {
    static int cnt[N];
    for (int i = 1; i <= n; ++i) {
      ++cnt[a[i] & mask];
    }
    ll tmp = 0;
    for (int i = 1; i <= n; ++i) {
      tmp += sc[cnt[a[i] & mask]][k];
      cnt[a[i] & mask] = 0;
    }
    if (__builtin_popcount(mask) & 1)
      ans -= tmp;
    else
      ans += tmp;
    if (!mask)
      break;
  }
  cout << ans << '\\n';
  return 0;
}