#P10258. [COCI 2023/2024 #5] Bitovi

[COCI 2023/2024 #5] Bitovi

Background

Translated from COCI 2023/2024 Contest #5 T2 “Bitovi”.

Problem Description

Which came first, the chicken or the egg? Is it better to live as a millionaire for one hundred years, or spend seven days in poverty? How do you become a chess master? How do you pull up blinds? How do you pass final exams? How do you train a dragon? These are all interesting questions we can think about after the contest, but now we will ask a less interesting computer science question.

You are given two sets of numbers AA and BB, both of size NN. In one operation, you may choose any element from set AA and change any one bit in its binary representation. The resulting number must not be an element of set AA before the change.

For example, the binary representation of 55 is 010120101_2. With one operation, it can become 13=1101213=1101_2, 1=000121=0001_2, 7=011127=0111_2, or 4=010024=0100_2.

Find a sequence of operations that transforms set AA into set BB. Two sets are considered equal if they have the same size and there is no element in AA that does not belong to BB.

Note: The number of operations does not have to be minimal, but it must satisfy the limits of the task.

Input Format

The first line contains an integer NN (1N2151 \le N \le 2^{15}), the size of sets AA and BB.

The second line contains NN distinct integers aia_i (0ai<2150 \le a_i < 2^{15}), the elements of set AA.

The third line contains NN distinct integers bib_i (0bi<2150 \le b_i < 2^{15}), the elements of set BB.

Output Format

On the first line, output the number of operations, not exceeding 2192^{19}.

Then output several lines. Each line should contain x,yx, y (0x,y<2150 \le x, y < 2^{15}), meaning that you change xx in set AA into yy. The binary representations of xx and yy must differ in exactly one bit. Also, it must hold that xAx \in A and y∉Ay \not\in A.

3
0 1 2
1 2 3
2
1 3
0 1
3
4 8 31
0 4 8
5
31 30
30 28
28 24
24 16
16 0
5
0 1 2 4 5
7 6 5 3 2
9
1 3
3 7
0 1
1 0
2 6
0 2
7 3
5 7
4 5

Hint

Sample Explanation 1

If we first perform 0 10\ 1 and then 1 31\ 3, then between the two operations, set AA will contain two equal elements, which is not allowed. Another possible solution is 2 32\ 3, 0 20\ 2.

Sample Explanation 2

31=11111231=11111_2. If we modify bits from the least significant bit to the most significant bit, we can obtain 30,28,24,1630, 28, 24, 16, and 00 in order. After all operations, AA and BB are the same.

Subtasks

Subtask Points Constraints
1 10 ai,bi214a_i,b_i \le 2^{14}
2 15 N7N \le 7
3 30 N27N \le 2^7
4 15 No additional constraints.

Translated by ChatGPT 5