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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 3e18;
const double INFD = 1e30;
const double EPS = 1e-10;
const double PI = acos(-1);
const int MOD = 998244353;
template <typename T> inline T read() {
  T X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
const int MAXN = 200005;
const int MAXV = 3;
int n, m, k;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
char text[MAXN];
int cnt[MAXN];

struct FenwickTree {
  FenwickTree() {}
  inline void increase(int t, int x) {
    for (int i = t; i <= n; i += lowbit(i))
      _arr[i] = ((_arr[i] + x) % M + M) % M;
  }
  inline int getSum(int t) {
    int s = 0;
    for (int i = t; i; i -= lowbit(i))
      s = (s + _arr[i]) % M;
    return s;
  }
  int _arr[MAXN];
  int M;
  inline int lowbit(int x) { return x & (-x); }
};
int fastExp(ll x, ll p, int mod) {
  ll ans = 1;
  while (p) {
    if (p & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    p >>= 1;
  }
  return ans;
}
FenwickTree f1, f2;
int main() {
#ifdef LOCALLL
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  scanf("%d", &n);
  scanf("%s", text);
  int popcnt = 0;
  for (int i = 0; i < n; i++) {
    popcnt += text[i] == '1';
  }
  f1.M = popcnt - 1;
  f2.M = popcnt + 1;
  cnt[0] = 0;
  for (int i = 1; i <= n; i++) {
    int x = i;
    while (x) {
      x %= __builtin_popcount(x);
      cnt[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (text[i] == '1') {
      if (f1.M != 0)
        f1.increase(i + 1, fastExp(2, n - i - 1, f1.M));
      f2.increase(i + 1, fastExp(2, n - i - 1, f2.M));
    }
  }
  for (int i = 0; i < n; i++) {
    if (text[i] == '1') {
      if (f1.M == 0) {
        printf("0\\n");
        continue;
      }
      f1.increase(i + 1, -fastExp(2, n - i - 1, f1.M));
      int s = f1.getSum(n);
      int m = (s + f1.M) % f1.M;
      printf("%d\\n", cnt[m] + 1);
      f1.increase(i + 1, fastExp(2, n - i - 1, f1.M));
    } else {
      f2.increase(i + 1, fastExp(2, n - i - 1, f2.M));
      int s = f2.getSum(n);
      int m = (s + f2.M) % f2.M;
      printf("%d\\n", cnt[m] + 1);
      f2.increase(i + 1, -fastExp(2, n - i - 1, f2.M));
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double LD;
using namespace std;
const int INF = 2147483647;
const int INF2 = 0x3f3f3f3f;
const ll INF64 = 3e18;
const double INFD = 1e30;
const double EPS = 1e-10;
const double PI = acos(-1);
const int MOD = 998244353;
template <typename T> inline T read() {
  T X = 0, w = 0;
  char ch = 0;
  while (!isdigit(ch)) {
    w |= ch == '-';
    ch = getchar();
  }
  while (isdigit(ch))
    X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
  return w ? -X : X;
}
const int MAXN = 200005;
const int MAXV = 3;
int n, m, k;
int dr[4] = {1, 0, -1, 0};
int dc[4] = {0, 1, 0, -1};
int dr2[8] = {1, 1, 1, -1, -1, -1, 0, 0};
int dc2[8] = {1, 0, -1, 1, 0, -1, 1, -1};
int CASE = 1;
char text[MAXN];
int cnt[MAXN];

struct FenwickTree {
  FenwickTree() {}
  inline void increase(int t, int x) {
    for (int i = t; i <= n; i += lowbit(i))
      _arr[i] = ((_arr[i] + x) % M + M) % M;
  }
  inline int getSum(int t) {
    int s = 0;
    for (int i = t; i; i -= lowbit(i))
      s = (s + _arr[i]) % M;
    return s;
  }
  int _arr[MAXN];
  int M;
  inline int lowbit(int x) { return x & (-x); }
};
int fastExp(ll x, ll p, int mod) {
  ll ans = 1;
  while (p) {
    if (p & 1)
      ans = ans * x % mod;
    x = x * x % mod;
    p >>= 1;
  }
  return ans;
}
FenwickTree f1, f2;
int main() {
#ifdef LOCALLL
  freopen("in", "r", stdin);
  freopen("out", "w", stdout);
#endif
  scanf("%d", &n);
  scanf("%s", text);
  int popcnt = 0;
  for (int i = 0; i < n; i++) {
    popcnt += text[i] == '1';
  }
  f1.M = popcnt - 1;
  f2.M = popcnt + 1;
  cnt[0] = 0;
  for (int i = 1; i <= n; i++) {
    int x = i;
    while (x) {
      x %= __builtin_popcount(x);
      cnt[i]++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (text[i] == '1') {
      f1.increase(i + 1, fastExp(2, n - i - 1, f1.M));
      f2.increase(i + 1, fastExp(2, n - i - 1, f2.M));
    }
  }
  for (int i = 0; i < n; i++) {
    if (text[i] == '1') {
      f1.increase(i + 1, -fastExp(2, n - i - 1, f1.M));
      int s = f1.getSum(n);
      int m = (s + f1.M) % f1.M;
      printf("%d\\n", cnt[m] + 1);
      f1.increase(i + 1, fastExp(2, n - i - 1, f1.M));
    } else {
      f2.increase(i + 1, fastExp(2, n - i - 1, f2.M));
      int s = f2.getSum(n);
      int m = (s + f2.M) % f2.M;
      printf("%d\\n", cnt[m] + 1);
      f2.increase(i + 1, -fastExp(2, n - i - 1, f2.M));
    }
  }

  return 0;
}