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. refresh的停车场(栈和队列的STL)

    refresh的停车场 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述  refresh近期发了一笔横財,开了一家停车场. 因 ...

  2. substring,subsequence,charAt执行效率的不同

    package com.java.tencent; public class T_2_longestPalindrome { public String test1(String s){ long s ...

  3. JQuery利用选择器定位动态id?

    假如我们需要去定位一个动态生成的div,我们需要为它指定一个动态的id 例如: 前台使用EL进行迭代LIST生成div,为其添加动态的id,生成之后变成下面样式 <div id="tz ...

  4. 【WPF学习笔记】之如何通过后台C#代码添加(增/删/改按钮)实现对SQLServer数据库数据的更改

    首先,需要连接SQLServer数据库的服务器名称server.数据库名database.数据库用户名uid以及密码pwd,如下图: 然后需要以下数据库SQL代码段,还有一个myHelper.cs代码 ...

  5. cocos2dx 3.2+ 项目创建与问题总汇

    本文为Cocos2d-x 3.x 全平台(Android,iOS)新手开发配置教程攻略,希望对大家有所帮助.由于这篇文章是面对新手的. 所以有些地方会啰嗦一些,请勿见怪. 假设教程中有错误.欢迎指正. ...

  6. H2 database 操作操作内存表

    本例开发工具为 NetBeans,使用b2前提安装jdk. 第一步:在官网下载驱动包 :http://www.h2database.com ,本例版本为: h2-1.4.192.jar 第二步:安装开 ...

  7. JS之字符串与JSON转换

    JS之字符串转换JSON 1.eval   古老的方式 function strToJson(str){ var json = eval('(' + str + ')'); return json; ...

  8. android菜鸟学习笔记4----android项目结构

    src: 应用程序源代码存放目录 gen: 自动生成的目录,目录中存放所有由Android开发工具自动生成的文件. 目录中最重要的就是R.java文件. 这个文件由Android开发工具自动产生的.A ...

  9. 九度OJ 1151:位操作练习 (位操作)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1679 解决:924 题目描述: 给出两个不大于65535的非负整数,判断其中一个的16位二进制表示形式,是否能由另一个的16位二进制表示形 ...

  10. sed相关

    1 global flag sed 's/xxx/xxx/' inputfile,如果没有带global flag g的话,匹配替换的只是inputfile中的每一行的第一个匹配项.如果带了g的话,才 ...