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

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
// const long long MOD = 998244353;
const long long INF_LL = 1e18 + 11;
const int INF_int = 1e9 + 11;
#define ll long long
#define mp std::make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define All(a) a.begin(), a.end()
#define Debug_Output_ALL(a)                                                    \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl;
// x^n(MOD)
long long power(ll x, long long n, long long MOD) {
  long long a = 1;
  long long b = x;
  while (n > 0) {
    if (n % 2 == 1) {
      a *= b;
      a %= MOD;
    }
    n /= 2;
    b = b * b;
    b %= MOD;
  }
  return a;
}
int solve(int a) {
  if (a == 0)
    return 1;
  int ret = 0;
  while (a != 0) {
    int pop_count = 0;
    rep(i, 30) if ((a & (1 << i)) != 0) pop_count++;
    a = a % pop_count;
    ret++;
  }
  return ret + 1;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int pc = 0;
  rep(i, s.size()) if (s[i] == '1') pc++;
  int mi = 0;
  int pl = 0;
  int now_pl = 1;
  int now_mi = 1;
  bool fl = true;
  if (pc == 1) {
    fl = false;
  }
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      pl += now_pl;
      if (fl)
        mi += now_mi;
      pl %= (pc + 1);
      if (fl)
        mi %= (pc - 1);
    }
    now_pl *= 2;
    if (fl)
      now_mi *= 2;
    now_pl %= (pc + 1);
    if (fl)
      now_mi %= (pc - 1);
  }
  vector<int> ans(n);
  // cout << pc << endl;
  // cout << now_pl << " " << now_mi << endl;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      if (!fl) {
        ans[i] = 0;
        continue;
      }
      int now = power(2LL, n - 1 - i, pc - 1);
      now = mi - now;
      now %= (pc - 1);
      if (now < 0)
        now += (pc - 1);
      ans[i] = solve(now);
      // cout << now << endl;
    } else {
      int now = power(2LL, n - 1 - i, pc + 1);
      now += pl;
      now %= (pc + 1);
      if (now < 0)
        now += (pc + 1);
      ans[i] = solve(now);
      // cout << now << endl;
    }
  }
  rep(i, n) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e9 + 7;
// const long long MOD = 998244353;
const long long INF_LL = 1e18 + 11;
const int INF_int = 1e9 + 11;
#define ll long long
#define mp std::make_pair
#define rep(i, n) for (int i = 0; i < n; i++)
#define lrep(i, n) for (long long i = 0; i < n; i++)
#define All(a) a.begin(), a.end()
#define Debug_Output_ALL(a)                                                    \\
  for (auto x : a)                                                             \\
    cout << x << " ";                                                          \\
  cout << endl;
// x^n(MOD)
long long power(ll x, long long n, long long MOD) {
  long long a = 1;
  long long b = x;
  while (n > 0) {
    if (n % 2 == 1) {
      a *= b;
      a %= MOD;
    }
    n /= 2;
    b = b * b;
    b %= MOD;
  }
  return a;
}
int solve(int a) {
  if (a == 0)
    return 1;
  int ret = 0;
  while (a != 0) {
    int pop_count = 0;
    rep(i, 30) if ((a & (1 << i)) != 0) pop_count++;
    a = a % pop_count;
    ret++;
  }
  return ret + 1;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int pc = 0;
  rep(i, s.size()) if (s[i] == '1') pc++;
  int mi = 0;
  int pl = 0;
  int now_pl = 1;
  int now_mi = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      pl += now_pl;
      mi += now_mi;
      pl %= (pc + 1);
      mi %= (pc - 1);
    }
    now_pl *= 2;
    now_mi *= 2;
    now_pl %= (pc + 1);
    now_mi %= (pc - 1);
  }
  vector<int> ans(n);
  // cout << pc << endl;
  // cout << now_pl << " " << now_mi << endl;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      int now = power(2LL, n - 1 - i, pc - 1);
      now = mi - now;
      now %= (pc - 1);
      if (now < 0)
        now += (pc - 1);
      ans[i] = solve(now);
      // cout << now << endl;
    } else {
      int now = power(2LL, n - 1 - i, pc + 1);
      now += pl;
      now %= (pc + 1);
      if (now < 0)
        now += (pc + 1);
      ans[i] = solve(now);
      // cout << now << endl;
    }
  }
  rep(i, n) cout << ans[i] << endl;
}