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

#include <bits/stdc++.h>
using namespace std;

#define fastio                                                                 \\
  {                                                                            \\
    ios::sync_with_stdio(0);                                                   \\
    cin.tie(0);                                                                \\
  }

#define file                                                                   \\
  {                                                                            \\
    freopen("io\\\\in.txt", "r", stdin);                                         \\
    freopen("io\\\\out.txt", "w", stdout);                                       \\
    freopen("io\\\\err.txt", "w", stderr);                                       \\
  }

#ifdef LOCAL
#define debug(args...)                                                         \\
  {                                                                            \\
    string _s(#args);                                                          \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    debug1(_it, args);                                                         \\
  }

#pragma GCC diagnostic ignored "-Wunused-parameter"
void debug1(istream_iterator<string> it) { cerr << endl; }
#pragma GCC diagnostic pop

template <class T, class... Args>
void debug1(istream_iterator<string> it, T a, Args... args) {
  cerr << '>' << *it << " = " << a << "; ";
  debug1(++it, args...);
}
#else
#define debug(args...)
#define cerr                                                                   \\
  if (false)                                                                   \\
  cerr
#endif

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using si = set<int>;
using ii = pair<int, int>;
using vii = vector<ii>;

#define rep(i, n) for (int i = 0; i < n; i++)
#define dotc()                                                                 \\
  int tcs;                                                                     \\
  if (cin >> tcs)                                                              \\
  rep(tc, tcs)
#define sz(x) (int)x.size()
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second

const int inf(0x3fffffff);
const int mod(1000000007);
const double pi(acos(-1.0));

signed main() {
  fastio;
#ifdef LOCAL
  file;
#endif

  int n;
  string s;
  cin >> n >> s;
  int pcnt = count(all(s), '1');

  vi a(n);
  rep(i, n) if (s[i] == '1') a[i] = 1;

  vi d1, d2;
  int t = pcnt + 1;
  while (t) {
    d1.pb(t & 1);
    t /= 2;
  }
  reverse(all(d1));

  if (pcnt > 1) {
    t = pcnt - 1;
    while (t) {
      d2.pb(t & 1);
      t /= 2;
    }
    reverse(all(d2));
  }

  vi a1(a);
  for (int i = 0; i < n - sz(d1) + 1; i++) {
    int x = 0;
    for (int j = 0; j <= sz(d1) && i + sz(d1) - j - 1 >= 0; j++) {
      x |= (a1[i + sz(d1) - j - 1] & 1) << j;
    }

    if (x >= pcnt + 1) {
      x -= pcnt + 1;
      for (int j = 0; j <= sz(d1) && i + sz(d1) - j - 1 >= 0; j++) {
        a1[i + sz(d1) - j - 1] = x >> j & 1;
      }
    }
  }

  vi a2(a);
  if (pcnt > 1) {
    for (int i = 0; i < n - sz(d2) + 1; i++) {
      int x = 0;
      for (int j = 0; j <= sz(d2) && i + sz(d2) - j - 1 >= 0; j++) {
        x |= (a2[i + sz(d2) - j - 1] & 1) << j;
      }

      if (x >= pcnt - 1) {
        x -= pcnt - 1;
        for (int j = 0; j <= sz(d2) && i + sz(d2) - j - 1 >= 0; j++) {
          a2[i + sz(d2) - j - 1] = x >> j & 1;
        }
      }
    }
  }

  int b1 = 0, b2 = 0;
  for (int i = 0; i < min(sz(a1), 24); i++) {
    b1 |= (a1[sz(a1) - i - 1] & 1) << i;
  }

  for (int i = 0; i < min(sz(a2), 24); i++) {
    b2 |= (a2[sz(a2) - i - 1] & 1) << i;
  }

  vi ret(n);
  int p1 = 1, p2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (pcnt == 1 && a[i] & 1)
      ret[i] = 0;
    else {
      int cur;
      if (a[i] & 1)
        cur = ((b2 - p2) % (pcnt - 1) + pcnt - 1) % (pcnt - 1);
      else
        cur = (b1 + p1) % (pcnt + 1);

      int k = 1;
      while (cur) {
        int cpcnt = __builtin_popcount(cur);
        cur = cur % cpcnt;
        k++;
      }

      ret[i] = k;
    }

    p1 = (p1 * 2) % (pcnt + 1);
    if (pcnt > 1)
      p2 = (p2 * 2) % (pcnt - 1);
  }

  rep(i, n) cout << ret[i] << '\\n';

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define fastio                                                                 \\
  {                                                                            \\
    ios::sync_with_stdio(0);                                                   \\
    cin.tie(0);                                                                \\
  }

#define file                                                                   \\
  {                                                                            \\
    freopen("io\\\\in.txt", "r", stdin);                                         \\
    freopen("io\\\\out.txt", "w", stdout);                                       \\
    freopen("io\\\\err.txt", "w", stderr);                                       \\
  }

#ifdef LOCAL
#define debug(args...)                                                         \\
  {                                                                            \\
    string _s(#args);                                                          \\
    replace(_s.begin(), _s.end(), ',', ' ');                                   \\
    stringstream _ss(_s);                                                      \\
    istream_iterator<string> _it(_ss);                                         \\
    debug1(_it, args);                                                         \\
  }

#pragma GCC diagnostic ignored "-Wunused-parameter"
void debug1(istream_iterator<string> it) { cerr << endl; }
#pragma GCC diagnostic pop

template <class T, class... Args>
void debug1(istream_iterator<string> it, T a, Args... args) {
  cerr << '>' << *it << " = " << a << "; ";
  debug1(++it, args...);
}
#else
#define debug(args...)
#define cerr                                                                   \\
  if (false)                                                                   \\
  cerr
#endif

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using si = set<int>;
using ii = pair<int, int>;
using vii = vector<ii>;

#define rep(i, n) for (int i = 0; i < n; i++)
#define dotc()                                                                 \\
  int tcs;                                                                     \\
  if (cin >> tcs)                                                              \\
  rep(tc, tcs)
#define sz(x) (int)x.size()
#define eb emplace_back
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fi first
#define se second

const int inf(0x3fffffff);
const int mod(1000000007);
const double pi(acos(-1.0));

signed main() {
  fastio;
#ifdef LOCAL
  file;
#endif

  int n;
  string s;
  cin >> n >> s;
  int pcnt = count(all(s), '1');

  vi a(n);
  rep(i, n) if (s[i] == '1') a[i] = 1;

  vi d1, d2;
  int t = pcnt + 1;
  while (t) {
    d1.pb(t & 1);
    t /= 2;
  }
  reverse(all(d1));

  t = pcnt - 1;
  while (t) {
    d2.pb(t & 1);
    t /= 2;
  }
  reverse(all(d2));

  vi a1(a);
  for (int i = 0; i < n - sz(d1) + 1; i++) {
    int x = 0;
    for (int j = 0; j <= sz(d1) && i + sz(d1) - j - 1 >= 0; j++) {
      x |= (a1[i + sz(d1) - j - 1] & 1) << j;
    }

    if (x >= pcnt + 1) {
      x -= pcnt + 1;
      for (int j = 0; j <= sz(d1) && i + sz(d1) - j - 1 >= 0; j++) {
        a1[i + sz(d1) - j - 1] = x >> j & 1;
      }
    }
  }

  vi a2(a);
  for (int i = 0; i < n - sz(d2) + 1; i++) {
    int x = 0;
    for (int j = 0; j <= sz(d2) && i + sz(d2) - j - 1 >= 0; j++) {
      x |= (a2[i + sz(d2) - j - 1] & 1) << j;
    }

    if (x >= pcnt - 1) {
      x -= pcnt - 1;
      for (int j = 0; j <= sz(d2) && i + sz(d2) - j - 1 >= 0; j++) {
        a2[i + sz(d2) - j - 1] = x >> j & 1;
      }
    }
  }

  int b1 = 0, b2 = 0;
  for (int i = 0; i < min(sz(a1), 24); i++) {
    b1 |= (a1[sz(a1) - i - 1] & 1) << i;
  }

  for (int i = 0; i < min(sz(a2), 24); i++) {
    b2 |= (a2[sz(a2) - i - 1] & 1) << i;
  }

  vi ret(n);
  int p1 = 1, p2 = 1;
  for (int i = n - 1; i >= 0; i--) {
    if (pcnt == 1 && a[i] & 1)
      ret[i] = 0;

    int cur;
    if (a[i] & 1)
      cur = ((b2 - p2) % (pcnt - 1) + pcnt - 1) % (pcnt - 1);
    else
      cur = (b1 + p1) % (pcnt + 1);

    int k = 1;
    while (cur) {
      int cpcnt = __builtin_popcount(cur);
      cur = cur % cpcnt;
      k++;
    }

    ret[i] = k;

    p1 = (p1 * 2) % (pcnt + 1);
    p2 = (p2 * 2) % (pcnt - 1);
  }

  rep(i, n) cout << ret[i] << '\\n';

  return 0;
}