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

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

int bitdiv(int c, vector<int> x, int N) {
  vector<int> d;
  while (c > 0) {
    d.push_back(c % 2);
    c /= 2;
  }
  int m = d.size();
  reverse(d.begin(), d.end());
  for (int i = 0; i < N - m + 1; i++) {
    int flag = 0;
    for (int j = 0; j < m; j++) {
      if (x[i + j] > d[j])
        break;
      else if (x[i + j] < d[j]) {
        flag = 1;
        break;
      }
    }
    if (i > 0)
      if (x[i - 1] == 1)
        flag = 0;
    if (flag == 0) {
      int carry = 0;
      for (int j = m - 1; j >= 0; j--) {
        if (x[i + j] >= (d[j] + carry)) {
          x[i + j] -= (d[j] + carry);
          carry = 0;
        } else {
          if (d[j] + carry - x[i + j] == 2)
            x[i + j] = 0;
          else
            x[i + j] = 1;
          carry = 1;
        }
      }
      // if(carry == 1) x[i-1] = 0;
    }
  }
  int ans = 0;
  for (int j = N - m; j < N; j++) {
    ans += x[j] * pow(2, N - j - 1);
  }
  return ans;
}

int fpop(int st) {
  int ans = 0;
  while (st > 0) {
    int d = 0, t;
    t = st;
    while (t > 0) {
      if (t % 2 == 1)
        d++;
      t /= 2;
    }
    st = st % d;
    ans++;
  }
  return ans;
}

int main() {
  int N, c = 0, er;
  cin >> N;
  string s;
  cin >> s;
  vector<int> X(N, 0);
  for (int i = 0; i < N; i++) {
    if (s[i] == '1') {
      c++;
      X[i] = 1;
      er = i;
    }
  }
  int cp = bitdiv(c + 1, X, N);
  int cn;
  if (c == 1)
    cn = 1;
  else
    cn = bitdiv(c - 1, X, N);
  vector<int> retp(N), retn(N);
  int tmp = 1;
  for (int i = 0; i < N; i++) {
    retp[i] = tmp % (c + 1);
    tmp = retp[i] * 2;
  }
  tmp = 1;
  if (c != 1) {
    for (int i = 0; i < N; i++) {
      retn[i] = tmp % (c - 1);
      tmp = retn[i] * 2;
    }
  }
  for (int i = 0; i < N; i++) {
    int st;
    if (X[i] == 1) {
      if (c != 1) {
        st = (cn - retn[N - i - 1]) % (c - 1);
        if (st < 0)
          st += (c - 1);
      }
    } else {
      st = (cp + retp[N - i - 1]) % (c + 1);
      if (st < 0)
        st += (c + 1);
    }

    if (c == 1 && er == i)
      cout << 0 << endl;
    else
      cout << fpop(st) + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int bitdiv(int c, vector<int> x, int N) {
  vector<int> d;
  while (c > 0) {
    d.push_back(c % 2);
    c /= 2;
  }
  int m = d.size();
  reverse(d.begin(), d.end());

  for (int i = 0; i < N - m + 1; i++) {
    int flag = 0;
    for (int j = 0; j < m; j++) {
      if (x[i + j] > d[j])
        break;
      else if (x[i + j] < d[j]) {
        flag = 1;
        break;
      }
    }
    if (i > 0)
      if (x[i - 1] == 1)
        flag = 0;
    if (flag == 0) {
      int carry = 0;
      for (int j = m - 1; j >= 0; j--) {
        if (x[i + j] >= (d[j] + carry)) {
          x[i + j] -= (d[j] + carry);
          carry = 0;
        } else {
          if (d[j] + carry - x[i + j] == 2)
            x[i + j] = 0;
          else
            x[i + j] = 1;
          carry = 1;
        }
      }
      // if(carry == 1) x[i-1] = 0;
    }
  }
  int ans = 0;
  for (int j = N - m; j < N; j++) {
    ans += x[j] * pow(2, N - j - 1);
  }
  return ans;
}

int fpop(int st) {
  int ans = 0;
  while (st > 0) {
    int d = 0, t;
    t = st;
    while (t > 0) {
      if (t % 2 == 1)
        d++;
      t /= 2;
    }
    st = st % d;
    ans++;
  }
  return ans;
}

int main() {
  int N, c = 0, er;
  cin >> N;
  string s;
  cin >> s;
  vector<int> X(N, 0);
  for (int i = 0; i < N; i++) {
    if (s[i] == '1') {
      c++;
      X[i] = 1;
      er = i;
    }
  }
  int cp = bitdiv(c + 1, X, N);
  int cn = bitdiv(c - 1, X, N);
  vector<int> retp(N), retn(N);
  int tmp = 1;
  for (int i = 0; i < N; i++) {
    retp[i] = tmp % (c + 1);
    tmp = retp[i] * 2;
  }
  tmp = 1;
  for (int i = 0; i < N; i++) {
    retn[i] = tmp % (c - 1);
    tmp = retn[i] * 2;
  }
  for (int i = 0; i < N; i++) {
    int st;
    if (X[i] == 1) {
      st = (cn - retn[N - i - 1]) % (c - 1);
      if (st < 0)
        st += (c - 1);
    } else {
      st = (cp + retp[N - i - 1]) % (c + 1);
      if (st < 0)
        st += (c + 1);
    }

    if (c == 1 && er == i)
      cout << 0 << endl;
    else
      cout << fpop(st) + 1 << endl;
  }
  return 0;
}