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

#include <bits/stdc++.h>
using namespace std;

// #define int long long
typedef long long ll;
// const int INF = 2e9;
// const ll INF = 9e18;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  string X;
  cin >> X;
  int popcount = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      popcount++;
  }
  int plus = popcount + 1;
  int minus = popcount - 1;
  vector<int> pow2plus(200005);
  vector<int> pow2minus(200005);
  pow2plus[0] = 1;
  pow2minus[0] = 1;
  for (int i = 0; i <= 200000; i++) {
    pow2plus[i + 1] = pow2plus[i] * 2;
    pow2plus[i + 1] %= plus;
    if (minus != 0) {
      pow2minus[i + 1] = pow2minus[i] * 2;
      pow2minus[i + 1] %= minus;
    }
  }
  int Xplus = 0, Xminus = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      Xplus += pow2plus[N - i - 1];
      Xplus %= plus;
      if (minus != 0) {
        Xminus += pow2minus[N - i - 1];
        Xminus %= minus;
      }
    }
  }
  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      // minusで計算する
      if (minus == 0)
        ans[i] = 0;
      else {
        int next = Xminus - pow2minus[N - i - 1];
        if (next < 0)
          next += minus;
        ans[i]++;
        while (next > 0) {
          int pop = 0;
          for (int bit = 0; bit <= 30; bit++) {
            if ((next >> bit) & 1)
              pop++;
          }
          next %= pop;
          ans[i]++;
        }
      }
    } else {
      // plusで計算する
      int next = Xplus + pow2plus[N - i - 1];
      next %= plus;
      ans[i]++;
      while (next > 0) {
        int pop = 0;
        for (int bit = 0; bit <= 30; bit++) {
          if ((next >> bit) & 1)
            pop++;
        }
        next %= pop;
        ans[i]++;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << ans[i] << "\\n";
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

// #define int long long
typedef long long ll;
// const int INF = 2e9;
// const ll INF = 9e18;

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N;
  cin >> N;
  string X;
  cin >> X;
  int popcount = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      popcount++;
  }
  int plus = popcount + 1;
  int minus = popcount - 1;
  vector<int> pow2plus(200005);
  vector<int> pow2minus(200005);
  pow2plus[0] = 1;
  pow2minus[0] = 1;
  for (int i = 0; i <= 200001; i++) {
    pow2plus[i + 1] = pow2plus[i] * 2;
    pow2minus[i + 1] = pow2minus[i] * 2;
    pow2plus[i + 1] %= plus;
    pow2minus[i + 1] %= minus;
  }
  int Xplus = 0, Xminus = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      Xplus += pow2plus[N - i - 1];
      Xplus %= plus;
      Xminus += pow2minus[N - i - 1];
      Xminus %= minus;
    }
  }
  vector<int> ans(N);
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      // minusで計算する
      if (minus == 0)
        ans[i] = 0;
      else {
        int next = Xminus - pow2minus[N - i - 1];
        if (next < 0)
          next += minus;
        ans[i]++;
        while (next > 0) {
          int pop = 0;
          for (int bit = 0; bit <= 30; bit++) {
            if ((next >> bit) & 1)
              pop++;
          }
          next %= pop;
          ans[i]++;
        }
      }
    } else {
      // plusで計算する
      int next = Xplus + pow2plus[N - i - 1];
      next %= plus;
      ans[i]++;
      while (next > 0) {
        int pop = 0;
        for (int bit = 0; bit <= 30; bit++) {
          if ((next >> bit) & 1)
            pop++;
        }
        next %= pop;
        ans[i]++;
      }
    }
  }
  for (int i = 0; i < N; i++) {
    cout << ans[i] << "\\n";
  }
}