/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 5ms 1.145 MiB
#2 Accepted 9ms 1.324 MiB
#3 Accepted 7ms 1.145 MiB
#4 Accepted 8ms 1.707 MiB
#5 Accepted 9ms 1.645 MiB
#6 Accepted 9ms 1.574 MiB
#7 Accepted 14ms 2.898 MiB
#8 Accepted 10ms 1.578 MiB
#9 Accepted 8ms 1.582 MiB
#10 Accepted 11ms 1.699 MiB
#11 Accepted 8ms 1.32 MiB
#12 Accepted 8ms 1.402 MiB
#13 Accepted 8ms 1.645 MiB
#14 Accepted 7ms 1.332 MiB
#15 Accepted 16ms 1.781 MiB
#16 Accepted 15ms 3.145 MiB
#17 Accepted 82ms 1.688 MiB
#18 Accepted 20ms 1.223 MiB
#19 Accepted 25ms 1.418 MiB
#20 Accepted 24ms 1.395 MiB
#21 Accepted 24ms 1.426 MiB
#22 Accepted 24ms 1.465 MiB
#23 Accepted 31ms 1.395 MiB
#24 Accepted 38ms 1.426 MiB
#25 Accepted 26ms 3.004 MiB
#26 Accepted 30ms 1.43 MiB
#27 Accepted 55ms 1.438 MiB
#28 Accepted 28ms 1.445 MiB
#29 Accepted 74ms 1.402 MiB
#30 Accepted 55ms 1.863 MiB
#31 Accepted 43ms 2.895 MiB
#32 Accepted 22ms 1.383 MiB
#33 Accepted 13ms 1.582 MiB
#34 Accepted 11ms 1.254 MiB

Code

#include<bits/stdc++.h>
using namespace std;
const long long M=3e5+10,MOD=1000000000;
typedef long long ll;
int arr[M];
int limit=100000;
int fre[M];
int c[2];
int n;
vector<int>finddivisor(int x){
    vector<int>d;
    for(int i=1;i*i<=x;i++){
        if(x%i==0){
           d.push_back(i);
           if(x/i==i)continue;
           d.push_back(x/i);
        }
    }
    return d;
}
int solve(vector<int>a,int target){
    int mx=0;
    for(auto i:a){
        int cnt=0;
        vector<int>f(limit+1,0);
        for(int j=i;j<=limit;j+=i){
            f[j]=fre[j];
            cnt+=f[j];
        }
        if(cnt<target)continue;
        if(cnt==n){
            mx=max(mx,i+c[0]);
            continue;
        }
        int num=0;
        for(int j=1;j<=limit;j++){
            if(fre[j]!=f[j]){
                num=j;
                break;
            }
        }
        vector<int>d=finddivisor(num);
        int rem=n-target;
        for(auto j:d){
            int even_pos=0;
            int common=0;
            for(int div=j;div<=limit;div+=j){
                  if(f[div]){
                    common+=f[div];
                  }
                  else if(fre[div])even_pos+=fre[div];
            }
            int odd_pos=cnt-common;
            if(odd_pos+even_pos+common!=n)continue;
            if(odd_pos+common>=target && even_pos+common>=rem){
                mx=max(mx,i+j);
            }
        }

    }
    return mx;
}
vector<int>finddiv(int x){
    vector<int>d;
    for(int i=1;i*i<=x;i++){
        if(x%i==0){
           d.push_back(i);
           if(x/i==i)continue;
           d.push_back(x/i);
        }
    }
    return d;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t=1;
    cin>>t;
    while(t--){
        cin>>n;
        for(int i=1;i<=n;i++)cin>>arr[i-1],fre[arr[i-1]]++;
        sort(arr,arr+n);
        vector<int>a=finddiv(arr[0]);
        c[0]=c[1]=1;
        for(int i=1;i<=limit;i++){
            int odd=(n+1)/2;
            int even=n/2;
            int cnt=0;
            for(int j=i;j<=limit;j+=i){
                cnt+=fre[j];
            }
            if(cnt>=odd)c[1]=i;
            if(cnt>=even)c[0]=i;
        }
          
        cout<<max(solve(a,n/2),solve(a,(n+1)/2))<<"\n";
        for(int i=1;i<=limit;i++)fre[i]=0;

     


    }




    
   
   return 0;
 
}

Information

Submit By
Type
Submission
Problem
P1077 Even Odd GCD (Hard Version)
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-10 11:21:49
Judged At
2024-08-10 11:21:49
Judged By
Score
100
Total Time
82ms
Peak Memory
3.145 MiB