Binary double summation cattle off the practice game 60 A question good luck

Good luck

Title Description
Here Insert Picture Description
Enter a description:

A first line integer n.
The second line n integers ai.

Output Description:

An integer that represents the summation of the above answers.


To think this way can put a number to be split into a binary summation,

E.g:

001、010、011、100、101

Take the first number (001) starts operation, the first bit of an operation carried out five times; get a second number (010) starts operation of a second operation performed five times; take the third number ( 011) began operation, the second of three 1 carried out five operations ...

For every 1, the & operator with an if, it is a constant; it can be seen that as long as the record number of each of these 1-bit binary numbers, you can calculate the answer;

years = i = 0 32 ( 1 < < i ) s u m [ i ] 2 \sum_{i=0}^{32} (1<<i)*sum[i]^2

SUM [i] represents the number of all data into binary bit i 1

Code:

#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,int>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=100010;
const int M=2000100;
const LL mod=2e9;
int a[N],s[32];
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	for(int i=1;i<=n;i++){
		for(int j=0;j<32;j++){
			if((1<<j)&a[i]) s[j]++;
		}
	}
	LL ans=0;
	for(int i=0;i<32;i++){
		ans+=(1ll*s[i]*s[i]*(1ll<<i));
	}
	cout<<ans<<endl;
	return 0;
}
Published 264 original articles · won praise 46 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_44291254/article/details/105160895