题解 CF734F 【Anton and School】

传送门

这种将位运算和普通运算结合起来的题目要拆位来考虑,可以得到\(log_{2}(\)值域\()\)的算法,甚至将值域看成常数。

根据

\(a|b+a \& b=a+b\)

得到

\(b_i+c_i=\Sigma a_i+na_i\)

于是

\(a_i=\frac{b_i+c_i- \Sigma a_i}{n}\)

根据这个式子,直接得到\(a_i\),注意在除的时候判断整除以免非法情况出现。

此时,我们要判断\(b_i\)和\(c_i\)是否真的合法,考虑到位运算的性质,我们开个\(cnt[x]\),记录所有\(a_i\)在二进制第\(x\)位出现的次数,此时,我们只需要检验——

\(b_i=2^k \times cnt[k]\)

\(c_i=\Sigma a_i+2^k \times (n-cnt[k])\)

这里的\(k\)满足

\(a_i\&(1<<(k-1))\)

总复杂度\(O(nlog(\)值域\())\),相当于\(O(n)\),但理论上会爆\(unsigned\) \(long\) \(long\) 但是它没有爆。

极其丑陋的代码

#include<bits/stdc++.h>

#define RP(t,a,b) for(register int (t)=(a),edd_=(b);t<=edd_;++t)
#define qit return puts("-1"),0 using namespace std;typedef unsigned long long ll;
template<class ccf> inline ccf qr(ccf k){
char c=getchar();
ccf x=0;
int q=1;
while(c<48||c>57)q=c==45?-1:q,c=getchar();
while(c>=48&&c<=57)x=x*10+c-48,c=getchar();
return q==-1?-x:x;
} const int maxn=200005;
ll a[maxn];
ll b[maxn];
ll c[maxn];
ll cnt[65];
ll n;ll sum; int main(){
#ifndef ONLINE_JUDGE
freopen("in.in","r",stdin);
freopen("out.out","w",stdout);
#endif
n=qr(1);
RP(t,1,n)
b[t]=qr(1ll);
RP(t,1,n)
c[t]=qr(1ll);
RP(t,1,n)
sum+=b[t]+c[t];
if(sum%(n<<1))
qit;
sum/=(n<<1);
RP(t,1,n){
a[t]=b[t]+c[t]-sum;
if(a[t]%n)
qit;//宏
else
a[t]/=n;
RP(i,1,63)
if((a[t]&(1ll<<(i-1))))
cnt[i]++;
} ll temp=0;
RP(t,1,n){
temp=0;
RP(i,1,63)
if(a[t]&(1ll<<(i-1)))
temp+=cnt[i]*(1ll<<(i-1));
if(temp!=b[t])
qit;//宏
temp=sum;
RP(i,1,63)
if(a[t]&(1ll<<(i-1)))
temp+=(n-cnt[i])*(1ll<<(i-1));
if(temp!=c[t])
qit;//宏
} RP(t,1,n)
cout<<a[t]<<' ';
puts("");
return 0;
}

最新文章

  1. struts 文件下载
  2. 学习git config配置文件
  3. css技术
  4. Nodejs学习(四)- express目录的分析
  5. JavaScript内存优化
  6. Project简介
  7. js基础知识温习:js中的对象
  8. Runtime类
  9. winform中拖动功能实现技巧
  10. POJ 2538 WERTYU水的问题
  11. IOS学习之路六(UITableView滑动删除指定行)
  12. 仿网易新闻app下拉标签选择菜单
  13. git的merge功能
  14. Codeforces 1083C Max Mex [线段树]
  15. 口碑订单,ERP本地加/退菜无法回流至手机端的解决办法-订单金额不统一erp本地加菜H5没有
  16. Python画图代码
  17. C# Hadoop学习笔记(二)—架构原理
  18. 二、K3 WISE 开发插件《 工业单据老单客户端插件事件、属性、方法》
  19. linux大小写转换
  20. Focalprice李培亮:梦想让人在我店里排队

热门文章

  1. 使用OPENROWSET爆破SQL Server密码
  2. Light oj 1233 - Coin Change (III) (背包优化)
  3. Java中获取当前时间并格式化
  4. 通达OA 小飞鱼工作流在线培训教程(七)工作流应用的意义及基础设置(图文)
  5. 2016.11.10 Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver
  6. mysql莫名的主键重复
  7. UNP学习笔记(第四章 基本TCP套接字编程)
  8. bottle的几个小坑
  9. 3、C++新的关键字
  10. Deploying Docker images via SSH