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

#include <bits/stdc++.h>

#define lol long long
#define gcd(x, y) __gcd(x, y)
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define fixed(x) fixed << setprecision(x)
using namespace std;
template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}
template <class A> inline lol abs(A &a) { return (a < 0 ? -a : a); }
bool inLine(int x, int y, int mx, int my) {
  return (x >= 0 && y >= 0 && x < mx && y < my);
}
const lol inf = (1LL << 62);
const lol MOD = (1e9) + 7;
const lol mod = 998244353;
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
lol n, m, k;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> n >> s;
  k = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      k++;
  }
  vector<int> ans(n + 10);
  bool can = false;
  if (k - 1 == 0)
    can = true;
  lol sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++) {
    sum1 *= 2;
    sum2 *= 2;
    sum1 += s[i] - '0';
    sum2 += s[i] - '0';
    sum1 %= k + 1;
    if (!can)
      sum2 %= k - 1;
  }
  lol tmp1 = 1, tmp2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    lol a;
    lol p = 1;
    if (s[i] == '1') {
      a = sum2;
      a -= tmp2;
      if (can) {
        ans[i] = 0;
        continue;
      }
      while (a < 0)
        a += k - 1;
    } else {
      a = sum1;
      a += tmp1;
      a %= k + 1;
    }
    tmp1 *= 2;
    tmp2 *= 2;
    tmp1 %= k + 1;
    if (!can)
      tmp2 %= k - 1;
    while (a != 0) {
      lol t = __builtin_popcount(a);
      a %= t;
      p++;
    }

    ans[i] = p;
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << '\\n';
  return (0);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define lol long long
#define gcd(x, y) __gcd(x, y)
#define mt make_tuple
#define mp make_pair
#define fi first
#define se second
#define fixed(x) fixed << setprecision(x)
using namespace std;
template <class A, class B> inline bool chmax(A &a, const B &b) {
  return b > a && (a = b, true);
}
template <class A, class B> inline bool chmin(A &a, const B &b) {
  return b < a && (a = b, true);
}
template <class A> inline lol abs(A &a) { return (a < 0 ? -a : a); }
bool inLine(int x, int y, int mx, int my) {
  return (x >= 0 && y >= 0 && x < mx && y < my);
}
const lol inf = (1LL << 62);
const lol MOD = (1e9) + 7;
const lol mod = 998244353;
const int dx[] = {1, 0, -1, 0, 1, 1, -1, -1};
const int dy[] = {0, 1, 0, -1, 1, -1, 1, -1};
lol n, m, k;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string s;
  cin >> n >> s;
  k = 0;
  for (int i = 0; i < n; i++) {
    if (s[i] == '1')
      k++;
  }
  vector<int> ans(n);
  lol sum1 = 0, sum2 = 0;
  for (int i = 0; i < n; i++) {
    sum1 *= 2;
    sum2 *= 2;
    sum1 += s[i] - '0';
    sum2 += s[i] - '0';
    sum1 %= k + 1;
    sum2 %= k - 1;
  }
  lol tmp1 = 1, tmp2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    lol a;
    lol p = 1;
    if (s[i] == '1') {
      a = sum2;
      a -= tmp2;
      while (a < 0)
        a += k - 1;
    } else {
      a = sum1;
      a += tmp1;
      a %= k + 1;
    }
    tmp1 *= 2;
    tmp2 *= 2;
    tmp1 %= k + 1;
    tmp2 %= k - 1;
    while (a != 0) {
      lol t = __builtin_popcount(a);
      a %= t;
      p++;
      if (a == 0)
        break;
    }

    ans[i] = p;
  }
  for (int i = 0; i < n; i++)
    cout << ans[i] << '\\n';
  return (0);
}