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.我们可以试着从前往后模拟一位一位 ...
随机推荐
- HTML有2种路径的写法:绝对路径和相对路径
HTML有2种路径的写法:绝对路径和相对路径 2016年11月30日 17:51:20 Bolon0708 阅读数 21775 版权声明:本文为博主原创文章,未经博主允许不得转载. https:/ ...
- intent 跳转
一.不需要返回值的跳转 Intent intent=new Intent(); intent.setClass(目前的acitivy.this, 目标activity.class); startAct ...
- php 投票系统
1.投票主界面(问题界面) <?php$db = new Mysqli("localhost","root","root"," ...
- Unity添加小米游戏SDK
因为游戏要上线小米的平台,所以游戏就要添加小米SDK,整了3天总算是把小米SDK添加上了~~ 多亏找到了这个帖子:Unity3D接入小米盒子SDK. (小米人家论坛有官方贴出来的其他开发者的接入经验~ ...
- hdu 1533 Going Home 最小费用最大流 (模板题)
Going Home Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- POJ 2718 Smallest Difference dfs枚举两个数差最小
Smallest Difference Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19528 Accepted: 5 ...
- 本地连接 HDFS 报错 Exception in thread "main" org.apache.hadoop.security.AccessControlException: org.apache.hadoop.security.AccessControlException: Permission denied: user=JM.H,access=WRITE, inode="":r
此时 到hdfs机器下修改hdfs-site.xml即可 添加如下配置 <property> <name>dfs.permissions</name> <va ...
- 论文阅读:Blink-Fast Connectivity Recovery Entirely in the Data Plane
1.背景 在网络中,链路故障的发生在所难免,为了降低故障带来的影响,就需要重新路由,将数据传输到合适的链路上.当因为链路故障发生处的不同,也有不同的解决方法. AS(Autonomous System ...
- Python测试进阶——(3)编写Python程序监控计算机的服务是否正常运行
用python写了个简单的监控进程的脚本,当发现进程消失的时候,立即调用服务,开启服务. 脚本的工作原理是这样的:脚本读取配置文件,读取预先配置好的调用系统服务的路径和所要监控的服务在进程管理器中的进 ...
- Day6-T3
原题目 某个帝国修了一条非常非常长的城墙来抵御外敌,城墙共分N段,每一段用一个整数来描述坚固程度. 过了几年,城墙年久失修,有很多段都己经损坏,于是皇帝决定派你去修理城墙,但是经费有限. 所以你准备先 ...