Anu has created her own function f: f(x,y)=(x|y)−y where | denotes the bitwise OR operation. For example, f(11,6)=(11|6)−6=15−6=9. It can be proved that for any nonnegative numbers x and y value of 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(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≤105).The second line contains n integers a1,a2,…,an (0≤ai≤109). Elements of the array are not guaranteed to be different.Output
Output n integers, the reordering of the array with maximum value. If there are multiple answers, print any.

Examples
inputCopy

4
4 0 11 6

outputCopy

11 6 4 0

inputCopy

1
13

outputCopy

13

Note

In the first testcase, value of the array [11,6,4,0] is f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9.
[11,4,0,6] is also a valid answer.

我们把a|b-b换成 a-a&b,也就是说使得a&b最小,那我们考虑什么时候最大,即所有数的最高位,只有一个为1不然一定在运算的过程中被削去,那么这个题就可以改成,在32位中找某一位只有一个数是1的那一个数,让他当第一。因为其余都会抵消。如果不存在,那么怎样输出都是0

#include <bits/stdc++.h>
using namespace std;
const int N = 100003;
int n, a[N];
int main()
{cin >> n;for (int i = 1; i <= n; ++i){cin >> a[i];}for (int j = 30; ~j; --j){int cnt = 0, p;for (int i = 1; i <= n; ++i)if ((a[i] >> j) & 1)++cnt, p = i;if (cnt == 1){printf("%d ", a[p]);for (int k = 1; k <= n; ++k)if (k != p)printf("%d ", a[k]);return 0;}}cout<<endl<<endl;for (int i = 1; i <= n; ++i)printf("%d ", a[i]);
}

Codeforces Round #618 (Div. 2)-C. Anu Has a Function相关推荐

  1. Codeforces Round #618 (Div. 1)-----A. Anu Has a Function

    提示: Codeforces Round #618 (Div. 1)-----A. Anu Has a Function 题意: 定义一个函数f(x , y) = f(x | y) - y 给定一个长 ...

  2. 【Codeforces Round#618 (Div. 2)】C. Anu Has a Function 题解

    题目链接:C. Anu Has a Function 题目 Anu has created her own function f: f(x,y)=(x|y)−y where | denotes the ...

  3. 寒假刷题13: Anu Has a Function Codeforces Round #618 (Div. 2) C

    题目链接: Anu Has a Function 题目解析: 观察函数f(x,y)定义:(x|y)-y 即 取出来x里是1但是y里不是1的地方 也就是 x&(~y) (也可以列真值表) 因此题 ...

  4. Codeforces Round #618 (Div. 2)C、Anu Has a Function

    C. Anu Has a Function time limit per test1 second memory limit per test256 megabytes inputstandard i ...

  5. Codeforces #618 div.2 C. Anu Has a Function

    C. Anu Has a Function time limit per test1 second memory limit per test256 megabytes inputstandard i ...

  6. 贪心 ---- Codeforces Round #618 (Div. 2)B. Assigning to Classes+贪心[证明过程]

    题目链接 题目大意:给你2∗n2*n2∗n个数,将这些数分成2个集合使得两个集合中位数的差值最小 解题思路:我懵了一个结论就是排序之后取中间的两个数然后就ac了 我们先对这些数字从小到大排序 证明:1 ...

  7. Codeforces Round #618 (Div. 2)-B. Assigning to Classes

    Reminder: the median of the array [a1,a2,-,a2k+1] of odd number of elements is defined as follows: l ...

  8. Codeforces Round #618 (Div. 2)-Non-zero

    Guy-Manuel and Thomas have an array a of n integers [a1,a2,-,an]. In one step they can add 1 to any ...

  9. Codeforces Round #506 (Div. 3)

    Codeforces Round #506 (Div. 3) 实习期间事不多,对div3 面向题解和数据编程了一波 A. Many Equal Substrings 题目链接 A题就是找后缀和前缀重合 ...

最新文章

  1. 鸿蒙开发-使用fetch发起网络请求
  2. PhpStudy 升级 MySQL 版本到5.7
  3. C或C 如何通过程序执行shell命令并获取命令执行结果?
  4. pat 乙级 1027 打印沙漏(C++)
  5. Spyder里面好像不能用汉字
  6. asp.net 子域跨域 带cookie
  7. C# 笔记3 - 重载一系列像python那样的print()方法
  8. mysql多线程访问总结
  9. 子界类型(Subrange types)
  10. Hadoop大数据开发整体思路
  11. 基于SWMM及自主开发城市内涝一维二维耦合软件的复杂城市排水系统建模技术及在城市排涝、海绵城市等领域实践应用
  12. 南宁:“数字城管”让智慧城市建设提质提速
  13. R语言风玫瑰图绘制(附代码)
  14. 机器人学——机器人导航
  15. 通过图像预处理解决深度学习模型对光照色彩敏感的问题处理
  16. 上海是怎么错失这些年的互联网机遇的?——写的很好,转
  17. 积分商城系统业务逻辑思维导图_怎么开发积分商城系统_OctShop
  18. 爬虫前行中(datawhale task2)
  19. Mock 的使用方法
  20. 计算机一级二级科目,计算机一级和二级

热门文章

  1. VC++编程实现多显示器控制(复制、横屏、纵屏,显示器个数)
  2. Android开发之AlertDialog设置左右边距的间接办法
  3. 推流地址 java_Java实现腾讯云直播生成推流地址和播放地址
  4. Android 封装一个通用的PopupWindow
  5. 使用分发列表过滤BGP路由
  6. ZooKeeper学习第七期--ZooKeeper一致性原理
  7. 离散型随机变量及其分布列习题
  8. 巴斯光年探险阿里聚安全攻防挑战赛!决战12月28日!
  9. 批量put和单条put
  10. NSMutableArray的一些知识