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

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define watch(x) cout << (#x) << " is " << (x) << "\\n"

ll power(ll a, ll b, ll MOD) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

int ans(int x) {
  int c = 0;
  while (x > 0) {
    x = x % __builtin_popcount(x);
    c++;
  }
  return c;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  string s;
  cin >> s;
  int x = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1')
      x++;
  }
  int r1, r2, r3;
  r1 = r2 = r3 = 0;
  int y = max(x - 1, 1);
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      r1 = (2 * r1) % x;
      r2 = (2 * r2) % (x + 1);
      r3 = (2 * r3) % y;
    } else {
      r1 = (2 * r1 + 1) % x;
      r2 = (2 * r2 + 1) % (x + 1);
      r3 = (2 * r3 + 1) % y;
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      cout << ans((r2 + (power(2, n - i - 1, x + 1))) % (x + 1)) + 1 << "\\n";
    } else {
      if (x == 1)
        cout << 0 << "\\n";
      else
        cout << ans((r3 - (power(2, n - i - 1, x - 1)) + (x - 1)) % (x - 1)) + 1
             << "\\n";
    }
  }
}


Error: -8

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define PI 3.14159265358979323
#define ll long long int
#define vi vector<int>
#define vl vector<ll>
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define watch(x) cout << (#x) << " is " << (x) << "\\n"

ll power(ll a, ll b, ll MOD) { // a^b
  ll res = 1;
  a = a % MOD;
  while (b > 0) {
    if (b & 1) {
      res = (res * a) % MOD;
      b--;
    }
    a = (a * a) % MOD;
    b >>= 1;
  }
  return res;
}

ll gcd(ll a, ll b) { return (b == 0) ? a : gcd(b, a % b); }

int ans(int x) {
  int c = 0;
  while (x > 0) {
    x = x % __builtin_popcount(x);
    c++;
  }
  return c;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  // for getting input from input.txt
  freopen("input.txt", "r", stdin);
  // for writing output to output.txt
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  string s;
  cin >> s;
  int x = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1')
      x++;
  }
  int r1, r2, r3;
  r1 = r2 = r3 = 0;
  int y = max(x - 1, 1);
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      r1 = (2 * r1) % x;
      r2 = (2 * r2) % (x + 1);
      r3 = (2 * r3) % y;
    } else {
      r1 = (2 * r1 + 1) % x;
      r2 = (2 * r2 + 1) % (x + 1);
      r3 = (2 * r3 + 1) % y;
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      cout << ans((r2 + (power(2, n - i - 1, x + 1))) % (x + 1)) + 1 << "\\n";
    } else {
      cout << ans((r3 - (power(2, n - i - 1, x - 1)) + (x - 1)) % (x - 1)) + 1
           << "\\n";
    }
  }
}