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 ...
随机推荐
- 如何在VS2013中进行Boost单元测试
对于如何在VS2013中进行Boost单元测试,这方面资料太少.自己也因此走了不少弯路.下文将会阐述一下如何在VS2013中进行Boost单元测试. 在开始Boost单元测试之前,我们需要先安装VS2 ...
- Struts源码之OgnlValueStack
public class OgnlValueStack implements Serializable, ValueStack, ClearableValueStack, MemberAccessVa ...
- 色彩转换——RGB & HSI
RGB to HSI I=(R+G+B)/3; S=1-3*min(R,G,B)/(R+G+B); H = cos^(-1)((0.5*((R-G)+(R-B))) / ((R-G)^2 + (R-B ...
- 数据包接收系列 — IP协议处理流程(二)
本文主要内容:在接收数据包时,IP协议的处理流程. 内核版本:2.6.37 Author:zhangskd @ csdn blog 我们接着来看数据包如何发往本地的四层协议. ip_local_del ...
- shell脚本里面相互调用时路径不要用pwd获取
shellA调用shellB,如果shellB 里面需要使用路径作为变量,去寻找其它文件.那么要注意,不用pwd,其返回的是系统中用户当前所在位置的路径,也就是shellA的路径,这样就错了.应该用d ...
- GEFGWT的HelloWorld
发现一个好玩的东西,gef-gwt,使用它可以轻松的在web上建立gef程序,原文在这里http://gefgwt.org/getting-started/(文章虽然是英文,但是很容易懂,我是按部就班 ...
- Oracle 10g DG 数据文件迁移
背景:某客户Oracle 10g 的DG由于空间不足,之前将部分数据文件迁移到其他目录,如今原目录扩容成功,要将之前迁移的数据文件再次迁移回来. 环境:Oracle 10.2.0.5 DG 单机 首先 ...
- getElementById 用法的一个技巧
假设实现把 TextBox1 的字符实时的拷贝到 TextBox2 中,代码如下: <Script language="Javascript"> fun ...
- 怎么分别javascript写在<head>里还是<body>里面?
怎么分别javascript写在<head>里还是<body>里面? 具体哪些语句写在<body>里,哪些语句写在<head>里 满意答案 BeginN ...
- Java的运行原理
在Java中引入了虚拟机的概念,即在机器和编译程序之间加入了一层抽象的虚拟的机器.这台虚拟的机器在任何平台上都提供给编译程序一个的共同的接口.编译程序只需要面向虚拟机,生成虚拟机能够理解的代码,然后由 ...