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

#include <bits/stdc++.h>
using namespace std;
template <class T> T input() {
  T t;
  cin >> t;
  return t;
}
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define PRINT_ARRAY_H(a)                                                       \\
  REPALL(__i, a) { cout << a[__i] << (__i != a.size() - 1 ? " " : ""); }       \\
  newline;
#define e1 first
#define e2 second
#define newline putchar('\\n')
#define cspace ' '
#define sspace " "
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};

ll mypow(ll a, ll b, ll mod) {
  ll r = 1ll;
  while (b != 0) {
    if (b & 1)
      r = r * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return r;
}

ll f(int num) {
  ll r = 0;
  while (num) {
    r += (num & 1ll);
    num /= 2;
  }
  return r;
}

int main() {
  ll n;
  string x;
  cin >> n >> x;
  ll pop = 0, incpop, decpop;
  ll num, incnum = 0ll, decnum = 0ll;
  REP(i, n) pop += (x[i] == '1');
  incpop = min(pop + 1, n);
  decpop = max(pop - 1, 0ll);
  REP(i, n) {
    if (x[i] == '1') {
      incnum += mypow(2, n - 1 - i, incpop);
      incnum %= incpop;
      if (decpop != 0) {
        decnum += mypow(2, n - 1 - i, decpop);
        decnum %= decpop;
      }
    }
  }

  // cout<<incpop<<sspace<<decpop<<endl;
  // cout<<incnum<<sspace<<decnum<<endl;
  REP(i, n) {

    int cnt = 1;
    if (x[i] == '0') {
      num = (incnum + mypow(2, n - 1 - i, incpop)) % incpop;
    } else {
      if (decpop == 0) {
        cout << 0 << endl;
        continue;
      }

      num = (decnum - mypow(2, n - 1 - i, decpop) + decpop) % decpop;
    }
    // cout<<num<<": ";
    while (num) {
      num = num % f(num);
      cnt++;
    }

    cout << cnt << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> T input() {
  T t;
  cin >> t;
  return t;
}
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, b) FOR(i, 0, b)
#define RFOR(i, a, b) for (ll i = a - 1; i >= b; i--)
#define RREP(i, a) RFOR(i, a, 0)
#define REPALL(i, x) for (ll i = 0; i < x.size(); i++)
#define RREPALL(i, x) for (ll i = x.size() - 1; i >= 0; i--)
#define REPITR(itr, x) for (auto itr = (x).begin(); itr != (x).end(); itr++)
#define ALL(x) (x).begin(), (x).end()
#define SORT(x) sort(ALL(x))
#define MIN_ELEMENT(x) min_element(ALL(x))
#define MAX_ELEMENT(x) max_element(ALL(x))
#define COUNT(x, num) count(ALL(x), num)
#define MEMSET(x, val) memset(x, val, sizeof(x))
#define CHMAX(a, b) a = max(a, b)
#define CHMIN(a, b) a = min(a, b)
#define debug(x) cerr << __LINE__ << ": " << (#x) << " = " << (x) << endl;
void YES(bool flag) { cout << (flag ? "YES" : "NO") << endl; }
void Yes(bool flag) { cout << (flag ? "Yes" : "No") << endl; }
void yes(bool flag) { cout << (flag ? "yes" : "no") << endl; }
#define PRINT_ARRAY_H(a)                                                       \\
  REPALL(__i, a) { cout << a[__i] << (__i != a.size() - 1 ? " " : ""); }       \\
  newline;
#define e1 first
#define e2 second
#define newline putchar('\\n')
#define cspace ' '
#define sspace " "
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> VI;
typedef vector<vector<int>> VVI;
typedef vector<ll> VLL;
typedef vector<vector<ll>> VVLL;
const int INF = 1e7;
const ll MOD = 1e9 + 7;
const double pi = 3.141592653589793;
const VI dx = {1, 0, -1, 0};
const VI dy = {0, 1, 0, -1};

ll mypow(ll a, ll b, ll mod) {
  ll r = 1ll;
  while (b != 0) {
    if (b & 1)
      r = r * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return r;
}

ll f(int num) {
  ll r = 0;
  while (num) {
    r += (num & 1ll);
    num /= 2;
  }
  return r;
}

int main() {
  ll n;
  string x;
  cin >> n >> x;
  ll pop = 0, incpop, decpop;
  ll num, incnum = 0ll, decnum = 0ll;
  REP(i, n) pop += (x[i] == '1');
  incpop = min(pop + 1, n);
  decpop = max(pop - 1, 0ll);
  REP(i, n) {
    if (x[i] == '1') {
      incnum += mypow(2, n - 1 - i, incpop);
      incnum %= incpop;
      decnum += mypow(2, n - 1 - i, decpop);
      decnum %= decpop;
    }
  }

  // cout<<incpop<<sspace<<decpop<<endl;
  // cout<<incnum<<sspace<<decnum<<endl;
  REP(i, n) {
    int cnt = 1;
    if (x[i] == '0') {
      num = (incnum + mypow(2, n - 1 - i, incpop)) % incpop;
    } else {
      num = (decnum - mypow(2, n - 1 - i, decpop) + decpop) % decpop;
    }
    // cout<<num<<": ";
    while (num) {
      num = num % f(num);
      cnt++;
    }

    cout << cnt << endl;
  }

  return 0;
}