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

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int LOG = 18;
const int MAXN = 53;

LL nCr[MAXN][MAXN];

LL f(int n, int k) {
  LL ans = 0;
  for (int i = 1; i <= n && i <= k; i++) {
    ans += nCr[n][i];
  }
  return ans;
}

int main() {
  nCr[0][0] = 1;
  for (int n = 1; n < MAXN; n++) {
    nCr[n][0] = 1;
    for (int r = 1; r <= n; r++) {
      nCr[n][r] = nCr[n - 1][r] + nCr[n - 1][r - 1];
    }
  }

  int n, k, s, t;
  cin >> n >> k >> s >> t;

  if ((s & t) != s) {
    cout << 0 << endl;
    return 0;
  }

  vector<int> A;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;

    bool okay = true;
    int y = 0;
    for (int j = 0, l = 0; j < LOG && okay; j++) {
      int _x = (x >> j) & 1;
      int _s = (s >> j) & 1;
      int _t = (t >> j) & 1;

      if (_s == 1 && _x == 0)
        okay = false;
      else if (_t == 0 && _x == 1)
        okay = false;
      else if (_s == 0 && _t == 1) {
        y |= _x << l;
        l++;
      }
    }

    if (okay) {
      //            cout << x << " -> " << y << endl;
      A.push_back(y);
    }
  }

  int L = 0;
  for (int j = 0; j < LOG; j++) {
    int _s = (s >> j) & 1;
    int _t = (t >> j) & 1;
    L += (_s == 0) && (_t == 1);
  }

  //    cout << "L : " << L << endl;

  LL ans = 0;
  for (int msk = 0; msk < (1 << L); msk++) {
    vector<int> B(A);
    for (int &x : B)
      x &= msk;
    sort(B.begin(), B.end());
    //        cout << "mask: " << bitset<2>(msk) << endl;
    //        for (int u : B) cout << u << " ";
    //        cout << endl;

    LL z = 0;
    for (int i = 0; i < B.size(); i++) {
      int j = i;
      while (j + 1 < B.size() && B[j + 1] == B[i])
        j++;
      z += f(j - i + 1, k);
      i = j;
    }

    if (__builtin_popcount(msk) & 1)
      ans -= z;
    else
      ans += z;
  }

  cout << ans << endl;

  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 50;
const int LOG = 18;

int A[MAXN];
vector<int> B;
int n, k, s, t;
int l;

void filter() {
  for (int i = 0; i < n; i++) {
    bool okay = true;
    int x = 0;
    for (int j = 0, l = 0; j < LOG; j++) {
      int _i = (A[i] >> j) & 1;
      int _s = (s >> j) & 1;
      int _t = (t >> j) & 1;

      if (_s == 1 && _i == 0) {
        okay = false;
        break;
      }
      if (_t == 0 && _i == 1) {
        okay = false;
        break;
      }

      if (_s == 0 && _t == 1) {
        x |= _i << l;
        l++;
      }
    }

    if (okay) {
      //            cout << "take " << i << " as " << x << endl;
      B.push_back(x);
    }
  }
}

int p3[LOG + 1];
// int getIndex(const vector< int >&w) {
//     int ans = 0;
//     for (int i = w.size()-1; i >= 0; i--) {
//         ans = ans*3+w[i];
//     }
//     return ans;
// }

// int replaceIndex(vector< int >&w, int idx, int p, int r) {
//     return idx+(r-w[p])*p3[p];
// }

int target;
LL dp[MAXN + 1][MAXN + 1][20000];

LL solve(int i, int taken, vector<int> w, int idx) {
  if (taken > k)
    return 0;
  if (i == B.size())
    return idx == target;
  LL &ans = dp[i][taken][idx];
  if (ans != -1)
    return ans;
  ans = solve(i + 1, taken, w, idx);

  int newidx = idx;
  for (int j = 0; j < l; j++) {
    if (B[i] & (1 << j)) {
      if (w[j] == 0) {
        w[j] = 2;
        newidx += 2 * p3[j];
      }
    } else {
      if (w[j] == 1) {
        w[j] = 2;
        newidx += p3[j];
      }
    }
  }

  ans += solve(i + 1, taken + 1, w, newidx);

  return ans;
}

void calcVariable() {
  target = 0;
  l = 0;
  for (int j = 0; j < LOG; j++) {
    int _s = (s >> j) & 1;
    int _t = (t >> j) & 1;
    if (_s == 0 && _t == 1) {
      target += 2 * p3[l];
      l++;
    }
  }
}

int main() {
  p3[0] = 1;
  for (int i = 1; i <= LOG; i++) {
    p3[i] = p3[i - 1] * 3;
  }

  cin >> n >> k >> s >> t;
  for (int i = 0; i < n; i++)
    cin >> A[i];

  if ((s & t) != s) {
    cout << 0 << endl;
    return 0;
  }

  calcVariable();
  //    cout << "l = " << l << endl;
  if (l > LOG / 2) {
    int msk = (1 << LOG) - 1;
    for (int i = 0; i < n; i++)
      A[i] ^= msk;
    s ^= msk, t ^= msk;
    swap(s, t);
    calcVariable();
    assert(l <= LOG / 2);
  }

  //    cout << "l = " << l << endl;

  filter();

  if (B.empty()) {
    cout << 0 << endl;
    return 0;
  }

  int m = B.size();
  memset(dp, -1, sizeof dp);

  LL ans = 0;
  for (int i = 0; i < m; i++) {
    vector<int> w(l, 0);
    int idx = 0;
    for (int j = 0; j < l; j++) {
      w[j] = (B[i] >> j) & 1;
      idx += w[j] * p3[j];
    }
    ans += solve(i + 1, 1, w, idx);
  }

  cout << ans << endl;

  return 0;
}