[Solution] Playing Around the Table solution codeforces

Playing Around the Table solution codeforces – There are 𝑛n players, numbered from 11 to 𝑛n sitting around a round table. The (𝑖+1)(i+1)-th player sits to the right of the 𝑖i-th player for 1𝑖<𝑛1≤i<n, and the 11-st player sits to the right of the 𝑛n-th player.

There are 𝑛2n2 cards, each of which has an integer between 11 and 𝑛n written on it. For each integer from 11 to 𝑛n, there are exactly 𝑛n cards having this number.

Initially, all these cards are distributed among all the players, in such a way that each of them has exactly 𝑛n cards. In one operation, each player chooses one of his cards and passes it to the player to his right. All these actions are performed simultaneously.

Player 𝑖i is called solid if all his cards have the integer 𝑖i written on them. Their objective is to reach a configuration in which everyone is solid. Find a way to do it using at most (𝑛2𝑛)(n2−n) operations. You do not need to minimize the number of operations.

Playing Around the Table solution codeforces

The first line contains a single integer 𝑛n (2𝑛1002≤n≤100).

Then 𝑛n lines follow. The 𝑖i-th of them contains 𝑛n integers 𝑐1,𝑐2,,𝑐𝑛c1,c2,…,cn (1𝑐𝑗𝑛1≤cj≤n) — the initial cards of the 𝑖i-th player.

It is guaranteed that for each integer 𝑖i from 11 to 𝑛n, there are exactly 𝑛n cards having the number 𝑖i.

Output

In the first line print an integer 𝑘k (0𝑘(𝑛2𝑛)0≤k≤(n2−n)) — the numbers of operations you want to make.

Then 𝑘k lines should follow. In the 𝑖i-th of them print 𝑛n integers 𝑑1,𝑑2,,𝑑𝑛d1,d2,…,dn (1𝑑𝑗𝑛1≤dj≤n) where 𝑑𝑗dj is the number written on the card which 𝑗j-th player passes to the player to his right in the 𝑖i-th operation.

We can show that an answer always exists under the given constraints. If there are multiple answers, print any.

Examples
input

Copy
2
2 1
1 2

Playing Around the Table solution codeforces

1
2 1 
input

Copy
3
1 1 1
2 2 2
3 3 3
output

Copy
6
1 2 3
3 1 2
2 3 1
1 2 3
3 1 2
2 3 1

Playing Around the Table solution codeforces

In the first test case, if the first player passes a card with number 22 and the second player passes a card with number 11, then the first player has two cards with number 11 and the second player has two cards with number 22. Then, after making this operation, both players are solid.

In the second test case, 00 operations would be enough too. Note that you do not need to minimize the number of operations.

Leave a Comment