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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

// #define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif

#define equals(a, b) (fabs((a) - (b)) < EPS)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

// #define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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 T> std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}

template <typename T> void print_array(const T &ary, int size) {
  REP(i, size) { cout << ary[i] << " "; }
  cout << endl;
}

int mod;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(12);
  int N;
  cin >> N;
  string X;
  cin >> X;
  string tX = X;
  reverse(tX.begin(), tX.end());
  int cnt = 0;
  REP(i, N) if (X[i] == '1') cnt++;

  VL memo(N, -1);
  memo[0] = 0;
  FOR(i, 1, N) {
    if (memo[i] == -1) {
      memo[i] = memo[i % __builtin_popcount(i)] + 1;
    }
  }
  LL base[3] = {1, 1, 1};
  LL acc[3] = {};
  REP(i, N) {
    if (tX[i] == '1') {
      REP(j, 3) {
        // if(j == 0 and cnt == 0) continue;
        acc[j] += base[j];
        if (cnt + (j - 1) != 0)
          acc[j] %= (cnt + (j - 1));
      }
    }

    REP(j, 3) {
      // if(j == 0 and cnt == 0) continue;
      base[j] *= 2;
      if (cnt + (j - 1) != 0)
        base[j] %= (cnt + (j - 1));
    }
  }
  REP(j, 3) base[j] = 1;

  VL ans;
  REP(i, N) {
    LL tacc;
    if (tX[i] == '0') {
      tacc = acc[2] + base[2];
      tacc %= cnt + 1;

      ans.eb(memo[tacc] + 1);
    } else {
      if (cnt == 1) {
        ans.eb(0);
        continue;
      }
      tacc = acc[0] - base[0];
      tacc = (tacc + (cnt - 1)) % (cnt - 1);

      ans.eb(memo[tacc] + 1);
    }

    REP(j, 3) {
      if (j == 0 and cnt == 0)
        continue;
      base[j] *= 2;
      if (cnt + (j - 1) != 0)
        base[j] %= (cnt + (j - 1));
    }
    dump(i)
  }
  reverse(ans.begin(), ans.end());
  REP(i, N) { cout << ans[i] << "\\n"; }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

// #define DEBUG_ //!!$BDs=P;~$K%3%a%s%H%"%&%H(B!!
#ifdef DEBUG_
#define dump(x) cerr << #x << " = " << (x) << endl;
#else
#define dump(x) ;
#endif

#define equals(a, b) (fabs((a) - (b)) < EPS)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

// #define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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 T> std::string printVector(const std::vector<T> &data) {
  std::stringstream ss;
  std::ostream_iterator<T> out_it(ss, ", ");
  ss << "[";
  std::copy(data.begin(), data.end() - 1, out_it);
  ss << data.back() << "]";
  return ss.str();
}

template <typename T> void print_array(const T &ary, int size) {
  REP(i, size) { cout << ary[i] << " "; }
  cout << endl;
}

int mod;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
const long double PI =
    3.14159265358979323846264338327950288419716939937510582097494459230781640628620899;
int dx[] = {0, 0, -1, 1};
int dy[] = {-1, 1, 0, 0};

signed main(int argc, char const *argv[]) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << setprecision(12);
  int N;
  cin >> N;
  string X;
  cin >> X;
  string tX = X;
  reverse(tX.begin(), tX.end());
  int cnt = 0;
  REP(i, N) if (X[i] == '1') cnt++;

  VL memo(N, -1);
  memo[0] = 0;
  FOR(i, 1, N) {
    if (memo[i] == -1) {
      memo[i] = memo[i % __builtin_popcount(i)] + 1;
    }
  }
  LL base[3] = {1, 1, 1};
  LL acc[3] = {};
  REP(i, N) {
    if (tX[i] == '1') {
      REP(j, 3) {
        if (j == 0 and cnt == 0)
          continue;
        acc[j] += base[j];
        acc[j] %= (cnt + (j - 1));
      }
    }

    if (i == N - 1)
      continue;

    REP(j, 3) {
      if (j == 0 and cnt == 0)
        continue;
      base[j] *= 2;
      base[j] %= (cnt + (j - 1));
    }
  }
  REP(j, 3) base[j] = 1;

  dump("ok")

      VL ans;
  REP(i, N) {
    LL tacc;
    if (tX[i] == '0') {
      tacc = acc[2] + base[2];
      dump(tacc) tacc %= cnt + 1;
    } else {
      tacc = acc[0] - base[0];
      dump(tacc) tacc = (tacc + (cnt - 1)) % (cnt - 1);
    }
    ans.eb(memo[tacc] + 1);
    dump("mid")

        REP(j, 3) {
      if (j == 0 and cnt == 0)
        continue;
      base[j] *= 2;
      base[j] %= (cnt + (j - 1));
    }
    dump(i)
  }
  reverse(ans.begin(), ans.end());
  REP(i, N) { cout << ans[i] << "\\n"; }
}