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

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include <intrin.h>
// #include <chrono>
// #include <random>
// #include <unordered_map>

using namespace std;

typedef long long ll;

ll bpow(ll x, ll p, ll mod) {
  if (p == 0)
    return 1;
  if (p % 2)
    return (bpow(x, p - 1, mod) * x) % mod;
  else {
    ll b = bpow(x, p / 2, mod);
    return (b * b) % mod;
  }
}

ll pop_cnt(ll a) {
  ll res = 0;
  while (a) {
    if ((a & 1) == 1)
      res++;
    a >>= 1;
  }
  return res;
}

ll triv(ll a) {
  ll res = 0;
  while (a) {
    a = a % pop_cnt(a);
    res++;
  }
  return res;
}

void solve() {
  int n;
  string s;
  cin >> n >> s;
  int cnt1 = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      cnt1++;

  if (cnt1 <= 1) {
    ll res2 = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        res2 += bpow(2, (n - i - 1), cnt1 + 1);
        res2 %= (cnt1 + 1);
      }
    }
    for (int i = 0; i < n; i++) {
      ll add, cur, ans = 1;
      if (s[i] == '1') {
        ans = 0;
      } else {
        add = bpow(2, (n - i - 1), cnt1 + 1);
        cur = (res2 + add + cnt1 + 1) % (cnt1 + 1);
        ans += triv(cur);
      }
      cout << ans << '\\n';
    }
  } else {
    ll res1 = 0, res2 = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        res1 += bpow(2, (n - i - 1), cnt1 - 1);
        res2 += bpow(2, (n - i - 1), cnt1 + 1);
        res1 %= (cnt1 - 1);
        res2 %= (cnt1 + 1);
      }
    }
    for (int i = 0; i < n; i++) {
      ll add, cur, ans = 1;
      if (s[i] == '1') {
        add = bpow(2, (n - i - 1), cnt1 - 1);
        cur = (res1 - add + cnt1 - 1) % (cnt1 - 1);
      } else {
        add = bpow(2, (n - i - 1), cnt1 + 1);
        cur = (res2 + add + cnt1 + 1) % (cnt1 + 1);
      }
      ans += triv(cur);
      cout << ans << '\\n';
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#if defined(_DEBUG)
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int q = 1;
  // cin >> q;
  for (; q > 0; q--) {
    solve();
    cout << '\\n';
  }
}

Error: WA

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#include <bits/stdc++.h>
// #include <intrin.h>
// #include <chrono>
// #include <random>
// #include <unordered_map>

using namespace std;

typedef long long ll;

ll bpow(ll x, ll p, ll mod) {
  if (p == 0)
    return 1;
  if (p % 2)
    return (bpow(x, p - 1, mod) * x) % mod;
  else {
    ll b = bpow(x, p / 2, mod);
    return (b * b) % mod;
  }
}

ll pop_cnt(ll a) {
  ll res = 0;
  while (a) {
    if ((a & 1) == 1)
      res++;
    a >>= 1;
  }
  return res;
}

ll triv(ll a) {
  ll res = 0;
  while (a) {
    a = a % pop_cnt(a);
    res++;
  }
  return res;
}

void solve() {
  int n;
  string s;
  cin >> n >> s;
  int cnt1 = 0;
  for (int i = 0; i < n; i++)
    if (s[i] == '1')
      cnt1++;

  if (cnt1 == 0) {
    ll res2 = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        res2 += bpow(2, (n - i - 1), cnt1 + 1);
        res2 %= (cnt1 + 1);
      }
    }
    for (int i = 0; i < n; i++) {
      ll add, cur, ans = 1;
      add = bpow(2, (n - i - 1), cnt1 + 1);
      cur = (res2 + add + cnt1 + 1) % (cnt1 + 1);
      ans += triv(cur);
      cout << ans << '\\n';
    }
  } else {
    ll res1 = 0, res2 = 0;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        res1 += bpow(2, (n - i - 1), cnt1 - 1);
        res2 += bpow(2, (n - i - 1), cnt1 + 1);
        res1 %= (cnt1 - 1);
        res2 %= (cnt1 + 1);
      }
    }
    for (int i = 0; i < n; i++) {
      ll add, cur, ans = 1;
      if (s[i] == '1') {
        add = bpow(2, (n - i - 1), cnt1 - 1);
        cur = (res1 - add + cnt1 - 1) % (cnt1 - 1);
      } else {
        add = bpow(2, (n - i - 1), cnt1 + 1);
        cur = (res2 + add + cnt1 + 1) % (cnt1 + 1);
      }
      ans += triv(cur);
      cout << ans << '\\n';
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
#if defined(_DEBUG)
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int q = 1;
  // cin >> q;
  for (; q > 0; q--) {
    solve();
    cout << '\\n';
  }
}