CF2171E.Anisphia Wynn Palettia and Good Permutations

普及+/提高

通过率:0%

时间限制:3.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

I've always loved the word magic. It has a way of making people happy, of putting a smile on their faces.

— Anisphia Wynn Palettia

Anis and her new assistant Euphie are improving the Witch's Broom! Magicology requires great precision and care — in order to fly, the construction of the broom must have sufficiently few imperfections.

For an arbitrary array aa of length mm, call an index ii (1≤i≤m−21\leq i\leq m-2) bad if aia_i, ai+1a_{i+1}, and ai+2a_{i+2} are all pairwise coprime. More formally, ii is a bad index if and only if gcd⁡(ai,ai+1)=gcd⁡(ai,ai+2)=gcd⁡(ai+1,ai+2)=1\gcd(a_i, a_{i+1}) = \gcd(a_i, a_{i+2}) = \gcd(a_{i+1}, a_{i+2}) = 1∗^{\text{∗}}. Furthermore, call aa good if it has at most 66 bad indices.

You are given an integer nn. Construct a good permutation†^{\text{†}} pp of length nn. It can be shown that such a permutation always exists.

Note that you do not have to minimize the number of bad indices.

∗^{\text{∗}}gcd⁡(x,y)\gcd(x, y) denotes the greatest common divisor of xx and yy

†^{\text{†}} A permutation of length nn is an array that contains every integer from 11 to nn exactly once, in any order.

我一向钟爱“魔法”这个词。它总能给人带来快乐,让人脸上绽放笑容。

——阿尼丝菲娅·温·帕莱提亚

阿尼丝与她的新助手尤菲正着手改进女巫的扫帚!魔法学要求极高的精确性与细致程度——为了使扫帚能够飞行,其构造必须具有足够少的缺陷。

对于任意长度为 mm 的数组 aa,称下标 ii(其中 1≤i≤m−21\leq i\leq m-2)是坏的,当且仅当 aia_i、ai+1a_{i+1} 与 ai+2a_{i+2} 两两互质。更准确地说,ii 是坏下标当且仅当

\gcd(a_i, a_{i+1}) = \gcd(a_i, a_{i+2}) = \gcd(a_{i+1}, a_{i+2}) = 1\,^{\text{∗}}。

进一步地,称数组 aa 是好的,若其至多含有 66 个坏下标。

现给定一个整数 nn。请构造一个长度为 nn 的好的排列†^{\text{†}} pp。可以证明,这样的排列总是存在的。

注意:你无需最小化坏下标的数量。

∗^{\text{∗}} gcd⁡(x,y)\gcd(x, y) 表示 xx 与 yy 的最大公约数

†^{\text{†}} 长度为 nn 的排列是指一个包含从 11 到 nn 的每个整数恰好一次的数组(顺序任意)。

输入格式

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

The only line of each test case contains a single integer nn (3≤n≤2⋅1053\leq n \leq 2\cdot 10^5).

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

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

每个测试用例仅有一行,包含一个整数 nn(3≤n≤2⋅1053\leq n \leq 2\cdot 10^5)。

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

输出格式

For each test case, output on a single line nn integers p1,p2,…,pnp_1, p_2, \dots, p_n, an example of a good permutation of length nn. If there are multiple good permutations, you may output any of them.

对于每个测试用例,在一行中输出 nn 个整数 p1,p2,…,pnp_1, p_2, \dots, p_n,即一个长度为 nn 的好排列的例子。如果存在多个好排列,你可以输出其中任意一个。

输入输出样例

  • 输入#1

    4
    3
    6
    8
    9

    输出#1

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

说明/提示

For n=9n=9, we have

ii

pip_i

pi+1p_{i+1}

pi+2p_{i+2}

gcd⁡(pi,pi+1)\gcd(p_i, p_{i+1})

gcd⁡(pi,pi+2)\gcd(p_i, p_{i+2})

gcd⁡(pi+1,pi+2)\gcd(p_{i+1}, p_{i+2})

11

55

44

88

11

11

44

22

44

88

11

44

11

11

33

88

11

99

11

11

11

44

11

99

33

11

11

33

55

99

33

66

33

33

33

66

33

66

22

33

11

22

77

66

22

77

22

11

11

The only bad index is 33. Since 1≤61\leq 6, pp is a good permutation.

当 n=9n=9 时,我们有

ii

pip_i

pi+1p_{i+1}

pi+2p_{i+2}

gcd⁡(pi,pi+1)\gcd(p_i, p_{i+1})

gcd⁡(pi,pi+2)\gcd(p_i, p_{i+2})

gcd⁡(pi+1,pi+2)\gcd(p_{i+1}, p_{i+2})

11

55

44

88

11

11

44

22

44

88

11

44

11

11

33

88

11

99

11

11

11

44

11

99

33

11

11

33

55

99

33

66

33

33

33

66

33

66

22

33

11

22

77

66

22

77

22

11

11

唯一的坏索引是 33。由于 1≤61\leq 6,pp 是一个好排列。

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

首页