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

#include <algorithm>
#include <assert.h>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define ALL(c) (c).begin(), (c).end()
#define PB push_back
#define IN(x, c) (find(c.begin(), c.end(), x) != (c).end())
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define INIT(a, v) memset(a, v, sizeof(a))
#define SORT_UNIQUE(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
template <class A, class B> A cvt(B x) {
  stringstream ss;
  ss << x;
  A y;
  ss >> y;
  return y;
}

#define SPC << " " <<
#define DEBUG(x)                                                               \\
  cerr << #x << " = ";                                                         \\
  cerr << x << endl;
#define DEBUG_ITER(x)                                                          \\
  cerr << #x << " = ";                                                         \\
  for (auto _ : x)                                                             \\
    cerr << _ << ' ';                                                          \\
  cerr << endl;

typedef pair<int, int> PII;
typedef long long int64;
typedef vector<int> VI;

int solve(int n) {
  if (n == 0)
    return 0;
  int s = 0, x = n;
  while (x != 0) {
    s += x % 2;
    x /= 2;
  }
  return 1 + solve(n % s);
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  int q = 0;
  REP(i, n) q += s[i] == '1';
  int p = q - 1, r = q + 1;
  VI p2(n), q2(n), r2(n);
  p2[0] = q2[0] = r2[0] = 1;
  FOR(i, 1, n - 1) {
    if (p != 0)
      p2[i] = (p2[i - 1] * 2) % p;
    if (q != 0)
      q2[i] = (q2[i - 1] * 2) % q;
    if (r != 0)
      r2[i] = (r2[i - 1] * 2) % r;
  }
  int xp = 0, xq = 0, xr = 0;
  REP(i, n) {
    if (p != 0)
      xp = (xp + p2[n - 1 - i] * (s[i] == '1')) % p;
    if (q != 0)
      xq = (xq + q2[n - 1 - i] * (s[i] == '1')) % q;
    if (r != 0)
      xr = (xr + r2[n - 1 - i] * (s[i] == '1')) % r;
  }
  REP(i, n) {
    int a;
    int ones = q;
    if (s[i] == '1')
      ones--;
    else
      ones++;
    if (ones == 0)
      printf("%d\\n", 0);
    else {
      if (s[i] == '1') {
        a = (xp - p2[n - 1 - i] + p) % p;
      } else {
        a = (xr + r2[n - 1 - i] + r) % r;
      }
      printf("%d\\n", 1 + solve(a));
    }
  }
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

#define ALL(c) (c).begin(), (c).end()
#define PB push_back
#define IN(x, c) (find(c.begin(), c.end(), x) != (c).end())
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define INIT(a, v) memset(a, v, sizeof(a))
#define SORT_UNIQUE(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
template <class A, class B> A cvt(B x) {
  stringstream ss;
  ss << x;
  A y;
  ss >> y;
  return y;
}

#define SPC << " " <<
#define DEBUG(x)                                                               \\
  cerr << #x << " = ";                                                         \\
  cerr << x << endl;
#define DEBUG_ITER(x)                                                          \\
  cerr << #x << " = ";                                                         \\
  for (auto _ : x)                                                             \\
    cerr << _ << ' ';                                                          \\
  cerr << endl;

typedef pair<int, int> PII;
typedef long long int64;
typedef vector<int> VI;

int solve(int n) {
  if (n == 0)
    return 0;
  int s = 0, x = n;
  while (x != 0) {
    s += x % 2;
    x /= 2;
  }
  return 1 + solve(n % s);
}

int main() {
  int n;
  string s;
  cin >> n >> s;
  int q = 0;
  REP(i, n) q += s[i] == '1';
  int p = q - 1, r = q + 1;
  VI p2(n), q2(n), r2(n);
  p2[0] = q2[0] = r2[0] = 1;
  FOR(i, 1, n - 1) {
    p2[i] = (p2[i - 1] * 2) % p;
    q2[i] = (q2[i - 1] * 2) % q;
    r2[i] = (r2[i - 1] * 2) % r;
  }
  int xp = 0, xq = 0, xr = 0;
  REP(i, n) {
    xp = (xp + p2[n - 1 - i] * (s[i] == '1')) % p;
    xq = (xq + q2[n - 1 - i] * (s[i] == '1')) % q;
    xr = (xr + r2[n - 1 - i] * (s[i] == '1')) % r;
  }
  REP(i, n) {
    int a;
    int ones = q;
    if (s[i] == '1') {
      ones--;
      a = (xp - p2[n - 1 - i] + p) % p;
    } else {
      ones++;
      a = (xr + r2[n - 1 - i] + r) % r;
    }
    if (ones == 0)
      printf("%d\\n", 0);
    else
      printf("%d\\n", 1 + solve(a));
  }
  return 0;
}