CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!)
A. Indirect Sort
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a permutation $$$a_1, a_2, \ldots, a_n$$$ of size $$$n$$$, where each integer from $$$1$$$ to $$$n$$$ appears exactly once.

You can do the following operation any number of times (possibly, zero):

  • Choose any three indices $$$i, j, k$$$ ($$$1 \le i \lt j \lt k \le n$$$).
  • If $$$a_i \gt a_k$$$, replace $$$a_i$$$ with $$$a_i + a_j$$$. Otherwise, swap $$$a_j$$$ and $$$a_k$$$.

Determine whether you can make the array $$$a$$$ sorted in non-descending order.

Input

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

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

The second line contains $$$n$$$ integers $$$a_1,a_2,\dots,a_n$$$ ($$$1 \le a_i \le n$$$, $$$a_i \neq a_j$$$ if $$$i \neq j$$$) — the elements of the array $$$a$$$.

Output

For each test case, output "Yes" (without quotes) if the array can be sorted in non-descending order, and "No" (without quotes) otherwise.

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

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

In the first test case, $$$[1,2,3]$$$ is already sorted in non-descending order.

In the second test case, we can choose $$$i = 1,j = 2,k = 3$$$. Since $$$a_1 \le a_3$$$, swap $$$a_2$$$ and $$$a_3$$$, the array then becomes $$$[1,2,3]$$$, which is sorted in non-descending order.

In the seventh test case, we can do the following operations successively:

  • Choose $$$i = 5,j = 6,k = 7$$$. Since $$$a_5 \le a_7$$$, swap $$$a_6$$$ and $$$a_7$$$, the array then becomes $$$[1,2,6,7,4,5,3]$$$.
  • Choose $$$i = 5,j = 6,k = 7$$$. Since $$$a_5 \gt a_7$$$, replace $$$a_5$$$ with $$$a_5+a_6=9$$$, the array then becomes $$$[1,2,6,7,9,5,3]$$$.
  • Choose $$$i = 2,j = 5,k = 7$$$. Since $$$a_2 \le a_7$$$, swap $$$a_5$$$ and $$$a_7$$$, the array then becomes $$$[1,2,6,7,3,5,9]$$$.
  • Choose $$$i = 2,j = 4,k = 6$$$. Since $$$a_2 \le a_6$$$, swap $$$a_4$$$ and $$$a_6$$$, the array then becomes $$$[1,2,6,5,3,7,9]$$$.
  • Choose $$$i = 1,j = 3,k = 5$$$. Since $$$a_1 \le a_5$$$, swap $$$a_3$$$ and $$$a_5$$$, the array then becomes $$$[1,2,3,5,6,7,9]$$$, which is sorted in non-descending order.

In the third, the fourth, the fifth and the sixth test cases, it can be shown that the array cannot be sorted in non-descending order.

B. Maximum Substring
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A binary string is a string consisting only of the characters 0 and 1. You are given a binary string $$$s$$$.

For some non-empty substring$$$^\dagger$$$ $$$t$$$ of string $$$s$$$ containing $$$x$$$ characters 0 and $$$y$$$ characters 1, define its cost as:

  • $$$x \cdot y$$$, if $$$x \gt 0$$$ and $$$y \gt 0$$$;
  • $$$x^2$$$, if $$$x \gt 0$$$ and $$$y = 0$$$;
  • $$$y^2$$$, if $$$x = 0$$$ and $$$y \gt 0$$$.

Given a binary string $$$s$$$ of length $$$n$$$, find the maximum cost across all its non-empty substrings.

$$$^\dagger$$$ A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

Input

Each test consists of 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 test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$.

The second line of each test case contains a 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 a single integer — the maximum cost across all substrings.

Example
Input
6
5
11100
7
1100110
6
011110
7
1001010
4
1000
1
0
Output
9
12
16
12
9
1
Note

In the first test case, we can take a substring $$$111$$$. It contains $$$3$$$ characters 1 and $$$0$$$ characters 0. So $$$a = 3$$$, $$$b = 0$$$ and its cost is $$$3^2 = 9$$$.

