CUC-SUMMER-CONTEST-2-C

C - Arpa’s obvious problem and Mehrdad’s terrible solution
Codeforces Round #383 (Div. 2)

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 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 xoroperation 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.


题意:给你一个序列和一个数x,求这个序列中有多少个数对的异或结果为x

解法:数列中的数有重复的,而且暴力会超时,已知xy=z,则xz=y,所以只要判断一个数与目标数的异或结果在不在序列中即可

代码:

#include<iostream>
using namespace std;
int a[100010];
int main()
{
    int n,x,t;
    long long cnt=0;
    cin>>n>>x;
    for(int i=0;i<n;i++){
        cin>>t;
        if(a[x^t])
            cnt+=a[x^t];
        a[t]++;
    }   
    cout<<cnt<<endl;
    return 0;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,921评论 0 33
  • O 今天到娘娘新调部门的办公室串门儿,见识和收获了不少宝贝。最亮眼的是娘娘的“办公桌花园”,迷你小花架上摆着很多小...
    葳葳一笑浥清清阅读 1,013评论 2 3
  • 古人云:腹有诗书气自华,这里的“气”,就是气质。气质本身是中性的,有很多不同的表现。 一个人的精神状态,是可以从气...
    花脸喵阅读 280评论 2 7
  • 以终为始,我觉得刘润的声音很好听,以前好像一直是听说以始为终, 讲了,目标 原则 计划 举例盖大楼就是按照这三个原则
    Fineyoga瑾璟阅读 176评论 0 0
  • 世人皆醒我独醉 晨风入梦酒力微 津口迷途归何处 此生蒙昧终有悔
    星尘梦羽阅读 403评论 0 8

友情链接更多精彩内容