Codeforces 734 F Anton and School
Discription
Anton goes to school, his favorite lessons are arraystudying. He usually solves all the tasks pretty fast, but this time the teacher gave him a complicated one: given two arrays b and c of length n, find array a, such that:
where a and b means bitwise AND, while a or b means bitwise OR.
Usually Anton is good in arraystudying, but this problem is too hard, so Anton asks you to help.
Input
The first line of the input contains a single integers n (1 ≤ n ≤ 200 000) — the size of arrays b and c.
The second line contains n integers bi (0 ≤ bi ≤ 109) — elements of the array b.
Third line contains n integers ci (0 ≤ ci ≤ 109) — elements of the array c.
Output
If there is no solution, print - 1.
Otherwise, the only line of the output should contain n non-negative integers ai — elements of the array a. If there are multiple possible solutions, you may print any of them.
Example
4
6 8 4 4
16 22 10 10
3 5 1 1
5
8 25 14 7 16
19 6 9 4 25
-1 有一个显然的性质就是 (a&b)+(a|b)=a+b。
这个考虑每一位的贡献就行了。
于是我们可以得到c[i]+b[i]=a[i]*n+∑a[],从而可以开开心心的算出每一个a,然后带回去验证一下就可以了
/*
We know that b[i]+c[i]=n*a[i]+∑a[]
*/
#include<bits/stdc++.h>
#define ll long long
#define maxn 200005
using namespace std;
int b[maxn],c[maxn];
int a[maxn],n,m,ci[60];
ll tot=0,now;
int main(){
ci[0]=1;
for(int i=1;i<=30;i++) ci[i]=ci[i-1]<<1; scanf("%d",&n);
for(int i=1;i<=n;i++) scanf("%d",b+i),tot+=(ll)b[i];
for(int i=1;i<=n;i++) scanf("%d",c+i),tot+=(ll)c[i];
//tot is used to calculate the sum of array a[]
if(tot%(n<<1)){
puts("-1");
return 0;
} tot/=(n<<1); for(int i=1;i<=n;i++){
now=c[i]+b[i]-tot;
if(now%n){
puts("-1");
return 0;
}
a[i]=now/n;
} for(int i=0;i<=30;i++){
int cnt=0;
for(int j=1;j<=n;j++) if(a[j]&ci[i]) cnt++;
for(int j=1;j<=n;j++){
if(a[j]&ci[i]) b[j]-=cnt*ci[i],c[j]-=n*ci[i];
else c[j]-=cnt*ci[i];
}
} for(int i=1;i<=n;i++) if(c[i]||b[i]){
puts("-1");
return 0;
} for(int i=1;i<=n;i++) printf("%d ",a[i]);
return 0;
}
Codeforces 734 F Anton and School的更多相关文章
- 【codeforces 734F】Anton and School
[题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...
- Codeforces 959 F. Mahmoud and Ehab and yet another xor task
\(>Codeforces\space959 F. Mahmoud\ and\ Ehab\ and\ yet\ another\ xor\ task<\) 题目大意 : 给出一个长度为 \ ...
- Codeforces 835 F. Roads in the Kingdom
\(>Codeforces\space835 F. Roads in the Kingdom<\) 题目大意 : 给你一棵 \(n\) 个点构成的树基环树,你需要删掉一条环边,使其变成一颗 ...
- Codeforces 785 D.Anton and School - 2(组合数处理)
Codeforces 785 D.Anton and School - 2 题目大意:从一串由"(",")"组成的字符串中,找出有多少个子序列满足:序列长度为偶 ...
- Codeforces 785 E. Anton and Permutation(分块,树状数组)
Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...
- Codeforces 731 F. Video Cards(前缀和)
Codeforces 731 F. Video Cards 题目大意:给一组数,从中选一个数作lead,要求其他所有数减少为其倍数,再求和.问所求和的最大值. 思路:统计每个数字出现的个数,再做前缀和 ...
- Codeforces Round #379 (Div. 2) F. Anton and School
题意: 给你n对 b[i], c[i], 让你求a[i],不存在输出-1 b[i] = (a[i] and a[1]) + (a[i] and a[2]) + (a[i] and a[3]) +... ...
- 【27.91%】【codeforces 734E】Anton and Tree
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【29.89%】【codeforces 734D】Anton and Chess
time limit per test4 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
随机推荐
- 浅谈I/O模型
在学习线程,NIO等知识时都需要知道一些基础知识. 一.什么是同步或异步 同步:个人通俗理解多个人排队打饭一个窗口,只有前面一个人打完了,后面的人才能打.如果前面人因为什么原因一直站在那里不走,后面的 ...
- 3 - JVM随笔分类(gc.log ,VisualVM插件介绍,VisualVM远程连接方式介绍)
gc.log 354.2 KB 对于对应用的监控上可以使用Jdk自带的VisualVM来做可视化监控,可以查看当前服务应用进程的堆大小的走向,以及类的加载数量等,除此之外,VisualVM可以支持很多 ...
- 转投emacs
(global-set-key [f9] 'compile-file) (global-set-key [f10] 'gud-gdb) (global-set-key (kbd "C-z&q ...
- linux学习(三) -- lnmp环境切换php版本,并安装相应redis扩展
原创文章,转载请注明出处 我想配置的环境是ubuntu+nginx+mysql+php+redis,其中php装两个版本,php7和php56 ubuntu+nginx+mysql+php的环境配 ...
- 16进制转10进制 HDU-1720
A+B Coming Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
- OV7725学习之SCCB协议(一)
OV7725摄像头只能作为从机,通过SCCB协议配置内置的172个寄存器.因此首先要了解的就是SCCB总线 1.SCCB协议简述 SCCB协议有两线也有三线,两线为SIO_C与SIO_D,三线为SIO ...
- 在数组中寻找出现次数大于N/K的数
给定一个int[]数组,给定一个整数k,打印所有出现次数大于N/k的数,没有的话,给出提示信息. === 核心思想:一次在数组中删除K个不同的数,不停的删除,直到剩下的数的种类不足K就停止删除,那么如 ...
- Mysql 死锁
http://www.cnblogs.com/benshan/archive/2013/05/09/3068886.html 声明:以下讨论只是针对InnoDB存储引擎. 何为死锁? 死锁是对资源 ...
- 用Vundle管理Vim插件
作为程序员,一个好用的Vim,是极其重要的,而插件能够使原本功能羸弱的Vim变得像其他功能强大的IDE一样好用.然而下载.配置插件的过程比较繁琐,大家往往需要自己进行下载/配置等操作,如果还涉及到更新 ...
- 指定某个git的版本代码拉取新的分支
在本地找到一个目录,执行 git clone http://gitlab.xxxxx.com/xxxxx/xxxxx.git cd xxxxx/ git log //找到对应版本的SHA值 例如2b1 ...