B. MEXor Mixup

time limit per test

2 seconds

memory limit per test

256 megabytes

Alice gave Bob two integers a and b (a>0 and b≥0). Being a curious boy, Bob wrote down an array of non-negative integers with MEX value of all elements equal to a and XOR value of all elements equal to b.

What is the shortest possible length of the array Bob wrote?

Recall that the MEX (Minimum EXcluded) of an array is the minimum non-negative integer that does not belong to the array and the XOR of an array is the bitwise XOR of all the elements of the array.

Input

The input consists of multiple test cases. The first line contains an integer t (1≤t≤5⋅104) --- the number of test cases. The description of the test cases follows.

The only line of each test case contains two integers a and b (1≤a≤3⋅105; 0≤b≤3⋅105) --- the MEX and XOR of the array, respectively.

Output

For each test case, output one (positive) integer --- the length of the shortest array with MEX a and XOR b. We can show that such an array always exists.

Example

Input

Copy

复制代码
5
1 1
2 1
2 0
1 10000
2 10000

Output

Copy

复制代码
3
2
3
2
3

Note

In the first test case, one of the shortest arrays with MEX 1 and XOR 1 is [0,2020,2021].

In the second test case, one of the shortest arrays with MEX 2 and XOR 1 is [0,1].

It can be shown that these arrays are the shortest arrays possible.

解题说明:此题是一道数学题,采用异或运算,可以先预处理一下,求出所有30000以下数字的异或值,MEX为a,即小于a的数都得出现,所以可以先将他们XOR起来。然后如果这些数的异或已经等于b了,那最小长度就是a,如果不等于,那么只要在找一个数,就可以异或得到b,长度为a+1,但如果这个数恰好等于a,就需要再找两个数,是这两个数异或等于a,长度为a+2。

cpp 复制代码
#include <stdio.h>
#include <string.h>
int a[300030];
int main()
{
	int t;
	scanf("%d", &t);
	int i;
	for (i = 1; i < 300030; i++)
	{
		a[i] = a[i - 1] ^ i;
	}
	while (t--)
	{
		int n, m;
		scanf("%d%d", &n, &m);
		if (m == a[n - 1])
		{
			printf("%d\n", n);
		}
		else if (m == a[n])
		{
			printf("%d\n", n + 2);
		}
		else
		{
			printf("%d\n", n + 1);
		}
	}
	return 0;
}
相关推荐
Rabitebla1 天前
【C++】string 类:原理、踩坑与对象语义
linux·c语言·数据结构·c++·算法·github·学习方法
小雅痞1 天前
[Java][Leetcode middle] 167. 两数之和 II - 输入有序数组
java·算法·leetcode
CN-Dust1 天前
【C++】输入cin例题专题
java·c++·算法
数模竞赛Paid answer1 天前
2025年MathorCup数学建模A题汽车风阻预测解题文档与程序
算法·数学建模·mathorcup
Old Uncle Tom1 天前
OpenClaw 记忆系统 -- 记忆预加载
java·数据结构·算法·agent
会编程的土豆1 天前
洛谷题单入门1 顺序结构
数据结构·算法·golang
生信碱移1 天前
PACells:这个方法可以鉴定疾病/预后相关的重要细胞亚群,作者提供的代码流程可以学习起来了,甚至兼容转录组与 ATAC 两种数据类型!
人工智能·学习·算法·机器学习·数据挖掘·数据分析·r语言
智者知已应修善业1 天前
【51单片机中的打飞机设计】2023-8-25
c++·经验分享·笔记·算法·51单片机
智者知已应修善业1 天前
【51单片机按键调节占空比3位数码管显示】2023-8-24
c++·经验分享·笔记·算法·51单片机
.5481 天前
## Sorting(排序算法)
python·算法·排序算法