DMOPC '21 Contest 5 P4 - Permutations & Primes

View as PDF

Submit solution


Points: 17 (partial)
Time limit: 2.0s
Memory limit: 256M

Author:
Problem types

Given an integer N, find any permutation p1,p2,,pN of 1,2,,N that satisfies the following, or report that no such permutation exists:

  1. For all 1iN1, |pi+1pi| is prime.
  2. The number of distinct pi+1pi as 1iN1 is the minimum possible among all permutations satisfying the first condition.

Note that there is no absolute value in the second condition.

To ensure the integrity of your solution, there may be up to T test cases.

Constraints

1T,N106

The sum of N over all test cases does not exceed 106.

Subtask 1 [20%]

N is a multiple of 10.

Subtask 2 [40%]

N is even.

Subtask 3 [40%]

No additional constraints.

Input Specification

The first line contains an integer T.

The next T lines each contain a single integer N, describing one test case.

Output Specification

For each test case, if no such permutation exists, output 1 on a line by itself. Otherwise, output N space-separated integers p1,p2,,pN, a permutation that satisfies the conditions.

Sample Input

Copy
2
4
3

Sample Output

Copy
3 1 4 2
-1

Explanation

For the first test case, |13|=2, |41|=3, and |24|=2 are all primes, and there are 2 distinct numbers among 13=2, 41=3, and 24=2.

For the second test case, it is not possible for all adjacent absolute differences to be prime.


Comments

There are no comments at the moment.