#P12356. Equalize Permutation by Averaging Operations
Equalize Permutation by Averaging Operations
Equalize Permutation by Averaging Operations
You are given a permutation of length \(n\) where initially \(p_i = i\) (for \(1 \le i \le n\)). In each operation you may choose exactly \(m\) distinct positions and simultaneously update each chosen element to the (exact) average of their current values (no rounding is performed). In the end you must have all elements equal (which will then equal the overall average \(\frac{n+1}{2}\)).
It turns out that a solution does not always exist. In particular, one may prove that when \(m \ge 3\) a necessary condition for a solution is that (i) if \(m=2\) a solution always exists by pairing numbers that sum to \(n+1\) (since \(i+(n+1-i)=n+1\)), and (ii) if \(m \ge 3\) then a solution exists under the restrictions that \(n\) is odd, \(m\) is odd, and \(n-1\) is divisible by \(m-1\) (so that the minimal number of operations is \(\frac{n-1}{m-1}\)).
Your task is to output a valid sequence of operations that equalizes the permutation using the minimal number of operations (if one exists) or output -1
if no solution exists. Note that in each operation the overall sum of the numbers is preserved so that if all numbers become equal the common value must be the overall average \(\frac{n+1}{2}\).
inputFormat
The input consists of two integers \(n\) and \(m\) on a single line.
outputFormat
If it is impossible to equalize the permutation under the operation rules, output -1
. Otherwise, on the first line output the minimum number of operations \(k\), and then output \(k\) lines. Each line should contain exactly \(m\) integers—the 1-indexed positions chosen in that operation. When the operations are performed (with exact arithmetic) all entries will become \(\frac{n+1}{2}\).
sample
3 2
1
1 3
</p>