Today, as a friendship gift, Bakry gave Badawy n integers a1,a2,…,an and challenged him to choose an integer X such that the value max1≤i≤n(ai⊕X) is minimum possible, where ⊕ denotes the bitwise XOR operation.
As always, Badawy is too lazy, so you decided to help him and find the minimum possible value of max1≤i≤n(ai⊕X).
Input
The first line contains integer n (1≤n≤105).
The second line contains n integers a1,a2,…,an (0≤ai≤230−1).
Output
Print one integer — the minimum possible value of max1≤i≤n(ai⊕X).
Examples
input
3
1 2 3
output
2
input
2
1 5
output
4
Note
In the first sample, we can choose X=3.
In the second sample, we can choose X=5.
题意
取一个数x,与序列中每个数异或后最大值,使这个数最小,输出
思路
如果某一位都是一样的肯定这一位是0,其他情况都是1,用dfs把序列分成两部分,分别是这一位是0,和是1的,然后再跑,跑完将最小值传递上去。
代码
#include <bits/stdc++.h>
typedef long long ll;
const ll mod = 9999999967;
using namespace std;
namespace fastIO {
inline void input(ll& res) {
char c = getchar();res = 0;int f = 1;
while (!isdigit(c)) { f ^= c == '-'; c = getchar(); }
while (isdigit(c)) { res = (res << 3) + (res << 1) + (c ^ 48);c = getchar(); }
res = f ? res : -res;
}
inline ll qpow(ll a, ll b) {
ll ans = 1, base = a;
while (b) {
if (b & 1) ans = (ans * base % mod +mod )%mod;
base = (base * base % mod + mod)%mod;
b >>= 1;
}
return ans;
}
}
using namespace fastIO;
const int N = 1e6 + 5;
ll Case,n,x;
ll solve(vector<ll> &zr,int n){
vector<ll> l,r;
if(n<0) return 0;
for(auto i:zr){
if(i&(1<<n)) l.push_back(i);
else r.push_back(i);
}
if(l.size()==0&&r.size()==0) return 0;
if(l.size()==0) return solve(r,n-1);
if(r.size()==0) return solve(l,n-1);
return min(solve(l,n-1),solve(r,n-1))+(1<<n);
}
int main(){
Case=1;
vector<ll> zr;
//input(Case);
while(Case--){
input(n);
for(int i=1;i<=n;i++){
input(x);
zr.push_back(x);
}
printf("%lld",solve(zr,30));
}
return 0;
}
/*
1 0001
5 0101
4 0100
*/