inclusionAI/AReaL-boba-2-RL-Code huggingface dataset:
This is an interactive problem.

Kostyanych has chosen a complete undirected graph$^{\dagger}$ with $n$ vertices, and then removed exactly $(n - 2)$ edges from it. You can ask queries of the following type:

  * "? $d$" — Kostyanych tells you the number of vertex $v$ with a degree at least $d$. Among all possible such vertices, he selects the vertex with the minimum degree, and if there are several such vertices, he selects the one with the minimum number. He also tells you the number of another vertex in the graph, with which $v$ is not connected by an edge (if none is found, then $0$ is reported). Among all possible such vertices, he selects the one with the minimum number. Then he removes the vertex $v$ and all edges coming out of it. If the required vertex $v$ is not found, then "$0\ 0$" is reported. 

Find a Hamiltonian path$^{\ddagger}$ in the original graph in at most $n$ queries. It can be proven that under these constraints, a Hamiltonian path always exists.

$^{\dagger}$A complete undirected graph is a graph in which there is exactly one undirected edge between any pair of distinct vertices. Thus, a complete undirected graph with $n$ vertices contains $\frac{n(n-1)}{2}$ edges.

$^{\ddagger}$A Hamiltonian path in a graph is a path that passes through each vertex exactly once.

Input Format:
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases. The description of the test cases follows.

The only line of each test case contains a single integer $n$ ($2 \le n \le 10^5$) — the number of vertices in the graph.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Output Format:


Tags:
['brute force', 'constructive algorithms', 'graphs', 'interactive']

Time Limit: 3000 ms
Memory Limit: 256 MB
Vlad is planning to hold $m$ rounds next month. Each round should contain one problem of difficulty levels 'A', 'B', 'C', 'D', 'E', 'F', and 'G'.

Vlad already has a bank of $n$ problems, where the $i$-th problem has a difficulty level of $a_i$. There may not be enough of these problems, so he may have to come up with a few more problems.

Vlad wants to come up with as few problems as possible, so he asks you to find the minimum number of problems he needs to come up with in order to hold $m$ rounds.

For example, if $m=1$, $n = 10$, $a=$ 'BGECDCBDED', then he needs to come up with two problems: one of difficulty level 'A' and one of difficulty level 'F'.

Input Format:
The first line contains a single integer $t$ ($1 \le t \le 1000$) — the number of test cases.

The first line of each test case contains two integers $n$ and $m$ ($1 \le n \le 50$, $1 \le m \le 5$) — the number of problems in the bank and the number of upcoming rounds, respectively.

The second line of each test case contains a string $a$ of $n$ characters from 'A' to 'G' — the difficulties of the problems in the bank.

Output Format:
For each test case, output a single integer — the minimum number of problems that need to come up with to hold $m$ rounds.

Tags:
['math']

Time Limit: 1000 ms
Memory Limit: 256 MB
Dmitry has $n$ cubes, numbered from left to right from $1$ to $n$. The cube with index $f$ is his favorite.

Dmitry threw all the cubes on the table, and the $i$-th cube showed the value $a_i$ ($1 \le a_i \le 100$). After that, he arranged the cubes in non-increasing order of their values, from largest to smallest. If two cubes show the same value, they can go in any order.

After sorting, Dmitry removed the first $k$ cubes. Then he became interested in whether he removed his favorite cube (note that its position could have changed after sorting).

For example, if $n=5$, $f=2$, $a = [4, \color{green}3, 3, 2, 3]$ (the favorite cube is highlighted in green), and $k = 2$, the following could have happened:

  * After sorting $a=[4, \color{green}3, 3, 3, 2]$, since the favorite cube ended up in the second position, it will be removed.    * After sorting $a=[4, 3, \color{green}3, 3, 2]$, since the favorite cube ended up in the third position, it will not be removed.

Input Format:
The first line contains an integer $t$ ($1 \le t \le 1000$) — the number of test cases. Then follow the descriptions of the test cases.

The first line of each test case description contains three integers $n$, $f$, and $k$ ($1 \le f, k \le n \le 100$) — the number of cubes, the index of Dmitry's favorite cube, and the number of removed cubes, respectively.

The second line of each test case description contains $n$ integers $a_i$ ($1 \le a_i \le 100$) — the values shown on the cubes.

Output Format:
For each test case, output one line — "YES" if the cube will be removed in all cases, "NO" if it will not be removed in any case, "MAYBE" if it may be either removed or left.

You can output the answer in any case. For example, the strings "YES", "nO", "mAyBe" will be accepted as answers.

Tags:
['sortings', 'sortings']

Time Limit: 1000 ms
Memory Limit: 256 MB
Sofia had an array of $n$ integers $a_1, a_2, \ldots, a_n$. One day she got bored with it, so she decided to sequentially apply $m$ modification operations to it.

Each modification operation is described by a pair of numbers $\langle c_j, d_j \rangle$ and means that the element of the array with index $c_j$ should be assigned the value $d_j$, i.e., perform the assignment $a_{c_j} = d_j$. After applying all modification operations sequentially, Sofia discarded the resulting array.

Recently, you found an array of $n$ integers $b_1, b_2, \ldots, b_n$. You are interested in whether this array is Sofia's array. You know the values of the original array, as well as the values $d_1, d_2, \ldots, d_m$. The values $c_1, c_2, \ldots, c_m$ turned out to be lost.

Is there a sequence $c_1, c_2, \ldots, c_m$ such that the sequential application of modification operations $\langle c_1, d_1, \rangle, \langle c_2, d_2, \rangle, \ldots, \langle c_m, d_m \rangle$ to the array $a_1, a_2, \ldots, a_n$ transforms it into the array $b_1, b_2, \ldots, b_n$?

Input Format:
The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

Then follow the descriptions of the test cases.

The first line of each test case contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the size of the array.

The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of the original array.

The third line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^9$) — the elements of the found array.

The fourth line contains an integer $m$ ($1 \le m \le 2 \cdot 10^5$) — the number of modification operations.

The fifth line contains $m$ integers $d_1, d_2, \ldots, d_m$ ($1 \le d_j \le 10^9$) — the preserved value for each modification operation.

It is guaranteed that the sum of the values of $n$ for all test cases does not exceed $2 \cdot 10^5$, similarly the sum of the values of $m$ for all test cases does not exceed $2 \cdot 10^5$.

Output Format:
Output $t$ lines, each of which is the answer to the corresponding test case. As an answer, output "YES" if there exists a suitable sequence $c_1, c_2, \ldots, c_m$, and "NO" otherwise.

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).

Tags:
['constructive algorithms', 'greedy']

Time Limit: 2000 ms
Memory Limit: 256 MB
GCD (Greatest Common Divisor) of two integers $x$ and $y$ is the maximum integer $z$ by which both $x$ and $y$ are divisible. For example, $GCD(36, 48) = 12$, $GCD(5, 10) = 5$, and $GCD(7,11) = 1$.

Kristina has an array $a$ consisting of exactly $n$ positive integers. She wants to count the GCD of each neighbouring pair of numbers to get a new array $b$, called GCD-sequence.

So, the elements of the GCD-sequence $b$ will be calculated using the formula $b_i = GCD(a_i, a_{i + 1})$ for $1 \le i \le n - 1$.

Determine whether it is possible to remove exactly one number from the array $a$ so that the GCD sequence $b$ is non-decreasing (i.e., $b_i \le b_{i+1}$ is always true).

For example, let Khristina had an array $a$ = [$20, 6, 12, 3, 48, 36$]. If she removes $a_4 = 3$ from it and counts the GCD-sequence of $b$, she gets:

  * $b_1 = GCD(20, 6) = 2$    * $b_2 = GCD(6, 12) = 6$    * $b_3 = GCD(12, 48) = 12$    * $b_4 = GCD(48, 36) = 12$ 

The resulting GCD sequence $b$ = [$2,6,12,12$] is non-decreasing because $b_1 \le b_2 \le b_3 \le b_4$.

Input Format:
The first line of input data contains a single number $t$ ($1 \le t \le 10^4$) — he number of test cases in the test.

This is followed by the descriptions of the test cases.

The first line of each test case contains a single integer $n$ ($3 \le n \le 2 \cdot 10^5$) — the number of elements in the array $a$.

The second line of each test case contains exactly $n$ integers $a_i$ ($1 \le a_i \le 10^9$) — the elements of array $a$.

It is guaranteed that the sum of $n$ over all test case does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output a single line:

  * "YES" if you can remove exactly one number from the array $a$ so that the GCD-sequence of $b$ is non-decreasing;    * "NO" otherwise. 

