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

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

const int N = 200005;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios::sync_with_stdio(false), cin.tie(0);

  int n;
  cin >> n;
  string s;
  cin >> s;
  cin >> s;
  int mod = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      mod++;
  }
  //	cout << mod << endl;
  int modplusrest[n];
  int modmoinsrest[n];
  int indp = 1;
  int indm = 1;
  if (mod == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << endl;
    }
  } else {
    for (int i = n - 1; i > -1; i--) {
      modplusrest[i] = indp;
      //		cout << modplusrest[i] << " ";
      modmoinsrest[i] = indm;
      indp = (indp * 2) % (mod + 1);
      //		cout << indp << endl;
      if (mod > 1)
        indm = indm * 2 % (mod - 1);
    }
    int mp = 0;
    int mm = 0;
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') {
        mp = (mp + modplusrest[j]) % (mod + 1);
        if (mod > 1)
          mm = (mm + modmoinsrest[j]) % (mod - 1);
      }
    }
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        int m = mp;
        m = (m + modplusrest[i]) % (mod + 1);
        //			cout << "m = " << m << " ";
        int ans = 1;
        while (m != 0) {
          int p = __builtin_popcount(m);
          m = m % p;
          ans++;
        }
        cout << ans << endl;
      } else if (mod != 1) {
        int m = mm;
        m = (mod - 1 + (m - modmoinsrest[i]) % (mod - 1)) % (mod - 1);
        int ans = 1;
        while (m != 0) {
          int p = __builtin_popcount(m);
          m = m % p;
          ans++;
        }
        cout << ans << endl;

      } else {
        cout << 0 << endl;
      }
    }
  }
  return 0;
}

Error: -11

Fixed submission:

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

const int N = 200005;

int main() {

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  ios::sync_with_stdio(false), cin.tie(0);

  int n;
  cin >> n;
  string s;
  cin >> s;
  cin >> s;
  int mod = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      mod++;
  }
  //	cout << mod << endl;
  int modplusrest[n];
  int modmoinsrest[n];
  int indp = 1;
  int indm = 1;
  if (mod == 0) {
    for (int i = 0; i < n; i++) {
      cout << 0 << endl;
    }
  } else {
    for (int i = n - 1; i > -1; i--) {
      modplusrest[i] = indp;
      //		cout << modplusrest[i] << " ";
      modmoinsrest[i] = indm;
      indp = (indp * 2) % (mod + 1);
      //		cout << indp << endl;
      if (mod > 1)
        indm = indm * 2 % (mod - 1);
    }
    int mp = 0;
    int mm = 0;
    for (int j = 0; j < n; j++) {
      if (s[j] == '1') {
        mp = (mp + modplusrest[j]) % (mod + 1);
        mm = (mm + modmoinsrest[j]) % (mod - 1);
      }
    }
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        int m = mp;
        m = (m + modplusrest[i]) % (mod + 1);
        //			cout << "m = " << m << " ";
        int ans = 1;
        while (m != 0) {
          int p = __builtin_popcount(m);
          m = m % p;
          ans++;
        }
        cout << ans << endl;
      } else if (mod != 1) {
        int m = mm;
        m = (mod - 1 + (m - modmoinsrest[i]) % (mod - 1)) % (mod - 1);
        int ans = 1;
        while (m != 0) {
          int p = __builtin_popcount(m);
          m = m % p;
          ans++;
        }
        cout << ans << endl;

      } else {
        cout << 0 << endl;
      }
    }
  }
  return 0;
}