博客
关于我
D. AND, OR and square sum
阅读量:244 次
发布时间:2019-03-01

本文共 2549 字,大约阅读时间需要 8 分钟。

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.ExamplesinputCopy

1123

outputCopy

15129

inputCopy

31 3 5

outputCopy

51

inputCopy

2349525 699050

outputCopy

1099509530625

Note

In the first sample no operation can be made, thus the answer is 12321232.

In the second sample we can obtain the collection 1,1,71,1,7, and 12+12+72=5112+12+72=51.

If xx and yy are represented in binary with equal number of bits (possibly with leading zeros), then each bit of x AND yx AND y is set to 11 if and only if both corresponding bits of xx and yy are set to 11. Similarly, each bit of x OR yx OR y is set to 11 if and only if at least one of the corresponding bits of xx and yy are set to 11. For example, x=3x=3 and y=5y=5 are represented as 01120112 and 10121012 (highest bit first). Then, x AND y=0012=1x AND y=0012=1, and x OR y=1112=7x OR y=1112=7.


这题有两个要点:

一个是x+y=x&y+x|y;这个条件可以启发我们和不变,也就是说总和不变,想使ai^2的总和最大。

考虑只有a和b两个数的情况,设总和为s,那么a^2+b^2=(a^2)+(s-a)^2——-展开化简可以知道是二次函数,取到极端是最大的。那么多个的类推过去

另一个结论就是每一位上的1的个数不变。也就是说当前位是1或者是0,和0或者1,1或者0进行&和|操作,最后该位上的1的个数不变。

那么两点结合,根据贪心,统计每一位上1的个数,有1就尽量放就可以了

#include<iostream>#include<vector>#include<queue>#include<cstring>#include<cmath>#include<cstdio>#include<algorithm>using namespace std;const int maxn=2e5+100;typedef long long LL;LL cnt[22];int main(void){  cin.tie(0);std::ios::sync_with_stdio(false);  LL n;cin>>n;  for(LL i=1;i<=n;i++){  	 LL x;cin>>x;  	 for(LL j=0;j<20;j++){  	    cnt[j]+=(x>>j)&1;		 }  }  LL sum=0;  for(LL i=1;i<=n;i++){  	 LL ans=0;  	 for(LL j=0;j<20;j++){  	    if(cnt[j]){  	     cnt[j]--;		 ans+=(1<<j);  			}		 }	 sum+=ans*ans;  }  cout<<sum<<endl;return 0;}

转载地址:http://ildt.baihongyu.com/

你可能感兴趣的文章
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>