[2020.10.28 SSL Popularization Simulation Tournament T4] Maximum XOR and [Binary XOR]

Insert picture description here
Insert picture description here

analysis

First split the binary and count 1 number
because there can be no two 0s or two 1s. Count the number of 1s in the binary of each number. If there is only 1 digit 1, then directly count 2 i 2^i2i
or the rest can be 1, you can choose to add up to2 j 2^j2j can

Upload code

//先 拆分二进制 统计1个数
//然后 10^9大概是30位 for(i=30~0) 如果1的个数=1 就累加2的i次方 >1就
//for(j=i~0) 累加2^j 
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[105],cnt[105],ans;
void count_one(int x)
{
    
    
	int j=0;
	while(x)
	{
    
    
		if(x%2==1) cnt[j]++;
		x/=2;
		j++;
	}
}
int ksm(int a,int n)
{
    
    
    long long ans=1;
    while(n)
	{
    
    
        if(n&1)
        {
    
    
        	ans=(ans*a);
		}
        a=a*a;
        n>>=1;
    }
    return ans;
}
int main()
{
    
    
	cin>>n;
	for(int i=1;i<=n;i++)
	{
    
    
		cin>>a[i];
		count_one(a[i]);
	}
	for(register int i=30;i>=0;i--)
	{
    
    
		if(cnt[i]==1) ans+=ksm(2,i);
		if(cnt[i]>=2)
		{
    
    
			for(register int j=i;j>=0;j--)
			{
    
    
				ans+=ksm(2,j);
			}	
			break;
		}	 
	}
	cout<<ans;
	return 0;
}

Guess you like

Origin blog.csdn.net/dglyr/article/details/109350906
XOR