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&) != (b&))
return (a&) < (b&);
a >>= ;
b >>= ;
}
return ;
} int n;
LL a[], ans; void dfs(int from, int to, int now)
{
if (now > )
return;
if (from >= to)
return;
int i;
for (i = from; i <= to; ++i)
{
if (a[i] & (<<now))
break;
}
LL x = i-from, y = to-i+;
ans += (((x*y)%N)*(<<now)) % N;
ans %= N;
dfs(from, i-, now+);
dfs(i, to, now+);
} void input()
{
scanf("%d", &n);
for (int i = ; i < n; ++i)
scanf("%I64d", &a[i]);
sort(a, a+n, cmp);
ans = ;
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times <= T; ++times)
{
printf("Case #%d: ", times);
input();
dfs(, n-, );
ans = (*ans) % N;
printf("%I64d\n", ans);
}
return ;
}

ACM学习历程—HDU5269 ZYB loves Xor I(位运算 && dfs && 排序)(BestCoder Round #44 1002题)的更多相关文章

  1. ACM学习历程—HDU5265 pog loves szh II(策略 && 贪心 && 排序)

    Description Pog and Szh are playing games.There is a sequence with $n$ numbers, Pog will choose a nu ...

  2. HDU--5269 ZYB loves Xor I (字典树)

    题目电波: HDU--5269 ZYB loves Xor I 首先我们先解决 ai xor aj 每个数转化为二进制  我们用字典树统计 每个节点 0 和 1 的出现的个数 #include< ...

  3. ACM学习历程—SGU 275 To xor or not to xor(xor高斯消元)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=275 这是一道xor高斯消元. 题目大意是给了n个数,然后任取几个数,让他们xor和 ...

  4. hdu5269 ZYB loves Xor I

    分治法和字典树都可以,都是递归,但字典树耗内存 从第一bit开始,若相同则xor为0,分到同一部分,不相同则统计,且此时lowbit为这一bit,最后结果要乘以2 /*分治法*/ #include&l ...

  5. ACM学习历程—HDU5592 ZYB's Premutation(逆序数 && 树状数组 && 二分)(BestCoder Round #65 1003)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5592 题目大意就是给了每个[1, i]区间逆序对的个数,要求复原原序列. 比赛的时候2B了一发. 首先 ...

  6. ACM学习历程—HDU5700 区间交(树状数组 && 前缀和 && 排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5700 这是这次百度之星初赛2B的第五题.省赛回来看了一下,有这样一个思路:对于所有的区间排序,按左值排序. 然后 ...

  7. ACM学习历程—HDU5475 An easy problem(线段树)(2015上海网赛08题)

    Problem Description One day, a useless calculator was being built by Kuros. Let's assume that number ...

  8. ACM学习历程—HDU 5459 Jesus Is Here(递推)(2015沈阳网赛1010题)

    Sample Input 9 5 6 7 8 113 1205 199312 199401 201314 Sample Output Case #1: 5 Case #2: 16 Case #3: 8 ...

  9. ACM学习历程—HDU 4726 Kia's Calculation( 贪心&&计数排序)

    DescriptionDoctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia is so carel ...

随机推荐

  1. centos 6.9 x86 安装搭建hadoop集群环境

    又来折腾hadoop了 文件准备: centos 6.9 x86 minimal版本 163的源 下软件的时候可能会用到 jdk-8u144-linux-i586.tar.gz ftp工具 putty ...

  2. 【Unity3D】【NGUI】Atlas的动态创建

    NGUI版本号:3.6.5 1.參见SZUIAtlasMakerRuntimeTest设置对应的值以上值须要提前设置好 2.没有检查是否atlas可以正确创建,自己可以改,增加返回值 3.代码都是在N ...

  3. 03 redis之string类型命令解析

    Redis字符串类型的操作 set key value [ex 秒数] / [px 毫秒数] [nx] /[xx] 如: set a 1 ex 10 , 10秒有效 Set a 1 px 9000 , ...

  4. leetCode 88.Merge Sorted Array (合并排序数组) 解题思路和方法

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note: Y ...

  5. 资源:Localization – 本地化

    Resource Dictionary –资源字典 所有的资源项在最终都会被整合到Resource Dictionary中的,也就是说无论是FrameworkElement的Resources,还是W ...

  6. 【BZOJ4052】[Cerc2013]Magical GCD 乱搞

    [BZOJ4052][Cerc2013]Magical GCD Description 给出一个长度在 100 000 以内的正整数序列,大小不超过 10^12.  求一个连续子序列,使得在所有的连续 ...

  7. 九度OJ 1058:反序输出 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8454 解决:3042 题目描述: 输入任意4个字符(如:abcd), 并按反序输出(如:dcba) 输入: 题目可能包含多组用例,每组用例 ...

  8. 九度OJ 1039:Zero-complexity Transposition(逆置) (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3093 解决:1255 题目描述: You are given a sequence of integer numbers. Zero-co ...

  9. haproxy + keepalived 实现web 双主模型的高可用负载均衡

    参考文章 http://xz159065974.blog.51cto.com/8618592/1405812 http://blog.chinaunix.net/uid-25266990-id-398 ...

  10. Webpack探索【8】--- 模块热替换详解

    本文主要讲模块热替换相关内容.