Zoj 3870 Team Formation (exclusive or operation), zoj3870

Source: Internet
Author: User

Zoj 3870 Team Formation (exclusive or operation), zoj3870

Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB

For an upcoming programming contest, Edward, the headmaster of Marjar University, is forming a two-man team fromNStudents of his university.

Edward knows the skill level of each student. He has found that if two students with skill levelAAndBForm a team, the skill level of the team will beABytesB, Where each 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.ABytesB> 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 integerTIndicating the number of test cases. For each test case:

The first line contains an integerN(2 <=N<= 100000), which indicates the number of student. The next line containsNPositive integers separated by spaces.IthInteger denotes the skill levelIthStudent. 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
 
Question: give n numbers. Select 2 numbers each time. Ask the total number of selection methods to make the two numbers exclusive or the latter value. The maximum value of the two numbers is even greater.
 
Analysis: exclusive or operation: 1 ^ 1 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 0 ^ 0 = 0.
For a number, if we change the value 0 in the binary representation of x to the value before 1 and 0, the new value is certainly greater than x. That is, if the I bit of x is 1 (The position of 1 in the highest bit of x) and the I bit of y is 0, then after z = x ^ y, z> max (x, y ).
# Include <cstdio> # include <cstring> # include <algorithm> using namespace std; const int MaxN = 1e5 + 10; int a [MaxN], bit [50]; // bit [I] indicates the number of the highest bit 1 in the I bit void solve (int x) {int l = 31; while (l> = 0) {if (x & (1 <l) {bit [l] ++; return;} l --;} return ;} int main () {int T, n; scanf ("% d", & T); while (T --) {scanf ("% d", & n); memset (bit, 0, sizeof (bit )); for (int I = 0; I <n; I ++) {scanf ("% d", & a [I]); s Olve (a [I]) ;}int ans = 0; for (int I = 0; I <n; I ++) {int l = 31; while (l> = 0) {if (a [I] & (1 <l) break; l --;} while (l> = 0) {if (! (A [I] & (1 <l) ans + = bit [l]; l -- ;}} printf ("% d \ n", ans );} return 0 ;}


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.