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

#include <algorithm>
#include <chrono>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, a, b) for (ll i = a; i > b; i--)
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (1); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = (1LL << 31) - 1;
const ll mod = 1e9 + 7;

ll N;
ll M[200005];
ll ans[200005];
ll bitcnt;
string S;
ll num;

int init(int k) {
  if (M[k] != -1) {
    return M[k];
  } else {
    int cnt = __builtin_popcount(k);
    M[k] = 1 + init(k % cnt);
    return M[k];
  }
}

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

  FOR(i, 0, 200001) M[i] = -1;
  M[0] = 0;
  M[1] = 1;
  M[2] = 1;
  FOR(i, 3, 200001) { init(i); }

  cin >> N;
  cin >> S;
  FOR(i, 0, N) {
    if (S[N - 1 - i] == '1') {
      bitcnt++;
    }
  }

  ll bp = 1, bm = 1;

  ll modNp = 0;
  ll modNm = 0;

  FOR(i, 0, N) {
    if (S[N - 1 - i] == '1') {
      modNp = (modNp + bp) % (bitcnt + 1);
      if (bitcnt > 1)
        modNm = (modNm + bm) % (bitcnt - 1);
    }
    bp = (bp * 2) % (bitcnt + 1);
    if (bitcnt > 1)
      bm = (bm * 2) % (bitcnt - 1);
  }

  bp = 1, bm = 1;
  FOR(i, 0, N) {
    ll cur = 0;
    if (S[N - 1 - i] == '1') {
      if (bitcnt == 1) {
        ans[N - 1 - i] = 0;
        continue;
      }
      cur = (modNm - bm + bitcnt - 1) % (bitcnt - 1);
    } else {
      cur = (modNp + bp + bitcnt + 1) % (bitcnt + 1);
    }

    ans[N - 1 - i] = M[cur] + 1;

    bp = (bp * 2) % (bitcnt + 1);
    if (bitcnt > 1)
      bm = (bm * 2) % (bitcnt - 1);
  }

  FOR(i, 0, N) cout << ans[i] << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <chrono>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
#define FOR(i, a, b) for (ll i = a; i < b; i++)
#define REP(i, a, b) for (ll i = a; i > b; i--)
#define CST(x) cout << fixed << setprecision(x) // 小数点以下の桁数指定
#define ct(a) cout << a << endl
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repl(i, l, r) for (int i = (1); i < (r); i++)
#define per(i, n) for (int i = ((n)-1); i >= 0; i--)
static const double pi = 3.141592653589793;
using namespace std;
typedef long long ll;
const ll MOD = 998244353;
const ll INF = (1LL << 31) - 1;
const ll mod = 1e9 + 7;

ll N;
ll M[200001];
ll ans[200001];
ll bitcnt;
string S;
ll num;

int init(int k) {
  if (M[k] != -1) {
    return M[k];
  } else {
    int cnt = __builtin_popcount(k);
    M[k] = 1 + init(k % cnt);
    return M[k];
  }
}

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

  FOR(i, 0, 200001) M[i] = -1;
  M[0] = 0;
  M[1] = 1;
  M[2] = 1;
  FOR(i, 3, 200001) { init(i); }

  cin >> N;
  cin >> S;
  FOR(i, 0, N) {
    if (S[N - 1 - i] == '1') {
      bitcnt++;
    }
  }

  ll bp = 1, bm = 1;

  ll modNp = 0;
  ll modNm = 0;

  FOR(i, 0, N) {
    if (S[N - 1 - i] == '1') {
      modNp = (modNp + bp) % (bitcnt + 1);
      modNm = (modNm + bm) % (bitcnt - 1);
    }
    bp = (bp * 2) % (bitcnt + 1);
    bm = (bm * 2) % (bitcnt - 1);
  }

  bp = 1, bm = 1;
  FOR(i, 0, N) {
    ll cur = 0;
    if (S[N - 1 - i] == '1') {
      cur = (modNm - bm + bitcnt - 1) % (bitcnt - 1);
    } else {
      cur = (modNp + bp + bitcnt + 1) % (bitcnt + 1);
    }
    // cout << " " << cur << endl;
    ans[N - 1 - i] = M[cur] + 1;

    bp = (bp * 2) % (bitcnt + 1);
    bm = (bm * 2) % (bitcnt - 1);
  }

  FOR(i, 0, N) cout << ans[i] << endl;
}