CF2227C.Snowfall

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Yousef has given you an array aa of nn positive integers.

Let f(a)f(a) denote the number of subarrays∗^{\text{∗}} of aa whose product is divisible by 66.

More formally, for every pair of indices ll and rr such that 1≤l≤r≤n1 \le l \le r \le n, consider the subarray al,al+1,…,ara_l, a_{l+1}, \dots, a_r. This subarray is counted if the product of its elements is divisible by 66.

For example, if a=[1,6,2]a = [1, 6, 2], then the subarrays whose products are divisible by 66 are [6][6], [1,6][1, 6], [6,2][6, 2], and [1,6,2][1, 6, 2], so f(a)=4f(a) = 4.

Your task is to reorder the elements of the array aa so that f(a)f(a) is minimized. If there are multiple ways to do this, you may output any of them.

∗^{\text{∗}}An array bb is a subarray of an array aa if bb can be obtained from aa by deleting several (possibly zero or all) elements from the beginning and several (possibly zero or all) elements from the end.

优素福给了你一个包含 nn 个正整数的数组 aa。

令 f(a)f(a) 表示数组 aa 中乘积能被 66 整除的子数组∗^{\text{∗}} 的个数。

更准确地说,对每一对满足 1≤l≤r≤n1 \le l \le r \le n 的下标 ll 和 rr,考虑子数组 al,al+1,…,ara_l, a_{l+1}, \dots, a_r。若该子数组所有元素的乘积能被 66 整除,则将其计入总数。

例如,若 a=[1,6,2]a = [1, 6, 2],则乘积能被 66 整除的子数组为 [6][6]、[1,6][1, 6]、[6,2][6, 2] 和 [1,6,2][1, 6, 2],因此 f(a)=4f(a) = 4。

你的任务是重新排列数组 aa 的元素,使得 f(a)f(a) 尽可能小。如果存在多种最优排列方式,输出任意一种即可。

∗^{\text{∗}} 若数组 bb 可通过从数组 aa 的开头删除若干(可能为零或全部)元素,并从结尾删除若干(可能为零或全部)元素而得到,则称 bb 是 aa 的一个子数组。

输入格式

The first line of the input contains an integer tt (1≤t≤1041 \le t \le 10^4) — the number of test cases.

The first line of each test case contains an integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5) — the size of the array.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (1≤ai≤1091 \le a_i \le 10^9) — the elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052 \cdot 10^5.

输入的第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)—— 表示数组的大小。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 表示数组的元素。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output the array after reordering it in such a way that f(a)f(a) is minimized. If there are multiple answers, you may output any of them.

对于每个测试用例,输出按某种方式重排后的数组,使得 f(a)f(a) 最小。如果存在多个满足条件的答案,你可以输出其中任意一个。

输入输出样例

  • 输入#1

    5
    6
    12 7 9 4 18 5
    4
    3 6 2 8
    7
    1 10 15 20 3 6 9
    5
    11 14 21 2 5
    3
    6 6 6

    输出#1

    12 18 4 7 5 9
    2 8 3 6
    6 10 20 1 15 3 9
    21 5 11 2 14
    6 6 6

说明/提示

In the first test case, an optimal arrangement is a=[12,18,4,7,5,9]a = [12, 18, 4, 7, 5, 9]. The subarrays whose products are divisible by 66 are:

  • [12][12]
  • [18][18]
  • [12,18][12, 18]
  • [18,4][18, 4]
  • [12,18,4][12, 18, 4]
  • [18,4,7][18, 4, 7]
  • [12,18,4,7][12, 18, 4, 7]
  • [18,4,7,5][18, 4, 7, 5]
  • [4,7,5,9][4, 7, 5, 9]
  • [12,18,4,7,5][12, 18, 4, 7, 5]
  • [18,4,7,5,9][18, 4, 7, 5, 9]
  • [12,18,4,7,5,9][12, 18, 4, 7, 5, 9]

Therefore, f(a)=12f(a) = 12. It can be proven that no other arrangement yields a smaller value of f(a)f(a).

在第一个测试用例中,一种最优排列为 a=[12,18,4,7,5,9]a = [12, 18, 4, 7, 5, 9]。其乘积能被 66 整除的子数组有:

  • [12][12]
  • [18][18]
  • [12,18][12, 18]
  • [18,4][18, 4]
  • [12,18,4][12, 18, 4]
  • [18,4,7][18, 4, 7]
  • [12,18,4,7][12, 18, 4, 7]
  • [18,4,7,5][18, 4, 7, 5]
  • [4,7,5,9][4, 7, 5, 9]
  • [12,18,4,7,5][12, 18, 4, 7, 5]
  • [18,4,7,5,9][18, 4, 7, 5, 9]
  • [12,18,4,7,5,9][12, 18, 4, 7, 5, 9]

因此,f(a)=12f(a) = 12。可以证明:不存在其他排列能使 f(a)f(a) 取得更小的值。

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

首页