Problem p02609 (C++)

Buggy submission (status: Runtime Error):
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) a.begin(), a.end()
template <typename T = ll> using v = vector<T>;
template <typename T = ll> using vv = v<v<T>>;
template <typename T = ll, typename U = ll> using p = pair<T, U>;
template <typename T> T chmin(T &a, T b) { return a = min(a, b); }
template <typename T> T chmax(T &a, T b) { return a = max(a, b); }
const ll INF = (ll)1e9;
template <int MOD> struct ModInt {
  using lint = long long;
  int val;

  // constructor
  ModInt(lint v = 0) : val(v % MOD) {
    if (val < 0)
      val += MOD;
  };

  // unary operator
  ModInt operator+() const { return ModInt(val); }
  ModInt operator-() const { return ModInt(MOD - val); }
  ModInt inv() const { return this->pow(MOD - 2); }

  // arithmetic
  ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
  ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; }
  ModInt pow(lint n) const {
    auto x = ModInt(1);
    auto b = *this;
    while (n > 0) {
      if (n & 1)
        x *= b;
      n >>= 1;
      b *= b;
    }
    return x;
  }

  // compound assignment
  ModInt &operator+=(const ModInt &x) {
    if ((val += x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &x) {
    if ((val -= x.val) < 0)
      val += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &x) {
    val = lint(val) * x.val % MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt &x) { return *this *= x.inv(); }
  //
  ModInt operator++(int) {
    auto t = *this;
    *this += 1;
    return t;
  }

  ModInt operator--(int) {
    auto t = *this;
    *this -= 1;
    return t;
  }
  ModInt operator<<(const int n) { return ModInt(*this) * ModInt(2).pow(n); }
  // compare
  bool operator==(const ModInt &b) const { return val == b.val; }
  bool operator!=(const ModInt &b) const { return val != b.val; }

  // I/O
  friend std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
    return is >> x.val;
  }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept {
    return os << x.val;
  }
};

constexpr int MOD = 1e9 + 7;
using mint = ModInt<MOD>;
void a() {
  ll L, R, d;
  cin >> L >> R >> d;
  ll cnt = d;
  ll ans = 0;
  if (R < d) {
    cout << 0 << endl;
    return;
  }

  while (cnt <= R) {
    if (L <= cnt)
      ans++;
    cnt += d;
  }
  cout << ans << endl;
}

void b() {
  ll N;
  cin >> N;
  auto A = v<>(N, 0);
  rep(i, N) cin >> A[i];
  ll ans = 0;
  for (ll i = 0; i < N; i += 2) {
    if (A[i] % 2)
      ans++;
  }
  cout << ans;
}

void c() {
  ll N;
  cin >> N;

  for (ll n = 1; n <= N; n++) {
    ll cnt = 0;
    for (ll x = 1; x * x < n; x++)
      for (ll y = 1; x * x + y * y + y * x < n; y++)
        for (ll z = 1; x * x + y * y + z * z + x * y + y * z + z * x <= n;
             z++) {
          if (x * x + y * y + z * z + x * y + y * z + z * x == n) {
            cnt++;
            break;
          }
        }
    cout << cnt << endl;
  }
}

ll popcount(ll x) { return __builtin_popcount(x); }

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

void d() {
  ll N;
  cin >> N;
  string s;
  cin >> s;
  v<> x(N, 0);
  rep(i, N) { x[i] = s[i] - '0'; }

  ll pct = accumulate(all(x), 0);

  v<> ans(N + 1, 0);
  rep(b, 2) {
    ll npct = (b == 0) ? pct + 1 : pct - 1;
    ll n = 0;
    for (ll i = 0; i < N; i++) {
      n = (n * 2) % npct;
      n += x[i];
    }

    ll k = 1;
    for (ll i = N - 1; i >= 0; i--) {
      if (x[i] == b) {
        ll nn = 0;
        if (b == 0)
          nn = (n + k) % npct;
        else
          nn = (n - k + npct) % npct;
        ans[i] = f(nn) + 1;
      }
      k = (k * 2) % npct;
    }
  }

  rep(i, N) { cout << ans[i] << endl; }
}

int main() { d(); }

Fixed submission:
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define all(a) a.begin(), a.end()
template <typename T = ll> using v = vector<T>;
template <typename T = ll> using vv = v<v<T>>;
template <typename T = ll, typename U = ll> using p = pair<T, U>;
template <typename T> T chmin(T &a, T b) { return a = min(a, b); }
template <typename T> T chmax(T &a, T b) { return a = max(a, b); }
const ll INF = (ll)1e9;
template <int MOD> struct ModInt {
  using lint = long long;
  int val;

  // constructor
  ModInt(lint v = 0) : val(v % MOD) {
    if (val < 0)
      val += MOD;
  };

  // unary operator
  ModInt operator+() const { return ModInt(val); }
  ModInt operator-() const { return ModInt(MOD - val); }
  ModInt inv() const { return this->pow(MOD - 2); }

  // arithmetic
  ModInt operator+(const ModInt &x) const { return ModInt(*this) += x; }
  ModInt operator-(const ModInt &x) const { return ModInt(*this) -= x; }
  ModInt operator*(const ModInt &x) const { return ModInt(*this) *= x; }
  ModInt operator/(const ModInt &x) const { return ModInt(*this) /= x; }
  ModInt pow(lint n) const {
    auto x = ModInt(1);
    auto b = *this;
    while (n > 0) {
      if (n & 1)
        x *= b;
      n >>= 1;
      b *= b;
    }
    return x;
  }

  // compound assignment
  ModInt &operator+=(const ModInt &x) {
    if ((val += x.val) >= MOD)
      val -= MOD;
    return *this;
  }
  ModInt &operator-=(const ModInt &x) {
    if ((val -= x.val) < 0)
      val += MOD;
    return *this;
  }
  ModInt &operator*=(const ModInt &x) {
    val = lint(val) * x.val % MOD;
    return *this;
  }
  ModInt &operator/=(const ModInt &x) { return *this *= x.inv(); }
  //
  ModInt operator++(int) {
    auto t = *this;
    *this += 1;
    return t;
  }

  ModInt operator--(int) {
    auto t = *this;
    *this -= 1;
    return t;
  }
  ModInt operator<<(const int n) { return ModInt(*this) * ModInt(2).pow(n); }
  // compare
  bool operator==(const ModInt &b) const { return val == b.val; }
  bool operator!=(const ModInt &b) const { return val != b.val; }

  // I/O
  friend std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
    return is >> x.val;
  }
  friend std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept {
    return os << x.val;
  }
};

constexpr int MOD = 1e9 + 7;
using mint = ModInt<MOD>;
void a() {
  ll L, R, d;
  cin >> L >> R >> d;
  ll cnt = d;
  ll ans = 0;
  if (R < d) {
    cout << 0 << endl;
    return;
  }

  while (cnt <= R) {
    if (L <= cnt)
      ans++;
    cnt += d;
  }
  cout << ans << endl;
}

void b() {
  ll N;
  cin >> N;
  auto A = v<>(N, 0);
  rep(i, N) cin >> A[i];
  ll ans = 0;
  for (ll i = 0; i < N; i += 2) {
    if (A[i] % 2)
      ans++;
  }
  cout << ans;
}

void c() {
  ll N;
  cin >> N;

  for (ll n = 1; n <= N; n++) {
    ll cnt = 0;
    for (ll x = 1; x * x < n; x++)
      for (ll y = 1; x * x + y * y + y * x < n; y++)
        for (ll z = 1; x * x + y * y + z * z + x * y + y * z + z * x <= n;
             z++) {
          if (x * x + y * y + z * z + x * y + y * z + z * x == n) {
            cnt++;
            break;
          }
        }
    cout << cnt << endl;
  }
}

ll popcount(ll x) { return __builtin_popcount(x); }

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

void d() {
  ll N;
  cin >> N;
  string s;
  cin >> s;
  v<> x(N, 0);
  rep(i, N) { x[i] = s[i] - '0'; }

  ll pct = accumulate(all(x), 0);

  v<> ans(N + 1, 0);
  rep(b, 2) {
    ll npct = (b == 0) ? pct + 1 : pct - 1;
    ll n = 0;
    if (npct == 0)
      continue;
    for (ll i = 0; i < N; i++) {
      n = (n * 2) % npct;
      n += x[i];
    }

    ll k = 1;
    for (ll i = N - 1; i >= 0; i--) {
      if (x[i] == b) {
        ll nn = 0;
        if (b == 0)
          nn = (n + k) % npct;
        else
          nn = (n - k + npct) % npct;
        ans[i] = f(nn) + 1;
      }
      k = (k * 2) % npct;
    }
  }

  rep(i, N) { cout << ans[i] << endl; }
}

int main() { d(); }