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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

ll popcount(ll n) {
  ll mask = 1, res = 0;
  while (n >= mask) {
    if (mask & n)
      res++;
    mask *= 2;
  }
  return res;
}

ll guchoku(ll n) {
  if (n == 0)
    return 0;
  if (n == -1)
    return -1;
  return guchoku(n % popcount(n)) + 1;
}

int main() {
  ll n, m = 0;
  string x;
  cin >> n >> x;
  reverse(x.begin(), x.end()); // x = sum of 2^i + x[i]
  for (auto e : x)
    m += (e - '0');
  /*
  - pw_plus[i] := 2^i % popcount(x)+1
  - pw_minus[i] := 2^i % popcount(x)-1
  */
  vector<ll> pw_plus(200010, 1), pw_minus(200010, 1);
  for (ll i = 1; i < 200010; i++) {
    pw_plus[i] = 2 * pw_plus[i - 1];
    pw_plus[i] %= (m + 1);
    if (m > 1) {
      pw_minus[i] = 2 * pw_minus[i - 1];
      pw_minus[i] %= (m - 1);
    }
  }
  /*
  - x_plus := x % popcount(x)+1
  - x_minus := x % popcount(x)-1
  */
  ll x_plus = 0, x_minus = 0;
  for (ll i = 0; i < n; i++) {
    x_plus += pw_plus[i] * (x[i] - '0');
    x_plus %= (m + 1);
    if (m > 1) {
      x_minus += pw_minus[i] * (x[i] - '0');
      x_minus %= (m - 1);
    }
  }
  vector<ll> rec_ans(n);
  for (ll i = 0; i < n; i++) {
    ll ans;
    if (x[i] == '1') {
      if (m == 1)
        ans = -1;
      else {
        ans = x_minus - pw_minus[i];
        ans = (ans + m - 1) % (m - 1);
      }
    } else {
      ans = x_plus + pw_plus[i];
      ans %= (m + 1);
    }
    rec_ans[i] = guchoku(ans) + 1;
  }
  for (ll i = n - 1; i >= 0; i--)
    cout << rec_ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, srt, end) for (long long i = (srt); i < (long long)(end); i++)

ll popcount(ll n) {
  ll mask = 1, res = 0;
  while (n >= mask) {
    if (mask & n)
      res++;
    mask *= 2;
  }
  return res;
}

ll guchoku(ll n) {
  if (n == 0)
    return 0;
  return guchoku(n % popcount(n)) + 1;
}

int main() {
  ll n, m = 0;
  string x;
  cin >> n >> x;
  reverse(x.begin(), x.end()); // x = sum of 2^i + x[i]
  for (auto e : x)
    m += (e - '0');
  /*
  - pw_plus[i] := 2^i % popcount(x)+1
  - pw_minus[i] := 2^i % popcount(x)-1
  */
  vector<ll> pw_plus(100010, 1), pw_minus(100010, 1);
  for (ll i = 1; i < 100010; i++) {
    pw_plus[i] = 2 * pw_plus[i - 1];
    pw_plus[i] %= (m + 1);
    if (m > 1) {
      pw_minus[i] = 2 * pw_minus[i - 1];
      pw_minus[i] %= (m - 1);
    }
  }
  /*
  - x_plus := x % popcount(x)+1
  - x_minus := x % popcount(x)-1
  */
  ll x_plus = 0, x_minus = 0;
  for (ll i = 0; i < n; i++) {
    x_plus += pw_plus[i] * (x[i] - '0');
    x_plus %= (m + 1);
    if (m > 1) {
      x_minus += pw_minus[i] * (x[i] - '0');
      x_minus %= (m - 1);
    }
  }
  vector<ll> rec_ans(n);
  for (ll i = 0; i < n; i++) {
    ll ans;
    if (x[i] == '1') {
      if (m == 1)
        ans = 0;
      else {
        ans = x_minus - pw_minus[i];
        ans = (ans + m - 1) % (m - 1);
      }
    } else {
      ans = x_plus + pw_plus[i];
      ans %= (m + 1);
    }
    rec_ans[i] = guchoku(ans) + 1;
  }
  for (ll i = n - 1; i >= 0; i--)
    cout << rec_ans[i] << endl;
  return 0;
}