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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
// lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

int popcount(int n) {
  int ans = 0;
  while (n > 0) {
    if (n & 1)
      ans++;
    n /= 2;
  }
  return ans;
}

int pc_str(string s) {
  int pc = 0;
  for (char c : s) {
    if (c == '1')
      pc++;
  }
  return pc;
}

int rem_str(string s, int pc) {
  int r = 0;
  for (char c : s) {
    r *= 2;
    if (c == '1')
      r++;
    r %= pc;
  }
  return r;
}

void app() {
  int n;
  cin >> n;

  string xs;
  cin >> xs;

  vector<int> s(n);
  s[0] = 0;
  rep_range(i, 1, n) { s[i] = 1 + s[i % popcount(i)]; }

  int pcb = pc_str(xs);
  int rem[2];
  rem[0] = rem_str(xs, pcb + 1);
  if (pcb != 1)
    rem[1] = rem_str(xs, pcb - 1);
  int rb[2] = {1, 1};
  rb[0] = rb[0] % (pcb + 1);
  if (pcb != 1)
    rb[1] = rb[1] % (pcb - 1);

  vector<int> ans(n);
  rep_rev(i, n, 0) {
    int pat = (xs[i] == '0') ? 0 : 1;
    if (pcb == 1 && pat == 1) {
      ans[i] = 0;
      continue;
    }

    ans[i] = 1 + s[(pat == 0) ? (rem[0] + rb[0]) % (pcb + 1)
                              : (rem[1] - rb[1] + (pcb - 1)) % (pcb - 1)];
    rb[0] = rb[0] * 2 % (pcb + 1);
    if (pcb != 1)
      rb[1] = rb[1] * 2 % (pcb - 1);
  }

  rep(i, n) cout << ans[i] << endl;
}

int main() {
  app();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep_range(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep_rev(i, s, e) for (int i = (s)-1; i >= (int)(e); i--)
#define sort_vec(v) sort((v).begin(), (v).end())
typedef int64_t lint;
// lint mod = 998244353;
// lint mod = 1000000007;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}

int popcount(int n) {
  int ans = 0;
  while (n > 0) {
    if (n & 1)
      ans++;
    n /= 2;
  }
  return ans;
}

int pc_str(string s) {
  int pc = 0;
  for (char c : s) {
    if (c == '1')
      pc++;
  }
  return pc;
}

int rem_str(string s, int pc) {
  int r = 0;
  for (char c : s) {
    r *= 2;
    if (c == '1')
      r++;
    r %= pc;
  }
  return r;
}

void app() {
  int n;
  cin >> n;

  string xs;
  cin >> xs;

  vector<int> s(n);
  s[0] = 0;
  rep_range(i, 1, n) { s[i] = 1 + s[i % popcount(i)]; }

  int pcb = pc_str(xs);
  int rem[2];
  rem[0] = rem_str(xs, pcb + 1);
  rem[1] = rem_str(xs, pcb - 1);
  int rb[2] = {1, 1};
  rb[0] = rb[0] % (pcb + 1);
  rb[1] = rb[1] % (pcb - 1);

  vector<int> ans(n);
  rep_rev(i, n, 0) {
    int pat = (xs[i] == '0') ? 0 : 1;

    ans[i] = 1 + s[(pat == 0) ? (rem[0] + rb[0]) % (pcb + 1)
                              : (rem[1] - rb[1] + (pcb - 1)) % (pcb - 1)];
    rb[0] = rb[0] * 2 % (pcb + 1);
    rb[1] = rb[1] * 2 % (pcb - 1);
  }

  rep(i, n) cout << ans[i] << endl;
}

int main() {
  app();
  return 0;
}