牛客练习赛60 A 大吉大利(位运算)

牛客练习赛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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值