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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include<unordered_map>
using namespace std;
#define ll long long
#define dd cout << endl
const long long int inf = 1e18 + 7;

using namespace std;

inline ll int max(ll int a, ll int b) { return a > b ? a : b; }
inline ll int min(ll int a, ll int b) { return a < b ? a : b; }

ll power(ll a, ll b, ll mod) {
  return b ? power(a * a % mod, b / 2, mod) * (b % 2 ? a : 1) % mod : 1;
}

ll cal(ll n) {
  ll cnt = 1;
  while (n) {
    n = n % __builtin_popcount(n);
    cnt++;
  }
  return cnt;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;

  string x;
  cin >> x;

  ll ans1 = 0, ans2 = 0, cnt = 0, ans;

  for (int i = 0; i < n; i++) {
    if (x[i] == '1')
      cnt++;
  }

  if (cnt > 1) {
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        ans1 = (ans1 + power(2, n - i - 1, cnt + 1)) % (cnt + 1),
        ans2 = (ans2 + power(2, n - i - 1, cnt - 1)) % (cnt - 1);
      }
    }

    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        ans = (ans2 + ((cnt - 1) - power(2, n - i - 1, cnt - 1) % (cnt - 1)) %
                          (cnt - 1)) %
              (cnt - 1);

        cout << cal(ans) << endl;
      } else {
        ans = (ans1 + power(2, n - i - 1, cnt + 1) % (cnt + 1)) % (cnt + 1);
        cout << cal(ans) << endl;
      }
    }
  } else if (cnt) {
    for (int i = 0; i < n; i++) {
      if (x[i] == '1') {
        ans1 = power(2, n - i - 1, cnt + 1) % (cnt + 1);
      }
    }

    for (int i = 0; i < n; i++) {
      if (x[i] == '0') {
        ans = (ans1 + power(2, n - i - 1, cnt + 1) % (cnt + 1)) % (cnt + 1);
        cout << cal(ans) << endl;
      } else {
        cout << '0' << endl;
      }
    }
  } else {
    for (int i = 0; i < n; i++)
      cout << '1' << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include<unordered_map>
using namespace std;
#define ll long long
#define dd cout << endl
const long long int inf = 1e18 + 7;

using namespace std;

inline ll int max(ll int a, ll int b) { return a > b ? a : b; }
inline ll int min(ll int a, ll int b) { return a < b ? a : b; }

// const int maxn=

inline ll int quickmi(ll int a, ll int n, ll int mod) {
  ll int ans = 1;
  while (n) {
    if (n & 1)
      ans = ans * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return ans;
}

inline void cal(string now) {
  int ans = 1;

  int cnt = 0;

  for (int i = 0; i < now.size(); i++) {
    if (now[i] == '1') {
      cnt++;
    }
  }

  ll int s = 0, base = 1;

  for (int i = (int)now.size() - 1; i >= 0; i--) {
    if (now[i] == '1') {
      s += base;
      s %= cnt;
    }
    base <<= 1;
    base %= cnt;
  }

  while (s != 0) {
    cnt = 0;

    ll int temp = s;

    while (temp) {
      if (temp & 1)
        cnt++;
      temp >>= 1;
    }

    ans++;

    s %= cnt;
  }

  cout << ans << endl;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n;
  while (cin >> n) {

    string str;

    cin >> str;

    for (int i = 0; i < n; i++) {
      string now = str;

      if (now[i] == '0')
        now[i] = '1';
      else
        now[i] = '0';

      cal(now);
    }
  }

  return 0;
}