233 lines
4.4 KiB
C++

/* Problem URL: https://codeforces.com/problemset/problem/1775/D */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <class T, class comp = less<>>
using ordered_set = tree<T, null_type , comp , rb_tree_tag , tree_order_statistics_node_update>;
#define V vector
#define rmin(a, b) a = min(a, b)
#define rmax(a, b) a = max(a, b)
#define rep(i, lim) for (int i = 0; i < (lim); i++)
#define nrep(i, s, lim) for (int i = s; i < (lim); i++)
#define repv(i, v) for (auto &i : (v))
#define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; }
#define sortv(v) sort(v.begin(), v.end())
#define all(v) (v).begin(), (v).end()
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vvvvi = vector<vvvi>;
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vl>;
using vvvl = vector<vvl>;
using vvvvl = vector<vvvl>;
template<class v>
auto operator<<(ostream &os, const vector<v> &vec)->ostream& {
os << vec[0];
for (size_t i = 1; i < vec.size(); i++) {
os << ' ' << vec[i];
}
os << '\n';
return os;
}
template<class v>
auto operator>>(istream &is, vector<v> &vec)->istream& {
for (auto &i : vec) {
is >> i;
}
return is;
}
template<class v>
auto operator<<(ostream &os, const vector<vector<v>> &vec)->ostream& {
for (auto &i : vec) {
os << i[0];
for (size_t j = 1; j < i.size(); j++) {
os << ' ' << i[j];
}
os << '\n';
}
return os;
}
template<class v>
auto operator>>(istream &is, vector<vector<v>> &vec)->istream& {
for (auto &i : vec) {
for (auto &j : i) {
is >> j;
}
}
return is;
}
const int oo = INT32_MAX >> 1;
const ll OO = INT64_MAX >> 1;
const int MAXN = 3e5 + 1;
vi primes;
int di[MAXN];
void pre()
{
fill(di, di + MAXN, 1);
nrep(i, 2, MAXN) {
if (di[i] != 1) {
continue;
}
for (int j = i; j < MAXN; j += i) {
di[j] = i;
}
}
}
#define TEST 0
void solve()
{
int n;
cin >> n;
vi a(n);
cin >> a;
int s, t;
cin >> s >> t;
s--, t--;
if (s == t) {
cout << "1\n";
cout << s + 1 << '\n';
return;
}
if (a[s] == 1 || a[t] == 1) {
cout << "-1\n";
return;
}
if (a[s] == a[t]) {
cout << "2\n";
cout << s + 1 << ' ' << t + 1 << '\n';
return;
}
V<bool> used(3e5 + 1);
used[a[s]] = true;
used[a[t]] = true;
vvi graph(MAXN + n);
auto getgraph = [&](int i) {
int now = a[i];
while (now > 1) {
int prev = di[now];
while (di[now] == prev) {
now /= di[now];
}
graph[prev].push_back(i + MAXN);
graph[i + MAXN].push_back(prev);
}
};
getgraph(s);
getgraph(t);
rep(i, n) {
// if ((i != s && a[i] == a[s]) || (i != t && a[i] == a[t])) {
// continue;
// }
// pos[a[i]] = i;
if (used[a[i]]) {
continue;
}
used[a[i]] = true;
getgraph(i);
}
// nrep(i, 2, 3e5 + 1) {
// if (pos[i] == -1) {
// continue;
// }
//
// for (int j = i * 2; j <= 3e5; j += i) {
// if (pos[j] == -1) {
// continue;
// }
//
// graph[pos[i]].push_back(pos[j]);
// graph[pos[j]].push_back(pos[i]);
// }
// }
vi dis(MAXN + n, oo);
vi parent(MAXN + n, -1);
queue<int> q;
dis[s + MAXN] = 0;
q.push(s + MAXN);
while (!q.empty()) {
auto i = q.front();
q.pop();
for (auto j : graph[i]) {
if (dis[j] > dis[i] + 1) {
dis[j] = dis[i] + 1;
parent[j] = i;
q.push(j);
}
}
}
if (dis[t + MAXN] == oo) {
cout << "-1\n";
return;
}
vi ans;
int now = t + MAXN;
while (parent[now] != -1) {
if (now >= MAXN) {
ans.push_back(now - MAXN + 1);
}
now = parent[now];
}
ans.push_back(s + 1);
reverse(all(ans));
cout << ans.size() << '\n';
cout << ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
pre();
int t;
(TEST && cin >> t) || (t = 1);
while (t--) {
solve();
}
}