D. Kuro and GCD and XOR and SUM
Kuro is currently playing an educational game about numbers. The game focuses on the greatest common divisor (GCD), the XOR value, and the sum of two numbers. Kuro loves the game so much that he solves levels by levels day by day.
Sadly, he's going on a vacation for a day, and he isn't able to continue his solving streak on his own. As Katie is a reliable person, Kuro kindly asked her to come to his house on this day to play the game for him.
Initally, there is an empty array aa. The game consists of qq tasks of two types. The first type asks Katie to add a number uiui to aa. The second type asks Katie to find a number vv existing in aa such that ki∣GCD(xi,v)ki∣GCD(xi,v), xi+v≤sixi+v≤si, and xi⊕vxi⊕v is maximized, where ⊕⊕denotes the bitwise XOR operation, GCD(c,d)GCD(c,d) denotes the greatest common divisor of integers cc and dd, and y∣xy∣x means xx is divisible by yy, or report -1 if no such numbers are found.
Since you are a programmer, Katie needs you to automatically and accurately perform the tasks in the game to satisfy her dear friend Kuro. Let's help her!
The first line contains one integer qq (2≤q≤1052≤q≤105) — the number of tasks the game wants you to perform.
qq lines follow, each line begins with an integer titi — the type of the task:
- If ti=1ti=1, an integer uiui follow (1≤ui≤1051≤ui≤105) — you have to add uiui to the array aa.
- If ti=2ti=2, three integers xixi, kiki, and sisi follow (1≤xi,ki,si≤1051≤xi,ki,si≤105) — you must find a number vv existing in the array aa such that ki∣GCD(xi,v)ki∣GCD(xi,v), xi+v≤sixi+v≤si, and xi⊕vxi⊕v is maximized, where ⊕⊕ denotes the XOR operation, or report -1 if no such numbers are found.
It is guaranteed that the type of the first task is type 11, and there exists at least one task of type 22.
For each task of type 22, output on one line the desired number vv, or -1 if no such numbers are found.
5
1 1
1 2
2 1 1 3
2 1 1 2
2 1 1 1
2
1
-1
10
1 9
2 9 9 22
2 3 3 18
1 25
2 9 9 20
2 25 25 14
1 20
2 26 26 3
1 14
2 20 20 9
9
9
9
-1
-1
-1
In the first example, there are 5 tasks:
- The first task requires you to add 11 into aa. aa is now {1}{1}.
- The second task requires you to add 22 into aa. aa is now {1,2}{1,2}.
- The third task asks you a question with x=1x=1, k=1k=1 and s=3s=3. Taking both 11 and 22 as vv satisfies 1∣GCD(1,v)1∣GCD(1,v) and 1+v≤31+v≤3. Because 2⊕1=3>1⊕1=02⊕1=3>1⊕1=0, 22 is the answer to this task.
- The fourth task asks you a question with x=1x=1, k=1k=1 and s=2s=2. Only v=1v=1 satisfies 1∣GCD(1,v)1∣GCD(1,v) and 1+v≤21+v≤2, so 11 is the answer to this task.
- The fifth task asks you a question with x=1x=1, k=1k=1 and s=1s=1. There are no elements in aa that satisfy the conditions, so we report-1 as the answer to this task.
这题其实可以说是01字典树的裸题, 看了别人的题解 然后就去学了一下01字典树
现在我也只是刚刚学有点懵逼

