Codeforces Round 1123 (Div. 2)
A. Turn Into a Palindrome
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Ali has a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. He also has a character $$$c$$$, which is a lowercase Latin letter. In one coin, he can perform the following operation on the string $$$s$$$:

  • First, he chooses an index $$$1\le i\le n$$$.
  • Then he replaces $$$s_i$$$ with the character $$$c$$$.

Ali wants to turn the string $$$s$$$ into a palindrome$$$^{\text{∗}}$$$, but he does not want to spend too many coins on it. Your task — compute the minimum number of coins he has to spend to turn the string $$$s$$$ into a palindrome.

$$$^{\text{∗}}$$$A string $$$t$$$ of length $$$m$$$ is a palindrome if $$$t_i = t_{m-i+1}$$$ holds for every $$$1\le i\le m$$$

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows.

The first line of each test case contains an integer $$$n$$$ and a lowercase Latin letter $$$c$$$ ($$$1\le n\le 100$$$) — the length of the string $$$s$$$ and the character $$$c$$$.

The second line of each test case contains the string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters.

Output

For each test case, output one number — the minimum number of coins Ali needs to spend for the string to become a palindrome.

Example
Input
5
4 b
abca
3 p
xyx
5 e
abcbb
8 d
adbccbad
10 c
codeforces
Output
1
0
2
2
8
Note

In the first test case, in one coin, you can replace $$$s_3$$$ with $$$\texttt{b}$$$. After the replacement, the string becomes $$$\texttt{abba}$$$, which is already a palindrome. It can be proven that $$$1$$$ is the minimum number of coins required.

In the second test case, the string $$$s$$$ is already a palindrome.

In the third test case, it is enough to change $$$s_1$$$ and $$$s_5$$$ to $$$\texttt{e}$$$. After two replacements, the string becomes $$$\texttt{ebcbe}$$$, which is already a palindrome.

In the fourth test case, in two coins, you can replace $$$s_1$$$ and $$$s_7$$$.

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

The mode of an array — is the number that appears the maximum number of times in the array. If several numbers appear the maximum number of times, the mode is the largest among them. For example, the mode of the array $$$[1, 1, 2]$$$ is $$$1$$$, and the mode of the array $$$[3, 4]$$$ is $$$4$$$.

You are given an array $$$a$$$ consisting of $$$n$$$ integers. You may arbitrarily permute the numbers in array $$$a$$$ in any order. Your task — is to rearrange the numbers in array $$$a$$$ so that the sum of the modes over all prefixes of the array is maximized.

For example, the array $$$[2, 3, 2]$$$ can be rearranged as $$$[3, 2, 2]$$$. Then the sum of the modes over all prefixes is determined as follows:

  • The prefix of length $$$1$$$ is $$$[3]$$$. The mode of this prefix is $$$3$$$.
  • The prefix of length $$$2$$$ is $$$[3, 2]$$$. The mode of this prefix is $$$3$$$.
  • The prefix of length $$$3$$$ is $$$[3, 2, 2]$$$. The mode of this prefix is $$$2$$$.
Thus, the sum of the modes is $$$3 + 3 + 2 = 8$$$. It can be proven that for this arrangement of array $$$a$$$, the answer is maximal.
Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 500$$$). The description of the test cases follows.

The first line of each test case contains one integer $$$n$$$ ($$$1 \le n \le 100$$$) — the size of the array.

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

Output

For each test case, output a new array whose sum of the modes over all prefixes is maximal. If there are several optimal answers, output any of them.

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

The first test case is described in the statement of the problem.

In the second test case, one suitable arrangement is $$$[4, 4, 3, 2, 1, 1]$$$. The mode of each prefix is $$$4$$$.

In the third test case, the arrangement $$$[4, 1, 3, 2, 2]$$$ is a valid answer. The mode of each prefix:

  • The prefix of length $$$1$$$ is $$$[4]$$$. The mode of this prefix is $$$4$$$.
  • The prefix of length $$$2$$$ is $$$[4, 1]$$$. The mode of this prefix is $$$4$$$.
  • The prefix of length $$$3$$$ is $$$[4, 1, 3]$$$. The mode of this prefix is $$$4$$$.
  • The prefix of length $$$4$$$ is $$$[4, 1, 3, 2]$$$. The mode of this prefix is $$$4$$$.
  • The prefix of length $$$5$$$ is $$$[4, 1, 3, 2, 2]$$$. The mode of this prefix is $$$2$$$.
Then the sum of the modes is $$$4 + 4 + 4 + 4 + 2 = 18$$$. It can be proven that this arrangement maximizes the sum of the modes.

C. GCD Treasury
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

