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

#include <algorithm>
#include <cmath>
#include <cstdio>
#define re register
const int M = 200010;
int m, sum = 0, ans = 0, sum1 = 0, sum2 = 0;
int num[M], mod1[M], mod2[M], mod[M], bit[M], cal[M];
template <typename T> inline void read(T &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch <= '9' && ch >= '0'; ch = getchar())
    x = x * 10 + ch - '0';
  x *= f;
}
inline void pre() {
  for (re int i = 1; i <= m; i++)
    bit[i] = bit[i >> 1] + (i & 1);
  for (re int i = 1; i <= m; i++)
    cal[i] = cal[i % bit[i]] + 1;
}
int main() {
  read(m);
  pre(); // 预处理popcount(m以内的)
  for (re int i = 1; i <= m; i++) {
    char ch = getchar();
    for (; ch != '0' && ch != '1'; ch = getchar())
      ;
    num[i] = ch - '0';
    sum += num[i];
  }
  if (sum == 0) {
    for (re int i = 1; i <= m; i++)
      printf("1\\n");
    return 0;
  }
  if (sum == 1) {
    for (re int i = 1; i <= m; i++)
      if (num[i] == 1)
        printf("0\\n");
      else if (num[m] == 1 || i == m)
        printf("2\\n");
      else
        printf("1\\n");
    return 0;
  }
  mod1[1] = 1;
  for (re int i = 2; i <= m; i++)
    mod1[i] = (mod1[i - 1] << 1) % (sum - 1);
  mod2[1] = 1;
  for (re int i = 2; i <= m; i++)
    mod2[i] = (mod2[i - 1] << 1) % (sum + 1);
  mod[1] = 1;
  for (re int i = 2; i <= m; i++)
    mod[i] = (mod[i - 1] << 1) % sum;
  for (re int i = 1; i <= m; i++)
    if (num[i])
      (ans += mod[m - i + 1]) %= sum, (sum1 += mod1[m - i + 1]) %= (sum - 1),
          (sum2 += mod2[m - i + 1]) %= (sum + 1);
  for (re int i = 1, tot; i <= m; i++)
    if (num[i] == 1)
      if (sum == 1)
        printf("1\\n");
      else
        tot = (sum1 - mod1[m - i + 1] + (sum - 1)) % (sum - 1),
        printf("%d\\n", cal[tot % (sum - 1)] + 1);
    else
      tot = (sum2 + mod2[m - i + 1]) % (sum + 1),
      printf("%d\\n", cal[tot % (sum + 1)] + 1);
  return 0;
}
/*
3
011

2
1
1

3
001

2
2
0

23
00110111001011011001110

2
1
2
2
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
3
*/

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#define re register
const int M = 200010;
int m, sum = 0, ans = 0, sum1 = 0, sum2 = 0;
int num[M], mod1[M], mod2[M], mod[M], bit[M], cal[M];
template <typename T> inline void read(T &x) {
  x = 0;
  char ch = getchar();
  int f = 1;
  for (; ch > '9' || ch < '0'; ch = getchar())
    if (ch == '-')
      f = -1;
  for (; ch <= '9' && ch >= '0'; ch = getchar())
    x = x * 10 + ch - '0';
  x *= f;
}
inline void pre() {
  for (re int i = 1; i <= m; i++)
    bit[i] = bit[i >> 1] + (i & 1);
  for (re int i = 1; i <= m; i++)
    cal[i] = cal[i % bit[i]] + 1;
}
int main() {
  read(m);
  pre(); // 预处理popcount(m以内的)
  for (re int i = 1; i <= m; i++) {
    char ch = getchar();
    for (; ch != '0' && ch != '1'; ch = getchar())
      ;
    num[i] = ch - '0';
    sum += num[i];
  }
  if (sum == 0) {
    for (re int i = 1; i <= m; i++)
      printf("1\\n");
    return 0;
  }
  if (sum == 1) {
    for (re int i = 1; i <= m; i++)
      if (num[i] == 1)
        printf("0\\n");
      else if (num[m] == 1 || i == m)
        printf("2\\n");
      else
        printf("1\\n");
  }
  mod1[1] = 1;
  for (re int i = 2; i <= m; i++)
    mod1[i] = (mod1[i - 1] << 1) % (sum - 1);
  mod2[1] = 1;
  for (re int i = 2; i <= m; i++)
    mod2[i] = (mod2[i - 1] << 1) % (sum + 1);
  mod[1] = 1;
  for (re int i = 2; i <= m; i++)
    mod[i] = (mod[i - 1] << 1) % sum;
  for (re int i = 1; i <= m; i++)
    if (num[i])
      (ans += mod[m - i + 1]) %= sum, (sum1 += mod1[m - i + 1]) %= (sum - 1),
          (sum2 += mod2[m - i + 1]) %= (sum + 1);
  for (re int i = 1, tot; i <= m; i++)
    if (num[i] == 1)
      if (sum == 1)
        printf("1\\n");
      else
        tot = (sum1 - mod1[m - i + 1] + (sum - 1)) % (sum - 1),
        printf("%d\\n", cal[tot % (sum - 1)] + 1);
    else
      tot = (sum2 + mod2[m - i + 1]) % (sum + 1),
      printf("%d\\n", cal[tot % (sum + 1)] + 1);
  return 0;
}
/*
3
011

2
1
1

3
001



23
00110111001011011001110

2
1
2
2
1
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
2
1
3
*/