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

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  ll N;
  cin >> N;
  string S;
  cin >> S;
  ll onenum = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      onenum++;
    }
  }
  ll d[3][N];
  d[0][0] = 1;
  d[1][0] = 1;
  d[2][0] = 1;

  if (onenum == 1) {
    for (ll i = 2; i < 3; i++) {
      ll a = 1;
      for (ll j = 1; j < N; j++) {
        a *= 2;
        a %= (onenum + i - 1);
        d[i][j] = a;
      }
    }

    ll sum4 = 0;
    for (ll i = N - 1; i >= 0; i--) {
      if (S[(N - 1) - i] == '1') {

        sum4 += d[2][i];
      }
      sum4 %= onenum + 1;
    }
    for (ll i = 0; i < N; i++) {
      ll s;
      if (S[i] == '1') {
        s = 0;
        cout << 0 << endl;
        continue;
      } else {
        s = (sum4 + d[2][N - 1 - i]) % (onenum + 1);
      }
      ll cnt = 1;
      while (s != 0) {
        int e = __builtin_popcount(s);
        s = s % e;
        cnt++;
      }
      cout << cnt << endl;
    }
    return 0;
  }
  if (onenum == 0) {
    for (ll i = 0; i < N; i++) {
      cout << 1 << endl;
    }
    return 0;
  }
  for (ll i = 0; i < 3; i++) {
    ll a = 1;
    for (ll j = 1; j < N; j++) {
      a *= 2;
      a %= (onenum + i - 1);
      d[i][j] = a;
    }
  }
  ll sum1_ = 0;
  ll sum2_ = 0;
  ll sum3_ = 0;
  for (ll i = N - 1; i >= 0; i--) {
    if (S[(N - 1) - i] == '1') {
      sum1_ += d[0][i];
      sum2_ += d[1][i];
      sum3_ += d[2][i];
    }
    sum1_ %= onenum - 1;
    sum2_ %= onenum;
    sum3_ %= onenum + 1;
  }
  for (ll i = 0; i < N; i++) {
    ll s;
    if (S[i] == '1') {
      s = (sum1_ - d[0][N - 1 - i]) % (onenum - 1);
      if (s < 0) {
        s += (onenum - 1);
      }
    } else {
      s = (sum3_ + d[2][N - 1 - i]) % (onenum + 1);
    }
    ll cnt = 1;
    while (s != 0) {
      int e = __builtin_popcount(s);
      s = s % e;
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define F first
#define S second
using namespace std;
using ll = long long;
typedef pair<int, int> P;
ll Mod = 1000000007;
int main() {
  int N;
  cin >> N;
  string S;
  cin >> S;
  int onenum = 0;
  for (int i = 0; i < N; i++) {
    if (S[i] == '1') {
      onenum++;
    }
  }
  ll d[3][N];
  d[0][0] = 1;
  d[1][0] = 1;
  d[2][0] = 1;
  for (int i = 0; i < 3; i++) {
    ll a = 1;
    for (int j = 1; j < N; j++) {
      a *= 2;
      a %= (onenum + i - 1);
      d[i][j] = a;
    }
  }
  int sum1 = 0;
  int sum2 = 0;
  int sum3 = 0;
  for (int i = N - 1; i >= 0; i--) {
    if (S[(N - 1) - i] == '1') {
      sum1 += d[0][i];
      sum2 += d[1][i];
      sum3 += d[2][i];
    }
    sum1 %= onenum - 1;
    sum2 %= onenum;
    sum3 %= onenum + 1;
  }
  for (int i = 0; i < N; i++) {
    int s;
    if (S[i] == '1') {
      s = (sum1 - d[0][N - 1 - i]) % (onenum - 1);
      if (s < 0) {
        s += (onenum - 1);
      }
    } else {
      s = (sum3 + d[2][N - 1 - i]) % (onenum + 1);
    }
    int cnt = 1;
    while (s != 0) {
      int e = __builtin_popcount(s);
      s = s % e;
      cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}