D. Kuro and GCD and XOR and SUM

时间:2022-06-16 20:48:45

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!

Input

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.

Output

For each task of type 22, output on one line the desired number vv, or -1 if no such numbers are found.

Examples
input
5
1 1
1 2
2 1 1 3
2 1 1 2
2 1 1 1
output
2
1
-1
input
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
output
9
9
9
-1
-1
-1
Note

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字典树

现在我也只是刚刚学有点懵逼

D. Kuro and GCD and XOR and SUM

我看这篇博客学的 转载

源地址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的更多相关文章

  1. 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, ...

  2. 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( ...

  3. Codeforces 979 D&period; Kuro and GCD and XOR and SUM&lpar;异或和,01字典树)

    Codeforces 979 D. Kuro and GCD and XOR and SUM 题目大意:有两种操作:①给一个数v,加入数组a中②给出三个数x,k,s:从当前数组a中找出一个数u满足 u ...

  4. CodeForces979D:Kuro and GCD and XOR and SUM(Trie树&amp&semi;指针&amp&semi;Xor)

    Kuro is currently playing an educational game about numbers. The game focuses on the greatest common ...

  5. codeforces 979D Kuro and GCD and XOR and SUM

    题意: 给出两种操作: 1.添加一个数字x到数组. 2.给出s,x,k,从数组中找出一个数v满足gcd(x,k) % v == 0 && x + v <= s &&amp ...

  6. Codeforces Round &num;482 &lpar;Div&period; 2&rpar; : Kuro and GCD and XOR and SUM (寻找最大异或值)

    题目链接:http://codeforces.com/contest/979/problem/D 参考大神博客:https://www.cnblogs.com/kickit/p/9046953.htm ...

  7. cf979d Kuro and GCD and XOR and SUM

    set做法 正解是trie-- 主要是要学会 \(a\ \mathrm{xor}\ b \leq a+b\) 这种操作 #include <iostream> #include <c ...

  8. 【Trie】【枚举约数】Codeforces Round &num;482 &lpar;Div&period; 2&rpar; D&period; Kuro and GCD and XOR and SUM

    题意: 给你一个空的可重集,支持以下操作: 向其中塞进一个数x(不超过100000), 询问(x,K,s):如果K不能整除x,直接输出-1.否则,问你可重集中所有是K的倍数的数之中,小于等于s-x,并 ...

  9. 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 ...

随机推荐

  1. C&plus;&plus;基本数据类型解惑

    记得刚学C语言的时候,对那些double,float,long,unsigned int各种混乱,基本是随便用,对数据类型没有一个整体的框架.最近学习<<C++ primer plus&g ...

  2. ASCII 码表

    下面的 ASCII 码表包含数值在0-127之间的字符的十进制.八进制以及十六进制表示. 十进制 八进制 十六进制 字符 描述 0 0 00 NUL   1 1 01 SOH start of hea ...

  3. Linux字符界面和图形界面

    Ubuntu图形界面和字符界面的切换 Ubuntu和其他的Linux系统一样,有图形界面和字符界面,同时能够设置默认的启动界面. linux的显示界面分为命令行的字符界面和图形界面,我们可以设置lin ...

  4. 网络安全*即将推出 手机App安全加密成必定趋势

    年05月22日宣布,为维护国家网络安全.保障中国用户合法利益,中国即将推出网络安全*,关系国家安全和公共利益的系统使用的.重要信息技术产品和服务,应通过网络安全审查.文章出处:*** 网络安全审 ...

  5. SharePoint 2010 BCS - 概要

    博客地址 http://blog.csdn.net/foxdave SharePoint 2010首次引入了BCS的概念 - Business Connectivity Service.即业务连接服务 ...

  6. UE4网络同步属性笔记

    GameMode只有服务端有,适合写游戏逻辑.PlayerController每个客户端拥有一个,并拥有主控权.GameState在服务端同步到全端. CLIENT生成的Actor对其有Authori ...

  7. Linux 使用 arp-scan 检查是否存在IP地址冲突

    如果前期没有做好IP地址规划,即使有IP地址统一不小心也会犯错!推荐服务器IP地址使用要登记明细,上次机房批量部署服务器,就将已再用的IP又分配给另一台服务器,还好对业务没有造成大的影响. 那么在给服 ...

  8. Security注解:&commat;PreAuthorize&comma;&commat;PostAuthorize&comma; &commat;Secured&comma; EL实现方法安全

     说明 (1)JDK版本:1.8(2)Spring Boot 2.0.6(3)Spring Security 5.0.9(4)Spring Data JPA 2.0.11.RELEASE(5)hibe ...

  9. 【iCore1S 双核心板&lowbar;ARM】例程六:WWDG看门狗实验——复位ARM

    实验原理: STM32内部包含窗口看门狗,通过看门狗可以监控程序运行,程序错误 时,未在规定时间喂狗,自动复位ARM.本实验通过按键按下,停止喂狗, 制造程序运行 错误,从而产生复位 . 实验现象: ...

  10. 怎样从外网访问内网Node&period;js?

    本地安装了一个Node.js,只能在局域网内访问,怎样从外网也能访问到本地的Node.js呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动Node.js 默认安装的Node.js端口 ...