Codeforces Round 907 (Div. 2)
A. Sorting with Twos
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array of integers $$$a_1, a_2, \ldots, a_n$$$. In one operation, you do the following:

  • Choose a non-negative integer $$$m$$$, such that $$$2^m \leq n$$$.
  • Subtract $$$1$$$ from $$$a_i$$$ for all integers $$$i$$$, such that $$$1 \leq i \leq 2^m$$$.

Can you sort the array in non-decreasing order by performing some number (possibly zero) of operations?

An array is considered non-decreasing if $$$a_i \leq a_{i + 1}$$$ for all integers $$$i$$$ such that $$$1 \leq i \leq n - 1$$$.

Input

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

The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 20$$$) — the length of array $$$a$$$.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ — the integers in array $$$a$$$ ($$$0 \leq a_i \leq 1000$$$).

Output

For each test case, output "YES" if the array can be sorted, and "NO" otherwise.

Example
Input
8
5
1 2 3 4 5
5
6 5 3 4 4
9
6 5 5 7 5 6 6 8 7
4
4 3 2 1
6
2 2 4 5 3 2
8
1 3 17 19 27 57 179 13
5
3 17 57 179 92
10
1 2 3 4 0 6 7 8 9 10
Output
YES
YES
YES
NO
NO
NO
YES
YES
Note

In the first test case, the array is already sorted in non-decreasing order, so we don't have to perform any operations.

In the second test case, we can choose $$$m = 1$$$ twice to get the array $$$[4, 3, 3, 4, 4]$$$. Then, we can choose $$$m = 0$$$ once and get the sorted in non-decreasing order array $$$[3, 3, 3, 4, 4]$$$.

In the third test case, we can choose $$$m = 0$$$ once and get the array $$$[5, 5, 5, 7, 5, 6, 6, 8, 7]$$$. Then, we can choose $$$m = 2$$$ twice and get the array $$$[3, 3, 3, 5, 5, 6, 6, 8, 7]$$$. After that, we can choose $$$m = 3$$$ once and get the sorted in non-decreasing order array $$$[2, 2, 2, 4, 4, 5, 5, 7, 7]$$$.

For the fourth and fifth test case, it can be shown that the array could not be sorted using these operations.

B. Deja Vu
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array $$$a$$$ of length $$$n$$$, consisting of positive integers, and an array $$$x$$$ of length $$$q$$$, also consisting of positive integers.

There are $$$q$$$ modification. On the $$$i$$$-th modification ($$$1 \leq i \leq q$$$), for each $$$j$$$ ($$$1 \leq j \leq n$$$), such that $$$a_j$$$ is divisible by $$$2^{x_i}$$$, you add $$$2^{x_i-1}$$$ to $$$a_j$$$. Note that $$$x_i$$$ ($$$1 \leq x_i \leq 30$$$) is a positive integer not exceeding 30.

After all modification queries, you need to output the final array.

Input

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

The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$1 \leq n, q \leq 10^5$$$) —the length of the array $$$a$$$ and the number of queries respectively.

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

The third line of each test case contains $$$q$$$ integers $$$x_1, x_2, x_3, \ldots, x_q$$$ — the elements of the array $$$x$$$ ($$$1 \leq x_i \leq 30$$$), which are the modification queries.

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

Output

For each test case, output the array after all of the modification queries.

Example
Input
4
5 3
1 2 3 4 4
2 3 4
7 3
7 8 12 36 48 6 3
10 4 2
5 4
2 2 2 2 2
1 1 1 1
5 5
1 2 4 8 16
5 2 3 4 1
Output
1 2 3 6 6 
7 10 14 38 58 6 3 
3 3 3 3 3 
1 3 7 11 19 
Note

In the first test case, the first query will add $$$2$$$ to the integers in positions $$$4$$$ and $$$5$$$. After this addition, the array would be $$$[1, 2, 3, 6, 6]$$$. Other operations will not modify the array.

In the second test case, the first modification query does not change the array. The second modification query will add $$$8$$$ to the integer in position $$$5$$$, so that the array would look like this: $$$[7, 8, 12, 36, 56, 6, 3]$$$. The third modification query will add $$$2$$$ to the integers in positions $$$2, 3$$$, $$$4$$$ and $$$5$$$. The array would then look like this: $$$[7, 10, 14, 38, 58, 6, 3]$$$.

