zoukankan      html  css  js  c++  java
  • codeforces742B

    Arpa’s obvious problem and Mehrdad’s terrible solution

     CodeForces - 742B 

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

    sol:询问有多少对i,j满足ai^aj = X,这就是Trie树可以轻松支持的,只要注意下X=0时的情况就可以了

    Ps:答案会爆int

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    inline ll read()
    {
        ll s=0;
        bool f=0;
        char ch=' ';
        while(!isdigit(ch))
        {
            f|=(ch=='-'); ch=getchar();
        }
        while(isdigit(ch))
        {
            s=(s<<3)+(s<<1)+(ch^48); ch=getchar();
        }
        return (f)?(-s):(s);
    }
    #define R(x) x=read()
    inline void write(ll x)
    {
        if(x<0)
        {
            putchar('-'); x=-x;
        }
        if(x<10)
        {
            putchar(x+'0');    return;
        }
        write(x/10);
        putchar((x%10)+'0');
        return;
    }
    #define W(x) write(x),putchar(' ')
    #define Wl(x) write(x),putchar('
    ')
    const int N=100005;
    int n,Num,a[N];
    struct Zidianshu
    {
        int Trie[N*25][25],cnt;
        int End[N*25];
        inline void Init()
        {
            cnt=0;
        }
        inline void Insert(int Num)
        {
            int i,Root=0;
            for(i=0;i<=23;i++)
            {
                int oo=(Num&(1<<i))>>i;
                if(!Trie[Root][oo]) Trie[Root][oo]=++cnt;
                Root=Trie[Root][oo];
            }
            End[Root]++;
        }
        inline int Query(int Num,int Want)
        {
            int i,Root=0;
            for(i=0;i<=23;i++)
            {
                int oo=((Want&(1<<i))>>i)^((Num&(1<<i))>>i);
                if(!Trie[Root][oo]) return 0;
                Root=Trie[Root][oo];
            }
            return End[Root];
        }
    }Trie;
    int main()
    {
        Trie.Init();
        int i;
        long long ans=0;
        R(n); R(Num);
        for(i=1;i<=n;i++)
        {
            a[i]=read();
            ans+=1ll*Trie.Query(a[i],Num);
            Trie.Insert(a[i]);
        }
        Wl(ans);
        return 0;
    }
    /*
    input
    2 3
    1 2
    output
    1
    
    input
    6 1
    5 1 2 3 4 1
    output
    2
    
    input
    10 0
    1 2 1 2 1 2 1 2 1 2
    output
    20
    */
    View Code
  • 相关阅读:
    HashMap底层实现原理/HashMap与HashTable区别/HashMap与HashSet区别(转)
    JSP语法
    Web开发基础(读书笔记)
    eclispe新导入的文件有个小红叉号(x)的问题
    Vue处理数据,数组更新,但视图无法得到及时更新
    VUE 利用tab切换+同路由跳转传参(check)+vant上拉加载制作订单列表(终)
    适配方案一之:利用rem和less计算制作移动端适配页面
    Git常用命令务忘
    git提交代码步骤笔记
    Vue仿淘宝订单状态的tab切换效果——(但现实中不会用此种方式进行存储数据)
  • 原文地址:https://www.cnblogs.com/gaojunonly1/p/10629371.html
Copyright © 2011-2022 走看看