[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. jquery checkbox勾选/取消勾选只能操作一次的诡异问题

    第一次执行,没问题,但第二次执行就有问题了,选择不了 解决办法:把attr()换成prop() $("#CheckedAll").click(function () { if ($ ...

  2. Appium 常用方法总结 (python 版)

    1.app后台运行 driver.background_app(5) 2.锁屏 driver.lock(5) 3.隐藏键盘 driver.hide_keyboard() 4.启动一个app或者在当前a ...

  3. 封装cuda/cudnn写卷积网络前向计算程序

    目录 基本编译配置 一些常识 BN层的坑 cuda基础 向cuda核函数传入结构体指针? 参考:http://galoisplusplus.coding.me/blog/2018/05/22/cuda ...

  4. 根据现有的XML文件生成其对应的实体类

    方法如下: 1.将完整的Xml文本复制一下, 2.在vs2013(或以上版本) .net4.5项目下建立一个类文件, 3.依次选择菜单:编辑->选择性粘贴->将XML粘贴为类.

  5. cuda by example【读书笔记2】

    常量内存 用常量内存来替换全局内存可以有效的减少内存带宽 __constant__修饰符标识常量内存,从主机内存复制到GPU上的常量内存时,需要特殊版本的cudaMemcpy(): cudaMemcp ...

  6. python全栈开发day66-视图系统、路由系统

    一.昨日内容回顾 1. tags 1. for循环 {% for name in name_list %} {{ name }} {% endfor %} {% for name in name_li ...

  7. Python_copy_深浅拷贝

    对于数字和字符串来说,无论是‘’赋值‘’还是‘’深拷贝‘’还是‘’浅拷贝‘’都是指向的同一个地址 深浅拷贝是copy类下的方法,创建方式为 import copy copy.copy() #浅拷贝 c ...

  8. sparkStreaming运行yarn,提交任务报错

    op.yarn.server.nodemanager.containermanager.launcher.ContainerLaunch.call(ContainerLaunch.java:302) ...

  9. 禁止root直接登陆linux系统

    直接修改文件 # vim /etc/ssh/sshd_config SyslogFacility AUTHPRIV PermitRootLogin no RSAAuthentication yes P ...

  10. net core体系-Xamarin-1概要

    大家在开发领域都知道Java是跨平台的,但是很多人认为.NET是只能在Windows下运行,不具有跨平台的特性,这种说法其实是不妥的. .NET其实在设计之初也是考虑像Java一样跨平台的,我们知道. ...