题目链接 题意:给定正整数 a , b a,b a,b,找一个整数 x x x,最小化 ( a ⊕ x ) + ( b ⊕ x ) (a \oplus x)+(b \oplus x) (a⊕x)+(b⊕x) 思路:考虑两数二进制下相加,只有把两数都为1的位改成不是1的才可以将两数的和减少,所以只要使得 x = a & b x=a\&b x=a&b即可,通过简单的位运算模拟我们可以简化这个式子,即 ( a ⊕ ( a & b ) ) + ( b ⊕ ( a & b ) ) = a ⊕ b (a \oplus (a\&b))+(b \oplus (a\&b))=a \oplus b (a⊕(a&b))+(b⊕(a&b))=a⊕b AC代码:
#include <bits/stdc++.h> #define ll long long using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } /***************main****************/ ll T=1; int main() { T=read(); while (T--) { ll a,b; cin>>a>>b; cout<<(a^b)<<endl; } return 0; }