Advertisement
Ankit_132

C

May 26th, 2024
1,090
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.04 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define int long long
  5.  
  6. signed  main(){
  7.     int t;
  8.     cin>>t;
  9.  
  10.     while(t--){
  11.         int n;
  12.         cin >> n;
  13.         vector<int> a(n);
  14.  
  15.         for(auto &e: a) cin>>e;
  16.  
  17.         int x = 1, f = 0;
  18.         int mx = *max_element(a.begin(),a.end());
  19.  
  20.         for(int i = 0; i < n; i++){
  21.             x = lcm(x,a[i]);
  22.             if(x > mx){
  23.                 cout << n << "\n";
  24.                 f = 1 ;
  25.                 break;
  26.             }
  27.         }
  28.  
  29.         if(f)       continue;
  30.  
  31.         map<int,int> dp, dp2;
  32.         dp[1] = 0;
  33.  
  34.         for(int i = 0; i < n; i++){
  35.             dp2 = dp;
  36.            
  37.             for(auto [x,y] : dp){
  38.                 int val = lcm(x,a[i]);
  39.                 dp2[val] = max(dp2[val], y + 1);
  40.             }
  41.  
  42.             dp = dp2;
  43.         }
  44.  
  45.         int ans = 0;
  46.         set<int> aa(a.begin(),a.end());
  47.  
  48.         for(auto [x,y] : dp){
  49.             if(!aa.count(x))
  50.                 ans = max(ans,y);
  51.         }
  52.  
  53.         cout << ans << "\n";
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement