1368D - AND, OR and square sum
题意:
给你N个数,你可以选择任意两个数 i,j ,使得原本a[i]=x,a[j]=y
变为a[i]=x&y,a[j]=x|y;
,可以做这种操作无数次,请使得这N个数的平方和最大
思路:
题上已经说得很清楚了,用二进制做,直接把每一个数的二进制位都扣下来,然后每次都去取这些二进制位来组成当前能组成的最大数来算平方和
代码附:
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e5+10;
int n;
int a[22];
void work(int x)
{
int now=0;
while(x)
{
if(x&1)
a[now]++;
x>>=1;
now++;
}
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
int x,now=1,ans=0;
for(int i=0; i<n; ++i)
{
cin>>x;
work(x);
}
while(now)
{
now=0;
for(int i=0; i<20; ++i)
if(a[i])
{
now+=(1<<i);
a[i]--;
}
ans+=now*now;
}
cout<<ans;
return 0;
}