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 ...
随机推荐
- UVA 12657 Boxes in a Line
双向链表 注意:如果算法是最后处理翻转情况时,注意指令4翻转后1,2两个指令也要翻转处理: 指令3 中交换盒子要注意两个盒子相邻的情况 #include <iostream> #inclu ...
- python操作redis--string
#!/usr/bin/python #!coding:utf-8 """ 完成用redis模块操作string类型的数据 """ impor ...
- Nginx学习笔记二基本配置
1.Nginx的配置文件默认在Nginx程序安装目录的conf二级目录下,主配置文件为nginx.conf.假设您的Nginx安装 在/usr/local/webserver/nginx/目录下,那么 ...
- progressbar样式
http://www.oschina.net/question/8676_11797 http://blog.csdn.net/ouyangtianhan/article/details/656576 ...
- 【Leetcode】查找二叉树中任意结点的最近公共祖先(LCA问题)
寻找最近公共祖先,示例如下: 1 / \ 2 3 / \ / \ 4 5 6 7 / \ ...
- Debian安装JAVA环境(转载)
Debian官方没有维护专门的Java软件包,所以不能直接用apt-get工具来安装.在Debian系统中要安装Java,有两种方式,一种是用传统方式:一种是Debian方式. 1. 传统方式 在 s ...
- IOS中http请求使用cookie
http://rainbird.blog.51cto.com/211214/805173 IOS中http请求使用cookie 2012-03-13 23:04:30 标签:http 记录 龙的传人 ...
- linux学习方法之六
相信不少想学习linux的新手们正愁不知道看什么linux学习教程好,下面小编给大家收集和整理了几点比较重要的教程,供大家学习,如需想学习更多的话,可到wdlinux学堂寻找更多教程. 1.linux ...
- 实现winfrom进度条及进度信息提示,winfrom程序假死处理
1.方法一:使用线程 功能描述:在用c#做WinFrom开发的过程中.我们经常需要用到进度条(ProgressBar)用于显示进度信息.这时候我们可能就需要用到多线程,如果不采用多线程控制进度条,窗口 ...
- 《think in python》学习-10
think in python 10 列表 和字符串相似,列表是值得序列.在列表中,它可以是任何类型,列表中的值成为元素,有时也称为列表项 s = [10,20,30,40] print s #列表也 ...