How to find maximum xor in a sub matrix?

后端 未结 3 778
没有蜡笔的小新
没有蜡笔的小新 2021-01-17 03:20

eg given 3 x 3 matrix

   1 2 3
   4 5 6
   7 8 9

has max xor value = 15 sub matrix

   2 
   5 
   8

tha

3条回答
  •  無奈伤痛
    2021-01-17 04:12

    One way to speed up a brute force try-all-possibilities approach is to find a way of working out the result of an arbitrary sub-matrix without xorring together all its elements. You can do this if you prepare a table where the entry at T(x, y) is the xor of all elements (i,j) where i <= x and j <= y, as you can then calculate any sub-matrix by xorring together four elements.

    Now if you want the answer for e.g. the submatrix M(3-10, 5-23) you should find that it is T(10, 23) ^ T(2, 23) ^ T(10, 4) ^ T(2, 4)

    The first term covers all of the elements you want, but includes those with i and j values too low. The second term removes those with i values too low. The second term removes those with j values too low, but also does a double removal of those with both i and j values too low. The final term corrects for this double removal.

    You can also think of this as an application of the principle of inclusion-exclusion (https://en.wikipedia.org/wiki/Inclusion%E2%80%93exclusion_principle) or draw yourself a picture showing four overlapping rectangles co-operating to draw a submatrix.

提交回复
热议问题