Title Link: http://codeforces.com/problemset/problem/582/A
The main idea of the topic
Given the disorder of GCD table of a sequence,
The definition of GCDtable is very simple,
It is required to restore the original sequence according to the table sequence
Topic analysis
We can find that the largest number in a table must be an order
A number in the column, so that the status can be reduced according to the current situation!!
If we have known the first k numbers, then for the current table sequence,
The maximum number must be one of the remaining sequences, but it needs to be eliminated
The effect of this number and previous numbers on the current sequence,
So I simulated this process and thought of using a vector to store the selected numbers,
Then use map to count
#include<bits/stdc++.h> using namespace std; #define debug puts("YES"); #define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++) #define ll long long #define lrt int l,int r,int rt #define lson l,mid,rt<<1 #define rson mid+1,r,rt<<1|1 #define root l,r,rt #define mst(a,b) memset((a),(b),sizeof(a)) #define pii pair<int,int> #define fi first #define se second #define mk(x,y) make_pair(x,y) const int mod=1e9+7; const int maxn=5e2+10; const int ub=1e6; ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;} int gcd(int x,int y){ if(y==0) return x; return gcd(y,x%y); } /* Main idea: Given the disorder of GCD table of a sequence, GCDtable The definition is very simple, It is required to restore the original sequence according to the table sequence Topic analysis: We can find that the largest number in a table must be an order A number in the column, so that the status can be reduced according to the current situation!! If we have known the first k numbers, then for the current table sequence, The maximum number must be one of the remaining sequences, but it needs to be eliminated The effect of this number and previous numbers on the current sequence, So I simulated this process and thought of using a vector to store the selected numbers, Then use map to count */ int n,x[maxn*maxn]; vector<int> ans; map<int,int> mp; map<int,int>::iterator it; int main(){ cin>>n; rep(i,0,n*n){ cin>>x[i]; mp[x[i]]++; } sort(x,x+n*n); for(int i=n*n-1;i>=0;i--){ if(mp[x[i]]==0) continue; mp[x[i]]--; for(int j=0;j<ans.size();j++){ int tmp=gcd(ans[j],x[i]); mp[tmp]-=2; } ans.push_back(x[i]); } for(int i=0;i<ans.size();i++) cout<<ans[i]<<" "; return 0; }