You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as a positive answer).

Tags:
['greedy', 'implementation', 'math', 'number theory']

Time Limit: 2000 ms
Memory Limit: 256 MB
You have been given a matrix $a$ of size $n$ by $m$, containing a permutation of integers from $1$ to $n \cdot m$.

A permutation of $n$ integers is an array containing all numbers from $1$ to $n$ exactly once. For example, the arrays $[1]$, $[2, 1, 3]$, $[5, 4, 3, 2, 1]$ are permutations, while the arrays $[1, 1]$, $[100]$, $[1, 2, 4, 5]$ are not.

A matrix contains a permutation if, when all its elements are written out, the resulting array is a permutation. Matrices $[[1, 2], [3, 4]]$, $[[1]]$, $[[1, 5, 3], [2, 6, 4]]$ contain permutations, while matrices $[[2]]$, $[[1, 1], [2, 2]]$, $[[1, 2], [100, 200]]$ do not.

You can perform one of the following two actions in one operation:

  * choose columns $c$ and $d$ ($1 \le c, d \le m$, $c \ne d$) and swap these columns;    * choose rows $c$ and $d$ ($1 \le c, d \le n$, $c \ne d$) and swap these rows. 

You can perform any number of operations.

You are given the original matrix $a$ and the matrix $b$. Your task is to determine whether it is possible to transform matrix $a$ into matrix $b$ using the given operations.

Input Format:
The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case description contains $2$ integers $n$ and $m$ ($1 \le n, m \le n \cdot m \le 2 \cdot 10^5$) — the sizes of the matrix.

The next $n$ lines contain $m$ integers $a_{ij}$ each ($1 \le a_{ij} \le n \cdot m$). It is guaranteed that matrix $a$ is a permutation.

The next $n$ lines contain $m$ integers $b_{ij}$ each ($1 \le b_{ij} \le n \cdot m$). It is guaranteed that matrix $b$ is a permutation.

It is guaranteed that the sum of the values $n \cdot m$ for all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, output "YES" if the second matrix can be obtained from the first, and "NO" otherwise.

You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.

Tags:
['constructive algorithms', 'data structures', 'greedy', 'hashing', 'implementation', 'math', 'matrices', 'sortings']

Time Limit: 3000 ms
Memory Limit: 256 MB
This is a hard version of the problem; it differs from the easy version only by the question. The easy version only needs you to print whether some values are non-zero or not. The hard version needs you to print the exact values.

Alice and Bob are dividing the field. The field is a rectangle of size $n \times m$ ($2 \le n, m \le 10^9$); the rows are numbered from $1$ to $n$ from top to bottom, and the columns are numbered from $1$ to $m$ from left to right. The cell at the intersection of row $r$ and column $c$ is denoted as ($r, c$).

Bob has $k$ ($2 \le k \le 2 \cdot 10^5$) fountains, all of them are located in different cells of the field. Alice is responsible for dividing the field, but she must meet several conditions:

  * To divide the field, Alice will start her path in any free (without a fountain) cell on the left or top side of the field and will move, each time moving to the adjacent cell down or right. Her path will end on the right or bottom side of the field.    * Alice's path will divide the field into two parts — one part will belong to Alice (this part includes the cells of her path), the other part — to Bob.    * Alice will own the part that includes the cell ($n, 1$).    * Bob will own the part that includes the cell ($1, m$). 

Alice wants to divide the field in such a way as to get as many cells as possible.

Bob wants to keep ownership of all the fountains, but he can give one of them to Alice. First, output the integer $\alpha$ — the maximum possible size of Alice's plot, if Bob does not give her any fountain (i.e., all fountains will remain on Bob's plot).

Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\alpha + a_i$.

Input Format:
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The first line of each test case contains three integers $n$, $m$, and $k$ ($2 \le n, m \le 10^9$, $2 \le k \le 2 \cdot 10^5$) — the field sizes and the number of fountains, respectively.

Then follow $k$ lines, each containing two numbers $r_i$ and $c_i$ ($1 \le r_i \le n$, $1 \le c_i \le m$) — the coordinates of the cell with the $i$-th fountain. It is guaranteed that all cells are distinct and none of them is ($n, 1$).

