C. Mikasa- Codeforces Round #735 (Div. 2)

原题链接Problem - 1554C - Codeforces

C. Mikasa

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given two integers nn and mm. Find the MEXMEX of the sequence n⊕0,n⊕1,…,n⊕mn⊕0,n⊕1,…,n⊕m. Here, ⊕⊕ is the bitwise XOR operator.

MEXMEX of the sequence of non-negative integers is the smallest non-negative integer that doesn't appear in this sequence. For example, MEX(0,1,2,4)=3MEX⁡(0,1,2,4)=3, and MEX(1,2021)=0MEX⁡(1,2021)=0.

Input

The first line contains a single integer tt (1≤t≤300001≤t≤30000)  — the number of test cases.

The first and only line of each test case contains two integers nn and mm (0≤n,m≤1090≤n,m≤109).

Output

For each test case, print a single integer  — the answer to the problem.

Example

input

Copy

5
3 5
4 6
3 2
69 696
123456 654321

output

Copy

4
3
0
640
530866

Note

In the first test case, the sequence is 3⊕0,3⊕1,3⊕2,3⊕3,3⊕4,3⊕53⊕0,3⊕1,3⊕2,3⊕3,3⊕4,3⊕5, or 3,2,1,0,7,63,2,1,0,7,6. The smallest non-negative integer which isn't present in the sequence i. e. the MEXMEX of the sequence is 44.

In the second test case, the sequence is 4⊕0,4⊕1,4⊕2,4⊕3,4⊕4,4⊕5,4⊕64⊕0,4⊕1,4⊕2,4⊕3,4⊕4,4⊕5,4⊕6, or 4,5,6,7,0,1,24,5,6,7,0,1,2. The smallest non-negative integer which isn't present in the sequence i. e. the MEXMEX of the sequence is 33.

In the third test case, the sequence is 3⊕0,3⊕1,3⊕23⊕0,3⊕1,3⊕2, or 3,2,13,2,1. The smallest non-negative integer which isn't present in the sequence i. e. the MEXMEX of the sequence is 00.

---------------------------------------------------------------------------------------------------------------------------

常规思路肯定是一个一个枚举,那必定会超时。CF里面一个套路就是,枚举不行时就构造。

这里需要一些知识和思维

首先是异或 a^b==c时  a^c==b

n^0,n^1,n^2....n^m分别对应某个数,但我们要找的那个数,在这里面不存在,设这个数是k,也就是

n^0!=k,n^1!=k,n^2!=k....n^m!=k  我们转化一下,n^k!=(0,1,2,3,....m)  再转化n^k>m,再转化,n^k>=m+1 

存在大小关系,我们从最高位开始比较容易确定大小关系,期间需要维护大于等于m+1的大小关系,并且最小化k

当前最高位 有四种情况(以下m+1称为m),

n==1,m==0   k这一位取0时,异或之后结果这一位是1,而m+1这一位是0,也就直接决定了后面的大小关系,无需进行比较。 k这一位取1,异或之后结果是0,k变大了不说,还得继续找大小关系,显然不如k=0;

n==0,m==0  k这1位取0,满足异或结果>=m,取1也是如此,显然取0更合适

n==0,m==1, k这1位取0,显然导致异或结果不满足,所以必须取1

n==1,m==1 k这一位取1,结果不满足,必须取0

# include<iostream>
# include<stack>
# include<cstring>
# include<math.h>
# include<map>

# include<iomanip>
using namespace std;
typedef long long int ll;



int main( )
{

   int t;
   cin>>t;

   while(t--)
   {
       int n,m;
       cin>>n>>m;
       int ans=0;
       // m+1
       m++;
       for(int i=30;i>=0;i--)
       {
           if(m&(1<<i))
           {
               if(n&(1<<i))
               {
                   ans=ans*2;

               }
               else
               {
                   ans=ans*2+1;
               }
           }
           else//是0
           {

               if(n&(1<<i))
               {
                   for(int j=i;j>=0;j--)
                   {
                       ans=ans*2;
                   }
                   break;

               }
               else
               {
                   ans=ans*2;
               }
           }
       }

       cout<<ans<<'\n';
   }

    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值