zoukankan      html  css  js  c++  java
  • D. AND, OR and square sum

    #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 1i,jn1≤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 (1n21051≤n≤2⋅105).

    The second line contains nn integers a1,,ana1,…,an (0ai<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.

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

  • 相关阅读:
    2017-09-13
    JavaSE07——异常
    FastDFS入门、搭建以及应用(转载)
    Centos7安装JDK1.8
    「扫盲」 Elasticsearch(转载)
    Java06——面向对象
    Java05——数组
    Java02——基础
    spring boot 配置文件配置项 数字特殊处理问题
    java动态代理机制之自定义实现动态代理
  • 原文地址:https://www.cnblogs.com/dealer/p/13174213.html
Copyright © 2011-2022 走看看