bestcoder r44 p3 hdu 5270 ZYB loves Xor II

这是昨晚队友跟我说的题,不知道当时是什么玄幻的事件发生了,,我看成了两两相乘的XOR 纠结了好长时间间 不知道该怎么办

今天早上看了下这道题,发现是两两相加的XOR  然后就想了想昨晚的思路 发现可做

对于 XOR 在我的记忆中 ,大部分的都是拆成数位来做

那么这题  。。。。 其实也是类似的

拆成数位。有两种拆法  将数据拆成数位和将答案拆成数位(其实就是考虑答案的每一位)

想了想将数据拆成数位··········

没法单独考虑每一位的‘功’

那就将考虑答案的每一位了

最好考虑的是第一位了

只有1+0 才能得到 1 也就是数数 a b数组 1 和 0 的个数了,

第2位呢?  如果两个数组前两位的值相加不小于2^1 并且小于2^2就行了。。他的第2位不就是1了么??

以此类推,,,每一位数1的个数。。。

开始写代码

wa

想了想原因 试了一下1 1 3 3 这组数据发现答案是4  又想了想,,

在考虑第2位,,2=<ai+bi<4 || 6=<ai+bi<8 才是对的,,,

然后以此类推,

又错了,,,怎么搞的?

确认算法没错之后,,看了看输入输出,,

好久没做题

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string.h>
 4 #include <algorithm>
 5
 6 #define cl(a,b) memset(a,b,sizeof(a))
 7 #define ll long long
 8 #define ull unsigned long long
 9 using namespace std;
10
11 #define maxn 200005
12
13 ull aa[maxn],bb[maxn];
14 ull taa[maxn],tbb[maxn];
15 ull cc[64],be[64],en[64];
16 int n;
17
18 void initcc()
19 {
20     cl(cc,0);
21     cc[0]=1;
22     be[0]=1;
23     en[0]=2;
24     for(int i=1;i<=62;i++)
25     {
26         cc[i]=cc[i-1]*2+1;
27         be[i]=be[i-1]*2;
28         en[i]=en[i-1]*2;
29     }
30 //    for(int i=0;i<62;i++)printf("%I64d %I64d %I64d\n",cc[i],be[i],en[i]);
31 }
32
33 int main()
34 {
35     initcc();
36     int tt,ii;
37     cin>>tt;
38     ii=0;
39     while(tt--)
40     {
41         scanf("%d",&n);
42         int i,j,k;
43         for(i=0;i<n;i++)scanf("%llu",&aa[i]);
44         for(i=0;i<n;i++)scanf("%llu",&bb[i]);
45         ull res=0,bitc;
46         int s1,s2,e1,e2;
47         for(k=0;k<62;k++)
48         {
49             for(i=0;i<n;i++)
50             {
51                 taa[i]=aa[i]&cc[k];
52                 tbb[i]=bb[i]&cc[k];
53             }
54             sort(taa,taa+n);
55             sort(tbb,tbb+n);
56             s1=n-1;e1=n-1;bitc=0;
57             s2=n-1;e2=n-1;
58             for(i=0;i<n;i++)
59             {
60                 while(s1>=0&&tbb[s1]+taa[i]>=be[k])s1--;
61                 while(e1>=0&&tbb[e1]+taa[i]>=en[k])e1--;
62
63                 while(s2>=0&&tbb[s2]+taa[i]>=be[k]+en[k])s2--;
64                 while(e2>=0&&tbb[e2]+taa[i]>=en[k+1])e2--;
65
66                 bitc+=e1-s1;
67                 bitc+=e2-s2;
68             }
69
70             res+=be[k]*(bitc%2);
71         }
72         printf("Case #%d: %llu\n",++ii,res);
73     }
74     return 0;
75 }

g++ 不支持 I64d???

无语了  换了输入输出

总算ac

时间: 2024-07-29 05:12:15

bestcoder r44 p3 hdu 5270 ZYB loves Xor II的相关文章

hdu 5269 ZYB loves Xor I(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5269 思路分析:当lowbit(AxorB)=2p 时,表示A与B的二进制表示的0-p-1位相等,第p位不同:考虑维护一棵字母树,将所有数字 转换为二进制形式并且从第0位开始插入树中,并在每个节点中记录通过该结点的数字数目:最后统计答案,对于每一个数字, 对于在其路径中的每一个结点X,假设其为第K层,统计通过与该结点不同的值的结点的数目count,则结果增加count*2k; 代码如下: #incl

hdu 5269 ZYB loves Xor I(计数

题意:给出n个数,n个数两两异或后的最后一个bit位k,求所有2^k的和. 比赛的时候递归写挂了....痛心啊...后来看了半天结果把一个数组移到函数体里就1a了(递归的时候覆盖了...)T_T. 思路是这样的:如果最后一位不相同,那么他们异或结果的最后一位与二者最后一位较低的相同,那么把这些数字按最后一位的位置分为若干组,然后不同组可以直接相乘.相同组因为最后一位相同,所以异或的结果与最后一位无关,把最后一位去掉然后递归处理这一组.因为一共有30位,数组长度为n,复杂度大概是O(30*n).

hdu 5269 ZYB loves Xor I

今晚best code第二题 好好学了下字典树,确实自己会的东西实在太少了 #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstdlib> #include<queue> #include<map> #include<stack> #include<list> #include<

HDU 5269 ZYB loves Xor I( 01 Trie 树)

题目链接:传送门 题意: 求 for i: 1~n  forj:1~n lowbit(a[i]^a[j]) lowbit(x)表示取x的最低位1. lowbit(3)=1    lowbit(6)=2; 分析: 因为其中有一个异或运算,我们就先来分析这个异或运算.发现如果lowbit(x^y) = 2^p; 那么把x^y转化成2进制后他们的前p-1位一定是相同的.那么思路就来了把所有的数字转换 成01字符串,注意要使他们的长度都相等,然后建一颗trie树.然后遍历的时候如果同时 有左右孩子的话那

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

Bestcoder round #65 &amp;&amp; hdu 5592 ZYB&#39;s Premutation 线段树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 74 Problem Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutat

hdu5269 ZYB loves Xor I 异或,字典树

hdu5269  ZYB loves Xor I   异或,字典树 ZYB loves Xor I Accepts: 142 Submissions: 696 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 ZYB喜欢研究Xor,现在他得到了一个长度为nn的数组A.于是他想知道:对于所有数对(i,j)(i \in [1,n],j \in [1,n])(i,j)(i∈[1,n

HDU 5268 ZYB loves Score (BestCoder Round#44)

题目链接:ZYB loves Score 题面: ZYB loves Score Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 395    Accepted Submission(s): 232 Problem Description One day,ZYB participated in the BestCoder Contest

ACM学习历程—HDU5269 ZYB loves Xor I(位运算 &amp;&amp; dfs &amp;&amp; 排序)(BestCoder Round #44 1002题)

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,