位运算—— AND, OR and square sum

d e c e p t i o n \color{blue}deception deception
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 a 1 , … , a n a_1,…,a_n a1,,an. You are allowed to perform the following operation: choose two distinct indices 1 ≤ i , j ≤ n 1≤i,j≤n 1i,jn. If before the operation a i = x , a j = y a_i=x, a_j=y ai=x,aj=y, then after the operation a i = x A N D y a_i=x AND y ai=xANDy, a j = x O R y , a_j=x OR y, aj=xORy, 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 = 1 n a i 2 ∑^n_{i=1}a_i^2 i=1nai2 — the sum of squares of all a i a_i ai. What is the largest sum of squares you can achieve?

I n p u t \color{blue}Input Input
The first line contains a single integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) (1≤n≤2⋅10^5) (1n2105).

The second line contains n integers a 1 , … , a n ( 0 ≤ a i < 2 20 ) a_1,…,a_n (0≤a_i<2^{20}) a1,,an(0ai<220).

O u t p u t \color{blue}Output Output
Print a single integer — the largest possible sum of squares that can be achieved after several (possibly zero) operations.

比如我有两个数 x x x, y y y,他们的二进制位中1的个数总共有 n n n个,然后我对x和y分别做与和或的操作,他们的二进制位中1的总数是不会变的。所以这题我们只要记录每个位上1的个数,然后贪心让数最大分配即可。

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-7;
#define endl '\n'
#define pb(a) push_back(a)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) int(x.size())
#define IOS ios::sync_with_stdio(0);
int n, a[N];
int bit[25];
int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", a + i);
        //记录1的个数
        for (int j = 0; j < 20; ++j) {
            bit[j] += (a[i] & 1);
            a[i] >>= 1;
        }
    }

    ll ans = 0;
    for (int i = 1; i <= n; ++i) {
        int x = 0;
        //开始分配,尽可能让数大
        for (int j = 0; j < 20; ++j) {
            if (bit[j]) {
                x += (1 << j);
                bit[j]--;
            }
        }
        ans += 1ll * x * x;
    }
    cout << ans << endl;

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值