Problem p02609 (C++)

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

// Inserted snippets: io, root
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}
// End snippets

ll bm(ll b, ll p, ll m) {
  ll r = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      r = r * b % m;
  return r;
}

ll longmod(string s, ll m) {
  ll r = 0;
  for (char c : s) {
    r = (r * 2 + c - '0') % m;
  }
  return r;
}

ll f(ll x) {
  if (x == 0)
    return 0;
  return 1 + f(x % __builtin_popcountll(x));
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n;
  read(n);
  string s;
  read(s);
  int bc = count(all(s), '1');

  map<int, int> init;
  for (int i = bc - 1; i <= bc + 1; i++)
    init[i] = longmod(s, i);

  for (int i = 0; i < n; i++) {
    int c1 = bc - int(s[i] == '1') + int(s[i] == '0');
    int rv = init[c1];
    if (c1 == 0) {
      write(0);
      continue;
    }
    if (s[i] == '1') {
      // subtract
      rv -= bm(2, n - 1 - i, c1);
    } else {
      rv += bm(2, n - 1 - i, c1);
    }
    rv = (rv % c1 + c1) % c1;
    cout << 1 + f(rv) << endl;
  }
}


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

// Inserted snippets: io, root
#define rep(i, a, b) for (int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
template <typename H> bool chmin(H &v1, const H v2) {
  if (v1 > v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> bool chmax(H &v1, const H v2) {
  if (v1 < v2) {
    v1 = v2;
    return true;
  }
  return false;
}
template <typename H> void read(H &head) { cin >> head; }
template <typename H, typename... T> void read(H &head, T &...tail) {
  cin >> head;
  read(tail...);
}
template <typename H> void write(H head) { cout << head << '\n'; }
template <typename H, typename... T> void write(H head, T... tail) {
  cout << head << " ";
  write(tail...);
}
template <typename... T> void die(T... tok) {
  write(tok...);
  exit(0);
}
// End snippets

ll bm(ll b, ll p, ll m) {
  ll r = 1;
  for (; p; p >>= 1, b = b * b % m)
    if (p & 1)
      r = r * b % m;
  return r;
}

ll longmod(string s, ll m) {
  ll r = 0;
  for (char c : s) {
    r = (r * 2 + c - '0') % m;
  }
  return r;
}

ll f(ll x) {
  if (x == 0)
    return 0;
  return 1 + f(x % __builtin_popcountll(x));
}

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);

  int n;
  read(n);
  string s;
  read(s);
  int bc = count(all(s), '1');

  map<int, int> init;
  for (int i = bc - 1; i <= bc + 1; i++)
    if (i > 0)
      init[i] = longmod(s, i);

  for (int i = 0; i < n; i++) {
    int c1 = bc - int(s[i] == '1') + int(s[i] == '0');
    int rv = init[c1];
    if (c1 == 0) {
      write(0);
      continue;
    }
    if (s[i] == '1') {
      // subtract
      rv -= bm(2, n - 1 - i, c1);
    } else {
      rv += bm(2, n - 1 - i, c1);
    }
    rv = (rv % c1 + c1) % c1;
    cout << 1 + f(rv) << endl;
  }
}