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

#include <bits/stdc++.h>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

int main() {

  int n;
  cin >> n;

  string s;
  cin >> s;

  vector<int> count(n + 1);

  for (int i = 1; i <= n; i++) {
    int pcount = __builtin_popcount(i);

    int tmp = i % pcount;
    count[i] = count[tmp] + 1;
  }

  int pcount = 0;

  rep(i, n) {
    if (s[i] == '1')
      pcount++;
  }

  cerr << "popcount : " << pcount << endl;

  if (pcount == 0) {
    rep(i, n) { cout << 1 << endl; }
  } else {
    // mod pcount + 1
    vector<int> modplus(n + 1);
    // mod pcount - 1
    vector<int> modminus(n + 1);

    // pcount - 1 == 0ならなにもしない
    if (pcount - 1 != 0) {
      modminus[1] = 1 % (pcount - 1);

      for (int i = 2; i <= n; i++) {
        modminus[i] = (modminus[i - 1] * 2) % (pcount - 1);
      }
    }

    modplus[1] = 1 % (pcount + 1);

    for (int i = 2; i <= n; i++) {
      modplus[i] = (modplus[i - 1] * 2) % (pcount + 1);
    }

    vector<int> summinus(n + 1);
    vector<int> sumplus(n + 1);

    rep(i, n) {
      if (s[n - 1 - i] == '1') {
        if (pcount - 1 != 0)
          summinus[i + 1] = (summinus[i] + modminus[i + 1]) % (pcount - 1);
        sumplus[i + 1] = (sumplus[i] + modplus[i + 1]) % (pcount + 1);
      } else {
        summinus[i + 1] = summinus[i];
        sumplus[i + 1] = sumplus[i];
      }
    }

    int summi = summinus[n];
    int sumpl = sumplus[n];

    cerr << summi << " " << sumpl << endl;

    rep(i, n) {
      if (s[i] == '0') {
        // plus
        cout << count[(sumpl + modplus[n - i]) % (pcount + 1)] + 1 << endl;
      } else {
        // minus
        if (pcount > 1) {
          cout << count[((summi - modminus[n - i]) < 0
                             ? (summi - modminus[n - i]) + (pcount - 1)
                             : (summi - modminus[n - i])) %
                        (pcount - 1)] +
                      1
               << endl;
        } else {
          cout << 0 << endl;
        }
      }
    }
  }

  return 0;
}


Error: 0
popcount : 2
0 0


Fixed submission:

#include <bits/stdc++.h>

#define rep(X, N) for (ll X = 0LL; X < (N); X++)
#define ALL(V) (V).begin(), (V).end()
#define endl "\\n"

using namespace std;
typedef long long ll;

const double PI = 3.1415926535897932384626;
const ll MODN = 1000000007;
const ll MODN2 = 998244353;
const double EPS = 1e-10;

int main() {

  int n;
  cin >> n;

  string s;
  cin >> s;

  vector<int> count(n + 1);

  for (int i = 1; i <= n; i++) {
    int pcount = __builtin_popcount(i);

    int tmp = i % pcount;
    count[i] = count[tmp] + 1;
  }

  int pcount = 0;

  rep(i, n) {
    if (s[i] == '1')
      pcount++;
  }

  cerr << "popcount : " << pcount << endl;

  if (pcount == 0) {
    rep(i, n) { cout << 1 << endl; }
  } else {
    // mod pcount + 1
    vector<int> modplus(n + 1);
    // mod pcount - 1
    vector<int> modminus(n + 1);

    // pcount - 1 == 0ならなにもしない
    if (pcount - 1 != 0) {
      modminus[1] = 1 % (pcount - 1);

      for (int i = 2; i <= n; i++) {
        modminus[i] = (modminus[i - 1] * 2) % (pcount - 1);
      }
    }

    modplus[1] = 1 % (pcount + 1);

    for (int i = 2; i <= n; i++) {
      modplus[i] = (modplus[i - 1] * 2) % (pcount + 1);
    }

    vector<int> summinus(n + 1);
    vector<int> sumplus(n + 1);

    rep(i, n) {
      if (s[n - 1 - i] == '1') {
        summinus[i + 1] = (summinus[i] + modminus[i + 1]) % (pcount - 1);
        sumplus[i + 1] = (sumplus[i] + modplus[i + 1]) % (pcount + 1);
      } else {
        summinus[i + 1] = summinus[i];
        sumplus[i + 1] = sumplus[i];
      }
    }

    int summi = summinus[n];
    int sumpl = sumplus[n];

    cerr << summi << " " << sumpl << endl;

    rep(i, n) {
      if (s[i] == '0') {
        // plus
        cout << count[(sumpl + modplus[n - i]) % (pcount + 1)] + 1 << endl;
      } else {
        // minus
        cout << count[((summi - modminus[n - i]) < 0 && pcount > 1
                           ? (summi - modminus[n - i]) + (pcount - 1)
                           : (summi - modminus[n - i])) %
                      (pcount - 1)] +
                    1
             << endl;
      }
    }
  }

  return 0;
}