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;
  t = 1;
  for (j = n - 1; j >= 0; j--) {
    if (s[j] == '1') {
      num1 += t;
      num1 %= k + 1;
    }
    t *= 2;
    t %= k + 1;
  }
  t = 1;
  for (j = n - 1; j >= 0 && c > 1; j--) {
    if (s[j] == '1') {
      num2 += t;
      num2 %= k - 1;
    }
    t *= 2;
    t %= 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;
}


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, s2;
  c = 0;
  cin >> s;
  for (i = 0; i < n; i++)
    if (s[i] == '1')
      c++;
  k = c;
  t = 1;
  ll num = 0;
  for (i = 0; i < n; i++) {
    cnt = 1;
    s2 = s;
    if (s[i] == '0') {
      k = c + 1;
      s2[i] = '1';
    } else {
      s2[i] = '0';
      k = c - 1;
    }
    if (k == 1) {
      cout << 1 << "\\n";
      continue;
    }
    num = 0;
    t = 1;
    for (j = n - 1; j >= 0; j--) {
      if (s2[j] == '1') {
        num += t;
        num %= k;
      }
      t *= 2;
      t %= k;
    }
    while (num > 0) {
      cnt++;
      num %= (__builtin_popcount(num));
    }
    cout << cnt << "\\n";
  }
  return 0;
}