CodeForces - 1368D AND, OR and square sum

本来是个数学题,让我整成了瞎搞题,写成二进制找规律
差不多就这样吧

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define ms(x,a) memset(x,a,sizeof(x))
using namespace std;
const int maxn = 2e5 + 10;
const ll mod = 1e9 + 7;
int n;
ll a[maxn],b[maxn][70],tmp[35];

ll qpow(ll x,ll n) {
	ll res = 1;
	while(n) {
		if(n & 1)res *= x;
		x *= x;
		n >>= 1;
	}
	return res;
}

void get_ans(ll x,int id){
	int cnt = 0;
	while(x){
		b[id][++cnt] = x % 2;
		x >>= 1;
	}
}

int main() {
	ms(tmp,0);
	ms(b,0);
	scanf("%d",&n);
	for(int i = 1;i <= n;i++){
		scanf("%lld",&a[i]);
		get_ans(a[i],i);
	}
	for(int i = 1;i <= 32;i++){
		for(int j = 1;j <= n;j++){
			if(b[j][i]){
				tmp[i]++;
			}
		}
	}
	ll ans = 0;
	while(1){
		bool flag = 0;
		ll sum = 0;
		for(int i = 1;i <= 32;i++){
			if(tmp[i]){
				flag = 1;
				sum += qpow(2,i - 1);
				tmp[i]--;
			}
		}
		ans += qpow(sum,2);
		if(!flag)break;
	}
	cout<<ans<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43873569/article/details/106849956