#P7747. [2019年杭电多校]Rikka with Stable Marriage
[2019年杭电多校]Rikka with Stable Marriage
Rikka with Stable Marriage
Problem Description
People in love always feel humble. Sometimes, Rikka is worried about whether she deserves love from Yuta. Stable marriage problem is an interesting theoretical model which has a strong connection with the real world. Given men and women, where each person has ranked all members of the opposite sex in order of preference. We use a permutation of length to represent a match that the th man gets married with the th woman. A match is stable if and only if there are no two people of opposite sexes who would both rather have each other than their current partners, i.e., $\forall i \neq j, \neg (r_i(p_j,p_i) \wedge r_{p_j}(i,j))$ where represents whether person loves more than . Rikka wants to resolve the confusion in her mind by considering a special case of the stable marriage problem. Rikka uses a feature integer to represents a person's character, and for two persons with feature integers equal to and , Rikka defines the suitable index of them as , where represents binary exclusive-or. Given men with feature integers and women with feature integers . For the th man, he loves the th woman more than the th woman if and only if ; for the th woman, she loves the th man more than the th man if and only if . Rikka wants to calculate the best stable match for this problem, i.e., let be the set of all stable match, she wants to calculate $\max_{p \in \mathbb P} \left(\sum_{i=1}^n \left(a_i \oplus b_{p_i} \right) \right)$. Since is quite large, this problem is too difficult for Rikka, could you please help her find the answer?
Input
The first line of the input contains a single integer , the number of test cases. For each test case, the fisrt line contains a sigle integer . The second line contains integers which represents the feature number of each man. The third line contains integers which represents the feature number of each woman. The input guarantees that there are no more than test cases with , and for any , and .
Output
For each test case, output a single line with a single integer, the value of the best stable match. If there is no stable match, output . Hint In the first test case, one of the best matches is . Therefore the answer is $(1 \oplus 2) + (2 \oplus 1) + (3 \oplus 4) + (4 \oplus 3) = 20$.
Sample Input
2
4
1 2 3 4
1 2 3 4
5
10 20 30 40 50
15 25 35 45 55
Sample Output
20
289
Source
2019 Multi-University Training Contest 9