orazma

f.cpp

May 20th, 2026
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define pb push_back
  5. #define mp make_pair
  6. #define ll long long
  7.  
  8. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  9.  
  10. using namespace std;
  11. const int INF = 1e9 + 1;
  12. const int MOD = 1e9 + 7;
  13. const int MAXN = 1e6 + 10;
  14. int min_div[MAXN + 10], a[MAXN], pos[MAXN];
  15. map <int, int> cnt;
  16. vector <int> prime_divs[MAXN + 10], primes, g[MAXN];
  17. bool was[MAXN];
  18. int dist[MAXN], pr[MAXN];
  19. void solve(){
  20.  
  21.     int n;
  22.     cin >> n;
  23.     for (int i = 1; i <= n; i++){
  24.         cin >> a[i];
  25.     }
  26.     int m = n;
  27.     for (int x : primes){
  28.         m++;
  29.         a[m] = x;
  30.         pos[x] = m;
  31.     }
  32.     for (int i = 1; i <= n; i++){
  33.         for (int x : prime_divs[a[i]]){
  34.             g[i].pb(pos[x]);
  35.             g[pos[x]].pb(i);
  36.         }
  37.     }
  38.  
  39.     queue <int> q;
  40.     int s, t;
  41.     cin >> s >> t;
  42.     q.push(s);
  43.     was[s] = 1;
  44.     dist[s] = 0;
  45.     pr[s] = -1;
  46.  
  47.     while(!q.empty()){
  48.         int v = q.front();
  49.         q.pop();
  50.  
  51.         for (int to : g[v]){
  52.             if (!was[to]){
  53.                 was[to] = 1;
  54.                 dist[to] = dist[v] + 1;
  55.                 q.push(to);
  56.                 pr[to] = v;
  57.             }
  58.         }
  59.     }
  60.     if (!was[t]){
  61.         cout << -1 << '\n';
  62.     }
  63.     else{
  64.         int x = t;
  65.         vector <int> ans;
  66.         while (x != -1){
  67.             ans.pb(x);
  68.             x = pr[x];
  69.         }
  70.         reverse(ans.begin(), ans.end());
  71.         cout << dist[t] / 2 + 1 << '\n';
  72.         for (int i = 0; i < ans.size(); i++){
  73.             if (i % 2 == 0){
  74.                 cout << ans[i] << ' ';
  75.             }
  76.         }
  77.         cout << '\n';
  78.  
  79.     }
  80.  
  81. }
  82.  
  83. int main() {
  84.     IOS;
  85.     int t = 1;
  86.     //cin >> t;
  87.  
  88.     for (int i = 2; i <= MAXN; i++){
  89.         if (prime_divs[i].size() == 0){
  90.             prime_divs[i].pb(i);
  91.             primes.pb(i);
  92.             for (int j = i + i; j <= MAXN; j += i){
  93.                 prime_divs[j].pb(i);
  94.             }
  95.         }
  96.     }
  97.  
  98.     while(t--){
  99.         solve();
  100.     }
  101.     return 0;
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment