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

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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n)                                                          \\
  for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n)                                                         \\
  for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout << d << endl;
#define coutd(d) cout << std::setprecision(10) << d << endl;
#define cinline(n) getline(cin, n);
#define replace_all(s, b, a) replace(s.begin(), s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll, ll, ll>>;
using vb = vector<bool>;

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

const ll INF = 1e9 + 10;
ll MOD = 1e9 + 7;
const ll LINF = 1e18;

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    (a *= a) %= MOD;
    n >>= 1;
  }
  return res;
}

ll n;
string x;
ll popcnt(string x) {
  ll res = 0;
  rep(i, n) if (x[i] == '1') res++;
  return res;
}
void rev(ll i) {
  if (x[i] == '1')
    x[i] = '0';
  else
    x[i] = '1';
}

ll bitcount(ll x) {
  if (x == 0)
    return 0;
  return bitcount(x >> 1) + (x & 1);
}

ll f(ll a) {
  ll cnt = bitcount(a);
  return a % cnt;
}

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

  cin >> n >> x;
  ll cnt = popcnt(x);

  // 1回目の+1, -1のmodを求めたい
  ll aa = 0, bb = 0;
  MOD = cnt + 1;
  rep(i, n) {
    if (x[i] == '1')
      (aa += modpow(2, n - i - 1)) %= MOD;
  }
  MOD = cnt - 1;
  if (MOD)
    rep(i, n) {
      if (x[i] == '1')
        (bb += modpow(2, n - i - 1)) %= MOD;
    }

  // cout<<aa<<bb<<endl;

  rep(i, n) {
    ll ans = 1;
    if (x[i] == '1') {
      MOD = cnt - 1;
      if (MOD == 0) {
        cout << 0 << endl;
        continue;
      }
      ll tmp = modpow(2, n - i - 1);
      ll mo = (bb - tmp + MOD) % MOD;
      while (mo > 0) {
        mo = f(mo);
        ans++;
      }
    } else {
      MOD = cnt + 1;
      ll tmp = modpow(2, n - i - 1);
      ll mo = (aa + tmp) % MOD;
      while (mo > 0) {
        mo = f(mo);
        ans++;
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

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

// #define int long long
#define rep(i, n) for (long long i = (long long)(0); i < (long long)(n); ++i)
#define reps(i, n) for (long long i = (long long)(1); i <= (long long)(n); ++i)
#define rrep(i, n) for (long long i = ((long long)(n)-1); i >= 0; i--)
#define rreps(i, n) for (long long i = ((long long)(n)); i > 0; i--)
#define irep(i, m, n)                                                          \\
  for (long long i = (long long)(m); i < (long long)(n); ++i)
#define ireps(i, m, n)                                                         \\
  for (long long i = (long long)(m); i <= (long long)(n); ++i)
#define SORT(v, n) sort(v, v + n);
#define REVERSE(v, n) reverse(v, v + n);
#define vsort(v) sort(v.begin(), v.end());
#define all(v) v.begin(), v.end()
#define mp(n, m) make_pair(n, m);
#define cout(d) cout << d << endl;
#define coutd(d) cout << std::setprecision(10) << d << endl;
#define cinline(n) getline(cin, n);
#define replace_all(s, b, a) replace(s.begin(), s.end(), b, a);
#define PI (acos(-1))
#define FILL(v, n, x) fill(v, v + n, x);
#define sz(x) long long(x.size())

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vs = vector<string>;
using vpll = vector<pair<ll, ll>>;
using vtp = vector<tuple<ll, ll, ll>>;
using vb = vector<bool>;

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

const ll INF = 1e9 + 10;
ll MOD = 1e9 + 7;
const ll LINF = 1e18;

ll modpow(ll a, ll n) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % MOD;
    (a *= a) %= MOD;
    n >>= 1;
  }
  return res;
}

ll n;
string x;
ll popcnt(string x) {
  ll res = 0;
  rep(i, n) if (x[i] == '1') res++;
  return res;
}
void rev(ll i) {
  if (x[i] == '1')
    x[i] = '0';
  else
    x[i] = '1';
}

ll bitcount(ll x) {
  if (x == 0)
    return 0;
  return bitcount(x >> 1) + (x & 1);
}

ll f(ll a) {
  ll cnt = bitcount(a);
  return a % cnt;
}

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

  cin >> n >> x;
  ll cnt = popcnt(x);

  // 1回目の+1, -1のmodを求めたい
  ll aa = 0, bb = 0;
  MOD = cnt + 1;
  rep(i, n) {
    if (x[i] == '1')
      (aa += modpow(2, n - i - 1)) %= MOD;
  }
  MOD = cnt - 1;
  rep(i, n) {
    if (x[i] == '1')
      (bb += modpow(2, n - i - 1)) %= MOD;
  }

  // cout<<aa<<bb<<endl;

  rep(i, n) {
    ll ans = 1;
    if (x[i] == '1') {
      MOD = cnt - 1;
      if (MOD == 0) {
        cout << 0 << endl;
        continue;
      }
      ll tmp = modpow(2, n - i - 1);
      ll mo = (bb - tmp + MOD) % MOD;
      while (mo > 0) {
        mo = f(mo);
        ans++;
      }
    } else {
      MOD = cnt + 1;
      ll tmp = modpow(2, n - i - 1);
      ll mo = (aa + tmp) % MOD;
      while (mo > 0) {
        mo = f(mo);
        ans++;
      }
    }
    cout << ans << endl;
  }
}