In the second test case, we can take the whole string. It contains $$$4$$$ characters 1 and $$$3$$$ characters 0. So $$$a = 4$$$, $$$b = 3$$$ and its cost is $$$4 \cdot 3 = 12$$$.

In the third test case, we can can take a substring $$$1111$$$ and its cost is $$$4^2 = 16$$$.

In the fourth test case, we can take the whole string and cost is $$$4 \cdot 3 = 12$$$.

In the fifth test case, we can take a substring $$$000$$$ and its cost is $$$3 \cdot 3 = 9$$$.

In the sixth test case, we can only take the substring $$$0$$$ and its cost is $$$1 \cdot 1 = 1$$$.

C. Complementary XOR
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You have two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$. You would like to make all the elements of both strings equal to $$$0$$$. Unfortunately, you can modify the contents of these strings using only the following operation:

  • You choose two indices $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$);
  • For every $$$i$$$ that respects $$$l \le i \le r$$$, change $$$a_i$$$ to the opposite. That is, $$$a_i := 1 - a_i$$$;
  • For every $$$i$$$ that respects either $$$1 \le i \lt l$$$ or $$$r \lt i \le n$$$, change $$$b_i$$$ to the opposite. That is, $$$b_i := 1 - b_i$$$.

Your task is to determine if this is possible, and if it is, to find such an appropriate chain of operations. The number of operations should not exceed $$$n + 5$$$. It can be proven that if such chain of operations exists, one exists with at most $$$n + 5$$$ operations.

Input

Each test consists of 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 test cases follows.

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

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

The third line of each test case contains a binary string $$$b$$$, consisting only of characters 0 and 1, of length $$$n$$$.

It is guaranteed that sum of $$$n$$$ over all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output

For each testcase, print first "YES" if it's possible to make all the elements of both strings equal to $$$0$$$. Otherwise, print "NO". If the answer is "YES", on the next line print a single integer $$$k$$$ ($$$0 \le k \le n + 5$$$) — the number of operations. Then $$$k$$$ lines follows, each contains two integers $$$l$$$ and $$$r$$$ ($$$1 \le l \le r \le n$$$) — the description of the operation.

If there are several correct answers, print any of them.

Example
Input
5
3
010
101
2
11
10
4
1000
0011
2
10
10
3
111
111
Output
YES
1
2 2
NO
NO
YES
2
1 2
2 2
YES
2
1 1
2 3
Note

In the first test case, we can perform one operation with $$$l = 2$$$ and $$$r = 2$$$. So $$$a_2 := 1 - 1 = 0$$$ and string $$$a$$$ became equal to 000. $$$b_1 := 1 - 1 = 0$$$, $$$b_3 := 1 - 1 = 0$$$ and string $$$b$$$ became equal to 000.

In the second and in the third test cases, it can be proven that it's impossible to make all elements of both strings equal to $$$0$$$.

In the fourth test case, we can perform an operation with $$$l = 1$$$ and $$$r = 2$$$, then string $$$a$$$ became equal to 01, and string $$$b$$$ doesn't change. Then we perform an operation with $$$l = 2$$$ and $$$r = 2$$$, then $$$a_2 := 1 - 1 = 0$$$ and $$$b_1 = 1 - 1 = 0$$$. So both of string $$$a$$$ and $$$b$$$ became equal to 00.

In the fifth test case, we can perform an operation with $$$l = 1$$$ and $$$r = 1$$$. Then string $$$a$$$ became equal to 011 and string $$$b$$$ became equal to 100. Then we can perform an operation with $$$l = 2$$$ and $$$r = 3$$$, so both of string $$$a$$$ and $$$b$$$ became equal to 000.

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

You are given two integers $$$n$$$ and $$$m$$$ and an array $$$a$$$ of $$$n$$$ integers. For each $$$1 \le i \le n$$$ it holds that $$$1 \le a_i \le m$$$.

