#P3599. Koishi's Modular Permutations

    ID: 16850 Type: Default 1000ms 256MiB

Koishi's Modular Permutations

Koishi's Modular Permutations

Koishi has set out from Gensokyo aiming to become a master of mathematics. Flandre, having heard of her skills, challenges her with the following two tasks:

Task1 (Prefix Sum Version)

Determine whether it is possible to construct a permutation p = [p1, p2, …, pn] of the numbers {1, 2, …, n} (where the number n is considered as 0 modulo n) such that the n prefix sums

[ S_k = (p_1 + p_2 + \cdots + p_k)\ (\bmod\ n), \quad 1 \le k \le n ]

are pairwise distinct (i.e. form a permutation of the residues modulo n). It turns out that aside from the trivial case n = 1 the answer exists if and only if n is even.

Task2 (Prefix Product Version)

Determine whether it is possible to construct a permutation p = [p1, p2, …, pn] of {1, 2, …, n} (again, interpret n as 0 modulo n) such that the n prefix products

[ P_k = (p_1 \times p_2 \times \cdots \times p_k)\ (\bmod\ n), \quad 1 \le k \le n ]

are pairwise distinct. In this case it can be shown that aside from n = 1 a valid construction exists if and only if n is a prime number.

You are given several test cases. Each test case provides an integer n and a task indicator t where t = 1 corresponds to Task1 and t = 2 corresponds to Task2. For each test case, if a valid permutation exists output YES on one line and a valid permutation on the next line (the numbers separated by spaces); otherwise output NO.

inputFormat

The input begins with a single integer T (T ≥ 1), denoting the number of test cases. Each of the following T lines contains two integers n and t separated by a space, where n (n ≥ 1) is the length of the permutation and t is the task indicator (1 for Task1, 2 for Task2).

outputFormat

For each test case, output the answer on a separate line. If a valid permutation exists, first output YES and on the next line output the permutation (n space‐separated integers). Otherwise, output NO.

sample

5
2 1
3 1
5 2
4 2
6 1
YES

2 1 NO YES 1 2 4 3 5 NO YES 6 1 4 3 2 5

</p>