Xor |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:32768KB |
Total submit users: 109, Accepted users: 92 |
Problem 11130 : No special judgement |
Problem description |
We all know that huicpc302 is good at maths, now he does a job below. At first he got a n numbers. Then he got a function below: long long Cal(int a[], int n) { int i, j; long long ret = 0; for(i = 0; i < n; i ++) { for(j = 0; j < n; j ++) { ret += a[i]^a[j]; } } return ret; }Now he wants to know what he will get, when using the function to the calculate the n numbers. Note that when n is too large, this function works slowly, but huicpc302 wants to get the answer as soon as possible. |
Input |
There are several test cases in the input, the first line of each test case is an integer n, the second line contains n(1 <= n <= 100000) integers, a0, a1, a2... an-1. (1 <= ai < 219). The input ends by EOF. |
Output |
For each test case, output one line, the result. |
Sample Input |
5 41 18467 6334 26500 19169 5 15724 11478 29358 26962 24464 |
Sample Output |
338484 352232 |
Judge Tips |
a[i]^a[j] means a[i] Xor a[j] |
Problem Source |
YiYi modified from xmu oj |
1 #include<stdio.h> 2 int a[100001][19]; 3 int main() 4 { 5 int n,i,j,b,dd1[20]; 6 __int64 sum; 7 while(~scanf("%d",&n)) 8 { 9 int t=20; 10 memset(dd1,0,sizeof(dd1)); 11 sum=0; 12 for(i=1;i<=n;i++) 13 { 14 scanf("%d",&b); 15 j=19; 16 while(b) 17 { 18 a[i][j]=b%2; 19 if(b%2!=0) 20 dd1[j]++; 21 b=b/2; 22 j--; 23 } 24 if(j+1<t) 25 t=j+1; 26 } 27 __int64 x=1; 28 for(i=1;i<=19-t;i++) 29 x=x*2; 30 for(i=t;i<=19;i++) 31 { 32 sum=sum+x*dd1[i]*(n-dd1[i]); 33 x=x/2; 34 } 35 printf("%I64d\n",2*sum); 36 } 37 return 0; 38 }