Alexander is a well-known programmer. Today he decided to finally go out and play football, but with the first hit he left a dent on the new Rolls-Royce of the wealthy businessman Big Vova. Vladimir has recently opened a store on the popular online marketplace “Zmey-Gorynych”, and offers Alex a job: if he shows his programming skills by solving a task, he’ll work as a cybersecurity specialist. Otherwise, he’ll be delivering some doubtful products for the next two years.
You’re given n n n positive integers a 1 , a 2 , … , a n a_1,a_2,…,a_n a1,a2,…,an. Using each of them exactly at once, you’re to make such sequence b 1 , b 2 , … , b n b_1,b_2,…,b_n b1,b2,…,bn that sequence c 1 , c 2 , … , c n c_1,c_2,…,c_n c1,c2,…,cn is lexicographically maximal, where c i = G C D ( b 1 , … , b i ) c_i=GCD(b_1,…,b_i) ci=GCD(b1,…,bi) - the greatest common divisor of the first i i i elements of b b b.
Alexander is really afraid of the conditions of this simple task, so he asks you to solve it.
A sequence a is lexicographically smaller than a a a sequence b b b if and only if one of the following holds:
a a a is a prefix of b b b, but a ≠ b a≠b a=b;
in the first position where a a a and b b b differ, the sequence a has a smaller element than the corresponding element in b b b.
Input
Each test contains multiple test cases. The first line contains the number of test cases t ( 1 ≤ t ≤ 1 0 3 ) t (1≤t≤10^3) t(1≤t≤103). Description of the test cases follows.
The first line of each test case contains a a a single integer n ( 1 ≤ n ≤ 1 0 3 ) n (1≤n≤10^3) n(1≤n≤103) — the length of the sequence a a a.
The second line of each test case contains n n n integers a 1 , … , a n ( 1 ≤ a i ≤ 1 0 3 ) a_1,…,a_n (1≤a_i≤10^3) a1,…,an(1≤ai≤103) — the sequence a a a.
It is guaranteed that the sum of n over all test cases does not exceed 1 0 3 10^3 103.
Output
扫描二维码关注公众号,回复: 12168431 查看本文章
For each test case output the answer in a single line — the desired sequence b b b. If there are multiple answers, print any.
Example
input
7
2
2 5
4
1 8 2 3
3
3 8 9
5
64 25 75 100 50
1
42
6
96 128 88 80 52 7
5
2 4 8 16 17
output
5 2
8 2 1 3
9 3 8
100 50 25 75 64
42
128 96 80 88 52 7
17 2 4 8 16
Note
In the first test case of the example, there are only two possible permutations b — [ 2 , 5 ] b — [2,5] b—[2,5] and [ 5 , 2 ] [5,2] [5,2]: for the first one c = [ 2 , 1 ] c=[2,1] c=[2,1], for the second one c = [ 5 , 1 ] c=[5,1] c=[5,1].
In the third test case of the example, number 9 9 9 should be the first in b, and G C D ( 9 , 3 ) = 3 GCD(9,3)=3 GCD(9,3)=3, G C D ( 9 , 8 ) = 1 GCD(9,8)=1 GCD(9,8)=1, so the second number of b should be 3 3 3.
In the seventh test case of the example, first four numbers pairwise have a common divisor ( a a a power of two), but none of them can be the first in the optimal permutation b b b.
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+10;
int a[maxn],n,vis[maxn];
int main() {
int _;
scanf("%d", &_);
while (_--) {
scanf("%d", &n);
int pre = 1;
for (int i = 1; i <= n; i++) {
vis[i] = 0;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
pre = max(pre, a[i]);
}
for (int i = 1; i <= n; i++) {
int mx = 1, p = -1;
for (int j = 1; j <= n; j++) {
if (vis[j] == 0 && __gcd(a[j], pre) >= mx) {
mx = __gcd(a[j], pre);
p = j;
}
}
pre = mx;
printf("%d ", a[p]);
vis[p] = 1;
}
printf("\n");
}
return 0;
}