Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1'000'000'000ll + 7;
// const i64 smod = 998244353ll;
// const i64 inf = 1'000'000'000'000'000'000ll + 7;
const i64 inf = 1'000'000'007;

const long double eps = 1e-8;

const i64 MAXN = 200'000 + 5;

i64 simul(i64 v, i64 m) {
  i64 R = 1;
  while (v) {
    R += 1;
    i64 c = 0;
    i64 t = v;
    while (t) {
      c += t % 2;
      t /= 2;
    }
    v %= c;
  }
  return R;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);
  cerr.precision(15);
  cerr.setf(ios::fixed);

  i64 n;
  string x;
  cin >> n;
  cin >> x;

  bool fake = false;
  if (fake) {
    srand(time(0));
    stringstream ss;

    for (i64 i = 0; i < n; i++) {
      ss << rand() % 2;
    }
    x = ss.str();
  }

  reverse(x.begin(), x.end());

  i64 cnt = 0;
  for (auto c : x) {
    if (c == '1') {
      cnt += 1;
    }
  }

  vector<i64> gr(n + 1);
  vector<i64> le(n + 1);

  i64 vgr = 0;
  i64 vle = 0;

  if (cnt < n) {
    for (i64 i = 0; i < n; i++) {
      if (i == 0) {
        gr[i] = 1 % (cnt + 1);
        continue;
      }
      gr[i] = (gr[i - 1] * 2) % (cnt + 1);
    }

    for (i64 i = 0; i < n; i++) {
      if (x[i] == '1') {
        vgr += gr[i];
        vgr %= (cnt + 1);
      }
    }
  }

  if (cnt > 0) {
    for (i64 i = 0; i < n; i++) {
      if (i == 0) {
        le[i] = 1 % (cnt - 1);
        continue;
      }
      le[i] = (le[i - 1] * 2) % (cnt - 1);
    }

    for (i64 i = 0; i < n; i++) {
      if (x[i] == '1') {
        vle += le[i];
        vle %= (cnt - 1);
      }
    }
  }

  vector<i64> R;
  for (i64 i = 0; i < n; i++) {
    if (x[i] == '1') {
      if (cnt == 1) {
        R.push_back(0);
        continue;
      }

      i64 r = vle;
      i64 m = cnt - 1;
      r = (r - le[i] + m) % m;
      R.push_back(simul(r, m));
    } else {
      i64 r = vgr;
      i64 m = cnt + 1;
      r = (r + gr[i]) % m;
      R.push_back(simul(r, m));
    }
  }

  reverse(R.begin(), R.end());
  stringstream ss;
  for (auto t : R) {
    ss << t << endl;
  }
  cout << ss.str();

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>

#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <deque>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <algorithm>
#include <bitset>
#include <chrono>
#include <functional>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1'000'000'000ll + 7;
// const i64 smod = 998244353ll;
// const i64 inf = 1'000'000'000'000'000'000ll + 7;
const i64 inf = 1'000'000'007;

const long double eps = 1e-8;

const i64 MAXN = 200'000 + 5;

i64 simul(i64 v, i64 m) {
  i64 R = 1;
  while (v) {
    R += 1;
    i64 c = 0;
    i64 t = v;
    while (t) {
      c += t % 2;
      t /= 2;
    }
    v %= c;
  }
  return R;
}

int main(int argc, char *argv[]) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
  cout.setf(ios::fixed);
  cerr.precision(15);
  cerr.setf(ios::fixed);

  i64 n;
  string x;
  cin >> n;
  cin >> x;

  bool fake = false;
  if (fake) {
    srand(time(0));
    stringstream ss;

    for (i64 i = 0; i < n; i++) {
      ss << rand() % 2;
    }
    x = ss.str();
  }

  reverse(x.begin(), x.end());

  i64 cnt = 0;
  for (auto c : x) {
    if (c == '1') {
      cnt += 1;
    }
  }

  vector<i64> gr(n + 1);
  vector<i64> le(n + 1);

  i64 vgr = 0;
  i64 vle = 0;

  if (cnt < n) {
    for (i64 i = 0; i < n; i++) {
      if (i == 0) {
        gr[i] = 1 % (cnt + 1);
        continue;
      }
      gr[i] = (gr[i - 1] * 2) % (cnt + 1);
    }

    for (i64 i = 0; i < n; i++) {
      if (x[i] == '1') {
        vgr += gr[i];
        vgr %= (cnt + 1);
      }
    }
  }

  if (cnt > 1) {
    for (i64 i = 0; i < n; i++) {
      if (i == 0) {
        le[i] = 1 % (cnt - 1);
        continue;
      }
      le[i] = (le[i - 1] * 2) % (cnt - 1);
    }

    for (i64 i = 0; i < n; i++) {
      if (x[i] == '1') {
        vle += le[i];
        vle %= (cnt - 1);
      }
    }
  }

  vector<i64> R;
  for (i64 i = 0; i < n; i++) {
    if (x[i] == '1') {
      if (cnt == 1) {
        R.push_back(0);
        continue;
      }

      i64 r = vle;
      i64 m = cnt - 1;
      r = (r - le[i] + m) % m;
      R.push_back(simul(r, m));
    } else {
      i64 r = vgr;
      i64 m = cnt + 1;
      r = (r + gr[i]) % m;
      R.push_back(simul(r, m));
    }
  }

  reverse(R.begin(), R.end());
  stringstream ss;
  for (auto t : R) {
    ss << t << endl;
  }
  cout << ss.str();

  return 0;
}