CodeForces 979 D Kuro and GCD and XOR and SUM
题意:给你一个空数组。 然后有2个操作, 1是往这个数组里面插入某个值, 2.给你一个x, k, s。要求在数组中找到一个v,使得k|gcd(x,v) (即gcd(x,v)是k的倍数,v+x <= k, x ^ v的值最大。
题解:XOR亦或问题是经典的题目,一般都是用01字典树去处理。 然后需要满足条件1, 所以我们可以对于每一个点建立一个字典树,每次添加数的时候都往这个数的因子添加这个值,这样我们直接访问对应的k就可以找到答案了。
代码:
#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+;
const int N = 1e5+;
vector<int> son[N];
struct Node{
int Min;
Node * p[];
Node(){
Min = INF;
p[] = p[] = nullptr;
}
}*rt[N];
bool vis[N];
void init(){
for(int i = ; i < N; i++){
rt[i] = new Node();
for(int j = i; j < N; j+=i){
son[j].pb(i);
}
}
}
void Add(int k, int u){
Node *tmp = rt[k];
tmp -> Min = min(tmp -> Min, u);
for(int i = ; i >= ; i--){
int id = u>>i & ;
if(tmp -> p[id] == nullptr)
tmp -> p[id] = new Node();
tmp = tmp -> p[id];
tmp -> Min = min(tmp -> Min, u);
}
}
int Query(int x, int k, int s){
Node *tmp = rt[k];
if(x%k != || tmp->Min+x > s)
return -;
int ret = ;
for(int i = ; i >= ; i--){
int id = x >> i & ;
if(tmp -> p[id^] != nullptr && tmp -> p[id^] -> Min + x <= s){
tmp = tmp -> p[id^];
ret += (id^) << i;
}
else {
tmp = tmp -> p[id];
ret += id << i;
}
}
return ret;
}
int main(){
///Fopen;
init();
int q, t, u, x, s, k;
scanf("%d", &q);
while(q--){
scanf("%d", &t);
if(t == ){
scanf("%d", &u);
if(!vis[u]){
vis[u] = ;
for(int k : son[u]){
Add(k, u);
}
}
}
else{
scanf("%d%d%d", &x, &k, &s);
printf("%d\n", Query(x,k,s));
}
}
return ;
}
CodeForces 979 D Kuro and GCD and XOR and SUM的更多相关文章
- Codeforces 979 D. Kuro and GCD and XOR and SUM(异或和,01字典树)
Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...
- CF 979D Kuro and GCD and XOR and SUM(异或 Trie)
CF 979D Kuro and GCD and XOR and SUM(异或 Trie) 给出q(<=1e5)个操作.操作分两种,一种是插入一个数u(<=1e5),另一种是给出三个数x, ...
- D. Kuro and GCD and XOR and SUM
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...
- CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&指针&Xor)
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...
- 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 979D Kuro and GCD and XOR and SUM
题意: 给出两种操作: 1.添加一个数字x到数组. 2.给出s,x,k,从数组中找出一个数v满足gcd(x,k) % v == 0 && x + v <= s && ...
- 【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,并 ...
- cf979d Kuro and GCD and XOR and SUM
set做法 正解是trie-- 主要是要学会 \(a\ \mathrm{xor}\ b \leq a+b\) 这种操作 #include <iostream> #include <c ...
- cf round 482D Kuro and GCD and XOR and SUM
题意: 开始有个空集合,现在有两种操作: $(1,x)$:给集合加一个数$x$,$x \leq 10^5$; $(2,x,k,s)$:在集合中找一个$a$,满足$a \leq s-x$,而且$k|gc ...
随机推荐
- python 感悟
* 优美胜于丑陋.* 显式胜于隐式.* 简单胜于复杂.* 复杂胜于难懂.* 扁平胜于嵌套.* 稀疏胜于紧密.* 可读性应当被重视.* 尽管实用性会打败纯粹性,特例也不能凌驾于规则之上.* 不要忽略任何 ...
- 计算机网络中IP地址和MAC地址
计算机 网络中的网络地址有I P 地址和物理地址之分,对 于主机间的通信时,它们的作用也不一样 . l I P 地址 为 了保证 I n t e r n e t 网上主机通信时能够相互识别 ,不引 ...
- Js 基础知识1
JS比较运算符,有两种比较运算符: 第一种是==比较,它会自动转换数据类型再比较 第二种是===比较,它不会自动转换数据类型,如果数据类型不一致,返回false,如果一致,再比较. 不要使用==比较, ...
- 用泛型写Redis缓存与数据库操作工具类
功能描述: 先从缓存获取数据,如果缓存没有,就从数据库获取数据,并设置到缓存中,返回数据. 如果数据库中没有数据,需要设置一个缓存标记flagKey,防止暴击访问数据库,用缓存保护数据库. 当删除缓存 ...
- 分布式ID系列(2)——UUID适合做分布式ID吗
UUID的生成策略: UUID的方式能生成一串唯一随机32位长度数据,它是无序的一串数据,按照开放软件基金会(OSF)制定的标准计算,UUID的生成用到了以太网卡地址.纳秒级时间.芯片ID码和许多可能 ...
- snort规则中byte_test参数详解
例子: byte_test:4,>,1000,20 这里是从本规则内前面匹配的位置结尾开始,向后偏移20个字节,再获取后面的4个字节的数据,与十进制数据1000进行比较,如果大于1000,就命中 ...
- Kubernetes Pod 驱逐详解
原文链接:Kubernetes Pod 驱逐详解 在 Kubernetes 中,Pod 使用的资源最重要的是 CPU.内存和磁盘 IO,这些资源可以被分为可压缩资源(CPU)和不可压缩资源(内存,磁盘 ...
- 调用链系列(1):解读UAVStack中的贪吃蛇
一.背景 对于分布式在线服务,一个请求需要经过多个系统中多个模块,可能多达上百台机器的协作才能完成单次请求.这种场景下单靠人力无法掌握整个请求中各个阶段的性能开销,更无法快速的定位系统中性能瓶颈.当发 ...
- 容易上手搭建vue2.0开发环境
第一步:安装node 前端开发框架和环境都是需要 Node.js ,先安装node.js开发环境,vue的运行是要依赖于node的npm的管理工具来实现,下载https://nodejs.org/en ...
- 论文解读2——Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition
背景 用ConvNet方法解决图像分类.检测问题成为热潮,但这些方法都需要先把图片resize到固定的w*h,再丢进网络里,图片经过resize可能会丢失一些信息.论文作者发明了SPP pooling ...