/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Time Exceeded ≥1026ms ≥328.0 KiB
#3 Time Exceeded ≥1037ms ≥540.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
P1076 Even Odd GCD (Easy Version)
Contest
Bangladesh 2.0
Language
C++17 (G++ 13.2.0)
Submit At
2024-08-16 16:17:24
Judged At
2024-11-11 03:15:17
Judged By
Score
1
Total Time
≥1037ms
Peak Memory
≥540.0 KiB