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

#include <bits/stdc++.h>
#define int long long
using namespace std;
int mod1, mod2;

int n, tot = 0, pos1 = 1, pos2 = 1, tot1 = 0, tot2 = 0;
int a[200005];

inline int Popcount(int x) {
  if (x == 0)
    return 0;

  int step = 0;
  while (x != 0) {
    int cnt = 0, k = x;
    while (k != 0) {
      if (k % 2 == 1)
        cnt++;
      k /= 2;
    }
    if (cnt != 0)
      x %= cnt;
    else
      x = 0;

    step++;
  }
  return step;
}

int quick_power1(int x, int y) {
  int res = 1;
  for (; y; y = y >> 1, x = (x * x) % mod1) {
    if (y & 1)
      res = (res * x) % mod1;
  }
  return res;
}

int quick_power2(int x, int y) {
  int res = 1;
  for (; y; y = y >> 1, x = (x * x) % mod2) {
    if (y & 1)
      res = (res * x) % mod2;
  }
  return res;
}

signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    char x;
    cin >> x;
    a[i] = x - '0';
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == 1)
      tot++;
  }
  int flag = 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] != 0) {
      flag = 0;
      break;
    }
  }
  if (flag == 1) {
    for (int i = 1; i <= n; i++)
      cout << 1 << endl;
    return 0;
  }
  mod1 = tot - 1;
  mod2 = tot + 1;

  for (int i = n; i >= 1; i--) {
    if (mod1 != 0)
      tot1 = (tot1 + a[i] * pos1) % mod1, pos1 = (pos1 * 2) % mod1;
    if (mod2 != 0)
      tot2 = (tot2 + a[i] * pos2) % mod2, pos2 = (pos2 * 2) % mod2;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0) // 调用的是:1的个数多1，即mod2的情况
    {
      int now;
      if (mod2 > 0) {
        now = (tot2 + quick_power2(2, n - i)) % mod2;
        cout << Popcount(now) + 1 << endl;
      } else
        cout << 0 << endl;
      // cout<<1<<' '<<now<<endl;
    } else {
      int now;

      if (mod1 > 0) {
        now = ((tot1 - quick_power1(2, n - i)) % mod1 + mod1) % mod1;
        cout << Popcount(now) + 1 << endl;
      } else
        cout << 0 << endl;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int mod1, mod2;

int n, tot = 0, pos1 = 1, pos2 = 1, tot1 = 0, tot2 = 0;
int a[200005];

inline int Popcount(int x) {
  if (x == 0)
    return 0;

  int step = 0;
  while (x != 0) {
    int cnt = 0, k = x;
    while (k != 0) {
      if (k % 2 == 1)
        cnt++;
      k /= 2;
    }
    x %= cnt;
    step++;
  }
  return step;
}

int quick_power1(int x, int y) {
  int res = 1;
  for (; y; y = y >> 1, x = (x * x) % mod1) {
    if (y & 1)
      res = (res * x) % mod1;
  }
  return res;
}

int quick_power2(int x, int y) {
  int res = 1;
  for (; y; y = y >> 1, x = (x * x) % mod2) {
    if (y & 1)
      res = (res * x) % mod2;
  }
  return res;
}

signed main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    char x;
    cin >> x;
    a[i] = x - '0';
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == 1)
      tot++;
  }
  int flag = 1;
  for (int i = 1; i <= n; i++) {
    if (a[i] != 0) {
      flag = 0;
      break;
    }
  }
  if (flag == 1) {
    for (int i = 1; i <= n; i++)
      cout << 0 << endl;
    return 0;
  }
  mod1 = tot - 1;
  mod2 = tot + 1;

  for (int i = n; i >= 1; i--) {
    tot1 = (tot1 + a[i] * pos1) % mod1, tot2 = (tot2 + a[i] * pos2) % mod2;
    pos1 = (pos1 * 2) % mod1, pos2 = (pos2 * 2) % mod2;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i] == 0) // 调用的是:1的个数多1，即mod2的情况
    {
      int now = (tot2 + quick_power2(2, n - i)) % mod2;
      // cout<<1<<' '<<now<<endl;

      cout << Popcount(now) + 1 << endl;
    } else {
      int now = ((tot1 - quick_power1(2, n - i)) % mod1 + mod1) % mod1;

      // cout<<2<<' '<<now<<endl;
      cout << Popcount(now) + 1 << endl;
    }
  }
  return 0;
}