Problem p02972 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define sz(x) ((int)x.size())
#define rd(type, ...)                                                          \
  type __VA_ARGS__;                                                            \
  sc(__VA_ARGS__)
#define rdv(type, v, n)                                                        \
  vector<type> v(n);                                                           \
  rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w)                                                     \
  vector<vector<type>>(h, vector<type>(w)) v;                                  \
  rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
typedef long long ll;
void solve();
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(12);
  solve();
  return 0;
}
template <typename T> void prv(vector<T> v) {
  rep(__ii, 0, sz(v)) {
    if (__ii)
      cout << ' ';
    cout << v[__ii];
  }
  cout << '\n';
}
template <typename T> void prm(vector<vector<T>> v) {
  rep(__ii, 0, sz(v)) {
    rep(__jj, 0, v[__ii].size()) {
      if (__jj)
        cout << ' ';
      cout << v[__ii][__jj];
    }
    cout << '\n';
  }
}
template <typename T> void sc(T &x) { cin >> x; }
template <typename Head, typename... Tail> void sc(Head &head, Tail &...tail) {
  cin >> head;
  sc(tail...);
}
template <typename T> void pr(const T &x) { cout << x << '\n'; }
template <typename Head, typename... Tail>
void pr(const Head &head, const Tail &...tail) {
  cout << head << ' ';
  pr(tail...);
}
template <typename... T> void err(const T &...cod) {
  pr(cod...);
  exit(0);
}

const int N = 1e5 + 10;

int a[N], b[N];

void solve() {
  rd(int, n);
  rep(i, 1, n + 1) cin >> a[i];
  per(i, n, n / 2 + 1) b[i] = a[i];
  per(i, n / 2, 1) {
    int sum = 0;
    for (int j = i + i; j <= n; j += i)
      sum += b[j];
    sum %= 2;
    if (a[i])
      b[i] = (sum ? 0 : 1);
    else
      b[i] = (sum ? 1 : 0);
  }
  vector<int> ans;
  rep(i, 1, n + 1) if (b[i]) ans.pb(i);
  pr(sz(ans));
  prv(ans);
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i >= b; i--)
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define eb emplace_back
#define sz(x) ((int)x.size())
#define rd(type, ...)                                                          \
  type __VA_ARGS__;                                                            \
  sc(__VA_ARGS__)
#define rdv(type, v, n)                                                        \
  vector<type> v(n);                                                           \
  rep(__ii, 0, n) cin >> v[__ii]
#define rdm(type, v, h, w)                                                     \
  vector<vector<type>>(h, vector<type>(w)) v;                                  \
  rep(__ii, 0, h) rep(__jj, 0, w) cin >> v[__ii][__jj]
typedef long long ll;
void solve();
signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed;
  cout.precision(12);
  solve();
  return 0;
}
template <typename T> void prv(vector<T> v) {
  rep(__ii, 0, sz(v)) {
    if (__ii)
      cout << ' ';
    cout << v[__ii];
  }
  cout << '\n';
}
template <typename T> void prm(vector<vector<T>> v) {
  rep(__ii, 0, sz(v)) {
    rep(__jj, 0, v[__ii].size()) {
      if (__jj)
        cout << ' ';
      cout << v[__ii][__jj];
    }
    cout << '\n';
  }
}
template <typename T> void sc(T &x) { cin >> x; }
template <typename Head, typename... Tail> void sc(Head &head, Tail &...tail) {
  cin >> head;
  sc(tail...);
}
template <typename T> void pr(const T &x) { cout << x << '\n'; }
template <typename Head, typename... Tail>
void pr(const Head &head, const Tail &...tail) {
  cout << head << ' ';
  pr(tail...);
}
template <typename... T> void err(const T &...cod) {
  pr(cod...);
  exit(0);
}

const int N = 2e5 + 10;

int a[N], b[N];

void solve() {
  rd(int, n);
  rep(i, 1, n + 1) cin >> a[i];
  per(i, n, n / 2 + 1) b[i] = a[i];
  per(i, n / 2, 1) {
    int sum = 0;
    for (int j = i + i; j <= n; j += i)
      sum += b[j];
    sum %= 2;
    if (a[i])
      b[i] = (sum ? 0 : 1);
    else
      b[i] = (sum ? 1 : 0);
  }
  vector<int> ans;
  rep(i, 1, n + 1) if (b[i]) ans.pb(i);
  pr(sz(ans));
  prv(ans);
}