310507: CF1844B. Permutations & Primes

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

B. Permutations & Primestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

You are given a positive integer $n$.

In this problem, the $\operatorname{MEX}$ of a collection of integers $c_1,c_2,\dots,c_k$ is defined as the smallest positive integer $x$ which does not occur in the collection $c$.

The primality of an array $a_1,\dots,a_n$ is defined as the number of pairs $(l,r)$ such that $1 \le l \le r \le n$ and $\operatorname{MEX}(a_l,\dots,a_r)$ is a prime number.

Find any permutation of $1,2,\dots,n$ with the maximum possible primality among all permutations of $1,2,\dots,n$.

Note:

  • A prime number is a number greater than or equal to $2$ that is not divisible by any positive integer except $1$ and itself. For example, $2,5,13$ are prime numbers, but $1$ and $6$ are not prime numbers.
  • A permutation of $1,2,\dots,n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The only line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).

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

Output

For each test case, output $n$ integers: a permutation of $1,2,\dots,n$ that achieves the maximum possible primality.

If there are multiple solutions, print any of them.

ExampleInput
3
2
1
5
Output
2 1
1
5 2 1 4 3
Note

In the first test case, there are $3$ pairs $(l,r)$ with $1 \le l \le r \le 2$, out of which $2$ have a prime $\operatorname{MEX}(a_l,\dots,a_r)$:

  • $(l,r) = (1,1)$: $\operatorname{MEX}(2) = 1$, which is not prime.
  • $(l,r) = (1,2)$: $\operatorname{MEX}(2,1) = 3$, which is prime.
  • $(l,r) = (2,2)$: $\operatorname{MEX}(1) = 2$, which is prime.
Therefore, the primality is $2$.

In the second test case, $\operatorname{MEX}(1) = 2$ is prime, so the primality is $1$.

In the third test case, the maximum possible primality is $8$.

Input

题意翻译

你需要构造一个长度为 $n$ 的序列,使其满足以下条件: - 包含 $1\sim n$ 的所有数 - 最大化满足如下条件的 $(l,r)$ 的对数:$1\leq l\leq r\leq n$ 且 $\operatorname{MEX}(a_l,a_{l+1}\dots a_{r-1},a_r)$ 为质数 构造任意一种即可。 共 $T$ 组数据。 by @[Larryyu](https://www.luogu.com.cn/user/475329)

Output

题目大意:
给定一个正整数n。定义一个数列的MEX为最小的正整数x,使得x不在数列中。定义一个数组的质数性为满足以下条件的(l,r)对的数量:1≤l≤r≤n,且MEX(ala,…,ar)是一个质数。求1,2,…,n的任意排列,使得其质数性在所有排列中最大。

输入输出数据格式:
输入:
第一行包含一个整数t(1≤t≤10^4),表示测试用例的数量。接下来t行,每行包含一个整数n(1≤n≤2×10^5),表示数组的大小。所有测试用例的n之和不超过2×10^5。

输出:
对于每个测试用例,输出n个整数:一个使得质数性最大的1,2,…,n的排列。如果有多个解,输出其中任意一个。

样例输入:
3
2
1
5

样例输出:
2 1
1
5 2 1 4 3题目大意: 给定一个正整数n。定义一个数列的MEX为最小的正整数x,使得x不在数列中。定义一个数组的质数性为满足以下条件的(l,r)对的数量:1≤l≤r≤n,且MEX(ala,…,ar)是一个质数。求1,2,…,n的任意排列,使得其质数性在所有排列中最大。 输入输出数据格式: 输入: 第一行包含一个整数t(1≤t≤10^4),表示测试用例的数量。接下来t行,每行包含一个整数n(1≤n≤2×10^5),表示数组的大小。所有测试用例的n之和不超过2×10^5。 输出: 对于每个测试用例,输出n个整数:一个使得质数性最大的1,2,…,n的排列。如果有多个解,输出其中任意一个。 样例输入: 3 2 1 5 样例输出: 2 1 1 5 2 1 4 3

加入题单

上一题 下一题 算法标签: