problem_id
stringlengths 5
6
| problem_title
stringlengths 6
50
| difficulty
stringclasses 3
values | problem_statement
stringlengths 719
6.37k
| platform
stringclasses 1
value | link
stringlengths 48
49
|
---|---|---|---|---|---|
2025D | D. Attribute Checks | easy | Imagine a game where you play as a character that has two attributes: "Strength" and "Intelligence", that are at zero level initially.
During the game, you'll acquire $m$ attribute points that allow you to increase your attribute levels — one point will increase one of the attributes by one level. But sometimes, you'll encounter a so-called "Attribute Checks": if your corresponding attribute is high enough, you'll pass it; otherwise, you'll fail it.
Spending some time, you finally prepared a list which contains records of all points you got and all checks you've met. And now you're wondering: what is the maximum number of attribute checks you can pass in a single run if you'd spend points wisely?
Note that you can't change the order of records.
### Input
The first line contains two integers $n$ and $m$ ($1 \le m \le 5000$; $m < n \le 2 \cdot 10^6$) — the number of records in the list and the total number of points you'll get during the game.
The second line contains $n$ integers $r_1, r_2, \dots, r_n$ ($-m \le r_i \le m$), where $r_i$ encodes the $i$-th record:
- If $r_i = 0$, then the $i$-th record is an acquiring one attribute point. You can spend to level up either Strength or Intelligence;
- If $r_i > 0$, then it's an Intelligence check: if your Intelligence level is greater than or equal to $|r_i|$, you pass.
- If $r_i < 0$, then it's a Strength check: if your Strength level is greater than or equal to $|r_i|$, you pass.
Additional constraint on the input: the sequence $r_1, r_2, \dots, r_n$ contains exactly $m$ elements equal to $0$.
### Output
Print one integer — the maximum number of checks you can pass.
### Examples
#### Input #1
```
10 5
0 1 0 2 0 -3 0 -4 0 -5
```
#### Output #1
```
3
```
#### Input #2
```
3 1
1 -1 0
```
#### Output #2
```
0
```
#### Input #3
```
9 3
0 0 1 0 2 -3 -2 -2 1
```
#### Output #3
```
4
```
### Note
In the first test, it's optimal to spend each point in Strength, so you'll fail $2$ Intelligence checks but pass $3$ Strength checks.
In the second test, you'll fail both checks, since the first point you get comes after the checks.
In the third test, one of the optimal strategies is:
1. spend the first point on Intelligence;
2. spend the second point on Strength;
3. spend the third point on Strength;
As a result, you'll pass $2$ Intelligence checks $r_3$ and $r_9$ and $2$ Strength checks $r_7$ and $r_8$. | codeforces | https://codeforces.com/problemset/problem/2025/D |
2025A | A. Two Screens | easy | There are two screens which can display sequences of uppercase Latin letters. Initially, both screens display nothing.
In one second, you can do one of the following two actions:
- choose a screen and an uppercase Latin letter, and append that letter to the end of the sequence displayed on that screen;
- choose a screen and copy the sequence from it to the other screen, overwriting the sequence that was displayed on the other screen.
You have to calculate the minimum number of seconds you have to spend so that the first screen displays the sequence $s$, and the second screen displays the sequence $t$.
### Input
The first line contains one integer $q$ ($1 \le q \le 500$) — the number of test cases.
Each test case consists of two lines. The first line contains the string $s$, and the second line contains the string $t$ ($1 \le |s|, |t| \le 100$). Both strings consist of uppercase Latin letters.
### Output
For each test case, print one integer — the minimum possible number of seconds you have to spend so that the first screen displays the sequence $s$, and the second screen displays the sequence $t$.
### Example
#### Input #1
```
3
GARAGE
GARAGEFORSALE
ABCDE
AABCD
TRAINING
DRAINING
```
#### Output #1
```
14
10
16
```
### Note
In the first test case, the following sequence of actions is possible:
- spend $6$ seconds to write the sequence GARAGE on the first screen;
- copy the sequence from the first screen to the second screen;
- spend $7$ seconds to complete the sequence on the second screen by writing FORSALE.
In the second test case, the following sequence of actions is possible:
- spend $1$ second to write the sequence A on the second screen;
- copy the sequence from the second screen to the first screen;
- spend $4$ seconds to complete the sequence on the first screen by writing BCDE;
- spend $4$ seconds to complete the sequence on the second screen by writing ABCD.
In the third test case, the fastest way to display the sequences is to type both of them character by character without copying, and this requires $16$ seconds. | codeforces | https://codeforces.com/problemset/problem/2025/A |
2025C | C. New Game | easy | There's a new game Monocarp wants to play. The game uses a deck of $n$ cards, where the $i$-th card has exactly one integer $a_i$ written on it.
At the beginning of the game, on the first turn, Monocarp can take any card from the deck. During each subsequent turn, Monocarp can take exactly one card that has either the same number as on the card taken on the previous turn or a number that is one greater than the number on the card taken on the previous turn.
In other words, if on the previous turn Monocarp took a card with the number $x$, then on the current turn he can take either a card with the number $x$ or a card with the number $x + 1$. Monocarp can take any card which meets that condition, regardless of its position in the deck.
After Monocarp takes a card on the current turn, it is removed from the deck.
According to the rules of the game, the number of distinct numbers written on the cards that Monocarp has taken must not exceed $k$.
If, after a turn, Monocarp cannot take a card without violating the described rules, the game ends.
Your task is to determine the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 200\,000$) — the number of cards in the deck and the maximum number of distinct numbers that can be written on the cards that Monocarp takes.
The second line contains a sequence of integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^{9}$), where $a_i$ is the number written on the $i$-th card.
Additional constraint of the input: the sum of $n$ over all test cases doesn't exceed $200\,000$.
### Output
For each test case, print the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.
### Example
#### Input #1
```
4
10 2
5 2 4 3 4 3 4 5 3 2
5 1
10 11 10 11 10
9 3
4 5 4 4 6 5 4 4 6
3 2
1 3 1
```
#### Output #1
```
6
3
9
2
```
### Note
In the first example, Monocarp needs to take any of the cards with the number $3$. On the next two turns, he needs to take the two remaining cards with the number $3$. On the next three turns, he needs to take three cards with the number $4$. After that, Monocarp will not be able to take any more cards from the deck, and he will have $6$ cards. | codeforces | https://codeforces.com/problemset/problem/2025/C |
2025B | B. Binomial Coefficients, Kind Of | easy | Recently, akshiM met a task that needed binomial coefficients to solve. He wrote a code he usually does that looked like this:
```
for (int n = 0; n < N; n++) { // loop over n from 0 to N-1 (inclusive)
C\[n\]\[0\] = 1;
C\[n\]\[n\] = 1;
for (int k = 1; k < n; k++) // loop over k from 1 to n-1 (inclusive)
C\[n\]\[k\] = C\[n\]\[k - 1\] + C\[n - 1\]\[k - 1\];
}
```
Unfortunately, he made an error, since the right formula is the following:
```
C\[n\]\[k\] = C\[n - 1\]\[k\] + C\[n - 1\]\[k - 1\]
```
But his team member keblidA is interested in values that were produced using the wrong formula. Please help him to calculate these coefficients for $t$ various pairs $(n_i, k_i)$. Note that they should be calculated according to the first (wrong) formula.
Since values $C[n_i][k_i]$ may be too large, print them modulo $10^9 + 7$.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^5$) — the number of pairs. Next, $t$ pairs are written in two lines.
The second line contains $t$ integers $n_1, n_2, \dots, n_t$ ($2 \le n_i \le 10^5$).
The third line contains $t$ integers $k_1, k_2, \dots, k_t$ ($1 \le k_i < n_i$).
### Output
Print $t$ integers $C[n_i][k_i]$ modulo $10^9 + 7$.
### Example
#### Input #1
```
7
2 5 5 100000 100000 100000 100000
1 2 3 1 33333 66666 99999
```
#### Output #1
```
2
4
8
2
326186014
984426998
303861760
``` | codeforces | https://codeforces.com/problemset/problem/2025/B |
2022C | C. Gerrymandering | easy | We all steal a little bit. But I have only one hand, while my adversaries have two.
Álvaro Obregón
Álvaro and José are the only candidates running for the presidency of Tepito, a rectangular grid of $2$ rows and $n$ columns, where each cell represents a house. It is guaranteed that $n$ is a multiple of $3$.
Under the voting system of Tepito, the grid will be split into districts, which consist of any $3$ houses that are connected$^{\text{∗}}$. Each house will belong to exactly one district.
Each district will cast a single vote. The district will vote for Álvaro or José respectively if at least $2$ houses in that district select them. Therefore, a total of $\frac{2n}{3}$ votes will be cast.
As Álvaro is the current president, he knows exactly which candidate each house will select. If Álvaro divides the houses into districts optimally, determine the maximum number of votes he can get.
$^{\text{∗}}$A set of cells is connected if there is a path between any $2$ cells that requires moving only up, down, left and right through cells in the set.
### 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 one integer $n$ ($3 \le n \le 10^5$; $n$ is a multiple of $3$) — the number of columns of Tepito.
The following two lines each contain a string of length $n$. The $i$-th line contains the string $s_i$, consisting of the characters $\texttt{A}$ and $\texttt{J}$. If $s_{i,j}=\texttt{A}$, the house in the $i$-th row and $j$-th column will select Álvaro. Otherwise if $s_{i,j}=\texttt{J}$, the house will select José.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
### Output
For each test case, output a single integer — the maximum number of districts Álvaro can win by optimally dividing the houses into districts.
### Example
#### Input #1
```
4
3
AAA
AJJ
6
JAJAJJ
JJAJAJ
6
AJJJAJ
AJJAAA
9
AJJJJAJAJ
JAAJJJJJA
```
#### Output #1
```
2
2
3
2
```
### Note
The image below showcases the optimal arrangement of districts Álvaro can use for each test case in the example.
 | codeforces | https://codeforces.com/problemset/problem/2022/C |