C. Smilo and Monsters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A boy called Smilo is playing a new game! In the game, there are $$$n$$$ hordes of monsters, and the $$$i$$$-th horde contains $$$a_i$$$ monsters. The goal of the game is to destroy all the monsters. To do this, you have two types of attacks and a combo counter $$$x$$$, initially set to $$$0$$$:

  • The first type: you choose a number $$$i$$$ from $$$1$$$ to $$$n$$$, such that there is at least one monster left in the horde with the number $$$i$$$. Then, you kill one monster from horde number $$$i$$$, and the combo counter $$$x$$$ increases by $$$1$$$.
  • The second type: you choose a number $$$i$$$ from $$$1$$$ to $$$n$$$, such that there are at least $$$x$$$ monsters left in the horde with number $$$i$$$. Then, you use an ultimate attack and kill $$$x$$$ monsters from the horde with number $$$i$$$. After that, $$$x$$$ is reset to zero.

Your task is to destroy all of the monsters, meaning that there should be no monsters left in any of the hordes. Smilo wants to win as quickly as possible, so he wants to the minimum number of attacks required to win the game.

Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The descriptions of the test cases follow.

The first line of each input data set contains a single integer $$$n$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$) — the number of hordes of monsters.

The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 10^9$$$) — the number of monsters in each horde.

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

Output

For each test case, out put the minimum number of attacks required to kill all monsters.

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

In the first test case, we can use an attack of the first type on the $$$1$$$-st, $$$3$$$-rd and $$$4$$$-th hordes, and then use an attack of the second type to finish off the $$$2$$$-nd horde. We need $$$4$$$ attacks in total.

In the second test case, we can use an attack of the first type on the $$$1$$$-st and $$$3$$$-rd hordes, then use an attack of the second type to finish off the $$$2$$$-nd horde, and then use an attack of the first type on the $$$4$$$-th horde. We need $$$4$$$ attacks in total.

In the fourth test case, we can use an attack of the first type once on the $$$1$$$-st horde, twice on the $$$2$$$-nd horde, and then use an attack of the second type on the $$$2$$$-nd horde, and finally use an attack of the first type to finish off the $$$2$$$-nd horde. We need $$$5$$$ attacks in total.

D. Suspicious logarithms
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Let $$$f$$$($$$x$$$) be the floor of the binary logarithm of $$$x$$$. In other words, $$$f$$$($$$x$$$) is largest non-negative integer $$$y$$$, such that $$$2^y$$$ does not exceed $$$x$$$.

Let $$$g$$$($$$x$$$) be the floor of the logarithm of $$$x$$$ with base $$$f$$$($$$x$$$). In other words, $$$g$$$($$$x$$$) is the largest non-negative integer $$$z$$$, such that $$${f(x)}^{z}$$$ does not exceed $$$x$$$.

You are given $$$q$$$ queries. The $$$i$$$-th query consists of two integers $$$l_i$$$ and $$$r_i$$$. The answer to the query is the sum of $$$g$$$($$$k$$$) across all integers $$$k$$$, such that $$$l_i \leq k \leq r_i$$$. Since the answers might be large, print them modulo $$${10^9 + 7}$$$.

Input

The first line contains a single integer $$$q$$$ — the number of queries ($$$1 \leq q \leq 10^5$$$).

The next $$$q$$$ lines each contain two integers $$$l_i$$$ and $$$r_i$$$ — the bounds of the $$$i$$$-th query ($$$4 \leq l_i \leq r_i \leq 10^{18}$$$).

Output

For each query, output the answer to the query modulo $$$10^9 + 7$$$.

Example
Input
12
4 6
4 7
4 8
4 100000
179 1000000000000000000
57 179
4 201018959
7 201018960
729 50624
728 50624
728 50625
729 50625
Output
6
8
9
348641
41949982
246
1
0
149688
149690
149694
149692
Note

The table below contains the values of the functions $$$f$$$($$$x$$$) and $$$g$$$($$$x$$$) for all $$$x$$$ such that $$$1 \leq x \leq 8$$$.

$$$x$$$$$$1$$$$$$2$$$$$$3$$$$$$4$$$$$$5$$$$$$6$$$$$$7$$$$$$8$$$
$$$f$$$$$$0$$$$$$1$$$$$$1$$$$$$2$$$$$$2$$$$$$2$$$$$$2$$$$$$3$$$
$$$g$$$$$$-$$$$$$-$$$$$$-$$$$$$2$$$$$$2$$$$$$2$$$$$$2$$$$$$1$$$

E. Brukhovich and Exams
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The boy Smilo is learning algorithms with a teacher named Brukhovich.

