Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include<memory>
- #include<memory.h>
- using namespace std;
- using ll = long long;
- ll a[100100], dp[100100][2], s1 = 0, s2 = 0, s3 = 0, n, cnt[22][2], cnt_pairs[100100][22][2];
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cin>>n;
- for(int i = 0; i < n; ++i) cin>>a[i], s1 += a[i];
- for(int i = 0; i<=22; ++i)
- cnt[i][a[0]&(1ll<<i)?1:0]++;
- memset(cnt_pairs[0], 0, sizeof(cnt_pairs[0]));
- for(int i = 1; i < n; ++i)
- {
- for(int bt = 0; bt <=20; ++bt)
- cnt_pairs[i][bt][0] = cnt_pairs[i-1][bt][0],
- cnt_pairs[i][bt][1] = cnt_pairs[i-1][bt][1];
- for(int bt = 0; bt <=20; ++bt)
- {
- //s2 += cnt[bt][a[i]&(1ll<<bt)?0:1]*(1ll<<bt);
- if(a[i]&(1ll<<bt))
- {
- cnt_pairs[i][bt][0] += cnt[bt][1];
- cnt_pairs[i][bt][1] += cnt[bt][0];
- }
- else{
- cnt_pairs[i][bt][0] += cnt[bt][0];
- cnt_pairs[i][bt][1] += cnt[bt][1];
- }
- }
- for(int bt = 0; bt<=20; ++bt)
- cnt[bt][a[i]&(1ll<<bt)?1:0]++;
- }
- for(int i = 2; i < n; ++i)
- {
- for(int bt = 0; bt<=20; ++bt)
- {
- if(a[i]&(1ll<<bt))
- {
- s3 += cnt_pairs[i-1][bt][0]*(1ll<<bt);
- } else{
- s3 += cnt_pairs[i-1][bt][1]*(1ll<<bt);
- }
- }
- }
- //s3 += s1*(n-1);
- cout<<s1 + 3*(n-1)*s1 + 6*s3;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement