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 ...
随机推荐
- Android 获取地理位置信息 封装好了 直接用
前言:花了一个早上研究了以下android获取经纬度,然后网上的参考资料都是杂七杂八,基本上都是过去几年的,现在我用 android6.0参照别人的结果发生好多错误,我的内心几乎是崩溃的.后来,不断百 ...
- Linux下Oracle JDK替换Open JDK
Oracle的产品需要Oracle JDK,但是Linux发行版附带的都是开源的Open JDK,这里给出的方法是在不删除原有Open JDK的情况下,安装Oracle JDK 环境 系统:CentO ...
- 如何拿到半数面试公司Offer——我的Python求职之路(转)
从八月底开始找工作,短短的一星期多一些,面试了9家公司,拿到5份Offer,可能是因为我所面试的公司都是些创业性的公司吧,不过还是感触良多,因为学习Python的时间还很短,没想到还算比较容易的找到了 ...
- 小知识(h5 js )
1.如果都为NaN但是他们不相等var a=NaN;var b=NaN;a==b //flase2.javascript 是一种脚本语言,可以创建服务器端和客户端的脚本3.javascript 中有两 ...
- Sql获取数据表字段说明
SELECT Sysobjects.name AS TABLE_NAME , syscolumns.Id , syscolumns.name AS COLUMN_NAME , systypes.nam ...
- StartWith 测试
var clientConfiguration = GetConfiguration("couchbase.json"); ClusterHelper.Initialize(cli ...
- 【Plus One】cpp
题目: Given a non-negative number represented as an array of digits, plus one to the number. The digit ...
- MongoDB快速入门学习笔记2 MongoDB的概念及简单操作
1.以下列举普通的关系型数据库和MongoDB数据库简单概念上的区别: 关系型数据库 MongoDB数据库 说明 database database 数据库 table collection 数据库表 ...
- python 中单例模式
1.什么是单例模式: 单例模式是指一个类有且只有一个实例对象,创建一个实例对象后,再创建实例是返回上一次的对象引用.(简单的讲就是两个实例对象的ID相同,节省了内存空间) 2.单例模式的创建: 举例创 ...
- Leetcode 488.祖玛游戏
祖玛游戏 回忆一下祖玛游戏.现在桌上有一串球,颜色有红色(R),黄色(Y),蓝色(B),绿色(G),还有白色(W). 现在你手里也有几个球. 每一次,你可以从手里的球选一个,然后把这个球插入到一串球中 ...