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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int popcount(int n) {
  int ans = 0;
  while (n != 0) {
    ans += n % 2;
    n = n / 2;
  }
  return ans;
}

int times(int n) {
  int ans = 0;
  while (n != 0) {
    n = n % popcount(n);
    ans++;
  }
  return ans;
}

int calc(int n, string s) {
  int up = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1')
      up++;
  }
  if (up == 0)
    return 0;
  int keta = 1;
  int m = 0;
  for (int i = 0; i < n; i++) {
    m = (m + keta * (s.at(n - 1 - i) - '0')) % up;
    keta = (keta * 2) % up;
  }
  return times(m % up) + 1;
}

int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  int up = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1')
      up++;
  }
  if (up == 1) {
    if (s.at(n - 1) == '0') {
      for (int i = 0; i < n - 1; i++) {
        if (s.at(i) == '0') {
          cout << 1 << endl;
        } else
          cout << 0 << endl;
      }
      cout << 2 << endl;
      return 0;
    } else {
      for (int i = 0; i < n - 1; i++) {
        cout << 2 << endl;
      }
      cout << 0 << endl;
      return 0;
    }
  }
  vector<int> keta_mod_up(n, 1);
  vector<int> keta_mod_down(n, 1);
  int keta = 1;
  for (int i = 0; i < n - 1; i++) {
    keta_mod_up.at(i + 1) = (keta * 2) % (up + 1);
    keta = (keta * 2) % (up + 1);
  }
  keta = 1;
  for (int i = 0; i < n - 1; i++) {
    keta_mod_down.at(i + 1) = (keta * 2) % (up - 1);
    keta = (keta * 2) % (up - 1);
  }
  int stay_up = 0;
  int stay_down = 0;
  for (int i = 0; i < n; i++) {
    stay_up =
        (stay_up + keta_mod_up.at(i) * (s.at(n - 1 - i) - '0')) % (up + 1);
  }
  for (int i = 0; i < n; i++) {
    stay_down =
        (stay_down + keta_mod_down.at(i) * (s.at(n - 1 - i) - '0')) % (up - 1);
  }
  for (int i = 0; i < n; i++) {
    bool z_to_o;
    if (s.at(i) == '0') {
      z_to_o = true;
    } else {
      z_to_o = false;
    }
    if (z_to_o) {
      int k;
      k = (stay_up + keta_mod_up.at(n - 1 - i)) % (up + 1);
      if (k % (up + 1) == 0)
        cout << 1 << endl;
      else {
        cout << times(k) + 1 << endl;
      }
    } else {
      int k;
      k = (stay_down - keta_mod_down.at(n - 1 - i)) % (up - 1);
      while (k < 0) {
        k += (up - 1);
      }
      if (k % (up - 1) == 0)
        cout << 1 << endl;
      else {
        cout << times(k) + 1 << endl;
      }
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int popcount(int n) {
  int ans = 0;
  while (n != 0) {
    ans += n % 2;
    n = n / 2;
  }
  return ans;
}

int times(int n) {
  int ans = 0;
  while (n != 0) {
    n = n % popcount(n);
    ans++;
  }
  return ans;
}

int calc(int n, string s) {
  int up = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1')
      up++;
  }
  if (up == 0)
    return 0;
  int keta = 1;
  int m = 0;
  for (int i = 0; i < n; i++) {
    m = (m + keta * (s.at(n - 1 - i) - '0')) % up;
    keta = (keta * 2) % up;
  }
  return times(m % up) + 1;
}

int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  int up = 0;
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1')
      up++;
  }
  vector<int> keta_mod_up(n, 1);
  vector<int> keta_mod_down(n, 1);
  int keta = 1;
  for (int i = 0; i < n - 1; i++) {
    keta_mod_up.at(i + 1) = (keta * 2) % (up + 1);
    keta = (keta * 2) % (up + 1);
  }
  keta = 1;
  for (int i = 0; i < n - 1; i++) {
    keta_mod_down.at(i + 1) = (keta * 2) % (up - 1);
    keta = (keta * 2) % (up - 1);
  }
  int stay_up = 0;
  int stay_down = 0;
  for (int i = 0; i < n; i++) {
    stay_up =
        (stay_up + keta_mod_up.at(i) * (s.at(n - 1 - i) - '0')) % (up + 1);
  }
  for (int i = 0; i < n; i++) {
    stay_down =
        (stay_down + keta_mod_down.at(i) * (s.at(n - 1 - i) - '0')) % (up - 1);
  }
  for (int i = 0; i < n; i++) {
    bool z_to_o;
    if (s.at(i) == '0') {
      z_to_o = true;
    } else {
      z_to_o = false;
    }
    if (z_to_o) {
      int k;
      k = (stay_up + keta_mod_up.at(n - 1 - i)) % (up + 1);
      if (k % (up + 1) == 0)
        cout << 1 << endl;
      else {
        cout << times(k) + 1 << endl;
      }
    } else {
      int k;
      k = (stay_down - keta_mod_down.at(n - 1 - i)) % (up - 1);
      while (k < 0) {
        k += (up - 1);
      }
      if (k % (up - 1) == 0)
        cout << 1 << endl;
      else {
        cout << times(k) + 1 << endl;
      }
    }
  }
}