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

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP1(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ALL(x) begin(x), end(x)
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

template <class T> inline bool chmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) {
  sort(begin(v), end(v), f);
  v.resize(unique(begin(v), end(v)) - begin(v));
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  string x;
  cin >> n >> x;
  // init
  vector<LL> a(n + 1);
  for (int i = 1; i <= n; i++) {
    a[i] = a[i % __builtin_popcount(i)] + 1;
  }

  LL M = 0;
  REP(i, n) if (x[i] == '1') M++;

  vector<LL> ans(n);
  if (M < n) {
    LL p = 1;
    LL res = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (x[i] == '1') {
        res += p % (M + 1);
      }
      p = (LL)p * 2 % (M + 1);
    }

    p = 1;
    for (int i = n - 1; i >= 0; i--) {
      if (x[i] == '0') {
        LL y = (res + p) % (M + 1);
        ans[i] = a[y] + 1;
      }
      p = p * 2 % (M + 1);
    }
  }
  if (M > 1) {
    LL p = 1;
    LL res = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (x[i] == '1') {
        res += p % (M - 1);
      }
      p = (LL)p * 2 % (M - 1);
    }

    p = 1;
    for (int i = n - 1; i >= 0; i--) {
      if (x[i] == '1') {
        LL y = (res - p) % (M - 1);
        if (y < 0)
          y += M - 1;
        ans[i] = a[y] + 1;
      }
      p = p * 2 % (M - 1);
    }
  }
  REP(i, n) cout << ans[i] << '\\n';
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define REP1(i, a, b) for (int i = a; i <= (int)(b); i++)
#define ALL(x) begin(x), end(x)
#define PB push_back
using namespace std;
typedef int64_t LL;
typedef vector<int> VI;
typedef pair<int, int> PII;

template <class T> inline bool chmax(T &a, const T &b) {
  return b > a ? a = b, true : false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  return b < a ? a = b, true : false;
}
template <class T> using MaxHeap = priority_queue<T>;
template <class T> using MinHeap = priority_queue<T, vector<T>, greater<T>>;
template <class T, class F = less<T>> void sort_uniq(vector<T> &v, F f = F()) {
  sort(begin(v), end(v), f);
  v.resize(unique(begin(v), end(v)) - begin(v));
}

const int N = 2e5 + 5;
int a[N];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  string x;
  cin >> n >> x;
  // init
  a[0] = 0;
  for (int i = 1; i < N; i++) {
    a[i] = a[i % __builtin_popcount(i)] + 1;
  }

  int M = 0;
  REP(i, n) if (x[i] == '1') M++;

  vector<int> ans(n);
  if (M < n) {
    int p = 1;
    int res = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (x[i] == '1') {
        res += p % (M + 1);
      }
      p = (LL)p * 2 % (M + 1);
    }

    p = 1;
    for (int i = n - 1; i >= 0; i--) {
      if (x[i] == '0') {
        int y = (res + p) % (M + 1);
        ans[i] = a[y] + 1;
      }
      p = (LL)p * 2 % (M + 1);
    }
  }
  if (M > 1) {
    int p = 1;
    int res = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (x[i] == '1') {
        res += p % (M - 1);
      }
      p = (LL)p * 2 % (M - 1);
    }

    p = 1;
    for (int i = n - 1; i >= 0; i--) {
      if (x[i] == '1') {
        int y = ((LL)res - p + M - 1) % (M - 1);
        if (y < 0)
          y += M - 1;
        ans[i] = a[y] + 1;
      }
      p = (LL)p * 2 % (M - 1);
    }
  }
  REP(i, n) cout << ans[i] << '\\n';
  return 0;
}