码迷,mamicode.com
首页 > 其他好文 > 详细

uestc1041Hug the princess

时间:2016-05-13 02:13:21      阅读:252      评论:0      收藏:0      [点我收藏+]

标签:

Hug the princess

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

There is a sequence with nn elements. Assuming they are a1,a2,?,ana1,a2,?,an.

Please calculate the following expession.

1i<jn(aiaj)+(ai|aj)+(ai&aj)∑1≤i<j≤n(ai∧aj)+(ai|aj)+(ai&aj)

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.

Input

The first line contains a single integer nn, which is the size of the sequence.

The second line contains nn integers, the ithith integer aiai is the ithith element of the sequence.

1n100000,0ai1000000001≤n≤100000,0≤ai≤100000000

Output

Print the answer in one line.

Sample input and output

Sample Input Sample Output
2
1 2
6

Hint

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



uestc1041Hug the princess

标签:

原文地址:http://blog.csdn.net/r1986799047/article/details/51347566

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!