Codeforces 734F Anton and School(位运算)
【题目链接】 http://codeforces.com/problemset/problem/734/F
【题目大意】

给出数列b和数列c,求数列a,如果不存在则输出-1
【题解】
我们发现:
bi+ci=2n*ai-(所有ai为1且aj为0的数位)+(ai为0且aj为1的数位)= n*ai+Σak
记为(1)式
同时又有(ai为1且aj为0的数位)+(aj为1且ai为0的数位)=ai xor aj
那么Σ(bi+ci)=Σ(2n*ai)-Σ(ai xor aj)+Σ(ai xor aj)=Σ(2n*ai) 记为(2)式
我们发现2*n*(1)-(2)=2*n*n*ai
这样就得到了我们要的东西,即ai数项了。
因为之前的推导都是建立在a数列存在的基础上,
因此在通过这个方法得到a数列之后需要对其进行验证。
【代码】
#include <cstdio>
#include <algorithm>
using namespace std;
const int N=200010;
int n,a[N],b[N],c[N],s[N],p[N][30],u[N],ave;
long long sum=0;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",b+i);
for(int i=1;i<=n;i++)scanf("%d",c+i);
for(int i=1;i<=n;i++)s[i]=b[i]+c[i],sum+=s[i];
ave=sum/(2*n); for(int i=1;i<=n;i++)a[i]=(s[i]-ave)/n;
for(int i=1;i<=n;i++){
for(int j=0;j<30;j++)if((a[i]>>j)&1)p[i][j]=1,u[j]++;
}int flag=1;
for(int i=1;i<=n;i++){
b[0]=c[0]=0;
for(int j=0;j<30;j++){
if(p[i][j]){
b[0]+=(1<<j)*u[j];
c[0]+=(1<<j)*n;
}else c[0]+=(1<<j)*u[j];
}if(b[0]!=b[i]||c[0]!=c[i]){flag=0;break;}
}if(flag){for(int i=1;i<=n;i++)printf("%d ",a[i]);}
else puts("-1");
return 0;
}
Codeforces 734F Anton and School(位运算)的更多相关文章
- CodeForces 734F Anton and School
位运算. 两个数的和:$A+B=(AandB)+(AorB)$,那么$b[i]+c[i]=n*a[i]+suma$.可以解出一组解,然后再按位统计贡献验证一下. #pragma comment(lin ...
- F. Anton and School 位运算 + 化简
http://codeforces.com/contest/734/problem/F 因为 x + y = (x & y) + (x | y) 有了这个公式后,然后应该手动模拟一下,把公式化 ...
- Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp
题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...
- CodeForces 165E Compatible Numbers(位运算 + 好题)
wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...
- Codeforces 868D Huge Strings - 位运算 - 暴力
You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...
- 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest
题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...
- Codeforces Round #716 (Div. 2), problem: (B) AND 0, Sum Big位运算思维
& -- 位运算之一,有0则0 原题链接 Problem - 1514B - Codeforces 题目 Example input 2 2 2 100000 20 output 4 2267 ...
- Divide by Zero 2021 and Codeforces Round #714 (Div. 2) B. AND Sequences思维,位运算 难度1400
题目链接: Problem - B - Codeforces 题目 Example input 4 3 1 1 1 5 1 2 3 4 5 5 0 2 0 3 0 4 1 3 5 1 output 6 ...
- CodeForces 282C(位运算)
C. XOR and OR time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- [XMPP]简易的聊天室实现[一]
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- VirtualBox 上安装CentOS 6.5
目标:1.在VirtualBox中安装CentOS2.配置虚拟机网络,实现: a.主机联网后,宿机能够通过主机上网 b.不管主机联网与否,主机都能SSH登录宿机,并且主宿机能互相传送文件 ...
- MEMS开关
MEMS器件在射频比如无线通信上有很好的应用.RF MEMS谐振器和诱导器品质因子在微波上有大幅度提高.MEMS开关极大地改进了高频性能和降低了能耗.本篇概要介绍MEMS开关. 自从1979年彼特森( ...
- 【Mysql5.7数据目录和配置文件目录】
Win7下 C:\ProgramData\MySQL\MySQL Server 5.7\ 如何查看mysql运行.访问记录等日志 1.首先确认你日志是否启用了mysql>show variabl ...
- C语言的本质(22)——C标准库之字符串操作
编译器.浏览器.Office套件等程序的主要功能都是符号处理,符号处理功能在程序中占相当大的比例,无论多复杂的符号处理都是由各种基本的字符串操作组成的,下面介绍如何用C语言的库函数做字符串初始化.取长 ...
- 【hihocoder 1257 Snake Carpet】构造
2015北京区域赛现场赛第4题. 题面:http://media.hihocoder.com/contests/icpcbeijing2015/problems.pdf OJ链接:http://hih ...
- OC基础3:一些基本概念
"OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.关于NSLog函数中的“%2i”: ( ...
- C语言---整型字符串转换
C语言提供了几个标准库函数,能够将随意类型(整型.长整型.浮点型等)的数字转换为字符串.下面是用itoa()函数将整数转 换为字符串的一个样例: # include <stdio.h> ...
- 修改ORACLE-NLS_DATE_FORMAT时间格式的四种方式
修改ORACLE-NLS_DATE_FORMAT时间格式的四种方式 改变ORACLE -NLS_DATE_FORMAT中时间显示格式的显示有以下方式: 1.可以在用户环境变量中指定(LINUX). 在 ...
- spring boot 中文文档
https://qbgbook.gitbooks.io/spring-boot-reference-guide-zh/content/VII.%20Spring%20Boot%20CLI/index. ...