#P15747. [JAG 2024 Summer Camp #3] Ball Passing
[JAG 2024 Summer Camp #3] Ball Passing
题目描述
There are balls of each color , and students are sitting in a circle playing with these balls. The students are numbered from to in the order in which they are seated.
Initially, each student has exactly balls. Specifically, the color of the -th ball that -th student has is color . They will perform the following operation to achieve a state where each student holds balls of only one color:
Operation: Each of the students simultaneously passes one ball they currently possess to their neighbor (the -th student passes a ball to the -th student, and the -th student passes a ball to the st student, as the -th student is considered to be the st student).
Your task is to determine whether, by repeating this operation at most 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 and , where represents the number of students and represents the number of balls of each color. Both and are between and (for ) and and (for ), inclusive.
Each of the following lines consists of positive integers . The integer represents the color of the -th ball that the -th student initially has. It is guaranteed that and that each integer occurs times among .
It is also guaranteed that the initial state does not satisfy the goal condition.
输出格式
Print if it is impossible to achieve the goal state by at most operations.
Otherwise, print — the number of operations. In the following lines, print integers . Here, represents the color of the ball passed from the -th student to the -th student at the -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