You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.
In one operation, you choose two indices $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$). Then the segment $$$a_l, a_{l+1}, \ldots, a_r$$$ is reversed, and every value in this segment is doubled.
In other words, after applying the operation, the chosen segment becomes $$$2\cdot a_r, 2\cdot a_{r-1}, \ldots, 2\cdot a_l$$$ and the full array is $$$$$$[a_1, a_2, \ldots, a_{l-1}, 2\cdot a_r, 2\cdot a_{r-1}, \ldots, 2\cdot a_l, a_{r+1}, \ldots, a_n].$$$$$$
The gap value of an array is the maximum value of $$$|a_i-a_{i+1}|$$$ over all $$$1 \le i \lt n$$$.
Find the minimum possible gap value of $$$a$$$ after applying at most one operation.
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 an integer $$$n$$$ ($$$2\le n\le 3\cdot 10^5$$$) — the length of $$$a$$$.
The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^6$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3\cdot 10^5$$$.
For each test case, print one integer — the minimum possible gap value after applying at most one operation.
699 9 8 2 4 4 3 5 325 651 7 4 1 10210 1655 2 6 5 368 3 9 9 9 8
416423
In the first test case, it is optimal to choose $$$l = 4$$$ and $$$r = 5$$$. The resulting array is $$$$$$[9, 9, 8, \mathbf{8}, \mathbf{4}, 4, 3, 5, 3]$$$$$$ and the resulting gap value is $$$4$$$, achieved at $$$i = 4$$$.
In the second test case, the gap value before applying any operations is $$$1$$$. We can show that applying any operation will increase this value, so the answer is $$$1$$$.
Given a nonempty subset $$$S$$$ of the integers, we call an element $$$x\in S$$$ stable if exactly two of the three values $$$\{x + 1, x + 2, x + 3\}$$$ are in $$$S$$$. Otherwise, we call $$$x\in S$$$ unstable.
Define a subset $$$S$$$ to be balanced if at most $$$2$$$ of its elements are unstable.
Given a set of distinct positive integers $$$A$$$, count the number of non-empty subsets of $$$A$$$ that are balanced. Since this number may be large, output it modulo $$$998\,244\,353$$$.
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$$$ ($$$1\le n\le 2\cdot 10^5$$$) — the number of integers in $$$A$$$.
The second line contains $$$n$$$ integers $$$a_1 \lt a_2 \lt \ldots \lt a_n$$$ ($$$1 \le a_i \le 3\cdot n$$$) — the elements of $$$A$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases is at most $$$2\cdot 10^5$$$.
For each test case, output a single integer: the number of non-empty subsets of $$$A$$$ that are balanced. Since this number may be large, output it modulo $$$998\,244\,353$$$.
441 2 3 4101 2 4 6 7 8 10 11 15 1661 2 3 15 17 1813
1466231
In the first example, the non-empty balanced subsets of $$$\{1, 2, 3, 4\}$$$ are (split on two lines for readability): $$$$$$\{1\}, \{2\}, \{3\}, \{4\}, \{1, 2\}, \{1, 3\}, \{1, 4\}, $$$$$$ $$$$$$\{2, 3\}, \{2, 4\}, \{3, 4\}, \{1, 2, 3\}, \{1, 2, 4\}, \{1, 3, 4\}, \{2, 3, 4\}.$$$$$$ Note that $$$S = \{1, 2, 3, 4\}$$$ is not balanced since all of $$$1, 3$$$, and $$$4$$$ are unstable. For example, $$$1$$$ is unstable since all of $$$2, 3$$$, and $$$4$$$ are also in $$$S$$$.
In the third example, one of the unbalanced subsets is $$$\{1, 2, 3, 18\}$$$ and one of the balanced subsets is $$$\{15, 17, 18 \}$$$.
You are given an array $$$a$$$ of length $$$n$$$ and a bound $$$\ell$$$. A pair of indices $$$1\le i \lt j \le n$$$ is called dangerous if $$$a_i \cdot a_j \ge \ell$$$.
In one operation, you can choose an index $$$i$$$ and decrease $$$a_i$$$ by $$$1$$$ (even if this makes $$$a_i$$$ zero or negative). Determine the minimum number of operations you have to apply to $$$a$$$ to make no pair of indices dangerous.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). The description of the test cases follows.
The first line of each test case contains the integers $$$n$$$ and $$$\ell$$$ ($$$2\le n\le 5\cdot 10^5$$$, $$$1\le \ell\le 10^9$$$) — the number of integers in $$$a$$$ and the bound, respectively.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$) — the elements of $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases is at most $$$5\cdot 10^5$$$.
For each testcase, output a single integer: the minimum number of operations you have to perform on the array to make every pair of indices not dangerous.
55 1110 2 6 3 14 51 2 3 32 133 44 91 1 1 96 10000000001000000000 1000000000 1 1000000000 1000000000 1000000000
82013999999997
In the first example, we can decrease each of the middle $$$3$$$ numbers down to $$$1$$$, which takes $$$1 + 5 + 2 = 8$$$ operations.
In the second example, we can decrease each of the last $$$2$$$ numbers down to $$$2$$$, which takes $$$2$$$ operations.
Given a connected, undirected, simple graph $$$G$$$ with $$$n$$$ vertices and $$$m$$$ edges, define its meta-graph $$$f(G)$$$ as follows:
Given two vertices $$$w_x$$$ and $$$y_z$$$, determine the number of edges on the shortest path between them in $$$f(G)$$$.
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 the two integers $$$n$$$ and $$$m$$$ ($$$2\le n\le 2\cdot 10^5$$$, $$$n - 1 \le m\le \min\left(2\cdot 10^5, \frac{n(n-1)}{2}\right)$$$) — the number of vertices and the number of edges in $$$G$$$.
The second line contains four integers $$$w$$$, $$$x$$$, $$$y$$$, and $$$z$$$ ($$$1\le w, x, y, z\le n$$$) — the start vertex $$$w_x$$$ and end vertex $$$y_z$$$, respectively.
The next $$$m$$$ lines each contain two integers $$$a$$$ and $$$b$$$ ($$$1\le a \lt b\le n$$$) — the edges of $$$G$$$. It is guaranteed that no edge appears more than once, and that $$$G$$$ is connected.
It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all testcases are bounded by $$$2\cdot 10^5$$$.
For each test case, output a single integer: the number of edges on the shortest path from $$$w_x$$$ to $$$y_z$$$ in $$$f(G)$$$. We can show that at least one path between them always exists under the conditions of the problem.
33 21 1 3 31 22 34 41 1 4 11 22 33 41 42 11 1 1 11 2
620
In the first example, the shortest (and only) path is $$$1_1, 1_2, 2_1, 2_2, 2_3, 3_2, 3_3$$$.
The meta-graph for the first example. In the second example, the shortest path is $$$1_1, 1_4, 4_1$$$.
Note that the memory limit is unusual.
Help, I've been trapped in a magical seal!
There is an array $$$a$$$ with $$$n$$$ integers. In one operation, you can choose $$$1\le i\le n - 1$$$ and either:
The seal becomes broken as soon as there is an index $$$i$$$ such that $$$a_i = 0$$$. Determine the minimum number of operations required to break the seal, or output $$$-1$$$ if you can never escape!
$$$^{\text{∗}}$$$$$$\oplus$$$ denotes the bitwise XOR operation.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1\le n\le 100$$$) — the number of elements in the array.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \lt 2^{25}$$$) — the elements of $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$100$$$.
For each testcase, output a single integer: the minimum number of operations required to break the seal, or $$$-1$$$ if it is impossible.
531 2 144 2 3 151 2 4 8 1630 1 211234
32-10-1
In the first example, we may do the following sequence of operations:
In the third example, we can prove that no sequence of operations breaks the seal.
As you may know, the game of "Nim" is played with several piles of stones and two players alternating turns. During a turn, a player picks any non-empty pile and removes any positive number of stones from it; the one who is not able to make a move loses the game.
Alice wants to play Nim with the Mad Hatter on $$$n$$$ piles $$$s_1, s_2, \ldots, s_n$$$ with distinct positive number of stones, but they get bored because they've played the game too many times. Instead, Alice proposes the following variant:
Note that the input is given encoded; see the Input section for a description of it. Because of the encoding format, you will need to solve the given problem for each prefix $$$s_1, s_2, \ldots, s_i$$$. However, you will only output this coloring for the entire set of piles $$$s_1, s_2, \ldots, s_n$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 7000$$$).
The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0\le a_i\le 10^9$$$).
The $$$s_i$$$ are encoded: in particular, let $$$k_i$$$ be the minimum number of colors needed on the prefix $$$s_1, s_2, \ldots, s_{i-1}$$$ with $$$k_1 = 0$$$. Then, $$$s_i := a_i \oplus k_i$$$, where $$$\oplus$$$ denotes the bitwise XOR operation. It is guaranteed that $$$1\le s_i \le 2n$$$ and that all $$$s_i$$$ are distinct.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$7000$$$.
For each test case, on the first line, output $$$k$$$ — the minimum number of colors required for the full collection of piles.
On the $$$i$$$-th of the following $$$k$$$ lines, first print $$$\ell_i$$$ ($$$1 \leq \ell_i \leq n$$$) — the number of piles in the $$$i$$$-th color. Then print $$$\ell_i$$$ integers representing the sizes of the piles in the $$$i$$$-th color.
If there are multiple solutions, print any of them. We can show that an answer always exists for all prefixes.
331 3 261 3 2 6 7 10918 12 17 3 6 4 13 11 9
2 2 1 2 1 3 2 3 1 2 4 3 3 5 8 3 4 16 4 2 9 4 6 15 18 13 1 11
In the first example, we can recover the input as follows:
Thus, the piles have values $$$[1, 2, 3]$$$. We can show that the minimum number of colors required here is $$$2$$$, and a possible coloring is making the first and second pile color $$$1$$$, and the third pile color $$$2$$$.