标签:
There is a sequence with
Please calculate the following expession.
In the expression above, ^
|
&
is
bit operation. If you don’t know bit operation, you can visit
http://en.wikipedia.org/wiki/Bitwise_operation
to get some useful information.
The first line contains a single integer
The second line contains
Print the answer in one line.
Sample Input | Sample Output |
---|---|
2 1 2 |
6 |
Because the answer is so large, please use long long instead of int. Correspondingly, please use %lld
instead
of %d
to scanf and printf.
Large input. You may get Time Limit Exceeded if you use "cin" to get the input. So "scanf" is suggested.
Likewise, you are supposed to use "printf" instead of "cout".
求出二进制位数计算出每个数的对结果的贡献即可
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<queue> #include<list> #include<cmath> #include<vector> using namespace std; const int maxn=100010; long long bit[30][2]; long long num2[maxn][30]; long long num[maxn]; long long bitnum[maxn]; long long POW[30]; int main() { POW[0]=1; for(int i=1;i<30;++i){ POW[i]=POW[i-1]*2ll; } int n,i,j,k; while(scanf("%d",&n)!=EOF){ memset(bit,0,sizeof(bit)); memset(num2,0,sizeof(num2)); for(i=1;i<=n;++i){ scanf("%lld",&num[i]); long long temp=num[i],cnt=0; while(temp){ bit[cnt][temp&1]++; num2[i][cnt]=temp&1; temp>>=1;cnt++; } for(j=cnt;j<30;++j)bit[j][0]++; } long long ans1=0; long long ans=0; for(i=1;i<=n;++i){ for(j=0;j<30;++j){ if(num2[i][j]==1){ ans=ans+(bit[j][0]*POW[j]); ans=ans+((n-i)*POW[j]); ans=ans+((bit[j][1]-1)*POW[j]); bit[j][1]--; } else { ans=ans+(bit[j][1]*POW[j]); ans=ans+(bit[j][1]*POW[j]); bit[j][0]--; } } } printf("%lld\n",ans); } return 0; }
标签:
原文地址:http://blog.csdn.net/r1986799047/article/details/51347566