luogu#P16340. 「ALFR Round 10」XOR and Simple Trick

    ID: 16183 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>洛谷原创O2优化分治排序字典树 Trie位运算洛谷月赛

「ALFR Round 10」XOR and Simple Trick

Problem Description

Given a sequence aa of nn non-negative integers, find the number of pairs of indices (i,j)(i, j) such that 1i<jn1 \le i < j \le n, and for every aka_k and every non-negative integer xx, the following condition holds: akxaixa_k \oplus x \le a_i \oplus x OR akxajxa_k \oplus x \ge a_j \oplus x.

Input Format

This problem contains multiple test cases. The first line contains a positive integer TT, representing the number of test cases.

For each test case:

  • The first line contains a positive integer nn.
  • The second line contains nn non-negative integers, where the ii-th number represents aia_i.

Output Format

For each test case, output a single integer representing the answer.

4
5
1 4 5 2 6
7
3 1 4 5 9 2 6
3
114 51 4
6
1 2 4 8 16 32
2
2
1
1

Hint

【Explanation of Sample 1】

For the first test case, the valid pairs (i,j)(i, j) are (1,4)(1, 4) and (2,3)(2, 3).

【Constraints】

Let n\sum n be the sum of nn over all test cases in a single test file, and VV be the maximum value of aia_i in a single test file.

For all test cases:

  • 1T5×1051 \le T \le 5 \times 10^5
  • 2n,n1062 \le n, \sum n \le 10^6
  • 0ai<2300 \le a_i < 2^{30} for all 1in1 \le i \le n
  • All aia_i are distinct within a single test case.

This problem uses bundled testing. Subtasks are as follows:

Subtask n\sum n \le V<V < Score
1 100100 272^7 66
2 500500 292^9 88
3 10410^4 ^ 2020
4 10610^6 2122^{12} 1414
5 200200 2302^{30} 2020
6 10610^6 ^ 3232