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的更多相关文章
随机推荐
- 分布式计算(三)Azkaban介绍
转载自:Azkaban学习之路 (一)Azkaban的基础介绍 目录 一.为什么需要工作流调度器 二.工作流调度实现方式 三.常见工作流调度系统 四.各种调度工具对比 五.Azkaban 与 Oozi ...
- Ubuntu下禁止自动打开U盘等设备
打开终端 禁止自动挂载: $ gsettings set org.gnome.desktop.media-handling automount false 禁止自动挂载并打开 $ gsettings ...
- luogu P2706 巧克力
题目 (第一道绿题) 有点像最大子矩阵qwq 用前缀和存图,l,r代表横向的一段区间,区间和就是a[r]-a[l-1] 然后用一个k从上到下dp...因为每次l,r变化的时候原来的k就没有用了,所以k ...
- Spring @Scheduled定时任务的fixedRate,fixedDelay,cron执行差异
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import org.sp ...
- Unity 消息管理(观察煮模式)
一.首先定义一份消息号(消息号用来标记发出的每一条消息,接收者通过注册要监听的消息号来监听相应的消息) public enum MSG_IDS { NONE = -, MSG_TEST01 = , M ...
- 在IIS上部署你的ASP.NET Core项目 (转载)
概述 与ASP.NET时代不同,ASP.NET Core不再是由IIS工作进程(w3wp.exe)托管,而是使用自托管Web服务器(Kestrel)运行,IIS则是作为反向代理的角色转发请求到Kest ...
- mybatis 反射bean规则
1,根据查询字段名,寻找 bean变量名设置,变量可为私有属性 2,根据查询字段名 set方法名,设置bean属性 此方法 为 ‘set‘+字段名,大小写忽略,即 方法set后面第一个字母可以是大小写 ...
- Codeforces Round #481 (Div. 3)
我实在是因为无聊至极来写Div3题解 感觉我主要的作用也就是翻译一下题目 第一次线上打CF的比赛,手速很重要. 这次由于所有题目都是1A,所以罚时还可以. 下面开始讲题 A.Remove Duplic ...
- 学习ML.NET(2): 使用模型进行预测
训练模型 在上一篇文章中,我们已经通过LearningPipeline训练好了一个“鸢尾花瓣预测”模型, var model = pipeline.Train<IrisData, IrisPre ...
- 【javascript】详解javaScript的深拷贝
前言: 最开始意识到深拷贝的重要性是在我使用redux的时候(react + redux), redux的机制要求在reducer中必须返回一个新的对象,而不能对原来的对象做改动,事实上,当时 ...