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

#include <algorithm>
#include <climits>
#include <iostream>
#include <numeric>
#include <vector>

constexpr int SINT_MAX = std::numeric_limits<int>::max();
constexpr int SINT_MIN = std::numeric_limits<int>::min();

using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;

template <class T> T choose(bool b, T t, T f) {
  if (b)
    return t;
  else
    return f;
}

const char *YesNo(bool b) { return choose(b, "Yes", "No"); }

const char *YESNO(bool b) { return choose(b, "YES", "NO"); }

template <class NumT> NumT diffabs(NumT l, NumT r) {
  if (l < r)
    return r - l;
  else
    return l - r;
}

struct myinout_t {
} io;

template <class T> myinout_t &operator>>(myinout_t &my, T &i) {
  cin >> i;
  return my;
}

myinout_t &operator>>(myinout_t &my, int &i) {
  int r = scanf("%d", &i);
  if (r != 1)
    exit(EXIT_FAILURE);
  return my;
}

myinout_t &operator>>(myinout_t &my, ll &i) {
  int r = scanf("%lld", &i);
  if (r != 1)
    exit(EXIT_FAILURE);
  return my;
}

template <class T> myinout_t &operator<<(myinout_t &my, const T &i) {
  cout << i;
  return my;
}

myinout_t &operator<<(myinout_t &my, int i) {
  printf("%d", i);
  return my;
}

myinout_t &operator<<(myinout_t &my, ll i) {
  printf("%lld", i);
  return my;
}

myinout_t &operator<<(myinout_t &my, double i) {
  printf("%.20f", i);
  return my;
}

constexpr char BR = '\\n';

///////////////////////////////////////////////////
#include <map>

int prepare(vi &shita, vi &ue, const string &s, int N) {
  int pop = 0;
  for (size_t i = 0; i < s.size(); i++)
    pop += (s[i] - '0');

  if (pop == 1)
    for (int i = 0; i <= N; i++)
      shita[i] = 0;

  int k_ue = 1, k_shita = 1;
  int m_ue = 0, m_shita = 0;

  size_t i = N;
  while (i-- > 0) {
    int ii = s[i] - '0';
    if (pop > 1) {
      shita[i] = k_shita;
      k_shita = (k_shita * 2) % (pop - 1);
      m_shita = (m_shita + ii * shita[i]) % (pop - 1);
    }

    ue[i] = k_ue;
    k_ue = (k_ue * 2) % (pop + 1);
    m_ue = (m_ue + ii * ue[i]) % (pop + 1);
  }

  shita[N] = m_shita;
  ue[N] = m_ue;
  return pop;
}

int main() {
  int N;
  string X;
  io >> N >> X;

  vi s(N + 1), u(N + 1);
  int pop = prepare(s, u, X, N);

  vi aans(N);

  for (int i = 0; i < N; i++) {
    int ans, mm;
    if (X[i] == '0') {
      int mod = pop + 1;
      ans = 1;
      mm = (u[N] + u[i]) % mod;
    } else if (pop == 1) {
      ans = 0;
      mm = 0;
    } else {
      int mod = pop - 1;
      ans = 1;
      mm = (s[N] - s[i] + mod) % mod;
    }

    while (mm > 0) {
      mm = mm % __builtin_popcount(mm);
      ++ans;
    }
    io << ans << BR;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

map<ll, ll> memo;
map<ll, ll> cnt_memo;

// assume x = 2;
ll pow(ll x, ll y, ll mod) {
  if (y == 0) {
    return 1;
  }
  ll ans;
  if (y % 2 == 0) {
    ans = pow(x * x % mod, y / 2, mod) % mod;
  } else {
    ans = x * pow(x, y - 1, mod) % mod;
  }
  return ans;
}

ll popcount(ll n) {
  ll cnt = 0;
  while (n > 0) {
    if (n % 2 == 1) {
      cnt++;
    }
    n /= 2;
  }
  return cnt;
}

ll search(ll n) {
  if (n == 0) {
    return 0;
  }
  if (cnt_memo.find(n) != cnt_memo.end()) {
    // cout << "Memo" << " " << n << endl;
    return cnt_memo[n];
  } else {
    ll div;
    if (memo.find(n) != memo.end()) {
      div = memo[n];
    } else {
      div = popcount(n);
      memo[n] = div;
    }
    ll next_n = n % div;
    ll ans;
    if (next_n == 0) {
      ans = 1;
    } else {
      ans = search(next_n) + 1;
    }
    cnt_memo[n] = ans;
    return ans;
  }
}

void Main() {
  ll N;
  cin >> N;
  string X;
  cin >> X;

  ll M = 0;
  rep(i, N) {
    ll tmp = X[i] - '0';
    M += tmp;
  }

  ll diff[3][N]; // mod M-1, modM, modM+1
  ll sum[3];
  rep(i, 3) { sum[i] = 0; }

  rep(i, N) {
    ll tmp = X[i] - '0';
    rep(j, 3) {
      ll mod = M + j - 1;
      ll n = pow(2, N - 1 - i, mod);
      if (tmp == 0) {
        diff[j][i] = n;
      } else {
        diff[j][i] = -n;
      }
      sum[j] = (sum[j] + tmp * n) % mod;
    }
  }

  rep(i, N) {
    ll tmp = X[i] - '0';
    if (tmp == 1) {
      if (M - 1 > 0) {
        ll num = (sum[0] - diff[0][i]) % (M - 1);
        if (num < 0) {
          num += (M - 1);
        }
        cout << search(num) + 1 << endl; // 1回すでに割ってあるので+1
      } else {
        // 1つしか1がなく，肝心のそいつが0になった場合割れない
        cout << 1 << endl;
      }
    } else {
      ll num = (sum[2] - diff[2][i]) % (M + 1);
      if (num < 0) {
        num += (M + 1);
      }
      cout << search(num) + 1 << endl; // 1回すでに割ってあるので+1
    }
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}