ZOJ - 3870 Team Formation(异或)
题意:给定N个数,求这N个数中满足A ⊕ B > max{A, B})的AB有多少对。(A,B是N中的某两个数)
分析:
1、异或,首先想到转化为二进制。
eg:110011(A)和 1(B)--------A中从右数第三个数是0,若某个数B(eg:110,101,111,……)从左向右数第三个数为1,那么两个异或一定满足A ⊕ B > max{A, B})。
还有哪些数B能让A ⊕ B > max{A, B})呢?
根据上述,同理,从右数第四个为1的数B也符合要求。
很容易想到,将N个数排序,对于A前面的数,二分统计在1000~1111和100~111中的数,这些数B都能使A ⊕ B > max{A, B}),
不过,这太耗时间了~
2、试想,只要从右数第三个数是1的数都符合,那二进制位数为3的数当然满足从右数第三个数是1呀,因为没有前导0呀,eg:100,101,110,111
所以,只要预处理出N个数中二进制位数为3的数,个数为x,那么这些数与A一定能使能使A ⊕ B > max{A, B})。
再扩展一下,A其实是满足从右数第三个数为0的一类数,我们预处理时也统计出来,个数为y,那么x*y就是对于二进制从右数第三位中满足要求的AB对数。
对于二进制中的每一位都是同理,最后求和即可。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 30 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int w[MAXN];
int id[MAXN];
int main(){
int T;
scanf("%d", &T);
while(T--){
memset(w, 0, sizeof w);
memset(id, 0, sizeof id);
int n;
scanf("%d", &n);
while(n--){
int x;
scanf("%d", &x);
int cnt = 1;
while(x){
if(x % 2 == 0) ++id[cnt];
++cnt;
x >>= 1;
}
++w[cnt - 1];
}
LL ans = 0;
for(int i = 0; i < 32; ++i){
ans += (LL)w[i] * id[i];
}
printf("%lld\n", ans);
}
return 0;
}
ZOJ - 3870 Team Formation(异或)的更多相关文章
- 位运算 ZOJ 3870 Team Formation
题目传送门 /* 题意:找出符合 A^B > max (A, B) 的组数: 位运算:异或的性质,1^1=0, 1^0=1, 0^1=1, 0^0=0:与的性质:1^1=1, 1^0=0, 0^ ...
- Zoj 3870——Team Formation——————【技巧,规律】
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contes ...
- ZOJ 3870 Team Formation 贪心二进制
B - Team Formation Description For an upcoming progr ...
- ZOJ 3870 Team Formation 位运算 位异或用与运算做的
For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-m ...
- 费用流 ZOJ 3933 Team Formation
题目链接 题意:两个队伍,有一些边相连,问最大组对数以及最多女生数量 分析:费用流模板题,设置两个超级源点和汇点,边的容量为1,费用为男生数量.建边不能重复建边否则会T.zkw费用流在稠密图跑得快,普 ...
- ZOJ 3933 Team Formation
费用流裸题......比赛的时候少写了一句话....导致增加了很多无用的边一直在TLE #include<cstdio> #include<cstring> #include& ...
- ZOJ 3870:Team Formation(位运算&思维)
Team Formation Time Limit: 2 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...
- ZOJ3870 Team Formation
/** Author: Oliver ProblemId: ZOJ3870 Team Formation */ /* 思路 1.异或运算,使用^会爆,想到二进制: 2.我们可以试着从前往后模拟一位一位 ...
随机推荐
- 新闻网大数据实时分析可视化系统项目——3、Hadoop2.X分布式集群部署
(一)hadoop2.x版本下载及安装 Hadoop 版本选择目前主要基于三个厂商(国外)如下所示: 1.基于Apache厂商的最原始的hadoop版本, 所有发行版均基于这个版本进行改进. 2.基于 ...
- Android 隐藏手机号中间四位和隐藏邮箱地址中间四位
/** * 手机号用****号隐藏中间数字 * * @param phone * @return */public static String settingphone(String phone) { ...
- Redis的两个典型应用场景
Redis简介 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化.除此之外,Red ...
- PyCharm 2018.1破解激活步骤
1.下载破解补丁 下载地址:https://pan.baidu.com/s/1qjI9uHaw0x374rwu6H8djA 将下载下来的破解补丁放到C:\software\JetBrains\PyCh ...
- spingboot中使用scheduled设置定时任务注意事项
在spring开发过程中经常会遇到需要定时执行的任务,如定时生成报表,定时推送消息等任务. springboot 提供了简单的 @Scheduled 表达式来配置定时任务.该方式默认是单线程的,任务在 ...
- JDK8中的ConcurrentHashMap源码
背景 上文JDK8中的HashMap源码写了HashMap,这次写ConcurrentHashMap ConcurrentHashMap源码 /** * Maps the specified key ...
- ssh与scp
ssh命令用于linux 的远程登录,其默认端口为:22,在工作中常常会修改掉这一默认端口的值. scp命令用于远程的文件相互拷贝. scp远程文件拷贝: [root@jerry a]#ls ...
- 在html中创建自定义标签
创建并使用自定义标签 Web Components 标准非常重要的一个特性是,它使开发者能够将HTML页面的功能封装为 custom elements(自定义标签),本篇介绍使用 CustomElem ...
- jenkins#构建并部署springboot的jar包
0. 前提是有一个可以用的没有问题的Jenkins环境,这是基础 1. 安装publish over ssh 插件,(如果网速太慢,请去github 克隆代码,然后自己构建,然后上传安装此插件) 2. ...
- 洛谷 P3801 红色的幻想乡
题目背景 蕾米莉亚的红雾异变失败后,很不甘心. 题目描述 经过上次失败后,蕾米莉亚决定再次发动红雾异变,但为了防止被灵梦退治,她决定将红雾以奇怪的阵势释放. 我们将幻想乡看做是一个n*m的方格地区,一 ...