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

#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

int mod_pow(int n, int p, int MOD) {
  int r = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1LL)
      r = (r * n) % MOD;
    n = (n * n) % MOD;
  }
  return r; // r = n^p % MOD
}

int f(int x, int b) {
  if (x == 0)
    return 0LL;
  int cnt = 0;
  int base = 1;
  while (x != 0) {
    if (x % (base * b) == 0) {

    } else {
      x -= base;
      cnt++;
    }
    base *= b;
  }
  return cnt;
}

signed main() {

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

  reverse(s.begin(), s.end());
  int bit = 0;
  for (int i = 0; i < n; i++) {
    bit += s[i] == '1';
  }

  int r1 = 0, r2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      r1 = (r1 + mod_pow(2, i, (bit + 1))) % (bit + 1);
    if (s[i] == '1') {
      if (bit - 1 == 0)
        r2 = 0;
      else
        r2 = (r2 + mod_pow(2, i, (bit - 1))) % (bit - 1);
    }
  }

  auto rec = [&](auto &&rec, int res) -> int {
    int b = f(res, 2);
    if (b == 0)
      return 1LL;
    return rec(rec, res % b) + 1;
  };

  vector<int> start(n), ans(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      start[i] = (r1 + mod_pow(2, i, (bit + 1)) % (bit + 1)) % (bit + 1);
      ans[i] = rec(rec, start[i]);
    } else {
      if (bit - 1 == 0) {
        ans[i] = 0;
      } else {
        start[i] = (r2 - mod_pow(2, i, (bit - 1)) + bit - 1) % (bit - 1);
        ans[i] = rec(rec, start[i]);
      }
    }
    // ans[i] = rec(rec, start[i]);
  }
  reverse(ans.begin(), ans.end());
  for (int p : ans) {
    cout << p << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#ifdef _DEBUG
#include "_DEBUG.hpp"
#endif
#define int long long
const int INF = 1LL << 60;

int mod_pow(int n, int p, int MOD) {
  int r = 1;
  for (; p > 0; p >>= 1) {
    if (p & 1LL)
      r = (r * n) % MOD;
    n = (n * n) % MOD;
  }
  return r; // r = n^p % MOD
}

int convert_base(int x, int b) {
  int cnt = 0;
  if (x == 0)
    return 0LL;
  // string s;
  int base = 1;
  while (x != 0) {
    if (x % (base * b) == 0) {
      // s += '0';
    } else {
      // s += '1';
      x -= base;
      cnt++;
    }
    base *= b;
  }
  // reverse(s.begin(),s.end());
  return cnt;
}

signed main() {

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

  reverse(s.begin(), s.end());
  int bit = 0;
  for (int i = 0; i < n; i++) {
    bit += s[i] == '1';
  }

  int r1 = 0, r2 = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      r1 = (r1 + mod_pow(2, i, (bit + 1))) % (bit + 1);
    if (s[i] == '1')
      r2 = (r2 + mod_pow(2, i, (bit - 1))) % (bit - 1);
  }

  auto rec = [&](auto &&rec, int res) -> int {
    int b = convert_base(res, 2);
    if (b == 0 || res == 0)
      return 1LL;
    return rec(rec, res % b) + 1;
  };

  vector<int> start(n), ans(n);
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      start[i] = (r1 + mod_pow(2, i, (bit + 1)) % (bit + 1)) % (bit + 1);
    } else {
      start[i] = (r2 - mod_pow(2, i, (bit - 1)) + bit - 1) % (bit - 1);
    }
    ans[i] = rec(rec, start[i]);
  }
  reverse(ans.begin(), ans.end());
  for (int p : ans) {
    cout << p << endl;
  }

  return 0;
}