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

#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>

#define start                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
#define int ll
#define ls st << 1
#define rs st << 1 | 1
#define pii pair<int, int>
#define rep(z, x, y) for (int z = x; z <= y; ++z)
#define repd(z, x, y) for (int z = y; z >= x; --z)
#define com bool operator<(const node &b)
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int maxn = (ll)3e5 + 5;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int T = 1;
int num1, num2;

int now = 0;
int f[maxn];

void f1(int val) {
  int tmp = (num1 + val) % (now + 1);
  cout << f[tmp] + 1 << '\\n';
}

void f2(int val) {
  int tmp = ((num2 + val) % (now - 1) + now - 1) % (now - 1);
  cout << f[tmp] + 1 << '\\n';
}

int fac1[maxn];
int fac2[maxn];

void solve() {
  rep(i, 1, 2e5) {
    int t = i;
    int s = __builtin_popcount(t);
    t %= s;
    f[i] = f[t] + 1;
  }
  string s;
  int n;
  cin >> n;
  fac1[0] = fac2[0] = 1;
  cin >> s;
  for (auto &i : s)
    now += (i == '1');
  if (now == 0) {
    rep(i, 1, n) cout << 1 << '\\n';
    return;
  }
  if (now == 1) {
    if (s[s.size() - 1] == '1') {
      rep(i, 1, n - 1) cout << 2 << '\\n';
      cout << 0 << '\\n';
    } else {
      rep(i, 0, n - 1) {
        if (s[i] == '1')
          cout << 0 << '\\n';
        else if (i != n - 1)
          cout << 1 << '\\n';
        else if (i == n - 1)
          cout << 2 << '\\n';
      }
    }
    return;
  }
  rep(i, 1, n) {
    fac1[i] = fac1[i - 1] * 2 % (now + 1);
    fac2[i] = fac2[i - 1] * 2 % (now - 1);
  }
  for (auto &i : s) {
    num1 = num1 * 2 + (i == '1');
    num2 = num2 * 2 + (i == '1');
    num1 %= (now + 1);
    num2 %= (now - 1);
  }
  for (int i = 0, j = n - 1; i < n; ++i, --j) {
    if (s[i] == '1')
      f2(-fac2[j]);
    else
      f1(fac1[j]);
  }
}

signed main() {
  start;
  while (T--)
    solve();
  return 0;
}

Error: 0

Fixed submission:

#pragma GCC optimize(3, "Ofast", "inline")

#include <bits/stdc++.h>

#define start                                                                  \\
  ios::sync_with_stdio(false);                                                 \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define ll long long
// #define int ll
#define ls st << 1
#define rs st << 1 | 1
#define pii pair<int, int>
#define rep(z, x, y) for (int z = x; z <= y; ++z)
#define repd(z, x, y) for (int z = y; z >= x; --z)
#define com bool operator<(const node &b)
using namespace std;
mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count());
const int maxn = (ll)3e5 + 5;
const int mod = 998244353;
const int inf = 0x3f3f3f3f;
int T = 1;
int num1, num2;

int now = 0;
int f[maxn];

void f1(int val) {
  int tmp = (num1 + val) % (now + 1);
  cout << f[tmp] + 1 << '\\n';
}

void f2(int val) {
  int tmp = ((num2 + val) % (now - 1) + now - 1) % (now - 1);
  cout << f[tmp] + 1 << '\\n';
}

void solve() {
  rep(i, 1, 2e5) {
    int t = i;
    int s = __builtin_popcount(t);
    t %= s;
    f[i] = f[t] + 1;
  }
  string s;
  int n;
  cin >> n;
  cin >> s;
  for (auto &i : s)
    now += (i == '1');
  if (now == 0) {
    rep(i, 1, n) cout << 1 << '\\n';
    return;
  }
  if (now == 1) {
    if (s[s.size() - 1] == '1') {
      rep(i, 1, n - 1) cout << 2 << '\\n';
      cout << 0 << '\\n';
    } else {
      rep(i, 0, n - 1) {
        if (s[i] == '1')
          cout << 0 << '\\n';
        else
          cout << 1 << '\\n';
      }
    }
    return;
  }
  for (auto &i : s) {
    num1 = num1 * 2 + (i == '1');
    num2 = num2 * 2 + (i == '1');
    num1 %= (now + 1);
    num2 %= (now - 1);
  }
  for (int i = 0, j = n - 1; i < n; ++i, --j) {
    if (s[i] == '1')
      f2(-(1ll << j));
    else
      f1(1ll << j);
  }
}

signed main() {
  start;
  while (T--)
    solve();
  return 0;
}