uestc1041Hug the princess

编程入门 行业动态 更新时间:2024-10-07 19:24:07

uestc1041Hug the <a href=https://www.elefans.com/category/jswz/34/1745720.html style=princess"/>

uestc1041Hug the princess

Hug the princess

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

There is a sequence with  n n elements. Assuming they are  a1,a2,⋯,an a1,a2,⋯,an.

Please calculate the following expession.

∑1≤i<j≤n(ai∧aj)+(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

to get some useful information.

Input

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

The second line contains  n n integers, the  ith ith integer  ai ai is the  ith ith element of the sequence.

1≤n≤100000,0≤ai≤100000000 1≤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

本文发布于:2024-02-06 03:21:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1745918.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:uestc1041Hug   princess

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!