Over the course of the year, Brukhovich will administer $$$n$$$ exams. For each exam, its difficulty $$$a_i$$$ is known, which is a non-negative integer.

Smilo doesn't like when the greatest common divisor of the difficulties of two consecutive exams is equal to $$$1$$$. Therefore, he considers the sadness of the academic year to be the number of such pairs of exams. More formally, the sadness is the number of indices $$$i$$$ ($$$1 \leq i \leq n - 1$$$) such that $$$gcd(a_i, a_{i+1}) = 1$$$, where $$$gcd(x, y)$$$ is the greatest common divisor of integers $$$x$$$ and $$$y$$$.

Brukhovich wants to minimize the sadness of the year of Smilo. To do this, he can set the difficulty of any exam to $$$0$$$. However, Brukhovich doesn't want to make his students' lives too easy. Therefore, he will perform this action no more than $$$k$$$ times.

Help Smilo determine the minimum sadness that Brukhovich can achieve if he performs no more than $$$k$$$ operations.

As a reminder, the greatest common divisor (GCD) of two non-negative integers $$$x$$$ and $$$y$$$ is the maximum integer that is a divisor of both $$$x$$$ and $$$y$$$ and is denoted as $$$gcd(x, y)$$$. In particular, $$$gcd(x, 0) = gcd(0, x) = x$$$ for any non-negative integer $$$x$$$.

Input

The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 \leq k \leq n \leq 10^5$$$) — the total number of exams and the maximum number of exams that can be simplified, respectively.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, a_3, \ldots, a_n$$$ — the elements of array $$$a$$$, which are the difficulties of the exams ($$$0 \leq a_i \leq 10^9$$$).

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

Output

For each test case, output the minimum possible sadness that can be achieved by performing no more than $$$k$$$ operations.

Example
Input
9
5 2
1 3 5 7 9
5 2
3 5 7 9 11
8 2
17 15 10 1 1 5 14 8
5 3
1 1 1 1 1
5 5
1 1 1 1 1
19 7
1 1 2 3 4 5 5 6 6 7 8 9 10 1 1 1 2 3 1
15 6
2 1 1 1 1 2 1 1 2 1 1 1 2 1 2
5 2
1 1 1 1 2
5 2
1 0 1 0 1
Output
1
0
2
2
0
5
5
2
1
Note

In the first test case, a sadness of $$$1$$$ can be achieved. To this, you can simplify the second and fourth exams. After this, there will be only one pair of adjacent exams with a greatest common divisor (GCD) equal to one, which is the first and second exams.

In the second test case, a sadness of $$$0$$$ can be achieved by simplifying the second and fourth exams.

F. A Growing Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a rooted tree with the root at vertex $$$1$$$, initially consisting of a single vertex. Each vertex has a numerical value, initially set to $$$0$$$. There are also $$$q$$$ queries of two types:

  • The first type: add a child vertex with the number $$$sz + 1$$$ to vertex $$$v$$$, where $$$sz$$$ is the current size of the tree. The numerical value of the new vertex will be $$$0$$$.
  • The second type: add $$$x$$$ to the numerical values of all vertices in the subtree of vertex $$$v$$$.

After all queries, output the numerical value of all of the vertices in the final tree.

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 10^4$$$) — the number of test cases. The descriptions of the test cases follow.

The first line of each test case contains a single integer $$$q$$$ ($$$1 \leq q \leq 5 \cdot 10^5$$$) — the number of queries.

The following $$$q$$$ lines can fall into two cases:

  • The first type of query: The $$$i$$$-th line contains two integers $$$t_i$$$ ($$$t_i = 1$$$), $$$v_i$$$. You need to add a child with the number $$$sz + 1$$$ to vertex $$$v_i$$$, where $$$sz$$$ is the current size of the tree. It is guaranteed that $$$1 \leq v_i \leq sz$$$.
  • The second type of query: The $$$i$$$-th line contains three integers $$$t_i$$$ ($$$t_i = 2$$$), $$$v_i$$$, $$$x_i$$$ ($$$-10^9 \leq x_i \leq 10^9$$$). You need to add $$$x_i$$$ to all numerical values of vertices in the subtree of $$$v_i$$$. It is guaranteed that $$$1 \leq v_i \leq sz$$$, where $$$sz$$$ is the current size of the tree.

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

Output

For each test case, output the numerical value of each vertex of the final tree after all queries have been performed.

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

In the first case, the final tree with the assigned numerical values will look like this:

The final tree with the assigned numerical values