Congratulations, you have been accepted to the Master's Assistance Center! However, you were extremely bored in class and got tired of doing nothing, so you came up with a game for yourself.
You are given a string $$$s$$$ and an even integer $$$n$$$. There are two types of operations that you can apply to it:
It is required to determine the lexicographically smallest$$$^{\dagger}$$$ string that can be obtained after applying exactly $$$n$$$ operations. Note that you can apply operations of different types in any order, but you must apply exactly $$$n$$$ operations in total.
$$$^{\dagger}$$$A string $$$a$$$ is lexicographically smaller than a string $$$b$$$ if and only if one of the following holds:
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 500$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single even integer $$$n$$$ ($$$2 \leq n \leq 10^9$$$) — the number of operations applied to the string $$$s$$$.
The second line of each test case contains a single string $$$s$$$ ($$$1 \leq |s| \leq 100$$$), consisting of lowercase English letters, — the string to which the operations are applied.
For each test case, output a single line — the lexicographically smallest string that can be obtained after applying exactly $$$n$$$ operations.
54cpm2grib10kupitimilablodarbuz1000000000capybara6abacaba
cpm birggrib kupitimilablodarbuz arabypaccapybara abacaba
In the first test case, you can apply the operation of the second type (i.e., reverse the string $$$s$$$) $$$4$$$ times. Then the string $$$s$$$ will remain equal to cpm.
In the second test case, you can do the following:
In the Master's Assistance Center, Nyam-Nyam was given a homework assignment in informatics.
There is an array $$$a$$$ of length $$$n$$$, and you want to divide it into $$$k \gt 1$$$ subsegments$$$^{\dagger}$$$ in such a way that the $$$\operatorname{MEX} ^{\ddagger}$$$ on each subsegment is equal to the same integer.
Help Nyam-Nyam find any suitable division, or determine that it does not exist.
$$$^{\dagger}$$$A division of an array into $$$k$$$ subsegments is defined as $$$k$$$ pairs of integers $$$(l_1, r_1), (l_2, r_2), \ldots, (l_k, r_k)$$$ such that $$$l_i \le r_i$$$ and for each $$$1 \le j \le k - 1$$$, $$$l_{j + 1} = r_j + 1$$$, and also $$$l_1 = 1$$$ and $$$r_k = n$$$. These pairs represent the subsegments themselves.
$$$^{\ddagger}\operatorname{MEX}$$$ of an array is the smallest non-negative integer that does not belong to the array.
For example:
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \lt n$$$) — the elements of the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output a single integer $$$-1$$$ if a suitable division does not exist.
Otherwise, on the first line, output an integer $$$k$$$ ($$$2 \le k \le n$$$) — the number of subsegments in the division.
Then output $$$k$$$ lines — the division into subsegments. The $$$i$$$-th line should contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \le n$$$) — the boundaries of the $$$i$$$-th subsegment.
The following conditions must be satisfied:
If there are multiple possible solutions, output any of them.
520 050 1 2 3 480 1 7 1 0 1 0 332 2 240 1 2 0
2 1 1 2 2 -1 3 1 3 4 5 6 8 3 1 1 2 2 3 3 -1
In the first test case, the array $$$a$$$ can be divided into $$$2$$$ subsegments with boundaries $$$[1, 1]$$$ and $$$[2, 2]$$$:
In the second test case, it can be proven that the required division does not exist.
In the third test case, the array $$$a$$$ can be divided into $$$3$$$ subsegments with boundaries $$$[1, 3]$$$, $$$[4, 5]$$$, $$$[6, 8]$$$:
In the new messenger for the students of the Master's Assistance Center, Keftemerum, an update is planned, in which developers want to optimize the set of messages shown to the user. There are a total of $$$n$$$ messages. Each message is characterized by two integers $$$a_i$$$ and $$$b_i$$$. The time spent reading the set of messages with numbers $$$p_1, p_2, \ldots, p_k$$$ ($$$1 \le p_i \le n$$$, all $$$p_i$$$ are distinct) is calculated by the formula:
$$$$$$\Large \sum_{i=1}^{k} a_{p_i} + \sum_{i=1}^{k - 1} |b_{p_i} - b_{p_{i+1}}|$$$$$$
Note that the time to read a set of messages consisting of one message with number $$$p_1$$$ is equal to $$$a_{p_1}$$$. Also, the time to read an empty set of messages is considered to be $$$0$$$.
The user can determine the time $$$l$$$ that he is willing to spend in the messenger. The messenger must inform the user of the maximum possible size of the set of messages, the reading time of which does not exceed $$$l$$$. Note that the maximum size of the set of messages can be equal to $$$0$$$.
The developers of the popular messenger failed to implement this function, so they asked you to solve this problem.
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 5 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$l$$$ ($$$1 \leq n \leq 2000$$$, $$$1 \leq l \leq 10^9$$$) — the number of messages and the time the user is willing to spend in the messenger.
The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \le a_i, b_i \le 10^9$$$) — characteristics of the $$$i$$$-th message.
It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$4 \cdot 10^6$$$.
For each test case, output a single integer — the maximum possible size of a set of messages, the reading time of which does not exceed $$$l$$$.
55 84 31 52 44 32 31 64 103 124 82 12 125 2624 78 2830 223 817 175 1415 31000000000 998244353179 239228 1337993 1007
3 1 2 1 0
In the first test case, you can take a set of three messages with numbers $$$p_1 = 3$$$, $$$p_2 = 2$$$, and $$$p_3 = 5$$$. The time spent reading this set is equal to $$$a_3 + a_2 + a_5 + |b_3 - b_2| + |b_2 - b_5| = 2 + 1 + 2 + |4 - 5| + |5 - 3| = 8$$$.
In the second test case, you can take a set of one message with number $$$p_1 = 1$$$. The time spent reading this set is equal to $$$a_1 = 4$$$.
In the fifth test case, it can be shown that there is no such non-empty set of messages, the reading time of which does not exceed $$$l$$$.
The Master's Assistance Center has announced an entrance exam, which consists of the following.
The candidate is given a set $$$s$$$ of size $$$n$$$ and some strange integer $$$c$$$. For this set, it is needed to calculate the number of pairs of integers $$$(x, y)$$$ such that $$$0 \leq x \leq y \leq c$$$, $$$x + y$$$ is not contained in the set $$$s$$$, and also $$$y - x$$$ is not contained in the set $$$s$$$.
Your friend wants to enter the Center. Help him pass the exam!
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$c$$$ ($$$1 \leq n \leq 3 \cdot 10^5$$$, $$$1 \leq c \leq 10^9$$$) — the size of the set and the strange integer.
The second line of each test case contains $$$n$$$ integers $$$s_1, s_2, \ldots, s_{n}$$$ ($$$0 \leq s_1 \lt s_2 \lt \ldots \lt s_{n} \leq c$$$) — the elements of the set $$$s$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
For each test case, output a single integer — the number of suitable pairs of integers.
83 31 2 31 179574 60 3 5 61 115 100 2 4 8 105 101 3 5 7 94 102 4 6 73 1000000000228 1337 998244353
3 16139 10 2 33 36 35 499999998999122959
In the first test case, the following pairs are suitable: $$$(0, 0)$$$, $$$(2, 2)$$$, $$$(3, 3)$$$.
In the third test case, the following pairs are suitable: $$$(0, 1)$$$, $$$(0, 2)$$$, $$$(0, 4)$$$, $$$(1, 3)$$$, $$$(2, 6)$$$, $$$(3, 4)$$$, $$$(3, 5)$$$, $$$(4, 5)$$$, $$$(4, 6)$$$, $$$(5, 6)$$$.
The New Year has arrived in the Master's Assistance Center, which means it's time to introduce a new feature!
Now students are given distance learning courses, with a total of $$$n$$$ courses available. For the $$$i$$$-th distance learning course, a student can receive a grade ranging from $$$x_i$$$ to $$$y_i$$$.
However, not all courses may be available to each student. Specifically, the $$$j$$$-th student is only given courses with numbers from $$$l_j$$$ to $$$r_j$$$, meaning the distance learning courses with numbers $$$l_j, l_j + 1, \ldots, r_j$$$.
The creators of the distance learning courses have decided to determine the final grade in a special way. Let the $$$j$$$-th student receive grades $$$c_{l_j}, c_{l_j + 1}, \ldots, c_{r_j}$$$ for their distance learning courses. Then their final grade will be equal to $$$c_{l_j}$$$ $$$|$$$ $$$c_{l_j + 1}$$$ $$$|$$$ $$$\ldots$$$ $$$|$$$ $$$c_{r_j}$$$, where $$$|$$$ denotes the bitwise OR operation.
Since the chatbot for solving distance learning courses is broken, the students have asked for your help. For each of the $$$q$$$ students, tell them the maximum final grade they can achieve.
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 2 \cdot 10^4$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of distance learning courses.
Each of the following $$$n$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$0 \le x_i \le y_i \lt 2^{30}$$$) — the minimum and maximum grade that can be received for the $$$i$$$-th course.
The next line contains a single integer $$$q$$$ ($$$1 \le q \le 2\cdot10^5$$$) — the number of students.
Each of the following $$$q$$$ lines contains two integers $$$l_j$$$ and $$$r_j$$$ ($$$1 \le l_j \le r_j \le n$$$) — the minimum and maximum course numbers accessible to the $$$j$$$-th student.
It is guaranteed that the sum of $$$n$$$ over all test cases and the sum of $$$q$$$ over all test cases do not exceed $$$2\cdot10^5$$$.
For each test case, output $$$q$$$ integers, where the $$$j$$$-th integer is the maximum final grade that the $$$j$$$-th student can achieve.
320 13 431 11 22 241 71 73 102 251 33 42 31 41 261 22 20 11 13 30 043 45 52 51 2
1 5 4 15 11 15 15 7 1 3 3 3
In the first test case:
In the second test case:
Master Andrey loves trees$$$^{\dagger}$$$ very much, so he has a tree consisting of $$$n$$$ vertices.
But it's not that simple. Master Timofey decided to steal one vertex from the tree. If Timofey stole vertex $$$v$$$ from the tree, then vertex $$$v$$$ and all edges with one end at vertex $$$v$$$ are removed from the tree, while the numbers of other vertices remain unchanged. To prevent Andrey from getting upset, Timofey decided to make the resulting graph a tree again. To do this, he can add edges between any vertices $$$a$$$ and $$$b$$$, but when adding such an edge, he must pay $$$|a - b|$$$ coins to the Master's Assistance Center.
Note that the resulting tree does not contain vertex $$$v$$$.
Timofey has not yet decided which vertex $$$v$$$ he will remove from the tree, so he wants to know for each vertex $$$1 \leq v \leq n$$$, the minimum number of coins needed to be spent to make the graph a tree again after removing vertex $$$v$$$, as well as which edges need to be added.
$$$^{\dagger}$$$A tree is an undirected connected graph without cycles.
Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$5 \le n \le 2\cdot10^5$$$) — the number of vertices in Andrey's tree.
The next $$$n - 1$$$ lines contain a description of the tree's edges. The $$$i$$$-th of these lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \le u_i, v_i \le n$$$) — the numbers of vertices connected by the $$$i$$$-th edge.
It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot10^5$$$.
For each test case, output the answer in the following format:
For each vertex $$$v$$$ (in the order from $$$1$$$ to $$$n$$$), in the first line output two integers $$$w$$$ and $$$m$$$ — the minimum number of coins that need to be spent to make the graph a tree again after removing vertex $$$v$$$, and the number of added edges.
Then output $$$m$$$ lines, each containing two integers $$$a$$$ and $$$b$$$ ($$$1 \le a, b \le n, a \ne v, b \ne v$$$, $$$a \ne b$$$) — the ends of the added edge.
If there are multiple ways to add edges, you can output any solution with the minimum cost.
351 31 44 53 254 24 33 55 152 11 51 41 3
1 1 3 4 0 0 1 1 1 2 2 1 3 5 0 0 0 0 0 0 1 1 1 2 1 1 1 2 1 1 1 2 3 3 2 3 4 5 3 4 0 0 0 0 0 0 0 0
In the first test case:
Consider the removal of vertex $$$4$$$:
The optimal solution would be to add an edge from vertex $$$5$$$ to vertex $$$3$$$. Then we will spend $$$|5 - 3| = 2$$$ coins.
In the third test case:
Consider the removal of vertex $$$1$$$:
The optimal solution would be:
Then we will spend a total of $$$1 + 1 + 1 = 3$$$ coins.
Consider the removal of vertex $$$2$$$:
No edges need to be added, as the graph will remain a tree after removing the vertex.