Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include<memory>
  3. #include<memory.h>
  4.  
  5. using namespace std;
  6. using ll = long long;
  7.  
  8. ll a[100100], dp[100100][2], s1 = 0, s2 = 0, s3 = 0, n, cnt[22][2], cnt_pairs[100100][22][2];
  9.  
  10. int main() {
  11.     ios_base::sync_with_stdio(false);
  12.     cin.tie(nullptr);
  13.     cin>>n;
  14.     for(int i = 0; i < n; ++i) cin>>a[i], s1 += a[i];
  15.     for(int i = 0; i<=22; ++i)
  16.         cnt[i][a[0]&(1ll<<i)?1:0]++;
  17.     memset(cnt_pairs[0], 0, sizeof(cnt_pairs[0]));
  18.     for(int i = 1; i < n; ++i)
  19.     {
  20.         for(int bt = 0; bt <=20; ++bt)
  21.             cnt_pairs[i][bt][0] = cnt_pairs[i-1][bt][0],
  22.             cnt_pairs[i][bt][1] = cnt_pairs[i-1][bt][1];
  23.         for(int bt = 0; bt <=20; ++bt)
  24.         {
  25.             //s2 += cnt[bt][a[i]&(1ll<<bt)?0:1]*(1ll<<bt);
  26.             if(a[i]&(1ll<<bt))
  27.             {
  28.                 cnt_pairs[i][bt][0] += cnt[bt][1];
  29.                 cnt_pairs[i][bt][1] += cnt[bt][0];
  30.             }
  31.             else{
  32.                 cnt_pairs[i][bt][0] += cnt[bt][0];
  33.                 cnt_pairs[i][bt][1] += cnt[bt][1];
  34.             }
  35.         }
  36.  
  37.         for(int bt = 0; bt<=20; ++bt)
  38.             cnt[bt][a[i]&(1ll<<bt)?1:0]++;
  39.     }
  40.  
  41.     for(int i = 2; i < n; ++i)
  42.     {
  43.         for(int bt = 0; bt<=20; ++bt)
  44.         {
  45.             if(a[i]&(1ll<<bt))
  46.             {
  47.                 s3 += cnt_pairs[i-1][bt][0]*(1ll<<bt);
  48.             } else{
  49.                 s3 += cnt_pairs[i-1][bt][1]*(1ll<<bt);
  50.             }
  51.         }
  52.     }
  53.  
  54.     //s3 += s1*(n-1);
  55.  
  56.     cout<<s1 + 3*(n-1)*s1 + 6*s3;
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement