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

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, b, a) for (int i = b; i >= a; i--)
#define fori(a) for (auto i : a)
#define all(a) begin(a), end(a)
#define set(a, b) memset(a, b, sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front // deque
#define mp make_pair
#define pq priority_queue
#define f first
#define s second
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define debug(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;
string repeat(string s, int n) {
  string s1 = "";
  for (int i = 0; i < n; i++)
    s1 += s;
  return s1;
}
string getString(char x) {
  string s(1, x);
  return s;
}

void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
ll power(ll x, ll y, ll mod) {
  if (y <= 0)
    return 1;
  ll z = power(x, y / 2, mod);
  if (y % 2)
    return ((z * z) % mod) * x % mod;
  return (z * z) % mod;
}
void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll c = 0;
  rep(i, 0, n) if (s[i] == '1') c++;
  if (c == 0) {
    rep(i, 0, n) cout << 1 << endl;
    return;
  }
  // if(c==1){
  //   rep(i,0,n){
  //     if(s[i]=='0')
  //   }
  // }
  vector<ll> ans(n, 0);
  vector<ll> v1 = {c - 1, c + 1};
  ll rem = 0, rem1 = 0;
  rep(i, 0, n) {
    if (s[i] == '1') {
      if (v1[0] > 0)
        rem = (rem + power(2LL, 1LL * (n - i - 1), v1[0])) % v1[0];
      else
        ans[i]--;
      rem1 = (rem1 + power(2LL, 1LL * (n - i - 1), v1[1])) % v1[1];
    }
  }

  rep(i, 0, n) {
    ll rem2 = s[i] == '1' ? rem : rem1;
    ll div = s[i] == '1' ? v1[0] : v1[1];
    if (s[i] == '0')
      rem2 = (rem2 + power(2LL, 1LL * (n - i - 1), div)) % div;
    if (s[i] == '1' && div > 0)
      rem2 = (rem2 - power(2LL, 1LL * (n - i - 1), div) + div) % div;
    ans[i]++;
    if (rem2 == 0)
      continue;
    div = __builtin_popcount(rem2);
    while (rem2 > 0 && div > 0) {
      rem2 = rem2 % div;
      div = __builtin_popcount(rem2);
      ans[i]++;
    }
  }
  rep(i, 0, n) cout << ans[i] << "\\n";
}
int main() {
  optimizeIO();
  // tc
  { solve(); }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (ll i = a; i < b; i++)
#define rrep(i, b, a) for (int i = b; i >= a; i--)
#define fori(a) for (auto i : a)
#define all(a) begin(a), end(a)
#define set(a, b) memset(a, b, sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front // deque
#define mp make_pair
#define pq priority_queue
#define f first
#define s second
#define pii pair<int, int>
#define vi vector<int>
#define vpii vector<pii>
#define debug(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;
string repeat(string s, int n) {
  string s1 = "";
  for (int i = 0; i < n; i++)
    s1 += s;
  return s1;
}
string getString(char x) {
  string s(1, x);
  return s;
}

void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
ll power(ll x, ll y, ll mod) {
  if (y <= 0)
    return 1;
  ll z = power(x, y / 2, mod);
  if (y % 2)
    return ((z * z) % mod) * x % mod;
  return (z * z) % mod;
}
void solve() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll c = 0;
  rep(i, 0, n) if (s[i] == '1') c++;
  if (c == 0) {
    rep(i, 0, n) cout << 1 << endl;
    return;
  }
  // if(c==1){
  //   rep(i,0,n){
  //     if(s[i]=='0')
  //   }
  // }
  vector<ll> v1 = {c - 1, c + 1};
  ll rem = 0, rem1 = 0;
  rep(i, 0, n) {
    if (s[i] == '1') {
      rem = (rem + power(2LL, 1LL * (n - i - 1), v1[0])) % v1[0];
      rem1 = (rem1 + power(2LL, 1LL * (n - i - 1), v1[1])) % v1[1];
    }
  }
  vector<ll> ans(n, 0);
  rep(i, 0, n) {
    ll rem2 = s[i] == '1' ? rem : rem1;
    ll div = s[i] == '1' ? v1[0] : v1[1];
    if (s[i] == '0')
      rem2 = (rem2 + power(2LL, 1LL * (n - i - 1), div)) % div;
    if (s[i] == '1')
      rem2 = (rem2 - power(2LL, 1LL * (n - i - 1), div) + div) % div;
    ans[i]++;
    if (rem2 == 0)
      continue;
    div = __builtin_popcount(rem2);
    while (rem2 > 0 && div > 0) {
      rem2 = rem2 % div;
      div = __builtin_popcount(rem2);
      ans[i]++;
    }
  }
  rep(i, 0, n) cout << ans[i] << "\\n";
}
int main() {
  optimizeIO();
  // tc
  { solve(); }
}