ZOJ 3870 Team Formation (bitwise operations)

Source: Internet
Author: User

Title Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5518


For an upcoming programming contest, Edward, the headmaster of Marjar University, was forming a Two-man team from N Stu Dents of his university.

Edward knows the skill level of each student. He has found so if students with skill level A B and form a team, the skill level of the team A would be ⊙ B , Where⊕means bitwise exclusive OR.  A team would play well if and only if the skill level of the team are greater than the skill level of each team member (i.e. A⊙ B > max{ A , B }).

Edward wants to form a team that'll play well in the contest. Please tell him the possible number of such teams. Teams is considered different if there was at least one different team member.

Input

There is multiple test cases. The first line of input contains an integer indicating the number of the T 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 would not exceed 109.

Output

For each case, print the answer on 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


Test instructions
Given the number of n, each time the number of two, asked how many kinds of election method can make the selected two number of the difference or value than the two number of the largest number of the larger.
That is
z = x^y, z > Max (x, y).
Ps:
For a number, if we turn 0 of the binary representation of X into 1, and 0 is the same, then the new value obtained is certainly larger than X.
The code is as follows:
#include <cstdio> #include <cstring> #include <cmath> #define MAXN 100017int A[MAXN], c[maxn];void cal (    int m) {int L = 31;            while (l >= 0) {if (M & (1<<l)) {c[l]++;        return;    } l--; } return;    int main () {int t;    int n;    scanf ("%d", &t);        while (t--) {memset (c,0,sizeof (c));        scanf ("%d", &n);            for (int i = 0; i < n; i++) {scanf ("%d", &a[i]);        Cal (A[i]);        } int ans = 0;            for (int i = 0; i < n; i++) {int l = 31;                    while (l >= 0) {if ((1<<l) & A[i])//Find the position of the front one {                Break            } l--; } while (l >= 0) {if (!) (                (1<<l) & A[i]) {ans+=c[l];            } l--;  }        }      printf ("%d\n", ans); } return 0;}




ZOJ 3870 Team Formation (bitwise operations)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.