概述
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of nn non-negative integers a1,…,ana1,…,an. You are allowed to perform the following operation: choose two distinct indices 1≤i,j≤n1≤i,j≤n. If before the operation ai=xai=x, aj=yaj=y, then after the operation ai=x AND yai=x AND y, aj=x OR yaj=x OR y, where ANDAND and OROR are bitwise AND and OR respectively (refer to the Notes section for formal description). The operation may be performed any number of times (possibly zero).
After all operations are done, compute ∑ni=1a2i∑i=1nai2 — the sum of squares of all aiai. What is the largest sum of squares you can achieve?
Input
The first line contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105).
The second line contains nn integers a1,…,ana1,…,an (0≤ai<2200≤ai<220).
Output
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
Examples
input
Copy
1 123
output
Copy
15129
input
Copy
3 1 3 5
output
Copy
51
input
Copy
2 349525 699050
output
Copy
1099509530625
题意:给你n个数,我们可以每次执行的操作是:选择两个下标i,j,a[i]=a[i]&a[j],a[j]=a[i]^a[j],可以进行无数次操作,求最后的所有数的平方和最大是多少
思路:设a[i]=x,a[j]=y,我们可以发现x+y==x&y+x^y,可以发现两个数当和一定的时候我们将其中一个数构造的最大的平方和是所有情况里和最大的
比如:x==101,y=110时,我们可以发现把x换成111,y换成100时的两个数的平方和最大
那么我们只需要统计一下二进制每位上的1的个数,
让他们尽量都安排在前面的数上,让他们尽可能大就行了
/*
.----------------. .----------------. .----------------. .----------------.
| .--------------. || .--------------. || .--------------. || .--------------. |
| | ________ | || | _________ | || | ____ ____ | || | ____ | |
| | |_ ___ `. | || | |_ ___ | | || ||_ / _|| || | .' `. | |
| | | | `. | || | | |_ _| | || | | / | | || | / .--. | |
| | | | | | | || | | _| _ | || | | | /| | | || | | | | | | |
| | _| |___.' / | || | _| |___/ | | || | _| |_/_| |_ | || | `--' / | |
| | |________.' | || | |_________| | || ||_____||_____|| || | `.____.' | |
| | | || | | || | | || | | |
| '--------------' || '--------------' || '--------------' || '--------------' |
'----------------' '----------------' '----------------' '----------------'
*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<stack>
#define int long long
#define lowbit(x) x&(-x)
#define PI 3.1415926535
#define endl "n"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
int gcd(int a,int b){
return b>0 ? gcd(b,a%b):a;
}
/*
int dx[8]={-2,-2,-1,1,2,2,-1,1};
int dy[8]={-1,1,2,2,1,-1,-2,-2};
int dx[4]={0,-1,0,1};
int dy[4]={-1,0,1,0};
int dx[8]={-1,1,0,0,-1,-1,1,1};
int dy[8]={0,0,-1,1,-1,1,-1,1};
*/
//int e[N],ne[N],h[N],idx,w[N];
/*void add(int a,int b,int c){
e[idx]=b;
w[idx]=c;
ne[idx]=h[a];
h[a]=idx++;
}
*/
const int N=2e5+10;
int n;
int a[N];
int cnt[22];
void sove(){
cin>>n;
for(int i=1;i<=n;i++){
int x;
cin>>x;
for(int j=0;j<=20;j++){
int op=(x>>j)&1;
cnt[j]+=op;
}
}
// for(int i=1;i<=20;i++)cout<<cnt[i]<<" ";
//cout<<endl;
for(int i=0;i<=20;i++){
int num=cnt[i];
for(int j=1;j<=n&#j++){
a[j]+=(1<<i);
num--;
}
}
int con=0;
for(int i=1;i<=n;i++){
con+=a[i]*a[i];
}
cout<<con<<endl;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie() ,cout.tie() ;
int t=1;
// cin>>t;
while(t--){
sove();
}
return 0;
}
最后
以上就是稳重银耳汤为你收集整理的D. AND, OR and square sum的全部内容,希望文章能够帮你解决D. AND, OR and square sum所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复