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

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9 + 7;

vector<int> memo(2 * 1e5 + 5, -1);

int solve(int n) {
  if (memo[n] != -1) {
    return memo[n];
  }
  int pcnt = 0;
  REP(i, 30) {
    if (n >> i & 1) {
      pcnt++;
    }
  }
  return memo[n] = (solve(n % pcnt) + 1);
}

int main() {
  memo[0] = 0;
  int N;
  cin >> N;
  string S;
  cin >> S;

  int pcnt = 0;
  int X = 0;
  int beki = 1;
  REP(i, min(N, 17)) {
    if (S[N - 1 - i] == '1') {
      X += beki;
    }
    beki *= 2;
  }
  REP(i, N) {
    if (S[i] == '1') {
      pcnt++;
    }
  }

  int modm = pcnt - 1, modp = pcnt + 1;
  vector<int> bekim(N + 1, 0), bekip(N + 1, 0);
  int summ = 0, sump = 0;
  if (modm != 0) {
    beki = 1;
    REPR(i, N - 1) {
      bekim[i] = beki;
      if (S[i] == '1') {
        summ += beki;
        summ %= modm;
      }
      beki *= 2;
      beki %= modm;
    }
  }

  beki = 1;
  REPR(i, N - 1) {
    bekip[i] = beki;
    if (S[i] == '1') {
      sump += beki;
      sump %= modp;
    }
    beki *= 2;
    beki %= modp;
  }

  // cout << "modm:" << modm << " modp:" << modp << endl;
  // cout << "summ:" << summ << " sump:" << sump << endl;
  REP(i, N) {
    int ans = 1;
    int Y;
    if (S[i] == '1' && modm != 0) {
      pcnt--;
      S[i] = '0';
      Y = summ - bekim[i];
      if (Y < 0) {
        Y += modm;
      }
      Y %= modm;
    } else if (S[i] == '1' && modm == 0) {
      cout << 0 << endl;
      continue;
    } else {
      pcnt++;
      S[i] = '1';
      Y = sump + bekip[i];
      Y %= modp;
    }
    if (Y == 1 && pcnt == 1 && S[N - 1] == '1') {
      ans = 0;
    }

    // cout << "Y:" << Y << " ";
    if (Y == 0 && pcnt == 0) {
      ans = 0;
    } else if (S[i] == '0') {
      ans = solve(Y % modm) + 1;
    } else {
      ans = solve(Y % modp) + 1;
    }
    cout << ans << endl;
    if (S[i] == '0') {
      S[i] = '1';
      pcnt++;
    } else {
      S[i] = '0';
      pcnt--;
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

#define REP(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define REPR(i, n) for (int(i) = (n); (i) >= 0; --(i))
#define FOR(i, n, m) for (int(i) = (n); (i) < (m); ++(i))

constexpr int INF = 1e9;
// constexpr ll INF = 1LL<<61;
constexpr ll mod = 1e9 + 7;

vector<int> memo(2 * 1e5 + 5, -1);

int solve(int n) {
  if (memo[n] != -1) {
    return memo[n];
  }
  int pcnt = 0;
  REP(i, 30) {
    if (n >> i & 1) {
      pcnt++;
    }
  }
  return memo[n] = (solve(n % pcnt) + 1);
}

int main() {
  memo[0] = 0;
  int N;
  cin >> N;
  string S;
  cin >> S;

  int pcnt = 0;
  int X = 0;
  int beki = 1;
  REP(i, min(N, 17)) {
    if (S[N - 1 - i] == '1') {
      X += beki;
    }
    beki *= 2;
  }
  REP(i, N) {
    if (S[i] == '1') {
      pcnt++;
    }
  }

  int modm = pcnt - 1, modp = pcnt + 1;
  vector<int> bekim(N + 1, 0), bekip(N + 1, 0);
  int summ = 0, sump = 0;
  if (modm != 0) {
    beki = 1;
    REPR(i, N - 1) {
      bekim[i] = beki;
      if (S[i] == '1') {
        summ += beki;
        summ %= modm;
      }
      beki *= 2;
      beki %= modm;
    }
  }

  beki = 1;
  REPR(i, N - 1) {
    bekip[i] = beki;
    if (S[i] == '1') {
      sump += beki;
      sump %= modp;
    }
    beki *= 2;
    beki %= modp;
  }

  // cout << "modm:" << modm << " modp:" << modp << endl;
  // cout << "summ:" << summ << " sump:" << sump << endl;
  REP(i, N) {
    int ans = 1;
    int Y;
    if (S[i] == '1') {
      pcnt--;
      S[i] = '0';
      Y = summ - bekim[i];
      if (Y < 0) {
        Y += modm;
      }
      Y %= modm;
    } else {
      pcnt++;
      S[i] = '1';
      Y = sump + bekip[i];
      Y %= modp;
    }
    if (Y == 1 && pcnt == 1 && S[N - 1] == '1') {
      ans = 0;
    }

    // cout << "Y:" << Y << " ";
    if (Y == 0 && pcnt == 0) {
      ans = 0;
    } else if (S[i] == '0') {
      ans = solve(Y % modm) + 1;
    } else {
      ans = solve(Y % modp) + 1;
    }
    cout << ans << endl;
    if (S[i] == '0') {
      S[i] = '1';
      pcnt++;
    } else {
      S[i] = '0';
      pcnt--;
    }
  }
  return 0;
}