CodeTON Round 5 (Div. 1 + Div. 2, Rated, Prizes!)
A. Tenzing and Tsondu
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Tsondu always runs first! ! !

Tsondu and Tenzing are playing a card game. Tsondu has $$$n$$$ monsters with ability values $$$a_1, a_2, \ldots, a_n$$$ while Tenzing has $$$m$$$ monsters with ability values $$$b_1, b_2, \ldots, b_m$$$.

Tsondu and Tenzing take turns making moves, with Tsondu going first. In each move, the current player chooses two monsters: one on their side and one on the other side. Then, these monsters will fight each other. Suppose the ability values for the chosen monsters are $$$x$$$ and $$$y$$$ respectively, then the ability values of the monsters will become $$$x-y$$$ and $$$y-x$$$ respectively. If the ability value of any monster is smaller than or equal to $$$0$$$, the monster dies.

The game ends when at least one player has no monsters left alive. The winner is the player with at least one monster left alive. If both players have no monsters left alive, the game ends in a draw.

Find the result of the game when both players play optimally.

Input

Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 2 \cdot 10^3$$$) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n,m \leq 50$$$) — the number of monsters Tsondu and Tenzing have respectively.

The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ $$$(1 \leq a_i \leq 10^9$$$) — the ability values of Tsondu's monsters.

The third line of each test case contains $$$m$$$ integers $$$b_1,b_2,\ldots,b_m$$$ $$$(1 \leq b_i \leq 10^9$$$) — the ability values of Tenzing's monsters.

Output

For each test case, output "Tsondu" if Tsondu wins, "Tenzing" if Tenzing wins, and "Draw" if the game ends in a draw. (Output without quotes.)

Note that the output is case-sensitive. For example, if the answer is "Tsondu", the outputs "tsondu", "TSONDU", and "tSonDu" will all be recognized as incorrect outputs.

Example
Input
6
1 3
9
1 2 3
2 3
1 2
1 1 1
3 2
1 2 3
1 1
3 3
1 1 1
2 2 2
10 10
1 2 3 3 2 2 1 1 2 2
3 3 3 3 2 1 1 1 1 1
10 10
1 2 3 4 5 6 7 8 9 10
6 7 8 9 10 11 1 1 1 1
Output
Tsondu
Draw
Tsondu
Tenzing
Draw
Draw
Note

Consider the first test case. It can be shown that Tsondu has a winning strategy. The following is a possible way that Tsondu can win (note that the players may not be playing optimally in this example):

  • In the first move, Tsondu chooses a monster with ability value $$$9$$$ on his side to fight against a monster with ability value $$$1$$$ on Tenzing's side, the ability value of both monsters become $$$8$$$ and $$$-8$$$ respectively. The monster with ability value $$$-8$$$ on Tenzing's side dies.
  • In the second move, Tenzing chooses a monster with ability value $$$2$$$ on his side to fight against a monster with ability value $$$8$$$ on Tsondu's side, the ability value of both monsters become $$$-6$$$ and $$$6$$$ respectively. The monster with ability value $$$-6$$$ on Tenzing's side dies.
  • In the third move, Tsondu chooses a monster with ability value $$$6$$$ on his side to fight against a monster with ability value $$$3$$$ onTenzing's side, the ability value of both monsters become $$$3$$$ and $$$-3$$$ respectively. The monster with ability value $$$-3$$$ on Tenzing's side dies.
  • Now, Tenzing has no monsters left alive. Since Tsondu still has monsters left alive, Tsondu wins.

B. Tenzing and Books
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Tenzing received $$$3n$$$ books from his fans. The books are arranged in $$$3$$$ stacks with $$$n$$$ books in each stack. Each book has a non-negative integer difficulty rating.

Tenzing wants to read some (possibly zero) books. At first, his knowledge is $$$0$$$.

To read the books, Tenzing will choose a non-empty stack, read the book on the top of the stack, and then discard the book. If Tenzing's knowledge is currently $$$u$$$, then his knowledge will become $$$u|v$$$ after reading a book with difficulty rating $$$v$$$. Here $$$|$$$ denotes the bitwise OR operation. Note that Tenzing can stop reading books whenever he wants.

Tenzing's favourite number is $$$x$$$. Can you help Tenzing check if it is possible for his knowledge to become $$$x$$$?

Input

Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 \leq n \leq 10^5$$$, $$$0 \leq x \leq 10^9$$$) — the number of books in each stack and Tenzing's favourite number.

The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$0 \leq a_i \leq 10^9$$$)  — the difficulty rating of the books in the first stack, from top to bottom.

