1844B.md (2377B)
1 Source: https://codeforces.com/problemset/problem/1844/B 2 3 B. Permutations & Primes 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 You are given a positive integer $n$. 22 23 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$. 24 25 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. 26 27 Find any permutation of $1,2,\dots,n$ with the maximum possible primality among all permutations of $1,2,\dots,n$. 28 29 Note: 30 31 * 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. 32 * 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). 33 34 Input 35 36 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. 37 38 The only line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$). 39 40 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 41 42 Output 43 44 For each test case, output $n$ integers: a permutation of $1,2,\dots,n$ that achieves the maximum possible primality. 45 46 If there are multiple solutions, print any of them. 47 48 Example 49 50 Input 51 52 ``` 53 3 54 55 2 56 57 1 58 59 5 60 ``` 61 62 Output 63 64 ``` 65 2 1 66 1 67 5 2 1 4 3 68 ``` 69 70 Note 71 72 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)$: 73 74 * $(l,r) = (1,1)$: $\operatorname{MEX}(2) = 1$, which is not prime. 75 * $(l,r) = (1,2)$: $\operatorname{MEX}(2,1) = 3$, which is prime. 76 * $(l,r) = (2,2)$: $\operatorname{MEX}(1) = 2$, which is prime. 77 78 Therefore, the primality is $2$. 79 80 In the second test case, $\operatorname{MEX}(1) = 2$ is prime, so the primality is $1$. 81 82 In the third test case, the maximum possible primality is $8$.