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

// 問題の URL を書いておく
//

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

// #define ENABLE_PRINT

#if defined(ENABLE_PRINT)

#define Print(v)                                                               \\
  do {                                                                         \\
    cout << #v << ": " << v << endl;                                           \\
  } while (0)

#define PrintVec(v)                                                            \\
  do {                                                                         \\
    for (int __i = 0; __i < v.size(); ++__i) {                                 \\
      cout << #v << "[" << __i << "]: " << v[__i] << endl;                     \\
    }                                                                          \\
  } while (0)

#else

#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)

#endif

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main(int, const char **) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> v(n);
  rep(i, n) { v[i] = s[i] - '0'; }
  int pc = 0;
  rep(i, n) { pc += v[i]; }

  vector<int> baseMods(2);
  vector<vector<int>> mods(2, vector<int>(n));
  int pcs[] = {pc + 1, pc - 1};
  mods[0][n - 1] = 1 % pcs[0];
  mods[1][n - 1] = pcs[1] > 0 ? 1 % pcs[1] : 0;
  rep(i, 2) {
    baseMods[i] = 0;
    rep(j, n) {
      baseMods[i] = pcs[i] > 0 ? (baseMods[i] * 2 + v[j]) % pcs[i] : 0;
    }
    rep(j, n - 1) {
      int index = n - j - 2;
      mods[i][index] = pcs[i] > 0 ? (mods[i][index + 1] * 2) % pcs[i] : 0;
    }
  }

  vector<int> smods(n);
  rep(i, n) {
    if (v[i] == 0) {
      smods[i] = (baseMods[0] + mods[0][i]) % pcs[0];
    } else {
      smods[i] = pcs[1] > 0 ? (baseMods[1] - mods[1][i] + pcs[1]) % pcs[1] : -1;
    }
  }

  rep(i, n) {
    int count = 1;
    auto m = smods[i];
    if (m == -1) {
      cout << 0 << endl;
      continue;
    }
    while (m != 0) {
      bitset<64> bs(m);
      auto p = bs.count();
      m %= p;
      count++;
    }
    cout << count << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

// 問題の URL を書いておく
//

#include <algorithm>
#include <array>
#include <bitset>
#include <cmath>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <vector>

using namespace std;

// #define ENABLE_PRINT

#if defined(ENABLE_PRINT)

#define Print(v)                                                               \\
  do {                                                                         \\
    cout << #v << ": " << v << endl;                                           \\
  } while (0)

#define PrintVec(v)                                                            \\
  do {                                                                         \\
    for (int __i = 0; __i < v.size(); ++__i) {                                 \\
      cout << #v << "[" << __i << "]: " << v[__i] << endl;                     \\
    }                                                                          \\
  } while (0)

#else

#define Print(v) ((void)0)
#define PrintVec(v) ((void)0)

#endif

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

int main(int, const char **) {
  int n;
  cin >> n;
  string s;
  cin >> s;
  vector<int> v(n);
  rep(i, n) { v[i] = s[i] - '0'; }
  int pc = 0;
  rep(i, n) { pc += v[i]; }

  vector<int> baseMods(2);
  vector<vector<int>> mods(2, vector<int>(n));
  int pcs[] = {pc + 1, pc - 1};
  mods[0][n - 1] = 1 % pcs[0];
  mods[1][n - 1] = 1 % pcs[1];
  rep(i, 2) {
    baseMods[i] = 0;
    rep(j, n) { baseMods[i] = (baseMods[i] * 2 + v[j]) % pcs[i]; }
    rep(j, n - 1) {
      int index = n - j - 2;
      mods[i][index] = (mods[i][index + 1] * 2) % pcs[i];
    }
  }

  vector<int> smods(n);
  rep(i, n) {
    if (v[i] == 0) {
      smods[i] = (baseMods[0] + mods[0][i]) % pcs[0];
    } else {
      smods[i] = (baseMods[1] - mods[1][i] + pcs[1]) % pcs[1];
    }
  }

  rep(i, n) {
    int count = 1;
    auto m = smods[i];
    while (m != 0) {
      bitset<64> bs(m);
      auto p = bs.count();
      m %= p;
      count++;
    }
    cout << count << endl;
  }
  return 0;
}