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

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

int f(int k) {
  int ans = 1;

  while (1) {
    int kk = k;
    int cnt = 0;

    while (k) {
      if (k % 2)
        cnt++;
      k /= 2;
    }

    k = kk % cnt;
    if (k == 0)
      return ans;
    ans++;
  }

  cout << "error" << endl;
  return -1;
}

void Main() {
  int n;
  string x;
  cin >> n >> x;

  int pcnt = 0;
  rep(i, n) {
    if (x[i] == '1')
      pcnt++;
  }

  int pcnt1 = pcnt + 1;
  int pcnt2 = pcnt - 1;
  int flag = 0;

  if (pcnt2 != 0)
    flag = 1;

  ll tot1 = 0;
  ll tot2 = 0;
  ll two1 = 1 % pcnt1;
  ll two2 = flag ? 1 % pcnt2 : -1;

  rep3rev(i, n - 1, 0) {
    int now = x[i] - '0';
    int tmp = (now * two1) % pcnt1;
    // cout << "now = " << now << ": ";
    // cout << two1 << ", " << two2 << ": ";
    // cout << tmp << " ";
    tot1 = (tot1 + tmp) % pcnt1;
    tmp = flag ? (now * two2) % pcnt2 : -1;
    // cout << tmp << endl;
    tot2 = flag ? (tot2 + tmp) % pcnt2 : -1;
    two1 = (two1 * 2) % pcnt1;
    two2 = flag ? (two2 * 2) % pcnt2 : -1;
  }

  // cout << pcnt1 << " " << tot1 << endl;
  // cout << pcnt2 << " " << tot2 << endl;

  two1 = 1 % pcnt1;
  two2 = flag ? 1 % pcnt2 : -1;
  vll ans(n);

  rep3rev(i, n - 1, 0) {
    int now = x[i] - '0';
    if (!now) {
      ans[i] = (tot1 + two1) % pcnt1;
      // cout << i << " " << tot1 << " " << two1 << " " << ans[i] << endl;
    } else {
      ans[i] = flag ? (tot2 + pcnt2 - two2) % pcnt2 : -1;
      // cout << i << " " << tot2 << " " << two2 << " " << ans[i] << endl;
    }
    two1 = (two1 * 2) % pcnt1;
    two2 = pcnt2 != 0 ? (two2 * 2) % pcnt2 : 0;
  }

  // rep3(i, 1, 10) cout << i << " " << f(i) << endl;

  //*
  rep(i, n) {
    // cout << ans[i] << " ";
    if (ans[i] == -1)
      cout << 0 << endl;
    else if (ans[i] == 0)
      cout << 1 << endl;
    else
      cout << f(ans[i]) + 1 << endl;
  }
  //*/
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}

Error: 0

Fixed submission:

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

#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rep3(i, m, n) for (int(i) = m; (i) <= (n); (i)++)
#define rep3rev(i, m, n) for (int(i) = m; (i) >= (n); (i)--)
#define all(a) (a.begin()), (a.end())
#define rall(a) (a.rbegin()), (a.rend())
#define fi first
#define se second
#define pb push_back
#define eb emplace_back

using ll = long long;
using vll = vector<ll>;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using P = pair<int, int>;

int f(int k) {
  int ans = 1;

  while (1) {
    int kk = k;
    int cnt = 0;

    while (k) {
      if (k % 2)
        cnt++;
      k /= 2;
    }

    k = kk % cnt;
    if (k == 0)
      return ans;
    ans++;
  }

  cout << "error" << endl;
  return -1;
}

void Main() {
  int n;
  string x;
  cin >> n >> x;

  int pcnt = 0;
  rep(i, n) {
    if (x[i] == '1')
      pcnt++;
  }

  int pcnt1 = pcnt + 1;
  int pcnt2 = pcnt - 1;

  ll tot1 = 0;
  ll tot2 = 0;
  ll two1 = 1 % pcnt1;
  ll two2 = 1 % pcnt2;

  rep3rev(i, n - 1, 0) {
    int now = x[i] - '0';
    int tmp = (now * two1) % pcnt1;
    // cout << "now = " << now << ": ";
    // cout << two1 << ", " << two2 << ": ";
    // cout << tmp << " ";
    tot1 = (tot1 + tmp) % pcnt1;
    tmp = (now * two2) % pcnt2;
    // cout << tmp << endl;
    tot2 = (tot2 + tmp) % pcnt2;
    two1 = (two1 * 2) % pcnt1;
    two2 = (two2 * 2) % pcnt2;
  }

  // cout << pcnt1 << " " << tot1 << endl;
  // cout << pcnt2 << " " << tot2 << endl;

  two1 = 1 % pcnt1;
  two2 = 1 % pcnt2;
  vll ans(n);

  rep3rev(i, n - 1, 0) {
    int now = x[i] - '0';
    if (!now) {
      ans[i] = (tot1 + two1) % pcnt1;
      // cout << i << " " << tot1 << " " << two1 << " " << ans[i] << endl;
    } else {
      ans[i] = (tot2 + pcnt2 - two2) % pcnt2;
      // cout << i << " " << tot2 << " " << two2 << " " << ans[i] << endl;
    }
    two1 = (two1 * 2) % pcnt1;
    two2 = (two2 * 2) % pcnt2;
  }

  // rep3(i, 1, 10) cout << i << " " << f(i) << endl;

  //*
  rep(i, n) {
    // cout << ans[i] << " ";
    if (ans[i] == 0)
      cout << 1 << endl;
    else
      cout << f(ans[i]) + 1 << endl;
  }
  //*/
  return;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}