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. .NET后台如何获取前台HMTL控件的值

    很多时候我们需要HTML控件,感觉比服务器控件更加简介,清爽,那么如何获取HMTL控件的值呢,请看下面例子: 前台页面代码: <input id="Text1" type=& ...

  2. ios电话/密码/验证码/身份证的正则表达式

    // 一 .电话号码正则表达式 -(BOOL)testPhoneNumber:(NSString *)text { NSString *regex =@"(13[0-9]|0[1-9]|0[ ...

  3. 矿场搭建(codevs 1996)

    题目描述 Description 煤矿工地可以看成是由隧道连接挖煤点组成的无向图.为安全起见,希望在工地发生事故时所有挖煤点的工人都能有一条出路逃到救援出口处.于是矿主决定在某些挖煤点设立救援出口,使 ...

  4. python基础——filter函数

    python基础——filter函数 Python内建的filter()函数用于过滤序列. 和map()类似,filter()也接收一个函数和一个序列.和map()不同的是,filter()把传入的函 ...

  5. NYOJ题目64鸡兔同笼

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAsAAAAIZCAIAAAAnfB5fAAAgAElEQVR4nO3dO1LjygIG4LsJchZC7I ...

  6. NYOJ题目1045看美女

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAK5CAIAAADCdSR7AAAgAElEQVR4nO3dP3Lbuv434HcT7r2Q1F

  7. iOS开发-正则表达式的使用方法

    前言:在表单验证中,我们经常会使用到正则,因为我们需要用它来判断用户输入的字符是否为合法的,如果是不合法的,那么应该提示用户输入错误,并不让提交至服务器.我们也可以通过正则表达式,从用户输入的字符串中 ...

  8. Arch Linux Installation Guide

    Arch Linux Installation Guide   timedatectl set-ntp true   sed -i '/Score/{/China/!{n;s/^/#/}}' /etc ...

  9. 在RedHat.Enterprise.Linux_v6.3系统中安装Oracle_11gR2教程

    在RedHat.Enterprise.Linux_v6.3系统中安装Oracle_11gR2教程 本教程提供PDF格式下载: 在RedHat.Enterprise.Linux_v6.3系统中安装Ora ...

  10. SQLAlchemy高级ORM之改查删除及GROUP,JOIN...

    按书上案例来的. #coding=utf-8 from datetime import datetime from sqlalchemy import (MetaData, Table, Column ...