ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

CF742 (Div. 2) B

2021-09-06 11:33:38  阅读:288  来源: 互联网

标签:XOR MEX 异或 test CF742 Div array shortest


MEXor Mixup(源地址自⇔CF742B

Problem

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*10^4\) ) — 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*10^5\) ; \(0≤b≤3*10^5\) ) — 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

5
1 1
2 1
2 0
1 10000
2 10000

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.

题意

现在有 \(a\) 、 \(b\) 两个限定条件,求解出最短的数组,满足:
1 . 数组内元素必须是 \(>0\) 的。
2 . \(a\) 是数组中没有出现过的最小正整数
3 . 数组中所有元素的按位异或和恰好等于 \(b\) 。
输出数组的最短长度。

思路:

由条件1和2可知:数组内必定包含 \(a\) 个数,且为 [0,1,2,……,a-1]

由所有元素的按位异或和可知:在没有限制的条件下,对于已有的数据 \(b1\) ,要得到指定的数据 \(b2\) ,仅有两种情况可以讨论:
1 . \(b1==b2\) ,此时,直接输出即可。
2 . \(b1!=b2\) ,此时,容易证明,一定能找到一个数字 \(k\) ,使得 \(k\) 异或 \(b1\) 答案为 \(b2\) 。

由条件3可知:限制条件在于 \(k\) 不能等于 \(a\) ,若等于,则需要另外寻找两个数字再来异或和(例如样例数据1)。
所以综上可知,需要先计算出 \(0\) 至 \(a\) \((a=(0,1,2,…,3*10^5))\) 所有数字的异或和。此后,对于每一组读入的 \(a\ b\) ,

AC代码:

//A WIDA Project
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct fx{
	ll x,y,w;
}a[1000005];
ll n,ans;
bool cmp(fx x,fx y) return x.w>y.w;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%lld%lld",&a[i].x,&a[i].y);
		a[i].w=a[i].x-a[i].y;
	}
	sort(a+1,a+1+n,cmp);
	for(int i=1;i<=n;i++) ans+=a[i].w*i-a[i].x+n*a[i].y;
	printf("%d\n",ans);
	return 0;
}

错误次数:1次

原因:未开long long

标签:XOR,MEX,异或,test,CF742,Div,array,shortest
来源: https://www.cnblogs.com/WIDA/p/15232969.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有