Chef and Submatrix
|
All submissions for this problem are available.
Read problems statements in Mandarin Chinese and Russian.
Chef has a square matrix A. He would like to choose a consecutive sub-matrix of A such that value of bitwise XOR of all elements in it is maximal.
Each sub-matrix can be defined by four numbers (x1,y1,x2,y2), with the meaning that the submatrix contains an element A[i][j] iff x1 ≤ i ≤ x2 and y1 ≤ j ≤ y2.
You can read more about bitwise XOR operation here.
Input
The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of the matrix A.
The following N lines contain N space-separated integers, denoting values of elements in given matrix. The j-th element in the i-th line denotes the value of A[i][j].
Output
For each test case, output a single line containing the maximal bitwise XOR a submatrix of A
Constraints
- 1 ≤ T ≤ 10
- 1 ≤ N ≤ 70
- 1 ≤ Ai,j ≤ 109
- Subtask 1 (40 points): 1 ≤ N ≤ 20
- Subtask 2 (60 points): 1 ≤ N ≤ 70
Example
Input: 1 3 1 4 3 1 8 6 1 2 3 Output: 15
Explanation
Chef can pick the following submatrix in order to make the XOR of elements equal to 15:
1 4 3 1 8 6 1 2 3
This submatrix can be defined by x1 = 1, y1 = 1, x2 = 3, y2 = 2.
==========================================editorial=======================================
==========================================editorial=======================================
DIFFICULTY:
Easy
PREREQUISITES:
bits, implementation
PROBLEM:
You are given a square matrix of size . For a submatrix of , we define its value as the bitwise of all elements of . Your task is to find the maximum value among all submatrices of .
QUICK EXPLANATION:
Since , if we are able to compute a value for a given submatrix in constant time, we can iterate over all submatrices and return the maximum value. In order to compute the value for a given submatrix fast, we first compute values of all submatrices which have the upper-left corner at . Then we can use these values to compute every other value using properties of operation.
EXPLANATION:
Let be the value of the submatrix which has its upper left corner at and its bottom right corner in . We can compute the table in or , or even , but since is quite small, it does not matter which method you choose.
After that, we notice the major property of , i.e. . Using this property and precomputed table , we can easily compute the value for any submatrix in constant time. Let's consider the below illustration and define 5 submatrices:
- , the Red submatrix, we are interested in computing its value
- , the Blue submatrix, we have its value computed and stored in table
- , the Green submatrix, we have its value computed and stored in table
- , the Yellow submatrix, we have its value computed and stored in table
- , the Purple submatrix, we have its value computed and stored in table
Let's define, for the above matrices, to be the value from table for a submatrix , for example is the value of the matrix . Then
This is duo to the major property of and the fact that both and contain as a submatrix.
Time complexity
For a single test case it is because we can compute the table in ; and we compute the value for every submatrix in a constant time, and there are different submatrices.
=============================code========================
- #include<iostream>
- using namespace std;
- typedef long long int lli;
- lli arr[100][100];
- int main()
- {
- int t;
- cin>>t;
- while(t--)
- {
- int n;
- cin>>n;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=n;j++)
- {
- cin>>arr[i][j];
- arr[i][j]^=arr[i-1][j]^arr[i][j-1]^arr[i-1][j-1];
- }
- }
- lli ans=0;
- for(int i=1;i<=n;i++)
- {
- for(int j=1;j<=n;j++)
- {
- for(int k=1;k<=n;k++)
- {
- for(int l=1;l<=n;l++)
- {
- lli com=arr[k][l]^arr[i-1][j-1]^arr[i-1][l]^arr[k][j-1];
- if(com>ans) ans=com;
- }
- }
- }
- }
- cout<<ans<<endl;
- }
- }