/ SeriousOJ /

Record Detail

Memory Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 368.0 KiB
#2 Accepted 3ms 576.0 KiB
#3 Accepted 1ms 320.0 KiB
#4 Accepted 2ms 536.0 KiB
#5 Accepted 1ms 532.0 KiB
#6 Accepted 2ms 532.0 KiB
#7 Accepted 2ms 536.0 KiB
#8 Accepted 4ms 620.0 KiB
#9 Accepted 2ms 532.0 KiB
#10 Accepted 3ms 624.0 KiB
#11 Accepted 3ms 576.0 KiB
#12 Memory Exceeded ≥2185ms ≥256.016 MiB
#13 Memory Exceeded ≥2198ms ≥256.016 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 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
    }
    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;
        
        // Precompute GCD for all subsets
        // There are 2^N subsets
        int total_masks = 1 << N;
        vector<int> subset_gcd(total_masks, 0);
        
        for(int mask = 1; mask < total_masks; mask++) {
            // Find the least significant set bit
            int lsb = mask & (-mask);
            int pos = log2(lsb);
            
            // Remove the least significant set bit to get the previous subset
            int prev_mask = mask ^ lsb;
            
            if(prev_mask == 0){
                // Only one element in the subset
                subset_gcd[mask] = A[pos];
            }
            else{
                // GCD of the current element and the GCD of the previous subset
                subset_gcd[mask] = gcd(A[pos], subset_gcd[prev_mask]);
                // Early termination if GCD becomes 1
                if(subset_gcd[mask] == 1){
                    // No need to compute further as GCD cannot be lower than 1
                    // But since mask includes more elements, it will still be processed
                }
            }
        }
        
        // Determine the number of elements in Subset 1 (Odd positions)
        int m = (N + 1) / 2;
        int max_sum = 0;
        
        // Iterate through all possible masks with exactly m bits set
        // Utilize bitmask enumeration and the precomputed GCDs
        for(int mask = 0; mask < total_masks; mask++) {
            if(__builtin_popcount(mask) != m) continue;
            
            // GCD for Subset 1 (Odd indices)
            int gcd1 = subset_gcd[mask];
            
            // GCD for Subset 2 (Even indices)
            int complement_mask = ((1 << N) - 1) ^ mask;
            int gcd2;
            if(complement_mask == 0){
                gcd2 = 0; // No elements in Subset 2
            }
            else{
                gcd2 = subset_gcd[complement_mask];
            }
            
            // Calculate the sum
            int current_sum = gcd1 + gcd2;
            
            // Update the maximum sum
            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:56:39
Judged At
2024-11-11 02:42:28
Judged By
Score
31
Total Time
≥2198ms
Peak Memory
≥256.016 MiB