hdu5269ZYB loves Xor I

来源:互联网 发布:鬼畜调音软件 编辑:程序博客网 时间:2024/06/15 17:30

这个题还挺有意思的,虽然做法还是很显然的,但是还是值得写篇博客


标程是字典树,看起来比较蛋疼


直接分治就好了,


排序之后,使得ai不被排在它后面的管控,也就是lowbit(a[i])<=lowbit(a[j]),i<j


然后接下来分治,每次把区间分成两半,使得前面一半管控后面一半,一边拆一边算ans


这里,我直接写了一发dfs,很显然递归深度最多也就logA,放心跑


最后打出ans即可


排序的复杂度是nlognlogA


分治就是nlogA


时间主要花在排序上,事实上,排序的实际时间应该更好,所以跟标程的nlogA并没有太大区别


这里因为hd服务器的原因,交g++会蜜汁re,c++很快就过了


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


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,5104]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
254 0 2 7 052 6 5 4 0
 

Sample Output
Case #1: 36Case #2: 40
 

Source
BestCoder Round #44

#include<map>#include<string>#include<cstring>#include<cstdio>#include<cstdlib>#include<cmath>#include<queue>#include<vector>#include<iostream>#include<algorithm>#include<bitset>#include<climits>#include<list>#include<iomanip>#include<stack>#include<set>using namespace std;typedef long long ll;const int mod=998244353;int lowbit(int x){return x&-x;}int a[50010],n,ans;void dfs(int l,int r){if(l==r||a[l]==0)return;for(int i=l;i<=r;i++){int t=lowbit(a[i]);a[i]-=t;if(i==r)dfs(l,r);else if(t!=lowbit(a[i+1])){ans=(ans+ll(i-l+1)*(r-i)*t*2%mod)%mod;dfs(l,i);dfs(i+1,r);break;}}}bool cmp(int one,int two){while(1){int t1=lowbit(one),t2=lowbit(two);if(t2==0)return 1;else if(t1==0)return 0;if(t1<t2)return 1;else if(t1>t2)return 0;one-=t1;two-=t2;}}int main(){int T;scanf("%d",&T);for(int cs=1;cs<=T;cs++){int n;scanf("%d",&n);for(int i=0;i<n;i++)scanf("%d",a+i);ans=0;sort(a,a+n,cmp);dfs(0,n-1);printf("Case #%d: %d\n",cs,ans);}}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝卖家寄多了衣服怎么办 集运地址选错怎么办 淘宝卖韩国化妆品退货怎么办 去韩国留学手机怎么办 韩国办无线网怎么办 淘宝卖家被骗怎么办 淘宝买软件被骗怎么办 被淘宝店诈骗怎么办? 支付宝被骗2000怎么办 给私人打款后不发货怎么办 毕业证寄丢了怎么办 微商下单返现被骗一千四怎么办 淘宝买东西卡里多扣钱怎么办 付款了卖家不发货怎么办 淘宝客服不解决问题怎么办 淘宝未付款订单怎么办 淘宝被限制购买怎么办 苹果官换机维修过怎么办 iphone x官换机坏了怎么办 小娃不要大人睡怎么办? 深度睡眠太少怎么办 踏板摩托车淹缸怎么办 电喷摩托车淹缸怎么办 踏板摩托不过油怎么办 火花塞被汽油淹怎么办 踏板车淹缸了怎么办 电喷汽车淹缸怎么办 踏板摩托车粘缸怎么办 鬼火打不着火怎么办 踏板摩托车没电怎么办 买摩托车被骗了怎么办 鬼火电瓶坏了怎么办 摩托闷油了怎么办 鬼火发不着火怎么办 淘宝买东西发错货了怎么办 咸鱼售假处罚怎么办 趣头条登录不上怎么办 淘宝集运超租怎么办 淘宝发货多发了怎么办 小学生数学学不好怎么办 小学生不爱学英语怎么办