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

#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i, a, b) for (int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef vector<ii> vii;
typedef vector<vi> vvi;
#define deb(x) cout << #x ": " << (x) << endl;

const int N = 1e3;
const lli INF = 1e18;
const lli MOD = 1e9 + 7;

bool m[N];
vi primes(1, 2);
ld nk[N][N];
void sieve() {
  for (int i = 4; i < N; i += 2)
    m[i] = 1;
  for (int i = 3; i * i <= N; i += 2)
    if (!m[i])
      for (int j = i * i; j < N; j += i)
        m[j] = 1;
  for (int i = 3; i < N; i += 2)
    if (!m[i])
      primes.pb(i);
}

void pascal() {
  fore(i, 0, N) nk[i][0] = nk[i][i] = 1.0;
  fore(i, 1, N) fore(j, 1, i) nk[i][j] = nk[i - 1][j - 1] + nk[i - 1][j];
}

lli gcd(lli a, lli b) { return (b ? gcd(b, a % b) : a); }

lli lcm(lli a, lli b) {
  if (a < b)
    swap(a, b);
  lli c = gcd(a, b);
  a /= c;
  return a * b;
}

int popcount(lli x) { return __builtin_popcountll(x); }

lli poww(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}

lli powm(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}

bool isPrime(lli x) {
  if (x == 1)
    return 0;
  for (auto i : primes) {
    if (i * i > x)
      return 1;
    if (x % i == 0)
      return 0;
  }
  return 1;
}

// ---- コーディングはここから！ ('-')7

string s;
int n, u;
void R() {
  if (u == 0)
    fore(i, 0, n) cout << 1 << ENDL;
  else {
    vi sol(n);
    if (s[0] == '1') {
      sol[0] = 0;
      fore(i, 1, n) sol[i] = 2;
    } else
      fore(i, 0, n) if (s[i] == '1') sol[i] = 0;
    else sol[i] = 1;
    if (s[0] == '0')
      sol[0] = 2;
    reverse(all(sol));
    for (auto i : sol)
      cout << i << ENDL;
  }
  exit(0);
}

int main() {
  _ cin >> n;
  cin >> s;
  reverse(all(s));
  u = 0;
  fore(i, 0, sz(s)) if (s[i] == '1') u++;
  if (u < 2)
    R();
  vi r(n + 1), t(n + 1), res(n);
  int X = 0, Y = 0;
  r[0] = 1 % (u + 1);
  t[0] = 1 % (u - 1);
  int p = 1, q = 1;
  fore(i, 0, n) {
    if (i == 0) {
      if (s[i] == '1') {
        X = (X + r[i]) % (u + 1);
        Y = (t[i] + Y) % (u - 1);
      }
      continue;
    }
    r[i] = p = (p * 2) % (u + 1); // cambiar de 0 a 1
    t[i] = q = (q * 2) % (u - 1); // cambiar de 1 a 0
    if (s[i] == '1') {
      X = (X + r[i]) % (u + 1);
      Y = (Y + t[i]) % (u - 1);
    }
  }
  fore(i, 0, n) {
    if (s[i] == '1')
      res[i] = (Y - t[i] + (u - 1)) % (u - 1);
    else
      res[i] = (X + r[i] + (u + 1)) % (u + 1);
  }
  vi sol(n, 1);
  fore(i, 0, n) {
    while (res[i]) {
      res[i] = res[i] % popcount(res[i]);
      sol[i]++;
    }
  }
  reverse(all(sol));
  for (auto i : sol)
    cout << i << ENDL;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i, a, b) for (int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _                                                                      \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef vector<ii> vii;
typedef vector<vi> vvi;
#define deb(x) cout << #x ": " << (x) << endl;

const int N = 1e3;
const lli INF = 1e18;
const lli MOD = 1e9 + 7;

bool m[N];
vi primes(1, 2);
ld nk[N][N];
void sieve() {
  for (int i = 4; i < N; i += 2)
    m[i] = 1;
  for (int i = 3; i * i <= N; i += 2)
    if (!m[i])
      for (int j = i * i; j < N; j += i)
        m[j] = 1;
  for (int i = 3; i < N; i += 2)
    if (!m[i])
      primes.pb(i);
}

void pascal() {
  fore(i, 0, N) nk[i][0] = nk[i][i] = 1.0;
  fore(i, 1, N) fore(j, 1, i) nk[i][j] = nk[i - 1][j - 1] + nk[i - 1][j];
}

lli gcd(lli a, lli b) { return (b ? gcd(b, a % b) : a); }

lli lcm(lli a, lli b) {
  if (a < b)
    swap(a, b);
  lli c = gcd(a, b);
  a /= c;
  return a * b;
}

int popcount(lli x) { return __builtin_popcountll(x); }

lli poww(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = res * a;
    a = a * a;
    b /= 2;
  }
  return res;
}

lli powm(lli a, lli b) {
  lli res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % MOD;
    a = (a * a) % MOD;
    b /= 2;
  }
  return res;
}

bool isPrime(lli x) {
  if (x == 1)
    return 0;
  for (auto i : primes) {
    if (i * i > x)
      return 1;
    if (x % i == 0)
      return 0;
  }
  return 1;
}

// ---- コーディングはここから！ ('-')7

int main() {
  _ int n;
  cin >> n;
  string s;
  cin >> s;
  reverse(all(s));
  int u = 0;
  fore(i, 0, sz(s)) if (s[i] == '1') u++;
  vi r(n + 1), t(n + 1), res(n);
  int X = 0, Y = 0;
  r[0] = 1 % (u + 1);
  t[0] = 1 % (u - 1);
  int p = 1, q = 1;
  fore(i, 0, n) {
    if (i == 0) {
      if (s[i] == '1') {
        X = (X + r[i]) % (u + 1);
        Y = (t[i] + Y) % (u - 1);
      }
      continue;
    }
    r[i] = p = (p * 2) % (u + 1); // cambiar de 0 a 1
    t[i] = q = (q * 2) % (u - 1); // cambiar de 1 a 0
    if (s[i] == '1') {
      X = (X + r[i]) % (u + 1);
      Y = (Y + t[i]) % (u - 1);
    }
  }
  fore(i, 0, n) {
    if (s[i] == '1')
      res[i] = (Y - t[i] + (u - 1)) % (u - 1);
    else
      res[i] = (X + r[i] + (u + 1)) % (u + 1);
  }
  vi sol(n, 1);
  fore(i, 0, n) {
    while (res[i]) {
      res[i] = res[i] % popcount(res[i]);
      sol[i]++;
    }
  }
  reverse(all(sol));
  for (auto i : sol)
    cout << i << ENDL;
}