Codeforces Round #843 (Div. 2) C. Interesting Sequence、
通过lowbit计算,对n进行增加,同时判断x在二进制下是不是n的子集,通过and
位运算,n只会越来越小,当n变成和x相等是,t也增加到了符合条件的情况。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int main()
{
int T;cin>>T;
while(T--)
{
LL n,x;cin>>n>>x;
LL t=n;
while(1)
{
if(n==x || (n&x)!=x) break;
t=t+(t&-t);
n=n&t;
}
if(n==x) cout<<t<<endl;
else cout<<"-1"<<endl;
}
return 0;
}