Description:
Anu has created her own function f : f ( x , y ) = ( x ∣ y ) − y f: f(x,y)=(x|y)−y f:f(x,y)=(x∣y)−y where | denotes the bitwise OR operation. For example, f ( 11 , 6 ) = ( 11 ∣ 6 ) − 6 = 15 − 6 = 9 f(11,6)=(11|6)−6=15−6=9 f(11,6)=(11∣6)−6=15−6=9. It can be proved that for any nonnegative numbers x x x and y y y value of f ( x , y ) 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] is defined as f ( f ( … f ( f ( a 1 , a 2 ) , a 3 ) , … a n − 1 ) , a n ) f(f(…f(f(a_{1},a_{2}),a_{3}),…a_{n−1}),a_{n}) 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 n ( 1 ≤ n ≤ 1 0 5 ) . n (1≤n≤10^5). n(1≤n≤105).
The second line contains n n n integers a 1 , a 2 , … , a n ( 0 ≤ a i ≤ 1 0 9 ) a_{1},a_{2},…,a_{n} (0≤a_{i}≤10^9) a1,a2,…,an(0≤ai≤109). Elements of the array are not guaranteed to be different.
Output
Output n n n 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] [11,6,4,0] is f ( f ( f ( 11 , 6 ) , 4 ) , 0 ) = f ( f ( 9 , 4 ) , 0 ) = f ( 9 , 0 ) = 9. f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9. f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9.
[ 11 , 4 , 0 , 6 ] [11,4,0,6] [11,4,0,6] is also a valid answer.
题意:
给出一个运算,求数组怎么排列可以使得最后结果最大。
𝑓(𝑥,𝑦)=(𝑥|𝑦)−𝑦
以二进制看,结果就是
x
x
x 的某位上有的而
y
y
y 没有的结果
举个例子:
x
=
01011
x = 01011
x=01011
y
=
01100
y = 01100
y=01100
x
∣
y
=
01111
x|y = 01111
x∣y=01111
x
∣
y
−
y
=
00011
x|y-y = 00011
x∣y−y=00011
所以
f
(
f
(
…
f
(
f
(
a
1
,
a
2
)
,
a
3
)
,
…
a
n
−
1
)
,
a
n
)
f(f(…f(f(a_{1},a_{2}),a_{3}),…a_{n−1}),a_{n})
f(f(…f(f(a1,a2),a3),…an−1),an)就是只在
a
1
a_{1}
a1 的二进制位有的而
a
2
,
a
3
,
…
,
a
n
−
1
,
a
n
a_{2},a_{3},…,a_{n-1},a_{n}
a2,a3,…,an−1,an 在那个二进制位没有。
所以我们只要找到二进制下仅在这一位下位1的并且尽量在高位的数作为第一个就行,后面的随便排。
AC代码:
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <stack>
#include <queue>
using namespace std;
#define sd(n) scanf("%d", &n)
#define sdd(n, m) scanf("%d%d", &n, &m)
#define sddd(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n, m) printf("%d %d\n", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n, m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld", &n)
#define sldd(n, m) scanf("%lld%lld", &n, &m)
#define slddd(n, m, k) scanf("%lld%lld%lld", &n, &m, &k)
#define sf(n) scanf("%lf", &n)
#define sc(n) scanf("%c", &n)
#define sff(n, m) scanf("%lf%lf", &n, &m)
#define sfff(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define ss(str) scanf("%s", str)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
#define mem(a, n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define mod(x) ((x) % MOD)
#define gcd(a, b) __gcd(a, b)
#define lowbit(x) (x & -x)
typedef pair<int, int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while (ch < '0' || ch > '9')
{
if (ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret * 10 + ch - '0';
ch = getchar();
}
return ret * sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
if (a > 9)
Out(a / 10);
putchar(a % 10 + '0');
}
ll gcd(ll a, ll b)
{
return b == 0 ? a : gcd(b, a % b);
}
ll lcm(ll a, ll b)
{
return a * b / gcd(a, b);
}
///快速幂m^k%mod
ll qpow(ll a, ll b, ll mod)
{
if (a >= mod)
a = a % mod + mod;
ll ans = 1;
while (b)
{
if (b & 1)
{
ans = ans * a;
if (ans >= mod)
ans = ans % mod + mod;
}
a *= a;
if (a >= mod)
a = a % mod + mod;
b >>= 1;
}
return ans;
}
// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
return qpow(a, p - 2, p);
}
///扩展欧几里得
int exgcd(int a, int b, int &x, int &y)
{
if (b == 0)
{
x = 1;
y = 0;
return a;
}
int g = exgcd(b, a % b, x, y);
int t = x;
x = y;
y = t - a / b * y;
return g;
}
///使用ecgcd求a的逆元x
int mod_reverse(int a, int p)
{
int d, x, y;
d = exgcd(a, p, x, y);
if (d == 1)
return (x % p + p) % p;
else
return -1;
}
///中国剩余定理模板0
ll china(int a[], int b[], int n) //a[]为除数,b[]为余数
{
int M = 1, y, x = 0;
for (int i = 0; i < n; ++i) //算出它们累乘的结果
M *= a[i];
for (int i = 0; i < n; ++i)
{
int w = M / a[i];
int tx = 0;
int t = exgcd(w, a[i], tx, y); //计算逆元
x = (x + w * (b[i] / t) * x) % M;
}
return (x + M) % M;
}
const int N = 2e5 + 10;
int n, m;
ll a[N];
int cnt[100];
ll ans[N];
int main()
{
mem(cnt, 0);
sd(n);
for (int i = 1; i <= n; i++)
{
sld(a[i]);
rep(j, 0, 32)
{
if ((a[i] >> j) & 1)
{
cnt[j]++;
}
}
}
int mx = 1;
rep(i, 1, n)
{
rep(j, 0, 32)
{
if ((a[i] >> j) & 1)
{
if (cnt[j] == 1)
{
ans[i] += 1ll << j;
}
}
}
if (ans[i] > ans[mx])
mx = i;
}
swap(a[mx], a[1]);
rep(i, 1, n)
printf("%lld ", a[i]);
return 0;
}