ZOJ - 3870 Team Formation(异或)
2024-09-04 05:04:08
题意:给定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;
}
最新文章
- [System] CentOS虚拟机系统克隆后的网络配置
- 03.JavaScript 面向对象精要--理解对象
- Python特殊语法--filter、map、reduce、lambda
- lucene-查询query->;PhraseQuery多关键字的搜索
- [译]Bundling and Minification
- 洛谷 P1433 吃奶酪 Label:dfs &;&; 剪枝Ex
- 如何提高码农产量,基于ASP.NET MVC的敏捷开发框架开发随笔一
- 对话框上右下角显示resize icon(可以拖动改变对话框的大小)(在WM_CREATE的时候,增加WS_THICKFRAME风格)
- 使用plspl创建orcale作业
- table+js实现网站左侧列表下拉隐藏
- Day3 - Linux系统安装_Centos6.9
- BZOJ 1012: [JSOI2008]最大数maxnumber【线段树单点更新求最值,单调队列,多解】
- Nginx+uwsgi部署django
- SSM框架中写sql在dao文件中以注解的方式
- 解决TextView drawableRight左侧图片大小不可控的问题
- 遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗? 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现! 是否有一个全局视角来查看系统的运行状况? 有什么办法可以监控到JVM的实时运行状态?
- VMware虚拟机的三种联网方法及原理(转)
- DOM之一些小实验demo
- spring boot 2.x拦截器导致静态资源404终极解决办法
- 【LA3415 训练指南】保守的老师 【二分图最大独立集,最小割】
热门文章
- day12-Python运维开发基础(推导式、生成器与生成器函数)
- Django: 页面设计,实现验证码刷新
- Address localhost:1099 is already in use(IDEA启动Tomcat报错1099 is already in use)
- 088、Java中String类之对象直接赋值
- struts2令牌(token)内部原理
- mysql explain参数解析
- 三 SVN权限设置&;用户&;组
- 一、Linux&;配置,依赖安装&;Tomcat,Mysql,jdk安装
- Jquery选择器大全汇总
- POJ 3349:Snowflake Snow Snowflakes 六片雪花找相同的 哈希