Juan and his friends are going to split themselves into $$$n$$$ teams and play a modified double-elimination football tournament, consisting of a winners' group and a losers' group. Initially, all teams belong to the winners' group.
In each round of the tournament, the following happens as long as one of the groups has at least two teams:
Note that in the above process, when a team loses a match in the winners' group, it drops down to the losers' group in the next round. That means, it is not considered for the pairing process in the current round's losers' group.
After multiple iterations of the aforementioned process, both groups end up with a single team each. When this happens, both teams face off against each other in a match and a victor emerges.
Determine how many matches were played in total. It can be proved that no matter how the teams are paired up and which ones win and lose, the answer remains the same.
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 one positive integer $$$n$$$ ($$$2 \le n \le 500$$$) — the number of teams.
For each test case, print the total number of matches played during the tournament.
223
24
In the first test case, it can be proved that exactly $$$2$$$ matches are played before a victor is declared.
In the second test case, the image below shows one possible tournament. Notice that $$$4$$$ matches were played in total.
Abraham is a brave explorer who goes where no other programmer has gone before. For his next expedition, he plans to investigate a peculiar maze. He knows that the maze is an $$$n \times n$$$ grid with an arrow in each cell that points in one of four directions: up, down, left and right. Abraham also knows that if he stands on an arrow, he will be forced to follow the arrows starting from that cell. Each arrow moves Abraham exactly $$$1$$$ cell in the direction that it is pointing. If he reaches an arrow that points towards the outside of the maze, Abraham will escape the maze.
Abraham doesn't know how the arrows are arranged, so he wants to plan for multiple scenarios. He tasks you with finding an arrangement of arrows in the grid such that there are exactly $$$k$$$ starting cells from which he can escape the maze.
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 only line of each test case contains two integers $$$n$$$, $$$k$$$ ($$$2 \le n \le 100$$$, $$$0 \le k \le n^2$$$) — the size of the grid and the number of cells from which Abraham should be able to escape.
It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, do one of the following:
If there are multiple solutions, print any of them.
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.
32 43 52 3
YES UU UU YES UUU RDR ULR NO
In the first test case, no matter which cell Abraham stands in initially, he will eventually exit the maze as he will move upward successively; thus, he can escape from all $$$4$$$ cells, as required.
In the second test case, Abraham eventually escapes if he stands on one of the following:
There is no other cell where he can stand and escape. We see that Abraham can escape from exactly $$$5$$$ cells in this arrangement, as required.
In the third test case, it can be proved that there's no arrangement of arrows so that Abraham can escape from exactly $$$3$$$ cells.
There are $$$n$$$ wizards in a row numbered $$$1$$$ to $$$n$$$ from left to right. Each wizard has an invisibility cape which can be worn either on his left side or on his right side. Harry walks from the position of wizard $$$1$$$ until the position of wizard $$$n$$$ ($$$1 \le n \le 10^5$$$), and registers how many wizards he sees from each wizard's position. A wizard in position $$$j$$$ is visible from position $$$i$$$ if:
In particular, note that wizard $$$i$$$ is visible from position $$$i$$$.
Harry's list is very old but, after much work, you managed to decipher it. The list is an array $$$a$$$ of $$$n$$$ elements, where the $$$i$$$-th element $$$a_i$$$ ($$$1 \le a_i \le n$$$) is the number of wizards that Harry saw from the position of wizard $$$i$$$.
Your task is to determine how many of all the possible cape arrangements that Harry could have seen are consistent with the data recorded by the list, modulo $$$676\,767\,677$$$.
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 10^5$$$) — the length of $$$a$$$.
The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$) — 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, print one integer — the number of arrangements for the wizards' capes that satisfy the condition, modulo $$$676\,767\,677$$$.
71144 4 3 231 3 222 132 2 233 2 333 2 2
2101200
The image below shows an arrangement of capes that matches Harry's list in the second test case.
Wizard 1 has the invisibility cape on his left side, while wizards $$$2$$$, $$$3$$$, and $$$4$$$ wear it on their right side.
Thus, Harry's list ends up being $$$[4, 4, 3, 2]$$$. It can be proved that this is the only possible arrangement.
In the third test case, it can be proved that Harry could not have obtained his list from any cape arrangement.
In the fifth case, note that there are two possible cape arrangements from which Harry could have gotten his list:
This is an interactive problem. Refer to the Interaction section below for better understanding.
There are $$$n$$$ ($$$2 \le n \le 40$$$) batteries numbered $$$1, 2, \ldots, n$$$. Some of them work while the others don't. Let $$$a$$$ be the number of batteries that work. It is guaranteed that $$$\mathbf{a \geq 2}$$$.
You are given $$$n$$$ but not $$$a$$$.
There is a flashlight which can hold two batteries and it only turns on when both batteries work. The batteries have been shuffled and you don't know which ones work and which ones don't. You can choose two batteries and try them in the flashlight.
You want to find a pair of batteries that work. However, you are worried about breaking the flashlight, so you want to limit the amount of trials you attempt.
Therefore, you should find a pair of working batteries using at most $$$\left \lfloor \frac{n^2}{a} \right \rfloor$$$ trials.
The interactor is adaptive. This means that whether battery $$$i$$$ ($$$1 \le i \le n$$$) works is not fixed and may change during the interaction. However, it is guaranteed that there exists a configuration of $$$a$$$ working batteries that is consistent with the information that you have received so far.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 50$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 40$$$) — the number of batteries.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$200$$$.
The interaction for each test case begins with reading the integer $$$n$$$.
To make a query, print a single line with two integers in the following format:
After that, read a single integer $$$x$$$ — indicating whether the flashlight turned on after inserting those $$$2$$$ batteries. If $$$x=1$$$, the flashlight turned on. Otherwise, if $$$x=0$$$, the flashlight did not turn on.
After finding a pair of batteries that turns the flashlight on, the interaction of the test case is considered successful, and it will terminate. After that, the interaction continues with the next test case.
After printing each query do not forget to output the end of line and flush$$$^{\text{∗}}$$$ the output. Otherwise, you will get Idleness limit exceeded verdict.
If, at any interaction step, you read $$$-1$$$ instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.
Hacks
Hacks are disabled for this problem.
$$$^{\text{∗}}$$$To flush, use:
2 3 0 1 10 0 0 1
1 2 2 3 1 2 1 3 1 4
In the first test, there are $$$3$$$ batteries. Only batteries $$$2$$$ and $$$3$$$ work. Therefore, $$$a=2$$$ and we have to find a pair of batteries that work in $$$\lfloor \frac{n^2}{a} \rfloor = \lfloor \frac{3^2}{2} \rfloor = 4$$$ queries.
The interaction proceeds as follows:
| Participant | Jury | Explanation |
| — | $$$3$$$ | There are $$$3$$$ batteries in this test case |
| $$$1$$$ $$$2$$$ | $$$0$$$ | The solution asks whether batteries $$$1$$$ and $$$2$$$ turn the flashlight on. The jury responds that they don't. |
| $$$2$$$ $$$3$$$ | $$$1$$$ | The solution asks whether batteries $$$2$$$ and $$$3$$$ turn the flashlight on. The jury responds that they do. A pair of batteries that work is found, so the interaction of the first test case terminates successfully. |
In the second test case, there are $$$10$$$ batteries. Only batteries $$$1$$$, $$$4$$$, $$$6$$$ and $$$9$$$ work. Therefore, $$$a=4$$$ and we have to find a pair of batteries that work in $$$\lfloor \frac{n^2}{a} \rfloor = \lfloor \frac{10^2}{4} \rfloor = 25$$$ queries.
The interaction proceeds as follows:
| Participant | Jury | Explanation |
| — | $$$10$$$ | There are $$$10$$$ batteries in this test case. |
| $$$1$$$ $$$2$$$ | $$$0$$$ | The solution asks whether batteries $$$1$$$ and $$$2$$$ turn the flashlight on. The jury responds that they don't. |
| $$$1$$$ $$$3$$$ | $$$0$$$ | The solution asks whether batteries $$$1$$$ and $$$3$$$ turn the flashlight on. The jury responds that they don't. |
| $$$1$$$ $$$4$$$ | $$$1$$$ | The solution asks whether batteries $$$1$$$ and $$$4$$$ turn the flashlight on. The jury responds that they do. A pair of batteries that work is found, so the interaction of the first test case terminates successfully. |
Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.
Mimo and Yuyu just finished their 1000-piece jigsaw puzzle of beautiful Bellas Artes! Now they are looking for other ways to entertain themselves.
There is an $$$n \times m$$$ grid of cells with columns labeled $$$1, 2, \ldots m$$$ from left to right and rows labeled $$$1, 2, \ldots n$$$ from top to bottom. Let $$$(u, v)$$$ ($$$1 \le u \le n, 1 \le v \le m$$$) denote the cell in the $$$u$$$-th row and $$$v$$$-th column. Each cell can contain any number of tokens which are indistinguishable among themselves. Initially, there are $$$k$$$ tokens, the $$$i$$$-th of which is located in $$$(x_i, y_i)$$$.
Mimo and Yuyu now play a game alternating turns. On his/her turn, a player chooses a token $$$c$$$ currently in the grid as well as a sequence of distinct cells $$$(a_1, b_1), (a_2, b_2), \ldots (a_p, b_p)$$$ ($$$p \ge 2$$$) such that the following conditions hold:
Then, he/she removes $$$c$$$ from the grid and adds 1 token to $$$(a_2, b_2), (a_3, b_3), \ldots (a_p, b_p)$$$ each. This concludes his/her turn.
The player who cannot make a turn loses. Mimo goes first. Determine who will win if both players play optimally.
For example, consider a game where $$$n=6$$$, $$$m=4$$$, and 3 tokens currently exist in $$$(2, 3)$$$, $$$(4, 2)$$$, and $$$(6, 4)$$$ (as shown in Figure 1). In this scenario, a valid turn, for instance, could consist of choosing $$$c$$$ as the token in $$$(6, 4)$$$ and the sequence of cells with $$$p=10$$$ defined by $$$a=[6,6,5,4,3,2,2,3,4,4]$$$ and $$$b=[4,3,3,3,3,3,2,2,2,1]$$$. Note that $$$(a_i, b_i)$$$ describes valid cells in the grid.
For the sake of clarity, a dashed line is shown in Figure 2 passing through this particular choice of $$$(a_1, b_1), (a_2, b_2), \ldots (a_p, b_p)$$$ in order. Figure 3 and 4 show the state of the game after the turn is performed, with and without the highlighted sequence respectively.
![]() | ![]() |
| Figure 1 | Figure 2 |
![]() | ![]() |
| Figure 3 | Figure 4 |
Note that the first and seventh test case in the example correspond to the games shown in Figure 1 and Figure 4 respectively.
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 three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$1 \le n, m, k \le 2 \cdot 10^5$$$).
The $$$i$$$-th of the next $$$k$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i \le n, 1 \le y_i \le m$$$).
It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
Note that there is no explicit upper bound on the sum of $$$n$$$ and $$$m$$$.
For each test case, output Mimo if Mimo wins, or Yuyu if Yuyu wins.
You can output the answer in any case (upper or lower). For example, the strings mIMo, mimo, Mimo, and MIMO will be recognized as responses indicating that the first player wins.
76 4 32 34 26 41 1 11 13 2 41 11 22 23 220 4 310 420 21 31 5 11 32 3 52 11 21 22 31 36 4 116 35 34 33 32 32 32 23 24 24 24 1
MimoYuyuMimoMimoYuyuYuyuYuyu
In the second test case, Mimo cannot make any moves, so Yuyu wins.
In the third test case, the token in $$$(1, 1)$$$ cannot be used as $$$c$$$ for any turn because there is no sequence of cells that begins with $$$(1, 1)$$$ and satisfies $$$b_1 \gt b_2$$$, so the game might unfold as follows:
It can be shown that Yuyu could not have played more optimally, so Mimo wins.
Juan has a beautiful tree with $$$n$$$ nodes numbered from $$$1$$$ to $$$n$$$. There are also $$$k$$$ distinct colors numbered from $$$1$$$ to $$$k$$$. Each node $$$u$$$ in the tree has its own set $$$C_u$$$ which contains colors. Let's denote with $$$s$$$ the quantity $$$\sum_{i=1}^n \left| C_i \right|$$$.
There are $$$q$$$ queries where you will be given nodes $$$u$$$ and $$$v$$$. Let $$$P$$$ denote the set of all nodes in the simple path between $$$u$$$ and $$$v$$$, inclusive. You are asked to calculate for each query the following quantity: $$$$$$\left| \bigcap_{w \in P} C_w \right|$$$$$$
That is, the cardinality of the intersection of the sets of all the nodes in the path from $$$u$$$ to $$$v$$$. In other words, calculate how many colors appear in every set on the path from $$$u$$$ to $$$v$$$.
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 four integers $$$n$$$, $$$k$$$, $$$s$$$, and $$$q$$$ ($$$1 \le n, k, q \le 3 \cdot 10^5, 1 \le s \le \min (nk, 3 \cdot 10^5)$$$) — the number of nodes in the tree, the number of distinct colors, the sum of the cardinalities of all sets of colors, and the number of queries, respectively.
The next $$$n-1$$$ lines each contain two integers $$$u$$$, $$$v$$$ ($$$1 \le u, v \le n$$$), indicating that there's an edge between nodes $$$u$$$ and $$$v$$$.
The next $$$s$$$ lines each contain two integers $$$v$$$ and $$$x$$$ ($$$1 \le v \le n, 1 \le x \le k$$$) indicating that color $$$x$$$ is in $$$C_v$$$. All $$$s$$$ lines are pairwise distinct.
The next $$$q$$$ lines each contain two integers $$$u$$$ and $$$v$$$ ($$$1 \le u, v \le n$$$), indicating a query between nodes $$$u$$$ and $$$v$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
It is guaranteed that the sum of $$$s$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
It is guaranteed that the sum of $$$q$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
For each test case, output a line: the answer to all queries in the order they appear in the input, separated by spaces.
23 5 10 41 32 11 11 21 31 41 52 12 22 53 13 21 32 31 21 19 3 12 107 22 46 89 62 15 82 53 91 36 19 39 15 12 38 14 35 38 37 33 14 71 44 55 54 29 92 22 25 27 3
2 2 3 51 1 1 2 1 2 1 1 1 0
In the first test case, there is a tree of $$$3$$$ nodes with edges $$$(1, 3)$$$ and $$$(2, 1)$$$. The colors in the sets of each node are:
The $$$4$$$ queries are between the pairs of nodes $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(1, 2)$$$, $$$(1, 1)$$$ and correspond to calculating the following quantities respectively: