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

#pragma region kyomukyomupurin

/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-07-12 12:50:00
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

template <class T> inline void eraque(std::vector<T> &vec) {
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  return;
}

template <class T> inline int lower_position(const std::vector<T> &vec, T val) {
  return static_cast<int>(std::distance(
      vec.begin(), std::lower_bound(vec.begin(), vec.end(), val)));
}

template <class T> inline int upper_position(const std::vector<T> &vec, T val) {
  return static_cast<int>(std::distance(
      vec.begin(), std::upper_bound(vec.begin(), vec.end(), val)));
}

template <class T> inline std::string to_binary(T n) {
  assert(n > 0);
  std::string ret = "";
  while (n)
    ret += (n & 1) ? '1' : '0', n >>= 1;
  std::reverse(ret.begin(), ret.end());
  return ret;
}

template <class T> inline void println(T val) { std::cout << val << '\\n'; }

template <class T>
using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

template <class Tuple, std::size_t... Is>
void tuple_out(std::ostream &os, const Tuple &tp, std::index_sequence<Is...>) {
  ((os << (Is ? ", " : "(") << std::get<Is>(tp)), ...) << ")";
}

template <class... Args>
std::ostream &operator<<(std::ostream &os, const std::tuple<Args...> &tp) {
  tuple_out(os, tp, std::index_sequence_for<Args...>{});
  return os;
}

template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
  int n = 0;
  for (auto e : vec)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T, class Compare>
std::ostream &operator<<(std::ostream &os, const std::set<T, Compare> &st) {
  int n = 0;
  for (auto e : st)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T, class U, class Compare>
std::ostream &operator<<(std::ostream &os, const std::map<T, U, Compare> &mp) {
  int n = 0;
  for (auto e : mp)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
  for (T &e : vec)
    is >> e;
  return is;
}

template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class Tuple, std::size_t... Is>
void tuple_in(std::istream &is, Tuple &tp, std::index_sequence<Is...>) {
  ((is >> std::get<Is>(tp)), ...);
}

template <class... Args>
std::istream &operator>>(std::istream &is, std::tuple<Args...> &tp) {
  tuple_in(is, tp, std::index_sequence_for<Args...>{});
  return is;
}

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << '\\n'; }

template <class Head, class... Tail>
void debug_out(Head &&head, Tail &&...tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0)
    std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

#pragma endregion kyomukyomupurin

