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

/**                                                     **\\
 *    created by:   Divianshu Bansal                     *
 *    e-mail id:    god_coder@icloud.com                 *
 *                                                        */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
#define MOD 1000000007
#define int long long
#define BIG 998244353
#define pf push_front
#define print(x)                                                               \\
  for (auto el : x)                                                            \\
    cout << el << " ";                                                         \\
  cout << endl
#define bits(x) __builtin_popcount(x)
#define ff first
#define se second
#define pb push_back
#define pii pair<int, int>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define PSET(x, y) fixed << setprecision(y) << x
#define mp make_pair
#define pi 3.141592653589

int power(int x, int y) {
  int r = 1, z = x;
  while (y) {
    if (y & 1)
      r *= z;
    z *= z;
    y = y >> 1;
  }
  return r;
}
int powerm(int x, int y, int p) {
  int r = 1;
  while (y) {
    if (y & 1)
      r = (r * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return r % p;
}
int modinv(int x, int m) { return powerm(x, m - 2, m); }
int logarithm(int a, int b) {
  int x = 0;
  while (a > 1) {
    x++;
    a /= b;
  }
  return x;
}
int max3(int a, int b, int c) { return max(a, max(b, c)); }
int min3(int a, int b, int c) { return min(a, min(b, c)); }
int32_t main() {
  fast;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int mi = 0, ma = 0;
  int tot = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      ++tot;
    }
  }
  int a = tot - 1;
  int b = tot + 1;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (a > 0) {
        mi += powerm(2, n - 1 - i, a);
        mi %= a;
      }
      ma += powerm(2, n - 1 - i, b);
      ma %= b;
    }
  }
  // return 0;
  int maf = ma;
  int mif = mi;
  for (int i = 0; i < n; ++i) {
    int tot = 0;
    ma = maf;
    mi = mif;
    if (s[i] == '0') {
      ma += powerm(2, n - 1 - i, b);
      ma %= b;
      ++tot;
      int p = ma;
      while (p > 0) {
        int f = __builtin_popcount(p);
        p %= f;
        ++tot;
      }
      cout << tot << '\\n';
    } else {
      if (a <= 0) {
        cout << 0 << '\\n';
        continue;
      }
      mi -= powerm(2, n - 1 - i, a);
      mi += a;
      mi %= a;
      ++tot;
      int p = mi;
      while (p > 0) {
        int f = __builtin_popcount(p);
        p %= f;
        ++tot;
      }
      cout << tot << '\\n';
    }
  }
  return 0;
}

Error: 0

Fixed submission:

/**                                                     **\\
 *    created by:   Divianshu Bansal                     *
 *    e-mail id:    god_coder@icloud.com                 *
 *                                                        */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <functional>

using namespace std;
using namespace __gnu_pbds;

typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    new_data_set;
#define MOD 1000000007
#define int long long
#define BIG 998244353
#define pf push_front
#define print(x)                                                               \\
  for (auto el : x)                                                            \\
    cout << el << " ";                                                         \\
  cout << endl
#define bits(x) __builtin_popcount(x)
#define ff first
#define se second
#define pb push_back
#define pii pair<int, int>
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define PSET(x, y) fixed << setprecision(y) << x
#define mp make_pair
#define pi 3.141592653589

int power(int x, int y) {
  int r = 1, z = x;
  while (y) {
    if (y & 1)
      r *= z;
    z *= z;
    y = y >> 1;
  }
  return r;
}
int powerm(int x, int y, int p) {
  int r = 1;
  while (y) {
    if (y & 1)
      r = (r * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return r % p;
}
int modinv(int x, int m) { return powerm(x, m - 2, m); }
int logarithm(int a, int b) {
  int x = 0;
  while (a > 1) {
    x++;
    a /= b;
  }
  return x;
}
int max3(int a, int b, int c) { return max(a, max(b, c)); }
int min3(int a, int b, int c) { return min(a, min(b, c)); }
int32_t main() {
  fast;
  int n;
  cin >> n;
  string s;
  cin >> s;
  int mi = 0, ma = 0;
  int tot = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      ++tot;
    }
  }
  int a = tot - 1;
  int b = tot + 1;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1') {
      if (a > 0) {
        mi += powerm(2, n - 1 - i, a);
      }
      ma += powerm(2, n - 1 - i, b);
      mi %= a;
      ma %= b;
    }
  }
  int maf = ma;
  int mif = mi;
  for (int i = 0; i < n; ++i) {
    int tot = 0;
    ma = maf;
    mi = mif;
    if (s[i] == '0') {
      ma += powerm(2, n - 1 - i, b);
      ma %= b;
      ++tot;
      int p = ma;
      while (p > 0) {
        int f = __builtin_popcount(p);
        p %= f;
        ++tot;
      }
      cout << tot << '\\n';
    } else {
      if (a <= 0) {
        cout << 0 << '\\n';
        continue;
      }
      mi -= powerm(2, n - 1 - i, a);
      mi += a;
      mi %= a;
      ++tot;
      int p = mi;
      while (p > 0) {
        int f = __builtin_popcount(p);
        p %= f;
        ++tot;
      }
      cout << tot << '\\n';
    }
  }
  return 0;
}