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

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
vector<int> ans;
int memo[220000];
int rec(int x) {
  if (memo[x] != -1)
    return memo[x];
  if (x == 0)
    return 0;
  int ret = 0;
  int popcnt = __builtin_popcountll(x);
  int newpp = __builtin_popcountll(x % popcnt);

  if (popcnt == 0)
    return 1;
  return memo[x] = rec(x % popcnt) + 1;
}

signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  rep(i, n + 1) memo[i] = -1;
  int popcnt = 0;
  int valp = 0, valm = 0;
  int ketap = 1;
  int ketam = 1;

  rep(i, n) popcnt += (s[i] - '0');
  if (popcnt == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }
  if (popcnt == 1) {
    rep(i, n) {
      if (s[i] == '1')
        cout << 0 << endl;
      else {
        // pp = 2;
        if (s[n - 1] == '1' || i == n - 1) {
          cout << 2 << endl;
        } else {
          cout << 1 << endl;
        }
      }
    }
    return 0;
  }
  int modp = popcnt + 1;
  int modm = popcnt - 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      valp += ketap;
      valp %= modp;
      valm += ketam;
      valm %= modm;
    }
    ketap *= 2;
    ketam *= 2;
    ketap %= modp;
    ketam %= modm;
  }
  ketap = 1;
  ketam = 1;
  // trace(valp,valm);

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      // plus
      int now = (valp + ketap) % modp;
      ans.push_back(rec(now));

    } else {
      // minus
      int now = (valm - ketam) % modm;
      now = (now % modm + modm) % modm;
      ans.push_back(rec(now));
    }
    ketap *= 2;
    ketap %= modp;
    ketam *= 2;
    ketam %= modm;
  }
  for (int i = (int)ans.size() - 1; i >= 0; i--)
    cout << ans[i] + 1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LOCAL
using namespace std;
template <typename A, typename B>
ostream &operator<<(ostream &out, const pair<A, B> &a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream &operator<<(ostream &out, const array<T, N> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T> ostream &operator<<(ostream &out, const vector<T> &a) {
  out << "[";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "]";
  return out;
}
template <typename T, class Cmp>
ostream &operator<<(ostream &out, const set<T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &v : a) {
    out << (first ? "" : ", ");
    out << v;
    first = 0;
  }
  out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream &operator<<(ostream &out, const map<U, T, Cmp> &a) {
  out << "{";
  bool first = true;
  for (auto &p : a) {
    out << (first ? "" : ", ");
    out << p.first << ":" << p.second;
    first = 0;
  }
  out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}
#define rep(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
#define int long long
using P = pair<int, int>;
// #########################################
vector<int> ans;
int memo[220000];
int rec(int x) {
  if (memo[x] != -1)
    return memo[x];
  if (x == 0)
    return 0;
  int ret = 0;
  int popcnt = __builtin_popcountll(x);
  int newpp = __builtin_popcountll(x % popcnt);

  if (popcnt == 0)
    return 1;
  return memo[x] = rec(x % popcnt) + 1;
}

signed main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  rep(i, n + 1) memo[i] = -1;
  int popcnt = 0;
  int valp = 0, valm = 0;
  int ketap = 1;
  int ketam = 1;

  rep(i, n) popcnt += (s[i] - '0');
  int modp = popcnt + 1;
  int modm = popcnt - 1;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      valp += ketap;
      valp %= modp;
      valm += ketam;
      valm %= modm;
    }
    ketap *= 2;
    ketam *= 2;
    ketap %= modp;
    ketam %= modm;
  }
  ketap = 1;
  ketam = 1;
  // trace(valp,valm);

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0') {
      // plus
      int now = (valp + ketap) % modp;
      ans.push_back(rec(now));

    } else {
      // minus
      int now = (valm - ketam) % modm;
      now = (now % modm + modm) % modm;
      ans.push_back(rec(now));
    }
    ketap *= 2;
    ketap %= modp;
    ketam *= 2;
    ketam %= modm;
  }
  for (int i = (int)ans.size() - 1; i >= 0; i--)
    cout << ans[i] + 1 << endl;
  return 0;
}