Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;

int pcount(int x) { return __builtin_popcount(x); }

int f(int x) {
  int ans = 1;
  int tmp;
  if (x == 0) {
    return ans;
  }
  while (x) {
    tmp = pcount(x);
    x %= tmp;
    ans++;
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int p1 = 0;
  int p2, p3;
  vector<int> amari2(n);
  vector<int> amari3(n);
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      p1++;
    }
  }
  p2 = p1 + 1;
  p3 = p1 - 1;
  for (int i = 0; i < n; i++) {
    if (i == 0) {
      amari2.at(i) = 1 % p2;
      amari3.at(i) = 1 % p3;
    } else {
      amari2.at(i) = amari2.at(i - 1) * 2 % p2;
      amari3.at(i) = amari3.at(i - 1) * 2 % p3;
    }
  }
  int x02 = 0;
  int x03 = 0;
  if (p2 != 0 && p3 != 0) {
    for (int i = 0; i < n; i++) {
      if (s.at(i) == '1') {
        x02 += amari2.at(i);
        x02 %= p2;
        x03 += amari3.at(i);
        x03 %= p3;
      }
    }
  } else if (p2 == 0 && p3 != 0) {
    for (int i = 0; i < n; i++) {
      if (s.at(i) == '1') {
        x03 += amari3.at(i);
        x03 %= p3;
      }
    }
    x02 = 0;
  } else if (p3 == 0 && p2 != 0) {
    for (int i = 0; i < n; i++) {
      if (s.at(i) == '1') {
        x02 += amari2.at(i);
        x02 %= p2;
      }
    }
    x03 = 0;
  }
  for (int i = n - 1; i >= 0; i--) {
    int x = 0;
    if (s.at(i) == '1') {
      bool zero = true;
      for (int j = 0; j < n; j++) {
        if (j == i)
          continue;
        if (s.at(j) == '1') {
          zero = false;
          break;
        }
      }
      if (zero) {
        cout << 0 << endl;
        continue;
      }
      x += x03;
      x -= amari3.at(i);
      if (x < 0) {
        x += p3;
      }
      x %= p3;
      if (x == 0) {
        cout << 1 << endl;
      } else {
        int anss = f(x);
        cout << anss << endl;
      }
    } else if (s.at(i) == '0') {
      x += x02;
      x += amari2.at(i);
      x %= p2;
      if (x == 0) {
        cout << 1 << endl;
      } else {
        int anss = f(x);
        cout << anss << endl;
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;

int pcount(int x) { return __builtin_popcount(x); }

int f(int x) {
  int ans = 1;
  int tmp;
  if (x == 0) {
    return ans;
  }
  while (x) {
    tmp = pcount(x);
    x %= tmp;
    ans++;
  }
  return ans;
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int p1 = 0;
  int p2, p3;
  vector<int> amari2(n);
  vector<int> amari3(n);
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      p1++;
    }
  }
  p2 = p1 + 1;
  p3 = p1 - 1;
  if (p3 != 0) {
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        amari2.at(i) = 1 % p2;
        amari3.at(i) = 1 % p3;
      } else {
        amari2.at(i) = amari2.at(i - 1) * 2 % p2;
        amari3.at(i) = amari3.at(i - 1) * 2 % p3;
      }
    }
  } else {
    for (int i = 0; i < n; i++) {
      if (i == 0) {
        amari2.at(i) = 1 % p2;
        amari3.at(i) = 0;
      } else {
        amari2.at(i) = amari2.at(i - 1) * 2 % p2;
        amari3.at(i) = 0;
      }
    }
  }
  int x02 = 0;
  int x03 = 0;
  if (p2 != 0 && p3 != 0) {
    for (int i = 0; i < n; i++) {
      if (s.at(i) == '1') {
        x02 += amari2.at(i);
        x02 %= p2;
        x03 += amari3.at(i);
        x03 %= p3;
      }
    }
  } else if (p2 == 0 && p3 != 0) {
    for (int i = 0; i < n; i++) {
      if (s.at(i) == '1') {
        x03 += amari3.at(i);
        x03 %= p3;
      }
    }
    x02 = 0;
  } else if (p3 == 0 && p2 != 0) {
    for (int i = 0; i < n; i++) {
      if (s.at(i) == '1') {
        x02 += amari2.at(i);
        x02 %= p2;
      }
    }
    x03 = 0;
  }
  for (int i = n - 1; i >= 0; i--) {
    int x = 0;
    if (s.at(i) == '1') {
      bool zero = true;
      for (int j = 0; j < n; j++) {
        if (j == i)
          continue;
        if (s.at(j) == '1') {
          zero = false;
          break;
        }
      }
      if (zero) {
        cout << 0 << endl;
        continue;
      }
      x += x03;
      x -= amari3.at(i);
      if (x < 0) {
        x += p3;
      }
      x %= p3;
      if (x == 0) {
        cout << 1 << endl;
      } else {
        int anss = f(x);
        cout << anss << endl;
      }
    } else if (s.at(i) == '0') {
      x += x02;
      x += amari2.at(i);
      x %= p2;
      if (x == 0) {
        cout << 1 << endl;
      } else {
        int anss = f(x);
        cout << anss << endl;
      }
    }
  }
}