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

#include <bits/stdc++.h>
using namespace std;
int64_t ni(int64_t x) {
  int64_t y;
  y = 0;
  while (x != 0) {
    if (x % 2 == 1) {
      y++;
    }
    x = x / 2;
  }
  return y;
}
int64_t san(int64_t z) {
  int64_t v;
  v = 0;
  while (z != 0) {
    z = z % ni(z);
    v++;
  }
  return v;
}

int main() {
  int64_t n, a, ans;
  a = 0;
  cin >> n;
  vector<char> kari(n);
  vector<int64_t> vec(n);
  for (int64_t i = 0; i < n; i++) {
    cin >> kari.at(i);
  }
  for (int64_t i = 0; i < n; i++) {
    if (kari.at(i) == '0') {
      vec.at(i) = 0;
    }
    if (kari.at(i) == '1') {
      vec.at(i) = 1;
      a++;
    }
  }
  int64_t b = a - 1;
  int64_t c = a + 1;
  vector<int64_t> rb(n);
  vector<int64_t> rc(n);
  rb.at(n - 1) = 1;
  rc.at(n - 1) = 1;
  for (int64_t i = 0; i < n - 1; i++) {
    if (b == 0) {
      break;
    }
    rb.at(n - 2 - i) = (rb.at(n - 1 - i) * 2) % b;
  }
  for (int64_t i = 0; i < n - 1; i++) {
    rc.at(n - 2 - i) = (rc.at(n - 1 - i) * 2) % c;
  }
  int64_t ab, ac;
  ab = 0;
  ac = 0;
  for (int64_t k = 0; k < n; k++) {
    if (b == 0) {
      break;
    }
    if (vec.at(k) == 1) {
      ab = (ab + rb.at(k)) % b;
    }
    ab += b;
  }
  for (int64_t k = 0; k < n; k++) {
    if (vec.at(k) == 1) {
      ac = (ac + rc.at(k)) % c;
    }
  }

  for (int64_t i = 0; i < n; i++) {
    vec.at(i) = 1 - vec.at(i);
    ans = 0;
    if (vec.at(i) == 0) {
      if (b != 0) {
        ans = (ab - rb.at(i)) % b;
        cout << san(ans) + 1 << endl;
      }
      if (b == 0) {
        cout << 0 << endl;
      }
    }
    if (vec.at(i) == 1) {
      ans = (ac + rc.at(i)) % c;
      cout << san(ans) + 1 << endl;
    }
    vec.at(i) = 1 - vec.at(i);
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int64_t ni(int64_t x) {
  int64_t y;
  y = 0;
  while (x != 0) {
    if (x % 2 == 1) {
      y++;
    }
    x = x / 2;
  }
  return y;
}
int64_t san(int64_t z) {
  int64_t v;
  v = 0;
  while (z != 0) {
    z = z % ni(z);
    v++;
  }
  return v;
}

int main() {
  int64_t n, a, ans;
  a = 0;
  cin >> n;
  vector<char> kari(n);
  vector<int64_t> vec(n);
  for (int64_t i = 0; i < n; i++) {
    cin >> kari.at(i);
  }
  for (int64_t i = 0; i < n; i++) {
    if (kari.at(i) == '0') {
      vec.at(i) = 0;
    }
    if (kari.at(i) == '1') {
      vec.at(i) = 1;
      a++;
    }
  }
  int64_t b = a - 1;
  int64_t c = a + 1;
  vector<int64_t> rb(n);
  vector<int64_t> rc(n);
  rb.at(n - 1) = 1;
  rc.at(n - 1) = 1;
  for (int64_t i = 0; i < n - 1; i++) {
    rb.at(n - 2 - i) = (rb.at(n - 1 - i) * 2) % b;
  }
  for (int64_t i = 0; i < n - 1; i++) {
    rc.at(n - 2 - i) = (rc.at(n - 1 - i) * 2) % c;
  }
  for (int64_t i = 0; i < n; i++) {
    vec.at(i) = 1 - vec.at(i);
    ans = 0;
    if (vec.at(i) == 0) {
      for (int64_t k = 0; k < n; k++) {
        if (vec.at(k) == 1) {
          ans = (ans + rb.at(k)) % b;
        }
      }
      cout << san(ans) + 1 << endl;
    }
    if (vec.at(i) == 1) {
      for (int64_t k = 0; k < n; k++) {
        if (vec.at(k) == 1) {
          ans = (ans + rc.at(k)) % c;
        }
      }
      cout << san(ans) + 1 << endl;
    }
    vec.at(i) = 1 - vec.at(i);
  }
}