# 2560 XOR and training issues jdfz - Qin Yue

Title Description

N numbers, select the required M times from N to select the number of the two numbers (Ai, Aj) ( but not before a certain time and choose the same ) , the score of the selected Ai XOR Aj .

Seeking maximum score.

Input Format

The first line contains two integers N, M

The next line Total N integers described N digits.

Output Format

Output an integer representing the maximum score is divided by 10 ^ 9 + 7 remainder of

solution

  Ans set number twenty-two exclusive OR obtained large number of m.

  The number N into a trie ran together, ans for each bit of Determine all XOR which a is a number of 1 to number ① adding a known XOR result is greater than the number of pairs of ans number ② , if> = 2 * m two (since the repetition time is calculated between every two numbers), then this must be 1, otherwise it must be 0 .

(for example, ans = 01xxxx, a ^ b = 011xxx, the statistical .a ^ b = 010xxx, no statistical .a ^ b = 101xxx not count.)

(ANS = 0101xx if a ^ b = 1xxxxx the statistics .a ^ b = 011xxx the statistics.)

  For convenience of description, the definition of "exclusive or an" exclusive OR of a result of two numbers. A number of p bits means that the binary numbers from left to right at this number Bit p.

  The calculation of the K-bit ans, ans previous K-1 bits are determined. Ans so the method of calculating the position of K is: the enumeration of a number N i, and then ran from the high to low in the trie tree. If the p-bit ans are 0, sum (total) = + after the first p-bit XOR i is a number from 1 to number. Equal to the XOR of the p-bit ans direction to go. Sum + = Finally, after the K-bit XOR is a number. Particularly, we need to store a size on each node in the trie, expressed in number of all the node number of the root node of the subtree. Thus, the XOR Statistical Bit p i is the number 1, can only access size O (1) was calculated. If sum> = 2 * m, then this is definitely 1, otherwise it must be 0. Complexity nlogn.

 

  After obtaining a large value m ans, each number enumerated, and it calculates the exclusive OR> = ans and number. Or from high to low: If the current K-bit is 1, then only to exclusive or after 1 is equal to the direction to go, direct recursion. Otherwise, we need to XOR tree after the child is 1 of statistics into the answer, then the direction of 0 XOR equal to go. How Statistics answer? In the establishment of a trie tree and need to maintain information for each node, this is the number of points to the root of the subtree contained in each and every number is the number 1, the number is the number 0, so that Fengyun can calculate a number and the number of all sub-tree and the exclusive-or within logn time. And then add the current number such that the i-th XOR bit for that node is the root of the subtree and XOR like.

  The overall complexity nlog ^ 2n, small but constant.

Guess you like

Origin www.cnblogs.com/MayDayMemory/p/12093079.html
yue
XOR