http://www.codechef.com/JULY12/problems/GRAYSC/ |
All submissions for this problem are available.
The Gray code (see wikipedia for more details) is a well-known concept.One of its important properties is that every two adjacent numbers have exactly one different digit in their binary representation.
In this problem, we will give you n non-negative integers in a sequenceA[1..n] (0<=A[i]<2^64), such that every two adjacent integers have exactly one different digit in their binary representation, similar to the Gray code.
Your task is to check whether there exist 4 numbers A[i1], A[i2], A[i3], A[i4] (1 <= i1 < i2 < i3 < i4 <= n) out of the givenn numbers such that A[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Herexor is a bitwise operation which is same as ^ in C, C++, Java and xor in Pascal.
Input
First line contains one integer n (4<=n<=100000).Second line containsn space seperated non-negative integers denoting the sequence A.
Output
Output “Yes” (quotes exclusive) if there exist four distinct indices i1, i2, i3, i4 such thatA[i1] xor A[i2] xor A[i3] xor A[i4] = 0. Otherwise, output "No" (quotes exclusive) please.
Example
Input: 5 1 0 2 3 7 Output: Yes
题意: 输入n个数 每2个相邻的数对应的二进制 只有其中的一位是不同的 问是否存在4个数 使得是个数抑或等于0
思路: 相邻的两个抑或肯定只剩下1位为1 相同为0 不同为1 所以如果数字够多的话得到的结果为2^0 2^1 2^3 .......2^64 如果数量大于2*64的话 一定会有重复的
那么2个重复的抑或等于0 所以当数量大于2*64 直接输出yes 否则暴力求解 即可
下面是标程
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
const int limit = 64*2+10;
int main()
{
int n;
unsigned long long a[limit];
cin >> n;
if (n>=limit){
puts("Yes");
return 0;
}
for (int i=0;i<n;++i)
cin >> a[i];
for (int i=0;i<n;++i)
for (int j=i+1;j<n;++j)
for (int k=j+1;k<n;++k)
for (int l=k+1;l<n;++l)
if ((a[i]^a[j]^a[k]^a[l])==0){
puts("Yes");
return 0;
}
puts("No");
return 0;
}