Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int main() {
- #ifdef DEBUG
- freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- #endif
- ios::sync_with_stdio(false);
- cin.tie(0);
- int n;
- cin >> n;
- vector<vector<int>> facts(n);
- vector<vector<int>> neigh(300000, vector<int>());
- vector<bool> passed_f(300000, 0);
- vector<bool> passed(n, 0);
- vector<int> prev(n, -1);
- int a;
- for (int i = 0; i < n; i++)
- {
- cin >> a;
- for (int f = 2; f * f <= a; f++)
- {
- if (!(a % f))
- facts[i].push_back(f);
- while (!(a % f))
- a /= f;
- }
- if (a != 1)
- facts[i].push_back(a);
- for (auto& f : facts[i])
- neigh[f].push_back(i);
- }
- int s, t;
- cin >> s >> t;
- if (s == t)
- {
- cout << 1 << endl << t << endl;
- return 0;
- }
- s--; t--;
- queue<int> que;
- passed[s] = 1;
- que.push(s);
- while (!que.empty())
- {
- int v = que.front();
- que.pop();
- for (auto& f : facts[v]) if (!passed_f[f])
- {
- passed_f[f] = 1;
- for (auto& u : neigh[f])
- {
- if (!passed[u])
- {
- que.push(u);
- prev[u] = v;
- passed[u] = 1;
- }
- }
- }
- }
- if (prev[t] == -1)
- {
- cout << -1 << endl;
- return 0;
- }
- vector<int> route;
- route.push_back(t);
- while (prev[t] >= 0)
- {
- t = prev[t];
- route.push_back(t);
- }
- reverse(route.begin(), route.end());
- cout << route.size() << endl;
- for (auto& a : route)
- cout << a + 1 << " ";
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement