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

//
// Created on 2020/07/11.
//

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <ios>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <numeric>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
using namespace std;
const long long LINF = 1e18;
const int INF = 1e9;

int bit_count(int i) {
  // 数字iを二進数表記にした際の1の数を調べる
  return __builtin_popcount(i);
}

int solve(int i) {
  if (i == 0)
    return 0;
  return solve(i % bit_count(i)) + 1;
}

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  int x0_cnt = 0;
  vector<int> x(n);
  for (int j = 0; j < n; ++j) {
    x[j] = s[j] - '0';
    x0_cnt += x[j];
  }
  reverse(all(x));
  vector<int> ans(n);

  for (int b = 0; b < 2; ++b) {
    int pc;
    if (b == 0)
      pc = x0_cnt + 1;
    else
      pc = x0_cnt - 1;
    if (pc <= 0)
      continue;
    ll x0_mod = 0;
    int r = 1;
    for (int j = 0; j < n; ++j) {
      x0_mod = (x0_mod + (r * x[j])) % pc;
      r = (r * 2) % pc;
    }
    r = 1;
    for (int k = 0; k < n; ++k) {
      if (x[k] == b) {
        ll now;
        if (b == 0) {
          now = (x0_mod + r) % pc;
        } else {
          now = (x0_mod - r) % pc;
          if (now < 0)
            now += pc;
        }
        ans[k] = solve(now) + 1;
      }
      r = (r * 2) % pc;
    }
  }
  reverse(all(ans));
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

//
// Created on 2020/07/11.
//

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iomanip>
#include <ios>
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <numeric>
#include <queue>         // queue, priority_queue
#include <set>           // set
#include <stack>         // stack
#include <string>        // string, to_string, stoi
#include <tuple>         // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
typedef long long ll;
using namespace std;
const long long LINF = 1e18;
const int INF = 1e9;

int bit_count(int i) {
  // 数字iを二進数表記にした際の1の数を調べる
  return __builtin_popcount(i);
}

int solve(int i) {
  if (i == 0)
    return 0;
  return solve(i % bit_count(i)) + 1;
}

int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  int x0_cnt = 0;
  vector<int> x(n);
  for (int j = 0; j < n; ++j) {
    x[j] = s[j] - '0';
    x0_cnt += x[j];
  }
  reverse(all(x));
  vector<int> ans(n);

  for (int b = 0; b < 2; ++b) {
    int pc;
    if (b == 0)
      pc = x0_cnt + 1;
    else
      pc = x0_cnt - 1;
    if (pc < 0)
      continue;
    ll x0_mod = 0;
    for (int j = 0; j < n; ++j) {
      x0_mod += ((ll)pow(2, j) * x[j]) % pc;
      x0_mod %= pc;
    }
    for (int k = 0; k < n; ++k) {
      if (x[k] == b) {
        ll now;
        if (b == 0) {
          now = (x0_mod + (ll)(pow(2, k)));
          now %= pc;
        } else {
          now = (x0_mod - (ll)(pow(2, k)));
          now %= pc;
          if (now < 0)
            now += pc;
        }
        ans[k] = solve(now) + 1;
      }
    }
  }
  reverse(all(ans));
  for (int i = 0; i < n; ++i) {
    cout << ans[i] << endl;
  }
  return 0;
}