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;
}

转载于:https://www.cnblogs.com/ffhuguang/p/3280612.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值