Codeforces——C. Anu Has a Function

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

input

4
4 0 11 6

output

11 6 4 0

input

1
13

output

1
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.

题目大意

找到一个排列使f(f(…f(f(a1,a2),a3),…an−1),an)的值最大。

分析

把每个数转化成二进制来看,我们也不用特意去计算每个数有几位,可以从第30位(从左往右)开始,当遇到第一个在这一位上只有一个数是1的数时,即可退出,因为如果这一位上有多个数是1的话,到最后都会被消掉。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
int a[100010];
int main()
{int n,cnt,k,t;cin>>n;for(int i=1;i<=n;i++){scanf("%d",&a[i]);}for(int j=30;j>=0;j--){cnt=0;for(int i=1;i<=n;i++){if((a[i]>>j)&1){cnt++;k=i;}}if(cnt==1){break;}}if(cnt==1){t=a[1];a[1]=a[k];a[k]=t;}for(int i=1;i<=n;i++){if(i!=n){printf("%d ",a[i]);}else{printf("%d\n",a[i]);}}return 0;
}

Codeforces——C. Anu Has a Function相关推荐

  1. codeforces 1299A Anu Has a Function

    传送门 题意:给一个序列,让改变序列的顺序,使其经过一个运算,最后值最大. 分析:本题考就考这个运算,这个运算就是二进制,所以考的就是二进制.分析这个运算:(以下摘自tql tql tql tql) ...

  2. CodeForces 1300C Anu Has a Function

    解题思路 只有在某一位上有唯一的1才会对答案做出贡献,我们把所有的数字转化成二进制,然后遍历每一位上的1,剩下的直接随意输出就ok #include <iostream> #include ...

  3. 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 给定一个长 ...

  4. 【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 ...

  5. 寒假刷题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) (也可以列真值表) 因此题 ...

  6. 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 ...

  7. 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 ...

  8. AC. Anu Has a Function

    A&C. Anu Has a Function Codeforces Round #618 (Div. 1&2) 题目链接 关键词 greedy math *1500 位运算 解题思路 ...

  9. 2月9号cf,c题 Anu Has a Function

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

最新文章

  1. java mobile phone games_j2me100-src Java
  2. DeepMind提出强化学习新方法,可实现人机合作
  3. java 顺序 读写 Properties 配置文件 支持中文 不乱码
  4. CCNP实验+笔记(完整版)
  5. ASP.NET MVC学习系列(一)-WebAPI初探
  6. mysql在没有任何用户的情况下,如何恢复
  7. aix卸载java,AIX系统学习之--卸载软件错误
  8. 2017年第八届蓝桥杯 - 省赛 - C/C++大学A组 - C. 魔方状态
  9. 树莓派3B 开启串口
  10. 示例1---从记事本中读取数值,然后写到数组中---切片,优化版本
  11. excel手机版_一秒用手机找回被误删的Excel文件,学会这招,再不怕文件丢失了...
  12. 攻防世界hello _pwn总结
  13. SAP常用后台表总结
  14. 微信小程序表单必填项设置
  15. apktool java_apktool 是GOOGLE提供的APK编译工具,需要JAVA运行环境,推荐使用JDK1.6或者JDK1.7...
  16. 自2018年2月28日起 iCloud 由云上贵州运营
  17. Win键无效/Win键失灵/Win键被禁用/Win键+组合键不起作用
  18. 我的浏览器 My Brower
  19. 实时操作系统UCOS学习笔记1----UCOSII简介
  20. 用 python 实现 简单AI 双人日麻(文字版)之一 建立胡牌逻辑

热门文章

  1. c语言 整数概念与扩展
  2. 智能社官网顶部导航实现demo
  3. Host name may not be null registration failed Cannot execute request on any known server
  4. 前端密钥怎么存储,才最安全?
  5. SourceTree使用小技巧
  6. 系统定制修改之修改 chrome 浏览器默认主页为指定网址
  7. C# treeView 点击问题
  8. 布隆过滤器原理(有眼睛就能看懂)
  9. 2019 GDUT 新生专题Ⅲ L题
  10. java umeditor使用_在java项目中使用umeditor