ZOJ3870 Team Formation【位运算+数学】
来源:互联网 发布:知乎 碧桂园森林城市 编辑:程序博客网 时间:2024/05/14 13:23
Team Formation
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 T indicating 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. Theith integer denotes the skill level of ith student. Every integer will not exceed 109.
Output
For each case, print the answer in one line.
Sample Input
231 2 351 2 3 4 5
Sample Output
16
Author: LIN, Xi
Source: The 12th Zhejiang Provincial Collegiate Programming Contest
问题链接:ZOJ3870 Team Formation。
问题简述:输入n个正整数,求任意两个数异或运算结果大于两个数中的最大值的组数。
问题分析:
这里给出异或运算的规则,1^1=0,1^0=1,0^1=1,0^0=0。
对于y而言,若y的第i位为1(从右到左分别为0-31位),y的第j位为0(j<i),
对于第j位为1并且是最高位的1的x,
那么y^x>y(按照假定满足x<y)。
程序说明:
数组bit[],bit[i]表示各个数(所有的a)中第i位是最高位1的数的个数。
数组left[],left[i]保存a[i]的为1的最高位。
题记:(略)AC的C++语言程序如下:
/* ZOJ3870 Team Formation */#include <iostream>#include <stdio.h>#include <string.h>const int N = 100000;const int N2 = 32;int a[N], left[N], bit[N2];void left1count(int x, int i) { int l = N2 - 1; left[i] = -1; while(l >= 0) { if(x & (1 << l)) { bit[l]++; left[i] = l; break; } l--; } return;}int main(){ int t, n; scanf("%d", &t); while(t--) { memset(bit, 0, sizeof(bit)); scanf("%d",&n); for(int i=0; i<n; i++) { scanf("%d", &a[i]); left1count(a[i], i); } long long ans = 0; for(int i=0; i<n; i++) { if(a[i]) { while(left[i] >= 0) { if(!(a[i] & (1 << left[i]))) ans += bit[left[i]]; left[i]--; } } } printf("%lld\n", ans); } return 0;}
- ZOJ3870 Team Formation【位运算+数学】
- zoj3870-Team Formation(异或运算+数学规律)
- ZOJ3870 Team Formation(2015浙江省赛) 位运算异或
- zoj3870-Team Formation(异或运算)
- Zoj 3870 Team Formation(异或运算),zoj3870
- ZOJ 题目3870 Team Formation(数学,位运算)
- ZOJ3870:Team Formation(浙江省赛2015)
- ZOJ 3870 Team Formation(位运算)
- ZOJ 3870 Team Formation 位运算
- zoj 3870 Team Formation 位运算
- ZOJ Team Formation 3870【位运算】
- 2015年省赛 B Team Formation【位运算】
- ZOJ 3870Team Formation(位运算)
- ZOJ - 3870 Team Formation (位运算)
- ZOJ-3870 Team Formation(位运算)
- ZOJ 3870 Team Formation 第12届浙江省省赛B题 [位运算+思维]【数学】
- 数学题ZOJ3870 Team Formation(2015浙江省赛)
- zoj 3870 Team Formation(位运算,超时)
- Codeforces #831E: Cards Sorting 题解
- python文件操作
- 浅析Java基本数据类型
- 【学习笔记】 初学JS写了个轮播图
- 苹果APP开发技术支持
- ZOJ3870 Team Formation【位运算+数学】
- 坚持#第186天~好久没写了,我想变优秀,所以要坚持写
- 线程互斥和同步(二 方式)线程
- intellij 中使用gulp
- freemarker语法
- 第一篇文章
- spring学习day2
- HttpURLConnection基本使用【L】
- 螺旋数组