Problem p02609 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod2 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define INF 2e9

int ans[200011];

string dtob(int num, int rlen) {
  string r = "";
  while (num > 0) {
    r = to_string(num % 2) + r;
    num /= 2;
  }
  int rem = rlen - r.length();
  string t(rem, '0');
  return t + r;
}

int func(string s) {
  int ans = 0;
  for (int i = 0; i < s.length(); i++)
    if (s[i] == '1')
      ans += 1;
  return ans;
}

int main() {
  ans[0] = 0;
  for (int i = 1; i <= 200011; i++) {
    int cnt = func(dtob(i, 20));
    ans[i] = 1 + ans[i % cnt];
  }
  int n;
  string x;
  cin >> n >> x;
  int cnt = func(x);
  vector<int> v1(n), v2(n), v3(n, 0), v4(n, 0);
  int p = 1;
  for (int i = n - 1; i > -1; i--) {
    if (x[i] == '1') {
      v1[i] = p;
      v2[i] = 0;
    } else {
      v1[i] = 0;
      v2[i] = p;
    }
    p = (p * 1LL * 2) % (cnt + 1);
  }
  p = 1;
  if (cnt - 1 > 0) {
    for (int i = n - 1; i > -1; i--) {
      if (x[i] == '1') {
        v3[i] = p;
        v4[i] = 0;
      } else {
        v3[i] = 0;
        v4[i] = p;
      }
      p = (p * 1LL * 2) % (cnt - 1);
    }
  }
  int sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++)
    sum1 = (sum1 * 1LL + v1[i]) % (cnt + 1);
  if (cnt > 1) {
    for (int i = 0; i < n; i++)
      sum2 = (sum2 * 1LL + v3[i]) % (cnt - 1);
  }
  // cout << sum1 << " " << sum2 << endl;
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      int temp = (sum1 * 1LL - v1[i] + v2[i]) % (cnt + 1);
      temp = (temp + cnt + 1) % (cnt + 1);
      // cout << "temp=" << temp << endl;
      cout << 1 + ans[temp] << endl;
    } else {
      if (cnt - 1 == 0)
        cout << "0" << endl;
      else {
        int temp = (sum2 * 1LL - v3[i] + v4[i]) % (cnt - 1);
        temp = (temp + cnt - 1) % (cnt - 1);
        cout << 1 + ans[temp] << endl;
      }
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
#define lli long long int
using namespace std;
#define mod2 1000000007
#define MOD 1000000037
#define mod1 998244353
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define INF 2e9

int ans[200011];

string dtob(int num, int rlen) {
  string r = "";
  while (num > 0) {
    r = to_string(num % 2) + r;
    num /= 2;
  }
  int rem = rlen - r.length();
  string t(rem, '0');
  return t + r;
}

int func(string s) {
  int ans = 0;
  for (int i = 0; i < s.length(); i++)
    if (s[i] == '1')
      ans += 1;
  return ans;
}

int main() {
  ans[0] = 0;
  for (int i = 1; i <= 200010; i++) {
    int cnt = func(dtob(i, 20));
    ans[i] = 1 + ans[i % cnt];
  }
  int n;
  string x;
  cin >> n >> x;
  int cnt = func(x);
  vector<int> v1(n), v2(n), v3(n, 0), v4(n, 0);
  int p = 1;
  for (int i = n - 1; i > -1; i--) {
    if (x[i] == '1') {
      v1[i] = p;
      v2[i] = 0;
    } else {
      v1[i] = 0;
      v2[i] = p;
    }
    p = (p * 1LL * 2) % (cnt + 1);
  }
  p = 1;
  if (cnt - 1 > 0) {
    for (int i = n - 1; i > -1; i--) {
      if (x[i] == '1') {
        v3[i] = p;
        v4[i] = 0;
      } else {
        v3[i] = 0;
        v4[i] = p;
      }
      p = (p * 1LL * 2) % (cnt - 1);
    }
  }
  int sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++)
    sum1 = (sum1 * 1LL + v1[i]) % (cnt + 1);
  if (cnt > 1) {
    for (int i = 0; i < n; i++)
      sum2 = (sum2 * 1LL + v3[i]) % (cnt - 1);
  }
  // cout << sum1 << " " << sum2 << endl;
  for (int i = 0; i < n; i++) {
    if (x[i] == '0') {
      int temp = (sum1 * 1LL - v1[i] + v2[i]) % (cnt + 1);
      temp = (temp + cnt + 1) % (cnt + 1);
      // cout << "temp=" << temp << endl;
      cout << 1 + ans[temp] << endl;
    } else {
      if (cnt - 1 == 0)
        cout << "0" << endl;
      else {
        int temp = (sum2 * 1LL - v3[i] + v4[i]) % (cnt - 1);
        temp = (temp + cnt - 1) % (cnt - 1);
        cout << 1 + ans[temp] << endl;
      }
    }
  }
}