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

/*Bismillahir Rahmanir Rahim*/ //{
#include <bits/stdc++.h>
#include <ext/numeric>
using namespace std;
using __gnu_cxx::power;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll llinf = (1ll << 61) - 1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
  static constexpr auto &os = cout; // replace by cerr in interactive.
  template <class C> static auto o(C *x) -> decltype(os << *x, 0);
  template <class C> static char o(...);
  template <class C>
  typename enable_if<sizeof o<C>(0) != 1, Dbg &>::type operator<<(const C &x) {
    os << x;
    return *this;
  }
  template <class C> Dbg &dump(C b, C e) {
    for (
        struct {
          C i;
          int j;
        } i = {(*this << "\\n[", b), 0};
        i.i != e and i.j < 26; i.i++, i.j++)
      *this << ", " + 2 * (i.i == b) << *i.i;
    return *this << ']';
  }
  template <class C>
  typename enable_if<sizeof o<C>(0) == 1, Dbg &>::type operator<<(const C &x) {
    return dump(begin(x), end(x));
  }
  template <class C, size_t X>
  typename enable_if<!is_same<C, const char>::value, Dbg &>::type
  operator<<(C (&x)[X]) {
    return dump(begin(x), end(x));
  }
  template <class B, class C> Dbg &operator<<(const pair<B, C> &x) {
    return *this << '{' << x.first << ", " << x.second << '}';
  }
  Dbg &operator<<(ostream &(*x)(std::ostream &)) {
    os << x;
    return *this;
  }
} dbg;
void err(istringstream *iss) { delete iss; }
template <class C, class... Args>
void err(istringstream *iss, const C &val, const Args &...args) {
  string name;
  *iss >> name;
  if (name.back() == ',')
    name.pop_back();
  dbg << name << " = " << val << "; ", err(iss, args...);
}
#define bug(args...)                                                           \\
  dbg << __LINE__ << ": ", err(new istringstream(#args), args), dbg << endl
#define cbug(a, args...)                                                       \\
  if (a)                                                                       \\
    bug(args);
#define ASSERT(a, o, b, args...)                                               \\
  if (!((a)o(b)))                                                              \\
    bug(a, b, ##args), assert((a)o(b));
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int T, Q, cn; //}
const int inf = 1000000007, mxn = 200005;
#define im(a, b)                                                               \\
  if (a >= b)                                                                  \\
    a -= b;                                                                    \\
  else if (a < 0)                                                              \\
    a += b;

int n, firstMod[4], p2Mod[4][mxn], pref[mxn], f[mxn];
char s[mxn];

signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  cout.precision(11), cout.setf(ios::fixed);
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    pref[i] = pref[i % __builtin_popcount(i)] + 1;
  }
  int cntSet = 0;
  for (int i = 0; i < n; i++) {
    cntSet += s[i] == '1';
  }
  for (int i = 0; i < 3; i++) {
    if (cntSet + i - 1 <= 0)
      continue;
    p2Mod[i][0] = 1 % (cntSet + i - 1);
    for (int j = 1; j <= n; j++) {
      p2Mod[i][j] = p2Mod[i][j - 1] << 1;
      im(p2Mod[i][j], cntSet + i - 1)
    }
    for (int j = 0; j < n; j++) {
      firstMod[i] += (s[j] == '1' ? p2Mod[i][n - 1 - j] : 0);
      im(firstMod[i], cntSet + i - 1)
    }
  }
  for (int i = 0; i < n; i++) {
    int o = s[i] == '1' ? 0 : 2;
    if (cntSet - 1 + o == 0)
      continue;
    int o2 = firstMod[o] +
             (s[i] == '1' ? -p2Mod[o][n - 1 - i] : p2Mod[o][n - 1 - i]);
    im(o2, cntSet - 1 + o) f[i + 1] = pref[o2] + 1;
  }
  for (int i = 1; i <= n; i++) {
    cout << f[i] << '\\n';
  }
}

Error: 0

Fixed submission:

/*Bismillahir Rahmanir Rahim*/ //{
#include <bits/stdc++.h>
#include <ext/numeric>
using namespace std;
using __gnu_cxx::power;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const ll llinf = (1ll << 61) - 1;
const double eps = 1e-6, ldeps = 1e-9;
struct Dbg {
  static constexpr auto &os = cout; // replace by cerr in interactive.
  template <class C> static auto o(C *x) -> decltype(os << *x, 0);
  template <class C> static char o(...);
  template <class C>
  typename enable_if<sizeof o<C>(0) != 1, Dbg &>::type operator<<(const C &x) {
    os << x;
    return *this;
  }
  template <class C> Dbg &dump(C b, C e) {
    for (
        struct {
          C i;
          int j;
        } i = {(*this << "\\n[", b), 0};
        i.i != e and i.j < 26; i.i++, i.j++)
      *this << ", " + 2 * (i.i == b) << *i.i;
    return *this << ']';
  }
  template <class C>
  typename enable_if<sizeof o<C>(0) == 1, Dbg &>::type operator<<(const C &x) {
    return dump(begin(x), end(x));
  }
  template <class C, size_t X>
  typename enable_if<!is_same<C, const char>::value, Dbg &>::type
  operator<<(C (&x)[X]) {
    return dump(begin(x), end(x));
  }
  template <class B, class C> Dbg &operator<<(const pair<B, C> &x) {
    return *this << '{' << x.first << ", " << x.second << '}';
  }
  Dbg &operator<<(ostream &(*x)(std::ostream &)) {
    os << x;
    return *this;
  }
} dbg;
void err(istringstream *iss) { delete iss; }
template <class C, class... Args>
void err(istringstream *iss, const C &val, const Args &...args) {
  string name;
  *iss >> name;
  if (name.back() == ',')
    name.pop_back();
  dbg << name << " = " << val << "; ", err(iss, args...);
}
#define bug(args...)                                                           \\
  dbg << __LINE__ << ": ", err(new istringstream(#args), args), dbg << endl
#define cbug(a, args...)                                                       \\
  if (a)                                                                       \\
    bug(args);
#define ASSERT(a, o, b, args...)                                               \\
  if (!((a)o(b)))                                                              \\
    bug(a, b, ##args), assert((a)o(b));
#define sl(a) int(strlen(a))
#define sz(a) int(a.size())
int T, Q, cn; //}
const int inf = 1000000007, mxn = 200005;
#define im(a, b)                                                               \\
  if (a >= b)                                                                  \\
    a -= b;                                                                    \\
  else if (a < 0)                                                              \\
    a += b;

int n, firstMod[4], p2Mod[4][mxn], pref[mxn], f[mxn];
char s[mxn];

signed main() {
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  cout.precision(11), cout.setf(ios::fixed);
  cin >> n >> s;
  for (int i = 1; i <= n; i++) {
    pref[i] = pref[i % __builtin_popcount(i)] + 1;
  }
  int cntSet = 0;
  for (int i = 0; i < n; i++) {
    cntSet += s[i] == '1';
  }
  for (int i = 0; i < 3; i++) {
    p2Mod[i][0] = 1 % (cntSet + i - 1);
    for (int j = 1; j <= n; j++) {
      p2Mod[i][j] = p2Mod[i][j - 1] << 1;
      im(p2Mod[i][j], cntSet + i - 1)
    }
    for (int j = 0; j < n; j++) {
      firstMod[i] += (s[j] == '1' ? p2Mod[i][n - 1 - j] : 0);
      im(firstMod[i], cntSet + i - 1)
    }
  }
  for (int i = 0; i < n; i++) {
    int o = s[i] == '1' ? 0 : 2;
    if (cntSet - 1 + o == 0)
      continue;
    int o2 = firstMod[o] +
             (s[i] == '1' ? -p2Mod[o][n - 1 - i] : p2Mod[o][n - 1 - i]);
    im(o2, cntSet - 1 + o) f[i + 1] = pref[o2] + 1;
  }
  for (int i = 1; i <= n; i++) {
    cout << f[i] << '\\n';
  }
}