Problem p02649 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI 3.14159265359
using namespace std;
using ll = long long;
using P = pair<int, int>;

int cnt[1000000], cntid[50];
ll c[50][50], cc[50][50];

int main() {
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  rep(i, 1000000) { cnt[i] = 0; }
  vector<int> atmp(n);
  rep(i, n) cin >> atmp[i];

  rep(i, n + 1) rep(j, n + 1) c[i][j] = 0;
  rep(i, n + 1) {
    c[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
    }
  }
  rep(i, n + 1) {
    cc[i][0] = 0;
    for (int j = 1; j <= n; j++) {
      cc[i][j] = cc[i][j - 1] + c[i][j];
    }
  }

  // if si = 1 and ti = 0, no set meets the condition;
  // if si = 0 and ti = 1, the i-th bit is to be considered;
  vector<int> bit;
  int maxb = -1;
  rep(i, 20) {
    int si = (s >> i) & 1, ti = (t >> i) & 1;
    if (si == 1 && ti == 0) {
      cout << 0 << endl;
      return 0;
    } else if (si == 0 && ti == 1) {
      bit.push_back(i);
      maxb++;
    }
  }

  // if aij = 0 and sj = 1 or aij = 1 and tj = 0, ai is not included in any set;
  // only bits included in bit are added to a;
  vector<int> a;
  rep(i, n) {
    int ai = atmp[i];
    bool flug = true;
    rep(j, 20) {
      int aij = (ai >> j) & 1, sj = (s >> j) & 1, tj = (t >> j) & 1;
      if ((aij == 0 && sj == 1) || (aij == 1 && tj == 0)) {
        flug = false;
      }
    }
    if (flug) {
      a.push_back(0);
      rep(j, bit.size()) {
        int aij = ai & (1 << bit[j]);
        if (aij) {
          maxb = max(maxb, j);
          a[a.size() - 1] += 1 << j;
        }
      }
    }
  }
  if (a.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  /*
  cout << "bit";
  rep(i, bit.size()) cout << " " << bit[i]; cout << endl;
  cout << "a ";
  rep(i, a.size()) cout << a[i] << " "; cout << endl; cout << "maxb " << maxb <<
  endl;
  */
  ll ans = 0;
  rep(i, 1 << (maxb + 1)) {
    int one = 0;
    rep(l, maxb + 1) one += (i >> l) & 1;
    // cout << "one " << one << endl;
    rep(l, a.size()) {
      int ani = a[l] & i;
      cnt[ani]++;
      cntid[l] = ani;
    }
    // cout << "i:" << i << ":"; rep(l, 1<<(maxb + 1)) cout << " " << cnt[l];
    // cout << endl;
    ll co = (one % 2 == 0) ? 1 : -1;
    rep(l, a.size()) {
      // choose at most k element from a set of size cnt[cntid[l]];
      ans += co * cc[cnt[cntid[l]]][k];
      cnt[cntid[l]] = 0;
    }
  }

  cout << ans << endl;

  return 0;
}

/*
 */


Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define PI 3.14159265359
using namespace std;
using ll = long long;
using P = pair<int, int>;

int cnt[1000000], cntid[50];
ll c[51][51], cc[51][51];

int main() {
  int n, k, s, t;
  cin >> n >> k >> s >> t;
  rep(i, 1000000) { cnt[i] = 0; }
  vector<int> atmp(n);
  rep(i, n) cin >> atmp[i];

  rep(i, n + 1) rep(j, n + 1) c[i][j] = 0;
  rep(i, n + 1) {
    c[i][0] = 1;
    for (int j = 1; j <= i; j++) {
      c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
    }
  }
  rep(i, n + 1) {
    cc[i][0] = 0;
    for (int j = 1; j <= n; j++) {
      cc[i][j] = cc[i][j - 1] + c[i][j];
    }
  }

  // if si = 1 and ti = 0, no set meets the condition;
  // if si = 0 and ti = 1, the i-th bit is to be considered;
  vector<int> bit;
  int maxb = -1;
  rep(i, 20) {
    int si = (s >> i) & 1, ti = (t >> i) & 1;
    if (si == 1 && ti == 0) {
      cout << 0 << endl;
      return 0;
    } else if (si == 0 && ti == 1) {
      bit.push_back(i);
      maxb++;
    }
  }

  // if aij = 0 and sj = 1 or aij = 1 and tj = 0, ai is not included in any set;
  // only bits included in bit are added to a;
  vector<int> a;
  rep(i, n) {
    int ai = atmp[i];
    bool flug = true;
    rep(j, 20) {
      int aij = (ai >> j) & 1, sj = (s >> j) & 1, tj = (t >> j) & 1;
      if ((aij == 0 && sj == 1) || (aij == 1 && tj == 0)) {
        flug = false;
      }
    }
    if (flug) {
      a.push_back(0);
      rep(j, bit.size()) {
        int aij = ai & (1 << bit[j]);
        if (aij) {
          maxb = max(maxb, j);
          a[a.size() - 1] += 1 << j;
        }
      }
    }
  }
  if (a.size() == 0) {
    cout << 0 << endl;
    return 0;
  }
  /*
  cout << "bit";
  rep(i, bit.size()) cout << " " << bit[i]; cout << endl;
  cout << "a ";
  rep(i, a.size()) cout << a[i] << " "; cout << endl; cout << "maxb " << maxb <<
  endl;
  */
  ll ans = 0;
  rep(i, 1 << (maxb + 1)) {
    int one = 0;
    rep(l, maxb + 1) one += (i >> l) & 1;
    // cout << "one " << one << endl;
    rep(l, a.size()) {
      int ani = a[l] & i;
      cnt[ani]++;
      cntid[l] = ani;
    }
    // cout << "i:" << i << ":"; rep(l, 1<<(maxb + 1)) cout << " " << cnt[l];
    // cout << endl;
    ll co = (one % 2 == 0) ? 1 : -1;
    rep(l, a.size()) {
      // choose at most k element from a set of size cnt[cntid[l]];
      ans += co * cc[cnt[cntid[l]]][k];
      cnt[cntid[l]] = 0;
    }
  }

  cout << ans << endl;

  return 0;
}

/*
 */