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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define vi vector<int>
#define vli vector<ll>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define M_PI 3.14159265358979323846
#define mp make_pair
#define sz(x) (int)x.size()
#define matrix vector<vector<ll>>
#define forn(i, a, b) for (int i = a; i < b; i++)
#define eps 0.0000000001
#define all(a) (a).begin(), (a).end()
#define ordered_set                                                            \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update>

const long long INF = (long long)1e12 + 1;
const long long mod = (long long)20437;
const long long hell = (long long)1000000007;

ll power(ll x, ll y, ll m) {
  ll res = 1;
  while (y > 0) {
    if (y % 2)
      res = (x % m * res % m) % m;
    x = (x % m * x % m) % m;
    y = y / 2;
  }

  return res % m;
}

ll modinv(ll x) { return power(x, hell - 2, 1); }

ll gcd(ll a, ll b) {
  while (b != 0) {
    ll t = b;
    b = a % t;
    a = t;
  }
  return a;
}

void fastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

ll func(int x) {
  if (x == 0)
    return 0;

  ll s = __builtin_popcount(x);
  return 1 + func(x % s);
}

void solve() {
  ll n;
  string s;
  cin >> n >> s;

  ll cur_set = 0;

  forn(i, 0, n) {
    if (s[i] == '1')
      cur_set++;
  }
  if (cur_set == 0) {
    forn(i, 0, n) { cout << 1 << "\\n"; }
    return;
  }

  ll c1 = cur_set - 1;
  ll c2 = cur_set + 1;

  ll rem1 = 0;
  ll rem2 = 0;
  ll p1 = 1;
  ll p2 = 1;

  for (int i = n - 1; i >= 0; i--) {
    if (c1 > 0)
      rem1 = (rem1 + p1 * (s[i] - 48)) % c1;
    rem2 = (rem2 + p2 * (s[i] - 48)) % c2;
    if (c1 > 0)
      p1 = (p1 * 2) % c1;
    p2 = (p2 * 2) % c2;
  }

  ll res[n];
  memset(res, 0, sizeof(res));

  for (ll i = n - 1; i >= 0; i--) {
    if (s[i] == '1' and c1 > 0) {
      ll cur = rem1;
      cur = (cur - power(2, n - i - 1, c1) + c1) % c1;
      res[i] = 1 + func(cur);

    } else if (s[i] == '0') {
      ll cur = rem2;
      cur = (cur + power(2, n - i - 1, c2) + c2) % c2;
      res[i] = 1 + func(cur);
    }
  }

  forn(i, 0, n) { cout << res[i] << "\\n"; }
}

int main() {
  fastIO();
  int test;
  // cin>>test;
  test = 1;

  while (test--)
    solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ull unsigned long long int
#define ll long long int
#define pb push_back
#define pii pair<int, int>
#define F first
#define S second
#define vi vector<int>
#define vli vector<ll>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define M_PI 3.14159265358979323846
#define mp make_pair
#define sz(x) (int)x.size()
#define matrix vector<vector<ll>>
#define forn(i, a, b) for (int i = a; i < b; i++)
#define eps 0.0000000001
#define all(a) (a).begin(), (a).end()
#define ordered_set                                                            \\
  tree<ll, null_type, less_equal<ll>, rb_tree_tag,                             \\
       tree_order_statistics_node_update>

const long long INF = (long long)1e12 + 1;
const long long mod = (long long)20437;
const long long hell = (long long)1000000007;

ll power(ll x, ll y, ll m) {
  ll res = 1;
  while (y > 0) {
    if (y % 2)
      res = (x % m * res % m) % m;
    x = (x % m * x % m) % m;
    y = y / 2;
  }

  return res % m;
}

ll modinv(ll x) { return power(x, hell - 2, 1); }

ll gcd(ll a, ll b) {
  while (b != 0) {
    ll t = b;
    b = a % t;
    a = t;
  }
  return a;
}

void fastIO() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}

int func(int x) {
  if (x == 0)
    return 0;

  int s = __builtin_popcount(x);
  return 1 + func(x % s);
}

void solve() {
  int n;
  string s;
  cin >> n >> s;

  int cur_set = 0;

  forn(i, 0, n) {
    if (s[i] == '1')
      cur_set++;
  }

  int c1 = cur_set - 1;
  int c2 = cur_set + 1;

  int rem1 = 0;
  int rem2 = 0;
  int p1 = 1;
  int p2 = 1;

  for (int i = n - 1; i >= 0; i--) {
    if (c1 != 0)
      rem1 = (rem1 + p1 * (s[i] - 48)) % c1;
    rem2 = (rem2 + p2 * (s[i] - 48)) % c2;
    p1 = (p1 * 2) % c1;
    p2 = (p2 * 2) % c2;
  }

  int res[n];
  memset(res, 0, sizeof(res));

  for (int i = n - 1; i >= 0; i--) {
    if (s[i] == '1' and c1 != 0) {
      int cur = rem1;
      cur = (cur - power(2, n - i - 1, c1) + c1) % c1;
      res[i] = 1 + func(cur);
    } else {
      int cur = rem2;
      cur = (cur + power(2, n - i - 1, c2) + c2) % c2;
      res[i] = 1 + func(cur);
    }
  }
  forn(i, 0, n) { cout << res[i] << "\\n"; }
}

int main() {
  fastIO();
  int test;
  // cin>>test;
  test = 1;

  while (test--)
    solve();
}