There are some beautiful girls in Arpa’s land as mentioned before.
Once Arpa came up with an obvious problem:
Given an array and a number x, count the number of pairs of indices i, j (1 ≤ i < j ≤ n) such that , where is bitwise xor operation (see notes for explanation).
Immediately, Mehrdad discovered a terrible solution that nobody trusted. Now Arpa needs your help to implement the solution to that problem.
Input
First line contains two integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the array and the integer x.
Second line contains n integers a1, a2, …, an (1 ≤ ai ≤ 105) — the elements of the array.
Output
Print a single integer: the answer to the problem.
Examples
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
2
Note
In the first sample there is only one pair of i = 1 and j = 2. so the answer is 1.
In the second sample the only two pairs are i = 3, j = 4 (since ) and i = 1, j = 5 (since ).
A bitwise xor takes two bit integers of equal length and performs the logical xor operation on each pair of corresponding bits. The result in each position is 1 if only the first bit is 1 or only the second bit is 1, but will be 0 if both are 0 or both are 1. You can read more about bitwise xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR.
题意:给出一个n和一个x,之后给出n个数,这个序列中,满足ai ^ aj == x的组数。
ps:首先先给出一个提示,如果大家在仔细思考之后还不懂得话再接着看下面的题解(个人感觉这样比较好)。
提示:如果有 a ^ b == c , 那么一定有c ^ b == a
(请认真思考之后再看题解)
思路:我们可以在输入的时候预处理出来一个cnt数组,记录每个数出现的次数。
那么我们之后就可以遍历一遍每一个数ai,求出与ai异或为x的tmp
这里分为两种情况,ai和tmp相等的情况和不等的情况
如果ai和tmp不等,那么直接累计tmp出现的次数即可
如若ai和tmp相等,那么累计(tmp出现的次数-1),因为ai和tmp是相等的,但是ai和ai是不能组合的,所以要减去本身,也就是减一的原因,ai只能和剩下的 cnt[tmp] - 1个数组合。
我们累计所有的tmp出现的次数,最后除以2即可。
为什么要除以2呢?
因为我们是要的组合。这就跟握手一样,如果我跟你握一次手,那么也就代表你跟我握了一次手,但
是我们是不需要握两次手的,所以只需要握一次即可。因此最后除以2
喜闻乐见代码系列:
#include <iostream>
#include <algorithm>
using namespace std;
//这里要注意,虽然题目中给出n的范围不超过1e5,但是两个数异或之后可能会超过1e5,所以要多开一些。
const int N = 1e6 + 10;
typedef long long LL;//因为两个数异或结果会很大,所以用long long了
LL n , x;
LL ans[N] , cnt[N] , vis[N];
int main(void)
{
cin >> n >> x;
for(int i = 1 ; i <= n ; ++ i)
{
scanf("%lld",&ans[i]);//读入n个数
cnt[ans[i]] ++;//对每一个数都累计一次次数
}
LL res = 0;//记录结果
for(int i = 1 ; i <= n ; ++ i)
{
LL tmp = ans[i] ^ x;//找到与ai亦或结果为x的tmp
if(tmp != ans[i]) res += cnt[tmp];
else res += cnt[tmp] - 1;
}
cout << res / 2 << endl;
return 0;
}
还有第二种做法。
我们知道,在这n个数中可能会有一些重复的元素,那么有没有一种方法可以只计算一次呢?
其实是有的
对于每一个ai,我们先求出tmp
如果ai不等于tmp,那么直接累计cnt[ai] * cnt[tmp]即可,直接就能计算出ai和tmp的所有组合方案数
如果ai等于tmp的话,那么就相当于在这cnt[ai](也是cnt[tmp])中任选2个数组合。
那这不就是排列组合中的组合吗?
所以如果ai等于tmp的话,直接累计cnt[ai] * (cnt[ai] - 1) / 2 即可
还有就是,用这种办法要再开一个数组作为标记数组
喜闻乐见代码系列:
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1e6 + 10;
typedef long long LL;
LL n , x;
LL ans[N] , cnt[N] , vis[N];
int main(void)
{
cin >> n >> x;
for(int i = 1 ; i <= n ; ++ i)
{
scanf("%lld",&ans[i]);
cnt[ans[i]] ++;
}
LL res = 0;
for(int i = 1 ; i <= n ; ++ i)
{
LL tmp = ans[i] ^ x;
if(!vis[tmp] && !vis[ans[i]])//如果当前这两个数都还没有被标记的话
{
if(ans[i] == tmp) res += cnt[tmp] * (cnt[tmp] - 1) / 2;
else res += cnt[tmp] * cnt[ans[i]];
vis[ans[i]] = 1;
vis[tmp] = 1;
}
}
cout << res<< endl;
return 0;
}