E. Compatible Numbers
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Two integers x and y are compatible, if the result of their bitwise "AND" equals zero, that is, a & b = 0. For example, numbers 90(10110102) and 36 (1001002) are compatible, as 10110102 & 1001002 = 02, and numbers 3 (112) and 6 (1102) are not compatible, as 112 & 1102 = 102.

You are given an array of integers a1, a2, ..., an. Your task is to find the following for each array element: is this element compatible with some other element from the given array? If the answer to this question is positive, then you also should find any suitable element.

Input

The first line contains an integer n (1 ≤ n ≤ 106) — the number of elements in the given array. The second line contains n space-separated integers a1, a2, ..., an (1 ≤ ai ≤ 4·106) — the elements of the given array. The numbers in the array can coincide.

Output

Print n integers ansi. If ai isn't compatible with any other element of the given array a1, a2, ..., an, then ansi should be equal to -1. Otherwise ansi is any such number, that ai & ansi = 0, and also ansi occurs in the array a1, a2, ..., an.

Examples
input
  1. 2
  2. 90 36
output
  1. 36 90
input
  1. 4
  2. 3 6 3 6
output
  1. -1 -1 -1 -1
input
  1. 5
  2. 10 6 9 8 2
output
  1. -1 8 2 2 8

题目链接:Codeforces 165E

看到题目可以发现这题显然是要打表的,而且可以发现除了对应的1位必定要为0,其他位却是任意的,对每一个符合的数都去枚举任意一位可任意取值的位到底是0还是1,复杂度会变成指数级别,那么可以间接地进行递推,比如00 0110的一开始对应数字为11 1001,其他与00 0110对应数字符合xx x00x的形式,其中x表示可以任意取0或1,反正只要后面那两个1的位置为0即可,那么我们可以一位一位地慢慢枚举让哪一位变成,从xxxx x00x递推到0xxx x00x、x0xx xx0x、xx0x x00x、xxx0 x00x、xxxx 0x00x、……,一开始可能会想着这样会不会漏了其它不止变动一位的情况吧,但是实际上是不会的,这样往后推1位之后,后面的位只要让循环从大到小遍历,就会依次被枚举,即从0xxx x00x可以推到00xx x00x以至确定3位、4位的情况,当然我们是枚举把哪一位变成0,遇到已经是0的位就不用处理了,最后说一下为什么要从大到小,因为是枚举某一位$i$将其从1变成0,数字必定会减小$2^i$,因此是从大的数推到小的数,这样复杂度就只有几千万的循环了。

代码:

  1. #include <stdio.h>
  2. #include <algorithm>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <bitset>
  6. #include <string>
  7. #include <stack>
  8. #include <cmath>
  9. #include <queue>
  10. #include <set>
  11. #include <map>
  12. using namespace std;
  13. #define INF 0x3f3f3f3f
  14. #define LC(x) (x<<1)
  15. #define RC(x) ((x<<1)+1)
  16. #define MID(x,y) ((x+y)>>1)
  17. #define fin(name) freopen(name,"r",stdin)
  18. #define fout(name) freopen(name,"w",stdout)
  19. #define CLR(arr,val) memset(arr,val,sizeof(arr))
  20. #define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
  21. typedef pair<int, int> pii;
  22. typedef long long LL;
  23. const double PI = acos(-1.0);
  24. const int N = 1e6 + 7;
  25. const int M = 22;
  26. int arr[N];
  27. int rev[1 << M];
  28.  
  29. int main(void)
  30. {
  31. int n, i, j;
  32. while (~scanf("%d", &n))
  33. {
  34. int one = (1 << 22) - 1;
  35. CLR(rev, 0);
  36. for (i = 0; i < n; ++i)
  37. {
  38. scanf("%d", arr + i);
  39. int op = arr[i] ^ one;
  40. rev[op] = arr[i];
  41. }
  42. for (i = one; i >= 0; --i)
  43. {
  44. if (rev[i])
  45. {
  46. for (j = 0; j < M; ++j)
  47. {
  48. if (((i >> j) & 1) == 1)//i的对应位为1才需要变成0
  49. {
  50. int v = i ^ (1 << j);//将i的第j位从1变成0
  51. rev[v] = rev[i];
  52. }
  53. }
  54. }
  55. }
  56. for (i = 0; i < n; ++i)
  57. printf("%d%c", rev[arr[i]] ? rev[arr[i]] : -1, " \n"[i == n - 1]);
  58. }
  59. return 0;
  60. }

