[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\)的数对的个数。

源代码:

  1. #include<cstdio>
  2. #include<cctype>
  3. typedef long long int64;
  4. inline int64 getint() {
  5. register char ch;
  6. while(!isdigit(ch=getchar()));
  7. register int64 x=ch^'0';
  8. while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
  9. return x;
  10. }
  11. const int N=1e5+1,B=59;
  12. const int64 A=1e18;
  13. int64 ans=0;
  14. class Trie {
  15. private:
  16. int val[N*B],ch[N*B][2],sz;
  17. public:
  18. void insert(const int64 &x) {
  19. for(register int i=B,p=0;i>=0;i--) {
  20. const bool b=(x>>i)&1;
  21. if(!ch[p][b]) ch[p][b]=++sz;
  22. p=ch[p][b];
  23. val[p]++;
  24. }
  25. }
  26. void query(const int &p,const int &q,const int64 &x,const int &d) {
  27. if(d==-1||(1llu<<(d+1))<=(unsigned long long)x) return;
  28. if((1ll<<d)>=x) {
  29. ans+=1ll*val[ch[p][0]]*val[ch[q][1]];
  30. ans+=1ll*val[ch[p][1]]*val[ch[q][0]];
  31. if(ch[p][0]&&ch[q][0]) query(ch[p][0],ch[q][0],x,d-1);
  32. if(ch[p][1]&&ch[q][1]) query(ch[p][1],ch[q][1],x,d-1);
  33. } else {
  34. if(ch[p][0]&&ch[q][1]) query(ch[p][0],ch[q][1],x-(1ll<<d),d-1);
  35. if(ch[p][1]&&ch[q][0]) query(ch[p][1],ch[q][0],x-(1ll<<d),d-1);
  36. }
  37. }
  38. };
  39. Trie t;
  40. int main() {
  41. const int n=getint();
  42. for(register int i=0;i<n;i++) {
  43. t.insert(getint());
  44. }
  45. for(register int64 i=1;i<=A;i*=10) {
  46. t.query(0,0,i,B);
  47. }
  48. ans/=2;
  49. printf("%lld\n",ans);
  50. return 0;
  51. }

[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. python指定分隔符来分割文件

    1 import re 2 p = re.compile('AAAAAAAA',re.S) 3 f = open(r"D:\test\oldfile.txt","r&qu ...

  2. 字符转ASCII码

    char k = '成'; int str = (int)k; Console.WriteLine(str); 结果25104就是‘成’对应的ASCII值

  3. PHP微信模板消息发送

    <?php class Wxtemplate extends Base { function __construct() { $this->appid = config('WXAPP_AP ...

  4. JS浮点计算精度问题分析与解决

    问题描述 在JS计算四则运算时会遇到精度丢失的问题,会引起诸多问题,看看以下例子: 例如:在chrome控制台输入 0.1 + 0.7 输出结果是 0.7999999999999999 例如:0.1+ ...

  5. Python执行ImportError:No module named MySQLdb异常

  6. python 链接数据库错误

    用python3链接oracle产生错误: DPI-1047: Cannot locate a 64-bit Oracle Client library: "The specified mo ...

  7. LR两种录制模式的区别

    loadrunner的录制模式分为如下两种录制模式: HTML-based script,也称为高层录制: URL-based script,也称为底层录制. 两种录制模式的区别:1)脚本不同:高层录 ...

  8. win 10 slmgr.vbs -xpr 无法运行,被豆麦笔记打开解决方法

    win 10 slmgr.vbs -xpr 无法运行,被豆麦笔记打开解决方法 删除这个豆麦笔记 如果之前已经在 控制面板 程序中卸载过,那么是找不到的,我们先运行 slmgr.vbs -xpr,这个时 ...

  9. os2

    1. os.getcwd() 显示当前路径 2. a = os.name 显示当前操作系统 3. a = listdir(path) 显示该路径的所有内容,类似与ls 4. os.chdir(&quo ...

  10. python 精确计算与向上取整 decimal math.ceil

    1. 精确计算 python的float型不精确,需要导入decimal包,以下是不精确举例: 导入decimal包后: 2. 向上取整 一般的取整数(向下取整): 向上取整的方法: