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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
inline int popcount(const int x) { return __builtin_popcount(x); }
inline ll popcount(const ll x) { return __builtin_popcountll(x); }
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class T> void drop(const T &x) {
  std::cout << x << endl;
  exit(0);
}
void debug_out() { std::cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  std::cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

struct InitIO {
  InitIO() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
  }
} init_io;

map<ll, int> mp;

int dfs(ll y) {
  if (y == 0)
    return 0;
  if (y == 0)
    return 1;
  if (mp.count(y))
    return mp[y];
  ll next = y % popcount(y);
  mp[y] = dfs(next) + 1;
  return mp[y];
}

int main() {
  int n;
  string X;
  cin >> n >> X;
  int cnt = 0;
  rep(i, n) {
    if (X[i] == '1')
      cnt++;
  }
  ll modpop1 = 0, modpop2 = 0;
  ll two1 = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (cnt - 1 == 0)
      break;
    if (X[i] == '1') {
      modpop1 += two1;
      modpop1 %= (cnt - 1);
    }
    two1 *= 2LL;
    two1 %= (cnt - 1);
  }
  ll two2 = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (X[i] == '1') {
      modpop2 += two2;
      modpop2 %= (cnt + 1);
    }
    two2 *= 2LL;
    two2 %= (cnt + 1);
  }

  // debug(modpop1, modpop2);
  two1 = 1LL, two2 = 1LL;
  vector<int> ans(n);
  for (int i = n - 1; i >= 0; --i) {
    ll y = 0;
    if (X[i] == '1') {
      if (cnt - 1 == 0) {
        ans[i] = 0;
        continue;
      }
      y = modpop1 - two1;
      y %= (cnt - 1);
      if (y < 0)
        y += ll(cnt - 1);
    } else {
      y = modpop2 + two2;
      y %= (cnt + 1);
    }
    if (cnt - 1 != 0) {
      two1 *= 2LL;
      two1 %= (cnt - 1);
    }
    two2 *= 2LL;
    two2 %= (cnt + 1);
    ans[i] = dfs(y) + 1;
    // debug(y, ans[i]);
  }
  rep(i, n) cout << ans[i] << "\\n";
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, m, n) for (int i = m; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
inline int popcount(const int x) { return __builtin_popcount(x); }
inline ll popcount(const ll x) { return __builtin_popcountll(x); }
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
template <class T> void drop(const T &x) {
  std::cout << x << endl;
  exit(0);
}
void debug_out() { std::cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  std::cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

struct InitIO {
  InitIO() {
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
  }
} init_io;

map<ll, int> mp;

int dfs(ll y) {
  if (y == 0)
    return 0;
  if (y == 0)
    return 1;
  if (mp.count(y))
    return mp[y];
  ll next = y % popcount(y);
  mp[y] = dfs(next) + 1;
  return mp[y];
}

int main() {
  int n;
  string X;
  cin >> n >> X;
  int cnt = 0;
  rep(i, n) {
    if (X[i] == '1')
      cnt++;
  }
  ll modpop1 = 0, modpop2 = 0;
  ll two1 = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (X[i] == '1') {
      modpop1 += two1;
      modpop1 %= (cnt - 1);
    }
    two1 *= 2LL;
    two1 %= (cnt - 1);
  }
  ll two2 = 1;
  for (int i = n - 1; i >= 0; --i) {
    if (X[i] == '1') {
      modpop2 += two2;
      modpop2 %= (cnt + 1);
    }
    two2 *= 2LL;
    two2 %= (cnt + 1);
  }

  // debug(modpop1, modpop2);
  two1 = 1LL, two2 = 1LL;
  vector<int> ans(n);
  for (int i = n - 1; i >= 0; --i) {
    ll y = 0;
    if (X[i] == '1') {
      y = modpop1 - two1;
      y %= (cnt - 1);
      if (y < 0)
        y += ll(cnt - 1);
    } else {
      y = modpop2 + two2;
      y %= (cnt + 1);
    }
    two1 *= 2LL;
    two1 %= (cnt - 1);
    two2 *= 2LL;
    two2 %= (cnt + 1);
    ans[i] = dfs(y) + 1;
    // debug(y, ans[i]);
  }
  rep(i, n) cout << ans[i] << "\\n";
  return 0;
}