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

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define filei freopen("input.txt", "r", stdin);
#define fileo freopen("output.txt", "w", stdout);
#define gooi cout << "Case #" << i + 1 << " :";
#define s(n) scanf("%d", &n)
#define rev(i, n) for (int i = n; i >= 0; --i)
#define REV(i, a, b) for (int i = a; i >= b; --i)
#define miN(a, b) (((a) < (b)) ? (a) : (b))
#define sc(n) scanf("%c", &n)
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define INF 1000000000
#define pii pair<long long int, long long int>
#define pb(a) push_back(a)
#define F first
#define S second
typedef long long ll;
using namespace std;
void pre() {}
int val(long long int a) {
  int ret = 0;
  while (a != 0) {
    a = a % __builtin_popcount(a);
    ret++;
  }
  return ret;
}
void solve() {
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int a = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      a++;
    }
  }
  long long int arr[n];
  long long int brr[n];
  for (int i = 0; i < n; i++) {
    brr[i] = 0;
  }
  arr[0] = 1 % (a + 1);
  if (a - 1 > 0)
    brr[0] = 1 % (a - 1);
  long long int up = arr[0] * (s[0] - '0'), down = brr[0] * (s[0] - '0');
  for (int i = 1; i < n; i++) {
    arr[i] = (arr[i - 1] * 2) % (a + 1);
    if (a - 1 > 0) {
      brr[i] = (brr[i - 1] * 2) % (a - 1);
    }
    up += arr[i] * (s[i] - '0');
    down += brr[i] * (s[i] - '0');
  }
  long long int temp = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      if (a == 1) {
        cout << 0 << "\\n";
        continue;
      }
      temp = down - brr[i];
      temp = (temp + a - 1) % (a - 1);
      cout << 1 + val(temp) << "\\n";
    } else {
      temp = up + arr[i];
      temp %= (a + 1);
      cout << 1 + val(temp) << "\\n";
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")
#define rep(i, n) for (int i = 0; i < n; ++i)
#define REP(i, a, b) for (int i = a; i <= b; ++i)
#define filei freopen("input.txt", "r", stdin);
#define fileo freopen("output.txt", "w", stdout);
#define gooi cout << "Case #" << i + 1 << " :";
#define s(n) scanf("%d", &n)
#define rev(i, n) for (int i = n; i >= 0; --i)
#define REV(i, a, b) for (int i = a; i >= b; --i)
#define miN(a, b) (((a) < (b)) ? (a) : (b))
#define sc(n) scanf("%c", &n)
#define tr(c, i) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define INF 1000000000
#define pii pair<long long int, long long int>
#define pb(a) push_back(a)
#define F first
#define S second
typedef long long ll;
using namespace std;
void pre() {}
int val(long long int a) {
  int ret = 0;
  while (a != 0) {
    a = a % __builtin_popcount(a);
    ret++;
  }
  return ret;
}
void solve() {
  long long int n;
  cin >> n;
  string s;
  cin >> s;
  reverse(s.begin(), s.end());
  int a = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      a++;
    }
  }
  long long int arr[n];
  long long int brr[n];
  for (int i = 0; i < n; i++) {
    brr[i] = 0;
  }
  arr[0] = 1 % (a + 1);
  if (a - 1 > 0)
    brr[0] = 1 % (a - 1);
  long long int up = arr[0] * (s[0] - '0'), down = brr[0] * (s[0] - '0');
  for (int i = 1; i < n; i++) {
    arr[i] = (arr[i - 1] * 2) % (a + 1);
    if (a - 1 > 0) {
      brr[i] = (brr[i - 1] * 2) % (a - 1);
    }
    up += arr[i] * (s[i] - '0');
    down += brr[i] * (s[i] - '0');
  }
  long long int temp = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      if (a == '1') {
        cout << 1 << "\\n";
        continue;
      }
      temp = down - brr[i];
      temp = (temp + a - 1) % (a - 1);
      cout << 1 + val(temp) << "\\n";
    } else {
      temp = up + arr[i];
      temp %= (a + 1);
      cout << 1 + val(temp) << "\\n";
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  solve();
}