Codeforces Round 838 (Div. 2)
A. Divide and Conquer
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

An array $$$b$$$ is good if the sum of elements of $$$b$$$ is even.

You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. In one operation, you can select an index $$$i$$$ and change $$$a_i := \lfloor \frac{a_i}{2} \rfloor$$$. $$$^\dagger$$$

Find the minimum number of operations (possibly $$$0$$$) needed to make $$$a$$$ good. It can be proven that it is always possible to make $$$a$$$ good.

$$$^\dagger$$$ $$$\lfloor x \rfloor$$$ denotes the floor function — the largest integer less than or equal to $$$x$$$. For example, $$$\lfloor 2.7 \rfloor = 2$$$, $$$\lfloor \pi \rfloor = 3$$$ and $$$\lfloor 5 \rfloor =5$$$.

Input

Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 1000$$$) — 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 50$$$) — the length of the array $$$a$$$.

The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^6$$$) — representing the array $$$a$$$.

Do note that the sum of $$$n$$$ over all test cases is not bounded.

Output

For each test case, output the minimum number of operations needed to make $$$a$$$ good.

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

In the first test case, array $$$a$$$ is already good.

In the second test case, we can perform on index $$$2$$$ twice. After the first operation, array $$$a$$$ becomes $$$[7,2]$$$. After performing on index $$$2$$$ again, $$$a$$$ becomes $$$[7,1]$$$, which is good. It can be proved that it is not possible to make $$$a$$$ good in less number of operations.

In the third test case, $$$a$$$ becomes $$$[0,2,4]$$$ if we perform the operation on index $$$1$$$ once. As $$$[0,2,4]$$$ is good, answer is $$$1$$$.

In the fourth test case, we need to perform the operation on index $$$1$$$ four times. After all operations, $$$a$$$ becomes $$$[0]$$$. It can be proved that it is not possible to make $$$a$$$ good in less number of operations.

B. Make Array Good
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

An array $$$b$$$ of $$$m$$$ positive integers is good if for all pairs $$$i$$$ and $$$j$$$ ($$$1 \leq i,j \leq m$$$), $$$\max(b_i,b_j)$$$ is divisible by $$$\min(b_i,b_j)$$$.

You are given an array $$$a$$$ of $$$n$$$ positive integers. You can perform the following operation:

  • Select an index $$$i$$$ ($$$1 \leq i \leq n$$$) and an integer $$$x$$$ ($$$0 \leq x \leq a_i$$$) and add $$$x$$$ to $$$a_i$$$, in other words, $$$a_i := a_i+x$$$.
  • After this operation, $$$a_i \leq 10^{18}$$$ should be satisfied.

You have to construct a sequence of at most $$$n$$$ operations that will make $$$a$$$ good. It can be proven that under the constraints of the problem, such a sequence of operations always exists.

Input

Each test contains multiple test cases. 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 10^5$$$) — the length of the array $$$a$$$.

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

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

Output

For each test, output a single integer $$$p$$$ ($$$0 \leq p \leq n$$$) — denoting the number of operations in your solution.

In each of the following $$$p$$$ lines, output two space-separated integers — $$$i$$$ and $$$x$$$.

You do not need to minimize the number of operations. It can be proven that a solution always exists.

Example
Input
4
4
2 3 5 5
2
4 8
5
3 4 343 5 6
3
31 5 17
Output
4
1 2
1 1
2 2
3 0
0
5
1 3
1 4
2 1
5 4
3 7
3
1 29
2 5
3 3
Note

In the first test case, array $$$a$$$ becomes $$$[5,5,5,5]$$$ after the operations. It is easy to see that $$$[5,5,5,5]$$$ is good.

In the second test case, array $$$a$$$ is already good.

In the third test case, after performing the operations, array $$$a$$$ becomes $$$[10,5,350,5,10]$$$, which is good.

In the fourth test case, after performing the operations, array $$$a$$$ becomes $$$[60,10,20]$$$, which is good.

C. Binary Strings are Fun
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A binary string$$$^\dagger$$$ $$$b$$$ of odd length $$$m$$$ is good if $$$b_i$$$ is the median$$$^\ddagger$$$ of $$$b[1,i]^\S$$$ for all odd indices $$$i$$$ ($$$1 \leq i \leq m$$$).

For a binary string $$$a$$$ of length $$$k$$$, a binary string $$$b$$$ of length $$$2k-1$$$ is an extension of $$$a$$$ if $$$b_{2i-1}=a_i$$$ for all $$$i$$$ such that $$$1 \leq i \leq k$$$. For example, 1001011 and 1101001 are extensions of the string 1001. String $$$x=$$$1011011 is not an extension of string $$$y=$$$1001 because $$$x_3 \neq y_2$$$. Note that there are $$$2^{k-1}$$$ different extensions of $$$a$$$.

You are given a binary string $$$s$$$ of length $$$n$$$. Find the sum of the number of good extensions over all prefixes of $$$s$$$. In other words, find $$$\sum_{i=1}^{n} f(s[1,i])$$$, where $$$f(x)$$$ gives number of good extensions of string $$$x$$$. Since the answer can be quite large, you only need to find it modulo $$$998\,244\,353$$$.

$$$^\dagger$$$ A binary string is a string whose elements are either $$$\mathtt{0}$$$ or $$$\mathtt{1}$$$.

$$$^\ddagger$$$ For a binary string $$$a$$$ of length $$$2m-1$$$, the median of $$$a$$$ is the (unique) element that occurs at least $$$m$$$ times in $$$a$$$.

$$$^\S$$$ $$$a[l,r]$$$ denotes the string of length $$$r-l+1$$$ which is formed by the concatenation of $$$a_l,a_{l+1},\ldots,a_r$$$ in that order.

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 2 \cdot 10^5$$$), where $$$n$$$ is the length of the binary string $$$s$$$.

The second line of each test case contains the binary string $$$s$$$ of length $$$n$$$.

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

Output

For each test case, print the answer modulo $$$998\,244\,353$$$.

Example
Input
6
1
1
1
0
2
11
3
010
9
101101111
37
1011011111011010000011011111111011111
Output
1
1
3
3
21
365
Note

In the first and second test cases, $$$f(s[1,1])=1$$$.

In the third test case, the answer is $$$f(s[1,1])+f(s[1,2])=1+2=3$$$.

In the fourth test case, the answer is $$$f(s[1,1])+f(s[1,2])+f(s[1,3])=1+1+1=3$$$.

$$$f(\mathtt{11})=2$$$ because two good extensions are possible: 101 and 111.

$$$f(\mathtt{01})=1$$$ because only one good extension is possible: 011.

D. GCD Queries
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

This is an interactive problem.

There is a secret permutation $$$p$$$ of $$$[0,1,2,\ldots,n-1]$$$. Your task is to find $$$2$$$ indices $$$x$$$ and $$$y$$$ ($$$1 \leq x, y \leq n$$$, possibly $$$x=y$$$) such that $$$p_x=0$$$ or $$$p_y=0$$$. In order to find it, you are allowed to ask at most $$$2n$$$ queries.

In one query, you give two integers $$$i$$$ and $$$j$$$ ($$$1 \leq i, j \leq n$$$, $$$i \neq j$$$) and receive the value of $$$\gcd(p_i,p_j)^\dagger$$$.

Note that the permutation $$$p$$$ is fixed before any queries are made and does not depend on the queries.

$$$^\dagger$$$ $$$\gcd(x, y)$$$ denotes the greatest common divisor (GCD) of integers $$$x$$$ and $$$y$$$. Note that $$$\gcd(x,0)=\gcd(0,x)=x$$$ for all positive integers $$$x$$$.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^4$$$).

After reading the integer $$$n$$$ for each test case, you should begin the interaction.

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

Interaction

The interaction for each test case begins by reading the integer $$$n$$$.

To make a query, output "? $$$i$$$ $$$j$$$" ($$$1 \leq i, j \leq n$$$, $$$i \neq j$$$) without quotes. Afterwards, you should read a single integer — the answer to your query $$$\gcd(p_i,p_j)$$$. You can make at most $$$2n$$$ such queries in each test case.

If you want to print the answer, output "! $$$x$$$ $$$y$$$" ($$$1 \leq x, y \leq n$$$) without quotes. After doing that, read $$$1$$$ or $$$-1$$$. If $$$p_x=0$$$ or $$$p_y=0$$$, you'll receive $$$1$$$, otherwise you'll receive $$$-1$$$. If you receive $$$-1$$$, your program must terminate immediately to receive a Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

If you receive the integer $$$-1$$$ instead of an answer or a valid value of $$$n$$$, it means your program has made an invalid query, has exceeded the limit of queries, or has given an incorrect answer on the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.