2022A | A. Bus to Pénjamo | easy | Ya vamos llegando a Péeeenjamoo ♫♫♫
There are $n$ families travelling to Pénjamo to witness Mexico's largest-ever "walking a chicken on a leash" marathon. The $i$-th family has $a_i$ family members. All families will travel using a single bus consisting of $r$ rows with $2$ seats each.
A person is considered happy if:
- Another family member is seated in the same row as them, or
- They are sitting alone in their row (with an empty seat next to them).
Determine the maximum number of happy people in an optimal seating arrangement. Note that everyone must be seated in the bus.
It is guaranteed that all family members will fit on the bus. Formally, it is guaranteed that $\displaystyle\sum_{i=1}^{n}a_i \le 2r$.
### Input
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 $r$ ($1 \le n \le 100$; $1 \le r \le 500$) — the number of families and the number of rows in the bus.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10$) — the number of family members in each family.
### Output
For each test case, output the maximum number of happy people in an optimal seating arrangement.
### Example
#### Input #1
```
4
3 3
2 3 1
3 3
2 2 2
4 5
1 1 2 2
4 5
3 1 1 3
```
#### Output #1
```
4
6
6
6
```
### Note
In the first test case, the two members of the first family can sit together in the first row, while the two members of the second family can sit together in the second row. The remaining member of the second family can sit in the third row along with a member of the third family. This seating arrangement is shown below, where the $4$ happy people are colored green.
$\color{green}{1}
$$\color{green}{1}$$
color{green}{2}
$$\color{green}{2}$$
2$$3$
In the second test case, a possible seating arrangement with $6$ happy people is shown below.
$\color{green}{3}
$$\color{green}{3}$$
color{green}{1}
$$\color{green}{1}$$
color{green}{2}$$\color{green}{2}$
In the third test case, a possible seating arrangement with $6$ happy people is shown below.
$\color{green}{4}
$$\color{green}{4}$$
color{green}{}
$$\color{green}{2}$$
color{green}{3}
$$\color{green}{3}$$
color{green}{1}
$$\color{green}{}$$
color{green}{}$$\color{green}{}$ | codeforces | https://codeforces.com/problemset/problem/2022/A |
2022B | B. Kar Salesman | easy | Karel is a salesman in a car dealership. The dealership has $n$ different models of cars. There are $a_i$ cars of the $i$-th model. Karel is an excellent salesperson and can convince customers to buy up to $x$ cars (of Karel's choice), as long as the cars are from different models.
Determine the minimum number of customers Karel has to bring in to sell all the cars.
### 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 \le 5 \cdot 10^5$; $1 \le x \le 10$) — the number of different models of cars and the maximum number of cars Karel can convince a customer to buy.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the number of cars of each model.
It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
### Output
For each test case, output the minimum possible number of customers needed to sell all the cars.
### Example
#### Input #1
```
4
3 2
3 1 2
3 3
2 1 3
5 3
2 2 1 9 2
7 4
2 5 3 3 5 2 5
```
#### Output #1
```
3
3
9
7
```
### Note
For the first case, Karel only needs to lure in $3$ customers. He will convince the customers to buy the following models of cars:
- Customer $1$ buys $2$ cars with model $1$ and $3$.
- Customer $2$ buys $2$ cars with model $1$ and $2$.
- Customer $3$ buys $2$ cars with model $1$ and $3$.
For the second case, Karel only needs to lure in $3$ customers. He will convince the customers to buy the following models of cars:
- Customer $1$ buys $2$ cars with model $1$ and $3$.
- Customer $2$ buys $3$ cars with model $1$, $2$ and $3$.
- Customer $3$ buys $1$ car with model $3$. | codeforces | https://codeforces.com/problemset/problem/2022/B |
2021A | A. Meaning Mean | easy | Pak Chanek has an array $a$ of $n$ positive integers. Since he is currently learning how to calculate the floored average of two numbers, he wants to practice it on his array $a$.
While the array $a$ has at least two elements, Pak Chanek will perform the following three-step operation:
1. Pick two different indices $i$ and $j$ ($1 \leq i, j \leq |a|$; $i \neq j$), note that $|a|$ denotes the current size of the array $a$.
2. Append $\\lfloor \\frac{a\_i+a\_j}{2} \\rfloor$$^{\\text{∗}}$ to the end of the array.
3. Remove elements $a_i$ and $a_j$ from the array and concatenate the remaining parts of the array.
For example, suppose that $a=[5,4,3,2,1,1]$. If we choose $i=1$ and $j=5$, the resulting array will be $a=[4,3,2,1,3]$. If we choose $i=4$ and $j=3$, the resulting array will be $a=[5,4,1,1,2]$.
After all operations, the array will consist of a single element $x$. Find the maximum possible value of $x$ if Pak Chanek performs the operations optimally.
$^{\\text{∗}}$$\\lfloor x \\rfloor$ denotes the floor function of $x$, which is the greatest integer that is less than or equal to $x$. For example, $\\lfloor 6 \\rfloor = 6$, $\\lfloor 2.5 \\rfloor=2$, $\\lfloor -3.6 \\rfloor=-4$ and $\\lfloor \\pi \\rfloor=3$
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 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 \le a_i \le 10^9$) — the elements of the array $a$.
Do note that the sum of $n$ over all test cases is not bounded.
### Output
For each test case, output a single integer: the maximum possible value of $x$ after all numbers have been picked.
### Example
#### Input #1
```
3
5
1 7 8 4 5
3
2 6 5
5
5 5 5 5 5
```
#### Output #1
```
6
4
5
```
### Note
In the first test case, the array is initially $a=[1,7,8,4,5]$. Pak Chanek will perform the following operations:
1. Pick $i=1$ and $j=2$, then $a=[8,4,5,4]$.
2. Pick $i=3$ and $j=2$, then $a=[8,4,4]$.
3. Pick $i=2$ and $j=3$, then $a=[8,4]$.
4. Pick $i=1$ and $j=2$, then $a=[6]$.
After all the operations, the array consists of a single element $x=6$. It can be proven that there is no series of operations that results in $x$ greater than $6$ in the end. | codeforces | https://codeforces.com/problemset/problem/2021/A |
2021C1 | C1. Adjust The Presentation (Easy Version) | easy | This is the easy version of the problem. In the two versions, the constraints on $q$ and the time limit are different. In this version, $q=0$. You can make hacks only if all the versions of the problem are solved.
A team consisting of $n$ members, numbered from $1$ to $n$, is set to present a slide show at a large meeting. The slide show contains $m$ slides.
There is an array $a$ of length $n$. Initially, the members are standing in a line in the order of $a_1, a_2, \ldots, a_n$ from front to back. The slide show will be presented in order from slide $1$ to slide $m$. Each section will be presented by the member at the front of the line. After each slide is presented, you can move the member at the front of the line to any position in the lineup (without changing the order of the rest of the members). For example, suppose the line of members is $[\color{red}{3},1,2,4]$. After member $3$ presents the current slide, you can change the line of members into either $[\color{red}{3},1,2,4]$, $[1,\color{red}{3},2,4]$, $[1,2,\color{red}{3},4]$ or $[1,2,4,\color{red}{3}]$.
There is also an array $b$ of length $m$. The slide show is considered good if it is possible to make member $b_i$ present slide $i$ for all $i$ from $1$ to $m$ under these constraints.
However, your annoying boss wants to make $q$ updates to the array $b$. In the $i$-th update, he will choose a slide $s_i$ and a member $t_i$ and set $b_{s_i} := t_i$. Note that these updates are persistent, that is changes made to the array $b$ will apply when processing future updates.
For each of the $q+1$ states of array $b$, the initial state and after each of the $q$ updates, determine if the slideshow is good.
### 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 three integers $n$, $m$ and $q$ ($1 \le n, m \le 2 \cdot 10^5$; $q=0$) — the number of members, the number of sections and the number of updates.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le n$) — the initial order of the members from front to back. It is guaranteed that each integer from $1$ to $n$ appears exactly once in $a$.
The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le n$) — the members who should present each section.
It is guaranteed that the sum of $n$ and the sum of $m$ over all test cases do not exceed $2 \cdot 10^5$ respectively.
### Output
For each test case, output $q+1$ lines corresponding to the $q+1$ states of the array $b$. Output "YA" if the slide show is good, and "TIDAK" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yA", "Ya", "ya", and "YA" will be recognized as positive responses.
### Example
#### Input #1
```
3
4 2 0
1 2 3 4
1 1
3 6 0
1 2 3
1 1 2 3 3 2
4 6 0
3 1 4 2
3 1 1 2 3 4
```
#### Output #1
```
YA
YA
TIDAK
```
### Note
For the first test case, you do not need to move the members as both slides are presented by member $1$, who is already at the front of the line.
For the second test case, the following is a possible way to move members so that the presentation is good:
1. $[1,2,3]$, do not move member $1$.
2. $[1,2,3]$, move member $1$ after member $3$.
3. $[2,3,1]$, move member $2$ after member $3$.
4. $[3,2,1]$, do not move member $3$.
5. $[3,2,1]$, move member $3$ after member $1$.
6. $[2,1,3]$, do not move member $2$. | codeforces | https://codeforces.com/problemset/problem/2021/C1 |
2021B | B. Maximize Mex | easy | You are given an array $a$ of $n$ positive integers and an integer $x$. You can do the following two-step operation any (possibly zero) number of times:
1. Choose an index $i$ ($1 \leq i \leq n$).
2. Increase $a_i$ by $x$, in other words $a_i := a_i + x$.
Find the maximum value of the $\operatorname{MEX}$ of $a$ if you perform the operations optimally.
The $\operatorname{MEX}$ (minimum excluded value) of an array is the smallest non-negative integer that is not in the array. For example:
- The $\operatorname{MEX}$ of $[2,2,1]$ is $0$ because $0$ is not in the array.
- The $\operatorname{MEX}$ of $[3,1,0,1]$ is $2$ because $0$ and $1$ are in the array but $2$ is not.
- The $\operatorname{MEX}$ of $[0,3,1,2]$ is $4$ because $0$, $1$, $2$ and $3$ are in the array but $4$ is not.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 2 \cdot 10^5$; $1 \le x \le 10^9$) — the length of the array and the integer to be used in the operation.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the given array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output a single integer: the maximum $\operatorname{MEX}$ of $a$ if you perform the operations optimally.
### Example
#### Input #1
```
3
6 3
0 3 2 1 5 2
6 2
1 3 4 1 0 2
4 5
2 5 10 3
```
#### Output #1
```
4
6
0
```
### Note
In the first test case, the $\operatorname{MEX}$ of $a$ is $4$ without performing any operations, which is the maximum.
In the second test case, the $\operatorname{MEX}$ of $a$ is $5$ without performing any operations. If we perform two operations both with $i=1$, we will have the array $a=[5,3,4,1,0,2]$. Then, the $\operatorname{MEX}$ of $a$ will become $6$, which is the maximum.
In the third test case, the $\operatorname{MEX}$ of $a$ is $0$ without performing any operations, which is the maximum. | codeforces | https://codeforces.com/problemset/problem/2021/B |
2053I1 | I1. Affectionate Arrays (Easy Version) | medium | You are the beginning of the letter, the development of a poem, and the end of a fairy tale.
— ilem, [Pinky Promise](https://www.bilibili.com/video/BV1Jb411U7u2/)
This is the easy version of the problem. The difference between the versions is that in this version, you need to compute the minimum length of the arrays. You can hack only if you solved all versions of this problem.
Iris treasures an integer array $a_1, a_2, \ldots, a_n$. She knows this array has an interesting property: the maximum absolute value of all elements is less than or equal to the sum of all elements, that is, $\max(\lvert a_i\rvert) \leq \sum a_i$.
Iris defines the boredom of an array as its maximum subarray$^{\text{∗}}$ sum.
Iris's birthday is coming, and Victor is going to send her another array $b_1, b_2, \ldots, b_m$ as a gift. For some seemingly obvious reasons, he decides the array $b_1, b_2, \ldots, b_m$ should have the following properties.
- $a_1, a_2, \ldots, a_n$ should be a subsequence$^{\text{†}}$ of $b_1, b_2, \ldots, b_m$.
- The two arrays have the same sum. That is, $\sum\limits_{i=1}^n a_i = \sum\limits_{i=1}^m b_i$.
- The boredom of $b_1, b_2, \ldots, b_m$ is the smallest possible.
- Among the arrays with the smallest boredom, the length of the array $b$ (i.e., $m$) is the smallest possible. And in this case, Iris will understand his regard as soon as possible!
Even constrained as above, there are still too many possible gifts. So Victor asks you to compute the value of $\boldsymbol{m}$ of any array $b_1, b_2, \ldots, b_m$ satisfying all the conditions above. He promises you: if you help him successfully, he will share a bit of Iris's birthday cake with you.
Note: since the input is large, you may need to optimize it for this problem.
For example, in C++, it is enough to use the following lines at the start of the main() function:
```
`int main() {<br/> std::ios::sync_with_stdio(false);<br/> std::cin.tie(nullptr); std::cout.tie(nullptr);<br/>}<br/>````
$^{\text{∗}}$An array $c$ is a subarray of an array $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
$^{\text{†}}$A sequence $c$ is a subsequence of a sequence $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) element from arbitrary positions.
### Input
Each test contains multiple test cases. The first line of input contains an 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 \leq n \leq 3\cdot 10^6$) — the length of the array $a_1, a_2, \ldots, a_n$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) — the initial array. It is guaranteed that $\max(\lvert a_i\rvert) \leq \sum a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot 10^6$.
### Output
For each test case, output a single line containing an integer: the length $m$ of a valid array $b$.
### Example
#### Input #1
```
4
4
1 2 3 4
4
2 -3 2 2
10
2 -7 6 3 -1 4 2 -5 8 -4
20
4 -2 4 3 -2 1 5 2 3 6 -5 -1 -4 -2 -3 5 -3 1 -4 1
```
#### Output #1
```
4
6
14
25
```
### Note
In the first test case, $a=[1, 2, 3, 4]$. The only array $b$ which satisfies all the properties above is $[1, 2, 3, 4]$, so we should output $4$.
In the second test case, $a=[2, -3, 2, 2]$. The possible arrays $b$ are $[1, 2, -3, 2, -1, 2]$ and $[2, 1, -3, 2, -1, 2]$, so we should output $6$. | codeforces | https://codeforces.com/problemset/problem/2053/I1 |
2053E | E. Resourceful Caterpillar Sequence | medium | Endless Repeating 7 Days
— r-906, [Panopticon](https://www.youtube.com/watch?v=_-Vd0ZGB-lo)
There is a tree consisting of $n$ vertices. Let a caterpillar be denoted by an integer pair $(p, q)$ ($1 \leq p, q \leq n$, $p \neq q$): its head is at vertex $p$, its tail is at vertex $q$, and it dominates all the vertices on the simple path from $p$ to $q$ (including $p$ and $q$). The caterpillar sequence of $(p, q)$ is defined as the sequence consisting only of the vertices on the simple path, sorted in the ascending order of the distance to $p$.
Nora and Aron are taking turns moving the caterpillar, with Nora going first. Both players will be using his or her own optimal strategy:
- They will play to make himself or herself win;
- However, if it is impossible, they will play to prevent the other person from winning (thus, the game will end in a tie).
In Nora's turn, she must choose a vertex $u$ adjacent to vertex $p$, which is not dominated by the caterpillar, and move all the vertices in it by one edge towards vertex $u$$^{\\text{∗}}$. In Aron's turn, he must choose a vertex $v$ adjacent to vertex $q$, which is not dominated by the caterpillar, and move all the vertices in it by one edge towards vertex $v$. Note that the moves allowed to the two players are different.
Whenever $p$ is a leaf$^{\text{†}}$, Nora wins$^{\text{‡}}$. Whenever $q$ is a leaf, Aron wins. If either initially both $p$ and $q$ are leaves, or after $10^{100}$ turns the game has not ended, the result is a tie.
Please count the number of integer pairs $(p, q)$ with $1 \leq p, q \leq n$ and $p \neq q$ such that, if the caterpillar is initially $(p, q)$, Aron wins the game.
$^{\text{∗}}$In other words: Let the current caterpillar sequence be $c_1, c_2, \ldots, c_k$, then after the move, the new caterpillar sequence becomes $d(u, c_1), d(u, c_2), \ldots, d(u, c_k)$. Here, $d(x, y)$ is the next vertex on the simple path from $y$ to $x$.
$^{\text{†}}$In a tree, a vertex is called a leaf if and only if its degree is $1$.
$^{\text{‡}}$Therefore, Nora never fails to choose a vertex $u$ when the game has not ended. The same goes for Aron.
### Input
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \leq n \leq 2\cdot 10^5$) — the number of vertices in the tree.
The following $n - 1$ lines each contain two integers $u$ and $v$ ($1 \leq u, v \leq n$), denoting an edge between vertices $u$ and $v$. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $n$ over all test cases does not exceed $4\cdot 10^5$.
### Output
For each test case, output a single line containing an integer: the number of integer pairs $(p, q)$ which make Aron win.
### Example
#### Input #1
```
5
2
1 2
5
1 2
1 3
2 4
2 5
12
1 6
11 2
4 8
12 3
2 7
6 12
8 1
2 3
5 12
9 2
10 3
10
1 2
2 3
3 4
4 5
5 6
4 7
6 8
4 9
4 10
25
1 16
11 22
6 14
3 1
20 14
23 17
25 19
10 11
3 18
10 6
2 21
4 5
11 12
4 9
9 13
8 6
6 1
3 7
8 19
10 24
15 13
1 2
3 4
17 8
```
#### Output #1
```
0
6
40
27
171
```
### Note
In the first test case, all possible caterpillars are $(1, 2)$ and $(2, 1)$, resulting in a tie at the beginning, since both $p$ and $q$ are leaves.
In the second test case, the caterpillars that allow Aron to win are the following: $(1, 3)$, $(1, 4)$, $(1, 5)$, $(2, 3)$, $(2, 4)$, $(2, 5)$. Let's look at some specific caterpillars.
- For the caterpillar $(1, 5)$: vertex $p = 1$ is not a leaf, but vertex $q = 5$ is, so Aron wins at the beginning.
- For the caterpillar $(2, 1)$: vertex $p = 2$ is not a leaf, neither is vertex $q = 1$. In Nora's first move, she can choose to move the caterpillar towards vertex $5$, therefore the caterpillar becomes $(5, 2)$, and vertex $p = 5$ is a leaf, so Nora will win. | codeforces | https://codeforces.com/problemset/problem/2053/E |
2053F | F. Earnest Matrix Complement | medium | 3, 2, 1, ... We are the — RiOI Team!
— Felix & All, [Special Thanks 3](https://www.luogu.com.cn/problem/T351681)
- Peter: Good news: My problem T311013 is approved!
- $\delta$: I'm glad my computer had gone out of battery so that I wouldn't have participated in wyrqwq's round and gained a negative delta.
- Felix: \[thumbs\_up\] The problem statement concerning a removed song!
- Aquawave: Do I mourn my Chemistry?
- E.Space: ahh?
- Trine: Bread.
- Iris: So why am I always testing problems?
Time will pass, and we might meet again. Looking back at the past, everybody has lived the life they wanted.
Aquawave has a matrix $A$ of size $n\times m$, whose elements can only be integers in the range $[1, k]$, inclusive. In the matrix, some cells are already filled with an integer, while the rest are currently not filled, denoted by $-1$.
You are going to fill in all the unfilled places in $A$. After that, let $c_{u,i}$ be the number of occurrences of element $u$ in the $i$-th row. Aquawave defines the beauty of the matrix as
$$\sum_{u=1}^k \sum_{i=1}^{n-1} c_{u,i} \cdot c_{u,i+1}.$$
You have to find the maximum possible beauty of $A$ after filling in the blanks optimally.
### Input
The first line of input contains a single integer $t$ ($1 \leq t \leq 2\cdot 10^4$) — the number of test cases. The description of test cases follows.
The first line of each test case contains three integers $n$, $m$, and $k$ ($2 \leq n \leq 2\cdot 10^5$, $2 \leq m \leq 2\cdot 10^5$, $n \cdot m \leq 6\cdot 10^5$, $1 \leq k \leq n\cdot m$) — the number of rows and columns of the matrix $A$, and the range of the integers in the matrix, respectively.
Then $n$ lines follow, the $i$-th line containing $m$ integers $A_{i,1},A_{i,2},\ldots,A_{i,m}$ ($1 \leq A_{i,j} \leq k$ or $A_{i,j} = -1$) — the elements in $A$.
It is guaranteed that the sum of $n\cdot m$ over all test cases does not exceed $6\cdot 10^5$.
### Output
For each test case, output a single integer — the maximum possible beauty.
### Example
#### Input #1
```
9
3 3 3
1 2 2
3 1 3
3 2 1
2 3 3
-1 3 3
2 2 -1
3 3 6
-1 -1 1
1 2 -1
-1 -1 4
3 4 5
1 3 2 3
-1 -1 2 -1
3 1 5 1
5 3 8
5 -1 2
1 8 -1
-1 5 6
7 7 -1
4 4 4
6 6 5
-1 -1 5 -1 -1 -1
-1 -1 -1 -1 2 -1
-1 1 3 3 -1 -1
-1 1 -1 -1 -1 4
4 2 -1 -1 -1 4
-1 -1 1 2 -1 -1
6 6 4
-1 -1 -1 -1 1 -1
3 -1 2 2 4 -1
3 1 2 2 -1 -1
3 3 3 3 -1 2
-1 3 3 -1 1 3
3 -1 2 2 3 -1
5 5 3
1 1 3 -1 1
2 2 -1 -1 3
-1 -1 -1 2 -1
3 -1 -1 -1 2
-1 1 2 3 -1
6 2 7
-1 7
-1 6
7 -1
-1 -1
-1 -1
2 2
```
#### Output #1
```
4
4
10
10
8
102
93
58
13
```
### Note
In the first test case, the matrix $A$ is already determined. Its beauty is
$$\sum_{u=1}^k \sum_{i=1}^{n-1} c_{u,i} \cdot c_{u,i+1} = c_{1,1}\cdot c_{1,2} + c_{1,2}\cdot c_{1,3} + c_{2,1}\cdot c_{2,2} + c_{2,2}\cdot c_{2,3} + c_{3,1}\cdot c_{3,2} + c_{3,2}\cdot c_{3,3} = 1\cdot 1 + 1\cdot 1 + 2\cdot 0 + 0\cdot 1 + 0\cdot 2 + 2\cdot 1 = 4.$$
In the second test case, one can fill the matrix as follows:
$$ \begin{bmatrix} 2 &3 &3 \\ 2 &2 &3 \end{bmatrix}, $$
and get the value $4$. It can be proven this is the maximum possible answer one can get.
In the third test case, one of the possible optimal configurations is:
$$ \begin{bmatrix} 1 &1 &1 \\ 1 &2 &1 \\ 1 &1 &4 \end{bmatrix}. $$
In the fourth test case, one of the possible optimal configurations is:
$$ \begin{bmatrix} 1 &3 &2 &3 \\ 1 &3 &2 &1 \\ 3 &1 &5 &1 \end{bmatrix}. $$
In the fifth test case, one of the possible optimal configurations is:
$$ \begin{bmatrix} 5 &5 &2 \\ 1 &8 &5 \\ 7 &5 &6 \\ 7 &7 &4 \\ 4 &4 &4 \end{bmatrix}. $$
| codeforces | https://codeforces.com/problemset/problem/2053/F |
2043E | E. Matrix Transformation | medium | You are given two matrices $A$ and $B$ of size $n \times m$, filled with integers between $0$ and $10^9$. You can perform the following operations on matrix $A$ in any order and any number of times:
- &=: choose two integers $i$ and $x$ ($1 \le i \le n$, $x \ge 0$) and replace each element in row $i$ with the result of the bitwise AND operation between $x$ and that element. Formally, for every $j \in [1, m]$, the element $A_{i,j}$ is replaced with $A_{i,j} \text{ & } x$;
- \|=: choose two integers $j$ and $x$ ($1 \le j \le m$, $x \ge 0$) and replace each element in column $j$ with the result of the bitwise OR operation between $x$ and that element. Formally, for every $i \in [1, n]$, the element $A_{i,j}$ is replaced with $A_{i,j} \text{ | } x$.
The value of $x$ may be chosen differently for different operations.
Determine whether it is possible to transform matrix $A$ into matrix $B$ using the given operations any number of times (including zero).
### Input
The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. Then, $t$ test cases follow.
Each test case is given as follows:
- the first line contains two integers $n$ and $m$ ($1 \le n, m \le 10^3$; $n \cdot m \le 10^3$) — the dimensions of the matrices $A$ and $B$;
- the following $n$ lines describe the matrix $A$, where the $i$-th line contains $m$ integers $A_{i,1}, A_{i,2}, \dots, A_{i,m}$ ($0 \le A_{i,j} \le 10^9$);
- the following $n$ lines describe the matrix $B$, where the $i$-th line contains $m$ integers $B_{i,1}, B_{i,2}, \dots, B_{i,m}$ ($0 \le B_{i,j} \le 10^9$).
### Output
For each test case, output Yes if it is possible to transform the matrix $A$ into the matrix $B$; otherwise, output No. Each letter can be output in any case, upper or lower.
### Example
#### Input #1
```
4
1 1
12
13
2 2
10 10
42 42
21 21
21 21
2 2
74 10
42 106
21 85
85 21
2 4
1 2 3 4
5 6 7 8
3 2 3 4
1 0 1 0
```
#### Output #1
```
Yes
Yes
No
Yes
```
### Note
Let's consider the second set of input data and show a sequence of operations that transforms matrix $A$ into matrix $B$:
Initially, the matrix looks like this:
$\begin{bmatrix} 10&10\\ 42&42\\ \end{bmatrix}$
Apply an operation of the first type with parameters $i = 1$ and $x = 0$. As a result, we get the matrix:
$\begin{bmatrix} 0&0\\ 42&42\\ \end{bmatrix}$
Apply an operation of the first type with parameters $i = 2$ and $x = 0$. As a result, we get the matrix:
$\begin{bmatrix} 0&0\\ 0&0\\ \end{bmatrix}$
Apply an operation of the second type with parameters $j = 1$ and $x = 21$. As a result, we get the matrix:
$\begin{bmatrix} 21&0\\ 21&0\\ \end{bmatrix}$
Apply an operation of the second type with parameters $j = 2$ and $x = 21$. As a result, we get the matrix:
$\begin{bmatrix} 21&21\\ 21&21\\ \end{bmatrix}$
Thus, we have transformed matrix $A$ into matrix $B$. | codeforces | https://codeforces.com/problemset/problem/2043/E |
2043G | G. Problem with Queries | medium | You are given an array $a$, consisting of $n$ integers. Your task is to process $q$ queries of two types:
- $1~p~x$ — set the value of the element at index $p$ equal to $x$;
- $2~l~r$ — count the number of pairs of indices $(i, j)$ such that $l \le i < j \le r$ and $a_i \ne a_j$.
Note that the queries in this task are encoded; each subsequent query can only be decoded after calculating the answer to the preceding query of the second type.
### Input
The first line contains a single integer $n$ ($1 \le n \le 10^5$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$).
The third line contains a single integer $q$ ($1 \le q \le 3 \cdot 10^5$) — the number of queries.
The next $q$ lines describe the queries in one of the following formats:
- $1~p'~x'$ ($0 \le p', x' \le n-1$);
- $2~l'~r'$ ($0 \le l', r' \le n-1$).
The queries are encoded as follows: let $\mathit{last}$ be the answer to the latest processed query of the second type (initially, $\mathit{last} = 0$).
- if the type of the query is $1$, then $p = ((p' + \mathit{last}) \bmod n) + 1$, $x = ((x' + \mathit{last}) \bmod n) + 1$.
- if the type of the query is $2$, $l = ((l' + \mathit{last}) \bmod n) + 1$, $r = ((r' + \mathit{last}) \bmod n) + 1$. If $l > r$, swap their values.
Don't forget to update the value of $\mathit{last}$ after answering each query of the second type.
Additional constraint on the input: there is at least one query of the second type.
### Output
For each query of the second type, print the answer — the number of pairs of indices $(i, j)$ such that $l \le i < j \le r$ and $a_i \ne a_j$.
### Examples
#### Input #1
```
3
1 2 3
5
2 0 2
1 0 2
2 0 2
1 2 0
2 1 0
```
#### Output #1
```
3 2 0 ```
#### Input #2
```
7
1 3 4 4 7 1 3
3
2 1 6
2 1 0
2 5 6
```
#### Output #2
```
13 18 0 ```
### Note
In the first example, the actual queries (after decoding) are:
- 2 1 3
- 1 1 3
- 2 1 3
- 1 2 3
- 2 1 3 | codeforces | https://codeforces.com/problemset/problem/2043/G |
2043F | F. Nim | medium | Recall the rules of the game "Nim". There are $n$ piles of stones, where the $i$-th pile initially contains some number of stones. Two players take turns choosing a non-empty pile and removing any positive (strictly greater than $0$) number of stones from it. The player unable to make a move loses the game.
You are given an array $a$, consisting of $n$ integers. Artem and Ruslan decided to play Nim on segments of this array. Each of the $q$ rounds is defined by a segment $(l_i, r_i)$, where the elements $a_{l_i}, a_{l_i+1}, \dots, a_{r_i}$ represent the sizes of the piles of stones.
Before the game starts, Ruslan can remove any number of piles from the chosen segment. However, at least one pile must remain, so in a single round he can remove at most $(r_i - l_i)$ piles. He is allowed to remove $0$ piles. After the removal, the game is played on the remaining piles within the segment.
All rounds are independent: the changes made in one round do not affect the original array or any other rounds.
Ruslan wants to remove as many piles as possible so that Artem, who always makes the first move, loses.
For each round, determine:
1. the maximum number of piles Ruslan can remove;
2. the number of ways to choose the maximum number of piles for removal.
Two ways are considered different if there exists an index $i$ such that the pile at index $i$ is removed in one way but not in the other. Since the number of ways can be large, output it modulo $998\,244\,353$.
If Ruslan cannot ensure Artem's loss in a particular round, output -1 for that round.
### Input
The first line of input contains two integers $n$ and $q$ ($1 \le n, q \le 10^5$) — the size of the array and the number of segments for which the answers need to be calculated.
The second line of input contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 50$) — the elements of the initial array.
The $i$-th of the next $q$ lines contains two integers $l_i, r_i$ ($1 \le l_i \le r_i \le n$) — the bounds of the segment on which the boys want to play the game during the $i$-th round.
### Output
For each round:
- if Ruslan can win, print two integers — the maximum number of piles that can be removed, and the number of ways to remove the maximum number of piles, taken modulo $998\,244\,353$;
- otherwise print -1.
### Example
#### Input #1
```
9 5
0 1 2 1 3 4 5 6 0
1 5
2 5
3 5
4 5
1 9
```
#### Output #1
```
4 1
2 1
0 1
-1
8 2
``` | codeforces | https://codeforces.com/problemset/problem/2043/F |
2051G | G. Snakes | medium | Suppose you play a game where the game field looks like a strip of $1 \times 10^9$ square cells, numbered from $1$ to $10^9$.
You have $n$ snakes (numbered from $1$ to $n$) you need to place into some cells. Initially, each snake occupies exactly one cell, and you can't place more than one snake into one cell. After that, the game starts.
The game lasts for $q$ seconds. There are two types of events that may happen each second:
- snake $s_i$ enlarges: if snake $s_i$ occupied cells $[l, r]$, it enlarges to a segment $[l, r + 1]$;
- snake $s_i$ shrinks: if snake $s_i$ occupied cells $[l, r]$, it shrinks to a segment $[l + 1, r]$.
Each second, exactly one of the events happens.
If at any moment of time, any snake runs into some obstacle (either another snake or the end of the strip), you lose. Otherwise, you win with the score equal to the maximum cell occupied by any snake so far.
What is the minimum possible score you can achieve?
### Input
The first line contains two integers $n$ and $q$ ($1 \le n \le 20$; $1 \le q \le 2 \cdot 10^5$) — the number of snakes and the number of events. Next $q$ lines contain the description of events — one per line.
The $i$-th line contains
- either "$s_i$ +" ($1 \le s_i \le n$) meaning that the $s_i$-th snake enlarges
- or "$s_i$ -" ($1 \le s_i \le n$) meaning that the $s_i$-th snake shrinks.
Additional constraint on the input: the given sequence of events is valid, i. e. a snake of length $1$ never shrinks.
### Output
Print one integer — the minimum possible score.
### Examples
#### Input #1
```
3 6
1 +
1 -
3 +
3 -
2 +
2 -
```
#### Output #1
```
4
```
#### Input #2
```
5 13
5 +
3 +
5 -
2 +
4 +
3 +
5 +
5 -
2 +
3 -
3 +
3 -
2 +
```
#### Output #2
```
11
```
### Note
In the first test, the optimal strategy is to place the second snake at cell $1$, the third snake — at $2$, and the first one — at $3$. The maximum occupied cell is cell $4$, and it's the minimum possible score.
In the second test, one of the optimal strategies is to place:
- snake $2$ at position $1$;
- snake $3$ at position $4$;
- snake $5$ at position $6$;
- snake $1$ at position $9$;
- snake $4$ at position $10$. | codeforces | https://codeforces.com/problemset/problem/2051/G |
2051F | F. Joker | medium | Consider a deck of $n$ cards. The positions in the deck are numbered from $1$ to $n$ from top to bottom. A joker is located at position $m$.
$q$ operations are applied sequentially to the deck. During the $i$-th operation, you need to take the card at position $a_i$ and move it either to the beginning or to the end of the deck. For example, if the deck is $[2, 1, 3, 5, 4]$, and $a_i=2$, then after the operation the deck will be either $[1, 2, 3, 5, 4]$ (the card from the second position moved to the beginning) or $[2, 3, 5, 4, 1]$ (the card from the second position moved to the end).
Your task is to calculate the number of distinct positions where the joker can be after each operation.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $q$ ($2 \le n \le 10^9$; $1 \le m \le n$; $1 \le q \le 2 \cdot 10^5$).
The second line contains $q$ integers $a_1, a_2, \dots, a_q$ ($1 \le a_i \le n$).
Additional constraint on the input: the sum of $q$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, print $q$ integers — the number of distinct positions where the joker can be after each operation.
### Example
#### Input #1
```
5
6 5 3
1 2 3
2 1 4
2 1 1 2
5 3 1
3
3 2 4
2 1 1 1
18 15 4
13 15 1 16
```
#### Output #1
```
2 3 5
2 2 2 2
2
2 3 3 3
2 4 6 8
``` | codeforces | https://codeforces.com/problemset/problem/2051/F |
2049F | F. MEX OR Mania | medium | An integer sequence $b_1, b_2, \ldots, b_n$ is good if $\operatorname{mex}(b_1, b_2, \ldots, b_n) - (b_1 | b_2 | \ldots | b_n) = 1$. Here, $\operatorname{mex(c)}$ denotes the MEX$^{\text{∗}}$ of the collection $c$, and $|$ is the [bitwise OR](https://en.wikipedia.org/wiki/Bitwise_operation#OR) operator.
Shohag has an integer sequence $a_1, a_2, \ldots, a_n$. He will perform the following $q$ updates on $a$:
- $i$ $x$ — increase $a_i$ by $x$.
After each update, help him find the length of the longest good subarray$^{\text{†}}$ of $a$.
$^{\text{∗}}$The minimum excluded (MEX) of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest non-negative integer $y$ which does not occur in the collection $c$.
$^{\text{†}}$An array $d$ is a subarray of an array $f$ if $d$ can be obtained from $f$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements 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 space-separated integers $n$ and $q$ ($1 \le n, q \le 10^5$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le n$).
The next $q$ lines of each test case are of the following form:
- $i$ $x$ ($1 \le i, x \le n$) — it means you should increase $a_i$ by $x$.
It is guaranteed that the sum of $n$ over all test cases doesn't exceed $10^5$ and the sum of $q$ doesn't exceed $10^5$.
### Output
For each test case, output $q$ lines — on the $i$-th line output the length of the longest good subarray of $a$ after the $i$-th update.
### Example
#### Input #1
```
2
6 3
0 0 1 0 1 0
6 1
3 2
6 3
3 1
1 3 1
1 1
```
#### Output #1
```
6
3
2
0
```
### Note
In the first test case, after the first update, the array becomes $[0, 0, 1, 0, 1, 1]$, and here the whole array is good because $\operatorname{mex}([0, 0, 1, 0, 1, 1]) - (0 | 0 | 1 | 0 | 1 | 1) = 2 - 1 = 1$.
After the second update, the array becomes $[0, 0, 3, 0, 1, 1]$, and here the subarray $[0, 1, 1]$ has the maximum length among all the good subarrays.
Finally, after the third update, the array becomes $[0, 0, 3, 0, 1, 4]$, and here the subarrays $[0, 0]$ and $[0, 1]$ both have the maximum length among all the good subarrays. | codeforces | https://codeforces.com/problemset/problem/2049/F |
2049D | D. Shift + Esc | medium | After having fun with a certain contraption and getting caught, Evirir the dragon decides to put their magical skills to good use — warping reality to escape fast!
You are given a grid with $n$ rows and $m$ columns of non-negative integers and an integer $k$. Let $(i, j)$ denote the cell in the $i$-th row from the top and $j$-th column from the left ($1 \le i \le n$, $1 \le j \le m$). For every cell $(i, j)$, the integer $a_{i, j}$ is written on the cell $(i, j)$.
You are initially at $(1, 1)$ and want to go to $(n, m)$. You may only move down or right. That is, if you are at $(i, j)$, you can only move to $(i+1, j)$ or $(i, j+1)$ (if the corresponding cell exists).
Before you begin moving, you may do the following operation any number of times:
- Choose an integer $i$ between $1$ and $n$ and cyclically shift row $i$ to the left by $1$. Formally, simultaneously set $a_{i,j}$ to $a_{i,(j \bmod m) + 1}$ for all integers $j$ ($1 \le j \le m$).
Note that you may not do any operation after you start moving.
After moving from $(1, 1)$ to $(n, m)$, let $x$ be the number of operations you have performed before moving, and let $y$ be the sum of the integers written on visited cells (including $(1, 1)$ and $(n, m)$). Then the cost is defined as $kx + y$.
Find the minimum cost to move from $(1, 1)$ to $(n, m)$.
### 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 contains three space-separated integers $n$, $m$, and $k$ ($1 \leq n, m \leq 200$, $0 \leq k \leq 10^9$).
Then, $n$ lines follow. The $i$-th line contains $m$ space-separated integers, $a_{i,1},\,a_{i,2},\,\ldots,\,a_{i,m}$ ($0 \leq a_{i,j} \leq 10^9$).
It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $5 \cdot 10^4$.
### Output
For each test case, output a single integer, the minimum cost to move from $(1, 1)$ to $(n, m)$.
### Example
#### Input #1
```
5
3 3 100
3 4 9
5 2 4
0 101 101
3 4 1
10 0 0 10
0 0 10 0
10 10 0 10
1 1 3
4
3 2 3
1 2
3 6
5 4
10 10 14
58 49 25 12 89 69 8 49 71 23
45 27 65 59 36 100 73 23 5 84
82 91 54 92 53 15 43 46 11 65
61 69 71 87 67 72 51 42 55 80
1 64 8 54 61 70 47 100 84 50
86 93 43 51 47 35 56 20 33 61
100 59 5 68 15 55 69 8 8 60
33 61 20 79 69 51 23 24 56 28
67 76 3 69 58 79 75 10 65 63
6 64 73 79 17 62 55 53 61 58
```
#### Output #1
```
113
6
4
13
618
```
### Note
In the first test case, the minimum cost of $113$ can be achieved as follows:
1. Cyclically shift row 3 once. The grid now becomes
$$\begin{bmatrix}3 & 4 & 9\\5 & 2 & 4\\101 & 101 & 0\end{bmatrix}.$$
2. Move as follows: $(1, 1) \to (1, 2) \to (2, 2) \to (2, 3) \to (3, 3)$.
$x = 1$ operation is done before moving. The sum of integers on visited cells is $y = 3 + 4 + 2 + 4 + 0 = 13$. Therefore, the cost is $kx + y = 100 \cdot 1 + 13 = 113$.
In the second test case, one can shift row 1 once, row 2 twice, and row 3 thrice. Then, the grid becomes
$$\begin{bmatrix}0 & 0 & 10 & 10\\10 & 0 & 0 & 0\\10 & 10 & 10 & 0\end{bmatrix}.$$
x = 6$ operations were done before moving, and there is a path of cost $y = 0$. Therefore, the cost is $6 \cdot 1 + 0 = 6$. | codeforces | https://codeforces.com/problemset/problem/2049/D |
2049E | E. Broken Queries | medium | You, a wizard whose creation was destroyed by a dragon, are determined to hunt it down with a magical AOE tracker. But it seems to be toyed with...
This is an interactive problem.
There is a hidden binary array $a$ of length $n$ ($\mathbf{n}$ is a power of 2) and a hidden integer $k\ (2 \le k \le n - 1)$. The array $a$ contains exactly one 1 (and all other elements are 0). For two integers $l$ and $r$ ($1 \le l \le r \le n$), define the range sum $s(l, r) = a_l + a_{l+1} + \cdots + a_r$.
You have a magical device that takes ranges and returns range sums, but it returns the opposite result when the range has length at least $k$. Formally, in one query, you can give it a pair of integers $[l, r]$ where $1 \le l \le r \le n$, and it will return either $0$ or $1$ according to the following rules:
- If $r - l + 1 < k$, it will return $s(l, r)$.
- If $r - l + 1 \ge k$, it will return $1 - s(l, r)$.
Find $k$ using at most $33$ queries.
The device is not adaptive. It means that the hidden $a$ and $k$ are fixed before the interaction and will not change during the interaction.
### Interaction
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 positive integer $n$ ($4 \le n \le 2^{30}$) — the length of the hidden array. It is guaranteed that $\mathbf{n}$ is a power of 2; that is, $n = 2^m$ for some non-negative integer $m$.
You can make queries in the following way — print one line of the form "$\mathtt{?}\,l\,r$" where $1 \le l \le r \le n$. After that, read a single integer: $0$ or $1$, as described in the statement.
If you want to print the answer $k$, output "$\mathtt{!}\,k$". Then, the interaction continues with the next test case.
Printing the answer does not count towards the number of queries made.
After printing each query do not forget to output the end of line and flush$^{\text{∗}}$ the output. Otherwise, you will get Idleness limit exceeded verdict.
If, at any interaction step, you read $-1$ instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.
Hacks
The format of the hacks should be the following: the first line should contain one integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of the test cases should follow.
The first and only line of each test case should contain three integers $n$, $p$, and $k$ ($4 \le n \le 2^{30}$, $1 \le p \le n$, $2 \le k \le n - 1$) — the length of the hidden array $a$, the position of the only 1 in $a$, and the hidden $k$. $n$ must be a power of $2$.
$^{\text{∗}}$To flush, use:
- fflush(stdout) or cout.flush() in C++;
- sys.stdout.flush() in Python;
- see the documentation for other languages.
### Example
#### Input #1
```
2
8
0
0
1
0
4
1
0```
#### Output #1
```
? 3 5
? 1 8
? 4 8
? 3 8
! 6
? 3 3
? 3 4
! 2
```
### Note
In the first test case, $k = 6$ and the 1 in the hidden array is at index 6, so $a = [0, 0, 0, 0, 0, 1, 0, 0]$.
- For the query 3 5, since $5-3+1 = 3 < k$, the device answers correctly. Since 6 is not contained in the range $[3, 5]$, the device answers $0$.
- For the query 1 8, since $8 - 1 + 1 = 8 \ge k$, the device answers $0$ incorrectly.
- For the query 4 8, since $8 - 4 + 1 = 5 < k$, the device answers $1$ correctly.
- For the query 3 8, since $8 - 3 + 1 = 6 \ge k$, the device answers $0$ incorrectly.
The example solution then outputs $6$ as the answer, which is correct.
In the second test case, $k = 2$ and the 1 in the hidden array is at index 3, so $a = [0, 0, 1, 0]$.
Note that the example solution may not have enough information to determine $k$ above; this is only an example. | codeforces | https://codeforces.com/problemset/problem/2049/E |
2048G | G. Kevin and Matrices | medium | Kevin has been transported to Sacred Heart Hospital, which contains all the $ n \times m $ matrices with integer values in the range $ [1,v] $.
Now, Kevin wants to befriend some matrices, but he is willing to befriend a matrix $ a $ if and only if the following condition is satisfied:
$$ \min_{1\le i\le n}\left(\max_{1\le j\le m}a_{i,j}\right)\le\max_{1\le j\le m}\left(\min_{1\le i\le n}a_{i,j}\right). $$
Please count how many matrices in Sacred Heart Hospital can be friends with Kevin.
Since Kevin is very friendly, there could be many matrices that meet this condition. Therefore, you only need to output the result modulo $998\,244\,353$.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $ t $ ($ 1 \le t \le 8\cdot 10^3 $).
The only line of each test case contains three integers $n$, $m$, $v$ ($ 1 \le n, v, n \cdot v \leq 10^6$, $1 \le m \le 10^9 $).
It is guaranteed that the sum of $ n \cdot v $ over all test cases doesn't exceed $ 10^6 $.
### Output
For each test case, output one integer — the number of matrices that can be friends with Kevin modulo $998\,244\,353$.
### Example
#### Input #1
```
3
2 2 2
2 3 4
11 45 14
```
#### Output #1
```
14
2824
883799966
```
### Note
In the first test case, besides the matrices $ a=\begin{bmatrix}1&2\\2&1\end{bmatrix} $ and $ a=\begin{bmatrix}2&1\\1&2\end{bmatrix} $, which do not satisfy the condition, the remaining $ 2^{2 \cdot 2} - 2 = 14 $ matrices can all be friends with Kevin. | codeforces | https://codeforces.com/problemset/problem/2048/G |
2048F | F. Kevin and Math Class | medium | Kevin is a student from Eversleeping Town, currently attending a math class where the teacher is giving him division exercises.
On the board, there are two rows of positive integers written, each containing $ n $ numbers. The first row is $ a_1, a_2, \ldots, a_n $, and the second row is $ b_1, b_2, \ldots, b_n $.
For each division exercise, Kevin can choose any segment $ [l, r] $ and find the smallest value $ x $ among $ b_l, b_{l+1}, \ldots, b_r $. He will then modify each $ a_i $ for $ l \leq i \leq r $ to be the ceiling of $ a_i $ divided by $ x $.
Formally, he selects two integers $ 1 \leq l \leq r \leq n $, sets $ x = \min_{l \leq i \leq r} b_i $, and changes all $ a_i $ for $ l \leq i \leq r $ to $ \lceil \frac{a_i}{x} \rceil $.
Kevin can leave class and go home when all $ a_i $ become $ 1 $. He is eager to leave and wants to know the minimum number of division exercises required to achieve this.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $ t $ ($ 1 \le t \le 10^4 $).
The first line of each test case contains an integer $ n $ ($ 1 \le n \leq 2 \cdot 10^5 $) — the length of the sequence $ a $ and $ b $.
The second line of each test case contains $ n $ integers $ a_1, a_2, \ldots, a_n $ ($ 1 \le a_i \le 10^{18} $) — the first row of integers on the board.
The third line of each test case contains $ n $ integers $ b_1, b_2, \ldots, b_n $ ($ 2 \le b_i \le 10^{18} $) — the second row of integers on the board.
It is guaranteed that the sum of $ n $ over all test cases doesn't exceed $ 2 \cdot 10^5 $.
### Output
For each test case, output one integer — the minimum number of division exercises required to leave class.
### Example
#### Input #1
```
3
3
5 4 2
6 3 2
5
3 6 1 3 2
3 5 3 2 2
6
8 3 3 7 5 8
3 2 3 4 2 3
```
#### Output #1
```
2
3
3
```
### Note
For the first test case: $ [{\color{red}{5,4}},2]\xrightarrow[\min(b_1,b_2)=3]{\text{operate segment }[1,2]}[{\color{red}{2,2,2}}]\xrightarrow[\min(b_1,b_2,b_3)=2]{\text{operate segment }[1,3]}[1,1,1] $.
For the second test case: $ [{\color{red}{3,6,1}},3,2]\xrightarrow[\min(b_1,b_2,b_3)=3]{\text{operate segment }[1,3]}[1,{\color{red}{2,1,3}},2]\xrightarrow[\min(b_2,b_3,b_4)=2]{\text{operate segment }[2,4]}[1,1,1,{\color{red}{2,2}}]\xrightarrow[\min(b_4,b_5)=2]{\text{operate segment }[4,5]}[1,1,1,1,1] $. | codeforces | https://codeforces.com/problemset/problem/2048/F |
2048E | E. Kevin and Bipartite Graph | medium | The Arms Factory needs a poster design pattern and finds Kevin for help.
A poster design pattern is a bipartite graph with $ 2n $ vertices in the left part and $ m $ vertices in the right part, where there is an edge between each vertex in the left part and each vertex in the right part, resulting in a total of $ 2nm $ edges.
Kevin must color each edge with a positive integer in the range $ [1, n] $. A poster design pattern is good if there are no monochromatic cycles$^{\text{∗}}$ in the bipartite graph.
Kevin needs your assistance in constructing a good bipartite graph or informing him if it is impossible.
$^{\text{∗}}$A monochromatic cycle refers to a simple cycle in which all the edges are colored with the same color.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $ t $ ($ 1 \le t \le 100 $).
The only line of each test case contains two integers $ n $ and $ m $ ($ 1 \le n, m \leq 10^3 $) — the bipartite graph has $ 2n $ vertices in the left part and $ m $ vertices in the right part.
It is guaranteed that both the sum of $ n $ and the sum of $ m $ over all test cases do not exceed $ 10^3 $.
### Output
For each test case, if there is no solution, then output No.
Otherwise, output Yes, and then output $ 2n $ lines, with each line containing $ m $ positive integers. The $ i $-th line's $ j $-th integer represents the color of the edge between the $ i $-th vertex in the left part and the $ j $-th vertex in the right part.
If there are multiple answers, you can print any of them.
You can output each letter in any case (for example, the strings yEs, yes, Yes, and YES will be recognized as a positive answer).
### Example
#### Input #1
```
3
2 2
3 7
5 4
```
#### Output #1
```
YES
1 2
2 1
2 2
2 1
NO
YES
1 1 1 1
1 2 2 2
1 2 3 3
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
```
### Note
For the first test case, the graph is shown as follows:

For the second test case, it can be proven that there is no valid solution. | codeforces | https://codeforces.com/problemset/problem/2048/E |
2044F | F. Easy Demon Problem | medium | For an arbitrary grid, Robot defines its beauty to be the sum of elements in the grid.
Robot gives you an array $a$ of length $n$ and an array $b$ of length $m$. You construct a $n$ by $m$ grid $M$ such that $M_{i,j}=a_i\cdot b_j$ for all $1 \leq i \leq n$ and $1 \leq j \leq m$.
Then, Robot gives you $q$ queries, each consisting of a single integer $x$. For each query, determine whether or not it is possible to perform the following operation exactly once so that $M$ has a beauty of $x$:
1. Choose integers $r$ and $c$ such that $1 \leq r \leq n$ and $1 \leq c \leq m$
2. Set $M_{i,j}$ to be $0$ for all ordered pairs $(i,j)$ such that $i=r$, $j=c$, or both.
Note that queries are not persistent, meaning that you do not actually set any elements to $0$ in the process — you are only required to output if it is possible to find $r$ and $c$ such that if the above operation is performed, the beauty of the grid will be $x$. Also, note that you must perform the operation for each query, even if the beauty of the original grid is already $x$.
### Input
The first line contains three integers $n$, $m$, and $q$ ($1 \leq n,m \leq 2\cdot 10^5, 1 \leq q \leq 5\cdot 10^4$) — the length of $a$, the length of $b$, and the number of queries respectively.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq |a_i| \leq n$).
The third line contains $m$ integers $b_1, b_2, \ldots, b_m$ ($0 \leq |b_i| \leq m$).
The following $q$ lines each contain a single integer $x$ ($1 \leq |x| \leq 2\cdot 10^5$), the beauty of the grid you wish to achieve by setting all elements in a row and a column to $0$.
### Output
For each testcase, output "YES" (without quotes) if there is a way to perform the aforementioned operation such that the beauty is $x$, 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).
### Examples
#### Input #1
```
3 3 6
-2 3 -3
-2 2 -1
-1
1
-2
2
-3
3
```
#### Output #1
```
NO
YES
NO
NO
YES
NO
```
#### Input #2
```
5 5 6
1 -2 3 0 0
0 -2 5 0 -3
4
-3
5
2
-1
2
```
#### Output #2
```
YES
YES
YES
YES
NO
YES
```
### Note
In the second example, the grid is
0 -2 5 0 -3
0 4 -10 0 6
0 -6 15 0 -9
0 0 0 0 0
0 0 0 0 0
By performing the operation with $r=4$ and $c=2$, we create the following grid:
0 0 5 0 -3
0 0 -10 0 6
0 0 15 0 -9
0 0 0 0 0
0 0 0 0 0
which has beauty $4$. Thus, we output YES.
In the second query, selecting $r=3$ and $c=5$ creates a grid with beauty $-3$.
In the third query, selecting $r=3$ and $c=3$ creates a grid with beauty $5$. | codeforces | https://codeforces.com/problemset/problem/2044/F |
2044G2 | G2. Medium Demon Problem (hard version) | medium | This is the hard version of the problem. The key difference between the two versions is highlighted in bold.
A group of $n$ spiders has come together to exchange plushies. Initially, each spider has $1$ plushie. Every year, if spider $i$ has at least one plushie, he will give exactly one plushie to spider $r_i$. Otherwise, he will do nothing. Note that all plushie transfers happen at the same time. In this version, each spider is allowed to have more than 1 plushie at any point in time.
The process is stable in the current year if each spider has the same number of plushies (before the current year's exchange) as he did the previous year (before the previous year's exchange). Note that year $1$ can never be stable.
Find the first year in which the process becomes stable.
### Input
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains an integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of spiders.
The following line contains $n$ integers $r_1, r_2, \ldots, r_n$ ($1 \leq r_i \leq n, r_i \neq i$) — the recipient of the plushie of each spider.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output an integer on a new line, the first year in which the process becomes stable.
### Example
#### Input #1
```
5
2
2 1
5
2 3 4 5 1
5
2 1 4 2 3
5
4 1 1 5 4
10
4 3 9 1 6 7 9 10 10 3
```
#### Output #1
```
2
2
5
5
5
```
### Note
For the second test case:
- At year $1$, the following array shows the number of plushies each spider has: $[1, 1, 1, 1, 1]$. Then, year $1$'s exchange happens.
- At year $2$, the following array shows the number of plushies each spider has: $[1, 1, 1, 1, 1]$. Since this array is the same as the previous year, this year is stable.
For the third test case:
- At year $1$, the following array shows the number of plushies each spider has: $[1, 1, 1, 1, 1]$. Then, year $1$'s exchange happens.
- At year $2$, the following array shows the number of plushies each spider has: $[1, 2, 1, 1, 0]$. Then, year $2$'s exchange happens.
- At year $3$, the following array shows the number of plushies each spider has: $[1, 3, 0, 1, 0]$. Then, year $3$'s exchange happens.
- At year $4$, the following array shows the number of plushies each spider has: $[1, 4, 0, 0, 0]$. Then, year $4$'s exchange happens.
- At year $5$, the following array shows the number of plushies each spider has: $[1, 4, 0, 0, 0]$. Since this array is the same as the previous year, this year is stable. | codeforces | https://codeforces.com/problemset/problem/2044/G2 |
2044H | H. Hard Demon Problem | medium | Swing is opening a pancake factory! A good pancake factory must be good at flattening things, so Swing is going to test his new equipment on 2D matrices.
Swing is given an $n \times n$ matrix $M$ containing positive integers. He has $q$ queries to ask you.
For each query, he gives you four integers $x_1$, $y_1$, $x_2$, $y_2$ and asks you to flatten the submatrix bounded by $(x_1, y_1)$ and $(x_2, y_2)$ into an array $A$. Formally, $A = [M_{(x1,y1)}, M_{(x1,y1+1)}, \ldots, M_{(x1,y2)}, M_{(x1+1,y1)}, M_{(x1+1,y1+1)}, \ldots, M_{(x2,y2)}]$.
The following image depicts the flattening of a submatrix bounded by the red dotted lines. The orange arrows denote the direction that the elements of the submatrix are appended to the back of $A$, and $A$ is shown at the bottom of the image.

