Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define deb(x) cout << #x << "=" << x << endl;
#define endl '\n'
// #define M 1000000007
#define int long long
#define INF 1e18
#define max_n 1000000

using namespace std;

ll p(ll n, ll k, ll M) {
  if (k == 0)
    return 1;
  ll x = p(n, k / 2, M) % M;
  if (k % 2 == 0)
    return ((x % M) * (x % M)) % M;
  else
    return ((((x % M) * (x % M)) % M) * n) % M;
}

void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll ones = 0;
  ll pre[n + 1];
  for (int i = 0; i <= n; ++i) {
    if (i == 0)
      pre[i] = 0;
    else
      pre[i] = pre[i % (__builtin_popcount(i))] + 1LL;
  }

  for (int i = 0; i < n; ++i) {
    ones += (s[i] - '0');
  }
  ll m1 = ones + 1;
  ll m2 = ones - 1;
  ll v1 = 0, v2 = 0;
  for (int i = 0; i < n; ++i) {
    if (s[n - i - 1] == '1') {
      v1 += p(2, i, m1);
      v2 += p(2, i, m2);
    }
    v1 %= m1, v2 %= m2;
  }
  // deb(v1)deb(v2)deb(ones)
  for (int i = 0; i < n; ++i) {
    ll temp = ones;
    if (s[i] == '1') {
      temp--;
      if (temp == 0) {
        cout << 0 << endl;
        continue;
      }
      ll val = (v2 - p(2, n - i - 1, m2) + m2) % m2;
      cout << pre[val] + 1 << endl;
    } else {
      temp++;
      ll val = (v1 + p(2, n - i - 1, m1) + m1) % m1;
      cout << pre[val] + 1 << endl;
    }
  }
}

int32_t main() {
  IOS ll T = 1;
  // cin>>T;
  for (ll i = 1; i <= T; ++i) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define IOS                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define deb(x) cout << #x << "=" << x << endl;
#define endl '\n'
// #define M 1000000007
#define int long long
#define INF 1e18
#define max_n 1000000

using namespace std;

ll p(ll n, ll k, ll M) {
  if (k == 0)
    return 1;
  ll x = p(n, k / 2, M) % M;
  if (k % 2 == 0)
    return ((x % M) * (x % M)) % M;
  else
    return ((((x % M) * (x % M)) % M) * n) % M;
}

void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll ones = 0;
  ll pre[n + 1];
  for (int i = 0; i <= n; ++i) {
    if (i == 0)
      pre[i] = 0;
    else
      pre[i] = pre[i % (__builtin_popcount(i))] + 1LL;
  }

  for (int i = 0; i < n; ++i) {
    ones += (s[i] - '0');
  }
  ll m1 = ones + 1;
  ll m2 = ones - 1;
  if (m2 <= 0)
    m2 = 1;
  ll v1 = 0, v2 = 0;
  for (int i = 0; i < n; ++i) {
    if (s[n - i - 1] == '1') {
      v1 += p(2, i, m1);
      v2 += p(2, i, m2);
    }
    v1 %= m1, v2 %= m2;
  }
  // deb(v1)deb(v2)deb(ones)
  for (int i = 0; i < n; ++i) {
    ll temp = ones;
    if (s[i] == '1') {
      temp--;
      if (temp == 0) {
        cout << 0 << endl;
        continue;
      }
      ll val = (v2 - p(2, n - i - 1, m2) + m2) % m2;
      cout << pre[val] + 1 << endl;
    } else {
      temp++;
      ll val = (v1 + p(2, n - i - 1, m1) + m1) % m1;
      cout << pre[val] + 1 << endl;
    }
  }
}

int32_t main() {
  IOS ll T = 1;
  // cin>>T;
  for (ll i = 1; i <= T; ++i) {
    // cout<<"Case #"<<i<<": ";
    solve();
  }
  return 0;
}