#P12352. Equalizing a Permutation
Equalizing a Permutation
Equalizing a Permutation
We are given a permutation (p = {p_1, p_2, \ldots, p_n}) of length (n) with initial condition (p_i = i) (i.e. the permutation is (1, 2, \ldots, n)). In one operation you can choose exactly (m) distinct positions and simultaneously set each of the chosen elements to their (arithmetic) average (note that the average is not rounded, and it may be a non‐integer number). Your goal is to make all elements equal. The final common value will be the overall average (\frac{1+2+\cdots+n}{n} = \frac{n+1}{2}). If a solution does not exist, you must output (-1).
Note: You do not need to minimize the number of operations. However, you must output an explicit list of operations when a solution exists. Each operation should list exactly (m) distinct indices (1–indexed) that you choose for that operation.
Important observations:
- If \(n = 1\), the array is already equal.
- If \(m = n\), one operation on the full array will immediately equalize it.
- For some values of \(m < n\) a solution exists and a valid constructive solution can be given. In our problem we will only produce solutions in the following cases:
- When \(m = 2\): We can pair up indices such that for a pair \((i, n+1-i)\) the average is \(\frac{i+(n+1-i)}{2} = \frac{n+1}{2}\). For even \(n\) we pair up all indices; for odd \(n\) the middle element is already \(\frac{n+1}{2}\) and the other indices are paired.
- When \(m = n\): A single operation over all indices suffices.
- When \(m = 3\) and \(n\) is odd: Note that the middle element is \(t = \frac{n+1}{2}\). For every index \(i\) with \(1 \le i < t\) the index \(j = n+1-i\) satisfies \(i + j = n+1\). Then an operation on indices \((t, i, j)\) yields an average of \(\frac{t + i + j}{3} = \frac{\frac{n+1}{2} + n+1}{3} = \frac{n+1}{2}\).
- If \(m = 1\) (with \(n > 1\)) or if the parameters do not fall into one of the above three cases, output \(-1\).
Thus, your task is to construct a (possible) operation plan following the above strategy. If the input parameters \(n\) and \(m\) do not allow a solution (i.e. if \(n > 1\) and \(m = 1\) or if \(m \not\in \{2,3,n\}\) with the additional requirement that when \(m=3\) we need \(n\) odd), then output \(-1\).
Input-Output summary:
- Input: Two integers \(n\) and \(m\) on one line.
- Output: If a solution exists, first output an integer \(k\) (the number of operations), followed by \(k\) lines each containing exactly \(m\) space‐separated integers representing the 1–indexed positions chosen in that operation. If no solution exists, output \(-1\). </p>
inputFormat
The input consists of a single line containing two integers (n) and (m) (where (1 \le n \le 10^5) and (1 \le m \le n)).
outputFormat
If a valid operation plan exists, first output an integer (k) (the number of operations), then output (k) lines, each containing exactly (m) distinct space–separated integers (the indices chosen for that operation). If no valid plan exists, output a single line with (-1).
sample
4 4
1
1 2 3 4
</p>