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

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using PP = pair<int, P>;
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rep0(i, n) for (int i = 0; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef _DEBUG
string _format(string args) {
  string r = "", t;
  istringstream s(args);
  while (getline(s, t, ','))
    r += t + ":%ld ";
  return r + "\\n";
}
#define debug(...)                                                             \\
  do {                                                                         \\
    printf(_format(#__VA_ARGS__).c_str(), __VA_ARGS__);                        \\
  } while (0)
#define fast_ios()                                                             \\
  do {                                                                         \\
  } while (0);
#else //_DEBUG
#define debug(...)                                                             \\
  do {                                                                         \\
  } while (0)
#define fast_ios()                                                             \\
  do {                                                                         \\
    std::ios::sync_with_stdio(false);                                          \\
    std::cin.tie(nullptr);                                                     \\
  } while (0)
#endif //_DEBUG

int n;
int s[200000];

int popcount(int x) {
  int popcnt = 0;
  while (x != 0) {
    if (x & 1)
      popcnt++;
    x >>= 1;
  }
  return popcnt;
}

int popcount_mod_s(int popcnt) {
  int a = 0;
  for (int j = 0; j < n; j++) {
    a = ((a << 1) + s[j]) % popcnt;
  }
  return a;
}

int pow_2_mod_inc[200000];
int pow_2_mod_dec[200000];

int main() {
  fast_ios();

  cin >> n;
  string ss;
  cin >> ss;

  int popcnt_x = 0;
  for (int i = 0; i < n; i++) {
    s[i] = ss[i] - '0';
    popcnt_x += s[i];
  }
  int pp = popcnt_x + 1;
  int pn = popcnt_x - 1;

  int x = 0, y = 0;
  rep0(j, n) {
    x = ((x << 1) + s[j]) % pp;
    if (pn)
      y = ((y << 1) + s[j]) % pn;
    debug(popcnt_x, x, y);
  }
  int inc = 1, dec = 1;
  rep0(j, n) {
    pow_2_mod_dec[j] = dec;
    pow_2_mod_inc[j] = inc;
    inc = (inc << 1) % pp;
    if (pn)
      dec = (dec << 1) % pn;
    debug(inc, dec);
  }

  rep0(i, n) {
    int popcnt = popcnt_x + (s[i] == 1 ? -1 : 1);
    int cnt = 0;
    if (popcnt > 0) {
      int a = ((s[i] == 1) ? (y - pow_2_mod_dec[n - 1 - i] + popcnt)
                           : (x + pow_2_mod_inc[n - 1 - i])) %
              popcnt;
      cnt = 1;
      debug(a, popcnt, cnt);
      while (popcnt > 0 && a > 0) {
        popcnt = popcount(a);
        debug(a, popcnt, cnt);
        a %= popcnt;
        cnt++;
      }
    }
    cout << cnt << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using P = pair<int, int>;
using PP = pair<int, P>;
#define rep1(i, n) for (int i = 1; i <= (n); i++)
#define rep0(i, n) for (int i = 0; i < (n); i++)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#ifdef _DEBUG
string _format(string args) {
  string r = "", t;
  istringstream s(args);
  while (getline(s, t, ','))
    r += t + ":%ld ";
  return r + "\\n";
}
#define debug(...)                                                             \\
  do {                                                                         \\
    printf(_format(#__VA_ARGS__).c_str(), __VA_ARGS__);                        \\
  } while (0)
#define fast_ios()                                                             \\
  do {                                                                         \\
  } while (0);
#else //_DEBUG
#define debug(...)                                                             \\
  do {                                                                         \\
  } while (0)
#define fast_ios()                                                             \\
  do {                                                                         \\
    std::ios::sync_with_stdio(false);                                          \\
    std::cin.tie(nullptr);                                                     \\
  } while (0)
#endif //_DEBUG

int n;
int s[200000];

int popcount(int x) {
  int popcnt = 0;
  while (x != 0) {
    if (x & 1)
      popcnt++;
    x >>= 1;
  }
  return popcnt;
}

int popcount_mod_s(int popcnt) {
  int a = 0;
  for (int j = 0; j < n; j++) {
    a = ((a << 1) + s[j]) % popcnt;
  }
  return a;
}

int pow_2_mod_inc[200000];
int pow_2_mod_dec[200000];

int main() {
  fast_ios();

  cin >> n;
  string ss;
  cin >> ss;

  int popcnt_x = 0;
  for (int i = 0; i < n; i++) {
    s[i] = ss[i] - '0';
    popcnt_x += s[i];
  }

  int x = 0, y = 0;
  rep0(j, n) {
    x = ((x << 1) + s[j]) % (popcnt_x + 1);
    if (popcnt_x > 0)
      y = ((y << 1) + s[j]) % (popcnt_x - 1);
    debug(popcnt_x, x, y);
  }
  int inc = 1, dec = 1;
  rep0(j, n) {
    pow_2_mod_dec[j] = dec;
    pow_2_mod_inc[j] = inc;
    inc = (inc << 1) % (popcnt_x + 1);
    if (popcnt_x > 0)
      dec = (dec << 1) % (popcnt_x - 1);
    debug(inc, dec);
  }

  rep0(i, n) {
    s[i] ^= 1; // i桁目を反転

    int popcnt = popcnt_x + (s[i] == 0 ? -1 : 1);
    int cnt = 0;
    if (popcnt > 0) {
      int a = ((s[i] == 0) ? (y - pow_2_mod_dec[n - 1 - i] + popcnt)
                           : (x + pow_2_mod_inc[n - 1 - i])) %
              popcnt;
      cnt = 1;
      debug(a, popcnt, cnt);
      while (a > 0) {
        popcnt = popcount(a);
        debug(a, popcnt, cnt);
        a %= popcnt;
        cnt++;
      }
    }
    cout << cnt << endl;
    s[i] ^= 1; // i桁目を元に戻す
  }
}