CIW '26 P2 - Number Shuffle
View as PDFCanadian Informatics Workshop 2026: Day 1, Problem 2
For her sixteenth birthday, Beryl received a lovely gift from her
grandmother: an grid of numbers. As per family tradition,
she must now perform a sequence of
operations on the grid. Each
operation is one of the following:
Transpose the grid; i.e., swap its rows and columns so that the number in row
, column
ends up in row
, column
. If the grid had
rows and
columns before this operation, it now has
rows and
columns.
Given an integer
that divides
, reshape the grid so that it has
rows and
columns. The numbers in the grid are unchanged, and they stay in the same order when read row by row.
What does the grid look like after performing the sequence of operations?
Input Specification
The first line of input contains two space-separated integers, and
: the number of rows and columns of the grid, respectively.
The next lines each contain
space-separated integers between 1
and
(inclusive); the
line represents the numbers in
row
of the grid.
The next line contains a single integer, .
The final lines each contain either the integer
1, denoting a
transpose operation, or two space-separated integers 2 k, denoting a
reshape operation.
The following table shows how the available 25 marks are distributed:
| Marks Awarded | Bounds on |
Bounds on |
Additional Constraints |
|---|---|---|---|
| 3 marks | None | ||
| 2 marks | No transpose operations | ||
| 2 marks | No reshape operations | ||
| 3 marks | |||
| 3 marks | |||
| 12 marks | None |
Output Specification
Output two space-separated integers and
, the grid dimensions
after performing all
operations. Then output
lines each
containing
space-separated integers; the
line should
contain the numbers in row
of the final grid, in order.
Sample Input
3 4
1 2 3 4
5 6 7 8
9 10 11 12
3
1
2 6
1
Sample Output
2 6
1 9 6 3 11 8
5 2 10 7 4 12
Explanation for Sample Output
After the first operation (a transpose), the grid looks like this:
1 5 9
2 6 10
3 7 11
4 8 12
After the second operation (a reshape with 6 rows), it looks like this:
1 5
9 2
6 10
3 7
11 4
8 12
After the final operation (a transpose), it looks like this:
1 9 6 3 11 8
5 2 10 7 4 12
Comments