#P10218. [省选联考 2024] 魔法手杖

[省选联考 2024] 魔法手杖

Problem Description

Note: We provide a brief, formal statement at the end of the description.

City C is a capital of magic, famous for its top-level wizards. For a wizard, the most important things are, of course, the magic wand and the magic crystals embedded in it.

Each magic wand and each magic crystal can be measured by a magic value. The magic value of a wand is the minimum magic value among all crystals embedded in it.

Apprentice wizard little ω\omega from City C wants to strengthen his wand. Before strengthening, his wand has nn crystals embedded, whose magic values are a1,a2,,ana_1,a_2,\dots,a_n.

Little ω\omega plans to use a powerful secret technique once to strengthen his wand. In this technique, he may choose any xx, and then change every crystal’s magic value from aia_i to (aix)(a_i \oplus x), where \oplus denotes bitwise XOR. Due to little ω\omega’s limited ability, a1,a2,,ana_1,a_2,\dots,a_n and xx are all integers in [0,2k1][0,2^k-1].

Little ω\omega also finds that this technique can be strengthened in a targeted way. Specifically, he can spend bib_i stamina to apply targeted strengthening to the ii-th crystal, changing its value from what would become (aix)(a_i \oplus x) into (ai+x)(a_i+x). Since little ω\omega is limited, the total stamina spent on targeted strengthening cannot exceed mm, and each crystal can be targeted at most once.

Little ω\omega wants to know the maximum possible magic value of the wand after strengthening. He cannot compute it, so please help him.

Formally: Given a1,a2,,ana_1,a_2,\dots,a_n and b1,b2,,bnb_1,b_2,\dots,b_n, where ai[0,2k1]a_i \in [0,2^k-1] and bi0b_i\geq 0, you need to find S{1,2,,n}S \subseteq \{1,2,\dots,n\} and x[0,2k1]x \in [0,2^k-1] satisfying:

  • iSbim\sum \limits_{i\in S} b_i\leq m;
  • Under the above constraint, maximize $val(S,x)=\min(\min \limits_{i \in S}(a_i+x),\min \limits_{i \in U \backslash S}(a_i \oplus x))$.

You only need to output the maximum value of val(S,x)val(S,x).

Input Format

This problem has multiple test cases. The first line contains two integers c,Tc,T, denoting the subtask index and the number of test cases. In the samples, cc means the constraints are the same as those of subtask cc.

Then for each test case:

  • The first line contains three integers n,m,kn,m,k.
  • The second line contains nn integers a1,a2,,ana_1,a_2,\dots,a_n, representing the initial magic values of the crystals.
  • The third line contains nn integers b1,b2,,bnb_1,b_2,\dots,b_n, representing the stamina cost of targeted strengthening for each crystal.

Output Format

For each test case, output one integer per line: the maximum magic value little ω\omega can obtain for the wand.

1 2
5 2 3
1 1 2 3 7
1 1 0 3 2
1 1 1
1
0
5
2

Hint

[Sample 1 Explanation]

  • For the first test case, one feasible plan is: apply targeted strengthening to crystal 55 (i.e. S={5}S=\{5\}) and choose x=4x=4. The final crystal magic values are 5,5,6,7,115,5,6,7,11, so the wand’s magic value is 55. It can be proven that no better plan exists.
  • For the second test case, one feasible plan is: apply targeted strengthening to crystal 11 (i.e. S={1}S=\{1\}) and choose x=1x=1.

[Sample 2]

See the attached xor2.in/ans.

This sample satisfies c=4c=4.

[Sample 3]

See the attached xor3.in/ans.

This sample satisfies c=7c=7.

[Sample 4]

See the attached xor4.in/ans.

This sample satisfies c=9c=9.

[Sample 5]

See the attached xor5.in/ans.

This sample satisfies c=11c=11.

[Sample 6]

See the attached xor6.in/ans.

This sample satisfies c=14c=14.

[Sample 7]

See the attached xor7.in/ans.

This sample satisfies c=22c=22.

[Subtasks]

Let n\sum n be the sum of nn over all test cases in a test file. For all testdata:

  • T1T \geq 1;
  • 1n1051 \leq n \leq 10^5, 1n5×1051 \leq \sum n \leq 5\times 10^5;
  • 0m1090 \leq m \leq 10^9;
  • 0k1200 \leq k \leq 120;
  • 1in,0ai<2k\forall 1 \leq i \leq n, 0 \leq a_i<2^k;
  • 1in,0bi109\forall 1 \leq i \leq n, 0 \leq b_i \leq 10^9.
Subtask Index n\sum n \leq nn \leq mm \leq kk \leq Special Property
131\sim 3 1010 10910^9 1010 /
464\sim 6 5×1055\times 10^5 10510^5 00 3030 A
7,87,8 22 10910^9 B
9,109,10 10510^5
111311\sim 13 C
14,1514,15 500500 10210^2 /
161816\sim 18 5×1045\times 10^4 10410^4 6060
192119\sim 21 3×1053\times 10^5 10510^5 120120
222522\sim 25 5×1055\times 10^5
  • Special Property A: m=0m=0; 1in,bi1\forall 1 \leq i\le n, b_i\geq 1.
  • Special Property B: m=1m=1; 1in,bi{1,2}\forall 1 \leq i\le n, b_i \in \{1,2\}, and at most one ii satisfies bi=1b_i=1.
  • Special Property C: m=1m=1; 1in,bi{1,2}\forall 1 \leq i\le n, b_i \in \{1,2\}.

[Notes]

The input file of this problem is large, so please use a faster input method.

In the judging environment, you may use the 128128-bit signed integer type __int128, which can store integers in [2127,21271][-2^{127},2^{127}-1]. Its usage is basically the same as other integer types.

Note that this type cannot be read or written using common I/O methods such as cin/cout or scanf/printf. We provide an implementation of __int128 I/O functions in the contestant directory for you to use.

Translated by ChatGPT 5