Dang_Quan_10_Tin

SPLIT

Aug 4th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.78 KB | None | 0 0
  1. #define NguyenDangQuan the_author
  2. #define my_heart love_you_to_the_moon_and_back
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6. using ll = long long;
  7. using ld = long double;
  8. using ull = unsigned long long;
  9.  
  10. bool typetest;
  11. inline void fastIOfileinput(){
  12.     ios::sync_with_stdio(0);
  13.     cin.tie(0);
  14.     cout.tie(0);
  15.     typetest = 1;
  16. }
  17.  
  18. const int N = 1e6 + 1;
  19. vector<int> ad[N], have[N], adj[N];
  20. int n, a[N];
  21. bool ok[N];
  22. /// Ethoras
  23. void Prepare(){
  24.     vector<bool> ck(N);
  25.     for(int i = 2; i < N; ++i)
  26.         if(!ck[i]){
  27.             for(int j = i; j < N; j += i)
  28.                 ck[j] = 1,
  29.                 ad[j].push_back(i);
  30.         }
  31. }
  32.  
  33. void Read(){
  34.     for(int i = 1; i < N; ++i)
  35.         have[i].clear();
  36.     cin >> n;
  37.     for(int i = 1; i <= n; ++i){
  38.         cin >> a[i];
  39.         ok[i] = 0;
  40.         adj[i].clear();
  41.                 /// Duyệt các ước nguyên tố của a[i]
  42.         for(auto j : ad[a[i]]){
  43.                         ///nếu tồn tại ít nhất 1 đỉnh thì ta nối nó vào
  44.             if(have[j].size())
  45.                 adj[i].push_back(have[j][0]),
  46.                 adj[have[j][0]].push_back(i);
  47.                         /// thêm đỉnh này vào thành phần liên thông
  48.             have[j].push_back(i);
  49.         }
  50.     }
  51. }
  52.  
  53. const ll mod = 1e9 + 7;
  54.  
  55. void dfs(int v){
  56.     ok[v] = 1;
  57.     for(auto i : adj[v])
  58.         if(!ok[i])
  59.             dfs(i);
  60. }
  61.  
  62. ll Pow(ll a, ll b){
  63.     if(b == 0)
  64.         return 1;
  65.     ll t = Pow(a, b / 2);
  66.     if(b & 1)
  67.         return (t * t) % mod * a % mod;
  68.     return t * t % mod;
  69. }
  70.  
  71. void Solve(){
  72.     ll ans = 0;
  73.         /// Đếm liên thông
  74.     for(int i = 1; i <= n; ++i)
  75.         if(!ok[i]){
  76.             ++ans;
  77.             dfs(i);
  78.         }
  79.         ///Đáp án là (2^ans - 2)
  80.     cout << ((Pow(2, ans) - 2ll) % mod + mod) % mod << "\n";
  81. }
  82.  
  83. main(){
  84.     fastIOfileinput();
  85.     if(typetest){
  86.         int t;
  87.         cin >> t;
  88.         Prepare();
  89.         while(t--){
  90.             Read();
  91.             Solve();
  92.         }
  93.     }
  94.     else{
  95.         Read();
  96.         Solve();
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment