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

#include <sys/time.h>

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using P = pair<double, double>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mkp(a, b) make_pair(a, b)
ll mod = 1000000007LL;

ll count_one(ll n) {
  ll count = 0;
  while (n > 0) {
    if ((n & 1) == 1)
      count++;
    n = n / 2;
  }
  return count;
}

ll solve(ll n) {
  ll d = count_one(n);

  if (d == 0)
    return 0;

  if (n % d == 0) {
    return 1;
  }
  n = n % d;

  return solve(n) + 1;
}

int main() {
  int n;
  cin >> n;
  string xs;
  cin >> xs;

  ll p_cnt = 0;
  for (char x : xs) {
    if (x == '1')
      p_cnt++;
  }

  vector<ll> a_p(n + 1, 0);
  vector<ll> a_m(n + 1, 0);

  ll cnt_plus = p_cnt + 1;
  ll cnt_minus = p_cnt - 1;

  ll cur = 1;
  ll plus_ans = 0;
  for (int i = 0; i < n; i++) {
    if (cnt_plus == 0) {
      break;
    }
    ll pos = n - i - 1;
    a_p[pos] = cur;
    if (xs[pos] == '1') {
      plus_ans += cur;
      plus_ans %= cnt_plus;
    }
    cur = cur * 2;
    cur %= cnt_plus;
  }

  cur = 1;
  ll minus_ans = 0;
  for (int i = 0; i < n; i++) {
    if (cnt_minus == 0) {
      break;
    }
    int pos = n - i - 1;
    a_m[pos] = cur;
    if (xs[pos] == '1') {
      minus_ans += cur;
      minus_ans %= cnt_minus;
    }
    cur = cur * 2;
    cur %= cnt_minus;
  }

  // cout << "middle:" << endl;

  rep(i, n) {
    ll cur_n = 0;
    // cout << "i:" << i  << endl;
    // cout << " xs[i]:" << xs[i] << endl;

    if (xs[i] == '1') {
      // cout << "minus_ans:" << minus_ans << endl;
      ll ans = minus_ans - a_m[i];

      // cout << "cnt_minus:" << cnt_minus << endl;

      if (cnt_minus > 0) {
        // cout << "a:" <<  endl;
        cur_n = (ans + cnt_minus) % cnt_minus;
      } else {
        cout << "0" << endl;
        continue;
      }

    } else {
      // cout << "plus_ans:" << plus_ans << endl;

      ll ans = plus_ans + a_p[i];
      cur_n = (ans + cnt_plus) % cnt_plus;
    }
    // cout << "cur_n:" << cur_n << endl;

    if (cur_n == 0) {
      cout << 1 << endl;
    } else {
      cout << solve(cur_n) + 1 << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <sys/time.h>

#include <algorithm>
#include <cctype>
#include <cmath>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

using ll = long long;
using P = pair<double, double>;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define mkp(a, b) make_pair(a, b)
ll mod = 1000000007LL;

ll count_one(ll n) {
  ll count = 0;
  while (n > 0) {
    if ((n & 1) == 1)
      count++;
    n = n / 2;
  }
  return count;
}

ll solve(ll n) {
  ll d = count_one(n);

  if (d == 0)
    return 0;

  if (n % d == 0) {
    return 1;
  }
  n = n % d;

  return solve(n) + 1;
}

int main() {
  int n;
  cin >> n;
  string xs;
  cin >> xs;

  ll p_cnt = 0;
  for (char x : xs) {
    if (x == '1')
      p_cnt++;
  }

  vector<ll> a_p(n, 0);
  vector<ll> a_m(n, 0);

  ll cnt_plus = p_cnt + 1;
  ll cnt_minus = p_cnt - 1;

  ll cur = 1;
  ll plus_ans = 0;
  for (int i = 0; i < n; i++) {
    ll pos = n - i - 1;
    a_p[pos] = cur;
    if (xs[pos] == '1') {
      plus_ans += cur;
      plus_ans %= cnt_plus;
    }
    cur = cur * 2;
    cur %= cnt_plus;
  }

  cur = 1;
  ll minus_ans = 0;
  for (int i = 0; i < n; i++) {
    int pos = n - i - 1;
    a_m[pos] = cur;
    if (xs[pos] == '1') {
      minus_ans += cur;
      minus_ans %= cnt_minus;
    }
    cur = cur * 2;
    cur %= cnt_minus;
  }

  rep(i, n) {
    ll cur_n;
    // cout << "i:" << i  << endl;
    // cout << " xs[i]:" << xs[i] << endl;

    if (xs[i] == '1') {
      // cout << "minus_ans:" << minus_ans << endl;
      ll ans = minus_ans - a_m[i];
      cur_n = (ans + cnt_minus) % cnt_minus;

    } else {
      // cout << "plus_ans:" << plus_ans << endl;

      ll ans = plus_ans + a_p[i];
      cur_n = (ans + cnt_plus) % cnt_plus;
    }
    // cout << "cur_n:" << cur_n << endl;

    if (cur_n == 0) {
      cout << 1 << endl;
    } else {
      cout << solve(cur_n) + 1 << endl;
    }
  }

  return 0;
}