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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define x first
#define y second
typedef long long ll;
const double esp = 1e-6, pi = acos(-1);
typedef pair<int, int> PII;
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
ll in() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - 48, ch = getchar();
  return x * f;
}
int ans[N], f[2][N];
int main() {
  int n;
  cin >> n;
  // if(n == 1) {
  //   string s ;
  //   cin >> s ;
  //
  //   if(s[0] == '1') cout << 0 << endl ;
  //   else cout << 1 << endl ;
  //
  //   return 0 ;
  // }
  for (int i = 0; i <= n; i++) {
    int res = 0, p = i;
    while (p) {
      int t = p;
      res++;
      int cnt = 0;
      while (t)
        cnt += t % 2, t /= 2;

      p %= cnt;
    }
    ans[i] = res;
  }
  string s;
  cin >> s;
  int y = 0;
  for (int i = 0; i < n; i++)
    y += s[i] == '1';
  int t1 = 0, t2 = 0;
  if (y == 1) {
    for (int i = 0; i < n; i++) {
      if (s[i] == '1')
        cout << 0 << endl;
      else {
        s[i] = '1';
        if (s[n - 1] != '1')
          cout << 1 << endl;
        else
          cout << 2 << endl;
        s[i] = '0';
      }
    }
    return 0;
  }
  for (int i = 0; i < n; i++) {
    t1 = t1 * 2;
    t2 = t2 * 2;
    if (s[i] == '1')
      t1++, t2++;
    t1 %= (y - 1), t2 %= (y + 1);
  }
  f[0][0] = f[1][0] = 1;

  for (int i = 1; i <= n; i++)
    f[1][i] = f[1][i - 1] * 2 % (y + 1), f[0][i] = f[0][i - 1] * 2 % (y - 1);
  for (int i = 0; i < n; i++) {
    int res = 0;
    if (s[i] == '1') {

      res = (t1 - f[0][n - i - 1]) % (y - 1);
      res = (res % (y - 1) + (y - 1)) % (y - 1);
    } else {

      res = (t2 + f[1][n - i - 1]) % (y + 1);
      res = (res % (y + 1) + (y + 1)) % (y + 1);
    }
    cout << ans[res] + 1 << endl;
  }
  return 0;
}
/*
 */


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <vector>
#pragma GCC optimize(3, "Ofast", "inline")
using namespace std;
#define ios ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define x first
#define y second
typedef long long ll;
const double esp = 1e-6, pi = acos(-1);
typedef pair<int, int> PII;
const int N = 1e6 + 10, INF = 0x3f3f3f3f, mod = 1e9 + 7;
ll in() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (isdigit(ch))
    x = x * 10 + ch - 48, ch = getchar();
  return x * f;
}
int ans[N], f[2][N];
int main() {
  int n;
  cin >> n;
  if (n == 1) {
    string s;
    cin >> s;

    if (s[0] == '1')
      cout << 0 << endl;
    else
      cout << 1 << endl;

    return 0;
  }
  for (int i = 0; i <= n; i++) {
    int res = 0, p = i;
    while (p) {
      int t = p;
      res++;
      int cnt = 0;
      while (t)
        cnt += t % 2, t /= 2;

      p %= cnt;
    }
    ans[i] = res;
  }
  string s;
  cin >> s;
  int y = 0;
  for (int i = 0; i < n; i++)
    y += s[i] == '1';
  int t1 = 0, t2 = 0;
  for (int i = 0; i < n; i++) {
    t1 = t1 * 2;
    t2 = t2 * 2;
    if (s[i] == '1')
      t1++, t2++;
    t1 %= (y - 1), t2 %= (y + 1);
  }
  f[0][0] = f[1][0] = 1;

  for (int i = 1; i <= n; i++)
    f[1][i] = f[1][i - 1] * 2 % (y + 1), f[0][i] = f[0][i - 1] * 2 % (y - 1);
  for (int i = 0; i < n; i++) {
    int res = 0;
    if (s[i] == '1') {

      res = (t1 - f[0][n - i - 1]) % (y - 1);
      res = (res % (y - 1) + (y - 1)) % (y - 1);
    } else {

      res = (t2 + f[1][n - i - 1]) % (y + 1);
      res = (res % (y + 1) + (y + 1)) % (y + 1);
    }
    cout << ans[res] + 1 << endl;
  }
  return 0;
}
/*
 */