CF1858C.Yet Another Permutation Problem

入门

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

Alex got a new game called "GCD permutations" as a birthday present. Each round of this game proceeds as follows:

  • First, Alex chooses a permutation†^{\dagger} a1,a2,…,ana_1, a_2, \ldots, a_n of integers from 11 to nn.
  • Then, for each ii from 11 to nn, an integer di=gcd⁡(ai,a(i mod n)+1)d_i = \gcd(a_i, a_{(i \bmod n) + 1}) is calculated.
  • The score of the round is the number of distinct numbers among d1,d2,…,dnd_1, d_2, \ldots, d_n.

Alex has already played several rounds so he decided to find a permutation a1,a2,…,ana_1, a_2, \ldots, a_n such that its score is as large as possible.

Recall that gcd⁡(x,y)\gcd(x, y) denotes the greatest common divisor (GCD) of numbers xx and yy, and x mod yx \bmod y denotes the remainder of dividing xx by yy.

†^{\dagger}A permutation of length nn is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array), and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

亚历克斯收到了一款名为“最大公约数排列”(GCD permutations)的新游戏作为生日礼物。每轮游戏按如下方式进行:

  • 首先,亚历克斯从 11 到 nn 的整数中选择一个排列†^{\dagger} a1,a2,…,ana_1, a_2, \ldots, a_n;
  • 然后,对每个 ii(从 11 到 nn),计算整数 di=gcd⁡(ai,a(i mod n)+1)d_i = \gcd(a_i, a_{(i \bmod n) + 1});
  • 本轮的得分为 d1,d2,…,dnd_1, d_2, \ldots, d_n 中不同数值的个数。

亚历克斯已玩过若干轮,因此他决定找出一个排列 a1,a2,…,ana_1, a_2, \ldots, a_n,使其得分尽可能大。

请回顾:gcd⁡(x,y)\gcd(x, y) 表示数 xx 与 yy 的最大公约数(GCD),而 x mod yx \bmod y 表示 xx 除以 yy 所得的余数。

†^{\dagger} 长度为 nn 的排列是指由 11 到 nn 的 nn 个互不相同的整数以任意顺序组成的数组。例如,[2,3,1,5,4][2,3,1,5,4] 是一个排列,但 [1,2,2][1,2,2] 不是排列(数字 22 在数组中出现了两次),[1,3,4][1,3,4] 也不是排列(此时 n=3n=3,但数组中出现了 44)。

输入格式

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

Each test case consists of one line containing a single integer nn (2≤n≤1052 \le n \le 10^5).

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

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

每个测试用例由一行组成,该行包含一个整数 nn(2≤n≤1052 \le n \le 10^5)。

保证所有测试用例的 nn 值之和不超过 10510^5。

输出格式

For each test case print nn distinct integers a1,a2,…,ana_{1},a_{2},\ldots,a_{n} (1≤ai≤n1 \le a_i \le n) — the permutation with the largest possible score.

If there are several permutations with the maximum possible score, you can print any one of them.

对于每个测试用例,输出 nn 个互不相同的整数 a1,a2,…,ana_{1},a_{2},\ldots,a_{n}(满足 1≤ai≤n1 \le a_i \le n)—— 即具有最大可能得分的排列。

如果存在多个具有最大可能得分的排列,你可以输出其中任意一个。

输入输出样例

  • 输入#1

    4
    5
    2
    7
    10

    输出#1

    1 2 4 3 5 
    1 2 
    1 2 3 6 4 5 7 
    1 2 3 4 8 5 10 6 9 7

说明/提示

In the first test case, Alex wants to find a permutation of integers from 11 to 55. For the permutation a=[1,2,4,3,5]a=[1,2,4,3,5], the array dd is equal to [1,2,1,1,1][1,2,1,1,1]. It contains 22 distinct integers. It can be shown that there is no permutation of length 55 with a higher score.

In the second test case, Alex wants to find a permutation of integers from 11 to 22. There are only two such permutations: a=[1,2]a=[1,2] and a=[2,1]a=[2,1]. In both cases, the array dd is equal to [1,1][1,1], so both permutations are correct.

In the third test case, Alex wants to find a permutation of integers from 11 to 77. For the permutation a=[1,2,3,6,4,5,7]a=[1,2,3,6,4,5,7], the array dd is equal to [1,1,3,2,1,1,1][1,1,3,2,1,1,1]. It contains 33 distinct integers so its score is equal to 33. It can be shown that there is no permutation of integers from 11 to 77 with a score higher than 33.

在第一个测试用例中,Alex 希望找到一个 11 到 55 的整数排列。对于排列 a=[1,2,4,3,5]a=[1,2,4,3,5],数组 dd 等于 [1,2,1,1,1][1,2,1,1,1]。它包含 22 个不同的整数。可以证明:不存在长度为 55 的排列具有更高的得分。

在第二个测试用例中,Alex 希望找到一个 11 到 22 的整数排列。这样的排列仅有两个:a=[1,2]a=[1,2] 和 a=[2,1]a=[2,1]。在这两种情况下,数组 dd 均等于 [1,1][1,1],因此这两个排列都是正确的。

在第三个测试用例中,Alex 希望找到一个 11 到 77 的整数排列。对于排列 a=[1,2,3,6,4,5,7]a=[1,2,3,6,4,5,7],数组 dd 等于 [1,1,3,2,1,1,1][1,1,3,2,1,1,1]。它包含 33 个不同的整数,因此其得分为 33。可以证明:不存在 11 到 77 的整数排列具有高于 33 的得分。

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

首页