Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back

#define sec second
#define fir first
#define mo 1000000007
#define inf 1e18
#define rep(i, s, n) for (ll i = s; i < n; i = i + 1)
#define rrep(i, s, n) for (ll i = s; i >= n; i--)

ll mod(ll n) { return (n % (ll)mo + (ll)mo) % (ll)mo; }

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll c = 0;
  rep(i, 0, n) {
    if (s[i] == '1')
      ++c;
  }

  vector<ll> cnt(n + 1, 0);
  cnt[1] = 1;
  cnt[2] = 1;
  rep(i, 3, n + 1) {
    ll x = __builtin_popcount(i);
    cnt[i] = cnt[i % x] + 1;
  }
  vector<ll> r1(n + 1), r2(n + 1);
  r1[0] = 1 % (c - 1);
  r2[0] = 1 % (c + 1);
  rep(i, 1, n) {
    r1[i] = (r1[i - 1] * 2) % (c - 1);
    r2[i] = (r2[i - 1] * 2) % (c + 1);
  }
  vector<ll> pr1(n + 1, 0), pr2(n + 1, 0);
  vector<ll> sr1(n + 2, 0), sr2(n + 2, 0);

  if (s[0] == '1')
    pr1[0] = r1[n - 1], pr2[0] = r2[n - 1];

  rep(i, 1, n) {
    if (s[i] == '1') {
      pr1[i] = (pr1[i - 1] + r1[n - i - 1]) % (c - 1);
      pr2[i] = (pr2[i - 1] + r2[n - i - 1]) % (c + 1);
    } else {
      pr1[i] = pr1[i - 1];
      pr2[i] = pr2[i - 1];
    }
    // cout<<i<<" "<<pr1[i]<<" "<<pr2[i]<<endl;
  }
  if (s[n - 1] == '1')
    sr1[n - 1] = r1[0], sr2[n - 1] = r2[0];
  rrep(i, n - 2, 0) {
    if (s[i] == '1') {
      sr1[i] = (sr1[i + 1] + r1[n - i - 1]) % (c - 1);
      sr2[i] = (sr2[i + 1] + r2[n - i - 1]) % (c + 1);
    } else {
      sr1[i] = sr1[i + 1];
      sr2[i] = sr2[i + 1];
    }
    // cout<<i<<" "<<sr1[i]<<" "<<sr2[i]<<endl;
  }

  rep(i, 0, n) {
    if (i == 0) {
      if (s[i] == '1') {
        cout << cnt[sr1[i + 1]] + 1 << endl;
      } else {
        ll r = (r2[n - i - 1] + sr2[i + 1]) % (c + 1);
        cout << cnt[r] + 1 << endl;
      }
    } else if (i == n - 1) {
      if (s[i] == '1') {
        cout << cnt[pr1[i - 1]] + 1 << endl;
      } else {
        ll r = (r2[n - i - 1] + pr2[i - 1]) % (c + 1);
        cout << cnt[r] + 1 << endl;
      }
    } else {
      if (s[i] == '1') {
        ll r = (pr1[i - 1] + sr1[i + 1]) % (c - 1);
        cout << cnt[r] + 1 << endl;
      } else {
        ll r = (pr2[i - 1] + sr2[i + 1] + r2[n - 1 - i]) % (c + 1);
        cout << cnt[r] + 1 << endl;
      }
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define pb push_back

#define sec second
#define fir first
#define mo 1000000007
#define inf 1e18
#define rep(i, s, n) for (ll i = s; i < n; i = i + 1)
#define rrep(i, s, n) for (ll i = s; i >= n; i--)

ll mod(ll n) { return (n % (ll)mo + (ll)mo) % (ll)mo; }

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll c = 0;
  rep(i, 0, n) {
    if (s[i] == '1')
      ++c;
  }

  if (c == 0) {
    rep(i, 0, n) { cout << 1 << endl; }
    return 0;
  }
  if (c == 1) {
    rep(i, 0, n - 1) {
      if (s[i] == '0') {
        if (s[n - 1] != '1')
          cout << 1 << endl;
        else
          cout << 2 << endl;
      }

      else {
        cout << 0 << endl;
      }
    }
    if (s[n - 1] == '0')
      cout << 2 << endl;
    else
      cout << 0 << endl;
    return 0;
  }
  vector<ll> cnt(n + 1, 0);
  cnt[1] = 1;
  cnt[2] = 1;
  rep(i, 3, n + 1) {
    ll x = __builtin_popcount(i);
    cnt[i] = cnt[i % x] + 1;
  }
  vector<ll> r1(n + 1), r2(n + 1);
  r1[0] = 1 % (c - 1);
  r2[0] = 1 % (c + 1);
  rep(i, 1, n) {
    r1[i] = (r1[i - 1] * 2) % (c - 1);
    r2[i] = (r2[i - 1] * 2) % (c + 1);
  }
  vector<ll> pr1(n + 1, 0), pr2(n + 1, 0);
  vector<ll> sr1(n + 2, 0), sr2(n + 2, 0);

  if (s[0] == '1')
    pr1[0] = r1[n - 1], pr2[0] = r2[n - 1];

  rep(i, 1, n) {
    if (s[i] == '1') {
      pr1[i] = (pr1[i - 1] + r1[n - i - 1]) % (c - 1);
      pr2[i] = (pr2[i - 1] + r2[n - i - 1]) % (c + 1);
    } else {
      pr1[i] = pr1[i - 1];
      pr2[i] = pr2[i - 1];
    }
    // cout<<i<<" "<<pr1[i]<<" "<<pr2[i]<<endl;
  }
  if (s[n - 1] == '1')
    sr1[n - 1] = r1[0], sr2[n - 1] = r2[0];
  rrep(i, n - 2, 0) {
    if (s[i] == '1') {
      sr1[i] = (sr1[i + 1] + r1[n - i - 1]) % (c - 1);
      sr2[i] = (sr2[i + 1] + r2[n - i - 1]) % (c + 1);
    } else {
      sr1[i] = sr1[i + 1];
      sr2[i] = sr2[i + 1];
    }
    // cout<<i<<" "<<sr1[i]<<" "<<sr2[i]<<endl;
  }

  rep(i, 0, n) {
    if (i == 0) {
      if (s[i] == '1') {
        cout << cnt[sr1[i + 1]] + 1 << endl;
      } else {
        ll r = (r2[n - i - 1] + sr2[i + 1]) % (c + 1);
        cout << cnt[r] + 1 << endl;
      }
    } else if (i == n - 1) {
      if (s[i] == '1') {
        cout << cnt[pr1[i - 1]] + 1 << endl;
      } else {
        ll r = (r2[n - i - 1] + pr2[i - 1]) % (c + 1);
        cout << cnt[r] + 1 << endl;
      }
    } else {
      if (s[i] == '1') {
        ll r = (pr1[i - 1] + sr1[i + 1]) % (c - 1);
        cout << cnt[r] + 1 << endl;
      } else {
        ll r = (pr2[i - 1] + sr2[i + 1] + r2[n - 1 - i]) % (c + 1);
        cout << cnt[r] + 1 << endl;
      }
    }
  }
}