ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

D. AND, OR and square sum

2020-06-21 22:03:28  阅读:327  来源: 互联网

标签:square const ll int sum operation include Copy


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
#include <unordered_set>
#include <unordered_map>
#define ll              long long
#define pii             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
using namespace std;
int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979323846;
const double eps = 1e-6;
const int mod = 1e9 + 7;
const int N = 1e3 + 5;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}
ll lcm(ll m, ll n)
{
    return m * n / gcd(m, n);
}
bool prime(int x) {
    if (x < 2) return false;
    for (int i = 2; i * i <= x; ++i) {
        if (x % i == 0) return false;
    }
    return true;
}
ll qpow(ll m, ll k, ll mod)
{
    ll res = 1, t = m;
    while (k)
    {
        if (k & 1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}

int main()
{
    ll n;
    cin >> n;
    vector<ll> b(25),a(n+1),v(n+1);
    rep(i, 1, n)
        cin >> a[i];
    rep(i, 1, n)
    {
        for (int j = 0; j <= 20; j++)
        {
            if (a[i] & (1 << j))
                b[j]++;
        }
    }
    rep(i, 0, 20)
    {
        rep(j, 1, b[i])
        {
            v[j] += 1 << i;
        }
    }
    ll res=0;
    rep(i, 1, n)
        res += v[i] * v[i];
    cout << res << endl;
    return 0;
}

 

------------恢复内容开始------------

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

 

 

------------恢复内容结束------------

标签:square,const,ll,int,sum,operation,include,Copy
来源: https://www.cnblogs.com/dealer/p/13174213.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有