Your task is to count the number of different arrays $$$b$$$ of length $$$n$$$ such that:

  • $$$1 \le b_i \le m$$$ for each $$$1 \le i \le n$$$, and
  • $$$\gcd(b_1,b_2,b_3,...,b_i) = a_i$$$ for each $$$1 \le i \le n$$$.

Here $$$\gcd(a_1,a_2,\dots,a_i)$$$ denotes the greatest common divisor (GCD) of integers $$$a_1,a_2,\ldots,a_i$$$.

Since this number can be too large, print it modulo $$$998\,244\,353$$$.

Input

Each test consist of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \le t \le 100$$$) — 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 \le n \le 2 \cdot 10^5$$$, $$$1 \le m \le 10^9$$$) — the length of the array $$$a$$$ and the maximum possible value of the element.

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

It is guaranteed that the sum of $$$n$$$ across all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output

For each test case, print a single integer — the number of different arrays satisfying the conditions above. Since this number can be large, print it modulo $$$998\,244\,353$$$.

Example
Input
5
3 5
4 2 1
2 1
1 1
5 50
2 3 5 2 3
4 1000000000
60 30 1 1
2 1000000000
1000000000 2
Output
3
1
0
595458194
200000000
Note

In the first test case, the possible arrays $$$b$$$ are:

  • $$$[4,2,1]$$$;
  • $$$[4,2,3]$$$;
  • $$$[4,2,5]$$$.

In the second test case, the only array satisfying the demands is $$$[1,1]$$$.

In the third test case, it can be proven no such array exists.

E. Bracket Cost
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Daemon Targaryen decided to stop looking like a Metin2 character. He turned himself into the most beautiful thing, a bracket sequence.

For a bracket sequence, we can do two kind of operations:

  • Select one of its substrings$$$^\dagger$$$ and cyclic shift it to the right. For example, after a cyclic shift to the right, "(())" will become ")(()";
  • Insert any bracket, opening '(' or closing ')', wherever you want in the sequence.

We define the cost of a bracket sequence as the minimum number of such operations to make it balanced$$$^\ddagger$$$.

Given a bracket sequence $$$s$$$ of length $$$n$$$, find the sum of costs across all its $$$\frac{n(n+1)}{2}$$$ non-empty substrings. Note that for each substring we calculate the cost independently.

$$$^\dagger$$$ A string $$$a$$$ is a substring of a string $$$b$$$ if $$$a$$$ can be obtained from $$$b$$$ by deletion of several (possibly, zero or all) characters from the beginning and several (possibly, zero or all) characters from the end.

$$$^\ddagger$$$ A sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters $$$+$$$ and $$$1$$$. For example, sequences "(())()", "()", and "(()(()))" are balanced, while ")(", "(()", and "(()))(" are not.

Input

Each test consists of 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 test cases follows.

The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the bracket sequence.

The second line of each test case contains a string $$$s$$$, consisting only of characters '(' and ')', of length $$$n$$$ — the bracket sequence.

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

Output

For each test case, print a single integer — the sum of costs of all substrings of $$$s$$$.