The third line of each test case contains $$$n$$$ integers $$$b_1,b_2,\ldots,b_n$$$ ($$$0 \leq b_i \leq 10^9$$$)  — the difficulty rating of the books in the second stack, from top to bottom.

The fourth line of each test case contains $$$n$$$ integers $$$c_1,c_2,\ldots,c_n$$$ ($$$0 \leq c_i \leq 10^9$$$)  — the difficulty rating of the books in the third stack, from top to bottom.

It is guaranteed that the sum of $$$n$$$ does not exceed $$$10^5$$$.

Output

For each test case, output "Yes" (without quotes) if Tenzing can make his knowledge equal to $$$x$$$, and "No" (without quotes) otherwise.

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
5 7
1 2 3 4 5
5 4 3 2 1
1 3 5 7 9
5 2
3 2 3 4 5
5 4 3 2 1
3 3 5 7 9
3 0
1 2 3
3 2 1
2 2 2
Output
Yes
No
Yes
Note

For the first test case, Tenzing can read the following $$$4$$$ books:

  • read the book with difficulty rating $$$1$$$ on the top of the first stack. Tenzing's knowledge changes to $$$0|1=1$$$.
  • read the book with difficulty rating $$$1$$$ on the top of the third stack. Tenzing's knowledge changes to $$$1|1=1$$$.
  • read the book with difficulty rating $$$2$$$ on the top of the first stack. Tenzing's knowledge changes to $$$1|2=3$$$.
  • read the book with difficulty rating $$$5$$$ on the top of the second stack. Tenzing's knowledge changes to $$$3|5=7$$$.

After reading all books, Tenzing's knowledge is $$$7$$$.

For the third test case, Tenzing can read $$$0$$$ books to make his final knowledge equals to $$$0$$$.

C. Tenzing and Balls
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Enjoy erasing Tenzing, identified as Accepted!

Tenzing has $$$n$$$ balls arranged in a line. The color of the $$$i$$$-th ball from the left is $$$a_i$$$.

Tenzing can do the following operation any number of times:

  • select $$$i$$$ and $$$j$$$ such that $$$1\leq i \lt j \leq |a|$$$ and $$$a_i=a_j$$$,
  • remove $$$a_i,a_{i+1},\ldots,a_j$$$ from the array (and decrease the indices of all elements to the right of $$$a_j$$$ by $$$j-i+1$$$).

Tenzing wants to know the maximum number of balls he can remove.

Input

Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1\leq t\leq 10^3$$$) — the number of test cases. The description of test cases follows.

The first line contains a single integer $$$n$$$ ($$$1\leq n\leq 2\cdot 10^5$$$) — the number of balls.

The second line contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\leq a_i \leq n$$$) — the color of the balls.

It is guaranteed that sum of $$$n$$$ of all test cases will not exceed $$$2\cdot 10^5$$$.

Output

For each test case, output the maximum number of balls Tenzing can remove.

Example
Input
2
5
1 2 2 3 3
4
1 2 1 2
Output
4
3
Note

In the first example, Tenzing will choose $$$i=2$$$ and $$$j=3$$$ in the first operation so that $$$a=[1,3,3]$$$. Then Tenzing will choose $$$i=2$$$ and $$$j=3$$$ again in the second operation so that $$$a=[1]$$$. So Tenzing can remove $$$4$$$ balls in total.

In the second example, Tenzing will choose $$$i=1$$$ and $$$j=3$$$ in the first and only operation so that $$$a=[2]$$$. So Tenzing can remove $$$3$$$ balls in total.

D. Tenzing and His Animal Friends
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Tell a story about me and my animal friends.

Tenzing has $$$n$$$ animal friends. He numbers them from $$$1$$$ to $$$n$$$.

One day Tenzing wants to play with his animal friends. To do so, Tenzing will host several games.

In one game, he will choose a set $$$S$$$ which is a subset of $$$\{1,2,3,...,n\}$$$ and choose an integer $$$t$$$. Then, he will play the game with the animals in $$$S$$$ for $$$t$$$ minutes.

But there are some restrictions:

  1. Tenzing loves friend $$$1$$$ very much, so $$$1$$$ must be an element of $$$S$$$.
  2. Tenzing doesn't like friend $$$n$$$, so $$$n$$$ must not be an element of $$$S$$$.
  3. There are m additional restrictions. The $$$i$$$-th special restriction is described by integers $$$u_i$$$, $$$v_i$$$ and $$$y_i$$$, suppose $$$x$$$ is the total time that exactly one of $$$u_i$$$ and $$$v_i$$$ is playing with Tenzing. Tenzing must ensure that $$$x$$$ is less or equal to $$$y_i$$$. Otherwise, there will be unhappiness.

