Binary Operation
题意:
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
算法:
1.朴素算法O(n ^2),肯定不行,百万级别数据
2.考虑二进制性质,统计所有位1的个数,由异或可知,0,1为1
分别统计每位的和。
比如第1位
sum += (N - bit[1]) * bit[1] * (1<<i)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<stdio.h> #include<string.h> #include<stdlib.h> int d[40]; int main( ) { int N,t,M; scanf("%d",&N); while(N--) { scanf("%d",&M); memset(d,0,sizeof(d)); for( int i = 1; i <= M; i++) { scanf("%d",&t); int ans = 0; while( t ) { int f = (1 & t); t >>= 1; if( f ) d[ans]++; ans++; } } long long sum = 0; for( int i = 0; i <= 32; i++) { sum += (long long) ( M - d[i] ) * d[i] * (1<<i); //没有转换为long long 一直wa.. } printf("%I64d\n",sum); } return 0; }
posted on 2012-07-30 16:48 more think, more gains 阅读(211) 评论(0) 编辑 收藏 举报