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

#include "bits/stdc++.h"
#include <algorithm>
#include <assert.h>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
using ll = long long int;

using Graph = vector<vector<int>>;

#define WHATS(var) // cout<<__LINE__<<' '<<#var<<"="<<var<<endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "( " << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<set<T>> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  int i = 0;
  for (auto it : v) {
    if (i > 0) {
      os << ' ';
    }
    os << it;
    i++;
  }
  return os;
}

#include <random>
const int mod = 1e9 + 7;

vector<vector<ll>> memo(51, vector<ll>(51));
vector<vector<ll>> sums(51, vector<ll>(51));

void init() {
  for (int i = 1; i <= 50; ++i) {
    for (int j = 1; j <= i; ++j) {
      ll answer = 1;
      for (int k = 0; k < j; ++k) {
        answer *= i - k;
        answer /= k + 1;
      }
      memo[i][j] = answer;
    }
  }
  for (int i = 0; i <= 50; ++i) {
    for (int j = 1; j <= 50; ++j) {
      if (j != 1)
        sums[i][j] += sums[i][j - 1];
      sums[i][j] += memo[i][j];
    }
  }
  // WHATS(sums)
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie();
  init();
  int N, K, S, T;
  cin >> N >> K >> S >> T;

  vector<int> t_on_places;
  for (int i = 0; i < 18; ++i) {
    if ((T & (1 << i)) && (!(S & (1 << i))))
      t_on_places.push_back(i);
  }
  WHATS(t_on_places)
  vector<int> as;
  int aN = N;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    if (((a & S) != S) || ((a & T) != a)) {
      aN--;
    } else {
      as.push_back(a);
    }
  }
  N = aN;
  WHATS(as)
  N = int(as.size());

  ll answer = 0;
  if ((S & T) != S) {

  } else {
    vector<vector<ll>> vs(t_on_places.size(), vector<ll>(2));

    for (int i = 0; i < N; ++i) {
      auto kk = as[i] ^ S;

      for (int j = 0; j < t_on_places.size(); ++j) {
        int xx = t_on_places[j];
        if ((kk >> xx) & 1) {
          vs[j][0] ^= 1ll << i;
        } else {
          vs[j][1] ^= 1ll << i;
        }
      }
    }

    for (int id = 0; id < 1 << (t_on_places.size()); ++id) {
      bool flag = __builtin_popcount(id) % 2;

      vector<ll> nums(1, (1ll << N) - 1);
      for (int j = 0; j < t_on_places.size(); ++j) {
        vector<ll> nnums;
        if (id & (1 << j)) {
          for (auto nu : nums) {
            ll l_use = nu & vs[j][0];
            ll r_use = nu & vs[j][1];
            assert((l_use ^ r_use) == nu);
            assert(!(l_use & r_use));
            // WHATS(l_use)
            // WHATS(r_use)
            if (l_use) {
              nnums.push_back(l_use);
            }
            if (r_use) {
              nnums.push_back(r_use);
            }
          }
          nums = nnums;
        }
      }
      WHATS(id)
      WHATS(nums)
      ll plus = 0;
      for (auto nu : nums) {
        plus += sums[__builtin_popcountll(nu)][K];
      }
      if (flag)
        answer += plus;
      else
        answer -= plus;
      WHATS(plus)
      WHATS(answer)
    }
    WHATS(vs)
  }
  answer = abs(answer);
  cout << answer << endl;

  return 0;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <algorithm>
#include <assert.h>
#include <complex>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;
using ll = long long int;

using Graph = vector<vector<int>>;