The greedy pirate Dimash found a treasury. It consists of $$$n$$$ piles of coins, numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th pile contains exactly $$$a_i$$$ coins. Pirate Dimash has a number $$$x$$$ that he can use to steal coins from the treasury. The stealing process works as follows:

  • First, he chooses an index $$$1\le i\le n$$$ such that $$$a_i\gt 0$$$ and $$$\gcd$$$$$$^{\text{∗}}$$$$$$(a_i, x) \neq 1$$$. If there is no such index, the pirate stops.
  • Now let $$$\gcd(a_i, x)$$$ be $$$g$$$. The pirate steals exactly $$$g$$$ coins from pile $$$i$$$, after which $$$a_i$$$ decreases by $$$g$$$.
  • Finally, he sets $$$x$$$ to $$$g$$$ and continues stealing coins.

Your task is to help the pirate steal the maximum possible number of coins. Find the maximum number of coins that can be stolen from the treasury.

$$$^{\text{∗}}$$$$$$\gcd(a_i, x)$$$ denotes the greatest common divisor (GCD) of integers $$$a_i$$$ and $$$x$$$.

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 two integers $$$n$$$ and $$$x$$$ ($$$1\le n, x\le 3\cdot 10^5$$$) — the number of piles of coins in the treasury and the pirate's number.

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

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 one number — the maximum number of coins that can be stolen.

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

In the first test case, no coin can be stolen.

In the second test case, the pirate steals as follows.

  • The pirate chooses pile number $$$3$$$. He takes $$$4$$$ coins, after which the treasury becomes $$$[2, 3, 0]$$$ and $$$x$$$ becomes $$$4$$$.
  • The pirate chooses pile number $$$1$$$. He takes $$$2$$$ coins, after which the treasury becomes $$$[0, 3, 0]$$$, and $$$x$$$ becomes $$$2$$$.
  • No suitable indices remain, so the pirate stops. The pirate managed to take $$$4 + 2 = 6$$$ coins.

D. Backrooms Hill
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Among the people from the city GR, there is a legend about a hill that leads to the backstage. An array $$$b$$$ consisting of $$$m$$$ integers is called a hill if there exists an index $$$k$$$ ($$$1\le k\le m$$$) such that the following conditions hold:

  • The first $$$k$$$ elements are strictly increasing. Formally, for all $$$i$$$ ($$$1\le i\lt k$$$), $$$b_i\lt b_{i+1}$$$.
  • The last $$$m-k+1$$$ elements are strictly decreasing. Formally, for all $$$i$$$ ($$$k\le i\lt m$$$), $$$b_i\gt b_{i+1}$$$.

You are given an array $$$a$$$ consisting of $$$n$$$ distinct integers. You may perform the following operation any number of times:

  • Choose any index $$$i$$$ ($$$1\le i\le n-2$$$).
  • Swap $$$a_i$$$ and $$$a_{i+2}$$$ in the array.

Your task is to determine whether it is possible to turn array $$$a$$$ into a hill by performing the operation any number of times.

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 an integer $$$n$$$ ($$$1\le n\le 2\cdot 10^5$$$) — the length of array $$$a$$$.

The second line of each test case contains $$$n$$$ distinct integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1\le a_i\le 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 "YES" if it is possible to make array $$$a$$$ a hill. Otherwise, print "NO".

You may print each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as positive answers.

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

In the first test case, it is impossible to turn array $$$a$$$ into a hill.

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

In the third test case, you can perform the operations as follows:

  • Swap $$$a_1$$$ and $$$a_3$$$. After this operation, array $$$a$$$ becomes $$$[4, 3, 5, 6, 2, 1]$$$.
  • Swap $$$a_2$$$ and $$$a_4$$$. After this operation, array $$$a$$$ becomes $$$[4, 6, 5, 3, 2, 1]$$$.
And array $$$[4, 6, 5, 3, 2, 1]$$$ is a hill.

E. Clean Substrings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A binary string $$$t$$$ of length $$$m$$$ is called clean if $$$t_i = t_{i+1}$$$ for all $$$1\le i\lt m$$$.

The chief engineer of the country has made another discovery. He invented a smart robot that can perform the following operation on any binary string $$$t$$$ for one coin:

  • Choose any clean substring$$$^{\text{∗}}$$$ of the string $$$t$$$.
  • Invert all elements of the substring (change $$$0$$$ to $$$1$$$ and vice versa).

Define the beauty of a string $$$t$$$ as the minimum number of coins needed to make it clean. Define the power of a string $$$t$$$ as the sum of the beauties of all its substrings.

You are given a binary string $$$s$$$ of length $$$n$$$. The engineer's competitors are going to modify the string exactly $$$q$$$ times. Each modification is described by one number $$$i$$$. After the modification, $$$s_i$$$ is inverted. Your task is to compute the power of the string $$$s$$$ before and after each modification.

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

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 two integers $$$n$$$ and $$$q$$$ ($$$1\le n, q\le 2\cdot 10^5$$$) — the length of the binary string and the number of modifications.

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

The next $$$q$$$ lines of each test case contain one integer $$$i$$$ ($$$1\le i\le n$$$) — the description of the modifications.

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

Output

For each test case, output $$$q + 1$$$ integers separated by spaces — the answer before the modifications and after each modification of the string.

Example
Input
4
3 1
110
2
4 2
1010
1
2
8 4
10101110
3
5
2
3
10 5
1100101110
1
6
10
7
3
Output
2 2
7 5 5
36 23 22 25 26
61 66 41 35 59 60
Note

Consider the first test case.

Before the modifications, the string $$$s$$$ is 110. The power of the string is $$$2$$$:

  • The beauty of substring $$$s[1, 1]$$$ is 0.
  • The beauty of substring $$$s[2, 2]$$$ is 0.
  • The beauty of substring $$$s[3, 3]$$$ is 0.
  • The beauty of substring $$$s[1, 2]$$$ is 0.
  • The beauty of substring $$$s[2, 3]$$$ is 1.
  • The beauty of substring $$$s[1, 3]$$$ is 1.
After the modification, the string $$$s$$$ is 100. Now the power is $$$2$$$:
  • The beauty of substring $$$s[1, 1]$$$ is 0.
  • The beauty of substring $$$s[2, 2]$$$ is 0.
  • The beauty of substring $$$s[3, 3]$$$ is 0.
  • The beauty of substring $$$s[1, 2]$$$ is 1.
  • The beauty of substring $$$s[2, 3]$$$ is 0.
  • The beauty of substring $$$s[1, 3]$$$ is 1.

F1. XOR Transformations (Easy Version)
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

This is the easy version of the problem. The difference between the versions is that in this version, the constraints on $$$n$$$ and $$$q$$$ are smaller. You can make hacks only if you have solved all versions of this problem.

For an array $$$b$$$ consisting of $$$m$$$ integers, define a transformation as follows:

  • Write down the values $$$b_i\oplus b_j$$$ for all $$$1\le i\lt j\le m$$$, where $$$\oplus$$$ denotes the bitwise XOR operation..
  • Take the $$$m$$$ smallest among the written values.
  • Replace the elements of the array with the taken $$$m$$$ values.

For example, consider the transformation of the array $$$[6, 7, 8, 9, 15]$$$. We write down the values $$$1, 1, 6, 7, 8, 9, 14, 14, 15, 15$$$. After the transformation, the array becomes $$$[1, 1, 6, 7, 8]$$$ — the $$$5$$$ smallest elements.

You are given an array $$$a$$$ consisting of $$$n$$$ non-negative integers. Let $$$\max(a)$$$ denote the maximum element of the array $$$a$$$, and $$$\min(a)$$$ — the minimum. Your task is to answer $$$q$$$ queries, each of which gives you one integer $$$x$$$. For each query, find the value of $$$\max(a) - \min(a)$$$ after $$$x$$$ transformations on the array. Note that the queries are independent, i.e. before each query, the array $$$a$$$ is restored to its original state.

Input

Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 100$$$). The description of the test cases follows.

The first line of each test case contains two integers $$$n$$$ and $$$q$$$ ($$$5\le n\le 2000, 1\le q\le 2000$$$) — the size of the array and the number of queries.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0\le a_i\lt 2^{30}$$$).

The next $$$q$$$ lines of each test case contain an integer $$$x$$$ ($$$0\le x\lt 2^{30}$$$).

It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ over all test cases do not exceed $$$2000$$$.

Output

For each test case, output $$$q$$$ integers — the answer to each query.

Example
Input
4
5 1
0 0 1 1 1
1
5 2
6 7 8 9 15
0
1
8 2
102 92 15 19 25 54 62 36
2
1
10 1
56 73 81 23 17 92 50 34 67 78
2
Output
1
9
7
12
31
7
Note

In the first test case, after one transformation, the array becomes $$$[0, 0, 0, 0, 1]$$$. Here $$$\max(a) - \min(a) = 1 - 0 = 1$$$.

In the second test case, the initial array is $$$[6, 7, 8, 9, 15]$$$. Initially, $$$\max(a) - \min(a) = 15 - 6 = 9$$$. After the transformation, the array becomes $$$[1, 1, 6, 7, 8]$$$, where $$$\max(a) - \min(a) = 8 - 1 = 7$$$.

F2. XOR Transformations (Hard Version)
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

This is the hard version of the problem. The difference between the versions is that in this version, the constraints on $$$n$$$ and $$$q$$$ are higher. You can make hacks only if you have solved all versions of this problem.

