XOR Clique
Time Limit: 1 Second Memory Limit: 65536 KB
BaoBao has a sequence . He would like to find a subset of such that , and is maximum, where means bitwise exclusive or.
Input
There are multiple test cases. The first line of input contains an integer , indicating the number of test cases. For each test case:
The first line contains an integer (), indicating the length of the sequence.
The second line contains n integers: (), indicating the sequence.
It is guaranteed that the sum of in all cases does not exceed .
Output
For each test case, output an integer denoting the maximum size of .
Sample Input
3
3
1 2 3
3
1 1 1
5
1 2323 534 534 5
Sample Output
2
3
2
题目大意:
在给定数字串中找子串的两个数异或后小于这两个数中的最小值的个数,不是求子串的个数,是求各个符合条件的子串中值的个数。
例:1,2, 3
'^'表示异或的意思。
1^2=(01)^(10)=(11)=3>min(1,2)则这两个不成立
2^3=(10)^(11)=(01)=1<min(2,3)则这个成立
1^3=(01)^(11)=(10)=2>min(1,3)则这个不成立
由此只有1,2这个子串,则有两个数。
思路:
根据样例可以观察到,如果两个数的二进制位数不相同,到最后异或之后一定大于两数中的最小值。
则只有当两个数的二进制位数相同时,异或后的数才会小于最小值,因为最高的那一位一定异或成了0.
#include<iostream>
#include<cstdio>
#include<algorithm>
#define p 100001
using namespace std;
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
int sum[30]={0};//10的9次方的二进制大约30位
scanf("%d",&n);
int a[p],b[p];
for(int i=0;i<n;i++){
int count=0;
scanf("%d",&a[i]);
b[i]=a[i];
while(b[i]>1){
b[i]=b[i]/2;
count++;
}
sum[count]++;
}
int maxx=*max_element(sum,sum+30);
if(maxx==1)
printf("%d\n",0);
else
printf("%d\n",maxx);
}
}