AhmedAshraff

Untitled

Jul 17th, 2024
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.38 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4. typedef long long ll;
  5. #define sz(s) (int)(s).size()
  6. #define all(s) s.begin(),s.end()
  7.  
  8. void Speed() {
  9.     ios_base::sync_with_stdio(false);
  10.     cin.tie(NULL);
  11. }
  12.  
  13. const int N = 1e6 + 5;
  14. int spf[N], need[N], dis[N], vis[N], vid;
  15. void solve() {
  16.     int n; cin >> n;
  17.     vector<vector<array<int, 2>>> a(n);
  18.     map<int, int> mp;
  19.     for(int i = 0; i < n; i++){
  20.         int x; cin >> x;
  21.         while(x > 1){
  22.             int p = spf[x]; x /= p;
  23.             if(a[i].empty() || a[i].back()[0] != p) a[i].push_back({p, 1});
  24.             else a[i].back()[1]++;
  25.         }
  26.         for(auto& [p, f] : a[i]){
  27.             if(mp.count(p)) mp[p] = max(mp[p], f);
  28.             else mp[p] = x;
  29.         }
  30.     }
  31.    
  32.     vector<array<int, 2>> primes;
  33.     for(auto& [p, f] : mp) primes.push_back({p, f});
  34.     vector<int> masks;
  35.     for(auto& v :a){
  36.         int p1 = 0, p2 = 0, cur_mask = 0;
  37.         while(p1 < sz(v)){
  38.             assert(p2 < sz(primes));
  39.             if(v[p1][0] > primes[p2][0]){
  40.                 p2++; continue;
  41.             }
  42.             if(v[p1][1] == primes[p2][1]) cur_mask |= (1<<p2);
  43.             p1++; p2++;
  44.         }
  45.         if(cur_mask > 0) masks.push_back(cur_mask);
  46.     }
  47.     if(masks.empty()) return cout << "1\n", void();
  48.     sort(all(masks));
  49.     vector<vector<int>> bit_masks(sz(primes));
  50.     int last = -1;
  51.     for(int x : masks){
  52.         if(x == last) continue;
  53.         last = x;
  54.         for(int b = 0; b < sz(primes); b++){
  55.             if((x >> b) & 1){
  56.                 bit_masks[b].push_back(x);
  57.             }
  58.         }
  59.     }
  60.  
  61.     queue<int> q;
  62.     vid++; q.push(0);
  63.     dis[0] = 0; vis[0] = vid;
  64.     while(!q.empty()){
  65.         int u = q.front(); q.pop();
  66.         int b = sz(primes) - 1;
  67.         while(b >= 0 && ((u >> b) & 1)) b--;
  68.         if(b == -1) return cout << dis[u] << '\n', void();
  69.         for(int x : bit_masks[b]){
  70.             int y = (u | x);
  71.             if(vis[y] == vid) continue;
  72.             vis[y] = vid; dis[y] = dis[u] + 1;
  73.             q.push(y);
  74.         }
  75.     }
  76.     assert(0);
  77. }
  78.  
  79. int main() {
  80.     for(int i = 2; i < N; i++){
  81.         if(spf[i]) continue;
  82.         for(int j = i; j < N; j += i){
  83.             if(spf[j]) continue;
  84.             spf[j] = i;
  85.         }
  86.     }
  87.     Speed();
  88.     int tc = 1;
  89.     cin >> tc;
  90.     while (tc--) {
  91.         solve();
  92.     }
  93.     return 0;
  94. }
Add Comment
Please, Sign In to add comment