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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int f(int nn) {
  int n = nn;
  int t = 0;
  while (n) {
    int pc = __builtin_popcount(n);
    n %= pc;
    t++;
  }
  return t;
}

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

  int pc = 0;
  for (auto c : s)
    if (c == '1')
      pc++;

  int pc0 = pc + 1;
  int pc1 = pc - 1;

  vector<int> pow2_pc1(n);
  int s1 = 0;
  if (pc >= 2) {
    int t1 = 1;
    rep(i, n) {
      int ci = n - 1 - i;
      pow2_pc1[ci] = t1 % pc1;
      t1 = t1 * 2 % pc1;
    }
    int t = 1;
    rrep(i, n) {
      if (s[i] == '1')
        s1 = (s1 + t) % pc1;
      t = t * 2 % pc1;
    }
  }

  vector<int> pow2_pc0(n);
  int s0 = 0;
  int t0 = 1;
  int t = 1;

  rep(i, n) {
    int ci = n - 1 - i;
    pow2_pc0[ci] = t0 % pc0;
    t0 = t0 * 2 % pc0;
  }

  rrep(i, n) {
    if (s[i] == '1')
      s0 = (s0 + t) % pc0;
    t = t * 2 % pc0;
  }

  rep(i, n) {
    int ci = n - 1 - i;
    if (s[i] == '1') {
      //            cout<<'u';
      int tmp = s1 - pow2_pc1[i];
      if (pc <= 1)
        cout << 0 << endl;
      else
        cout << f((tmp + pc1) % pc1) + 1 << endl;
    } else {
      int tmp = (s0 + pow2_pc0[i]) % pc0;

      //      cout<<'d';
      //      cout<<tmp<<' '<<s0<<' '<<pow2_pc0[i]<<'('<<i<<')';

      cout << f(tmp) + 1 << endl;
    }
  }

  return 0;

  cout << "---" << endl;
  cout << pc0 << ' ' << pc1 << ' ' << s1 << ' ' << s0 << endl;
  rep(i, n) cout << pow2_pc1[i] << ' ';
  cout << endl;
  rep(i, n) cout << pow2_pc0[i] << ' ';
  cout << endl;

  rep(i, n) cout << s[i];
  cout << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rrep1(i, n) for (int i = (n); i > 0; i--)

#define ll long long
#define pi pair<int, int>
#define pll pair<ll, ll>

#define MOD 1000000007
#define INF 1000000000000000LL

using namespace std;

int f(int nn) {
  int n = nn;
  int t = 0;
  while (n) {
    int pc = __builtin_popcount(n);
    n %= pc;
    t++;
  }
  return t;
}

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

  int pc = 0;
  for (auto c : s)
    if (c == '1')
      pc++;

  if (pc == 0) {
    rep(i, n) cout << 0 << endl;

    return 0;
  }

  int pc0 = pc + 1;
  int pc1 = pc - 1;

  vector<int> pow2_pc0(n);
  int t0 = 1;
  rep(i, n) {
    int ci = n - 1 - i;
    pow2_pc0[ci] = t0 % pc0;
    t0 = t0 * 2 % pc0;
  }

  vector<int> pow2_pc1(n);
  int t1 = 1;
  rep(i, n) {
    int ci = n - 1 - i;
    pow2_pc1[ci] = t1 % pc1;
    t1 = t1 * 2 % pc1;
  }

  int s0 = 0;
  int t = 1;
  rrep(i, n) {
    if (s[i] == '1')
      s0 = (s0 + t) % pc0;
    t = t * 2 % pc0;
  }

  int s1 = 0;
  t = 1;
  rrep(i, n) {
    if (s[i] == '1')
      s1 = (s1 + t) % pc1;
    t = t * 2 % pc1;
  }

  rep(i, n) {
    int ci = n - 1 - i;
    if (s[i] == '1') {
      //            cout<<'u';
      int tmp = s1 - pow2_pc1[i];
      if (pc <= 1)
        cout << 0 << endl;
      else
        cout << f((tmp + pc1) % pc1) + 1 << endl;
    } else {
      int tmp = (s0 + pow2_pc0[i]) % pc0;

      //      cout<<'d';
      //      cout<<tmp<<' '<<s0<<' '<<pow2_pc0[i]<<'('<<i<<')';

      cout << f(tmp) + 1 << endl;
    }
  }

  return 0;

  cout << "---" << endl;
  cout << pc0 << ' ' << pc1 << ' ' << s1 << ' ' << s0 << endl;
  rep(i, n) cout << pow2_pc1[i] << ' ';
  cout << endl;
  rep(i, n) cout << pow2_pc0[i] << ' ';
  cout << endl;

  rep(i, n) cout << s[i];
  cout << endl;

  return 0;
}