题意:
给你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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. Codeforces Round #379 (Div. 2) B. Anton and Digits 水题

    B. Anton and Digits 题目连接: http://codeforces.com/contest/734/problem/B Description Recently Anton fou ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. easyui tabs update后tab上关闭图标失效的解决方案

    问题:使用easyui的tabs组件的时候,调用了tab的update方法,更新后的tab标签上的关闭图标失效 使用的js文件是1.3版本的jquery.easyui.min.js文件,通过读源文件发 ...

  2. SQLServer - 约束

    一.约束的分类 在SQLServer中,有3种不同类型的约束. 1.实体约束 实体约束是关于行的,比如某一行出现的值就不允许出现在其他行,例如主键. 2.域约束 域约束是关于列的,对于所有行,某一列有 ...

  3. AppCode 2016.2.3 发布,支持 Swift3 的特性

    AppCode 2016.2.3 (build 162.2380.5)发布了,AppCode 是一个全新的 Objective-C.Swift 的集成开发环境,用于帮助开发 Mac.iPhone 和 ...

  4. 国内app快速生成平台对比

     泰格老虎 2013-03-07 00:39:10 这是海恒CEO高鹏写的一篇国内app快速生成平台对比文章,介绍了国内快速生成APP的平台与自己平台的对比,很有参考价值. 同类网站 安米网 http ...

  5. VisualStudio自动编码插件(Autocode——devprojects.net)

    今天无意之中发现了一个VS插件,功能还不错,推荐给大家.官网http://www.devprojects.net/ 有以下一些功能: 智能感知! 快速命令调用,只需按Ctrl +回车 T4和ASPX一 ...

  6. 兼容解决 IE 、火狐、谷歌浏览器中 Iframe框架的页面缓存的方法

    <script type="text/javascript"> document.write('<iframe src="/ad_footer.html ...

  7. 【LeetCode】Product of Array Except Self

    Product of Array Except Self Given an array of n integers where n > 1, nums, return an array outp ...

  8. LayaAir引擎——(六)

    LayaAir引擎——TiledMap地图图块属性获取和进行墙壁碰撞检测 需要的软件: TiledMap LayaAir IDE 1.0.2版本 所画的地图: pass层: floor层: pass层 ...

  9. C#串口通讯实例

    本文参考<C#网络通信程序设计>(张晓明  编著) 程序界面如下图: 参数设置界面代码如下: using System; using System.Collections.Generic; ...

  10. 问题:C++ 删除数组指针实用 delete []变量 汇编怎么实现的?

    问题:C++ 删除数组指针实用  delete []变量    汇编怎么实现的?