CF1407B.Big Vova

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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 nn positive integers a1,a2,…,ana_1, a_2, \dots, a_n. Using each of them exactly at once, you're to make such sequence b1,b2,…,bnb_1, b_2, \dots, b_n that sequence c1,c2,…,cnc_1, c_2, \dots, c_n is lexicographically maximal, where ci=GCD(b1,…,bi)c_i=GCD(b_1,\dots,b_i) - the greatest common divisor of the first ii elements of bb.

Alexander is really afraid of the conditions of this simple task, so he asks you to solve it.

A sequence aa is lexicographically smaller than a sequence bb if and only if one of the following holds:

  • aa is a prefix of bb, but a≠ba \ne b;
  • in the first position where aa and bb differ, the sequence aa has a smaller element than the corresponding element in bb.

亚历山大是一位著名的程序员。今天他决定终于出门踢足球,但第一脚就将富商“大沃瓦”(Big Vova)崭新的劳斯莱斯车身上踢出了一个凹痕。弗拉基米尔最近在热门在线电商平台“兹梅伊-戈雷内奇”(Zmey-Gorynych)开设了一家店铺,并向亚历克斯提供了一份工作:如果他能通过解决一道编程题来展示自己的编程能力,便可担任网络安全专家;否则,接下来两年里他将不得不去配送一些可疑的产品。

给你 nn 个正整数 a1,a2,…,ana_1, a_2, \dots, a_n。你需要恰好使用每个数一次,构造一个序列 b1,b2,…,bnb_1, b_2, \dots, b_n,使得序列 c1,c2,…,cnc_1, c_2, \dots, c_n 的字典序最大,其中 ci=GCD(b1,…,bi)c_i = \mathrm{GCD}(b_1,\dots,b_i) 表示 bb 的前 ii 个元素的最大公约数。

亚历山大对这道看似简单的题目的条件感到非常害怕,因此他请你来解决它。

当且仅当满足以下任一条件时,序列 aa 的字典序小于序列 bb:

  • aa 是 bb 的一个真前缀(即 aa 是 bb 的前缀,但 a≠ba \ne b);
  • 在 aa 和 bb 首次出现差异的位置上,aa 中对应位置的元素比 bb 中对应位置的元素更小。

输入格式

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤1031 \le t \le 10^3). Description of the test cases follows.

The first line of each test case contains a single integer nn (1≤n≤1031 \le n \le 10^3) — the length of the sequence aa.

The second line of each test case contains nn integers a1,…,ana_1,\dots,a_n (1≤ai≤1031 \le a_i \le 10^3) — the sequence aa.

It is guaranteed that the sum of nn over all test cases does not exceed 10310^3.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1031 \le t \le 10^3)。随后是测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1031 \le n \le 10^3)——序列 aa 的长度。

每个测试用例的第二行包含 nn 个整数 a1,…,ana_1,\dots,a_n(1≤ai≤1031 \le a_i \le 10^3)——序列 aa。

保证所有测试用例的 nn 之和不超过 10310^3。

输出格式

For each test case output the answer in a single line — the desired sequence bb. If there are multiple answers, print any.

对于每个测试用例,在一行中输出答案——所求的序列 bb。如果存在多个答案,输出任意一个即可。

输入输出样例

  • 输入#1

    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

    输出#1

    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

说明/提示

In the first test case of the example, there are only two possible permutations bb — [2,5][2, 5] and [5,2][5, 2]: for the first one c=[2,1]c=[2, 1], for the second one c=[5,1]c=[5, 1].

In the third test case of the example, number 99 should be the first in bb, and GCD(9,3)=3GCD(9, 3)=3, GCD(9,8)=1GCD(9, 8)=1, so the second number of bb should be 33.

In the seventh test case of the example, first four numbers pairwise have a common divisor (a power of two), but none of them can be the first in the optimal permutation bb.

在示例的第一个测试用例中,仅有两种可能的排列 bb:[2,5][2, 5] 和 [5,2][5, 2];对前者有 c=[2,1]c=[2, 1],对后者有 c=[5,1]c=[5, 1]。

在示例的第三个测试用例中,数字 99 必须位于 bb 的首位,且 GCD(9,3)=3\mathrm{GCD}(9, 3)=3、GCD(9,8)=1\mathrm{GCD}(9, 8)=1,因此 bb 的第二个数应为 33。

在示例的第七个测试用例中,前四个数两两具有公共因子(即某个 2 的幂),但其中任意一个数均不能作为最优排列 bb 的首位。

输入解题思路,AI测评打分。不知道怎么写?

首页