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

    思路:

    只要还能操作,就有收益。所以数组最终的状态会呈现任意两个相邻的数字前一个是后一个的子集(如果将每个比特位上的1当作一个元素的话)。

    实现:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int main()
     4 {
     5     int n;
     6     while (cin >> n)
     7     {
     8         int x;
     9         vector<int> v(20, 0);
    10         for (int i = 0; i < n; i++) 
    11         {
    12             cin >> x;
    13             int j = 0;
    14             while (x)
    15             {
    16                 if (x & 1) v[j]++;     
    17                 j++;
    18                 x >>= 1;
    19             }
    20         }
    21         vector<int> res(n, 0);
    22         for (int i = 0; i < 20; i++)
    23         {
    24             for (int j = 0; j < v[i]; j++)
    25             {
    26                 res[n - 1 - j] += 1 << i;
    27             }
    28         }
    29         long long ans = 0;
    30         for (int i = 0; i < n; i++)
    31         {
    32             ans += (long long)res[i] * res[i];
    33         }
    34         cout << ans << endl;
    35     }
    36     return 0;
    37 }
  • 相关阅读:
    工厂方法模式
    代理模式
    观察者模式
    策略模式
    单例模式
    简单工厂模式
    lintcode:等价二叉树
    lintcode:被围绕的区域
    lintcode:二叉树的所有路径
    lintcode:快乐数
  • 原文地址:https://www.cnblogs.com/wangyiming/p/14764465.html
Copyright © 2011-2022 走看看