【题目链接】 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. 注册表添加python

    win(python2.7)下: 执行此文件 #!/usr/bin/env python # encoding:utf-8 # # script to register Python 2.0 or l ...

  2. SQL Server 查看备份集元数据的 4 种方法。

    方法 1. restore labelonly 方法 2. restore headeronly 方法 3. restore filelistonly 方法 4. restore verifyonly ...

  3. LoadRunner Tutorial

    LoadRunner Tutorial Welcome to the LoadRunner tutorial. The tutorial is a self-paced guide that lead ...

  4. 100个linux常用命令

    1,echo “aa” > test.txt 和 echo “bb” >> test.txt //>将原文件清空,并且内容写入到文件中,>>将内容放到文件的尾部 2 ...

  5. C语言的本质(14)——不完全类型和复杂声明

    ISO 将 C 的类型分为三个不同的集合: 函数.对象和不完全类型三大类.函数类型很明显:对象类型包含其他一切,除非不知道对象的大小.该标准使用术语"对象类型"指定指派的对象必须具 ...

  6. Node.js log1: ERR can not find module express

    1.win7下创建项目中提示输入的命令:cd project&&npm install    安装失败 输入上面提示的命令,预期结果:自动安装了依赖 ejs 和 express,失败提 ...

  7. Search gold(dp)

    Search gold Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Subm ...

  8. 【机房系统知识小结点系列】之遍历窗体中的控件,判断Text是否为空?

    做机房系统时,几乎每个窗体中都会用到判断界面中的控件是否为空的情景.我们曾经是这样走来的: 第一版: 好处:对窗体界面中的Text等控件,逐一做判断,当用户输入某一项为空的时候,会议弹出框的形式,告诉 ...

  9. CF#263

    昨天没打,今天写了一下,前三题都没有难度吧. A. Appleman and Easy Task time limit per test 1 second memory limit per test ...

  10. 【Trie】【HDU1247】【Hat’s Wordsfd2】

    题目大意: hat's word 的定义是字典中 恰好由另外两个单词连接起来的单词 给你一本字典,问有多少个hat's word,(字典按字典序给出) 单词数50000.. 初步思路: 单词分为前缀单 ...