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

    传送门

    题目:

    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,…,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=1n)ai^2 — the sum of squares of all ai. What is the largest sum of squares you can achieve?

    思路:x + y = x | y + x & y,ai = x | y, bi = x & y,可以看出总价值不变,只是把y可给x利用的价值加上,然后y减去被利用的价值,而二进制中利用的都是不同权值上的1,说明二进制上1的个数不变,只是在于转移给其他二进制串,这样我们可以统计所有权值的二进制位为1,然后构造出还能构造出的最大的二进制即可(x^2  + y^2 <= (x + y) ^ 2 , x,y>=0).

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <queue>
     5 #include <string>
     6 #include <vector>
     7 #include <cmath>
     8 #include <stack> 
     9  
    10 using namespace std;
    11  
    12 #define ll long long
    13 #define pb push_back
    14 #define fi first
    15 #define se second
    16  
    17 const int N = 2e5 + 10;
    18 int bit[N];
    19 int a[N];
    20  
    21 void solve()
    22 {
    23     int n;
    24     cin >> n;
    25     for(int i = 1; i <= n; ++i) cin >> a[i];
    26     for(int i = 1; i <= n; ++i){
    27         for(int b = 0; b <= 25; ++b){
    28             if(a[i] & (1 << b)) bit[b]++;
    29         }
    30     }
    31  
    32     ll ans = 0;
    33     for(int i = 1; i <= n; ++i){
    34         int num = 0;
    35         for(int b = 30; b >= 0; --b){
    36             if(bit[b]){
    37                 num += (1 << b);
    38                 bit[b]--;
    39             }
    40         }
    41         ans += (ll)num * num;
    42     }
    43     //cout << "ans = ";       
    44     cout << ans << endl;
    45 }
    46  
    47 int main()
    48 {
    49     ios::sync_with_stdio(false);
    50     cin.tie(0);
    51     cout.tie(0); 
    52     solve();
    53  
    54     return 0;
    55 }
    56  
    57 // 10101010101010101010
    58 // 01010101010101010101
    59 // 111
  • 相关阅读:
    tfidf 问题
    Win32汇编窗口程序设计[03]第一个窗口程序
    Win32汇编窗口程序设计[01]开发环境的搭建
    Win32汇编窗口程序设计[02]RadASM简介
    寄存器引用结构体变量
    始终居中的弹出层
    JSonJavaScript
    json
    <收录>jqueryuidatapicker生成的页面html
    网站数据抓取
  • 原文地址:https://www.cnblogs.com/SSummerZzz/p/13380816.html
Copyright © 2011-2022 走看看