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

#include <bits/stdc++.h>
using namespace std;

#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define REP(i, a, n) for (ll i = a; i < n; i++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define show(x) cout << (x) << endl
#define UM unordered_map
#define PB push_back
#define V vector
#define EPS 1e-8
typedef unsigned long ul;
typedef long long ll;
typedef string str;
/*FIN*/

ll power(ll x, ll n, ll m) {
  if (x == 0)
    return 0;
  if (n == 0)
    return 1;
  ll y;
  if (n & 1) {
    y = x % m;
    y = (y * power(x, n - 1, m) % m) % m;
  } else {
    y = power(x, n >> 1, m);
    y = (y * y) % m;
  }
  return ((y + m) % m);
}

int main() {
  FAST;
  ll n;
  str x;
  cin >> n;
  cin >> x;
  ll b = 0;
  REP(i, 0, n) b += (x[i] == '1');
  if (b > 1) {
    ll num1 = 0, num2 = 0;
    reverse(ALL(x));
    REP(i, 0, n) {
      if (x[i] == '1') {
        num1 = (num1 + power(2, i, b + 1)) % (b + 1);
        num2 = (num2 + power(2, i, b - 1)) % (b - 1);
      }
    }
    ll dp[n + 1];
    dp[0] = 0;
    dp[1] = 1;
    REP(i, 2, n + 1) {
      ll j = __builtin_popcountll(i);
      dp[i] = 1 + dp[i % j];
    }
    REP(i, 0, n) {
      ll m = n - i - 1;
      if (x[m] == '0')
        show(1 + dp[(num1 + power(2, m, b + 1)) % (b + 1)]);
      else
        show(1 + dp[(num2 - power(2, m, b - 1) + b - 1) % (b - 1)]);
    }
  } else if (b == 1) {
    ll num1 = (x[n - 1] == '1');
    REP(i, 0, n - 1) {
      if (x[i] == '1')
        show(0);
      else
        show(1 + num1);
    }
    show(x[n - 1] == '1' ? 0 : 2);
  } else {
    REP(i, 0, n) show(1);
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define FAST                                                                   \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define REP(i, a, n) for (int i = a; i < n; i++)
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((int)(x).size())
#define show(x) cout << (x) << endl
#define UM unordered_map
#define PB push_back
#define V vector
#define EPS 1e-8
typedef unsigned long ul;
typedef long long ll;
typedef string str;
/*FIN*/

int power(int x, int y, int p) {
  if (x == 0)
    return 0;
  if (y == 0)
    return 1;
  int z;
  if (y % 2 == 0) {
    z = power(x, y >> 1, p);
    z = (z * z) % p;
  } else {
    z = x % p;
    z = (z * power(x, y - 1, p)) % p;
  }
  return (int)((z + p) % p);
}

int main() {
  FAST;
  int n;
  str x;
  cin >> n;
  cin >> x;
  int b = 0;
  REP(i, 0, n) b += (x[i] == '1');
  if (b > 1) {
    int num1 = 0, num2 = 0;
    reverse(ALL(x));
    REP(i, 0, n) {
      if (x[i] == '1') {
        num1 = (num1 + power(2, i, b + 1)) % (b + 1);
        num2 = (num2 + power(2, i, b - 1)) % (b - 1);
      }
    }
    int dp[n];
    dp[0] = 0;
    dp[1] = 1;
    REP(i, 2, n + 1) {
      int j = __builtin_popcount(i);
      dp[i] = 1 + dp[i % j];
    }
    REP(i, 0, n) {
      if (x[n - i - 1] == '1') {
        show(1 + dp[(num2 - power(2, n - i - 1, b - 1) + (b - 1)) % (b - 1)]);
      } else {
        show(1 + dp[(num1 + power(2, n - i - 1, b + 1)) % (b + 1)]);
      }
    }
  } else {
    REP(i, 0, n) show(0);
  }
  return 0;
}