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

#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \\
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7, INF = 1e16;

lint dpL[200001][3], dpR[200001][3];
lint dp2[200001][3];
lint N, cnt = 0;
string s;
lint dp3[200001];

lint dfs(lint i) {
  if (dp3[i] != INF)
    return dp3[i];
  lint b_cnt = 0, curr = i;
  while (curr != 0) {
    if (curr % 2 == 1)
      b_cnt++;
    curr /= 2;
  }
  return dp3[i] = dfs(i % b_cnt) + 1;
}

int main() {
  cin >> N;
  cin >> s;
  REP(i, N) {
    if (s[i] == '1')
      cnt++;
  }

  REP(i, 3) {
    dpL[0][i] = 0;
    dpR[N][i] = 0;
  }
  REP(i, N) {
    if (cnt > 1)
      dpL[i + 1][0] = (dpL[i][0] * 2 + (s[i] - '0')) % (cnt - 1);
    dpL[i + 1][2] = (dpL[i][2] * 2 + (s[i] - '0')) % (cnt + 1);
  }
  if (cnt > 1)
    dp2[N][0] = 1 % (cnt - 1);
  dp2[N][2] = 1 % (cnt + 1);
  IREP(i, N) {
    if (cnt > 1)
      dp2[i][0] = (dp2[i + 1][0] * 2) % (cnt - 1);
    dp2[i][2] = (dp2[i + 1][2] * 2) % (cnt + 1);
  }
  IREP(i, N) {
    if (cnt > 1)
      dpR[i][0] = (dpR[i + 1][0] + (s[i] - '0') * dp2[i + 1][0]) % (cnt - 1);
    dpR[i][2] = (dpR[i + 1][2] + (s[i] - '0') * dp2[i + 1][2]) % (cnt + 1);
  }

  REP(i, N) { dp3[i] = INF; }

  dp3[0] = 0;
  REP(i, N + 1) { dfs(i); }
  REP(i, N) {
    if (s[i] == '0') {
      int a = (dpL[i + 1][2] * dp2[i + 1][2] + dp2[i + 1][2] + dpR[i + 1][2]) %
              (cnt + 1);
      cout << dp3[a] + 1 << endl;
    } else {
      if (cnt == 1) {
        cout << 0 << endl;
      } else {
        int a = (dpL[i][0] * dp2[i][0] + dpR[i + 1][0]) % (cnt - 1);
        cout << dp3[a] + 1 << endl;
      }
    }
  }
}


Error: 0

Fixed submission:

#include "bits/stdc++.h"
#include <random>
using namespace std;
typedef long long int lint;
typedef pair<lint, lint> plint;
typedef pair<double long, double long> pld;
#define ALL(x) (x).begin(), (x).end()
#define SZ(x) ((lint)(x).size())
#define POW2(n) (1LL << (n))
#define FOR(i, begin, end)                                                     \\
  for (lint i = (begin), i##_end_ = (end); i < i##_end_; i++)
#define IFOR(i, begin, end)                                                    \\
  for (lint i = (end)-1, i##_begin_ = (begin); i >= i##_begin_; i--)
#define REP(i, n) FOR(i, 0, n)
#define IREP(i, n) IFOR(i, 0, n)
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T1, typename T2>
pair<T1, T2> operator+(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first + r.first, l.second + r.second);
}
template <typename T1, typename T2>
pair<T1, T2> operator-(const pair<T1, T2> &l, const pair<T1, T2> &r) {
  return make_pair(l.first - r.first, l.second - r.second);
}
const lint MOD = 1e9 + 7, INF = 1e16;

lint dpL[200001][3], dpR[200001][3];
lint dp2[200001][3];
lint N, cnt = 0;
string s;
lint dp3[200001];

lint dfs(lint i) {
  if (dp3[i] != INF)
    return dp3[i];
  lint b_cnt = 0, curr = i;
  while (curr > 0) {
    if (curr % 2 == 1)
      b_cnt++;
    curr /= 2;
  }
  if (b_cnt == 0)
    return 0;
  return dp3[i] = dfs(i % b_cnt) + 1;
}

int main() {
  cin >> N;
  cin >> s;
  REP(i, N) {
    if (s[i] == '1')
      cnt++;
  }
  REP(i, 3) {
    dpL[0][i] = 0;
    dpR[N][i] = 0;
  }
  REP(i, N) {
    dpL[i + 1][0] = (dpL[i][0] * 2 + (s[i] - '0')) % (cnt - 1);
    dpL[i + 1][1] = (dpL[i][1] * 2 + (s[i] - '0')) % cnt;
    dpL[i + 1][2] = (dpL[i][2] * 2 + (s[i] - '0')) % (cnt + 1);
  }
  dp2[N][0] = 1 % (cnt - 1);
  dp2[N][1] = 1 % (cnt - 0);
  dp2[N][2] = 1 % (cnt + 1);
  IREP(i, N) {
    dp2[i][0] = (dp2[i + 1][0] * 2) % (cnt - 1);
    dp2[i][1] = (dp2[i + 1][1] * 2) % (cnt);
    dp2[i][2] = (dp2[i + 1][2] * 2) % (cnt + 1);
  }
  IREP(i, N) {
    dpR[i][0] = (dpR[i + 1][0] + (s[i] - '0') * dp2[i + 1][0]) % (cnt - 1);
    dpR[i][1] = (dpR[i + 1][1] + (s[i] - '0') * dp2[i + 1][1]) % cnt;
    dpR[i][2] = (dpR[i + 1][2] + (s[i] - '0') * dp2[i + 1][2]) % (cnt + 1);
  }
  REP(i, N) { dp3[i] = INF; }
  dp3[0] = 0;
  REP(i, N + 1) { dfs(i); }
  REP(i, N) {
    if (s[i] == '0') {
      int a = (dpL[i + 1][2] * dp2[i + 1][2] + dp2[i + 1][2] + dpR[i + 1][2]) %
              (cnt + 1);
      cout << dp3[a] + 1 << endl;
    } else {
      int a = (dpL[i][0] * dp2[i][0] + dpR[i + 1][0]) % (cnt - 1);
      cout << dp3[a] + 1 << endl;
    }
  }
}