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

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int f(int n) {
  if (n <= 0)
    return 0;
  int deno = 0;
  for (int nbuf = n; nbuf > 0; nbuf >>= 1) {
    if (nbuf & 1)
      deno++;
  }
  return f(n % deno) + 1;
}

int main(void) {
  int N;
  string strX;
  cin >> N;
  cin >> strX;
  vector<int> res(N, 1);
  vector<int> curNum(N, 0);
  int defPop = 0;
  for (char C : strX) {
    if (C == '1')
      defPop++;
  }
  vector<int> Power2ModPlus;
  vector<int> Power2ModMinus;
  int Power2ModPlusBuf = 1;
  int Power2ModMinusBuf = 1;
  for (int i = 0; i < N; i++) {
    Power2ModPlus.push_back(Power2ModPlusBuf);
    Power2ModPlusBuf = Power2ModPlusBuf * 2 % (defPop + 1);
    if (defPop > 1) {
      Power2ModMinus.push_back(Power2ModMinusBuf);
      Power2ModMinusBuf = Power2ModMinusBuf * 2 % (defPop - 1);
    }
  }
  int fPlusBuf = 0;
  int fMinusBuf = 0;
  for (int i = 0; i < N; i++) {
    if (strX[N - i - 1] == '1') {
      fPlusBuf += Power2ModPlus[i];
      fPlusBuf %= (defPop + 1);
      if (defPop > 1) {
        fMinusBuf += Power2ModMinus[i];
        fMinusBuf %= (defPop - 1);
      }
    }
  }
  for (int i = 0; i < N; i++) {
    if (strX[N - i - 1] == '1') {
      if (defPop > 1) {
        curNum[i] = fMinusBuf + (defPop - 1) - Power2ModMinus[i];
        curNum[i] %= (defPop - 1);
      } else
        curNum[i] = -1;
    } else {
      curNum[i] = fPlusBuf + Power2ModPlus[i];
      curNum[i] %= (defPop + 1);
    }
  }
  for (int i = 0; i < N; i++) {
    if (curNum[i] >= 0) {
      res[i] += f(curNum[i]);
    } else
      res[i] = 0;
  }
  for (int i = N - 1; i >= 0; i--) {
    cout << res[i] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int f(int n) {
  if (n == 0)
    return 0;
  int deno = 0;
  for (int nbuf = n; nbuf > 0; nbuf >>= 1) {
    if (nbuf & 1 == 1)
      deno++;
  }
  return f(n % deno) + 1;
}

int main(void) {
  int N;
  string strX;
  cin >> N;
  cin >> strX;
  vector<int> res(N, 1);
  vector<int> curNum(N, 0);
  int defPop = 0;
  for (char C : strX) {
    if (C == '1')
      defPop++;
  }
  vector<int> Power2ModPlus;
  vector<int> Power2ModMinus;
  int Power2ModPlusBuf = 1;
  int Power2ModMinusBuf = 1;
  for (int i = 0; i < N; i++) {
    Power2ModPlus.push_back(Power2ModPlusBuf);
    Power2ModPlusBuf = Power2ModPlusBuf * 2 % (defPop + 1);
    Power2ModMinus.push_back(Power2ModMinusBuf);
    Power2ModMinusBuf = Power2ModMinusBuf * 2 % (defPop - 1);
  }
  int fPlusBuf = 0;
  int fMinusBuf = 0;
  for (int i = 0; i < N; i++) {
    if (strX[N - i - 1] == '1') {
      fPlusBuf += Power2ModPlus[i];
      fPlusBuf %= (defPop + 1);
      fMinusBuf += Power2ModMinus[i];
      fMinusBuf %= (defPop - 1);
    }
  }
  for (int i = 0; i < N; i++) {
    if (strX[N - i - 1] == '1') {
      curNum[i] = fMinusBuf + (defPop - 1) - Power2ModMinus[i];
      curNum[i] %= defPop - 1;
    } else {
      curNum[i] = fPlusBuf + Power2ModPlus[i];
      curNum[i] %= defPop + 1;
    }
  }
  for (int i = 0; i < N; i++) {
    res[i] += f(curNum[i]);
  }
  for (int i = N - 1; i >= 0; i--) {
    cout << res[i] << endl;
  }
  return 0;
}