牛客练习赛60 A 大吉大利(位运算)
链接 https://ac.nowcoder.com/acm/contest/4853/A
题意
给定n个整数,依次为,a1,a2,a3,a4…an.(1<=n<=1e5)
求
首先暴力肯定是会 t 的;
然后就开始找规律 首先 & 操作是,有 0就变成 0.
把这组换成二进制 1 2 3 4 5
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
我们可以按二进制位一位一位考虑:
考虑一下 0 号位,每一个数都会与整个数组进行 & 操作
所以 0号位的贡献就是 cnt0(0号位1的数量)cnt0(1<<0);
所以答案就为 ∑cnti * cnti* (1<<i);
#include<iostream>
#include<cstdio>
#include<map>
#include<math.h>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=2e5+7;
const int mod=1e9+7;
ll a[maxn];
int main (){
ll n,ans=0,temp;
cin>>n;
for(int i=0;i<n;i++) scanf("%d",&a[i]);
for(int i=0;i<32;i++){
temp=0;
for(int j=0;j<n;j++){
if((a[j]>>i)&1) temp++;
}
ans+=(1<<i)*temp*temp;
}
printf ("%lld\n",ans);
}