Arpa’s obvious problem and Mehrdad’s terrible solution 思维

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.

Example

Input

2 31 2

Output

1

Input

6 15 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.

这道题一开始没看懂啥意思,后来才明白原来是异或^,a^b=c,那么a^c=b;抓住这一点就好做了,所有的ai去异或x得到cnt,然后记录cnt,看看数组里面有几个cnt就好了,可以开个map,注意结果可能超过long 要用long long

代码:

#include <iostream>
#include <cstdio>
#include <map>
using namespace std;
int n,x,a[100000];
map<int,int> mark;
void input()
{
    cin>>n>>x;
    for(int i=0;i<n;i++)
    {
        cin>>a[i];
        mark[a[i]]++;
    }
}
long long check()
{
    long long cnt,ans=0;
    for(int i=0;i<n;i++)
    {
        cnt=a[i]^x;
        ///x如果是0 那么 任意一个数q q^0=q;
        if(a[i]==cnt)ans+=mark[cnt]-1;
        else ans+=mark[cnt];
    }
    return ans/2;
}
int main()
{
    input();
    cout<<check();
}
时间: 2024-11-09 09:04:06

Arpa’s obvious problem and Mehrdad’s terrible solution 思维的相关文章

CodeForces 742B Arpa’s obvious problem and Mehrdad’s terrible solution (暴力枚举)

题意:求定 n 个数,求有多少对数满足,ai^bi = x. 析:暴力枚举就行,n的复杂度. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <c

CodeForces - 742B Arpa’s obvious problem and Mehrdad’s terrible solution

假期训练的一道题,用了一些异或的一些性质1^2=3,3^1=2,3^2=1 就是相当于反向异或运算然后查找个数. 提供一组样例 5 0 1 1 1 1 1 这就是用long long的原因 下面是代码 1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 1e6+5; 4 typedef long long ll; 5 int b[maxn], n, x; 6 7 int main() 8 { 9 while(~

http://codeforces.com/contest/741/problem/B B. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses

题意: 给出上限体重W 然后还给出每个人的体重wi 和 魅力值 bi 互为伙伴的对(xi, yi) 可以凑成group 思路: 并查集找出所有的group 暴力背包 对于每一个group 要选出这一组内选一个人时的最优结果, 如果所有人的体重和小于等于W,还得考虑选所有人的情况 #include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> #include &l

D. Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses 分组背包模板题

http://codeforces.com/problemset/problem/742/D 并查集预处理出所有关系. 一开始的时候,我预处理所有关系后,然后选择全部的时候,另起了一个for,然后再判断. 这样是不对的.因为这样使得同一组里面可能选择了两次. 3 0 2 1 2 3 1 1 3 #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include &

Codeforces 741B Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses

[题目链接] http://codeforces.com/problemset/problem/741/B [题目大意] 给出一张图,所有连通块构成分组,每个点有价值和代价, 要么选择整个连通块,要么只能在连通块中选择一个,或者不选,为最大价值 [题解] 首先我们用并查集求出连通块,然后对连通块进行分组背包即可. [代码] #include <cstdio> #include <vector> #include <algorithm> #include <cstr

Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses CodeForces - 742D

Just to remind, girls in Arpa's land are really nice. Mehrdad wants to invite some Hoses to the palace for a dancing party. Each Hos has some weight wi and some beauty bi. Also each Hos may have some friends. Hoses are divided in some friendship grou

cfodeforces 741D Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths

题目链接:Arpa's letter-marked tree and Mehrdad's Dokhtar-kosh paths 第一次写\(dsu\ on\ tree\),来记录一下 \(dsu\ on\ tree\)主要维护子树信息,往往可以省掉一个数据结构的启发式合并.大体思路如下: 轻重链路径剖分之后,对每个点先递归处理他的所有轻儿子,每次处理完轻儿子之后把这棵子树的信息清空.最后再来处理重孩子,重儿子的信息就可以不用清空了.由于我们是用一个全局数组来记录信息的,重儿子子树的信息就仍然保留

codeforces 742D Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses ——(01背包变形)

题意:给你若干个集合,每个集合内的物品要么选任意一个,要么所有都选,求最后在背包能容纳的范围下最大的价值. 分析:对于每个并查集,从上到下滚动维护即可,其实就是一个01背包= =. 代码如下: 1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 #include <vector> 5 using namespace std; 6 const int N = 1000 + 5;

并查集+背包 【CF741B】 Arpa&#39;s weak amphitheater and Mehrdad&#39;s valuable Hoses

Descirption 有n个人,每个人都有颜值bi与体重wi.剧场的容量为W.有m条关系,xi与yi表示xi和yi是好朋友,在一个小组. 每个小组要么全部参加舞会,要么参加人数不能超过1人. 问保证总重量不超过W,剧场中的颜值最大能到多少? Input 第一行,三个整数n,m,w 第二行,n个整数w1,w2,...,wn 第三行,n个整数b1,b2,...,bn 接下来m行,每行表示一个关系,第i行有两个整数xi和yi. 每一组朋友关系都是不同的. Output ?一行,表示最大的魅力值总和.