题解 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. Codeforces Round #324 (Div. 2) Dima and Lisa 哥德巴赫猜想

    原题链接:http://codeforces.com/contest/584/problem/D 题意: 给你一个奇数,让你寻找三个以内素数,使得和为这个奇数. 题解: 这题嘛...瞎比搞搞就好,首先 ...

  2. iOS7自定义back按钮和pop交互手势

    Clambake for iPhone有一个回退按钮在所有的导航条上.这是一个简单的没有文字箭头. 实现一个自定义按钮是简单的.类似这个设置controller 的navigationItem一个le ...

  3. [置顶] zabbix告警信息-lykchat信息发送系统

    lykchat信息发送系统 lykchat信息发送系统是Python3开发的,通过模拟微信网页端,基于个人微信号,为系统管理人员提供信息发送工具. 实现的功能有用户登录管理.微信登陆管理和微信信息发送 ...

  4. java项目热加载工具jrebel

    flask有热加载的功能,修为代码后,自动生效. java项目也有类似的功能,不过需要使用收费的插件jrebel 提供一个免费的注册服务器:http://139.199.89.239:1008/884 ...

  5. LeetCode 290 Word Pattern(单词模式)(istringstream、vector、map)(*)

    翻译 给定一个模式,和一个字符串str.返回str是否符合同样的模式. 这里的符合意味着全然的匹配,所以这是一个一对多的映射,在pattern中是一个字母.在str中是一个为空的单词. 比如: pat ...

  6. JS: document.getElementBy(), setInerval()

    ylbtech-JavaScript-DOM document.getElementBy(),setInerval() 1.A,document.getElementBy()返回顶部 document ...

  7. 某音乐类App评论相关API的分析及SQL注入尝试

    关键字:APIfen.工具使用.sql注入 涉及工具/包:Fiddler.Burpsuite.Js2Py.Closure Compiler.selenium.phantomjs.sqlmap 摘要: ...

  8. 2017.2.22 activiti实战--第六章--任务表单

    学习资料:<Activiti实战> 第六章 任务表单 本章将一步步完成一个协同办公系统(OA)的请假流程的设计,讲解如何将Activiti和实际业务联系起来. 首先讲解动态表单与外置表单的 ...

  9. IP反查网站,ip反查接口,旁站查询接口大全,通过IP查域名汇总:

    http://cn.bing.com/search?q=ip%3A220.181.111.85     http://dns.aizhan.com/?q=www.baidu.com     http: ...

  10. java debug

    java debug 模式下各button作用 F5:跳入方法  Step Into F6:向下逐行调试  Step Over F7:跳出方法  Step Return F8:直接跳转到下一个断点 D ...