Tenzing wants to know the maximum total time that he can play with his animal friends. Please find out the maximum total time that Tenzing can play with his animal friends and a way to organize the games that achieves this maximum total time, or report that he can play with his animal friends for an infinite amount of time. Also, Tenzing does not want to host so many games, so he will host at most $$$n^2$$$ games.

Input

The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \leq 100$$$, $$$0 \leq m \leq \frac{n(n-1)}{2}$$$) — the number of animal friends and the number of special restrictions.

The $$$i$$$-th of the following $$$m$$$ lines of input contains three integers $$$u_i$$$, $$$v_i$$$ and $$$y_i$$$ ($$$1\leq u_i \lt v_i\leq n$$$, $$$0\leq y_i\leq 10^9$$$) — describing the $$$i$$$-th special restriction. It is guaranteed that for $$$1 \leq i \lt j \leq m$$$, $$$(u_i,v_i) \neq (u_j,v_j)$$$.

Output

If Tenzing can play with his animal friends for an infinite amount of time, output "inf". (Output without quotes.)

Otherwise, in the first line, output the total time $$$T$$$ ($$$0 \leq t \leq 10^{18}$$$) and the number of games $$$k$$$ ($$$0 \leq k \leq n^2$$$).

In the following $$$k$$$ lines of output, output a binary string $$$s$$$ of length $$$n$$$ and an integer $$$t$$$ ($$$0 \leq t \leq 10^{18}$$$) — representing the set $$$S$$$ and the number of minutes this game will be played. If $$$s_i=\texttt{1}$$$, then $$$i \in S$$$, otherwise if $$$s_i=\texttt{0}$$$, then $$$i \notin S$$$.

Under the constraints of this problem, it can be proven that if Tenzing can only play with his friends for a finite amount of time, then he can only play with them for at most $$$10^{18}$$$ minutes.

Examples
Input
5 4
1 3 2
1 4 2
2 3 1
2 5 1
Output
4 4
10000 1
10010 1
10100 1
11110 1
Input
3 0
Output
inf
Note

In the first test case:

  1. Tenzing will host a game with friend $$$\{1\}$$$ for $$$1$$$ minute.
  2. Tenzing will host a game with friends $$$\{1,4\}$$$ for $$$1$$$ minute.
  3. Tenzing will host a game with friends $$$\{1,3\}$$$ for $$$1$$$ minute.
  4. Tenzing will host a game with friends $$$\{1,2,3,4\}$$$ for $$$1$$$ minute.

If after that, Tenzing host another game with friends $$$\{1,2\}$$$ for $$$1$$$ minute. Then the time of exactly one of friends $$$2$$$ or $$$3$$$ with Tenzing will becomes $$$2$$$ minutes which will not satisfy the $$$3$$$-rd special restriction.

In the second test case, there is no special restrictions. So Tenzing can host a game with friend $$$\{1\}$$$ for an infinite amount of time.

E. Tenzing and Triangle
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are $$$n$$$ pairwise-distinct points and a line $$$x+y=k$$$ on a two-dimensional plane. The $$$i$$$-th point is at $$$(x_i,y_i)$$$. All points have non-negative coordinates and are strictly below the line. Alternatively, $$$0 \leq x_i,y_i, x_i+y_i \lt k$$$.

Tenzing wants to erase all the points. He can perform the following two operations:

  1. Draw triangle: Tenzing will choose two non-negative integers $$$a$$$, $$$b$$$ that satisfy $$$a+b \lt k$$$, then all points inside the triangle formed by lines $$$x=a$$$, $$$y=b$$$ and $$$x+y=k$$$ will be erased. It can be shown that this triangle is an isosceles right triangle. Let the side lengths of the triangle be $$$l$$$, $$$l$$$ and $$$\sqrt 2 l$$$ respectively. Then, the cost of this operation is $$$l \cdot A$$$.

    The blue area of the following picture describes the triangle with $$$a=1,b=1$$$ with cost $$$=1\cdot A$$$.

  2. Erase a specific point: Tenzing will choose an integer $$$i$$$ that satisfies $$$1 \leq i \leq n$$$ and erase the point $$$i$$$. The cost of this operation is $$$c_i$$$.

Help Tenzing find the minimum cost to erase all of the points.

Input

The first line of the input contains three integers $$$n$$$, $$$k$$$ and $$$A$$$ ($$$1\leq n,k\leq 2\cdot 10^5$$$, $$$1\leq A\leq 10^4$$$) — the number of points, the coefficient describing the hypotenuse of the triangle and the coefficient describing the cost of drawing a triangle.

