cf769D(枚举&位或运算)
题目链接:http://codeforces.com/problemset/problem/769/D
题意:求给出的 n 个数中有多少对数字的二进制形式恰好有 k 位不同
思路:两个数a, b的二进制形式恰好 k 位不同即 a ^ b 中 1 的个数,那么可以枚举.注意 n 为 1e5 枚举 ai 会tle,不过 ai 的范围是 1e4,显然有大量重复的元素;
可以先去重并记录每个元素出现的次数,再枚举每个元素即可;
代码:
#include <iostream>
#include <stdio.h>
#define ll long long
using namespace std; const int MAXN = 1e5 + ;
ll a[MAXN], vis[MAXN]; int BitCount(unsigned int n){//求 n 二进制中 1 的数目
unsigned int tmp = n - ((n >> ) & ) - ((n >> ) & );
return ((tmp + (tmp >> )) & ) % ;
} int main(void){
int n, k, x, indx = ;
ll ans = ;
scanf("%d%d", &n, &k);
for(int i = ; i < n; i++){
scanf("%d", &x);
if(!vis[x]) a[indx++] = x;
vis[x]++;
}
for(int i = ; i < indx; i++){
if(k == ){
ans += (vis[a[i]] - ) * vis[a[i]] / ;
continue;
}
for(int j = i + ; j < indx; j++){
int cnt = a[i] ^ a[j];
if(k == BitCount(cnt)) ans += vis[a[i]] * vis[a[j]];
}
}
printf("%lld\n", ans);
return ;
}
cf769D(枚举&位或运算)的更多相关文章
- Codeforces Round #344 (Div. 2)(按位或运算)
Blake is a CEO of a large company called "Blake Technologies". He loves his company very m ...
- Swift学习笔记 - 位移枚举的按位或运算
在OC里面我们经常遇到一些枚举值可以多选的,需要用或运算来把这些枚举值链接起来,这样的我们称为位移枚举,但是在swift里面却不能这么做,下面来讲解一下如何在swift里面使用 OC的位移枚举的区分 ...
- poj 1950 Dessert(dfs枚举,模拟运算过程)
/* 这个代码运行的时间长主要是因为每次枚举之后都要重新计算一下和的值! 如果要快的话,应该在dfs,也就是枚举的过程中计算出前边的数值(这种方法见第二个代码),直到最后,这样不必每一次枚举都要从头再 ...
- POJ 2436 二进制枚举+位运算
题意:给出n头牛的得病的种类情况,一共有m种病,要求找出最多有K种病的牛的数目: 思路:二进制枚举(得病处为1,否则为0,比如得了2 1两种病,代号就是011(十进制就是3)),首先枚举出1的个数等于 ...
- c#枚举位运算操作
抛出预设问题 需要有一个npc需要在一周中的,周一,周二,周三会出现,其他时间不可见 解决问题 因为一周时间是固定的,所以创建枚举类型比较合适,如下 enum Days { None, Sunday, ...
- 深入V8引擎-枚举+位运算实现参数配置
不知不觉都快月底了,看了看上一篇还是6号写的,惭愧惭愧,说好的坚持.为了证明没有偷懒(其实还是沉迷了一会dota2),先上一个图自证清白. 基本上从初始化引擎,到Isolate.handleScope ...
- [poj]开关类问题 枚举 位运算
poj 1222 EXTENDED LIGHTS OUT 开关只有两种方案 按和不按,按两次相当于关 只用枚举第一排开关的按法即可,剩下的行为使上一排的灯全部关闭,按法可以确定,并且是唯一的. 最后 ...
- BZOJ 1647 [Usaco2007 Open]Fliptile 翻格子游戏:部分枚举 位运算
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1647 题意: 在一个n*m(1 <= n,m <= 15)的棋盘上,每一个格子 ...
- triples I(按位或运算及3的特性)(2019牛客暑期多校训练营(第四场)D)
示例1: 输入: 2 3 7 输出: 1 32 3 6 说明:3=3, (3|6)=7 题意:输出尽可能少的数字,他们的按位或结果为输入的数字a. 题解:(表示看不懂题解,看山东大佬的代码看懂的)首先 ...
随机推荐
- wifi debug command
==================================================================================================== ...
- 利用framebuffer,命令行显示图片
上代码 import fcntl import struct import mmap import contextlib import os import time import numpy as n ...
- RDLC报表-分组序号
1.RowNumber("group_name")是按行每个分组重新1,2,3这样来显示的,如果需要按每个分组来显示1,并且递增,则需要通过自定义代码来控制,在报表-属性-代码里插 ...
- Carthage的使用
1.安装Carthage https://github.com/Carthage/Carthage/releases 2.进入Cartfile文件所在的目录地址 cd 拖入文件Cartfile,把最后 ...
- leetcode 859. Buddy Strings
Given two strings A and B of lowercase letters, return true if and only if we can swap two letters i ...
- P5111 zhtobu3232的线段树
P5111 zhtobu3232的线段树 维护左子树右子树的贡献和跨区间贡献 #include<bits/stdc++.h> using namespace std; typedef lo ...
- the art of seo(chapter eight)
How Social Media and User Data Play a Role in Search Results and Rankings ***Correlation Between Soc ...
- ES 搜索结果expalain 可以类似数据库性能调优来看排序算法的选择
When we run a simple term query with explain set to true (see Understanding the Score), you will see ...
- 集群 openfire
openfire_3.8.2集群配置 测试机4台1.四台机器都安装openfire,随即一台安装mysql,执行openfire_mysql.sql脚本.2.四台机器都配置到同一个mysql机器上(不 ...
- java.sql.SQLException: Access denied for user 'Administrator'@'localhost' (using password: YES)
早上在做MyBatis+Spring整合的时候爆了个奇葩的bug: 十月 19, 2017 11:18:11 上午 org.springframework.context.support.Abstra ...