Rayan Programming Contest 2024 - Selection (Codeforces Round 989, Div. 1 + Div. 2)
E
Permutations Harmony
Codeforces
2200
原题
← DF1 →
翻译中…

Rayan wants to present a gift to Reyhaneh to win her heart. However, Reyhaneh is particular and will only accept a k-harmonic set of permutations.

翻译中…

We define a k-harmonic set of permutations as a set of $k$ pairwise distinct permutations $p_1, p_2, \ldots, p_k$ of size $n$ such that for every pair of indices $i$ and $j$ (where $1 \leq i, j \leq n$), the following condition holds:

翻译中…

$$ p_1[i] + p_2[i] + \ldots + p_k[i] = p_1[j] + p_2[j] + \ldots + p_k[j] $$

翻译中…

Your task is to help Rayan by either providing a valid k-harmonic set of permutations for given values of $n$ and $k$ or by determining that such a set does not exist.

翻译中…

We call a sequence of length $n$ a permutation if it contains every integer from $1$ to $n$ exactly once.

翻译中…

Input

翻译中…

The first line contains a single integer $t$ ($1 \leq t \leq 1000$), the number of test cases.

翻译中…

Each test case consists of two integers $n$ and $k$ ($1 \leq n, k \leq 10^5$). The sum of $n \cdot k$ over all test cases does not exceed $5 \cdot 10^5$.

翻译中…

Output

翻译中…

For each test case, if a k-harmonic set of permutations exists, print YES on the first line. Then, print $k$ lines, each containing a distinct permutation of the integers from $1$ to $n$.

翻译中…

If no such set exists, print NO on the first line.

翻译中…

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response).

翻译中…

If multiple answers are possible, you can output any of them.

翻译中…

Note

翻译中…

In example 1, we have $p_1 = [1, 2, 3]$, $p_2 = [2, 3, 1]$, and $p_3 = [3, 1, 2]$. It's easy to see that $p_1[1] + p_2[1] + p_3[1] = p_1[2] + p_2[2] + p_3[2] = p_1[3] + p_2[3] + p_3[3] = 6$.

翻译中…

In example 2, we have $p_1 = [1, 2, 3, 4]$ and $p_2 = [4, 3, 2, 1]$. It's easy to see that $p_1[1] + p_2[1] = p_1[2] + p_2[2] = p_1[3] + p_2[3] = p_1[4] + p_2[4] = 5$.

翻译中…

In example 3, as there are five distinct elements in $p_1$, it's obvious that the answer is "No".

翻译中…

样例

Input #1
4
3 3
4 2
5 1
3 2
Output #1
YES
1 2 3
2 3 1
3 1 2
YES
1 2 3 4
4 3 2 1
NO
YES
1 2 3
3 2 1
InputCopy OutputCopy
题目信息
题号
2034E
时间限制
2 s
内存限制
256 MB
标签
组合数学
构造
贪心
哈希
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。