CodeForces - 742B
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.
Example
Input
2 3
1 2
Output
1
Input
6 1
5 1 2 3 4 1
Output
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 xor operation 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.
题目的意思:
给出n和m,n是接下来输入数字的数量,m是下面一行数字取出两个进行某种位运算后的数值,求出这样的对数的个数。某种位运算指 相同为0,不同为1
如1-> 0001,2->0010. 1?2=0011,即3,有一对,输出1.
补了一下基础知识,位运算,桶排序。
& 按位与 只有对应的两个二进位均为1时,结果位才为1 ,否则为0
| 按位或 参与运算的两数各对应的二进位相或。只要对应的二个二进位有一个为1 时,结果位就为1
^ 按位异或 参与运算的两数各对应的二进位相异或,当两对应的二进位相异时,结 果为1
~按位取反 1变0,0变1.
很明显上面的某种运算是指按位异或,然而直接暴力会超时。
所以要用到按位运算的一些式子
异或满足交换律:a^b^c=a^c^b。
还有 a^b=c,b=a^c
下面是代码
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<map>
using namespace std;
#define maxn 100005
long long int s[maxn];
long long int arr[maxn * 10];
int main()
{long long n, x;long long sum, cnt;while (cin>>n>>x){sum = 0;cnt = 0;memset(arr, 0, sizeof(arr));for (int i = 0; i<n; i++){scanf("%lld", &s[i]);arr[s[i]]++;}for (int i = 0; i<n; i++){cnt = x^s[i];if (cnt == s[i])sum += arr[cnt] - 1;elsesum += arr[cnt];}cout << sum / 2 << endl;}return 0;
}
CodeForces - 742B相关推荐
- Codeforces 742B Arpa’s obvious problem and Mehrdad’s terrible solution
http://codeforces.com/contest/742/problem/B B. Arpa's obvious problem and Mehrdad's terrible solutio ...
- Codeforces 742B B. Arpa’s obvious problem and Mehrdad’s terrible solution
B. Arpa's obvious problem and Mehrdad's terrible solution time limit per test 1 second memory limit ...
- CodeForces 375D Tree and Queries
传送门:https://codeforces.com/problemset/problem/375/D 题意: 给你一颗有根树,树上每个节点都有其对应的颜色,有m次询问,每次问你以点v为父节点的子树内 ...
- 「日常训练」Bad Luck Island(Codeforces Round 301 Div.2 D)
题意与分析(CodeForces 540D) 是一道概率dp题. 不过我没把它当dp做... 我就是凭着概率的直觉写的,还好这题不算难. 这题的重点在于考虑概率:他们喜相逢的概率是多少?考虑超几何分布 ...
- 【codeforces 812C】Sagheer and Nubian Market
[题目链接]:http://codeforces.com/contest/812/problem/C [题意] 给你n个物品; 你可以选购k个物品;则 每个物品有一个基础价值; 然后还有一个附加价值; ...
- CodeForces 获得数据
针对程序的输出可以看见 CodeForces :当输入.输出超过一定字符,会隐藏内容 所以:分若干个程序进行输入数据的获取 1. 1 for (i=1;i<=q;i++) 2 { 3 scanf ...
- codeforces水题100道 第二十七题 Codeforces Round #172 (Div. 2) A. Word Capitalization (strings)...
题目链接:http://www.codeforces.com/problemset/problem/281/A 题意:将一个英文字母的首字母变成大写,然后输出. C++代码: #include < ...
- CodeForces 595A
题目链接: http://codeforces.com/problemset/problem/595/A 题意: 一栋楼,有n层,每层有m户,每户有2个窗户,问这栋楼还有多少户没有睡觉(只要一个窗户灯 ...
- codeforces A. Jeff and Digits 解题报告
题目链接:http://codeforces.com/problemset/problem/352/A 题目意思:给定一个只有0或5组成的序列,你要重新编排这个序列(当然你可以不取尽这些数字),使得这 ...
最新文章
- David与Vincent的博弈游戏[树型DP]
- 洛谷 P1008 三连击 Label:水
- 远程代理模式-Remote Proxy(Java实现)
- 写文件 —— 将内容按照指定格式写入配置文件(fprintf()函数-》》本机的监听地址列表中port值)
- springboot设置运行内存_docker run容器 设置 jvm 运行springboot 程序
- 第三届 Apache Flink 极客挑战赛暨 AAIG CUP 报名开始!
- 十六、python沉淀之路--迭代器
- Linux -- 进程或线程独占CPU
- g++ linux 编译开栈_方舟编译器编译hello world踩坑全记录
- 作者:杨燕(1964-),女,西南交通大学信息科学与技术学院教授、博士生导师。...
- 一个apply的实例
- iOS-获取通讯录信息
- IE下常见兼容性问题
- Firefox控制台日志转入文件
- 新浪微博API使用入门:申请应用、授权、使用官方java版本SDK
- 制度罚则-- 代码走查规范
- 郑州园博园“私房照”曝光,8月试运营对市民免费开放!这可是咱郑州人家门口的“苏州园林”!...
- 共享计算机扫描,windows系统下怎么共享扫描仪?
- codelite交叉编译动态库学习记录
- 对中国国家气象局进行api数据分析
热门文章
- 本地连接gitlab远程仓库
- 笔记本电脑进水后的正确处理方法
- web前端大一实训-HTML5+CSS大作业——节日圣诞节(5页)节日带背景音乐带视频(5页)带登录
- Android Studio 安卓手机上实现火柴人动画(Java源代码—Python)
- mysql 计算时间相差
- C# 百度翻译机翻英文字幕
- 携手商机信息网,实现线上销售新突破
- Android 打开相册选择图片及相册图片返回路径获取
- inputstream读取数据不全
- css显示文字时怎么加空格,css的text-align-last属性,以及IE下text-align-last的文字间要加空格...