After printing a query or the answer, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

  • fflush(stdout) or cout.flush() in C++;
  • System.out.flush() in Java;
  • flush(output) in Pascal;
  • stdout.flush() in Python;
  • see documentation for other languages.

Hacks

To hack, use the following format.

The first line should contain a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$).

The first line of each test case should contain a single integer $$$n$$$ ($$$2 \leq n \leq 2 \cdot 10^4$$$).

The second line of each test case should contain $$$n$$$ space separated integers $$$p_1,p_2,\ldots,p_n$$$. $$$p$$$ should be a permutation of $$$[0,1,2,\ldots,n-1]$$$.

The sum of $$$n$$$ should not exceed $$$2 \cdot 10^4$$$.

Example
Input
2
2

1

1
5

2

4

1
Output


? 1 2

! 1 2


? 1 2

? 2 3

! 3 3

Note

In the first test, the interaction proceeds as follows.

SolutionJuryExplanation
$$$\texttt{2}$$$There are 2 test cases.
$$$\texttt{2}$$$In the first test case, the hidden permutation is $$$[1,0]$$$, with length $$$2$$$.
$$$\texttt{? 1 2}$$$$$$\texttt{1}$$$The solution requests $$$\gcd(p_1,p_2)$$$, and the jury responds with $$$1$$$.
$$$\texttt{! 1 2}$$$$$$\texttt{1}$$$The solution knows that either $$$p_1=0$$$ or $$$p_2=0$$$, and prints the answer. Since the output is correct, the jury responds with $$$1$$$ and continues to the next test case.
$$$\texttt{5}$$$In the second test case, the hidden permutation is $$$[2,4,0,1,3]$$$, with length $$$5$$$.
$$$\texttt{? 1 2}$$$$$$\texttt{2}$$$The solution requests $$$\gcd(p_1,p_2)$$$, and the jury responds with $$$2$$$.
$$$\texttt{? 2 3}$$$$$$\texttt{4}$$$The solution requests $$$\gcd(p_2,p_3)$$$, and the jury responds with $$$4$$$.
$$$\texttt{! 3 3}$$$$$$\texttt{1}$$$The solution has somehow determined that $$$p_3=0$$$, and prints the answer. Since the output is correct, the jury responds with $$$1$$$.

Note that the empty lines in the example input and output are for the sake of clarity, and do not occur in the real interaction.

After each test case, make sure to read $$$1$$$ or $$$-1$$$.

E. Tree Sum
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Let us call an edge-weighted tree with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$ good if the weight of each edge is either $$$1$$$ or $$$-1$$$ and for each vertex $$$i$$$, the product of the edge weights of all edges having $$$i$$$ as one endpoint is $$$-1$$$.

You are given a positive integer $$$n$$$. There are $$$n^{n-2} \cdot 2^{n-1}$$$ distinct$$$^\dagger$$$ edge-weighted trees with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$ such that each edge is either $$$1$$$ or $$$-1$$$. Your task is to find the sum of $$$d(1,n)^\ddagger$$$ of all such trees that are good. Since the answer can be quite large, you only need to find it modulo $$$998\,244\,353$$$.

$$$^\dagger$$$ Two trees are considered to be distinct if either:

  • there exists two vertices such that there is an edge between them in one of the trees, and not in the other.
  • there exists two vertices such that there is an edge between them in both trees but the weight of the edge between them in one tree is different from the one in the other tree.

Note that by Cayley's formula, the number of trees on $$$n$$$ labeled vertices is $$$n^{n-2}$$$. Since we have $$$n-1$$$ edges, there are $$$2^{n-1}$$$ possible assignment of weights(weight can either be $$$1$$$ or $$$-1$$$). That is why total number of distinct edge-weighted tree is $$$n^{n-2} \cdot 2^{n-1}$$$.

$$$^\ddagger$$$ $$$d(u,v)$$$ denotes the sum of the weight of all edges on the unique simple path from $$$u$$$ to $$$v$$$.

Input

The first and only line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 5 \cdot 10^5$$$).

Output

The only line of output should contain a single integer, the required answer, modulo $$$998\,244\,353$$$.

Examples
Input
2
Output
998244352
Input
1
Output
0
Input
4
Output
998244343
Input
10
Output
948359297
Input
43434
Output
86232114
Note

