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

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge {
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
// using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

int calc(int x) {
  if (x == 0)
    return 0;
  x %= __builtin_popcount(x);
  return 1 + calc(x);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  string X;
  cin >> X;

  reverse(X.begin(), X.end());

  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      cnt++;
    }
  }

  ll sum1 = 0, sum2 = 0;
  ll now1 = 1, now2 = 1;

  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      sum1 = (sum1 + now1) % (cnt + 1);
      if (cnt > 1)
        sum2 = (sum2 + now2) % (cnt - 1);
    }
    now1 = (2 * now1) % (cnt + 1);
    if (cnt > 1) {
      now2 = (2 * now2) % (cnt - 1);
    }
  }

  now1 = 1, now2 = 1;

  vector<int> ans;

  for (int i = 0; i < N; i++) {
    int x = 0;
    if (X[i] == '1') {
      if (cnt > 1) {
        int d = (sum2 - now2 + cnt - 1) % (cnt - 1);
        x = 1 + calc(d);
      }
    } else {
      int d = (sum1 + now1) % (cnt + 1);
      x = 1 + calc(d);
    }
    now1 = (2 * now1) % (cnt + 1);
    if (cnt > 1)
      now2 = (2 * now2) % (cnt - 1);
    ans.push_back(x);
  }

  reverse(ans.begin(), ans.end());

  for (auto it : ans)
    cout << it << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <cassert>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
struct Edge {
  int to;
  int weight;
  Edge(int t, int w) : to(t), weight(w) {}
};
using Graph = vector<vector<Edge>>;
// using Graph = vector<vector<int>>;

const long long INF = 1LL << 60;
const int INT_INF = 1000000000;

int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
// int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1}, dy[8] = {-1, 0, 1, 1, -1, 1, 0, -1};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int N;
  cin >> N;
  string X;
  cin >> X;
  reverse(X.begin(), X.end());
  int cnt = 0;
  for (int i = 0; i < N; i++) {
    if (X[i] == '1') {
      cnt++;
    }
  }

  ll sum1 = 0, sum2 = 0;
  ll now1 = 1, now2 = 1;
  int MOD1 = cnt - 1;
  int MOD2 = cnt + 1;
  vector<ll> mulMOD1(N + 1);
  vector<ll> mulMOD2(N + 1);
  if (X[0] == '1') {
    sum1 = 1, sum2 = 1;
    sum1 %= MOD1, sum2 %= MOD2;
    mulMOD1[0] = sum1, mulMOD2[0] = sum2;
  }

  for (int i = 1; i < N; i++) {
    now1 *= 2, now2 *= 2;
    now1 %= MOD1, now2 %= MOD2;
    mulMOD1[i] = now1, mulMOD2[i] = now2;
    if (X[i] == '1') {
      sum1 += now1, sum2 += now2;
      sum1 %= MOD1, sum2 %= MOD2;
    }
  }

  vector<int> ans;

  for (int i = 0; i < N; i++) {
    int play = 0;
    int popcount = cnt;
    ll Xi;
    if (X[i] == '1' && MOD1 > 0) {
      play++;
      Xi = sum1;
      Xi = ((Xi - mulMOD1[i]) + MOD1) % MOD1;
      popcount = __builtin_popcount(Xi);
      // cerr << "MOD1 : " << MOD1 << endl;
    } else if (MOD2 > 0) {
      play++;
      Xi = sum2;
      Xi = (Xi + mulMOD2[i]) % MOD2;
      popcount = __builtin_popcount(Xi);
      // cerr << "MOD2 : " << MOD2 << endl;
    }
    // cerr << "i : " << i << "  Xi : " << Xi << "  popcount:" << popcount <<
    // endl;
    while (Xi > 0 && popcount > 0) {
      Xi = Xi % popcount;
      // cerr << "Xi: " << Xi << endl;
      popcount = __builtin_popcount(Xi);
      play++;
    }
    ans.push_back(play);
  }

  for (int i = N - 1; i >= 0; i--)
    cout << ans[i] << endl;
  return 0;
}

// mod cntの世界で考える　考えるべきはたかがcnt + 1とcnt - 1のみ