【题解】 CF734F 【Anton and School】
题解 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;
}
【题解】 CF734F 【Anton and School】的更多相关文章
- CF734F Anton and School (构造)
\(solution\) : 这道题做法很巧妙,需要对位运算有足够了解: $( a $ & $ b )$ \(+\) $( a $ | $ b )$ \(=\) \(a+b\) ,所以有 \( ...
- CF734F Anton and School 构造+数论
正解:构造 解题报告: 先放下传送门QwQ 这题首先要知道一个结论:(x&y)+(x|y)=x+y 还是能理解的趴? 所以我们把bi+ci就能得到∑a+n*a[i] 然后我们就能成功求出∑a ...
- 【CF734F】Anton and School(构造)
[CF734F]Anton and School(构造) 题面 Codeforces 洛谷 题解 算是一道\(easy\)? 发现\((a\&b)+(a|b)=a+b\). 那么根据给定条件我 ...
- 题解 CF734A 【Anton and Danik】
本蒟蒻闲来无事刷刷水题 话说这道题,看楼下的大佬们基本都是用字符 ( char ) 来做的,那么我来介绍一下C++的优势: string ! string,也就是类型串,是C语言没有的,使用十分方便 ...
- 题解 CF785E 【Anton and Permutation】
考虑用分块解决这个题,一次交换对当前逆序对个数的影响是,加上两倍的在区间\([l+1,r-1]\)中比\(a_r\)小的元素个数,减去两倍的在区间\([l+1,r-1]\)中比\(a_l\)小的元素个 ...
- CF734B Anton and Digits 题解
Content 有 \(k_2\) 个 \(2\).\(k_3\) 个 \(3\).\(k_5\) 个 \(5\) 和 \(k_6\) 个 \(6\),你可以用这里面的数字来组成 \(256,32\) ...
- 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) 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) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...
随机推荐
- Kali Linux 2017中Scapy运行bug解决
Kali Linux 2017中Scapy运行bug解决 Scapy是一款强大的网络数据包构建工具.在Kali Linux 2017中,当在scapy的命令行中,运行res.graph()生成图形 ...
- mysql事物中行锁与表锁
事物与锁 什么叫不支持事物: 首先要了解数据库里的事务是什么意思.事务在计算机数据库里 :在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序执行单元(unit).在关系数据库中,一个事务可以 ...
- centos7.5更换docker-ce镜像源
更换成阿里云 cd /etc/yum.repos.d/ vim docker-ce.repo # 按ecs进行非编辑模式 :%s/https:\/\/download.docker.com/https ...
- STM32命名
STM32产品命名 示例: STM32 F 100 C 6 T 6 B XXX 1 2 3 4 5 6 7 8 9 从上面的料号可以看出以下信息: ST品牌ARM Cortex-Mx系列内核32位超值 ...
- Zlib编译
转自原文 编译和使用zlib 由于要编译Cesium Terrain Build,其中不仅需要gdal,还用到了zlib,所以此时不得不总结一下Zlib的编译之道了. 在windows下用到zlib库 ...
- [译]在IB中实现自动布局
有关自动布局的其他文章: Autolayout Visual format language for autolayout Creating individual constraints 可怜的界面编 ...
- 微信小程序 - 传参的几种方式
1. navigator navigator?第一参数&第二参数 .... 在传递页面的options可以拿到传递过来的参数 <navigator url='start-test/sta ...
- HDU 3657 Game(取数 最小割)经典
Game Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- C# 调用API接口处理公共类 自带JSON实体互转类
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net ...
- MongoDB 快速入门
本作品由Man_华创作,采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可.基于http://www.cnblogs.com/manhua/上的作品创作. MongoDB No ...