Anu Has a Function CodeForces - 1300C(二进制位运算)
Anu has created her own function ff: f(x,y)=(x|y)?yf(x,y)=(x|y)?y where || denotes the bitwise OR operation. For example, f(11,6)=(11|6)?6=15?6=9f(11,6)=(11|6)?6=15?6=9. It can be proved that for any nonnegative numbers xx and yy value of f(x,y)f(x,y) is also nonnegative.
She would like to research more about this function and has created multiple problems for herself. But she isn’t able to solve all of them and needs your help. Here is one of these problems.
A value of an array [a1,a2,…,an][a1,a2,…,an] is defined as f(f(…f(f(a1,a2),a3),…an?1),an)f(f(…f(f(a1,a2),a3),…an?1),an) (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?
Input
The first line contains a single integer nn (1≤n≤1051≤n≤105).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109). Elements of the array are not guaranteed to be different.
Output
Output nn integers, the reordering of the array with maximum value. If there are multiple answers, print any.
Examples
Input
4
4 0 11 6
Output
11 6 4 0
Input
1
13
Output
13
Note
In the first testcase, value of the array [11,6,4,0][11,6,4,0] is f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9.
[11,4,0,6][11,4,0,6] is also a valid answer.
經過打表后可以發現,f(x,y)=(x|y)-y=x&(~y),可以寫成這種形式。
那么我們就去求x1&(~x2)&( ~x3)&…&( ~xn)最大的最優排列順序。
既然用到了位運算就要轉化成二進制去求。觀察上面那個式子可以發現,其實最后的結果僅由第一個數字決定的。如果有一位上僅有一個數字是1,而其他的數字在這一位上全都是0,那么將為1的那個數字排列在第一位,那么最后的結果這一位一定為1(剩下的數字在取反后這一位也是1)。我們位數由大到小來找這樣的數字,那么只要找到一個這樣的數字,將這一個數字安排在第一位,剩下的隨便放就可以了。
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的Anu Has a Function CodeForces - 1300C(二进制位运算)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Assigning to Classes
- 下一篇: Aerodynamic CodeForc