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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define MOD 1000000007
#define endl "\\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(var) (var).begin(), (var).end()
#define MAXX 9000000000000000000
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define vvpi vector<vector<pair<int, int>>>

int power(int x, int y, int p) {
  int res = 1;
  if (x == 0) {
    return 0;
  }

  while (y > 0) {
    if (y & 1) {
      res = (res * x) % p;
    }

    y = y >> 1;
    x = (x * x) % p;
  }
  return res % p;
}
int cnt[200005];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n, i, j, k, x, y;

  cnt[0] = 0;
  cnt[1] = 1;
  cnt[2] = 1;
  for (i = 3; i <= 200002; i++) {
    x = i;
    y = 0;
    while (x) {
      if (x & 1) {
        y++;
      }
      x /= 2;
    }
    cnt[i] = 1 + cnt[i % y];
  }

  cin >> n;
  string s;
  cin >> s;
  x = 0;
  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      x++;
    }
  }

  int ca1 = 0, ca2 = 0;
  for (i = n - 1, j = 1, k = 1; i >= 0; i--) {
    if (s[i] == '1') {
      ca1 += j;
      ca2 += k;
      ca1 %= max(1LL, (x - 1));
      ca2 %= (x + 1);
    }
    j *= 2;
    k *= 2;
    j %= max(1LL, (x - 1));
    k %= (x + 1);
  }

  // cout << x << endl;

  for (i = 0; i < n; i++) {
    if (s[i] == '0') {
      int te = ca2;
      te += power(2, n - 1 - i, (x + 1));
      // cout << 'a' << power(2, n-1-i, (x+1)) << endl;
      te %= (x + 1);
      cout << 1 + cnt[te] << endl;
    } else {
      if (x - 1 == 0) {
        cout << 0 << endl;
        continue;
      }
      int te = ca1;
      te -= power(2, n - 1 - i, (x - 1));
      te += 100 * (x - 1);
      te %= (x - 1);
      cout << 1 + cnt[te] << endl;
    }
  }

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define ld long double
#define MOD 1000000007
#define endl "\\n"
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(var) (var).begin(), (var).end()
#define MAXX 9000000000000000000
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define vvpi vector<vector<pair<int, int>>>

int cnt[200005];

int power(int x, int y, int p) {
  int res = 1; // Initialize result

  x = x % p; // Update x if it is more than or
             // equal to p

  if (x == 0)
    return 0; // In case x is divisible by p;

  while (y > 0) {
    // If y is odd, multiply x with result
    if (y & 1) {
      res = (res * x) % p;
    }

    // y must be even now
    y = y >> 1; // y = y/2
    x = (x * x) % p;
  }
  return res;
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  int n, i, j, k, x, y;

  cnt[0] = 0;
  cnt[1] = 1;
  cnt[2] = 1;
  for (i = 3; i <= 200002; i++) {
    x = i;
    y = 0;
    while (x) {
      if (x & 1) {
        y++;
      }
      x /= 2;
    }
    cnt[i] = 1 + cnt[i % y];
  }

  cin >> n;
  string s;
  cin >> s;
  x = 0;
  for (i = 0; i < n; i++) {
    if (s[i] == '1') {
      x++;
    }
  }

  int ca1 = 0, ca2 = 0;
  for (i = n - 1, j = 1, k = 1; i >= 0; i--) {
    if (s[i] == '1') {
      ca1 += j;
      ca2 += k;
      ca1 %= (x - 1);
      ca1 %= (x + 1);
    }
    j *= 2;
    k *= 2;
    j %= (x - 1);
    k %= (x + 1);
  }

  // cout << x << endl;

  for (i = 0; i < n; i++) {
    if (s[i] == '0') {
      // ca1 (x-1)

      int te = ca2;
      te += power(2, n - 1 - i, (x + 1));
      // cout << 'a' << power(2, n-1-i, (x+1)) << endl;
      te %= (x + 1);
      cout << 1 + cnt[te] << endl;
    } else {
      int te = ca1;
      te -= power(2, n - 1 - i, (x - 1));
      te += 100 * (x - 1);
      te %= (x - 1);
      cout << 1 + cnt[te] << endl;
    }
  }

  return 0;
}