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]) +...+ (a[i] and a[n]);
c[i] = (a[i] or a[1]) + (a[i] or a[2]) + (a[i] or a[3]) +...+ (a[i] or a[n]);
and 和 or 是按位与 或
思路:
(a and b) + (a or b) = (a + b) 证明显然
所以把每个b[i]+c[i] = n*a[i] + s, s为所有a的和
这样 就可以解出每个 a[i]了,且解唯一,证明比较显然
但是,解出的解不一定就是正确解!
为什么会这样呢?
因为解方程的时候 把b[i] c[i]看成了一个整体。
也就是他们的和是满足的,但是他们自身可能不满足。
举个例子:n = 1, b[0] = 3, c[0] = 5;
发现用上面的方法是有解的 a[0] = 4, 但是很显然,这个也并不合法
所有我们需要检测每一个b,c是否合法。
n那么大,暴力n方肯定是不行了。
我们要这样操作:
用A[i][j]表示 a[i]的第j位是否为1,顺便求出k[j],k[j]表示所有a中的第j位有多少个1 处理的时间复杂度O(n*logv)
再求出B[i][j], C[i][j]
B[i][j] = (A[1][j] and A[i][j]) + (A[2][j] and A[i][j]) + ... + (A[n][j] and A[i][j]);
C[i][j] = (A[1][j] or A[i][j]) + (A[2][j] or A[i][j]) + ... + (A[n][j] or A[i][j]);
这个不用暴力求,因为我们刚刚已经求出了k[j]
显然地,有下面的式子:
if A[i][j] = 0: B[i][j] = 0, C[i][j] = k[j]
else B[i][j] = k[j], C[i][j] = n
有了B C后我们就能很快的求出b, c了
b[i] = B[i][0]*2^0 + B[i][1]*2^1 + ...
c[i] = C[i][0]*2^0 + C[i][1]*2^1 + ...
最终的时间复杂度O(n*logv), v=max(a1,a2,..,an)
具体代码如下:
const int maxn = + ;
LL b[maxn], c[maxn], a[maxn];
LL A[maxn][], B[maxn][], C[maxn][], k[maxn];
LL s;
int n; void init()
{
scanf("%d", &n);
for (int i = ; i < n; i ++) scanf("%lld", b + i);
for (int i = ; i < n; i ++) scanf("%lld", c + i);
} bool check() //返回求出的答案是否合法
{
for (int j = ; j < ; j++)
{
for (int i = ; i < n; i++)
{
A[i][j] = a[i] & (1ll << j) ? : ;
k[j] += A[i][j];
}
}
for (int j = ; j < ; j++)
{
for (int i = ; i < n; i++)
{
if (A[i][j])
{
B[i][j] = k[j];
C[i][j] = n;
}
else
{
B[i][j] = ;
C[i][j] = k[j];
}
}
} for (int i = ; i < n; i++)
{
LL sumB = , sumC = ;
for (int j = ; j < ; j++)
{
sumB += B[i][j] * (1ll << j);
sumC += C[i][j] * (1ll << j);
}
if (sumB != b[i] || sumC != c[i]) return false;
}
return true;
} void solve()
{
bool ans = true;
for (int i = ; i < n; i++)
{
s += b[i] + c[i];
}
if (s % ( * n)) ans = false;
s /= * n;
for (int i = ; i < n; i++)
{
a[i] = (b[i] + c[i] - s) / n;
if ((b[i] + c[i] - s) % n) ans = false;
}
if (!ans || !check()) printf("-1\n");
else
{
for (int i = ; i < n;i ++)
{
printf("%lld ", a[i]);
}
}
} int main()
{
init();
solve();
return ;
}
Codeforces Round #379 (Div. 2) F. Anton and School的更多相关文章
- Codeforces Round #379 (Div. 2) D. Anton and Chess 水题
D. Anton and Chess 题目连接: http://codeforces.com/contest/734/problem/D Description Anton likes to play ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess —— 基础题
题目链接:http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test 4 seconds me ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree 缩点 直径
E. Anton and Tree 题目连接: http://codeforces.com/contest/734/problem/E Description Anton is growing a t ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...
- Codeforces Round #379 (Div. 2) B. Anton and Digits 水题
B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...
- Codeforces Round #379 (Div. 2) A. Anton and Danik 水题
A. Anton and Danik 题目连接: http://codeforces.com/contest/734/problem/A Description Anton likes to play ...
- Codeforces Round #379 (Div. 2) D. Anton and Chess 模拟
题目链接: http://codeforces.com/contest/734/problem/D D. Anton and Chess time limit per test4 secondsmem ...
- Codeforces Round #379 (Div. 2) E. Anton and Tree —— 缩点 + 树上最长路
题目链接:http://codeforces.com/contest/734/problem/E E. Anton and Tree time limit per test 3 seconds mem ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions —— 二分
题目链接:http://codeforces.com/contest/734/problem/C C. Anton and Making Potions time limit per test 4 s ...
随机推荐
- 借用Google API在线生成网站二维码地址方法
二维码其实很早就出现了,在国外很多年前就已经在应用了,国内这两年才开始异常的火爆,智能手机的发展,以及微博.微信等移动应用带动了二维码的普及.那么,如果为网址在线生成二维码呢?下面我们就来介绍一下Go ...
- ios安装app提示【未受信任的企业级开发者】。在设置中信任此开发者
最近在测试app,ios安装app后点击提示如下图: 解决方法: 1 点击 [设置] >[通用] >[设备管理] 2 点击企业级应用 > 信任该开发者 > 信任.设置之后 ...
- PCB Layout高速电路设计小知识点
1.单位:1mil = 0.0254mm 2.default线宽及线距(综合考虑高速电路性能及PCB板厂制程能力):6mil 3.差分走线特征阻抗:100ohm 4.3W原则,即线间距采用3倍线宽,多 ...
- C语言程序设计第7堂作业
一.本次课主要内容: 本次以计算圆柱体体积为例,通过定义体积计算功能的函数和主函数调用的例子,引出函数定义的一般形式:函数首部加函数体,且在函数结尾处通过return 语句返回结果.本节要重 ...
- 在web中使用windows控件,实现摄像头功能
最近做的一个Web版的视频会议项目,需要在网页中播放来自远程摄像头采集的实时视频,我们已经有了播放远程实时视频的使用C#编写的windows控件,如何将其嵌入到网页中去了?这需要使用一种古老的技术,A ...
- SharePoint 2013 中的 PowerPoint Automation Services
简介 许多大型和小型企业都将其 Microsoft SharePoint Server 库用作 Microsoft PowerPoint 演示文稿的存储库.所有这些企业在 ...
- "流"派家族,一脉相承
(更多内容请关注本人微信订阅号:it_pupil) 你没进错,我们讲的是Java的输入输出流. 概述 ➤ 可以从其中读入一个字节序列的对象称作输入流.(输入流是一个对象,可以从这个对象中读取一个字 ...
- spring 包下载地址
留着,以备不时之需: http://repo.spring.io/libs-release-local/org/springframework/spring/
- 使用Cargo实现自动化部署
Cargo是一组帮助用户操作Web容器的工具,它能帮助用户实现自动化部署,而且它几乎支持所有的Web容器,如Tomcat.JBoss.Jetty和Glassfish.Cargo通过cargo-mave ...
- phonegap android 开发环境搭建
1.下载JDK并安装 http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html 配置环境变量 ...