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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define VREP(it, v)                                                            \\
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
typedef pair<ll, ll> pi;
typedef vector<ll> vi;

template <typename T>
using pbds_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename K, typename V>
using pbds_map =
    tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
ll n, x;
char arr[200005];
ll ans[200005];
ll dp[200005];

ll power(ll a, ll b, ll mod) {
  a %= mod;
  if (b == 0)
    return (ll)1;
  if (b == 1)
    return a;
  ll half = power(a, b / 2, mod);
  half *= half;
  half %= mod;
  if (b % 2)
    half *= a;
  half %= mod;
  return half;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  REP(i, 0, n) cin >> arr[i];
  ll tot = 0;
  REP(i, 0, n) {
    if (arr[i] == '1')
      tot++;
  }

  dp[0] = 0;
  REP(i, 1, 200001) {
    ll x = i;
    ll ans = 0;
    while (x != 0) {
      if (x % 2 == 1)
        ans++;
      x /= 2;
    }
    dp[i] = 1 + dp[i % ans];
  }
  ll atotal = 0, btotal = 0, cur = 1, cur2 = 1;
  for (ll i = n - 1; i >= 0; i--) {
    atotal += cur * (arr[i] == '1');
    atotal %= (tot + 1);
    cur *= 2;
    cur %= (tot + 1);
    if (tot > 1) {
      btotal += cur2 * (arr[i] == '1');
      btotal %= (tot - 1);
      cur2 *= 2;
      cur2 %= (tot - 1);
    }
  }

  REP(i, 0, n) {
    if (tot == 1 && arr[i] == '1') {
      ans[i] = 0;
      continue;
    }

    if (arr[i] == '1') {
      ans[i] =
          1 +
          dp[(btotal - power((ll)2, n - 1 - i, tot - 1) + tot - 1) % (tot - 1)];
    } else {
      ans[i] =
          1 +
          dp[(atotal + power((ll)2, n - 1 - i, tot + 1) + tot + 1) % (tot + 1)];
    }
  }

  REP(i, 0, n) cout << ans[i] << endl;
}

// cout.flush()

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define REP(i, a, b) for (ll i = a; i < b; i++)
#define VREP(it, v)                                                            \\
  for (vector<int>::iterator it = v.begin(); it != v.end(); it++)
typedef pair<ll, ll> pi;
typedef vector<ll> vi;

template <typename T>
using pbds_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

template <typename K, typename V>
using pbds_map =
    tree<K, V, less<K>, rb_tree_tag, tree_order_statistics_node_update>;
ll n, x;
char arr[200005];
ll ans[200005];
ll dp[200005];

ll power(ll a, ll b, ll mod) {
  a %= mod;
  if (b == 0)
    return (ll)1;
  if (b == 1)
    return a;
  ll half = power(a, b / 2, mod);
  half *= half;
  half %= mod;
  if (b % 2)
    half *= a;
  half %= mod;
  return half;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  REP(i, 0, n) cin >> arr[i];
  ll tot = 0;
  REP(i, 0, n) {
    if (arr[i] == '1')
      tot++;
  }

  dp[0] = 0;
  REP(i, 1, 200001) {
    ll x = i;
    ll ans = 0;
    while (x != 0) {
      if (x % 2 == 1)
        ans++;
      x /= 2;
    }
    dp[i] = 1 + dp[i % ans];
  }
  ll atotal = 0, btotal = 0, cur = 1;
  for (ll i = n - 1; i >= 0; i--) {
    atotal += cur * (arr[i] == '1');
    atotal %= (tot + 1);
    if (tot > 1) {
      btotal += cur * (arr[i] == '1');
      btotal %= (tot - 1);
    }
    cur *= 2;
  }

  REP(i, 0, n) {
    if (tot == 1 && arr[i] == '1') {
      ans[i] = 0;
      continue;
    }

    if (arr[i] == '1') {
      ans[i] =
          1 +
          dp[(btotal - power((ll)2, n - 1 - i, tot - 1) + tot - 1) % (tot - 1)];
    } else {
      ans[i] =
          1 +
          dp[(atotal + power((ll)2, n - 1 - i, tot + 1) + tot + 1) % (tot + 1)];
    }
  }

  REP(i, 0, n) cout << ans[i] << endl;
}

// cout.flush()