Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define nl "\n"
- #define fi first
- #define se second
- #define pb push_back
- #define ll long long
- #define ull unsigned ll
- #define RV return void
- #define sz(x) int(x.size())
- #define all(v) v.begin(), v.end()
- #define rall(v) v.rbegin(), v.rend()
- #define fixed(n) fixed << setprecision(n)
- #define cin(v) for (auto&i:v) cin >> i;
- #define cout(v) for (auto&i:v) cout << i << " ";
- #define clr(memo, x) memset(memo, x, sizeof memo)
- #define FastCode ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
- void Zainab(){
- // #ifndef ONLINE_JUDGE
- // freopen("hobz.in", "r", stdin);
- // freopen("longpath.out", "w", stdout);
- // #endif
- }
- void myCode( ){
- ll n ;
- cin >>n ;
- vector < ll > v(n) , pref(n+1,1 ) , suff(n+1,1);
- cin(v);
- for(int i=0 ; i < n ; i++){
- pref[i] = v[i];
- if(i) pref[i] = __gcd(pref[i-1] , pref[i]);
- }
- for(int i=n-1 ;i >=0 ; i--){
- suff[i] = v[i];
- if(i != n-1)
- suff[i] = __gcd(suff[i+1] , suff[i]);
- }
- ll ans=1;
- map < ll , ll > saved;
- for(int i =0; i < n ; i++){
- ll tx = suff[i+1];
- if(i)
- tx = pref[i-1];
- ll gc = __gcd( tx , suff[i+1])
- , gc2 = __gcd(v[i] , gc)
- , fin = gc/gc2
- , mx =1;
- if(saved[fin])
- mx = saved[fin];
- else{
- for(ll j =2 ; j*j <= fin ; j+= 1 + (j & 1)){
- if(fin % j == 0){
- mx = max(mx , j);
- while(fin % j ==0)
- fin/=j;
- }
- }
- if( fin > 1)
- mx = max(mx , fin);
- }
- saved[gc/ gc2] = mx;
- ans = max(ans , __gcd(gc , mx*gc2));
- // cout << i << " " << gc << " " << gc2 << " " << mx << nl;
- }
- cout << ans << nl;
- }
- int main(){
- FastCode ;
- int testCase=1;
- cin >> testCase ;
- for(int i=1 ; i<= testCase ; i++)
- myCode();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement