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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <assert.h>
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <complex>  // complex
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iomanip>  // setprecision
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue> // queue, priority_queue
#include <random>
#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 rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1'000'000'007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ---------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  string S;
  cin >> S;
  int cnt = 0;
  reverse(S.begin(), S.end());
  for (int i = 0; i < N; i++) {
    cnt += (S[i] == '1');
  }
  ll two;
  ll sum_mn = 0;
  vector<ll> two_mn(N);
  two = 1;
  if (cnt > 1) {
    for (int i = 0; i < N; i++) {
      if (S[i] == '1') {
        sum_mn += two;
        sum_mn %= (cnt - 1);
      }
      two_mn[i] = two;
      two *= 2;
      two %= (cnt - 1);
    }
  }
  vector<ll> two_mx(N);
  ll sum_mx = 0;
  two = 1;
  if (cnt != N) {
    for (int i = 0; i < N; i++) {
      if (S[i] == '1') {
        sum_mx += two;
        sum_mx %= (cnt + 1);
      }
      two_mx[i] = two;
      two *= 2;
      two %= (cnt + 1);
    }
  }
  vector<int> ans(N, 0);
  for (int i = 0; i < N; i++) {
    if (S[i] == '0') {
      ll now = sum_mx;
      now += two_mx[i];
      now %= (cnt + 1);
      ll temp = now;
      ans[i] = 1;
      while (temp) {
        ll nxt = 0;
        ll temp2 = temp;
        while (temp2) {
          nxt += temp2 % 2;
          temp2 /= 2;
        }
        temp %= nxt;
        ans[i]++;
      }
    } else {
      if (cnt == 1) {
        ans[i] = 0;
        continue;
      }
      ll now = sum_mn;
      now -= two_mn[i];
      now += (cnt - 1);
      now %= (cnt - 1);
      ans[i] = 1;
      ll temp = now;
      while (temp) {
        ll nxt = 0;
        ll temp2 = temp;
        while (temp2) {
          nxt += temp2 % 2;
          temp2 /= 2;
        }
        temp %= nxt;
        ans[i]++;
      }
    }
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < N; i++) {
    cout << ans[i] << "\\n";
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <assert.h>
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <climits>
#include <complex>  // complex
#include <cstdint>  // int64_t, int*_t
#include <cstdio>   // printf
#include <deque>    // deque
#include <iomanip>  // setprecision
#include <iostream> // cout, endl, cin
#include <map>      // map
#include <math.h>
#include <queue> // queue, priority_queue
#include <random>
#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 rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr ll mod = 1'000'000'007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[8] = {0, 1, 0, -1, 1, -1, 1, -1};
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// ---------------------------------------------------------------------------

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  string X;
  cin >> X;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    sum += X[i] == '1';
  }
  reverse(X.begin(), X.end());
  vector<int> ans(N, 0);
  for (int i = 0; i < N; i++) {
    int now = sum;
    if (X[i] == '1') {
      X[i] = '0';
      now--;
    } else {
      X[i] = '1';
      now++;
    }
    if (now == 0) {
      ans[i] = 0;
      if (X[i] == '1') {
        X[i] = '0';
        now--;
      } else {
        X[i] = '1';
        now++;
      }
      continue;
    }
    ll temp = 0;
    for (int j = 0; j < min(25, N); j++) {
      if (X[j] == '1') {
        temp += (1LL << j);
      }
    }
    ans[i]++;
    ll temp3 = temp;
    now = 0;

    while (temp3) {
      now += temp3 % 2;
      temp3 /= 2;
    }
    temp %= now;

    while (temp) {
      ll temp2 = temp;
      now = 0;
      while (temp2) {
        now += temp2 % 2;
        temp2 /= 2;
      }
      temp %= now;
      ans[i]++;
    }

    if (X[i] == '1') {
      X[i] = '0';
      now--;
    } else {
      X[i] = '1';
      now++;
    }
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < N; i++) {
    cout << ans[i] << "\\n";
  }
  return 0;
}