Educational Codeforces Round 196 (Rated for Div. 2)
A. Numbers on the Board
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

All integers from $$$l$$$ to $$$r$$$ inclusive are written on the board, each exactly once.

We call a number on the board beautiful if it is equal to the sum of all numbers on the board, including this number itself. For example, if the numbers $$$-4$$$, $$$-1$$$, $$$2$$$, $$$3$$$, $$$5$$$ are written on the board, then the number $$$5$$$ is beautiful.

You may erase any numbers from the board, but at least one number must remain. Find the minimum number of numbers that need to be erased so that after that there is at least one beautiful number on the board. It is not necessary to erase any numbers if there is already a beautiful number on the board.

Input

The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

Each test case consists of one line containing two integers $$$l$$$ and $$$r$$$ ($$$-100 \le l \le r \le 100$$$).

Output

For each test case, print one integer — the minimum number of numbers that need to be erased from the board.

Example
Input
8
0 0
2 6
-5 -3
-2 2
-2 3
-2 5
-6 2
0 4
Output
0
4
2
0
0
2
3
3
Note

For the test cases from the statement, you should do the following:

  1. Leave the number $$$0$$$. It is equal to the sum of all numbers on the board, so nothing needs to be erased.
  2. Erase the numbers $$$2$$$, $$$3$$$, $$$4$$$, $$$5$$$. Only the number $$$6$$$ will remain on the board, and it will be beautiful.
  3. Erase the numbers $$$-4$$$ and $$$-3$$$. Only the number $$$-5$$$ will remain on the board, and it will be beautiful.
  4. Do not erase anything. The numbers $$$-2$$$, $$$-1$$$, $$$0$$$, $$$1$$$, $$$2$$$ will remain on the board. Their sum is $$$0$$$, so the number $$$0$$$ is beautiful.
  5. Do not erase anything. The numbers $$$-2$$$, $$$-1$$$, $$$0$$$, $$$1$$$, $$$2$$$, $$$3$$$ will remain on the board. Their sum is $$$3$$$, so the number $$$3$$$ is beautiful.
  6. Erase the numbers $$$4$$$ and $$$5$$$. The numbers $$$-2$$$, $$$-1$$$, $$$0$$$, $$$1$$$, $$$2$$$, $$$3$$$ will remain on the board. Their sum is $$$3$$$, so the number $$$3$$$ is beautiful.
  7. Erase the numbers $$$-6$$$, $$$-5$$$, $$$-3$$$. The numbers $$$-4$$$, $$$-2$$$, $$$-1$$$, $$$0$$$, $$$1$$$, $$$2$$$ will remain on the board. Their sum is $$$-4$$$, so the number $$$-4$$$ is beautiful.
  8. Erase the numbers $$$1$$$, $$$2$$$, $$$3$$$. The numbers $$$0$$$ and $$$4$$$ will remain on the board. Their sum is $$$4$$$, so the number $$$4$$$ is beautiful.
B. Rhythmic String
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

We call a binary string $$$s$$$ of length $$$m$$$ periodic if there exists an integer $$$k$$$ ($$$1 \le k \lt m$$$) such that $$$s_i = s_{i+k}$$$ for every $$$i$$$ from $$$1$$$ to $$$m-k$$$ inclusive. The characters of the string are numbered starting from $$$1$$$. The smallest such $$$k$$$ is called the period of the string.

We call a string $$$s$$$ rhythmic if it is periodic and among the first $$$p$$$ characters of the string (where $$$p$$$ is the period of the string), there is exactly one 1.

For example, the string 0010010 is periodic with period $$$p=3$$$ and rhythmic: among the first three characters there is exactly one 1. The string 0110110 is also periodic with period $$$p=3$$$, but it is not rhythmic: among the first three characters there are two 1s.

You are given an array $$$a$$$ of $$$n$$$ integers and a string $$$s$$$, initially consisting of $$$10^{18}$$$ zeros. For each element $$$a_i$$$, all characters of the string whose indices are divisible by $$$a_i$$$ are replaced by ones. In other words, the characters at positions $$$a_i,2a_i,3a_i,\ldots$$$, not exceeding the length of the string, become ones. Replacing a character with one again does nothing.

For example, if $$$a=[3,7,5,3]$$$, then the string $$$s$$$ will begin with 001011101101011...

Before performing these changes, you may remove any elements from array $$$a$$$. Find the minimum number of elements that need to be removed so that the resulting string $$$s$$$ is rhythmic.

Input

The first line contains an integer $$$t$$$ ($$$1 \le t \le 5000$$$) — the number of test cases.

The following descriptions of the test cases follow.

The first line of each test case contains an integer $$$n$$$ ($$$1 \le n \le 5000$$$) — the length of array $$$a$$$.

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

Additional constraints on the input:

  • the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$.
Output

For each test case, output one integer — the minimum number of elements that need to be removed from the array so that string $$$s$$$ is rhythmic.

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

In the first test case, you can remove elements $$$7$$$ and $$$5$$$. Then the string will begin with 001001001... and will be rhythmic with period $$$3$$$.

In the second test case, nothing needs to be removed. The string will begin with 000100010001... and will be rhythmic with period $$$4$$$.

In the third test case, it is enough to remove element $$$6$$$. The resulting string will be rhythmic with period $$$10$$$.

In the fourth test case, you need to keep any one element of the array.

C. Big XOR
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given an integer $$$n$$$. Consider the array $$$a = [1, 2, \ldots, n]$$$. You may choose any elements of the array and multiply each chosen element by $$$2$$$. Each element can be doubled at most once. You may choose any subset of elements, including all of them or none of them. Denote the resulting array by $$$b$$$.

Let $$$k$$$ be the bitwise OR of all elements of the original array $$$a$$$, and let $$$m$$$ be the bitwise XOR of all elements of the resulting array $$$b$$$:

$$$$$$ k = a_1 \mathbin{|} a_2 \mathbin{|} \cdots \mathbin{|} a_n, \qquad m = b_1 \oplus b_2 \oplus \cdots \oplus b_n. $$$$$$

Choose which elements to double so that $$$m \ge 2k$$$. It can be shown that this is possible for every valid value of $$$n$$$. You do not need to minimize the number of doubled elements.

Input

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

The only line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the array.

Additional constraints on the input:

  • the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
Output

For each test case, print $$$n$$$ integers $$$d_1, d_2, \ldots, d_n$$$. Each $$$d_i$$$ must be either $$$0$$$ or $$$1$$$: if $$$d_i = 1$$$, the element $$$a_i$$$ should be doubled; if $$$d_i = 0$$$, it should be left unchanged.

If there are multiple valid answers, output any of them.

Example
Input
5
3
6
9
5
11
Output
1 0 1
0 1 1 0 0 1
0 1 1 1 0 1 0 0 1
0 1 1 1 0
0 0 1 0 1 1 0 1 1 0 1
Note

In the first test case, $$$k = 3$$$. After doubling the chosen elements, $$$b = [2, 2, 6]$$$, and $$$m = 6$$$.

In the second test case, $$$k = 7$$$. After doubling the chosen elements, $$$b = [1, 4, 6, 4, 5, 12]$$$, and $$$m = 14$$$.

D. Black, Gray and White
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a tree with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. Initially, all vertices are white.

You must choose a single integer $$$k$$$ from $$$1$$$ to $$$2n$$$, inclusive. Then, for each $$$i$$$ from $$$1$$$ to $$$n$$$ in increasing order, if the tree contains a vertex numbered $$$k-i$$$ and $$$k-i \ne i$$$, the following operation is performed:

  1. vertices $$$i$$$ and $$$k-i$$$ are recolored black, regardless of their current colors;
  2. all white vertices on the simple path between $$$i$$$ and $$$k-i$$$ are recolored gray. The colors of the other vertices on this path do not change.

You must choose $$$k$$$ to maximize the number of black vertices (higher priority) and the number of gray vertices (lower priority). In other words, choose $$$k$$$ so that the number of black vertices in the tree is as large as possible, and among the values of $$$k$$$ that give the maximum number of black vertices, choose one that gives as many gray vertices as possible.

Find any optimal value of $$$k$$$. If there are multiple suitable values of $$$k$$$, print any of them.

