HDU 5661 Claris and XOR 贪心
题目链接:
hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5661
bc(中文):http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=686&pid=1002
题解:
考虑从高位到低位贪心,对于每一位,
(1)、如果x,y只有唯一的取法,那么只能这么取;否则贪心地必须使答案的这一位等于1。
(2)、如果x,y都是0,1都能取,则设这是从右向左数第len位,因为x,y能取的值一定都是连续的一段,因此x,y的后len位都能取0111...1(len-1个1)和1000...0(len-1个0)(否则做不到从右向左数第len位都能取0,1)。也就是说,后len位的贡献一定能达到可能的上界111...1(len个1)。此时不必继续考虑后面的位。
(3)、如果x,y在这一位并不是0,1都能取,那么由于要使得答案的这一位等于1,也只有唯一的取法。
至此,这一位考虑完毕,然后根据选取的方案,修正一下x和y的范围(只有第(3)种情况要考虑调整x,y范围,比如说如果在第i位,x可以选0,也可以选1,则x选0后b中比i小的位数会变成全1,如果x选1,则a中比i小的位数会变全零),然后对后一位继续这样处理即可。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL; const LL one=; int n; int main(){
int tc;
scanf("%d",&tc);
while(tc--){
LL a,b,c,d;
int tag1,tag2,tag3,tag4;
scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
LL ans=;
for(int i=;i>=;i--){
tag1=tag2=tag3=tag4=;
if(a&(one<<i)) tag1=;
if(b&(one<<i)) tag2=;
if(c&(one<<i)) tag3=;
if(d&(one<<i)) tag4=;
// if(i<=3) printf("(%d,%d,%d,%d)\n",tag1,tag2,tag3,tag4);
if((tag1^tag2)&&(tag3^tag4)){
ans|=((one<<(i+))-);
break;
}else if(!(tag1^tag2)&&!(tag3^tag4)){
if(tag1^tag3){
ans|=(one<<i);
}
}else if(tag1^tag2){
if(tag1^tag3){
b=(one<<i)-;
}else{
a=;
}
ans|=(one<<i);
}else if(tag3^tag4){
if(tag3^tag1){
d=(one<<i)-;
}else{
c=;
}
ans|=(one<<i);
}
}
printf("%lld\n",ans);
}
return ;
}
HDU 5661 Claris and XOR 贪心的更多相关文章
- hdu 5661 Claris and XOR
Claris and XOR Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- BC之Claris and XOR
http://acm.hdu.edu.cn/showproblem.php?pid=5661 Claris and XOR Time Limit: 2000/1000 MS (Java/Others) ...
- Claris and XOR(模拟)
Claris and XOR Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- HDU5661 Claris and XOR
我们求二进制是怎么求的呢:先看看二进制的每一位代表多大:.......32 16 8 4 2 1 假如n=10, ..... 32>n ,不要. 16>n,不要. 8<=n,要,然后 ...
- HDU 5813 Elegant Construction (贪心)
Elegant Construction 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5813 Description Being an ACMer ...
- HDU 5802 Windows 10 (贪心+dfs)
Windows 10 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5802 Description Long long ago, there was ...
- HDU 5500 Reorder the Books 贪心
Reorder the Books Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php? ...
- HDU 5938 Four Operations 【贪心】(2016年中国大学生程序设计竞赛(杭州))
Four Operations Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- HDU 3697 Selecting courses(贪心)
题目链接:pid=3697" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=3697 Prob ...
随机推荐
- ASP.net 加载不了字体Failed to load resource: the server responded with a status of 404 (Not Found)
在bootstrap下加载不了字体内容.出现下列错误. 1.打开IIS找到部署的网站,点击MIME类型,把.woff和.woff2两个类型分别添加到新类型中,重启网站即可.
- 大专生自学html5到找到工作的心得
先做个自我介绍,我13年考上一所很烂专科民办的学校,学的是生物专业,具体的学校名称我就不说出来献丑了.13年我就辍学了,我在那样的学校,一年学费要1万多,但是根本没有人学习,我实在看不到希望,我就退学 ...
- Python-type函数使用
- (杭电1019 最小公倍数) Least Common Multiple
Least Common Multiple Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- gcd 模板
声明 给 x,y 两个数,求 x,y 的最大公因数. 辗转相除法,直接套!!! function gcd(x,y:longint):longint; begin then exit(x) else e ...
- centos7下mysql 开启远程登录
安装mysql 安装mysql就不做过多的介绍了,相信小伙伴们都可以很轻松的安装 进入mysql命令行 mysql -u用户名 -p密码 新建远程登录的用户 grant all on *.* to a ...
- 【转】 mysql使用federated引擎实现远程访问数据库(跨网络同时操作两个数据库中的表)
原文转自:http://www.2cto.com/database/201412/358397.html 问题: 这里假设我需要在IP1上的database1上访问IP2的database数据库内的t ...
- Python小白学习之如何添加类属性和类方法,修改类私有属性
如何添加类属性和类方法,修改类私有属性 2018-10-26 11:42:24 类属性.定义类方法.类实例化.属性初始化.self参数.类的私有变量的个人学习笔记 直接上实例: class play ...
- appium+python自动化☞环境搭建
前言:appium可以说是做app最火的一个自动化框架,它的主要优势是支持android和ios,另外脚本语言也是支持java和Python.略懂Python,所以接下来的教程是 appium+pyt ...
- 第三篇 Python关于mysql的API--pymysql模块, mysql事务
python关于mysql的API--pymysql模块 pymysql是Python中操作MySQL的模块,其使用方法和py2的MySQLdb几乎相同. 模块安装 pip install pymys ...