/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 324.0 KiB
#2 Time Exceeded ≥2087ms ≥532.0 KiB
#3 Accepted 1381ms 484.0 KiB
#4 Time Exceeded ≥2095ms ≥324.0 KiB

Code

#include<bits/stdc++.h>
using namespace std;
#define ll long long int 
#define nl "\n"
#define all(x) (x).begin(), (x).end()
#define Yes cout<<"Yes"<<"\n";
#define No  cout<<"No"<<"\n"; 
int gcdArray(const vector<int>& arr) {
    return accumulate(all(arr), arr[0], [](int a, int b) { return __gcd(a, b); });
}
int maxGCDSum(vector<int>& A) {
    int n = A.size();
    vector<int> indices(n);
    iota(all(indices), 0);
    int maxSum = 0;
    do {
        vector<int> oddIndexed, evenIndexed;
        for (int i = 0; i < n;i++) {
            if (i % 2 == 0) {
                evenIndexed.push_back(A[indices[i]]);
            } else {
                oddIndexed.push_back(A[indices[i]]);
            }
        }
        int oddGCD = gcdArray(oddIndexed);
        int evenGCD = gcdArray(evenIndexed);
        maxSum = max(maxSum, oddGCD + evenGCD);
    } while (next_permutation(all(indices)));
    return maxSum;
}
int main() {
    int T;
    cin >> T;
    while (T--) {
        int N;
        cin >> N;
        vector<int> A(N);
        for (int i = 0; i < N; i++) {
            cin >> A[i];
        }
        cout << maxGCDSum(A) <<nl;
    }
    
    return 0;
}

Information

Submit By
Type
Submission
Problem
P1077 Even Odd GCD (Hard Version)
Contest
Bangladesh 2.0
Language
C++20 (G++ 13.2.0)
Submit At
2024-08-16 16:22:36
Judged At
2024-11-11 03:14:58
Judged By
Score
4
Total Time
≥2095ms
Peak Memory
≥532.0 KiB