In the first test case, there is only $$$1$$$ distinct good tree. The value of $$$d(1,2)$$$ for that tree is $$$-1$$$, which is $$$998\,244\,352$$$ under modulo $$$998\,244\,353$$$.

In the second test case, the value of $$$d(1,1)$$$ for any tree is $$$0$$$, so the answer is $$$0$$$.

In the third test case, there are $$$16$$$ distinct good trees. The value of $$$d(1,4)$$$ is:

  • $$$-2$$$ for $$$2$$$ trees;
  • $$$-1$$$ for $$$8$$$ trees;
  • $$$0$$$ for $$$4$$$ trees;
  • $$$1$$$ for $$$2$$$ trees.

The sum of $$$d(1,4)$$$ over all trees is $$$2 \cdot (-2) + 8 \cdot (-1) + 4 \cdot (0) + 2 \cdot (1) = -10$$$, which is $$$998\,244\,343$$$ under modulo $$$998\,244\,353$$$.

F. Good Pairs
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array $$$a$$$ consisting of $$$n$$$ integers and an integer $$$k$$$.

A pair $$$(l,r)$$$ is good if there exists a sequence of indices $$$i_1, i_2, \dots, i_m$$$ such that

  • $$$i_1=l$$$ and $$$i_m=r$$$;
  • $$$i_j \lt i_{j+1}$$$ for all $$$1 \leq j \lt m$$$; and
  • $$$|a_{i_j}-a_{i_{j+1}}| \leq k$$$ for all $$$1 \leq j \lt m$$$.

Find the number of pairs $$$(l,r)$$$ ($$$1 \leq l \leq r \leq n$$$) that are good.

Input

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

The first line of each test case contains two space-separated integers $$$n$$$ and $$$k$$$ ($$$1 \leq n \leq 5 \cdot 10^5$$$; $$$0 \leq k \leq 10^5$$$) — the length of the array $$$a$$$ and the integer $$$k$$$.

The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq 10^5$$$) — representing the array $$$a$$$.

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

Output

For each test case, print the number of good pairs.

Example
Input
4
3 0
1 1 1
4 2
4 8 6 8
6 4
7 2 5 8 3 8
20 23
110 57 98 14 20 1 60 82 108 37 82 73 8 46 38 35 106 115 58 112
Output
6
9
18
92
Note

In the first test case, good pairs are $$$(1,1)$$$, $$$(1,2)$$$, $$$(1,3)$$$, $$$(2,2)$$$, $$$(2,3)$$$, and $$$(3,3)$$$.

In the second test case, good pairs are $$$(1,1)$$$, $$$(1,3)$$$, $$$(1,4)$$$, $$$(2,2)$$$, $$$(2,3)$$$, $$$(2,4)$$$, $$$(3,3)$$$, $$$(3,4)$$$ and $$$(4,4)$$$. Pair $$$(1,4)$$$ is good because there exists a sequence of indices $$$1, 3, 4$$$ which satisfy the given conditions.

G. Unequal Adjacent Elements
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given an array $$$a$$$ consisting of $$$n$$$ positive integers.

Find any permutation $$$p$$$ of $$$[1,2,\dots,n]$$$ such that:

  • $$$p_{i-2} \lt p_i$$$ for all $$$i$$$, where $$$3 \leq i \leq n$$$, and
  • $$$a_{p_{i-1}} \neq a_{p_i}$$$ for all $$$i$$$, where $$$2 \leq i \leq n$$$.

Or report that no such permutation exists.

Input

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

The first line of each test case contains a single integer $$$n$$$ ($$$3 \leq n \leq 3 \cdot 10^5$$$) — the length of the array $$$a$$$.

The second line of each test case contains $$$n$$$ space-separated integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1 \leq a_i \leq n$$$) — representing the array $$$a$$$.

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

Output

For each test case, output "NO" if no such permutation exists, otherwise output "YES" in the first line and print the permutation $$$p$$$ in the next line.

In case there are multiple permutations, print any one of them.

You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).

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

In the first test case, $$$p=[1,2,3]$$$ is the only permutation of $$$[1,2,3]$$$ that satisfy the given constraints.

In the second test case, $$$[1,3,2,4]$$$, $$$[2,1,4,3]$$$ and some other permutations are also acceptable.

In the third test case, it can be proved that there does not exist any permutation of $$$[1,2,3]$$$ satisfying the given constraints.