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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int popc(int n) {
  int res = 0;
  while (n != 0) {
    if (n % 2 == 1)
      res++;
    n /= 2;
  }
  return res;
}

void fc(string &x, int now, vector<int> &fi) {
  int ans = 0;
  int nx = fi.at(now);
  ans++;
  if (nx == -1)
    ans = 0;
  while (nx != 0 && nx != -1) {
    ans++;
    int pc = popc(nx);
    nx %= pc;
  }
  cout << ans << endl;
}

void make_in(string &x, vector<int> &fi) {
  vector<int> pm(x.size()), pp(x.size());
  int pc = 0;
  rep(i, x.size()) {
    if (x.at(i) == '1')
      pc++;
  }
  // cerr << pc << endl;
  int npp = 1, npm = 1;
  if (pc == 1)
    npm = -1;
  rep(i, x.size()) {
    pm.at(i) = npm;
    pp.at(i) = npp;
    if (pc > 1)
      npm = (npm * 2) % (pc - 1);
    npp = (npp * 2) % (pc + 1);
  }

  int xp = 0, xm = 0;
  rep(i, x.size()) {
    if (x.at(i) == '1') {
      xp += pp.at(i);
      xm += pm.at(i);
      xp %= (pc + 1);
      if (pc > 1)
        xm %= (pc - 1);
    }
  }
  rep(i, x.size()) {
    if (x.at(i) == '1') {
      if (pc <= 1)
        fi.at(i) = -1;
      else
        fi.at(i) = (xm - pm.at(i) + pc - 1) % (pc - 1);
    } else {
      fi.at(i) = (xp + pp.at(i) + pc + 1) % (pc + 1);
    }
  }
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  reverse(ALL(x));
  vector<int> fi(x.size());
  make_in(x, fi);
  // rep(i, x.size()){
  //   cerr << fi.at(i) << " ";
  // }
  // cerr << endl;
  rrep(i, x.size()) { fc(x, i, fi); }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri, x, n) for (int ri = (int)(n - 1); ri >= (int)(x); ri--)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int popc(int n) {
  int res = 0;
  while (n != 0) {
    if (n % 2 == 1)
      res++;
    n /= 2;
  }
  return res;
}

void fc(string &x, int now) {
  x.at(now) = '1' - x.at(now) + '0';
  int ans = 0, pc = 0;
  rep(i, x.size()) {
    if (x.at(i) == '1')
      pc++;
  }
  int nx = 0, pow = 1;
  rep(i, x.size()) {
    if (x.at(i) == '1') {
      nx += pow;
    }
    nx %= pc;
    pow *= 2;
    pow %= pc;
  }
  ans++;
  while (nx != 0) {
    ans++;
    pc = popc(nx);
    nx %= pc;
  }
  cout << ans << endl;

  x.at(now) = '1' - x.at(now) + '0';
}

int main() {
  int n;
  cin >> n;
  string x;
  cin >> x;
  reverse(ALL(x));
  rrep(i, x.size()) { fc(x, i); }
  return 0;
}