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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template <typename TYPE> void print_vec(const vector<TYPE> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}

template <typename TYPE> void print_vec2(const vector<vector<TYPE>> &v) {
  cout << endl;
  cout << "       ";
  for (int i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (int j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

int func(int X) {
  int res = 0;
  while (X != 0) {
    bitset<20> bit(X);
    int pop_cnt = bit.count();
    X = X % pop_cnt;
    res++;
    // cout << "          X: " << X << " pop_cnt: " << pop_cnt << " res: " <<
    // res << endl;
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  int pop_cnt = 0;
  for (int i = 0; i < N; i++) {
    if (s[i] == '1')
      pop_cnt++;
  }

  vector<int> Xi(N, 0);
  vector<int> ans(N, 0);
  for (int bit = 0; bit < 2; bit++) { // s[i]==0の場合と1の場合で2回ループ
    int pc;
    if (bit == 0)
      pc = pop_cnt + 1;
    else
      pc = pop_cnt - 1;
    if (pc <= 0)
      continue; // 以降0割り(%だけど)が発生するのでcontinueする
    // cout << " pop_cnt: " << pop_cnt << "   pc: " << pc << endl;
    int X = 0;
    int pow2_i = 1 % pc;
    for (int i = 0; i < N; i++) {
      X += (s[i] - '0') * pow2_i;
      X %= pc;
      pow2_i *= 2;
      pow2_i %= pc;
    }
    pow2_i = 1 % pc;
    for (int i = 0; i < N; i++) {
      if ((s[i] - '0') == bit) {
        if (bit == 0)
          Xi[i] = (X + pow2_i) % pc;
        else
          Xi[i] = (X + pc - pow2_i) % pc;
        ans[i] = 1 + func(Xi[i]); // すでに pop_cntで割った余りを1回計算したので
                                  // func()に+1
      }
      pow2_i *= 2;
      pow2_i %= pc;
    }
  }

  // vector<int> ans(N, 1);// すでに pop_cntで割った余りを1回計算したので
  // 0ではなく1で初期化 for(int i=0; i<N; i++){  //しかしこれだと
  // 1を0にしたときに全部の桁が0になった場合も答えが1になってしまうので，上のforループにいれるのが正解
  // そのためここはコメントアウト
  // 	// cout << " Xi[" << i << "]: " << Xi[i] << endl;
  // 	ans[i] += func(Xi[i]);
  // 	//if(pop_cnt == 0) ans[i] = 0;
  // }

  for (int i = 0; i < N; i++) {
    cout << ans[N - i - 1] << endl;
  }
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

template <typename TYPE> void print_vec(const vector<TYPE> &v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}

template <typename TYPE> void print_vec2(const vector<vector<TYPE>> &v) {
  cout << endl;
  cout << "       ";
  for (int i = 0; i < v[0].size(); i++)
    cout << i << "   ";
  cout << endl;
  for (int i = 0; i < v.size(); i++) {
    cout << "i=" << i << ":   ";
    for (int j = 0; j < v[i].size(); j++) {
      if (v[i][j] == 0)
        cout << "\\x1B[0m" << v[i][j] << "   ";
      else
        cout << "\\x1B[31m" << v[i][j]
             << "   "; // https://stackoverrun.com/ja/q/12618775
    }
    cout << "\\x1B[0m" << endl;
  }
}

int func(int X) {
  int res = 0;
  while (X != 0) {
    bitset<20> bit(X);
    int pop_cnt = bit.count();
    X = X % pop_cnt;
    res++;
    // cout << "          X: " << X << " pop_cnt: " << pop_cnt << " res: " <<
    // res << endl;
  }
  return res;
}

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  int pop_cnt = 0;
  for (int i = 0; i < N; i++) {
    if (s[i] == '1')
      pop_cnt++;
  }

  vector<int> Xi(N, 0);
  for (int bit = 0; bit < 2; bit++) {
    int pc;
    if (bit == 0)
      pc = pop_cnt + 1;
    else
      pc = pop_cnt - 1;
    if (pop_cnt <= 0)
      continue;
    int X = 0;
    int pow2_i = 1 % pc;
    for (int i = 0; i < N; i++) {
      X += (s[i] - '0') * pow2_i;
      X %= pc;
      pow2_i *= 2;
      pow2_i %= pc;
    }
    pow2_i = 1 % pc;
    for (int i = 0; i < N; i++) {
      if ((s[i] - '0') == bit) {
        if (bit == 0)
          Xi[i] = (X + pow2_i) % pc;
        else
          Xi[i] = (X + pc - pow2_i) % pc;
      }
      pow2_i *= 2;
      pow2_i %= pc;
    }
  }

  vector<int> ans(
      N, 1); // すでに pop_cntで割った余りを1回計算したので 0ではなく1で初期化
  for (int i = 0; i < N; i++) {
    // cout << " Xi[" << i << "]: " << Xi[i] << endl;
    ans[i] += func(Xi[i]);
  }

  for (int i = 0; i < N; i++) {
    cout << ans[N - i - 1] << endl;
  }
}