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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
using ll = int_fast64_t;
using pll = pair<ll, ll>;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#if loc || local
template <class T> void dump(T &&t) { cerr << t << endl; }
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {
  cerr << h << ", ";
  dump(forward<Ts>(t)...);
}
#else
void dump() {}
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {}
#endif
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto const &vi : v)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << p.first << "," << p.second;
  return os;
}
template <class T> vector<T> vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}

#define popc(x) __builtin_popcount(x)

void test() {
  map<int, int> mem;
  mem[0] = 0;
  mem[1] = 1;
  mem[2] = 1;
  auto f = [&](auto &&f, int x) -> int {
    if (mem.count(x))
      return mem[x];
    return mem[x] = f(f, x % popc(x)) + 1;
  };
  int lim = 1000000;
  for (int i = 0; i < lim; ++i) {
    // cout<<i<<" : "<<f(f,i)<<endl;
    if (f(f, i) > 10)
      cout << i << endl;
  }
}

map<ll, ll> mem;
ll f(ll x) {
  if (mem.count(x))
    return mem[x];
  return mem[x] = f(x % popc(x)) + 1;
}

signed main() {

  // test();return 0;

  ll n;
  string s;
  cin >> n >> s;

  mem[0] = 0;
  mem[1] = 1;
  mem[2] = 1;

  reverse(begin(s), end(s));
  ll mod = 0;
  rep(i, n) if (s[i] == '1') mod++;
  if (mod == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }

  if (mod == 1) {
    if (s[0] == '1') {
      rep(i, n - 1) cout << 2 << endl;
      cout << 0 << endl;
      return 0;
    } else {
      reverse(begin(s), end(s));
      rep(i, n - 1) {
        if (s[i] == '1')
          cout << 0 << endl;
        else
          cout << 1 << endl;
      }
      cout << 2 << endl;
      return 0;
    }
  }

  ll moda = mod + 1;
  ll modb = mod - 1;
  vector<ll> pa(n), pb(n);
  pa[0] = pb[0] = 1;
  rep(i, n - 1) {
    pa[i + 1] = (pa[i] * 2) % moda;
    pb[i + 1] = (pb[i] * 2) % modb;
  }
  ll ma = 0, mb = 0;

  rep(i, n) if (s[i] == '1') {
    ma += pa[i];
    mb += pb[i];
    ma %= moda;
    mb %= modb;
  }

  vector<ll> ans(n);
  rep(i, n) {
    if (s[i] == '1') {
      ll x = (mb + modb - pb[i]) % modb;
      ans[i] = f(x) + 1;
    } else {
      ll x = (ma + pa[i]) % moda;
      ans[i] = f(x) + 1;
    }
  }

  reverse(begin(ans), end(ans));
  for (auto x : ans)
    cout << x << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (ll i = 0; i < (n); ++i)
using ll = int_fast64_t;
using pll = pair<ll, ll>;
constexpr ll INF = 1LL << 60;
constexpr ll MOD = 1e9 + 7;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#if loc || local
template <class T> void dump(T &&t) { cerr << t << endl; }
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {
  cerr << h << ", ";
  dump(forward<Ts>(t)...);
}
#else
void dump() {}
template <class T, class... Ts> void dump(T &&h, Ts &&...t) {}
#endif
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (auto &elemnt : v)
    is >> elemnt;
  return is;
}
template <class T, class U> istream &operator>>(istream &is, pair<T, U> &p) {
  is >> p.first >> p.second;
  return is;
}
template <class T> ostream &operator<<(ostream &os, vector<T> const &v) {
  for (auto const &vi : v)
    os << vi << " ";
  return os;
}
template <class T, class U>
ostream &operator<<(ostream &os, pair<T, U> const &p) {
  os << p.first << "," << p.second;
  return os;
}
template <class T> vector<T> vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto vec(size_t a, Ts... ts) {
  return vector<decltype(vec<T>(ts...))>(a, vec<T>(ts...));
}

#define popc(x) __builtin_popcount(x)

void test() {
  map<int, int> mem;
  mem[0] = 0;
  mem[1] = 1;
  mem[2] = 1;
  auto f = [&](auto &&f, int x) -> int {
    if (mem.count(x))
      return mem[x];
    return mem[x] = f(f, x % popc(x)) + 1;
  };
  int lim = 1000000;
  for (int i = 0; i < lim; ++i) {
    // cout<<i<<" : "<<f(f,i)<<endl;
    if (f(f, i) > 10)
      cout << i << endl;
  }
}

map<ll, ll> mem;
ll f(ll x) {
  if (mem.count(x))
    return mem[x];
  return mem[x] = f(x % popc(x)) + 1;
}

signed main() {

  // test();return 0;

  ll n;
  string s;
  cin >> n >> s;

  mem[0] = 0;
  mem[1] = 1;
  mem[2] = 1;

  reverse(begin(s), end(s));
  ll mod = 0;
  rep(i, n) if (s[i] == '1') mod++;
  if (mod == 0) {
    rep(i, n) cout << 1 << endl;
    return 0;
  }

  if (mod == 1) {
    if (s[0] == '1') {
      rep(i, n - 1) cout << 2 << endl;
      cout << 0 << endl;
      return 0;
    } else {
      return 1;
      reverse(begin(s), end(s));
      rep(i, n) {
        if (s[i] == '1')
          cout << 0 << endl;
        else
          cout << 1 << endl;
      }
      return 0;
    }
  }

  ll moda = mod + 1;
  ll modb = mod - 1;
  vector<ll> pa(n), pb(n);
  pa[0] = pb[0] = 1;
  rep(i, n - 1) {
    pa[i + 1] = (pa[i] * 2) % moda;
    pb[i + 1] = (pb[i] * 2) % modb;
  }
  ll ma = 0, mb = 0;

  rep(i, n) if (s[i] == '1') {
    ma += pa[i];
    mb += pb[i];
    ma %= moda;
    mb %= modb;
  }

  vector<ll> ans(n);
  rep(i, n) {
    if (s[i] == '1') {
      ll x = (mb + modb - pb[i]) % modb;
      ans[i] = f(x) + 1;
    } else {
      ll x = (ma + pa[i]) % moda;
      ans[i] = f(x) + 1;
    }
  }

  reverse(begin(ans), end(ans));
  for (auto x : ans)
    cout << x << endl;
}