Codeforces 165E Compatible Numbers(二进制+逆序枚举)的更多相关文章

  1. CodeForces 165E Compatible Numbers(位运算 + 好题)

    wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...

  2. C语言整数按照二进制逆序,输出逆序后的整数值

    问题来源,今天早上和一舍友吃早餐的时候谈到的一个问题,将一个整数按照二进制逆序,然后输出逆序后的数值. 我们知道数值在内存中都是以二进制的形式存放的,假如我们是32位机,每8位为一个字节,int型在3 ...

  3. CFdiv2 165E. Compatible Numbers 子集枚举

    传送门 题意: 给出一个序列,输出每个数x对应的一个ans,要求ans在数列中,并且ans & x  = 0:数列的每个数小于(4e6) 思路: 这道题的方向比较难想.想到了就比较轻松了,可以 ...

  4. 2018ICPC徐州区域赛网络赛B(逆序枚举或者正序深度搜索)

    #include<bits/stdc++.h>using namespace std;int n,m,k,l;int x[1007],y[1007],z[1007];int dp[1007 ...

  5. 二进制打印与逆序_C语言(转)

    //二进制逆序 by MoreWindows( http://blog.csdn.net/MoreWindows ) #include <stdio.h> //二进制打印函数 templa ...

  6. hdu.1043.Eight (打表 || 双广 + 奇偶逆序)

    Eight Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  7. C++ 排序函数 sort(),qsort()的含义与用法 ,字符串string 的逆序排序等

    上学时我们很多学了很多种排序算法,不过在c++stl中也封装了sort等函数,头文件是#include <algorithm> 函数名 功能描述 sort 对给定区间所有元素进行排序 st ...

  8. POJ 2541 Binary Witch(逆序KMP,好题)

    逆序KMP,真的是强大! 参考链接,下面有题意解释:http://blog.sina.com.cn/s/blog_6ec5c2d00100tphp.htmlhttp://blog.csdn.net/s ...

  9. 笔记-NSArray 逆序reverseObjectEnumerator 及 NSEnumerator 遍历

    //1.原始数组 NSMutableArray *array = [NSMutableArray arrayWithObjects:@"1",@"2",@&qu ...

随机推荐

  1. Webpack4 学习笔记五 图片解析、输出的文件划分目录

    前言 此内容是个人学习笔记,以便日后翻阅.非教程,如有错误还请指出 webpack打包图片和划分文件路径 使用图片的方式 通过 new Image() 在 css中设置 background-imag ...

  2. 漂亮提醒框js

    <script type="text/javascript"> var filename = "PICC_V2.1.3.0_新增功能操作手册.doc" ...

  3. Delphi的Edit控件中只能输入数字且只能输入一个小数点

    使用这种功能必须使用 OnKeyPress 事件,该事件是在窗体中获得键盘输入的焦点,并且在用户按键时发生.OnKeyPress 事件中有个重要参数:Key.Key 参数为Char 型,它能够获得用户 ...

  4. http2.2配置

    http: 超文本传输协议,工作在应用层 CentOS 6程序环境:httpd-2.2 配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.con ...

  5. Servlet学习笔记07——什么是cookie,session?

    7)cookie的路径问题 a.什么是cookie路径问题? 浏览器在向服务器上的某个地址发送请求时, 会查看cookie的路径是否与该地址匹配,只有 匹配的cookie才会被发送. b.cookie ...

  6. tcl之文件操作-文件名相关

  7. tcl之控制流-switch

  8. php图片压缩-高清晰度

    php高清晰度无损压缩 经常会用到把上传的大图片压缩,特别是体积,在微信等APP应用上,也默认都是有压缩的,那么,怎么样对图片大幅度压缩却仍能保持较高的清晰度呢? 压缩通常是有按比例缩放,和指定宽度压 ...

  9. 虚拟机桥接模式下多台Ubuntu16.04系统互相连接

    1.首先新建一个虚拟机并在该虚拟机上安装Ubuntu16.04系统.为这台虚拟机起名为Ubuntu3. 2.对Ubuntu3进行克隆,为新克隆生成的虚拟机起名为Ubuntu2.(这时我们会发现Ubun ...

  10. linux基础命令3(man)

    Type:显示指定的命令是那种类型.                                            Linux下有两种模式的时间 date:用于系统时间管理.(软件操作的系统时 ...