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

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <unordered_map>
#include <vector>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define ll long long
#define INF 1000000000000000000
#define MOD 1000000007
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define uid uniform_int_distribution<int>
using namespace std;

int power(ll a, ll b, ll c) {
  ll res = 1;
  while (b != 0) {
    if (b & 1)
      res = (res * a) % c;
    a = (a * a) % c;
    b /= 2;
  }
  return (int)res;
}
int func(int val) {
  int ans = 1;
  while (val != 0) {
    val = val % __builtin_popcount(val);
    ans++;
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  int c0 = 0, c1 = 0, c2 = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      c0 = (c0 + power(2, n - 1 - i, cnt)) % cnt;
      if (cnt - 1 != 0)
        c1 = (c1 + power(2, n - 1 - i, cnt - 1)) % (cnt - 1);
      c2 = (c2 + power(2, n - 1 - i, cnt + 1)) % (cnt + 1);
    }
  }
  // cout << c0 << " " << c1 << " " << c2 << "\\n";
  int ans;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      ans = func((c2 + (power(2, n - i - 1, cnt + 1)) % (cnt + 1)) % (cnt + 1));
    } else {
      if (cnt - 1 != 0) {
        int val = (c1 - (power(2, n - i - 1, cnt - 1)) % (cnt - 1));
        val = (val + (cnt - 1)) % (cnt - 1);
        ans = func(val);
      } else {
        ans = 0;
      }
    }
    cout << ans << "\\n";
  }
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string.h>
#include <unordered_map>
#include <vector>
#define all(x) x.begin(), x.end()
#define ff first
#define ss second
#define ll long long
#define INF 1000000000000000000
#define MOD 1000000007
#define rnd mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define FIO                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define uid uniform_int_distribution<int>
using namespace std;

int power(ll a, ll b, ll c) {
  ll res = 1;
  while (b != 0) {
    if (b & 1)
      res = (res * a) % c;
    a = (a * a) % c;
    b /= 2;
  }
  return (int)res;
}
int func(int val) {
  int ans = 1;
  while (val != 0) {
    val = val % __builtin_popcount(val);
    ans++;
  }
  return ans;
}
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      cnt++;
  }
  int c0 = 0, c1 = 0, c2 = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1') {
      c0 = (c0 + power(2, n - 1 - i, cnt)) % cnt;
      c1 = (c1 + power(2, n - 1 - i, cnt - 1)) % (cnt - 1);
      c2 = (c2 + power(2, n - 1 - i, cnt + 1)) % (cnt + 1);
    }
  }
  // cout << c0 << " " << c1 << " " << c2 << "\\n";
  int ans;
  for (int i = 0; i < n; i++) {
    if (s[i] == '0') {
      ans = func((c2 + (power(2, n - i - 1, cnt + 1)) % (cnt + 1)) % (cnt + 1));
    } else {
      int val = (c1 - (power(2, n - i - 1, cnt - 1)) % (cnt - 1));
      val = (val + (cnt - 1)) % (cnt - 1);
      ans = func(val);
    }
    cout << ans << "\\n";
  }
}