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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using namespace std;
using namespace __gnu_pbds;

typedef tree<pair<long long int, long long int>, null_type,
             less<pair<long long int, long long int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

long long int power(long long int base, long long p, long long int mod) {
  long long int ans = 1;
  while (p) {
    if (p & 1)
      ans = (ans * base) % mod, p--;
    else
      base = (base * base) % mod, p /= 2;
  }
  return ans % mod;
}
void go() {
  long long int n, o = 0, z;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      o++;
  }
  long long int sum_rem_add = 0, sum_rem_sub = 0;
  if (o == 1) {
    for (int i = 0; i < n - 1; i++) {
      if (s[i] == '0')
        cout << 1 << endl;
      else
        cout << 0 << endl;
    }
    if (s[n - 1] == '0')
      cout << 2 << endl;
    else
      cout << 0 << endl;
    return;
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      sum_rem_add = (sum_rem_add + power(2, n - 1 - i, o + 1)) % (o + 1);
      sum_rem_sub = (sum_rem_sub + power(2, n - 1 - i, o - 1)) % (o - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      long long int temp_sum_rem =
          (sum_rem_add + power(2, n - 1 - i, o + 1)) % (o + 1);
      long long int temp_ans = 1;
      long long int temp = temp_sum_rem;
      while (temp)
        temp %= (__builtin_popcount(temp)), temp_ans++;
      cout << temp_ans << endl;
    } else {
      long long int temp_sum_rem =
          (sum_rem_sub - (power(2, n - 1 - i, o - 1)) + (o - 1)) % (o - 1);
      long long int temp_ans = 1;
      long long int temp = temp_sum_rem;
      while (temp)
        temp %= (__builtin_popcount(temp)), temp_ans++;
      cout << temp_ans << endl;
    }
  }
}
int main() {
  FIO;
  //    int t;
  //    cin>>t;
  //    while(t--)
  go();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
using namespace std;
using namespace __gnu_pbds;

typedef tree<pair<long long int, long long int>, null_type,
             less<pair<long long int, long long int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

long long int power(long long int base, long long p, long long int mod) {
  long long int ans = 1;
  while (p) {
    if (p & 1)
      ans = (ans * base) % mod, p--;
    else
      base = (base * base) % mod, p /= 2;
  }
  return ans % mod;
}
void go() {
  long long int n, o = 0, z, sum_rem = 0;
  cin >> n;
  string s;
  cin >> s;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      o++;
  }
  int sum_rem_add = 0, sum_rem_sub = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      sum_rem_add = (sum_rem_add + power(2, n - 1 - i, o + 1)) % (o + 1);
      sum_rem_sub = (sum_rem_sub + power(2, n - 1 - i, o - 1)) % (o - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      int temp_sum_rem = (sum_rem_add + power(2, n - 1 - i, o + 1)) % (o + 1);
      int temp_ans = 1;
      int temp = temp_sum_rem;
      while (temp)
        temp %= (__builtin_popcount(temp)), temp_ans++;
      cout << temp_ans << endl;
    } else {
      int temp_sum_rem =
          (sum_rem_sub - (power(2, n - 1 - i, o - 1)) + (o - 1)) % (o - 1);
      int temp_ans = 1;
      int temp = temp_sum_rem;
      while (temp)
        temp %= (__builtin_popcount(temp)), temp_ans++;
      cout << temp_ans << endl;
    }
  }
}
int main() {
  FIO;
  //    int t;
  //    cin>>t;
  //    while(t--)
  go();
  return 0;
}