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

#include <bits/stdc++.h>
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> state;
long double pi = acos(-1);
const int INF = 1001001001;

// 最小公倍数を求める
int euqlid(int a, int b) {
  int temp;
  temp = a % b;
  if (temp == 0)
    return b;
  return euqlid(b, temp);
}
ll conbination(ll a, ll b) {
  ll u = 1;
  ll d = 1;
  while (b != 0) {
    u *= a;
    d *= b;
    a--;
    b--;
  }
  return u / d;
}
int strtoint(char s) { return (int(s - '0')); }
char changeS(char alpha) {
  if (0x41 <= alpha and alpha <= 0x5A)
    return (alpha + 0x20);
  else if (0x61 <= alpha and alpha <= 0x7A)
    return (alpha - 0x20);
  return alpha;
}
int fact(int n) {
  if (n == 1)
    return 1;
  return n * fact(n - 1);
}
int ceil(int a, int b) {
  // a/bの切り上げを考える
  return (a + b - 1) / b;
}

int popcount(int n) {
  int cnt = 0;
  while (n > 0) {
    if (n == 1) {
      cnt++;
      break;
    }
    if (n % 2 == 1)
      cnt++;
    n /= 2;
  }
  return cnt;
}

int f(int n, int cnt) {
  if (n == 0)
    return cnt;
  n = n % popcount(n);
  cnt++;
  return f(n, cnt);
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;

  // 可変配列の初期化．初期値は0
  vector<int> X(n);
  vector<int> ans(n);

  // 文字列で入力した2進数をint型で再格納
  rep(i, n) X[i] = x[i] - '0';

  // 与えられた2進数でビットが立っている桁数を求める(popcount(n))
  int pc = 0;
  rep(i, n) pc += X[i];

  // popcount(n+1),popcount(n-1)について処理を行う．
  rep(b, 2) {
    int pcnt = pc;
    if (b == 0)
      pcnt++;
    else
      pcnt--;
    if (pcnt <= 0)
      continue;

    // n % popcountを行う
    int r = 0;
    rep(i, n) {
      r = (r * 2) % pcnt;
      r += X[i];
    }

    int k = 1;
    for (int i = n - 1; i >= 0; i--) {
      // 1bitごとにずらした分だけn % popcount(n)の処理を行う
      if (X[i] == b) {
        int tempr = r;
        if (b == 0)
          tempr = (tempr + k) % pcnt;
        else
          tempr = (tempr - k + pcnt) % pcnt;
        ans[i] = f(tempr, 0) + 1;
      }
      k = (k * 2) % pcnt;
    }
  }
  rep(i, n) cout << ans[i] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n)                                                              \\
  ;                                                                            \\
  for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef tuple<int, int, int> state;
long double pi = acos(-1);
const int INF = 1001001001;

// 最小公倍数を求める
int euqlid(int a, int b) {
  int temp;
  temp = a % b;
  if (temp == 0)
    return b;
  return euqlid(b, temp);
}
ll conbination(ll a, ll b) {
  ll u = 1;
  ll d = 1;
  while (b != 0) {
    u *= a;
    d *= b;
    a--;
    b--;
  }
  return u / d;
}
int strtoint(char s) { return (int(s - '0')); }
char changeS(char alpha) {
  if (0x41 <= alpha and alpha <= 0x5A)
    return (alpha + 0x20);
  else if (0x61 <= alpha and alpha <= 0x7A)
    return (alpha - 0x20);
  return alpha;
}
int fact(int n) {
  if (n == 1)
    return 1;
  return n * fact(n - 1);
}
int ceil(int a, int b) {
  // a/bの切り上げを考える
  return (a + b - 1) / b;
}

int popcount(int n) {
  int cnt = 0;
  while (n > 0) {
    if (n % 2 == 1)
      cnt++;
    n = n / 2;
  }
  return cnt;
}

ll f(int n, int ans) {
  if (n == 0)
    return ans;
  n = n % popcount(n);
  return f(n, ans + 1);
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  ll n10 = 0;
  rep(i, n) if (x[i] == '1') n10 += pow(2, n - 1 - i);

  for (int i = 0; i < n; i++) {
    int temp = n10;
    if (x[i] == '0')
      temp += pow(2, n - 1 - i);
    else
      temp -= pow(2, n - 1 - i);
    cout << f(temp, 0) << endl;
  }
}