#P15134. [ROIR 2026] XOR 染色

[ROIR 2026] XOR 染色

Problem Description

Given two non-negative integer arrays A=[a1,a2,…,an]A=[a_1, a_2, \ldots, a_n] and B=[b1,b2,…,bm]B=[b_1, b_2, \ldots, b_m].

Define S(i)={j∣(ai⊕bj)≤x}S(i) = \{j | (a_i \oplus b_j) \leq x\}. In other words, S(i)S(i) is the set of indices jj in array BB such that the bitwise XOR of aia_i and bjb_j is at most xx.

Find the minimum number kk such that the elements in array AA can be colored using kk colors, and the following condition holds: if S(x)S(x) and S(y)S(y) intersect, then xx and yy must be colored with different colors.

That is, you need to find c1,c2,…,cnc_1, c_2, \ldots, c_n such that 1≤ci≤k1 \le c_i \le k, and if S(x)∩S(y)≠∅S(x) \cap S(y) \neq \varnothing, then cx≠cyc_x \neq c_y.

As a reminder, the bitwise XOR (⊕\oplus, xor) of two non-negative integers is defined as follows: write both numbers in binary, and the ii-th bit of the result is 1 if and only if exactly one of the two numbers has a 1 in that bit. For example, $(14 \text{ xor } 7) = (1110_2 \oplus 0111_2) = 1001_2 = 9$. This operation is implemented in all modern programming languages: it is written as ^ in C++, Java, and Python, and as xor in Pascal.

Input Format

The input contains multiple test cases.

The first line contains an integer tt (1≤t≤100)(1 \le t \le 100), the number of test cases.

The following lines describe the test cases.

The first line of each test case contains three integers nn, mm, and xx (1≤n,m≤500 0001 \le n, m \le 500\,000, 0≤x<2300 \leq x < 2^{30}).

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n, the elements of array AA (0≤ai<2300 \le a_i < 2^{30}).

The third line contains mm integers b1,b2,…,bmb_1, b_2, \ldots, b_m, the elements of array BB (0≤bi<2300 \le b_i < 2^{30}).

It is guaranteed that the sum of nn over all test cases and the sum of mm over all test cases are both at most 500 000500\,000.

Output Format

For each test case, output one integer, the required minimum kk.

3
2 2 0
0 0
1 1
5 5 3
0 1 2 3 4
0 1 2 3 4
5 5 4
0 1 2 3 4
0 1 2 3 4
1
4
5

Hint

Scoring Rules

Subtask Points Additional Constraints Required Subtasks
1 5 n≤2n \le 2 ---
2 n≤5n \le 5 1
3 n≤15n \le 15 1, 2
4 n≤100n \le 100 1–3
5 n≤2 000n \le 2\,000 1–4
6 10 n≤5 000n \le 5\,000 1–5
7 5 n≤100 000n \le 100\,000,m=2m = 2 ---
8 10 n≤100 000n \leq 100\,000,m=3m = 3
9 5 n,m≤100 000n, m \le 100\,000;ai,bi,k<2a_i, b_i, k < 2
10 n,m≤100 000n, m \le 100\,000;ai,bi,k<4a_i, b_i, k < 4 9
11 35 No additional constraints. 1–10

Translated by ChatGPT 5