The following $$$n$$$ lines of the input the $$$i$$$-th line contains three integers $$$x_i,y_i,c_i$$$ ($$$0\leq x_i,y_i,x_i+y_i \lt k$$$, $$$1\leq c_i\leq 10^4$$$) — the coordinate of the $$$i$$$-th points and the cost of erasing it using the second operation. It is guaranteed that the coordinates are pairwise distinct.

Output

Output a single integer —the minimum cost needed to erase all of the points.

Examples
Input
4 6 1
1 2 1
2 1 1
1 1 1
3 2 6
Output
4
Input
6 7 1
4 2 1
3 3 1
5 1 4
3 2 5
4 1 1
0 6 4
Output
4
Input
10 4 100
0 0 1
0 1 1
0 2 50
0 3 200
1 0 1
1 1 1
1 2 1
2 0 200
2 1 200
3 0 200
Output
355
Note

The picture of the first example:

Tenzing do the following operations:

  1. draw a triangle with $$$a=3,b=2$$$, the cost $$$=1\cdot A=1$$$.
  2. erase the first point, the cost $$$=1$$$.
  3. erase the second point, the cost $$$=1$$$.
  4. erase the third point, the cost $$$=1$$$.

The picture of the second example:

F. Tenzing and Tree
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Tenzing has an undirected tree of $$$n$$$ vertices.

Define the value of a tree with black and white vertices in the following way. The value of an edge is the absolute difference between the number of black nodes in the two components of the tree after deleting the edge. The value of the tree is the sum of values over all edges.

For all $$$k$$$ such that $$$0 \leq k \leq n$$$, Tenzing wants to know the maximum value of the tree when $$$k$$$ vertices are painted black and $$$n-k$$$ vertices are painted white.

Input

The first line of the input contains a single integer $$$n$$$ ($$$1\leq n\leq 5000$$$) — the number of vertices.

The following $$$n-1$$$ lines of the input contains $$$2$$$ integers $$$u_i$$$ and $$$v_i$$$ ($$$1 \leq u_i, v_i \leq n, u_i \neq v_i$$$) — indicating an edge between vertices $$$u_i$$$ and $$$v_i$$$. It is guaranteed that the given edges form a tree.

Output

Output $$$n+1$$$ numbers. The $$$i$$$-th number is the answer of $$$k=i-1$$$.

Examples
Input
4
1 2
3 2
2 4
Output
0 3 4 5 6 
Input
1
Output
0 0 
Note

Consider the first example. When $$$k=2$$$, Tenzing can paint vertices $$$1$$$ and $$$2$$$ black then the value of edge $$$(1,2)$$$ is 0, and the values of other edges are all equal to $$$2$$$. So the value of that tree is $$$4$$$.

G. Tenzing and Random Operations
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output
Yet another random problem.

Tenzing has an array $$$a$$$ of length $$$n$$$ and an integer $$$v$$$.

Tenzing will perform the following operation $$$m$$$ times:

  1. Choose an integer $$$i$$$ such that $$$1 \leq i \leq n$$$ uniformly at random.
  2. For all $$$j$$$ such that $$$i \leq j \leq n$$$, set $$$a_j := a_j + v$$$.

Tenzing wants to know the expected value of $$$\prod_{i=1}^n a_i$$$ after performing the $$$m$$$ operations, modulo $$$10^9+7$$$.

Formally, let $$$M = 10^9+7$$$. 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 \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output the integer $$$x$$$ that $$$0 \le x \lt M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.

Input

The first line of input contains three integers $$$n$$$, $$$m$$$ and $$$v$$$ ($$$1\leq n\leq 5000$$$, $$$1\leq m,v\leq 10^9$$$).

The second line of input contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\leq a_i\leq 10^9$$$).

Output

Output the expected value of $$$\prod_{i=1}^n a_i$$$ modulo $$$10^9+7$$$.

Examples
Input
2 2 5
2 2
Output
84
Input
5 7 9
9 9 8 2 4
Output
975544726
Note

There are three types of $$$a$$$ after performing all the $$$m$$$ operations :

1. $$$a_1=2,a_2=12$$$ with $$$\frac{1}{4}$$$ probability.

2. $$$a_1=a_2=12$$$ with $$$\frac{1}{4}$$$ probability.

3. $$$a_1=7,a_2=12$$$ with $$$\frac{1}{2}$$$ probability.

