D. Kuro and GCD and XOR and SUM

字典树真好玩。。。

牛老板提供的思路:建1e5个 字典树,每个数插入到以它的因子为根所在的字典树中,这样就实现了整除,当然gcd(k, x) = k是必须的

然后如何保证v + x <= s 和 v ^ x 最大呢?

对于v + x <= s,我们可以维护01字典树中,经过每个节点的最小值,这样我们在访问每个节点时,直接min + x <= s 判断是否成立即可, 不成立就不用往下走了,因为最小值都不成立。

对于v ^ x最大,就是一般字典树思路了,~x与字典树比较选相同的位走,并且同时判断不等式条件就可以啦。

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int maxn = 1e5 + ;
  5. struct node
  6. {
  7. int next[];
  8. int v;
  9. };
  10. node tree[maxn * ];
  11. int root[maxn];
  12. int sz = ;
  13. void build(int p, int x)
  14. {
  15. if(!root[p]) root[p] = sz++;
  16. int tmp = root[p];
  17. for(int i = ; i >= ; i--)
  18. {
  19. int id = (x >> i) & ;
  20. if(tree[tmp].next[id] == )
  21. {
  22. memset(tree[sz].next, , sizeof(tree[sz].next));
  23. tree[sz].v = 1e6;
  24. tree[tmp].next[id] = sz++;
  25. }
  26. tmp = tree[tmp].next[id];
  27. tree[tmp].v = min(tree[tmp].v, x);
  28. }
  29. }
  30. void match(int k, int s, int x)
  31. {
  32. int par = root[k];
  33. if(par == )
  34. {
  35. printf("-1\n");
  36. return;
  37. }
  38. int fx = ~x;
  39. // printf("%d\n", ((fx >> 1) & 1));
  40. int ans = ;
  41. int flag = ;
  42. for(int i = ; i >= ; i--)
  43. {
  44. int id = (fx >> i) & ;
  45. if(tree[par].next[id] && (tree[tree[par].next[id]].v + x) <= s) ///维护最小值,表示最少存在这样的解
  46. {
  47. ans = tree[tree[par].next[id]].v;
  48. par = tree[par].next[id];
  49. }
  50. else if(tree[par].next[ - id] && (tree[tree[par].next[ - id]].v + x) <= s)
  51. {
  52. ans = tree[tree[par].next[ - id]].v;
  53. par = tree[par].next[ - id];
  54. }
  55. else
  56. {
  57. flag = ;
  58. break;
  59. }
  60. }
  61. if(flag || (!ans))
  62. {
  63. printf("-1\n");
  64. }
  65. else
  66. {
  67. printf("%d\n", ans);
  68. }
  69. }
  70. int gcd(int a, int b)
  71. {
  72. return b == ? a : gcd(b, a % b);
  73. }
  74. int main()
  75. {
  76. memset(root, , sizeof(root));
  77. int q; scanf("%d", &q);
  78. while(q--)
  79. {
  80. int t;
  81. scanf("%d", &t);
  82. if(t == )
  83. {
  84. int u; scanf("%d", &u);
  85. for(int i = ; i * i <= u; i++)
  86. {
  87. if(u % i == ) ///i是u的因子
  88. {
  89. build(i, u);
  90. build(u / i, u);
  91. }
  92. }
  93. }
  94. else
  95. {
  96. int x, k, s;
  97. scanf("%d %d %d", &x, &k, &s);
  98. int g = gcd(k, x);
  99. if(g != k)
  100. {
  101. printf("-1\n");
  102. }
  103. else
  104. {
  105. match(k, s, x);
  106. }
  107. }
  108. }
  109. }

Code

Codeforces Round #482 (Div. 2)的更多相关文章

  1. Codeforces Round #482 (Div. 2) : Kuro and GCD and XOR and SUM (寻找最大异或值)

    题目链接:http://codeforces.com/contest/979/problem/D 参考大神博客:https://www.cnblogs.com/kickit/p/9046953.htm ...

  2. Codeforces Round #482 (Div. 2) :B - Treasure Hunt

    题目链接:http://codeforces.com/contest/979/problem/B 解题心得: 这个题题意就是三个人玩游戏,每个人都有一个相同长度的字符串,一共有n轮游戏,每一轮三个人必 ...

  3. Codeforces Round #482 (Div. 2) B题

    题目链接:http://codeforces.com/contest/979/problem/B B. Treasure Hunt time limit per test1 second memory ...

  4. Codeforces Round #482 (Div. 2) C 、 Kuro and Walking Route(dfs)979C

    题目链接:http://codeforces.com/contest/979/problem/C 大致题意 给出n个点,有n-1个边将他们链接.给出x,y,当某一路径中出现x....y时,此路不通.路 ...

  5. Codeforces Round #482 (Div. 2) :C - Kuro and Walking Route

    题目连接:http://codeforces.com/contest/979/problem/C 解题心得: 题意就是给你n个点,在点集中间有n-1条边(无重边),在行走的时候不能从x点走到y点,问你 ...

  6. 【Trie】【枚举约数】Codeforces Round #482 (Div. 2) D. Kuro and GCD and XOR and SUM

    题意: 给你一个空的可重集,支持以下操作: 向其中塞进一个数x(不超过100000), 询问(x,K,s):如果K不能整除x,直接输出-1.否则,问你可重集中所有是K的倍数的数之中,小于等于s-x,并 ...

  7. 【枚举】【贪心】Codeforces Round #482 (Div. 2) B. Treasure Hunt

    题意:给你3个字符串,3个人各对自己的字符串执行n轮操作,每一次选择一个字符变为任意一个和原来不同的字符.最后问你谁能使自己的串中的任意重复子串出现的次数最大化. 显然只需关注字符而非子串. 枚举每个 ...

  8. Codeforces Round #482 (Div. 2) B、Treasure Hunt(模拟+贪心)979B

    题目 大致题意 n表示要进行n次操作,接着给出三个字符串,表示三个人初始拥有的串.每次操作要替换字符串中的字母,询问最后在游戏中曾出现过的相同的子串谁最多. 思路 (1)  讨论最多的子串,肯定是全部 ...

  9. Codeforces Round #482 (Div. 2) C Kuro and Walking Route

    C. Kuro and Walking Route time limit per test 2 seconds memory limit per test 256 megabytes input st ...

随机推荐

  1. nginx站点目录及文件URL访问控制

    一.根据扩展名限制程序和文件访问 利用nginx配置禁止访问上传资源目录下的PHP.Shell.Perl.Python程序文件. 配置nginx,禁止解析指定目录下的指定程序. location ~ ...

  2. Ubuntu 16.04系统安装步骤

    1.安装系统 2.设置更新源,自动检测最优更新源 3.关闭自动更新 4.设置终端样式 5.设置终端快捷键 6.安装vim,配置.vimrc 7.修改.bashrc第62行,小写w为大写W,设置终端不显 ...

  3. 1.python中的变量

    什么是变量 1.在任何语言中都有变量的概念,在python中变量是用一个变量名表示,变量名必须是用大小写英文字母,数字,下滑写(_)组成.不能用数字开头.(但用中文做变量名也可以,不要这样做) 例: ...

  4. SQL登录注册练习

    /class User package com.neusoft.bean; public class User { private int password; private String name; ...

  5. Aizu - 1378 Secret of Chocolate Poles (DP)

    你有三种盘子,黑薄,白薄,黑厚. 薄的盘子占1,厚的盘子占k. 有一个高度为L的桶,盘子总高度不能超出桶的总高度(可以小于等于).相同颜色的盘子不能挨着放. 问桶内装盘子的方案数. 如 L = 5,k ...

  6. 官网下载MySQL

    1)首先我们访问MySQL官网https://dev.mysql.com/,然后如下 2)我们向下拉取滚动条,来到如下界面,选择Source Code 3)向下拉取滚动条,来到如下界面,操作如下: 4 ...

  7. UVa 10655 Contemplation! Algebra 矩阵快速幂

    题意: 给出\(p=a+b\)和\(q=ab\),求\(a^n+b^n\). 分析: 这种题目关键还是在于构造矩阵: \(\begin{bmatrix} 0 & 1 \\ -(a+b) &am ...

  8. Chromium Embedded Framework

    关于CEF 近期由于工作需要开始研究了Google的Chromium Embedded Framework(CEF),这是一个基于Google Chromium开源代码的项目,使用CEF可以很方便的在 ...

  9. VS重置开发环境的方法

    经常由于各种插件的原因,导致VS有使用过程中出现断点进不去等各种情况的时候,这个方法可以让你的VS回到最初安装的状态,当然,这时候,各种配置也没有了,不到万不得已就勿使用. 下面以Vs2015来说明: ...

  10. Hive中集合类型

    Hive中集合类型 创建表,集合是以 - 分割的 数据文件 加载数据 查询数据 查询数组中第一个字段 再建一个表,使用map 查看数据文件 加载数据 查询数据 查询键值 创建表,struct类型 查看 ...