Problem p02609 (C++) — buggy submission, verdict: Runtime Error

/*---------------------------------- NEVER GIVE UP
 * --------------------------------------------*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef double ld;
typedef pair<ll, ll> pii;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;

#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define debug1(x) cerr << #x << " : " << x << endl
#define debug2(x, y) cerr << #x << " : " << x << " " << #y << " : " << y << endl
#define debug3(x, y, z)                                                        \\
  cerr << #x << " : " << x << " " << #y << " : " << y << " " << #z << " : "    \\
       << z << endl
#define fastt                                                                  \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)

const ll modd = 1e9 + 7;
const ll inff = 1e18;

ll poww(ll a, ll b, ll modulo) {
  ll ans = 1;
  while (b) {
    if (b & 1LL)
      ans = (ans * a) % modulo;
    a = (a * a) % modulo;
    b = b >> 1LL;
  }
  return ans;
}

/*-----------------------------------------------------------------------------------------------*/

int main() {
  fastt;

  ll n, rem[3] = {0}, tot = 0;
  string x;

  cin >> n >> x;

  for (auto j : x)
    tot += (j == '1' ? 1 : 0);

  for (ll i = 0; i < n; i++) {
    if (tot - 1 > 0) {
      rem[0] =
          (rem[0] + (x[i] - '0') * poww(2, n - i - 1, (tot - 1))) % (tot - 1);
    }
    if (tot > 0) {
      rem[1] = (rem[1] + (x[i] - '0') * poww(2, n - i - 1, (tot))) % (tot);
    }
    if (tot + 1 > 0) {
      rem[2] =
          (rem[2] + (x[i] - '0') * poww(2, n - i - 1, (tot + 1))) % (tot + 1);
    }
  }

  for (ll i = 0; i < n; i++) {
    ll ans = 0, num = 0, cnt = 0;
    if (x[i] == '1') {
      if (tot - 1 > 0) {
        ans++;
        num =
            (rem[0] - poww(2, n - i - 1, tot - 1) + 2 * (tot - 1)) % (tot - 1);
      }
      while (tot - 1 > 0 && num > 0) {
        cnt = __builtin_popcount(num);
        num %= cnt;
        ans++;
      }
      cout << ans << '\\n';
    } else {
      if (tot + 1 > 0) {
        ans++;
        num = (rem[2] + poww(2, n - i - 1, tot + 1)) % (tot + 1);
      }
      while (tot + 1 > 0 && num > 0) {
        cnt = __builtin_popcount(num);
        num %= cnt;
        ans++;
      }
      cout << ans << '\\n';
    }
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const int inf = 1001001001;

int pcnt(int n) { return __builtin_popcount(n); }

int f(int n) {
  if (n == 0)
    return 0;
  return f(n % pcnt(n)) + 1;
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  vector<int> x(n);
  rep(i, n) x.at(i) = s.at(i) - '0';

  int pc = 0;
  rep(i, n) pc += x.at(i);
  vector<int> ans(n);

  rep(b, 2) {
    int npc = pc;
    if (b == 0)
      npc++;
    else
      npc--;
    if (pc <= 0)
      continue;
    int r0 = 0;
    rep(j, n) {
      r0 = (r0 * 2) % npc;
      r0 += x.at(b);
    }

    int k = 1;
    for (int j = n - 1; j >= 0; j--) {
      if (x.at(j) == b) {
        int r = r0;
        if (b == 0)
          r = (r + k) % npc;
        else
          r = (r - k + npc) % npc;
        ans.at(j) = f(r) + 1;
      }
      k = (k * 2) % npc;
    }
  }
  rep(i, n) cout << ans.at(i) << endl;
}