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)的更多相关文章

  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. Docker系列一:Docker的介绍和安装

    Docker介绍 Docker是指容器化技术,用于支持创建和实验Linux Container.借助Docker,你可以将容器当做重量轻.模块化的虚拟机来使用,同时,你还将获得高度的灵活性,从而实现对 ...

  2. angular4使用代理

    1. 在angular-cli项目根目录下创建proxy.config.json { "/api/v1": { "target": "http://1 ...

  3. apply(), applymap(), map()

    Pandas 中map, applymap and apply的区别  https://blog.csdn.net/u010814042/article/details/76401133/ Panda ...

  4. 通过session模拟登陆

    import requests # 这个练习对比的是上一个登陆练习,这个是不用自己传入cookie参数,而是利用session方法登陆 # 实例化一个session session = request ...

  5. Applied Nonparametric Statistics-lec1

    参考网址: https://onlinecourses.science.psu.edu/stat464/node/2 Binomial Distribution Normal Distribution ...

  6. k短路模板

    https://acm.taifua.com/archives/jsk31445.html 链接: https://nanti.jisuanke.com/t/31445 #include <io ...

  7. virtual function c++

    之前一直不明白为什么要用虚函数,我只知道这样的规则, Base b = new derived(); b->do(); 调用的是子类的do(): virtue class只是一个虚拟的,调用的是 ...

  8. 大数据学习——scala类相关操作

    1 类的定义 package com /** * Created by Administrator on 2019/6/3. */ //类并不用声明为public. class Person { // ...

  9. Python_Virtualenv及Pycharm配置

    Virtualenv存在的意义 在Python使用过程中,你是否有遇到过同时需要开发多个应用的情况? 假设A应用需要使用DJango1.X版本,而B应用需要使用DJango2.X的版本,而你全局开发环 ...

  10. 聊聊、Nginx GDB与MAIN参数

    接着上一篇,我们学习 Nginx 的 main 方法.用 gdb 工具调试 Nginx,首先 gdb nginx.如下: gdb 调试工具有很多的命令,上一篇为了找 main 方法用了 b 命令,也就 ...