zoukankan      html  css  js  c++  java
  • CF165E Compatible Numbers

    CF165E Compatible Numbers

    洛谷传送门

    题目描述

    Two integers xx and yy are compatible, if the result of their bitwise "AND" equals zero, that is, aa & b=0b=0 . For example, numbers 9090 (1011010_{2})(10110102) and 3636 (100100_{2})(1001002) are compatible, as 1011010_{2}10110102 & 100100_{2}=0_{2}1001002=02 , and numbers 33 (11_{2})(112) and 66 (110_{2})(1102) are not compatible, as 11_{2}112 & 110_{2}=10_{2}1102=102 .

    You are given an array of integers a_{1},a_{2},...,a_{n}a1,a2,...,a**n . 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.

    输入格式

    The first line contains an integer nn ( 1<=n<=10^{6}1<=n<=106 ) — the number of elements in the given array. The second line contains nn space-separated integers a_{1},a_{2},...,a_{n}a1,a2,...,a**n ( 1<=a_{i}<=4·10^{6}1<=a**i<=4⋅106 ) — the elements of the given array. The numbers in the array can coincide.

    输出格式

    Print nn integers ans_{i}ans**i . If a_{i}a**i isn't compatible with any other element of the given array a_{1},a_{2},...,a_{n}a1,a2,...,a**n , then ans_{i}ans**i should be equal to -1. Otherwise ans_{i}ans**i is any such number, that a_{i}a**i & ans_{i}=0ans**i=0 , and also ans_{i}ans**i occurs in the array a_{1},a_{2},...,a_{n}a1,a2,...,a**n .

    题意翻译

    有两个整数a,b。如果a&b=0,那么我们称a与b是相容的。比如90(1011010)与36(100100)相容。给出一个序列aa ,你的任务是对于每个ai,找到在序列中与之相容的aj 。如果找不到这样的数,输出-1。


    题解:

    高维前缀和。

    Code:

    #include<bits/stdc++.h>
    using namespace std;
    int n, a[1000005], f[1<<22];
    int main() 
    {
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++) 
    	{
    		scanf("%d", &a[i]);
    		f[a[i]]=a[i];
    	}
    	for(int i=0;i<=21;i++) 
    		for(int j=0;j<(1<<22);j++) 
    			if((j&(1<<i))&&f[j^(1<<i)]) 
    				f[j]=f[j^(1<<i)];
    	for(int i=1;i<=n;i++) 
    	{
    		int b=((1<<22)-1)^a[i];
    		printf("%d ", f[b]?f[b]:-1);
    	}
    	return 0;
    }
    
  • 相关阅读:
    LeetCode Binary Tree Inorder Traversal
    LeetCode Populating Next Right Pointers in Each Node
    LeetCode Construct Binary Tree from Inorder and Postorder Traversal
    LeetCode Reverse Linked List II
    LeetCode Populating Next Right Pointers in Each Node II
    LeetCode Pascal's Triangle
    Palindrome Construct Binary Tree from Preorder and Inorder Traversal
    Pascal's Triangle II
    LeetCode Word Ladder
    LeetCode Binary Tree Zigzag Level Order Traversal
  • 原文地址:https://www.cnblogs.com/fusiwei/p/13746531.html
Copyright © 2011-2022 走看看