/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Time Exceeded ≥5069ms ≥540.0 KiB
#3 Time Exceeded ≥5073ms ≥556.0 KiB

Code

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// Function to calculate the GCD of two numbers
int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

// Function to find the maximum GCD for all combinations of size k
int maxGCD(const vector<int>& arr, int k) {
    int n = arr.size();
    int max_gcd = 0;
    vector<bool> combination(n, false);

    // Mark the first k elements for combination
    fill(combination.begin(), combination.begin() + k, true);

    do {
        int current_gcd = 0;
        for (int i = 0; i < n; ++i) {
            if (combination[i]) {
                if (current_gcd == 0) {
                    current_gcd = arr[i];
                } else {
                    current_gcd = gcd(current_gcd, arr[i]);
                }
            }
        }
        max_gcd = max(max_gcd, current_gcd);
    } while (prev_permutation(combination.begin(), combination.end()));

    return max_gcd;
}

int main() {
    int n;
    cin >> n;
    vector<int> arr(n);

    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }

    vector<int> result(n);

    for (int k = 1; k <= n; ++k) {
        result[k - 1] = maxGCD(arr, k);
    }

    for (int i = 0; i < n; ++i) {
        cout << result[i] << " ";
    }
    cout << endl;

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1151 Max gcd group
Contest
Happy New Year 2025
Language
C++17 (G++ 13.2.0)
Submit At
2025-01-02 14:52:39
Judged At
2025-01-02 14:52:39
Judged By
Score
0
Total Time
≥5073ms
Peak Memory
≥556.0 KiB