📜  所有子矩阵的XOR的XOR

📅  最后修改于: 2021-05-25 04:36:21             🧑  作者: Mango

给定一个“ N * N”矩阵,任务是找到所有可能子矩阵的XOR的XOR。
例子:

Input :arr =  {{3, 1},
               {1, 3}}
Output : 0
Explanation: All the elements lie in 4 submatrices each. 
4 being even, there total contribution towards 
final answer becomes 0. Thus, ans = 0.

Input : arr = {{6, 7, 13},
               {8, 3, 4},
               {9, 7, 6}};
Output : 4

一种简单的方法是生成所有可能的子矩阵,唯一地找到每个子矩阵的XOR,然后对它们进行XOR。该方法的时间复杂度将为O(n 6 )。
更好的解决方案:对于每个索引(R,C),我们将尝试查找该索引所在的子矩阵的数量。如果子矩阵的数量为奇数,则最终答案将更新为ans =(ans ^ arr [R] [C]) 。如果是偶数,我们不需要更新答案。之所以可行,是因为与自身进行异或的数字为零,并且运算顺序不会影响最终的异或值。
假设基于0的索引,则索引(R,C)所在的子矩阵的数量等于

(R + 1)*(C + 1)*(N - R)*(N - C)

下面是上述方法的实现:

C++
// C++ program to find the XOR of XOR's of
// all submatrices
 
#include 
using namespace std;
 
#define n 3
 
// Function to find to required
// XOR value
int submatrixXor(int arr[][n])
{
    int ans = 0;
 
    // Nested loop to find the
    // number of sub-matrix each
    // index belongs to
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            // Number of ways to choose
            // from top-left elements
            int top_left = (i + 1) * (j + 1);
 
            // Number of ways to choose
            // from bottom-right elements
            int bottom_right = (n - i) * (n - j);
 
            if ((top_left % 2 == 1) && (bottom_right % 2 == 1))
                ans = (ans ^ arr[i][j]);
        }
    }
 
    return ans;
}
 
// Driver Code
int main()
{
    int arr[][n] = { { 6, 7, 13 },
                     { 8, 3, 4 },
                     { 9, 7, 6 } };
 
    cout << submatrixXor(arr);
 
    return 0;
}


Java
//Java program to find the XOR of XOR's
// of all submatrices
class GFG
{
     
// Function to find to required
// XOR value
static int submatrixXor(int[][]arr)
{
    int n = 3;
    int ans = 0;
 
    // Nested loop to find the
    // number of sub-matrix each
    // index belongs to
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            // Number of ways to choose
            // from top-left elements
            int top_left = (i + 1) * (j + 1);
 
            // Number of ways to choose
            // from bottom-right elements
            int bottom_right = (n - i) * (n - j);
 
            if ((top_left % 2 == 1) &&
                (bottom_right % 2 == 1))
                ans = (ans ^ arr[i][j]);
        }
    }
 
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int[][] arr = {{ 6, 7, 13},
                   { 8, 3, 4 },
                   { 9, 7, 6 }};
 
    System.out.println(submatrixXor(arr));
}
}
 
// This code is contributed
// by Code_Mech.


Python3
# Python3 program to find the XOR of
# XOR's of all submatrices
 
# Function to find to required
# XOR value
def submatrixXor(arr, n):
 
    ans = 0
 
    # Nested loop to find the
    # number of sub-matrix each
    # index belongs to
    for i in range(0, n):
        for j in range(0, n):
 
            # Number of ways to choose
            # from top-left elements
            top_left = (i + 1) * (j + 1)
 
            # Number of ways to choose
            # from bottom-right elements
            bottom_right = (n - i) * (n - j)
            if (top_left % 2 == 1 and
                bottom_right % 2 == 1):
                ans = (ans ^ arr[i][j])
    return ans
 
# Driver code
n = 3
arr = [[6, 7, 13],
       [8, 3, 4],
       [9, 7, 6]]
print(submatrixXor(arr, n))
 
# This code is contributed by Shrikant13


C#
// C# program to find the XOR of XOR's
// of all submatrices
using System;
 
class GFG
{
     
// Function to find to required
// XOR value
static int submatrixXor(int [,]arr)
{
    int n = 3;
    int ans = 0;
 
    // Nested loop to find the
    // number of sub-matrix each
    // index belongs to
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            // Number of ways to choose
            // from top-left elements
            int top_left = (i + 1) * (j + 1);
 
            // Number of ways to choose
            // from bottom-right elements
            int bottom_right = (n - i) * (n - j);
 
            if ((top_left % 2 == 1) &&
                (bottom_right % 2 == 1))
                ans = (ans ^ arr[i, j]);
        }
    }
 
    return ans;
}
 
// Driver Code
public static void Main()
{
    int [, ]arr = {{ 6, 7, 13},
                   { 8, 3, 4 },
                   { 9, 7, 6 }};
 
    Console.Write(submatrixXor(arr));
}
}
 
// This code is contributed
// by Akanksha Rai


PHP


Javascript


输出:
4

时间复杂度:O(N 2 )
辅助空间: O(1)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。