template <class T, class U> T mod_pow(T x, U n, T mod) {
  T res = 1;
  while (n > 0) {
    if (n & 1)
      (res *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  string s;
  cin >> s;
  int pcnt = count(all(s), '1');

  if (pcnt == 0) {
    for (int i = 0; i < n; ++i)
      cout << 1 << '\\n';
    return 0;
  } else if (pcnt == 1) {
    if (s.back() == '1') {
      for (int i = 0; i < n - 1; ++i)
        cout << 2 << '\\n';
      cout << 0 << '\\n';
    } else {
      for (int i = 0; i < n; ++i) {
        if (i == n - 1) {
          cout << 2 << '\\n';
        } else if (s[i] == '0') {
          cout << 1 << '\\n';
        } else {
          cout << 0 << '\\n';
        }
      }
    }
    return 0;
  }

  vector<int> md_pos(202020, 0); // md_pos[i] : (1 << i) % (pcnt + 1)
  for (int i = 0; i < 202020; ++i) {
    md_pos[i] = mod_pow(2LL, i, (int64)pcnt + 1);
  }
  vector<int> md_neg(202020, 0); // md_neg[i] : (1 << i) % (pcnt - 1)
  for (int i = 0; i < 202020; ++i) {
    md_neg[i] = mod_pow(2LL, i, (int64)pcnt - 1);
  }

  vector<int> need(202020, 0);

  for (int i = 0; i < 202020; ++i) {
    int t = i;
    int cnt = 0;
    while (t) {
      t %= __builtin_popcount(t);
      ++cnt;
    }
    need[i] = cnt;
  }

  int ini_pos = 0, ini_neg = 0;
  for (int i = 0; i < n; ++i) {
    if (s[n - 1 - i] == '1') {
      (ini_pos += md_pos[i]) %= (pcnt + 1);
      (ini_neg += md_neg[i]) %= (pcnt - 1);
    }
  }

  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      cout << need[(ini_pos + md_pos[n - 1 - i]) % (pcnt + 1)] + 1 << '\\n';
    } else {
      cout << need[(ini_neg - md_neg[n - 1 - i] + (pcnt - 1)) % (pcnt - 1)] + 1
           << '\\n';
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#pragma region kyomukyomupurin

/**
 *    author  : 𝒌𝒚𝒐𝒎𝒖𝒌𝒚𝒐𝒎𝒖𝒑𝒖𝒓𝒊𝒏
 *    created : 2020-07-12 12:50:00
 **/

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

template <class T> inline void eraque(std::vector<T> &vec) {
  vec.erase(std::unique(vec.begin(), vec.end()), vec.end());
  return;
}

template <class T> inline int lower_position(const std::vector<T> &vec, T val) {
  return static_cast<int>(std::distance(
      vec.begin(), std::lower_bound(vec.begin(), vec.end(), val)));
}

template <class T> inline int upper_position(const std::vector<T> &vec, T val) {
  return static_cast<int>(std::distance(
      vec.begin(), std::upper_bound(vec.begin(), vec.end(), val)));
}

template <class T> inline std::string to_binary(T n) {
  assert(n > 0);
  std::string ret = "";
  while (n)
    ret += (n & 1) ? '1' : '0', n >>= 1;
  std::reverse(ret.begin(), ret.end());
  return ret;
}

template <class T> inline void println(T val) { std::cout << val << '\\n'; }

template <class T>
using binary_heap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T, class U>
std::ostream &operator<<(std::ostream &os, const std::pair<T, U> &p) {
  return os << '(' << p.first << ", " << p.second << ')';
}

template <class Tuple, std::size_t... Is>
void tuple_out(std::ostream &os, const Tuple &tp, std::index_sequence<Is...>) {
  ((os << (Is ? ", " : "(") << std::get<Is>(tp)), ...) << ")";
}

template <class... Args>
std::ostream &operator<<(std::ostream &os, const std::tuple<Args...> &tp) {
  tuple_out(os, tp, std::index_sequence_for<Args...>{});
  return os;
}

template <class T>
std::ostream &operator<<(std::ostream &os, const std::vector<T> &vec) {
  int n = 0;
  for (auto e : vec)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T, class Compare>
std::ostream &operator<<(std::ostream &os, const std::set<T, Compare> &st) {
  int n = 0;
  for (auto e : st)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T, class U, class Compare>
std::ostream &operator<<(std::ostream &os, const std::map<T, U, Compare> &mp) {
  int n = 0;
  for (auto e : mp)
    os << (n++ ? ", " : "{") << e;
  return os << (n ? "}" : "{}");
}

template <class T>
std::istream &operator>>(std::istream &is, std::vector<T> &vec) {
  for (T &e : vec)
    is >> e;
  return is;
}

template <class T, class U>
std::istream &operator>>(std::istream &is, std::pair<T, U> &p) {
  return is >> p.first >> p.second;
}

template <class Tuple, std::size_t... Is>
void tuple_in(std::istream &is, Tuple &tp, std::index_sequence<Is...>) {
  ((is >> std::get<Is>(tp)), ...);
}

template <class... Args>
std::istream &operator>>(std::istream &is, std::tuple<Args...> &tp) {
  tuple_in(is, tp, std::index_sequence_for<Args...>{});
  return is;
}

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]: ", debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

void debug_out() { std::cerr << '\\n'; }

template <class Head, class... Tail>
void debug_out(Head &&head, Tail &&...tail) {
  std::cerr << head;
  if (sizeof...(Tail) != 0)
    std::cerr << ", ";
  debug_out(std::forward<Tail>(tail)...);
}

#pragma endregion kyomukyomupurin

template <class T, class U> T mod_pow(T x, U n, T mod) {
  T res = 1;
  while (n > 0) {
    if (n & 1)
      (res *= x) %= mod;
    (x *= x) %= mod;
    n >>= 1;
  }
  return res;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;
  string s;
  cin >> s;
  int pcnt = count(all(s), '1');

  if (pcnt == 0) {
    for (int i = 0; i < n; ++i)
      cout << 1 << '\\n';
    return 0;
  } else if (pcnt == 1) {
    if (s.back() == '1') {
      for (int i = 0; i < n - 1; ++i)
        cout << 2 << '\\n';
      cout << 0 << '\\n';
    } else {
      for (int i = 0; i < n; ++i) {
        if (i == n - 1) {
          cout << 2 << '\\n';
        } else if (s[i] == '0') {
          cout << 1 << '\\n';
        } else {
          cout << 0 << '\\n';
        }
      }
    }
    return 0;
  }

  vector<int> md_pos(202020, 0); // md_pos[i] : (1 << i) % (pcnt + 1)
  for (int i = 0; i < 202020; ++i) {
    md_pos[i] = mod_pow(2, i, pcnt + 1);
  }
  vector<int> md_neg(202020, 0); // md_neg[i] : (1 << i) % (pcnt - 1)
  for (int i = 0; i < 202020; ++i) {
    md_neg[i] = mod_pow(2, i, pcnt - 1);
  }

  vector<int> need(202020, 0);

  for (int i = 0; i < 202020; ++i) {
    int t = i;
    int cnt = 0;
    while (t) {
      t %= __builtin_popcount(t);
      ++cnt;
    }
    need[i] = cnt;
  }

  int ini_pos = 0, ini_neg = 0;
  for (int i = 0; i < n; ++i) {
    if (s[n - 1 - i] == '1') {
      (ini_pos += md_pos[i]) %= (pcnt + 1);
      (ini_neg += md_neg[i]) %= (pcnt - 1);
    }
  }

  for (int i = 0; i < n; ++i) {
    if (s[i] == '0') {
      cout << need[(ini_pos + md_pos[n - 1 - i]) % (pcnt + 1)] + 1 << '\\n';
    } else {
      cout << need[(ini_neg - md_neg[n - 1 - i] + (pcnt - 1)) % (pcnt - 1)] + 1
           << '\\n';
    }
  }

  return 0;
}