/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 328.0 KiB
#2 Accepted 3ms 540.0 KiB
#3 Wrong Answer 2ms 540.0 KiB
#4 Accepted 4ms 796.0 KiB
#5 Accepted 3ms 540.0 KiB
#6 Accepted 4ms 812.0 KiB
#7 Accepted 4ms 584.0 KiB
#8 Accepted 3ms 728.0 KiB
#9 Accepted 4ms 748.0 KiB
#10 Accepted 3ms 740.0 KiB
#11 Accepted 4ms 804.0 KiB
#12 Accepted 4ms 820.0 KiB
#13 Accepted 4ms 820.0 KiB
#14 Accepted 2ms 512.0 KiB
#15 Accepted 13ms 820.0 KiB
#16 Accepted 14ms 780.0 KiB
#17 Accepted 89ms 1.43 MiB
#18 Wrong Answer 8ms 1.031 MiB

Code

#include <bits/stdc++.h>
using namespace std;

// Function to compute GCD of a list of numbers
int compute_gcd_list(const vector<int>& nums) {
    if (nums.empty()) return 0; // Undefined, but set to 0
    int result = nums[0];
    for(int i = 1; i < nums.size(); ++i){
        result = gcd(result, nums[i]);
        if(result ==1) break; // Early termination if GCD becomes 1
    }
    return result;
}

void solve(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    int T;
    cin >> T;
    while(T--){
        int N;
        cin >> N;
        vector<int> A(N);
        for(auto &x : A) cin >> x;
        // Calculate m = ceil(N / 2)
        int m = (N +1)/2;
        // Find the maximum element in A
        int max_A = 0;
        for(auto x : A) max_A = max(max_A, x);
        // Build frequency array
        // To handle large max_A efficiently, use vector<int> with size max_A +1
        // Initialize all frequencies to 0
        vector<int> freq(max_A +1, 0);
        for(auto x : A) freq[x]++;
        // Initialize maximum sum
        long long max_sum = 0;
        // Iterate Y from max_A down to1
        for(int Y = max_A; Y >=1; Y--){
            // Count the number of elements divisible by Y
            int count_Y =0;
            for(int k=1; k * Y <=max_A; k++){
                count_Y += freq[k * Y];
            }
            // If at least (N - m) elements are divisible by Y
            if(count_Y >= (N - m)){
                // Assign S2 as the first (N - m) elements divisible by Y
                vector<int> S2;
                vector<int> S1;
                S2.reserve(N - m);
                S1.reserve(m);
                for(auto x : A){
                    if(x % Y ==0 && (int)S2.size() < (N - m)){
                        S2.push_back(x);
                    }
                    else{
                        S1.push_back(x);
                    }
                }
                // Compute GCD of S1
                int X = compute_gcd_list(S1);
                // Compute sum X + Y
                long long current_sum = (long long)X + (long long)Y;
                // Update maximum sum if necessary
                if(current_sum > max_sum){
                    max_sum = current_sum;
                }
            }
        }
        // Output the maximum sum for the current test case
        cout << max_sum << "\n";
    }
}

int main(){
    solve();
}

Information

Submit By
Type
Submission
Problem
P1077 Even Odd GCD (Hard Version)
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-04 20:25:41
Judged At
2024-11-11 02:42:11
Judged By
Score
46
Total Time
89ms
Peak Memory
1.43 MiB