题意:
给你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. Delphi用QJSON解析JSON格式的数据

    本来用superobject来解析JSON已经够用了,可惜这个东东不能在移动端使用,于是找到QJSON来处理. 这是一个国内高手写开源免费的东西,赞一个. 假入数据如下: {"message ...

  2. Objective-C学习笔记-第四天(1)

    解决以下昨天遇到的问题 1.@class与import是怎么样的呢?参考:http://www.cnblogs.com/ios8/p/ios-oc-test.html 在头文件中, 一般只需要知道被引 ...

  3. 到入百度LSS framework Reason: image not found

    dyld: Library not loaded: @rpath/VideoCore.framework/VideoCore Referenced from: /var/containers/Bund ...

  4. Ubuntu Git 入门

    参考自 码农生涯 中大熊猫 hustpzb的专栏 1 在github创建账号--创建repository 2 创建后网页会出现提示,指导如何使用 3 安装git sudo apt-get instal ...

  5. npm(cnpm)介绍

    1.npm(node package manager) nodejs的包管理器,用于node插件管理(安装.卸载.更新.管理依赖等); 2.使用npm安装安装插件: 1).命令提示符执行 npm in ...

  6. Activity packagename has leaked window android.widget.PopupWindow$PopupDecorView{4f92660 V.E...... .......D 0,0-455,600} that was originally added here

    原因是在销毁Activity时,Activity中的popupwindow还处于显示状态. 解决方法是重写Activity的onDestroy()方法,在Activity销毁前调用popupWindo ...

  7. BZOJ 2898 模拟

    普及组水题. 按位模拟第一个序列和第二个序列,细节比较多.. 仅为部分看后面两位的和,如果大于10就近位小于8就不进位等于9就看下一位. #include <cstdio> #define ...

  8. Stars_树状数组

    Problem Description Astronomers often examine star maps where stars are represented by points on a p ...

  9. eclipse快捷键以及使用技巧大全

    eclipse快捷键以及使用技巧大全1. 打开MyEclipse 6.0.1,然后"window"→"Preferences" 2. 选择"java& ...

  10. WIN32服务程序(二):卸载服务

    卸载服务的过程是这样的,用OpenSCManager打开SCM,使用OpenService打开准备卸载的服务,通过QueryServiceStatus查询该服务的状态是否停止,如果否,则先停止该服务C ...