Codeforces Round #482 (Div. 2)
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与字典树比较选相同的位走,并且同时判断不等式条件就可以啦。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
struct node
{
int next[];
int v;
};
node tree[maxn * ];
int root[maxn];
int sz = ;
void build(int p, int x)
{
if(!root[p]) root[p] = sz++;
int tmp = root[p];
for(int i = ; i >= ; i--)
{
int id = (x >> i) & ;
if(tree[tmp].next[id] == )
{
memset(tree[sz].next, , sizeof(tree[sz].next));
tree[sz].v = 1e6;
tree[tmp].next[id] = sz++;
}
tmp = tree[tmp].next[id];
tree[tmp].v = min(tree[tmp].v, x);
}
}
void match(int k, int s, int x)
{
int par = root[k];
if(par == )
{
printf("-1\n");
return;
}
int fx = ~x;
// printf("%d\n", ((fx >> 1) & 1));
int ans = ;
int flag = ;
for(int i = ; i >= ; i--)
{
int id = (fx >> i) & ;
if(tree[par].next[id] && (tree[tree[par].next[id]].v + x) <= s) ///维护最小值,表示最少存在这样的解
{
ans = tree[tree[par].next[id]].v;
par = tree[par].next[id];
}
else if(tree[par].next[ - id] && (tree[tree[par].next[ - id]].v + x) <= s)
{
ans = tree[tree[par].next[ - id]].v;
par = tree[par].next[ - id];
}
else
{
flag = ;
break;
}
}
if(flag || (!ans))
{
printf("-1\n");
}
else
{
printf("%d\n", ans);
}
}
int gcd(int a, int b)
{
return b == ? a : gcd(b, a % b);
}
int main()
{
memset(root, , sizeof(root));
int q; scanf("%d", &q);
while(q--)
{
int t;
scanf("%d", &t);
if(t == )
{
int u; scanf("%d", &u);
for(int i = ; i * i <= u; i++)
{
if(u % i == ) ///i是u的因子
{
build(i, u);
build(u / i, u);
}
}
}
else
{
int x, k, s;
scanf("%d %d %d", &x, &k, &s);
int g = gcd(k, x);
if(g != k)
{
printf("-1\n");
}
else
{
match(k, s, x);
}
}
}
}
Code
Codeforces Round #482 (Div. 2)的更多相关文章
- 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 ...
- Codeforces Round #482 (Div. 2) :B - Treasure Hunt
题目链接:http://codeforces.com/contest/979/problem/B 解题心得: 这个题题意就是三个人玩游戏,每个人都有一个相同长度的字符串,一共有n轮游戏,每一轮三个人必 ...
- Codeforces Round #482 (Div. 2) B题
题目链接:http://codeforces.com/contest/979/problem/B B. Treasure Hunt time limit per test1 second memory ...
- 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时,此路不通.路 ...
- Codeforces Round #482 (Div. 2) :C - Kuro and Walking Route
题目连接:http://codeforces.com/contest/979/problem/C 解题心得: 题意就是给你n个点,在点集中间有n-1条边(无重边),在行走的时候不能从x点走到y点,问你 ...
- 【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,并 ...
- 【枚举】【贪心】Codeforces Round #482 (Div. 2) B. Treasure Hunt
题意:给你3个字符串,3个人各对自己的字符串执行n轮操作,每一次选择一个字符变为任意一个和原来不同的字符.最后问你谁能使自己的串中的任意重复子串出现的次数最大化. 显然只需关注字符而非子串. 枚举每个 ...
- Codeforces Round #482 (Div. 2) B、Treasure Hunt(模拟+贪心)979B
题目 大致题意 n表示要进行n次操作,接着给出三个字符串,表示三个人初始拥有的串.每次操作要替换字符串中的字母,询问最后在游戏中曾出现过的相同的子串谁最多. 思路 (1) 讨论最多的子串,肯定是全部 ...
- 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 ...
随机推荐
- Docker 守护进程的配置和操作 & 远程访问
守护进程的配置和操作 1.查看守护进程 linux命令: ps -ef | gerp docker sudo status docker 2.开启关闭重启守护进程 sudo service docke ...
- svn提交报错,提示:locked,需要cleanup
版权声明:本文为博主原创文章,未经博主允许不得转载. 原文地址: https://www.cnblogs.com/poterliu/p/9285137.html 在使用SVN提交代码或更新代码时经常会 ...
- thinkcmf5更新模板代码分析,解决模板配置json出错导致数据库保存的配置项内容丢失问题
private function updateThemeFiles($theme, $suffix = 'html') { $dir = 'themes/' . $theme; $themeDir = ...
- DeepFaceLab小白入门(3):软件使用!
换脸程序执行步骤,大部分程序都是类似.DeepFaceLab 虽然没有可视化界面,但是将整个过程分成了8个步骤,每个步骤只需点击BAT文件即可执行.只要看着序号,一个个点过去就可以了,这样的操作应该不 ...
- 百度地图和高德地图的API视频教程
学习地址: http://www.houdunren.com/houdunren18_lesson_152?vid=10228 素材地址: https://gitee.com/houdunwang/v ...
- Linux系统入门-Bash初识
目录 Linux系统入门-Bash初识 Bash Shell介绍 Bash Shell的作用 Bash的两种使用方式 命令提示符 shell的基础语法 shell的基本特性 命令补全 linux快捷键 ...
- LeetCode(201) Bitwise AND of Numbers Range
题目 Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numb ...
- JDK1.8 HashMap$TreeNode.balanceInsertion 红黑树平衡插入
红黑树介绍 1.节点是红色或黑色. 2.根节点是黑色. 3.每个叶子节点都是黑色的空节点(NIL节点). 4 每个红色节点的两个子节点都是黑色.(从每个叶子到根的所有路径上不能有两个连续的红色节点) ...
- hdu 5984
PockyTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submissio ...
- kafka消息的可靠性
本文来自网易云社区 作者:田宏增 Kafka的高可靠性的保障来源于其健壮的副本(replication)策略.通过调节其副本相关参数,可以使得Kafka在性能和可靠性之间运转的游刃有余.Kafka从0 ...