There's a $$$1 \times n$$$ grid. For a string $$$y$$$ of length $$$l$$$, a robot moves according to the following pattern.
You are given a possibly incomplete string $$$x$$$ of length $$$m$$$. Your task is to complete the string $$$x$$$ such that, regardless of which cell the robot selects as the starting point, it will always explode. If there's no such string, output "NO" instead.
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 1000$$$). The description of the test cases follows.
The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n,m \le 50$$$).
The second line of each test case contains a string $$$x_1x_2 \ldots x_{m}$$$ of length $$$m$$$ consisting of characters 'L', 'R' and '?'. If $$$x_i=\mathtt{?}$$$ ($$$1 \le i \le m$$$), it means you need to choose 'L' or 'R' as the value of $$$x_i$$$.
For each test case, if there's no such string, output "NO" in a new line. Otherwise, output "YES" in a new line, then output the complete string $$$x$$$ in the next line.
41 1L2 1?3 5LR?R?50 5?????
YES L NO YES LRRRL NO
In the first test case, the robot can only choose the first cell as the start point. Then, in the first move, the robot moves one cell to the left, which is out of bounds. After the first move, the robot will explode, so the answer is "YES".
In the second test case, if $$$x=\mathtt{L}$$$, the robot can choose the second cell as the start point. If $$$x=\mathtt{R}$$$, the robot can choose the first cell as the start point. In both cases, the robot will not explode. Thus, the answer is "NO".
There are $$$n$$$ doors. You want to open all of them. There are some rules.
At first, you can only open the first door. For each $$$i \ge 1$$$, you can open the $$$i+1$$$-th door only if you have already opened the $$$i$$$-th door.
Initially, you have $$$x$$$ coins. There is a lock on the $$$i$$$-th door with a cost of $$$c_i$$$. In other words, you need to spend $$$c_i$$$ coins to open the $$$i$$$-th door. If the number of coins you currently have is strictly less than $$$c_i$$$, you will not be able to open the $$$i$$$-th door. After opening the $$$i$$$-th door, you will receive $$$a_i$$$ coins as a prize.
You can do the following operation any number of times:
Find the minimum number of operations necessary to open all the doors.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^5$$$), the number of test cases. For each test case:
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 operations needed to open all the doors.
31 1113 109 6 51 1 13 105 9 91 2 9
0 1 2
In the first test case, you don't need to perform any operations. You can just spend $$$1$$$ coin to open the first (and only) lock.
In the second test case, you can first set $$$c_i$$$ to $$$0$$$. Then, you can do the following:
You are given a sequence $$$a$$$ of length $$$n$$$. There is a sequence $$$b$$$ which is initially empty. You need to perform $$$n$$$ operations.
In the $$$i$$$-th operation, you can choose one of the following two types of operations:
Solve the following problem for each $$$j$$$, where $$$0 \le j \le n$$$: find the minimum possible number of inversions$$$^\dagger$$$ in $$$b$$$ if exactly $$$j$$$ operations of the first type are performed.
$$$^\dagger$$$ An inversion in a sequence is a pair of elements where the earlier element is greater than the later element. In other words, for a sequence $$$b$$$, a pair $$$(b_i, b_j)$$$ is an inversion only if $$$i \lt j$$$ and $$$b_i \gt b_j$$$.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^5$$$), the number of test cases. For each test case:
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
For each test case, output $$$n+1$$$ integers. The $$$j$$$-th integer should be the minimum possible number of inversions when exactly $$$j$$$ operations of the first type are performed, for $$$j=0,\ldots,n$$$.
322 153 2 4 1 263 2 6 1 5 4
1 0 0 6 3 2 1 1 3 7 4 3 3 4 6 8
Consider the first test case.
You are given $$$n$$$ intervals $$$[l_i,r_i]$$$, where $$$l_i \le r_i$$$. Your task is to determine if there exist $$$n$$$ integers $$$x_1, x_2, \ldots, x_n$$$ satisfying:
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^4$$$), the number of test cases.
For each test case:
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.
For each test case, output:
The letters in the word YES and NO are case insensitive. For example, YES, yeS and No are all considered valid.
431 5 105 6 1142 3 4 10000002 3 4 1000000534 15 55 233 5134 20 99 299 5131 1 11 1 2
YES 5 5 10 NO YES 34 17 85 255 51 NO
In the first example, we choose $$$5$$$, $$$5$$$ and $$$10$$$ respectively from each range. Then, we have $$$\operatorname{gcd}(5,5,10) = 5 \neq 1$$$.
In the second example, it is not possible to choose $$$n$$$ integers satisfying the condition.
We call a tree with nodes numbered from $$$1$$$ to $$$n$$$ a Manhattan tree only if there exist $$$n$$$ points $$$(x_1, y_1), (x_2, y_2), \ldots, (x_n, y_n)$$$ in a two-dimensional plane such that for any $$$i, j$$$ ($$$1 \leq i \lt j \leq n$$$), the Manhattan distances$$$^*$$$ from $$$(x_i, y_i)$$$ to $$$(x_j, y_j)$$$ is equal to the distance$$$^\dagger$$$ between node $$$i$$$ and node $$$j$$$ in the tree.
Given $$$n$$$, output the number of different$$$^\ddagger$$$ Manhattan trees with nodes numbered from $$$1$$$ to $$$n$$$, modulo $$$998244353$$$.
$$$^*$$$ The Manhattan distance between two points $$$(x_1, y_1)$$$ and $$$(x_2, y_2)$$$ in a two-dimensional plane is defined as $$$|x_1 - x_2| + |y_1 - y_2|$$$.
$$$^\dagger$$$ The distance between two nodes $$$i$$$ and $$$j$$$ in a tree is defined as the number of edges in the simple path between node $$$i$$$ and node $$$j$$$.
$$$^\ddagger$$$ We call tree $$$T_1$$$ and tree $$$T_2$$$ different if and only if there's an edge $$$(u, v)$$$ such that $$$(u, v) \in T_1$$$ and $$$(u, v) \notin T_2$$$.
The first line contains an integer $$$t$$$ ($$$1 \le t \le 10^5$$$), the number of test cases.
For each test case:
For each test case, output in a new line — the number of different Manhattan trees, modulo $$$998244353$$$.
62367996999999
1 3 1290 16170 412965036 886558470
In the third test case ($$$n=6$$$), for example, the following tree is a Manhattan tree
because there exist $$$6$$$ points $$$(x_1, y_1), (x_2, y_2), \ldots, (x_6, y_6)$$$ in a two-dimensional plane such that for any $$$i, j$$$ ($$$1 \leq i \lt j \leq 6$$$), the Manhattan distance from $$$(x_i, y_i)$$$ to $$$(x_j, y_j)$$$ is equal to the distance between node $$$i$$$ and node $$$j$$$ in the tree.
An example of the possible 6 points. However, the following tree is not a Manhattan tree.
You're given two integers $$$n$$$ and $$$q$$$ and two arrays $$$a$$$ and $$$b$$$ of size $$$n$$$.
You need to answer $$$q$$$ queries. In the $$$i$$$-th query, you're given three integers $$$k_i, l_i$$$, and $$$r_i$$$.
At first, a variable $$$x$$$ is set to $$$k_i$$$. Then, for each $$$j$$$ from $$$l_i$$$ to $$$r_i$$$, you must choose exactly one of the following two types of operations:
You need to find the maximum possible value of $$$x$$$ for each query.
The first line contains one positive integer $$$t$$$ ($$$1 \le t \le 10^5$$$), the number of test cases.
Each of the next $$$q$$$ lines contains three integers $$$k_i, l_i$$$, and $$$r_i$$$ ($$$-10^9 \leq k_i \leq 10^9, 1 \le l_i \le r_i \le n$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases and the sum of $$$q$$$ over all test cases do not exceed $$$5 \cdot 10^5$$$.
For each query, output an integer in a new line — the maximum possible value of $$$x$$$.
14 510 2 3 -120 24 28 -108 1 217 1 219 2 325 2 3-2 4 4
24 29 28 30 -2
15 5756829654 557600139 843962687 -24632597 -866775049505319834 616877613 657487273 912786344 924284486481299777 1 3-566119234 2 4478992801 1 2-694494781 3 5-579759578 1 4
2639692257 1460840300 1793422594 924284486 1906882660
In the first query of the first test case, we have $$$x=8$$$ at first. We can do the following operations:
It can be proven that $$$24$$$ is the maximum possible value of $$$x$$$.
In the second query of the first test case, we have $$$x=17$$$ at first. We can do the following operations:
It can be proven that $$$29$$$ is the maximum possible value of $$$x$$$.
You're given two binary strings $$$a$$$ and $$$b$$$ of length $$$n$$$.
You can do the following operation any number of times:
Find the minimum number of operations to change $$$a$$$ to $$$b$$$. If you cannot change $$$a$$$ to $$$b$$$, output $$$-1$$$ instead.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$), the number of test cases.
For each test case:
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$4 \cdot 10^5$$$.
For each test case, output a single line containing the minimum number of operations to change $$$a$$$ to $$$b$$$. If you can not change $$$a$$$ to $$$b$$$, output $$$-1$$$ instead.
530000113000111300000040010010051110110011
-1 -1 0 2 3
In the first and second test cases, you cannot change $$$a$$$ to $$$b$$$.
In the third test case, $$$a$$$ and $$$b$$$ are already equal, so you don't need to do any operations.
In the fourth test case, you can change $$$a$$$ to $$$b$$$ after the following two operations:
It can be proven that $$$2$$$ is the minimum number of operations.
You are given an integer $$$n$$$.
Construct a permutation $$$p_0, p_1, \ldots, p_{n-1}$$$ of the integers $$$\{0,1,\ldots,n-1\}$$$, such that $$$$$$f(p)=\sum_{i=0}^{n-2} (p_i \oplus p_{i+1})$$$$$$ is minimum possible.
Here, $$$\oplus$$$ denotes the bitwise XOR operation (https://en.wikipedia.org/wiki/Bitwise_operation#XOR)
The first line contains an integer $$$t$$$ $$$(1 \leq t \leq 500)$$$, representing the number of test cases.
Each of the next $$$t$$$ lines contains a single integer $$$n$$$ $$$(2 \leq n \leq 2 \cdot 10^5)$$$, which denotes the size of the permutation.
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output a single line containing the permutation $$$p$$$ of $$$\{0, 1, \dots, n-1\}$$$ such that $$$f(p)=\sum_{i=0}^{n-2} (p_i \oplus p_{i+1})$$$ is minimum possible.
224
1 0 3 2 0 1
In the first test case, $$$f(p)=0 \oplus 1=1$$$, which is the minimum possible. Another valid permutation is $$$p=[0,1]$$$.
In the second test case, $$$f(p)=(3 \oplus 2)+(2 \oplus 0)+(0 \oplus 1)=1+2+1=4$$$, which is the minimum possible. Other valid permutations, such as $$$p=[0,1,3,2]$$$, also exist.
You are given two polynomials $$$f(x) = Ax + B$$$ and $$$g(x) = Cx^n + Dx^{n-1}$$$. Your task is to find the minimum integer $$$k$$$, such that $$$g^{(k)}(x)$$$ is divisible by $$$f(x)$$$.
Here, $$$g^{(k)}(x)$$$ represents taking the derivative of $$$g(x)$$$ exactly $$$k$$$ times.
Formally,
and
where $$$E$$$, $$$F$$$, and $$$G$$$ are constants.
For example, for $$$g(x) = x^3 + 3x^2$$$, we can find that:
We say that a polynomial $$$P_1(x)$$$ is divisible by $$$P_2(x)$$$ if and only if there exists a polynomial $$$Q(x)$$$ such that $$$P_1(x) = Q(x)P_2(x)$$$, where the coefficients of $$$Q$$$ are integers.
The first line contains one positive integer $$$t$$$ ($$$1\leq t \leq 10^5$$$) — the number of test cases.
Each test case consists of one line which contains $$$5$$$ integers $$$A,B,C,D,n$$$ ($$$1\leq A,B,C,D,n\leq 2\cdot 10^5$$$).
For each test case, output the minimum integer $$$k$$$, such that $$$g^{(k)}(x)$$$ is divisible by $$$f(x)$$$.
61 2 2 4 14 2 3 3 42 4 1 3 32 1 5 2 4131296 123463 91609 133724 142208172458 127836 190471 141192 190476
0 2 4 5 50599 190477
In the first test case, $$$f(x)=x+2$$$ and $$$g(x)=2x+4$$$. We can see that $$$g^{(0)}(x)$$$ is divisible by $$$f(x)$$$ because $$$g^{(0)}(x)=g(x)=2x+4=2 \cdot f(x)$$$. So, the answer is $$$0$$$.
In the second test case, $$$f(x)=4x+2$$$ and $$$g(x)=3x^4+3x^3$$$. We can see that $$$k=2$$$ is the minimum integer such that $$$g^{(k)}(x)$$$ is divisible by $$$f(x)$$$ because $$$g^{(2)}(x)=36x^2+18x=9x \cdot f(x)$$$.
In the third test case, $$$f(x)=2x+4$$$ and $$$g(x)=x^3+3x^2$$$. We can see that $$$k=4$$$ is the minimum integer such that $$$g^{(k)}(x)$$$ is divisible by $$$f(x)$$$ because $$$g^{(4)}(x)=0=0 \cdot f(x)$$$.
Note that although $$$g^{(1)}(x)=3x^2+6x=\frac{2}{3}x \cdot f(x)$$$, $$$g^{(1)}(x)$$$ is not divisible by $$$f(x)$$$ since $$$\frac{2}{3}$$$ is not an integer.
You're given a rooted tree with $$$n$$$ vertices. The root node is $$$1$$$. You can set the weight of each edge to either $$$0$$$ or $$$1$$$. We call the sum value of node $$$i$$$ the sum of the weights of all edges incident to it.
Count the number of different assignments of edge weights that satisfy the following condition.
Since the answer may be large, output the answer modulo $$$998\,244\,353$$$.
$$$^\dagger$$$ A node is called a non-leaf node if and only if it is the root node $$$1$$$ or there are at least $$$2$$$ edges incident to it.
The input consists of multiple test cases. The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 10^5$$$), the number of test cases.
For each test case:
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, output a single integer representing the number of different trees satisfying the given condition, modulo $$$998\,244\,353$$$.
531 21 331 22 341 42 43 161 42 33 64 35 281 22 43 44 85 16 47 3
4 2 4 3 6
In the first test case, there is only one non-leaf node: node $$$1$$$. You can set edge weights arbitrarily; thus, there are $$$4$$$ different assignments of edge weights.
In the second test case, there are two non-leaf nodes: nodes $$$1$$$ and $$$2$$$. There are $$$2$$$ valid assignments as shown below.
![]() | ![]() |
You're given two even integers $$$n$$$ and $$$m$$$.
We define the $$$\operatorname{MAD}$$$ (Maximum Appearing Duplicate) of an array as the largest number that appears at least twice in the array. If there is no number that appears at least twice, the $$$\operatorname{MAD}$$$ value is $$$0$$$.
For example, $$$\operatorname{MAD}([1, 2, 1]) = 1$$$, $$$\operatorname{MAD}([2, 2, 3, 3]) = 3$$$, $$$\operatorname{MAD}([1, 2, 3, 4]) = 0$$$.
Construct an array $$$a$$$ of length $$$n$$$ satisfying:
If no such array exists, output $$$-1$$$ instead.
The first line contains one positive integer $$$t\;(1\leq t \leq 10^3)$$$ — the number of test cases.
Each test case consists of one line which contains two integers $$$n$$$ and $$$m$$$ ($$$2 \leq n \le 10^4$$$, $$$0 \le m \le \frac{n \cdot n \cdot (n-1)}{2}$$$). It is guaranteed that $$$n$$$ and $$$m$$$ are both even.
It is guaranteed that the sum of $$$n$$$ over all test cases will not exceed $$$10^4$$$.
For each test case, output an array $$$a$$$ of length $$$n$$$ that satisfies the given conditions. If no such array exists, output $$$-1$$$ instead.
42 04 104 228 40
0 0 4 2 2 4 -1 5 2 1 2 1 5 2 1
In the first test case, $$$$$$\sum_{1 \le l \lt r \le n} \operatorname{MAD}([a_l,a_{l+1}, \ldots, a_r])=\operatorname{MAD}([a_1,a_2])=\operatorname{MAD}([0,0])=0. $$$$$$
In the second test case, the following table summarizes the $$$\operatorname{MAD}$$$ of each subarray. $$$$$$ \begin{array}{c|cccc} & 1 & 2 & 3 & 4 \\ \hline 1 & & 0 & 2 & 4 \\ 2 & & & 2 & 2 \\ 3 & & & & 0 \\ 4 & & & & \end{array} $$$$$$ The sum of all $$$\operatorname{MAD}$$$ values is $$$10$$$, as required.
In the third test case, it is impossible to construct such an array.
Alice and Bob are playing a game on an array $$$a$$$ of length $$$n$$$. They take turns performing the operation. Alice takes the lead. The person who cannot make a move loses.
In an operation, a player can:
Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^5$$$). The description of the test cases follows.
The first line of each test case contains a single integer $$$n$$$ ($$$1 \le n \le 3\cdot 10^5$$$).
The second line of each test case contains $$$n$$$ integers $$$a_1,a_2,\ldots, a_n$$$ ($$$1 \le a_i \le 10^9$$$).
It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 \cdot 10^5$$$.
For each test case, if Alice has a winning strategy, output YES. Otherwise, output NO.
671 3 1 3 3 4 541 1 1 11100000000031 2 341 2 3 486 10 9 2 8 6 5 4
YES YES NO NO YES NO
In the first test case, Alice can choose the range $$$[1,3]$$$, then choose all indices satisfying $$$a_i \in [1,2]$$$ and decrease $$$a_i$$$ by $$$1$$$. In other words, $$$a_1$$$ and $$$a_3$$$ are decreased by $$$1$$$. After that, $$$a=[0,3,0,3,3,4,5]$$$. Then, Alice can choose some $$$i$$$ satisfying $$$a_i=3$$$. In this case, Alice chooses $$$i=2,4$$$. After that,$$$a=[0,2,0,2,3,4,5]$$$;
It can be proven Alice has a winning strategy after the operation above.
In the second test case, Alice can choose $$$[1,1]$$$. Then, Alice can choose some indices $$$i$$$ satisfying $$$a_i=1$$$. Here, Alice chooses $$$i=1,2,3,4$$$. After this operation, $$$a=[0,0,0,0]$$$ and Alice wins.
You are given a tree with $$$n$$$ nodes and a fixed even integer $$$k$$$. Node $$$i$$$ has weight $$$a_i$$$.
A closed path on a tree is a path that starts and ends at the same node. A closed path may visit the same node multiple times. A closed path of length $$$k$$$ that starts at node $$$1$$$ can be written as $$$x_1 \rightarrow x_2 \rightarrow \ldots \rightarrow x_{k+1}$$$, where:
The score of the closed path $$$x_1 \rightarrow x_2 \rightarrow \ldots \rightarrow x_{k+1}$$$ is defined as $$$\sum_{i=1}^{k+1} a_{x_i}$$$.
You need to answer $$$q$$$ queries. In the $$$i$$$-th query, you are given two numbers $$$p_i$$$ and $$$v_i$$$, which means you should update the value of $$$a_{p_i}$$$ to $$$v_i$$$. After the update, you need to find the score of a closed path of length $$$k$$$ starting at node $$$1$$$ that has the maximum possible score. Note that the updates are permanent.
The first line contains an integer $$$t$$$ ($$$1 \leq t \leq 2.5 \cdot 10^4$$$), the number of test cases.
For each test case:
It is guaranteed that the sum of $$$n$$$ and $$$q$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$ respectively.
For each query, output a single integer in a new line — the maximum score of a closed path of length $$$k$$$ starting at node $$$1$$$ after applying the update.
24 61 4 3 11 21 34 322 34 22 10000000001 11 241 10000000002 10000000001 12 1
13 15 500000001500000000 1000000001000000000 500000000500000001 1000000001
In the first query of the first test case, $$$a=[1,3,3,1]$$$ and $$$k=6$$$. One of the possible closed paths of length $$$k$$$ that starts at node $$$1$$$ is $$$1 \rightarrow 2 \rightarrow 1 \rightarrow 3 \rightarrow 4 \rightarrow 3 \rightarrow 1$$$. The score of this closed path is $$$13$$$.
It can be proven that $$$13$$$ is the maximum possible score of a closed path.
In the first query of the first test case, $$$a=[1,3,3,2]$$$ and $$$k=6$$$. One of the possible closed paths of length $$$k$$$ that starts at node $$$1$$$ is $$$1 \rightarrow 3 \rightarrow 4 \rightarrow 3 \rightarrow 4 \rightarrow 3 \rightarrow 1$$$. The score of this closed path is $$$15$$$.
It can be proven that $$$15$$$ is the maximum possible score of a closed path.