So the expected value of $$$a_1\cdot a_2$$$ is $$$\frac{1}{4}\cdot (24+144) + \frac{1}{2}\cdot 84=84$$$.

H. Tenzing and Random Real Numbers
time limit per test
1 second
memory limit per test
1024 megabytes
input
standard input
output
standard output

There are $$$n$$$ uniform random real variables between 0 and 1, inclusive, which are denoted as $$$x_1, x_2, \ldots, x_n$$$.

Tenzing has $$$m$$$ conditions. Each condition has the form of $$$x_i+x_j\le 1$$$ or $$$x_i+x_j\ge 1$$$.

Tenzing wants to know the probability that all the conditions are satisfied, modulo $$$998~244~353$$$.

Formally, let $$$M = 998~244~353$$$. 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 \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output the integer $$$x$$$ that $$$0 \le x \lt M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.

Input

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1\le n\le 20$$$, $$$0\le m\le n^2+n$$$).

Then following $$$m$$$ lines of input contains three integers $$$t$$$, $$$i$$$ and $$$j$$$ ($$$t \in \{0,1\}$$$, $$$1\le i\le j\le n$$$).

  • If $$$t=0$$$, the condition is $$$x_i+x_j\le 1$$$.
  • If $$$t=1$$$, the condition is $$$x_i+x_j\ge 1$$$.

It is guaranteed that all conditions are pairwise distinct.

Output

Output the probability that all the conditions are satisfied, modulo $$$M = 998~244~353$$$.

Examples
Input
3 2
0 1 2
1 3 3
Output
748683265
Input
3 3
0 1 2
0 1 3
0 2 3
Output
748683265
Input
3 4
0 1 2
0 1 3
1 2 3
1 2 2
Output
935854081
Input
4 4
0 1 2
0 3 4
1 1 3
1 2 4
Output
0
Input
8 12
0 1 2
0 2 3
1 3 4
0 1 4
0 5 6
0 6 7
1 7 8
0 5 8
1 3 7
1 3 8
1 4 7
1 4 8
Output
997687297
Note

In the first test case, the conditions are $$$x_1+x_2 \le 1$$$ and $$$x_3+x_3\ge 1$$$, and the probability that each condition is satisfied is $$$\frac 12$$$, so the probability that they are both satisfied is $$$\frac 12\cdot \frac 12=\frac 14$$$, modulo $$$998~244~353$$$ is equal to $$$748683265$$$.

In the second test case, the answer is $$$\frac 14$$$.

In the third test case, the answer is $$$\frac 1{16}$$$.

In the fourth test case, the sum of all variables must equal $$$2$$$, so the probability is $$$0$$$.

I. Tenzing and Necklace
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output
bright, sunny and innocent......

Tenzing has a beautiful necklace. The necklace consists of $$$n$$$ pearls numbered from $$$1$$$ to $$$n$$$ with a string connecting pearls $$$i$$$ and $$$(i \text{ mod } n)+1$$$ for all $$$1 \leq i \leq n$$$.

One day Tenzing wants to cut the necklace into several parts by cutting some strings. But for each connected part of the necklace, there should not be more than $$$k$$$ pearls. The time needed to cut each string may not be the same. Tenzing needs to spend $$$a_i$$$ minutes cutting the string between pearls $$$i$$$ and $$$(i \text{ mod } n)+1$$$.

Tenzing wants to know the minimum time in minutes to cut the necklace such that each connected part will not have more than $$$k$$$ pearls.

Input

Each test contains multiple test cases. The first line of input contains a single integer $$$t$$$ ($$$1 \le t \le 10^5$$$) — the number of test cases. The description of test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2\leq n\leq 5\cdot 10^5$$$, $$$1\leq k \lt n$$$).

The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\leq a_i\leq 10^9$$$).

It is guaranteed that the sum of $$$n$$$ of all test cases does not exceed $$$5 \cdot 10^5$$$.

Output

For each test case, output the minimum total time in minutes required.

Example
Input
4
5 2
1 1 1 1 1
5 2
1 2 3 4 5
6 3
4 2 5 1 3 3
10 3
2 5 6 5 2 1 7 9 7 2
Output
3
7
5
15
Note

In the first test case, the necklace will be cut into $$$3$$$ parts: $$$[1,2][3,4][5]$$$, so the total time is $$$3$$$.

In the second test case, the necklace will be cut into $$$3$$$ parts: $$$[5,1][2][3,4]$$$, Tenzing will cut the strings connecting $$$(1,2), (2,3)$$$ and $$$(4,5)$$$, so the total time is $$$a_1+a_2+a_4=7$$$.