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

/*
  Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;

inline ll read() {
  ll f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return f * x;
}

void print(ll x) {
  if (x < 10) {
    putchar(x + 48);
    return;
  }
  print(x / 10);
  putchar(x % 10 + 48);
}

const int N = 5e5 + 10;

char str[N];
int a1[N], a2[N], n;
vector<int> ans;

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  scanf("%d", &n), scanf("%s", str + 1);
  int sz = 0;
  for (int i = 1; i <= n; i++)
    if (str[i] == '1')
      sz++;
  const int mod1 = sz + 1, mod2 = sz - 1;
  a1[0] = a2[0] = 1;
  for (int i = 1; i <= n; i++) {
    a1[i] = (a1[i - 1] * 2) % mod1;
    if (mod2)
      a2[i] = (a2[i - 1] * 2) % mod2;
  }
  int sum1 = 0, sum2 = 0;
  for (int i = 1, j = n - 1; i <= n; i++, j--) {
    sum1 = (sum1 + a1[j] * (str[i] - '0')) % mod1;
    if (mod2)
      sum2 = (sum2 + a2[j] * (str[i] - '0')) % mod2;
  }
  for (int i = 1, j = n - 1; i <= n; i++, j--) {
    int temp;
    if (str[i] == '1') {
      if (mod2 == 0) {
        ans.pb(0);
        continue;
      }
      temp = ((sum2 - a2[j]) % mod2 + mod2) % mod2;
    } else {
      temp = (sum1 + a1[j]) % mod1;
    }
    int num = 1;
    while (temp) {
      int sum = 0;
      for (int j = 0; j < 30; j++)
        if ((temp >> j) & 1)
          sum++;
      num++;
      temp %= sum;
    }
    ans.pb(num);
  }
  for (int i : ans)
    printf("%d\\n", i);
  return 0;
}

Error: 0

Fixed submission:

/*
  Author : lifehappy
*/
#pragma GCC optimize(2)
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;

const double pi = acos(-1.0);
const double eps = 1e-7;
const int inf = 0x3f3f3f3f;

inline ll read() {
  ll f = 1, x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = (x << 1) + (x << 3) + (c ^ 48);
    c = getchar();
  }
  return f * x;
}

void print(ll x) {
  if (x < 10) {
    putchar(x + 48);
    return;
  }
  print(x / 10);
  putchar(x % 10 + 48);
}

const int N = 5e5 + 10;

char str[N];
int a1[N], a2[N], n;
vector<int> ans;

int main() {
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
  // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  scanf("%d", &n), scanf("%s", str + 1);
  // cout << (str + 1) << endl;
  int sz = 0;
  for (int i = 1; i <= n; i++)
    if (str[i] == '1')
      sz++;
  const int mod1 = sz + 1, mod2 = sz - 1;
  a1[0] = a2[0] = 1;
  for (int i = 1; i <= n; i++) {
    a1[i] = (a1[i - 1] * 2) % mod1;
    a2[i] = (a2[i - 1] * 2) % mod2;
  }
  // for(int i = 1; i <= n; i++) {
  //   a1[i] = a1[i - 1] * 2;
  //   a2[i] = a2[i - 1] * 2;
  //   cout << a1[i] << " " << a2[i] << endl;
  // }
  int sum1 = 0, sum2 = 0;
  for (int i = 1, j = n - 1; i <= n; i++, j--) {
    sum1 = (sum1 + a1[j] * (str[i] - '0')) % mod1;
    sum2 = (sum2 + a2[j] * (str[i] - '0')) % mod2;
  }
  // for(int i = 1, j = n - 1; i <= n; i++, j--) {
  //   sum1 = (sum1 + a1[j] * (str[i] - '0'));
  //   sum2 = (sum2 + a2[j] * (str[i] - '0'));
  // }
  // cout << sum1 << " " << sum2 << endl;
  for (int i = 1, j = n - 1; i <= n; i++, j--) {
    int temp;
    if (str[i] == '1') {
      // temp = sum2 - a2[j];
      temp = ((sum2 - a2[j]) % mod2 + mod2) % mod2;
    } else {
      // temp = sum1 + a1[j];
      temp = (sum1 + a1[j]) % mod1;
    }
    // cout << temp << endl;
    int num = 1;
    while (temp) {
      int sum = 0;
      for (int j = 0; j < 30; j++)
        if ((temp >> j) & 1)
          sum++;
      num++;
      temp %= sum;
    }
    ans.pb(num);
  }
  for (int i : ans)
    printf("%d\\n", i);
  return 0;
}