/ SeriousOJ /

Record Detail

Time Exceeded


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 320.0 KiB
#2 Time Exceeded ≥5095ms ≥536.0 KiB
#3 Time Exceeded ≥5097ms ≥532.0 KiB

Code

#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

int gcd(int a, int b) {
    return std::gcd(a, b);
}

int calcGCD(const std::vector<int>& arr) {
    return std::accumulate(arr.begin(), arr.end(), 0, gcd);
}

std::vector<int> solve(int n, const std::vector<int>& a) {
    std::vector<int> res(n);
    
    for (int k = 1; k <= n; ++k) {
        int maxGCD = 0;
        
        std::vector<bool> mask(n, false);
        std::fill(mask.begin(), mask.begin() + k, true);
        
        do {
            std::vector<int> sub;
            for (int i = 0; i < n; ++i) {
                if (mask[i]) {
                    sub.push_back(a[i]);
                }
            }
            maxGCD = std::max(maxGCD, calcGCD(sub));
        } while (std::prev_permutation(mask.begin(), mask.end()));
        
        res[k - 1] = maxGCD;
    }
    
    return res;
}

int main() {
    int n;
    std::cin >> n;
    std::vector<int> a(n);
    for (int i = 0; i < n; ++i) {
        std::cin >> a[i];
    }
    
    std::vector<int> res = solve(n, a);
    
    for (int i = 0; i < n; ++i) {
        std::cout << res[i] << " ";
    }
    std::cout << std::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 15:26:55
Judged At
2025-01-02 15:26:55
Judged By
Score
0
Total Time
≥5097ms
Peak Memory
≥536.0 KiB