hdu 5269 ZYB loves Xor I
2015-06-30 10:36
411 查看
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 538 Accepted Submission(s): 259
[align=left]Problem Description[/align]
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
[align=left]Input[/align]
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]
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
5
4 0 2 7 0
5
2 6 5 4 0
[align=left]Sample Output[/align]
Case #1: 36
Case #2: 40
[align=left]Source[/align]
BestCoder Round #44
Total Submission(s): 538 Accepted Submission(s): 259
[align=left]Problem Description[/align]
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
[align=left]Input[/align]
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]
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
2
5
4 0 2 7 0
5
2 6 5 4 0
[align=left]Sample Output[/align]
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 ; 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); } }
[align=left]Source[/align]
BestCoder Round #44
相关文章推荐
- 【java0008】多线程卖票(二)
- 社説 20150630 ギリシャ危機 混乱の拡大防ぐ最善の努力を
- Python 多线程分块读文件
- C++ set
- 关于matlab中regionprops及BoundingBox用法,mark一下
- MySQL DATE_ADD() 函数
- 【MyEclipse 2015】 逆向破解实录系列【1】(纯研究)
- MySQL内存分配
- 在INSTALL TINY时出现下面的问题怎么办?
- JavaScript学习11:事件入门
- Android应用实例之有道辞典(WebView)
- SFP模块光信号强度知识介绍
- MiniTwitter登陆界面的实现
- iOS开发 xcode6制作framework静态库
- centos同步时间
- CodeBlocks创建静态链接库和使用
- android 反编译
- SQL 表 被锁
- 社説 20150630 新国立競技場 工費圧縮へ設計から出直せ
- Tomcat中更改网站根目录和默认页的配置方法