F. Make Them Similar

Topic links:

The meaning of problems: a given number n of the array a, and asked whether to find a satisfying x b [i] = a [i] ^ x. The binary number b 1 of the array which are equal for all elements

a[i]<(2^30)-1  

Ideas:

From the data range, within a certain range of x 2 ^ 30, so it is easy to think of meet in the middle! ! ! For a 30-bit binary search complexity is 2 ^ 15 AC casually can it (literally WA 4 hair, wrote a huge long emmm)

First, we pretreated 15 prior to a state, and then to do with a whole array of exclusive OR element, we can then for modulo 15 is removed to prevent the impact.

Then we can get the number of the current status of each exclusive-or before 15 new elements obtained 1, then we look at the statistics after a cut difference between the current position (Why I'll say)

So mark this vector array, the open map on the line, but also the way to go into the current state

 

So after 15 Enumeration is the same token we can get an array of 15 after, for this we use the array after the current position minus one.

Then for two complementary arrays, there is a relationship between them, with a reduction of the current after a bit, a bit down with the current one, the resulting array will be the same.

In fact, it proves difficult to think: If they are complementary, their sequence must be reversed. So there is this condition! ! !

So if you have to meet this requirement we can get the answer

For details, see the code!

#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define re register
#define pb push_back
#define fi first
#define se second
const int N=2e6+10;
void read(int &a)
{
    a=0;int d=1;char ch;
    while(ch=getchar(),ch>'9'||ch<'0')
        if(ch=='-')
            d=-1;
    a=ch^48;
    while(ch=getchar(),ch>='0'&&ch<='9')
        a=(a<<3)+(a<<1)+(ch^48);
    a*=d;
}
int a[N],n;
vector <int> v;
map <vector <int>,int> vis;
years = -int1;
void dfs1(int i,int now)
{
    if(i==15)
    {
        v.clear();
        for(re int j=1;j<=n;j++) v.pb(__builtin_popcount((now^a[j])%(1<<15)));
        for(re int j=1;j<n;j++) v[j-1]=v[j]-v[j-1];
        v.pop_back();
        if(!vis.count(v)) vis[v]=now;
        return;
    }
    dfs1(i+1,now);
    dfs1(i+1,now|(1<<i));
}
void dfs2(int i,int now)
{
    if(i==15)
    {
        v.clear();
        for(re int j=1;j<=n;j++) v.pb(__builtin_popcount(now^(a[j]>>15)));
        for(re int j=1;j<n;j++) v[j-1]-=v[j];
        v.pop_back();
        if(vis.count(v)) ans=vis[v]|(now<<15);
        return;
    }
    dfs2(i+1,now);
    dfs2(i+1,now|(1<<i));
}
int main()
{
    read(n);
    for(re int i=1;i<=n;i++) read(a[i]);
    dfs1(0,0);dfs2(0,0);
    printf("%d",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/acm1ruoji/p/11870284.html