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

#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
// #define endl "\\n"
#define ciosup                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (int i = 0; i < v.size(); ++i) {
    is >> v[i];
  }
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size() - 1; ++i) {
    os << v[i] << " ";
  }
  if (v.size() > 0) {
    os << v[v.size() - 1] << endl;
  }
  return os;
}

int pcnt(ll orig) {
  int ans = 0;
  if (orig == 0)
    return 0;
  while (orig > 0) {
    ++ans;
    ll tmp = 1, pcc = 0;
    while (tmp <= orig) {
      if ((tmp & orig) > 0)
        ++pcc;
      tmp <<= 1;
    }
    orig %= pcc;
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  // REP(i, 200000) { cout << '0'; }
  cout << endl;
  int totpop = 0;
  REP(i, n) {
    if (x[i] == '1') {
      ++totpop;
    }
  }
  vll ans(n, 0);
  if (totpop == 0) {
    vll memo2(n, 0);
    ll mod2 = totpop + 1;
    ll tot2 = 0, now2 = 1;
    IREP(i, n) {
      if (x[i] == '1')
        tot2 = (tot2 + now2) % mod2;
      memo2[i] = now2;
      now2 = (now2 * 2) % mod2;
    }
    IREP(i, n) {
      ll tmp = tot2;
      tmp = (tmp + memo2[i]) % mod2;
      ans[i] = 1 + pcnt(tmp);
    }
  } else if (totpop == n) {
    vll memo0(n, 0);
    ll mod0 = totpop - 1;
    if (mod0 == 0) {
      REP(i, n) { cout << ans[i] << endl; }
      return 0;
    }
    ll tot0 = 0, now0 = 1;
    IREP(i, n) {
      if (x[i] == '1')
        tot0 = (tot0 + now0) % mod0;
      memo0[i] = now0;
      now0 = (now0 * 2) % mod0;
    }
    IREP(i, n) {
      ll tmp = tot0;
      tmp = (tmp + mod0 - memo0[i]) % mod0;
      ans[i] = 1 + pcnt(tmp);
    }
  } else if (totpop == 1) {
    vll memo0(n, 0), memo2(n, 0);
    ll mod2 = totpop + 1;
    ll tot2 = 0, now2 = 1;
    IREP(i, n) {
      // if (x[i] == '1') tot0 = (tot0 + now0) % mod0;
      if (x[i] == '1')
        tot2 = (tot2 + now2) % mod2;
      // memo0[i] = now0;
      memo2[i] = now2;
      // now0 = (now0 * 2) % mod0;
      now2 = (now2 * 2) % mod2;
    }
    IREP(i, n) {
      if (x[i] == '1') {
        ans[i] = 0;
      } else {
        ll tmp = tot2;
        tmp = (tmp + memo2[i]) % mod2;
        ans[i] = 1 + pcnt(tmp);
      }
    }
  } else {
    vll memo0(n, 0), memo2(n, 0);
    ll mod0 = totpop - 1, mod2 = totpop + 1;
    ll tot0 = 0, tot2 = 0, now0 = 1, now2 = 1;
    IREP(i, n) {
      if (x[i] == '1')
        tot0 = (tot0 + now0) % mod0;
      if (x[i] == '1')
        tot2 = (tot2 + now2) % mod2;
      memo0[i] = now0;
      memo2[i] = now2;
      now0 = (now0 * 2) % mod0;
      now2 = (now2 * 2) % mod2;
    }
    IREP(i, n) {
      if (x[i] == '1') {
        ll tmp = tot0;
        tmp = (tmp + mod0 - memo0[i]) % mod0;
        ans[i] = 1 + pcnt(tmp);
      } else {
        ll tmp = tot2;
        tmp = (tmp + memo2[i]) % mod2;
        ans[i] = 1 + pcnt(tmp);
      }
    }
  }
  REP(i, n) { cout << ans[i] << endl; }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using pint = pair<int, int>;
using ll = long long;
using ull = unsigned long long;
using vll = vector<long long>;
using pll = pair<ll, ll>;
#define FOR(i, begin, end)                                                     \\
  for (int i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (int i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
#define VREP(s, ite) for (auto ite = s.begin(); ite != s.end(); ++ite)
#define FI first
#define SE second
#define ALL(v) v.begin(), v.end()
// #define endl "\\n"
#define ciosup                                                                 \\
  cin.tie(0);                                                                  \\
  ios::sync_with_stdio(false);
#define eb emplace_back
#define vint vector<int>
constexpr ll INF = 1e15 + 7LL;
constexpr ll MOD = 1e9 + 7LL;
template <typename T> istream &operator>>(istream &is, vector<T> &v) {
  for (int i = 0; i < v.size(); ++i) {
    is >> v[i];
  }
  return is;
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  for (int i = 0; i < v.size() - 1; ++i) {
    os << v[i] << " ";
  }
  if (v.size() > 0) {
    os << v[v.size() - 1] << endl;
  }
  return os;
}

int pcnt(int orig) {
  int ans = 0;
  if (orig == 0)
    return 0;
  while (orig > 0) {
    ++ans;
    int tmp = 1, pcc = 0;
    while (tmp <= orig) {
      if ((tmp & orig) > 0)
        ++pcc;
      tmp <<= 1;
    }
    orig %= pcc;
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  ll now = 0;
  int totpop = 0;
  REP(i, n) {
    if (x[i] == '1') {
      ++totpop;
    }
  }
  vll ans(n, 0);
  if (totpop == 0) {
    vll memo2(n, 0);
    ll mod2 = totpop + 1;
    ll tot2 = 0, now2 = 1;
    IREP(i, n) {
      if (x[i] == '1')
        tot2 = (tot2 + now2) % mod2;
      memo2[i] = now2;
      now2 = (now2 * 2) % mod2;
    }
    IREP(i, n) {
      ll tmp = tot2;
      tmp = (tmp + memo2[i]) % mod2;
      ans[i] = 1 + pcnt(tmp);
    }
  } else if (totpop == n) {
    vll memo0(n, 0);
    ll mod0 = totpop - 1;
    ll tot0 = 0, now0 = 1;
    IREP(i, n) {
      if (x[i] == '1')
        tot0 = (tot0 + now0) % mod0;
      memo0[i] = now0;
      now0 = (now0 * 2) % mod0;
    }
    IREP(i, n) {
      ll tmp = tot0;
      tmp = (tmp + mod0 - memo0[i]) % mod0;
      ans[i] = 1 + pcnt(tmp);
    }
  } else {
    vll memo0(n, 0), memo2(n, 0);
    ll mod0 = totpop - 1, mod2 = totpop + 1;
    ll tot0 = 0, tot2 = 0, now0 = 1, now2 = 1;
    IREP(i, n) {
      if (x[i] == '1')
        tot0 = (tot0 + now0) % mod0;
      if (x[i] == '1')
        tot2 = (tot2 + now2) % mod2;
      memo0[i] = now0;
      memo2[i] = now2;
      now0 = (now0 * 2) % mod0;
      now2 = (now2 * 2) % mod2;
    }
    IREP(i, n) {
      if (x[i] == '1') {
        ll tmp = tot0;
        tmp = (tmp + mod0 - memo0[i]) % mod0;
        ans[i] = 1 + pcnt(tmp);
      } else {
        ll tmp = tot2;
        tmp = (tmp + memo2[i]) % mod2;
        ans[i] = 1 + pcnt(tmp);
      }
    }
  }
  REP(i, n) { cout << ans[i] << endl; }
}