P17768 「LDOI R2」卡牌 / Cards

Description

There are $m$ players participating in a card game. Each player holds one card for each number from $0$ to $n$ ($n \ge m$). In each round of the game, every player plays one card, with the requirement that the sum of all played card numbers in that round must equal $n$. Each card can be used by a player only once. The game ends when no valid card combination can satisfy the round's sum requirement. Determine the maximum number of rounds they can play and provide a feasible playing strategy.

Input Format

**There are multiple test cases for this problem.** The first line contains a positive integer $T$, representing the number of test cases. The next $T$ lines each contain two positive integers $n$ and $m$.

Output Format

For each test case: - The first line contains a positive integer $k$, representing the maximum number of rounds. - The following $k$ lines each contain $m$ numbers, representing the cards played by each player in that round. Any valid solution is acceptable.

Explanation/Hint

### Explanation In the first test case, the output's second through fourth lines show the cards played by the two players across $3$ rounds, where each round's card numbers sum to $2$. It can be proven that the maximum number of possible rounds is indeed $3$. ### Constraints **This problem uses subtask scoring.** **The output may be large, please use fast output methods.** | Subtask | Conditions | Score | |:-:|:-:|:-:| | 1 | $n \le 5$ | $10$ | | 2 | $n \le 10^3$ | $20$ | | 3 | $m$ is even | $10$ | | 4 | $m = 3$ | $20$ | | 5 | None | $40$ | For all test cases, $2 \le n \le 10^5$, $2 \le m \le 10^3$, $m \le n$, $1 \le T \le 20$.