CF1368D.AND, OR and square sum
普及+/提高
通过率:0%
时间限制:2.00s
内存限制:512MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
Gottfried learned about binary number representation. He then came up with this task and presented it to you.
You are given a collection of n non-negative integers a1,…,an. You are allowed to perform the following operation: choose two distinct indices 1≤i,j≤n. If before the operation ai=x, aj=y, then after the operation ai=x AND y, aj=x OR y, where AND and OR 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 ∑i=1nai2 — the sum of squares of all ai. What is the largest sum of squares you can achieve?
戈特弗里德学习了二进制数表示法,随后提出了这个题目并交给你解决。
给定一个包含 n 个非负整数 a1,…,an 的集合。你被允许执行如下操作:选择两个不同的下标 1≤i,j≤n。若操作前 ai=x、aj=y,则操作后 ai=x AND y、aj=x OR y,其中 AND 和 OR 分别表示按位与和按位或运算(形式化定义参见“注释”部分)。该操作可执行任意多次(包括零次)。
在所有操作完成后,计算 ∑i=1nai2 —— 即所有 ai 的平方之和。你能达到的最大平方和是多少?
输入格式
The first line contains a single integer n (1≤n≤2⋅105).
The second line contains n integers a1,…,an (0≤ai<220).
第一行包含一个整数 n(1≤n≤2⋅105)。
第二行包含 n 个整数 a1,…,an(0≤ai<220)。
输出格式
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.
输出一个整数——经过若干次(可能为零次)操作后所能达到的最大平方和。
输入输出样例
输入#1
1 123
输出#1
15129
输入#2
3 1 3 5
输出#2
51
输入#3
2 349525 699050
输出#3
1099509530625
说明/提示
In the first sample no operation can be made, thus the answer is 1232.
In the second sample we can obtain the collection 1,1,7, and 12+12+72=51.
If x and y are represented in binary with equal number of bits (possibly with leading zeros), then each bit of x AND y is set to 1 if and only if both corresponding bits of x and y are set to 1. Similarly, each bit of x OR y is set to 1 if and only if at least one of the corresponding bits of x and y are set to 1. For example, x=3 and y=5 are represented as 0112 and 1012 (highest bit first). Then, x AND y=0012=1, and x OR y=1112=7.
在第一个样例中,无法进行任何操作,因此答案为 1232。
在第二个样例中,我们可以得到集合 1,1,7,且 12+12+72=51。
若 x 和 y 以二进制表示,且位数相同(允许高位补零),则 x AND y 的每一位为 1 当且仅当 x 和 y 在该位上均为 1;类似地,x OR y 的每一位为 1 当且仅当 x 和 y 在该位上至少有一个为 1。例如,x=3 和 y=5 分别表示为 0112 和 1012(高位在前)。于是,x AND y=0012=1,而 x OR y=1112=7。
输入解题思路,AI测评打分。不知道怎么写?