Mocha likes arrays, so before her departure, Bazoka gave her an array $$$a$$$ consisting of $$$n$$$ positive integers as a gift.
Now Mocha wants to know whether array $$$a$$$ could become sorted in non-decreasing order after performing the following operation some (possibly, zero) times:
For example, if $$$a=[3,1,4,1,5]$$$, we can choose $$$x=[3,1]$$$ and $$$y=[4,1,5]$$$, satisfying $$$a=x+y$$$. Then, we can set $$$a:= y + x = [4,1,5,3,1]$$$. We can also choose $$$x=[3,1,4,1,5]$$$ and $$$y=[\,]$$$, satisfying $$$a=x+y$$$. Then, we can set $$$a := y+x = [3,1,4,1,5]$$$. Note that we are not allowed to choose $$$x=[3,1,1]$$$ and $$$y=[4,5]$$$, neither are we allowed to choose $$$x=[1,3]$$$ and $$$y=[5,1,4]$$$, as both these choices do not satisfy $$$a=x+y$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1\leq t\leq 1000$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2\leq n\leq 50$$$) — the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\leq a_i \leq 10^6$$$) — the elements of array $$$a$$$.
For each test case, output "Yes" if $$$a$$$ could become non-decreasing after performing the operation any number of times, and output "No" if not.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
361 1 4 5 1 457 9 2 2 331 2 3
No Yes Yes
In the first test case, it can be proven that $$$a$$$ cannot become non-decreasing after performing the operation any number of times.
In the second test case, we can perform the following operations to make $$$a$$$ sorted in non-decreasing order:
Mocha likes arrays, so before her departure, 378QAQ gave her an array $$$a$$$ consisting of $$$n$$$ positive integers as a gift.
Mocha thinks that $$$a$$$ is beautiful if there exist two numbers $$$i$$$ and $$$j$$$ ($$$1\leq i,j\leq n$$$, $$$i\neq j$$$) such that for all $$$k$$$ ($$$1 \leq k \leq n$$$), $$$a_k$$$ is divisible$$$^\dagger$$$ by either $$$a_i$$$ or $$$a_j$$$.
Determine whether $$$a$$$ is beautiful.
$$$^\dagger$$$ $$$x$$$ is divisible by $$$y$$$ if there exists an integer $$$z$$$ such that $$$x = y \cdot z$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1\leq t\leq 500$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$3\leq n\leq 10^5$$$) — the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\leq a_i \leq 10^9$$$) — the elements of the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output "Yes" if array $$$a$$$ is beautiful, and output "No" otherwise.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
437 3 857 1 9 3 554 12 2 6 357 49 9 3 1000000000
No Yes Yes No
In the first test case, any two numbers in the array are coprime, so the answer is "No".
In the second test case, we can pick $$$i=2$$$ and $$$j=1$$$. Since every number in the array is divisible by $$$a_i = 1$$$, the answer is "Yes".
In the third test case, we can pick $$$i=3$$$ and $$$j=5$$$. $$$2$$$ and $$$4$$$ is divisible by $$$a_i = 2$$$ while $$$3$$$, $$$6$$$ and $$$12$$$ is divisible by $$$a_j = 3$$$, so the answer is "Yes".
Mocha likes arrays, so before her departure, Chamo gave her an array $$$a$$$ consisting of $$$n$$$ positive integers as a gift.
Mocha doesn't like arrays containing different numbers, so Mocha decides to use magic to change the array. Mocha can perform the following three-step operation some (possibly, zero) times:
Suppose $$$a=[1,2,3,4,5]$$$ initially:
Mocha will perform the operation until the array contains only the same number. Mocha wants to know what is the maximum possible value of this number.
$$$^\dagger$$$ The median in an array $$$b$$$ of length $$$m$$$ is an element that occupies position number $$$\lfloor \frac{m+1}{2} \rfloor$$$ after we sort the elements in non-decreasing order. For example, the median of $$$[3,1,4,1,5]$$$ is $$$3$$$ and the median of $$$[5,25,20,24]$$$ is $$$20$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1\leq t\leq 500$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$2\leq n\leq 10^5$$$) — the length of the array $$$a$$$.
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots,a_n$$$ ($$$1\leq a_i \leq 10^9$$$) — the elements of the array $$$a$$$.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output the maximum value of the number.
221 251 2 3 4 5
1 4
In the first test case, $$$a=[1,2]$$$. Mocha can only choose the interval $$$(l,r)=(1,2)$$$. The array will be changed to $$$a=[1,1]$$$. Therefore, the answer is $$$1$$$.
In the second test case, Mocha can perform the following operations:
The array contains only the same number, which is $$$4$$$. It can be proven that the maximum value of the final number cannot be greater than $$$4$$$.
378QAQ has a tree with $$$n$$$ vertices. Initially, all vertices are white.
There are two chess pieces called $$$P_A$$$ and $$$P_B$$$ on the tree. $$$P_A$$$ and $$$P_B$$$ are initially located on vertices $$$a$$$ and $$$b$$$ respectively. In one step, 378QAQ will do the following in order:
Initially, the vertex $$$a$$$ is painted red. If $$$a=b$$$, the vertex $$$a$$$ is painted blue instead. Note that both the chess pieces must be moved in each step. Two pieces can be on the same vertex at any given time.
378QAQ wants to know the minimum number of steps to paint all vertices blue.
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 one integer $$$n$$$ ($$$1\leq n\leq 2\cdot 10^5$$$).
The second line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1\leq a,b\leq n$$$).
Then $$$n - 1$$$ lines follow, each line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i,y_i \le n$$$), indicating an edge between vertices $$$x_i$$$ and $$$y_i$$$. It is guaranteed that these edges form a tree.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2\cdot 10^5$$$.
For each test case, output the minimum number of steps to paint all vertices blue.
321 21 251 21 21 31 41 585 47 11 51 88 37 28 63 4
2 8 13
In the first test case, 378QAQ can paint all vertices blue in the following order:
You are given a tree of $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. Initially, all vertices are colored white or black.
You are asked to perform $$$q$$$ queries:
After each query, you should answer whether all the black vertices form a chain. That is, there exist two black vertices such that the simple path between them passes through all the black vertices and only the black vertices. Specifically, if there is only one black vertex, they form a chain. If there are no black vertices, they do not form a chain.
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 two integers $$$n$$$ and $$$q$$$ ($$$1\leq n,q\leq 2\cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers $$$c_1,c_2,\ldots,c_n$$$ ($$$c_i \in \{ \mathtt{0}, \mathtt{1} \}$$$) — the initial color of the vertices. $$$c_i$$$ denotes the color of vertex $$$i$$$ where $$$\mathtt{0}$$$ denotes the color white, and $$$\mathtt{1}$$$ denotes the color black.
Then $$$n - 1$$$ lines follow, each line contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 \le x_i,y_i \le n$$$), indicating an edge between vertices $$$x_i$$$ and $$$y_i$$$. It is guaranteed that these edges form a tree.
The following $$$q$$$ lines each contain an integer $$$u_i$$$ ($$$1 \le u_i \le n$$$), indicating the color of vertex $$$u_i$$$ needs to be toggled.
It is guaranteed that the sum of $$$n$$$ and $$$q$$$ over all test cases respectively does not exceed $$$2\cdot 10^5$$$.
For each query, output "Yes" if the black vertices form a chain, and output "No" otherwise.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
22 11 01 215 41 0 0 0 01 21 31 53 44325
No No Yes Yes No
45 31 1 1 1 13 52 53 41 51114 40 0 0 01 22 31 412321 1111 101
Yes No Yes Yes Yes Yes No No Yes
In the second test case, the color of the vertices are as follows:
The initial tree:
The first query toggles the color of vertex $$$4$$$:
The second query toggles the color of vertex $$$3$$$:
The third query toggles the color of vertex $$$2$$$:
The fourth query toggles the color of vertex $$$5$$$:
Define the binary encoding of a finite set of natural numbers $$$T \subseteq \{0,1,2,\ldots\}$$$ as $$$f(T) = \sum\limits_{i \in T} 2^i$$$. For example, $$$f(\{0,2\}) = 2^0 + 2^2 = 5$$$ and $$$f(\{\}) = 0$$$. Notice that $$$f$$$ is a bijection from all such sets to all non-negative integers. As such, $$$f^{-1}$$$ is also defined.
You are given an integer $$$n$$$ along with $$$2^n-1$$$ sets $$$V_1,V_2,\ldots,V_{2^n-1}$$$.
Find all sets $$$S$$$ that satisfy the following constraint:
Due to the large input and output, both input and output will be given in terms of binary encodings of the sets.
The first line of input contains a single integer $$$n$$$ ($$$1 \leq n \leq 20$$$).
The second line of input contains $$$2^n-1$$$ integers $$$v_1,v_2,\ldots,v_{2^n-1}$$$ ($$$0 \leq v_i \lt 2^{n+1}$$$) — the sets $$$V_i$$$ given in their binary encoding where $$$V_i = f^{-1}(v_i)$$$.
The first line of output should contain an integer $$$k$$$ indicating the number of possible $$$S$$$.
In the following $$$k$$$ lines, you should output $$$f(S)$$$ for all possible $$$S$$$ in increasing order.
315 15 15 15 15 15 12
4 3 5 6 7
563 63 63 63 6 63 63 63 63 63 63 5 63 63 63 63 63 63 8 63 63 63 63 2 63 63 63 63 63 63 63
1 19
In the first test case, one possible $$$S$$$ is $$$f^{-1}(3) = \{0,1\}$$$. All the non-empty subsets $$$T \subseteq \{0,1,2\}$$$ and the corresponding $$$|S \cap T|$$$, $$$f(T)$$$ and $$$V_f(T)$$$ are as follows:
| $$$T$$$ | $$$|S\cap T|$$$ | $$$f(T)$$$ | $$$V_{f(T)}$$$ |
| $$$\{0\}$$$ | $$$1$$$ | $$$1$$$ | $$$\{0,1,2,3\}$$$ |
| $$$\{1\}$$$ | $$$1$$$ | $$$2$$$ | $$$\{0,1,2,3\}$$$ |
| $$$\{2\}$$$ | $$$0$$$ | $$$4$$$ | $$$\{0,1,2,3\}$$$ |
| $$$\{0,1\}$$$ | $$$2$$$ | $$$3$$$ | $$$\{0,1,2,3\}$$$ |
| $$$\{0,2\}$$$ | $$$1$$$ | $$$5$$$ | $$$\{0,1,2,3\}$$$ |
| $$$\{1,2\}$$$ | $$$1$$$ | $$$6$$$ | $$$\{0,1,2,3\}$$$ |
| $$$\{0,1,2\}$$$ | $$$2$$$ | $$$7$$$ | $$$\{2,3\}$$$ |
One day, Zimpha casually came up with a problem. As a member of "Zimpha fan club", you decided to solve that problem.
You are given two strings $$$s$$$ and $$$t$$$ of length $$$n$$$ and $$$m$$$, respectively. Both strings only consist of lowercase English letters, - and *.
You need to replace all occurrences of * and -, observing the following rules:
Note that you can replace two different instances of - with different characters. You can also replace each two different instances of * with different strings.
Suppose $$$s$$$ and $$$t$$$ have been transformed into $$$s'$$$ and $$$t'$$$. Now you're wondering if there's a replacement that makes $$$s'=t'$$$.
The first line of input contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n, m \leq 2 \cdot 10^6$$$) — the length of the strings $$$s$$$ and $$$t$$$, respectively.
The second line contains the string $$$s$$$ of length $$$n$$$. It is guaranteed that $$$s$$$ only consists of lowercase English letters, - and *.
The third line contains the string $$$t$$$ of length $$$m$$$. It is guaranteed that $$$t$$$ only consists of lowercase English letters, - and *.
For each test case, output "Yes" if there is a replacement that makes $$$s'=t'$$$, and output "No" otherwise.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
10 10justmonikaj-stsayori
No
7 8ttk-wxx*tt-l-xx
Yes
13 11asoulwangziji-soulg*z-y-
No
7 3abc*cbaa*c
No
20 18bulijiojio-dibuliduo*li*ji-*ox*i*-du*-
Yes
In the second test case, we can transform both strings into ttklwxx. In $$$s$$$, - will be replaced with l. In $$$t$$$, * will be replaced by the empty string with the first and second - will be replaced with k and w respectively.
In the fifth test case, we can transform both strings into bulijiojioxdibuliduo.
378QAQ has a string $$$s$$$ of length $$$n$$$. Define the core of a string as the substring$$$^\dagger$$$ with maximum lexicographic$$$^\ddagger$$$ order.
For example, the core of "$$$\mathtt{bazoka}$$$" is "$$$\mathtt{zoka}$$$", and the core of "$$$\mathtt{aaa}$$$" is "$$$\mathtt{aaa}$$$".
378QAQ wants to rearrange the string $$$s$$$ so that the core is lexicographically minimum. Find the lexicographically minimum possible core over all rearrangements of $$$s$$$.
$$$^\dagger$$$ A substring of string $$$s$$$ is a continuous segment of letters from $$$s$$$. For example, "$$$\mathtt{defor}$$$", "$$$\mathtt{code}$$$" and "$$$\mathtt{o}$$$" are all substrings of "$$$\mathtt{codeforces}$$$" while "$$$\mathtt{codes}$$$" and "$$$\mathtt{aaa}$$$" are not.
$$$^\ddagger$$$ A string $$$p$$$ is lexicographically smaller than a string $$$q$$$ if and only if one of the following holds:
For example, "$$$\mathtt{code}$$$" and "$$$\mathtt{coda}$$$" are both lexicographically smaller than "$$$\mathtt{codeforces}$$$" while "$$$\mathtt{codeforceston}$$$" and "$$$\mathtt{z}$$$" are not.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1\leq t\leq 10^5$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1\leq n\leq 10^6$$$) — the length of string $$$s$$$.
The next line of each test case contains the string $$$s$$$ of length $$$n$$$. The string $$$s$$$ consists of lowercase English letters.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$.
For each test case, output the lexicographically minimum possible core over all rearrangements of $$$s$$$.
63qaq4cccc6bazoka6zazzzz7ababbbb7ccbabcc
qaq cccc z zzz bbababb cbcacbc
In the first test case, all possible rearrangements and their corresponding cores are as follows:
So the core with the minimum lexicographic order in all rearrangement plans is "$$$\mathtt{qaq}$$$".
Jellyfish is playing a one-player card game called "Slay the Spire". There are $$$n$$$ cards in total numbered from $$$1$$$ to $$$n$$$. The $$$i$$$-th card has power $$$c_i$$$.
There is a binary string $$$s$$$ of length $$$n$$$. If $$$s_i = \texttt{0}$$$, the $$$i$$$-th card is initially in the draw pile. If $$$s_i = \texttt{1}$$$, the $$$i$$$-th card is initially in Jellyfish's hand.
Jellyfish will repeat the following process until either her hand or the draw pile is empty.
At the end of this process, find the probability that Jellyfish can empty the draw pile, modulo $$$1\,000\,000\,007$$$.
Formally, let $$$M=1\,000\,000\,007$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$\frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q \not \equiv 0 \pmod{M}$$$. Output the integer equal to $$$p \cdot q^{-1} \bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 \le x \lt M$$$ and $$$x \cdot q \equiv p \pmod{M}$$$.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1\leq t\leq 100$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \leq n \leq 120$$$) — the number of cards.
The second line of each test case contains $$$n$$$ integers $$$c_1,c_2,\ldots,c_n$$$ ($$$0 \leq c_i \leq n$$$) — the powers of the cards. It is guaranteed that $$$c_1 \leq c_2 \leq \ldots \leq c_n$$$.
The third line of each test case contains a binary string $$$s$$$ of length $$$n$$$. If $$$s_i = \texttt{0}$$$, the $$$i$$$-th card is initially in the draw pile. If $$$s_i = \texttt{1}$$$, the $$$i$$$-th card is initially in Jellyfish's hand.
It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$120^2$$$.
For each test case, output the probability that Jellyfish can empty the draw pile modulo $$$1\,000\,000\,007$$$.
450 1 1 1 20010032 3 3000100 0 0 0 0 0 0 1 1 11111011111200 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 2 3 3 400000000001000101010
500000004 0 0 675898154
In the first test case, Jellyfish will keep playing cards with power $$$1$$$ until Jellyfish draws a card with power $$$0$$$ or power $$$2$$$. If Jellyfish draws a card with power $$$0$$$, she will eventually empty her hand. If Jellyfish draws a card with power $$$2$$$, she will eventually empty the draw pile. Since there is an equal chance of drawing $$$0$$$ or $$$2$$$, the answer is $$$\frac{1}{2}$$$, and $$$2 \cdot 500\,000\,004 \equiv 1 \pmod {10^9+7}$$$