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

#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

ll solve(ll x) {
  ll ans = 0;
  while (x > 0) {
    ll now = 0;
    rep(i, 40) if (x & (1LL << i)) now++;
    x %= now;
    ans++;
  }
  return ans;
}

ll modpow(ll a, ll b, ll m) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  ll memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}

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

  ll sum1 = 0, sum2 = 0;
  reverse(all(b));
  rep(i, n) if (b[i] == 1) sum1 = (sum1 + modpow(2, i, (cnt + 1))) % (cnt + 1);
  if (cnt > 1) {
    rep(i, n) if (b[i] == 1) sum2 =
        (sum2 + modpow(2, i, (cnt - 1))) % (cnt - 1);
  }

  vector<ll> ans(n);
  rep(i, n) {
    if (b[i] == 0) {
      cnt++;
      ll now = (sum1 + modpow(2, i, cnt)) % cnt;
      ans[i] = solve(now) + 1;
      cnt--;
    } else {
      if (cnt == 1) {
        ans[i] = 0;
        continue;
      }
      cnt--;
      ll now = (sum2 - modpow(2, i, cnt) + 1000 * cnt) % cnt;
      ans[i] = solve(now) + 1;
      cnt++;
    }
  }

  reverse(all(ans));
  rep(i, n) cout << ans[i] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else {
    return false;
  }
}
#define ll long long
#define double long double
#define rep(i, n) for (int i = 0; i < (n); i++)
#define REP(i, n) for (int i = 1; i <= (n); i++)
#define mod (ll)(1e9 + 7)
#define inf (ll)(3e18 + 7)
#define pi (double)acos(-1.0)
#define P pair<int, int>
#define PiP pair<int, pair<int, int>>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
using namespace std;

ll solve(ll x) {
  ll ans = 0;
  while (x > 0) {
    ll now = 0;
    rep(i, 40) if (x & (1LL << i)) now++;
    x %= now;
    ans++;
  }
  return ans;
}

ll modpow(ll a, ll b, ll m) {
  if (!b)
    return 1;
  if (b & 1)
    return modpow(a, b - 1, m) * a % m;
  ll memo = modpow(a, b >> 1, m);
  return memo * memo % m;
}

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

  ll sum1 = 0, sum2 = 0;
  reverse(all(b));
  rep(i, n) if (b[i] == 1) sum1 = (sum1 + modpow(2, i, (cnt + 1))) % (cnt + 1);
  if (cnt != 0) {
    rep(i, n) if (b[i] == 1) sum2 =
        (sum2 + modpow(2, i, (cnt - 1))) % (cnt - 1);
  }

  vector<ll> ans(n);
  rep(i, n) {
    if (b[i] == 0) {
      cnt++;
      ll now = (sum1 + modpow(2, i, cnt)) % cnt;
      ans[i] = solve(now) + 1;
      cnt--;
    } else {
      cnt--;
      ll now = (sum2 - modpow(2, i, cnt) + 1000 * cnt) % cnt;
      ans[i] = solve(now) + 1;
      cnt++;
    }
  }

  reverse(all(ans));
  rep(i, n) cout << ans[i] << endl;
}