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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

int F(ll x) {
  int res = 0;
  while (x > 0) {
    res++;
    x %= __builtin_popcount(x);
  }
  return res;
}

int main() {
  // 最初の１回は割ってから総和にする、逆から、ビット反転したものが０の場合に注意
  // popcは２通り持っておく+1がupと-1がdown
  int N;
  cin >> N;
  string X;
  cin >> X;
  ll upTotal = 0;
  ll downTotal = 0;
  int popc = count(X.begin(), X.end(), '1');
  if (popc == 0) {
    rep(i, N) {
      cout << 1 << '\\n';
      return 0;
    }
  } else if (popc == 1) {
    ll upCoef = 1;
    for (int i = N - 1; i >= 0; i--) {
      // 1の位から計算
      if (X[i] == '1') {
        ll upAdd = upCoef % (popc + 1);
        upTotal += upAdd;
      }
      upCoef *= 2;
      upCoef %= (popc + 1);
    }
    vector<int> ans(N);
    upCoef = 1;
    for (int i = N - 1; i >= 0; i--) {
      if (X[i] == '0') {
        int temp = F((upTotal + upCoef) % (popc + 1));
        ans[i] = temp + 1;
      } else {
        ans[i] = 0;
      }
      upCoef *= 2;
      upCoef %= (popc + 1);
    }
    rep(i, N) { cout << ans[i] << '\\n'; }
    return 0;
  }
  // cout << popc << '\\n';
  ll upCoef = 1;
  ll downCoef = 1;
  for (int i = N - 1; i >= 0; i--) {
    // 1の位から計算
    if (X[i] == '1') {
      ll upAdd = upCoef % (popc + 1);
      upTotal += upAdd;
      ll downAdd = downCoef % (popc - 1);
      downTotal += downAdd;
    }
    upCoef *= 2;
    upCoef %= (popc + 1);
    downCoef *= 2;
    downCoef %= (popc - 1);
    // cout << upTotal << '\\n';
  }
  vector<int> ans(N);
  upCoef = 1;
  downCoef = 1;
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '0') {
      int temp = F((upTotal + upCoef) % (popc + 1));
      ans[i] = temp + 1;
    } else {
      if (popc == 1) {
        ans[i] = 0;
      } else {
        int temp = F((downTotal - downCoef) % (popc - 1));
        ans[i] = temp + 1;
      }
    }
    upCoef *= 2;
    upCoef %= (popc + 1);
    downCoef *= 2;
    downCoef %= (popc - 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 < (n); ++i)
#define sz(x) int(x.size())
#define show(x)                                                                \\
  {                                                                            \\
    for (auto i : x) {                                                         \\
      cout << i << " ";                                                        \\
    }                                                                          \\
    cout << endl;                                                              \\
  }
using namespace std;
using ll = long long;
using P = pair<int, int>;

int F(ll x) {
  int res = 0;
  while (x > 0) {
    res++;
    x %= __builtin_popcount(x);
  }
  return res;
}

int main() {
  // 最初の１回は割ってから総和にする、逆から、ビット反転したものが０の場合に注意
  // popcは２通り持っておく+1がupと-1がdown
  int N;
  cin >> N;
  string X;
  cin >> X;
  ll upTotal = 0;
  ll downTotal = 0;
  int popc = count(X.begin(), X.end(), '1');
  // cout << popc << '\\n';
  ll upCoef = 1;
  ll downCoef = 1;
  for (int i = N - 1; i >= 0; i--) {
    // 1の位から計算
    if (X[i] == '1') {
      ll upAdd = upCoef % (popc + 1);
      upTotal += upAdd;
      ll downAdd = downCoef % (popc - 1);
      downTotal += downAdd;
    }
    upCoef *= 2;
    upCoef %= (popc + 1);
    downCoef *= 2;
    downCoef %= (popc - 1);
    // cout << upTotal << '\\n';
  }
  vector<int> ans(N);
  upCoef = 1;
  downCoef = 1;
  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '0') {
      int temp = F((upTotal + upCoef) % (popc + 1));
      ans[i] = temp + 1;
    } else {
      int temp = F((downTotal - downCoef) % (popc - 1));
      ans[i] = temp + 1;
    }
    upCoef *= 2;
    upCoef %= (popc + 1);
    downCoef *= 2;
    downCoef %= (popc - 1);
  }
  rep(i, N) { cout << ans[i] << '\\n'; }
  return 0;
}