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的更多相关文章
随机推荐
- 洛谷 P2256 一中校运会之百米跑
题目链接 https://www.luogu.org/problemnew/show/P2256 题目背景 在一大堆秀恩爱的**之中,来不及秀恩爱的苏大学神踏着坚定(?)的步伐走向了100米跑的起点. ...
- MYSQL表情存储数据库报错
1.windows安装5.5.5.9的服务器 2.打开window服务找到mysql的服务,右键属性查看路径 3.打开路径,修改mysql配置文件my.ini 4.修改[client]中的defaul ...
- AngularJs的ng-include的使用与实现
想在angularjs动态加载一个内容,我们可以使用ng-include来实现. 今天Insus.NET就在ASP.NET MVC环境中,举个例子来演示它的功能. 你可以在一个视图动态加载任一其它视图 ...
- WPF中的Bitmap与byte
原文:WPF中的Bitmap与byte public MainWindow() { InitializeComponent(); byte[] b = GetPictureData(@"F: ...
- 利用Costura.Fody制作绿色单文件程序(C#程序(含多个Dll)合并成一个Exe)
原文:利用Costura.Fody制作绿色单文件程序(C#程序(含多个Dll)合并成一个Exe) 开发程序的时候经常会引用一些第三方的DLL,然后编译生成的exe文件就不能脱离这些DLL独立运行了.这 ...
- BootStrap学习(6)_模态框
一.模态框 模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等. 如果只使用该功能,只引入BootSt ...
- 【Qt】窗口居中显示
w.move((a.desktop()->width() - w.width())/, (a.desktop()->height() - w.height())/); 上述方法可以置中,但 ...
- BZOJ 3561 DZY Loves Math VI
BZOJ 3561 DZY Loves Math VI 求\(\sum_{i=1}^{n}\sum_{j=1}^{m}\text{lcm}(i,j)^{\gcd(i,j)}\),钦定\(n\leq m ...
- ngx_lua 模块
ngx_lua模块的原理: 1.每个worker(工作进程)创建一个Lua VM,worker内所有协程共享VM:2.将Nginx I/O原语封装后注入 Lua VM,允许Lua代码直接访问:3.每个 ...
- linux 与 windows 挖门罗币总结
比特币之前一直很火,初次了解的时候才2000RMB一枚..看不懂哇,错失良机...当然了,看得懂也不买不起..当时还是穷学生. 最近又一直看到黑客利用linux漏洞挖门罗币获利的新闻,决定好生研究一下 ...