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

#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define rrep(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).begin(), (x).end()
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
#define LINF 1e18 + 10
using ll = long long;

using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
using PPI = pair<P, int>;
using Ps = pair<int, string>;

using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vd = vector<double>;
using vs = vector<string>;
using vp = vector<P>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
using vvb = vector<vector<bool>>;
template <typename T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

//////////////////////////////////////
ll modpow(ll a, ll b, int mod) {
  ll r = 1;
  while (b > 0) {
    if (b % 2 == 1)
      (r *= a) %= mod;
    (a *= a) %= mod;
    b /= 2;
  }
  return r;
}

int f(ll n) {
  int ret = 0;
  while (n > 0) {
    ret += n % 2;
    n /= 2;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt1 = 0;
  rep(i, n) {
    if (s[i] == '1')
      cnt1++;
  }
  vl a(n);
  vl b(n);
  ll suma = 0, sumb = 0;
  rep(i, n) {
    a[i] = modpow(2, n - 1 - i, cnt1 + 1);
    if (cnt1 > 1)
      b[i] = modpow(2, n - 1 - i, cnt1 - 1);
    if (s[i] == '1') {
      suma += a[i];
      sumb += b[i];
    }
  }
  vl ans(n);
  rep(i, n) {

    if (s[i] == '1') {
      int cnt = cnt1 - 1;
      ll tmp = sumb - b[i];
      if (cnt > 0) {
        tmp %= (cnt);
        ans[i]++;
      } else if (cnt)
        ans[i] = 1;
      while (tmp > 0) {
        cnt = f(tmp);
        tmp %= (cnt);
        ans[i]++;
      }
    } else {
      int cnt = cnt1 + 1;
      ll tmp = suma + a[i];
      if (cnt > 0) {
        tmp %= (cnt);
        ans[i]++;
      } else if (cnt)
        ans[i] = 1;
      while (tmp > 0) {

        cnt = f(tmp);
        tmp %= (cnt);
        ans[i]++;
      }
    }
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define rrep(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).begin(), (x).end()
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
#define LINF 1e18 + 10
using ll = long long;

using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
using PPI = pair<P, int>;
using Ps = pair<int, string>;

using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vd = vector<double>;
using vs = vector<string>;
using vp = vector<P>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvd = vector<vector<double>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
using vvb = vector<vector<bool>>;
template <typename T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

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

//////////////////////////////////////
ll modpow(ll a, ll b, int mod) {
  ll r = 1;
  while (b > 0) {
    if (b % 2 == 1)
      (r *= a) %= mod;
    (a *= a) %= mod;
    b /= 2;
  }
  return r;
}

int f(ll n) {
  int ret = 0;
  while (n > 0) {
    ret += n % 2;
    n /= 2;
  }
  return ret;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt1 = 0;
  rep(i, n) {
    if (s[i] == '1')
      cnt1++;
  }
  vl a(n);
  vl b(n);
  ll suma = 0, sumb = 0;
  rep(i, n) {
    a[i] = modpow(2, n - 1 - i, cnt1 + 1);
    b[i] = modpow(2, n - 1 - i, cnt1 - 1);
    if (s[i] == '1') {
      suma += a[i];
      sumb += b[i];
    }
  }
  vl ans(n);
  rep(i, n) {
    int cnt = cnt1;
    if (s[i] == '1') {
      ll tmp = sumb - b[i];
      if (cnt > 1) {
        tmp %= (cnt - 1);
        ans[i]++;
      }
      while (tmp > 0) {

        cnt = f(tmp);
        tmp %= (cnt);
        ans[i]++;
      }
    } else {
      ll tmp = suma + a[i];
      if (tmp > 0) {
        tmp %= (cnt + 1);
        ans[i]++;
      }
      while (tmp > 0) {

        cnt = f(tmp);
        tmp %= (cnt);
        ans[i]++;
      }
    }
  }
  rep(i, n) cout << ans[i] << endl;
  return 0;
}