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

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

ll comb[53][53];
ll combsum[53][53];
void make_table() {
  for (ll i = 0; i <= 51; i++) {
    comb[i][0] = 1;
    comb[i][i] = 1;
  }
  for (ll i = 1; i <= 51; i++)
    for (ll j = 1; j < i; j++) {
      comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]);
    }
}

ll cnt0[(1 << 18)];
ll cnt1[(1 << 18)];
ll pcnt[(1 << 18)], sgn[(1 << 18)];

ll bitv(ll a, ll n) { return (a >> n) & 1LL; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  make_table();
  rep(i, 52) {
    rep(j, 52) { combsum[i][j + 1] = combsum[i][j] + comb[i][j]; }
  }

  const ll B = 18;
  rep(i, 1 << B) {
    pcnt[i] = bcnt(i);
    if (pcnt[i] % 2 == 1)
      sgn[i] = -1;
    else
      sgn[i] = +1;
  }

  ll N, K, S, T;
  cin >> N >> K >> S >> T;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<ll> tmp;
  ll mask = (1 << B) - 1;
  rep(j, B) {
    if (bitv(S, j) == 1 && bitv(T, j) == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  rep(j, B) {
    if (bitv(S, j) == 1 || bitv(T, j) == 0) {
      mask ^= (1LL << j);
    }
  }
  // dbg(mask);
  rep(i, N) {
    bool ok = true;
    rep(j, B) {
      if (bitv(S, j) == 1 && bitv(A[i], j) == 0) {
        ok = false;
      }
      if (bitv(T, j) == 0 && bitv(A[i], j) == 1) {
        ok = false;
      }
    }
    if (ok) {
      tmp.push_back(A[i]);
    }
  }
  A = tmp;
  N = A.size();
  // dbg(N);

  rep(i, N) A[i] = (A[i] & mask);

  ll res = 0;
  rep(U, 1 << B) {
    if ((U | mask) != mask)
      continue;
    map<ll, ll> cnt;
    rep(i, N) {
      ll s = (U & A[i]);
      cnt[s]++;
    }
    for (P p : cnt) {
      res += (combsum[p.se][K + 1] - 1) * sgn[U];
    }
  }
  cout << res << endl;

  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define mmax(x, y) (x > y ? x : y)
#define mmin(x, y) (x < y ? x : y)
#define maxch(x, y) x = mmax(x, y)
#define minch(x, y) x = mmin(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt __builtin_popcountll

#define INF 1e16
#define mod 1000000007

ll comb[53][53];
ll combsum[53][53];
void make_table() {
  for (ll i = 0; i <= 51; i++) {
    comb[i][0] = 1;
    comb[i][i] = 1;
  }
  for (ll i = 1; i <= 51; i++)
    for (ll j = 1; j < i; j++) {
      comb[i][j] = (comb[i - 1][j - 1] + comb[i - 1][j]) % mod;
    }
}

ll cnt0[(1 << 18)];
ll cnt1[(1 << 18)];
ll pcnt[(1 << 18)], sgn[(1 << 18)];

inline ll bitv(ll a, ll n) { return (a >> n) & 1LL; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  make_table();
  rep(i, 52) {
    combsum[i][0] = comb[i][0];
    rep(j, 51) { combsum[i][j + 1] = combsum[i][j] + comb[i][j + 1]; }
  }

  const ll B = 18;
  rep(i, 1 << B) {
    pcnt[i] = bcnt(i);
    if (pcnt[i] % 2 == 1)
      sgn[i] = -1;
    else
      sgn[i] = +1;
  }

  ll N, K, S, T;
  cin >> N >> K >> S >> T;
  vector<ll> A(N);
  rep(i, N) cin >> A[i];
  vector<ll> tmp;
  ll mask = (1 << B) - 1;
  rep(j, B) {
    if (bitv(S, j) == 1 && bitv(T, j) == 0) {
      cout << 0 << endl;
      return 0;
    }
  }
  rep(j, B) {
    if (bitv(S, j) == 1 || bitv(T, j) == 0) {
      mask ^= (1LL << j);
    }
  }
  // dbg(mask);
  rep(i, N) {
    bool ok = true;
    rep(j, B) {
      if (bitv(S, j) == 1 && bitv(A[i], j) == 0) {
        ok = false;
      }
      if (bitv(T, j) == 0 && bitv(A[i], j) == 1) {
        ok = false;
      }
    }
    if (ok) {
      tmp.push_back(A[i]);
    }
  }
  A = tmp;
  N = A.size();
  // dbg(N);

  rep(S0, 1 << B) {
    if ((S0 | mask) != mask)
      continue;
    rep(i, N) {
      ll ax = A[i] ^ mask;
      if ((S0 | ax) == ax)
        cnt0[S0] |= (1 << i);
    }
  }

  rep(S1, 1 << B) {
    if ((S1 | mask) != mask)
      continue;
    rep(i, N) {
      if ((S1 | A[i]) == A[i])
        cnt1[S1] |= (1 << i);
    }
  }

  ll res = 0;
  rep(U, 1 << B) {
    if ((U | mask) != mask)
      continue;
    for (ll U1 = U; U1 >= 0; U1--) {
      if (U1 < 0)
        break;
      U1 &= U;

      // dbg(U);
      ll U2 = U ^ U1;
      // dbg(U1); dbg(U2);
      ll cpcnt = pcnt[cnt0[U1] & cnt1[U2]];
      // dbg(cnt0[U1]); dbg(cnt1[U2]);
      ll add = combsum[cpcnt][K] - 1;
      // dbg(cpcnt);
      // dbg(add);
      res += add * sgn[U];
    }
  }
  cout << res << endl;

  return 0;
}