Binary Operation

Problem Description

Consider a binary operation ☆ defined on digits 0 and 1, 0 if the two operand are equal, 1 if they differ. A binary operation ⊙ is a generalization of ☆to positive integers, The result of a⊙b is defined in the following way: aligning the binary code of b under the binary code of a, if one of the numbers a and b has fewer digits than the other in binary notation, then append leading zeroes to it, so that the numbers are of the same length; then applying the operation☆in each column, finally, converting the binary result back to decimal notation. For example, 19⊙10:
10011(19)⊙01010(10)=11001(25)
a1⊙a2⊙a3= a1⊙a2+ a1⊙a3+ a2⊙a3
a1⊙a2⊙a3⊙a4= a1⊙a2+ a1⊙a3+ a1⊙a4+ a2⊙a3+ a2⊙a4+ a3⊙a4
......

Input

The first line contains an integer T(1 <= T <= 10), indicating the number of test cases.For each test case , the first and only line of input contains an integer N (the number of operands in ⊙ expression, 1 ≤ N ≤ 1 000 000), then followed by N positive integers(di∈(0,1 000 000), indicating operand).

Output

For each test case, output a single number in a line–the value of
d1⊙d2⊙..⊙dN= d1⊙d2+ d1⊙d3...+ d1⊙dN+ d2⊙d3+... + d2⊙dN +... dN-1⊙dN

Sample Input

2
3 7 3 5
5 9 13 1 9 6

Sample Output

12
84
题意:给定n个数,要求对每一个数,后面的数都对它进行去异或操作,求和。
题解:统计这些数中每一位累积的1的个数然后根据该位对应的权值乘以1的个数再乘以0的个数即可
#include<stdio.h>
#include<iostream>
#include<string.h>
#define ll __int64
using namespace std;
ll a[25];
ll m;
int main()
{
     ll i,j,n,sum,test,m;
    scanf("%I64d",&test);
 while(test--)
 {
  scanf("%I64d",&n);
  memset(a,0,sizeof(a));
  for(i=0;i<n;i++)
  {
   scanf("%I64d",&m);
            for(j=0;j<20;j++)
   {
              if(m&1) a[j]++;
     m>>=1;
     if(!m) break;
   }
  }
  sum=0;
  for(i=0;i<20;i++)
  {
   sum+=a[i]*(n-a[i])*(1<<i);
  }
  printf("%I64d\n",sum);
 }
 return 0;
}
posted @ 2013-08-25 11:46  forevermemory  阅读(718)  评论(0编辑  收藏  举报