加载中...

二进制:给定a b数组 在某种顺序下如果能在找到个c=a^b 而且 对c的&值f的值是最大的

https://codeforces.ml/contest/1721/problem/D
因为最终答案必须是唯一的 然后从最高位开始 当且当a b 各个数子的当前位的1和0是一样的时候 就可以通过分配使得c数组当前位1 级所有当前位上的 0 1数量相同:
~b 等于 a 就满足条件 让ans+=1<<i

#include<bits/stdc++.h>
using namespace std;
#define int long long 
#pragma GCC optimize(3)
const int N = 1e5+10;
int n;
int a[N],b[N],c[N],d[N];
bool check(int x){
    for(int i=1;i<=n;i++)c[i]=a[i]&x;
    for(int i=1;i<=n;i++)d[i]=~b[i]&x;
    sort(c+1,c+1+n);
    sort(d+1,d+1+n);
    for(int i=1;i<=n;i++)if(c[i]!=d[i])return false;
    return true;
}
void cf(){
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)cin>>b[i];
    int ans=0;
    for(int i=30;i>=0;i--){
        if(check(ans|1<<i))ans|=1<<i;
    }
    cout<<ans<<endl;
}
signed main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int _=1;
    cin>>_;
    while(_--){
        cf();
    }
    return 0;
}
posted @ 2022-08-29 23:00  liang302  阅读(32)  评论(0编辑  收藏  举报