码迷,mamicode.com
首页 > 编程语言 > 详细

ACM学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(BestCoder Round #44 1002题)

时间:2015-06-13 22:53:56      阅读:226      评论:0      收藏:0      [点我收藏+]

标签:

Problem Description

Memphis loves xor very musch.Now he gets an array A.The length of A is 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 be too big.You just need to output ans mod 998244353

 

Input

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

 

Output

For each case, the output should occupies exactly one line. The output format is Case #x: ans, here 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

 

首先题目要求的lowbit自然是两数从后往前第一个非0位。

对于两个元素来说,亦或运算是相同为0,不同为1.

所以从最后一位往前考虑第一个不同位。

 

一开始想用set数组统计每位有0或1的集合,发现最后复杂度是O(n*n*logn)。。。果断是没想好。。。写了一半果断扔了。

 

后来对第一组样例手写后发现。

000

000

100

010

111

对于末尾是0的集合和末尾是1的集合,两集合间互相映射的元素必然亦或后取lowbit的结果是1。而集合内元素lowbit的结果必然不是1。

所以能通过最后一位亦或得到的lowbit个数就是两个集合元素个数的乘积。

这样这两个集合间运算的结果就有了,不需要再考虑了。

所以进行分治,接下来考虑集合内部的。对于某个集合内部,自然考虑倒数第二位元素是0的子集和倒数第二位是1的子集。同理这样递归定义下去。

然而这样的话两个元素间只计算了一次,而题目需要求两次,最终ans自然乘上2。

另外能进行上述操作的话,需要预先对数组进行按每一位的排序,这里采用了STL的sort,写了cmp函数。

然后用dfs进行搜索即可。

 

代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <string>
#define LL long long
#define N 998244353

using namespace std;

bool cmp(LL a, LL b)
{
    while (a || b)
    {
        if ((a&1) != (b&1))
            return (a&1) < (b&1);
        a >>= 1;
        b >>= 1;
    }
    return 0;
}

int n;
LL a[50005], ans;

void dfs(int from, int to, int now)
{
    if (now > 30)
        return;
    if (from >= to)
        return;
    int i;
    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);
}

void input()
{
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
        scanf("%I64d", &a[i]);
    sort(a, a+n, cmp);
    ans = 0;
}

int main()
{
    //freopen("test.in", "r", stdin);
    int T;
    scanf("%d", &T);
    for (int times = 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学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(BestCoder Round #44 1002题)

标签:

原文地址:http://www.cnblogs.com/andyqsmart/p/4574151.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!