我看这篇博客学的 转载
源地址https://blog.csdn.net/riba2534/article/details/80344026
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define inf 0x3fffffff using namespace std;
typedef long long LL;
const int maxn = 2e5 + ;
int a[maxn], minn[maxn], tree[ * maxn][];
int sum, root;
int newnode() {
tree[sum][] = tree[sum][] = -;
sum++;
return sum - ;
} void init() {
sum = ;
memset(minn, 0x3f, sizeof(minn));
root = newnode();
}
void add(int x) {
int p = x;
int now = root ;
minn[now] = min(minn[now], p);
for (int i = ; i >= ; i--) {
int to = (x >> i) & ;
if (tree[now][to] == -) tree[now][to] = newnode();
now = tree[now][to];
minn[now] = min(minn[now], p);
}
}
/*
int getans(int x, int p) {
int now = root;
if (minn[now] > p) return -1;
for (int i = 31 ; i >= 0 ; i-- ) {
int to = (x >> i) & 1 ;
if (tree[now][to ^ 1] != -1 && minn[tree[now][to ^ 1]] <= p ) to ^= 1;
now = tree[now][to];
}
return minn[now];
}*/
int getans(int x, int p) {
int now = root ;
if (minn[now] > p ) return -;
for (int i = ; i >= ; i-- ) {
int to = (x >> i) & ;
if (tree[now][to ^ ] != - && minn[tree[now][to ^ ]] <= p ) to ^= ;
now = tree[now][to];
}
return minn[now];
}
int main() {
int t, op, x, k, s;
init();
scanf("%d", &t);
while(t--) {
scanf("%d", &op);
if (op == ) {
scanf("%d", &x);
a[x] = ;
add(x);
} else {
scanf("%d%d%d", &x, &k, &s);
if (x % k != ) {
printf("-1\n");
continue;
}
if (k == ) {
printf("%d\n", getans(x, s - x));
continue;
}
int maxn = -, ans = -;
for (int i = k ; i <= s - x ; i += k) {
if (a[i] && (i ^ x) > maxn) {
maxn = i ^ x;
ans = i;
}
}
printf("%d\n", ans);
}
}
return ;
}
D. Kuro and GCD and XOR and SUM的更多相关文章
- 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, ...
- CodeForces 979 D Kuro and GCD and XOR and SUM
Kuro and GCD and XOR and SUM 题意:给你一个空数组. 然后有2个操作, 1是往这个数组里面插入某个值, 2.给你一个x, k, s.要求在数组中找到一个v,使得k|gcd( ...
- 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 ...
- 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 979D Kuro and GCD and XOR and SUM
题意: 给出两种操作: 1.添加一个数字x到数组. 2.给出s,x,k,从数组中找出一个数v满足gcd(x,k) % v == 0 && x + v <= s && ...
- 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 ...
- cf979d Kuro and GCD and XOR and SUM
set做法 正解是trie-- 主要是要学会 \(a\ \mathrm{xor}\ b \leq a+b\) 这种操作 #include <iostream> #include <c ...
- 【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,并 ...
- 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 ...
随机推荐
- TCP中的MSS解读(转)
本文摘录自TCP中的MSS解读. MSS 是TCP选项中最经常出现,也是最早出现的选项.MSS选项占4byte.MSS是每一个TCP报文段中数据字段的最大长度,注意:只是数据部分的字段,不包括TCP的 ...
- Mahout Bayes分类
Mahout Bayes分类器是按照<Tackling the Poor Assumptions of Naive Bayes Text Classiers>论文写出来了,具体查看论文 实 ...
- iOS在GitHub Top 前100 简介
主要对当前 GitHub 排名前 100 的项目做一个简单的简介, 方便初学者快速了解到当前 Objective-C 在 GitHub 的情况. 项目名称 项目信息 1. AFNetworking 作 ...
- objective-c 2.0的字面量Literals
obj-c 2.0增加了许多核心对象字面量的简单语法,向ruby学习吗? 直接上代码: #import <Foundation/Foundation.h> int main(void){ ...
- Markdown语法及编辑器
宗旨 Markdown 的目标是实现「易读易写」. 可读性,无论如何,都是最重要的.一份使用 Markdown 格式撰写的文件应该可以直接以纯文本发布,并且看起来不会像是由许多标签或是格式指令所构成. ...
- Core Animation简介
一.Core Animation简介 * Core Animation,中文翻译为核心动画,它是一组非常强大的动画处理API,使用它能做出非常炫丽的动画效果,而且往往是事半功倍.也就是说,使用少量的代 ...
- 关闭ipv6的方法
公司研发反应,几台机器开了一些端口,但是访问一直不通. 检查后发现,发现服务开启的是ipv6的端口,所有首先想到的办法就是关闭ipv6. 关闭ipv6的方法有两种: 第一个是在内核配置文件修改配置(p ...
- Ubuntu系统下配置IP地址方法介绍
配置IP方式有两种: 1.通过命令直接配置 sudo ifconfig eth0 IP地址 netmask 子网掩码------配置IP地 sudo route add default gw 网关-- ...
- Js 浅克隆详解
浅克隆:不仅赋值,而且赋予了内存地址深度克隆:赋值,内存地址不同var a = [1,2,3]; var b = a; a = [4,5,6]; alert(b); //[1,2,3] 面试时被问到这 ...
- 菜鸟级Git GitHub创建仓库
菜鸟标准:知道pwd ,rm 命令是什么. 一.Git 是什么. git 是目前世界上最先进的分布式版本控制系统 二.SVN与Git 1.版本控制系统 SVN 是集中式版本控制系统,版本库是集中放在中 ...