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 ...
随机推荐
- sedgewick增量序列的希尔排序
#include<bits/stdc++.h> using namespace std; int s[3]={1,5,19}; void shellsort(int *a,int n){ ...
- Windows 2008 WinRM和WinRS能帮你做什么?
介绍 WinRM及WinRS更新包含在Windows Vista, Windows Server 2003 R2, Windows Server 2008 (及 Server 2008 Cor ...
- Pass Data Between ASP.NET Pages
There is the data to send in current page <asp:TextBox ID="DataToSendTextBox" runat=&qu ...
- web访问流程
客户端发送请求—->httpd得到请求—->httpd解析请求的格式(html,css,jsp)—->请求相应的PHP解析—->PHP解析程序执行完毕—–>db(数据库) ...
- Mybatis通过接口的方式实现增删改查
导入jar包 [mybatis] [oracle] 生成数据库 1.添加Mybatis的配置文件mybatis-config.xml 在src目录下创建一个mybatis-config.xml文件,如 ...
- svg图片做图标字体
https://icomoon.io 这个网站,把svg变图标
- android在JNI_OnLoad入口函数下断点动态调试so库
一般来说,很多APK的校验代码,都会在程序运行的时候自动加载一些动态so库,然后执行这些库中的校验代码.所以为了能够通过程序的校验,我们必须在执行这些函数之前下断点——理想的方法就是在JNI_OnLo ...
- 水晶报表 IE设置
水晶报表:Crystal Reports(水晶报表)是一款商务智能(BI)软件,主要用于设计及产生报表.水晶报表是业内最专业.功能最强的报表系统,它除了强大的报表功能外,最大的优势是实现了与绝大多数流 ...
- 洛谷 P1653 == COGS 2043 猴子
P2107 可爱的猴子 时间限制:1000MS 空间限制:65535KB 问题描述: 树上有n只猴子.它们编号为 1 到n.1 号猴子用它的尾巴勾着树枝.剩下的猴子都被其他的猴子用手抓着.每只猴子的 ...
- js5:框架的使用,使框架之间无痕连接
原文发布时间为:2008-11-08 -- 来源于本人的百度文章 [由搬家工具导入] <html> <head> <base target="js4" ...