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

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

int solve(int popcnt, string X) {
  int rem = 0;

  for (int i = 0; i < X.size(); i++) {
    rem = rem * 2;
    if (X[i] == '1') {
      rem = rem + 1;
    }

    rem = rem % popcnt;
  }

  return rem;
}

int main() {
  int N;
  string X;

  cin >> N;
  cin >> X;

  int popcnt = 0;

  int dp[200005];
  dp[0] = 0;

  const int MAX_NUM = N;

  for (int i = 1; i <= MAX_NUM; i++) {
    int cnt = __builtin_popcount(i);
    dp[i] = dp[i % cnt] + 1;
  }

  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      popcnt++;
    }
  }

  int dpRem[5][200005];
  const int PLUS_ONE = 1;
  const int MIN_ONE = 0;
  dpRem[PLUS_ONE][0] = 1 % (popcnt + 1);
  dpRem[MIN_ONE][0] = popcnt > 1 ? 1 % (popcnt - 1) : 0;

  for (int i = 1; i <= N; i++) {
    dpRem[PLUS_ONE][i] = (dpRem[PLUS_ONE][i - 1] * 2) % (popcnt + 1);
    dpRem[MIN_ONE][i] =
        popcnt > 1 ? (dpRem[MIN_ONE][i - 1] * 2) % (popcnt - 1) : 0;
  }

  int plusOne = -1;
  int minOne = -1;

  for (int i = 0; i < N; i++) {
    int ans;
    int remind;
    if (X[i] == '1') {
      if (minOne == -1) {
        minOne = popcnt > 1 ? solve(popcnt - 1, X) : 0;
      }
      int reminder = minOne - dpRem[MIN_ONE][N - i - 1];
      while (reminder < 0) {
        reminder += (popcnt - 1);
      }
      if (popcnt - 1 == 0) {
        ans = 0;
      } else {
        reminder %= (popcnt - 1);
        ans = dp[reminder] + 1;
      }
      remind = reminder;
    } else {
      if (plusOne == -1) {
        plusOne = solve(popcnt + 1, X);
      }
      int reminder = plusOne + dpRem[PLUS_ONE][N - i - 1];
      while (reminder < 0) {
        reminder += (popcnt + 1);
      }

      reminder %= (popcnt + 1);
      ans = dp[reminder] + 1;

      remind = reminder;
    }

    cout << ans << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

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

int solve(int popcnt, string X) {
  int rem = 0;

  for (int i = 0; i < X.size(); i++) {
    rem = rem * 2;
    if (X[i] == '1') {
      rem = rem + 1;
    }

    rem = rem % popcnt;
  }

  return rem;
}

int main() {
  int N;
  string X;

  cin >> N;
  cin >> X;

  int popcnt = 0;

  int dp[200005];
  dp[0] = 0;

  const int MAX_NUM = N;

  for (int i = 1; i <= MAX_NUM; i++) {
    int cnt = __builtin_popcount(i);
    dp[i] = dp[i % cnt] + 1;
  }

  for (int i = N - 1; i >= 0; i--) {
    if (X[i] == '1') {
      popcnt++;
    }
  }

  int dpRem[5][200005];
  const int PLUS_ONE = 1;
  const int MIN_ONE = 0;
  dpRem[PLUS_ONE][0] = 1 % (popcnt + 1);
  dpRem[MIN_ONE][0] = 1 % (popcnt - 1);

  for (int i = 1; i <= N; i++) {
    dpRem[PLUS_ONE][i] = (dpRem[PLUS_ONE][i - 1] * 2) % (popcnt + 1);
    dpRem[MIN_ONE][i] = (dpRem[MIN_ONE][i - 1] * 2) % (popcnt - 1);
  }

  int plusOne = -1;
  int minOne = -1;

  for (int i = 0; i < N; i++) {
    int ans;
    int remind;
    if (X[i] == '1') {
      if (minOne == -1) {
        minOne = solve(popcnt - 1, X);
      }
      int reminder = minOne - dpRem[MIN_ONE][N - i - 1];
      while (reminder < 0) {
        reminder += (popcnt - 1);
      }
      reminder %= (popcnt - 1);
      ans = dp[reminder] + 1;
      remind = reminder;
    } else {
      if (plusOne == -1) {
        plusOne = solve(popcnt + 1, X);
      }
      int reminder = plusOne + dpRem[PLUS_ONE][N - i - 1];
      while (reminder < 0) {
        reminder += (popcnt + 1);
      }

      reminder %= (popcnt + 1);
      ans = dp[reminder] + 1;

      remind = reminder;
    }

    cout << ans << endl;
  }

  return 0;
}