


You are given a matrix βMβ having dimensions βWIDTH x βHEIGHTβ and provided with an integer βNβ. The matrix βMβ can take only binary values, i.e., 0 and 1. Your task is to find the βmaximum number of onesβ that the matrix βMβ can have such that every square sub-matrix of M of dimensions βNβ x βNβ has no more than βMAXONESβ ones.
The first line contains an integer βTβ, which denotes the number of test cases to be run. Then, the T test cases follow.
The first and only line of each test case contains four positive integers, βWIDTHβ, βHEIGHTβ, βNβ, and βMAXONESβ, as described in the problem statement.
Output Format:
For each test case, print a single line containing a single integer denoting the maximum number of ones that the matrix M can have.
The output for each test case will be printed in a separate line.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= βTβ <= 10
1 <= βWIDHTβ, βHEIGHTβ <= 1000
1 <= βNβ <= WIDHT, HEIGHT
0 <= βMAXONESβ <= N * N
Where βTβ is the number of test cases, βWIDHTβ x βHEIGHTβ is the dimension of the matrix, βNβ * βNβ is the dimension of the square sub-matrix, and βMAXONESβ is the maximum number of ones a square sub-matrix can have.
Time Limit: 1sec.
1
2 2 1 1
4
In a 2*2 matrix, each 1*1 sub-matrix can have at most 1 one. So, the best solution will be:
[1, 1]
[1, 1]
1
3 3 2 0
0
Each sub-matrix of dimensions 2*2 must have 0 ones. So, the matrix M must contain all entries to be 0:
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
Try to divide the matrix into non-overlapping sub-matrices.
O((N ^ 2) * log(N ^ 2)), where N * N is the dimensions of the sub-matrix.
We are creating an array of size N * N that contains all the subMatrix elements, and we are sorting this array. So, the time complexity will be O((N ^ 2) * log(N ^ 2)).
O(N ^ 2), where N * N is the dimensions of the sub-matrix.
As we are creating an extra matrix and an array both of size N * N. Hence, the space complexity will be O(N ^ 2).