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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

int f(int x, vector<int> &memo) {
  if (memo.at(x) != -1)
    return memo.at(x);
  int b{};
  int y = x;
  while (y) {
    if (1 & y)
      b++;
    y >>= 1;
  }
  if (x % b == 0) {
    memo.at(x) = 1;
    return memo.at(x);
  } else {
    memo.at(x) = f(x % b, memo) + 1;
    return memo.at(x);
  }
}
int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;

  int m{};
  vector<int> memo(n + 1, -1);
  int a{}, b{};
  vector<int> c(n);
  vector<int> d(n);
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      m++;
    }
  }
  if (m == 1) {
    if (s.back() == '1') {
      for (int i = 0; i < n - 1; i++) {
        cout << 2 << endl;
      }
      cout << 0 << endl;
    } else {
      for (int i = 0; i < n - 1; i++) {
        if (s.at(i) == '1') {
          cout << 0 << endl;
        } else {
          cout << 1 << endl;
        }
      }
      cout << 2 << endl;
    }
    return 0;
  }
  if (m == 0) {
    for (int i = 0; i < n; i++) {
      cout << 1 << endl;
    }
    return 0;
  }
  c.at(0) = 1;
  d.at(0) = 1;
  for (int i = 1; i < n; i++) {
    c.at(i) = c.at(i - 1) * 2 % (m + 1);
  }
  for (int i = 1; i < n; i++) {
    d.at(i) = d.at(i - 1) * 2 % (m - 1);
  }

  reverse(s.begin(), s.end());
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      a += c.at(i);
      a %= (m + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      b += d.at(i);
      b %= (m - 1);
    }
  }
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      if (m > 1) {
        int e = (b - d.at(i) + m - 1) % (m - 1);
        if (e % (m - 1) == 0)
          ans.at(i) = 1;
        else
          ans.at(i) = f(e % (m - 1), memo) + 1;
      } else
        ans.at(i) = 0;
    } else {
      int e = (a + c.at(i)) % (m + 1);
      if (e % (m + 1) == 0)
        ans.at(i) = 1;
      else
        ans.at(i) = f(e % (m + 1), memo) + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  for (auto i : ans)
    cout << i << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 1e9 + 7;

int f(int x, vector<int> &memo) {
  if (memo.at(x) != -1)
    return memo.at(x);
  int b{};
  int y = x;
  while (y) {
    if (1 & y)
      b++;
    y >>= 1;
  }
  if (x % b == 0) {
    memo.at(x) = 1;
    return memo.at(x);
  } else {
    memo.at(x) = f(x % b, memo) + 1;
    return memo.at(x);
  }
}
int main() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());

  int m{};
  vector<int> memo(n + 1, -1);
  int a{}, b{};
  vector<int> c(n);
  vector<int> d(n);
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      m++;
    }
  }
  c.at(0) = 1;
  d.at(0) = 1;
  for (int i = 1; i < n; i++) {
    c.at(i) = c.at(i - 1) * 2 % (m + 1);
  }
  for (int i = 1; i < n; i++) {
    d.at(i) = d.at(i - 1) * 2 % (m - 1);
  }

  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      a += c.at(i);
      a %= (m + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      b += d.at(i);
      b %= (m - 1);
    }
  }
  vector<int> ans(n);
  for (int i = 0; i < n; i++) {
    if (s.at(i) == '1') {
      if (m > 1) {
        int e = (b - d.at(i) + m - 1) % (m - 1);
        if (e % (m - 1) == 0)
          ans.at(i) = 1;
        else
          ans.at(i) = f(e % (m - 1), memo) + 1;
      } else
        ans.at(i) = 0;
    } else {
      int e = (a + c.at(i)) % (m + 1);
      if (e % (m + 1) == 0)
        ans.at(i) = 1;
      else
        ans.at(i) = f(e % (m + 1), memo) + 1;
    }
  }
  reverse(ans.begin(), ans.end());
  for (auto i : ans)
    cout << i << endl;
}