Link to the original question: https://codeforces.com/contest/1421/problem/A
Test sample
input
6
6 12
4 9
59 832
28 14
4925 2912
1 1
output
10
13
891
18
6237
0
Note
For the first test case Sana can choose x=4 and the value will be ( 6 ⊕ 4 ) + ( 12 ⊕ 4 ) = 2 + 8 = 10 (6⊕4) + (12⊕4) = 2+8 = 10 (6⊕4)+(12⊕4)=2+8=10. It can be shown that this is the smallest possible value.
Question: Give two numbers aaa andbbb , you are asked to choose a numberxxx such that(a ⊕ x) + (b ⊕ x) (a⊕x) + (b⊕x)(a⊕x)+(b⊕x ) has the largest result. (⊕ stands for exclusive OR operation)
Problem-solving ideas: Let’s think about it. If the XOR operation is performed, the bits of the same bit are taken as 0 00 , if the bit is different, take1 11 , then we hope to make the same bit as possible so that the number will become smaller after the operation, so how to choose?Combine two numbers aaa, b b b , Hypothesisiii rank upaaa is 1,bbb is0 00 , then no matter whatxx you arex of theiii will not change the size for any value, only when they are all1 11 when1 11 . After XOR operation, this bit becomes0 00 , the sum will become smaller.So what we have to do is to build a xxx , so that it can optimize the above operations. We have two ways to solve it. The first method was solved during the game, which is to take out the2 2 of thetwo numbers.2 decimal digits, and then compared to givexxx . The second method is particularly good, is to refer tocf cfc f big guy, that is, the best result is actuallya ⊕ ba⊕ba⊕b . why? Because this is the same number of bits, take 0, and different ones take1 11 . The final addition is indeed different digits, one of which is 0 and one is 1, then the addition is 1. This effect can be achieved by performing an exclusive OR operation directly. It is equivalent to a formula derivation, cow.
AC code 1
/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h> //POJ不支持
#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int t,n;
int a,b;
void solve(){
vector<int> a1;
vector<int> b1;
int temp1=a,temp2=b;
while(a){
a1.push_back(1&a);
a=a>>1;
}
while(b){
b1.push_back(1&b);
b=b>>1;
}
int len=min(a1.size(),b1.size());
int ans=0;
rep(i,0,len-1){
ans+=a1[i]*pow(2,i);
}
cout<<(ans^temp1)+(ans^temp2)<<endl;
}
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>a>>b;
solve();
}
}
return 0;
}
AC code 2
/*
*邮箱:[email protected]
*blog:https://me.csdn.net/hzf0701
*注:文章若有任何问题请私信我或评论区留言,谢谢支持。
*
*/
#include<bits/stdc++.h> //POJ不支持
#define rep(i,a,n) for (int i=a;i<=n;i++)//i为循环变量,a为初始值,n为界限值,递增
#define per(i,a,n) for (int i=a;i>=n;i--)//i为循环变量, a为初始值,n为界限值,递减。
#define pb push_back
#define IOS ios::sync_with_stdio(false);cin.tie(0); cout.tie(0)
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int inf = 0x3f3f3f3f;//无穷大
const int maxn = 1e5;//最大值。
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
//*******************************分割线,以上为自定义代码模板***************************************//
int t,n;
int a,b;
int main(){
//freopen("in.txt", "r", stdin);//提交的时候要注释掉
IOS;
while(cin>>t){
while(t--){
cin>>a>>b;
cout<<(a^b)<<endl;
}
}
return 0;
}