【题目链接】 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(位运算)的更多相关文章

  1. CodeForces 734F Anton and School

    位运算. 两个数的和:$A+B=(AandB)+(AorB)$,那么$b[i]+c[i]=n*a[i]+suma$.可以解出一组解,然后再按位统计贡献验证一下. #pragma comment(lin ...

  2. F. Anton and School 位运算 + 化简

    http://codeforces.com/contest/734/problem/F 因为 x + y = (x & y) + (x | y) 有了这个公式后,然后应该手动模拟一下,把公式化 ...

  3. Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp

    题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...

  4. CodeForces 165E Compatible Numbers(位运算 + 好题)

    wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...

  5. Codeforces 868D Huge Strings - 位运算 - 暴力

    You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...

  6. 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest

    题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...

  7. 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 ...

  8. 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 ...

  9. CodeForces 282C(位运算)

    C. XOR and OR time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. Linux命令(持续更新ing)

    *.命令语法:  a.在进行参数设定时,通常为“-”号,若为完整参数名称,则输入“--”符号;  b.指令太长的时候,可以使用“\”符号使指令连续到下一行;  c.各种符号的意义:    ''     ...

  2. UI产品设计流程中的14个要点

    http://www.sj33.cn/digital/wyll/201404/38318.html 自从我在 Dribbble 上贴了一幅我的产品设计成果,受到了大家伙热烈的反馈,对此我深受鼓励,我决 ...

  3. C# 弗洛伊德(Floyd)算法

    弗洛伊德(Floyd)算法 主要是用于计算图中所有顶点对之间的最短距离长度的算法,如果是要求某一个特定点到图中所有顶点之间的最短距离可以用;        ;    ;    ;            ...

  4. 单元测试(UT)、功能测试(FT)(转)

    纯个人总结: 单元测试(UT).功能测试(FT): 目的:1.尽量避免写的代码测试人员频繁的来找你其他地方又出问题了:2.提供的接口不可用:3.一个bug修复了引入了其他的bug或者其他用例变红了: ...

  5. Ubuntu 12.04安装NFS server

    首先安装nfs-kernel-server apt-get install nfs-kernel-server 然后创建一个目录: mkdir -p /opt/share 并赋予权限777: chmo ...

  6. cf466C Number of Ways

    C. Number of Ways time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. (续)顺序表之单循环链表(C语言实现)

    单循环链表和单链表的唯一区别在于单循环链表的最后一个节点的指针域指向第一个节点, 使得整个链表形成一个环. C实现代码如下: #include<stdio.h> typedef struc ...

  8. word2vec 入门(三)模型介绍

    两种模型,两种方法 模型:CBOW和Skip-Gram 方法:Hierarchical Softmax和Negative Sampling CBOW模型Hierarchical Softmax方法 C ...

  9. Storages and virtual servers

    1. IBM Storages: SONAS,V7k,V7ku,SVC,XIV 存储设备都安装了个性化定制的Linux系统,来完成不同的服务,这几台存储设备使用原理都是类似的,以SONAS (Scal ...

  10. 什么是OAuth

    什么是OAuth 如今很多网站的功能都强调彼此间的交互,因此我们需要一种简单,标准的解决方案来安全的完成应用的授权,于是,OAuth应运而生,看看官网对其的定义: An open protocol t ...