【题目链接】 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. nginx---Beginner's Guide

    一 启动 nginx -s signal Where signal may be one of the following: stop — fast shutdown quit — graceful ...

  2. tcpdump 抓包让wireshark来分析

    在linux下面用tcpdump 抓包非常方便, 但是抓的包要提取出来进行分析, 还是得用wireshark来过滤分析比较方便. 下面先介绍一下 TCPDUMP 的使用 例:tcpdump host ...

  3. Sublime Text 3 Build 3047 32bit/64bit 简体中文安装破解版

    Sublime Text 3 Build 3047 32bit/64bit 简体中文安装破解版 Sublime Text 3 Build 3047 32bit 简体中文安装破解版下载:http://y ...

  4. linux下自动同步internet时间

    linux下很简单直接一句即可: ntpdate time.nist.gov ntp后面参数为internet时间服务器url或ip即可. 但是ntpdate命令需要root特权,如果做成自动运行每次 ...

  5. iOS 堆和栈 的理解

    本文部分内容整理于网络,感谢原作者. 堆(heap)和栈(stack)是C/C++编程不可避免会碰到的两个基本概念.首先,这两个概念都可以在讲数据 结构的书中找到,他们都是基本的数据结构,虽然栈更为简 ...

  6. 使用Web Application Stress Tool 进行压力测试

    1.在测试客户端机器上启动Web Application Stress Tool,在弹出的“建立新脚本”对话框中选择“Record”按钮: 2.在“Record”参数设置第一步中,所有的checkbo ...

  7. LINQ 基本子句之一 (select/where/group/into)

    特别喜欢同事看到我写了一句小排序的时候说,他当然喜欢Linq了,虽然我只是baidu之,不知其然也不知其所以然. 基本格式 var<变量> = from <项目> in < ...

  8. DoNet开源项目-基于Amaze UI的点餐系统

    帮朋友做的点餐系统,主要是为了让顾客在餐桌上,使用微信扫描二维码,就可以直接点菜,吃完使用微信付款. 系统演示地址,账户名和密码均为:admin.(请不要删除admin用户) GitHub Clone ...

  9. 使用PowerShell管理Windows8应用

    引子(?): 我从消费者预览版开始使用的win8,大概是因为我年龄不大的缘故,我很快熟悉了这个操作系统并习惯了使用windows8的Modern App.我之前使用过一个叫Windows8 Moder ...

  10. Three.js学习笔记 – “我和小伙伴都惊呆了”的特效和Three.js初探

    什么是Three.js three.js是JavaScript编写的WebGL第三方库.提供了非常多的3D显示功能.Three.js 是一款运行在浏览器中的 3D 引擎,你可以用它创建各种三维场景,包 ...