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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};

ll beki[2][200020];

ll f(ll n) {
  ll ret = 0;
  ll tmp = n;
  ll now = 0;
  while (tmp) {
    if (tmp % 2)
      now++;
    tmp /= 2;
  }
  if (now)
    ret = 1 + f(n % now);
  return ret;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      cnt++;
  // mod -> cnt+1, cnt-1
  ll l = 0, r = 0;
  beki[0][0] = 1;
  beki[1][0] = 1;
  for (int i = 0; i < n; i++) {
    l *= 2;
    r *= 2;
    beki[0][i + 1] = beki[0][i] * 2;
    if (cnt != 1)
      beki[1][i + 1] = beki[1][i] * 2;
    if (s[i] == '1') {
      l++;
      r++;
    }
    l %= (cnt + 1);
    beki[0][i + 1] %= (cnt + 1);
    if (cnt != 1) {
      r %= (cnt - 1);
      beki[1][i + 1] %= (cnt - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    ll now = 0;
    if (s[i] == '1') {
      // cnt-1
      ll ans = r - beki[1][n - 1 - i] + cnt - 1;
      if (cnt != 1)
        now++;
      if (cnt != 1)
        ans %= (cnt - 1);
      cout << now + f(ans) << "\\n";
    } else {
      ll ans = l + beki[0][n - 1 - i];
      now++;
      ans %= (cnt + 1);
      cout << now + f(ans) << "\\n";
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <typename T1, typename T2> bool chmin(T1 &a, T2 b) {
  if (a <= b)
    return 0;
  a = b;
  return 1;
}
template <typename T1, typename T2> bool chmax(T1 &a, T2 b) {
  if (a >= b)
    return 0;
  a = b;
  return 1;
}
int dx[4] = {0, 1, -1, 0};
int dy[4] = {1, 0, 0, -1};

ll beki[2][200020];

ll f(ll n) {
  ll ret = 0;
  ll tmp = n;
  ll now = 0;
  while (tmp) {
    if (tmp % 2)
      now++;
    tmp /= 2;
  }
  if (now)
    ret = 1 + f(n % now);
  return ret;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll cnt = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      cnt++;
  // mod -> cnt+1, cnt-1
  ll l = 0, r = 0;
  beki[0][0] = 1;
  beki[1][0] = 1;
  for (int i = 0; i < n; i++) {
    l *= 2;
    r *= 2;
    beki[0][i + 1] = beki[0][i] * 2;
    if (cnt != 1)
      beki[1][i + 1] = beki[1][i] * 2;
    if (s[i] == '1') {
      l++;
      r++;
    }
    l %= (cnt + 1);
    beki[0][i + 1] %= (cnt + 1);
    if (cnt != 1) {
      r %= (cnt - 1);
      beki[1][i + 1] %= (cnt - 1);
    }
  }
  for (int i = 0; i < n; i++) {
    ll now = 0;
    if (s[i] == '1') {
      // cnt-1
      ll ans = r - beki[1][n - 1 - i] + cnt - 1;
      if (cnt != 1)
        now++;
      ans %= (cnt - 1);
      cout << now + f(ans) << "\\n";
    } else {
      ll ans = l + beki[0][n - 1 - i];
      now++;
      ans %= (cnt + 1);
      cout << now + f(ans) << "\\n";
    }
  }
}