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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
int ans[200010];
int cur[2][200010];
int bitcount(int n) {
  int ans = 0;
  while (n != 0) {
    ans++;
    n &= n - 1;
  }
  return ans;
}
int rec(int n) {
  if (ans[n] != -1)
    return ans[n];
  int b = bitcount(n);
  return ans[n] = 1 + rec(n % b);
}
int main() {
  int N;
  string X;
  cin >> N >> X;
  int count = 0;
  //   cout << X.size() << endl;

  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      count++;
  }
  int o1 = 1;
  int o2 = 1;
  int S[2];
  memset(S, 0, sizeof(S));
  for (int i = N - 1; i >= 0; i--) {
    cur[0][i] = o1;
    cur[1][i] = o2;
    //      cout << o1 <<" "<< o2 << endl;
    if (X[i] == '1') {
      // cout << o1 << endl;
      S[0] += o1;
      S[0] %= (count + 1);
      if (count != 1) {
        S[1] += o2;
        S[1] %= (count - 1);
      }
    }
    o1 *= 2;
    o1 %= (count + 1);
    if (count != 1) {
      o2 *= 2;
      o2 %= (count - 1);
    }
  }
  //   cout << S[0] << " " << S[1]<<endl;

  memset(ans, -1, sizeof(ans));
  ans[0] = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      int nxt = S[1] + (count - 1) - cur[1][i];
      if (count == 1) {
        cout << 0 << endl;
      } else {
        nxt %= (count - 1);
        // cout << "nxt:" << nxt << endl;
        cout << 1 + rec(nxt) << endl;
      }
    } else {
      int nxt = S[0] + (count + 1) + cur[0][i];
      //         cout << S[0] << " " << count+1 << " " << cur[0][i];
      nxt %= (count + 1);
      // cout << "nxt: " << nxt << endl;
      cout << 1 + rec(nxt) << endl;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
int ans[200010];
int cur[2][200010];
int bitcount(int n) {
  int ans = 0;
  while (n != 0) {
    ans++;
    n &= n - 1;
  }
  return ans;
}
int rec(int n) {
  if (ans[n] != -1)
    return ans[n];
  int b = bitcount(n);
  return ans[n] = 1 + rec(n % b);
}
int main() {
  int N;
  string X;
  cin >> N >> X;
  int count = 0;
  //   cout << X.size() << endl;

  for (int i = 0; i < N; i++) {
    if (X[i] == '1')
      count++;
  }
  int o1 = 1;
  int o2 = 1;
  int S[2];
  memset(S, 0, sizeof(S));
  for (int i = N - 1; i >= 0; i--) {
    cur[0][i] = o1;
    cur[1][i] = o2;
    //      cout << o1 <<" "<< o2 << endl;
    if (X[i] == '1') {
      // cout << o1 << endl;
      S[0] += o1;
      S[0] %= (count + 1);
      S[1] += o2;
      S[1] %= (count - 1);
    }
    o1 *= 2;
    o2 *= 2;
    o1 %= (count + 1);
    o2 %= (count - 1);
  }
  //   cout << S[0] << " " << S[1]<<endl;

  memset(ans, -1, sizeof(ans));
  ans[0] = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      int nxt = S[1] + (count - 1) - cur[1][i];
      nxt %= (count - 1);
      // cout << "nxt:" << nxt << endl;
      cout << 1 + rec(nxt) << endl;
    } else {
      int nxt = S[0] + (count + 1) + cur[0][i];
      //         cout << S[0] << " " << count+1 << " " << cur[0][i];
      nxt %= (count + 1);
      // cout << "nxt: " << nxt << endl;
      cout << 1 + rec(nxt) << endl;
    }
  }
  return 0;
}