Problem p02609 (C++)

Buggy submission (status: Runtime Error):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class DAnythingGoesToZero {
public:
  void solve(istream &cin, ostream &cout) {
    SPEED;
    int n;
    cin >> n;
    string s;
    cin >> s;
    vector<int> f(n + 1);
    for (int i = 1; i <= n; i++) {
      int p = __builtin_popcount(i);
      f[i] = 1 + f[i % p];
    }

    int cnt = 0;
    rep(i, n) {
      if (s[i] == '1') {
        cnt++;
      }
    }

    vector<int> mod(3);
    mod[0] = max(1, cnt - 1);
    mod[1] = cnt;
    mod[2] = cnt + 1;

    vector<vector<int>> beki(n + 1, vector<int>(3));
    rep(i, 3) { beki[0][i] = 1 % mod[i]; }

    for (int i = 0; i < n; i++) {
      rep(j, 3) { beki[i + 1][j] = beki[i][j] * 2 % mod[j]; }
    }

    vector<int> x(3);
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        rep(j, 3) {
          x[j] += beki[n - 1 - i][j];
          x[j] %= mod[j];
        }
      }
    }

    for (int i = 0; i < n; i++) {
      int j = 1;
      int now = 0;
      if (s[i] == '0') {
        j = 2;
        now = x[j] + beki[n - 1 - i][j];
        now %= mod[j];
      } else {
        j = 0;
        now = x[j] - beki[n - 1 - i][j] + mod[j];
        now %= mod[j];
        if (cnt == 1) {
          cout << 0 << '\n';
          continue;
        }
      }
      cout << 1 + f[now] << '\n';
    }
  }
};

signed main() {
  DAnythingGoesToZero solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}

Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;

constexpr long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

class DAnythingGoesToZero {
public:
  void solve(istream &cin, ostream &cout) {
    SPEED;
    int n;
    cin >> n;
    string s;
    cin >> s;
    vector<int> f(n + 1);
    for (int i = 1; i <= n; i++) {
      int p = __builtin_popcount(i);
      f[i] = 1 + f[i % p];
    }

    int cnt = 0;
    rep(i, n) {
      if (s[i] == '1') {
        cnt++;
      }
    }

    vector<int> mod(3);
    mod[0] = max(1, cnt - 1);
    mod[1] = max(1, cnt);
    mod[2] = cnt + 1;

    vector<vector<int>> beki(n + 1, vector<int>(3));
    rep(i, 3) { beki[0][i] = 1 % mod[i]; }

    for (int i = 0; i < n; i++) {
      rep(j, 3) { beki[i + 1][j] = beki[i][j] * 2 % mod[j]; }
    }

    vector<int> x(3);
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        rep(j, 3) {
          x[j] += beki[n - 1 - i][j];
          x[j] %= mod[j];
        }
      }
    }

    for (int i = 0; i < n; i++) {
      int j = 1;
      int now = 0;
      if (s[i] == '0') {
        j = 2;
        now = x[j] + beki[n - 1 - i][j];
        now %= mod[j];
      } else {
        j = 0;
        now = x[j] - beki[n - 1 - i][j] + mod[j];
        now %= mod[j];
        if (cnt == 1) {
          cout << 0 << '\n';
          continue;
        }
      }
      cout << 1 + f[now] << '\n';
    }
  }
};

signed main() {
  DAnythingGoesToZero solver;
  std::istream &in(std::cin);
  std::ostream &out(std::cout);
  solver.solve(in, out);
  return 0;
}