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

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

#define int long long
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define F first
#define S second
#define sz(x) ((int)x.size())
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
#define mod2 998244353
#define what_is(x) cerr << #x << " is " << x << "\\n";
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.rbegin(), v.rend())
#define PI 3.14159265358979323846
#define vout(a)                                                                \\
  for (auto x : a)                                                             \\
    cout << x << " ";
#define INF 1000000000000

// #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag,
// tree_order_statistics_node_update>

// using namespace __gnu_pbds;
using namespace std;

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
using mi = multiset<int>;
using mii = multiset<pii>;

int lcm(int a, int b) { return (a * (b / __gcd(a, b))); }

int modp(int a, int m, int n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a % m;
  int res = modp(a, m, n / 2);
  if (n % 2 == 0)
    return (res * res) % m;
  else
    return (a * res * res) % m;
}

void solve() {
  int n, m, t, i, l, k, r, u, v, w, j, ss, sum = 0, pos, flag = 0, count = 0;
  string S, T, U;
  // int x,y,a,b,c;
  cin >> n >> S;
  int popcnt = 0;
  rep(i, 0, n) popcnt += (S[i] == '1');
  if (!popcnt) {
    rep(i, 0, n) { cout << "1\\n"; }
    return;
  }
  int num = 0, minus1 = 0, plus1 = 0;
  reverse(all(S));
  if (popcnt == 1) {
    rep(i, 0, n) {
      plus1 = (plus1 % (popcnt + 1) + (S[i] - '0') * modp(2, popcnt + 1, i)) %
              (popcnt + 1);
    }
    vi ans(n);
    rep(i, 0, n) {
      if (S[i] == '0') {
        t = (plus1 + modp(2, popcnt + 1, i) + (popcnt + 1)) % (popcnt + 1);
        count = 1;
        while (t) {
          t = t % (__builtin_popcountll(t));
          count++;
        }
        ans[i] = count;
      } else {
        ans[i] = 0;
      }
    }
    reverse(all(ans));
    for (int x : ans)
      cout << x << "\\n";
    return;
  }
  rep(i, 0, n) {
    plus1 = (plus1 % (popcnt + 1) + (S[i] - '0') * modp(2, popcnt + 1, i)) %
            (popcnt + 1);
    minus1 = (minus1 % (popcnt - 1) + (S[i] - '0') * modp(2, popcnt - 1, i)) %
             (popcnt - 1);
  }
  vi ans(n);
  rep(i, 0, n) {
    if (S[i] == '0') {
      t = (plus1 + modp(2, popcnt + 1, i) + (popcnt + 1)) % (popcnt + 1);
      count = 1;
      while (t) {
        t = t % (__builtin_popcountll(t));
        count++;
      }
    } else {
      t = (minus1 - modp(2, popcnt - 1, i) + (popcnt - 1)) % (popcnt - 1);
      count = 1;
      while (t) {
        t = t % (__builtin_popcountll(t));
        count++;
      }
    }
    ans[i] = count;
  }
  reverse(all(ans));
  for (int x : ans)
    cout << x << "\\n";
}

signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  int T = 0;
  while (t--) {
    T++;
    // cout<<"Scenario #"<<T<<":\\n";
    solve();
    if (t)
      cout << "\\n";
  }
  return 0;
}

/*
6 4
1 3 4 5 6 2
*/

Error: WA

Fixed submission:

#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>

#define int long long
#define rep(i, begin, end)                                                     \\
  for (__typeof(end) i = (begin) - ((begin) > (end));                          \\
       i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define F first
#define S second
#define sz(x) ((int)x.size())
#define pb push_back
#define pf push_front
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define mod 1000000007
#define mod2 998244353
#define what_is(x) cerr << #x << " is " << x << "\\n";
#define sortA(v) sort(v.begin(), v.end())
#define sortD(v) sort(v.rbegin(), v.rend())
#define PI 3.14159265358979323846
#define vout(a)                                                                \\
  for (auto x : a)                                                             \\
    cout << x << " ";
#define INF 1000000000000

// #define ordered_set tree<ll, null_type, less<ll>, rb_tree_tag,
// tree_order_statistics_node_update>

// using namespace __gnu_pbds;
using namespace std;

using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
using mi = multiset<int>;
using mii = multiset<pii>;

int lcm(int a, int b) { return (a * (b / __gcd(a, b))); }

int modp(int a, int m, int n) {
  if (n == 0)
    return 1;
  if (n == 1)
    return a % m;
  int res = modp(a, m, n / 2);
  if (n % 2 == 0)
    return (res * res) % m;
  else
    return (a * res * res) % m;
}

void solve() {
  int n, m, t, i, l, k, r, u, v, w, j, ss, sum = 0, pos, flag = 0, count = 0;
  string S, T, U;
  // int x,y,a,b,c;
  cin >> n >> S;
  int popcnt = 0;
  rep(i, 0, n) popcnt += (S[i] == '1');
  if (!popcnt) {
    rep(i, 0, n) { cout << "1\\n"; }
    return;
  }
  int num = 0, minus1 = 0, plus1 = 0;
  reverse(all(S));
  rep(i, 0, n) {
    num = (num % popcnt + (S[i] - '0') * modp(2, popcnt, i)) % popcnt;
  }
  rep(i, 0, n) {
    plus1 = (plus1 % (popcnt + 1) + (S[i] - '0') * modp(2, popcnt + 1, i)) %
            (popcnt + 1);
    minus1 = (minus1 % (popcnt - 1) + (S[i] - '0') * modp(2, popcnt - 1, i)) %
             (popcnt - 1);
  }
  vi ans(n);
  rep(i, 0, n) {
    if (S[i] == '0') {
      t = (plus1 + modp(2, popcnt + 1, i) + (popcnt + 1)) % (popcnt + 1);
      count = 1;
      while (t) {
        t = t % (__builtin_popcountll(t));
        count++;
      }
    } else {
      t = (minus1 - modp(2, popcnt - 1, i) + (popcnt - 1)) % (popcnt - 1);
      count = 1;
      while (t) {
        t = t % (__builtin_popcountll(t));
        count++;
      }
    }
    ans[i] = count;
  }
  reverse(all(ans));
  for (int x : ans)
    cout << x << "\\n";
}

signed main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  // cin>>t;
  int T = 0;
  while (t--) {
    T++;
    // cout<<"Scenario #"<<T<<":\\n";
    solve();
    if (t)
      cout << "\\n";
  }
  return 0;
}

/*
6 4
1 3 4 5 6 2
*/