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

#include "bits/stdc++.h"

#define REP(i, num) for (ll i = 0; i < (num); ++i)
#define FOR(i, c, num) for (ll(i) = (c); (i) < (num); ++(i))
#define LOOP(i) while (i--)
#define ALL(c) c.begin(), c.end()
#define PRINTALL(c)                                                            \\
  for (auto pitr = c.begin(); pitr != c.end(); ++pitr) {                       \\
    cout << *pitr;                                                             \\
    if (next(pitr, 1) != c.end())                                              \\
      cout << ' ';                                                             \\
  }                                                                            \\
  cout << endl;
#define PAIRCOMP(c, comp)                                                      \\
  [](const pair<ll, ll> &lhs, const pair<ll, ll> &rhs) {                       \\
    return lhs.c comp rhs.c;                                                   \\
  }

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

constexpr ll atcoder_mod = 1e9 + 7;

template <typename T = ll> T in() {
  T x;
  cin >> x;
  return (x);
}
template <typename T = ll, typename C = vector<T>> C vecin(int N) {
  C x(N);
  REP(i, N) { x[i] = in<T>(); }
  return x;
}

void vout() { cout << endl; }
template <typename Head, typename... Tail> void vout(Head &&h, Tail &&...t) {
  cout << ' ' << h;
  vout(forward<Tail>(t)...);
}
void out() { cout << endl; }
template <typename Head, typename... Tail> void out(Head &&h, Tail &&...t) {
  cout << h;
  vout(forward<Tail>(t)...);
}

template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vll M;

ll powermod(ll a, ll b, ll m) {
  if (b == 0) {
    return 1LL;
  }
  if (b % 2) {
    return (a * powermod(a, b - 1LL, m)) % m;
  }
  return powermod((a * a) % m, b / 2LL, m);
}

ll dfs(ll v) {
  if (M[v] >= 0)
    return M[v];

  ll B = 0, V = v;
  while (V) {
    if (V & 1)
      B++;
    V /= 2;
  }

  if (!B) {
    M[v] = 0;
    return 0;
  }
  if (!(v % B)) {
    M[v] = 1;
    return 1;
  }

  ll C = dfs(v % B) + 1;
  M[v] = C;
  return C;
}

ll safemod(ll a, ll m) { return (a % m + m) % m; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  auto N = in();
  string X = in<string>();
  M.resize(N + 1, -1);
  ll C = 0;
  REP(i, N) {
    if (X[i] == '1') {
      C++;
    }
  }
  ll modP = 0, modD = 0;
  if (C < N) {
    REP(j, N) {
      if (X[N - 1 - j] == '1') {
        modP += powermod(2, j, C + 1);
        modP %= (C + 1);
      }
    }
  }
  if (C > 1) {
    REP(j, N) {
      if (X[N - 1 - j] == '1') {
        modD += powermod(2, j, C - 1);
        modD %= (C - 1);
      }
    }
  }

  ll K[2] = {C - 1, C + 1};
  REP(i, N) {
    ll B;
    auto T = X;
    ll mod = 0;
    if (T[i] == '0') {
      T[i] = '1';
      B = 1;
      mod = modP;
      mod += powermod(2, N - 1 - i, K[B]);
      mod %= K[B];
    } else {
      T[i] = '0';
      B = 0;
      if (K[B] == 0) {
        out(0);
        continue;
      }
      mod = modD;
      mod -= powermod(2, N - 1 - i, K[B]);
      mod = safemod(mod, K[B]);
    }

    ll Z = dfs(mod) + 1;
    out(Z);
  }

  return 0;
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"

#define REP(i, num) for (ll i = 0; i < (num); ++i)
#define FOR(i, c, num) for (ll(i) = (c); (i) < (num); ++(i))
#define LOOP(i) while (i--)
#define ALL(c) c.begin(), c.end()
#define PRINTALL(c)                                                            \\
  for (auto pitr = c.begin(); pitr != c.end(); ++pitr) {                       \\
    cout << *pitr;                                                             \\
    if (next(pitr, 1) != c.end())                                              \\
      cout << ' ';                                                             \\
  }                                                                            \\
  cout << endl;
#define PAIRCOMP(c, comp)                                                      \\
  [](const pair<ll, ll> &lhs, const pair<ll, ll> &rhs) {                       \\
    return lhs.c comp rhs.c;                                                   \\
  }

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

constexpr ll atcoder_mod = 1e9 + 7;

template <typename T = ll> T in() {
  T x;
  cin >> x;
  return (x);
}
template <typename T = ll, typename C = vector<T>> C vecin(int N) {
  C x(N);
  REP(i, N) { x[i] = in<T>(); }
  return x;
}

void vout() { cout << endl; }
template <typename Head, typename... Tail> void vout(Head &&h, Tail &&...t) {
  cout << ' ' << h;
  vout(forward<Tail>(t)...);
}
void out() { cout << endl; }
template <typename Head, typename... Tail> void out(Head &&h, Tail &&...t) {
  cout << h;
  vout(forward<Tail>(t)...);
}

template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

vll M;

ll powermod(ll a, ll b, ll m) {
  if (b == 0) {
    return 1LL;
  }
  if (b % 2) {
    return (a * powermod(a, b - 1LL, m)) % m;
  }
  return powermod((a * a) % m, b / 2LL, m);
}

ll dfs(ll v) {
  if (M[v] >= 0)
    return M[v];

  ll B = 0, V = v;
  while (V) {
    if (V & 1)
      B++;
    V /= 2;
  }

  if (!B) {
    M[v] = 0;
    return 0;
  }
  if (!(v % B)) {
    M[v] = 1;
    return 1;
  }

  ll C = dfs(v % B) + 1;
  M[v] = C;
  return C;
}

ll safemod(ll a, ll m) { return (a % m + m) % m; }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  auto N = in();
  string X = in<string>();
  M.resize(N + 1, -1);
  ll C = 0;
  REP(i, N) {
    if (X[i] == '1') {
      C++;
    }
  }
  ll modP = 0, modD = 0;
  if (C < N) {
    REP(j, N) {
      if (X[N - 1 - j] == '1') {
        modP += powermod(2, j, C + 1);
        modP %= (C + 1);
      }
    }
  }
  if (C > 0) {
    REP(j, N) {
      if (X[N - 1 - j] == '1') {
        modD += powermod(2, j, C - 1);
        modD %= (C - 1);
      }
    }
  }

  ll K[2] = {C - 1, C + 1};
  REP(i, N) {
    ll B;
    auto T = X;
    ll mod = 0;
    if (T[i] == '0') {
      T[i] = '1';
      B = 1;
      mod = modP;
      mod += powermod(2, N - 1 - i, K[B]);
      mod %= K[B];
    } else {
      T[i] = '0';
      B = 0;
      mod = modD;
      mod -= powermod(2, N - 1 - i, K[B]);
      mod = safemod(mod, K[B]);
    }

    ll Z = dfs(mod) + 1;
    out(Z);
  }

  return 0;
}