zoj-3870 (二进制)
For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-man team from N students of his university.
Edward knows the skill level of each student. He has found that if two students with skill level A and B form a team, the skill level of the team will be A ⊕ B, where ⊕ means bitwise exclusive or. A team will play well if and only if the skill level of the team is greater than the skill level of each team member (i.e. A ⊕ B > max{A, B}).
Edward wants to form a team that will play well in the contest. Please tell him the possible number of such teams. Two teams are considered different if there is at least one different team member.
Input
There are multiple test cases. The first line of input contains an integer Tindicating the number of test cases. For each test case:
The first line contains an integer N (2 <= N <= 100000), which indicates the number of student. The next line contains N positive integers separated by spaces. The ithinteger denotes the skill level of ith student. Every integer will not exceed 109.
<h4< dd="">Output
For each case, print the answer in one line.
<h4< dd="">Sample Input
2
3
1 2 3
5
1 2 3 4 5
<h4< dd="">Sample Output
1
6
这题真的做的我失去梦想,成为了一条咸鱼- -;
题目很好理解,就是求有多少种 两个数的异或值大于这两个数 的情况。
赤裸裸的二进制,然鹅,我和我队友都不知道咋处理。
突然我灵光一闪,对他说,我们打表找规律吧!
结果我还真找到了规律,写了一大圈子,想了几个样例还全过了,信誓旦旦交上去 --WA。
(哇的一声哭出来)
然后一脸懵逼对着打的表想样例,咋输,咋对。
又进行 一系列 真·玄学debug 法 果不其然全部 -- WA。
(尴尬又不失礼貌的微笑)
...
好了,回归正题。
这题的正解自然是用二进制思想,思路是判断什么情况下,两个数的异或值会大于这两个数。
显然 1^1=0 1^0=1 0^0=0 那么设需要判断的两个数为a, b且 a<b(如果a=b,则a^b=0)
因为1^0=1,那么只要a的最高位对应在b中的值为0,则满足条件。这样讲比较抽象,举个例子:
b=10110,那么什么情况下 a^b>b 呢?肯定是当a的最高位在b的从左到右第2位,第5位时才会a^b>b,即:
b: 10110 10110
a: 1xxx 1
异或值:11xxx 10111
是吧,这样情况下的异或值都要大于a和b,也就是说只需要设一个book[Max],把每个数的首位位置记下来,再根据这个记录,判断是否满足情况。
具体看代码:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
const int M = 111111;
const int Mx=33;
int nu[M];
int book[Mx]; //记录最高位的位置
int main(){
ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--){
memset(book,0,sizeof(book));
int n,ans=0;
cin>>n;
for(int i=0;i<n;i++){
cin>>nu[i];
int l=31; //题目中说数据不超过1e9
while(!(nu[i]&(1<<l))) l--; //从第31位进行进行与运算,
book[l]++; //找到该数二进制首位后,记录
}
for(int i=0;i<n;i++){
int l=31;
while(!(nu[i]&(1<<l))) l--;//找nu[i]的二进制首位
while(l--){ //找到二进制首位后,继续遍历
if(!(nu[i]&(1<<l))) //找到该数二进制中的所有0
ans+=book[l]; //把该位是0的book[l]的所有记录相加,便是答案
} }
cout<<ans<<endl;
}
return 0;
}
这题的题解我看的是这个:http://blog.csdn.net/ZzebraA/article/details/51272652
zoj-3870 (二进制)的更多相关文章
- 位运算 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^ ...
- (二进制 异或)Team Formation --ZOJ --3870
链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3870 http://acm.hust.edu.cn/vjudge/ ...
- ZOJ 3870 Team Formation 贪心二进制
B - Team Formation Description For an upcoming progr ...
- 【ZOJ 3870】 Team Formation
题意 n个数,找出有几对a.b 符合 a ^ b > max(a,b) .^表示异或号 分析 对于数a,如果它的二进制是: 1 0 1 0 0 1,那么和它 ^ 后 能比他大的数就是: 0 1 ...
- Zoj 3870——Team Formation——————【技巧,规律】
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contes ...
- ZOJ - 3870 Team Formation(异或)
题意:给定N个数,求这N个数中满足A ⊕ B > max{A, B})的AB有多少对.(A,B是N中的某两个数) 分析: 1.异或,首先想到转化为二进制. eg:110011(A)和 1(B)- ...
- ZOJ 3870:Team Formation(位运算&思维)
Team Formation Time Limit: 2 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...
- zoj 3870
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5518 题意:n个数,从中选出两个数,问这两个数的异或值大于两个数较大 ...
- ZOJ 3870 Team Formation 位运算 位异或用与运算做的
For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-m ...
- ZOJ - 3870-Team Formation二进制,位运算
传送门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3870 题意:找出一个数列中的两个数,所有通过异或和使得结果同时大于 ...
随机推荐
- java.net.NoRouteToHostException: 没有到主机的路由
今天在配置Jenkins 的云服务器的时候提示:java.net.NoRouteToHostException: 没有到主机的路由,网上查到的没有主机路由问题提到的大多是防火墙问题. 查看防火墙状态: ...
- django url别名和反向解析 命名空间
url别名和反向解析 我们平时写的url名字都是死的,如果项目过大,需要项目中某个文件名改动一下,那么改动起来就不是一般的麻烦了,所以我们就在定义的时候给url起一个别名,以后不管哪个文件中运用都是用 ...
- 从零开始学spring源码之xml解析(一):入门
谈到spring,首先想到的肯定是ioc,DI依赖注入,aop,但是其实很多人只是知道这些是spring核心概念,甚至不知道这些代表了什么意思,,作为一个java程序员,怎么能说自己对号称改变了jav ...
- 使用pushplus+python实现亚马逊到货消息推送微信
xbox series和ps5发售以来,国内黄牛价格一直居高不下.虽然海外amazon上ps5补货很少而且基本撑不过一分钟,但是xbox series系列明显要好抢很多. 日亚.德亚的xbox ser ...
- 网易新闻App架构重构实践:DDD正走向流行
网易新闻App架构重构实践:DDD正走向流行 https://mp.weixin.qq.com/s/FdwrT_xn3CQqpWoRVBttvQ 小智 InfoQ 2020-05-14 作者 | 小智 ...
- IDE 阅读代码时候如何防止误触
在 JetBrains 系列的编辑器中,点击右下角小锁图标,就可以只读防止误修改. Visual Studio 下安装 CodeMaid 插件 http://www.codemaid.net/ htt ...
- SumatraPDF设置护眼背景
高级选项中: 1 FixedPageUI [ 2 TextColor = #000000 3 BackgroundColor = #C7EDCC 4 SelectionColor = #f5fc0c ...
- Share Memory By Communicating 一等公民
Share Memory By Communicating - The Go Programming Language https://golang.google.cn/doc/codewalk/sh ...
- 题解 UVA11694 【Gokigen Naname谜题 Gokigen Naname】
题目 题解 考场上连暴力都不会打的码农题,深搜是真的难 /kk 前置问题 怎么输出"\" cout<<"\\"; 2.怎么处理不在一个环里,可以考虑 ...
- Linux常用命令,目录解析,思维导图
文章目录 下载地址 Linux常用命令 linux系统常用快捷键及符号命令 Linux常用Shell命令 Linux系统目录解析 Shell Vi全文本编辑器 Linux安装软件 Linux脚本编制编 ...