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 ...
随机推荐
- AngularJS进阶(十二)AngularJS常用知识汇总(不断更新中....)
AngularJS常用知识汇总(不断更新中....) 注:请点击此处进行充电! app.controller('editCtrl',['$http','$location','$rootScope', ...
- C语言之归并排序
即将两个都升序(或降序)排列的数据序列合并成一个仍按原序排列的序列. 上代码: #include <stdio.h> #include <stdlib.h> #define m ...
- Android 4.1.2系统添加重启功能
对于Android的的手机或者平板长期使用,感觉会出现慢的情况,所以偶尔还是需要重启一下,而长按电源键弹出的菜单又没有重启选项,所以特在此记录自己添加这个功能的过程. 首先关机的那个弹出菜单是在fra ...
- OpenCV meanshift 图像分割代码
参考:这个帖子的主要代码有错误,根据回帖改了一些 http://www.cnblogs.com/tornadomeet/archive/2012/06/06/2538695.html // means ...
- Oracle rownum 分页, 排序
Oracle rownum 分页, 排序 什么是rownum, rownum的生成, rownum相关的符号操作 Rownum是oracle生成结果集时得到的一个伪列, 按照读出行的顺序, 第一条ro ...
- 史上最全webview详解
本文来自:http://www.jianshu.com/users/320f9e8f7fc9/latest_articles WebView在现在的项目中使用的频率应该还是非常高的. 我个人总觉得HT ...
- The 15th tip of DB Query Analyzer
The 15th tip of DB Query Analyzer ---- SQL Execute Schedule function is realized in 6.01 Ma Gen ...
- objective-c中所谓的僵尸对象
正常情况下向已回收的对象发送消息时灵时不灵,具体要看该对象所占内存有没有被覆写.cocoa提供了僵尸对象(Zombie Object)这个功能,简单的说:启用该调试功能后,运行时会将所有已回收的实例转 ...
- 数据准备<2>:数据质量检查-实战篇
上一篇文章:<数据质量检查-理论篇>主要介绍了数据质量检查的基本思路与方法,本文作为补充,从Python实战角度,提供具体的实现方法. 承接上文,仍然从重复值检查.缺失值检查.数据倾斜问题 ...
- 添加极光推送以及在ios中的问题
项目为 ionic1 + angular1 1.添加极光推送插件 用cordova进行添加 cordova plugin add jpush-phonegap-plugin --variable AP ...