Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll

template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a) {
  in >> a.fr >> a.sc;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> a) {
  out << a.fr << " " << a.sc;
  return out;
}
template <typename T, typename T1> T amax(T &a, T1 b) {
  if (b > a)
    a = b;
  return a;
}
template <typename T, typename T1> T amin(T &a, T1 b) {
  if (b < a)
    a = b;
  return a;
}

const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;

const int N = 2e5 + 5;

int pw(int a, int p = M - 2, int MOD = M) {
  int result = 1;
  while (p > 0) {
    if (p & 1)
      result = a * result % MOD;
    a = a * a % MOD;
    p >>= 1;
  }
  return result;
}

int f(int n) {
  if (n == 0)
    return 0;
  int pc = ppc(n);
  return 1 + f(n % pc);
}

int rem[N];
int x[N];

void solve() {
  int n;
  string s;
  cin >> n >> s;

  int pc = 0;

  rep(i, 0, n) {
    x[i] = s.back() - '0';
    s.ppb();
    pc += x[i];
  }

  rep(p, pc - 1, pc + 2) {
    if (p <= 0)
      continue;
    int ans = 0;
    rep(i, 0, n) {
      if (x[i])
        ans += pw(2, i, p);
      ans %= p;
    }
    rem[p] = ans;
  }

  for (int i = n - 1; i >= 0; i--) {
    if (x[i]) {
      if (pc == 1) {
        cout << 0 << "\n";
      }
      int k = (rem[pc - 1] - pw(2, i, pc - 1) + pc - 1) % (pc - 1);
      cout << 1 + f(k) << "\n";
    } else {
      int k = (rem[pc + 1] + pw(2, i, pc + 1)) % (pc + 1);
      cout << 1 + f(k) << "\n";
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#ifdef SIEVE
  sieve();
#endif
#ifdef NCR
  init();
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define int long long
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define all(x) (x).begin(), (x).end()
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define sz(x) (int)((x).size())
#define fr first
#define sc second
#define pii pair<int, int>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define ppc __builtin_popcount
#define ppcll __builtin_popcountll

template <typename T1, typename T2>
istream &operator>>(istream &in, pair<T1, T2> &a) {
  in >> a.fr >> a.sc;
  return in;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> a) {
  out << a.fr << " " << a.sc;
  return out;
}
template <typename T, typename T1> T amax(T &a, T1 b) {
  if (b > a)
    a = b;
  return a;
}
template <typename T, typename T1> T amin(T &a, T1 b) {
  if (b < a)
    a = b;
  return a;
}

const long long INF = 1e18;
const int32_t M = 1e9 + 7;
const int32_t MM = 998244353;

const int N = 2e5 + 5;

int pw(int a, int p = M - 2, int MOD = M) {
  int result = 1;
  while (p > 0) {
    if (p & 1)
      result = a * result % MOD;
    a = a * a % MOD;
    p >>= 1;
  }
  return result;
}

int f(int n) {
  if (n == 0)
    return 0;
  int pc = ppc(n);
  return 1 + f(n % pc);
}

int rem[N];
int x[N];

void solve() {
  int n;
  string s;
  cin >> n >> s;

  int pc = 0;

  rep(i, 0, n) {
    x[i] = s.back() - '0';
    s.ppb();
    pc += x[i];
  }

  rep(p, pc - 1, pc + 2) {
    if (p <= 0)
      continue;
    int ans = 0;
    rep(i, 0, n) {
      if (x[i])
        ans += pw(2, i, p);
      ans %= p;
    }
    rem[p] = ans;
  }

  for (int i = n - 1; i >= 0; i--) {
    if (x[i]) {
      if (pc == 1) {
        cout << 0 << "\n";
        continue;
      }
      int k = (rem[pc - 1] - pw(2, i, pc - 1) + pc - 1) % (pc - 1);
      cout << 1 + f(k) << "\n";
    } else {
      int k = (rem[pc + 1] + pw(2, i, pc + 1)) % (pc + 1);
      cout << 1 + f(k) << "\n";
    }
  }
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#ifdef SIEVE
  sieve();
#endif
#ifdef NCR
  init();
#endif
  int t = 1;
  // cin>>t;
  while (t--)
    solve();
  return 0;
}