Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define testcase()                                                             \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define forn(i, n) for (int i = 0; i < n; i++)
#define forn1(i, n) for (int i = 1; i <= n; i++)
#define vll vector<ll>
#define vi vector<int>
#define all(v) v.begin(), v.end()

// ll M = 1e9 + 7;
double pi = acos(-1.0);
ll powerm(ll x, ll y, ll M) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return res % M;
}
ll power(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {

  // ifstream cin("input.txt");
  // ofstream cout("output.txt");
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cc = 0;
  forn(i, n) if (s[i] == '1') cc++;
  ll p1 = cc + 1, p2 = cc - 1;
  if (p2 < 0)
    p2 = p1;
  ll curr1 = 0, curr2 = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0')
      continue;
    ll h = n - 1 - i;
    curr1 += powerm((ll)2, h, p1);
    curr1 %= p1;
    curr2 += powerm((ll)2, h, p2);
    curr2 %= p2;
    // cout<<i<<':'<<curr1<<':'<<curr2<<endl;
  }
  ll pre[n + 10];
  pre[0] = 0;
  forn1(i, n) {
    ll f = __builtin_popcountll(i);
    pre[i] = pre[i % f] + 1;
  }
  // forn1(i,n) cout<<pre[i]<<' '; cout<<endl;
  // cout<<curr1<<":"<<curr2<<endl;
  forn(i, n) {
    if (s[i] == '0') {
      ll r = n - 1 - i;
      ll d = curr1 + powerm((ll)2, r, p1);
      d %= p1;
      cout << 1 + pre[d] << '\n';
    } else {
      if (p2 == 0) {
        cout << 0 << '\n';
        continue;
      }
      ll r = n - 1 - i;
      ll d = curr2 - powerm((ll)2, r, p2) + p2;
      d %= p2;
      cout << 1 + pre[d] << '\n';
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define ff first
#define ss second
#define pii pair<int, int>
#define pll pair<ll, ll>
#define testcase()                                                             \
  int t;                                                                       \
  cin >> t;                                                                    \
  while (t--)
#define forn(i, n) for (int i = 0; i < n; i++)
#define forn1(i, n) for (int i = 1; i <= n; i++)
#define vll vector<ll>
#define vi vector<int>
#define all(v) v.begin(), v.end()

// ll M = 1e9 + 7;
double pi = acos(-1.0);
ll powerm(ll x, ll y, ll M) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x) % M;
    y = y >> 1;
    x = (x * x) % M;
  }
  return res % M;
}
ll power(ll x, ll y) {
  ll res = 1;
  while (y) {
    if (y & 1)
      res = (res * x);
    y = y >> 1;
    x = (x * x);
  }
  return res;
}
ll gcd(ll a, ll b) {
  if (b > a)
    return gcd(b, a);
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

int main() {

  // ifstream cin("input.txt");
  // ofstream cout("output.txt");
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cc = 0;
  forn(i, n) if (s[i] == '1') cc++;
  ll p1 = cc + 1, p2 = cc - 1;
  if (p2 < 0)
    p2 = p1;
  ll curr1 = 0, curr2 = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '0')
      continue;
    ll h = n - 1 - i;
    curr1 += powerm((ll)2, h, p1);
    curr1 %= p1;
    if (p2 == 0)
      continue;
    curr2 += powerm((ll)2, h, p2);
    curr2 %= p2;
    // cout<<i<<':'<<curr1<<':'<<curr2<<endl;
  }
  ll pre[n + 10];
  pre[0] = 0;
  forn1(i, n) {
    ll f = __builtin_popcountll(i);
    pre[i] = pre[i % f] + 1;
  }
  // forn1(i,n) cout<<pre[i]<<' '; cout<<endl;
  // cout<<curr1<<":"<<curr2<<endl;
  forn(i, n) {
    if (s[i] == '0') {
      ll r = n - 1 - i;
      ll d = curr1 + powerm((ll)2, r, p1);
      d %= p1;
      cout << 1 + pre[d] << '\n';
    } else {
      if (p2 == 0) {
        cout << 0 << '\n';
        continue;
      }
      ll r = n - 1 - i;
      ll d = curr2 - powerm((ll)2, r, p2) + p2;
      d %= p2;
      cout << 1 + pre[d] << '\n';
    }
  }

  return 0;
}