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

#include <bits/stdc++.h>
#define ll long long
#define ls id << 1
#define rs id << 1 | 1
#define mem(array, value, size, type)                                          \\
  memset(array, value, ((size) + 5) * sizeof(type))
#define memarray(array, value) memset(array, value, sizeof(array))
#define pb(x) push_back(x)
#define st(x) (1LL << (x))
#define pii pair<int, int>
#define mp(a, b) make_pair((a), (b))
#define Flush fflush(stdout)
using namespace std;
const int N = 500050;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f;
const ll mod = 998244353LL;
clock_t TIME_START, TIME_END;
void program_end() {
#ifdef ONLINE
  printf("\\nTime used: %.6lf(s)\\n",
         ((double)TIME_END - TIME_START) / CLOCKS_PER_SEC);
  system("pause");
#endif
}
ll mi(ll a, ll b, ll nowmod) {
  if (nowmod == 0)
    return 0;
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % nowmod;
    a = a * a % nowmod;
    b >>= 1;
  }
  return ret;
}
int f(ll x) {
  int ret = 0;
  while (x) {
    x %= __builtin_popcount(x);
    ret++;
  }
  return ret;
}
int n;
char a[N];
int one;
ll sum1, sum2;
void pre1() {
  for (int i = 0; i < n; ++i) {
    if (a[i] == '0')
      continue;
    ll now = mi(2, n - i - 1, one + 1);
    sum1 += now, sum1 %= one + 1;
  }
}
void pre2() {
  for (int i = 0; i < n; ++i) {
    if (a[i] == '0')
      continue;
    ll now = mi(2, n - i - 1, one - 1);
    sum2 += now;
    if (one - 1)
      sum2 %= one - 1;
  }
}

void solve() {
  cin >> n;
  scanf("%s", a);
  for (int i = 0; i < n; ++i)
    if (a[i] == '1')
      one++;
  if (one == 0) {
    for (int i = 1; i <= n; ++i) {
      printf("1\\n");
    }
    return;
  }
  pre1();
  pre2();
  for (int i = 0; i < n; ++i) {
    if (a[i] == '0') {
      a[i] = '1';
      ll tmp = mi(2, n - i - 1, one + 1);
      sum1 += tmp;
      sum1 %= one + 1;
      ll ans = 1;
      ans += f(sum1);
      sum1 -= tmp;
      sum1 += one + 1, sum1 %= one + 1;
      a[i] = '0';
      cout << ans << '\\n';
    } else {
      a[i] = '0';
      ll tmp = mi(2, n - i - 1, one - 1);
      sum2 -= tmp;
      sum2 += one - 1;
      if (one - 1)
        sum2 %= one - 1;
      ll ans = (one - 1 > 0);
      ans += f(sum2);
      sum2 += tmp;
      sum2 += one - 1;
      if (one - 1)
        sum2 %= one - 1;
      a[i] = '1';
      cout << ans << '\\n';
    }
  }
}

int main() {
  TIME_START = clock();
  int Test = 1;
  // cin >> Test;
  while (Test--)
    solve();
  TIME_END = clock();
  program_end();
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define ls id << 1
#define rs id << 1 | 1
#define mem(array, value, size, type)                                          \\
  memset(array, value, ((size) + 5) * sizeof(type))
#define memarray(array, value) memset(array, value, sizeof(array))
#define pb(x) push_back(x)
#define st(x) (1LL << (x))
#define pii pair<int, int>
#define mp(a, b) make_pair((a), (b))
#define Flush fflush(stdout)
using namespace std;
const int N = 500050;
const int inf = 0x3f3f3f3f;
const ll llinf = 0x3f3f3f3f3f3f;
const ll mod = 998244353LL;
clock_t TIME_START, TIME_END;
void program_end() {
#ifdef ONLINE
  printf("\\nTime used: %.6lf(s)\\n",
         ((double)TIME_END - TIME_START) / CLOCKS_PER_SEC);
  system("pause");
#endif
}
ll mi(ll a, ll b, ll nowmod) {
  ll ret = 1;
  while (b) {
    if (b & 1)
      ret = ret * a % nowmod;
    a = a * a % nowmod;
    b >>= 1;
  }
  return ret;
}
int f(ll x) {
  int ret = 0;
  while (x) {
    x %= __builtin_popcount(x);
    ret++;
  }
  return ret;
}
int n;
char a[N];
int one;
ll sum1, sum2;
void pre1() {
  for (int i = 0; i < n; ++i) {
    if (a[i] == '0')
      continue;
    ll now = mi(2, n - i - 1, one + 1);
    sum1 += now, sum1 %= one + 1;
  }
}
void pre2() {
  for (int i = 0; i < n; ++i) {
    if (a[i] == '0')
      continue;
    ll now = mi(2, n - i - 1, one - 1);
    sum2 += now, sum2 %= one - 1;
  }
}

void solve() {
  cin >> n;
  scanf("%s", a);
  for (int i = 0; i < n; ++i)
    if (a[i] == '1')
      one++;
  if (one == 0) {
    for (int i = 1; i <= n; ++i) {
      printf("1\\n");
    }
    return;
  }
  pre1();
  pre2();
  for (int i = 0; i < n; ++i) {
    if (a[i] == '0') {
      a[i] = '1';
      ll tmp = mi(2, n - i - 1, one + 1);
      sum1 += tmp;
      sum1 %= one + 1;
      ll ans = 1;
      ans += f(sum1);
      sum1 -= tmp;
      sum1 += one + 1, sum1 %= one + 1;
      a[i] = '0';
      cout << ans << '\\n';
    } else {
      a[i] = '0';
      ll tmp = mi(2, n - i - 1, one - 1);
      sum2 -= tmp;
      sum2 += one - 1;
      sum2 %= one - 1;
      ll ans = 1;
      ans += f(sum2);
      sum2 += tmp;
      sum2 += one - 1, sum2 %= one - 1;
      a[i] = '1';
      cout << ans << '\\n';
    }
  }
}

int main() {
  TIME_START = clock();
  int Test = 1;
  // cin >> Test;
  while (Test--)
    solve();
  TIME_END = clock();
  program_end();
  return 0;
}