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的更多相关文章
随机推荐
- odoo学习之弹框显示
按条件隐藏: <xpath expr="//group[1]" position="attributes"> <attribute name= ...
- Vue-详解设置路由导航的两种方法: <router-link :to="..."> 和router.push(...)
一.<router-link :to="..."> to里的值可以是一个字符串路径,或者一个描述地址的对象.例如: // 字符串 <router-link to= ...
- WPF中反转3D列表项
原文:WPF中反转3D列表项 WPF中反转3D列表项 周银辉记得在苹果电脑中有一个很酷的 ...
- 线程中join()的用法
Thread中,join()方法的作用是调用线程等待该线程完成后,才能继续用下运行. public static void main(String[] args) throws Interrupted ...
- 换了电脑如何使用hexo继续写博客
前言 我们知道,使用 Github+hexo 搭建一个个人博客确实需要花不少时间的,我们搭好博客后使用的挺好,但是如果我们有一天电脑突然坏了,或者换了系统,那么我们怎么使用 hexo 再发布文章到个人 ...
- Elasticsearch Query DSL 整理总结(四)—— Multi Match Query
目录 引言 概要 fields 字段 通配符 提升字段权重 multi_match查询的类型 best_fields 类型 dis_max 分离最大化查询 best_fields 维权使者 tie_b ...
- Elasticsearch5.5.1插件开发指南
Elasticsearch5.5.1插件开发指南 原文地址: https://www.elastic.co/guide/en/elasticsearch/plugins/5.5/plugin-auth ...
- Scrum与看板区别
看板:在制品(work-in-progress, WIP)必须被限制 WIP上限和拉动式生产 1. Scrum与看板简述 Scrum:组织拆分,工作拆分,开发时间拆分,优化发布计划,过程优化 看板 ...
- Python数据类型-7
什么数据类型. int 1,2,3用于计算. bool:True,False,用户判断. str:存储少量数据,进行操作 'fjdsal' '二哥','`13243','fdshklj' '战三,李四 ...
- apacheTomcat
Window+R ------>cmd || Window PowerShell apacheTomcat\bin> ./startup.sh