Topic Links:
Claris and XOR
Time limit:2000/1000 MS (java/others)
Memory limit:65536/65536 K (java/others)
Problem Descriptionclaris loves bitwise operations very much, especially XOR, because it has many beautiful features. He gets four positive integersa,b,c,D that satisfiesa≤b andc≤d . He wants to choose, integersx,y that satisfiesa≤x≤b andc≤y≤d , and maximize the value ofx xOR y . But he doesn ' t know how to does it, so please tell him the maximum value ofx xOR y.
Inputthe first line contains an integerT(1≤t≤ten) --the number of the test cases.
For each test case, the only line contains four integersA,B,c, D ( 1≤a ,b , c, D≤ 1018) . Between each of the adjacent integers there are a white space separated.
Outputfor Each test case, the only line contains a integer that is the maximum value of x xOR y.
Sample Input21 2 3 45 7 13 15
Sample output611 Test Instructions: asked a<=x<=b,c<=y<=d; the largest x^y value is how much; ideas: greedy, from high to low, can take 1 of 1 can not take 0, Update the value of the a,b,c,d at the same time; &NBSP;AC code:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < cmath> #include <bits/stdc++.h> #include <stack> #include <map> using namespace std; #define for (i,j,n) for (int i=j;i<=n;i++) #define MST (SS,B) memset (ss,b,sizeof (ss)); typedef long Long LL; Template<class t> void Read (t&num) {char CH; bool F=false; For (Ch=getchar (); ch< ' 0 ' | | Ch> ' 9 '; f= ch== '-', Ch=getchar ()); for (num=0; ch>= ' 0 ' &&ch<= ' 9 '; num=num*10+ch-' 0 ', Ch=getchar ()); F && (num=-num);} int stk[70], tp;template<class t> inline void print (T p) {if (!p) {puts ("0"); return;} while (p) stk[++ TP] = p%10, p/=10; while (TP) Putchar (stk[tp--] + ' 0 '); Putchar (' \ n ');} Const LL Mod=1e9+7;const double Pi=acos ( -1.0); const int INF=1E18;CONST int N=1e5+10;const int Maxn=5e3+4;const double eps= 1e-12; LL A,b,c,d;int Main () {//freopen ("In.txt", "R", stdin); int T; Read (T); while (t--) {Read (a); Read (b); Read (c); Read (d); LL ans=0; for (int i=63;i>=0;i--) {int fa= ((a>>i) &1), fb= ((b>>i) &1), fc= ((c>>i) &1), Fd= ((d>>i) &1); if (FA!=FB&&FC!=FD) {ans|= (1ll<< (i+1)) -1;break;} else if (FA!=FB) {ans|= (1ll<<i); if (FC) {b= (1ll<<i)-1; c-= (1ll<<i); d-= (1ll<<i); } else {a=0; b-= (1ll<<i); }} else if (FC!=FD) {ans|= (1ll<<i); if (FA) {d= (1ll<<i)-1; a-= (1ll<<i); b-= (1ll<<i); } else {c=0; d-= (1LL<<i); }} else {if (FA==FC) {if (FA) {LL temp= (1ll<<i); A-=temp; B-=temp; C-=temp; D-=temp; }} else {ans|= (1ll<<i); if (FA) {a-= (1ll<<i); b-= (1ll<<i); } else {c-= (1ll<<i); d-= (1ll<<i); }}}} print (ANS); } return 0;}
hdu-5661 Claris and XOR (greedy)