You have an array $$$a$$$ of length $$$n$$$. For every positive integer $$$x$$$ you are going to perform the following operation during the $$$x$$$-th second:
You have to make $$$a$$$ nondecreasing as fast as possible. Find the smallest number $$$T$$$ such that you can make the array nondecreasing after at most $$$T$$$ seconds.
Array $$$a$$$ is nondecreasing if and only if $$$a_{1} \le a_{2} \le \ldots \le a_{n}$$$.
You have to answer $$$t$$$ independent test cases.
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 single integer $$$n$$$ ($$$1 \le n \le 10^{5}$$$) — the length of array $$$a$$$. It is guaranteed that the sum of values of $$$n$$$ over all test cases in the input does not exceed $$$10^{5}$$$.
The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, \ldots, a_{n}$$$ ($$$-10^{9} \le a_{i} \le 10^{9}$$$).
For each test case, print the minimum number of seconds in which you can make $$$a$$$ nondecreasing.
341 7 6 551 2 3 4 520 -4
2 0 3
In the first test case, if you select indices $$$3, 4$$$ at the $$$1$$$-st second and $$$4$$$ at the $$$2$$$-nd second, then $$$a$$$ will become $$$[1, 7, 7, 8]$$$. There are some other possible ways to make $$$a$$$ nondecreasing in $$$2$$$ seconds, but you can't do it faster.
In the second test case, $$$a$$$ is already nondecreasing, so answer is $$$0$$$.
In the third test case, if you do nothing at first $$$2$$$ seconds and select index $$$2$$$ at the $$$3$$$-rd second, $$$a$$$ will become $$$[0, 0]$$$.
You have unweighted tree of $$$n$$$ vertices. You have to assign a positive weight to each edge so that the following condition would hold:
Note that you can put very large positive integers (like $$$10^{(10^{10})}$$$).
It's guaranteed that such assignment always exists under given constraints. Now let's define $$$f$$$ as the number of distinct weights in assignment.
In this example, assignment is valid, because bitwise XOR of all edge weights between every pair of leaves is $$$0$$$. $$$f$$$ value is $$$2$$$ here, because there are $$$2$$$ distinct edge weights($$$4$$$ and $$$5$$$).
In this example, assignment is invalid, because bitwise XOR of all edge weights between vertex $$$1$$$ and vertex $$$6$$$ ($$$3, 4, 5, 4$$$) is not $$$0$$$.
What are the minimum and the maximum possible values of $$$f$$$ for the given tree? Find and print both.
The first line contains integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$) — the number of vertices in given tree.
The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 \le a_{i} \lt b_{i} \le n$$$) — it means there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. It is guaranteed that given graph forms tree of $$$n$$$ vertices.
Print two integers — the minimum and maximum possible value of $$$f$$$ can be made from valid assignment of given tree. Note that it's always possible to make an assignment under given constraints.
6 1 3 2 3 3 4 4 5 5 6
1 4
6 1 3 2 3 3 4 4 5 4 6
3 3
7 1 2 2 7 3 4 4 7 5 6 6 7
1 6
In the first example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.
In the second example, possible assignments for each minimum and maximum are described in picture below. The $$$f$$$ value of valid assignment of this tree is always $$$3$$$.
In the third example, possible assignments for each minimum and maximum are described in picture below. Of course, there are multiple possible assignments for each minimum and maximum.
Consider the infinite sequence $$$s$$$ of positive integers, created by repeating the following steps:
You have integer $$$n$$$. Find the $$$n$$$-th element of $$$s$$$.
You have to answer $$$t$$$ independent test cases.
A sequence $$$a$$$ is lexicographically smaller than a sequence $$$b$$$ if in the first position where $$$a$$$ and $$$b$$$ differ, the sequence $$$a$$$ has a smaller element than the corresponding element in $$$b$$$.
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases.
Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$1\le n \le 10^{16}$$$) — the position of the element you want to know.
In each of the $$$t$$$ lines, output the answer to the corresponding test case.
9 1 2 3 4 5 6 7 8 9
1 2 3 4 8 12 5 10 15
The first elements of $$$s$$$ are $$$1, 2, 3, 4, 8, 12, 5, 10, 15, \dots $$$
You have a tree of $$$n$$$ vertices. You are going to convert this tree into $$$n$$$ rubber bands on infinitely large plane. Conversion rule follows:
Now let's define following things:
This is an example of conversion. Note that rubber bands $$$5$$$ and $$$6$$$ are nested. It can be proved that is it possible to make a conversion and sequence of nested rubber bands under given constraints.
What is the maximum length of sequence of nested rubber bands can be obtained from given tree? Find and print it.
The first line contains integer $$$n$$$ ($$$3 \le n \le 10^{5}$$$) — the number of vertices in tree.
The $$$i$$$-th of the next $$$n-1$$$ lines contains two integers $$$a_{i}$$$ and $$$b_{i}$$$ ($$$1 \le a_{i} \lt b_{i} \le n$$$) — it means there is an edge between $$$a_{i}$$$ and $$$b_{i}$$$. It is guaranteed that given graph forms tree of $$$n$$$ vertices.
Print the answer.
6 1 3 2 3 3 4 4 5 4 6
4
4 1 2 2 3 3 4
2
In the first sample, you can obtain a nested sequence of $$$4$$$ rubber bands($$$1$$$, $$$2$$$, $$$5$$$, and $$$6$$$) by the conversion shown below. Of course, there are other conversions exist to make a nested sequence of $$$4$$$ rubber bands. However, you cannot make sequence of $$$5$$$ or more nested rubber bands with given tree.
You can see one of the possible conversions for the second sample below.
Due to the success of TWICE, JYP Entertainment has earned countless money and emerged as the biggest entertainment firm by market capitalization. Therefore, the boss, JYP, has decided to create a new nation and has appointed you to provide a design diagram.
The new nation consists of $$$n$$$ cities and some roads between them. JYP has given some restrictions:
JYP has given criteria for your diagram. For two cities $$$A$$$,$$$B$$$, let $$$dis(A,B)$$$ be the smallest number of roads you have to go through to get from $$$A$$$ to $$$B$$$. If it is not possible to walk from $$$A$$$ to $$$B$$$, $$$dis(A,B) = 614n$$$. Then, the efficiency value is defined to be the sum of $$$dis(A,B)$$$ for all ordered pairs of distinct cities $$$(A,B)$$$.
Note that $$$dis(A,B)$$$ doesn't have to be equal to $$$dis(B,A)$$$.
You have drawn a design diagram that satisfies JYP's restrictions. Find the sum of $$$dis(A,B)$$$ over all ordered pairs of cities $$$(A,B)$$$ with $$$A\neq B$$$.
Note that the input is given in compressed form. But even though it is compressed, you'd better use fast input.
The first line contains a single integer $$$n$$$ ($$$4 \le n \le 8000$$$, $$$n \equiv 0 \pmod{4}$$$) — the number of cities.
A binary matrix is encrypted in the following format. Each of $$$n$$$ next lines contains $$$\frac{n}{4}$$$ one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from $$$0$$$ to $$$9$$$ or as uppercase Latin letters from $$$A$$$ to $$$F$$$). Binary representation of each of these numbers denotes next $$$4$$$ elements of the matrix in the corresponding row. For example, if the number $$$B$$$ is given, then the corresponding elements are $$$1011$$$, and if the number is $$$5$$$, then the corresponding elements are $$$0101$$$.
After you obtain the decrypted binary matrix, the $$$j$$$-th character of the $$$i$$$-th row is $$$1$$$ if the one-directional road between cities $$$i$$$ and $$$j$$$ is directed from $$$i$$$ to $$$j$$$, and $$$0$$$ otherwise. It is guaranteed that the graph satisfies the restrictions mentioned above.
Output one integer, representing the sum of $$$dis(A,B)$$$ over all ordered pairs of cities $$$(A,B)$$$ with $$$A\neq B$$$.
4 7 2 1 4
7380
8 7F 3F 1F 0C 06 03 11 18
88464
The first example corresponds to the matrix:
$$$\begin{matrix} 0111 \\ 0010 \\ 0001 \\ 0100 \\ \end{matrix}$$$
Which corresponds to this graph:

$$$dis(1,2)=dis(1,3)=dis(1,4)=dis(2,3)=dis(3,4)=dis(4,2)=1$$$
$$$dis(2,4)=dis(4,3)=dis(3,2)=2$$$
$$$dis(2,1)=dis(3,1)=dis(4,1)=2456$$$
Therefore the answer for the diagram is $$$7380$$$.