Example
Input
5
1
)
4
)()(
3
())
5
(((((
10
)(())))())
Output
1
9
6
35
112
Note

In the first test case, there is the only substring ")". Its cost is $$$1$$$ because we can insert '(' to the beginning of this substring and get a string "()", that is a balanced string.

In the second test case, the cost of each substring of length one is $$$1$$$. The cost of a substring ")(" is $$$1$$$ because we can cyclically shift it to right and get a string "()". The cost of strings ")()" and "()(" is $$$1$$$ because its enough to insert one bracket to each of them. The cost of substring ")()(" is $$$1$$$ because we can cyclically shift it to right and get a string "()()". So there are $$$4 + 2 + 2 + 1 = 9$$$ substring of cost $$$1$$$ and $$$1$$$ substring of cost $$$0$$$. So the sum of the costs is $$$9$$$.

In the third test case,

  • "(", the cost is $$$1$$$;
  • "()", the cost is $$$0$$$;
  • "())", the cost is $$$1$$$;
  • ")", the cost is $$$1$$$;
  • "))", the cost is $$$2$$$;
  • ")", the cost is $$$1$$$.

So the sum of the costs is $$$6$$$.

F. Majority
time limit per test
2 seconds
memory limit per test
1024 megabytes
input
standard input
output
standard output

Everyone was happy coding, until suddenly a power shortage happened and the best competitive programming site went down. Fortunately, a system administrator bought some new equipment recently, including some UPSs. Thus there are some servers that are still online, but we need all of them to be working in order to keep the round rated.

Imagine the servers being a binary string $$$s$$$ of length $$$n$$$. If the $$$i$$$-th server is online, then $$$s_i = 1$$$, and $$$s_i = 0$$$ otherwise.

A system administrator can do the following operation called electricity spread, that consists of the following phases:

  • Select two servers at positions $$$1 \le i \lt j \le n$$$ such that both are online (i.e. $$$s_i=s_j=1$$$). The spread starts only from online servers.
  • Check if we have enough power to make the spread. We consider having enough power if the number of turned on servers in range $$$[i, j]$$$ is at least the number of turned off servers in range $$$[i, j]$$$. More formally, check whether $$$2 \cdot (s_i + s_{i+1} + \ldots + s_j) \ge j - i + 1$$$.
  • If the check is positive, turn on all the offline servers in range $$$[i, j]$$$. More formally, make $$$s_k := 1$$$ for all $$$k$$$ from $$$i$$$ to $$$j$$$.

We call a binary string $$$s$$$ of length $$$n$$$ rated if we can turn on all servers (i.e. make $$$s_i = 1$$$ for $$$1 \le i \le n$$$) using the electricity spread operation any number of times (possibly, $$$0$$$). Your task is to find the number of rated strings of length $$$n$$$ modulo $$$m$$$.

Input

The first and only line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 5000$$$, $$$10 \le m \le 10^9$$$) — the length of the string and the required module.

Output

Print a single integer — the number of rated binary strings of length $$$n$$$. Since this number can be large, print it modulo $$$m$$$.

Examples
Input
2 100
Output
1
Input
3 10
Output
2
Input
4 3271890
Output
4
Input
17 123456
Output
32347
Note

In the first example, the only rated string is 11. So the answer is $$$1$$$.

In the second example, the rated strings are:

  • 111;
  • 101, because we can perform an operation with $$$i = 1$$$ and $$$j = 3$$$.
So the answer is $$$2$$$.

In the third sample, the rated strings are:

  • 1001;
  • 1111;
  • 1011;
  • 1101.
So the answer is $$$4$$$.

G. Doping
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

We call an array $$$a$$$ of length $$$n$$$ fancy if for each $$$1 \lt i \le n$$$ it holds that $$$a_i = a_{i-1} + 1$$$.

Let's call $$$f(p)$$$ applied to a permutation$$$^\dagger$$$ of length $$$n$$$ as the minimum number of subarrays it can be partitioned such that each one of them is fancy. For example $$$f([1,2,3]) = 1$$$, while $$$f([3,1,2]) = 2$$$ and $$$f([3,2,1]) = 3$$$.

Given $$$n$$$ and a permutation $$$p$$$ of length $$$n$$$, we define a permutation $$$p'$$$ of length $$$n$$$ to be $$$k$$$-special if and only if:

  • $$$p'$$$ is lexicographically smaller$$$^\ddagger$$$ than $$$p$$$, and
  • $$$f(p') = k$$$.

Your task is to count for each $$$1 \le k \le n$$$ the number of $$$k$$$-special permutations modulo $$$m$$$.

$$$^\dagger$$$ A permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array) and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array).

$$$^\ddagger$$$ A permutation $$$a$$$ of length $$$n$$$ is lexicographically smaller than a permutation $$$b$$$ of length $$$n$$$ if and only if the following holds: in the first position where $$$a$$$ and $$$b$$$ differ, the permutation $$$a$$$ has a smaller element than the corresponding element in $$$b$$$.

Input

The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n \le 2000$$$, $$$10 \le m \le 10^9$$$) — the length of the permutation and the required modulo.

The second line contains $$$n$$$ distinct integers $$$p_1, p_2, \ldots, p_n$$$ ($$$1 \le p_i \le n$$$) — the permutation $$$p$$$.

Output

Print $$$n$$$ integers, where the $$$k$$$-th integer is the number of $$$k$$$-special permutations modulo $$$m$$$.

Examples
Input
4 666012
1 3 4 2
Output
1 0 1 1 
Input
3 10
3 2 1
Output
1 2 2 
Input
7 1000000000
7 2 1 3 5 4 6
Output
1 6 40 201 705 1635 1854 
Input
10 11
10 9 8 7 6 5 4 3 2 1
Output
1 9 9 0 1 5 5 0 1 0 
Note

In the first example, the permutations that are lexicographically smaller than $$$[1,3,4,2]$$$ are:

  • $$$[1,2,3,4]$$$, $$$f([1,2,3,4])=1$$$;
  • $$$[1,2,4,3]$$$, $$$f([1,2,4,3])=3$$$;
  • $$$[1,3,2,4]$$$, $$$f([1,3,2,4])=4$$$.

Thus our answer is $$$[1,0,1,1]$$$.

In the second example, the permutations that are lexicographically smaller than $$$[3,2,1]$$$ are:

  • $$$[1,2,3]$$$, $$$f([1,2,3])=1$$$;
  • $$$[1,3,2]$$$, $$$f([1,3,2])=3$$$;
  • $$$[2,1,3]$$$, $$$f([2,1,3])=3$$$;
  • $$$[2,3,1]$$$, $$$f([2,3,1])=2$$$;
  • $$$[3,1,2]$$$, $$$f([3,1,2])=2$$$.

Thus our answer is $$$[1,2,2]$$$.

H. BinaryStringForces
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

You are given a binary string $$$s$$$ of length $$$n$$$. We define a maximal substring as a substring that cannot be extended while keeping all elements equal. For example, in the string $$$11000111$$$ there are three maximal substrings: $$$11$$$, $$$000$$$ and $$$111$$$.

In one operation, you can select two maximal adjacent substrings. Since they are maximal and adjacent, it's easy to see their elements must have different values. Let $$$a$$$ be the length of the sequence of ones and $$$b$$$ be the length of the sequence of zeros. Then do the following:

  • If $$$a \ge b$$$, then replace $$$b$$$ selected zeros with $$$b$$$ ones.
  • If $$$a \lt b$$$, then replace $$$a$$$ selected ones with $$$a$$$ zeros.

As an example, for $$$1110000$$$ we make it $$$0000000$$$, for $$$0011$$$ we make it $$$1111$$$. We call a string being good if it can be turned into $$$1111...1111$$$ using the aforementioned operation any number of times (possibly, zero). Find the number of good substrings among all $$$\frac{n(n+1)}{2}$$$ non-empty substrings of $$$s$$$.

Input

Each test consists of 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 test cases follows.

The first line of each test case contains $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the length of the string $$$s$$$.

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

It is guaranteed that sum of $$$n$$$ across all test cases doesn't exceed $$$2 \cdot 10^5$$$.

Output

For each test case, print a single integer — the number of good substrings.

Example
Input
4
6
100011
3
101
5
11111
6
010101
Output
8
5
15
18
Note

Let's define a substring from index $$$l$$$ to index $$$r$$$ as $$$[l, r]$$$.

For the first test case, the good substrings are:

  • $$$[1,1]$$$,
  • $$$[1,2]$$$,
  • $$$[3,6]$$$,
  • $$$[4,5]$$$,
  • $$$[4,6]$$$,
  • $$$[5,5]$$$,
  • $$$[5,6]$$$,
  • $$$[6,6]$$$.

In the second test case, all substrings are good except $$$[2,2]$$$.

In the third test case, all substrings are good.