Codeforces Round 1056 (Div. 2)
A. El fucho
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  • All teams in the winners' group pair up.
    • If there is an odd number of teams in the winners' group, there would be a team that didn't get paired up (and wouldn't play a match). That team stays in the winners' group.
    • For teams in the winners' that got paired up, each pair plays a football match in which there are no ties.
      • If a team wins, it stays in the winners' group.
      • If a team loses, it drops down to the losers' group in the next round.
  • All teams in the losers' group pair up.
    • If there is an odd number of teams in the losers' group, there would be a team that didn't get paired up (and wouldn't play a match). That team stays in the losers' group.
    • For teams in the losers' that got paired up, each pair plays a football match in which there are no ties.
      • If a team wins, it stays in the losers' group.
      • If a team loses, it gets eliminated from the tournament.

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.

Input

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.

Output

For each test case, print the total number of matches played during the tournament.

Example
Input
2
2
3
Output
2
4
Note

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.

B. Abraham's Great Escape
time limit per test
1.5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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$$$.

Output

For each test case, do one of the following:

  • If there exists a grid satisfying the requirement, print YES and then print $$$n$$$ lines with $$$n$$$ characters in each line indicating the direction of the arrows. Each character should be one of U (up), R (right), L (left), or D (down).
  • Otherwise, declare that the task is impossible by printing NO.

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.

Example
Input
3
2 4
3 5
2 3
Output
YES
UU
UU
YES
UUU
RDR
ULR
NO
Note

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:

  • Any cell in the first row (all of which are U)
  • Any cell in the third column (one of which is U and the other two R)

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.

C. The Ancient Wizards' Capes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  • Wizard $$$j$$$ wears his cape on his left side and $$$i \ge j$$$.
  • Wizard $$$j$$$ wears his cape on his right side and $$$i \le j$$$.

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$$$.

Input

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$$$.

Output

For each test case, print one integer — the number of arrangements for the wizards' capes that satisfy the condition, modulo $$$676\,767\,677$$$.

Example
Input
7
1
1
4
4 4 3 2
3
1 3 2
2
2 1
3
2 2 2
3
3 2 3
3
3 2 2
Output
2
1
0
1
2
0
0
Note

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.

  • From position $$$1$$$, we can see wizards $$$1$$$, $$$2$$$, $$$3$$$, and $$$4$$$.
  • From position $$$2$$$, we can see wizards $$$1$$$, $$$2$$$, $$$3$$$, and $$$4$$$.
  • From position $$$3$$$, we can see wizards $$$1$$$, $$$3$$$, and $$$4$$$.
  • From position $$$4$$$, we can see wizards $$$1$$$ and $$$4$$$.

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:

  • $$$1 \mid \quad \mid 2 \quad 3 \, \mid$$$
  • $$$\mid \, 1 \quad 2 \mid \quad \mid 3$$$

D. Batteries
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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$$$.

Interaction

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:

  • $$$\mathbb{u}\,\mathbb{v}$$$ ($$$1 \leq u, v \leq n$$$ and $$$u \neq v$$$) — the indices of the batteries you want to test.

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:

  • fflush(stdout) or cout.flush() in C++;
  • sys.stdout.flush() in Python;
  • see the documentation for other languages.
Example
Input
2
3

0

1
10

0

0

1
Output


1 2

2 3


1 2

1 3

1 4

Note

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:

ParticipantJuryExplanation
—$$$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:

ParticipantJuryExplanation
—$$$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.

E. Mimo & Yuyu
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  • $$$c$$$ is located in $$$(a_1, b_1)$$$
  • For all $$$i$$$ ($$$1 \le i \lt p$$$), $$$\left|a_{i+1} - a_i\right|+\left|b_{i+1} - b_i\right| = 1$$$. That is, adjacent cells in the sequence must be adjacent in the grid.
  • $$$b_1 \ge b_2 \ge \ldots \ge b_p$$$. That is, the columns of the cells of the sequence must form a non-increasing sequence (never stepping away from column $$$1$$$).
  • $$$b_p = 1$$$. That is, the last cell of the sequence must lie in column $$$1$$$.
  • $$$b_1 \gt b_2$$$. In particular, $$$b_2 = b_1-1$$$. That is, $$$(a_1, b_1)$$$ must be the only cell of the sequence lying in column $$$b_1$$$.

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 1Figure 2
Figure 3Figure 4

Note that the first and seventh test case in the example correspond to the games shown in Figure 1 and Figure 4 respectively.

Input

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$$$.

Output

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.

Example
Input
7
6 4 3
2 3
4 2
6 4
1 1 1
1 1
3 2 4
1 1
1 2
2 2
3 2
20 4 3
10 4
20 2
1 3
1 5 1
1 3
2 3 5
2 1
1 2
1 2
2 3
1 3
6 4 11
6 3
5 3
4 3
3 3
2 3
2 3
2 2
3 2
4 2
4 2
4 1
Output
Mimo
Yuyu
Mimo
Mimo
Yuyu
Yuyu
Yuyu
Note

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:

  • Mimo removes the token in $$$(1, 2)$$$ and adds a token to $$$(1, 1)$$$.
  • Yuyu removes the token in $$$(2, 2)$$$ and adds a token to $$$(2, 1)$$$.
  • Mimo removes the token in $$$(3, 2)$$$ and adds a token to $$$(3, 1)$$$.

It can be shown that Yuyu could not have played more optimally, so Mimo wins.

F. Juan's Colorful Tree
time limit per test
3 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

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$$$.

Input

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$$$.

Output

For each test case, output a line: the answer to all queries in the order they appear in the input, separated by spaces.

Example
Input
2
3 5 10 4
1 3
2 1
1 1
1 2
1 3
1 4
1 5
2 1
2 2
2 5
3 1
3 2
1 3
2 3
1 2
1 1
9 3 12 10
7 2
2 4
6 8
9 6
2 1
5 8
2 5
3 9
1 3
6 1
9 3
9 1
5 1
2 3
8 1
4 3
5 3
8 3
7 3
3 1
4 7
1 4
4 5
5 5
4 2
9 9
2 2
2 2
5 2
7 3
Output
2 2 3 5
1 1 1 2 1 2 1 1 1 0
Note

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:

  • $$$C_1 = \{ 1, 2, 3, 4, 5 \}$$$
  • $$$C_2 = \{ 1, 2, 5 \}$$$
  • $$$C_3 = \{ 1, 2 \}$$$

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:

  • $$$\left| C_1 \cap C_3 \right| = \left| \{ 1, 2 \} \right| = 2$$$
  • $$$\left| C_2 \cap C_1 \cap C_3 \right| = \left| \{ 1, 2 \} \right| = 2$$$
  • $$$\left| C_2 \cap C_1 \right| = \left| \{ 1, 2, 5 \} \right| = 3$$$
  • $$$\left| C_1 \right| = \left| \{ 1, 2, 3, 4, 5 \} \right| = 5$$$