Description

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 AB, 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. AB > 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. The ith 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

  1. 2
  2. 3
  3. 1 2 3
  4. 5
  5. 1 2 3 4 5

Sample Output

  1. 1
  2. 6

这道题思路一次进入,直接1A。关键是抓住位运算的特点。对于亦或运算1和0运算的结果还是1,0和0运算的结果还是0,而1和1运算结果会变成0。所以对于一个二进制数A,比如1001001.那么对于B,不妨设A>B,要想让A^B > A,必然B中的第一位1要和A中的0进行亦或运算。所以对于一个二进制位固定长度的B,只要第一位1和A中的0进行亦或,那么答案一定是变大的。

所以只需要枚举A中0的位置,然后找其二进制位对应长度的B的个数即可。而且预处理计算出二进制各长度的数的个数,并保存在cnt数组里,加速了计算。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <cstring>
  5. #include <cmath>
  6. #include <algorithm>
  7. #include <set>
  8. #include <map>
  9. #include <queue>
  10. #include <string>
  11. #define LL long long
  12.  
  13. using namespace std;
  14.  
  15. bool cmp(int a, int b)
  16. {
  17. return a > b;
  18. }
  19.  
  20. int n, a[100005], l[100005];
  21. int cnt[40];
  22.  
  23. int GetLen(int n)
  24. {
  25. int len = 0;
  26. while (n)
  27. {
  28. len++;
  29. n >>= 1;
  30. }
  31. return len;
  32. }
  33.  
  34. void Init()
  35. {
  36. memset(cnt, 0, sizeof(cnt));
  37. for (int i = 0; i < n; ++i)
  38. {
  39. l[i] = GetLen(a[i]);
  40. cnt[l[i]]++;
  41. }
  42. }
  43.  
  44. LL Work()
  45. {
  46. int len;
  47. LL ans = 0;
  48. for (int i = 0; i < n; ++i)
  49. {
  50. len = l[i];
  51. int j;
  52. for (j = len-1; j >= 0; j--)
  53. {
  54. if ((a[i] & (1<<j)) == 0)
  55. {
  56. ans += cnt[j+1];
  57. }
  58. }
  59. }
  60. return ans;
  61. }
  62.  
  63. int main()
  64. {
  65. //freopen("test.in", "r", stdin);
  66. int T;
  67. scanf("%d", &T);
  68. for (int times = 0; times < T; ++times)
  69. {
  70. scanf("%d", &n);
  71. for (int i = 0; i < n; ++i)
  72. scanf("%d", &a[i]);
  73. sort(a, a+n, cmp);
  74. Init();
  75. printf("%lld\n", Work());
  76. }
  77. return 0;
  78. }

AndyQsmart ACM学习历程——ZOJ3870 Team Formation(位运算)的更多相关文章

  1. ACM学习历程—Hihocoder 1178 计数(位运算 && set容器)(hihoCoder挑战赛12)

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB   描述 Rowdark是一个邪恶的魔法师.在他阅读大巫术师Lich的传记时,他发现一类黑魔法来召唤远古生物,鱼丸. 魔法n能召 ...

  2. ACM学习历程——UVA540 Team Queue(队列,map:Hash)

    Description   Team Queue   Team Queue  Queues and Priority Queues are data structures which are know ...

  3. AndyQsmart ACM学习历程——ZOJ3872 Beauty of Array(递推)

    Description Edward has an array A with N integers. He defines the beauty of an array as the summatio ...

  4. ZOJ 3870 Team Formation 位运算 位异或用与运算做的

    For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-m ...

  5. ZOJ3870 Team Formation

    /** Author: Oliver ProblemId: ZOJ3870 Team Formation */ /* 思路 1.异或运算,使用^会爆,想到二进制: 2.我们可以试着从前往后模拟一位一位 ...

  6. ACM学习历程—SGU 275 To xor or not to xor(xor高斯消元)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=275 这是一道xor高斯消元. 题目大意是给了n个数,然后任取几个数,让他们xor和 ...

  7. ACM学习历程—HDU1719 Friend(数论)

    Description Friend number are defined recursively as follows. (1) numbers 1 and 2 are friend number; ...

  8. ios开发学习笔记004-进制与位运算

    进制 二进制   0 1组成,封2进1 八进制 0-7组成,封8进1 十进制 0-9组成,封10进1 十六进制 0-15组成,封16进1 printf以不同进制形式进行输出 变量的内存地址形式 变量在 ...

  9. ACM学习历程—计蒜客15 单独的数字(位运算)

    http://nanti.jisuanke.com/t/15 题目要求是求出只出现一次的数字,其余数字均出现三次. 之前有过一个题是其余数字出现两次,那么就是全部亦或起来就得到答案. 这题有些不太一样 ...

随机推荐

  1. syslog,rsyslog and syslog-ng

    http://en.wikipedia.org/wiki/Syslog Syslog is a standard for computer message logging. It permits se ...

  2. POJ 2985 Treap平衡树(求第k大的元素)

    这题也能够用树状数组做,并且树状数组姿势更加优美.代码更加少,只是这个Treap树就是求第K大元素的专家--所以速度比較快. 这个也是从那本红书上拿的模板--自己找了资料百度了好久,才理解这个Trea ...

  3. rtems 4.11 console驱动 (arm, beagle)

    console驱动框架主要文件是 c/src/lib/libbsp/shared/console.c,驱动的入口是 console_initialize()主要作用是初始化BSP提供的全局变量 Con ...

  4. linux SPI驱动——spi core(四)

    一: SPI核心,就是指/drivers/spi/目录下spi.c文件中提供给其他文件的函数,首先看下spi核心的初始化函数spi_init(void). 1: static int __init s ...

  5. SkipList跳跃表(Java实现)

    取自网络https://github.com/spratt/SkipList AbstractSortedSet.java package skiplist_m; /***************** ...

  6. Android应用开发:网络工具——Volley(二)

    引言 在Android应用开发:网络工具--Volley(一)中结合Cloudant服务介绍了Volley的一般使用方法.当中包括了两种请求类型StringRequest和JsonObjectRequ ...

  7. ASP.NET动态网站制作(6)-- JS(1)

    前言:JS的第一节课,在Visual Studio 2013中编写及运行.新建项目->Web->ASP.NET Web应用程序->Empty,打开后在项目下添加新建css文件夹和js ...

  8. Django Rest Framework remove csrf

    37down votefavorite 14 I know that there are answers regarding Django Rest Framework, but I couldn't ...

  9. 九度OJ 1165:字符串匹配 (模式匹配)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3219 解决:1149 题目描述: 读入数据string[ ],然后读入一个短字符串.要求查找string[ ]中和短字符串的所有匹配,输出 ...

  10. GIT / SVN 提交代码时, 注释该怎么写

    大致可以分为 6 种 : 添加 修改 修复 --> 修正 优化 --> 改进 ;