Advertisement
Zeinab_Hamdy

Untitled

Apr 4th, 2024
849
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define nl "\n"
  4. #define fi first
  5. #define se second
  6. #define pb push_back
  7. #define ll long long
  8. #define ull unsigned ll
  9. #define RV  return void
  10. #define sz(x) int(x.size())
  11. #define all(v) v.begin(), v.end()
  12. #define rall(v) v.rbegin(), v.rend()
  13. #define fixed(n) fixed << setprecision(n)
  14. #define cin(v) for (auto&i:v) cin >> i;
  15. #define cout(v) for (auto&i:v) cout << i << " ";
  16. #define clr(memo, x) memset(memo, x, sizeof memo)
  17. #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  18.  
  19. void  Zainab(){
  20.             // #ifndef ONLINE_JUDGE
  21.               // freopen("hobz.in", "r", stdin);
  22.               // freopen("longpath.out", "w", stdout);
  23.             // #endif
  24. }
  25.  
  26.  
  27. void myCode( ){
  28.  
  29. ll n ;
  30.     cin >>n ;
  31.     vector < ll > v(n) , pref(n+1,1 ) , suff(n+1,1);
  32.     cin(v);
  33.    
  34.     for(int i=0 ; i < n ; i++){
  35.         pref[i] = v[i];
  36.         if(i) pref[i] = __gcd(pref[i-1] , pref[i]);
  37.     }
  38.    
  39.     for(int i=n-1 ;i >=0 ; i--){
  40.         suff[i] = v[i];
  41.         if(i != n-1)
  42.             suff[i] = __gcd(suff[i+1] , suff[i]);
  43.     }
  44.    
  45.     ll ans=1;
  46.     map < ll , ll > saved;
  47.     for(int i =0; i < n ; i++){
  48.         ll tx = suff[i+1];
  49.         if(i)
  50.             tx = pref[i-1];
  51.        
  52.         ll gc = __gcd( tx , suff[i+1])
  53.         , gc2 = __gcd(v[i] , gc)
  54.         , fin = gc/gc2
  55.         , mx =1;
  56.        
  57.        
  58.         if(saved[fin])
  59.             mx = saved[fin];
  60.        
  61.         else{
  62.             for(ll j =2 ; j*j <= fin ; j+= 1 + (j &  1)){
  63.                 if(fin % j == 0){
  64.                     mx = max(mx , j);
  65.                    
  66.                     while(fin % j ==0)
  67.                         fin/=j;
  68.                 }
  69.             }
  70.                  
  71.             if( fin > 1)
  72.                 mx = max(mx , fin);
  73.         }
  74.        
  75.         saved[gc/ gc2] = mx;
  76.         ans = max(ans , __gcd(gc , mx*gc2));
  77.            
  78.             // cout << i << " " << gc << " " << gc2 << " " << mx << nl;
  79.     }
  80.    
  81.    
  82.  
  83.  
  84.     cout << ans << nl;
  85.  
  86.    
  87. }
  88.  
  89.  
  90. int main(){
  91.                                    FastCode ;
  92.  
  93.     int testCase=1;
  94.           cin >> testCase ;
  95.       for(int i=1 ; i<= testCase ; i++)
  96.         myCode();
  97.  
  98.     return 0;
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement