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

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

void solve(long long n, std::string x) {
  vector<int> res(300000);
  for (int i = 0; i < 300000; ++i) {
    int y = i;
    int cnt = 0;
    while (y) {
      y = y % __builtin_popcount(y);
      ++cnt;
    }
    res[i] = cnt;
  }

  vector<int> mod(3, 0);
  int pcnt = 0;

  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      ++pcnt;
    }
  }

  vector<vector<int>> add_mod(n, vector<int>(3, 1));
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      if (pcnt + j - 1 < 2)
        continue;
      add_mod[i][j] = (add_mod[i - 1][j] * 2) % (pcnt + j - 1);
    }
  }

  auto update = [&](int i, bool fl) {
    for (int j = 0; j < 3; ++j) {
      if (pcnt + j - 1 < 2)
        continue;
      if (fl) {
        mod[j] += add_mod[i][j];
        mod[j] %= (pcnt + j - 1);
      } else {
        mod[j] += (pcnt + j - 1) - add_mod[i][j];
        mod[j] %= (pcnt + j - 1);
      }
    }
  };

  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      update(i, true);
    }
  }

  for (int i = n - 1; i >= 0; --i) {
    auto pre = mod;
    if (x[i] == '1')
      update(i, false);
    else
      update(i, true);
    int ans = [&] {
      int pc = pcnt + (x[i] == '0' ? 1 : -1);
      if (pc == 0)
        return 0;
      else if (pc == 1)
        return 1;
      else
        return res[mod[x[i] == '0' ? 2 : 0]] + 1;
    }();
    printf("%d\\n", ans);
    mod = move(pre);
  }
}

signed main() {
  long long N;
  scanf("%lld", &N);
  std::string X;
  std::cin >> X;
  reverse(X.begin(), X.end());
  solve(N, X);
  return 0;
}


Error: 0

Fixed submission:

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools

#include <bits/stdc++.h>

using namespace std;

using i64 = long long;

const i64 MOD = 1e9 + 7;
const i64 INF = i64(1e18) + 7;

template <typename T> bool chmin(T &x, T y) {
  if (x > y) {
    x = y;
    return true;
  }
  return false;
}

template <typename T> bool chmax(T &x, T y) {
  if (x < y) {
    x = y;
    return true;
  }
  return false;
}

void solve(long long n, std::string x) {
  vector<int> res(300000);
  for (int i = 0; i < 300000; ++i) {
    int y = i;
    int cnt = 0;
    while (y) {
      y = y % __builtin_popcount(y);
      ++cnt;
    }
    res[i] = cnt;
  }

  vector<int> mod(3, 0);
  int pcnt = 0;

  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      ++pcnt;
    }
  }

  vector<vector<int>> add_mod(n, vector<int>(3, 1));
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < 3; ++j) {
      add_mod[i][j] = (add_mod[i - 1][j] * 2) % (pcnt + j - 1);
    }
  }

  auto update = [&](int i, bool fl) {
    for (int j = 0; j < 3; ++j) {
      if (fl) {
        mod[j] += add_mod[i][j];
        mod[j] %= (pcnt + j - 1);
      } else {
        mod[j] += (pcnt + j - 1) - add_mod[i][j];
        mod[j] %= (pcnt + j - 1);
      }
    }
  };

  for (int i = 0; i < n; ++i) {
    if (x[i] == '1') {
      update(i, true);
    }
  }

  for (int i = n - 1; i >= 0; --i) {
    auto pre = mod;
    if (x[i] == '1')
      update(i, false);
    else
      update(i, true);
    int ans = [&] {
      int pc = pcnt + (x[i] == '0' ? 1 : -1);
      if (pc == 0)
        return 0;
      else if (pc == 1)
        return 1;
      else
        return res[mod[x[i] == '0' ? 2 : 0]] + 1;
    }();
    printf("%d\\n", ans);
    mod = move(pre);
  }
}

signed main() {
  long long N;
  scanf("%lld", &N);
  std::string X;
  std::cin >> X;
  reverse(X.begin(), X.end());
  solve(N, X);
  return 0;
}