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

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

#define ONLINE_JUDGE

#define V vector
#define P pair

using LL = long long;
#define FOR(i, x, y)                                                           \\
  for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y)                                                          \\
  for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  do {                                                                         \\
    cerr << "\\033[32;1m" << #x << " -> ";                                      \\
    err(x);                                                                    \\
  } while (0)
void err() { cerr << "\\033[39;0m" << endl; }
template <template <typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
  for (auto v : a)
    cerr << v << ' ';
  err(x...);
}
template <typename T, typename... A> void err(T a, A... x) {
  cerr << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif
#define sz(x) (int)x.size()

LL bin(LL a, LL b, LL p) {
  LL res = 1;
  a = (a % p + p) % p;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1)
      res = res * a % p;

  return res;
}

const int N = 2e5 + 50;

int f[N];
int p2[3][N];

inline int popcount(int x) {
  int res = 0;
  while (x) {
    ++res;
    x &= (x - 1);
  }
  return res;
}

void init() {
  for (int i = 1; i < N; ++i) {
    f[i] = 0;
    int t = i;
    while (t) {
      ++f[i];
      t = t % popcount(t);
    }
  }
}

int n;
char s[N];

void solve() {
  scanf("%d", &n);
  scanf("%s", s);
  vector<int> res(3, 0);

  int bitcnt = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      bitcnt += 1;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      if ((bitcnt - 1 + j) == 0)
        continue;
      res[j] = (res[j] << 1 | (s[i] - '0')) % (bitcnt - 1 + j);
    }
  }
  p2[0][0] = p2[1][0] = p2[2][0] = 1;
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      if ((bitcnt - 1 + j) == 0)
        continue;
      p2[j][i] = (p2[j][i - 1] << 1) % (bitcnt - 1 + j);
    }
  }

  for (int i = 0; i < n; ++i) {
    int nmod, ans;
    if (bitcnt == 1 && s[i] == '1') {
      puts("0");
      continue;
    }

    if (s[i] == '1') {
      nmod = bitcnt - 1;
      ans = (res[0] - p2[0][n - i - 1]) % nmod + nmod;
    } else {
      nmod = bitcnt + 1;
      ans = (res[2] + p2[2][n - i - 1]) % nmod + nmod;
    }
    ans %= nmod;
    printf("%d\\n", f[ans] + 1);
  }
}

int main() {
  init();

  // for (int i=1; i<=20; ++i) {
  //     dbg(i, f[i], popcount(i));
  // }
  // int t;
  // cin >> t;
  // while (t--) solve();

  solve();

  return 0;
}

Error: 0

Fixed submission:

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

#define ONLINE_JUDGE

#define V vector
#define P pair

using LL = long long;
#define FOR(i, x, y)                                                           \\
  for (decay<decltype(y)>::type i = (x), _##i = (y); i < _##i; ++i)
#define FORD(i, x, y)                                                          \\
  for (decay<decltype(x)>::type i = (x), _##i = (y); i > _##i; --i)
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  do {                                                                         \\
    cerr << "\\033[32;1m" << #x << " -> ";                                      \\
    err(x);                                                                    \\
  } while (0)
void err() { cerr << "\\033[39;0m" << endl; }
template <template <typename...> class T, typename t, typename... A>
void err(T<t> a, A... x) {
  for (auto v : a)
    cerr << v << ' ';
  err(x...);
}
template <typename T, typename... A> void err(T a, A... x) {
  cerr << a << ' ';
  err(x...);
}
#else
#define dbg(...)
#endif
#define sz(x) (int)x.size()

LL bin(LL a, LL b, LL p) {
  LL res = 1;
  a = (a % p + p) % p;
  for (; b; b >>= 1, a = a * a % p)
    if (b & 1)
      res = res * a % p;

  return res;
}

const int N = 2e5 + 50;

int f[N];
int p2[3][N];

inline int popcount(int x) {
  int res = 0;
  while (x) {
    ++res;
    x &= (x - 1);
  }
  return res;
}

void init() {
  for (int i = 1; i < N; ++i) {
    f[i] = 0;
    int t = i;
    while (t) {
      ++f[i];
      t = t % popcount(t);
    }
  }
}

int n;
char s[N];

void solve() {
  scanf("%d", &n);
  scanf("%s", s);
  vector<int> res(3, 0);

  int bitcnt = 0;
  for (int i = 0; i < n; ++i) {
    if (s[i] == '1')
      bitcnt += 1;
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      res[j] = (res[j] << 1 | (s[i] - '0')) % (bitcnt - 1 + j);
    }
  }
  p2[0][0] = p2[1][0] = p2[2][0] = 1;
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < 3; ++j)
      p2[j][i] = (p2[j][i - 1] << 1) % (bitcnt - 1 + j);
  }

  for (int i = 0; i < n; ++i) {
    int nmod, ans;
    if (s[i] == '1') {
      nmod = bitcnt - 1;
      ans = (res[0] - p2[0][n - i - 1]) % nmod + nmod;
    } else {
      nmod = bitcnt + 1;
      ans = (res[2] + p2[2][n - i - 1]) % nmod + nmod;
    }
    ans %= nmod;
    printf("%d\\n", f[ans] + 1);
  }
}

int main() {
  init();

  // for (int i=1; i<=20; ++i) {
  //     dbg(i, f[i], popcount(i));
  // }
  // int t;
  // cin >> t;
  // while (t--) solve();

  solve();

  return 0;
}