Valera has got a rectangle table consisting of n rows and m columns. Valera numbered the table rows starting from one, from top to bottom and the columns – starting from one, from left to right. We will represent cell that is on the intersection of row x and column y by a pair of integers (x, y).
Valera wants to place exactly k tubes on his rectangle table. A tube is such sequence of table cells (x1, y1), (x2, y2), ..., (xr, yr), that:
r ≥ 2;
for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds;
each table cell, which belongs to the tube, must occur exactly once in the sequence.
Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled:
no pair of tubes has common cells;
each cell of the table belongs to some tube.
Help Valera to arrange k tubes on his rectangle table in a fancy manner.
Input
The first line contains three space-separated integers n, m, k (2 ≤ n, m ≤ 300; 2 ≤ 2k ≤ n·m) — the number of rows, the number of columns and the number of tubes, correspondingly.
Output
Print k lines. In the i-th line print the description of the i-th tube: first print integer ri (the number of tube cells), then print 2ri integers xi1, yi1, xi2, yi2, ..., xiri, yiri (the sequence of table cells).
If there are multiple solutions, you can print any of them. It is guaranteed that at least one solution exists.
Examples
Input
3 3 3
Output
3 1 1 1 2 1 3
3 2 1 2 2 2 3
3 3 1 3 2 3 3
Input
2 3 1
Output
6 1 1 1 2 1 3 2 3 2 2 2 1
Note
Picture for the first sample:
Picture for the second sample:
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We need to construct exactly k tubes (paths covering all cells without overlapping, each tube must be a simple path (non-self-intersecting, no cycles) covering a set of cells, and each tube must have length at least 2 (r ≥ 2). Total cells n*m must be partitioned into k tubes.
Given constraints: n,m >=2, 2 ≤ 2k ≤ nm (so k ≤ floor(nm/2)). So we can always partition the grid into k paths each of length at least 2.
We need to output each path as a sequence of cells (coordinates) along the path. The path must be a simple path: consecutive cells are adjacent (Manhattan distance 1), and each cell appears exactly once in the sequence (so it's a simple path). Tubes cannot share cells.
We can think of covering the grid with k disjoint paths covering all vertices. This is like partitioning the grid graph (a rectangular grid) into k vertex-disjoint paths covering all vertices. Paths may be of varying lengths, each at least 2.
We need to produce any valid solution.
Approach: Since the total number of cells is nm, and each tube uses at least 2 cells, we have k ≤ nm/2, which is given. So we need to partition the grid into k paths.
One simple method: Use a Hamiltonian path covering the entire grid (since grid graph is connected and has a Hamiltonian path for any n,m) and then split that path into k segments, each segment being a path. However, each segment must be a valid tube: it must be a contiguous subsequence of the Hamiltonian path, which forms a path (since subsequence of a path is still a path). But we need each tube to be a sequence of cells such that consecutive cells are adjacent; that holds if we take contiguous subsequences. Also each tube will have at least 2 cells: we can split the Hamiltonian path into k segments each of length at least 2. Since total cells is N = n*m, we need to partition N into k parts each ≥ 2. That's possible if N >= 2k, which holds. So we can generate a Hamiltonian path covering all cells, then cut it into k pieces of appropriate lengths (all >=2). The output will then be k lines: each line gives the segment: length and coordinates.
But careful: The tube must be exactly the path segment; the segment is a path, yes. The whole path is a sequence of cells; each segment is contiguous. That's valid. However, the problem statement says: "each cell of the table belongs to some tube." That's satisfied. "no pair of tubes has common cells." Satisfied because segments are disjoint and cover whole. So that works.
But is there any requirement that the tubes must be exactly k? Yes. So splitting a Hamiltonian path into k segments yields exactly k tubes. Need to ensure each segment length >= 2; we can allocate lengths accordingly. For example, if N = nm, we need to partition N into k integers each at least 2. Since N >= 2k, we can do e.g., first k-1 segments of length 2, and the last segment of length N - 2(k-1). But we must check that N - 2*(k-1) >= 2, which is equivalent to N >= 2k, which holds. But the last segment might be large; that's okay.
But we must also ensure that the Hamiltonian path covers all cells. There are many ways. For a grid, a simple snake-like pattern works: start at (1,1), go right across first row, then down one, go left across second row, etc. That yields a Hamiltonian path.
So the solution: Generate a snake order (alternating direction). Then we have a list of cells in order. Then split into k parts: first k-1 tubes with length 2, and the last with remaining length N - 2(k-1) (which is at least 2). However, we need to be careful: The last tube must have at least 2 cells, yes. But we can also choose any partition as long as each >=2.
But is there any issue: The path must be a sequence of cells where each consecutive are adjacent. In the snake order, consecutive cells are adjacent: if moving right/left across a row, adjacent horizontally; at the turn, moving down one row, that is vertical adjacency. So it's a valid Hamiltonian path.
So algorithm:
Compute N = n * m.
Create array cells = [].
For i from 1 to n:
if i is odd: for j from 1 to m: cells.append((i, j))
else: for j from m down to 1: cells.append((i, j))
Now we have the Hamiltonian path of length N.
Determine lengths: We need k tubes. We want to assign lengths l_i such that sum l_i = N, each l_i >= 2. Simplest: assign l_i = 2 for i=1..k-1, and l_k = N - 2*(k-1). Since N >= 2k, l_k >= 2.
Then iterate over cells, split accordingly, and output each tube.
But we need to verify if there are any constraints about tube length being exactly r? No.
This solves the problem. However, is there any nuance: The problem statement says "tubes are arranged in a fancy manner" with conditions: no common cells; each cell belongs to some tube. Our solution meets that. Also each tube is a simple path, meeting conditions.
Check examples: Sample 1: n=3,m=3,k=3. N=9. Our snake: row1 odd: (1,1),(1,2),(1,3); row2 even: (2,3),(2,2),(2,1); row3 odd: (3,1),(3,2),(3,3). So order: (1,1),(1,2),(1,3),(2,3),(2,2),(2,1),(3,1),(3,2),(3,3). Partition: first two tubes length2: tube1: (1,1),(1,2) – but wait, tube length 2: should be 2 cells? Actually output of sample has each tube length 3 covering each column of a row. Our solution would give three tubes: length2, length2, length5? Because k=3, l1=2,l2=2,l3=9-4=5. But sample output has each tube of length3. That's fine; any solution works. So we would output something different, but still valid. However, the sample output shows each row as a tube; that's also a snake partition if we cut differently. So our approach is valid.
But is there any hidden requirement about the tubes being "connected"? Already they are paths.
Thus solution is straightforward.
But wait: The problem says "Valera wants to place exactly k tubes." The definition of a tube: sequence of cells with r>=2, adjacent consecutive cells, and each cell occurs exactly once in the sequence. That means the tube itself is a simple path. Our segments from the Hamiltonian path are indeed simple paths.
Thus solution is correct.
Edge Cases: n,m >= 2, but can be even/odd. For n=2,m=2,k=2: N=4. Our snake: (1,1),(1,2),(2,2),(2,1). Partition: l1=2, l2=2: tube1: (1,1),(1,2); tube2: (2,2),(2,1). Check: tube2: sequence (2,2),(2,1) is adjacent horizontally? Actually (2,2) to (2,1) are adjacent horizontally; fine. Both tubes length2. Valid.
But check connectivity: tube2 is just two cells adjacent; okay.
What about n=2,m=3,k=1: N=6, k=1, then we have only one tube. Condition 2 <= 2k <= N? Here 2k=2 ≤ 6, ok. But k=1, so we need a single tube covering all cells, which must be a path of length 6. That's exactly a Hamiltonian path. Our snake order would produce (1,1),(1,2),(1,3),(2,3),(2,2),(2,1) which is a path. Output would be 6 and those coordinates. That matches sample 2? Sample 2: n=2,m=3,k=1 output: 6 1 1 1 2 1 3 2 3 2 2 2 1, which is exactly snake order. So our method produces sample output for that case.
So our solution matches the second sample.
Thus we can implement.
But we need to be careful about output format: For each tube, print ri then 2ri integers: coordinates in order.
Implementation steps:
Read n, m, k.
Build list cells.
For i in 1..n:
if i%2==1: for j in 1..m: append (i,j)
else: for j in m..1: append (i,j)
Let N = len(cells)
Compute lengths: remaining = N; need k tubes.
We can assign: first k-1 tubes of length 2, but ensure that after assigning 2*(k-1) we have at least 2 left. Alternatively, we can assign roughly equal lengths? But simpler: assign 2 to each of the first k-1, and rest to last.
But we must ensure that the last length is at least 2. Since N >= 2k, N - 2*(k-1) >= 2. So okay.
We can also handle case k=1 specially: just output the whole list.
Procedure:
idx = 0
For tube i from 1 to k-1:
ri = 2
Print ri, then cells[idx]..cells[idx+ri-1] flattened (x1 y1 ...)
idx += ri
Last tube: ri = N - idx
Print ri and remaining cells.
But note: The problem may have input where k might be 0? No, k at least 2? Actually constraints: 2 ≤ 2k ≤ n·m, so k could be 1? Because 21=2 ≤ nm, so k can be 1. Yes, examples include k=1. So k>=1? Actually lower bound: 2 ≤ 2k means k ≥ 1? 2 ≤ 2k => k ≥ 1. So k can be 1. So we need to handle k=1.
Alternatively, we could distribute lengths more evenly, but not necessary.
One potential issue: When k is large, the first k-1 tubes are of length 2, the last tube length might be huge, but that's acceptable. There is no constraint that tubes must be balanced.
But is there any requirement that each tube must be a simple path that is not necessarily contiguous in the Hamiltonian path? It is contiguous by construction.
Thus solution works.
But we must confirm that the Hamiltonian path we build indeed covers all cells and is a valid path. Yes.
Complexity: O(n*m) time and memory, with n,m ≤ 300 => N ≤ 90000, fine.
Now, we need to provide solution in Python.
We'll write solve() function.
Implementation details:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
n, m, k = map(int, data[:3])
cells = []
for i in range(1, n+1):
if i % 2 == 1:
for j in range(1, m+1):
cells.append((i, j))
else:
for j in range(m, 0, -1):
cells.append((i, j))
N = len(cells)
# split
idx = 0
out_lines = []
# for first k-1 tubes, length 2
for i in range(k-1):
ri = 2
seg = cells[idx:idx+ri]
# flatten
flat = [str(ri)] + [str(coord) for pair in seg for coord in pair] # Actually need to output ri then the pairs
out_lines.append(" ".join(flat))
idx += ri
# last tube
remaining = N - idx
seg = cells[idx:]
flat = [str(remaining)] + [str(coord) for pair in seg for coord in pair]
out_lines.append(" ".join(flat))
sys.stdout.write("\n".join(out_lines))
But careful: When k=1, the loop for i in range(k-1) will be range(0) so skip, then remaining = N, and we output the whole list. That's fine.
Edge: If k-1 > 0, but what if after assigning k-1 tubes of length 2, we have exactly 2 cells left? Then last tube length 2, works. If more, works.
Thus solution done.
But wait: The problem says "r ≥ 2; for any integer i (1 ≤ i ≤ r - 1) the following equation |xi - xi + 1| + |yi - yi + 1| = 1 holds; each table cell, which belongs to the tube, must occur exactly once in the sequence." That's satisfied.
One more nuance: The tube's sequence must be such that consecutive cells are adjacent. In our segment, the adjacency holds because the overall Hamiltonian path ensures adjacency between consecutive cells in the entire sequence, so within a segment, the adjacency between the first and second cell of the segment is obviously the same as in the overall path; similarly for each consecutive pair inside the segment. The only possible issue: If we cut the Hamiltonian path into segments, the adjacency between the last cell of one segment and the first cell of the next segment is not part of any tube, so that's fine. So each segment is a path.
Thus correct.
But is it possible that the Hamiltonian path contains a turn that might cause a segment of length 2 to be not a simple path? It is still a simple path: two cells adjacent, that's a path of length 2. Yes.
Thus the solution is straightforward.
But what about the constraint that tubes must be exactly k? Yes.
Thus final answer.
But the problem statement: "Valera thinks that the tubes are arranged in a fancy manner if the following conditions are fulfilled: no pair of tubes has common cells; each cell of the table belongs to some tube." That's it. So our method satisfies.
I will produce final solution.
However, I want to double-check: The input constraint: "2 ≤ 2k ≤ n·m" ensures that k can be as high as floor(nm/2). In our partition with first k-1 tubes length 2, the last tube length = N - 2(k-1). Since k ≤ floor(N/2), N - 2(k-1) = N - 2k + 2 ≥ N - N + 2 = 2, so okay. And if k = N/2 exactly (N even), then last tube length = N - 2*(N/2 - 1) = N - N + 2 = 2, so all tubes length 2.
Thus works.
I think this is the intended simple solution.
Now produce final answer.
But maybe there is a more intricate requirement: The tube cells must be consecutive in the path? Not specified. So fine.
Thus final answer.
Now note: In the output, we need to print exactly k lines. Our code prints exactly k lines (k-1 + 1 = k). Good.
Now I'll write solution with proper function.
We can also generate snake order differently: use a boolean flag to alternate direction.
Now, provide final solution in Python.
But what if n,m up to 300, so N up to 90000; flattening coordinates into string and constructing output string might be large but fine.
Optimization: Use sys.stdout.write.
I'll produce final code.
I'll also add a check for k==0? Not needed.
Thus final.
But I recall some Codeforces problem "Fancy Fence" or similar. Actually this is from Codeforces Round #... Possibly problem "Fancy Number"? Not sure. But this approach is common.
Now let's think about alternative: The problem says "It is guaranteed that at least one solution exists." So we can always produce some.
But is there any issue with the Hamiltonian path existence? For any grid, there is a Hamiltonian path (like a snake). So fine.
Now final.
I will produce final solution. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
n, m, k = map(int, data[:3])
code
# build a Hamiltonian path (snake order)
cells = []
for row in range(1, n + 1):
if row % 2 == 1: # left to right
for col in range(1, m + 1):
cells.append((row, col))
else: # right to left
for col in range(m, 0, -1):
cells.append((row, col))
total = len(cells)
out_lines = []
idx = 0
# first k-1 tubes have length 2
for i in range(k - 1):
r = 2
segment = cells[idx:idx + r]
flat = [str(r)] + [str(x) for pair in segment for x in pair]
out_lines.append(' '.join(flat))
idx += r
# last tube gets the remaining cells
r = total - idx
segment = cells[idx:]
flat = [str(r)] + [str(x) for pair in segment for x in pair]
out_lines.append(' '.join(flat))
sys.stdout.write('\n'.join(out_lines))