Afterwards, he asks you for the value of $\sum_{i=1}^{|A|} A_i \cdot i$ (sum of $A_i \cdot i$ over all $i$).
### Input
The first line contains an integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases.
The first line of each test contains two integers $n$ and $q$ ($1 \leq n \leq 2000, 1 \leq q \leq 10^6$) — the length of $M$ and the number of queries.
The following $n$ lines contain $n$ integers each, the $i$'th of which contains $M_{(i,1)}, M_{(i,2)}, \ldots, M_{(i,n)}$ ($1 \leq M_{(i, j)} \leq 10^6$).
The following $q$ lines contain four integers $x_1$, $y_1$, $x_2$, and $y_2$ ($1 \leq x_1 \leq x_2 \leq n, 1 \leq y_1 \leq y_2 \leq n$) — the bounds of the query.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$ and the sum of $q$ over all test cases does not exceed $10^6$.
### Output
For each test case, output the results of the $q$ queries on a new line.
### Example
#### Input #1
```
2
4 3
1 5 2 4
4 9 5 3
4 5 2 3
1 5 5 2
1 1 4 4
2 2 3 3
1 2 4 3
3 3
1 2 3
4 5 6
7 8 9
1 1 1 3
1 3 3 3
2 2 2 2
```
#### Output #1
```
500 42 168
14 42 5
```
### Note
In the second query of the first test case, $A = [9, 5, 5, 2]$. Therefore, the sum is $1 \cdot 9 + 2 \cdot 5 + 3 \cdot 5 + 4 \cdot 2 = 42$. | codeforces | https://codeforces.com/problemset/problem/2044/H |
2040E | E. Control of Randomness | medium | You are given a tree with $n$ vertices.
Let's place a robot in some vertex $v \ne 1$, and suppose we initially have $p$ coins. Consider the following process, where in the $i$-th step (starting from $i = 1$):
- If $i$ is odd, the robot moves to an adjacent vertex in the direction of vertex $1$;
- Else, $i$ is even. You can either pay one coin (if there are some left) and then the robot moves to an adjacent vertex in the direction of vertex $1$, or not pay, and then the robot moves to an adjacent vertex chosen uniformly at random.
The process stops as soon as the robot reaches vertex $1$. Let $f(v, p)$ be the minimum possible expected number of steps in the process above if we spend our coins optimally.
Answer $q$ queries, in the $i$-th of which you have to find the value of $f(v_i, p_i)$, modulo$^{\text{∗}}$ $998\,244\,353$.
$^{\text{∗}}$ Formally, let $M = 998\,244\,353$. 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 < M$ and $x \cdot q \equiv p \pmod{M}$.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $q$ ($2 \le n \le 2 \cdot 10^3$; $1 \le q \le 2 \cdot 10^3$) — the number of vertices in the tree and the number of queries.
The next $n - 1$ lines contain the edges of the tree, one edge per line. The $i$-th line contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$; $u_i \neq v_i$), denoting the edge between the nodes $u_i$ and $v_i$.
The next $q$ lines contain two integers $v_i$ and $p_i$ ($2 \le v_i \le n$; $0 \le p_i \le n$).
It's guaranteed that the given edges form a tree.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 3$.
It is guaranteed that the sum of $q$ over all test cases does not exceed $2 \cdot 10 ^ 3$.
### Output
For each test case, print $q$ integers: the values of $f(v_i, p_i)$ modulo $998\,244\,353$.
Formally, let $M = 998\,244\,353$. 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 < M$ and $x \cdot q \equiv p \pmod{M}$.
### Example
#### Input #1
```
2
4 4
1 2
2 3
2 4
2 0
3 0
4 0
3 1
12 10
1 2
2 3
2 4
1 5
5 6
6 7
6 8
6 9
8 10
10 11
10 12
6 0
9 0
10 0
11 0
3 1
7 1
10 1
12 1
12 2
11 12
```
#### Output #1
```
1
6
6
2
4
9
8
15
2
3
6
9
5
5
```
### Note
The tree in the first test case:

In the first query, the expected value is equal to $1$, since the robot starts moving from vertex $2$ to vertex $1$ in the first step and the process stops.
Let's calculate the expected value in the second query ($x$ is the number of steps):
- $P(x < 2) = 0$, the distance to vertex $1$ is $2$ and the robot cannot reach it in fewer steps.
- $P(x = 2) = \frac{1}{3}$, since there is only one sequence of steps leading to $x = 2$. This is $3 \rightarrow_{1} 2 \rightarrow_{0.33} 1$ with probability $1 \cdot \frac{1}{3}$.
- $P(x \bmod 2 = 1) = 0$, since the robot can reach vertex $1$ by only taking an even number of steps.
- $P(x = 4) = \frac{2}{9}$: possible paths $3 \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.33} 1$.
- $P(x = 6) = \frac{4}{27}$: possible paths $3 \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.33} 1$.
- $P(x = i \cdot 2) = \frac{2^{i - 1}}{3^i}$ in the general case.
As a result, $f(v, p) = \sum\limits_{i=1}^{\infty}{i \cdot 2 \cdot \frac{2^{i - 1}}{3^i}} = 6$.
The tree in the second test case:
 | codeforces | https://codeforces.com/problemset/problem/2040/E |
2040F | F. Number of Cubes | medium | Consider a rectangular parallelepiped with sides $a$, $b$, and $c$, that consists of unit cubes of $k$ different colors. We can apply cyclic shifts to the parallelepiped in any of the three directions any number of times$^{\text{∗}}$.
There are $d_i$ cubes of the $i$-th color ($1 \le i \le k$). How many different parallelepipeds (with the given sides) can be formed from these cubes, no two of which can be made equal by some combination of cyclic shifts?
$^{\text{∗}}$On the image:
- Top left shows the top view of the original parallelepiped. Lower layers will shift in the same way as the top layer.
- Top right shows the top view of a parallelepiped shifted to the right by $1$.
- Bottom left shows the top view of a parallelepiped shifted down by $2$.
- Bottom right shows the top view of a parallelepiped shifted to the right by $1$ and down by $2$.

### 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 four integers: $a$, $b$, $c$, and $k$ ($1 \le a, b, c \le 3 \cdot 10^6$; $a \cdot b \cdot c \le 3 \cdot 10^6$; $1 \le k \le 10^6$) — three sides of the parallelepiped and the number of colors of unit cubes.
The second line of each test case contains $k$ integers $d_1, d_2, \ldots, d_k$ ($1 \le d_1 \le d_2 \le \ldots \le d_k \le 3 \cdot 10^6$) — the elements of the array $d$: the number of cubes of a given color.
It is guaranteed that in each test case the sum of the elements of the array $d$ is equal to $a \cdot b \cdot c$.
It is guaranteed that the sum of $k$ over all test cases does not exceed $10 ^ 6$.
### Output
For each test case, print one integer — the number of different parallelepipeds modulo $998\,244\,353$.
### Example
#### Input #1
```
6
1 1 1 1
1
6 1 1 3
1 2 3
12 1 1 3
2 4 6
3 3 1 2
3 6
2 3 3 2
6 12
72 60 96 4
17280 86400 120960 190080
```
#### Output #1
```
1
10
1160
12
1044
231490207
```
### Note
In the first test case, there is only one parallelepiped, which consists of one unit cube.
Possible parallelepipeds in the second test case | codeforces | https://codeforces.com/problemset/problem/2040/F |
2040D | D. Non Prime Tree | medium | You are given a tree with $n$ vertices.
You need to construct an array $a_1, a_2, \ldots, a_n$ of length $n$, consisting of unique integers from $1$ to $2 \cdot n$, and such that for each edge $u_i \leftrightarrow v_i$ of the tree, the value $|a_{u_i} - a_{v_i}|$ is not a prime number.
Find any array that satisfies these conditions, or report that there is no such array.
### 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 a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of vertices in the tree.
The next $n - 1$ lines contain the edges of the tree, one edge per line. The $i$-th line contains two integers $u_i$ and $v_i$ ($1 \le u_i, v_i \le n$; $u_i \neq v_i$), denoting the edge between the nodes $u_i$ and $v_i$.
It's guaranteed that the given edges form a tree.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10 ^ 5$.
### Output
For each test case, if an array that satisfies the conditions exists, print its elements $a_1, a_2, \ldots, a_n$. Otherwise, print $-1$.
### Example
#### Input #1
```
2
5
1 2
2 3
2 4
3 5
7
1 2
1 3
2 4
3 5
3 6
3 7
```
#### Output #1
```
2 10 1 6 5
8 7 12 1 4 6 3
```
### Note
The possible answers are shown below. Instead of the vertex numbers, the corresponding elements of the array $a$ are written in them.
The image of the tree in the first test caseThe image of the tree in the second test case | codeforces | https://codeforces.com/problemset/problem/2040/D |
2050G | G. Tree Destruction | medium | Given a tree$^{\text{∗}}$ with $n$ vertices. You can choose two vertices $a$ and $b$ once and remove all vertices on the path from $a$ to $b$, including the vertices themselves. If you choose $a=b$, only one vertex will be removed.
Your task is to find the maximum number of connected components$^{\text{†}}$ that can be formed after removing the path from the tree.
$^{\text{∗}}$A tree is a connected graph without cycles.
$^{\text{†}}$A connected component is a set of vertices such that there is a path along the edges from any vertex to any other vertex in the set (and it is not possible to reach vertices not belonging to this set)
### Input
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the size of the tree.
The next $n-1$ lines contain two integers $u$ and $v$ ($1 \le u, v \le n$, $u \ne v$) — the vertices connected by an edge. It is guaranteed that the edges form a tree.
It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output one integer — the maximum number of connected components that can be achieved using the described operation.
### Example
#### Input #1
```
6
2
1 2
5
1 2
2 3
3 4
3 5
4
1 2
2 3
3 4
5
2 1
3 1
4 1
5 4
6
2 1
3 1
4 1
5 3
6 3
6
2 1
3 2
4 2
5 3
6 4
```
#### Output #1
```
1
3
2
3
4
3
``` | codeforces | https://codeforces.com/problemset/problem/2050/G |
2046E1 | E1. Cheops and a Contest (Easy Version) | medium | This is the easy version of the problem. The difference between the versions is that in this version, $m$ equals $2$. You can hack only if you solved all versions of this problem.
There is a problem-solving competition in Ancient Egypt with $n$ participants, numbered from $1$ to $n$. Each participant comes from a certain city; the cities are numbered from $1$ to $m$. There is at least one participant from each city.
The $i$-th participant has strength $a_i$, specialization $s_i$, and wisdom $b_i$, so that $b_i \ge a_i$. Each problem in the competition will have a difficulty $d$ and a unique topic $t$. The $i$-th participant will solve the problem if
- $a_i \ge d$, i.e., their strength is not less than the problem's difficulty, or
- $s_i = t$, and $b_i \ge d$, i.e., their specialization matches the problem's topic, and their wisdom is not less than the problem's difficulty.
Cheops wants to choose the problems in such a way that each participant from city $i$ will solve strictly more problems than each participant from city $j$, for all $i < j$.
Please find a set of at most $5n$ problems, where the topics of all problems are distinct, so that Cheops' will is satisfied, or state that it is impossible.
### 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$, $m$ ($2 \mathbf{=} m \le n \le 3 \cdot {10}^5$) — the number of participants and the number of cities.
The following $n$ lines describe the participants. The $i$-th line contains three integers —$a_i$, $b_i$, $s_i$ ($0 \le a_i, b_i, s_i \le {10}^9$, $a_i \le b_i$) — strength, wisdom, and specialization of the $i$-th participant, respectively.
The next $m$ lines describe the cities. In the $i$-th line, the first number is an integer $k_i$ ($1 \le k_i \le n$) — the number of participants from the $i$-th city. It is followed by $k_i$ integers $q_{i, 1}, q_{i, 2}, \ldots, q_{i, k_i}$ — ($1 \le q_{i, j} \le n$, $1 \le j \le k_i$) — the indices of the participants from this city. It is guaranteed that each participant is mentioned exactly once.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
### Output
For each test case, if there exists a set of problems that satisfies Cheops' conditions, then in the first line output a single integer $p$ ($1 \le p \le 5n$) — the number of problems in your solution.
Then output $p$ lines, each containing two integers $d$ and $t$ ($0 \le d, t \le {10}^9$) — the difficulty and topic of the respective problem. The topics must be distinct.
If there is no set of problems that meets Cheops' wishes, print $-1$ instead.
### Example
#### Input #1
```
2
5 2
5 7 1
6 7 2
3 9 2
5 10 3
4 4 1
2 1 2
3 3 4 5
2 2
1 2 1
1 2 1
1 2
1 1
```
#### Output #1
```
7
6 4
6 5
5 6
5 7
4 8
4 9
7 1
-1
``` | codeforces | https://codeforces.com/problemset/problem/2046/E1 |
2046C | C. Adventurers | medium | Once, four Roman merchants met in a Roman mansion to discuss their trading plans. They faced the following problem: they traded the same type of goods, and if they traded in the same city, they would inevitably incur losses. They decided to divide up the cities between them where they would trade.
The map of Rome can be represented in this problem as a plane with certain points marked — the cities of the Roman Empire.
The merchants decided to choose a certain dividing point $(x_0, y_0)$. Then, in a city with coordinates $(x_i, y_i)$,
- the first merchant sells goods if $x_0 \le x_i$ and $y_0 \le y_i$;
- the second merchant sells goods if $x_0 > x_i$ and $y_0 \le y_i$;
- the third merchant sells goods if $x_0 \le x_i$ and $y_0 > y_i$;
- the fourth merchant sells goods if $x_0 > x_i$ and $y_0 > y_i$.
The merchants want to choose $(x_0, y_0)$ in such a way as to maximize the smallest number of cities that any of them gets (i. e., as fair as possible). Please find such a point for them.
### 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 a single integer $n$ ($4 \le n \le 10^5$) — the number of cities on the map.
Each of the next $n$ lines contains two integers $x_i, y_i$ ($-10^9 \le x_i, y_i \le 10^9$) — the coordinates of the cities.
Note that some points may coincide. This is because some cities may be so close that they cannot be distinguished on the map at the given scale.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
### Output
For each test case, in the first line, print a single integer $k$ ($0 \le k \le \frac{n}{4}$) — the maximum possible number of cities that each merchant can get at a minimum.
In the second line, print two integers $x_0$ and $y_0$ ($|x_0|, |y_0| \le 10^9$) — the coordinates of the dividing point. If there are multiple suitable points, print any of them.
### Example
#### Input #1
```
4
4
1 1
1 2
2 1
2 2
4
0 0
0 0
0 0
0 0
8
1 2
2 1
2 -1
1 -2
-1 -2
-2 -1
-2 1
-1 2
7
1 1
1 2
1 3
1 4
2 1
3 1
4 1
```
#### Output #1
```
1
2 2
0
0 0
2
1 0
0
0 0
``` | codeforces | https://codeforces.com/problemset/problem/2046/C |
2042D | D. Recommendations | medium | Suppose you are working in some audio streaming service. The service has $n$ active users and $10^9$ tracks users can listen to. Users can like tracks and, based on likes, the service should recommend them new tracks.
Tracks are numbered from $1$ to $10^9$. It turned out that tracks the $i$-th user likes form a segment $[l_i, r_i]$.
Let's say that the user $j$ is a predictor for user $i$ ($j \neq i$) if user $j$ likes all tracks the $i$-th user likes (and, possibly, some other tracks too).
Also, let's say that a track is strongly recommended for user $i$ if the track is not liked by the $i$-th user yet, but it is liked by every predictor for the $i$-th user.
Calculate the number of strongly recommended tracks for each user $i$. If a user doesn't have any predictors, then print $0$ for that user.
### Input
The first line contains one integer $t$ ($1 \le t \le 10^4$) — the number of test cases. Next, $t$ cases follow.
The first line of each test case contains one integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of users.
The next $n$ lines contain two integers $l_i$ and $r_i$ per line ($1 \le l_i \le r_i \le 10^9$) — the segment of tracks the $i$-th user likes.
Additional constraint on the input: the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
### Output
For each test case, print $n$ integers, where the $i$-th integer is the number of strongly recommended tracks for the $i$-th user (or $0$, if that user doesn't have any predictors).
### Example
#### Input #1
```
4
3
3 8
2 5
4 5
2
42 42
1 1000000000
3
42 42
1 1000000000
42 42
6
1 10
3 10
3 7
5 7
4 4
1 2
```
#### Output #1
```
0
0
1
999999999
0
0
0
0
0
2
3
2
4
8
```
### Note
In the first test case:
- the first user has no predictors;
- the second user has no predictors;
- the third user has two predictors: users $1$ and $2$; only track $3$ is liked by both of them and not liked by the third user.
In the second test case, the second user is a predictor for the first user. Therefore, all tracks, except $42$, are strongly recommended for the first user.
In the third test case, the first user has two predictors: users $2$ and $3$, but there is no track that is liked by them and not liked by the first user himself. | codeforces | https://codeforces.com/problemset/problem/2042/D |
2042E | E. Vertex Pairs | medium | You are given a tree consisting of $2n$ vertices. Recall that a tree is a connected undirected graph with no cycles. Each vertex has an integer from $1$ to $n$ written on it. Each value from $1$ to $n$ is written on exactly two different vertices. Each vertex also has a cost —vertex $i$ costs $2^i$.
You need to choose a subset of vertices of the tree such that:
- the subset is connected; that is, from each vertex in the subset, you can reach every other vertex in the subset by passing only through the vertices in the subset;
- each value from $1$ to $n$ is written on at least one vertex in the subset.
Among all such subsets, you need to find the one with the smallest total cost of the vertices in it. Note that you are not required to minimize the number of vertices in the subset.
### Input
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$).
The second line contains $2n$ integers $a_1, a_2, \dots, a_{2n}$ ($1 \le a_i \le n$). Each value from $1$ to $n$ appears exactly twice.
Each of the next $2n-1$ lines contains two integers $v$ and $u$ ($1 \le v, u \le 2n$) — the edges of the tree. These edges form a valid tree.
### Output
In the first line, print a single integer $k$ — the number of vertices in the subset.
In the second line, print $k$ distinct integers from $1$ to $2n$ — the indices of the vertices in the chosen subset. The vertices can be printed in an arbitrary order.
### Examples
#### Input #1
```
3
1 1 3 2 3 2
4 2
1 6
6 2
6 3
2 5
```
#### Output #1
```
3
2 4 5
```
#### Input #2
```
3
2 3 1 3 2 1
6 4
2 4
5 2
3 6
3 1
```
#### Output #2
```
4
1 3 4 6
```
#### Input #3
```
6
5 2 3 4 6 4 2 5 6 1 1 3
10 8
2 10
12 7
4 10
5 9
6 2
1 9
3 4
12 6
11 5
4 5
```
#### Output #3
```
6
2 3 4 5 8 10
```
### Note

The images show the answers to the first two examples. The numbers in parentheses are the values written on the vertices.
In the first example, there are valid subsets such as: $[2, 4, 5]$ (with a cost of $2^2 + 2^4 + 2^5 = 52$), $[2, 4, 5, 6]$ (with a cost of $116$), $[1, 6, 3]$ (with a cost of $74$), $[2, 6, 3]$ (with a cost of $76$), and many others.
In the second example, the cost of the subset $[4, 6, 3, 1]$ is $90$. | codeforces | https://codeforces.com/problemset/problem/2042/E |
2042F | F. Two Subarrays | medium | You are given two integer arrays $a$ and $b$, both of size $n$.
Let's define the cost of the subarray $[l, r]$ as $a_l + a_{l + 1} + \cdots + a_{r - 1} + a_r + b_l + b_r$. If $l=r$, then the cost of the subarray is $a_l + 2 \cdot b_l$.
You have to perform queries of three types:
- "$1$ $p$ $x$" — assign $a_{p} := x$;
- "$2$ $p$ $x$" — assign $b_{p} := x$;
- "$3$ $l$ $r$" — find two non-empty non-overlapping subarrays within the segment $[l, r]$ with the maximum total cost and print their total cost.
### Input
The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10^9 \le a_i \le 10^9$).
The third line contains $n$ integers $b_1, b_2, \dots, b_n$ ($-10^9 \le b_i \le 10^9$).
The fourth line contains a single integer $q$ ($1 \le q \le 2 \cdot 10^5$).
The next $q$ lines contain the queries: one per line. Each query is of one of three types:
- "$1$ $p$ $x$" ($1 \le p \le n$; $-10^9 \le x \le 10^9$);
- "$2$ $p$ $x$" ($1 \le p \le n$; $-10^9 \le x \le 10^9$);
- "$3$ $l$ $r$" ($1 \le l < r \le n$).
It is guaranteed that there is at least one query of the third type.
### Output
For each query of the third type, print the maximum possible total cost of two non-empty non-overlapping subarrays within the segment $[l, r]$.
### Examples
#### Input #1
```
7
3 -1 4 -3 2 4 0
0 6 1 0 -3 -2 -1
6
3 1 7
1 2 0
3 3 6
2 5 -3
1 3 2
3 1 5
```
#### Output #1
```
18
7
16
```
#### Input #2
```
10
2 -1 -3 -2 0 4 5 6 2 5
2 -4 -5 -1 6 2 5 -6 4 2
10
3 6 7
1 10 -2
3 5 7
3 2 8
2 1 -5
2 7 4
3 1 3
3 3 8
3 2 3
1 4 4
```
#### Output #2
```
23
28
28
-17
27
-22
``` | codeforces | https://codeforces.com/problemset/problem/2042/F |
2034F1 | F1. Khayyam's Royal Decree (Easy Version) | medium | This is the easy version of the problem. The only differences between the two versions are the constraints on $k$ and the sum of $k$.
In ancient Persia, [Khayyam](https://en.wikipedia.org/wiki/Omar_Khayyam), a clever merchant and mathematician, is playing a game with his prized treasure chest containing $n$ red rubies worth $2$ dinars each and $m$ blue sapphires worth $1$ dinar each. He also has a satchel, which starts empty, and $k$ scrolls with pairs $(r_1, b_1), (r_2, b_2), \ldots, (r_k, b_k)$ that describe special conditions.

The game proceeds for $n + m$ turns as follows:
1. Khayyam draws a gem uniformly at random from the chest.
2. He removes the gem from the chest and places it in his satchel.
3. If there exists a scroll $i$ ($1 \leq i \leq k$) such that the chest contains exactly $r_i$ red rubies and $b_i$ blue sapphires, Khayyam receives a royal decree that doubles the value of all the gems in his satchel as a reward for achieving a special configuration.
Note that the value of some gems might be affected by multiple decrees, and in that case the gems' value is doubled multiple times.
Determine the expected value of Khayyam's satchel at the end of the game, modulo $998,244,353$.
Formally, let $M = 998,244,353$. It can be shown that the exact 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 < M$ and $x \cdot q \equiv p \pmod{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 three integers $n$, $m$, and $k$ ($1 \leq n, m \leq 2 \cdot 10^5$, $0 \leq k \leq 500$) — the number of red rubies, the number of blue sapphires, and the number of scrolls describing special conditions, respectively.
Each of the next $k$ lines contains two integers $r_i$, $b_i$ ($0 \leq r_i \leq n$, $0 \leq b_i \leq m$, $1 \leq r_i + b_i \leq n+m-1$). It is guaranteed that the pairs $(r_i, b_i)$ are distinct.
It is guaranteed that the sum of $n$ and the sum of $m$ over all test cases do not exceed $2 \cdot 10^5$, and the sum of $k$ over all test cases does not exceed $500$.
### Output
For each test case, print a single integer: the expected value of Khayyam's satchel at the end of the process, modulo $998,244,353$.
### Example
#### Input #1
```
5
3 4 0
1 1 1
1 0
3 3 2
1 1
2 2
3 3 2
2 1
1 2
10 4 5
1 0
8 0
6 4
0 2
7 4
```
#### Output #1
```
10
499122180
798595498
149736666
414854846
```
### Note
In the first test case, at the end of the process, there will always be $3$ red rubies and $4$ blue sapphires. None of the special conditions described in the scrolls are met, so the value of Khayyam's satchel remains unchanged. The total value of the satchel at the end is always $2 \cdot 3 + 1 \cdot 4 = 10$.
In the second test case, consider the following two cases:
- With probability $1/2$, Khayyam draws a red ruby, and the value of his satchel becomes $2$. Then with probability $1$, he draws a blue sapphire, and the value of his satchel becomes $3$.
- With probability $1/2$, Khayyam draws a blue sapphire, and the value of his satchel becomes $1$. At this point, the chest contains $r_1 = 1$ red rubies and $b_1 = 0$ blue sapphires, which match the special condition described in a scroll. As a result, the value of the satchel is doubled to $2 \cdot 1 = 2$. Then with probability $1$, he draws a red ruby, and the value of his satchel becomes $4$.
Thus, the expected value at the end is $\frac{1}{2} \cdot 3 + \frac{1}{2} \cdot 4 = \frac{7}{2}$, which is $499,122,180$ modulo $998,244,353$. | codeforces | https://codeforces.com/problemset/problem/2034/F1 |
2034F2 | F2. Khayyam's Royal Decree (Hard Version) | medium | This is the hard version of the problem. The only differences between the two versions are the constraints on $k$ and the sum of $k$.
In ancient Persia, [Khayyam](https://en.wikipedia.org/wiki/Omar_Khayyam), a clever merchant and mathematician, is playing a game with his prized treasure chest containing $n$ red rubies worth $2$ dinars each and $m$ blue sapphires worth $1$ dinar each. He also has a satchel, which starts empty, and $k$ scrolls with pairs $(r_1, b_1), (r_2, b_2), \ldots, (r_k, b_k)$ that describe special conditions.

The game proceeds for $n + m$ turns as follows:
1. Khayyam draws a gem uniformly at random from the chest.
2. He removes the gem from the chest and places it in his satchel.
3. If there exists a scroll $i$ ($1 \leq i \leq k$) such that the chest contains exactly $r_i$ red rubies and $b_i$ blue sapphires, Khayyam receives a royal decree that doubles the value of all the gems in his satchel as a reward for achieving a special configuration.
Note that the value of some gems might be affected by multiple decrees, and in that case the gems' value is doubled multiple times.
Determine the expected value of Khayyam's satchel at the end of the game, modulo $998,244,353$.
Formally, let $M = 998,244,353$. It can be shown that the exact 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 < M$ and $x \cdot q \equiv p \pmod{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 three integers $n$, $m$, and $k$ ($1 \leq n, m \leq 2 \cdot 10^5$, $0 \leq k \leq 5000$) — the number of red rubies, the number of blue sapphires, and the number of scrolls describing special conditions, respectively.
Each of the next $k$ lines contains two integers $r_i$, $b_i$ ($0 \leq r_i \leq n$, $0 \leq b_i \leq m$, $1 \leq r_i + b_i \leq n+m-1$). It is guaranteed that the pairs $(r_i, b_i)$ are distinct.
It is guaranteed that the sum of $n$ and the sum of $m$ over all test cases do not exceed $2 \cdot 10^5$, and the sum of $k$ over all test cases does not exceed $5000$.
### Output
For each test case, print a single integer: the expected value of Khayyam's satchel at the end of the process, modulo $998,244,353$.
### Example
#### Input #1
```
5
3 4 0
1 1 1
1 0
3 3 2
1 1
2 2
3 3 2
2 1
1 2
10 4 5
1 0
8 0
6 4
0 2
7 4
```
#### Output #1
```
10
499122180
798595498
149736666
414854846
```
### Note
In the first test case, at the end of the process, there will always be $3$ red rubies and $4$ blue sapphires. None of the special conditions described in the scrolls are met, so the value of Khayyam's satchel remains unchanged. The total value of the satchel at the end is always $2 \cdot 3 + 1 \cdot 4 = 10$.
In the second test case, consider the following two cases:
- With probability $1/2$, Khayyam draws a red ruby, and the value of his satchel becomes $2$. Then with probability $1$, he draws a blue sapphire, and the value of his satchel becomes $3$.
- With probability $1/2$, Khayyam draws a blue sapphire, and the value of his satchel becomes $1$. At this point, the chest contains $r_1 = 1$ red rubies and $b_1 = 0$ blue sapphires, which match the special condition described in a scroll. As a result, the value of the satchel is doubled to $2 \cdot 1 = 2$. Then with probability $1$, he draws a red ruby, and the value of his satchel becomes $4$.
Thus, the expected value at the end is $\frac{1}{2} \cdot 3 + \frac{1}{2} \cdot 4 = \frac{7}{2}$, which is $499,122,180$ modulo $998,244,353$. | codeforces | https://codeforces.com/problemset/problem/2034/F2 |
2034E | E. Permutations Harmony | medium | Rayan wants to present a gift to Reyhaneh to win her heart. However, Reyhaneh is particular and will only accept a k-harmonic set of permutations.
We define a k-harmonic set of permutations as a set of $k$ pairwise distinct permutations $p_1, p_2, \ldots, p_k$ of size $n$ such that for every pair of indices $i$ and $j$ (where $1 \leq i, j \leq n$), the following condition holds:
$$ p_1[i] + p_2[i] + \ldots + p_k[i] = p_1[j] + p_2[j] + \ldots + p_k[j] $$
Your task is to help Rayan by either providing a valid k-harmonic set of permutations for given values of $n$ and $k$ or by determining that such a set does not exist.
We call a sequence of length $n$ a permutation if it contains every integer from $1$ to $n$ exactly once.
### Input
The first line contains a single integer $t$ ($1 \leq t \leq 1000$), the number of test cases.
Each test case consists of two integers $n$ and $k$ ($1 \leq n, k \leq 10^5$). The sum of $n \cdot k$ over all test cases does not exceed $5 \cdot 10^5$.
### Output
For each test case, if a k-harmonic set of permutations exists, print YES on the first line. Then, print $k$ lines, each containing a distinct permutation of the integers from $1$ to $n$.
If no such set exists, print NO on the first line.
You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response).
If multiple answers are possible, you can output any of them.
### Example
#### Input #1
```
4
3 3
4 2
5 1
3 2
```
#### Output #1
```
YES
1 2 3
2 3 1
3 1 2
YES
1 2 3 4
4 3 2 1
NO
YES
1 2 3
3 2 1
```
### Note
In example 1, we have $p_1 = [1, 2, 3]$, $p_2 = [2, 3, 1]$, and $p_3 = [3, 1, 2]$. It's easy to see that $p_1[1] + p_2[1] + p_3[1] = p_1[2] + p_2[2] + p_3[2] = p_1[3] + p_2[3] + p_3[3] = 6$.
In example 2, we have $p_1 = [1, 2, 3, 4]$ and $p_2 = [4, 3, 2, 1]$. It's easy to see that $p_1[1] + p_2[1] = p_1[2] + p_2[2] = p_1[3] + p_2[3] = p_1[4] + p_2[4] = 5$.
In example 3, as there are five distinct elements in $p_1$, it's obvious that the answer is "No". | codeforces | https://codeforces.com/problemset/problem/2034/E |
2039E | E. Shohag Loves Inversions | medium | Shohag has an array $a$ of integers. Initially $a = [0, 1]$. He can repeatedly perform the following operation any number of times:
- Let $k$ be the number of inversions$^{\text{∗}}$ in the current array $a$.
- Insert $k$ at any position in $a$, including the beginning or the end.
For example, if $a = [4, 6, 2, 4]$, then the number of inversions is $k = 3$. So Shohag can obtain the following arrays after the operation: $[\textbf{3}, 4, 6, 2, 4]$, $[4, \textbf{3}, 6, 2, 4]$, $[4, 6, \textbf{3}, 2, 4]$, $[4, 6, 2, \textbf{3}, 4]$, and $[4, 6, 2, 4, \textbf{3}]$.
Given an integer $n$, help Shohag count, modulo $998\,244\,353$, the number of distinct arrays of length $n$ that can be obtained after performing the operations.
$^{\text{∗}}$The number of inversions in an array $a$ is the number of pairs of indices ($i$, $j$) such that $i < j$ and $a_i > a_j$.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first and only line of each test case contains an integer $n$ ($2 \le n \le 10^6$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
### Output
For each test case, output an integer — the number of possible arrays modulo $998\,244\,353$.
### Example
#### Input #1
```
4
4
2
7
69
```
#### Output #1
```
5
1
682
325188814
```
### Note
In the first test case, the following $5$ arrays can be obtained (the inserted inversion count is shown in bold):
- $[0, 1] \rightarrow [0, \textbf{0}, 1] \rightarrow [0, 0, 1, \textbf{0}]$,
- $[0, 1] \rightarrow [0, \textbf{0}, 1] \rightarrow [0, 0, \textbf{0}, 1]$,
- $[0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [0, 1, 0, \textbf{1}]$,
- $[0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [0, 1, \textbf{1}, 0]$,
- $[0, 1] \rightarrow [0, 1, \textbf{0}] \rightarrow [\textbf{1}, 0, 1, 0]$. | codeforces | https://codeforces.com/problemset/problem/2039/E |
2039F1 | F1. Shohag Loves Counting (Easy Version) | medium | This is the easy version of the problem. The only differences between the two versions of this problem are the constraints on $t$, $m$, and the sum of $m$. You can only make hacks if both versions of the problem are solved.
For an integer array $a$ of length $n$, define $f(k)$ as the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of the maximum values of all subarrays$^{\text{∗}}$ of length $k$. For example, if the array is $[2, 1, 4, 6, 2]$, then $f(3) = \operatorname{gcd}(\operatorname{max}([2, 1, 4]), \operatorname{max}([1, 4, 6]), \operatorname{max}([4, 6, 2])) = \operatorname{gcd}(4, 6, 6) = 2$.
An array is good if $f(i) \neq f(j)$ is satisfied over all pairs $1 \le i \lt j \le n$.
Shohag has an integer $m$. Help him count the number, modulo $998\,244\,353$, of non-empty good arrays of arbitrary length such that each element of the array is an integer from $1$ to $m$.
$^{\text{∗}}$An array $d$ is a subarray of an array $c$ if $d$ can be obtained from $c$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
### Input
The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first and only line of each test case contains an integer $m$ ($1 \le m \le 10^5$).
It is guaranteed that the sum of $m$ over all test cases does not exceed $10^5$.
### Output
For each test case, output an integer — the number of valid arrays modulo $998\,244\,353$.
### Example
#### Input #1
```
3
2
5
9
```
#### Output #1
```
4
29
165
```
### Note
In the first test case, the valid arrays are $[1]$, $[1, 2]$, $[2]$, and $[2, 1]$.
In the second test case, there are a total of $29$ valid arrays. In particular, the array $[2, 1, 4]$ with length $n = 3$ is valid because all elements are from $1$ to $m = 5$ and $f(1)$, $f(2)$ and $f(n = 3)$ all are distinct:
- $f(1) = \operatorname{gcd}(\operatorname{max}([2]), \operatorname{max}([1]), \operatorname{max}([4])) = \operatorname{gcd}(2, 1, 4) = 1.$
- $f(2) = \operatorname{gcd}(\operatorname{max}([2, 1]), \operatorname{max}([1, 4])) = \operatorname{gcd}(2, 4) = 2.$
- $f(3) = \operatorname{gcd}(\operatorname{max}([2, 1, 4])) = \operatorname{gcd}(4) = 4.$ | codeforces | https://codeforces.com/problemset/problem/2039/F1 |
2037G | G. Natlan Exploring | medium | You are exploring the stunning region of Natlan! This region consists of $n$ cities, and each city is rated with an attractiveness $a_i$. A directed edge exists from City $i$ to City $j$ if and only if $i < j$ and $\gcd(a_i,a_j)\neq 1$, where $\gcd(x, y)$ denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$.
Starting from City $1$, your task is to determine the total number of distinct paths you can take to reach City $n$, modulo $998\,244\,353$. Two paths are different if and only if the set of cities visited is different.
### Input
The first line contains an integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of cities.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($2 \leq a_i \leq 10^6$) — the attractiveness of each city.
### Output
Output the total number of distinct paths you can take to reach City $n$, modulo $998\,244\,353$.
### Examples
#### Input #1
```
5
2 6 3 4 6
```
#### Output #1
```
5
```
#### Input #2
```
5
4 196 2662 2197 121
```
#### Output #2
```
2
```
#### Input #3
```
7
3 6 8 9 11 12 20
```
#### Output #3
```
7
```
#### Input #4
```
2
2 3
```
#### Output #4
```
0
```
### Note
In the first example, the five paths are the following:
- City $1\rightarrow$ City $5$
- City $1\rightarrow$ City $2\rightarrow$ City $5$
- City $1\rightarrow$ City $2\rightarrow$ City $3\rightarrow$ City $5$
- City $1\rightarrow$ City $2\rightarrow$ City $4\rightarrow$ City $5$
- City $1\rightarrow$ City $4\rightarrow$ City $5$
In the second example, the two paths are the following:
- City $1\rightarrow$ City $3\rightarrow$ City $5$
- City $1\rightarrow$ City $2\rightarrow$ City $3\rightarrow$ City $5$ | codeforces | https://codeforces.com/problemset/problem/2037/G |
2037F | F. Ardent Flames | medium | You have obtained the new limited event character Xilonen. You decide to use her in combat.
There are $n$ enemies in a line. The $i$'th enemy from the left has health $h_i$ and is currently at position $x_i$. Xilonen has an attack damage of $m$, and you are ready to defeat the enemies with her.
Xilonen has a powerful "ground stomp" attack. Before you perform any attacks, you select an integer $p$ and position Xilonen there ($p$ can be any integer position, including a position with an enemy currently). Afterwards, for each attack, she deals $m$ damage to an enemy at position $p$ (if there are any), $m-1$ damage to enemies at positions $p-1$ and $p+1$, $m-2$ damage to enemies at positions $p-2$ and $p+2$, and so on. Enemies that are at least a distance of $m$ away from Xilonen take no damage from attacks.
Formally, if there is an enemy at position $x$, she will deal $\max(0,m - |p - x|)$ damage to that enemy each hit. Note that you may not choose a different $p$ for different attacks.
Over all possible $p$, output the minimum number of attacks Xilonen must perform to defeat at least $k$ enemies. If it is impossible to find a $p$ such that eventually at least $k$ enemies will be defeated, output $-1$ instead. Note that an enemy is considered to be defeated if its health reaches $0$ or below.
### Input
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) – the number of test cases.
The first line of each test case contains three integers $n$, $m$, and $k$ ($1 \leq k \leq n \leq 10^5$, $1 \leq m \leq 10^9$).
The following line contains $n$ integers $h_1, h_2, ..., h_n$ ($1 \leq h_i \leq 10^9$).
The last line of each testcase contains $n$ integers $x_1, x_2, ..., x_n$ ($1\leq x_i \leq 10^9$, $x_i < x_{i+1}$ for all $1 \leq i < n$)
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
### Output
For each test case, output an integer on a new line, the minimum number of attacks that must be performed to defeat at least $k$ enemies. If it is impossible to find a $p$ such that eventually at least $k$ enemies will be defeated, output $-1$ instead.
### Example
#### Input #1
```
6
5 5 3
7 7 7 7 7
1 2 3 4 5
9 5 9
2 4 6 8 10 8 6 4 2
1 2 3 4 5 6 7 8 9
2 10 2
1 1
1 20
2 10 1
69696969 420420420
1 20
2 10 2
10 15
1 19
2 2 2
1000000000 1
1 3
```
#### Output #1
```
2
2
-1
6969697
15
1000000000
```
### Note
In the first testcase, it is optimal to select $p=2$. Each attack, the first enemy takes $5-|2-1|=4$ damage, the second enemy takes $5$ damage, the third enemy takes $4$ damage, the fourth enemy takes $3$ damage, and the fifth enemy takes $2$ damage. After $2$ attacks, the first three enemies will be defeated. It can be shown that it is impossible to defeat $3$ enemies in less than $2$ attacks, no matter which $p$ is selected.
In the second testcase, we must kill all $9$ enemies. By selecting $p=5$, all nine enemies will be defeated in $2$ attacks.
In the third testcase, we must kill both enemies. However, it can be shown that no $p$ selected will damage both enemies at the same time, so the answer is $-1$.
In the fourth testcase, selecting $p=1$ will enable us to defeat the first enemy in $6969697$ attacks.
In the fifth testcase, selecting $p=10$ will make each enemy take $1$ damage per attack. Both enemies will be defeated in $15$ attacks. | codeforces | https://codeforces.com/problemset/problem/2037/F |
2031F | F. Penchick and Even Medians | medium | This is an interactive problem.
Returning from a restful vacation on Australia's Gold Coast, Penchick forgot to bring home gifts for his pet duck Duong Canh! But perhaps a beautiful problem crafted through deep thought on the scenic beaches could be the perfect souvenir.
There is a hidden permutation$^{\text{∗}}$ $p$ of length $n$, where $n$ is even. You are allowed to make the following query:
- Choose a subsequence$^{\text{†}}$ of the permutation $p$ with even length $4\le k\le n$. The interactor will return the value of the two medians$^{\text{‡}}$ in the chosen subsequence.
Find the index of the two medians in permutation $p$ using at most $80$ queries.
Note that the interactor is non-adaptive. This means that the permutation $p$ is fixed at the beginning and will not change based on your queries.
$^{\text{∗}}$A permutation of length $n$ 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).
$^{\text{†}}$A sequence $a$ is a subsequence of a sequence $b$ if $a$ can be obtained from $b$ by the deletion of several (possibly, zero or all) element from arbitrary positions.
$^{\text{‡}}$The two medians of an array $a$ with even length $k$ are defined as the $\frac{k}{2}$-th and $\left(\frac{k}{2} + 1\right)$-th smallest element in the array ($1$-indexed).
### Input
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 only line of each test case contains a single integer $n$ ($6 \le n \le 100$, $n$ is even) — the length of the hidden permutation $p$.
For each test case, after reading the integer $n$, you should begin the interaction and find the answer before reading $n$ for the next test case.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$.
### Interaction
To make a query, print a single line in the following format:
- $\mathtt{?}\;k\;x_1\;x_2 \ldots x_{k-1}\;x_k$ ($4\le k\le n$, $k$ is even, $1 \le x_i \le n$, $x_i$ is pairwise distinct) — the length of the chosen subsequence followed by the indices of the chosen subsequence.
After each query, you should read a line containing two integers $m_1$ and $m_2$ ($1 \le m_1 < m_2 \le n$) — the value of the two medians in array $[p_{x_1}, p_{x_2}, \ldots, p_{x_{k-1}}, p_{x_k}]$.
You can make at most $80$ such queries in each test case.
To give the final answer, print a single line in the following format:
- $\mathtt{!}\;i_1\;i_2$ ($1\le i_1, i_2 \le n$) — the index of the two medians.
Note that the order in which $i_1$ and $i_2$ is printed does not matter. In other words, your solution is valid as long as $p_{i_1} = \frac{n}{2}$ and $p_{i_2} = \frac{n}{2} + 1$, or $p_{i_1} = \frac{n}{2} + 1$ and $p_{i_2} = \frac{n}{2}$.
After printing each query do not forget to output the end of line and flush$^{\text{∗}}$ the output. Otherwise, you will get Idleness limit exceeded verdict.
If, at any interaction step, you read $-1$ instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.
Hack format
For hacks, use the following format.
The first line should contain $t$ — the number of test cases.
The first line of each test case should contain a single even integer $n$.
The second line of each test case should contain a permutation $p_1, p_2, \ldots, p_n$ of length $n$.
As an example, the hack format for the example input is:
```
2
6
6 2 3 5 1 4
10
10 9 8 7 6 5 4 3 2 1
```
$^{\text{∗}}$To flush, use:
- fflush(stdout) or cout.flush() in C++;
- sys.stdout.flush() in Python;
- see the documentation for other languages.
### Example
#### Input #1
```
2
6
3 4
3 4
2 3
10
3 4
6 7
```
#### Output #1
```
? 6 1 2 3 4 5 6
? 4 3 6 1 5
? 4 3 6 2 5
! 3 6
? 6 1 3 7 8 9 10
? 8 1 2 3 4 5 6 7 8
! 6 5```
### Note
In the first test case, the hidden permutation is $p = [6, 2, 3, 5, 1, 4]$.
1. The entire permutation was chosen for the first query. The two medians of the entire permutation $p$ are $3$ and $4$.
2. The indices of the chosen subsequence in the second query are $3$, $6$, $1$, and $5$. The interactor returns the two medians of the subsequence $[p_3, p_6, p_1, p_5] = [3, 4, 6, 1]$, which are $3$ and $4$.
3. The indices of the chosen subsequence in the second query are $3$, $6$, $2$, and $5$. The interactor returns the two medians of the subsequence $[p_3, p_6, p_2, p_5] = [3, 4, 2, 1]$, which are $2$ and $3$.
The answer "! 3 6" is valid as $p_3 = 3$ and $p_6 = 4$.
In the second test case, the hidden permutation is $p = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]$.
1. The indices of the chosen subsequence in the second query are $1$, $3$, $7$, $8$, $9$, and $10$. The interactor returns the two medians of the subsequence $[p_1, p_3, p_7, p_8, p_9, p_{10}] = [10, 8, 4, 3, 2, 1]$, which are $3$ and $4$.
2. The indices of the chosen subsequence in the second query are $1$, $2$, $3$, $4$, $5$, $6$, $7$, and $8$. The interactor returns the two medians of the subsequence $[p_1, p_2, p_3, p_4, p_5, p_6, p_7, p_8] = [10, 9, 8, 7, 6, 5, 4, 3]$, which are $6$ and $7$.
The answer "! 5 6" is valid as $p_5 = 6$ and $p_6 = 5$. | codeforces | https://codeforces.com/problemset/problem/2031/F |
2031E | E. Penchick and Chloe's Trees | medium | With just a few hours left until Penchick and Chloe leave for Singapore, they could hardly wait to see the towering trees at the Singapore Botanic Gardens! Attempting to contain their excitement, Penchick crafted a rooted tree to keep Chloe and himself busy.
Penchick has a rooted tree$^{\text{∗}}$ consisting of $n$ vertices, numbered from $1$ to $n$, with vertex $1$ as the root, and Chloe can select a non-negative integer $d$ to create a perfect binary tree$^{\text{†}}$ of depth $d$.
Since Penchick and Chloe are good friends, Chloe wants her tree to be isomorphic$^{\text{‡}}$ to Penchick's tree. To meet this condition, Chloe can perform the following operation on her own tree any number of times:
- Select an edge $(u,v)$, where $u$ is the parent of $v$.
- Remove vertex $v$ and all the edges connected to $v$, then connect all of $v$'s previous children directly to $u$.
In particular, doing an operation on an edge $(u, v)$ where $v$ is a leaf will delete vertex $v$ without adding any new edges.
Since constructing a perfect binary tree can be time-consuming, Chloe wants to choose the minimum $d$ such that a perfect binary tree of depth $d$ can be made isomorphic to Penchick's tree using the above operation. Note that she can't change the roots of the trees.
$^{\text{∗}}$A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root. The parent of vertex $v$ is the first vertex on the simple path from $v$ to the root. The root has no parent. A child of vertex $v$ is any vertex $u$ for which $v$ is the parent. A leaf is any vertex without children.
$^{\text{†}}$A full binary tree is rooted tree, in which each node has $0$ or $2$ children. A perfect binary tree is a full binary tree in which every leaf is at the same distance from the root. The depth of such a tree is the distance from the root to a leaf.
$^{\text{‡}}$Two rooted trees, rooted at $r_1$ and $r_2$ respectively, are considered isomorphic if there exists a permutation $p$ of the vertices such that an edge $(u, v)$ exists in the first tree if and only if the edge $(p_u, p_v)$ exists in the second tree, and $p_{r_1} = r_2$.
### Input
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$ ($2 \le n \le 10^6$) — the number of vertices in Penchick's tree.
The second line of each test case contains $n-1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i \leq i-1$) — the parent of vertex $i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
### Output
For each test case, output a single integer on each line: the minimum depth of Chloe's perfect binary tree.
### Example
#### Input #1
```
5
6
1 2 2 1 1
15
1 1 2 2 3 3 4 4 5 5 6 6 7 7
5
1 2 2 2
7
1 1 2 1 1 2
10
1 1 1 2 2 2 4 3 3
```
#### Output #1
```
2
3
3
3
3
```
### Note
For the first test case, create a perfect binary tree with depth $2$.

Consider carrying out the operation on edge $AC$. Then the edges $AC$, $CF$, and $CG$ are removed, and edges $AF$ and $AG$ are added.

The resulting tree is isomorphic to the tree given in the input. It can be proven that no sequence of operations carried out on a binary tree of depth less than $2$ can lead to a tree isomorphic to the tree given in the input.
In the second test case, the tree is already isomorphic to a perfect binary tree of depth $3$. | codeforces | https://codeforces.com/problemset/problem/2031/E |
2028F | F. Alice's Adventures in Addition | medium | Note that the memory limit is unusual.
The Cheshire Cat has a riddle for Alice: given $n$ integers $a\_1, a\_2, \\ldots, a\_n$ and a target $m$, is there a way to insert $+$ and $\\times$ into the circles of the expression
$$a_1 \circ a_2 \circ \cdots \circ a_n = m$$
to make it true? We follow the usual order of operations: $\times$ is done before $+$.
Although Alice is excellent at chess, she is not good at math. Please help her so she can find a way out of Wonderland!
### Input
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n, m$ ($1\le n\le 2\cdot 10^5$; $1\le m\le 10^4$) — the number of integers and the target, respectively.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0\le a_i\le 10^4$) — the elements of the array $a$.
The sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
### Output
For each test case, output "YES" without quotes if it is possible to get the target by inserting $+$ or $\times$ and "NO" otherwise.
You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
### Example
#### Input #1
```
6
5 4
2 1 1 1 2
5 5
2 1 1 1 2
5 6
2 1 1 1 2
5 7
2 1 1 1 2
5 8
2 1 1 1 2
5 6
2 0 2 2 3
```
#### Output #1
```
YES
YES
YES
YES
NO
YES
```
### Note
Possible solutions for the first four test cases are shown below.
$$\begin{align*} 2 \times 1 + 1 \times 1 \times 2 &= 4 \\ 2 \times 1 + 1 + 1 \times 2 &= 5 \\ 2 \times 1 + 1 + 1 + 2 &= 6 \\ 2 + 1 + 1 + 1 + 2 &= 7 \\ \end{align*}$$
It is impossible to get a result of $8$ in the fifth test case. | codeforces | https://codeforces.com/problemset/problem/2028/F |
2028D | D. Alice's Adventures in Cards | medium | Alice is playing cards with the Queen of Hearts, King of Hearts, and Jack of Hearts. There are $n$ different types of cards in their card game. Alice currently has a card of type $1$ and needs a card of type $n$ to escape Wonderland. The other players have one of each kind of card.
In this card game, Alice can trade cards with the three other players. Each player has different preferences for the $n$ types of cards, which can be described by permutations$^{\text{∗}}$ $q$, $k$, and $j$ for the Queen, King, and Jack, respectively.
A player values card $a$ more than card $b$ if for their permutation $p$, $p_a > p_b$. Then, this player is willing to trade card $b$ to Alice in exchange for card $a$. Alice's preferences are straightforward: she values card $a$ more than card $b$ if $a > b$, and she will also only trade according to these preferences.
Determine if Alice can trade up from card $1$ to card $n$ subject to these preferences, and if it is possible, give a possible set of trades to do it.
$^{\text{∗}}$A permutation of length $n$ 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).
### 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$ ($2\le n\le 2\cdot 10^5$) — the number of card types.
The next three lines contain the preferences of the Queen, King, and Jack respectively. Each of these lines contains $n$ integers $p_1, p_2, \ldots, p_n$ ($1\le p_i\le n$) — a permutation corresponding to the player's preferences.
The sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
### Output
For each test case, on the first line output a single string "YES" or "NO" (without the quotes) denoting whether Alice can trade up to card $n$.
If the first line was "YES", then on the next line output $k$ — the number of trades Alice will make. On the next $k$ lines output space separated a character $c\in \{\texttt{q}, \texttt{k}, \texttt{j}\}$ and integer $x$, denoting that Alice trades with player $c$ to get card $x$. It must be the case that on the $k$'th line, $x = n$. If there are multiple solutions, print any of them.
You can output this answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. The same goes for the character $c$ denoting the player in the trade ($\texttt{Q}, \texttt{K}, \texttt{J}$ will all be accepted alongside their lowercase variants).
### Example
#### Input #1
```
2
3
1 3 2
2 1 3
1 2 3
4
2 3 1 4
1 2 3 4
1 4 2 3
```
#### Output #1
```
YES
2
k 2
q 3
NO
```
### Note
In the first testcase, Alice can trade with the King to get card $2$. She can then trade with the Queen to get card $3$.
In the second testcase, even though Alice can trade with the Queen to get card $3$, with the King to get card $2$, and then with the Jack to get card $4$, this is not a valid solution since it doesn't respect Alice's preferences. We can show that there is no way for Alice to get to card $4$. | codeforces | https://codeforces.com/problemset/problem/2028/D |
2028E | E. Alice's Adventures in the Rabbit Hole | medium | Alice is at the bottom of the rabbit hole! The rabbit hole can be modeled as a tree$^{\text{∗}}$ which has an exit at vertex $1$, and Alice starts at some vertex $v$. She wants to get out of the hole, but unfortunately, the Queen of Hearts has ordered her execution.
Each minute, a fair coin is flipped. If it lands heads, Alice gets to move to an adjacent vertex of her current location, and otherwise, the Queen of Hearts gets to pull Alice to an adjacent vertex of the Queen's choosing. If Alice ever ends up on any of the non-root leaves$^{\text{†}}$ of the tree, Alice loses.
Assuming both of them move optimally, compute the probability that Alice manages to escape for every single starting vertex $1\le v\le n$. Since these probabilities can be very small, output them modulo $998\,244\,353$.
Formally, let $M = 998\,244\,353$. It can be shown that the exact 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 < M$ and $x \cdot q \equiv p \pmod{M}$.
$^{\text{∗}}$A tree is a connected simple graph which has $n$ vertices and $n-1$ edges.
$^{\text{†}}$A leaf is a vertex that is connected to exactly one edge.
### 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 a single integer $n$ ($2\le n\le 2\cdot 10^5$) — the number of vertices in the tree.
The $i$-th of the next $n - 1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$ and $x_i \neq y_i$) — the edges of the tree. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
### Output
For each test case, output $n$ integers on one line — the probabilities of Alice escaping starting from vertex $1, 2, \ldots, n$. Since these probabilities can be very small, output them modulo $998\,244\,353$.
### Example
#### Input #1
```
2
5
1 2
1 3
2 4
3 5
9
1 2
2 3
4 5
5 6
7 8
8 9
2 4
5 7
```
#### Output #1
```
1 499122177 499122177 0 0
1 499122177 0 332748118 166374059 0 443664157 720954255 0
```
### Note
For the first test case:
1. Alice escapes from the root (vertex $1$) by definition with probability $1$.
2. Alice immediately loses from vertices $4$ and $5$ since they are leaves.
3. From the other two vertices, Alice escapes with probability $\frac 12$ since the Queen will pull her to the leaves. | codeforces | https://codeforces.com/problemset/problem/2028/E |
2029F | F. Palindrome Everywhere | medium | You are given a cycle with $n$ vertices numbered from $0$ to $n-1$. For each $0\le i\le n-1$, there is an undirected edge between vertex $i$ and vertex $((i+1)\bmod n)$ with the color $c_i$ ($c_i=\texttt{R}$ or $\texttt{B}$).
Determine whether the following condition holds for every pair of vertices $(i,j)$ ($0\le i<j\le n-1$):
- There exists a palindrome route between vertex $i$ and vertex $j$. Note that the route may not be simple. Formally, there must exist a sequence $p=[p_0,p_1,p_2,\ldots,p_m]$ such that:
- $p_0=i$, $p_m=j$;
- For each $0\leq x\le m-1$, either $p_{x+1}=(p_x+1)\bmod n$ or $p_{x+1}=(p_{x}-1)\bmod n$;
- For each $0\le x\le y\le m-1$ satisfying $x+y=m-1$, the edge between $p_x$ and $p_{x+1}$ has the same color as the edge between $p_y$ and $p_{y+1}$.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer $n$ ($3\leq n\leq10^6$) — the number of vertices in the cycle.
The second line contains a string $c$ of length $n$ ($c_i=\texttt{R}$ or $\texttt{B}$) — the color of each edge.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
### Output
For each test case, print "YES" (without quotes) if there is a palindrome route between any pair of nodes, and "NO" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
### Example
#### Input #1
```
7
5
RRRRR
5
RRRRB
5
RBBRB
6
RBRBRB
6
RRBBRB
5
RBRBR
12
RRBRRBRRBRRB
```
#### Output #1
```
YES
YES
YES
NO
NO
YES
NO
```
### Note
In the first test case, it is easy to show that there is a palindrome route between any two vertices.
In the second test case, for any two vertices, there exists a palindrome route with only red edges.
In the third test case, the cycle is as follows: $0\color{red}{\overset{\texttt{R}}{\longleftrightarrow}}1\color{blue}{\overset{\texttt{B}}{\longleftrightarrow}}2\color{blue}{\overset{\texttt{B}}{\longleftrightarrow}}3\color{red}{\overset{\texttt{R}}{\longleftrightarrow}}4\color{blue}{\overset{\texttt{B}}{\longleftrightarrow}}0$. Take $(i,j)=(0,3)$ as an example, then $0\color{red}{\overset{\texttt{R}}{\longrightarrow}}1\color{blue}{\overset{\texttt{B}}{\longrightarrow}}2\color{blue}{\overset{\texttt{B}}{\longrightarrow}}3\color{red}{\overset{\texttt{R}}{\longrightarrow}}4\color{blue}{\overset{\texttt{B}}{\longrightarrow}}0\color{blue}{\overset{\texttt{B}}{\longrightarrow}}4\color{red}{\overset{\texttt{R}}{\longrightarrow}}3$ is a palindrome route. Thus, the condition holds for $(i,j)=(0,3)$.
In the fourth test case, when $(i,j)=(0,2)$, there does not exist a palindrome route. | codeforces | https://codeforces.com/problemset/problem/2029/F |
2029D | D. Cool Graph | medium | You are given an undirected graph with $n$ vertices and $m$ edges.
You can perform the following operation at most $2\cdot \max(n,m)$ times:
- Choose three distinct vertices $a$, $b$, and $c$, then for each of the edges $(a,b)$, $(b,c)$, and $(c,a)$, do the following:
- If the edge does not exist, add it. On the contrary, if it exists, remove it.
A graph is called cool if and only if one of the following holds:
- The graph has no edges, or
- The graph is a tree.
You have to make the graph cool by performing the above operations. Note that you can use at most $2\cdot \max(n,m)$ operations.
It can be shown that there always exists at least one solution.
### Input
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases. The description of test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($3\le n\le 10^5$, $0\le m\le \min\left(\frac{n(n-1)}{2},2\cdot 10^5\right)$) — the number of vertices and the number of edges.
Then $m$ lines follow, the $i$-th line contains two integers $u_i$ and $v_i$ ($1\le u_i,v_i\le n$) — the two nodes that the $i$-th edge connects.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$, and the sum of $m$ over all test cases does not exceed $2\cdot 10^5$.
It is guaranteed that there are no self-loops or multiple-edges in the given graph.
### Output
For each test case, in the first line output an integer $k$ ($0\le k\le 2\cdot \max(n, m)$) — the number of operations.
Then output $k$ lines, the $i$-th line containing three distinct integers $a$, $b$, and $c$ ($1\le a,b,c\le n$) — the three integers you choose in the $i$-th operation.
If there are multiple solutions, you can output any of them.
### Example
#### Input #1
```
5
3 0
3 1
1 2
3 2
1 2
2 3
3 3
1 2
2 3
3 1
6 6
1 2
1 6
4 5
3 4
4 6
3 6
```
#### Output #1
```
0
1
1 2 3
0
1
1 2 3
3
1 3 6
2 4 5
3 4 6
```
### Note
In the first test case, the graph is already cool because there are no edges.
In the second test case, after performing the only operation, the graph becomes a tree, so it is cool.
In the third test case, the graph is already cool because it is a tree.
In the fourth test case, after performing the only operation, the graph has no edges, so it is cool.
In the fifth test case:
OperationGraph before the operationGraph after the operation$1$$2$$3$
Note that after the first operation, the graph has already become cool, and there are two extra operations. As the graph is still cool after the two extra operations, this is a valid answer. | codeforces | https://codeforces.com/problemset/problem/2029/D |
2029E | E. Common Generator | medium | For two integers $x$ and $y$ ($x,y\ge 2$), we will say that $x$ is a generator of $y$ if and only if $x$ can be transformed to $y$ by performing the following operation some number of times (possibly zero):
- Choose a divisor $d$ ($d\ge 2$) of $x$, then increase $x$ by $d$.
For example,
- $3$ is a generator of $8$ since we can perform the following operations: $3 \xrightarrow{d = 3} 6 \xrightarrow{d = 2} 8$;
- $4$ is a generator of $10$ since we can perform the following operations: $4 \xrightarrow{d = 4} 8 \xrightarrow{d = 2} 10$;
- $5$ is not a generator of $6$ since we cannot transform $5$ into $6$ with the operation above.
Now, Kevin gives you an array $a$ consisting of $n$ pairwise distinct integers ($a_i\ge 2$).
You have to find an integer $x\ge 2$ such that for each $1\le i\le n$, $x$ is a generator of $a_i$, or determine that such an integer does not exist.
### Input
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 10^4$) — 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 10^5$) — the length of the array $a$.
The second line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($2\le a_i\le 4\cdot 10^5$) — the elements in the array $a$. It is guaranteed that the elements are pairwise distinct.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
### Output
For each test case, output a single integer $x$ — the integer you found. Print $-1$ if there does not exist a valid $x$.
If there are multiple answers, you may output any of them.
### Example
#### Input #1
```
4
3
8 9 10
4
2 3 4 5
2
147 154
5
3 6 8 25 100000
```
#### Output #1
```
2
-1
7
3
```
### Note
In the first test case, for $x=2$:
- $2$ is a generator of $8$, since we can perform the following operations: $2 \xrightarrow{d = 2} 4 \xrightarrow{d = 4} 8$;
- $2$ is a generator of $9$, since we can perform the following operations: $2 \xrightarrow{d = 2} 4 \xrightarrow{d = 2} 6 \xrightarrow{d = 3} 9$.
- $2$ is a generator of $10$, since we can perform the following operations: $2 \xrightarrow{d = 2} 4 \xrightarrow{d = 2} 6 \xrightarrow{d = 2} 8 \xrightarrow{d = 2} 10$.
In the second test case, it can be proven that it is impossible to find a common generator of the four integers. | codeforces | https://codeforces.com/problemset/problem/2029/E |
2029G | G. Balanced Problem | medium | There is an array $a$ consisting of $n$ integers. Initially, all elements of $a$ are equal to $0$.
Kevin can perform several operations on the array. Each operation is one of the following two types:
- Prefix addition — Kevin first selects an index $x$ ($1\le x\le n$), and then for each $1\le j\le x$, increases $a_j$ by $1$;
- Suffix addition — Kevin first selects an index $x$ ($1\le x\le n$), and then for each $x\le j\le n$, increases $a_j$ by $1$.
In the country of KDOI, people think that the integer $v$ is balanced. Thus, Iris gives Kevin an array $c$ consisting of $n$ integers and defines the beauty of the array $a$ as follows:
- Initially, set $b=0$;
- For each $1\le i\le n$, if $a_i=v$, add $c_i$ to $b$;
- The beauty of $a$ is the final value of $b$.
Kevin wants to maximize the beauty of $a$ after all the operations. However, he had already performed $m$ operations when he was sleepy. Now, he can perform an arbitrary number (possibly zero) of new operations.
You have to help Kevin find the maximum possible beauty if he optimally performs the new operations.
However, to make sure that you are not just rolling the dice, Kevin gives you an integer $V$, and you need to solve the problem for each $1\le v\le V$.
### Input
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 1000$) — the number of test cases. The description of test cases follows.
The first line of each test case contains three integers $n$, $m$, and $V$ ($1\le n, m\le 2\cdot 10^5$, $1\le V\le 2000$) — the length of the array $a$, the number of initial operations, and the number that Kevin gives you.
The second line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1\le c_i\le 10^9$) — the elements in the array $c$.
Then $m$ lines follow, the $i$-th line containing a character $op$ and an integer $x$ ($op=\mathtt{L}$ or $\mathtt{R}$, $1\le x\le n$) — the type of the $i$-th operation and the selected index.
- If $op=\mathtt{L}$, this operation is a prefix addition on index $x$;
- If $op=\mathtt{R}$, this operation is a suffix addition on index $x$.
It is guaranteed that:
- the sum of $n$ over all test cases does not exceed $2\cdot 10^5$;
- the sum of $m$ over all test cases does not exceed $2\cdot 10^5$;
- the sum of $V^2$ over all test cases does not exceed $4\cdot 10^6$.
### Output
For each test case, output $V$ integers in a single line, the $i$-th integer denoting the maximum possible beauty after Kevin performs some new operations when $v=i$.
### Example
#### Input #1
```
5
3 3 2
1 2 4
L 3
R 3
L 1
3 3 2
5 1 4
L 3
R 3
L 1
5 4 5
1 1 1 1 1
L 3
R 2
L 5
L 4
10 12 9
10 9 8 7 6 5 4 3 2 1
L 2
L 4
R 4
R 4
L 6
R 8
L 3
L 2
R 1
R 10
L 8
L 1
1 1 4
1000000000
L 1
```
#### Output #1
```
2 6
1 9
0 1 3 5 5
0 0 0 6 25 32 35 44 51
1000000000 1000000000 1000000000 1000000000
```
### Note
In the first test case, the array $a$ changes as follows for the initial operations: $[0, 0, 0] \xrightarrow{\mathtt{L}\ 3} [1, 1, 1] \xrightarrow{\mathtt{R}\ 3} [1, 1, 2] \xrightarrow{\mathtt{L}\ 1} [2, 1, 2]$.
- For $v=1$, it is optimal to not perform any new operations, and the beauty is $b=c_2=2$;
- For $v=2$, it is optimal to perform a prefix addition operation on index $2$. After that, $a$ becomes $[3,2,2]$, and the beauty is $b=c_2+c_3=6$.
In the second test case, for both $v=1$ and $v=2$, it is optimal to not perform any new operations. | codeforces | https://codeforces.com/problemset/problem/2029/G |
2036F | F. XORificator 3000 | medium | Alice has been giving gifts to Bob for many years, and she knows that what he enjoys the most is performing [bitwise XOR](http://tiny.cc/xor_wiki_eng) of interesting integers. Bob considers a positive integer $x$ to be interesting if it satisfies $x \not\equiv k (\bmod 2^i)$. Therefore, this year for his birthday, she gifted him a super-powerful "XORificator 3000", the latest model.
Bob was very pleased with the gift, as it allowed him to instantly compute the XOR of all interesting integers in any range from $l$ to $r$, inclusive. After all, what else does a person need for happiness? Unfortunately, the device was so powerful that at one point it performed XOR with itself and disappeared. Bob was very upset, and to cheer him up, Alice asked you to write your version of the "XORificator".
### Input
The first line of input contains a single integer $t$ $(1 \leq t \leq 10^4)$ — the number of XOR queries on the segment. The following $t$ lines contain the queries, each consisting of the integers $l$, $r$, $i$, $k$ $(1 \leq l \leq r \leq 10^{18}$, $0 \leq i \leq 30$, $0 \leq k < 2^i)$.
### Output
For each query, output a single integer — the XOR of all integers $x$ in the range $[l, r]$ such that $x \not\equiv k \mod 2^i$.
### Example
#### Input #1
```
6
1 3 1 0
2 28 3 7
15 43 1 0
57 2007 1 0
1010 1993 2 2
1 1000000000 30 1543
```
#### Output #1
```
2
2
13
0
4
1000000519
```
### Note
In the first query, the interesting integers in the range $[1, 3]$ are $1$ and $3$, so the answer will be $1 \oplus 3 = 2$. | codeforces | https://codeforces.com/problemset/problem/2036/F |
2036G | G. Library of Magic | medium | This is an interactive problem.
The Department of Supernatural Phenomena at the Oxenfurt Academy has opened the Library of Magic, which contains the works of the greatest sorcerers of Redania — $n$ ($3 \leq n \leq 10^{18}$) types of books, numbered from $1$ to $n$. Each book's type number is indicated on its spine. Moreover, each type of book is stored in the library in exactly two copies! And you have been appointed as the librarian.
One night, you wake up to a strange noise and see a creature leaving the building through a window. Three thick tomes of different colors were sticking out of the mysterious thief's backpack. Before you start searching for them, you decide to compute the numbers $a$, $b$, and $c$ written on the spines of these books. All three numbers are distinct.
So, you have an unordered set of tomes, which includes one tome with each of the pairwise distinct numbers $a$, $b$, and $c$, and two tomes for all numbers from $1$ to $n$, except for $a$, $b$, and $c$. You want to find these values $a$, $b$, and $c$.
Since you are not working in a simple library, but in the Library of Magic, you can only use one spell in the form of a query to check the presence of books in their place:
- "xor l r" — Bitwise XOR query with parameters $l$ and $r$. Let $k$ be the number of such tomes in the library whose numbers are greater than or equal to $l$ and less than or equal to $r$. You will receive the result of the computation $v_1 \oplus v_2 \oplus ... \oplus v_k$, where $v_1 ... v_k$ are the numbers on the spines of these tomes, and $\oplus$ denotes the operation of [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng).
Since your magical abilities as a librarian are severely limited, you can make no more than $150$ queries.
### Input
The first line of input contains an integer $t$ ($1 \le t \le 300$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($3 \leq n \leq 10^{18}$) — the number of types of tomes.
### Interaction
The interaction for each test case begins with reading the integer $n$.
Then you can make up to $150$ queries.
To make a query, output a string in the format "xor l r" (without quotes) ($1 \leq l \leq r \leq n$). After each query, read an integer — the answer to your query.
To report the answer, output a string in the format "ans a b c" (without quotes), where $a$, $b$, and $c$ are the numbers you found as the answer to the problem. You can output them in any order.
The interactor is not adaptive, which means that the answer is known before the participant makes queries and does not depend on the queries made by the participant.
After making $150$ queries, the answer to any other query will be $-1$. Upon receiving such an answer, terminate the program to receive a verdict of "WA" (Wrong answer).
After outputting a query, do not forget to output a newline and flush the output buffer. Otherwise, you will receive a verdict of "IL" (Idleness limit exceeded). To flush the buffer, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- refer to the documentation for other languages.
Hacks
To make a hack, use the following format.
The first line should contain a single integer $t$ ($1 \leq t \leq 300$) — the number of test cases.
The only line of each test case should contain four integers $n$, $a$, $b$, and $c$ ($3 \leq n \leq 10^{18}$, $1 \le a, b, c \le n$) — the number of books in the library and the numbers of the stolen tomes. The numbers $a$, $b$, and $c$ must be distinct.
### Example
#### Input #1
```
2
6
0
2
3
5
3
```
#### Output #1
```
xor 1 1
xor 2 2
xor 3 3
xor 4 6
ans 2 3 5
ans 1 2 3
```
### Note
In the first test case, the books in the library after the theft look like this:

Now consider the answers to the queries:
- For the query "xor 1 1", you receive the result $1 \oplus 1 = 0$. Two tomes satisfy the condition specified in the query — both with the number $1$.
- For the query "xor 2 2", you receive the result $2$, as only one tome satisfies the specified condition.
- For the query "xor 3 3", you receive the result $3$.
- For the query "xor 4 6", you receive the result $4 \oplus 6 \oplus 4 \oplus 5 \oplus 6 = 5$.
In the second test case, there are only $3$ types of books, and it is easy to guess that the missing ones have the numbers $1$, $2$, and $3$. | codeforces | https://codeforces.com/problemset/problem/2036/G |
2032E | E. Balanced | medium | You are given a cyclic array $a$ with $n$ elements, where $n$ is odd. In each operation, you can do the following:
- Choose an index $1 \le i \le n$ and increase $a_{i - 1}$ by $1$, $a_i$ by $2$, and $a_{i + 1}$ by $1$. The element before the first element is the last element because this is a cyclic array.
A cyclic array is called balanced if all its elements are equal to each other.
Find any sequence of operations to make this cyclic array balanced or determine that it is impossible. Please note that you do not have to minimize the number of operations.
### Input
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \le n < 2 \cdot 10^5$, $n$ is odd) — the length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^{6}$) — the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case:
- If it is impossible to make the cyclic array balanced, output $-1$.
- Otherwise, output $n$ integers $v_1, v_2, \ldots, v_n$ ($0 \leq v_i \leq 10^{18}$) — where $v_i$ denotes the number of operations applied to index $i$. It can be proved that if any solution exists, then there exists a solution under the given constraints. If there are several solutions under the given constraints, output any of them.
### Example
#### Input #1
```
6
3
2 1 2
3
1 2 3
5
1 2 1 2 1
7
1 2 1 2 1 3 1
9
10000 10000 10000 10000 10000 10001 10002 10001 10000
1
10
```
#### Output #1
```
0 1 0
2 1 0
2 0 3 0 2
4 2 7 0 8 0 6
1 1 1 1 1 1 0 1 1
0
```
### Note
In the first test case:
- After $1$ operation applied to index $i = 2$, the array $a = [3, 3, 3]$.
In the second test case:
- After $2$ operations applied to index $i = 1$, the array $a = [5, 4, 5]$.
- After $1$ operation applied to index $i = 2$, the array $a = [6, 6, 6]$.
In the third test case:
- After $2$ operations applied to index $i = 1$, the array $a = [5, 4, 1, 2, 3]$.
- After $3$ operations applied to index $i = 3$, the array $a = [5, 7, 7, 5, 3]$.
- After $2$ operations applied to index $i = 5$, the array $a = [7, 7, 7, 7, 7]$. | codeforces | https://codeforces.com/problemset/problem/2032/E |
2032F | F. Peanuts | medium | Having the magical beanstalk, Jack has been gathering a lot of peanuts lately. Eventually, he has obtained $n$ pockets of peanuts, conveniently numbered $1$ to $n$ from left to right. The $i$-th pocket has $a_i$ peanuts.
Jack and his childhood friend Alice decide to play a game around the peanuts. First, Alice divides the pockets into some boxes; each box will have a non-zero number of consecutive pockets, and each pocket will, obviously, belong to exactly one box. At the same time, Alice does not change the order of the boxes, that is, the boxes are numbered in ascending order of the indices of the pockets in them.
After that, Alice and Jack will take turns alternately, with Alice going first.
At each turn, the current player will remove a positive number of peanuts from exactly one pocket which belongs to the leftmost non-empty box (i.e., the leftmost box containing at least one non-empty pocket). In other words, if we number the boxes from left to right, then each player can only pick peanuts from the pocket in the $j$-th box ($j \ge 2$) only if the $(j - 1)$-th box has no peanuts left. The player who cannot make a valid move loses.
Alice is sure she will win since she has the advantage of dividing the pockets into boxes herself. Thus, she wanted to know how many ways there are for her to divide the peanuts into boxes at the start of the game so that she will win, assuming both players play optimally. Can you help her with the calculation?
As the result can be very large, output it modulo $998\,244\,353$.
### Input
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. 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 number of pockets.
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 number of peanuts in each pocket.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
### Output
For each test case, output a single integer — the number of ways for Alice to divide the pockets into boxes at the start of the game to guarantee her win, assuming both players play optimally, modulo $998\,244\,353$.
### Example
#### Input #1
```
5
3
1 2 3
4
1 2 3 1
5
1 1 1 1 1
2
1 1
10
1 2 3 4 5 6 7 8 9 10
```
#### Output #1
```
1
4
16
0
205
```
### Note
In the first test case, the only way for Alice to win is to divide the pockets into two boxes as follows: $([1, 2], [3])$ (the first box contains the first two pockets and the second box contains the third pocket). Alice wins by taking both peanuts from the second pocket, leaving Jack with $([1], [3])$. Jack is forced to take the only peanut left in the first box, which allows Alice to take the remaining ones in the second box.
In the second test case, the winning divisions for Alice are $([1], [2, 3, 1])$, $([1, 2, 3, 1])$, $([1, 2], [3], [1])$, and $([1, 2], [3, 1])$.
In the third test case, Alice always wins no matter how she divides the pockets into boxes.
In the fourth test case, Alice always loses no matter how she divides the pockets into boxes. | codeforces | https://codeforces.com/problemset/problem/2032/F |
2026D | D. Sums of Segments | medium | You are given a sequence of integers $[a_1, a_2, \dots, a_n]$. Let $s(l,r)$ be the sum of elements from $a_l$ to $a_r$ (i. e. $s(l,r) = \sum\limits_{i=l}^{r} a_i$).
Let's construct another sequence $b$ of size $\frac{n(n+1)}{2}$ as follows: $b = [s(1,1), s(1,2), \dots, s(1,n), s(2,2), s(2,3), \dots, s(2,n), s(3,3), \dots, s(n,n)]$.
For example, if $a = [1, 2, 5, 10]$, then $b = [1, 3, 8, 18, 2, 7, 17, 5, 15, 10]$.
You are given $q$ queries. During the $i$-th query, you are given two integers $l_i$ and $r_i$, and you have to calculate $\sum \limits_{j=l_i}^{r_i} b_j$.
### Input
The first line contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10 \le a_i \le 10$).
The third line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$).
Then $q$ lines follow, the $i$-th of them contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le \frac{n(n+1)}{2}$).
### Output
Print $q$ integers, the $i$-th of which should be equal to $\sum \limits_{j=l_i}^{r_i} b_j$.
### Example
#### Input #1
```
4
1 2 5 10
15
1 1
1 2
1 3
1 4
1 5
1 10
5 10
6 10
2 8
3 4
3 10
3 8
5 6
5 5
1 8
```
#### Output #1
```
1
4
12
30
32
86
56
54
60
26
82
57
9
2
61
``` | codeforces | https://codeforces.com/problemset/problem/2026/D |
2026F | F. Bermart Ice Cream | medium | In the Bermart chain of stores, a variety of ice cream is sold. Each type of ice cream has two parameters: price and tastiness.
Initially, there is one store numbered $1$, which sells nothing. You have to process $q$ queries of the following types:
- $1~x$ — a new store opens, that sells the same types of ice cream as store $x$. It receives the minimum available positive index. The order of the types of ice cream in the new store is the same as in store $x$.
- $2~x~p~t$ — a type of ice cream with price $p$ and tastiness $t$ becomes available in store $x$.
- $3~x$ — a type of ice cream that was available the longest (appeared the earliest) in store $x$ is removed.
- $4~x~p$ — for store $x$, find the maximum total tastiness of a subset of types of ice cream that are sold there, such that the total price does not exceed $p$ (each type can be used in the subset no more than once).
### Input
The first line contains a single integer $q$ ($1 \le q \le 3 \cdot 10^4$) — the number of queries.
Each of the following $q$ lines contains a query in the format described in the statement:
- $1~x$;
- $2~x~p~t$ ($1 \le p, t \le 2000$);
- $3~x$;
- $4~x~p$ ($1 \le p \le 2000$).
Additional constraints on the input data:
- $x$ in each query does not exceed the current number of stores (that is, $1$ plus the number of type $1$ queries);
- query type $3$ is not applied to a store that has no types of ice cream;
- there is at least one query of type $4$.
### Output
For each query of type $4$, output a single integer — for store $x$, find the maximum total tastiness of a subset of types of ice cream that are sold there, such that the total price does not exceed $p$ (each type can be used in the subset no more than once).
### Example
#### Input #1
```
12
2 1 5 7
2 1 3 4
4 1 4
4 1 8
4 1 2
1 1
2 2 4 10
4 1 9
4 2 9
3 1
4 1 9
4 2 9
```
#### Output #1
```
4
11
0
11
17
4
17
``` | codeforces | https://codeforces.com/problemset/problem/2026/F |
2026E | E. Best Subsequence | medium | Given an integer array $a$ of size $n$.
Let's define the value of the array as its size minus the number of set bits in the bitwise OR of all elements of the array.
For example, for the array $[1, 0, 1, 2]$, the bitwise OR is $3$ (which contains $2$ set bits), and the value of the array is $4-2=2$.
Your task is to calculate the maximum possible value of some subsequence of the given array.
### Input
The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 100$).
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i < 2^{60}$).
### Output
For each test case, print the maximum possible value of some subsequence of the given array.
### Example
#### Input #1
```
4
3
0 0 0
4
1 0 1 2
1
5
8
7 1 48 14 13 8 7 6
```
#### Output #1
```
3
2
0
3
``` | codeforces | https://codeforces.com/problemset/problem/2026/E |
2035F | F. Tree Operations | medium | This really says a lot about our society.
One day, a turtle gives you a tree with $n$ nodes rooted at node $x$. Each node has an initial nonnegative value; the $i$-th node has starting value $a_i$.
You want to make the values of all nodes equal to $0$. To do so, you will perform a series of operations on the tree, where each operation will be performed on a certain node. Define an operation on node $u$ as choosing a single node in $u$'s subtree$^{\text{∗}}$ and incrementing or decrementing its value by $1$. The order in which operations are performed on nodes is as follows:
- For $1 \le i \le n$, the $i$-th operation will be performed on node $i$.
- For $i > n$, the $i$-th operation will be performed on the same node as operation $i - n$.
More formally, the $i$-th operation will be performed on the $(((i - 1) \bmod n) + 1)$-th node.$^{\text{†}}$
Note that you cannot skip over operations; that is, you cannot perform the $i$-th operation without first performing operations $1, 2, \ldots, i - 1$.
Find the minimum number of operations you must perform before you can make the values of all nodes equal to $0$, assuming you pick operations optimally. If it's impossible to make the values of all nodes equal to $0$ after finite operations, output $-1$.
$^{\text{∗}}$The subtree of a node $u$ is the set of nodes for which $u$ lies on the shortest path from this node to the root, including $u$ itself.
$^{\text{†}}$Here, $a \bmod b$ denotes the remainder from dividing $a$ by $b$.
### Input
The first line contains a single integer $t$ ($1\le t\le 100$) — the number of test cases.
The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 2000$, $1 \le x \le n$) — the number of nodes and the root of the tree.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) — the starting value of each node.
Each of the next $n - 1$ lines of each test case contains two integers $u$ and $v$ ($1 \le u, v \le n$, $u \neq v$) representing an undirected edge from $u$ to $v$. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$.
### Output
For each test case, output a single integer denoting the minimum amount of operations needed to make all nodes $0$. If it's impossible to make all nodes $0$, output $-1$.
### Example
#### Input #1
```
5
2 1
1 2
1 2
3 2
2 1 3
2 1
3 2
4 1
1 1 0 1
1 2
2 3
1 4
12 6
14 4 5 6 12 9 5 11 6 2 1 12
3 9
10 6
6 12
4 3
3 1
5 11
9 7
5 6
1 8
2 8
5 1
1 1
0
```
#### Output #1
```
3
6
5
145
0
```
### Note
In the first test case, you can make the following valid sequence of operations:
- For operation $1$, decrease the value of node $1$. This is valid because $(((1 - 1) \bmod n) + 1) = 1$, and node $1$ is in the subtree of node $1$.
- For operation $2$, decrease the value of node $2$. This is valid because $(((2 - 1) \bmod n) + 1) = 2$, and node $2$ is in the subtree of node $2$.
- For operation $3$, decrease the value of node $2$. This is valid because $(((3 - 1) \bmod n) + 1) = 1$, and node $2$ is in the subtree of node $1$. | codeforces | https://codeforces.com/problemset/problem/2035/F |
2035E | E. Monster | medium | Man, this Genshin boss is so hard. Good thing they have a top-up of $6$ coins for only $ \$4.99$. I should be careful and spend no more than I need to, lest my mom catches me...
You are fighting a monster with $z$ health using a weapon with $d$ damage. Initially, $d=0$. You can perform the following operations.
- Increase $d$ — the damage of your weapon by $1$, costing $x$ coins.
- Attack the monster, dealing $d$ damage and costing $y$ coins.
You cannot perform the first operation for more than $k$ times in a row.
Find the minimum number of coins needed to defeat the monster by dealing at least $z$ damage.
### Input
The first line contains a single integer $t$ ($1\le t\le 100$) — the number of test cases.
The only line of each test case contains 4 integers $x$, $y$, $z$, and $k$ ($1\leq x, y, z, k\leq 10^8$) — the first operation's cost, the second operation's cost, the monster's health, and the limitation on the first operation.
### Output
For each test case, output the minimum number of coins needed to defeat the monster.
### Example
#### Input #1
```
4
2 3 5 5
10 20 40 5
1 60 100 10
60 1 100 10
```
#### Output #1
```
12
190
280
160
```
### Note
In the first test case, $x = 2$, $y = 3$, $z = 5$, and $k = 5$. Here's a strategy that achieves the lowest possible cost of $12$ coins:
- Increase damage by $1$, costing $2$ coins.
- Increase damage by $1$, costing $2$ coins.
- Increase damage by $1$, costing $2$ coins.
- Attack the monster, dealing $3$ damage, costing $3$ coins.
- Attack the monster, dealing $3$ damage, costing $3$ coins.
You deal a total of $3 + 3 = 6$ damage, defeating the monster who has $5$ health. The total number of coins you use is $2 + 2 + 2 + 3 + 3 = 12$ coins.
In the second test case, $x = 10$, $y = 20$, $z = 40$, and $k = 5$. Here's a strategy that achieves the lowest possible cost of $190$ coins:
- Increase damage by $5$, costing $5\cdot x$ = $50$ coins.
- Attack the monster once, dealing $5$ damage, costing $20$ coins.
- Increase damage by $2$, costing $2\cdot x$ = $20$ coins.
- Attack the monster $5$ times, dealing $5\cdot 7 = 35$ damage, costing $5\cdot y$ = $100$ coins.
You deal a total of $5 + 35 = 40$ damage, defeating the monster who has exactly $40$ health. The total number of coins you use is $50 + 20 + 20 + 100 = 190$ coins. | codeforces | https://codeforces.com/problemset/problem/2035/E |
2027E1 | E1. Bit Game (Easy Version) | medium | This is the easy version of this problem. The only difference is that you need to output the winner of the game in this version, and the number of stones in each pile are fixed. You must solve both versions to be able to hack.
Alice and Bob are playing a familiar game where they take turns removing stones from $n$ piles. Initially, there are $x_i$ stones in the $i$-th pile, and it has an associated value $a_i$. A player can take $d$ stones away from the $i$-th pile if and only if both of the following conditions are met:
- $1 \le d \le a_i$, and
- $x \, \& \, d = d$, where $x$ is the current number of stones in the $i$-th pile and $\&$ denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND).
The player who cannot make a move loses, and Alice goes first.
You're given the $a_i$ and $x_i$ values for each pile, please determine who will win the game if both players play optimally.
### Input
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 $n$ ($1 \le n \le 10^4$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i < 2^{30}$).
The third line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \le x_i < 2^{30}$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^4$.
### Output
Print a single line with the winner's name. If Alice wins, print "Alice", otherwise print "Bob" (without quotes).
### Example
#### Input #1
```
7
2
1 6
10 7
3
10 8 15
25 4 14
4
8 32 65 64
7 45 126 94
3
20 40 1
23 55 1
5
12345 9876 86419 8641 1
6789 54321 7532 97532 1
2
20 64
44 61
3
57 109 55
69 90 85
```
#### Output #1
```
Bob
Bob
Bob
Bob
Bob
Alice
Alice
```
### Note
In the first test case, neither player can take any stones from the first pile since there is no value of $d$ satisfying the conditions. For the second pile, to begin with, Alice can remove between $1$ and $6$ stones. No matter which move Alice performs, Bob can remove the rest of the stones on his turn. After Bob's move, there are no more moves that Alice can perform, so Bob wins.
In the second test case, here is one example of how the game might go. Alice moves first, and she decides to remove from the first pile. She cannot take $17$ stones, because $17 > 10$, which fails the first condition. She cannot take $10$ stones, because $25 \, \& \, 10 = 8$ which fails the second condition. One option is to take $9$ stones; now the pile has $16$ stones left. On Bob's turn he decides to take stones from the second pile; the only option here is to take all $4$. Now, no more stones can be taken from either of the first two piles, so Alice must take some stones from the last pile. She decides to take $12$ stones, and Bob then follows by taking the last $2$ stones on that pile. Since Alice now has no legal moves left, Bob wins. It can be shown that no matter which strategy Alice follows, Bob will always be able to win if he plays optimally. | codeforces | https://codeforces.com/problemset/problem/2027/E1 |
2027D2 | D2. The Endspeaker (Hard Version) | medium | This is the hard version of this problem. The only difference is that you need to also output the number of optimal sequences in this version. You must solve both versions to be able to hack.
You're given an array $a$ of length $n$, and an array $b$ of length $m$ ($b_i > b_{i+1}$ for all $1 \le i < m$). Initially, the value of $k$ is $1$. Your aim is to make the array $a$ empty by performing one of these two operations repeatedly:
- Type $1$ — If the value of $k$ is less than $m$ and the array $a$ is not empty, you can increase the value of $k$ by $1$. This does not incur any cost.
- Type $2$ — You remove a non-empty prefix of array $a$, such that its sum does not exceed $b_k$. This incurs a cost of $m - k$.
You need to minimize the total cost of the operations to make array $a$ empty. If it's impossible to do this through any sequence of operations, output $-1$. Otherwise, output the minimum total cost of the operations, and the number of sequences of operations which yield this minimum cost modulo $10^9 + 7$.
Two sequences of operations are considered different if you choose a different type of operation at any step, or the size of the removed prefix is different at any step.
### Input
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 3 \cdot 10^5$, $\boldsymbol{1 \le n \cdot m \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$).
The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le 10^9$).
It is also guaranteed that $b_i > b_{i+1}$ for all $1 \le i < m$.
It is guaranteed that the sum of $\boldsymbol{n \cdot m}$ over all test cases does not exceed $3 \cdot 10^5$.
### Output
For each test case, if it's possible to make $a$ empty, then output two integers. The first should be the minimum total cost of the operations, and the second should be the number of sequences of operations which achieve this minimum cost, modulo $10^9 + 7$.
If there is no possible sequence of operations which makes $a$ empty, then output a single integer $-1$.
### Example
#### Input #1
```
5
4 2
9 3 4 3
11 7
1 2
20
19 18
10 2
2 5 2 1 10 3 2 9 9 6
17 9
10 11
2 2 2 2 2 2 2 2 2 2
20 18 16 14 12 10 8 6 4 2 1
1 6
10
32 16 8 4 2 1
```
#### Output #1
```
1 3
-1
2 11
10 42
4 1
```
### Note
In the first test case, there are $3$ optimal sequences of operations which yield a total cost of $1$:
- All $3$ sequences begin with a type $2$ operation, removing the prefix $[9]$ to make $a = [3, 4, 3]$, incurring a cost of $1$. Then, we perform a type $1$ operation to increase the value of $k$ by $1$. All subsequent operations now incur a cost of $0$.
- One sequence continues by removing the prefixes $[3, 4]$ then $[3]$.
- Another sequence continues by removing the prefixes $[3]$ then $[4, 3]$.
- Another sequence continues by removing the prefixes $[3]$ then $[4]$ then $[3]$.
In the second test case, it's impossible to remove any prefix of the array since $a_1 > b_1$, so array $a$ cannot be made empty by any sequence of operations. | codeforces | https://codeforces.com/problemset/problem/2027/D2 |
2033G | G. Sakurako and Chefir | medium | Given a tree with $n$ vertices rooted at vertex $1$. While walking through it with her cat Chefir, Sakurako got distracted, and Chefir ran away.
To help Sakurako, Kosuke recorded his $q$ guesses. In the $i$-th guess, he assumes that Chefir got lost at vertex $v_i$ and had $k_i$ stamina.
Also, for each guess, Kosuke assumes that Chefir could move along the edges an arbitrary number of times:
- from vertex $a$ to vertex $b$, if $a$ is an ancestor$^{\text{∗}}$ of $b$, the stamina will not change;
- from vertex $a$ to vertex $b$, if $a$ is not an ancestor of $b$, then Chefir's stamina decreases by $1$.
If Chefir's stamina is $0$, he cannot make a move of the second type.
For each assumption, your task is to find the distance to the farthest vertex that Chefir could reach from vertex $v_i$, having $k_i$ stamina.
$^{\text{∗}}$Vertex $a$ is an ancestor of vertex $b$ if the shortest path from $b$ to the root passes through $a$.
### Input
The first line contains a single integer $t$ ($1\le t\le 10^4$) — the number of test cases.
Each test case is described as follows:
- The first line contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of vertices in the tree.
- The next $n-1$ lines contain the edges of the tree. It is guaranteed that the given edges form a tree.
- The next line consists of a single integer $q$ $(1\le q\le 2 \cdot 10^5)$, which denotes the number of guesses made by Kosuke.
- The next $q$ lines describe the guesses made by Kosuke, with two integers $v_i$, $k_i$ $(1\le v_i \le n, 0 \le k_i\le n)$.
It is guaranteed that the sum of $n$ and the sum of $q$ across all test cases does not exceed $2\cdot 10^5$.
### Output
For each test case and for each guess, output the maximum distance to the farthest vertex that Chefir could reach from the starting point $v_i$ having $k_i$ stamina.
### Example
#### Input #1
```
3
5
1 2
2 3
3 4
3 5
3
5 1
3 1
2 0
9
8 1
1 7
1 4
7 3
4 9
3 2
1 5
3 6
7
6 0
2 3
6 2
8 2
2 4
9 2
6 3
6
2 1
2 5
2 4
5 6
4 3
3
3 1
1 3
6 5
```
#### Output #1
```
2 1 2
0 5 2 4 5 5 5
1 3 4
```
### Note
In the first example:
- In the first query, you can go from vertex $5$ to vertex $3$ (after which your stamina will decrease by $1$ and become $0$), and then you can go to vertex $4$;
- In the second query, from vertex $3$ with $1$ stamina, you can only reach vertices $2$, $3$, $4$, and $5$;
- In the third query, from vertex $2$ with $0$ stamina, you can only reach vertices $2$, $3$, $4$, and $5$; | codeforces | https://codeforces.com/problemset/problem/2033/G |
2023D | D. Many Games | medium | Recently, you received a rare ticket to the only casino in the world where you can actually earn something, and you want to take full advantage of this opportunity.
The conditions in this casino are as follows:
- There are a total of $n$ games in the casino.
- You can play each game at most once.
- Each game is characterized by two parameters: $p_i$ ($1 \le p_i \le 100$) and $w_i$ — the probability of winning the game in percentage and the winnings for a win.
- If you lose in any game you decide to play, you will receive nothing at all (even for the games you won).
You need to choose a set of games in advance that you will play in such a way as to maximize the expected value of your winnings.
In this case, if you choose to play the games with indices $i_1 < i_2 < \ldots < i_k$, you will win in all of them with a probability of $\prod\limits_{j=1}^k \frac{p_{i_j}}{100}$, and in that case, your winnings will be equal to $\sum\limits_{j=1}^k w_{i_j}$.
That is, the expected value of your winnings will be $\left(\prod\limits_{j=1}^k \frac{p_{i_j}}{100}\right) \cdot \left(\sum\limits_{j=1}^k w_{i_j}\right)$.
To avoid going bankrupt, the casino owners have limited the expected value of winnings for each individual game. Thus, for all $i$ ($1 \le i \le n$), it holds that $w_i \cdot p_i \le 2 \cdot 10^5$.
Your task is to find the maximum expected value of winnings that can be obtained by choosing some set of games in the casino.
### Input
The first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the number of games offered to play.
The $i$-th of the following $n$ lines contains two integers $p_i$ and $w_i$ ($1 \leq p_i \leq 100$, $1 \leq w_i, p_i \cdot w_i \leq 2 \cdot 10^5$) — the probability of winning and the size of the winnings in the $i$-th game.
### Output
Output a single number — the maximum expected value of winnings in the casino that can be obtained by choosing some subset of games.
Your answer will be accepted if the relative or absolute error does not exceed $10^{-6}$. Formally, if $a$ is your answer and $b$ is the jury's answer, it will be accepted if $\frac{|a-b|}{\max(b, 1)} \le 10^{-6}$.
### Examples
#### Input #1
```
3
80 80
70 100
50 200
```
#### Output #1
```
112.00000000
```
#### Input #2
```
2
100 1
100 1
```
#### Output #2
```
2.00000000
```
#### Input #3
```
4
1 100
2 1000
2 100
3 1
```
#### Output #3
```
20.00000000
```
#### Input #4
```
5
34 804
78 209
99 191
61 439
90 79
```
#### Output #4
```
395.20423800
```
### Note
In the first example, you can choose the first and third games. In this case, the expected value of winnings will be $\left(\frac{p_1}{100}\cdot \frac{p_3}{100}\right) \cdot (w_1 + w_3) = \left(\frac{80}{100}\cdot \frac{50}{100}\right) \cdot (80 + 200) = 112$.
In the second example, you can choose the first and second games. In this case, the expected value of winnings will be $\left(\frac{p_1}{100}\cdot \frac{p_2}{100}\right) \cdot (w_1 + w_2) = \left(\frac{100}{100}\cdot \frac{100}{100}\right) \cdot (1 + 1) = 2$.
In the third example, you can choose only the second game. In this case, the expected value of winnings will be $\frac{p_2}{100} \cdot w_2 = \frac{2}{100} \cdot 1000 = 20$. | codeforces | https://codeforces.com/problemset/problem/2023/D |
2023C | C. C+K+S | medium | You are given two strongly connected$^{\dagger}$ directed graphs, each with exactly $n$ vertices, but possibly different numbers of edges. Upon closer inspection, you noticed an important feature — the length of any cycle in these graphs is divisible by $k$.
Each of the $2n$ vertices belongs to exactly one of two types: incoming or outgoing. For each vertex, its type is known to you.
You need to determine whether it is possible to draw exactly $n$ directed edges between the source graphs such that the following four conditions are met:
- The ends of any added edge lie in different graphs.
- From each outgoing vertex, exactly one added edge originates.
- Into each incoming vertex, exactly one added edge enters.
- In the resulting graph, the length of any cycle is divisible by $k$.
$^{\dagger}$A strongly connected graph is a graph in which there is a path from every vertex to every other vertex.
### Input
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$) — the number of vertices in each graph and the value by which the length of each cycle is divisible.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($a_i \in \{0, 1\}$). If $a_i = 0$, then vertex $i$ of the first graph is incoming. If $a_i = 1$, then vertex $i$ of the first graph is outgoing.
The third line of each test case contains a single integer $m_1$ ($1 \le m_1 \le 5 \cdot 10^5$) — the number of edges in the first graph.
The next $m_1$ lines contain descriptions of the edges of the first graph. The $i$-th of them contains two integers $v_i$ and $u_i$ ($1 \le v_i, u_i \le n$) — an edge in the first graph leading from vertex $v_i$ to vertex $u_i$.
Next, in the same format, follows the description of the second graph.
The next line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($b_i \in \{0, 1\}$). If $b_i = 0$, then vertex $i$ of the second graph is incoming. If $b_i = 1$, then vertex $i$ of the second graph is outgoing.
The next line contains a single integer $m_2$ ($1 \le m_2 \le 5 \cdot 10^5$) — the number of edges in the second graph.
The next $m_2$ lines contain descriptions of the edges of the second graph. The $i$-th of them contains two integers $v_i$ and $u_i$ ($1 \le v_i, u_i \le n$) — an edge in the second graph leading from vertex $v_i$ to vertex $u_i$.
It is guaranteed that both graphs are strongly connected, and the lengths of all cycles are divisible by $k$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. It is guaranteed that the sum of $m_1$ and the sum of $m_2$ over all test cases does not exceed $5 \cdot 10^5$.
### Output
For each test case, output "YES" (without quotes) if it is possible to draw $n$ new edges such that all conditions are met, and "NO" (without quotes) otherwise.
You may output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
### Example
#### Input #1
```
3
4 2
1 0 0 1
4
1 2
2 3
3 4
4 1
1 0 0 1
4
1 3
3 2
2 4
4 1
3 3
0 0 0
3
1 2
2 3
3 1
1 1 0
3
1 2
2 3
3 1
4 2
1 1 1 1
4
1 2
2 3
3 4
4 1
0 0 0 0
6
1 2
2 1
1 3
3 1
1 4
4 1
```
#### Output #1
```
YES
NO
YES
```
### Note
In the first test case, it is possible to draw edges from the first graph to the second graph as $(1, 3)$ and $(4, 2)$ (the first number in the pair is the vertex number in the first graph, and the second number in the pair is the vertex number in the second graph), and from the second graph to the first graph as $(1, 2)$, $(4, 3)$ (the first number in the pair is the vertex number in the second graph, and the second number in the pair is the vertex number in the first graph).
In the second test case, there are a total of $4$ incoming vertices and $2$ outgoing vertices, so it is not possible to draw $3$ edges. | codeforces | https://codeforces.com/problemset/problem/2023/C |
2030E | E. MEXimize the Score | medium | Suppose we partition the elements of an array $b$ into any number $k$ of non-empty multisets $S_1, S_2, \ldots, S_k$, where $k$ is an arbitrary positive integer. Define the score of $b$ as the maximum value of $\\operatorname{MEX}(S\_1)
$$^{\text{∗}}$$
operatorname{MEX}(S\_2) + \\ldots + \\operatorname{MEX}(S\_k)$ over all possible partitions of $b$ for any integer $k$.
Envy is given an array $a$ of size $n$. Since he knows that calculating the score of $a$ is too easy for you, he instead asks you to calculate the sum of scores of all $2^n - 1$ non-empty subsequences of $a$.$^{\text{†}}$ Since this answer may be large, please output it modulo $998\,244\,353$.
$^{\\text{∗}}$$\\operatorname{MEX}$ of a collection of integers $c\_1, c\_2, \\ldots, c\_k$ is defined as the smallest non-negative integer $x$ that does not occur in the collection $c$. For example, $\\operatorname{MEX}(\[0,1,2,2\]) = 3$ and $\\operatorname{MEX}(\[1,2,2\]) = 0$
$^{\text{†}}$A sequence $x$ is a subsequence of a sequence $y$ if $x$ can be obtained from $y$ by deleting several (possibly, zero or all) elements.
### Input
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the length of $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < n$) — the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output the answer, modulo $998\,244\,353$.
### Example
#### Input #1
```
4
3
0 0 1
4
0 0 1 1
5
0 0 1 2 2
4
1 1 1 1
```
#### Output #1
```
11
26
53
0
```
### Note
In the first testcase, we must consider seven subsequences:
- $[0]$: The score is $1$.
- $[0]$: The score is $1$.
- $[1]$: The score is $0$.
- $[0,0]$: The score is $2$.
- $[0,1]$: The score is $2$.
- $[0,1]$: The score is $2$.
- $[0,0,1]$: The score is $3$.
The answer for the first testcase is $1+1+2+2+2+3=11$.
In the last testcase, all subsequences have a score of $0$. | codeforces | https://codeforces.com/problemset/problem/2030/E |
2030G1 | G1. The Destruction of the Universe (Easy Version) | medium | This is the easy version of the problem. In this version, $n \leq 5000$. You can only make hacks if both versions of the problem are solved.
Orangutans are powerful beings—so powerful that they only need $1$ unit of time to destroy every vulnerable planet in the universe!
There are $n$ planets in the universe. Each planet has an interval of vulnerability $[l, r]$, during which it will be exposed to destruction by orangutans. Orangutans can also expand the interval of vulnerability of any planet by $1$ unit.
Specifically, suppose the expansion is performed on planet $p$ with interval of vulnerability $[l_p, r_p]$. Then, the resulting interval of vulnerability may be either $[l_p - 1, r_p]$ or $[l_p, r_p + 1]$.
Given a set of planets, orangutans can destroy all planets if the intervals of vulnerability of all planets in the set intersect at least one common point. Let the score of such a set denote the minimum number of expansions that must be performed.
Orangutans are interested in the sum of scores of all non-empty subsets of the planets in the universe. As the answer can be large, output it modulo $998\,244\,353$.
### Input
The first line contains an integer $t$ ($1 \leq t \leq 1000$) — the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 5000$) — the number of planets in the universe.
The following $n$ lines contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq n$) — the initial interval of vulnerability of the $i$-th planet.
It is guaranteed that the sum of $n$ does not exceed $5000$ over all test cases.
### Output
For each test case, output an integer — the sum of scores to destroy all non-empty subsets of the planets in the universe, modulo $998\,244\,353$.
### Example
#### Input #1
```
3
3
1 1
2 3
3 3
4
1 4
2 3
2 4
1 1
5
1 2
2 3
3 4
4 5
1 5
```
#### Output #1
```
5
6
24
```
### Note
In the first testcase, there are seven non-empty subsets of planets we must consider:
- For each of the subsets $\{[1,1]\}, \{[2,3]\}, \{[3,3]\}$, the score is $0$.
- For the subset $\{[2,3], [3,3]\}$, the score is $0$, because the point $3$ is already contained in both planets' interval of vulnerability.
- For the subset $\{[1,1], [2,3]\}$, the score is $1$. By using one operation on changing the interval of vulnerability of the second planet to be $[1,3]$, the two planets now both have the point $1$ in their interval.
- For the subset $\{[1,1], [3,3]\}$, the score is $2$. By using two operations on changing the interval of vulnerability of the first planet to be $[1,3]$, the two planets now both have the point $3$ in their interval.
- For the subset $\{[1,1], [2,3], [3,3]\}$, the score is $2$. By using one operation on changing the interval of vulnerability of the first planet to be $[1,2]$ and one operation on changing the interval of vulnerability of the third planet to $[2,3]$, all three planets will have the point $2$ in their interval.
The sum of scores of all non-empty subsets of the first testcase is $0 \cdot 4 + 1 \cdot 1 + 2\cdot2 = 5$. | codeforces | https://codeforces.com/problemset/problem/2030/G1 |
2030F | F. Orangutan Approved Subarrays | medium | Suppose you have an array $b$. Initially, you also have a set $S$ that contains all distinct elements of $b$. The array $b$ is called orangutan-approved if it can be emptied by repeatedly performing the following operation:
- In one operation, select indices $l$ and $r$ ($1 \leq l \leq r \leq |b|$) such that $v = b_l = b_{l+1} = \ldots = b_r$ and $v$ is present in $S$. Remove $v$ from $S$, and simultaneously remove all $b_i$ such that $l \leq i \leq r$. Then, reindex the elements $b_{r+1}, b_{r+2}, \ldots$ as $b_l, b_{l+1}, \ldots$ accordingly.
You are given an array $a$ of length $n$ and $q$ queries.
Each query consists of two indices $l$ and $r$ ($1 \le l \le r \le n$), and you need to determine whether or not the subarray $a_{l}, a_{l+1}, \ldots, a_r$ is orangutan-approved.
### Input
The first line contains $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains integers $n$ and $q$ ($1 \leq n,q \leq 2 \cdot 10^5$) — the size of $a$ and the number of queries, respectively.
The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) — the elements of the array $a$.
The following $q$ lines contain two integers $l$ and $r$ — the endpoints of the subarray for each query ($1 \leq l \leq r \leq n$).
It is guaranteed that the sum of $n$ and $q$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each query, output "YES" (without quotes) if the subarray from $l$ to $r$ is orangutan-approved, 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 #1
```
3
4 2
1 2 2 1
1 4
1 3
5 3
1 2 1 2 1
2 5
3 5
1 3
8 4
1 2 3 2 1 3 2 3
1 5
2 8
3 5
6 8
```
#### Output #1
```
YES
YES
NO
YES
YES
YES
NO
YES
YES
```
### Note
In the first query of the first testcase, the answer is YES.
- Initially, $S=\{1,2\}$ and $b=[1,2,2,1]$
- Select $l=2$ and $r=3$. Since $b_2=b_3=2$ is in $S$, we may erase $b_2$ and $b_3$ from the array, as well as erasing $2$ from $S$. The set $S$ becomes $\{1\}$ and the array becomes $[1,1]$.
- Select $l=1$ and $r=2$. Since $b_1=b_2=1$ is in $S$, we may erase $b_1$ and $b_2$ from the array, as well as erasing $1$ from $S$. The set $S$ becomes $\{\}$ and the array becomes $[]$.
- Since the array is now empty, we can say the original array is orangutan-approved.
In the first query of the second testcase, the answer is NO, because it can be shown that the subarray $[2,1,2,1]$ cannot become empty through any sequence of valid operations. | codeforces | https://codeforces.com/problemset/problem/2030/F |
2025G | G. Variable Damage | medium | Monocarp is gathering an army to fight a dragon in a videogame.
The army consists of two parts: the heroes and the defensive artifacts. Each hero has one parameter — his health. Each defensive artifact also has one parameter — its durability.
Before the battle begins, Monocarp distributes artifacts to the heroes so that each hero receives at most one artifact.
The battle consists of rounds that proceed as follows:
- first, the dragon deals damage equal to $\frac{1}{a + b}$ (a real number without rounding) to each hero, where $a$ is the number of heroes alive and $b$ is the number of active artifacts;
- after that, all heroes with health $0$ or less die;
- finally, some artifacts are deactivated. An artifact with durability $x$ is deactivated when one of the following occurs: the hero holding the artifact either dies or receives $x$ total damage (from the start of the battle). If an artifact is not held by any hero, it is inactive from the beginning of the battle.
The battle ends when there are no heroes left alive.
Initially, the army is empty. There are $q$ queries: add a hero with health $x$ or an artifact with durability $y$ to the army. After each query, determine the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.
### Input
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) — the number of queries.
In the $i$-th of the following $q$ lines, there are two integers $t_i$ and $v_i$ ($t_i \in \{1, 2\}$; $1 \le v_i \le 10^9$) — the type of the query and the value of the query parameter. If the type is $1$, a hero with health $v_i$ is added. If the type is $2$, an artifact with durability $v_i$ is added.
### Output
Print $q$ integers. After each query, output the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.
### Examples
#### Input #1
```
3
2 5
1 4
1 10
```
#### Output #1
```
0
8
19
```
#### Input #2
```
10
1 9
1 6
2 4
1 8
1 3
2 10
1 3
1 6
1 10
2 6
```
#### Output #2
```
9
15
19
27
30
39
42
48
59
65
```
### Note
Let's consider the first example.
- An artifact with durability $5$ is added. Since there are no heroes yet, the battle ends immediately.
- A hero with health $4$ is added. Monocarp can give him an artifact with durability $5$. First, there are rounds in which the hero takes $\frac{1}{1 + 1} = \frac{1}{2}$ damage. After $8$ such rounds, a total of $4$ damage will have been dealt, and the hero will die, while the artifact will deactivate. There are no more heroes alive, so the battle ends after $8$ rounds.
- A hero with health $10$ is added. Now let the artifact with durability $5$ be with this hero. Then, in the first $12$ rounds, the heroes will take $12 \cdot \frac{1}{2 + 1} = 4$ damage, and the first hero will die. The second hero has $6$ health left, and the artifact has $1$ durability. Now the damage is $\frac{1}{2}$, so after another $2$ rounds, the artifact will deactivate. The second hero has $5$ health left. After another $5$ rounds, the second hero will die. Therefore, the answer is $12 + 2 + 5 = 19$. | codeforces | https://codeforces.com/problemset/problem/2025/G |
2025E | E. Card Game | medium | In the most popular card game in Berland, a deck of $n \times m$ cards is used. Each card has two parameters: suit and rank. Suits in the game are numbered from $1$ to $n$, and ranks are numbered from $1$ to $m$. There is exactly one card in the deck for each combination of suit and rank.
A card with suit $a$ and rank $b$ can beat a card with suit $c$ and rank $d$ in one of two cases:
- $a = 1$, $c \ne 1$ (a card of suit $1$ can beat a card of any other suit);
- $a = c$, $b > d$ (a card can beat any other card of the same suit but of a lower rank).
Two players play the game. Before the game starts, they receive exactly half of the deck each. The first player wins if for every card of the second player, he can choose his card that can beat it, and there is no card that is chosen twice (i. e. there exists a matching of the first player's cards with the second player's cards such that in each pair the first player's card beats the second player's card). Otherwise, the second player wins.
Your task is to calculate the number of ways to distribute the cards so that the first player wins. Two ways are considered different if there exists a card such that in one way it belongs to the first player and in the other way it belongs to the second player. The number of ways can be very large, so print it modulo $998244353$.
### Input
The only line contains two integers $n$ and $m$ ($1 \le n, m \le 500$).
Additional constraint on the input: $m$ is even.
### Output
Print a single integer — the number of ways to distribute the cards so that the first player wins, taken modulo $998244353$.
### Examples
#### Input #1
```
1 4
```
#### Output #1
```
2
```
#### Input #2
```
2 2
```
#### Output #2
```
2
```
#### Input #3
```
3 6
```
#### Output #3
```
1690
```
#### Input #4
```
5 4
```
#### Output #4
```
568
```
#### Input #5
```
500 500
```
#### Output #5
```
84693741
``` | codeforces | https://codeforces.com/problemset/problem/2025/E |
2025F | F. Choose Your Queries | medium | You are given an array $a$, consisting of $n$ integers (numbered from $1$ to $n$). Initially, they are all zeroes.
You have to process $q$ queries. The $i$-th query consists of two different integers $x_i$ and $y_i$. During the $i$-th query, you have to choose an integer $p$ (which is either $x_i$ or $y_i$) and an integer $d$ (which is either $1$ or $-1$), and assign $a_p = a_p + d$.
After each query, every element of $a$ should be a non-negative integer.
Process all queries in such a way that the sum of all elements of $a$ after the last query is the minimum possible.
### Input
The first line contains two integers $n$ and $q$ ($2 \le n \le 3 \cdot 10^5$; $1 \le q \le 3 \cdot 10^5$) — the number of elements in $a$ and the number of queries, respectively.
Then $q$ lines follow. The $i$-th of these lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$; $x_i \ne y_i$) — the description of the $i$-th query.
### Output
For each query, print a line containing two characters:
- the first character should be x if you choose $p=x_i$, or y if you choose $p=y_i$;
- the second character should be + if you choose $d=1$, or - if you choose $d=-1$.
If there are multiple answers, print any of them.
### Examples
#### Input #1
```
3 4
1 2
3 2
3 1
1 2
```
#### Output #1
```
y+
x+
x-
y-
```
#### Input #2
```
4 4
1 2
2 3
3 4
3 2
```
#### Output #2
```
y+
y+
x-
y-
```
#### Input #3
```
4 2
2 1
4 3
```
#### Output #3
```
y+
x+
``` | codeforces | https://codeforces.com/problemset/problem/2025/F |
2022E1 | E1. Billetes MX (Easy Version) | medium | This is the easy version of the problem. In this version, it is guaranteed that $q = 0$. You can make hacks only if both versions of the problem are solved.
An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:
- All elements of the grid are integers between $0$ and $2^{30}-1$, and
- For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.
However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.
For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.
### Input
The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k \le 10^5$; $q = 0$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.
The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) — indicating that $G_{r, c}$ is assigned the integer $v$.
The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) — indicating that $G_{r, c}$ is assigned the integer $v$.
It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.
It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.
### Output
For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.
### Example
#### Input #1
```
2
3 3 8 0
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
2 5 2 0
1 1 10
1 2 30
```
#### Output #1
```
1
489373567
```
### Note
In the first test case of the example, we have the following grid:
$0
$$6$$
10
$$6$$
0
$$12$$
10
$$12$$
It can be proven that the only valid value for tile $(3, 3)$ is $0$. | codeforces | https://codeforces.com/problemset/problem/2022/E1 |
2022E2 | E2. Billetes MX (Hard Version) | medium | This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved.
An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:
- All elements of the grid are integers between $0$ and $2^{30}-1$, and
- For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.
However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.
For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.
### Input
The first line contains $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) — the number of rows, the number of columns, the number of fixed cells, and the number of updates.
The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.
The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) indicating that $G_{r, c}$ is assigned the integer $v$.
It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.
It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.
### Output
For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.
### Example
#### Input #1
```
3
3 3 8 1
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
3 3 1
2 5 2 0
1 1 10
1 2 30
2 5 0 2
1 1 10
1 2 30
```
#### Output #1
```
1
0
489373567
651321892
769740174
489373567
```
### Note
In the first test case of the example, we initially have the following grid:
$0
$$6$$
10
$$6$$
0
$$12$$
10
$$12$$
It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the answer is $0$. | codeforces | https://codeforces.com/problemset/problem/2022/E2 |
2022D1 | D1. Asesino (Easy Version) | medium | This is the easy version of the problem. In this version, you can ask at most $n+69$ questions. You can make hacks only if both versions of the problem are solved.
This is an interactive problem.
It is a tradition in Mexico's national IOI trainings to play the game "Asesino", which is similar to "Among Us" or "Mafia".
Today, $n$ players, numbered from $1$ to $n$, will play "Asesino" with the following three roles:
- Knight: a Knight is someone who always tells the truth.
- Knave: a Knave is someone who always lies.
- Impostor: an Impostor is someone everybody thinks is a Knight, but is secretly a Knave.
Each player will be assigned a role in the game. There will be exactly one Impostor but there can be any (possible zero) number of Knights and Knaves.
As the game moderator, you have accidentally forgotten the roles of everyone, but you need to determine the player who is the Impostor.
To determine the Impostor, you will ask some questions. In each question, you will pick two players $i$ and $j$ ($1 \leq i, j \leq n$; $i \neq j$) and ask if player $i$ thinks that player $j$ is a Knight. The results of the question is shown in the table below.
KnightKnaveImpostorKnightYesNoYesKnaveNoYesNoImpostorNoYes—The response of the cell in row $a$ and column $b$ is the result of asking a question when $i$ has role $a$ and $j$ has row $b$. For example, the "Yes" in the top right cell belongs to row "Knight" and column "Impostor", so it is the response when $i$ is a Knight and $j$ is an Impostor.
Find the Impostor in at most $n + 69$ questions.
Note: the grader is adaptive: the roles of the players are not fixed in the beginning and may change depending on your questions. However, it is guaranteed that there exists an assignment of roles that is consistent with all previously asked questions under the constraints of this problem.
### Input
The first line of input contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($3 \le n \le 10^5$) — the number of people playing the game.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
### Interaction
To ask a question, output a line in the following format:
- "? i j" ($1 \leq i,j \leq n$; $i \neq j$) — to ask player $i$ if they think player $j$ is a Knight.
The jury will output a "1" if player $i$ thinks player $j$ is a Knight, and "0" otherwise.
When you have determined which player the Impostor is, output a line in the following format:
- "! i" ($1 \leq i \leq n$) — the Impostor is player $i$.
Note that answering does not count to your limit of $n+69$ questions.
If you have made an invalid output, used more than $n+69$ questions or wrongly determined the Impostor, the jury will respond with "-1" and you will receive a Wrong Answer verdict. Upon receiving "-1", your program must terminate immediately. Otherwise, you may receive an arbitrary verdict because your solution might be reading from a closed stream.
After printing a query do not forget to output the end of the line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- sys.stdout.flush() in Python;
- std::io::stdout().flush() in Rust;
- see the documentation for other languages.
Hack format
For hacks, use the following format.
The first line should contain a single integer $t$ — the number of test cases.
The first line of each test case should contain the integer $n$ followed by the string "manual".
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \le a_i \le 1$) — the roles of each player. $1$ denotes a Knight, $0$ denotes a Knave, and $-1$ dentoes an Impostor. There must be exactly one Impostor, that is there must be exactly one index $i$ such that $a_i=-1$.
As an example, the hack format for the example input is:
```
`2<br/>7 manual<br/>0 1 0 -1 0 1 0<br/>4 manual<br/>0 1 -1 0<br/>````
### Example
#### Input #1
```
2
7
1
0
0
1
1
0
0
1
4
0
1
1
1
```
#### Output #1
```
? 1 3
? 7 6
? 2 5
? 6 2
? 4 5
? 4 6
? 1 4
? 2 4
! 4
? 1 2
? 2 3
? 3 4
? 4 1
! 3
```
### Note
Note that the example test cases do not represent an optimal strategy for asking questions and are only shown for the sake of demonstrating the interaction format. Specifically, we cannot determine which player is the Impostor from the questions asked in the examples.
In the first test case of the example, players at indices $2$ and $6$ are Knights, players at indices $1$, $3$, $5$, and $7$ are Knaves, and the Impostor is at index $4$. The following is an explanation of the questions asked:
- In the first query, player $i$ is a Knave and player $j$ is a Knave. The answer is "yes" since Knaves always lie.
- In the second query, player $i$ is a Knave and player $j$ is a Knight. The answer is "no" since Knaves always lie.
- In the third query, player $i$ is a Knight and player $j$ is a Knave. The answer is "no" since Knights always tell the truth.
- In the fourth query, player $i$ is a Knight and player $j$ is a Knight. The answer is "yes" since Knights always tell the truth.
- In the fifth query, player $i$ is a Impostor and player $j$ is a Knave. The answer is "yes" since the Impostor always lies.
- In the sixth query, player $i$ is a Impostor and player $j$ is a Knight. The answer is "no" since the Impostor always lies.
- In the seventh query, player $i$ is a Knave and player $j$ is a Impostor. The answer is "no" since Knaves always lie and Knaves thinks that the Impostor is a Knight.
- In the eighth query, player $i$ is a Knight and player $j$ is a Impostor. The answer is "yes" since Knights always tell the truth and Knights think that the Impostor is a Knight. | codeforces | https://codeforces.com/problemset/problem/2022/D1 |
2022D2 | D2. Asesino (Hard Version) | medium | This is the hard version of the problem. In this version, you must use the minimum number of queries possible. You can make hacks only if both versions of the problem are solved.
This is an interactive problem.
It is a tradition in Mexico's national IOI trainings to play the game "Asesino", which is similar to "Among Us" or "Mafia".
Today, $n$ players, numbered from $1$ to $n$, will play "Asesino" with the following three roles:
- Knight: a Knight is someone who always tells the truth.
- Knave: a Knave is someone who always lies.
- Impostor: an Impostor is someone everybody thinks is a Knight, but is secretly a Knave.
Each player will be assigned a role in the game. There will be exactly one Impostor but there can be any (possible zero) number of Knights and Knaves.
As the game moderator, you have accidentally forgotten the roles of everyone, but you need to determine the player who is the Impostor.
To determine the Impostor, you will ask some questions. In each question, you will pick two players $i$ and $j$ ($1 \leq i, j \leq n$; $i \neq j$) and ask if player $i$ thinks that player $j$ is a Knight. The results of the question is shown in the table below.
KnightKnaveImpostorKnightYesNoYesKnaveNoYesNoImpostorNoYes—The response of the cell in row $a$ and column $b$ is the result of asking a question when $i$ has role $a$ and $j$ has row $b$. For example, the "Yes" in the top right cell belongs to row "Knight" and column "Impostor", so it is the response when $i$ is a Knight and $j$ is an Impostor.
Find the Impostor in the minimum number of queries possible. That is, let $f(n)$ be the minimum integer such that for $n$ players, there exists a strategy that can determine the Impostor using at most $f(n)$ questions. Then, you should use at most $f(n)$ questions to determine the Impostor.
Note: the grader is adaptive: the roles of the players are not fixed in the beginning and may change depending on your questions. However, it is guaranteed that there exists an assignment of roles that is consistent with all previously asked questions under the constraints of this problem.
### Input
The first line of input contains a single integer $t$ ($1 \leq t \leq 10^3$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($3 \le n \le 10^5$) — the number of people playing the game.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
### Interaction
To ask a question, output a line in the following format:
- "? i j" ($1 \leq i,j \leq n$; $i \neq j$) — to ask player $i$ if they think player $j$ is a Knight.
The jury will output a "1" if player $i$ thinks player $j$ is a Knight, and "0" otherwise.
When you have determined which player the Impostor is, output a line in the following format:
- "! i" ($1 \leq i \leq n$) — the Impostor is player $i$.
Note that answering does not count to your limit of $f(n)$ questions.
If you have made an invalid output, used more than $f(n)$ questions or wrongly determined the Impostor, the jury will respond with "-1" and you will receive a Wrong Answer verdict. Upon receiving "-1", your program must terminate immediately. Otherwise, you may receive an arbitrary verdict because your solution might be reading from a closed stream.
After printing a query do not forget to output the end of the line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:
- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- sys.stdout.flush() in Python;
- std::io::stdout().flush() in Rust;
- see the documentation for other languages.
Hack format
For hacks, use the following format.
The first line should contain a single integer $t$ — the number of test cases.
The first line of each test case should contain the integer $n$ followed by the string "manual".
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-1 \le a_i \le 1$) — the roles of each player. $1$ denotes a Knight, $0$ denotes a Knave, and $-1$ dentoes an Impostor. There must be exactly one Impostor, that is there must be exactly one index $i$ such that $a_i=-1$.
As an example, the hack format for the example input is:
```
`2<br/>7 manual<br/>0 1 0 -1 0 1 0<br/>4 manual<br/>0 1 -1 0<br/>````
### Example
#### Input #1
```
2
7
1
0
0
1
1
0
0
4
0
1
1
1
```
#### Output #1
```
? 1 3
? 7 6
? 2 5
? 6 2
? 4 5
? 4 6
? 1 4
! 4
? 1 2
? 2 3
? 3 4
? 4 1
! 3
```
### Note
Note that the example test cases do not represent an optimal strategy for asking questions and are only shown for the sake of demonstrating the interaction format. Specifically, we cannot determine which player is the Impostor from the questions asked in the examples.
In the first test case of the example, players at indices $2$ and $6$ are Knights, players at indices $1$, $3$, $5$, and $7$ are Knaves, and the Impostor is at index $4$. The following is an explanation of the questions asked:
- In the first query, player $i$ is a Knave and player $j$ is a Knave. The answer is "yes" since Knaves always lie.
- In the second query, player $i$ is a Knave and player $j$ is a Knight. The answer is "no" since Knaves always lie.
- In the third query, player $i$ is a Knight and player $j$ is a Knave. The answer is "no" since Knights always tell the truth.
- In the fourth query, player $i$ is a Knight and player $j$ is a Knight. The answer is "yes" since Knights always tell the truth.
- In the fifth query, player $i$ is a Impostor and player $j$ is a Knave. The answer is "yes" since the Impostor always lies.
- In the sixth query, player $i$ is a Impostor and player $j$ is a Knight. The answer is "no" since the Impostor always lies.
- In the seventh query, player $i$ is a Knave and player $j$ is a Impostor. The answer is "no" since Knaves always lie and Knaves thinks that the Impostor is a Knight. | codeforces | https://codeforces.com/problemset/problem/2022/D2 |
2021E1 | E1. Digital Village (Easy Version) | medium | This is the easy version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved.
Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$.
There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to.
For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet.
### 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 three integers $n$, $m$, $p$ ($2 \le n \le 400$; $n-1 \le m \le 400$; $1 \le p \le n$) — the number of houses, the number of cables and the number of houses that need internet.
The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) — the houses that need internet. It is guaranteed that all elements of $s$ are distinct.
The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) — the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph.
It is guaranteed that the sum of $n^3$ and the sum of $m^3$ do not exceed $10^8$.
### Output
For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.
### Example
#### Input #1
```
2
9 8 5
2 5 6 8 9
1 2 1
1 3 2
3 4 10
4 5 3
4 6 5
1 7 10
7 8 4
7 9 2
3 3 2
3 1
1 2 1
2 3 3
1 3 2
```
#### Output #1
```
34 19 9 4 0 0 0 0 0
2 0 0
```
### Note
In the first test case for $k=3$, a possible optimal solution is to install servers at vertices $2$, $6$ and $8$ and obtain the following latency:
- $\text{latency}(2) = 0$
- $\text{latency}(5) = \max(3, 5) = 5$
- $\text{latency}(6) = 0$
- $\text{latency}(8) = 0$
- $\text{latency}(9) = \max(2, 4) = 4$
So the total latency is $9$. | codeforces | https://codeforces.com/problemset/problem/2021/E1 |
2021E3 | E3. Digital Village (Extreme Version) | medium | This is the extreme version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved.
Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$.
There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to.
For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet.
### 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 3 integers $n$, $m$, $p$ ($2 \le n \le 2 \cdot 10^5$; $n-1 \le m \le 2 \cdot 10^5$; $1 \le p \le n$) — the number of houses, the number of cables, and the number of houses that need internet.
The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) — the houses that need internet. It is guaranteed that all elements of $s$ are distinct.
The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) — the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph.
It is guaranteed that the sum of $n$ and the sum of $m$ do not exceed $2 \cdot 10^5$.
### Output
For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.
### Example
#### Input #1
```
2
9 8 5
2 5 6 8 9
1 2 1
1 3 2
3 4 10
4 5 3
4 6 5
1 7 10
7 8 4
7 9 2
3 3 2
3 1
1 2 1
2 3 3
1 3 2
```
#### Output #1
```
34 19 9 4 0 0 0 0 0
2 0 0
```
### Note
In the first test case for $k=3$, a possible optimal solution is to install servers at vertices $2$, $6$ and $8$ and obtain the following latency:
- $\text{latency}(2) = 0$
- $\text{latency}(5) = \max(3, 5) = 5$
- $\text{latency}(6) = 0$
- $\text{latency}(8) = 0$
- $\text{latency}(9) = \max(2, 4) = 4$
So the total latency is $9$. | codeforces | https://codeforces.com/problemset/problem/2021/E3 |
2021E2 | E2. Digital Village (Hard Version) | medium | This is the hard version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved.
Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$.
There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to.
For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2000$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, $p$ ($2 \le n \le 5000$; $n-1 \le m \le 5000$; $1 \le p \le n$) — the number of houses, the number of cables, and the number of houses that need internet.
The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) — the houses that need internet. It is guaranteed that all elements of $s$ are distinct.
The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) — the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph.
It is guaranteed that the sum of $n$ and the sum of $m$ do not exceed $5000$.
### Output
For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.
### Example
#### Input #1
```
2
9 8 5
2 5 6 8 9
1 2 1
1 3 2
3 4 10
4 5 3
4 6 5
1 7 10
7 8 4
7 9 2
3 3 2
3 1
1 2 1
2 3 3
1 3 2
```
#### Output #1
```
34 19 9 4 0 0 0 0 0
2 0 0
```
### Note
In the first test case for $k=3$, a possible optimal solution is to install servers at vertices $2$, $6$ and $8$ and obtain the following latency:
- $\text{latency}(2) = 0$
- $\text{latency}(5) = \max(3, 5) = 5$
- $\text{latency}(6) = 0$
- $\text{latency}(8) = 0$
- $\text{latency}(9) = \max(2, 4) = 4$
So the total latency is $9$. | codeforces | https://codeforces.com/problemset/problem/2021/E2 |
2021D | D. Boss, Thirsty | medium | Pak Chanek has a friend who runs a drink stall in a canteen. His friend will sell drinks for $n$ days, numbered from day $1$ to day $n$. There are also $m$ types of drinks, numbered from $1$ to $m$.
The profit gained from selling a drink on a particular day can vary. On day $i$, the projected profit from selling drink of type $j$ is $A_{i, j}$. Note that $A_{i, j}$ can be negative, meaning that selling the drink would actually incur a loss.
Pak Chanek wants to help his friend plan the sales over the $n$ days. On day $i$, Pak Chanek must choose to sell at least one type of drink. Furthermore, the types of drinks sold on a single day must form a subarray. In other words, in each day, Pak Chanek will select $i$ and $j$ such that $1 \leq i \leq j \leq m$. Then all types of drinks between $i$ and $j$ (inclusive) will be sold.
However, to ensure that customers from the previous day keep returning, the selection of drink types sold on day $i$ ($i>1$) must meet the following conditions:
- At least one drink type sold on day $i$ must also have been sold on day $i-1$.
- At least one drink type sold on day $i$ must not have been sold on day $i-1$.
The daily profit is the sum of the profits from all drink types sold on that day. The total profit from the sales plan is the sum of the profits over $n$ days. What is the maximum total profit that can be achieved if Pak Chanek plans the sales optimally?
### Input
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 \leq n \leq 2 \cdot 10^5$; $3 \leq m \leq 2 \cdot 10^5$; $n \cdot m \leq 2 \cdot 10^5$) — the number of rows and columns in a grid.
The next $n$ lines of each test case contain $m$ integers each, where the $i$-th line contains the integers $A_{i,1} A_{i,2}, \ldots, A_{i,m}$ ($-10^9 \leq A_{i,j} \leq 10^9$) — project profits of each drink type on the $i$-th day.
It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output a single integer: the maximum profit that Pak Chanek can achieve.
### Example
#### Input #1
```
1
3 6
79 20 49 5 -1000 500
-105 9 109 24 -98 -499
14 47 12 39 23 50
```
#### Output #1
```
475
```
### Note
Here is Pak Chanek's optimal plan:

- On day $1$, Pak Chanek sells drink types $1$ to $3$. Generating a profit of $79+20+49 = 148$.
- On day $2$, Pak Chanek sells drink types $2$ to $4$. Generating a profit of $9+109+24 = 142$
- On day $3$, Pak Chanek sells drink types $1$ to $6$. Generating a profit of $185$.
So, the total profit of Pak Chanek's plan is $148 + 142 + 185 = 475$. | codeforces | https://codeforces.com/problemset/problem/2021/D |
2021C2 | C2. Adjust The Presentation (Hard Version) | medium | This is the hard version of the problem. In the two versions, the constraints on $q$ and the time limit are different. In this version, $0 \leq q \leq 2 \cdot 10^5$. You can make hacks only if all the versions of the problem are solved.
A team consisting of $n$ members, numbered from $1$ to $n$, is set to present a slide show at a large meeting. The slide show contains $m$ slides.
There is an array $a$ of length $n$. Initially, the members are standing in a line in the order of $a_1, a_2, \ldots, a_n$ from front to back. The slide show will be presented in order from slide $1$ to slide $m$. Each section will be presented by the member at the front of the line. After each slide is presented, you can move the member at the front of the line to any position in the lineup (without changing the order of the rest of the members). For example, suppose the line of members is $[\color{red}{3},1,2,4]$. After member $3$ presents the current slide, you can change the line of members into either $[\color{red}{3},1,2,4]$, $[1,\color{red}{3},2,4]$, $[1,2,\color{red}{3},4]$ or $[1,2,4,\color{red}{3}]$.
There is also an array $b$ of length $m$. The slide show is considered good if it is possible to make member $b_i$ present slide $i$ for all $i$ from $1$ to $m$ under these constraints.
However, your annoying boss wants to make $q$ updates to the array $b$. In the $i$-th update, he will choose a slide $s_i$ and a member $t_i$ and set $b_{s_i} := t_i$. Note that these updates are persistent, that is changes made to the array $b$ will apply when processing future updates.
For each of the $q+1$ states of array $b$, the initial state and after each of the $q$ updates, determine if the slideshow is good.
### 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 three integers $n$, $m$ and $q$ ($1 \le n, m \le 2 \cdot 10^5$; $0 \leq q \leq 2 \cdot 10^5$) — the number of members and the number of sections.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1 \le a_i \le n$) — the initial order of the members from front to back. It is guaranteed that each integer from $1$ to $n$ appears exactly once in $a$.
The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le n$) — the members who should present each section.
Each of the next $q$ lines contains two integers $s_i$ and $t_i$ ($1 \le s_i \le m$, $1 \le t_i \le n$) — parameters of an update.
It is guaranteed that the sum of $n$, the sum of $m$ and the sum of $q$ over all test cases do not exceed $2 \cdot 10^5$ respectively.
### Output
For each test case, output $q+1$ lines corresponding to the $q+1$ states of the array $b$. Output "YA" if the slide show is good, and "TIDAK" otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yA", "Ya", "ya", and "YA" will be recognized as positive responses.
### Example
#### Input #1
```
3
4 2 2
1 2 3 4
1 1
1 2
1 1
3 6 2
1 2 3
1 1 2 3 3 2
3 3
2 2
4 6 2
3 1 4 2
3 1 1 2 3 4
3 4
4 2
```
#### Output #1
```
YA
TIDAK
YA
YA
TIDAK
YA
TIDAK
YA
YA
```
### Note
For the first test case, you do not need to move the members as both slides are presented by member $1$, who is already at the front of the line. After that, set $b_1 := 2$, now slide $1$ must be presented by member $2$ which is impossible as member $1$ will present slide $1$ first. Then, set $b_1 = 1$, the $b$ is the same as the initial $b$, making a good presentation possible. | codeforces | https://codeforces.com/problemset/problem/2021/C2 |
2053H | H. Delicate Anti-monotonous Operations | hard | I shall be looking for you who would be out of Existence.
— HyuN, [Disorder](https://soundcloud.com/k-sounds-studio/g2r2018-hyun-disorder-feat-yuri)
There are always many repetitive tasks in life. Iris always dislikes them, so she refuses to repeat them. However, time cannot be turned back; we only have to move forward.
Formally, Iris has an integer sequence $a_1, a_2, \ldots, a_n$, where each number in the sequence is between $1$ and $w$, inclusive. It is guaranteed that $w \geq 2$.
Iris defines an operation as selecting two numbers $a_i, a_{i+1}$ satisfying $a_i = a_{i+1}$, and then changing them to two arbitrary integers within the range $[1, w]$. Iris does not like equality, so she must guarantee that $a_i \neq a_{i+1}$ after the operation. Two identical pairs $a_i, a_{i+1}$ can be selected multiple times.
Iris wants to know the maximum possible sum of all elements of $a$ after several (possible, zero) operations, as well as the minimum number of operations required to achieve this maximum value.
### Input
Each test contains multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $w$ ($1 \leq n \leq 2\cdot 10^5$, $2 \leq w \leq 10^8$) — the length of the array, and the maximum allowed value of the elements.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq w$) — the elements in the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
### Output
For each test case, output two integers — the maximum possible sum of all elements of $a$ and the minimum number of operations required, respectively.
### Example
#### Input #1
```
2
5 8
1 2 3 4 5
7 5
3 1 2 3 4 1 1
```
#### Output #1
```
15 0
34 6
```
### Note
In the first test case, no operation can be performed so the answers are $\sum a_i = 15$ and $0$, respectively.
In the second test case, the operations can be performed as follows:
$$[3, 1, 2, 3, 4, \underline{1, 1}] \rightarrow [3, 1, 2, 3, \underline{4, 4}, 5] \rightarrow [3, 1, 2, \underline{3, 3}, 5, 5] \rightarrow [3, 1, \underline{2, 2}, 5, 5, 5] \rightarrow [3, \underline{1, 1}, 5, 5, 5, 5] \rightarrow [\underline{3, 3}, 5, 5, 5, 5, 5] \rightarrow [4, 5, 5, 5, 5, 5, 5]$$
It can be shown this is optimal, so we should output $\sum a_i = 34$ and the number of operations, $6$, respectively. | codeforces | https://codeforces.com/problemset/problem/2053/H |
2053I2 | I2. Affectionate Arrays (Hard Version) | hard | Note that this statement is different to the version used in the official round. The statement has been corrected to a solvable version. In the official round, all submissions to this problem have been removed.
This is the hard version of the problem. The difference between the versions is that in this version, you need to compute the sum of value of different arrays. You can hack only if you solved all versions of this problem.
Iris treasures an integer array $a_1, a_2, \ldots, a_n$. She knows this array has an interesting property: the maximum absolute value of all elements is less than or equal to the sum of all elements, that is, $\max(\lvert a_i\rvert) \leq \sum a_i$.
Iris defines the boredom of an array as its maximum subarray$^{\text{∗}}$ sum.
Iris's birthday is coming, and Victor is going to send her another array $b_1, b_2, \ldots, b_m$ as a gift. For some seemingly obvious reasons, he decides the array $b_1, b_2, \ldots, b_m$ should have the following properties.
- $a_1, a_2, \ldots, a_n$ should be a subsequence$^{\text{†}}$ of $b_1, b_2, \ldots, b_m$.
- The two arrays have the same sum. That is, $\sum\limits_{i=1}^n a_i = \sum\limits_{i=1}^m b_i$.
- The boredom of $b_1, b_2, \ldots, b_m$ is the smallest possible.
- Among the arrays with the smallest boredom, the length of the array $b$ (i.e., $m$) is the smallest possible. And in this case, Iris will understand his regard as soon as possible!
For a possible array $b_1, b_2, \ldots, b_m$ satisfying all the conditions above, Victor defines the value of the array as the number of occurrences of array $a$ as subsequences in array $b$. That is, he counts the number of array $c_1, c_2, \ldots, c_{n}$ that $1\le c_1< c_2< \ldots< c_n\le m$ and for all integer $i$ that $1\le i\le n$, $b_{c_{i}}=a_i$ is satisfied, and let this be the value of array $b$.
Even constrained as above, there are still too many possible gifts. So Victor asks you to calculate the sum of value of all possible arrays $b_1, b_2, \ldots, b_m$. Since the answer may be large, Victor only needs the number modulo $998\,244\,353$. He promises you: if you help him successfully, he will share a bit of Iris's birthday cake with you.
$^{\text{∗}}$An array $c$ is a subarray of an array $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
$^{\text{†}}$A sequence $c$ is a subsequence of a sequence $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) element from arbitrary positions.
### Input
Each test contains multiple test cases. The first line of input contains an 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 \leq n \leq 3\cdot 10^6$) — the length of the array $a_1, a_2, \ldots, a_n$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) — the initial array. It is guaranteed that $\max(\lvert a_i\rvert) \leq \sum a_i$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3\cdot 10^6$.
### Output
For each test case, output a single line containing an integer: the sum of values of valid arrays $b_1, b_2, \ldots, b_m$, modulo $998\,244\,353$.
### Example
#### Input #1
```
5
4
1 2 3 4
4
2 -3 2 2
4
1 -2 2 1
10
2 -7 6 3 -1 4 2 -5 8 -4
20
4 -2 4 3 -2 1 5 2 3 6 -5 -1 -4 -2 -3 5 -3 1 -4 1
```
#### Output #1
```
1
2
2
20
1472
```
### Note
In the first test case, $a=[1, 2, 3, 4]$. The only possible array $b$ is $[1, 2, 3, 4]$, and its value is $1$.
In the second test case, $a=[2, -3, 2, 2]$. The possible arrays $b$ are $[1, 2, -3, 2, -1, 2]$ and $[2, 1, -3, 2, -1, 2]$. Both arrays have value $1$.
In the third test case, $a=[1, -2, 2, 1]$. The only possible array $b$ is $[1, 1, -2, 2, -1, 1]$. It has value $2$, because we can find arrays $c=[1,3,4,6]$ or $[2,3,4,6]$. That is, the array $a$ occurs twice in $b$, so the answer is $2$. | codeforces | https://codeforces.com/problemset/problem/2053/I2 |
2053G | G. Naive String Splits | hard | And I will: love the world that you've adored; wish the smile that you've longed for. Your hand in mine as we explore, please take me to tomorrow's shore.
— Faye Wong, [As Wished](https://www.youtube.com/watch?v=ZoJCN0pV7Qs)
Cocoly has a string $t$ of length $m$, consisting of lowercase English letters, and he would like to split it into parts. He calls a pair of strings $(x, y)$ beautiful if and only if there exists a sequence of strings $a_1, a_2, \ldots, a_k$, such that:
- $t = a_1 + a_2 + \ldots + a_k$, where $+$ denotes string concatenation.
- For each $1 \leq i \leq k$, at least one of the following holds: $a_i = x$, or $a_i = y$.
Cocoly has another string $s$ of length $n$, consisting of lowercase English letters. Now, for each $1 \leq i < n$, Cocoly wants you to determine whether the pair of strings $(s_1s_2 \ldots s_i, \, s_{i+1}s_{i+2} \ldots s_n)$ is beautiful.
Note: since the input and output are large, you may need to optimize them for this problem.
For example, in C++, it is enough to use the following lines at the start of the main() function:
```
`int main() {<br/> std::ios::sync_with_stdio(false);<br/> std::cin.tie(nullptr); std::cout.tie(nullptr);<br/>}<br/>````
### Input
Each test contains multiple test cases. The first line contains an integer $T$ ($1 \leq T \leq 10^5$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq m \leq 5 \cdot 10^6$) — the lengths of $s$ and the length of $t$.
The second line of each test case contains a single string $s$ of length $n$, consisting only of lowercase English letters.
The third line of each test case contains a single string $t$ of length $m$, consisting only of lowercase English letters.
It is guaranteed that the sum of $m$ over all test cases does not exceed $10^7$.
### Output
For each test case, output a single binary string $r$ of length $n - 1$: for each $1 \leq i < n$, if the $i$-th pair is beautiful, $r_i=\texttt{1}$; otherwise, $r_i=\texttt{0}$. Do not output spaces.
### Example
#### Input #1
```
7
3 5
aba
ababa
4 10
czzz
czzzzzczzz
5 14
dream
dredreamamamam
5 18
tcccc
tcctccccctccctcccc
7 11
abababc
abababababc
7 26
aaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaa
19 29
bbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbb
```
#### Output #1
```
11
011
0010
0000
010100
111111
110010001100010011
```
### Note
In the first test case, $s = \tt aba$, $t = \tt ababa$.
- For $i = 1$: Cocoly can split $t = \texttt{a} + \texttt{ba} + \texttt{ba}$, so the string pair $(\texttt{a}, \texttt{ba})$ is beautiful.
- For $i = 2$: Cocoly can split $t = \texttt{ab} + \texttt{ab} + \texttt{a}$, so the string pair $(\texttt{ab}, \texttt{a})$ is beautiful.
In the second test case, $s = \tt czzz$, $t = \tt czzzzzczzz$.
- For $i = 1$: It can be proven that there is no solution to give a partition of $t$ using strings $\texttt{c}$ and $\texttt{zzz}$.
- For $i = 2$: Cocoly can split $t$ into $\texttt{cz} + \texttt{zz} + \texttt{zz} + \texttt{cz} + \texttt{zz}$.
- For $i = 3$: Cocoly can split $t$ into $\texttt{czz} + \texttt{z} + \texttt{z} + \texttt{z} + \texttt{czz} + \texttt{z}$. | codeforces | https://codeforces.com/problemset/problem/2053/G |
2048H | H. Kevin and Strange Operation | hard | Kevin is exploring problems related to binary strings in Chinatown. When he was at a loss, a stranger approached him and introduced a peculiar operation:
- Suppose the current binary string is $ t $, with a length of $ \vert t \vert $. Choose an integer $ 1 \leq p \leq \vert t \vert $. For all $ 1 \leq i < p $, simultaneously perform the operation $ t_i = \max(t_i, t_{i+1}) $, and then delete $ t_p $.
For example, suppose the current binary string is 01001, and you choose $ p = 4 $. Perform $ t_i = \max(t_i, t_{i+1}) $ for $t_1$, $t_2$, and $ t_3 $, transforming the string into 11001, then delete $ t_4 $, resulting in 1101.
Kevin finds this strange operation quite interesting. Thus, he wants to ask you: Given a binary string $ s $, how many distinct non-empty binary strings can you obtain through any number of operations (possibly zero)?
Since the answer may be very large, you only need to output the result modulo $998\,244\,353$.
### Input
Each test contains multiple test cases. The first line contains a single integer $t$ ($1\le t \le 10^4$) — the number of test cases.
For each test case, the only line contains a binary string $ s $ ($ 1 \le \lvert s \rvert \le 10^6 $).
It is guaranteed that the sum of $\lvert s \rvert$ over all test cases does not exceed $10^6$.
### Output
For each test case, print a single integer in the only line of the output — the number of distinct non-empty binary strings you can obtain, modulo $998\,244\,353$.
### Example
#### Input #1
```
2
11001
000110111001100
```
#### Output #1
```
9
73
```
### Note
In the first test case, all the binary strings you can obtain are: 11001, 1001, 1101, 001, 101, 111, 01, 11, and 1. There are $ 9 $ in total. | codeforces | https://codeforces.com/problemset/problem/2048/H |
2048I1 | I1. Kevin and Puzzle (Easy Version) | hard | This is the easy version of the problem. The difference between the versions is that in this version, you need to find any one good array. You can hack only if you solved all versions of this problem.
Kevin is visiting the Red Church, and he found a puzzle on the wall.
For an array $ a $, let $ c(l,r) $ indicate how many distinct numbers are among $ a_l, a_{l+1}, \ldots, a_r $. In particular, if $ l > r $, define $ c(l,r) = 0 $.
You are given a string $ s $ of length $ n $ consisting of letters $ \texttt{L} $ and $ \texttt{R} $ only. Let a non-negative array $ a $ be called good, if the following conditions hold for $ 1 \leq i \leq n $:
- if $s_i=\verb!L!$, then $c(1,i-1)=a_i$;
- if $s_i=\verb!R!$, then $c(i+1,n)=a_i$.
If there is a good array $a$, print any of the good arrays. Otherwise, report that no such arrays exists.
### Input
Each test contains multiple test cases. The first line contains a single integer $t$ ($1\le t \le 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\leq n\leq 2\cdot 10^5$) — the length of string $s$.
The second line of each test case contains a string $s$ with a length $n$, containing only English uppercase letters $\verb!L!$ and $\verb!R!$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
### Output
For each test case, if a good array exists, print $n$ non-negative integers: a good array $a$. Otherwise, print a single integer $-1$.
If there are multiple arrays $a$ satisfying the conditions, you can output any of them.
### Example
#### Input #1
```
4
3
LLR
3
RRL
4
RRLR
5
LLRLR
```
#### Output #1
```
0 1 0
2 1 2
-1
0 1 2 3 0```
### Note
In the first test case, the array $[0,1,0]$ satisfies the conditions because:
- When $i=1$, $s_i=\verb!L!$, and $c(1,0)=0$;
- When $i=2$, $s_i=\verb!L!$, and $c(1,1)=1$, since there is only one distinct number in $a_1$;
- When $i=3$, $s_i=\verb!R!$, and $c(4,3)=0$.
In the second test case, another suitable answer is $[1,1,1]$.
In the third test case, it can be proven that there's no array satisfying the conditions. | codeforces | https://codeforces.com/problemset/problem/2048/I1 |
2048I2 | I2. Kevin and Puzzle (Hard Version) | hard | This is the hard version of the problem. The difference between the versions is that in this version, you need to count the number of good arrays. You can hack only if you solved all versions of this problem.
Kevin is visiting the Red Church, and he found a puzzle on the wall.
For an array $ a $, let $ c(l,r) $ indicate how many distinct numbers are among $ a_l, a_{l+1}, \ldots, a_r $. In particular, if $ l > r $, define $ c(l,r) = 0 $.
You are given a string $ s $ of length $ n $ consisting of letters $ \texttt{L} $ and $ \texttt{R} $ only. Let a non-negative array $ a $ be called good, if the following conditions hold for $ 1 \leq i \leq n $:
- if $s_i=\verb!L!$, then $c(1,i-1)=a_i$;
- if $s_i=\verb!R!$, then $c(i+1,n)=a_i$.
You need to count the number of good arrays $a$. Since the answer may be large, you only need to output the answer modulo $998\,244\,353$.
### Input
Each test contains multiple test cases. The first line contains a single integer $t$ ($1\le t \le 10^4$) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\leq n\leq 2\cdot 10^5$) — the length of string $s$.
The second line of each test case contains a string $s$ with a length $n$, containing only English uppercase letters $\verb!L!$ and $\verb!R!$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
### Output
For each test case, output the number of good arrays modulo $998\,244\,353$.
### Example
#### Input #1
```
4
3
LLR
3
RRL
4
RRLR
5
LLRLR
```
#### Output #1
```
1
2
0
1
```
### Note
All arrays satisfying the conditions can be found in the easy version of this problem. | codeforces | https://codeforces.com/problemset/problem/2048/I2 |
2046D | D. For the Emperor! | hard | In Ancient Rome, a plan to defeat the barbarians was developed, but for its implementation, each city must be informed about it.
The northern part of the Roman Empire consists of $n$ cities connected by $m$ one-way roads. Initially, the $i$-th city has $a_i$ messengers, and each messenger can freely move between cities following the existing roads. A messenger can carry a copy of the plan with him and inform the cities he visits, and can make unlimited copies for other messengers in the city he is currently in.
At the start, you will produce some number of plans and deliver them to messengers of your choice. Your goal is to make sure that every city is visited by a messenger with a plan. Find the smallest number of the plans you need to produce originally, so that the messengers will deliver them to every city, or determine that it is impossible to do so at all.
### 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 contains two integers $n$ and $m$ ($2 \le n \le 200$, $1 \le m \le 800$) — the number of cities and roads.
The second line contains $n$ non-negative integers $a_1, a_2, \ldots, a_n$ ($0 \le a_{i} \le n$) — the initial number of messengers in each city.
Each of the following $m$ lines contains two integers $u$ and $v$ ($1 \le u,v \le n, u \ne v$), indicating that there is a one-way road from city $u$ to city $v$. The roads may repeat.
It is guaranteed that the sum of $n$ over all test cases does not exceed $200$. It is guaranteed that the sum of $m$ over all test cases does not exceed $800$.
### Output
Output a single line containing a single integer — the smallest number of messengers you need to give a copy of the plan in the beginning, or $-1$ if it is not possible to inform all cities.
### Example
#### Input #1
```
2
7 6
2 1 0 1 2 3 4
1 2
1 3
2 4
2 5
3 6
3 7
4 4
1 1 1 1
1 2
1 3
2 4
3 4
```
#### Output #1
```
2
2
``` | codeforces | https://codeforces.com/problemset/problem/2046/D |
2046E2 | E2. Cheops and a Contest (Hard Version) | hard | This is the hard version of the problem. The difference between the versions is that in this version, $m$ is arbitrary. You can hack only if you solved all versions of this problem.
There is a problem-solving competition in Ancient Egypt with $n$ participants, numbered from $1$ to $n$. Each participant comes from a certain city; the cities are numbered from $1$ to $m$. There is at least one participant from each city.
The $i$-th participant has strength $a_i$, specialization $s_i$, and wisdom $b_i$, so that $b_i \ge a_i$. Each problem in the competition will have a difficulty $d$ and a unique topic $t$. The $i$-th participant will solve the problem if
- $a_i \ge d$, i.e., their strength is not less than the problem's difficulty, or
- $s_i = t$, and $b_i \ge d$, i.e., their specialization matches the problem's topic, and their wisdom is not less than the problem's difficulty.
Cheops wants to choose the problems in such a way that each participant from city $i$ will solve strictly more problems than each participant from city $j$, for all $i < j$.
Please find a set of at most $5n$ problems, where the topics of all problems are distinct, so that Cheops' will is satisfied, or state that it is impossible.
### 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$, $m$ ($2 \le m \le n \le 3 \cdot {10}^5$) — the number of participants and the number of cities.
The following $n$ lines describe the participants. The $i$-th line contains three integers —$a_i$, $b_i$, $s_i$ ($0 \le a_i, b_i, s_i \le {10}^9$, $a_i \le b_i$) — strength, wisdom, and specialization of the $i$-th participant, respectively.
The next $m$ lines describe the cities. In the $i$-th line, the first number is an integer $k_i$ ($1 \le k_i \le n$) — the number of participants from the $i$-th city. It is followed by $k_i$ integers $q_{i, 1}, q_{i, 2}, \ldots, q_{i, k_i}$ — ($1 \le q_{i, j} \le n$, $1 \le j \le k_i$) — the indices of the participants from this city. It is guaranteed that each participant is mentioned exactly once.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
### Output
For each test case, if there exists a set of problems that satisfies Cheops' conditions, then in the first line output a single integer $p$ ($1 \le p \le 5n$) — the number of problems in your solution.
Then output $p$ lines, each containing two integers $d$ and $t$ ($0 \le d, t \le {10}^9$) — the difficulty and topic of the respective problem. The topics must be distinct.
If there is no set of problems that meets Cheops' wishes, print $-1$ instead.
### Example
#### Input #1
```
2
5 2
5 7 1
6 7 2
3 9 2
5 10 3
4 4 1
2 1 2
3 3 4 5
2 2
1 2 1
1 2 1
1 2
1 1
```
#### Output #1
```
7
6 4
6 5
5 6
5 7
4 8
4 9
7 1
-1
``` | codeforces | https://codeforces.com/problemset/problem/2046/E2 |
2046F2 | F2. Yandex Cuneiform (Hard Version) | hard | This is the hard version of the problem. The difference between the versions is that in this version, there is no restriction on the number of question marks. You can hack only if you solved all versions of this problem.
For a long time, no one could decipher Sumerian cuneiform. However, it has finally succumbed to pressure! Today, you have the chance to decipher Yandex cuneiform.
Yandex cuneiform is defined by the following rules:
1. An empty string is a Yandex cuneiform.
2. If you insert exactly one copy of each of the three letters 'Y', 'D', and 'X' into a Yandex cuneiform in such a way that no two adjacent letters become equal after the operation, you obtain a Yandex cuneiform.
3. If a string can't be obtained using the above rules, it is not a Yandex cuneiform.
You are given a template. A template is a string consisting of the characters 'Y', 'D', 'X', and '?'.
You need to check whether there exists a way to replace each question mark with 'Y', 'D', or 'X' to obtain a Yandex cuneiform, and if it exists, output any of the matching options, as well as a sequence of insertion operations to obtain the resulting cuneiform.
In this version of the problem, the number of question marks in the template can be arbitrary.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5 \cdot 10^4$). The description of the test cases follows.
Each test case consists of a single line containing a template of length $n$ ($3 \leq n < 2 \cdot 10^5$, $n \bmod 3 = 0$), consisting only of characters 'Y', 'D', 'X', and '?'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output a single line containing 'NO' if it is not possible to obtain a cuneiform from the given template.
Otherwise, output 'YES' on the first line, and on the second line, any obtainable cuneiform. After that, you need to output the sequence of operations that leads to the cuneiform you printed.
A sequence of operations is described by $\frac{n}{3}$ triples of pairs. A pair has the form c p, where $c$ is one of the letters 'Y', 'D', or 'X', and $p$ is the position at which the letter $c$ should be inserted. The insertion position is the number of letters to skip from the beginning of the string for the insertion. For example, after inserting the character 'D' into the string "YDX" with $p=3$, the result is "YDXD", and with $p=0$, it is "DYDX". Note that the index cannot exceed the current length of the string.
The operations are applied from top to bottom, left to right. After inserting each triple to the string, there should be no two adjacent identical characters.
### Example
#### Input #1
```
4
???
Y??D?X
???
D??DXYXYX
```
#### Output #1
```
YES
YDX
X 0 D 0 Y 0
YES
YDXDYX
X 0 Y 0 D 1
X 2 D 3 Y 4
YES
YDX
Y 0 D 1 X 2
NO
```
### Note
In the second example, the string is transformed like this: $"" \to \mathtt{YDX} \to \mathtt{YDXDYX}$. | codeforces | https://codeforces.com/problemset/problem/2046/F2 |
2046F1 | F1. Yandex Cuneiform (Easy Version) | hard | This is the easy version of the problem. The difference between the versions is that in this version, there are no question marks. You can hack only if you solved all versions of this problem.
For a long time, no one could decipher Sumerian cuneiform. However, it has finally succumbed to pressure! Today, you have the chance to decipher Yandex cuneiform.
Yandex cuneiform is defined by the following rules:
1. An empty string is a Yandex cuneiform.
2. If you insert exactly one copy of each of the three letters 'Y', 'D', and 'X' into a Yandex cuneiform in such a way that no two adjacent letters become equal after the operation, you obtain a Yandex cuneiform.
3. If a string can't be obtained using the above rules, it is not a Yandex cuneiform.
You are given a template. A template is a string consisting of the characters 'Y', 'D', 'X', and '?'.
You need to check whether there exists a way to replace each question mark with 'Y', 'D', or 'X' to obtain a Yandex cuneiform, and if it exists, output any of the matching options, as well as a sequence of insertion operations to obtain the resulting cuneiform.
In this version of the problem, there are no question marks in the template.
### Input
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5 \cdot 10^4$). The description of the test cases follows.
Each test case consists of a single line containing a template of length $n$ ($3 \leq n < 2 \cdot 10^5$, $n \bmod 3 = 0$), consisting only of characters 'Y', 'D', 'X'.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case, output a single line containing 'NO' if it is not possible to obtain a cuneiform from the given template.
Otherwise, output 'YES' on the first line, and on the second line, any obtainable cuneiform. After that, you need to output the sequence of operations that leads to the cuneiform you printed.
A sequence of operations is described by $\frac{n}{3}$ triples of pairs. A pair has the form c p, where $c$ is one of the letters 'Y', 'D', or 'X', and $p$ is the position at which the letter $c$ should be inserted. The insertion position is the number of letters to skip from the beginning of the string for the insertion. For example, after inserting the character 'D' into the string "YDX" with $p=3$, the result is "YDXD", and with $p=0$, it is "DYDX". Note that the index cannot exceed the current length of the string.
The operations are applied from top to bottom, left to right. After inserting each triple to the string, there should be no two adjacent identical characters.
### Example
#### Input #1
```
4
YDX
YDXDYX
YDX
DYYDXYXYX
```
#### Output #1
```
YES
YDX
X 0 D 0 Y 0
YES
YDXDYX
X 0 Y 0 D 1
X 2 D 3 Y 4
YES
YDX
Y 0 D 1 X 2
NO```
### Note
In the second example, the string is transformed like this: $"" \to \mathtt{YDX} \to \mathtt{YDXDYX}$. | codeforces | https://codeforces.com/problemset/problem/2046/F1 |
2034H | H. Rayan vs. Rayaneh | hard | Rayan makes his final efforts to win Reyhaneh's heart by claiming he is stronger than Rayaneh (i.e., computer in Persian). To test this, Reyhaneh asks [Khwarizmi](https://en.wikipedia.org/wiki/Al-Khwarizmi) for help. Khwarizmi explains that a set is integer linearly independent if no element in the set can be written as an integer linear combination of the others. Rayan is given a set of integers each time and must identify one of the largest possible integer linearly independent subsets.
Note that a single element is always considered an integer linearly independent subset.
An integer linearly combination of $a_1, \ldots, a_k$ is any sum of the form $c_1 \cdot a_1 + c_2 \cdot a_2 + \ldots + c_k \cdot a_k$ where $c_1, c_2, \ldots, c_k$ are integers (which may be zero, positive, or negative).
### Input
The first line contains an integer $t$ ($1 \leq t \leq 100$), the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 10^5$), the size of the set. The second line contains $n$ distinct integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^5$).
The sum of $n$ over all test cases does not exceed $3 \cdot 10^6$.
### Output
In the first line of each test case print the size of the largest integer linearly independent subset.
In the next line, print one such subset in any order. If there are multiple valid subsets, print any one of them.
### Example
#### Input #1
```
3
5
2 4 6 8 10
5
12 15 21 30 35
3
2 3 6
```
#### Output #1
```
2
4 6
3
35 21 30
2
2 3
```
### Note
In example 1, $\{4, 6\}$ is an integer linearly independent subset. It can be proven that there is no integer linearly independent subset with at least $3$ elements.
In example 2, $\{35, 21, 30\}$ is an integer linearly independent subset because no integer linear combination of any two elements can create the third. There is no integer linearly independent subset with at least $4$ elements.
In example 3, $\{2, 3, 6\}$ is not an integer linearly independent subset since $6$ can be written as $6 \cdot 2 + (-2) \cdot 3$, which is an integer linear combination of $\{2, 3\}$. | codeforces | https://codeforces.com/problemset/problem/2034/H |
2034G1 | G1. Simurgh's Watch (Easy Version) | hard | The only difference between the two versions of the problem is whether overlaps are considered at all points or only at integer points.
The legendary [Simurgh](https://www.eavartravel.com/blog/2023/11/3/140727/simurgh/), a mythical bird, is responsible for keeping watch over vast lands, and for this purpose, she has enlisted $n$ vigilant warriors. Each warrior is alert during a specific time segment $[l_i, r_i]$, where $l_i$ is the start time (included) and $r_i$ is the end time (included), both positive integers.

One of Simurgh's trusted advisors, [Zal](https://asia-archive.si.edu/learn/shahnama/zal-and-the-simurgh/), is concerned that if multiple warriors are stationed at the same time and all wear the same color, the distinction between them might be lost, causing confusion in the watch. To prevent this, whenever multiple warriors are on guard at the same moment (which can be non-integer), there must be at least one color which is worn by exactly one warrior.
So the task is to determine the minimum number of colors required and assign a color $c_i$ to each warrior's segment $[l_i, r_i]$ such that, for every (real) time $t$ contained in at least one segment, there exists one color which belongs to exactly one segment containing $t$.
### Input
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
For each test case:
- The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of warriors stationed by Simurgh.
- The next $n$ lines each contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq 10^9$) — the start and end times of the warrior's watch segment.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case:
- Output the minimum number of colors $k$ needed.
- Then, output a line of $n$ integers $c_i$ ($1 \leq c_i \leq k$), where each $c_i$ is the color assigned to the $i$-th warrior.
### Example
#### Input #1
```
5
2
1 2
3 4
2
1 2
2 3
3
1 4
2 5
3 6
5
1 4
2 8
3 7
5 10
6 9
5
1 5
2 6
3 7
4 7
6 7
```
#### Output #1
```
1
1 1
2
1 2
2
1 2 1
3
2 3 1 2 1
3
2 1 3 1 1
```
### Note
We can represent each warrior's watch segment as an interval on the X-axis;
In test case 1, we have two independent intervals, which can be colored with the same color.
In test case 2, point 2 is common to two intervals, meaning we cannot color them with the same color.
In test case 3, the intervals can be colored as shown below (intervals are colored with the selected color; areas are colored if this color occurs exactly once at this point in time):

