【Codeforces - 769D】k-Interesting Pairs Of Integers(暴力,统计,思维,数学,异或)
题干:
Vasya has the sequence consisting of n integers. Vasya consider the pair of integers x and y k-interesting, if their binary representation differs from each other exactly in k bits. For example, if k = 2, the pair of integers x = 5 and y = 3 is k-interesting, because their binary representation x=101 and y=011 differs exactly in two bits.
Vasya wants to know how many pairs of indexes (i, j) are in his sequence so that i < j and the pair of integers ai and aj is k-interesting. Your task is to help Vasya and determine this number.
Input
The first line contains two integers n and k (2 ≤ n ≤ 105, 0 ≤ k ≤ 14) — the number of integers in Vasya's sequence and the number of bits in which integers in k-interesting pair should differ.
The second line contains the sequence a1, a2, ..., an (0 ≤ ai ≤ 104), which Vasya has.
Output
Print the number of pairs (i, j) so that i < j and the pair of integers ai and aj is k-interesting.
Examples
Input
4 1
0 3 2 1
Output
4
Input
6 0
200 100 100 100 200 200
Output
6
Note
In the first test there are 4 k-interesting pairs:
- (1, 3),
- (1, 4),
- (2, 3),
- (2, 4).
In the second test k = 0. Consequently, integers in any k-interesting pair should be equal to themselves. Thus, for the second test there are 6 k-interesting pairs:
- (1, 5),
- (1, 6),
- (2, 3),
- (2, 4),
- (3, 4),
- (5, 6).
题目大意:
给你N个数的一个序列a,如果有两个数二进制中有K个位置不同,那么对应就算作一对可行解,问一共有多少对可行解。
(2 ≤ n ≤ 1e5, 0 ≤ k ≤ 14, 0 ≤ ai ≤ 1e4)
解题报告:
介绍这个题的两种写法。首先很显然看x和y有多少个位子不同,直接看x^y对应的二进制中1的个数即可。
所以第一种想法啊十分好想,预处理1~20000所有数字的1的个数,然后暴力枚举1e4以内的每个数字i和j,如果i^1的1的个数等于k,则直接统计i,j两个数的出现次数。我这里偷懒了没预处理,直接用内置函数了。这种方法复杂度O(1e8)
第二种方法就是读入完k之后,先暴力枚举所有1的个数等于k的数,打表可以发现对任意一个k,1e4以内不会超过3000个数。问题等价于求x^y=z,x和y符合的二元组个数,已知x<=1e4,z最多3000个,所以直接枚举x和z来反求y。复杂度O(3e8)左右。
AC代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define FF first
#define SS second
#define ll long long
#define pb push_back
#define pm make_pair
using namespace std;
typedef pair<int,int> PII;
const int MAX = 1<<15;
int n,k;
int dp[MAX];
vector<int> vv;
int cnt[MAX],mx;
ll ans;
int main()
{
// cout << (1<<14) << endl;cin >> n >> k;for(int x,i = 1; i<=n; i++) {scanf("%d",&x); dp[x]++; mx = max(mx,x);}if(k == 0) {for(int i = 0; i<=mx; i++) ans += 1LL*dp[i]*(dp[i]-1)/2;cout << ans << endl; return 0;}for(int i = 0; i<=mx; i++) {for(int j = i+1; j<=mx; j++) {if(__builtin_popcount(i^j) == k) {ans += 1LL*dp[i] * dp[j];}}} cout << ans << endl;return 0 ;
}
【Codeforces - 769D】k-Interesting Pairs Of Integers(暴力,统计,思维,数学,异或)相关推荐
- Codeforces Round #410 (Div. 2) D. Mike and distribution 思维+数学
链接: http://codeforces.com/contest/798/problem/D 题意: 给你两个长度为n的数列a和b,让你选n/2+1个下标,使得2*∑ai>suma,2*∑bi ...
- 【CodeForces - 467C】George and Job(dp,思维)
题干: The new ITone 6 has been released recently and George got really keen to buy it. Unfortunately, ...
- 可持久化Trie+堆优化 OR Trie树上求XOR第K大 ---- P5283 [十二省联考2019]异或粽子
题目大意 题目大意: 考虑先做个 prefix xor 前缀异或bi=⨁j=1iaj(1≤i≤n)b_i=\bigoplus_{j=1}^{i}a_j(1\leq i \leq n)bi=j=1⨁i ...
- CodeForces - 1311D Three Integers(暴力)
题目链接:点击查看 题目大意:给出三个数 a , b , c ,每次操作可以挑选任意一个数让其加一或减一,现在问最少需要操作多少次,可以使得: a 可以整除 b b 可以整除 c 题目分析:乍一看是一 ...
- CodeForces - 387D George and Interesting Graph(二分图最大匹配+暴力)
题目链接:点击查看 题目大意:给出n个点和m条边组成的有向图,现在我们需要找出一个点作为中心点,然后增加或减少边的条数以达到下面的目标: 除了中心点外,其他的每个点的入度为2且出度为2 中心点和每个点 ...
- Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)
G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...
- 【CodeForces - 761C】Dasha and Password (暴力可过,标解dp,字符串,有坑总结)
题干: After overcoming the stairs Dasha came to classes. She needed to write a password to begin her c ...
- 【Codeforces 652 C Foe Pairs 】
C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard input o ...
- Codeforces Round #188 (Div. 1) B. Ants 暴力
B. Ants Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/317/problem/B Des ...
最新文章
- OpenC 仿射变换
- vue 项目配置sass
- virtualBox NAT模式,设置虚拟机可上网,宿主机可访问虚拟机的方法
- 关于 Head First SQL 中文版
- POJ 2029 Get Many Persimmon Trees
- PAT(乙级)1014
- oracle 11g 数据库
- Zookeeper的一致性是什么情况?
- git rebase原理(转)
- 实例化Java对象_Java面向对象基础之对象实例化
- linux上TCP connection timeout的原因查找
- Django学习笔记5-url
- JVM 性能调优实战之:使用阿里开源工具 TProfiler 在海量业务代码中精确定位性能代码...
- 阿里巴巴普惠_阿里巴巴的普惠字体来了,再也不用担心版权问题了。
- html重复渐变包括,CSS3怎么实现重复线性渐变效果
- css实现单色或图片背景透明,而文字不透明的方法
- 仅改变rowspan背景_山西耗资10亿的小镇,人称长治“小周庄”,距市中心仅16公里...
- GPU渲染技术及性能优化
- 可转债历史最低最高价格统计表
- VSCode替换掉/去掉空行
热门文章
- Linux网络管理基本
- [BUGKU][CTF][PWN][2020] PWN writeup
- [密码学基础][每个信息安全博士生应该知道的52件事][Bristol Cryptography][第15篇]RSA-OAEP和ECIES的密钥生成,加密和解密
- java中sofa并发访问,云上的日子:用块存储、文件存储还是对象存储?
- html表白照片墙,【原创】【申精】用python去告白 绘制照片墙(自定义格式)
- android开源系统brvah,Android Jetpack之通用Adapter(Databinding+BRVAH)
- centos7挂载nas存储_CentOS7搭建NAS文件共享存储
- 以python程序调用的系统_python 系统调用的实例详解
- linux中常用的shell脚本,Linux常用shell脚本
- visual studio 的各个版本下载地址