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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (n); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << '{' << p.first << "," << p.second << '}';
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? ']' : ',');
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

int dp[200010];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  string x;
  cin >> n >> x;
  dp[0] = 0;
  rep2(i, 1, 200010) {
    int cnt = __builtin_popcount(i);
    dp[i] = dp[i % cnt] + 1;
  }
  int cnt = 0;
  rep(i, n) if (x[i] == '1') cnt++;
  int ppl = 1, pdis = 1;
  int pl = 0, dis = 0;
  rrep(i, n) {
    if (cnt < n) {
      if (x[i] == '1')
        (pl += ppl) %= cnt + 1;
      (ppl *= 2) %= cnt + 1;
    }
    if (cnt > 1) {
      if (x[i] == '1')
        (dis += pdis) %= cnt - 1;
      (pdis *= 2) %= cnt - 1;
    }
  }
  ppl = 1;
  pdis = 1;
  vi ans(n);
  rrep(i, n) {
    if (x[i] == '1') {
      if (cnt == 1) {
        ans[i] = 0;
        continue;
      }
      int nsum = dis - pdis;
      nsum %= cnt - 1;
      nsum += cnt - 1;
      nsum %= cnt - 1;
      ans[i] = dp[nsum] + 1;
    } else {
      int nsum = pl + ppl;
      nsum %= cnt + 1;
      ans[i] = dp[nsum] + 1;
    }
    if (cnt < n) {
      (ppl *= 2) %= cnt + 1;
    }
    if (cnt > 1) {
      (pdis *= 2) %= cnt - 1;
    }
  }
  rep(i, n) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (n); ++i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vd vector<double>
#define vs vector<string>
#define vc vector<char>
#define vb vector<bool>
#define vp vector<P>
using namespace std;
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, ll>;
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.first >> p.second;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << '{' << p.first << "," << p.second << '}';
}
template <class T> istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) {
    is >> t;
  }
  return is;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '[';
  rep(i, v.size()) os << v[i] << (i == v.size() - 1 ? ']' : ',');
  return os;
}
void Yes(bool b) { cout << (b ? "Yes" : "No") << endl; }
void YES(bool b) { cout << (b ? "YES" : "NO") << endl; }
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
const int inf = 1001001001;
const ll linf = 1001001001001001001;

int dp[200010];

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  string x;
  cin >> n >> x;
  dp[0] = 0;
  rep2(i, 1, 200010) {
    int cnt = __builtin_popcount(i);
    dp[i] = dp[i % cnt] + 1;
  }
  int cnt = 0;
  rep(i, n) if (x[i] == '1') cnt++;
  int ppl = 1, pdis = 1;
  int pl = 0, dis = 0;
  rrep(i, n) {
    if (cnt < n) {
      if (x[i] == '1')
        (pl += ppl) %= cnt + 1;
      (ppl *= 2) %= cnt + 1;
    }
    if (cnt > 0) {
      if (x[i] == '1')
        (dis += pdis) %= cnt - 1;
      (pdis *= 2) %= cnt - 1;
    }
  }
  ppl = 1;
  pdis = 1;
  vi ans(n);
  rrep(i, n) {
    if (x[i] == '1') {
      if (cnt == 1) {
        ans[i] = 0;
        continue;
      }
      int nsum = dis - pdis;
      nsum += cnt - 1;
      nsum %= cnt - 1;
      ans[i] = dp[nsum] + 1;
    } else {
      int nsum = pl + ppl;
      nsum %= cnt + 1;
      ans[i] = dp[nsum] + 1;
    }
    if (cnt < n) {
      (ppl *= 2) %= cnt + 1;
    }
    if (cnt > 0) {
      (pdis *= 2) %= cnt - 1;
    }
  }
  rep(i, n) cout << ans[i] << endl;
}