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

#line 1 "/workspaces/compro/lib/template.hpp"

#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T>
std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes, T no) {
  cout << (val ? yes : no) << "\\n";
}

void say(bool val, string yes = "Yes", string no = "No") {
  say<string>(val, yes, no);
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// C++ 17に完全移行したら消す
// 最大公約数を求める
template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; }

// 最小公倍数を求める
template <class T> T lcm(T n, T m) {
  int g = gcd(n, m);
  return n * m / g;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

#line 2 "aising2020_d.cpp"

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

  int n;
  cin >> n;
  string x;
  cin >> x;
  int popX = 0;
  REP(i, n) {
    if (x[i] == '1')
      popX++;
  }

  if (popX == 0) {
    REP(i, n) { cout << 1 << endl; }
    return 0;
  }

  if (popX == 1) {
    REP(i, n) {
      if (x[i] == '1') {
        cout << 0 << endl;
      } else {
        if (i == n - 1) {
          cout << 2 << endl;
        } else {
          cout << 1 << endl;
        }
      }
    }
    return 0;
  }

  vi modp(n, 0), modm(n, 0);
  int modpSum = 0, modmSum = 0;
  {
    int tmpp = 1;
    int tmpm = 1;
    REP(i, n) {
      modp[n - 1 - i] = tmpp % (popX + 1);
      modm[n - 1 - i] = tmpm % (popX - 1);

      tmpp = (tmpp * 2) % (popX + 1);
      tmpm = (tmpm * 2) % (popX - 1);
    }
  }

  REP(i, n) {
    if (x[i] == '1') {
      modpSum = (modpSum + modp[i]) % (popX + 1);
      modmSum = (modmSum + modm[i]) % (popX - 1);
    }
  }

  REP(i, n) {
    int ans = 1;
    int sum = 0;
    if (x[i] == '0') {
      sum = (modpSum + modp[i]) % (popX + 1);
    } else {
      sum = (modmSum - modm[i] + popX - 1) % (popX - 1);
    }

    while (sum > 0) {
      ans++;
      sum = sum % __builtin_popcount(sum);
    }
    cout << ans << endl;
  }
}


Error: 0

Fixed submission:

#line 1 "/workspaces/compro/lib/template.hpp"

#line 1 "/workspaces/compro/lib/io/vector.hpp"
#include <iostream>
#include <vector>

#ifndef IO_VECTOR
#define IO_VECTOR

template <class T>
std::ostream &operator<<(std::ostream &out, const std::vector<T> &v) {
  int size = v.size();
  for (int i = 0; i < size; i++) {
    std::cout << v[i];
    if (i != size - 1)
      std::cout << " ";
  }
  return out;
}

template <class T>
std::istream &operator>>(std::istream &in, std::vector<T> &v) {
  for (auto &el : v) {
    std::cin >> el;
  }
  return in;
}

#endif
#line 4 "/workspaces/compro/lib/template.hpp"
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>
#define MM << " " <<

using namespace std;

template <class T> void say(bool val, T yes, T no) {
  cout << (val ? yes : no) << "\\n";
}

void say(bool val, string yes = "Yes", string no = "No") {
  say<string>(val, yes, no);
}

template <class T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

template <class T> void chmax(T &a, T b) {
  if (a < b)
    a = b;
}

// C++ 17に完全移行したら消す
// 最大公約数を求める
template <class T> T gcd(T n, T m) { return n ? gcd(m % n, n) : m; }

// 最小公倍数を求める
template <class T> T lcm(T n, T m) {
  int g = gcd(n, m);
  return n * m / g;
}

// 重複を消す。計算量はO(NlogN)
template <class T> void unique(std::vector<T> &v) {
  std::sort(v.begin(), v.end());
  v.erase(std::unique(v.begin(), v.end()), v.end());
}

#line 2 "aising2020_d.cpp"

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

  int n;
  cin >> n;
  string x;
  cin >> x;
  int popX = 0;
  REP(i, n) {
    if (x[i] == '1')
      popX++;
  }

  if (popX == 0) {
    REP(i, n) { cout << 1 << endl; }
  }

  vi modp(n), modm(n);
  int modpSum = 0, modmSum = 0;
  {
    int tmpp = 1;
    int tmpm = 1;
    REP(i, n) {
      modp[n - 1 - i] = tmpp % (popX + 1);
      modm[n - 1 - i] = tmpm % (popX - 1);

      tmpp = (tmpp * 2) % (popX + 1);
      tmpm = (tmpm * 2) % (popX - 1);
    }
  }

  REP(i, n) {
    if (x[i] == '1') {
      modpSum += modp[i];
      modmSum += modm[i];
    }
  }

  REP(i, n) {
    int ans = 1;
    int sum;
    if (x[i] == '0') {
      sum = (modpSum + modp[i]) % (popX + 1);
    } else {
      sum = (modmSum - modm[i]) % (popX - 1);
    }

    while (sum != 0) {
      ans++;
      sum = sum % __builtin_popcount(sum);
    }
    cout << ans << endl;
  }
}