ACM learning process-hdu5269 ZYB loves Xor I (bitwise operations && DFS && sort) (Bestcoder Round #44 1002 Questions)

Source: Internet
Author: User
Tags bitwise

Problem Description

Memphis loves xor very musch. Now he gets A array a.the length of A was N.now he wants to know the sum of all (Lowbit (Ai xor Aj) (I,j∈[1,n])
We define that Lowbit (x) =2^k,k is the smallest integer satisfied ((x and 2^k) >0)
Specially,lowbit (0) =0

Because the ANS may too big. You just need to output ans mod 998244353

Input

Multiple test cases, the first line contains a integer T (no more than), indicating the number of cases. Each test case contains the lines
The first line have an integer n
The second line has n integers a1,a2 .... An
N∈[1,5∗10^4],AI∈[0,2^29]

Output

The output should occupies exactly one line. The output format is case #x: ans, where x is the data number begins at 1.

Sample Input

2

5

4 0 2) 7 0

5

2 6 5) 4 0

Sample Output

Case #1:36

Case #2:40

First the lowbit nature of the topic requirement is two number from the back forward first not 0 bits.

For two elements, the same or the operation is the same as 0, the difference is 1.

So from the last one consider the first different bit forward.

At first, I wanted to use the set array to count 0 or 1 of each bit, and found that the final complication was O (N*n*logn) ... Decisive is not to think well ... Wrote half decisive throw away.

Later on the first set of samples were found after handwriting.

000

000

100

010

111

For the collection at the end of which is 0 and the end is a collection of 1, the elements that are mapped between the two sets must also be followed by the lowbit result of 1. The result of the element lowbit in the set must not be 1.

So the number of lowbit that can be obtained by the last one is the product of the number of two set elements.

The result of these two sets of operations is that there is no need to reconsider.

So divide and conquer, then consider the inside of the set. For a set, the second-most natural consideration is a subset of 0 and the second-to-last is a subset of 1. The same goes for recursive definition.

In this case, however, the two elements are counted only once, and the topic needs to be two times, and the final ans naturally multiply by 2.

In addition, in order to do the above, we need to sort each bit in advance, using the STL's sort and writing the CMP function.

You can then search with DFS.

Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include<cstring>#include<algorithm>#include<Set>#include<map>#include<queue>#include<string>#defineLL Long Long#defineN 998244353using namespacestd;BOOLCMP (ll A, ll b) { while(A | |b) {if((a&1)! = (b&1))            return(a&1) < (b&1); A>>=1; b>>=1; }    return 0;}intN; LL a[50005], ans;voidDfsint  from,intTo,intNow ) {    if(Now > -)        return; if( from>=to )return; inti;  for(i = from; I <= to; ++i) {if(A[i] & (1<<Now ))  Break; } LL x= I from, y = to-i+1; Ans+ = (((x*y)%N) * (1<<now))%N; Ans%=N; DFS ( from, I-1, now+1); DFS (I, to, now+1);}voidinput () {scanf ("%d", &N);  for(inti =0; I < n; ++i) scanf ("%i64d", &A[i]); Sort (A, a+N, CMP); Ans=0;}intMain () {//freopen ("test.in", "R", stdin);    intT; scanf ("%d", &T);  for(intTimes =1; Times <= T; ++Times ) {printf ("Case #%d:", times);        Input (); DFS (0, N-1,0); Ans= (2*ans)%N; printf ("%i64d\n", ans); }    return 0;}

ACM learning process-hdu5269 ZYB loves Xor I (bitwise operations && DFS && sort) (Bestcoder Round #44 1002 Questions)

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.