#define WHATS(var) // cout<<__LINE__<<' '<<#var<<"="<<var<<endl;

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "( " << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<set<T>> &v) {
  for (int i = 0; i < v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const set<T> &v) {
  int i = 0;
  for (auto it : v) {
    if (i > 0) {
      os << ' ';
    }
    os << it;
    i++;
  }
  return os;
}
int say(vector<int> v) {
  assert(!v.empty());
  cout << "? " << v.size();
  for (auto x : v) {
    cout << ' ' << x + 1;
  }
  cout << endl;
  ll num;
  cin >> num;
  return num;
}
void say2(vector<ll> v) {
  cout << "!";
  for (auto x : v) {
    cout << ' ' << x;
  }
  cout << endl;
}
#include <random>
const int mod = 1e9 + 7;

void solveB() {
  int N;
  cin >> N;
  string st;
  cin >> st;
  vector<int> nums(2, 0);
  int cnt = 0;
  vector<int> nex_connects;
  for (int i = 0; i < N; ++i) {
    if (st[i] == 'G') {
      cnt++;
    } else {
      nums.push_back(cnt);
      cnt = 0;
    }
  }
  int sum = count(st.begin(), st.end(), 'G');
  nums.push_back(cnt);
  cnt = 0;
  nums.push_back(0);
  nums.push_back(0);
  int answer = 0;
  for (int l = 2; l < nums.size() - 2; ++l) {
    answer = max(answer, nums[l] + nums[l + 1] + 1);
  }
  answer = min(answer, sum);
  cout << answer << endl;
}
void solveC() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> powers(M);
  for (int i = 0; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    a--;
    powers[a].push_back(b);
  }
  for (auto &po : powers) {
    sort(po.begin(), po.end(), greater<int>());
  }
  sort(powers.begin(), powers.end(),
       [](const vector<int> &l, const vector<int> &r) {
         return l.size() > r.size();
       });

  vector<int> sums(M);
  int total = 0;
  int answer = 0;
  for (int use = 1; use <= N; ++use) {
    for (int id = 0; id < M; ++id) {
      if (powers[id].size() + 1 == use) {
        total -= sums[id];

        sums[id] = 0;
      } else if (powers[id].size() + 1 < use) {

        break;
      } else {
        total -= sums[id];
        sums[id] += powers[id][use - 1];
        sums[id] = max(sums[id], 0);
        total += sums[id];
      }
    }
    answer = max(answer, total);
  }
  cout << answer << endl;
}
void solveD() {
  int N;
  cin >> N;
  map<int, vector<pair<int, int>>> mp;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    int xa = min(2, a);
    mp[xa].push_back(make_pair(a, i));
  }
  auto two_vs = mp[2];
  auto one_vs = mp[1];

  Graph g(N);
  vector<pair<int, int>> edges;
  for (int i = 0; i < int(two_vs.size()) - 1; ++i) {
    edges.emplace_back(two_vs[i].second, two_vs[i + 1].second);
  }
  vector<int> rest_ids;
  if (two_vs.size() >= 2) {
    rest_ids.push_back(two_vs.front().second);
    rest_ids.push_back(two_vs.back().second);

    for (int i = 0; i < int(two_vs.size()); ++i) {
      int num = two_vs[i].first - 2;
      while (num--)
        rest_ids.push_back(two_vs[i].second);
    }
  } else {
    for (int i = 0; i < int(two_vs.size()); ++i) {
      int num = two_vs[i].first;
      while (num--)
        rest_ids.push_back(two_vs[i].second);
    }
  }
  reverse(rest_ids.begin(), rest_ids.end());
  bool ok = true;
  for (int i = 0; i < one_vs.size(); ++i) {
    if (rest_ids.empty()) {
      ok = false;
    } else {
      edges.emplace_back(rest_ids.back(), one_vs[i].second);
      rest_ids.pop_back();
    }
  }

  int answer = two_vs.size();
  answer += min(2, int(one_vs.size()));
  answer--;
  if (ok) {
    cout << "YES " << answer << endl;
    cout << N - 1 << endl;
    for (auto e : edges) {
      cout << e.first + 1 << ' ' << e.second + 1 << endl;
    }
  } else {
    cout << "NO" << endl;
  }
}
void solveE() {
  int N, A;
  cin >> N >> A;
  map<int, int> mp;
  int max_plus = 0;
  int A_cnt = 0;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    if (a == A) {
      A_cnt++;
      auto it = mp.begin();
      while (it != mp.end()) {
        it->second--;
        if (it->second == 0)
          it = mp.erase(it);
        else
          it++;
      }

    } else {
      mp[a]++;
      max_plus = max(max_plus, mp[a]);
    }
  }
  cout << A_cnt + max_plus << endl;
}
vector<vector<ll>> memo(51, vector<ll>(51));
vector<vector<ll>> sums(51, vector<ll>(51));

void init() {
  for (int i = 1; i <= 50; ++i) {
    for (int j = 1; j <= i; ++j) {
      ll answer = 1;
      for (int k = 0; k < j; ++k) {
        answer *= i - k;
        answer /= k + 1;
      }
      memo[i][j] = answer;
    }
  }
  for (int i = 0; i <= 50; ++i) {
    for (int j = 1; j <= 50; ++j) {
      if (j != 1)
        sums[i][j] += sums[i][j - 1];
      sums[i][j] += memo[i][j];
    }
  }
  // WHATS(sums)
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie();
  init();
  int N, K, S, T;
  cin >> N >> K >> S >> T;

  vector<int> t_on_places;
  for (int i = 0; i < 18; ++i) {
    if ((T & (1 << i)) && (!(S & (1 << i))))
      t_on_places.push_back(i);
  }
  WHATS(t_on_places)
  vector<int> as;
  int aN = N;
  for (int i = 0; i < N; ++i) {
    int a;
    cin >> a;
    if (((a & S) != S) || ((a & T) != a)) {
      aN--;
    } else {
      as.push_back(a);
    }
  }
  N = aN;
  WHATS(as)
  N = int(as.size());

  ll answer = 0;
  if ((S & T) != S) {

  } else {
    vector<vector<ll>> vs(t_on_places.size(), vector<ll>(2));

    for (int i = 0; i < N; ++i) {
      auto kk = as[i] ^ S;

      for (int j = 0; j < t_on_places.size(); ++j) {
        int xx = t_on_places[j];
        if ((kk >> xx) & 1) {
          vs[j][0] ^= 1 << i;
        } else {
          vs[j][1] ^= 1 << i;
        }
      }
    }

    for (int id = 0; id < 1 << (t_on_places.size()); ++id) {
      bool flag = __builtin_popcount(id) % 2;

      vector<ll> nums(1, (1ll << N) - 1);
      for (int j = 0; j < t_on_places.size(); ++j) {
        int xx = t_on_places[j];
        vector<ll> nnums;
        if (id & (1 << j)) {
          for (auto nu : nums) {
            ll l_use = nu & vs[j][0];
            ll r_use = nu & vs[j][1];
            assert((l_use ^ r_use) == nu);
            assert(!(l_use & r_use));
            // WHATS(l_use)
            // WHATS(r_use)
            if (l_use) {
              nnums.push_back(l_use);
            }
            if (r_use) {
              nnums.push_back(r_use);
            }
          }
          nums = nnums;
        }
      }
      WHATS(id)
      WHATS(nums)
      ll plus = 0;
      for (auto nu : nums) {
        plus += sums[__builtin_popcount(nu)][K];
      }
      if (flag)
        answer += plus;
      else
        answer -= plus;
      WHATS(plus)
      WHATS(answer)
    }
    WHATS(vs)
  }
  answer = abs(answer);
  cout << answer << endl;

  return 0;
}