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

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
// typedef vector<vector<ll>> Graph;

const ll mod = 1e9 + 7;
// const ll mod = 998244353;

#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
  for (ll i = 0; i < h; i++) {
    cerr << v[i][0];
    for (ll j = 1; j < w; j++)
      cerr spa v[i][j];
    cerr << endl;
  }
};
template <typename T> void debug(vector<T> &v, ll n) {
  if (n != 0)
    cerr << v[0];
  for (ll i = 1; i < n; i++)
    cerr spa v[i];
  cerr << endl;
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  string S;
  cin >> N >> S;

  ll B = 0;
  REP(i, N) B += (S[i] - '0');
  vvl pow2(2, vl(N, 0));

  REP(b, 2) {
    ll val = 1;
    ll M = (b == 0 ? B + 1 : B - 1);
    REP(i, N) {
      if (M != 0) {
        pow2[b][N - i - 1] = val;
        val *= 2;
        val %= M;
      } else {
        pow2[b][N - i - 1] = 0;
      }
    }
  }

  vl sum(2);
  REP(b, 2) {
    ll val = 0;
    ll M = (b == 0 ? B + 1 : B - 1);
    REP(i, N) {
      val += (S[i] - '0') * pow2[b][i];
      if (M != 0)
        val %= M;
    }
    sum[b] = val;
  }

  auto G = [](ll x) {
    ll cnt = 0;
    while (x > 0) {
      ll bi = __builtin_popcountll(x);
      x = x % bi;
      cnt++;
    }
    return cnt;
  };

  vector<ll> res(N);
  REP(i, N) {
    ll M, X;
    if (S[i] == '0') {
      M = B + 1;
      X = sum[0] + pow2[0][i];
      X %= M;
    } else {
      M = B - 1;
      if (M != 0) {
        X = sum[1] - pow2[1][i];
        X = ((X % M) + M) % M;
      } else {
        X = 0;
      }
    }
    dump(B) if (S[i] == '1' and B == 1) res[i] = 0;
    else res[i] = G(X) + 1;
  }

  REP(i, N) cout << res[i] << endl;

  return 0;
}

Error: 0

Fixed submission:

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

typedef unsigned long long ull;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
// typedef vector<vector<ll>> Graph;

const ll mod = 1e9 + 7;
// const ll mod = 998244353;

#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define spa << " " <<
#define fi first
#define se second

template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> v) {
  os << "(" << v.first << ", " << v.second << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << " ";
    }
    os << v[i];
  }
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<vector<T>> v) {
  for (int i = 0; i < (int)v.size(); i++) {
    if (i > 0) {
      os << endl;
    }
    os << v[i];
  }
  return os;
}

template <typename T> void debug(vector<vector<T>> &v, ll h, ll w) {
  for (ll i = 0; i < h; i++) {
    cerr << v[i][0];
    for (ll j = 1; j < w; j++)
      cerr spa v[i][j];
    cerr << endl;
  }
};
template <typename T> void debug(vector<T> &v, ll n) {
  if (n != 0)
    cerr << v[0];
  for (ll i = 1; i < n; i++)
    cerr spa v[i];
  cerr << endl;
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll N;
  string S;
  cin >> N >> S;

  ll B = 0;
  REP(i, N) B += (S[i] - '0');
  vvl pow2(2, vl(N, 0));

  REP(b, 2) {
    ll val = 1;
    ll M = (b == 0 ? B + 1 : B - 1);
    REP(i, N) {
      if (M != 0) {
        pow2[b][N - i - 1] = val;
        val *= 2;
        val %= M;
      } else {
        pow2[b][N - i - 1] = 0;
      }
    }
  }

  vl sum(2);
  REP(b, 2) {
    ll val = 0;
    ll M = (b == 0 ? B + 1 : B - 1);
    REP(i, N) {
      val += (S[i] - '0') * pow2[b][i];
      val %= M;
    }
    sum[b] = val;
  }

  auto G = [](ll x) {
    ll cnt = 0;
    while (x > 0) {
      ll bi = __builtin_popcountll(x);
      x = x % bi;
      cnt++;
    }
    return cnt;
  };

  vector<ll> res(N);
  REP(i, N) {
    ll M, X;
    if (S[i] == '0') {
      M = B + 1;
      X = sum[0] + pow2[0][i];
      X %= M;
    } else {
      M = B - 1;
      if (M != 0) {
        X = sum[1] - pow2[1][i];
        X = ((X % M) + M) % M;
      } else {
        X = 0;
      }
    }
    res[i] = G(X) + 1;
  }

  REP(i, N) cout << res[i] << endl;

  return 0;
}