当前位置: 首页 > news >正文

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,这样可以使大部分数字变小。

#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;
}

 

http://www.lryc.cn/news/224715.html

相关文章:

  • K8S容器持续Terminating无法正常关闭(sider-car容器异常,微服务容器正常)
  • Spring 循环依赖
  • MySQL 8.0.13升级到8.0.35记录 .NET
  • flink udtaf 常年不能用
  • 路由汇总的四要点
  • HashMap存值、取值及哈希碰撞原理分析
  • 【SVN】
  • 编程语言,脚本语言
  • 探索双十一:从技术角度剖析电商狂欢节
  • Ubuntu LTS 坚持 10 年更新不动摇
  • Python将多个相同格式的变量存储到列表中
  • 前端字符串转数组对象实现方式-开发bug总结6
  • 99 颜色分类
  • 计算机视觉与深度学习 | 基于GPS/BDS多星座加权图因子优化的行人智能手机导航
  • 低代码平台,业务开发的“银弹”
  • 补偿 FIR 滤波器引入的延迟
  • 图数据库Neo4j详解
  • 系列一、Shiro概述
  • SpringCloudAlibaba——Sentinel
  • Java编写简易rabbitmq生产者与消费者
  • 3.0.3版vsftpd所支持的FTP命令
  • OTA包添加自定义内容
  • Luatos Air700 改变BL0942串口波特率
  • 不可忽视的国外服务器地址IP选择指南
  • C语言 预处理详解
  • c++ async 使用详解,创建异步任务的多种方法
  • 万物皆数——用matlab求解二阶微分方程
  • jmeter接口自动化部署jenkins教程
  • 前端js实现将数组对象组装成自己需要的属性,或者去掉对象中不必要的属性
  • MeterSphere 任意文件读取漏洞(CVE-2023-25814)