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. hdu1316

    链接:pid=1316" target="_blank">点击打开链接 题意:问区间[a,b]中有多少斐波那契数 代码: #include <iostream ...

  2. python3 configparser对配置文件读写

    import configparser #read data from conf filecf=configparser.ConfigParser()cf.read("biosver.cfg ...

  3. android开发系列之使用xml自定义控件

    在android开发的过程中,有的时候面对多个Activity里面一些相同的布局,我们需要写多次相同的代码,同时这种方法给我们的项目维护也带来了很大不便.那么有没有一种可行的办法能够将Activity ...

  4. jdk并发工具包之锁

    1.cynchronized扩展:可重锁入ReentrantLock ReentrantLock是通过cas算法实现的 RenntrantLock lock=new ReentrantLock(); ...

  5. 【selenium+Python WebDriver API】之复选框顺序正选和顺序反选

    from selenium import webdriver from selenium.webdriver.common.by import By import os,time driver = w ...

  6. php生成唯一的串

    1.方法一: <?php md5(uniqid('aa',true)); ?> 2.方法2: //生成16位的串$randLength=6; $chars='abcdefghijklmno ...

  7. iOS - web自适应宽高(预设置的大小)

    //web自适应宽高 -(void)webViewDidFinishLoad:(UIWebView *)webView { NSLog(@"wessd"); [ webView s ...

  8. 【iOS开发-51】案例学习:动画新写法、删除子视图、视图顺序、延迟方法、button多功能使用方法及icon图标和启动页设置

    案例效果: (1)导入所需的素材,然后用storyboard把上半截位置和大小相对固定的东西布局起来.当然,这些控件也要定义成对应地IBOutlet和IBAction方便兴许使用它们. 注意:本案例在 ...

  9. Android API Guides---Storage Access Framework

    存储訪问架构 Android 4.4系统(API级别19)推出存储訪问框架(SAF).新加坡武装部队变得很easy,为用户在其全部自己喜欢的文件存储提供商的浏览和打开文档,图像和其它文件.一个标准的, ...

  10. 基于EasyDarwin框架实现EasyNVR H5无插件直播流媒体服务器方案

    在之前的一篇博客<web无插件播放RTSP摄像机方案,拒绝插件,拥抱H5!>中,描述了实现一套H5无插件直播方案的各个组件的参考建议,又在博客<EasyNVR H5流媒体服务器方案架 ...