#PS0000302. Các số tương thích (Compatible Numbers)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Các số tương thích (Compatible Numbers)
Compatible Numbers
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Given an array of positive integers, call two integers and compatible if their bitwise AND is zero, i.e. . They have no bit position at which both bits are .
For each position , find an entry at another position in the array that is compatible with . If no such entry exists, the result for position is . If there are multiple suitable entries, any one is accepted.
Report the value of the chosen entry, not its index.
Input
The first line contains an integer , the length of the array.
The second line contains positive integers ; equal values are allowed.
Output
Print integers in input order on one line. Each is a valid compatible value from the sequence or if none exists.
Subtasks
- Subtask 1 (20 points): ; (for every ).
- Subtask 2 (30 points): ; (for every ).
- Subtask 3 (50 points): ; (for every ).
Example 1
Input
2
90 36
Output
36 90
Explanation
and share no set bit, so . The first entry selects and the second selects ; print 36 90.
Example 2
Input
4
3 6 3 6
Output
-1 -1 -1 -1
Explanation
The only values are and . Since , , and , all possible pairs have nonzero AND. No entry has a compatible value; all four results are -1.
Example 3
Input
5
10 6 9 8 2
Output
-1 8 2 2 8
Explanation
For , its AND with is respectively . None is zero, so its result is -1.
The other four choices satisfy , , , and . Every chosen value occurs in the array. Hence -1 8 2 2 8 is valid, but it is not the only valid answer.