#K61217. Matrix Construction with Row Parity Constraints
Matrix Construction with Row Parity Constraints
Matrix Construction with Row Parity Constraints
You are given two integers (N) and (M). Your task is to construct an (N \times N) matrix such that exactly (M) rows have an even sum and the remaining (N-M) rows have an odd sum. The matrix is initially filled with consecutive integers from 1 to (N^2) in row-major order. After that, for the first (M) rows, if the row sum is odd, you must add 1 to the last element of that row to make the sum even, and for the remaining rows, if the row sum is even, you must add 1 to the last element to ensure the sum is odd.
The input is read from standard input and the matrix must be printed to standard output with each row on a new line, and elements within a row separated by a single space.
inputFormat
The input consists of a single line containing two space-separated integers (N) and (M), where (N) is the dimension of the matrix and (M) is the number of rows required to have an even sum. It is guaranteed that (0 \le M \le N).
outputFormat
Print the resulting (N \times N) matrix to standard output. Each of the (N) lines represents a row of the matrix, with elements separated by a single space.## sample
3 2
1 2 3
4 5 7
7 8 10
</p>