In test case 4, the intervals can be colored as shown below:

In test case 5, the intervals can be colored as shown below. The image on the right demonstrates an example of incorrect coloring for this test case; there is no unique color at the moment $5.5$:
 | codeforces | https://codeforces.com/problemset/problem/2034/G1 |
2034G2 | G2. Simurgh's Watch (Hard Version) | hard | The only difference between the two versions of the problem is whether overlaps are considered at all points or only at integer points.
The legendary [Simurgh](https://www.eavartravel.com/blog/2023/11/3/140727/simurgh/), a mythical bird, is responsible for keeping watch over vast lands, and for this purpose, she has enlisted $n$ vigilant warriors. Each warrior is alert during a specific time segment $[l_i, r_i]$, where $l_i$ is the start time (included) and $r_i$ is the end time (included), both positive integers.

One of Simurgh's trusted advisors, [Zal](https://asia-archive.si.edu/learn/shahnama/zal-and-the-simurgh/), is concerned that if multiple warriors are stationed at the same time and all wear the same color, the distinction between them might be lost, causing confusion in the watch. To prevent this, whenever multiple warriors are on guard at the same integer moment, there must be at least one color which is worn by exactly one warrior.
So the task is to determine the minimum number of colors required and assign a color $c_i$ to each warrior's segment $[l_i, r_i]$ such that, for every (integer) time $t$ contained in at least one segment, there exists one color which belongs to exactly one segment containing $t$.
### Input
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
For each test case:
- The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) — the number of warriors stationed by Simurgh.
- The next $n$ lines each contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq 10^9$) — the start and end times of the warrior's watch segment.
The sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
### Output
For each test case:
- Output the minimum number of colors $k$ needed.
- Then, output a line of $n$ integers $c_i$ ($1 \leq c_i \leq k$), where each $c_i$ is the color assigned to the $i$-th warrior.
### Example
#### Input #1
```
3
5
1 4
2 8
3 7
5 10
6 9
5
1 5
2 6
3 7
4 7
6 7
5
4 9
8 17
2 15
12 19
6 13
```
#### Output #1
```
2
1 2 2 1 2
2
1 2 2 2 1
3
1 1 2 3 1
```
### Note
We can represent each warrior's watch segment as an interval on the X-axis;
In test case 1, the intervals can be colored as shown below (intervals are colored with the selected color; areas are colored if this color occurs exactly once at this point in time):

