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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;

int func(int N) {
  int num = 0;
  int kaisuu = 0;
  int b = N;
  while (b > 0) {
    b = b / 2;
    kaisuu++;
  }
  rep(i, kaisuu + 1) {
    if ((N >> i) & 1) {
      num++;
    }
  }
  return N % num;
}

int main() {
  int n;
  string a;
  cin >> n >> a;
  vector<int> x(n);
  rep(i, n) { x[i] = a[n - 1 - i] - '0'; }
  int cnt = 0;
  rep(i, n) if (x[i] == 1) cnt++;
  int def_ans_plus = 0, def_ans_minus = 0;
  vector<int> cnt_plus(n, 1), cnt_minus(n, 1);
  vector<int> ans(n + 1, 1);
  vector<int> now(n + 1, 0);
  rep(i, n - 1) {
    cnt_plus[i + 1] = (cnt_plus[i] * 2) % (cnt + 1);
    if (cnt != 1) {
      cnt_minus[i + 1] = (cnt_minus[i] * 2) % (cnt - 1);
    }
  }
  rep(i, n) {
    if (x[i] == 1) {
      def_ans_plus += cnt_plus[i];
      def_ans_plus = def_ans_plus % (cnt + 1);
      if (cnt != 1) {
        def_ans_minus += cnt_minus[i];
        def_ans_minus = def_ans_minus % (cnt - 1);
      }
    }
  }
  rep(i, n) {
    if (x[n - 1 - i] == 0) {
      now[i + 1] = (def_ans_plus + cnt_plus[n - 1 - i]) % (cnt + 1);
    } else {
      int tmp = def_ans_minus - cnt_minus[n - 1 - i];
      if (tmp < 0)
        tmp += cnt - 1;
      now[i + 1] = tmp;
      if (cnt == 1) {
        ans[i + 1] = 0;
        now[i + 1] = 0;
      }
    }
  }
  bool end = false;
  while (end == false) {
    end = true;
    REP(i, n) {
      if (now[i] != 0) {
        now[i] = func(now[i]);
        ans[i]++;
        end = false;
      }
    }
  }
  REP(i, n) { cout << ans[i] << endl; }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
typedef long long ll;

int func(int N) {
  int num = 0;
  int kaisuu = 0;
  int b = N;
  while (b > 0) {
    b = b / 2;
    kaisuu++;
  }
  rep(i, kaisuu + 1) {
    if ((N >> i) & 1) {
      num++;
    }
  }
  return N % num;
}

int main() {
  int n;
  string a;
  cin >> n >> a;
  vector<int> x(n);
  rep(i, n) { x[i] = a[n - 1 - i] - '0'; }
  int cnt = 0;
  rep(i, n) if (x[i] == 1) cnt++;
  int def_ans_plus = 0, def_ans_minus = 0;
  vector<int> cnt_plus(n, 1), cnt_minus(n, 1);
  vector<int> ans(n + 1, 1);
  vector<int> now(n + 1, 0);
  rep(i, n - 1) {
    cnt_plus[i + 1] = (cnt_plus[i] * 2) % (cnt + 1);
    cnt_minus[i + 1] = (cnt_minus[i] * 2) % (cnt - 1);
  }
  rep(i, n) {
    if (x[i] == 1) {
      def_ans_plus += cnt_plus[i];
      def_ans_plus = def_ans_plus % (cnt + 1);
      def_ans_minus += cnt_minus[i];
      def_ans_minus = def_ans_minus % (cnt - 1);
    }
  }
  rep(i, n) {
    if (x[n - 1 - i] == 0) {
      now[i + 1] = (def_ans_plus + cnt_plus[n - 1 - i]) % (cnt + 1);
    } else {
      int tmp = def_ans_minus - cnt_minus[n - 1 - i];
      if (tmp < 0)
        tmp += cnt - 1;
      now[i + 1] = tmp;
    }
  }
  bool end = false;
  while (end == false) {
    end = true;
    REP(i, n) {
      if (now[i] != 0) {
        now[i] = func(now[i]);
        ans[i]++;
        end = false;
      }
    }
  }
  REP(i, n) { cout << ans[i] << endl; }
}