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

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
const int MOOD = 998244353;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

int f[200010];
int amari[200010][2];

int main() {
  int n, popcount = 0;
  string s;
  cin >> n >> s;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '1')
      popcount++;
  }
  for (int i = 1; i <= 200000; i++) {
    int coun = 0;
    for (int j = 0; j <= 18; j++) {
      if (i & (1LL << j))
        coun++;
    }
    f[i] = f[(i % coun)] + 1;
  }

  if (popcount != 1) {
    amari[0][0] = 1 % (popcount - 1);
  }
  amari[0][1] = 1 % (popcount + 1);

  for (int i = 1; i < n; i++) {
    if (popcount != 1) {
      amari[i][0] = (amari[i - 1][0] * 2) % (popcount - 1);
    }
    amari[i][1] = (amari[i - 1][1] * 2) % (popcount + 1);
  }
  int defamari0 = 0;
  int defamari1 = 0;

  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      defamari0 += amari[s.length() - 1 - i][0];
      defamari1 += amari[s.length() - 1 - i][1];
      if (popcount != 1) {
        defamari0 %= (popcount - 1);
      }
      defamari1 %= (popcount + 1);
    }
  }
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '0') {
      int newamari = defamari1 + amari[s.length() - 1 - i][1];
      newamari %= (popcount + 1);
      cout << f[newamari] + 1 << endl;
    } else if (s[i] == '1' && popcount != 1) {
      int newamari = defamari0 - amari[s.length() - 1 - i][0];
      newamari %= (popcount + 1);
      if (newamari < 0)
        newamari += (popcount - 1);
      cout << f[newamari] + 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
const int MOD = 1e9 + 7;
const int MOOD = 998244353;
#define PI 3.14159265359
typedef long long ll;
using namespace std;

int f[200010];
int amari[200010][2];

int main() {
  int n, popcount = 0;
  string s;
  cin >> n >> s;
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '1')
      popcount++;
  }
  for (int i = 1; i <= 200000; i++) {
    int coun = 0;
    for (int j = 0; j <= 18; j++) {
      if (i & (1LL << j))
        coun++;
    }
    f[i] = f[(i % coun)] + 1;
  }

  if (popcount != 1) {
    amari[0][0] = 1 % (popcount - 1);
  }
  amari[0][1] = 1 % (popcount + 1);

  for (int i = 1; i < n; i++) {
    if (popcount != 1) {
      amari[i][0] = (amari[i - 1][0] * 2) % (popcount - 1);
    }
    amari[i][1] = (amari[i - 1][1] * 2) % (popcount + 1);
  }
  int defamari0 = 0;
  int defamari1 = 0;

  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      defamari0 += amari[s.length() - 1 - i][0];
      defamari1 += amari[s.length() - 1 - i][1];
      defamari0 %= (popcount - 1);
      defamari1 %= (popcount + 1);
    }
  }
  for (int i = 0; i < s.length(); i++) {
    if (s[i] == '0') {
      int newamari = defamari1 + amari[s.length() - 1 - i][1];
      newamari %= (popcount + 1);
      cout << f[newamari] + 1 << endl;
    } else if (s[i] == '1' && popcount != 1) {
      int newamari = defamari0 - amari[s.length() - 1 - i][0];
      newamari %= (popcount + 1);
      if (newamari < 0)
        newamari += (popcount - 1);
      cout << f[newamari] + 1 << endl;
    } else {
      cout << 0 << endl;
    }
  }

  return 0;
}