/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 328.0 KiB
#2 Accepted 7ms 328.0 KiB
#3 Accepted 2ms 540.0 KiB
#4 Accepted 3ms 332.0 KiB
#5 Accepted 2ms 492.0 KiB
#6 Accepted 6ms 796.0 KiB
#7 Accepted 5ms 540.0 KiB
#8 Accepted 11ms 376.0 KiB
#9 Accepted 5ms 560.0 KiB
#10 Accepted 8ms 544.0 KiB
#11 Accepted 7ms 540.0 KiB
#12 Time Exceeded ≥2020ms ≥512.0 KiB
#13 Time Exceeded ≥2064ms ≥540.0 KiB

Code

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

// Function to calculate GCD of a list of numbers
int compute_gcd(const vector<int>& nums) {
    if (nums.empty()) return 0; // Undefined, but handled appropriately
    int result = nums[0];
    for(int i = 1; i < nums.size(); i++) {
        result = gcd(result, nums[i]);
        if(result == 1) break; // Early termination since GCD can't be lower than 1
    }
    return result;
}

int main(){
    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;
        
        // Determine the number of elements in Subset 1 (Odd positions)
        int m = (N + 1) / 2;
        int max_sum = 0;
        
        // Generate all combinations of m elements from N
        // Using bitmask: iterate from 0 to (1<<N)-1 and select those with exactly m bits set
        // Since N <=10, (1<<10)=1024, manageable
        for(int mask = 0; mask < (1<<N); mask++) {
            // Count number of set bits
            if(__builtin_popcount(mask) != m) continue;
            
            vector<int> subset1, subset2;
            for(int i = 0; i < N; i++) {
                if(mask & (1<<i)) subset1.push_back(A[i]);
                else subset2.push_back(A[i]);
            }
            
            // Compute GCDs
            int X = compute_gcd(subset1);
            int Y = compute_gcd(subset2);
            int current_sum = X + Y;
            
            if(current_sum > max_sum) {
                max_sum = current_sum;
            }
        }
        
        cout << max_sum << "\n";
    }
}

Information

Submit By
Type
Submission
Problem
P1077 Even Odd GCD (Hard Version)
Language
C++17 (G++ 13.2.0)
Submit At
2024-10-04 17:55:07
Judged At
2024-11-11 02:42:28
Judged By
Score
31
Total Time
≥2064ms
Peak Memory
≥796.0 KiB