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

#include <algorithm>
#include <bitset>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp                                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);

const ll SIZE = 1e5 * 2 + 10, INF = 1e9 + 10, MOD = 1e9 + 7;

ll binpow(ll a, ll n, ll md) {
  if (n == 0)
    return 1;
  if (n % 2 == 1)
    return (binpow(a, n - 1, md) * a) % md;
  else {
    ll b = binpow(a, n / 2, md);
    return (b * b) % md;
  }
}

ll bts(ll vl) {
  ll ret = 0;
  for (ll i = 60; i >= 0; i--) {
    if ((vl >> i) & 1)
      ret++;
  }
  return ret;
}
int main() {
  fastInp;

  ll n;
  string s;
  cin >> n;
  cin >> s;
  vector<pair<ll, ll>> vals;
  vector<ll> sm(2);

  ll cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      cnt++;
  }

  if (cnt == 0) {
    for (int i = 0; i < n; i++) {
      cout << "1\\n";
    }
    return 0;
  }

  vals.resize(n);
  for (int i = 0; i < s.size(); i++) {
    ll vl1 = 0, vl2 = 0;
    if (s[i] == '1') {
      if (cnt + 1 >= 1)
        vl1 = binpow(2, s.size() - i - 1, cnt + 1);
      if (cnt - 1 >= 1)
        vl2 = binpow(2, s.size() - i - 1, cnt - 1);
    }
    vals[i].first = vl1;
    vals[i].second = vl2;
    sm[0] += vl1;
    sm[1] += vl2;
    if (cnt + 1 >= 1)
      sm[0] %= cnt + 1;
    if (cnt - 1 >= 1)
      sm[1] %= cnt - 1;
  }

  // cerr << sm[0] << " " << sm[1] << "\\n";
  for (int i = 0; i < s.size(); i++) {
    // cerr << vals[i].first << " " << vals[i].second << "\\n";
    if (cnt == 1) {
      if (s[i] == '0') {
        ll vl = 0, c = 1;
        vl = (sm[0] + binpow(2, s.size() - i - 1, cnt + 1) + cnt + 1) %
             (cnt + 1);
        while (vl > 0) {
          vl = vl % bts(vl);
          c++;
        }
        cout << c << "\\n";
      } else {
        cout << "0\\n";
      }

      continue;
    }
    ll vl = 0, c = 1;
    if (s[i] == '0') {
      vl = (sm[0] + binpow(2, s.size() - i - 1, cnt + 1) + cnt + 1) % (cnt + 1);
    } else {
      vl = (sm[1] - binpow(2, s.size() - i - 1, cnt - 1) + cnt - 1 + cnt - 1) %
           (cnt - 1);
    }

    while (vl > 0) {
      vl = vl % bts(vl);
      c++;
    }
    cout << c << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <chrono>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
typedef long double ld;
#define fastInp                                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  ios_base::sync_with_stdio(0);

const ll SIZE = 1e5 * 2 + 10, INF = 1e9 + 10, MOD = 1e9 + 7;

ll binpow(ll a, ll n, ll md) {
  if (n == 0)
    return 1;
  if (n % 2 == 1)
    return (binpow(a, n - 1, md) * a) % md;
  else {
    ll b = binpow(a, n / 2, md);
    return (b * b) % md;
  }
}

ll bts(ll vl) {
  ll ret = 0;
  for (ll i = 60; i >= 0; i--) {
    if ((vl >> i) & 1)
      ret++;
  }
  return ret;
}
int main() {
  fastInp;

  ll n;
  string s;
  cin >> n;
  cin >> s;
  vector<pair<ll, ll>> vals;
  vector<ll> sm(2);

  ll cnt = 0;
  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '1')
      cnt++;
  }

  if (cnt == 0) {
    for (int i = 0; i < n; i++) {
      cout << "1\\n";
    }
    return 0;
  }

  vals.resize(n);
  for (int i = 0; i < s.size(); i++) {
    ll vl1 = 0, vl2 = 0;
    if (s[i] == '1') {
      if (cnt + 1 >= 1)
        vl1 = binpow(2, s.size() - i - 1, cnt + 1);
      if (cnt - 1 >= 1)
        vl2 = binpow(2, s.size() - i - 1, cnt - 1);
    }
    vals[i].first = vl1;
    vals[i].second = vl2;
    sm[0] += vl1;
    sm[1] += vl2;
    sm[0] %= cnt + 1;
    sm[1] %= cnt - 1;
  }

  // cerr << sm[0] << " " << sm[1] << "\\n";
  for (int i = 0; i < s.size(); i++) {
    // cerr << vals[i].first << " " << vals[i].second << "\\n";
    if (cnt == 1) {
      if (s[i] == '0') {
        ll vl = 0, c = 1;
        vl = (sm[0] + binpow(2, s.size() - i - 1, cnt + 1) + cnt + 1) %
             (cnt + 1);
        while (vl > 0) {
          vl = vl % bts(vl);
          c++;
        }
        cout << c << "\\n";
      } else {
        cout << "0\\n";
      }

      continue;
    }
    ll vl = 0, c = 1;
    if (s[i] == '0') {
      vl = (sm[0] + binpow(2, s.size() - i - 1, cnt + 1) + cnt + 1) % (cnt + 1);
    } else {
      vl = (sm[1] - binpow(2, s.size() - i - 1, cnt - 1) + cnt - 1 + cnt - 1) %
           (cnt - 1);
    }

    while (vl > 0) {
      vl = vl % bts(vl);
      c++;
    }
    cout << c << "\\n";
  }

  return 0;
}