Problem p02609 (C++) — buggy submission, verdict: 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;
    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;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define l long
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define M1 1000000007
#define M2 998244353

#define fl(i, a, b) for (ll i = a; i < b; i++)
#define bfl(i, a, b) for (ll i = b - 1; i >= a; i--)
#define f(i, n) for (ll i = 0; i < n; i++)
#define bf(i, n) for (ll i = n - 1; i >= 0; i--)

#define mapll map<ll, ll>
#define p0(a) cout << a << " "
#define p11(a) cout << a << endl
// #define p2(a, b) cout << a << " " << b << endl
#define p3(a, b, c) cout << a << " " << b << " " << c << endl
#define p4(a, b, c, d) cout << a << " " << b << " " << c << " " << d << endl

#define bn "\\n"

//------------------------------------------------------------------------
// #define V(a) vector<a>
// #define vt() vector< tuple < ll ,ll ,ll > > ;
// #define vector< ll ,ll > pv;
// vector< pair <ll,ll> > vect;

//------------------------------------------------------------------------

ll modI(ll a, ll m);
ll gcd(ll a, ll b);
ll powM(ll x, ll y, ll m);
ll swap(ll a, ll b);

void swap(ll &a, ll &b) {
  ll tp = a;
  a = b;
  b = tp;
}

ll gcd(ll x, ll y) {
  if (x == 0)
    return y;
  return gcd(y % x, x);
}

ll powM(ll x, ll y, ll m) {
  ll vec = 1, r = 1;
  x %= m;
  while (r > 0 && r <= y) {
    if (r & y) {
      vec *= x;
      vec %= m;
    }
    r <<= 1;
    x *= x;
    x %= m;
  }
  return vec;
}

ll modI(ll a, ll m) {
  ll m0 = m, y = 0, x = 1;
  if (m == 1)
    return 0;
  while (a > 1) {
    ll q = a / m;
    ll t = m;
    m = a % m;
    a = t;
    t = y;
    y = x - q * y;
    x = t;
  }
  if (x < 0)
    x += m0;
  return x;
}
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;
}

//---------------------------------------------------------------------------

// variable = Excalcssion1 ? Excalcssion2 : Excalcssion3
// max = (n1 > n2) ? n1 : n2;
// (n>m) ? cout<<"True"<<endl:cout<<"false"<<endl ;

// ---------------------------------------------------------------------------

// ll n;cin>>n;ll a[n];for(ll i=0;i<n;i++)cin>>a[i];
// for(ll i=0;i<n;i++)cout<<a[i];
// ll a,b;cin>>a>>b;

//----------------------------------------------------------------------------

// vector< tuple < ll ,ll ,ll > > tv;
// vector< ll ,ll > pv;

//----------------------------------------------------------------------------

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(0);
  ll tt = 1;
  // cin>>tt;
  while (tt--) {
    ll n;
    cin >> n;
    string s;
    cin >> s;
    ll cc = 0;
    f(i, n) {
      bool ok3 = 0;
      if (s[i] == '1')
        ok3 = 1;
      if (ok3)
        cc++;
    }
    ll p1 = cc + 1, p2 = cc - 1;
    bool flag = 0;
    if (p2 < 0)
      flag = 1;
    if (flag)
      p2 = p1;
    // 	p1(flag);
    ll ans1 = 0, ans2 = 0;

    bf(i, n) {
      if (s[i] == '0')
        continue;
      ll h = n - 1 - i;
      ans1 = ans1 + powerm((ll)2, h, p1);
      ans1 %= p1;
      bool ok = 0;
      if (p2 == 0)
        ok = 1;
      if (ok)
        continue;
      ans2 = ans2 + powerm((ll)2, h, p2);
      ans2 %= p2;
      // cout<<i<<':'<<ans1<<':'<<ans2<<endl;
    }
    ll calc[n + 10];
    calc[0] = 0;
    fl(i, 1, n + 1) {
      ll f = __builtin_popcountll(i);
      calc[i] = calc[i % f] + 1;
    }

    // cout<<ans1<<":"<<ans2<<endl;
    f(i, n) {
      if (s[i] == '0') {
        ll zz = n - 1 - i;
        ll d = ans1 + powerm((ll)2, zz, p1);
        d = d % p1;
        cout << 1 + calc[d] << '\\n';
      } else {
        bool ok = 0;
        if (p2 == 0)
          ok = 0;
        if (ok) {
          p11(0);
          continue;
        }
        ll r = n - 1 - i;
        ll d = ans2 - powerm((ll)2, r, p2) + p2;
        d %= p2;
        p11(1 + calc[d]);
        // cout<<1 + calc[d]<<'\\n';
      }
    }
  }

  return 0;
}