/ SeriousOJ /

Record Detail

Accepted


  
# Status Time Cost Memory Cost
#1 Accepted 1ms 532.0 KiB
#2 Accepted 1ms 532.0 KiB
#3 Accepted 1ms 324.0 KiB
#4 Accepted 1ms 532.0 KiB
#5 Accepted 1ms 440.0 KiB
#6 Accepted 1ms 552.0 KiB
#7 Accepted 1ms 440.0 KiB
#8 Accepted 2ms 532.0 KiB
#9 Accepted 1ms 440.0 KiB
#10 Accepted 1ms 480.0 KiB
#11 Accepted 1ms 532.0 KiB
#12 Accepted 5ms 352.0 KiB
#13 Accepted 4ms 532.0 KiB

Code

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

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

std::vector<int> solve(int n, const std::vector<int>& a) {
    std::vector<int> res(n);

    std::vector<std::set<int>> dp(n + 1);
    
    dp[0].insert(0); 
    
    for (int i = 0; i < n; ++i) {
        int ai = a[i];
 
        for (int k = n; k >= 1; --k) {
            for (int g : dp[k - 1]) {
                dp[k].insert(gcd(g, ai));
            }
        }
        
        dp[1].insert(ai);  
    }
    
    for (int k = 1; k <= n; ++k) {
        res[k - 1] = *dp[k].rbegin();   
    }
    
    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:28:36
Judged At
2025-01-02 15:28:36
Judged By
Score
100
Total Time
5ms
Peak Memory
552.0 KiB