美文网首页
CUC-SUMMER-CONTEST-2-C

CUC-SUMMER-CONTEST-2-C

作者: Nioge | 来源:发表于2017-08-18 19:06 被阅读0次
    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;
    }
    

    相关文章

      网友评论

          本文标题:CUC-SUMMER-CONTEST-2-C

          本文链接:https://www.haomeiwen.com/subject/ttzarxtx.html