对于100% 的数据,1≤n≤105

做法是求异或前缀和然后按位讨论贡献

如果当前位k有奇数个1那么贡献为(numof1×numof0)×(1<<k)

 

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<algorithm>
#include<cstdlib>
#include<iterator>
using namespace std;

long long n,seq[100010]={0},xsum[100010]={0},cache1;
long long pl[110]={0},powx[110]={0};
long long ans=0;

inline void readx(long long& x)
{
	x=0; long long k=1; register char ch=0;
	while (ch<'0' || ch>'9') { ch=getchar(); if (ch=='-') k-1; }
	while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
	x*=k;
}

int main()
{
	powx[0]=1;
	for (int i=1;i<=45;i++) powx[i]=powx[i-1]+powx[i-1];
	
	readx(n);
	for (int i=1;i<=n;i++) readx(seq[i]);
	
	xsum[1]=seq[1];
	for (int i=2;i<=n;i++) xsum[i]=xsum[i-1]^seq[i];
	
	for (int i=1;i<=n;i++)
	{
		cache1=xsum[i];
		for (int k=0;k<=44;k++) if (cache1&powx[k]) pl[k]++;
	}
	
	for (int i=0;i<=44;i++) ans+=pl[i]*(n-pl[i]+1)*powx[i];
	printf("%lld\n",ans);
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.