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

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

ll power(ll x, ll y, ll m) {
  ll res = 1;
  x %= m;
  while (y) {
    if (y & 1)
      res = (res * x) % m;
    y >>= 1;
    x = (x * x) % m;
  }
  return res;
}
int work(int res) {
  int cnt = 0;
  while (res) {
    int x = __builtin_popcount(res);
    res %= x;
    ++cnt;
  }
  return cnt;
}
int main() {
  boost;
  int n, i;
  string s;
  cin >> n;
  cin >> s;

  int cnt = 0;
  for (char c : s)
    cnt += (c == '1');

  if (cnt == 0) {
    fo(i, 0, n) cout << 1 << '\\n';
    return 0;
  }

  ll pre1[n], suf1[n];
  // Add 1 part

  fo(i, 0, n) pre1[i] = ((i ? pre1[i - 1] : 0) +
                         (s[i] == '1' ? power(2, n - 1 - i, cnt + 1) : 0)) %
                        (cnt + 1);

  for (i = n - 1; i >= 0; i--)
    suf1[i] = ((i + 1 < n ? suf1[i + 1] : 0) +
               (s[i] == '1' ? power(2, n - 1 - i, cnt + 1) : 0)) %
              (cnt + 1);

  ll pre2[n], suf2[n];
  // Sub 1 part

  if (cnt > 1) {
    fo(i, 0, n) pre2[i] = ((i ? pre2[i - 1] : 0) +
                           (s[i] == '1' ? power(2, n - 1 - i, cnt - 1) : 0)) %
                          (cnt - 1);

    for (i = n - 1; i >= 0; i--)
      suf2[i] = ((i + 1 < n ? suf2[i + 1] : 0) +
                 (s[i] == '1' ? power(2, n - 1 - i, cnt - 1) : 0)) %
                (cnt - 1);
  }

  fo(i, 0, n) {
    int ans = 0;
    if (s[i] == '1') {
      if (cnt == 1)
        ans = 0;
      else {
        ll res =
            ((i ? pre2[i - 1] : 0) + (i + 1 < n ? suf2[i + 1] : 0)) % (cnt - 1);
        ans = 1 + work(res);
      }
    } else {
      ll res =
          ((i ? pre1[i - 1] : 0) + (i + 1 < n ? suf1[i + 1] : 0)) % (cnt + 1);
      res = (res + power(2, n - 1 - i, cnt + 1)) % (cnt + 1);
      ans = 1 + work(res);
    }
    cout << ans << '\\n';
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define ld long double
#define f first
#define s second
#define pb push_back
#define eb emplace_back
#define mk make_pair
#define mt make_tuple
#define MOD 1000000007
#define fo(i, a, b) for (i = a; i < b; i++)
#define foe(i, a, b) for (i = a; i <= b; i++)
#define all(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<long long int>
#define pii pair<int, int>
#define pll pair<long long int, long long int>
#define vpii vector<pair<int, int>>
#define vpll vector<pair<long long int, long long int>>
#define boost                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0)
using namespace std;
const int inf = 1e9 + 5;
const ll inf64 = 1e18 + 5;

ll power(ll x, ll y, ll m) {
  ll res = 1;
  x %= m;
  while (y) {
    if (y & 1)
      res = (res * x) % m;
    y >>= 1;
    x = (x * x) % m;
  }
  return res;
}
int work(int res) {
  int cnt = 0;
  while (res) {
    int x = __builtin_popcount(res);
    res %= x;
    ++cnt;
  }
  return cnt;
}
int main() {
  boost;
  int n, i;
  string s;
  cin >> n;
  cin >> s;

  int cnt = 0;
  for (char c : s)
    cnt += (c == '1');

  if (cnt == 0) {
    fo(i, 0, n) cout << 1 << '\\n';
    return 0;
  }

  ll pre1[n], suf1[n];
  // Add 1 part

  fo(i, 0, n) pre1[i] = ((i ? pre1[i - 1] : 0) +
                         (s[i] == '1' ? power(2, n - 1 - i, cnt + 1) : 0)) %
                        (cnt + 1);

  for (i = n - 1; i >= 0; i--)
    suf1[i] = ((i + 1 < n ? suf1[i + 1] : 0) +
               (s[i] == '1' ? power(2, n - 1 - i, cnt + 1) : 0)) %
              (cnt + 1);

  ll pre2[n], suf2[n];
  // Sub 1 part

  fo(i, 0, n) pre2[i] = ((i ? pre2[i - 1] : 0) +
                         (s[i] == '1' ? power(2, n - 1 - i, cnt - 1) : 0)) %
                        (cnt - 1);

  for (i = n - 1; i >= 0; i--)
    suf2[i] = ((i + 1 < n ? suf2[i + 1] : 0) +
               (s[i] == '1' ? power(2, n - 1 - i, cnt - 1) : 0)) %
              (cnt - 1);

  fo(i, 0, n) {
    int ans = 0;
    if (s[i] == '1') {
      if (cnt == 1)
        ans = 0;
      else {
        ll res =
            ((i ? pre2[i - 1] : 0) + (i + 1 < n ? suf2[i + 1] : 0)) % (cnt - 1);
        ans = 1 + work(res);
      }
    } else {
      ll res =
          ((i ? pre1[i - 1] : 0) + (i + 1 < n ? suf1[i + 1] : 0)) % (cnt + 1);
      res = (res + power(2, n - 1 - i, cnt + 1)) % (cnt + 1);
      ans = 1 + work(res);
    }
    cout << ans << '\\n';
  }
}