{"problem":{"name":"D. Kuro and GCD and XOR and SUM","description":{"content":"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 solve","description_type":"Markdown"},"platform":"Codeforces","limit":{"time_limit":2000,"memory_limit":524288},"difficulty":"None","is_remote":true,"is_sync":true,"sync_url":null,"sign":"CF979D"},"statements":[{"statement_type":"Markdown","content":"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.\n\nSadly, 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.\n\nInitally, there is an empty array $a$. The game consists of $q$ tasks of two types. The first type asks Katie to add a number $u_i$ to $a$. The second type asks Katie to find a number $v$ existing in $a$ such that $k_i \\mid GCD(x_i, v)$, $x_i + v \\leq s_i$, and $x_i \\oplus v$ is maximized, where $\\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), $GCD(c, d)$ denotes the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $c$ and $d$, and $y \\mid x$ means $x$ is divisible by $y$, or report _\\-1_ if no such numbers are found.\n\nSince 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!\n\n## Input\n\nThe first line contains one integer $q$ ($2 \\leq q \\leq 10^{5}$) — the number of tasks the game wants you to perform.\n\n$q$ lines follow, each line begins with an integer $t_i$ — the type of the task:\n\n*   If $t_i = 1$, an integer $u_i$ follow ($1 \\leq u_i \\leq 10^{5}$) — you have to add $u_i$ to the array $a$.\n*   If $t_i = 2$, three integers $x_i$, $k_i$, and $s_i$ follow ($1 \\leq x_i, k_i, s_i \\leq 10^{5}$) — you must find a number $v$ existing in the array $a$ such that $k_i \\mid GCD(x_i, v)$, $x_i + v \\leq s_i$, and $x_i \\oplus v$ is maximized, where $\\oplus$ denotes the XOR operation, or report _\\-1_ if no such numbers are found.\n\nIt is guaranteed that the type of the first task is type $1$, and there exists at least one task of type $2$.\n\n## Output\n\nFor each task of type $2$, output on one line the desired number $v$, or _\\-1_ if no such numbers are found.\n\n[samples]\n\n## Note\n\nIn the first example, there are 5 tasks:\n\n*   The first task requires you to add $1$ into $a$. $a$ is now $\\left{1\\right}$.\n*   The second task requires you to add $2$ into $a$. $a$ is now $\\left{1, 2\\right}$.\n*   The third task asks you a question with $x = 1$, $k = 1$ and $s = 3$. Taking both $1$ and $2$ as $v$ satisfies $1 \\mid GCD(1, v)$ and $1 + v \\leq 3$. Because $2 \\oplus 1 = 3 &gt; 1 \\oplus 1 = 0$, $2$ is the answer to this task.\n*   The fourth task asks you a question with $x = 1$, $k = 1$ and $s = 2$. Only $v = 1$ satisfies $1 \\mid GCD(1, v)$ and $1 + v \\leq 2$, so $1$ is the answer to this task.\n*   The fifth task asks you a question with $x = 1$, $k = 1$ and $s = 1$. There are no elements in $a$ that satisfy the conditions, so we report _\\-1_ as the answer to this task.","is_translate":false,"language":"English"},{"statement_type":"Markdown","content":"Kuro 正在玩一个关于数字的教育游戏。该游戏聚焦于两个数的最大公约数（GCD）、异或值以及两数之和。Kuro 非常喜欢这个游戏，每天都逐关挑战。\n\n遗憾的是，他有一天要去度假，无法独自继续他的解题连胜。由于 Katie 是一个可靠的人，Kuro 善意地请她在这一天来他家替他玩游戏。\n\n初始时，有一个空数组 $a$。游戏包含 $q$ 个任务，分为两种类型。第一种类型要求 Katie 将数字 $u_i$ 添加到 $a$ 中。第二种类型要求 Katie 找到一个存在于 $a$ 中的数字 $v$，使得 $k_i \\mid GCD(x_i, v)$，$x_i + v \\leq s_i$，且 $x_i \\oplus v$ 最大化，其中 $\\oplus$ 表示按位异或运算，$GCD(c, d)$ 表示整数 $c$ 和 $d$ 的最大公约数，$y \\mid x$ 表示 $x$ 能被 $y$ 整除；若不存在这样的数，则输出 _-1_。\n\n由于你是一名程序员，Katie 需要你自动且准确地完成游戏中的任务，以满足她的好友 Kuro。快来帮助她吧！\n\n第一行包含一个整数 $q$（$2 \\leq q \\leq 10^5$）——表示游戏要求你执行的任务数量。\n\n接下来 $q$ 行，每行以一个整数 $t_i$ 开头——表示任务类型：\n\n保证第一个任务的类型为类型 $1$，且至少存在一个类型 $2$ 的任务。\n\n对于每个类型 $2$ 的任务，在一行中输出所求的数字 $v$，若不存在则输出 _-1_。\n\n## Input\n\n第一行包含一个整数 $q$（$2 \\leq q \\leq 10^5$）——表示游戏要求你执行的任务数量。接下来 $q$ 行，每行以一个整数 $t_i$ 开头——表示任务类型：\n\n若 $t_i = 1$，则后跟一个整数 $u_i$（$1 \\leq u_i \\leq 10^5$）——你需要将 $u_i$ 添加到数组 $a$ 中。\n\n若 $t_i = 2$，则后跟三个整数 $x_i$、$k_i$ 和 $s_i$（$1 \\leq x_i, k_i, s_i \\leq 10^5$）——你需要在数组 $a$ 中找到一个数 $v$，使得 $k_i \\mid GCD(x_i, v)$，$x_i + v \\leq s_i$，且 $x_i \\oplus v$ 最大化，其中 $\\oplus$ 表示异或运算；若不存在这样的数，则输出 _-1_。\n\n保证第一个任务的类型为类型 $1$，且至少存在一个类型 $2$ 的任务。\n\n## Output\n\n对于每个类型 $2$ 的任务，在一行中输出所求的数字 $v$，若不存在则输出 _-1_。\n\n[samples]\n\n## Note\n\n在第一个例子中，有 5 个任务：\n\n第一个任务要求将 $1$ 加入 $a$。此时 $a = \\{1\\}$。\n\n第二个任务要求将 $2$ 加入 $a$。此时 $a = \\{1, 2\\}$。\n\n第三个任务询问 $x = 1$、$k = 1$、$s = 3$ 的情况。取 $v = 1$ 或 $v = 2$ 均满足 $1 \\mid GCD(1, v)$ 且 $1 + v \\leq 3$。由于 $2 \\oplus 1 = 3 > 1 \\oplus 1 = 0$，因此答案为 $2$。\n\n第四个任务询问 $x = 1$、$k = 1$、$s = 2$ 的情况。只有 $v = 1$ 满足 $1 \\mid GCD(1, v)$ 且 $1 + v \\leq 2$，因此答案为 $1$。\n\n第五个任务询问 $x = 1$、$k = 1$、$s = 1$ 的情况。数组 $a$ 中没有元素满足条件，因此答案为 _-1_。","is_translate":true,"language":"Chinese"},{"statement_type":"Markdown","content":"**Definitions**  \nLet $ q \\in \\mathbb{Z} $ be the number of tasks.  \nLet $ A \\subseteq \\mathbb{Z}^+ $ be the multiset of numbers added to the array (initially empty).  \n\nFor each task $ i \\in \\{1, \\dots, q\\} $:  \n- If $ t_i = 1 $: add integer $ u_i $ to $ A $.  \n- If $ t_i = 2 $: given integers $ x_i, s_i, k_i $, find $ v \\in A $ such that:  \n  1. $ k_i \\mid \\gcd(x_i, v) $,  \n  2. $ x_i + v \\leq s_i $,  \n  3. $ x_i \\oplus v $ is maximized,  \n  where $ \\oplus $ denotes bitwise XOR. If no such $ v $ exists, return $-1$.  \n\n**Constraints**  \n1. $ 2 \\leq q \\leq 10^5 $  \n2. For each task:  \n   - If $ t_i = 1 $: $ 1 \\leq u_i \\leq 10^5 $  \n   - If $ t_i = 2 $: $ 1 \\leq x_i, s_i, k_i \\leq 10^5 $  \n3. The first task is always type 1.  \n4. At least one task of type 2 exists.  \n\n**Objective**  \nFor each type-2 task $ i $, compute:  \n$$\nv^* = \\arg\\max_{\\substack{v \\in A \\\\ k_i \\mid \\gcd(x_i, v) \\\\ x_i + v \\leq s_i}} (x_i \\oplus v)\n$$  \nand output $ v^* $, or $-1$ if the set of valid $ v $ is empty.","is_translate":false,"language":"Formal"}],"meta":{"iden":"CF979D","tags":["binary search","bitmasks","brute force","data structures","dp","dsu","greedy","math","number theory","strings","trees"],"sample_group":[["5\n1 1\n1 2\n2 1 1 3\n2 1 1 2\n2 1 1 1","2\n1\n-1"],["10\n1 9\n2 9 9 22\n2 3 3 18\n1 25\n2 9 9 20\n2 25 25 14\n1 20\n2 26 26 3\n1 14\n2 20 20 9","9\n9\n9\n-1\n-1\n-1"]],"created_at":"2026-03-03 11:00:39"}}