给定的阵列ARR []的长度N,尺寸N * N的矩阵是在该阵列ARR定义[]其中,M I,J =常用3 I&ARRĴ。给定四个整数X,Y,S和T ,任务是找到子矩阵从左上角(X,Y)到右下角(S,T)的所有元素的按位XOR。
例子:
Input: N = 3, A[] = {2, 3, 4}, (X, Y)=(0, 1), (S, T)=(2, 2)
Output: 5
Explanation:
Matrix defined on A is
{{(2&2), (2&3), (2&4)},
{(3&2), (3&3), (3&4)},
{(4&2), (4&3), (4&4)}}
Finally, the matrix will be:
{{2, 2, 0},
{2, 3, 0},
{0, 0, 4}}
XOR value= (2^0)^(3^0)^(0^4) = 5
Input: N=3, A[]={1, 2, 3}, (X, Y)=(0, 1), (S, T)=(1, 2)
Output: 1
天真的方法:最简单的方法是从给定的数组生成矩阵M ,并计算M的给定子矩阵中存在的所有元素的按位XOR。
时间复杂度: O(N 2 )
辅助空间: O(N 2 )
高效方法:想法是使用“ XOR”和“ AND”运算的以下分布属性:
(A & B) ^ (A & C) = A & (B ^ C)
因此,可以从下式计算子矩阵从左上角(X,Y)到右下角(S,T)的最终XOR:
Final XOR
= (XOR of row X)^(XOR of row X+1)^. . . . ^(XOR of row S)
= (AX & (AY ^. . . .^ AT)) ^ ….
. . . ^(AS & (AY^. . . .^AT))
= (AY^. . . .^AT)&(AX^. . .^AS)
- 从索引Y到T遍历数组,然后计算元素的XOR 。
- 从索引X到S遍历数组,并计算元素的XOR。
- 最后,计算所计算的XOR的按位与,等于从(X,Y)到(S,T)的子矩阵的按位XOR。
下面是上述方法的实现:
C++
// C++ program of the
// above approach
#include
using namespace std;
// Function to find the submatrix
// XOR of the given matrix
int submatrix_xor(int* A, int N,
int X, int Y,
int S, int T)
{
int left_xor = 0, i, right_xor = 0;
// Calculating left xor
// i.e A[Y]^A[Y+1]^. . .^A[T]
for (i = Y; i <= T; i++) {
left_xor ^= A[i];
}
// Calculating right xor
// i.e A[X]^A[X+1]^. . .^A[S]
for (i = X; i <= S; i++) {
right_xor ^= A[i];
}
// Bitwise AND of left_xor and
// right_xor gives required result
return left_xor & right_xor;
}
// Driver Code
int main()
{
int A[3] = { 2, 3, 4 }, X = 0,
Y = 1, S = 2, T = 2, N = 3;
// Printing xor of submatrix
cout << submatrix_xor(A, N, X,
Y, S, T);
return 0;
}
Java
// Java program of the
// above approach
import java.io.*;
class GFG{
// Function to find the submatrix
// XOR of the given matrix
static int submatrix_xor(int[] A, int N,
int X, int Y,
int S, int T)
{
int left_xor = 0, i, right_xor = 0;
// Calculating left xor
// i.e A[Y]^A[Y+1]^. . .^A[T]
for(i = Y; i <= T; i++)
{
left_xor ^= A[i];
}
// Calculating right xor
// i.e A[X]^A[X+1]^. . .^A[S]
for(i = X; i <= S; i++)
{
right_xor ^= A[i];
}
// Bitwise AND of left_xor and
// right_xor gives required result
return left_xor & right_xor;
}
// Driver Code
public static void main (String[] args)
{
int[] A = { 2, 3, 4 };
int X = 0, Y = 1, S = 2,
T = 2, N = 3;
// Printing xor of submatrix
System.out.print(submatrix_xor(A, N, X,
Y, S, T));
}
}
// This code is contributed by code_hunt
Python3
# Python3 program of the
# above approach
# Function to find the submatrix
# XOR of the given matrix
def submatrix_xor(A, N, X, Y, S, T):
left_xor = 0
i = 0
right_xor = 0
# Calculating left xor
# i.e A[Y]^A[Y+1]^. . .^A[T]
for i in range(Y, T + 1):
left_xor ^= A[i]
# Calculating right xor
# i.e A[X]^A[X+1]^. . .^A[S]
for i in range(X, S + 1):
right_xor ^= A[i]
# Bitwise AND of left_xor and
# right_xor gives required result
return left_xor & right_xor
# Driver Code
if __name__ == '__main__':
A = [ 2, 3, 4 ]
X = 0
Y = 1
S = 2
T = 2
N = 3
# Printing xor of submatrix
print(submatrix_xor(A, N, X, Y, S, T))
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the submatrix
// XOR of the given matrix
static int submatrix_xor(int[] A, int N,
int X, int Y,
int S, int T)
{
int left_xor = 0, i, right_xor = 0;
// Calculating left xor
// i.e A[Y]^A[Y+1]^. . .^A[T]
for(i = Y; i <= T; i++)
{
left_xor ^= A[i];
}
// Calculating right xor
// i.e A[X]^A[X+1]^. . .^A[S]
for(i = X; i <= S; i++)
{
right_xor ^= A[i];
}
// Bitwise AND of left_xor and
// right_xor gives required result
return left_xor & right_xor;
}
// Driver Code
public static void Main ()
{
int[] A = { 2, 3, 4 };
int X = 0, Y = 1, S = 2,
T = 2, N = 3;
// Printing xor of submatrix
Console.Write(submatrix_xor(A, N, X,
Y, S, T));
}
}
// This code is contributed by code_hunt
Javascript
5
时间复杂度: O(N),其中N是数组的大小
辅助空间: O(1)