Codeforces 734F Anton and School(位运算)
【题目链接】 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(位运算)的更多相关文章
- CodeForces 734F Anton and School
位运算. 两个数的和:$A+B=(AandB)+(AorB)$,那么$b[i]+c[i]=n*a[i]+suma$.可以解出一组解,然后再按位统计贡献验证一下. #pragma comment(lin ...
- F. Anton and School 位运算 + 化简
http://codeforces.com/contest/734/problem/F 因为 x + y = (x & y) + (x | y) 有了这个公式后,然后应该手动模拟一下,把公式化 ...
- Codeforces 1208F Bits And Pieces 位运算 + 贪心 + dp
题意:给你一个序列a, 问a[i] ^ (a[j] & a[k])的最大值,其中i < j < k. 思路:我们考虑对于每个a[i]求出它的最优解.因为是异或运算,所以我们从高位向 ...
- CodeForces 165E Compatible Numbers(位运算 + 好题)
wo integers x and y are compatible, if the result of their bitwise "AND" equals zero, that ...
- Codeforces 868D Huge Strings - 位运算 - 暴力
You are given n strings s1, s2, ..., sn consisting of characters 0 and 1. m operations are performed ...
- 图论/位运算 Codeforces Round #285 (Div. 2) C. Misha and Forest
题目传送门 /* 题意:给出无向无环图,每一个点的度数和相邻点的异或和(a^b^c^....) 图论/位运算:其实这题很简单.类似拓扑排序,先把度数为1的先入对,每一次少一个度数 关键在于更新异或和, ...
- 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 ...
- 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 ...
- CodeForces 282C(位运算)
C. XOR and OR time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- Asp.net MVC 之 ActionResult
Action运行完后,回传的值通过ActionResult 类别或者其衍生的类别操作.ActionResult是一个抽象类,因此,Asp.net MVC 本身就实作了许多不同类型的ActionResu ...
- Python 函数 切片 迭代 列表生成器
函数 编写 定义一个函数要用def语句 def sum(i,n): ⚠有冒号 返回多值 实际上是返回一个tuple 定义默认参数 默认参数的作用是简化调用 def ...
- Nginx 教程的连载计划
下面以教程系列为单位,列举出了已经发表和计划发表的连载教程: Nginx 新手起步 Nginx 是如何匹配 URI 的 Nginx 变量漫谈 Nginx 配置指令的执行顺序 Nginx 的 if 是邪 ...
- python学习day1
python简单介绍: Python的创始人为Guido van Rossum.1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,做为ABC 语言的一种 ...
- hdu 2157 How many ways_ 矩阵快速幂
题意:略 直接矩阵乘法就行了 #include <iostream> #include<cstdio> #include<cstring> using namesp ...
- jQuery中设置form表单中action值的方法
jQuery中设置form表单中action值的方法 (2011-03-17 10:18:19) 转载▼ 标签: 杂谈 html代码: <form id="myFormId&quo ...
- JAVA程序猿面试题汇总
(此话题将不断更新,请留意) 第一,谈谈final, finally, finalize的差别. final 用于声明属性,方法和类,分别表示属性不可变,方法不可覆盖,类不可继承.finally是异常 ...
- XML是什么,它能够做什么?——写给XML入门者
XML就可以扩展标记语言(eXtensible Markup Language).标记是指计算机所能理解的信息符号,通过此种标记,计算机之间能够处理包括各种信息的文章等.怎样定义这些标记,既能够选择国 ...
- SQL Server DML(SELECT)常见用法(二)
1 引言 上篇讲到SQL Server中DML的基本使用方法,其中SELECT语句是最常用的语句,其功能强大,结构复杂,下面通过例子,具体介绍其使用方法. 2 SELECT查询语句 SELECT语 ...
- Struts2 访问web元素
访问web元素的四种方法(耦合,依赖注入).(耦合,非依赖注入).(非耦合,依赖注入).(非耦合,非依赖注入) 耦合:可以得到HttpServletResponse,HttpServletReques ...