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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \\
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

int popcount_str(string s) {
  int ans = 0;
  rep(i, s.size()) ans += (s[i] == '1');
  return ans;
}
int popcount(ll x) {
  int ans = 0;
  while (x > 0) {
    ans += x % 2;
    x /= 2;
  }
  return ans;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  string x;
  cin >> n >> x;
  vector<int> memo(n + 1);
  vector<int> one(n);
  int k = popcount_str(x), modm = 0, modp = 0;
  ll deb = 0;
  rep(i, n) {
    deb = (deb * 2 + (x[i] == '1'));
    if (k != 1)
      modm = (modm * 2 + (x[i] == '1')) % (k - 1);
    modp = (modp * 2 + (x[i] == '1')) % (k + 1);
  }
  eprintf("%lld %d %d %d\\n", deb, k, modp, modm);
  int flipm = 1, flipp = 1;
  reprev(i, n) {
    if (x[i] == '1' && k == 1)
      one[i] = -1;
    else if (x[i] == '1') {
      one[i] = (modm - flipm + (k - 1)) % (k - 1);
    } else {
      one[i] = (modp + flipp + (k + 1)) % (k + 1);
    }
    eprintf("%d ", one[i]);
    if (k != 1)
      flipm = (flipm * 2) % (k - 1);
    flipp = (flipp * 2) % (k + 1);
  }
  vector<int> ans(n);
  rep(i, n) {
    if (one[i] == 0)
      ans[i] = 1;
    else if (one[i] == -1)
      ans[i] = 0;
    else if (memo[one[i]])
      ans[i] = memo[one[i]];
    else {
      int tmp = 1, y = one[i];
      while (y) {
        y %= popcount(y);
        tmp++;
      }
      ans[i] = memo[one[i]] = tmp;
    }
  }
  rep(i, n) cout << ans[i] << "\\n";
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#ifdef LOCAL
#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
#define eprintf(...) 42
#endif

#define rep_(i, a_, b_, a, b, ...)                                             \\
  for (int i = (a), i##_len = (b); i < i##_len; ++i)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define reprev_(i, a_, b_, a, b, ...)                                          \\
  for (int i = (b - 1), i##_min = (a); i >= i##_min; --i)
#define reprev(i, ...) reprev_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
// template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef long double ld;

int popcount_str(string s) {
  int ans = 0;
  rep(i, s.size()) ans += (s[i] == '1');
  return ans;
}
int popcount(ll x) {
  int ans = 0;
  while (x > 0) {
    ans += x % 2;
    x /= 2;
  }
  return ans;
}

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  string x;
  cin >> n >> x;
  vector<int> memo(n + 1);
  vector<int> one(n);
  int k = popcount_str(x), modm = 0, modp = 0;
  ll deb = 0;
  rep(i, n) {
    deb = (deb * 2 + (x[i] == '1'));
    modm = (modm * 2 + (x[i] == '1')) % (k - 1);
    modp = (modp * 2 + (x[i] == '1')) % (k + 1);
  }
  eprintf("%lld %d %d %d\\n", deb, k, modp, modm);
  int flipm = 1, flipp = 1;
  reprev(i, n) {
    if (x[i] == '1') {
      one[i] = (modm - flipm + (k - 1)) % (k - 1);
    } else {
      one[i] = (modp + flipp + (k + 1)) % (k + 1);
    }
    eprintf("%d ", one[i]);
    flipm = (flipm * 2) % (k - 1);
    flipp = (flipp * 2) % (k + 1);
  }
  vector<int> ans(n);
  rep(i, n) {
    if (one[i] == 0)
      ans[i] = 1;
    else if (memo[one[i]])
      ans[i] = memo[one[i]];
    else {
      int tmp = 1, y = one[i];
      while (y) {
        y %= popcount(y);
        tmp++;
      }
      ans[i] = memo[one[i]] = tmp;
    }
  }
  rep(i, n) cout << ans[i] << "\\n";
}