Hydracody

Untitled

Apr 4th, 2023 (edited)
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.56 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define endl "\n"
  4. #define pb push_back
  5. #define pf push_front
  6. #define pob pop_back
  7. #define pof pop_front
  8. #define mp make_pair
  9. #define fi first
  10. #define se second
  11. #define set_bits __builtin_popcountll
  12. #define precise(ans)  cout<<fixed<<setprecision(15)<<ans
  13. #define fo(i,n) for(ll i=0;i<n;i++)
  14. #define Fo(i,k,n) for(ll i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
  15. #define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
  16. #define sz(x) ((ll)(x).size())
  17. #define all(x) x.begin(), x.end()
  18. #define PI 3.1415926535897932384626
  19. #define MOD 1000000007
  20. #define MOD1 998244353
  21. 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>>;
  22. const double eps = 1e-9; const ll INF = (ll) 1e9; const ll inf64 = 2e18; const ll INF64 = 9e18;
  23. #ifndef ONLINE_JUDGE
  24. #define debug(x) cerr << #x <<" "; _prll(x); cerr << endl;
  25. #else
  26. #define debug(x)
  27. #endif
  28. 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 << "]";}
  29. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  30. ll max_N=3e5+100;
  31. vl primeFactorize2(ll x) {
  32.     ll num = x;
  33.      vl store;
  34.       for (ll i = 2; i <= sqrt(x); i++) {
  35.         if(num%i==0){
  36.           store.pb(i);
  37.          while ((num % i) == 0) {
  38.             num /= i;
  39.         }
  40.         }
  41.   }
  42.   if (num > 1){
  43.     store.pb(num);
  44.   }
  45. return store;
  46. }
  47.  
  48.  
  49.  
  50. void  chal(){
  51.   ll n;
  52.   cin>>n;
  53.   vl aa(n+1);
  54.   Fo(i,1,n+1)cin>>aa[i];
  55.   debug(aa);
  56.   ll s,d;
  57.   cin>>s>>d;
  58.      if (aa[s] == aa[d]) {
  59.         if (s == d) {
  60.             cout << "1\n" << s<< '\n';
  61.         } else if (aa[s] == 1) {
  62.             cout << -1;
  63.         } else {
  64.             cout << "2\n" << s << " " << d;
  65.         }
  66.         return ;
  67.     }
  68.   vvl bb(max_N);
  69.   vector<vl> cc(n+1);
  70.   Fo(i,1,n+1){
  71.     cc[i]=primeFactorize2(aa[i]);
  72.     fo(j,sz(cc[i])){
  73.       bb[cc[i][j]].pb(i);
  74.     }
  75.   }
  76.   vl dis(n+1,INF64);
  77.   vl par(n+1,-1);
  78.   vl vis(n+1,0);
  79.   queue<ll> q;
  80.   q.push(s);
  81.   par[s]=0;
  82.   vis[s]=1;
  83.   dis[s]=0;
  84.   while(!q.empty()){
  85.     auto node=q.front();
  86.     // cout<<aa[node]<<"->";
  87.     q.pop();
  88.     for(auto ch1:cc[node]){
  89.       for(auto ch2:bb[ch1]){
  90.         if(!vis[ch2]){
  91.           // cout<<aa[ch2]<<" ";
  92.           dis[ch2]=dis[node]+1;
  93.           vis[ch2]=1;
  94.           par[ch2]=node;
  95.           q.push(ch2);
  96.         }
  97.       }
  98.       bb[ch1].clear();
  99.     }
  100.     // cout<<endl;
  101.   }
  102.   if(dis[d]==INF64){
  103.     cout<<-1<<endl;
  104.     return;
  105.   }
  106.   vl ans;
  107.   while(par[d]!=0){
  108.     ans.pb(d);
  109.     d=par[d];
  110.   }
  111.   ans.pb(s);
  112.   debug(ans);
  113.   reverse(all(ans));
  114.   cout<<ans.size()<<endl;
  115.   fo(i,sz(ans)){
  116.     cout<<ans[i]<<" ";
  117.   }
  118.   cout<<endl;
  119.   // cout<<dis[d]<<endl;
  120.  
  121.  
  122.  
  123.  
  124.  
  125.  
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  134. int32_t main() {
  135.   ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  136. #ifndef ONLINE_JUDGE
  137.   freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); freopen("Error.txt", "w", stderr);
  138. #endif
  139.   ll  t; t = 1;
  140.   // cin >> t;
  141.   for (ll i = 1; i <= t; i++) {
  142.     chal();
  143.   } return 0;
  144. }
Advertisement
Add Comment
Please, Sign In to add comment