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

#ifdef _MSC_VER
#include <__msvc_all_public_headers.hpp>
#undef min
#undef max
#else
#define NDEBUG
#include <bits/stdc++.h>
#endif

using namespace std;
using large = long long;
static vector<int> table;

static int popcount(int x) {
  int cnt = 0;
  while (x) {
    cnt += x & 1;
    x >>= 1;
  }
  return cnt;
}

static int f(int x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return 1;
  if (x == 2)
    return 1;

  auto &m = table[x];
  if (m >= 0)
    return m;

  m = 1 + f(x % popcount(x));
  return m;
}

int main() {
  int n;
  string x;
  cin >> n >> x;

  table = vector<int>(200'005, -1);

  auto pc_orig = count(x.begin(), x.end(), '1');
  auto posplus = vector<int>(200'005, -1);
  auto posminus = vector<int>(200'005, -1);
  auto remplus = 0;
  auto remminus = 0;
  {
    auto plus = 1;
    auto minus = 1;

    for (auto i = n - 1; i >= 0; --i) {
      posplus[i] = plus;
      posminus[i] = minus;

      remplus += (x[i] - '0') * plus;
      remplus %= (pc_orig + 1);
      remminus += (x[i] - '0') * minus;
      remminus %= pc_orig <= 1 ? 1 : (pc_orig - 1);

      plus = plus * 2 % (pc_orig + 1);
      minus = pc_orig <= 1 ? 0 : minus * 2 % (pc_orig - 1);
    }
  }

  for (auto i = 0; i < n; ++i) {
    auto &&pos = x[i] == '0' ? posplus : posminus;
    auto pc = pc_orig + (x[i] == '0' ? 1 : -1);
    auto cnt = 0;
    if (pc > 0) {
      auto rem = 0;
      if (x[i] == '0') {
        rem = remplus;
        rem += posplus[i];
        rem %= pc;
      } else {
        rem = remminus;
        rem += pc;
        rem -= posminus[i];
        rem %= pc;
      }

      cnt = 1 + f(rem);
      // cerr << x << " // " << rem << "\\n";
    }

    cout << cnt << "\\n";
  }

  return 0;
}


Error: 0

Fixed submission:

#ifdef _MSC_VER
#include <__msvc_all_public_headers.hpp>
#undef min
#undef max
#else
#define NDEBUG
#include <bits/stdc++.h>
#endif

using namespace std;
using large = long long;
static vector<int> table;

static int popcount(int x) {
  int cnt = 0;
  while (x) {
    cnt += x & 1;
    x >>= 1;
  }
  return cnt;
}

static int f(int x) {
  if (x == 0)
    return 0;
  if (x == 1)
    return 1;
  if (x == 2)
    return 1;

  auto &m = table[x];
  if (m >= 0)
    return m;

  m = 1 + f(x % popcount(x));
  return m;
}

int main() {
  int n;
  string x;
  cin >> n >> x;

  table = vector<int>(100'005, -1);

  auto pc_orig = count(x.begin(), x.end(), '1');
  auto posplus = vector<int>(100'005, -1);
  auto posminus = vector<int>(100'005, -1);
  auto remplus = 0;
  auto remminus = 0;
  {
    auto plus = 1;
    auto minus = 1;

    for (auto i = n - 1; i >= 0; --i) {
      posplus[i] = plus;
      posminus[i] = minus;

      remplus += (x[i] - '0') * plus;
      remplus %= (pc_orig + 1);
      remminus += (x[i] - '0') * minus;
      remminus %= pc_orig <= 1 ? 1 : (pc_orig - 1);

      plus = plus * 2 % (pc_orig + 1);
      minus = pc_orig <= 1 ? 0 : minus * 2 % (pc_orig - 1);
    }
  }

  for (auto i = 0; i < n; ++i) {
    auto &&pos = x[i] == '0' ? posplus : posminus;
    auto pc = pc_orig + (x[i] == '0' ? 1 : -1);
    auto cnt = 0;
    if (pc > 0) {
      auto rem = 0;
      if (x[i] == '0') {
        rem = remplus;
        rem += posplus[i];
        rem %= pc;
      } else {
        rem = remminus;
        rem += pc;
        rem -= posminus[i];
        rem %= pc;
      }

      cnt = 1 + f(rem);
      // cerr << x << " // " << rem << "\\n";
    }

    cout << cnt << "\\n";
  }

  return 0;
}