Input

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 a single integer $$$n$$$ ($$$3 \le n \le 3 \cdot 10^5$$$) — the number of vertices in the tree.

Each of the following $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \le u,v \le n$$$) — the vertices connected by an edge.

Additional constraints on the input:

  • the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$;
  • in each test case, the edges form a tree.
Output

For each test case, print one integer $$$k$$$ ($$$1 \le k \le 2n$$$) that satisfies the requirements of the problem. If there are multiple optimal values, output any of them.

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

In the first test case, $$$k=6$$$ is chosen. After all operations, vertices $$$1$$$, $$$2$$$, $$$4$$$, and $$$5$$$ are black, and vertex $$$3$$$ is gray.

In the second test case, $$$k=7$$$ is chosen. After all operations, all six vertices are black, and there are no gray vertices.

E. Partition of a Binary String
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a binary string $$$s$$$ of length $$$n$$$. The characters of the string are numbered from $$$1$$$ to $$$n$$$.

You have to partition all integers from $$$1$$$ to $$$n$$$ into nonempty sets so that each integer belongs to exactly one set. For each pair of distinct integers $$$x$$$ and $$$y$$$ in the same set, at least one of the following conditions must hold:

  • $$$s_x = s_y$$$;
  • $$$|x-y| = 1$$$.

For a set $$$M$$$, let $$$f(M)$$$ be the number of distinct characters among $$$s_x$$$ over all $$$x \in M$$$. The cost of the set $$$M$$$ is $$$|M| \cdot f(M)$$$, where $$$|M|$$$ is the number of elements in $$$M$$$. The cost of a partition is the sum of the costs of all its sets.

For example, if the string $$$s$$$ is 10001000, the partition into the sets $$$\{1, 2\}, \{3, 8\}, \{4, 5, 6\}, \{7\}$$$ is valid, and its cost is $$$2 \cdot 2 + 2 \cdot 1 + 3 \cdot 2 + 1 \cdot 1 = 13$$$.

Find the maximum possible cost of a partition and the number of partitions with this cost. Partitions that differ only in the order of the sets or the order of elements within the sets are considered the same. Output the number of partitions modulo $$$998\,244\,353$$$.

Input

The first line contains one integer $$$t$$$ ($$$1 \le t \le 500$$$) — the number of test cases.

The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 5000$$$) — the length of the string.

The second line of each test case contains one string $$$s$$$ of length $$$n$$$, consisting only of the characters 0 and 1.

Output

For each test case, print two integers: the maximum cost of a partition and the number of partitions with this cost (taken modulo $$$998\,244\,353$$$).

Example
Input
3
8
10001000
6
000111
8
01010101
Output
13 5
8 4
16 4
F. Minimize the Graph
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a directed graph with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. The graph is constructed as follows: for each vertex $$$i$$$ from $$$2$$$ to $$$n$$$, you are given two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 \le l_i \le r_i \lt i$$$). For each integer $$$j \in [l_i, r_i]$$$, the graph contains a directed edge from $$$j$$$ to $$$i$$$. There are no other edges in the graph.

Consider a second directed graph on the same $$$n$$$ vertices. For any two distinct vertices $$$x$$$ and $$$y$$$, a path from $$$x$$$ to $$$y$$$ must exist in the second graph if and only if it exists in the first graph.

Find the minimum possible number of edges in the second graph.

Input

The first line contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases.

The first line of each test case contains one integer $$$n$$$ ($$$2 \le n \le 3 \cdot 10^5$$$) — the number of vertices.

The next $$$n-1$$$ lines describe the edges of the graph. The $$$i$$$-th of these lines contains two integers $$$l_{i+1}$$$ and $$$r_{i+1}$$$ ($$$1 \le l_{i+1} \le r_{i+1} \le i$$$).

Additional constraints on the input:

  • the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
Output

For each test case, print one integer — the minimum possible number of edges in the second graph.

Example
Input
5
5
1 1
1 2
2 3
2 4
6
1 1
1 1
2 3
2 3
4 5
5
1 1
1 2
1 3
1 4
6
1 1
1 1
1 1
2 4
2 4
6
1 1
1 2
1 1
2 4
3 5
Output
4
8
4
9
6
Note