For an array $$$b$$$ consisting of $$$m$$$ integers, define a transformation as follows:

  • Write down the values $$$b_i\oplus b_j$$$ for all $$$1\le i\lt j\le m$$$, where $$$\oplus$$$ denotes the bitwise XOR operation.
  • Take the $$$m$$$ smallest among the written values.
  • Replace the elements of the array with the taken $$$m$$$ values.

For example, consider the transformation of the array $$$[6, 7, 8, 9, 15]$$$. We write down the values $$$1, 1, 6, 7, 8, 9, 14, 14, 15, 15$$$. After the transformation, the array becomes $$$[1, 1, 6, 7, 8]$$$ — the $$$5$$$ smallest elements.

You are given an array $$$a$$$ consisting of $$$n$$$ non-negative integers. Let $$$\max(a)$$$ denote the maximum element of the array $$$a$$$, and $$$\min(a)$$$ — the minimum. Your task is to answer $$$q$$$ queries, each of which gives you one integer $$$x$$$. For each query, find the value of $$$\max(a) - \min(a)$$$ after $$$x$$$ transformations on the array. Note that the queries are independent, i.e. before each query, the array $$$a$$$ is restored to its original state.

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 two integers $$$n$$$ and $$$q$$$ ($$$5\le n\le 10^5, 1\le q\le 10^5$$$) — the size of the array and the number of queries.

The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0\le a_i\lt 2^{30}$$$).

The next $$$q$$$ lines of each test case contain an integer $$$x$$$ ($$$0\le x\lt 2^{30}$$$).

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

Output

For each test case, print $$$q$$$ integers — the answer to each query.

Example
Input
4
5 1
0 0 1 1 1
1
5 2
6 7 8 9 15
0
1
8 2
102 92 15 19 25 54 62 36
2
1
10 1
56 73 81 23 17 92 50 34 67 78
2
Output
1
9
7
12
31
7
Note

In the first test case, after one transformation, the array becomes $$$[0, 0, 0, 0, 1]$$$. Here $$$\max(a) - \min(a) = 1 - 0 = 1$$$.

In the second test case, the initial array is $$$[6, 7, 8, 9, 15]$$$. Initially, $$$\max(a) - \min(a) = 15 - 6 = 9$$$. After the transformation, the array becomes $$$[1, 1, 6, 7, 8]$$$, where $$$\max(a) - \min(a) = 8 - 1 = 7$$$.

G. New LRT
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

A new LRT (light rail transit) has opened in the famous city. It is a train that moves along a straight line.

You are given two numbers $$$n$$$ and $$$m$$$, and an array $$$c$$$ consisting of $$$m$$$ integers. You are at position $$$0$$$, and you need to get to position $$$n$$$. If you are at position $$$i$$$, you can ride the train as follows:

  • Choose a positive integer $$$x$$$ such that $$$m\& x = x$$$, where $$$\&$$$ denotes the bitwise AND operation.
  • Move from position $$$i$$$ to $$$i + x$$$, paying $$$c_x$$$ coins.
The cost of a trip is the total number of coins that had to be paid to get from position $$$0$$$ to position $$$n$$$. Two trips are considered different if the order of moves differs or the moves themselves differ. Your task is to determine the sum of the costs of all possible trips. Since the answer may be large, output it modulo $$$10^9 + 7$$$.
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 two integers $$$n$$$ and $$$m$$$ ($$$1\le n, m\lt 2^{20}$$$).

The second line of each test case contains $$$m$$$ integers $$$c_1, c_2, \ldots, c_m$$$ ($$$1\le c_i\le 10^9$$$).

It is guaranteed that the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2^{20}$$$.

Output

For each test case, output one number — the answer to the problem modulo $$$10^9 + 7$$$.

Example
Input
4
3 3
2 2 1
4 5
3 1 2 4 5
5 3
9 1 5
8 3
1000000000 1000000000 1000000000
Output
15
16
271
999997081
Note

In the first test case, there are $$$4$$$ ways to get to position $$$n$$$:

  • $$$0\rightarrow 1\rightarrow 2\rightarrow 3$$$. The cost of this trip is $$$c_1 + c_1 + c_1 = 6$$$.
  • $$$0\rightarrow 1\rightarrow 3$$$. The cost of this trip is $$$c_1 + c_2 = 4$$$.
  • $$$0\rightarrow 2\rightarrow 3$$$. The cost of this trip is $$$c_2 + c_1 = 4$$$.
  • $$$0\rightarrow 3$$$. The cost of this trip is $$$c_3 = 1$$$.
Thus, the answer is $$$6 + 4 + 4 + 1 = 15$$$.