/ SeriousOJ /

Record Detail

Wrong Answer


  
# Status Time Cost Memory Cost
#1 Accepted 2ms 540.0 KiB
#2 Wrong Answer 2ms 540.0 KiB
#3 Wrong Answer 2ms 540.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;
        
        for (int mask = 0; mask < (1 << n); ++mask) {
            int cnt = __builtin_popcount(mask);
            
            if (cnt == k) {
                std::vector<int> sub;
                for (int i = 0; i < n; ++i) {
                    if (mask & (1 << i)) {
                        sub.push_back(a[i]);
                    }
                }
                maxGCD = std::max(maxGCD, calcGCD(sub));
            }
        }
        
        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:01
Judged At
2025-01-02 15:26:01
Judged By
Score
0
Total Time
2ms
Peak Memory
540.0 KiB