Maximizing the Matrix

4

2 votes
Medium, Approved
Problem

This is an approximate problem.

Dexter has started learning matrix multiplication recently. His teacher gives him a task to perform.

She gives him two N x N matrices, A and B, containing integers. In a single operation he can swap Ai,j and Bi,j, where 1iN and 1jN.

He is allowed to perform up to K such operations on the matrices A and B in some particular order. After performing these operations, he finds the product of the two matrices, i.e., C=AB. Now, he finds the sum of values at all the cells of C. His target is to maximize the value of this sum.

Suppose he performed T(0<=T<=K) such operations, then he should output the value of T. And then T lines describing each operation in the order he performed them.

Input Format

The first line of input contains two space separated integers N and K, denoting the width of the two square matrices A and B and the number of operations allowed, respectively.

Next N lines contain N integers per line, denoting the values in the matrix A. The jth integer in the (i+1)th line denotes the value Ai,j.

Next N lines contain N integers per line, denoting the values in the matrix B. The jth integer in the (i+N+1)th line denotes the value Bi,j.

Output Format

The first line of input contains a single integer, T, the number of operations performed by Dexter. Next T lines contain two space separated integers per line,i,j denoting that Ai,j and Bi,j had been swapped by Dexter in that operation.

Constraints

1N100
1K50
5 x 105Ai,j,Bi,j5 x 105, where 1iN and 1jN.

Tests

Contest test set contains 50 tests.

Tests 1-5: 1N3,1K10

Tests 6-12: 1N3,1K10, contain only 0s and 1s.

Tests 13-17: 1N10,1K50

Tests 18-24: 1N10,1K50, contain only 0s and 1s

Tests 25-39: 70N100,1K50,

Tests 40-50: 70N100,1K50, contain only 0s and 1s

The solutions will be run on additional 50 tests after the contest ends.

Time Limit: 3
Memory Limit: 512
Source Limit:
Explanation

It can be verified that if we use this sequence of operations, then the value of the obtained sum after matrix multiplication of A and B is maximized and is equal to 538.

Editor Image

?