-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXORQueries.cpp
More file actions
36 lines (36 loc) · 852 Bytes
/
XORQueries.cpp
File metadata and controls
36 lines (36 loc) · 852 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include<math.h>
int solve(int A, int B, int C) {
//find d such that d>=B && d<=C A^d is maximum
long long output = 0;
int power = 0;
for(int bitIndex = 0;bitIndex<32;++bitIndex){
int bit = (A>>bitIndex) & 1;
if(bit==1){
// ans bit would be 0;
++power;
}
else{
//ans bit would be 1;
long long temp = output;
temp = output + pow(2, power);
if(temp>C){
//thats it. current output is max;
break;
}
else{
output = temp;
}
++power;
}
}
output = output^A;
return output;
}
int main(){
int A,B,C;
std::cin>>A>>B>>C;
int output = solve(A,B,C);
std::cout<<output<<std::endl;
return 0;
}