CF742B 题解

CF742B 题解

题意

给定两个整数 \(n\)\(t\),并给出一个数组 \(a\)\(a_1,a_2,\cdots,a_n\),问有多少组 \(a_i,a_j\) 使得 \(a_i \bigoplus a_j(i,j\in [1,n])\)

思路

首先,先熟悉一下 $ \bigoplus$(异或)的性质,其实他就是相同为假,不同为真:\(1 \bigoplus0=1,0 \bigoplus1=1,1 \bigoplus1=0,0 \bigoplus0=0\)

于是再进阶一下,就可以知道异或的逆运算:若有 \(a \bigoplus b=c\),则有 \(a \bigoplus c=b,b \bigoplus c=a\)。于是就想到用一个桶数组,来记录 \(a_i \bigoplus x=a_j\) 的情况数,最后加起来就可以输出正确答案了。

总结

  1. 位运算。
  2. 开 long long,不开 long long 见祖宗。
  3. 快读快写。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
long long n,x,ans,t[200005],cnt;//一定要开long long 
long long read(){//快读 
	long long 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<<1)+(x<<3)+ch-'0';
		ch=getchar();
	}
	return x*f;
}
void write(long long x){//快写 
	if(x>9) write(x/10);
	putchar(x%10+'0'); 
}
int main(){
	n=read();x=read();
	for(int i=1; i<=n; i++){
		cnt=read();
		ans+=t[x^cnt];
        t[cnt]++;
	}
	write(ans);
	return 0;
} 
posted @ 2025-01-29 15:34  naroto2022  阅读(19)  评论(0)    收藏  举报