0w1

CFR 583 C. GCD Table ( Ad hoc, Construction )

Problem - C - Codeforces
First, it is easy to observe that the largest value in the initial input is definitely part of the answer. So we will continually take out the largest element from the set, and remove each pair of gcds created by the value about to be removed and the determined values in the answer.

void solve(){
    int N; cin >> N;
    multiset< int > st;
    for( int i = 0; i < N * N; ++i ){
        int u; cin >> u;
        st.insert( u );
    }
    vi ans;
    while( not st.empty() ){
        int u = *prev( st.end() );
        st.erase( prev( st.end() ) );
        for( int i = 0; i < ans.size(); ++i )
            assert( st.count( __gcd( ans[ i ], u ) ) ),
            st.erase( st.find( __gcd( ans[ i ], u ) ) ),
            st.erase( st.find( __gcd( u, ans[ i ] ) ) );
        ans.push_back( u );
    }
    for( int i = 0; i < ans.size(); ++i )
        cout << ans[ i ] << ( i + 1 == ans.size() ? '\n' : ' ' );
}