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

#ifdef __APPLE__
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <ctgmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

using namespace std;
#define io ios_base::sync_with_stdio(false), cin.tie(NULL)
#define GFOR(i, a, n) for (i = a; i < n; i++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define RFOR(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define tc(t) for (int tc = 1; tc <= t; tc++)
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<long long int> vll;
typedef vector<pair<int, int>> vpi;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b, ll cmod) {
  ll res = 1;
  a %= cmod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % cmod;
    a = a * a % cmod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
// head

int get_ans(ll val) {
  int cnt = 0;
  while (val > 0)
    val = val % ((ll)__builtin_popcount(val)), cnt++;
  return cnt;
}

void solve() {
  int n;
  cin >> n;
  string b;
  cin >> b;

  int ones = 0;
  for (auto x : b)
    ones += (x == '1');

  vi ans(n);
  FOR(d, -1, 2) {
    int cnt = ones + d;
    ll val = 0, mul = 1;
    if (cnt == 0)
      val = 0;
    else {
      RFOR(i, 0, n)
      val += mul * (b[i] == '1'), val %= (ll)cnt, mul *= 2, mul %= (ll)cnt;
    }

    FOR(j, 0, n) {
      int ccnt = ones;
      if (b[j] == '1')
        ccnt--;
      else
        ccnt++;
      if (ccnt == cnt) {
        if (ccnt == 0)
          ans[j] = 0;
        else {
          ll cval = val;

          if (b[j] == '1')
            cval -= powmod(2, n - j - 1, cnt);
          else
            cval += powmod(2, n - j - 1, cnt);

          cval = (cval % cnt + cnt) % cnt;
          ans[j] = 1 + get_ans(cval);
        }
      }
    }
  }

  for (auto x : ans)
    cout << x << "\\n";
}

int main() {
#ifdef LOCAL
  auto t1 = std::chrono::high_resolution_clock::now();
#endif

  io;
  solve();

#ifdef LOCAL
  auto t2 = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();

  std::cout << "Time Taken: " << duration << "\\n";
#endif
}
// check for edge cases in limits
// printing double may print 1.223232e9 like values, beware of that


Error: 0

Fixed submission:

#ifdef __APPLE__
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cmath>
#include <ctgmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

using namespace std;
#define io ios_base::sync_with_stdio(false), cin.tie(NULL)
#define GFOR(i, a, n) for (i = a; i < n; i++)
#define FOR(i, a, n) for (int i = a; i < n; i++)
#define RFOR(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define sz(x) ((int)(x).size())
#define tc(t) for (int tc = 1; tc <= t; tc++)
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<long long int> vll;
typedef vector<pair<int, int>> vpi;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
mt19937 mrand(random_device{}());
const ll mod = 1000000007;
int rnd(int x) { return mrand() % x; }
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll modInverse(ll a) {
  ll p = mod - 2;
  return powmod(a, p);
}
// head

int get_ans(ll val) {
  int cnt = 0;
  while (val > 0)
    val %= ((ll)__builtin_popcount(val)), cnt++;
  return cnt;
}

void solve() {
  int n;
  cin >> n;
  string b;
  cin >> b;

  int ones = 0;
  for (auto x : b)
    ones += (x == '1');
  ll val = 0;
  ll mul = 1;
  RFOR(i, 0, n)
  val += mul * (b[i] == '1'), val %= mod, mul *= 2, mul %= mod;

  for (int i = 0; i < n; i++) {
    int cnt = ones;
    ll cval = val;
    if (b[i] == '1')
      cnt--, cval -= powmod(2, n - i - 1);
    else
      cnt++, cval += powmod(2, n - i - 1);
    cval = (cval % mod + mod) % mod;
    cout << 1 + get_ans(cval % (ll)cnt) << "\\n";
  }
}

int main() {
#ifdef LOCAL
  auto t1 = std::chrono::high_resolution_clock::now();
#endif

  io;
  int t = 1;
  tc(t) { solve(); }

#ifdef LOCAL
  auto t2 = std::chrono::high_resolution_clock::now();
  auto duration =
      std::chrono::duration_cast<std::chrono::milliseconds>(t2 - t1).count();

  std::cout << "Time Taken: " << duration << "\\n";
#endif
}
// check for edge cases in limits
// printing double may print 1.223232e9 like values, beware of that