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

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
typedef long long ll;
//	Table is the complete reverse of dp
//	Always think of the pattern (LCM, GCD)
//	Be careful of unsigned
//	double epsilon = FLT_EPSILON;
//	Think of a solution that gets TLE and optimize it
int MOD;
long long fast_power(long long base, long long power) {
  long long result = 1;
  while (power > 0) {
    if (power & 1) {
      result = ((result % MOD) * (base % MOD)) % MOD;
    }
    base = ((base % MOD) * (base % MOD)) % MOD;
    power >>= 1;
  }
  return result;
}
int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;
  const int N = 2e5 + 1;
  int ans[N];
  ans[0] = 0;
  for (int i = 1; i < N; i++) {
    int cnt = 0;
    for (int j = 0; j < 18; j++)
      if (i & (1 << j))
        cnt++;
    ans[i] = 1 + ans[i % cnt];
  }
  int cnt = 0;
  for (char &i : s)
    if (i == '1')
      cnt++;
  int mod[2], tmp = 0;
  cnt++;
  for (int i = 0; i < n; i++) {
    tmp <<= 1;
    tmp += s[i] - '0';
    if (tmp >= cnt)
      tmp -= cnt;
  }
  mod[0] = tmp;
  cnt -= 2;
  if (cnt <= 0)
    mod[1] = 0;
  else {
    tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp <<= 1;
      tmp += s[i] - '0';
      if (tmp >= cnt)
        tmp -= cnt;
    }
    mod[1] = tmp;
  }
  cnt++;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      MOD = cnt - 1;
    else
      MOD = cnt + 1;
    if (!MOD) {
      cout << 0 << endl;
      continue;
    }
    tmp = fast_power(2, n - i - 1);
    if (s[i] == '1')
      cout << 1 + ans[((mod[1] - tmp) % MOD + MOD) % MOD];
    else
      cout << 1 + ans[(mod[0] + tmp) % MOD];
    cout << endl;
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define endl '\\n'
typedef long long ll;
//	Table is the complete reverse of dp
//	Always think of the pattern (LCM, GCD)
//	Be careful of unsigned
//	double epsilon = FLT_EPSILON;
//	Think of a solution that gets TLE and optimize it
int MOD;
long long fast_power(long long base, long long power) {
  long long result = 1;
  while (power > 0) {
    if (power & 1) {
      result = ((result % MOD) * (base % MOD)) % MOD;
    }
    base = ((base % MOD) * (base % MOD)) % MOD;
    power >>= 1;
  }
  return result;
}
int main() {
  cin.tie(nullptr);
  cout.tie(nullptr);
  ios_base::sync_with_stdio(false);
  int n;
  string s;
  cin >> n >> s;
  const int N = 2e5 + 1;
  int ans[N];
  memset(ans, 0, sizeof(ans));
  for (int i = 1; i < N; i++) {
    int cnt = 0;
    for (int j = 0; j < 18; j++)
      if (i & (1 << j))
        cnt++;
    ans[i] = 1 + ans[i % cnt];
  }
  int cnt = 0;
  for (char &i : s)
    if (i == '1')
      cnt++;
  int mod[2], tmp = 0;
  cnt++;
  for (int i = 0; i < n; i++) {
    tmp <<= 1;
    tmp += s[i] - '0';
    if (tmp >= cnt)
      tmp -= cnt;
  }
  mod[0] = tmp;
  cnt -= 2;
  tmp = 0;
  for (int i = 0; i < n; i++) {
    tmp <<= 1;
    tmp += s[i] - '0';
    if (tmp >= cnt)
      tmp -= cnt;
  }
  mod[1] = tmp;
  cnt++;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      MOD = cnt - 1;
    else
      MOD = cnt + 1;
    tmp = fast_power(2, n - i - 1);
    if (s[i] == '1')
      cout << 1 + ans[((mod[1] - tmp) % MOD + MOD) % MOD];
    else
      cout << 1 + ans[(mod[0] + tmp) % MOD];
    cout << endl;
  }
}