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$$$:
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$$$
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.
For each test case, output one number — the minimum number of coins Ali needs to spend for the string to become a palindrome.
54 babca3 pxyx5 eabcbb8 dadbccbad10 ccodeforces
10228
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$$$.
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:
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.
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.
732 3 264 4 2 1 3 151 3 2 4 241 1 1 271 2 3 4 5 6 781 1 4 2 3 3 3 284 3 3 3 2 1 4 1
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
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 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:
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$$$.
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$$$.
For each test case, output one number — the maximum number of coins that can be stolen.
53 12 3 53 42 3 44 22 2 2 26 62 3 2 3 2 37 69 9 4 4 4 4 4
068920
In the first test case, no coin can be stolen.
In the second test case, the pirate steals as follows.
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:
You are given an array $$$a$$$ consisting of $$$n$$$ distinct integers. You may perform the following operation any number of times:
Your task is to determine whether it is possible to turn array $$$a$$$ into a hill by performing the operation any number of times.
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$$$.
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.
732 1 341 2 3 465 3 4 6 2 161 5 2 3 6 471 2 3 4 7 5 655 4 1 3 264 1 3 2 6 5
NOYESYESNONOYESNO
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:
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:
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.
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$$$.
For each test case, output $$$q + 1$$$ integers separated by spaces — the answer before the modifications and after each modification of the string.
43 111024 21010128 410101110352310 51100101110161073
2 27 5 536 23 22 25 2661 66 41 35 59 60
Consider the first test case.
Before the modifications, the string $$$s$$$ is 110. The power of the string is $$$2$$$:
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:
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.
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$$$.
For each test case, output $$$q$$$ integers — the answer to each query.
45 10 0 1 1 115 26 7 8 9 15018 2102 92 15 19 25 54 62 362110 156 73 81 23 17 92 50 34 67 782
19712317
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$$$.
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:
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.
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$$$.
For each test case, print $$$q$$$ integers — the answer to each query.
45 10 0 1 1 115 26 7 8 9 15018 2102 92 15 19 25 54 62 362110 156 73 81 23 17 92 50 34 67 782
19712317
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$$$.
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:
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}$$$.
For each test case, output one number — the answer to the problem modulo $$$10^9 + 7$$$.
43 32 2 14 53 1 2 4 55 39 1 58 31000000000 1000000000 1000000000
1516271999997081
In the first test case, there are $$$4$$$ ways to get to position $$$n$$$: