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 a of length m, call an index i (1≤i≤m−2) bad if ai, ai+1, and ai+2 are all pairwise coprime. More formally, i is a bad index if and only if gcd(ai,ai+1)=gcd(ai,ai+2)=gcd(ai+1,ai+2)=1∗. Furthermore, call a good if it has at most 6 bad indices.
You are given an integer n. Construct a good permutation† p of length n. It can be shown that such a permutation always exists.
Note that you do not have to minimize the number of bad indices.
∗gcd(x,y) denotes the greatest common divisor of x and y
† A permutation of length n is an array that contains every integer from 1 to n exactly once, in any order.
我一向钟爱“魔法”这个词。它总能给人带来快乐,让人脸上绽放笑容。
——阿尼丝菲娅·温·帕莱提亚
阿尼丝与她的新助手尤菲正着手改进女巫的扫帚!魔法学要求极高的精确性与细致程度——为了使扫帚能够飞行,其构造必须具有足够少的缺陷。
对于任意长度为 m 的数组 a,称下标 i(其中 1≤i≤m−2)是坏的,当且仅当 ai、ai+1 与 ai+2 两两互质。更准确地说,i 是坏下标当且仅当
\gcd(a_i, a_{i+1}) = \gcd(a_i, a_{i+2}) = \gcd(a_{i+1}, a_{i+2}) = 1\,^{\text{∗}}。进一步地,称数组 a 是好的,若其至多含有 6 个坏下标。
现给定一个整数 n。请构造一个长度为 n 的好的排列† p。可以证明,这样的排列总是存在的。
注意:你无需最小化坏下标的数量。
∗ gcd(x,y) 表示 x 与 y 的最大公约数
† 长度为 n 的排列是指一个包含从 1 到 n 的每个整数恰好一次的数组(顺序任意)。
输入格式
The first line contains a single integer t (1≤t≤104) — the number of test cases.
The only line of each test case contains a single integer n (3≤n≤2⋅105).
It is guaranteed that the sum of n over all test cases does not exceed 2⋅105.
第一行包含一个整数 t(1≤t≤104)——测试用例的数量。
每个测试用例仅有一行,包含一个整数 n(3≤n≤2⋅105)。
保证所有测试用例的 n 值之和不超过 2⋅105。
输出格式
For each test case, output on a single line n integers p1,p2,…,pn, an example of a good permutation of length n. If there are multiple good permutations, you may output any of them.
对于每个测试用例,在一行中输出 n 个整数 p1,p2,…,pn,即一个长度为 n 的好排列的例子。如果存在多个好排列,你可以输出其中任意一个。
输入输出样例
输入#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=9, we have
i
pi
pi+1
pi+2
gcd(pi,pi+1)
gcd(pi,pi+2)
gcd(pi+1,pi+2)
1
5
4
8
1
1
4
2
4
8
1
4
1
1
3
8
1
9
1
1
1
4
1
9
3
1
1
3
5
9
3
6
3
3
3
6
3
6
2
3
1
2
7
6
2
7
2
1
1
The only bad index is 3. Since 1≤6, p is a good permutation.
当 n=9 时,我们有
i
pi
pi+1
pi+2
gcd(pi,pi+1)
gcd(pi,pi+2)
gcd(pi+1,pi+2)
1
5
4
8
1
1
4
2
4
8
1
4
1
1
3
8
1
9
1
1
1
4
1
9
3
1
1
3
5
9
3
6
3
3
3
6
3
6
2
3
1
2
7
6
2
7
2
1
1
唯一的坏索引是 3。由于 1≤6,p 是一个好排列。
输入解题思路,AI测评打分。不知道怎么写?