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

#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for (int i = 0; (i) < (n); (i)++)
#define REP2(i, a, b) for (int i = (a); (i) < (b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__)
#define MOD 1'000'000'007

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (size_t i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 == v.size() ? "" : ", ");
  os << "}";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}

long long calc(long long N, string X, long long cnt, long long tab[222222]) {
  if (cnt < 0)
    return 0;
  long long ret = 0;
  for (long long i = N - 1; i >= 0; i--) {
    long long j = N - 1 - i;
    if (X[i] == '1')
      ret += tab[j];
  }
  return ret;
}

long long tab1[222222];
long long tab2[222222];

long long func(long long x) {
  if (x == 0)
    return 0;
  long long cnt = __builtin_popcountll(x);
  x = x % cnt;
  return 1 + func(x);
}

int main() {
  long long N;
  string X;
  cin >> N >> X;
  long long cnt = 0;
  for (long long i = 0; i < N; i++)
    if (X[i] == '1')
      cnt++;
  if (cnt - 1 > 0) {
    tab1[0] = 1 % (cnt - 1);
    for (long long i = 1; i <= 200000; i++)
      tab1[i] = (tab1[i - 1] * 2) % (cnt - 1);
  }
  tab2[0] = 1 % (cnt + 1);
  for (long long i = 1; i <= 200000; i++)
    tab2[i] = (tab2[i - 1] * 2) % (cnt + 1);

  long long a = 0, b = 0;
  if (cnt - 1 > 0)
    a = calc(N, X, cnt - 1, tab1);
  b = calc(N, X, cnt + 1, tab2);
  vector<long long> ans;
  for (long long i = N - 1; i >= 0; i--) {
    long long j = N - 1 - i;
    long long tmp;
    if (X[i] == '0') {
      tmp = b;
      tmp += tab2[j];
      tmp %= (cnt + 1);
    } else {
      if (cnt == 1) {
        ans.push_back(-1);
        continue;
      }
      tmp = a;
      tmp += (cnt - 1 - tab1[j]);
      tmp %= (cnt - 1);
    }
    ans.push_back(tmp);
  }
  reverse(ans.begin(), ans.end());

  for (long long i = 0; i < N; i++) {
    if (ans[i] == -1)
      cout << 0 << endl;
    else
      cout << func(ans[i]) + 1 << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define SELECTER(_1, _2, _3, SELECT, ...) SELECT
#define REP1(i, n) for (int i = 0; (i) < (n); (i)++)
#define REP2(i, a, b) for (int i = (a); (i) < (b); (i)++)
#define REP(...) SELECTER(__VA_ARGS__, REP2, REP1, )(__VA_ARGS__)
#define MOD 1'000'000'007

template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << "{";
  for (size_t i = 0; i < v.size(); i++)
    os << v[i] << (i + 1 == v.size() ? "" : ", ");
  os << "}";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}

long long calc(long long N, string X, long long cnt, long long tab[222222]) {
  if (cnt < 0)
    return 0;
  long long ret = 0;
  for (long long i = N - 1; i >= 0; i--) {
    long long j = N - 1 - i;
    if (X[i] == '1')
      ret += tab[j];
  }
  return ret;
}

long long tab1[222222];
long long tab2[222222];

long long func(long long x) {
  if (x == 0)
    return 0;
  long long cnt = __builtin_popcountll(x);
  x = x % cnt;
  return 1 + func(x);
}

int main() {
  long long N;
  string X;
  cin >> N >> X;
  long long cnt = 0;
  for (long long i = 0; i < N; i++)
    if (X[i] == '1')
      cnt++;
  if (cnt - 1 > 0) {
    tab1[0] = 1 % (cnt - 1);
    for (long long i = 1; i <= 200000; i++)
      tab1[i] = (tab1[i - 1] * 2) % (cnt - 1);
  }
  tab2[0] = 1 % (cnt + 1);
  for (long long i = 1; i <= 200000; i++)
    tab2[i] = (tab2[i - 1] * 2) % (cnt + 1);

  long long a = 0, b = 0;
  if (cnt - 1 > 0)
    a = calc(N, X, cnt - 1, tab1);
  b = calc(N, X, cnt + 1, tab2);
  vector<long long> ans;
  for (long long i = N - 1; i >= 0; i--) {
    long long j = N - 1 - i;
    long long tmp;
    if (X[i] == '0') {
      tmp = b;
      tmp += tab2[j];
      tmp %= (cnt + 1);
    } else {
      if (cnt == 1)
        ans.push_back(-1);
      continue;
      tmp = a;
      tmp -= tab1[j];
      tmp %= (cnt - 1);
    }
    ans.push_back(tmp);
  }
  reverse(ans.begin(), ans.end());

  for (long long i = 0; i < N; i++) {
    if (ans[i] == -1)
      cout << 0 << endl;
    else
      cout << func(ans[i]) + 1 << endl;
  }

  return 0;
}