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的更多相关文章
随机推荐
- 数据库隔离级别深入理解(ORACLE)
TRANSACTION_READ_UNCOMMITTED 1 这种隔离级别最低,脏读,不可重复读,幻读都会发生,我用的oracle,并没有支持这个级别,不作研究. TRANSACTION_READ_C ...
- B Long Path
有n+1个房间.从1-n个房间.每个房间有两扇门.一扇去i+1的房间另一扇去编号为pi的房间. 起点为1,终点为n+1,对应第i个各点,如果我奇数次到达,那么下一步走到a[i]的位子,如果是偶数次到达 ...
- 【数据库摘要】10_Sql_Create_Index
CREATE INDEX 语句 CREATE INDEX 语句用于在表中创建索引. 在不读取整个表的情况下.索引使数据库应用程序能够更快地查找数据. 索引 您能够在表中创建索引,以便更加高速高效地查询 ...
- 马加爵遗书 VS 药家鑫遗书
前言:今天是贰零壹柒年最后一个工作日,亦是2017年12月29日,因为明天就放元旦假了,公司同事比往常相对轻松些.中午吃完午饭,在办公室大家有说有笑,有人说姓马的人都挺牛X啊,比如:马云, ...
- Nginx 反向代理 上传大文件报 413
Nginx 中上传文件限制是 2m,上传太大就报错,配置一下 client_max_body_size 1024m; 就可以上传 1G 大小文件 添加在 location 中,如果是反向代理就添加在反 ...
- Luogu P1447 [NOI2010]能量采集
Preface 最近反演题做多了看什么都想反演.这道题由于数据弱,解法多种多样,这里简单分析一下. 首先转化下题目就是对于一个点\((x,y)\),所消耗的能量就是\(2(\gcd(x,y)-1)+1 ...
- [UWP 自定义控件]了解模板化控件(4):TemplatePart
1. TemplatePart TemplatePart(部件)是指ControlTemplate中的命名元素.控件逻辑预期这些部分存在于ControlTemplate中,并且使用protected ...
- Steamworks上传游戏
1.在steamPipe下配置Depot,每个Depot表示程序对应的分支配置语言,操作系统,架构组合等 2.安装,启动项目是配置游戏启动文件的相关信息,不同的操作系统架构等需要添加不同的启动项 3. ...
- 手动编写的几个简单的puppet管理配置
puppet在自动化配置管理方面有很强大的优势,这里就不做过多介绍了,下面记录下几个简单的puppet管理配置: 一.首先在服务端和客户端安装puppet和facter 1)服务端 安装Puppet ...
- [Android]记录一次处理app:transformDexArchiveWithExternalLibsDexMergerForDebug错误
第一种情况: Android 目录结构如下: app中build.gradle包含: implementation 'com.squareup.okhttp3:okhttp:3.6.0' implem ...