Hug The Princess (thinking, bitwise arithmetic)

Source: Internet
Author: User
Tags ord

Hug the PrincessTime limit:3000/1000ms (java/others) Memory limit:65535/65535kb (java/others)Submit Status

There is a sequence withNNElements. Assuming they is a1,a2,?,an a1,a2,?, an.

Please calculate the following expession.

∑1≤I<J≤N(AI∧AJ)+ (a i| aj ) + (a I&aj) ∑1≤i<j≤n (Ai∧aj) + (Ai|aj) + (Ai&aj)

In the expression above, ^  |  &  is bit operation. If you don ' t know bit operation, you can visit

Http://en.wikipedia.org/wiki/Bitwise_operation

To get some useful information.

Input

The first line contains a single integer nN and which is the size of the sequence.

The second line containsNNIntegers, the ItH ithIntegerai ai is the   i t h "> ith ith element of the sequence.

1≤n≤100000,0≤ai≤100000000 1≤n≤100000,0≤ai≤100000000

Output

Print the answer in one line.

Sample Input and output
Sample Input Sample Output
21 2
6
Hint

Because the answer is so large, please use a long long instead of int. Correspondingly, please use %lld instead of< c3> to %d scanf and printf.

Large input. You could get time Limit exceeded if you use "cin" to get the input. So "scanf" is suggested.

Likewise, you is supposed to use "printf" instead of "cout".

Exercises

Let this expression, we can find each bit of the prefix and then based on each bit to figure out the front is 1, is 0 of the current number of bits, and then add CNT * (1 << j) just fine.

Code:

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespacestd;Const intMAXN = 1e5 + -;intnum[maxn][ *];inta[ *];intP[maxn];typedefLong LongLL;intMain () {intN;  while(~SCANF ("%d", &N)) {        intx; memset (num,0,sizeof(num));  for(inti =1; I <= N; i++) {scanf ("%d", &x);  for(intj =0; J < -; J + +) {Num[i][j]= Num[i-1][J] + x%2; X= x/2; }} LL ans=0;  for(inti =1; I <= N; i++){            inttemp =0, pos =-1;  for(intj =0; J < -; J + +) {A[j]= Num[i][j]-num[i-1][j]; //printf ("%d", a[j]);}//puts ("");             for(intj =0; J < -; J + +){                intCNT =0; if(A[j]) {CNT+ = (I-1-Num[i-1][j]); CNT+ = i-1; CNT+ = Num[i-1][j]; }                Else{cnt+ = Num[i-1][j]; CNT+ = Num[i-1][j]; } ans+ = (LL) CNT * (1<<j); }} printf ("%lld\n", ans); }    return 0;}

Hug The Princess (thinking, bitwise arithmetic)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.