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.我们可以试着从前往后模拟一位一位 ...
随机推荐
- linux修改键盘按键
我的电脑:Fedora-19 $ uname -a Linux localhost.localdomain 3.11.10-200.fc19.i686 #1 SMP Mon Dec 2 20:48:2 ...
- SpringCloud学习之Zuul路由转发、拦截和熔断处理(七)
Spring Cloud Zuul 服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能. Sp ...
- php 实现店铺装修8
/** * @title 店铺装修--根据分类获取商品列表 * @param source 是 int 来源(1--h5.2--app) * @param type 是 string 店铺类型--首页 ...
- Centos7 下vmware NAT模式配置网络连接与DNS
NAT模式配置网络 1.首先查看NAT模式下的网络 从这边可以知道我的vmware下的nat模式的网络是192.168.109.*网段 上图这个网段也可以修改为别的网段 2.NAT模式下的网关 3.配 ...
- mysql 单表索引优化
建表语句 CREATE TABLE IF NOT EXISTS `article` ( `id` INT(10) UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMEN ...
- MQTT 协议学习: 总结 与 各种定义的速查表
背景 经过几天的学习与实操,对于MQTT(主要针对 v3.1.1版本)的学习告一段落,为了方便日后的查阅 本文链接:<MQTT 协议学习: 总结 与 各种定义的速查表> 章节整理 MQTT ...
- PE文件结构体-IMAGE_SECTION_HEADER
在PE文件头与原始数据之间存在一个区块表(Section Table),它是一个IMAGE_SECTION_HEADER结构数组, 区块表包含每个块在映像中的信息(如位置.长度.属性),分别指向不同的 ...
- 数据结构 c++ 广义表
// CTest.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include &l ...
- axis2--生成的wsdl文件方法的参数问题
我是一个使用axis2的新手,发现一个问题: * axis2生成的wsdl文件中关于提供服务的方法,其参数名称丢失,会变成args0 * , 原因: axis2 无法从java字节码中获取关于方法签名 ...
- java#内部类和嵌套类
内容思路来自Java编程思想,个人读书做的笔记,仅个人复习之用,故他人参考请自行辨别内容是否有错误. 在类的类部可以定义类,叫做内部类.如果这个内部类被static修饰,此时内部的类叫做嵌套类. 内部 ...