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

//*****Jai Mata di*****//

#include <bits/stdc++.h>
using namespace std;
#define Nitroboost                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ll long long int
const int mod = 1e9 + 7;
const int N = 2e5 + 15;
#define debug cout << "Good uptill here" << endl << flush;
#define deb(x) cout << #x << ":" << x << endl << flush;

vector<int> adj[N];
int d[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int h[N];

ll power(ll a, ll b, ll md) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return power((a * a) % md, b / 2, md);
  else
    return (a * power(a, b - 1, md)) % md;
}

void solve() {

  int dp[300001] = {
      0,
  };
  for (int i = 1; i <= 300000; i++)
    dp[i] = dp[i % __builtin_popcountll(i)] + 1;

  int n;
  cin >> n;
  string s;
  cin >> s;

  int rem1 = 0;
  int rem2 = 0;
  int bits = 0;

  int ten1[n];
  int ten2[n];
  for (int i = 0; i < s.size(); i++)
    if (s[i] == '1')
      bits++;

  int div1 = bits + 1;
  int div2 = bits - 1;

  for (int i = n - 1, r = 1; i >= 0; i--) {
    ten1[i] = r;
    rem1 = (rem1 + r * (s[i] == '1')) % div1;
    r = (r * 2) % div1;
  }

  if (div2 > 0) {
    for (int i = n - 1, r = 1; i >= 0; i--) {
      ten2[i] = r;
      rem2 = (rem2 + r * (s[i] == '1')) % div2;
      r = (r * 2) % div2;
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (div2 == 0)
        cout << 0 << endl;
      else {
        ll t = (rem2 - ten2[i] + div2) % div2;
        cout << dp[t] + 1 << endl;
      }
    } else {
      ll t = (rem1 + ten1[i] + div1) % div1;
      cout << dp[t] + 1 << endl;
    }
  }
}

int main() {
  Nitroboost;
  solve();
}

Error: WA

Fixed submission:

//*****Jai Mata di*****//

#include <bits/stdc++.h>
using namespace std;
#define Nitroboost                                                             \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define ll long long int
const int mod = 1e9 + 7;
const int N = 2e5 + 15;
#define debug cout << "Good uptill here" << endl << flush;
#define deb(x) cout << #x << ":" << x << endl << flush;

vector<int> adj[N];
int d[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
int h[N];
int dp[N];

ll power(ll a, ll b, ll md) {
  if (b == 0)
    return 1;
  if (b % 2 == 0)
    return power((a * a) % md, b / 2, md);
  else
    return (a * power(a, b - 1, md) % md);
}

int main() {
  Nitroboost;
  int n;
  cin >> n;

  string s;
  cin >> s;
  int l = s.length();
  int cnt = 0;

  for (int i = 0; i < l; i++)
    if (s[i] - '0')
      cnt++;

  ll num1 = 0;
  cnt++;
  for (int i = l - 1; i >= 0; i--) {
    if (s[i] == '1') {
      num1 += power(2, l - i - 1, cnt);
      num1 %= cnt;
    }
  }

  num1 = (num1 + cnt) % cnt;

  cnt--;
  ll num2 = 0;
  if (cnt > 0) {
    cnt--;
    for (int i = l - 1; i >= 0; i--) {
      if (s[i] == '1') {
        num2 += power(2, l - i - 1, cnt);
        num2 %= cnt;
      }
    }
    num2 = (num2 + cnt) % cnt;
    cnt++;
  }

  dp[1] = 1;
  for (int i = 2; i < N; i++) {
    int x = __builtin_popcountll(i);
    dp[i] = 1 + dp[(i % x + x) % x];
  }

  for (int i = 0; i < l; i++) {
    if (s[i] == '1') {
      int bt = cnt - 1;
      ll x = (num1 + power(2, l - 1 - i, bt)) % bt;
      cout << 1 + dp[x] << "\\n";
    } else {
      int bt = cnt - 1;
      ll x = (num2 - power(2, l - 1 - i, bt)) % bt;
      cout << 1 + dp[x] << "\\n";
    }
  }
}