In the first test case, you can add the edges $$$1 \to 2$$$, $$$2 \to 3$$$, $$$3 \to 4$$$, and $$$4 \to 5$$$ to the second graph.

In the second test case, you can add the edges $$$1 \to 2$$$, $$$1 \to 3$$$, $$$2 \to 4$$$, $$$3 \to 4$$$, $$$2 \to 5$$$, $$$3 \to 5$$$, $$$4 \to 6$$$, and $$$5 \to 6$$$ to the second graph.

G. Order Statistics on the Graph
time limit per test
7 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You are given a directed graph with $$$n$$$ vertices, numbered from $$$1$$$ to $$$n$$$. Each vertex has at least one incoming edge. The graph may contain self-loops, but it has no multiple edges. For each vertex $$$v$$$, an integer $$$k_v$$$ is given, ranging from $$$1$$$ to the number of incoming edges of that vertex.

Each vertex has a number written on it. Every second, all the numbers are updated as follows. For a vertex $$$v$$$, take the numbers at the vertices $$$u$$$ for which there is an edge $$$u\to v$$$, sort them in non-decreasing order, and update vertex $$$v$$$ with the number at the position $$$k_v$$$. Positions in the sorted list are numbered starting from $$$1$$$. In other words, the new number at vertex $$$v$$$ is the $$$k_v$$$-th order statistic of the array consisting of the numbers at the vertices $$$u$$$ for which there is an edge $$$u \to v$$$.

All vertices are updated simultaneously: only the numbers from before the current update are used to compute the new numbers. If a vertex has a self-loop, its own number also affects the update.

You have to process $$$q$$$ queries of the following form:

  • $$$t$$$ $$$a_1$$$ $$$a_2$$$ ... $$$a_n$$$ — determine the numbers that will be written on the vertices of the graph after $$$t$$$ seconds if their initial numbers are $$$a_1, a_2, \dots, a_n$$$.
Input

The first line contains three integers $$$n$$$, $$$m$$$, and $$$q$$$ ($$$1\le n\le20$$$; $$$n\le m\le n^2$$$; $$$1\le q\le10^5$$$) — the number of vertices, edges, and queries, respectively.

The second line contains $$$n$$$ integers $$$k_1,k_2,\ldots,k_n$$$. For each vertex $$$v$$$, it holds that $$$1\le k_v\le d_v$$$, where $$$d_v$$$ is the number of incoming edges of that vertex.

Each of the next $$$m$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1\le u,v\le n$$$), denoting an edge from $$$u$$$ to $$$v$$$. All ordered pairs $$$(u,v)$$$ are distinct.

Each of the next $$$q$$$ lines contains $$$n+1$$$ integers $$$t,a_1,a_2,\ldots,a_n$$$ ($$$0\le t\le10^{18}$$$; $$$1\le a_i\le10^9$$$) — the parameters of a query.

Output

For each query, print $$$n$$$ integers — the values on vertices $$$1,2,\dots,n$$$ after $$$t$$$ seconds.

Examples
Input
3 3 4
1 1 1
1 2
2 3
3 1
0 4 8 15
1 4 8 15
2 4 8 15
1000000000000000000 1 2 3
Output
4 8 15
15 4 8
8 15 4
3 1 2
Input
2 4 3
1 2
1 1
1 2
2 1
2 2
1 10 1
1000000000000000000 7 7
0 1000000000 1
Output
1 10
7 7
1000000000 1
Input
4 8 4
1 2 2 1
1 1
2 1
1 2
2 2
3 2
2 3
4 3
3 4
1 8 3 5 1
2 8 3 5 1
3 8 3 5 1
1000000000000000000 8 3 5 1
Output
3 5 3 5
3 3 5 3
3 3 3 5
3 3 5 3
Note

In the first example, each number moves to the next vertex of the cycle. After three seconds, all numbers return to their original positions.

In the second example, the first vertex takes the minimum of the two numbers, while the second takes the maximum.

In the third example, the array $$$[8,3,5,1]$$$ becomes $$$[3,5,3,5]$$$ after the first update and $$$[3,3,5,3]$$$ after the second update.