codeforces742B
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.
Examples
2 3
1 2
1
6 1
5 1 2 3 4 1
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=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
int n,Num,a[N];
struct Zidianshu
{
int Trie[N*][],cnt;
int End[N*];
inline void Init()
{
cnt=;
}
inline void Insert(int Num)
{
int i,Root=;
for(i=;i<=;i++)
{
int oo=(Num&(<<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=;
for(i=;i<=;i++)
{
int oo=((Want&(<<i))>>i)^((Num&(<<i))>>i);
if(!Trie[Root][oo]) return ;
Root=Trie[Root][oo];
}
return End[Root];
}
}Trie;
int main()
{
Trie.Init();
int i;
long long ans=;
R(n); R(Num);
for(i=;i<=n;i++)
{
a[i]=read();
ans+=1ll*Trie.Query(a[i],Num);
Trie.Insert(a[i]);
}
Wl(ans);
return ;
}
/*
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
*/
codeforces742B的更多相关文章
随机推荐
- 转载:(原创)odoo11配置邮件功能的那些事儿
https://www.cnblogs.com/goyier/p/9246001.html 关于odoo的邮件服务器的配置,百度的结果是众说纷纭,但是都没有能够清楚的说明,odoo系统的邮件功能,仅仅 ...
- 【SQL】四种排序开窗函数
一 .简单了解什么是开窗函数 什么是开窗函数,开窗函数有什么作用,特征是什么? 所谓开窗函数就是定义一个行为列,简单讲,就是在你查询的结果上,直接多出一列值(可以是聚合值或是排序号),特征就是带有ov ...
- [React]全自动数据表格组件——BodeGrid
表格是在后台管理系统中用的最频繁的组件之一,相关的功能有数据的新增和编辑.查询.排序.分页.自定义显示以及一些操作按钮.我们逐一深入进行探讨以及介绍我的设计思路: 新增和编辑 想想我们最开始写新增 ...
- Object-Oriented(一)创建对象
自用备忘笔记 前言 虽然可以使用 Object 和对象字面量创建对象,但是如果要创建大量相似的对象又显得麻烦.为解决这个问题,人们开始使用工厂模式的变种. 工厂模式 function person(n ...
- nginx的web缓存服务环境部署记录
web缓存位于内容源Web服务器和客户端之间,当用户访问一个URL时,Web缓存服务器会去后端Web源服务器取回要输出的内容,然后,当下一个请求到来时,如果访问的是相同的URL,Web缓存服务器直接输 ...
- Gerrit日常维护记录
Gerrit代码审核工具是个好东西,尤其是在和Gitlab和Jenkins对接后,在代码控制方面有着无与伦比的优势. 在公司线上部署了一套Gerrit系统,在日常运维中,使用了很多gerrit命令,在 ...
- python基础学习笔记(六)
学到这里已经很不耐烦了,前面的数据结构什么的看起来都挺好,但还是没法用它们做什么实际的事. 基本语句的更多用法 使用逗号输出 >>> print 'age:',25 age: 25 ...
- Visual Studio2013的安装过程及练习测试
一.安装环境: 支持安装的操作系统版本:Windows XP,Windows7,Windows8,Windows10. CPU大小:Intel(R)Core(TM)i5-4210U CPU @1.7G ...
- Linux内核第六节 20135332武西垚
如何描述一个进程:进程描述符的数据结构: 如何创建一个进程:内核是如何执行的,以及新创建的进程从哪里开始执行: 使用gdb跟踪新进程的创建过程. 进程的描述 操作系统三大功能: 进程管理(最核心最基础 ...
- <<浪潮之巅>>阅读笔记三
纵看世界,横看国内.我们国内也有很多很优秀的企业正在走向或者已经处于浪潮之巅.阿里巴巴.腾讯和百度这三巨头应该是我们计算机行业的龙头.但是 不得不说,在创新方面我们做的并不多,这是值得每一个从事计算机 ...