题解 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】的更多相关文章

  1. CF734F Anton and School (构造)

    \(solution\) : 这道题做法很巧妙,需要对位运算有足够了解: $( a $ & $ b )$ \(+\) $( a $ | $ b )$ \(=\) \(a+b\) ,所以有 \( ...

  2. CF734F Anton and School 构造+数论

    正解:构造 解题报告: 先放下传送门QwQ 这题首先要知道一个结论:(x&y)+(x|y)=x+y 还是能理解的趴? 所以我们把bi+ci就能得到∑a+n*a[i] 然后我们就能成功求出∑a ...

  3. 【CF734F】Anton and School(构造)

    [CF734F]Anton and School(构造) 题面 Codeforces 洛谷 题解 算是一道\(easy\)? 发现\((a\&b)+(a|b)=a+b\). 那么根据给定条件我 ...

  4. 题解 CF734A 【Anton and Danik】

    本蒟蒻闲来无事刷刷水题 话说这道题,看楼下的大佬们基本都是用字符 ( char ) 来做的,那么我来介绍一下C++的优势: string ! string,也就是类型串,是C语言没有的,使用十分方便 ...

  5. 题解 CF785E 【Anton and Permutation】

    考虑用分块解决这个题,一次交换对当前逆序对个数的影响是,加上两倍的在区间\([l+1,r-1]\)中比\(a_r\)小的元素个数,减去两倍的在区间\([l+1,r-1]\)中比\(a_l\)小的元素个 ...

  6. CF734B Anton and Digits 题解

    Content 有 \(k_2\) 个 \(2\).\(k_3\) 个 \(3\).\(k_5\) 个 \(5\) 和 \(k_6\) 个 \(6\),你可以用这里面的数字来组成 \(256,32\) ...

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

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

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

随机推荐

  1. 暂停时间 以及dict 循环

    暂停时间 #!/usr/bin/python # -*- coding: UTF-8 -*- import time myD = {1: 'a', 2: 'b'} for key, value in ...

  2. luogu P3376 【模板】网络最大流(no)ek

    题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...

  3. ie8 不能加载dll的问题解决

    请问是在打开IE的时候提示无法加载DLL文件吗? 请尝试重置IE: 1. 关闭所有Internet Explorer窗口. 2. 单击开始,点击运行,输入inetcpl.cpl,按回车. 3. 点击高 ...

  4. 批量去掉文件中的^M

    "^M"在Linux下的输入方法 ctrl v + ctrl m 方案一: find -type f | xargs sed -i 's/^M//g' 或者 find -name ...

  5. “应用程序无法正常启动(oxc000007b)”解决方案

  6. 解决 ultraedit 菜单字体模糊

    新装完ultraedit后,使用时菜单里的字体非常小,模糊不清非常不爽. 1.安装好UltraEdit后,到安装目录里,复制一份Uedit32.exe文件用于修改.2.使用UltraEdit打开复制的 ...

  7. MFC中 给基于CFormView的单文档添加背景图片

    关于基于CFormView的单文档应用程序,添加一个图片背景的方法之一如下: 下面是利用LoadImage实现.(先在程序目录中添加背景图片back.bmp) 1.在view类中添加类成员变量:(为C ...

  8. DB11 TCP数据协议拆包接收主要方法

    北京地标(DB11) 据接收器. /// <summary> /// DB11协议拆包器 /// </summary> public class SplictProtocol ...

  9. 转:maven2创建一个eclipse工程,设置M2_REPO

    from: http://tonychanhoho.iteye.com/blog/1584324 M2_REPO是一个用来定义 maven 2仓库在硬盘中的存储位置,windows默认是C:\User ...

  10. log4j.properties(信息打印)

    ### set log levels ###log4j.rootLogger = INFO , console , debug , error ### console ###log4j.appende ...