[LOJ6469]Magic

题目大意:

有\(n(n\le10^5)\)个物品,每个物品有一个权值\(w_i(w_i\le10^{18})\)。求所有\(n\choose 2\)对物品\((i,j)\)对应\(\lfloor\log_{10}(w_i\oplus w_j)\rfloor+1\)之和。

思路:

相当于枚举\(10\)的若干次方\(m\),然后在字典树上查找\(\ge m\)的数对的个数。

源代码:

#include<cstdio>
#include<cctype>
typedef long long int64;
inline int64 getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int64 x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=1e5+1,B=59;
const int64 A=1e18;
int64 ans=0;
class Trie {
private:
int val[N*B],ch[N*B][2],sz;
public:
void insert(const int64 &x) {
for(register int i=B,p=0;i>=0;i--) {
const bool b=(x>>i)&1;
if(!ch[p][b]) ch[p][b]=++sz;
p=ch[p][b];
val[p]++;
}
}
void query(const int &p,const int &q,const int64 &x,const int &d) {
if(d==-1||(1llu<<(d+1))<=(unsigned long long)x) return;
if((1ll<<d)>=x) {
ans+=1ll*val[ch[p][0]]*val[ch[q][1]];
ans+=1ll*val[ch[p][1]]*val[ch[q][0]];
if(ch[p][0]&&ch[q][0]) query(ch[p][0],ch[q][0],x,d-1);
if(ch[p][1]&&ch[q][1]) query(ch[p][1],ch[q][1],x,d-1);
} else {
if(ch[p][0]&&ch[q][1]) query(ch[p][0],ch[q][1],x-(1ll<<d),d-1);
if(ch[p][1]&&ch[q][0]) query(ch[p][1],ch[q][0],x-(1ll<<d),d-1);
}
}
};
Trie t;
int main() {
const int n=getint();
for(register int i=0;i<n;i++) {
t.insert(getint());
}
for(register int64 i=1;i<=A;i*=10) {
t.query(0,0,i,B);
}
ans/=2;
printf("%lld\n",ans);
return 0;
}

[LOJ6469]Magic的更多相关文章

  1. LOJ6469 Magic(trie)

    纪念我菜的真实的一场模拟赛 首先看到这个题目,一开始就很毒瘤.一定是没有办法直接做的. 我们考虑转化问题 假设,我们选择枚举\(x\),其中\(x\)是\(10\)的若干次方,那么我们只需要求有多少对 ...

  2. Codeforces CF#628 Education 8 D. Magic Numbers

    D. Magic Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. [8.3] Magic Index

    A magic index in an array A[0...n-1] is defined to be an index such that A[i] = i. Given a sorted ar ...

  4. Python魔术方法-Magic Method

    介绍 在Python中,所有以"__"双下划线包起来的方法,都统称为"Magic Method",例如类的初始化方法 __init__ ,Python中所有的魔 ...

  5. 【Codeforces717F】Heroes of Making Magic III 线段树 + 找规律

    F. Heroes of Making Magic III time limit per test:3 seconds memory limit per test:256 megabytes inpu ...

  6. 2016中国大学生程序设计竞赛 - 网络选拔赛 C. Magic boy Bi Luo with his excited tree

    Magic boy Bi Luo with his excited tree Problem Description Bi Luo is a magic boy, he also has a migi ...

  7. 一个快速double转int的方法(利用magic number)

    代码: int i = *reinterpret_cast<int*>(&(d += 6755399441055744.0)); 知识点: 1.reinterpret_cast&l ...

  8. MAGIC XPA最新版本Magic xpa 2.4c Release Notes

    New Features, Feature Enhancements and Behavior ChangesSubforms – Behavior Change for Unsupported Ta ...

  9. Magic xpa 2.5发布 Magic xpa 2.5 Release Notes

    Magic xpa 2.5發佈 Magic xpa 2.5 Release Notes Magic xpa 2.5 Release NotesNew Features, Feature Enhance ...

随机推荐

  1. shell脚本批量创建用户

    #!/bin/bash DATE=$(date +%F_%T) USER_FILE=user.txt echo_color() { == "green" ];then echo - ...

  2. 如何录制Chrome或者Linux下的应用

    说明: PortMapping的这种用法其实早就有了,开始我一直没注意到这点,后面才发现了这个功能,特别在<性能测试进阶指南Loadrunner11实战>第二版中更新. 不是所有的对象都能 ...

  3. Python数据分析几个比较常用的方法

    1,表头或是excel的索引如果是中文的话,输出会出错 ​​解决方法:python的版本问题!换成python3就自动解决了!当然也有其他的方法,这里就不再深究 2,如果有很多列,如何输出指定的列? ...

  4. .Net分布式锁

    项目中一般使用lock作为锁,以便于多线程操作确保库内数据统一.但是如果分布式部署项目,则这种锁就是去了意义,这时可以使用redis或memcache的add方法作为分布式锁. 栗子

  5. 【BZOJ2212】[POI2011]Tree Rotations (线段树合并)

    题解: 傻逼题 启发式合并线段树里面查$nlog^2$ 线段树合并顺便维护一下$nlogn$ 注意是叶子为n 总结点2n 代码: #include <bits/stdc++.h> usin ...

  6. Ubuntu14.04创建无线WiFi,android可以连接上网

    前提条件: ubuntu14.04 unity,已经通过有线连接到internet 一般环境下创建的wifi热点android设备是无法识别的,网上说通过ap-hotspot方式创建出来的热点手机可以 ...

  7. 【转】android:paddingLeft与android:layout_marginLeft的区别

    http://www.blogjava.net/anchor110/articles/342206.html 当按钮分别设置以上两个属性时,得到的效果是不一样的. android:paddingLef ...

  8. 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。

    该题还是考杨辉三角计算,只不过最后每一行都放入List集合中,然后返回,直接看代码: public static List<List<Integer>> generate(in ...

  9. GIT结合android studio使用总结

    使用GIT前请阅读(有git基础可略过) git指引 :http://www.bootcss.com/p/git-guide/ 一. 下载git  http://git-scm.com/downloa ...

  10. HDU2255 奔小康赚大钱 (最大权完美匹配) 模板题【KM算法】

    <题目链接> 奔小康赚大钱 Problem Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子.这可是一件大事,关系到人民的住房问题啊 ...