The Chtholly Tree is a data structure used for certain sequence maintenance problems. Each node in a Chtholly Tree represents a consecutive block of equal elements in the sequence. These nodes are maintained in any type of sorted data structure such as set in C++ or a balanced binary search tree.
For example, the Chtholly Tree of the sequence $$$[1,2,3,3,2,2,1,1]$$$ will contain the following nodes: $$$[1,1]$$$, $$$[2,2]$$$, $$$[3,4]$$$, $$$[5,6]$$$, $$$[7,8]$$$.
Problems solvable using Chtholly Tree usually involve the following type of operation: choose a segment $$$[l,r]$$$ of the sequence, and for all $$$l \le i \le r$$$, set $$$a_i=x$$$ for some value $$$x$$$. It can be proven that if $$$l$$$, $$$r$$$, and $$$x$$$ are chosen randomly, the expected number of nodes in a Chtholly Tree is $$$O(\log n)$$$, where $$$n$$$ is the length of the sequence.
However, Baozii would like to know the exact expected number of nodes rather than its asymptotic representation. To do so, he did the following experiment:
After a sufficiently large number of operations, Baozii observed that the number of nodes seems to converge to around $$$2 \ln n$$$. Now for a given $$$n$$$, can you help Baozii find the exact expected number of nodes?
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 only line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 10^{18}$$$) — the length of the initial sequence.
For each test case, output a real number: the expected number of nodes in the Chtholly Tree. The absolute difference between your answer and the jury's answer should not be more than $$$10^{-3}$$$.
512345
1.000000 1.666667 2.200000 2.642857 3.020202
Baozii prefers using Go for programming problems that demand efficiency. In this task, Baozii challenges you to implement an interpreter for a simplified version of Go, called GoGo.
You are now given a GoGo script. Your interpreter should output exactly what the program will output if it is run, and report any runtime errors.
Some possible clarifications:
A GoGo script consisting of no more than $$$10^4$$$ characters.
If a runtime error occurs, output "runtime error"(without quotes) on a single line only. Otherwise, output exactly what is printed to the standard output if the GoGo script is run. It is guaranteed that the total number of characters printed will be no more than $$$10^4$$$.
func main() {
var x int
var y int
y = -10
Println(y)
var s string
s = "abc"
for range 5 {
Println(1)
Println(x)
x += x
x += 1
s += "p"
s += ""
}
Println("")
Println(s)
Println(x)
}
-10 1 0 1 1 1 3 1 7 1 15 abcppppp 31
func main() {
Println(x)
}
runtime error
func main() {
var x int
var x int
}
runtime error
Baozii presents to you a strip of $$$n$$$ cells, all initially white. You are given a target pattern consisting of $$$n$$$ cells, where each cell is either white or black. Your task is to determine whether it is possible to transform the strip into the given target pattern by performing a finite sequence of the following operation:
Determine if the transformation is achievable. If so, output a valid sequence of operations.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 100$$$) — length of Baozii's strip.
The second line contains a binary string of length $$$n$$$, representing the pattern. If the $$$i$$$-th character is 1, the $$$i$$$-th cell should be colored black; otherwise, the $$$i$$$-th cell should be colored white.
For each test case, output "YES" (without quotes) if there exists a finite sequence of operations which colors the strip to the given pattern, and "NO" (without quotes) otherwise.
If the answer is "YES", output the sequence in the following format:
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
45010102101001111001003000
YES 2 1 3 NO YES 9 7 7 7 7 4 3 2 1 1 YES 0
You are given an integer $$$n$$$. Construct a $$$n \times n$$$ matrix $$$M$$$ that contains each integer from $$$1$$$ to $$$n^2$$$ exactly once. The $$$\gcd$$$ of all numbers in each row should be equal to $$$1$$$, and the $$$\gcd$$$ of all numbers in each column should be equal to $$$1$$$.
Formally, $$$M$$$ has to satisfy the following conditions:
Each test contains multiple test cases. The first line of each test contains an integer $$$t$$$ ($$$1 \le t \le 200$$$) — the number of test cases.
The only line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 500$$$) — the size of $$$M$$$ to be constructed.
It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, if no possible $$$M$$$ exists, output $$$-1$$$.
Otherwise, output $$$n$$$ lines of $$$n$$$ integers each, where the $$$j$$$-th integer on the $$$i$$$-th line represents $$$M_{i,j}$$$.
3123
1 1 2 4 3 1 2 3 4 5 6 9 8 7
You are given an array $$$a$$$ of $$$n$$$ integers. Find:
$$$$$$\max_{1 \le i,j \le n} \gcd(a_i,a_j) + (a_i \oplus a_j)$$$$$$
where $$$\oplus$$$ denotes the bitwise XOR operation.
The first line of each test contains an integer $$$n$$$ ($$$1 \le n \le 5 \cdot 10 ^5$$$) — the length of $$$a$$$.
The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 5 \cdot 10^5$$$) — the elements of $$$a$$$.
Output an integer representing the maximum value of the expression above.
31 2 3
4
59 9 3 8 2
13
DreamChaser is excited about a new video game, Big Baozi, Small Baozi. In this game, he plays as a Baozi trying to consume other Baozi to grow bigger.
The game's map is represented by an undirected connected graph with $$$n$$$ vertices and $$$m$$$ edges. Each vertex contains a Baozi with a weight $$$w_u$$$ ($$$1 \leq u \leq n$$$).
The game consists of $$$q$$$ rounds. At the start of each round:
After the round begins, DreamChaser can perform the following operation repeatedly:
This process continues until there are no consumable neighbors left. For each round of the game, please help DreamChaser determine his final weight at the end of the round.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The first line of each test case contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \le n \le 2 \cdot 10^5$$$, $$$n - 1 \le m \le \min(2 \cdot 10^5, \frac{n(n-1)}{2})$$$, $$$1 \le q \le 2 \cdot 10^5$$$) — the number of vertices, edges, and rounds respectively.
The second line contains $$$n$$$ integers $$$w_1,,w_2,\ldots,w_n$$$ ($$$1 \le w_i \le 10^9$$$) — the weight of the Baozi on each vertex.
Each of the next $$$m$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$, $$$u \ne v$$$), representing an edge between vertices $$$u$$$ and $$$v$$$. It is guaranteed that there are no self loops or multiple edges in the graph.
Each of the next $$$q$$$ lines contains two integers $$$v$$$ and $$$x$$$ ($$$1 \le v \le n$$$, $$$0 \le x \le 10^9$$$) — the starting vertex and additional weight boost for each round.
It is guaranteed that the sum of $$$n$$$, $$$m$$$ and $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output $$$q$$$ integers, where the $$$i$$$-th integer represents the final weight of DreamChaser for the $$$i$$$-th round.
15 4 41 2 2 5 111 22 35 43 41 04 04 12 10
1 10 22 31
This is an interactive problem.
There is a hidden array $$$a$$$ of length $$$n$$$, consisting of distinct positive integers no larger than $$$10^9$$$. Your task is to find the second maximum element of $$$a$$$. To achieve this, you may query a non-empty range $$$[l,r]$$$ $$$(1 \le l \le r \le n)$$$, and the judge will return you the maximum element among $$$a_l, a_{l+1}, \ldots, a_r$$$.
You may not use more than $$$24$$$ queries.
Each test contains multiple test cases. The first line contains one integer $$$t$$$ ($$$1\le t\le500$$$) — the number of test cases. The description of each test case follows.
The first line of each test case contains one integer $$$n$$$ ($$$2\le n\le1000$$$) — the length of the hidden array $$$a$$$.
To ask a query, output a line in the following format:
where $$$[l,r]$$$ is the query range.
After each query, you should read one line containing one integer, denoting the maximum element among $$$a_l,a_{l+1},\ldots,a_r$$$.
Print the answer in a line in the following format:
where $$$x$$$ is the value of the second maximum element, and $$$i$$$ ($$$1 \le i \le n$$$) is the index of $$$x$$$ in $$$a$$$.
Note that printing the answer is not counted towards the total number of queries.
You can ask at most $$$24$$$ queries in each test case.
The interactor is NOT adaptive, meaning that the answer is known before the participant asks the queries and doesn't depend on the queries asked by the participant.
If your program makes more than $$$24$$$ queries for one test case, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.
After printing a query do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
1 10 9 10 6 1
? 1 3 ? 2 10 ? 9 10 ? 3 3 ! 9 1
In the sample test case, the hidden array $$$a$$$ is $$$[9,8,1,3,2,10,5,7,6,4]$$$.
You are given an array $$$a$$$ of length $$$n$$$ containing positive integers. You are allowed to rearrange the elements of $$$a$$$ in any order. Find the minimum possible value of the following expression: $$$$$$\sum_{i=1}^{n-1} \frac{a_{i}^2 + a_{i+1}^2}{a_i a_{i+1}}$$$$$$
Each test case contains multiple test cases. The first line of each test contains an integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases.
The first line of each test case contains an integer $$$n$$$ ($$$2 \le n \le 10^5$$$) — the length of $$$a$$$.
The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \le a_i \le 10^6$$$) — the elements of $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output a real number: the minimum possible value of the given expression.
Your answer is considered correct if its absolute or relative error does not exceed $$$10^{-6}$$$.
Formally, let your answer be $$$a$$$, and the jury's answer be $$$b$$$. Your answer is accepted if and only if $$$\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-6}$$$.
221 2311451 19198 114514
2.5000000000 8.4055429827
For a set $$$S$$$ and an array $$$a$$$ of length $$$n$$$, where $$$n$$$ is even, define $$$f(a, S)$$$ to be the number of permutations $$$p$$$ of $$$\{1,2,\ldots,n \}$$$ such that $$$a_{p_i}+a_{p_{n-i+1}} \in S$$$ for all $$$1 \le i \le n$$$.
You are given a set $$$T$$$ and $$$m$$$ sets $$$A_1,A_2,\ldots,A_m$$$, where $$$m$$$ is even. Now construct another array $$$b$$$ as follows:
Find the expected value of $$$f(b,T)$$$, modulo $$$998244353$$$.
The first line of each test contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 10^4$$$, $$$2 \le m \le 30$$$, $$$m$$$ is even) — the size of $$$T$$$ and the number of arrays.
The second line contains $$$n$$$ distinct integers $$$T_1,T_2,\ldots,T_n$$$ ($$$0 \le T_i \le 2 \cdot 10^4$$$) — the elements of $$$T$$$.
The $$$2i-1$$$-th line of the next $$$2m$$$ lines contains an integer $$$k_i$$$ ($$$1 \le k_i \le 10^4$$$) — the size of $$$A_i$$$.
The $$$2i$$$-th line contains $$$k_i$$$ integers $$$A_{i,1},A_{i,2},\ldots,A_{i,k_i}$$$ ($$$0 \le A_{i,j} \le 10^4$$$) — the elements of $$$A_i$$$.
Output an integer indicating the expected value of $$$f(b,T)$$$ modulo $$$998244353$$$. Formally, let $$$M=998244353$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \ne 0$$$. Output $$$p \cdot q ^{-1} \bmod M$$$, where $$$q^{-1}$$$ denotes the multiplicative inverse of $$$q$$$ modulo $$$M$$$.
1 2020 120 1
499122177
3 42 3 430 2 251 2 1 1 11120 3
798595492
For any two integer arrays $$$a$$$ and $$$b$$$ of lengths $$$n$$$ and $$$m$$$, each containing distinct elements and there are no common elements between them, define $$$f(a,b)$$$ as follows:
$$$$$$ f(a,b) = \begin{cases} \emptyset & a = \emptyset \land b = \emptyset \\ a & b = \emptyset \\ b & a = \emptyset \\ [a_1]+f([a_2,a_3,\ldots,a_n],b) & a_1 \lt b_1 \\ [b_1]+f(a,[b_2,b_3,\ldots,b_m]) & a_1 \gt b_1 \end{cases} $$$$$$
where $$$+$$$ is defined as the concatenation of two arrays.
You are now given a permutation $$$p$$$ of length $$$2n$$$. You need to determine whether it is possible to find two arrays $$$a$$$ and $$$b$$$, both of length $$$n$$$ and together containing all integers from $$$1$$$ to $$$2n$$$ exactly once, such that $$$f(a,b)=p$$$.
A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 10^6$$$) — half the length of $$$p$$$.
The second line contains $$$2n$$$ integers $$$p_1,p_2,\ldots,p_{2n}$$$ ($$$1 \le p_i \le 2n$$$) — the permutation $$$p$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, if there exist $$$a$$$, $$$b$$$ satisfying the constraints, output YES; otherwise, output NO. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
322 3 1 423 1 2 443 2 6 1 5 7 8 4
YES NO YES
Let $$$a$$$ be a sequence of integers and $$$k$$$ be a given integer. Define $$$f(a)$$$ to be the largest element that appears at least $$$k$$$ times in the sequence. Specially, if all elements of $$$a$$$ occur less than $$$k$$$ times, then $$$f(a)=0$$$.
You are given an undirected tree with $$$n$$$ vertices rooted at vertex $$$1$$$. Recall that a tree is a connected acyclic graph. Each vertex $$$i$$$ has a value $$$v_i$$$ assigned to it. We say that $$$j$$$ is in the subtree of $$$i$$$ if and only if:
The problem is as follows: for each vertex $$$i$$$, calculate $$$f(a_i)$$$, where $$$a_i$$$ is the sequence of $$$v_j$$$ for all $$$j$$$ in the subtree of $$$i$$$.
Each test contains multiple test cases.
The first line of each test contains one integer $$$t$$$ ($$$1\le{t}\le1000$$$) – the number of test cases.
The first line of each test case contains two integers $$$n,k$$$ ($$$1\le{n}\le2\cdot{10^5}$$$, $$$1 \le k \le 2 \cdot 10^5$$$) – the number of vertices in the given tree.
The second line contains $$$n$$$ integers $$$v_1,v_2,\ldots{v_n}$$$ ($$$1\le{v_i}\le{10^9}$$$) – the value of each vertex.
Then follows $$$n-1$$$ lines:
Each line contains two integers $$$i,j$$$ ($$$1\le{i,j}\le{n}$$$) – indicating an edge between the vertices $$$i$$$ and $$$j$$$.
It is guaranteed that the given input forms a valid tree rooted at $$$1$$$.
It is further guaranteed that the sum of all $$$n$$$ across the test cases does not exceed $$$2\cdot{10^5}$$$.
For each test case, output $$$n$$$ integers in a single line: the $$$i$$$-th integer represents the value $$$f(a_i)$$$.
35 23 3 3 3 31 23 52 35 45 24 4 3 3 51 22 33 44 53 3114514 114514 1145141 33 2
3 3 3 0 3 4 3 3 0 0 114514 0 0
DreamChaser is an employee at Baozii Corporations. As a considerate boss, Baozii provides a daily transportation allowance of $$$k$$$ dollars for each employee before they go home after work. To complete an ongoing project, DreamChaser must work overtime for $$$n$$$ consecutive days. Fortunately, the company offers $$$m$$$ shuttle services each day to send employees home.
The cost and arrival time of each shuttle are represented by two matrices, $$$c$$$ and $$$t$$$. Specifically, $$$c_{i,j}$$$ represents the cost of the $$$j$$$-th shuttle on the $$$i$$$-th day, and $$$t_{i,j}$$$ represents the time at which the $$$j$$$-th shuttle on the $$$i$$$-th day arrives at DreamChaser's home.
DreamChaser plans to use only the transportation allowance provided by the company to take the shuttles. Note that any unused transportation allowance from previous days can be carried over to subsequent days, but at no point can DreamChaser's funds become negative. Additionally, DreamChaser must return home every day.
For a time $$$s$$$, if DreamChaser can return home no later than $$$s$$$, then $$$s$$$ is considered good. Find the smallest good $$$s$$$, or report it does not exist.
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 three integers $$$n$$$, $$$m$$$, $$$k$$$ ($$$1 \le n \le 1000$$$, $$$1 \le m \le 1000$$$, $$$1 \le k \le 10^9$$$) — the number of days of working overtime, and the number of shuttle services provided.
The following $$$n$$$ lines describe the matrix $$$c$$$, where the $$$i$$$-th line contains $$$m$$$ integers $$$c_{i,1},c_{i,2},\ldots,c_{i,m}$$$ ($$$1 \le c_{i,j} \le 10^9$$$).
The following $$$n$$$ lines describe the matrix $$$t$$$, where the $$$i$$$-th line contains $$$m$$$ integers $$$t_{i,1},t_{i,2},\ldots,t_{i,m}$$$ ($$$1 \le t_{i,j} \le 10^9$$$).
It is guaranteed that the sum of $$$n$$$ and $$$m$$$ over all test cases does not exceed $$$1000$$$.
For each test case, if the transportation allowance is enough, output the smallest good $$$s$$$. Otherwise, output $$$-1$$$.
13 2 33 42 33 41 13 23 4
3
You are given an undirected tree with $$$n$$$ vertices rooted at $$$1$$$. Recall that a tree is a connected acyclic graph. You are also given a permutation $$$p$$$ of length $$$n$$$. A preorder traversal of a rooted tree with $$$n$$$ vertices is a permutation of length $$$n$$$ that can be generated by the following pseudo-code:
seq = list()
function preorder(u):
seq.append(u)
shuffle(children(u))
for v in children(u):
preorder(v)
preorder(1)
where seq is the generated preorder traversal. Note that there might be more than $$$1$$$ possible preorder traversals of a tree.
Among all possible preorder traversals, you need to find one such that the following sum is maximised: $$$$$$\sum_{i=1}^n \left[p_i=s_i \right]$$$$$$
where $$$s$$$ is the preorder traversal you have chosen. Informally, you need to find a preorder traversal $$$s$$$ such that it has the most number of matching positions with $$$p$$$.
If there are many possible solutions, you may output any of them.
The first line of each test contains a single integer $$$n$$$ ($$$1 \le n \le 18$$$) — the number of vertices in the given tree.
The second line contains $$$n$$$ distinct integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i \le n$$$) — the given permutation.
Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$, $$$u \ne v$$$), indicating an edge between vertices $$$u$$$ and $$$v$$$.
It is guaranteed that the input forms a tree.
Output $$$n$$$ integers, representing the preorder traversal you found.
53 5 2 4 11 25 43 21 4
1 2 3 4 5