It is guaranteed that the sum of $k$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, first output $\alpha$ — the maximum size of the plot that can belong to Alice if Bob does not give her any of the fountains. Then output $k$ non-negative integers $a_1, a_2, \dots, a_k$, where $a_i$ is a value such that after Bob gives Alice the $i$-th fountain, the maximum size of her plot will be $\alpha + a_i$.

Tags:
['math', 'sortings']

Time Limit: 3000 ms
Memory Limit: 256 MB
Yasya was walking in the forest and accidentally found a tree with $n$ vertices. A tree is a connected undirected graph with no cycles.

Next to the tree, the girl found an ancient manuscript with $m$ queries written on it. The queries can be of two types.

The first type of query is described by the integer $y$. The weight of each edge in the tree is replaced by the [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng) of the weight of that edge and the integer $y$.

The second type is described by the vertex $v$ and the integer $x$. Yasya chooses a vertex $u$ ($1 \le u \le n$, $u \neq v$) and mentally draws a bidirectional edge of weight $x$ from $v$ to $u$ in the tree.

Then Yasya finds a simple cycle in the resulting graph and calculates the [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng) of all the edges in it. She wants to choose a vertex $u$ such that the calculated value is maximum. This calculated value will be the answer to the query. It can be shown that such a cycle exists and is unique under the given constraints (independent of the choice of $u$). If an edge between $v$ and $u$ already existed, a simple cycle is the path $v \to u \to v$.

Note that the second type of query is performed mentally, meaning the tree does not change in any way after it.

Help Yasya answer all the queries.

Input Format:
The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases.

The descriptions of the test cases follow.

The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m \le 2 \cdot 10^5$) — the number of vertices in the tree and the number of queries.

The next $n - 1$ lines of each test case contain three integers $v$, $u$, $w$ ($1 \le v, u \le n$, $1 \le w \le 10^9$) — the ends of some edge in the tree and its weight.

It is guaranteed that the given set of edges forms a tree.

The next $m$ lines of each test case describe the queries:

  * ^ $y$ ($1 \le y \le 10^9$) — parameter of the first type query;    * ? $v$ $x$ ($1 \le v \le n$, $1 \le x \le 10^9$) — parameters of the second type query. 

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. The same is guaranteed for $m$.

Output Format:
For each test case, output the answers to the queries of the second type.

Tags:
['bitmasks', 'data structures', 'dfs and similar', 'graphs', 'greedy', 'strings', 'trees']

Time Limit: 2500 ms
Memory Limit: 512 MB
Turtle and Piggy are playing a number game.

First, Turtle will choose an integer $x$, such that $l \le x \le r$, where $l, r$ are given. It's also guaranteed that $2l \le r$.

Then, Piggy will keep doing the following operation until $x$ becomes $1$:

  * Choose an integer $p$ such that $p \ge 2$ and $p \mid x$ (i.e. $x$ is a multiple of $p$).    * Set $x$ to $\frac{x}{p}$, and the score will increase by $1$. 

The score is initially $0$. Both Turtle and Piggy want to maximize the score. Please help them to calculate the maximum score.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains two integers $l, r$ ($1 \le l \le r \le 10^9, 2l \le r$) — The range where Turtle can choose the integer from.

Output Format:
For each test case, output a single integer — the maximum score.

