题意:
给你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. [转]Objective-c中@interface、@implementation、@protocal

    原处:http://blog.csdn.net/l271640625/article/details/8393531 以下Objective-c简称OC 从事java开发的程序员们都知道,在java中 ...

  2. logsatsh input 插件之 collectd

    logsatsh input 插件之 collectd 标签(空格分隔): logstash 作用:用于监控内存,cpu,磁盘I等信息 未完待续,时间未定. 参考: logstash 官网 elast ...

  3. 移动端1px

    移动端不同尺寸设备dpi不同,会造成1px线条不同程度的缩放,可利用媒体查询device-pixel-ratio,进行不同情况匹配: @media(-webkit-min-device-pixel-r ...

  4. CDS

    very nice artical talk about mergechangelog and cleardataset Delta and Data http://www.cnblogs.com/y ...

  5. aspjpeg 组件在asp中的使用

    本来好的系统,你却没有做好迁移等交接工作,所以,要麻烦死自己了-------for 凌杰 首先,该系统为asp系统,需要aspjpeg  支持.... 经过使用和测试.发现有如下小结. 1. 安装时 ...

  6. 通过工厂方式配置bean

    src\dayday\CarFactoryBean.java package dayday;import org.springframework.beans.factory.FactoryBean;/ ...

  7. MySQL数据库8 -子查询,联合查询

    一 使用IN关键字的子查询 问题: 查询游戏类型是'棋牌类' 的游戏的分数信息 - 游戏分数表中并未包含游戏类型信息 思路一:采用链接查询 思路二: 分两步进行,首先找到所以'棋牌类'游戏的编号,再以 ...

  8. 通过本地IIS服务器+路由器==实现本地局域网WIFI覆盖

    这是一张手机连接wifi局域网下载视频的图片,速度可以达到10M/S左右,下面让我们来看一下,本地服务器是如何建立的. 1.启动本地IIS服务 步骤如下 一.电脑右键-属性-控制面板-程序和功能-打开 ...

  9. elasticsearch,python包pyes进行的处理

    elasticsearch:高性能搜索引擎,官网:https://www.elastic.co/products/elasticsearch/ 对于它相信大家都不陌生,es的使用已经广泛存在 各大网站 ...

  10. 面向对象to1

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...