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

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<pairs, null_type, greater<pairs>, rb_tree_tag,                          \\
       tree_order_statistics_node_update>
#define ll long long
#define lld long double
#define vc vector<ll>
const ll MOD = (1e9 + 7);
typedef pair<ll, ll> pairs;
ll power(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll res = 1;
  a = a % p;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % p;
      b--;
    }
    a = (a * a) % p;
    b >>= (ll)1;
  }
  return res;
}

int main() {
  // your code goes here
  std::ios::sync_with_stdio(false);
  ll t, n, i, j, k, l, c, f, cnt;
  cin >> n;
  string s;
  c = 0;
  ll num1 = 0, num2 = 0, num;
  cin >> s;
  for (i = 0; i < n; i++)
    if (s[i] == '1')
      c++;
  k = c;
  ll t1 = 1, t2 = 1;
  for (j = n - 1; j >= 0; j--) {
    if (s[j] == '1') {
      num1 += t1;
      num2 += t2;
      num1 %= k + 1;
      if (k == 1)
        num2 %= 1;
      else
        num2 %= k - 1;
    }
    t1 *= 2;
    t2 *= 2;
    t1 %= k + 1;
    if (k > 1)
      t2 %= k - 1;
    else
      t2 %= 1;
  }
  for (i = 0; i < n; i++) {
    cnt = 1;
    k = c;
    if (s[i] == '0')
      num = (num1 + power((ll)2, (n - 1 - i), ++k)) % k;
    else {
      if (k == 1) {
        cout << 0 << "\\n";
        continue;
      }
      num = (num2 - power((ll)2, (n - 1 - i), --k) + k) % k;
    }
    while (num > 0) {
      cnt++;
      num %= (__builtin_popcount(num));
    }
    cout << cnt << "\\n";
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<pairs, null_type, greater<pairs>, rb_tree_tag,                          \\
       tree_order_statistics_node_update>
#define ll long long
#define lld long double
#define vc vector<ll>
const ll MOD = (1e9 + 7);
typedef pair<ll, ll> pairs;
ll power(ll a, ll b, ll p) {
  if (b == 0)
    return 1;
  ll res = 1;
  a = a % p;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % p;
      b--;
    }
    a = (a * a) % p;
    b >>= (ll)1;
  }
  return res;
}

int main() {
  // your code goes here
  std::ios::sync_with_stdio(false);
  ll t, n, i, j, k, l, c, f, cnt;
  cin >> n;
  string s;
  c = 0;
  ll num1 = 0, num2 = 0, num;
  cin >> s;
  for (i = 0; i < n; i++)
    if (s[i] == '1')
      c++;
  k = c;
  ll t1 = 1, t2 = 1;
  for (j = n - 1; j >= 0; j--) {
    if (s[j] == '1') {
      num1 += t1;
      num2 += t2;
      num1 %= k + 1;
      num2 %= k - 1;
    }
    t1 *= 2;
    t2 *= 2;
    t1 %= k + 1;
    t2 %= k - 1;
  }
  for (i = 0; i < n; i++) {
    cnt = 1;
    k = c;
    if (s[i] == '0')
      num = (num1 + power((ll)2, (n - 1 - i), ++k)) % k;
    else {
      if (k == 1) {
        cout << 0 << "\\n";
        continue;
      }
      num = (num2 - power((ll)2, (n - 1 - i), --k) + k) % k;
    }
    while (num > 0) {
      cnt++;
      num %= (__builtin_popcount(num));
    }
    cout << cnt << "\\n";
  }
  return 0;
}