582A - GCD Table - CodeForces Solution


constructive algorithms greedy number theory *1700

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;

int gcd(int a, int b){
    return !b ? a : gcd(b, a%b);
}
int kor(int n){
    int q = sqrt(n);
    return q + ((q+1)*(q+1) == n);
}

int main(){
    int n; cin >> n;
    int n2 = n*n, p = n2;
    int a[n2];
    for(int i=0; i < n2; i++) cin >> a[i];
    sort(a, a+n2);
    vector<int> b;
    map<int, int> m;
    for(int i = n2-1; i >= 0; i--){
        if(i && a[i] == a[i-1]) continue;
        for(int k = p-i; k > m[a[i]]; k--){
            for(int t : b) m[gcd(a[i], t)] += 2;
            b.push_back(a[i]);
        }
        p=i;
    }
    for(int t : b) cout << t << ' ';
    return 0;
}


Comments

Submit
0 Comments
More Questions

1486B - Eastern Exhibition
1363A - Odd Selection
131B - Opposites Attract
490C - Hacking Cypher
158B - Taxi
41C - Email address
1373D - Maximum Sum on Even Positions
1574C - Slay the Dragon
621A - Wet Shark and Odd and Even
1395A - Boboniu Likes to Color Balls
1637C - Andrew and Stones
1334B - Middle Class
260C - Balls and Boxes
1554A - Cherry
11B - Jumping Jack
716A - Crazy Computer
644A - Parliament of Berland
1657C - Bracket Sequence Deletion
1657B - XY Sequence
1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap