[LOJ6469]Magic
[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的更多相关文章
- LOJ6469 Magic(trie)
纪念我菜的真实的一场模拟赛 首先看到这个题目,一开始就很毒瘤.一定是没有办法直接做的. 我们考虑转化问题 假设,我们选择枚举\(x\),其中\(x\)是\(10\)的若干次方,那么我们只需要求有多少对 ...
- 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 ...
- [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 ...
- Python魔术方法-Magic Method
介绍 在Python中,所有以"__"双下划线包起来的方法,都统称为"Magic Method",例如类的初始化方法 __init__ ,Python中所有的魔 ...
- 【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 ...
- 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 ...
- 一个快速double转int的方法(利用magic number)
代码: int i = *reinterpret_cast<int*>(&(d += 6755399441055744.0)); 知识点: 1.reinterpret_cast&l ...
- MAGIC XPA最新版本Magic xpa 2.4c Release Notes
New Features, Feature Enhancements and Behavior ChangesSubforms – Behavior Change for Unsupported Ta ...
- 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 ...
随机推荐
- Centos7上vsftp脚本--> sh vsftp.sh 用户名 密码 --> sh vsftp.sh install
#!/bin/bash #vsftp install . /etc/rc.d/init.d/functions users=/etc/vsftpd/vftpuser.txt login=/etc/vs ...
- Jmeter测试demo
复制代码,保存为.jmx文件 需要安装插件: JMeterPlugins-ExtrasLibs E:\软件\apache-jmeter-3.0\lib\ext <?xml version=&qu ...
- python 内置数据类型之数字
目录: 1.2. 数字 1.2.1. 数字类型 1.2.2. 浮点数 1.2.3. 进制记数 1.2.4. 设置小数精度 1.2.5. 分数 1.2.6. 除法 1.2 数字 1.2.1 数字类型 ...
- 【转】asp.net Core 系列【二】—— 使用 ASP.NET Core 和 VS2017 for Windows 创建 Web API
在本教程中,将生成用于管理“待办事项”列表的 Web API. 不会生成 UI. 概述 以下是将创建的 API: API 描述 请求正文 响应正文 GET /api/todo 获取所有待办事项 无 待 ...
- textarea文本域宽度和高度width及height自动适应实现代码
<HTML> <HEAD> <TITLE>textarea宽度.高度自动适应处理方法</TITLE> <!-- 控制宽度的自动适应 --> ...
- Windows无法自动将IP协议堆栈绑定到网络适配器 的解决办法
实验室的台式机在升级驱动后上不了网了,有线网卡驱动卸载后重装了,还是不行,通过Windows诊断发现“Windows 无法自动将 IP 协议堆栈绑定到网络适配器的解决办法”. 解决办法: 打开“控制面 ...
- ubuntud安装Adobe Flash Player / Plugin
1.https://get.adobe.com/flashplayer/ , select tar.gz for other Linux, download 2.Unpack the tar.gz f ...
- NOIP2017提高组Day1T3 逛公园 洛谷P3953 Tarjan 强连通缩点 SPFA 动态规划 最短路 拓扑序
原文链接https://www.cnblogs.com/zhouzhendong/p/9258043.html 题目传送门 - 洛谷P3953 题目传送门 - Vijos P2030 题意 给定一个有 ...
- 普林斯顿微积分读本 大纲与重点 (by zzd)
普林斯顿微积分读本 大纲重点 由于博客园太菜,所以我用图片上传. 当前更新状态:未完待续,挖坑暂时不填了. UPD(2018-07-08): 稍微更一下,加一个本书的某一版本下载链接:https:// ...
- js将时间戳转为时间格式
时间戳转时间格式 //分钟 let timeM= parseInt(msg/1000/60%60); if(timeM<10){ timeM="0"+timeM; } //秒 ...