/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 540.0 KiB
#2 Wrong Answer 1ms 540.0 KiB
#3 Wrong Answer 2ms 536.0 KiB

Code

#include <iostream>
#include <vector>
#include <numeric> // for std::gcd
#include <algorithm> // for std::max

int gcd(int a, int b) {
    while (b) {
        a %= b;
        std::swap(a, b);
    }
    return a;
}

std::vector<int> calculate_max_gcd(const std::vector<int>& arr) {
    int n = arr.size();
    std::vector<int> max_gcds(n);

    max_gcds[0] = *std::max_element(arr.begin(), arr.end()); // For group size 1

    for (int k = 2; k <= n; ++k) {
        int max_gcd = 0;
        for (int i = 0; i < (1 << n); ++i) { // Iterate through all subsets
            if (__builtin_popcount(i) == k) { // Check if the subset has k elements
                std::vector<int> subset;
                for (int j = 0; j < n; ++j) {
                    if ((i >> j) & 1) {
                        subset.push_back(arr[j]);
                    }
                }

                int current_gcd = subset[0];
                for (size_t j = 1; j < subset.size(); ++j) {
                    current_gcd = gcd(current_gcd, subset[j]);
                }
                max_gcd = std::max(max_gcd, current_gcd);
            }
        }
        max_gcds[k - 1] = max_gcd;
    }

    return max_gcds;
}

int main() {
    int n;
    std::cin >> n;

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

    std::vector<int> result = calculate_max_gcd(arr);
    for (int val : result) {
        std::cout << val << " ";
    }
    std::cout << std::endl;

    return 0;
}

Information

Submit By
Type
Submission
Problem
P1151 Max gcd group
Contest
Happy New Year 2025
Language
C++11 (G++ 13.2.0)
Submit At
2025-01-02 15:22:18
Judged At
2025-01-02 15:22:18
Judged By
Score
0
Total Time
2ms
Peak Memory
540.0 KiB