This page is still under construction.

Parts of this page are still being built. What you see may change.

XOR Sequence

Time limit2sMemory limit512 MB

Summary
Choose B in [0, N-1] to XOR every element of A, then find the maximum possible count of index pairs i < j with C_i < C_j.
Level

Hard8 of 10

Topics
Divide and conquer, Bit manipulation, Sorting, Recursion
Solved
No attempts yet

Problem

You are given an integer NN and a sequence AA of length MM. NN is a power of two, and every element of AA is an integer between 00 and N−1N-1.

You may pick one integer BB between 00 and N−1N-1 and build a new sequence CC. For every ii, set Ci=Ai⊕BC_i = A_i \oplus B, where ⊕\oplus is bitwise exclusive or (XOR).

Then count the pairs (i,j)(i, j) with i<ji < j and Ci<CjC_i < C_j in CC.

Write a program that finds the largest possible number of such pairs over every choice of BB.

Input

The first line contains NN. (2≤N≤2302 \le N \le 2^{30}, NN is a power of two)

The second line contains the size MM of the sequence AA. (2≤M≤1310722 \le M \le 131072)

The third line contains A1,A2,…,AMA_1, A_2, \dots, A_M separated by spaces. (0≤Ai≤N−10 \le A_i \le N-1)

Output

Print the largest possible number of pairs (i,j)(i, j) with i<ji < j and Ci<CjC_i < C_j.

Hint

For N=4N = 4 and A=[3,2,1,0,3,2]A = [3, 2, 1, 0, 3, 2], choosing B=3B = 3 gives C=[0,1,2,3,0,1]C = [0, 1, 2, 3, 0, 1]. That sequence has 8 pairs with i<ji < j and Ci<CjC_i < C_j.

Examples4

  1. Example 1

    Input
    4
    6
    3 2 1 0 3 2
    
    Expected output
    8
    
  2. Example 2

    Input
    8
    8
    2 5 7 2 3 5 2 5
    
    Expected output
    13
    
  3. Example 3

    Input
    8
    7
    3 0 7 2 7 4 3
    
    Expected output
    12
    
  4. Example 4

    Input
    32
    15
    7 9 0 4 9 31 2 26 11 21 4 16 13 11 6
    
    Expected output
    60