CF BUDDY
← Problems·

1787E · The Harmonization of XOR

2100 · bitmasks, constructive algorithms, greedy

Problem: You are given an array of exactly nn numbers [1,2,3,,n][1,2,3,\ldots,n] along with integers kk and xx.

Partition the array in exactly kk non-empty disjoint subsequences such that the bitwise XOR of all numbers in each subsequence is xx, and each number is in exactly one subsequence. Notice that there are no constraints on the length of each subsequence.

A sequence aa is a subsequence of a sequence bb if aa can be obtained from bb by the deletion of several (possibly, zero or all) elements.

For example, for n=15n = 15, k=6k = 6, x=7x = 7, the following scheme is valid:

  • [6,10,11][6,10,11], 61011=76 \oplus 10 \oplus 11 = 7,
  • [5,12,14][5,12,14], 51214=75 \oplus 12 \oplus 14 = 7,
  • [3,9,13][3,9,13], 3913=73 \oplus 9 \oplus 13 = 7,
  • [1,2,4][1,2,4], 124=71 \oplus 2 \oplus 4 = 7,
  • [8,15][8,15], 815=78 \oplus 15 = 7,
  • [7][7], 7=77 = 7,

The following scheme is invalid, since 88, 1515 do not appear:

  • [6,10,11][6,10,11], 61011=76 \oplus 10 \oplus 11 = 7,
  • [5,12,14][5,12,14], 51214=75 \oplus 12 \oplus 14 = 7,
  • [3,9,13][3,9,13], 3913=73 \oplus 9 \oplus 13 = 7,
  • [1,2,4][1,2,4], 124=71 \oplus 2 \oplus 4 = 7,
  • [7][7], 7=77 = 7.

The following scheme is invalid, since 33 appears twice, and 11, 22 do not appear:

  • [6,10,11][6,10,11], 61011=76 \oplus 10 \oplus 11 = 7,
  • [5,12,14][5,12,14], 51214=75 \oplus 12 \oplus 14 = 7,
  • [3,9,13][3,9,13], 3913=73 \oplus 9 \oplus 13 = 7,
  • [3,4][3,4], 34=73 \oplus 4 = 7,
  • [8,15][8,15], 815=78 \oplus 15 = 7,
  • [7][7], 7=77 = 7.

Input Format: Each test contains multiple test cases. The first line contains an integer tt (1t1041 \le t \le 10^4) — the number of test cases.

The first and the only line of each test case contains three integers nn, kk, xx (1kn21051 \le k \le n \le 2 \cdot 10^5; 1x1091\le x \le 10^9) — the length of the array, the number of subsequences and the required XOR.

It's guaranteed that the sum of nn does not exceed 21052 \cdot 10^5.

Output Format: For each test case, if it is possible to partition the sequence, print "YES" in the first line. In the ii-th of the following kk lines first print the length sis_i of the ii-th subsequence, then print sis_i integers, representing the elements in the ii-th subsequence. If there are multiple answers, print any. Note that you can print a subsequence in any order.

If it is not possible to partition the sequence, print "NO".

Note: In the first test case, we construct the following 66 subsequences:

  • [6,10,11][6,10,11], 61011=76 \oplus 10 \oplus 11 = 7,
  • [5,12,14][5,12,14], 51214=75 \oplus 12 \oplus 14 = 7,
  • [3,9,13][3,9,13], 3913=73 \oplus 9 \oplus 13 = 7,
  • [1,2,4][1,2,4], 124=71 \oplus 2 \oplus 4 = 7,
  • [8,15][8,15], 815=78 \oplus 15 = 7,
  • [7][7], 7=77 = 7.

In the second test case, we construct the following 44 subsequences:

  • [1,4][1,4], 14=51 \oplus 4 = 5,
  • [2,7][2,7], 27=52 \oplus 7 = 5,
  • [3,6][3,6], 36=53 \oplus 6 = 5,
  • [5,8,9,10,11][5,8,9,10,11], 5891011=55 \oplus 8 \oplus 9 \oplus 10 \oplus 11 = 5.

The following solution is considered correct in this test case as well:

  • [1,4][1,4], 14=51 \oplus 4 = 5,
  • [2,7][2,7], 27=52 \oplus 7 = 5,
  • [5][5], 5=55 = 5,
  • [3,6,8,9,10,11][3,6,8,9,10,11], 36891011=53 \oplus 6 \oplus 8 \oplus 9 \oplus 10 \oplus 11 = 5.

Sample Cases

Case 1

Input

7
15 6 7
11 4 5
5 3 2
4 1 4
6 1 7
11 5 5
11 6 5

Output

YES
3 6 10 11
3 5 12 14
3 3 9 13
3 1 2 4
2 8 15
1 7
YES
2 1 4
2 2 7
2 3 6
5 5 8 9 10 11
NO
YES
4 1 2 3 4
YES
6 1 2 3 4 5 6
NO
NO

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback