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

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define all(x) x.begin(), x.end()
#define STRINGIFY(n) #n
#define TOSTRING(n) STRINGIFY(n)
#define PREFIX "#" TOSTRING(__LINE__) "| "
#define debug(x)                                                               \\
  { std::cout << PREFIX << #x << " = " << x << std::endl; }

std::ostream &output_indent(std::ostream &os, int ind) {
  for (int i = 0; i < ind; i++)
    os << " ";
  return os;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p);
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v);

template <class S, class T>
std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p) {
  return (os << "(" << p.first << ", " << p.second << ")");
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size(); i++)
    os << v[i] << ", ";
  return (os << "]");
}

template <class T> static inline std::vector<T> ndvec(size_t &&n, T val) {
  return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail> static inline auto ndvec(size_t &&n, Tail &&...tail) {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}
template <class Cond> struct chain {
  Cond cond;
  chain(Cond cond) : cond(cond) {}
  template <class T> bool operator()(T &a, const T &b) const {
    if (cond(a, b)) {
      a = b;
      return true;
    }
    return false;
  }
};
template <class Cond> chain<Cond> make_chain(Cond cond) {
  return chain<Cond>(cond);
}

i64 solve(i64 X) {
  i64 ans = 0;
  while (X > 0) {
    X %= __builtin_popcountll(X);
    ans++;
  }
  return ans;
}

int main() {
  i64 N;
  cin >> N;
  string X;
  cin >> X;
  reverse(all(X));

  i64 pc = 0;
  for (auto x : X) {
    if (x == '1')
      pc++;
  }

  vector<i64> up;
  i64 up_value = 0;
  {
    i64 now = 1;
    rep(i, 0, N) {
      up.push_back(now % (pc + 1));
      if (X[i] == '1') {
        up_value += now;
        up_value %= (pc + 1);
      }
      now *= 2;
      now %= (pc + 1);
    }
  }
  vector<i64> down;
  i64 down_value = 0;
  if (pc <= 1) {
    down.assign(N, -1);
    down_value = -1;
  } else {
    i64 now = 1;
    rep(i, 0, N) {
      down.push_back(now % (pc - 1));
      if (X[i] == '1') {
        down_value += now;
        down_value %= (pc - 1);
      }
      now *= 2;
      now %= (pc - 1);
    }
  }
  vector<i64> vec(N);
  vector<i64> ans(N, -1);
  rep(i, 0, N) {
    if (X[i] == '0') {
      // up
      vec[i] = (up_value + up[i]) % (pc + 1);
      ans[i] = solve(vec[i]) + 1;
    } else if (X[i] == '1') {
      if (down_value == -1) {
        ans[i] = 0;
      } else {
        vec[i] = (down_value - down[i] + (pc - 1)) % (pc - 1);
        ans[i] = solve(vec[i]) + 1;
      }
    }
  }
  reverse(all(ans));
  rep(i, 0, N) { cout << ans[i] << "\\n"; }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define all(x) x.begin(), x.end()
#define STRINGIFY(n) #n
#define TOSTRING(n) STRINGIFY(n)
#define PREFIX "#" TOSTRING(__LINE__) "| "
#define debug(x)                                                               \\
  { std::cout << PREFIX << #x << " = " << x << std::endl; }

std::ostream &output_indent(std::ostream &os, int ind) {
  for (int i = 0; i < ind; i++)
    os << " ";
  return os;
}

template <class S, class T>
std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p);
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v);

template <class S, class T>
std::ostream &operator<<(std::ostream &os, const std::pair<S, T> &p) {
  return (os << "(" << p.first << ", " << p.second << ")");
}
template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &v) {
  os << "[";
  for (int i = 0; i < v.size(); i++)
    os << v[i] << ", ";
  return (os << "]");
}

template <class T> static inline std::vector<T> ndvec(size_t &&n, T val) {
  return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail> static inline auto ndvec(size_t &&n, Tail &&...tail) {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}
template <class Cond> struct chain {
  Cond cond;
  chain(Cond cond) : cond(cond) {}
  template <class T> bool operator()(T &a, const T &b) const {
    if (cond(a, b)) {
      a = b;
      return true;
    }
    return false;
  }
};
template <class Cond> chain<Cond> make_chain(Cond cond) {
  return chain<Cond>(cond);
}

i64 solve(i64 X) {
  i64 ans = 0;
  while (X > 0) {
    X %= __builtin_popcountll(X);
    ans++;
  }
  return ans;
}

int main() {
  i64 N;
  cin >> N;
  string X;
  cin >> X;
  reverse(all(X));

  i64 pc = 0;
  for (auto x : X) {
    if (x == '1')
      pc++;
  }

  vector<i64> up;
  i64 up_value = 0;
  {
    i64 now = 1;
    rep(i, 0, N) {
      up.push_back(now % (pc + 1));
      if (X[i] == '1') {
        up_value += now;
        up_value %= (pc + 1);
      }
      now *= 2;
      now %= (pc + 1);
    }
  }
  vector<i64> down;
  i64 down_value = 0;
  if (pc <= 1) {
    down.assign(N, -1);
    i64 down_value = -1;
  } else {
    i64 now = 1;
    rep(i, 0, N) {
      down.push_back(now % (pc - 1));
      if (X[i] == '1') {
        down_value += now;
        down_value %= (pc - 1);
      }
      now *= 2;
      now %= (pc - 1);
    }
  }
  vector<i64> vec(N);
  vector<i64> ans(N, -1);
  rep(i, 0, N) {
    if (X[i] == '0') {
      // up
      vec[i] = (up_value + up[i]) % (pc + 1);
      ans[i] = solve(vec[i]) + 1;
    } else if (X[i] == '1') {
      if (down_value == -1) {
        ans[i] = 0;
      } else {
        vec[i] = (down_value - down[i] + (pc - 1)) % (pc - 1);
        ans[i] = solve(vec[i]) + 1;
      }
    }
  }
  reverse(all(ans));
  rep(i, 0, N) { cout << ans[i] << "\\n"; }
}