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

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

typedef long long ll;

typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;

#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)

template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << ", " << a.second << ")";
}

template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  out << "[";
  for (auto it = a.begin(); it != a.end(); ++it) {
    if (it != a.begin())
      out << ", ";
    out << *it;
  }
  return out << "]";
}
int arr[200001];

void solve() {
  ll n;
  cin >> n;
  string x;
  cin >> x;

  // n = 1
  if (n == 1) {
    if (x == "0")
      cout << 1 << "\\n";
    else
      cout << 0 << "\\n";
    return;
  }
  arr[0] = 0;

  for (int i = 1; i <= 200000; i++) {
    arr[i] = arr[i % __builtin_popcount(i)] + 1;
  }
  ll curr1 = 0;
  ll curr2 = 0;
  vll pw1 = {1};
  vll pw2 = {1};
  ll cnt = 0;
  for (auto cc : x) {
    if (cc == '1')
      cnt++;
  }
  if (cnt == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << "\\n";
    return;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      curr1 += pw1.back();
      curr2 += pw2.back();
      curr2 %= cnt + 1;
      if (cnt != 1)
        curr1 %= cnt - 1;
    }
    if (cnt != 1)
      pw1.push_back((2LL * pw1.back()) % (cnt - 1));
    pw2.push_back((2LL * pw2.back()) % (cnt + 1));
    // pw1.push_back((2LL*pw1.back()));
    // pw2.push_back((2LL*pw2.back()));
  }

  reverse(all(pw1));
  reverse(all(pw2));

  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      cout << arr[(curr2 + pw2[i + 1]) % (cnt + 1)] + 1 << "\\n";
    } else {

      if (cnt == 1)
        cout << 0 << "\\n";
      else
        cout << arr[(curr1 - pw1[i + 1] + (cnt - 1)) % (cnt - 1)] + 1 << "\\n";
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}


Error: 0

Fixed submission:

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

typedef long long ll;

typedef pair<ll, ll> pll;
typedef vector<ll> vll;
typedef vector<pll> vpll;
typedef vector<vll> vvll;

#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)

template <class A, class B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  return out << "(" << a.first << ", " << a.second << ")";
}

template <class A> ostream &operator<<(ostream &out, const vector<A> &a) {
  out << "[";
  for (auto it = a.begin(); it != a.end(); ++it) {
    if (it != a.begin())
      out << ", ";
    out << *it;
  }
  return out << "]";
}
int arr[200001];

void solve() {
  ll n;
  cin >> n;
  string x;
  cin >> x;

  // n = 1
  if (n == 1) {
    if (x == "0")
      cout << 1 << "\\n";
    else
      cout << 0 << "\\n";
    return;
  }
  arr[0] = 0;

  for (int i = 1; i <= 200000; i++) {
    arr[i] = arr[i % __builtin_popcount(i)] + 1;
  }
  ll curr1 = 0;
  ll curr2 = 0;
  vll pw1 = {1};
  vll pw2 = {1};
  ll cnt = 0;
  for (auto cc : x) {
    if (cc == '1')
      cnt++;
  }
  if (cnt == 0) {
    for (int i = 0; i < n; i++)
      cout << 1 << "\\n";
    return;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (x[i] == '1') {
      curr1 += pw1.back();
      curr2 += pw2.back();
      curr2 %= cnt + 1;
      curr1 %= cnt - 1;
    }
    if (cnt != 1)
      pw1.push_back((2LL * pw1.back()) % (cnt - 1));
    pw2.push_back((2LL * pw2.back()) % (cnt + 1));
    // pw1.push_back((2LL*pw1.back()));
    // pw2.push_back((2LL*pw2.back()));
  }

  reverse(all(pw1));
  reverse(all(pw2));
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      cout << arr[(curr2 + pw2[i + 1]) % (cnt + 1)] + 1 << "\\n";
    } else {
      if (cnt == 1)
        cout << 0 << "\\n";
      else
        cout << arr[(curr1 - pw1[i + 1] + (cnt - 1)) % (cnt - 1)] + 1 << "\\n";
    }
  }
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }

  return 0;
}