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

/// mAAria...
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(v) v.begin(), v.end()
#define allr(v) v.begin(), v.end()
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define pii pair<int, int>
// #define pi 3.1415926535897932384626433832
#define mod 1000000007
// #define mod 998244353
#define inf 1000000000
#define pr1(a) cout << a << endl
#define pans1(a, b) cout << a << " " << b << endl
#define pans2(a, b, c) cout << a << " " << b << " " << c << endl

const long double PI = acos(-1);

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

vector<int> preficnt_function(string s) {
  int n = (int)s.length();
  vector<int> pi(n);
  for (int i = 1; i < n; i++) {
    int j = pi[i - 1];
    // we will try to shorten the match string length until we revert to the
    // beginning of match (indecnt 1)
    while (j > 0 && s[i] != s[j])
      j = pi[j - 1];
    if (s[i] == s[j])
      j++;
    pi[i] = j;
  }
  return pi;
}

const int MAX = 200005;
int perm1[MAX], perm2[MAX];

void solve() {

  int n;
  cin >> n;
  string s;
  cin >> s;

  int cnt = 0;
  int ans1 = 0, ans2 = 0;

  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  cnt--;
  if (cnt != 0) {
    perm1[0] = 1;
    for (int i = 1; i <= n; i++) {
      perm1[i] = perm1[i - 1] * 2 % cnt;
    }
  }
  cnt += 2;

  perm2[0] = 1;
  for (int i = 1; i <= n; i++) {
    perm2[i] = perm2[i - 1] * 2 % cnt;
  }
  cnt--;

  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (cnt != 1)
        ans1 = (ans1 + perm1[n - i - 1]) % (cnt - 1);
      ans2 = (ans2 + perm2[n - i - 1]) % (cnt + 1);
    }
  }
  for (int i = 0; i < n; i++) {
    if (s[i] == '1') {
      if (cnt != 1) {
        int cur = (ans1 - perm1[n - i - 1] + cnt - 1) % (cnt - 1);
        int ans = 1;
        // cout << cur << endl;
        while (cur) {
          int cnt = __builtin_popcount(cur);
          cur = cur % cnt;
          ans++;
        }
        cout << ans << endl;
      } else {
        cout << 0 << endl;
      }
    } else {
      int cur = (ans2 + perm2[n - i - 1]) % (cnt + 1);

      int ans = 1;
      // cout << cur << endl;
      while (cur) {
        int cnt = __builtin_popcount(cur);
        cur = cur % cnt;
        ans++;
      }
      cout << ans << endl;
    }
  }
}

signed main() {
  IOS;
  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}


Error: 0

Fixed submission:

/// mAAria...
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define all(v) v.begin(), v.end()
#define allr(v) v.begin(), v.end()
#define ld long double
#define ff first
#define ss second
#define pb push_back
#define pii pair<int, int>
// #define pi 3.1415926535897932384626433832
#define mod 1000000007
// #define mod 998244353
#define inf 1000000000
#define pr1(a) cout << a << endl
#define pr2(a, b) cout << a << " " << b << endl
#define pr3(a, b, c) cout << a << " " << b << " " << c << endl

const long double PI = acos(-1);

int powm(int a, int b) {
  int res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}

int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}

vector<int> prefix_function(string s) {
  int n = (int)s.length();
  vector<int> pi(n);
  for (int i = 1; i < n; i++) {
    int j = pi[i - 1];
    // we will try to shorten the match string length until we revert to the
    // beginning of match (index 1)
    while (j > 0 && s[i] != s[j])
      j = pi[j - 1];
    if (s[i] == s[j])
      j++;
    pi[i] = j;
  }
  return pi;
}
int val(int x, int y, int z) {
  return (x * x + y * y + z * z + x * y + y * z + z * x);
}
void solve() {

  int n;
  cin >> n;
  string s;
  cin >> s;
  n = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      n += (1LL << (s.size() - i - 1));
  }
  // cout << n << endl;
  for (int i = 0; i < s.size(); i++) {
    long long cur = n;

    if (s[i] == '0')
      cur += (1LL << (s.size() - i - 1));
    else
      cur -= (1LL << (s.size() - i - 1));
    // else cur-= powm(2, s.size()-i-1);

    int ans = 0;
    // cout << cur << endl;
    while (cur) {
      int cnt = __builtin_popcount(cur);
      cur = cur % cnt * 1LL;
      ans++;
    }
    cout << ans << endl;
  }
}

signed main() {
  IOS;
  int t = 1;
  // cin >> t;

  for (int i = 1; i <= t; i++) {
    solve();
  }
  return 0;
}