Codefoeces 734F. Anton and School 数学
题目大意:
给定两个正整数序列\(b,c\)构造一个正整数序列\(a\)使其满足
\begin{array}{}
b_i=(a_i\text{ and }a_1)+(a_i\text{ and }a_2)+...+(a_i\text{ and }a_n) \\
c_i = (a_i\text{ or }a_1)+(a_i\text{ or }a_2)+...+(a_i\text{ or }a_n)
\end{array}
\right.
\]
题解:
我们有这么一个性质\((a_i \text{ and } b_i)+(a_i\text{ or }b_i)= a+b\)
所以我们把所有的\(b_i,c_i\)加和,得到
\begin{array}{}
b_1+c_1 = na_1 + \sum{a_i} \\
b_2+c_2 = na_2 + \sum{a_i} \\
... \\\
b_n+c_n = na_n + \sum{a_i}
\end{array}
\right.
\]
然后再把所有的式子加和得到
\(\sum{a_i} = \frac{\sum{b_i} + \sum{c_i}}{2n}\)
然后我们可以利用这个解出所有的\(a_i\)
我也不知道为什么必须对每一位都特殊判定,不过不判定的话下面这组数据过不去
1
3
5
应该是不可行,但是如果不进行数位判定的话会输出4...
哪位大神可以来救救我。。。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
template<typename T>inline void read(T &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline int cat_max(const int &a,const int &b){return a>b ? a:b;}
inline int cat_min(const int &a,const int &b){return a<b ? a:b;}
const int maxn = 200010;
ll b[maxn],c[maxn],a[maxn];
int cnt[32][2];
int main(){
int n;read(n);
ll sum = 0;
for(int i=1;i<=n;++i) read(b[i]),sum += b[i];
for(int i=1;i<=n;++i) read(c[i]),sum += c[i];
if(sum % (n<<1) != 0) return puts("-1");
sum /= (n<<1);
for(int i=1;i<=n;++i){
ll x = b[i] + c[i] - sum;
if(x < 0 || (x%n != 0)) return puts("-1");
a[i] = x/n;
for(int j=0;j<31;++j) ++cnt[j][(a[i]>>j)&1];
}
for(int i=1;i<=n;++i){
for(int j=0;j<31;++j) b[i] -= ((a[i]>>j)&1)*cnt[j][1]<<j;
if(b[i] != 0) return puts("-1");
}
for(int i=1;i<=n;++i){
printf("%I64d",a[i]);
if(i != n) putchar(' ');
else putchar('\n');
}
getchar();getchar();
return 0;
}
Codefoeces 734F. Anton and School 数学的更多相关文章
- Codeforces 734F Anton and School(位运算)
[题目链接] http://codeforces.com/problemset/problem/734/F [题目大意] 给出数列b和数列c,求数列a,如果不存在则输出-1 [题解] 我们发现: bi ...
- CodeForces 734F Anton and School
位运算. 两个数的和:$A+B=(AandB)+(AorB)$,那么$b[i]+c[i]=n*a[i]+suma$.可以解出一组解,然后再按位统计贡献验证一下. #pragma comment(lin ...
- Codeforces Round #404 (Div. 2) D. Anton and School - 2 数学
D. Anton and School - 2 题目连接: http://codeforces.com/contest/785/problem/D Description As you probabl ...
- CodeFoeces 1215 D Ticket Game(数学,思维)
CodeFoeces 1215 D Ticket Game 题目大意 M和B轮流玩游戏(每一轮M先手 现在给出一个长度为偶数的串,包含字符'?'和数字 现在两人依次在'?'上填数字\(0\)~\(9\ ...
- 【codeforces 734F】Anton and School
[题目链接]:http://codeforces.com/problemset/problem/734/F [题意] 给你两个数组b和c; 然后让你找出一个非负数组a满足题中所给关系; [题解] 有个 ...
- C. Anton and Fairy Tale(数学推式子)
\(数学题,式子并不难推,但边界是真的烦\) \(\color{Red}{Ⅰ.其实可以发现,当m>=n时,每次都可以粮食补到n,所以一定是在第n天消耗完毕}\) \(\color{Purple} ...
- PDF分享:国外优秀数学教材选评
<国外优秀数学教材选评>推荐书目下载 具体内容请查看原内容: http://www.library.fudan.edu.cn/wjzx/list/373-1-20.htm 或者http:/ ...
- 数学思想:为何我们把 x²读作x平方
要弄清楚这个问题,我们得先认识一个人.古希腊大数学家 欧多克索斯,其在整个古代仅次于阿基米德,是一位天文学家.医生.几何学家.立法家和地理学家. 为何我们把 x²读作x平方呢? 古希腊时代,越来越多的 ...
- 速算1/Sqrt(x)背后的数学原理
概述 平方根倒数速算法,是用于快速计算1/Sqrt(x)的值的一种算法,在这里x需取符合IEEE 754标准格式的32位正浮点数.让我们先来看这段代码: float Q_rsqrt( float nu ...
随机推荐
- firework压缩图片类似于GD库中压缩图片的思路
1.先建一张空白图片, 2.再把需要压缩的图片拖上去, 3.符合画布 4.调到需要的大小
- Spring Boot 从入门到实战汇总
之前写过几篇spring boot入门到实战的博文,因为某些原因没能继续. 框架更新迭代很快,之前还是基于1.x,现在2.x都出来很久了.还是希望能从基于该框架项目开发的整体有一个比较系统的梳理,于是 ...
- 完好用户体验: 活用window.location与window.open解决页面跳转问题
原文地址: JavaScript Redirects and window.open原文日期: 2014年08月27日翻译日期: 2014年08月31日翻译人员: 铁锚 (译者注: 本文解决的是按 C ...
- 有一个数组a[N]顺序存放0~N-1,要求每隔两个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。以8个数(N=7)为例:{0,1,2,3,4,5,6,7},0->1->2(删除)->3->4->5(删除)->6->7->0(删除),如此循环直到最后一个数被删除。
// ConsoleApplication12.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" // ConsoleApplication1 ...
- 4.锁--并行编程之条件变量(posix condition variables)
在整理Java LockSupport.park()的东东.看到了个"Spurious wakeup".又一次梳理下. 首先来个<UNIX环境高级编程>里的样例: [c ...
- java 常用设计模式(转载)
http://www.cnblogs.com/hnrainll/archive/2011/12/29/2305582.html 设计模式:一个程序员对设计模式的理解:“不懂”为什么要把很简单的东西搞得 ...
- Spring Cloud 微服务一:Consul注册中心
Consul介绍 Consul is a service mesh solution providing a full featured control plane with service disc ...
- jQuery中slideToggle()的详细使用方法和解释
$(selector).slideToggle(speed,callback) 参数 speed和callback Speed 可选.规定元素从隐藏到显示的速度,默认‘normal’可能 ...
- 【BZOJ4197】[Noi2015]寿司晚宴 状压DP+分解质因数
[BZOJ4197][Noi2015]寿司晚宴 Description 为了庆祝 NOI 的成功开幕,主办方为大家准备了一场寿司晚宴.小 G 和小 W 作为参加 NOI 的选手,也被邀请参加了寿司晚宴 ...
- Something Starts While Something Ends
(1)最终还是没能参加比赛,一次都没有机会. (2)有梦想,不到最后一刻不会放弃. (3)这里应该会搬次家,转到github上. (4)作为一个新手,什么东西都需要从头学起来,就从最基础的数据结构开始 ...