#P15747. [JAG 2024 Summer Camp #3] Ball Passing

[JAG 2024 Summer Camp #3] Ball Passing

题目描述

There are MM balls of each color 1,2,,N1, 2, \ldots, N, and NN students are sitting in a circle playing with these balls. The students are numbered from 11 to NN in the order in which they are seated.

Initially, each student has exactly MM balls. Specifically, the color of the jj-th ball that ii-th student has is color ai,ja_{i,j}. They will perform the following operation to achieve a state where each student holds balls of only one color:

Operation: Each of the NN students simultaneously passes one ball they currently possess to their neighbor (the ii-th student passes a ball to the (i+1)(i + 1)-th student, and the NN-th student passes a ball to the 11st student, as the (N+1)(N + 1)-th student is considered to be the 11st student).

Your task is to determine whether, by repeating this operation at most NMNM times, it is possible to achieve the goal. If possible, output a sequence of operations.

输入格式

The input consists of a single test case of the following format.

$$\begin{aligned} & N \ M \\ & a_{1,1} \ a_{1,2} \ \ldots \ a_{1,M} \\ & a_{2,1} \ a_{2,2} \ \ldots \ a_{2,M} \\ & \vdots \\ & a_{N,1} \ a_{N,2} \ \ldots \ a_{N,M} \end{aligned}$$

The first line contains two integers NN and MM, where NN represents the number of students and MM represents the number of balls of each color. Both NN and MM are between 22 and 100100 (for NN) and 22 and 100100 (for MM), inclusive.

Each of the following NN lines consists of MM positive integers ai,1,ai,2,,ai,Ma_{i,1}, a_{i,2}, \ldots, a_{i,M}. The integer ai,ja_{i,j} represents the color of the jj-th ball that the ii-th student initially has. It is guaranteed that 1ai,jN1 \leq a_{i,j} \leq N and that each integer 1,2,,N1, 2, \ldots, N occurs MM times among ai,j (1iN,1jM)a_{i,j} \ (1 \leq i \leq N, 1 \leq j \leq M).

It is also guaranteed that the initial state does not satisfy the goal condition.

输出格式

Print 1-1 if it is impossible to achieve the goal state by at most NMNM operations.

Otherwise, print KK — the number of operations. In the following KK lines, print NN integers ci,1,ci,2,,ci,Nc_{i,1}, c_{i,2}, \ldots, c_{i,N}. Here, ci,jc_{i,j} represents the color of the ball passed from the jj-th student to the (j+1)(j+1)-th student at the ii-th operation.

2 4
1 2 1 2
2 1 2 1
2
1 2
1 2
3 3
1 2 3
2 3 1
3 1 2
3
2 3 1
3 1 2
2 3 1