XOR Construction

思路:

通过题目可以得出结论

b1^b2=a1

b2^b3=a2

.......

bn-1^bn=an-1

所以就可以得出

(b1^b2)^(b2^b3)=a1^a2

b1^b3=a1^a2

有因为当确定一个数的时候就可以通过异或得到其他所有的数,且题目所求的是一个n-1的全排列

那么求出a的前缀异或和arr之后就得到bi=b1^arri

实际上实在寻找一个 b1 使得异或出来的所有值越小越好,所以拆位,假设所有数字的第 i位为 1 的个数大于为 0 的个数,那我们最好异或上一个 2^i,这样可以使大部分数字变小。

cpp 复制代码
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<stack>
#include<string>
#include<algorithm>
#include<unordered_map>
#include<map>
#include<bitset>
#include<cstring>
#include <unordered_set>
//#include<priority_queue>
#include<queue>
#include<deque>
#include<set>
#include<stdlib.h>
#define dbug cout<<"*****hear*****"<<endl;
#define rep(a,b,c) for(ll a=b;a<=c;a++)
#define per(a,b,c) for(ll a=b;a>=c;a--)
#define no cout<<"NO"<<endl;
#define yes cout<<"YES"<<endl;
#define endl "\n"//交互题一定要关!!!!!!!!!
#define lowbit(x) (x&-x)
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//priority_queue<int,vector<int>,greater<int> >q;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> PII;
typedef pair<long double,long double> PDD;
 ll  INF = 0x3f3f3f3f;
//const ll LINF=LLONG_MAX;
// int get_len(int x1,int y1,int x2,int y2)
// {
//   return (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
// }
const ll N = 2e5+ 10;
 const ll mod1 =998244353;
 const ll mod2 =1e9+7;
// const ll hash_num = 3e9+9;
ll n,m,ca;
ll arr[N],brr[N],crr[N],drr[N];
//ll h[N],ne[N],e[N],w[N],book[N],idx;
//ll idx;

// void add(ll a, ll b , ll c)
// {
//   e[idx] = b, w[idx] = c,ne[idx] = h[a], h[a] =idx ++ ; 
// }


void solve()
{
  cin >> n;
  arr[0]=0;
  rep(i,1,n-1)
  {
    cin >> arr[i];
    arr[i] ^= arr[i-1];
  }
  ll ans=0;
   rep(i,0,20)
   {
      ll sum1=0;
      ll sum2=0;
      rep(j,0,n-1)
      {
        if(arr[j]>>i&1)sum1++;
        else
        {
          sum2++;
        }
      }
      if(sum1>sum2)ans|=1<<i;
   }
   rep(i,0,n-1)arr[i]^=ans;
   rep(i,0,n-1)cout << arr[i]<<' ';
}


int main()
{
   IOS;
   ll _;
    _=1;
    //scanf("%lld",&_);
   //cin>>_;
  
    ca=1;
    while(_--)
    {
      solve(); 
      ca++;
    }    
    return 0;
}
相关推荐
_清歌30 分钟前
DSpark 深度解读:DeepSeek-V4 如何用「半自回归」把推理速度提升 85%
算法
统计实现局32 分钟前
SVD 的三步走:双对角化、Givens 收敛、排序
算法
躬行见万象32 分钟前
《VLA 系列》UniLab 强化训练 | G1 机器人 |复现
算法
统计实现局32 分钟前
对称不定分解(Bunch-Kaufman):为什么 Cholesky 不够用
算法
统计实现局33 分钟前
dqrsl 拆解:拿着 QR 结果能算出哪 5 种东西
算法
卷无止境36 分钟前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
统计实现局42 分钟前
为什么 Cholesky 求逆比 Gauss-Jordan 快一倍——行列式溢出防护详
算法
To_OC12 小时前
LC 994 腐烂的橘子:人人都说是 BFS 入门题,我却写了三遍才过
javascript·算法·leetcode
金銀銅鐵16 小时前
[Python] 扩展欧几里得算法
python·数学·算法
To_OC18 小时前
LC 200 岛屿数量:经典 DFS 入门题,我第一次写居然连方向都搞错了
javascript·算法·leetcode