Tags:
['brute force', 'greedy', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
There is a sequence $a_0, a_1, a_2, \ldots$ of infinite length. Initially $a_i = i$ for every non-negative integer $i$.

After every second, each element of the sequence will simultaneously change. $a_i$ will change to $a_{i - 1} \mid a_i \mid a_{i + 1}$ for every positive integer $i$. $a_0$ will change to $a_0 \mid a_1$. Here, $|$ denotes [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR).

Turtle is asked to find the value of $a_n$ after $m$ seconds. In particular, if $m = 0$, then he needs to find the initial value of $a_n$. He is tired of calculating so many values, so please help him!

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains two integers $n, m$ ($0 \le n, m \le 10^9$).

Output Format:
For each test case, output a single integer — the value of $a_n$ after $m$ seconds.

Tags:
['bitmasks', 'math']

Time Limit: 1000 ms
Memory Limit: 256 MB
Turtle was playing with a sequence $a_1, a_2, \ldots, a_n$ consisting of positive integers. Unfortunately, some of the integers went missing while playing.

Now the sequence becomes incomplete. There may exist an arbitrary number of indices $i$ such that $a_i$ becomes $-1$. Let the new sequence be $a'$.

Turtle is sad. But Turtle remembers that for every integer $i$ from $1$ to $n - 1$, either $a_i = \left\lfloor\frac{a_{i + 1}}{2}\right\rfloor$ or $a_{i + 1} = \left\lfloor\frac{a_i}{2}\right\rfloor$ holds for the original sequence $a$.

Turtle wants you to help him complete the sequence. But sometimes Turtle makes mistakes, so you need to tell him if you can't complete the sequence.

Formally, you need to find another sequence $b_1, b_2, \ldots, b_n$ consisting of positive integers such that:

  * For every integer $i$ from $1$ to $n$, if $a'_i \ne -1$, then $b_i = a'_i$.    * For every integer $i$ from $1$ to $n - 1$, either $b_i = \left\lfloor\frac{b_{i + 1}}{2}\right\rfloor$ or $b_{i + 1} = \left\lfloor\frac{b_i}{2}\right\rfloor$ holds.    * For every integer $i$ from $1$ to $n$, $1 \le b_i \le 10^9$. 

If there is no sequence $b_1, b_2, \ldots, b_n$ that satisfies all of the conditions above, you need to report $-1$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the length of the sequence.

The second line of each test case contains $n$ integers $a'_1, a'_2, \ldots, a'_n$ ($a'_i = -1$ or $1 \le a'_i \le 10^8$) — the elements of the sequence $a'$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.

Output Format:
For each test case, if there is no sequence $b_1, b_2, \ldots, b_n$ that satisfies all of the conditions, output a single integer $-1$.

Otherwise, output $n$ integers $b_1, b_2, \ldots, b_n$ — the elements of the sequence $b_1, b_2, \ldots, b_n$ you find. The sequence should satisfy that $1 \le b_i \le 10^9$ for every integer $i$ from $1$ to $n$. If there are multiple answers, print any of them.

Tags:
['bitmasks', 'brute force', 'constructive algorithms', 'greedy', 'implementation', 'math']

Time Limit: 3000 ms
Memory Limit: 256 MB
Turtle just learned how to multiply two integers in his math class, and he was very excited.

Then Piggy gave him an integer $n$, and asked him to construct a sequence $a_1, a_2, \ldots, a_n$ consisting of integers which satisfied the following conditions:

  * For all $1 \le i \le n$, $1 \le a_i \le 3 \cdot 10^5$.    * For all $1 \le i < j \le n - 1$, $a_i \cdot a_{i + 1} \ne a_j \cdot a_{j + 1}$. 

Of all such sequences, Piggy asked Turtle to find the one with the minimum number of distinct elements.

Turtle definitely could not solve the problem, so please help him!

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \le n \le 10^6$) — the length of the sequence $a$.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.

Output Format:
For each test case, output $n$ integers $a_1, a_2, \ldots, a_n$ — the elements of the sequence $a$.

If there are multiple answers, print any of them.

Tags:
['constructive algorithms', 'dfs and similar', 'graphs', 'number theory']

Time Limit: 3000 ms
Memory Limit: 512 MB
Turtle just received $n$ segments and a sequence $a_1, a_2, \ldots, a_n$. The $i$-th segment is $[l_i, r_i]$.

Turtle will create an undirected graph $G$. If segment $i$ and segment $j$ intersect, then Turtle will add an undirected edge between $i$ and $j$ with a weight of $|a_i - a_j|$, for every $i \ne j$.

Turtle wants you to calculate the sum of the weights of the edges of the minimum spanning tree of the graph $G$, or report that the graph $G$ has no spanning tree.

We say two segments $[l_1, r_1]$ and $[l_2, r_2]$ intersect if and only if $\max(l_1, l_2) \le \min(r_1, r_2)$.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($2 \le n \le 5 \cdot 10^5$) — the number of segments.

The $i$-th of the following $n$ lines contains three integers $l_i, r_i, a_i$ ($1 \le l_i \le r_i \le 10^9, 1 \le a_i \le 10^9$) — the $i$-th segment and the $i$-th element of the sequence.

It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.

Output Format:
For each test case, output a single integer — the sum of the weights of the edges of the minimum spanning tree of the graph $G$. If the graph $G$ has no spanning tree, output $-1$.

Tags:
['data structures', 'dsu', 'graphs', 'greedy']

Time Limit: 5000 ms
Memory Limit: 512 MB