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

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll from;
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RES(a, n) (a).resize(n)
#define VLCIN(a, n)                                                            \\
  REP(i, n) { cin >> a[i]; }
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 2.0 * asin(1.0);
const ll INF = 1L << 60;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll MAX = (ll)pow(10, 7);
/****** define constant end ********/
/****** define variable start ******/
ll n;
string x;
/******* define variable end *******/
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
int solve() {
  cin >> n >> x;
  ll oc = 0;
  vl dp(n + 1);
  dp[0] = 0;
  for (ll i = 1; i <= n; i++) {
    ll octmp = 0;
    ll tmp = i;
    while (tmp > 0) {
      octmp += tmp % 2;
      tmp /= 2;
    }
    if (octmp == 0) {
      dp[i] = 0;
      continue;
    }
    dp[i] = dp[i % octmp] + 1;
  }
  REP(i, n) {
    if (x.substr(i, 1) == "1") {
      oc++;
    }
  }
  vll po(n, vl(3));
  REP(i, n) {
    REP(j, 3) {
      if (oc + j % 3 - 1 == 0) {
        po[i][j] = 0;
        continue;
      }
      po[i][j] = modpow(2, i, oc + j % 3 - 1);
    }
  }
  vl number(3);
  REP(i, n) {
    REP(j, 3) {
      if (x.substr(i, 1) == "1") {
        number[j] += po[n - 1 - i][j];
      }
    }
  }
  for (ll i = 0; i <= n - 1; i++) {
    if (x.substr(i, 1) == "1") {
      if (oc - 1 == 0) {
        cout << dp[0] << endl;
        continue;
      }
      cout << dp[(oc - 1 + number[0] - modpow(2, n - 1 - i, oc - 1)) %
                 (oc - 1)] +
                  1
           << endl;
    } else {
      cout << dp[(number[2] + modpow(2, n - 1 - i, oc + 1)) % (oc + 1)] + 1
           << endl;
    }
  }
  return 0;
}
int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
using pll = pair<ll, ll>;
using vl = vector<ll>;
using vll = vector<vl>;
using vpll = vector<pll>;
struct Edge {
  ll from;
  ll to;
  ll cost;
};
using Graph = vector<vector<Edge>>;

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define RES(a, n) (a).resize(n)
#define VLCIN(a, n)                                                            \\
  REP(i, n) { cin >> a[i]; }
/***** define constant start *******/
const ll MOD = 1000000007;
const double PI = 2.0 * asin(1.0);
const ll INF = 1L << 60;
string abc = "abcdefghijklmnopqrstuvwxyz";
string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const ll MAX = (ll)pow(10, 7);
/****** define constant end ********/
/****** define variable start ******/
ll n;
string x;
/******* define variable end *******/
long long modpow(long long a, long long n, long long mod) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
int solve() {
  cin >> n >> x;
  ll oc = 0;
  vl dp(n + 1);
  dp[0] = 0;
  for (ll i = 1; i <= n; i++) {
    ll octmp = 0;
    ll tmp = i;
    while (tmp > 0) {
      octmp += tmp % 2;
      tmp /= 2;
    }
    if (octmp == 0) {
      dp[i] = 0;
      continue;
    }
    dp[i] = dp[i % octmp] + 1;
  }
  REP(i, n) {
    if (x.substr(i, 1) == "1") {
      oc++;
    }
  }
  vll po(n, vl(3));
  REP(i, n) {
    REP(j, 3) {
      if (j % 3 - 1 == 0) {
        po[i][j] = 0;
        continue;
      }
      po[i][j] = modpow(2, i, oc + j % 3 - 1);
    }
  }
  vl number(3);
  REP(i, n) {
    REP(j, 3) {
      if (x.substr(i, 1) == "1") {
        number[j] += po[n - 1 - i][j];
      }
    }
  }
  for (ll i = 0; i <= n - 1; i++) {
    if (x.substr(i, 1) == "1") {
      cout << dp[(oc - 1 + number[0] - modpow(2, n - 1 - i, oc - 1)) %
                 (oc - 1)] +
                  1
           << endl;
    } else {
      cout << dp[(number[2] + modpow(2, n - 1 - i, oc + 1)) % (oc + 1)] + 1
           << endl;
    }
  }
  return 0;
}
int main() {
  // clock_t start = clock();
  cout << std::fixed << std::setprecision(10);
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int end_solve = solve();
  /*
  clock_t end = clock();
  const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC *
  1000.0; printf("time %lf[ms]\\n", time);
  */

  return end_solve;
}