Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define ull unsigned long long
#define f first
#define s second
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
// erase,insert same as normal set

const ll MOD = 1e9 + 7;

void min_self(ll &a, ll b) { a = min(a, b); }
void max_self(ll &a, ll b) { a = max(a, b); }
ll add(ll x, ll y) { return (x + y) % MOD; }
ll sub(ll x, ll y) { return add(x, MOD - y); }
ll mul(ll x, ll y) { return (x * 1ll * y) % MOD; }
ll binpow(ll x, ll y) {
  ll z = 1;
  while (y > 0) {
    if (y % 2 == 1)
      z = mul(z, x);
    x = mul(x, x);
    y /= 2;
  }
  return z;
}
ll inv(ll x) { return binpow(x, MOD - 2); }
const int MXN = 1e6 + 10;
ll fact[MXN];
void fac_pre() {
  fact[0] = 1;
  for (ll i = 1; i < MXN; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
}
ll C(ll n, ll k) { return mul(fact[n], inv(mul(fact[k], fact[n - k]))); }

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
///--//
int popcount(int n) {
  int count = 0;
  while (n) {
    n &= (n - 1);
    count++;
  }
  return count;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("input.txt", "r", stdin);
  /* KEEP IN MIND
   * take care when sorting a 1 based indexed vector
   * const int N is defined way above in the starting
   */
  int n;
  cin >> n;
  string s;
  cin >> s;
  int sb = 0;

  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      ++sb;
  }

  vector<int> f(2e5 + 10, 0);
  for (int i = 1; i < f.size(); i++) {
    f[i] = f[i % (popcount(i))] + 1;
  }

  vector<int> mods{sb - 1, sb, sb + 1};
  vector<vector<int>> modpow(n, vector<int>(3, 1));
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (mods[j] <= 0)
        continue;
      modpow[i][j] = (modpow[i - 1][j] * 2) % mods[j];
    }
  }
  vector<int> modsum(3, 0);
  reverse(s.begin(), s.end());
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (mods[j] <= 0)
        continue;
      modsum[j] += (s[i] == '1') * modpow[i][j];
      modsum[j] %= mods[j];
    }
  }
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      int x = modsum[0];
      x -= modpow[i][0];
      x += mods[0];
      x %= mods[0];
      ans[i] = f[x] + 1;
    } else {
      int x = modsum[2];
      x += modpow[i][2];
      x %= mods[2];
      ans[i] = f[x] + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define ull unsigned long long
#define f first
#define s second
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define ordered_set                                                            \
  tree<int, null_type, less<int>, rb_tree_tag,                                 \
       tree_order_statistics_node_update>
// find_by_order(k)  returns iterator to kth element starting from 0;
// order_of_key(k) returns count of elements strictly smaller than k;
// erase,insert same as normal set

const ll MOD = 1e9 + 7;

void min_self(ll &a, ll b) { a = min(a, b); }
void max_self(ll &a, ll b) { a = max(a, b); }
ll add(ll x, ll y) { return (x + y) % MOD; }
ll sub(ll x, ll y) { return add(x, MOD - y); }
ll mul(ll x, ll y) { return (x * 1ll * y) % MOD; }
ll binpow(ll x, ll y) {
  ll z = 1;
  while (y > 0) {
    if (y % 2 == 1)
      z = mul(z, x);
    x = mul(x, x);
    y /= 2;
  }
  return z;
}
ll inv(ll x) { return binpow(x, MOD - 2); }
const int MXN = 1e6 + 10;
ll fact[MXN];
void fac_pre() {
  fact[0] = 1;
  for (ll i = 1; i < MXN; i++) {
    fact[i] = mul(fact[i - 1], i);
  }
}
ll C(ll n, ll k) { return mul(fact[n], inv(mul(fact[k], fact[n - k]))); }

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
///--//
int popcount(int n) {
  int count = 0;
  while (n) {
    n &= (n - 1);
    count++;
  }
  return count;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  // freopen("input.txt", "r", stdin);
  /* KEEP IN MIND
   * take care when sorting a 1 based indexed vector
   * const int N is defined way above in the starting
   */
  int n;
  cin >> n;
  string s;
  cin >> s;
  int sb = 0;

  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      ++sb;
  }

  vector<int> f(2e5 + 10, 0);
  for (int i = 1; i < f.size(); i++) {
    f[i] = f[i % (popcount(i))] + 1;
  }

  vector<int> mods{sb - 1, sb, sb + 1};
  vector<vector<int>> modpow(n, vector<int>(3, 1));
  for (int i = 1; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (mods[j] <= 0)
        continue;
      modpow[i][j] = (modpow[i - 1][j] * 2) % mods[j];
    }
  }
  vector<int> modsum(3, 0);
  reverse(s.begin(), s.end());
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 3; j++) {
      if (mods[j] <= 0)
        continue;
      modsum[j] += (s[i] == '1') * modpow[i][j];
      modsum[j] %= mods[j];
    }
  }
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (sb == 1) {
        ans[i] = 0;
        continue;
      }
      int x = modsum[0];
      x -= modpow[i][0];
      x += mods[0];
      x %= mods[0];
      ans[i] = f[x] + 1;
    } else {
      int x = modsum[2];
      x += modpow[i][2];
      x %= mods[2];
      ans[i] = f[x] + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  for (int i = 0; i < n; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}