Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define endl "\n"
- #define pb push_back
- #define pf push_front
- #define pob pop_back
- #define pof pop_front
- #define mp make_pair
- #define fi first
- #define se second
- #define set_bits __builtin_popcountll
- #define precise(ans) cout<<fixed<<setprecision(15)<<ans
- #define fo(i,n) for(ll i=0;i<n;i++)
- #define Fo(i,k,n) for(ll i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
- #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
- #define sz(x) ((ll)(x).size())
- #define all(x) x.begin(), x.end()
- #define PI 3.1415926535897932384626
- #define MOD 1000000007
- #define MOD1 998244353
- typedef long long ll; typedef unsigned long long ull; typedef long double lld; typedef pair<ll, ll> pii; typedef pair<ll, ll> pl; typedef vector<ll> vi; typedef vector<vi> vvi; typedef vector<vvi> vvvi; typedef vector<ll> vl; typedef vector<vl> vvl; typedef vector<pii> vpii; typedef vector<pl> vpl; template <typename T> using prq_mx = priority_queue<T>; template <typename T> using prq_mn = priority_queue<T, vector<T>, greater<T>>;
- const double eps = 1e-9; const ll INF = (ll) 1e9; const ll inf64 = 2e18; const ll INF64 = 9e18;
- #ifndef ONLINE_JUDGE
- #define debug(x) cerr << #x <<" "; _prll(x); cerr << endl;
- #else
- #define debug(x)
- #endif
- void _prll(ll t) {cerr << t;} void _print(ll t) {cerr << t;} void _prll(string t) {cerr << t;} void _prll(char t) {cerr << t;} void _prll(lld t) {cerr << t;} void _prll(double t) {cerr << t;} void _prll(ull t) {cerr << t;} template <class T, class V> void _prll(pair <T, V> p); template <class T> void _prll(vector <T> v); template <class T> void _prll(set <T> v); template <class T, class V> void _prll(map <T, V> v); template <class T> void _prll(multiset <T> v); template <class T, class V> void _prll(pair <T, V> p) {cerr << "{"; _prll(p.fi); cerr << ","; _prll(p.se); cerr << "}";} template <class T> void _prll(vector <T> v) {cerr << "[ "; for (T i : v) {_prll(i); cerr << " ";} cerr << "]";} template <class T> void _prll(set <T> v) {cerr << "[ "; for (T i : v) {_prll(i); cerr << " ";} cerr << "]";} template <class T> void _prll(multiset <T> v) {cerr << "[ "; for (T i : v) {_prll(i); cerr << " ";} cerr << "]";} template <class T, class V> void _prll(map <T, V> v) {cerr << "[ "; for (auto i : v) {_prll(i); cerr << " ";} cerr << "]";}
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- ll max_N=3e5+100;
- vl primeFactorize2(ll x) {
- ll num = x;
- vl store;
- for (ll i = 2; i <= sqrt(x); i++) {
- if(num%i==0){
- store.pb(i);
- while ((num % i) == 0) {
- num /= i;
- }
- }
- }
- if (num > 1){
- store.pb(num);
- }
- return store;
- }
- void chal(){
- ll n;
- cin>>n;
- vl aa(n+1);
- Fo(i,1,n+1)cin>>aa[i];
- debug(aa);
- ll s,d;
- cin>>s>>d;
- if (aa[s] == aa[d]) {
- if (s == d) {
- cout << "1\n" << s<< '\n';
- } else if (aa[s] == 1) {
- cout << -1;
- } else {
- cout << "2\n" << s << " " << d;
- }
- return ;
- }
- vvl bb(max_N);
- vector<vl> cc(n+1);
- Fo(i,1,n+1){
- cc[i]=primeFactorize2(aa[i]);
- fo(j,sz(cc[i])){
- bb[cc[i][j]].pb(i);
- }
- }
- vl dis(n+1,INF64);
- vl par(n+1,-1);
- vl vis(n+1,0);
- queue<ll> q;
- q.push(s);
- par[s]=0;
- vis[s]=1;
- dis[s]=0;
- while(!q.empty()){
- auto node=q.front();
- // cout<<aa[node]<<"->";
- q.pop();
- for(auto ch1:cc[node]){
- for(auto ch2:bb[ch1]){
- if(!vis[ch2]){
- // cout<<aa[ch2]<<" ";
- dis[ch2]=dis[node]+1;
- vis[ch2]=1;
- par[ch2]=node;
- q.push(ch2);
- }
- }
- bb[ch1].clear();
- }
- // cout<<endl;
- }
- if(dis[d]==INF64){
- cout<<-1<<endl;
- return;
- }
- vl ans;
- while(par[d]!=0){
- ans.pb(d);
- d=par[d];
- }
- ans.pb(s);
- debug(ans);
- reverse(all(ans));
- cout<<ans.size()<<endl;
- fo(i,sz(ans)){
- cout<<ans[i]<<" ";
- }
- cout<<endl;
- // cout<<dis[d]<<endl;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- int32_t main() {
- ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count());
- #ifndef ONLINE_JUDGE
- freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("Error.txt", "w", stderr);
- #endif
- ll t; t = 1;
- // cin >> t;
- for (ll i = 1; i <= t; i++) {
- chal();
- } return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment