Problem p02609 (C++)

Buggy submission (status: Runtime Error):
// K-OS WITH THE OCDE

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long int ll;
typedef long double db;

#define TEST                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define vell vector<ll>
#define pl pair<ll, ll>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define dbg(x) cerr << #x << ": " << x << endl
#define dbg2(x, y) cerr << #x << ": " << x << " || " << #y << ": " << y << endl

#define endl "\n"
#define MAX 1e18
#define MIN INT_MIN
// #define mod 1000000007

const int mxn = 2e5 + 2;
int gre[200005];
ll low[200005];
ll store[mxn];

ll mult(ll x, ll y, ll mod) { return (((x % mod) * (y % mod)) % mod); }
void pre() {
  for (ll i = 0; i < mxn; ++i) {

    ll x = i;
    ll count = 0;
    while (x) {

      ll y = __builtin_popcount(x);
      x %= y;
      count++;
    }

    store[i] = count;
  }

  // dbg2(store[1] , store[0]);
}

void prel(ll k, ll n) {
  low[0] = 1 % k;

  for (ll i = 1; i < n; ++i) {

    low[i] = mult(low[i - 1], (2 % k), k);
    low[i] %= k;
    //  cout << low[i] << " ";
  }
}

void preg(ll k, ll n) {
  gre[0] = 1 % k;

  for (ll i = 1; i < n; ++i) {

    gre[i] = mult(gre[i - 1], (2 % k), k);
    gre[i] %= k;
    // cout << gre[i] << " ";
  }
}

ll small = 0, large = 0;

void sl(ll k, string s, ll n) {
  ll i = 0, j = n - 1;

  while (i < n) {

    if (s[j] == '1') {
      small += low[i];
      small %= k;
    }
    i++;
    j--;
  }
}

void sg(ll k, string s, ll n) {
  ll i = 0, j = n - 1;

  while (i < n) {

    if (s[j] == '1') {
      large += gre[i];
      large %= k;
    }
    i++;
    j--;
  }
}

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

  ll f = 0;
  for (ll i = 0; i < n; ++i) {

    f += (s[i] == '1');
  }

  if (f - 1)
    prel(f - 1, n);
  preg(f + 1, n);
  if (f - 1)
    sl(f - 1, s, n);
  sg(f + 1, s, n);

  for (ll i = 0; i < n; ++i) {
    ll x;
    if (s[i] == '1' && (f - 1)) {
      x = (small - low[n - i - 1] + (f - 1)) % (f - 1);
    } else if (s[i] == '0') {
      x = (large + gre[n - i - 1] + (f + 1));
      x %= (f + 1);
    }

    cout << 1 + store[x] << endl;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // TEST
  pre();
  solve();
}


Fixed submission:
// K-OS WITH THE OCDE

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

typedef long long int ll;
typedef long double db;

#define TEST                                                                   \
  ll t;                                                                        \
  cin >> t;                                                                    \
  while (t--)
#define vell vector<ll>
#define pl pair<ll, ll>
#define all(v) v.begin(), v.end()
#define pb push_back
#define ff first
#define ss second
#define mp make_pair
#define dbg(x) cerr << #x << ": " << x << endl
#define dbg2(x, y) cerr << #x << ": " << x << " || " << #y << ": " << y << endl

#define endl "\n"
#define MAX 1e18
#define MIN INT_MIN
// #define mod 1000000007

const int mxn = 2e5 + 2;
int gre[200005];
ll low[200005];
ll store[mxn];

ll mult(ll x, ll y, ll mod) { return (((x % mod) * (y % mod)) % mod); }
void pre() {
  for (ll i = 0; i < mxn; ++i) {

    ll x = i;
    ll count = 0;
    while (x) {

      ll y = __builtin_popcount(x);
      x %= y;
      count++;
    }

    store[i] = count;
  }

  // dbg2(store[1] , store[0]);
}

void prel(ll k, ll n) {
  low[0] = 1 % k;

  for (ll i = 1; i < n; ++i) {

    low[i] = mult(low[i - 1], (2 % k), k);
    low[i] %= k;
    //  cout << low[i] << " ";
  }
}

void preg(ll k, ll n) {
  gre[0] = 1 % k;

  for (ll i = 1; i < n; ++i) {

    gre[i] = mult(gre[i - 1], (2 % k), k);
    gre[i] %= k;
    // cout << gre[i] << " ";
  }
}

ll small = 0, large = 0;

void sl(ll k, string s, ll n) {
  ll i = 0, j = n - 1;

  while (i < n) {

    if (s[j] == '1') {
      small += low[i];
      small %= k;
    }
    i++;
    j--;
  }
}

void sg(ll k, string s, ll n) {
  ll i = 0, j = n - 1;

  while (i < n) {

    if (s[j] == '1') {
      large += gre[i];
      large %= k;
    }
    i++;
    j--;
  }
}

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

  ll f = 0;
  for (ll i = 0; i < n; ++i) {

    f += (s[i] == '1');
  }

  if (f - 1)
    prel(f - 1, n);
  preg(f + 1, n);
  if (f - 1)
    sl(f - 1, s, n);
  sg(f + 1, s, n);

  for (ll i = 0; i < n; ++i) {
    ll x = 0;
    if (s[i] == '1' && f - 1 == 0) {
      cout << 0 << endl;
      continue;
    }

    if (s[i] == '1') {
      x = (small - low[n - i - 1] + (f - 1)) % (f - 1);
    } else if (s[i] == '0') {
      x = (large + gre[n - i - 1] + (f + 1));
      x %= (f + 1);
    }

    cout << 1 + store[x] << endl;
  }
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  // TEST
  pre();
  solve();
}