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

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int64_t N;
  string S;
  cin >> N >> S;
  vector<int64_t> digits(N);
  int64_t original_pop_count = 0;
  for (int64_t i = 0; i < N; ++i) {
    digits[i] = static_cast<int64_t>(S[i] - '0');
    if (digits[i] == 1) {
      ++original_pop_count;
    }
  }
  const int64_t plus_pop_count = original_pop_count + 1;
  const int64_t minus_pop_count = original_pop_count - 1;

  int64_t original_plus = 0;
  int64_t original_minus = 0;
  if (plus_pop_count > 1) {
    for (int64_t i = 0; i < N; ++i) {
      original_plus *= 2;
      original_plus += digits[i];
      original_plus %= plus_pop_count;
    }
  }
  if (minus_pop_count > 1) {
    for (int64_t i = 0; i < N; ++i) {
      original_minus *= 2;
      original_minus += digits[i];
      original_minus %= minus_pop_count;
    }
  }

  auto PopCount = [&](int64_t n) -> int64_t {
    int64_t result = 0;
    while (n > 0) {
      result += (n & 1L);
      n = (n >> 1L);
    }
    return result;
  };
  auto F = [&](auto &&Func, const int64_t n) -> int64_t {
    if (n == 0) {
      return 0;
    }
    const int64_t p = PopCount(n);
    if (p == 1) {
      return 1;
    }
    int64_t next = n % p;
    const int64_t result = Func(Func, next) + 1;
    return result;
  };

  int64_t diff_plus = 1;
  int64_t diff_minus = 1;
  vector<int64_t> results(N, 0);
  for (int64_t i = N - 1; i >= 0; --i) {
    int64_t pop_count = (digits[i] == 0) ? plus_pop_count : minus_pop_count;
    if (pop_count <= 0) {
      results[i] = 0;
      continue;
    }
    int64_t next_val = 0;
    if (digits[i] == 0) {
      next_val = original_plus + diff_plus;
      next_val %= pop_count;
    } else {
      next_val = original_minus - diff_minus;
      next_val %= pop_count;
      next_val += pop_count;
      next_val %= pop_count;
    }
    results[i] = F(F, next_val) + 1;

    if (plus_pop_count > 0) {
      diff_plus *= 2;
      diff_plus %= plus_pop_count;
    }
    if (minus_pop_count > 0) {
      diff_minus *= 2;
      diff_minus %= minus_pop_count;
    }
  }
  for (const auto &r : results) {
    cout << r << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

/**
 * @copyright (c) 2020 Daisuke Hashimoto
 */

#include <bits/stdc++.h>
using namespace std;
using Pair = pair<int64_t, int64_t>;
constexpr int64_t kInf = INT64_MAX / 2L;

// std::cout << std::setprecision(20) << 1.1 << endl;

int64_t Popcount(int64_t n) {
  int64_t result = 0;
  while (n > 0) {
    result += n & 1;
    n = n >> 1;
  }
  return result;
}

int64_t Popcount(const string X) {
  int64_t L = static_cast<int64_t>(X.length());
  int64_t result = 0;
  for (int64_t i = 0; i < L; ++i) {
    if (X[i] == '1') {
      ++result;
    }
  }
  return result;
}

// int64_t GetDigit(const int64_t n, const int64_t i, const int64_t N) {
//   const int64_t k = N - i;
//   return ((n >> k) & 1);
// }

int64_t GetNumberOfDigits(const int64_t N) {
  // 桁数の取得
  constexpr int64_t kBase = 10;
  int64_t result;
  if (N == 0) {
    result = 1;
  } else {
    result = 0;
    int64_t temp = N;
    while (temp != 0) {
      ++result;
      temp /= kBase;
    }
  }
  return result;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int64_t N;
  string X;
  cin >> N >> X;
  const int64_t original_pop = Popcount(X);

  vector<int64_t> pops(N + 1, -1);
  vector<int64_t> anss(N + 1, -1);

  auto Dfs = [&](auto &&Func, const int64_t n) -> int64_t {
    if (n == 0) {
      return 0;
    } else if (anss[n] >= 0) {
      return anss[n];
    } else if (pops[n] < 0) {
      pops[n] = Popcount(n);
    }
    anss[n] = Func(Func, n % pops[n]) + 1;
    return anss[n];
  };

  for (int64_t i = 1; i <= N; ++i) {
    int64_t po = original_pop;
    if (X[i - 1] == '0') {
      ++po;
    } else {
      --po;
    }
    int64_t next_n = 0;
    if (po > 1) {
      for (int64_t j = 1; j <= N; ++j) {
        int64_t k = N - j;
        int64_t current_d = static_cast<int64_t>(X[j - 1] - '0');
        if (i == j) {
          current_d = 1L - current_d;
        }
        int64_t temp = 0;
        if (current_d > 0) {
          k %= (po - 1);
          temp = (1 << k);
        }
        next_n += temp;
      }
      next_n %= po;
    }
    // cerr << i << ", next_n=" << next_n << endl;
    if (po == 0) {
      cout << 0 << endl;
    } else {
      cout << Dfs(Dfs, next_n) + 1 << endl;
    }
  }
  return 0;
}