Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 538    Accepted Submission(s): 259

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)=2k,k is the smallest integer satisfied ((x and 2k)>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∗104],Ai∈[0,229]
 
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

/*time 62ms
by atrp
*/
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std;
typedef long long ll;
const int N = 50005;
int a[N];
int n, forc;
ll ans;
int cmp(int a, int b)
{
return (a & (1 << forc)) < (b & (1 << forc));
}
int calc(int low, int high)//寻找排序后a[low。。high - 1]中二进制第forc位不同的分界点,区间为[low,high);
{
int i;
for(i = low; i < high; ++i)
if((a[i] & (1 << forc)) ^ (a[i + 1] & (1 << forc))) break;
if(i == high) return i;
else return i + 1;//注意这里的边界处理
}
void solve(int low, int high)
{
sort(a + low, a + high, cmp);
int m = calc(low, high);
// printf("[%d] - [%d]\n", m, high);
int mi = *min_element(a + low, a + high);
int mx = *max_element(a + low, a + high);
if(mi == mx) return;//当[low,high)中的元素相等时,无需继续递归
forc++;
solve(low, m);
solve(m, high);
forc--;
ans += ((m - low) % 998244353) * ((high - m) % 998244353) * (1 << forc) ;
}
int main()
{
int t, ca = 1;
scanf("%d", &t);
while(t --)
{
scanf("%d", &n);
for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
forc = 0;
ans = 0;
solve(0, n);
printf("Case #%d: %lld\n", ca++,(ans << 1) % 998244353);
}
}

  

Source

hdu 5269 ZYB loves Xor I的更多相关文章

  1. HDU 5269 ZYB loves Xor I Trie树

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5269 bc:http://bestcoder.hdu.edu.cn/contests/con ...

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5269 思路分析:当lowbit(AxorB)=2p 时,表示A与B的二进制表示的0-p-1位相等,第p ...

  3. hdu 5269 ZYB loves Xor I &amp;&amp; BestCoder Round #44

    题意: ZYB喜欢研究Xor,如今他得到了一个长度为n的数组A. 于是他想知道:对于全部数对(i,j)(i∈[1,n],j∈[1,n]).lowbit(AixorAj)之和为多少.因为答案可能过大,你 ...

  4. hdu 5269 ZYB loves Xor I 分治 || Trie

    题目大意: 长度为\(n\)的数组A.求对于所有数对\((i,j)(i \in [1,n],j \in [1,n])\),\(lowbit(A_i xor A_j)\)之和.答案对998244353取 ...

  5. HDU 5269 ZYB loves Xor I (二分法)

    题意: 给出一个序列,对每两个数求异或结果后取最低位的1出来作为一个数,然后求这些数字的和.比如:{a,b,c},结果是lowbit(a^b)+lowbit(a^c)+lowbit(b^a)+lowb ...

  6. 【HDU】5269 ZYB loves Xor I

    [算法]trie [题解] 为了让数据有序,求lowbit无法直接排序,从而考虑倒过来排序,然后数据就会呈现出明显的规律: 法一:将数字倒着贴在字典树上,则容易发现两数的lowbit就是它们岔道结点的 ...

  7. bestcoder r44 p3 hdu 5270 ZYB loves Xor II

    这是昨晚队友跟我说的题,不知道当时是什么玄幻的事件发生了,,我看成了两两相乘的XOR 纠结了好长时间间 不知道该怎么办 今天早上看了下这道题,发现是两两相加的XOR  然后就想了想昨晚的思路 发现可做 ...

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

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

  9. ZYB loves Xor I(hud5269)

    ZYB loves Xor I Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

随机推荐

  1. 决绝Capturing 'demo' strongly in this block is likely to lead to a retain cycle

    - (IBAction)onTest:(id)sender { BlockDemo *demo = [[BlockDemo alloc]init];  __weak typeof(BlockDemo) ...

  2. October 14th 2016 Week 42nd Friday

    Who am I? Coming October 18, 2016! 我是谁?2016.10.18 拭目以待! Don't worry. You will be a wow. Don't worry. ...

  3. Oracle BFILE备忘

    创建目录 create or replace directory exp_dir as '/tmp'; 赋权 grant read, write on directory exp_dir to PUB ...

  4. Mysql tablespace

    对于innodb引擎的独立表空间,参考:http://blog.csdn.net/imzoer/article/details/8287938, 关键有两个变量:innodb_file_per_tab ...

  5. jq点击和鼠标移上效果以及一个等号"=" 二个等号"==" 三个等号"===" 的区别

    <body> <div class="a" bs='1' style="width:100px; height:30px; border:1px sol ...

  6. MVC基础知识 – 2.新语法

    1.自动属性 Auto-Implemented Properties 2.隐式类型 var 3.参数默认值 和 命名参数 4.对象初始化器 与 集合初始化器 { } 5.匿名类 & 匿名方法 ...

  7. SpringMVC @RequestBody问题:Unrecognized field , not marked as ignorable

    http://blog.csdn.net/isea533/article/details/33397735

  8. 【JAVA网络流之URL】

    一.URL URL对象可以认为是URLConnection对象+Socket对象. Java.lang.Object |-Java.net.URL 常用构造方法: URL(String spec)   ...

  9. [LeetCode] Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...

  10. maven File encoding has not been set

    原pom.xml配置文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&qu ...