2071B.md (2429B)
1 Source: https://codeforces.com/problemset/problem/2071/B 2 3 B. Perfecto 4 5 time limit per test 6 7 1.5 seconds 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 A permutation $p$ of length $n$$^{\text{∗}}$ is perfect if, for each index $i$ ($1 \le i \le n$), it satisfies the following: 22 23 * The sum of the first $i$ elements $p_1 + p_2 + \ldots + p_i$ is not a perfect square$^{\text{†}}$. 24 25 You would like things to be perfect. Given a positive integer $n$, find a perfect permutation of length $n$, or print $-1$ if none exists. 26 27 $^{\text{∗}}$A permutation of length $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). 28 29 $^{\text{†}}$A perfect square is an integer that is the square of an integer, e.g., $9=3^2$ is a perfect square, but $8$ and $14$ are not. 30 31 Input 32 33 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. 34 35 The first and only line of each test case contains a single integer $n$ ($1 \le n \le 5 \cdot 10^5$). 36 37 It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$. 38 39 Output 40 41 For each test case: 42 43 * If no solution exists, print a single integer $-1$. 44 * Otherwise, print $n$ integers $p_1,p_2,\ldots,p_n$ — the perfect permutation you find. 45 46 If there are multiple solutions, print any of them. 47 48 Example 49 50 Input 51 52 ``` 53 3 54 1 55 4 56 5 57 ``` 58 59 Output 60 61 ``` 62 -1 63 2 4 1 3 64 5 1 4 3 2 65 ``` 66 67 Note 68 69 In the first test case, there is only one permutation with length $n = 1$ that is $p = [1]$, which is not perfect: 70 71 * $p_1 = 1 = x^2$ for $x = 1$. 72 73 In the second test case, one possible perfect permutation with length $n = 4$ is $p = [2, 4, 1, 3]$: 74 75 * $p_1 = 2 \neq x^2$; 76 * $p_1 + p_2 = 2 + 4 = 6 \neq x^2$; 77 * $p_1 + p_2 + p_3 = 2 + 4 + 1 = 7 \neq x^2$; 78 * $p_1 + p_2 + p_3 + p_4 = 2 + 4 + 1 + 3 = 10 \neq x^2$. 79 80 In the third test case, one possible perfect permutation with length $n = 5$ is $p = [5, 1, 4, 3, 2]$: 81 82 * $p_1 = 5 \neq x^2$; 83 * $p_1 + p_2 = 5 + 1 = 6 \neq x^2$; 84 * $p_1 + p_2 + p_3 = 5 + 1 + 4 = 10 \neq x^2$; 85 * $p_1 + p_2 + p_3 + p_4 = 5 + 1 + 4 + 3 = 13 \neq x^2$; 86 * $p_1 + p_2 + p_3 + p_4 + p_5 = 5 + 1 + 4 + 3 + 2 = 15 \neq x^2$.