【异或+贪心】HDU5661Claris and XOR【BestCoder Round #79 (div.2)】
来源:互联网 发布:网站推广软件skycc 编辑:程序博客网 时间:2024/06/05 03:57
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5661
问题描述
Claris非常喜爱位运算,尤其是异或(XOR),因为它具有很多优美的性质。他有四个正整数a,b,c,d,满足a≤b且c≤d。他想选择两个整数x,y,满足a≤x≤b且c≤y≤d,使得x XOR y的值最大。但是他不知道该怎么做,所以请你告诉他x XOR y的最大值是多少。
输入描述
有多组测试数据,第一行一个整数T(1≤T≤10,000),表示测试数据的组数。对于每组测试数据:仅一行,四个整数a,b,c,d(1≤a,b,c,d≤1018),相邻两个整数之间有一个空格隔开。
输出描述
对于每组测试数据,仅一行,一个整数,即x XOR y的最大值。
输入样例
21 2 3 45 7 13 15
输出样例
611
Hint
在第一组数据中,当且仅当x=2,y=4,x XOR y取得最大值。在第一组数据中,当且仅当x=5,y=14或x=6,y=13,x XOR y取得最大值。
代码:
#include<iostream>#include<string>#include<cstring>#include<algorithm>#include<cstdio>#define LL long longusing namespace std;LL a,b,c,d; // [a,b],[c,d]区间寻找x^y的最大值;int isok(LL x,LL y,LL p){ LL xx=x+(1LL<<p)-1; LL yy=y+(1LL<<p)-1; if(x>b||y>d) return 0; // 已经填好第p个位置的值之后保证不能大于b,d边界; else if(xx<a||yy<c) return 0; // 在接下来尚未判断的位置全部填1保证不能低于最小值; else return 1;}void TanXin() // 贪心算法,从最高位开始,判断是否可以形成01状态;{ LL x=0,y=0; for(int i=61;i>=0;i--){ if(isok(x,y+(1LL<<i),i)){ // 目前最高位能否放01; y+=1LL<<i; }else if(isok(x+(1LL<<i),y,i)){ // 目前最高位能否放10; x+=1LL<<i; }else if(isok(x+(1LL<<i),y+(1LL<<i),i)){ // 目前最高位能否放11; x+=1LL<<i; y+=1LL<<i; } } cout<<(x^y)<<endl;}int main(){ int t; cin>>t; while(t--){ cin>>a>>b>>c>>d; TanXin(); } return 0;}
0 0
- 【异或+贪心】HDU5661Claris and XOR【BestCoder Round #79 (div.2)】
- BestCoder Round #79 (div.2) 1002 Claris and XOR
- BestCoder Round #79 1002/hdu 5661 Claris and XOR 贪心
- BestCoder Round #79 (div.1) Claris and XOR 题解。。。
- Codeforces Round #435 (Div. 2)-Mahmoud and Ehab and the xor(异或)
- Codeforces Round #435 (Div. 2)C. Mahmoud and Ehab and the xor(异或的性质)
- Codeforces Round #435 (Div. 2)-异或规律&思维-Mahmoud and Ehab and the xor
- 【Codeforces Round 169 (Div 2) D】【简单数位贪心】Little Girl and Maximum XOR 区间选两数使得异或值尽可能大
- Codeforces Round #177 (Div. 2)---E. Polo the Penguin and XOR operation(贪心)
- bestcoder Round #79 (div.2)jrMz and angles
- BestCoder Round #79 (div.2)-jrMz and angles,,暴力求解~
- 【BestCoder Round #79 (div.2)】HDU5660jrMz and angles
- HDU 5055 Bob and math problem(贪心)——BestCoder Round #11(div.2)
- HDU 5463 Clarke and minecraft(贪心)——BestCoder Round #56(div.2)
- BestCoder Round #62 (div.2)Clarke and food (简单贪心)
- BestCoder Round #83 1004 zxa and xor
- bestcoder 79 Claris and XOR
- BestCoder Round #59 (div.2) HDU5499 SDOI 排序+贪心
- Helloworld
- 笔记未整理
- 设计模式学习第四天
- 栈的链表表示
- opnet14.5 tutorial翻译——包交换网络仿真
- 【异或+贪心】HDU5661Claris and XOR【BestCoder Round #79 (div.2)】
- [学习笔记]php数据库访问抽象层PDO 1
- StringBuilder StringBuffer
- LeetCode 299. Bulls and Cows(公牛和母牛)
- 【补充】过程是什么?
- Android学习笔记(动画)
- Leetcode no. 169
- hadoop平台搭建(4)--伪分布式的配置及运行
- 上周工作中遇到的问题以及解决方法总结