#P16635. [GKS 2017 #G] Cards Game

[GKS 2017 #G] Cards Game

Problem Description

Professor Shekhu was a famous scientist working in the field of game theory in the early days of computer science. Right now, he's working on a game which involves a box containing NN distinct cards. The ii-th of these cards has a red number written on one side, and a blue number written on the other side. Both of these numbers are positive integers. The game proceeds as follows:

  • The player starts with a total of 00 points. The objective of the game is to finish with the lowest possible total.
  • As long as there are at least two cards remaining in the box, the player must repeat the following move:
    • Remove two cards of their choice from the box. Choose a red number RR from one card and a blue number BB from the other card.
    • Add the value RBR \oplus B to the total, where \oplus denotes bitwise XOR operation.
    • Return one of the two cards to the box, and remove the other from the game.
  • The game ends when there is only one card remaining in the box (and so it is impossible to make another move).

Professor Shekhu has summoned his best student, Akki, to play this game. Can you help Akki find the minimum possible total, considering all possible ways in which he can play the game?

Input Format

The first line of the input contains an integer TT, the number of test cases. TT test cases follow; each test case consists of three lines: First line of the each test case will contain an integer NN.

  1. The first line contains a positive integer NN: the number of cards in the box.
  2. The second line contains a list of NN positive integers RiR_i; the ii-th of these represents the red number on the ii-th card.
  3. The third line contains a list of NN positive integers BiB_i; the ii-th of these represents the blue number on the ii-th card.

Output Format

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yy is the minimum possible total that Akki can attain, if he plays optimally.

2
2
1 2
3 3
3
1 101 501
3 2 3
Case #1: 1
Case #2: 5

Hint

In Sample Case 11, Akki has only one move in which he picks up the available cards and has two options.

  1. He can choose red number from the first card and blue number from the second card to add 13=21 \oplus 3 = 2 to the total.
  2. He can choose red number from the second card and blue number from the first card to add 23=12 \oplus 3 = 1 to the total.

The second option is better and the answer is 11.

In Sample Case 22, one optimal strategy is to take the red number from first card and the blue number from second card, add 12=31 \oplus 2 = 3 to the total, and return first card to the box. Then, take the red number from first card and the blue number from third card, add 13=21 \oplus 3 = 2 to the total, and return either of the cards to the box. The final total is 55.

Limits

1T1001 \le T \le 100.

1Ri1091 \le R_i \le 10^9.

1Bi1091 \le B_i \le 10^9.

Small dataset (Test set 1 - Visible)

2N52 \le N \le 5.

Large dataset (Test set 2 - Hidden)

2N1002 \le N \le 100.