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

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using ppi = pair<pii, int>;

int solve(int t) {
  if (t == 0)
    return 0;
  int p = t;
  int cnt = 0;
  while (p) {
    if (p % 2)
      cnt++;
    p /= 2;
  }
  return 1 + solve(t % cnt);
}

int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  int cnt = 0;
  int sm, bi;
  for (int i = 0; i < n; i++) {
    x[n - i - 1] = (int)s[i] - 48;
    if (x[n - i - 1])
      cnt++;
  }
  sm = cnt - 1;
  bi = cnt + 1;

  vi ans(n, 0);

  if (sm) {
    vvi mod(n, vi(2, 0));
    vvi acc(n, vi(2, 0));
    mod[0][0] = 1 % sm;
    mod[0][1] = 1 % bi;
    acc[0][0] = (x[0]) ? mod[0][0] : 0;
    acc[0][1] = (x[0]) ? mod[0][1] : 0;
    for (int i = 1; i < n; i++) {
      mod[i][0] = mod[i - 1][0] * 2 % sm, mod[i][1] = mod[i - 1][1] * 2 % bi;
      if (x[i])
        acc[i][0] = (acc[i - 1][0] + mod[i][0]) % sm,
        acc[i][1] = (acc[i - 1][1] + mod[i][1]) % bi;
      else
        acc[i][0] = acc[i - 1][0], acc[i][1] = acc[i - 1][1];
    }
    int t;
    for (int i = 0; i < n; i++) {
      if (x[i])
        t = (sm + acc[n - 1][0] - mod[i][0]) % sm;
      else
        t = (acc[n - 1][1] + mod[i][1]) % bi;
      ans[i] = 1 + solve(t);
    }
  } else {
    vvi mod(n, vi(2, 0));
    vvi acc(n, vi(2, 0));
    mod[0][1] = 1 % bi;
    acc[0][1] = (x[0]) ? mod[0][1] : 0;
    for (int i = 1; i < n; i++) {
      mod[i][1] = mod[i - 1][1] * 2 % bi;
      if (x[i])
        acc[i][1] = (acc[i - 1][1] + mod[i][1]) % bi;
      else
        acc[i][1] = acc[i - 1][1];
    }
    int t;
    for (int i = 0; i < n; i++) {
      if (x[i])
        t = 0;
      else
        t = (acc[n - 1][1] + mod[i][1]) % bi;
      if (x[i])
        ans[i] = 0;
      else
        ans[i] = 1 + solve(t);
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans[n - 1 - i] << endl;
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <ctime>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using pii = pair<int, int>;
using ppi = pair<pii, int>;

int f_dir[2][4] = {{-1, 0, 1, 0}, {0, 1, 0, -1}}; // ↑→↓←
int e_dir[2][8] = {{-1, -1, -1, 0, 0, 1, 1, 1}, {-1, 0, 1, -1, 1, -1, 0, 1}};
bool is_in_field(int y, int x, int h, int w) {
  return (y >= 0 && y < h && x >= 0 && x < w);
}

int solve(int t) {
  if (t == 0)
    return 0;
  int p = t;
  int cnt = 0;
  while (p) {
    if (p % 2)
      cnt++;
    p /= 2;
  }
  return 1 + solve(t % cnt);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int ti = clock();
  // start-----------------------------------------------
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> x(n);
  int cnt = 0;
  int sm, bi;
  for (int i = 0; i < n; i++) {
    x[n - i - 1] = (int)s[i] - 48;
    if (x[n - i - 1])
      cnt++;
  }
  sm = cnt - 1;
  bi = cnt + 1;

  vvi mod(n, vi(2, 0));
  vvi acc(n, vi(2, 0));
  mod[0][0] = 1 % sm;
  mod[0][1] = 1 % bi;
  acc[0][0] = (x[0]) ? mod[0][0] : 0;
  acc[0][1] = (x[0]) ? mod[0][1] : 0;
  for (int i = 1; i < n; i++) {
    mod[i][0] = mod[i - 1][0] * 2 % sm, mod[i][1] = mod[i - 1][1] * 2 % bi;
    if (x[i])
      acc[i][0] = (acc[i - 1][0] + mod[i][0]) % sm,
      acc[i][1] = (acc[i - 1][1] + mod[i][1]) % bi;
    else
      acc[i][0] = acc[i - 1][0], acc[i][1] = acc[i - 1][1];
  }
  vi ans(n, 0);
  int t;
  for (int i = 0; i < n; i++) {
    if (x[i])
      t = (sm + acc[n - 1][0] - mod[i][0]) % sm;
    else
      t = (acc[n - 1][1] + mod[i][1]) % bi;
    ans[i] = 1 + solve(t);
  }

  for (int i = 0; i < n; i++) {
    cout << ans[n - 1 - i] << endl;
  }

  // end-----------------------------------------------
  // cerr << 1.0 * (clock() - ti) / CLOCKS_PER_SEC << endl;
}