In test case 2, the intervals can be colored as shown below:

In test case 3, the intervals can be colored as shown below:
 | codeforces | https://codeforces.com/problemset/problem/2034/G2 |
2039F2 | F2. Shohag Loves Counting (Hard Version) | hard | This is the hard version of the problem. The only differences between the two versions of this problem are the constraints on $t$, $m$, and the sum of $m$. You can only make hacks if both versions of the problem are solved.
For an integer array $a$ of length $n$, define $f(k)$ as the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of the maximum values of all subarrays$^{\text{∗}}$ of length $k$. For example, if the array is $[2, 1, 4, 6, 2]$, then $f(3) = \operatorname{gcd}(\operatorname{max}([2, 1, 4]), \operatorname{max}([1, 4, 6]), \operatorname{max}([4, 6, 2])) = \operatorname{gcd}(4, 6, 6) = 2$.
An array is good if $f(i) \neq f(j)$ is satisfied over all pairs $1 \le i \lt j \le n$.
Shohag has an integer $m$. Help him count the number, modulo $998\,244\,353$, of non-empty good arrays of arbitrary length such that each element of the array is an integer from $1$ to $m$.
$^{\text{∗}}$An array $d$ is a subarray of an array $c$ if $d$ can be obtained from $c$ by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
### Input
The first line contains a single integer $t$ ($1 \le t \le 3 \cdot 10^5$) — the number of test cases.
The first and only line of each test case contains an integer $m$ ($1 \le m \le 10^6$).
Note that there is no limit on the sum of $m$ over all test cases.
### Output
For each test case, output an integer — the number of valid arrays modulo $998\,244\,353$.
### Example
#### Input #1
```
3
2
5
9
```
#### Output #1
```
4
29
165
```
### Note
In the first test case, the valid arrays are $[1]$, $[1, 2]$, $[2]$, and $[2, 1]$.
In the second test case, there are a total of $29$ valid arrays. In particular, the array $[2, 1, 4]$ with length $n = 3$ is valid because all elements are from $1$ to $m = 5$ and $f(1)$, $f(2)$ and $f(n = 3)$ all are distinct:
- $f(1) = \operatorname{gcd}(\operatorname{max}([2]), \operatorname{max}([1]), \operatorname{max}([4])) = \operatorname{gcd}(2, 1, 4) = 1.$
- $f(2) = \operatorname{gcd}(\operatorname{max}([2, 1]), \operatorname{max}([1, 4])) = \operatorname{gcd}(2, 4) = 2.$
- $f(3) = \operatorname{gcd}(\operatorname{max}([2, 1, 4])) = \operatorname{gcd}(4) = 4.$ | codeforces | https://codeforces.com/problemset/problem/2039/F2 |
2039H2 | H2. Cool Swap Walk (Hard Version) | hard | This is the hard version of the problem. The only difference is the maximum number of operations you can perform. You can only make hacks if both versions are solved.
You are given an array $a$ of size $n$.
A cool swap walk is the following process:
- In an $n \times n$ grid, we note the cells in row $i$ and column $j$ as $(i, j)$. You need to walk from $(1,1)$ to $(n,n)$, taking only steps to the right or down.
- Formally, if you are in $(x,y)$ currently, you can step to either $(x+1,y)$ or $(x,y+1)$, but you can not step beyond the boundaries of the grid.
- When you step in $(i,j)$, you must swap $a_i$ and $a_j$ when $i \neq j$.
You can perform at most $n+4$ cool swap walks. Sort the array $a_1, a_2, \ldots, a_n$ in non-decreasing order. We can show that it's always possible to do so.
### Input
The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains an integer $n$ ($2 \leq n \leq 500$) — 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 n$) — the elements of the array.
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $2.5 \cdot 10^5$.
### Output
For each test case, your output should consist of several lines:
- The first line contains an integer $k$ ($0 \leq k \leq n+4$), representing the number of cool swap walks you perform.
- Each of the next $k$ lines contains a string $s$ of length $2n-2$ consisting only of R and D, representing the path (letters are case sensitive). For all $1 \le i \le 2n-2$, if $s_i=$ R, you walk right in the $i$-th step, otherwise you walk down in the $i$-th step.
### Example
#### Input #1
```
3
2
1 2
3
2 1 3
4
3 2 3 4
```
#### Output #1
```
0
2
RRDD
DRDR
3
RRDRDD
DRDDRR
DDRRRD
```
### Note
In the first test case, the array $a$ is already non-decreasing, so you don't need to perform any walk.
In the second test case, $a=[2,1,3]$ initially.
In the first walk:
- In the $1$-st step, you step right to $(1,2)$. Then, $a=[1,2,3]$. Note that although the array $a$ is already non-decreasing, you can not stop until you reach $(n,n)$.
- In the $2$-nd step, you step right to $(1,3)$. Then, $a=[3,2,1]$.
- In the $3$-rd step, you step down to $(2,3)$. Then, $a=[3,1,2]$.
- In the $4$-th step, you step down to $(3,3)$. Then, $a=[3,1,2]$.
In the second walk:
- In the $1$-st step, you step down to $(2,1)$. Then, $a=[1,3,2]$.
- In the $2$-nd step, you step right to $(2,2)$. Then, $a=[1,3,2]$.
- In the $3$-rd step, you step down to $(3,2)$. Then, $a=[1,2,3]$.
- In the $4$-th step, you step down to $(3,3)$. Then, $a=[1,2,3]$.
After the two cool swap walks above, we get $a=[1,2,3]$, which is non-decreasing. | codeforces | https://codeforces.com/problemset/problem/2039/H2 |
2039H1 | H1. Cool Swap Walk (Easy Version) | hard | This is the easy version of the problem. The only difference is the maximum number of operations you can perform. You can only make hacks if both versions are solved.
You are given an array $a$ of size $n$.
A cool swap walk is the following process:
- In an $n \times n$ grid, we note the cells in row $i$ and column $j$ as $(i, j)$. You need to walk from $(1,1)$ to $(n,n)$, taking only steps to the right or down.
- Formally, if you are in $(x,y)$ currently, you can step to either $(x+1,y)$ or $(x,y+1)$, but you can not step beyond the boundaries of the grid.
- When you step in $(i,j)$, you must swap $a_i$ and $a_j$ when $i \neq j$.
You can perform at most $2n+4$ cool swap walks. Sort the array $a_1, a_2, \ldots, a_n$ in non-decreasing order. We can show that it's always possible to do so.
### Input
The first line contains an integer $t$ ($1 \le t \le 10^4$) — the number of test cases.
The first line of each test case contains an integer $n$ ($2 \leq n \leq 500$) — 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 n$) — the elements of the array.
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $2.5 \cdot 10^5$.
### Output
For each test case, your output should consist of several lines:
- The first line contains an integer $k$ ($0 \leq k \leq 2n+4$), representing the number of cool swap walks you perform.
- Each of the next $k$ lines contains a string $s$ of length $2n-2$ consisting only of R and D, representing the path (letters are case sensitive). For all $1 \le i \le 2n-2$, if $s_i=$ R, you walk right in the $i$-th step, otherwise you walk down in the $i$-th step.
### Example
#### Input #1
```
3
2
1 2
3
2 1 3
4
3 2 3 4
```
#### Output #1
```
0
2
RRDD
DRDR
3
RRDRDD
DRDDRR
DDRRRD
```
### Note
In the first test case, the array $a$ is already non-decreasing, so you don't need to perform any walk.
In the second test case, $a=[2,1,3]$ initially.
In the first walk:
- In the $1$-st step, you step right to $(1,2)$. Then, $a=[1,2,3]$. Note that although the array $a$ is already non-decreasing, you can not stop until you reach $(n,n)$.
- In the $2$-nd step, you step right to $(1,3)$. Then, $a=[3,2,1]$.
- In the $3$-rd step, you step down to $(2,3)$. Then, $a=[3,1,2]$.
- In the $4$-th step, you step down to $(3,3)$. Then, $a=[3,1,2]$.
In the second walk:
- In the $1$-st step, you step down to $(2,1)$. Then, $a=[1,3,2]$.
- In the $2$-nd step, you step right to $(2,2)$. Then, $a=[1,3,2]$.
- In the $3$-rd step, you step down to $(3,2)$. Then, $a=[1,2,3]$.
- In the $4$-th step, you step down to $(3,3)$. Then, $a=[1,2,3]$.
After the two cool swap walks above, we get $a=[1,2,3]$, which is non-decreasing. | codeforces | https://codeforces.com/problemset/problem/2039/H1 |
2039G | G. Shohag Loves Pebae | hard | Shohag has a tree with $n$ nodes.
Pebae has an integer $m$. She wants to assign each node a value — an integer from $1$ to $m$. So she asks Shohag to count the number, modulo $998\,244\,353$, of assignments such that following conditions are satisfied:
- For each pair $1 \le u \lt v \le n$, the [least common multiple (LCM)](https://en.wikipedia.org/wiki/Least_common_multiple) of the values of the nodes in the unique simple path from $u$ to $v$ is not divisible by the number of nodes in the path.
- The [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of the values of all nodes from $1$ to $n$ is $1$.
But this problem is too hard for Shohag to solve. As Shohag loves Pebae, he has to solve the problem. Please save Shohag!
### Input
The first line contains two space-separated integers $n$ and $m$ ($2 \le n \le 10^6$, $1 \le m \le 10^{9}$).
Each of the next $n - 1$ lines contains two integers $u$ and $v$ ($1 \le u, v \le n$) indicating there is an edge between vertices $u$ and $v$. It is guaranteed that the given edges form a tree.
### Output
Print a single integer — the number of valid ways to assign each vertex a value, modulo $998\,244\,353$.
### Examples
#### Input #1
```
6 6
1 2
2 3
3 4
4 5
3 6
```
#### Output #1
```
2
```
#### Input #2
```
2 5
1 2
```
#### Output #2
```
7
```
#### Input #3
```
12 69
3 5
1 4
2 3
4 5
5 6
8 9
7 3
4 8
9 10
1 11
12 1
```
#### Output #3
```
444144548
```
### Note
In the first test case, the valid assignments are $[1, 1, 1, 1, 1, 1]$ and $[1, 1, 1, 1, 1, 5]$.
In the second test case, the valid assignments are $[1, 1]$, $[1, 3]$, $[1, 5]$, $[3, 1]$, $[3, 5]$, $[5, 1]$ and $[5, 3]$. | codeforces | https://codeforces.com/problemset/problem/2039/G |
2029I | I. Variance Challenge | hard | Kevin has recently learned the definition of variance. For an array $a$ of length $n$, the variance of $a$ is defined as follows:
- Let $x=\dfrac{1}{n}\displaystyle\sum_{i=1}^n a_i$, i.e., $x$ is the mean of the array $a$;
- Then, the variance of $a$ is
$$ V(a)=\frac{1}{n}\sum_{i=1}^n(a_i-x)^2. $$
Now, Kevin gives you an array $a$ consisting of $n$ integers, as well as an integer $k$. You can perform the following operation on $a$:
- Select an interval $[l,r]$ ($1\le l\le r\le n$), then for each $l\le i\le r$, increase $a_i$ by $k$.
For each $1\le p\le m$, you have to find the minimum possible variance of $a$ after exactly $p$ operations are performed, independently for each $p$.
For simplicity, you only need to output the answers multiplied by $n^2$. It can be proven that the results are always integers.
### Input
Each test contains multiple test cases. The first line of the input 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 three integers $n$, $m$, and $k$ ($1\le n,m\le 5000$, $\color{red}{n\cdot m\le 2\cdot 10^4}$, $1\le k\le 10^5$) — the length of the array $a$, the maximum number of operations, and the number you add to $a_i$ each time, respectively.
The second line contains $n$ integers $a_1,a_2,\ldots, a_n$ ($1\le a_i\le 10^5$) — the elements of the array $a$.
It is guaranteed that the sum of $n\cdot m$ over all tests does not exceed $2\cdot 10^4$.
### Output
For each test case, output $m$ integers in a single line, the $p$-th integer denoting the minimum possible variance of $a$ when exactly $p$ operations are performed, multiplied by $n^2$.
### Example
#### Input #1
```
9
3 2 1
1 2 2
3 2 2
1 2 2
10 2 1
10 1 1 1 1 10 1 1 1 1
6 8 2
1 1 4 5 1 3
8 8 7
20 43 24 2 4 3 20 43
8 8 3
20 43 24 2 4 3 20 43
10 12 1
5 3 3 5 4 1 8 1 1 1
13 10 100000
1 2 3 4 5 6 7 8 9 10 11 5 4
10 5 10000
2308 9982 4435 3310 100000 9 7 8100 1919 100000
```
#### Output #1
```
0 0
2 2
1161 1024
53 21 21 5 5 5 5 5
10608 6912 4448 3104 1991 1312 535 304
13248 11184 9375 7815 6447 5319 4383 3687
385 316 269 224 181 156 124 101 80 56 41 29
1486 1486 1486 1486 1486 1486 1486 1486 1486 1486
134618047140 119919447140 107020847140 93922247140 82623647140
```
### Note
In the first test case:
- For $p = 1$, you can perform the operation on $[1, 1]$, changing $a$ from $[1, 2, 2]$ to $[2, 2, 2]$. Since all of the elements are equal, the variance is equal to $0$.
- For $p = 2$, you can perform the operation on $[1, 3]$ and then $[1, 1]$, changing $a$ from $[1, 2, 2]$ to $[2, 3, 3]$ to $[3, 3, 3]$. Since all of the elements are equal, the variance is equal to $0$.
In the second test case, some possible optimal choices are:
- $p=1$: $[\underline{1,}\,2,2]\to [3,2,2]$;
- $p=2$: $[1,\underline{2,2}] \to [\underline{1,}\,4,4] \to [3,4,4]$.
In the third test case, some possible optimal choices are:
- $p=1$: $[10,\underline{1,1,1,1,10,1,1,1,1}]\to[10,2,2,2,2,11,2,2,2,2]$;
- $p=2$: $[10,1,1,1,1,10,\underline{1,1,1,1}] \to [10,\underline{1,1,1,1},10,2,2,2,2] \to [10,2,2,2,2,10,2,2,2,2]$.
In the eighth test case, the optimal choice for all $p$ is to perform the operation on the whole array $p$ times. | codeforces | https://codeforces.com/problemset/problem/2029/I |
2029H | H. Message Spread | hard | Given is an undirected graph with $n$ vertices and $m$ edges. Each edge connects two vertices $(u, v)$ and has a probability of $\frac{p}{q}$ of appearing each day.
Initially, vertex $1$ has a message. At the end of the day, a vertex has a message if and only if itself or at least one of the vertices adjacent to it had the message the day before. Note that each day, each edge chooses its appearance independently.
Calculate the expected number of days before all the vertices have the message, modulo $998\,244\,353$.
### Input
The first line contains two integers $n$ and $m$ ($1\leq n\leq 21$, $n-1\leq m\leq\frac{n(n-1)}{2}$).
Then $m$ lines follow, each containing four integers $u$, $v$, $p$, and $q$ ($1\leq u\neq v\leq n$, $1\leq p<q<998\,244\,353$, $\gcd(p,q)=1$) — there is an undirected edge between $u$ and $v$, and it has a probability of appearance of $\frac{p}{q}$ each day.
It is guaranteed that there are no self-loops or multiple-edges in the graph and that the graph is connected if all of the edges appear.
Additional constraint in the input: Let $g\_{i,j}$ be the probability of appearance of the edge between $i$ and $j$ ($g\_{i,j}=0$ if there is no edge between $i$ and $j$). It is guaranteed that for any $S\\subseteq\\{1,2,\\ldots,n\\}$ ($\|S\|\\ge 1$),
$$ \prod_{i\in S}\left(\prod_{j\in\{1,2,\ldots,n\}\setminus S}(1-g_{i,j})\right)\not\equiv1\pmod{998\,244\,353}. $$
Output
Print a single integer in the only line of the output — the expected number of days, modulo $998\,244\,353$.
Formally, let $M = 998\,244\,353$. It can be shown that the exact 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 < M$ and $x \cdot q \equiv p \pmod{M}$.
### Examples
#### Input #1
```
2 1
1 2 1 10
```
#### Output #1
```
10```
#### Input #2
```
3 3
1 2 1 2
1 3 1 2
2 3 1 2
```
#### Output #2
```
887328316```
#### Input #3
```
1 0
```
#### Output #3
```
0```
#### Input #4
```
5 8
1 2 1 11
1 3 2 11
1 4 3 11
1 5 4 11
2 4 5 11
2 5 6 11
3 4 7 11
4 5 8 11
```
#### Output #4
```
469993557```
#### Input #5
```
21 22
1 2 3 4
2 3 4 5
3 4 5 6
5 6 7 8
6 7 8 9
7 8 9 10
8 9 2 3
9 10 3 4
10 11 4 5
11 12 5 6
12 13 6 7
13 14 7 8
14 15 8 9
15 16 9 10
16 17 2 3
17 18 3 4
18 19 4 5
19 20 5 6
20 21 6 7
1 10 100 1001
15 4 147 220
4 11 1 998244352
```
#### Output #5
```
299529765```
### Note
In the first test, the answer is equal to the expected number of days before the only edge in the graph first appears, and that is $\frac{1}{0.1}=10$.
In the second test, the answer is equal to $\frac{20}{9}$ before it is taken modulo $998\,244\,353$.
In the third test, the only vertex already has the message, so the answer is $0$. | codeforces | https://codeforces.com/problemset/problem/2029/H |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.