Cattle-off practice match 60 A good luck solution to a problem (bit operation)

Topic Link

Subject to the effect

Here Insert Picture Description

Topic ideas

First of all on a violent Code (will t) to find out title

#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int n,a[maxn];
ll ans;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i]);
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            ans+=a[i]&a[j];    
        }
    }
    printf("%lld",ans);
    return 0;
}

First, understand the meaning of the title, certainly violence t, in fact, subject to the relevant bit computing in general must take into account the contribution, whither think, will soon have an answer.
We consider the number of direct answers every 1 to each bit of statistics about each ai, for a bit, if there are k 1, k is the fact that the answer ^ 2 1, simulate what binary addition can be.

Code

#include<cstdio>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
int n,a[maxn],cnt[40]; 
ll ans,sum;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=1;i<=n;i++){
		for(int j=0;j<=30;j++){
			if(a[i]&1<<j){
				cnt[j]++;
			}
		}
	}	
	for(int i=0;i<=30;i++){
		ans+=1ll*(1<<i)*cnt[i]*cnt[i];
	}
	printf("%lld",ans);
	return 0;
} 
Published 68 original articles · won praise 2 · Views 2242

Guess you like

Origin blog.csdn.net/m0_46209312/article/details/105267243