problem_id
stringlengths
5
6
url
stringlengths
48
49
title
stringlengths
2
46
rating
int64
800
3.5k
tags
sequencelengths
1
11
div
stringclasses
5 values
time_limit_ms
int64
1k
13k
memory_limit_mb
int64
32
1.02k
description
stringlengths
67
2.35k
input
stringlengths
0
1.93k
output
stringlengths
0
856
interaction
stringclasses
18 values
examples
sequencelengths
1
5
note
stringlengths
0
2.18k
1999B
https://codeforces.com/problemset/problem/1999/B
Card Game
1,000
[ "brute force", "constructive algorithms", "implementation" ]
Div. 4
2,000
256
Suneet and Slavic play a card game. The rules of the game are as follows: * Each card has an integer value between $1$ and $10$. * Each player receives $2$ cards which are face-down (so a player doesn't know their cards). * The game is turn-based and consists exactly of two turns. In a round, both players pick a random unflipped card and flip it. The player who flipped a card with a strictly greater number wins the round. In case of equality, no one wins the round. * A player wins a game if he wins the most number of rounds (i.e. strictly greater than the other player). In case of equality, no one wins the game. Since Suneet and Slavic aren't best friends, you need to calculate the number of ways the game could happen that Suneet would end up as the winner. For a better understanding, please check the notes section.
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first and only line of each test case contains $4$ integers $a_1$, $a_2$, $b_1$, $b_2$ ($1 \leq a_1, a_2, b_1, b_2 \leq 10$) where $a_1$ and $a_2$ represent the cards Suneet has, and $b_1$ and $b_2$ represent the cards Slavic has, respectively.
For each test case, output a single integer β€” the number of games Suneet would win considering all possible games.
[ [ "5\n3 8 2 6\n1 1 1 1\n10 10 2 2\n1 1 10 10\n3 8 7 2", "2\n0\n4\n0\n2" ] ]
Consider the first test case when Slavic starts with the cards that have the values $2$ and $6$, and Suneet starts with cards that have the values $3$ and $8$. The game could happen in $4$ different ways: * Suneet flips $3$ and Slavic flips $2$. Suneet wins the first round. Then, Suneet flips $8$ and Slavic flips $6$. Suneet wins the second round as well. Since Suneet won $2$ rounds, he wins the game. * Suneet flips $3$ and Slavic flips $6$. Slavic wins the first round. Then, Suneet flips $8$ and Slavic flips $2$. Suneet wins the second round. Nobody wins since both players won an equal amount of rounds. * Suneet flips $8$ and Slavic flips $6$. Suneet wins the first round. Then, Suneet flips $3$ and Slavic flips $2$. Suneet wins the second round as well. Since Suneet won $2$ rounds, he wins the game. * Suneet flips $8$ and Slavic flips $2$. Suneet wins the first round. Then, Suneet flips $3$ and Slavic flips $6$. Slavic wins the round. Nobody wins since both players won an equal amount of rounds.
1999C
https://codeforces.com/problemset/problem/1999/C
Showering
800
[ "greedy", "implementation" ]
Div. 4
2,000
256
As a computer science student, Alex faces a hard challenge β€” showering. He tries to shower daily, but despite his best efforts there are always challenges. He takes $s$ minutes to shower and a day only has $m$ minutes! He already has $n$ tasks planned for the day. Task $i$ is represented as an interval $(l_i$, $r_i)$, which means that Alex is busy and can not take a shower in that time interval (at any point in time strictly between $l_i$ and $r_i$). No two tasks overlap. Given all $n$ time intervals, will Alex be able to shower that day? In other words, will Alex have a free time interval of length at least $s$? ![](CDN_BASE_URL/5d5195053b99e5c6936ccefadc239679) In the first test case, Alex can shower for the first $3$ minutes of the day and not miss any of the tasks.
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains three integers $n$, $s$, and $m$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq s, m \leq 10^9$) β€” the number of time intervals Alex already has planned, the amount of time Alex takes to take a shower, and the amount of minutes a day has. Then $n$ lines follow, the $i$-th of which contains two integers $l_i$ and $r_i$ ($0 \leq l_i < r_i \leq m$) β€” the time interval of the $i$-th task. No two tasks overlap. Additional constraint on the input: $l_i > r_{i-1}$ for every $i > 1$. The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case output "YES" (without quotes) if Alex can take a shower for that given test case, and "NO" (also 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).
[ [ "4\n3 3 10\n3 5\n6 8\n9 10\n3 3 10\n1 2\n3 5\n6 7\n3 3 10\n1 2\n3 5\n6 8\n3 4 10\n1 2\n6 7\n8 9", "YES\nYES\nNO\nYES" ] ]
1999D
https://codeforces.com/problemset/problem/1999/D
Slavic's Exam
1,100
[ "greedy", "implementation", "strings" ]
Div. 4
2,000
256
Slavic has a very tough exam and needs your help in order to pass it. Here is the question he is struggling with: There exists a string $s$, which consists of lowercase English letters and possibly zero or more "?". Slavic is asked to change each "?" to a lowercase English letter such that string $t$ becomes a subsequence (not necessarily continuous) of the string $s$. Output any such string, or say that it is impossible in case no string that respects the conditions exists.
The first line contains a single integer $T$ ($1 \leq T \leq 10^4$) β€” the number of test cases. The first line of each test case contains a single string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$, and $s$ consists only of lowercase English letters and "?"-s) – the original string you have. The second line of each test case contains a single string $t$ ($1 \leq |t| \leq |s|$, and $t$ consists only of lowercase English letters) – the string that should be a subsequence of string $s$. The sum of $|s|$ over all test cases doesn't exceed $2 \cdot 10^5$, where $|x|$ denotes the length of the string $x$.
For each test case, if no such string exists as described in the statement, output "NO" (without quotes). Otherwise, output "YES" (without quotes). Then, output one line β€” the string that respects all conditions. 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.
[ [ "5\n?????\nxbx\nab??e\nabcde\nayy?x\na\nab??e\ndac\npaiu\nmom", "YES\nxabax\nYES\nabcde\nYES\nayyyx\nNO\nNO" ] ]
1999E
https://codeforces.com/problemset/problem/1999/E
Triple Operations
1,300
[ "dp", "implementation", "math" ]
Div. 4
1,000
256
On the board Ivy wrote down all integers from $l$ to $r$, inclusive. In an operation, she does the following: * pick two numbers $x$ and $y$ on the board, erase them, and in their place write the numbers $3x$ and $\lfloor \frac{y}{3} \rfloor$. (Here $\lfloor \bullet \rfloor$ denotes rounding down to the nearest integer). What is the minimum number of operations Ivy needs to make all numbers on the board equal $0$? We have a proof that this is always possible.
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The only line of each test case contains two integers $l$ and $r$ ($1 \leq l < r \leq 2 \cdot 10^5$).
For each test case, output a single integer β€” the minimum number of operations needed to make all numbers on the board equal $0$.
[ [ "4\n1 3\n2 4\n199999 200000\n19 84", "5\n6\n36\n263" ] ]
In the first test case, we can perform $5$ operations as follows: $$ 1,2,3 \xrightarrow[x=1,\,y=2]{} 3,0,3 \xrightarrow[x=0,\,y=3]{} 1,0,3 \xrightarrow[x=0,\,y=3]{} 1,0,1 \xrightarrow[x=0,\,y=1]{} 0,0,1 \xrightarrow[x=0,\,y=1]{} 0,0,0 .$$
1999F
https://codeforces.com/problemset/problem/1999/F
Expected Median
1,500
[ "combinatorics", "math" ]
Div. 4
3,000
256
Arul has a binary array$^{\text{βˆ—}}$ $a$ of length $n$. He will take all subsequences$^{\text{†}}$ of length $k$ ($k$ is odd) of this array and find their median.$^{\text{‑}}$ What is the sum of all these values? As this sum can be very large, output it modulo $10^9 + 7$. In other words, print the remainder of this sum when divided by $10^9 + 7$. $^{\text{βˆ—}}$A binary array is an array consisting only of zeros and ones. $^{\text{†}}$An array $b$ is a subsequence of an array $a$ if $b$ can be obtained from $a$ by the deletion of several (possibly, zero or all) elements. Subsequences don't have to be contiguous. $^{\text{‑}}$The median of an array of odd length $k$ is the $\frac{k+1}{2}$-th element when sorted.
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \leq k \leq n \leq 2 \cdot 10^5$, $k$ is odd) β€” the length of the array and the length of the subsequence, respectively. The second line of each test case contains $n$ integers $a_i$ ($0 \leq a_i \leq 1$) β€” the elements of the array. It is guaranteed that sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, print the sum modulo $10^9 + 7$.
[ [ "8\n4 3\n1 0 0 1\n5 1\n1 1 1 1 1\n5 5\n0 1 0 1 0\n6 3\n1 0 1 0 1 1\n4 3\n1 0 1 1\n5 3\n1 0 1 1 0\n2 1\n0 0\n34 17\n1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1", "2\n5\n0\n16\n4\n7\n0\n333606206" ] ]
In the first test case, there are four subsequences of $[1,0,0,1]$ with length $k=3$: * $[1,0,0]$: median $= 0$. * $[1,0,1]$: median $= 1$. * $[1,0,1]$: median $= 1$. * $[0,0,1]$: median $= 0$. The sum of the results is $0+1+1+0=2$. In the second test case, all subsequences of length $1$ have median $1$, so the answer is $5$.
1999G1
https://codeforces.com/problemset/problem/1999/G1
Ruler (easy version)
1,500
[ "binary search", "interactive" ]
Div. 4
1,000
256
This is the easy version of the problem. The only difference between the two versions is that in this version, you can make at most $\mathbf{10}$ queries. This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read [the guide for participants](https://codeforces.com/blog/entry/45307). We have a secret ruler that is missing one number $x$ ($2 \leq x \leq 999$). When you measure an object of length $y$, the ruler reports the following values: * If $y < x$, the ruler (correctly) measures the object as having length $y$. * If $y \geq x$, the ruler incorrectly measures the object as having length $y+1$. ![](CDN_BASE_URL/f2ba8b56cc626dab02991bcad6d908b8) The ruler above is missing the number $4$, so it correctly measures the first segment as length $3$ but incorrectly measures the second segment as length $6$ even though it is actually $5$. You need to find the value of $x$. To do that, you can make queries of the following form: * $\texttt{?}~a~b$ β€” in response, we will measure the side lengths of an $a \times b$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $x=4$ and you query a $3 \times 5$ rectangle, we will measure its side lengths as $3 \times 6$ and report $18$ back to you. Find the value of $x$. You can ask at most $\mathbf{10}$ queries.
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \leq t \leq 1000$) β€” the number of test cases.
There is no initial input for each test case. You should begin the interaction by asking a query. To make a query, output a single line of the form $\texttt{?}~a~b$ ($1 \leq a, b \leq 1000$). In response, you will be told the measured area of the rectangle, according to our secret ruler. When you are ready to print the answer, output a single line of the form $\texttt{!}~x$ ($2 \leq x \leq 999$). After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as a query. The interactor is not adaptive, meaning that the answer is known before the participant asks the queries and doesn't depend on the queries asked by the participant. If your program makes more than $10$ queries for one set of input data, makes an invalid query, or prints the wrong value of $x$, then the response to the query will be $-1$. After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a query do not forget to output the end of line and flush the output. Otherwise, you may get Idleness limit exceeded verdict. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see 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 1000$) β€” the number of test cases. The only line of each test case should contain a single integer $x$ ($2 \leq x \leq 999$) β€” the missing number on the ruler.
[ [ "2\n\n18\n\n25\n\n\n9999", "? 3 5\n\n? 4 4\n\n! 4\n? 99 100\n\n! 100" ] ]
In the first test, the interaction proceeds as follows. Solution| Jury| Explanation ---|---|--- | $\texttt{2}$| There are 2 test cases. $\texttt{? 3 5}$| $\texttt{18}$| Secretly, the jury picked $x=4$. The solution requests the $3 \times 5$ rectangle, and the jury responds with $3 \times 6 = 18$, as described in the statement. $\texttt{? 4 4}$| $\texttt{25}$| The solution requests the $4 \times 4$ rectangle, which the jury measures as $5 \times 5$ and responds with $25$. $\texttt{! 4}$| | The solution has somehow determined that $x=4$, and outputs it. Since the output is correct, the jury continues to the next test case. $\texttt{? 99 100}$| $\texttt{1}$| Secretly, the jury picked $x=100$. The solution requests the $99 \times 100$ rectangle, which the jury measures as $99 \times 101$ and responds with $9999$. $\texttt{! 100}$| | The solution has somehow determined that $x=100$, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.
1999G2
https://codeforces.com/problemset/problem/1999/G2
Ruler (hard version)
1,700
[ "binary search", "interactive", "ternary search" ]
Div. 4
1,000
256
This is the hard version of the problem. The only difference between the two versions is that in this version, you can make at most $\mathbf{7}$ queries. This is an interactive problem. If you are unsure how interactive problems work, then it is recommended to read [the guide for participants](https://codeforces.com/blog/entry/45307). We have a secret ruler that is missing one number $x$ ($2 \leq x \leq 999$). When you measure an object of length $y$, the ruler reports the following values: * If $y < x$, the ruler (correctly) measures the object as having length $y$. * If $y \geq x$, the ruler incorrectly measures the object as having length $y+1$. ![](CDN_BASE_URL/f2ba8b56cc626dab02991bcad6d908b8) The ruler above is missing the number $4$, so it correctly measures the first segment as length $3$ but incorrectly measures the second segment as length $6$ even though it is actually $5$. You need to find the value of $x$. To do that, you can make queries of the following form: * $\texttt{?}~a~b$ β€” in response, we will measure the side lengths of an $a \times b$ rectangle with our ruler and multiply the results, reporting the measured area of the rectangle back to you. For example, if $x=4$ and you query a $3 \times 5$ rectangle, we will measure its side lengths as $3 \times 6$ and report $18$ back to you. Find the value of $x$. You can ask at most $\mathbf{7}$ queries.
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \leq t \leq 1000$) β€” the number of test cases.
There is no initial input for each test case. You should begin the interaction by asking a query. To make a query, output a single line of the form $\texttt{?}~a~b$ ($1 \leq a, b \leq 1000$). In response, you will be told the measured area of the rectangle, according to our secret ruler. When you are ready to print the answer, output a single line of the form $\texttt{!}~x$ ($2 \leq x \leq 999$). After that, proceed to process the next test case or terminate the program if it was the last test case. Printing the answer does not count as a query. The interactor is not adaptive, meaning that the answer is known before the participant asks the queries and doesn't depend on the queries asked by the participant. If your program makes more than $7$ queries for one set of input data, makes an invalid query, or prints the wrong value of $x$, then the response to the query will be $-1$. After receiving such a response, your program should immediately terminate to receive the verdict Wrong Answer. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. After printing a query do not forget to output the end of line and flush the output. Otherwise, you may get Idleness limit exceeded verdict. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see 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 1000$) β€” the number of test cases. The only line of each test case should contain a single integer $x$ ($2 \leq x \leq 999$) β€” the missing number on the ruler.
[ [ "2\n\n18\n\n25\n\n\n9999", "? 3 5\n\n? 4 4\n\n! 4\n? 99 100\n\n! 100" ] ]
In the first test, the interaction proceeds as follows. Solution| Jury| Explanation ---|---|--- | $\texttt{2}$| There are 2 test cases. $\texttt{? 3 5}$| $\texttt{18}$| Secretly, the jury picked $x=4$. The solution requests the $3 \times 5$ rectangle, and the jury responds with $3 \times 6 = 18$, as described in the statement. $\texttt{? 4 4}$| $\texttt{25}$| The solution requests the $4 \times 4$ rectangle, which the jury measures as $5 \times 5$ and responds with $25$. $\texttt{! 4}$| | The solution has somehow determined that $x=4$, and outputs it. Since the output is correct, the jury continues to the next test case. $\texttt{? 99 100}$| $\texttt{1}$| Secretly, the jury picked $x=100$. The solution requests the $99 \times 100$ rectangle, which the jury measures as $99 \times 101$ and responds with $9999$. $\texttt{! 100}$| | The solution has somehow determined that $x=100$, and outputs it. Since the output is correct and there are no more test cases, the jury and the solution exit. Note that the line breaks in the example input and output are for the sake of clarity, and do not occur in the real interaction.
2000A
https://codeforces.com/problemset/problem/2000/A
Primary Task
800
[ "implementation", "math", "strings" ]
Div. 3
1,000
256
Dmitry wrote down $t$ integers on the board, and that is good. He is sure that he lost an important integer $n$ among them, and that is bad. The integer $n$ had the form $\text{10^x}$ ($x \ge 2$), where the symbol '$\text{^}$' denotes exponentiation.. Something went wrong, and Dmitry missed the symbol '$\text{^}$' when writing the important integer. For example, instead of the integer $10^5$, he would have written $105$, and instead of $10^{19}$, he would have written $1019$. Dmitry wants to understand which of the integers on the board could have been the important integer and which could not.
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of integers on the board. The next $t$ lines each contain an integer $a$ ($1 \le a \le 10000$) β€” the next integer from the board.
For each integer on the board, output "YES" if it could have been the important integer and "NO" otherwise. You may output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
[ [ "7\n100\n1010\n101\n105\n2033\n1019\n1002", "NO\nYES\nNO\nYES\nNO\nYES\nNO" ] ]
2000B
https://codeforces.com/problemset/problem/2000/B
Seating in a Bus
800
[ "two pointers" ]
Div. 3
2,000
256
In Berland, a bus consists of a row of $n$ seats numbered from $1$ to $n$. Passengers are advised to always board the bus following these rules: * If there are no occupied seats in the bus, a passenger can sit in any free seat; * Otherwise, a passenger should sit in any free seat that has at least one occupied neighboring seat. In other words, a passenger can sit in a seat with index $i$ ($1 \le i \le n$) only if at least one of the seats with indices $i-1$ or $i+1$ is occupied. Today, $n$ passengers boarded the bus. The array $a$ chronologically records the seat numbers they occupied. That is, $a_1$ contains the seat number where the first passenger sat, $a_2$ β€” the seat number where the second passenger sat, and so on. You know the contents of the array $a$. Determine whether all passengers followed the recommendations. For example, if $n = 5$, and $a$ = [$5, 4, 2, 1, 3$], then the recommendations were not followed, as the $3$-rd passenger sat in seat number $2$, while the neighboring seats with numbers $1$ and $3$ were free.
The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The following describes the input test cases. The first line of each test case contains exactly one integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of seats in the bus and the number of passengers who boarded the bus. The second line of each test case contains $n$ distinct integers $a_i$ ($1 \le a_i \le n$) β€” the seats that the passengers occupied in chronological order. It is guaranteed that the sum of $n$ values across all test cases does not exceed $2 \cdot 10^5$, and that no passenger sits in an already occupied seat.
For each test case, output on a separate line: * "YES", if all passengers followed the recommendations; * "NO" 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).
[ [ "4\n5\n5 4 2 1 3\n3\n2 3 1\n4\n2 3 1 4\n5\n1 2 3 5 4", "NO\nYES\nYES\nNO" ] ]
The first test case is explained in the problem statement.
2000C
https://codeforces.com/problemset/problem/2000/C
Numeric String Template
1,000
[ "data structures", "strings" ]
Div. 3
2,000
256
Kristina has an array $a$, called a template, consisting of $n$ integers. She also has $m$ strings, each consisting only of lowercase Latin letters. The strings are numbered from $1$ to $m$. She wants to check which strings match the template. A string $s$ is considered to match the template if all of the following conditions are simultaneously satisfied: * The length of the string $s$ is equal to the number of elements in the array $a$. * The same numbers from $a$ correspond to the same symbols from $s$. So, if $a_i = a_j$, then $s_i = s_j$ for ($1 \le i, j \le n$). * The same symbols from $s$ correspond to the same numbers from $a$. So, if $s_i = s_j$, then $a_i = a_j$ for ($1 \le i, j \le n$). In other words, there must be a one-to-one correspondence between the characters of the string and the elements of the array. For example, if $a$ = [$3, 5, 2, 1, 3$], then the string "abfda" matches the template, while the string "afbfa" does not, since the character "f" corresponds to both numbers $1$ and $5$.
The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The following descriptions are for the test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of elements in the array $a$. The second line of each test case contains exactly $n$ integers $a_i$ ($-10^9 \le a_i \le 10^9$) β€” the elements of the array $a$. The third line of each test case contains a single integer $m$ ($1 \le m \le 2 \cdot 10^5$) β€” the number of strings to check for template matching. Following are $m$ strings, each containing a non-empty string $s_j$ ($1 \le |s_j| \le 2 \cdot 10^5$), consisting of lowercase Latin letters. It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$, and that the sum of the lengths of all strings does not exceed $2 \cdot 10^5$.
For each test case, output $m$ lines. On the $i$-th line ($1 \le i \le m$) output: * "YES", if the string with index $i$ matches the template; * "NO" 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).
[ [ "3\n5\n3 5 2 1 3\n2\nabfda\nafbfa\n2\n1 2\n3\nab\nabc\naa\n4\n5 -3 5 -3\n4\naaaa\nbcbc\naba\ncbcb", "YES\nNO\nYES\nNO\nNO\nNO\nYES\nNO\nYES" ] ]
The first test case is explained in the problem statement.
2000D
https://codeforces.com/problemset/problem/2000/D
Right Left Wrong
1,200
[ "greedy", "implementation", "two pointers" ]
Div. 3
2,000
256
Vlad found a strip of $n$ cells, numbered from left to right from $1$ to $n$. In the $i$-th cell, there is a positive integer $a_i$ and a letter $s_i$, where all $s_i$ are either 'L' or 'R'. Vlad invites you to try to score the maximum possible points by performing any (possibly zero) number of operations. In one operation, you can choose two indices $l$ and $r$ ($1 \le l < r \le n$) such that $s_l$ = 'L' and $s_r$ = 'R' and do the following: * add $a_l + a_{l + 1} + \dots + a_{r - 1} + a_r$ points to the current score; * replace $s_i$ with '.' for all $l \le i \le r$, meaning you can no longer choose these indices. For example, consider the following strip: $3$| $5$| $1$| $4$| $3$| $2$ ---|---|---|---|---|--- L| R| L| L| L| R You can first choose $l = 1$, $r = 2$ and add $3 + 5 = 8$ to your score. $3$| $5$| $1$| $4$| $3$| $2$ ---|---|---|---|---|--- .| .| L| L| L| R Then choose $l = 3$, $r = 6$ and add $1 + 4 + 3 + 2 = 10$ to your score. $3$| $5$| $1$| $4$| $3$| $2$ ---|---|---|---|---|--- .| .| .| .| .| . As a result, it is impossible to perform another operation, and the final score is $18$. What is the maximum score that can be achieved?
The first line 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 length of the strip. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$) β€” the numbers written on the strip. The third line of each test case contains a string $s$ of $n$ characters 'L' and 'R'. It is guaranteed that the sum of the values of $n$ across all test cases does not exceed $2 \cdot 10^5$.
For each test case, output one integer β€” the maximum possible number of points that can be scored.
[ [ "4\n6\n3 5 1 4 3 2\nLRLLLR\n2\n2 8\nLR\n2\n3 9\nRL\n5\n1 2 3 4 5\nLRLRR", "18\n10\n0\n22" ] ]
2000E
https://codeforces.com/problemset/problem/2000/E
Photoshoot for Gorillas
1,400
[ "combinatorics", "data structures", "greedy", "math" ]
Div. 3
2,000
256
You really love gorillas, so you decided to organize a photoshoot for them. Gorillas live in the jungle. The jungle is represented as a grid of $n$ rows and $m$ columns. $w$ gorillas agreed to participate in the photoshoot, and the gorilla with index $i$ ($1 \le i \le w$) has a height of $a_i$. You want to place all the gorillas in the cells of the grid such that there is no more than one gorilla in each cell. The spectacle of the arrangement is equal to the sum of the spectacles of all sub-squares of the grid with a side length of $k$. The spectacle of a sub-square is equal to the sum of the heights of the gorillas in it. From all suitable arrangements, choose the arrangement with the maximum spectacle.
The first line contains an integer $t$ ($1 \le t \le 10^3$) β€” the number of test cases. The descriptions of the test cases follow. The first line contains integers $n$, $m$, $k$ ($1 \le n, m \le 2 \cdot 10^5$, $1 \le n \cdot m \le 2 \cdot 10^5$, $1 \le k \le \min(n, m)$) β€” the dimensions of the grid and the side length of the square. The second line contains an integer $w$ ($1 \le w \le n \cdot m$) β€” the number of gorillas. The third line contains $w$ integers $a_1, a_2, \ldots, a_w$ ($1 \le a_i \le 10^9$) β€” the heights of the gorillas. It is guaranteed that the sum of $n \cdot m$ across all test cases does not exceed $2 \cdot 10^5$. The same guarantee applies to $w$.
For each test case, output a single integer β€” the maximum spectacle of a suitable arrangement.
[ [ "5\n3 4 2\n9\n1 1 1 1 1 1 1 1 1\n2 1 1\n2\n5 7\n20 15 7\n9\n4 1 4 5 6 1 1000000000 898 777\n1984 1 1\n4\n5 4 1499 2004\n9 5 5\n6\n6 7 14 16 16 6", "21\n12\n49000083104\n3512\n319" ] ]
In the first test case of the first input set, the spectacle of the following sub-squares is summed: ![](CDN_BASE_URL/8e995cd395fd9d2272c862709df70705) Yellow color corresponds to the sub-squares, green β€” to the rest of the grid squares. The picture shows the optimal arrangement of the gorillas. The spectacle of the arrangement is $4 + 4 + 3 + 3 + 4 + 3 = 21$.
2000F
https://codeforces.com/problemset/problem/2000/F
Color Rows and Columns
1,900
[ "dp", "greedy", "implementation", "math" ]
Div. 3
3,000
256
You have $n$ rectangles, the $i$-th of which has a width of $a_i$ and a height of $b_i$. You can perform the following operation an unlimited number of times: choose a rectangle and a cell in it, and then color it. Each time you completely color any row or column, you earn $1$ point. Your task is to score at least $k$ points with as few operations as possible. Suppose you have a rectangle with a width of $6$ and a height of $3$. You can score $4$ points by coloring all the cells in any $4$ columns, thus performing $12$ operations.
The first line contains an integer $t$ ($1 \le t \le 100$) β€” the number of test cases. The following are the descriptions of the test cases. The first line of each test case description contains two integers $n$ and $k$ ($1 \le n \le 1000, 1 \le k \le 100$) β€” the number of rectangles in the case and the required number of points. The next $n$ lines contain the descriptions of the rectangles. The $i$-th line contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le 100$) β€” the width and height of the $i$-th rectangle. It is guaranteed that the sum of the values of $n$ across all test cases does not exceed $1000$.
For each test case, output a single integer β€” the minimum number of operations required to score at least $k$ points. If it is impossible to score at least $k$ points, output -1.
[ [ "7\n1 4\n6 3\n1 5\n4 4\n5 10\n1 1\n1 1\n1 1\n1 1\n1 1\n2 100\n1 2\n5 6\n3 11\n2 2\n3 3\n4 4\n3 25\n9 2\n4 3\n8 10\n4 18\n5 4\n8 5\n8 3\n6 2", "12\n14\n5\n-1\n17\n80\n35" ] ]
2000G
https://codeforces.com/problemset/problem/2000/G
Call During the Journey
2,100
[ "binary search", "brute force", "graphs", "greedy", "shortest paths" ]
Div. 3
4,000
256
You live in a city consisting of $n$ intersections and $m$ streets connecting some pairs of intersections. You can travel in either direction on each street. No two streets connect the same pair of intersections, and no street connects an intersection to itself. You can reach any intersection from any other, possibly passing through some other intersections. Every minute, you can board a bus at intersection $u_i$ and travel for $l_{i1}$ minutes to intersection $v_i$. Conversely, you can travel from intersection $v_i$ to intersection $u_i$ in $l_{i1}$ minutes. You can only board and exit the bus at intersections. You can only board the bus at an intersection if you are currently there. You can also walk along each street, which takes $l_{i2} > l_{i1}$ minutes. You can make stops at intersections. You live at intersection number $1$. Today you woke up at time $0$, and you have an important event scheduled at intersection number $n$, which you must reach no later than time $t_0$. You also have a phone call planned that will last from $t_1$ to $t_2$ minutes ($t_1 < t_2 < t_0$). During the phone call, you cannot ride the bus, but you can walk along any streets, make stops, or stay at home. You can exit the bus at minute $t_1$ and board the bus again at minute $t_2$. Since you want to get enough sleep, you became curious β€” how late can you leave home to have time to talk on the phone and still not be late for the event?
The first line contains an integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The following are the descriptions of the test cases. The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 10^5, 1 \le m \le 10^5$) β€” the number of intersections and streets in the city. The second line of each test case contains three integers $t_0$, $t_1$, $t_2$ ($1 \le t_1 < t_2 < t_0 \le 10^9$) β€” the start time of the event, the start time of the phone call, and its end time, respectively. The next $m$ lines of each test case contain descriptions of the streets. The $i$-th line contains four integers $u_i$, $v_i$, $l_{i1}$, $l_{i2}$ ($1 \le u_i, v_i \le n$, $u_i \neq v_i$, $1 \le l_{i1} < l_{i2} \le 10^9$) β€” the numbers of the intersections connected by the $i$-th street, as well as the travel time along the street by bus and on foot. It is guaranteed that no two streets connect the same pair of intersections and that it is possible to reach any intersection from any other. It is guaranteed that the sum of the values of $n$ across all test cases does not exceed $10^5$. It is also guaranteed that the sum of the values of $m$ across all test cases does not exceed $10^5$.
For each test case, output a single integer β€” the latest time you can leave home to have time to talk on the phone and not be late for the event. If you cannot reach the event on time, output -1.
[ [ "7\n5 5\n100 20 80\n1 5 30 100\n1 2 20 50\n2 3 20 50\n3 4 20 50\n4 5 20 50\n2 1\n100 50 60\n1 2 55 110\n4 4\n100 40 60\n1 2 30 100\n2 4 30 100\n1 3 20 50\n3 4 20 50\n3 3\n100 80 90\n1 2 1 10\n2 3 10 50\n1 3 20 21\n3 2\n58 55 57\n2 1 1 3\n2 3 3 4\n2 1\n12 9 10\n2 1 6 10\n5 5\n8 5 6\n2 1 1 8\n2 3 4 8\n4 2 2 4\n5 3 3 4\n4 5 2 6", "0\n-1\n60\n80\n53\n3\n2" ] ]
2000H
https://codeforces.com/problemset/problem/2000/H
Ksyusha and the Loaded Set
2,200
[ "binary search", "brute force", "data structures", "implementation" ]
Div. 3
3,000
512
Ksyusha decided to start a game development company. To stand out among competitors and achieve success, she decided to write her own game engine. The engine must support a set initially consisting of $n$ distinct integers $a_1, a_2, \ldots, a_n$. The set will undergo $m$ operations sequentially. The operations can be of the following types: * Insert element $x$ into the set; * Remove element $x$ from the set; * Report the $k$-load of the set. The $k$-load of the set is defined as the minimum positive integer $d$ such that the integers $d, d + 1, \ldots, d + (k - 1)$ do not appear in this set. For example, the $3$-load of the set $\\{3, 4, 6, 11\\}$ is $7$, since the integers $7, 8, 9$ are absent from the set, and no smaller value fits. Ksyusha is busy with management tasks, so you will have to write the engine. Implement efficient support for the described operations.
The first line contains an integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The following lines describe the test cases. The first line contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the initial size of the set. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_1 < a_2 < \ldots < a_n \le 2 \cdot 10^6$) β€” the initial state of the set. The third line contains an integer $m$ ($1 \le m \le 2 \cdot 10^5$) β€” the number of operations. The next $m$ lines contain the operations. The operations are given in the following format: * + $x$ ($1 \le x \le 2 \cdot 10^6$) β€” insert element $x$ into the set (it is guaranteed that $x$ is not in the set); * - $x$ ($1 \le x \le 2 \cdot 10^6$) β€” remove element $x$ from the set (it is guaranteed that $x$ is in the set); * ? $k$ ($1 \le k \le 2 \cdot 10^6$) β€” output the value of the $k$-load of the set. It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$, and the same holds for $m$.
For each test case, output the answers to the operations of type "?".
[ [ "3\n5\n1 2 5 905 2000000\n15\n- 2\n? 2\n? 1\n- 1\n? 1\n+ 4\n+ 2\n? 2\n+ 6\n- 4\n+ 7\n? 2\n? 3\n? 4\n? 2000000\n5\n3 4 5 6 8\n9\n? 5\n- 5\n? 5\n+ 1\n? 2\n- 6\n- 8\n+ 6\n? 5\n5\n6 7 8 9 10\n10\n? 5\n- 6\n? 4\n- 10\n+ 5\n- 8\n+ 3\n+ 2\n- 3\n+ 10", "2 2 1 6 3 8 8 2000001 \n9 9 9 7 \n1 1" ] ]
2001A
https://codeforces.com/problemset/problem/2001/A
Make All Equal
800
[ "greedy", "implementation" ]
Div. 2
1,000
256
You are given a cyclic array $a_1, a_2, \ldots, a_n$. You can perform the following operation on $a$ at most $n - 1$ times: * Let $m$ be the current size of $a$, you can choose any two adjacent elements where the previous one is no greater than the latter one (In particular, $a_m$ and $a_1$ are adjacent and $a_m$ is the previous one), and delete exactly one of them. In other words, choose an integer $i$ ($1 \leq i \leq m$) where $a_i \leq a_{(i \bmod m) + 1}$ holds, and delete exactly one of $a_i$ or $a_{(i \bmod m) + 1}$ from $a$. Your goal is to find the minimum number of operations needed to make all elements in $a$ equal.
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 a single integer $n$ ($1 \le n \le 100$) β€” 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 n$) β€” the elements of array $a$.
For each test case, output a single line containing an integer: the minimum number of operations needed to make all elements in $a$ equal.
[ [ "7\n1\n1\n3\n1 2 3\n3\n1 2 2\n5\n5 4 3 2 1\n6\n1 1 2 2 3 3\n8\n8 7 6 3 8 7 6 3\n6\n1 1 4 5 1 4", "0\n2\n1\n4\n4\n6\n3" ] ]
In the first test case, there is only one element in $a$, so we can't do any operation. In the second test case, we can perform the following operations to make all elements in $a$ equal: * choose $i = 2$, delete $a_3$, then $a$ would become $[1, 2]$. * choose $i = 1$, delete $a_1$, then $a$ would become $[2]$. It can be proven that we can't make all elements in $a$ equal using fewer than $2$ operations, so the answer is $2$.
2001B
https://codeforces.com/problemset/problem/2001/B
Generate Permutation
800
[ "constructive algorithms" ]
Div. 2
1,500
256
There is an integer sequence $a$ of length $n$, where each element is initially $-1$. Misuki has two typewriters where the first one writes letters from left to right, with a pointer initially pointing to $1$, and another writes letters from right to left with a pointer initially pointing to $n$. Misuki would choose one of the typewriters and use it to perform the following operations until $a$ becomes a permutation of $[1, 2, \ldots, n]$ * write number: write the minimum positive integer that isn't present in the array $a$ to the element $a_i$, $i$ is the position where the pointer points at. Such operation can be performed only when $a_i = -1$. * carriage return: return the pointer to its initial position (i.e. $1$ for the first typewriter, $n$ for the second) * move pointer: move the pointer to the next position, let $i$ be the position the pointer points at before this operation, if Misuki is using the first typewriter, $i := i + 1$ would happen, and $i := i - 1$ otherwise. Such operation can be performed only if after the operation, $1 \le i \le n$ holds. Your task is to construct any permutation $p$ of length $n$, such that the minimum number of carriage return operations needed to make $a = p$ is the same no matter which typewriter Misuki is using.
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 500$) β€” 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 \le 2 \cdot 10^5$) β€” the length of the permutation. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output a line of $n$ integers, representing the permutation $p$ of length $n$ such that the minimum number of carriage return operations needed to make $a = p$ is the same no matter which typewriter Misuki is using, or $-1$ if it is impossible to do so. If there are multiple valid permutations, you can output any of them.
[ [ "3\n1\n2\n3", "1\n-1\n3 1 2" ] ]
In the first testcase, it's possible to make $a = p = [1]$ using $0$ carriage return operations. In the second testcase, it is possible to make $a = p = [1, 2]$ with the minimal number of carriage returns as follows: If Misuki is using the first typewriter: * Write number: write $1$ to $a_1$, $a$ becomes $[1, -1]$ * Move pointer: move the pointer to the next position. (i.e. $2$) * Write number: write $2$ to $a_2$, $a$ becomes $[1, 2]$ If Misuki is using the second typewriter: * Move pointer: move the pointer to the next position. (i.e. $1$) * Write number: write $1$ to $a_1$, $a$ becomes $[1, -1]$ * Carriage return: return the pointer to $2$. * Write number: write $2$ to $a_2$, $a$ becomes $[1, 2]$ It can be proven that the minimum number of carriage returns needed to transform $a$ into $p$ when using the first typewriter is $0$ and it is $1$ when using the second one, so this permutation is not valid. Similarly, $p = [2, 1]$ is also not valid, so there is no solution for $n = 2$. In the third testcase, it is possibile to make $a = p = [3, 1, 2]$ with $1$ carriage return with both the first and the second typewriter. It can be proven that, for both typewriters, it is impossible to write $p$ with $0$ carriage returns. With the first typewriter it is possible to: * Move pointer: move the pointer to the next position. (i.e. $2$) * Write number: write $1$ to $a_2$, $a$ becomes $[-1, 1, -1]$ * Move pointer: move the pointer to the next position. (i.e. $3$) * Write number: write $2$ to $a_3$, $a$ becomes $[-1, 1, 2]$ * Carriage return: return the pointer to $1$. * Write number: write $3$ to $a_1$, $a$ becomes $[3,1,2]$ With the second typewriter it is possible to: * Move pointer: move the pointer to the next position. (i.e. $2$) * Write number: write $1$ to $a_2$, $a$ becomes $[-1, 1, -1]$ * Carriage return: return the pointer to $3$. * Write number: write $2$ to $a_3$, $a$ becomes $[-1, 1, 2]$ * Move pointer: move the pointer to the next position. (i.e. $2$) * Move pointer: move the pointer to the next position. (i.e. $1$) * Write number: write $3$ to $a_1$, $a$ becomes $[3, 1, 2]$
2001C
https://codeforces.com/problemset/problem/2001/C
Guess The Tree
1,500
[ "binary search", "brute force", "dfs and similar", "divide and conquer", "dsu", "greedy", "interactive", "trees" ]
Div. 2
2,000
256
This is an interactive problem. Misuki has chosen a secret tree with $n$ nodes, indexed from $1$ to $n$, and asked you to guess it by using queries of the following type: * "? a b" β€” Misuki will tell you which node $x$ minimizes $|d(a,x) - d(b,x)|$, where $d(x,y)$ is the distance between nodes $x$ and $y$. If more than one such node exists, Misuki will tell you the one which minimizes $d(a,x)$. Find out the structure of Misuki's secret tree using at most $15n$ queries!
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 200$) β€” the number of test cases. Each test case consists of a single line with an integer $n$ ($2 \le n \le 1000$), the number of nodes in the tree. It is guaranteed that the sum of $n$ across all test cases does not exceed $1000$.
The interaction begins by reading the integer $n$. Then you can make up to $15n$ queries. To make a query, output a line in the format "? a b" (without quotes) ($1 \le a,b \le n$). After each query, read an integer β€” the answer to your query. To report the answer, output a line in the format "! $a_1$ $b_1$ $a_2$ $b_2$ ... $a_{n-1}$ $b_{n-1}$" (without quotes), meaning that there is an edge between nodes $a_i$ and $b_i$, for each $1 \le i \le n-1$. You can print the edges in any order. After $15n$ queries have been made, the response to any other query will be $-1$. Once you receive such a response, terminate the program to receive the Wrong Answer verdict. After printing each line, do not forget to output the end of line and flush the output buffer. Otherwise, you will receive the Idleness limit exceeded verdict. To flush, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see the documentation for other languages. Hacks For hacks, use the following format: The first line contains an integer $t$ ($1 \le t \le 200$) β€” the number of test cases. The first line of each test contains an integer $n$ β€” the number of nodes in the hidden tree. Then $n-1$ lines follow. The $i$-th of them contains two integers $a_i$ and $b_i$ ($1 \le a_i, b_i \le n$), meaning that there is an edge between $a_i$ and $b_i$ in the hidden tree. The sum of $n$ over all test cases must not exceed $1000$.
[ [ "1\n4\n1\n1\n3", "? 1 2\n\n? 1 3\n\n? 1 4\n\n! 1 2 1 3 3 4" ] ]
A tree is an undirected acyclic connected graph. A tree with $n$ nodes will always have $n-1$ edges. In the example case, the answer to "? 1 2" is $1$. This means that there is an edge between nodes $1$ and $2$. The answer to "? 1 3" is $1$. This means that there is an edge between nodes $1$ and $3$. The answer to "? 1 4" is $3$. It can be proven that this can only happen if node $3$ is connected to both node $1$ and $4$. The edges of the tree are hence $(1,2)$, $(1,3)$ and $(3,4)$.
2001D
https://codeforces.com/problemset/problem/2001/D
Longest Max Min Subsequence
1,900
[ "brute force", "constructive algorithms", "data structures", "greedy", "implementation" ]
Div. 2
2,000
256
You are given an integer sequence $a_1, a_2, \ldots, a_n$. Let $S$ be the set of all possible non-empty subsequences of $a$ without duplicate elements. Your goal is to find the longest sequence in $S$. If there are multiple of them, find the one that minimizes lexicographical order after multiplying terms at odd positions by $-1$. For example, given $a = [3, 2, 3, 1]$, $S = \\{[1], [2], [3], [2, 1], [2, 3], [3, 1], [3, 2], [2, 3, 1], [3, 2, 1]\\}$. Then $[2, 3, 1]$ and $[3, 2, 1]$ would be the longest, and $[3, 2, 1]$ would be the answer since $[-3, 2, -1]$ is lexicographically smaller than $[-2, 3, -1]$. 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) elements. A sequence $c$ is lexicographically smaller than a sequence $d$ if and only if one of the following holds: * $c$ is a prefix of $d$, but $c \ne d$; * in the first position where $c$ and $d$ differ, the sequence $c$ has a smaller element than the corresponding element in $d$.
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. The first line of each test case contains an integer $n$ ($1 \le n \le 3 \cdot 10^5$) β€” the length of $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) β€” the sequence $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
For each test case, output the answer in the following format: Output an integer $m$ in the first line β€” the length of $b$. Then output $m$ integers $b_1, b_2, \ldots, b_m$ in the second line β€” the sequence $b$.
[ [ "4\n4\n3 2 1 3\n4\n1 1 1 1\n9\n3 2 1 3 2 1 3 2 1\n1\n1", "3\n3 2 1\n1\n1\n3\n3 1 2\n1\n1" ], [ "10\n2\n1 2\n10\n5 2 1 7 9 7 2 5 5 2\n2\n1 2\n10\n2 2 8 7 7 9 8 1 9 6\n9\n9 1 7 5 8 5 6 4 1\n3\n3 3 3\n6\n1 6 4 4 6 5\n6\n3 4 4 5 3 3\n10\n4 1 4 5 4 5 10 1 5 1\n7\n1 2 1 3 2 4 6", "2\n1 2\n5\n5 1 9 7 2\n2\n1 2\n6\n2 7 9 8 1 6\n7\n9 1 7 5 8 6 4\n1\n3\n4\n1 4 6 5\n3\n4 5 3\n4\n5 4 10 1\n5\n2 1 3 4 6" ] ]
In the first example, $S = \\{[1], [2], [3], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2], [2, 1, 3], [3, 2, 1]\\}$. Among them, $[2, 1, 3]$ and $[3, 2, 1]$ are the longest and $[-3, 2, -1]$ is lexicographical smaller than $[-2, 1, -3]$, so $[3, 2, 1]$ is the answer. In the second example, $S = \\{[1]\\}$, so $[1]$ is the answer.
2001E1
https://codeforces.com/problemset/problem/2001/E1
Deterministic Heap (Easy Version)
2,400
[ "combinatorics", "dp", "math", "trees" ]
Div. 2
3,000
512
This is the easy version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $n$ and $t$. You can make hacks only if both versions of the problem are solved. Consider a perfect binary tree with size $2^n - 1$, with nodes numbered from $1$ to $2^n-1$ and rooted at $1$. For each vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), vertex $2v$ is its left child and vertex $2v + 1$ is its right child. Each node $v$ also has a value $a_v$ assigned to it. Define the operation $\mathrm{pop}$ as follows: 1. initialize variable $v$ as $1$; 2. repeat the following process until vertex $v$ is a leaf (i.e. until $2^{n - 1} \le v \le 2^n - 1$); 1. among the children of $v$, choose the one with the larger value on it and denote such vertex as $x$; if the values on them are equal (i.e. $a_{2v} = a_{2v + 1}$), you can choose any of them; 2. assign $a_x$ to $a_v$ (i.e. $a_v := a_x$); 3. assign $x$ to $v$ (i.e. $v := x$); 3. assign $-1$ to $a_v$ (i.e. $a_v := -1$). Then we say the $\mathrm{pop}$ operation is deterministic if there is a unique way to do such operation. In other words, $a_{2v} \neq a_{2v + 1}$ would hold whenever choosing between them. A binary tree is called a max-heap if for every vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), both $a_v \ge a_{2v}$ and $a_v \ge a_{2v + 1}$ hold. A max-heap is deterministic if the $\mathrm{pop}$ operation is deterministic to the heap when we do it for the first time. Initially, $a_v := 0$ for every vertex $v$ ($1 \le v \le 2^n - 1$), and your goal is to count the number of different deterministic max- heaps produced by applying the following operation $\mathrm{add}$ exactly $k$ times: * Choose an integer $v$ ($1 \le v \le 2^n - 1$) and, for every vertex $x$ on the path between $1$ and $v$, add $1$ to $a_x$. Two heaps are considered different if there is a node which has different values in the heaps. Since the answer might be large, print it modulo $p$.
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, k, p$ ($1 \le n, k \le 500$, $10^8 \le p \le 10^9$, $p$ is a prime). It is guaranteed that the sum of $n$ and the sum of $k$ over all test cases does not exceed $500$.
For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation $\mathrm{add}$ exactly $k$ times, modulo $p$.
[ [ "7\n1 13 998244353\n2 1 998244353\n3 2 998244853\n3 3 998244353\n3 4 100000037\n4 2 100000039\n4 3 100000037", "1\n2\n12\n52\n124\n32\n304" ], [ "1\n500 500 100000007", "76297230" ], [ "6\n87 63 100000037\n77 77 100000039\n100 200 998244353\n200 100 998244353\n32 59 998244853\n1 1 998244353", "26831232\n94573603\n37147649\n847564946\n727060898\n1" ] ]
For the first testcase, there is only one way to generate $a$, and such sequence is a deterministic max-heap, so the answer is $1$. For the second testcase, if we choose $v = 1$ and do the operation, we would have $a = [1, 0, 0]$, and since $a_2 = a_3$, we can choose either of them when doing the first $\mathrm{pop}$ operation, so such heap is not a deterministic max-heap. And if we choose $v = 2$, we would have $a = [1, 1, 0]$, during the first $\mathrm{pop}$, the following would happen: * initialize $v$ as $1$ * since $a_{2v} > a_{2v + 1}$, choose $2v$ as $x$, then $x = 2$ * assign $a_x$ to $a_v$, then $a = [1, 1, 0]$ * assign $x$ to $v$, then $v = 2$ * since $v$ is a leaf, assign $-1$ to $a_v$, then $a = [1, -1, 0]$ Since the first $\mathrm{pop}$ operation is deterministic, this is a deterministic max-heap. Also, if we choose $v = 3$, $a$ would be a deterministic max-heap, so the answer is $2$.
2001E2
https://codeforces.com/problemset/problem/2001/E2
Deterministic Heap (Hard Version)
2,900
[ "combinatorics", "dp", "trees" ]
Div. 2
4,000
512
This is the hard version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $n$ and $t$. You can make hacks only if both versions of the problem are solved. Consider a perfect binary tree with size $2^n - 1$, with nodes numbered from $1$ to $2^n-1$ and rooted at $1$. For each vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), vertex $2v$ is its left child and vertex $2v + 1$ is its right child. Each node $v$ also has a value $a_v$ assigned to it. Define the operation $\mathrm{pop}$ as follows: 1. initialize variable $v$ as $1$; 2. repeat the following process until vertex $v$ is a leaf (i.e. until $2^{n - 1} \le v \le 2^n - 1$); 1. among the children of $v$, choose the one with the larger value on it and denote such vertex as $x$; if the values on them are equal (i.e. $a_{2v} = a_{2v + 1}$), you can choose any of them; 2. assign $a_x$ to $a_v$ (i.e. $a_v := a_x$); 3. assign $x$ to $v$ (i.e. $v := x$); 3. assign $-1$ to $a_v$ (i.e. $a_v := -1$). Then we say the $\mathrm{pop}$ operation is deterministic if there is a unique way to do such operation. In other words, $a_{2v} \neq a_{2v + 1}$ would hold whenever choosing between them. A binary tree is called a max-heap if for every vertex $v$ ($1 \le v \le 2^{n - 1} - 1$), both $a_v \ge a_{2v}$ and $a_v \ge a_{2v + 1}$ hold. A max-heap is deterministic if the $\mathrm{pop}$ operation is deterministic to the heap when we do it for the first and the second time. Initially, $a_v := 0$ for every vertex $v$ ($1 \le v \le 2^n - 1$), and your goal is to count the number of different deterministic max- heaps produced by applying the following operation $\mathrm{add}$ exactly $k$ times: * Choose an integer $v$ ($1 \le v \le 2^n - 1$) and, for every vertex $x$ on the path between $1$ and $v$, add $1$ to $a_x$. Two heaps are considered different if there is a node which has different values in the heaps. Since the answer might be large, print it modulo $p$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 50$). The description of the test cases follows. The first line of each test case contains three integers $n, k, p$ ($2 \le n \le 100$, $1 \le k \le 500$, $10^8 \le p \le 10^9$, $p$ is a prime). It is guaranteed that the sum of $n$ does not exceed $100$ and the sum of $k$ over all test cases does not exceed $500$.
For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation $\mathrm{add}$ exactly $k$ times, modulo $p$.
[ [ "6\n2 1 998244353\n3 2 998244853\n3 3 998244353\n3 4 100000037\n4 2 100000039\n4 3 100000037", "2\n12\n40\n100\n32\n224" ], [ "1\n100 500 100000037", "66681128" ], [ "2\n87 63 100000037\n13 437 100000039", "83566569\n54517140" ] ]
For the first testcase, if we choose $v = 1$ and do the operation, we would have $a = [1, 0, 0]$, and since $a_2 = a_3$, we can choose either of them when doing the first $\mathrm{pop}$ operation, so such heap is not a deterministic max-heap. And if we choose $v = 2$, we would have $a = [1, 1, 0]$, during the first $\mathrm{pop}$, the following would happen: * initialize $v$ as $1$ * since $a_{2v} > a_{2v + 1}$, choose $2v$ as $x$, then $x = 2$ * assign $a_x$ to $a_v$, then $a = [1, 1, 0]$ * assign $x$ to $v$, then $v = 2$ * since $v$ is a leaf, assign $-1$ to $a_v$, then $a = [1, -1, 0]$ And during the second $\mathrm{pop}$, the following would happen: * initialize $v$ as $1$ * since $a_{2v} < a_{2v + 1}$, choose $2v + 1$ as $x$, then $x = 3$ * assign $a_x$ to $a_v$, then $a = [0, -1, 0]$ * assign $x$ to $v$, then $v = 3$ * since $v$ is a leaf, assign $-1$ to $a_v$, then $a = [0, -1, -1]$ Since both the first and the second $\mathrm{pop}$ operation are deterministic, this is a deterministic max-heap. Also, if we choose $v = 3$, $a$ would be a deterministic max-heap, so the answer is $2$.
2002A
https://codeforces.com/problemset/problem/2002/A
Distanced Coloring
800
[ "constructive algorithms", "implementation", "math" ]
Div. 1 + 2
1,000
256
You received an $n\times m$ grid from a mysterious source. The source also gave you a magic positive integer constant $k$. The source told you to color the grid with some colors, satisfying the following condition: * If $(x_1,y_1)$, $(x_2,y_2)$ are two distinct cells with the same color, then $\max(|x_1-x_2|,|y_1-y_2|)\ge k$. You don't like using too many colors. Please find the minimum number of colors needed to color the grid.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le1000$). The description of the test cases follows. The only line of each test case consists of three positive integers $n$, $m$, $k$ ($1\le n,m,k\le10^4$) β€” the dimensions of the grid and the magic constant.
For each test case, print a single integer β€” the minimum number of colors needed to color the grid.
[ [ "6\n3 3 2\n5 1 10000\n7 3 4\n3 2 7\n8 9 6\n2 5 4", "4\n5\n12\n6\n36\n8" ] ]
In the first test case, one of the optimal constructions is: ![](CDN_BASE_URL/a1b4551a26d9369b34abeb1ee2f829ed) In the second test case, the color of all cells must be pairwise different, so the answer is $5$.
2002B
https://codeforces.com/problemset/problem/2002/B
Removals Game
1,000
[ "constructive algorithms", "games" ]
Div. 1 + 2
1,000
256
Alice got a permutation $a_1, a_2, \ldots, a_n$ of $[1,2,\ldots,n]$, and Bob got another permutation $b_1, b_2, \ldots, b_n$ of $[1,2,\ldots,n]$. They are going to play a game with these arrays. In each turn, the following events happen in order: * Alice chooses either the first or the last element of her array and removes it from the array; * Bob chooses either the first or the last element of his array and removes it from the array. The game continues for $n-1$ turns, after which both arrays will have exactly one remaining element: $x$ in the array $a$ and $y$ in the array $b$. If $x=y$, Bob wins; otherwise, Alice wins. Find which player will win if both players play optimally.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1\le n\le 3\cdot 10^5$). The next line contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\le a_i\le n$, all $a_i$ are distinct) β€” the permutation of Alice. The next line contains $n$ integers $b_1,b_2,\ldots,b_n$ ($1\le b_i\le n$, all $b_i$ are distinct) β€” the permutation of Bob. It is guaranteed that the sum of all $n$ does not exceed $3\cdot 10^5$.
For each test case, print a single line with the name of the winner, assuming both players play optimally. If Alice wins, print $\texttt{Alice}$; otherwise, print $\texttt{Bob}$.
[ [ "2\n2\n1 2\n1 2\n3\n1 2 3\n2 3 1", "Bob\nAlice" ] ]
In the first test case, Bob can win the game by deleting the same element as Alice did. In the second test case, Alice can delete $3$ in the first turn, and then in the second turn, delete the element that is different from the one Bob deleted in the first turn to win the game.
2002C
https://codeforces.com/problemset/problem/2002/C
Black Circles
1,200
[ "brute force", "geometry", "greedy", "math" ]
Div. 1 + 2
2,000
256
There are $n$ circles on a two-dimensional plane. The $i$-th circle is centered at $(x_i,y_i)$. Initially, all circles have a radius of $0$. The circles' radii increase at a rate of $1$ unit per second. You are currently at $(x_s,y_s)$; your goal is to reach $(x_t,y_t)$ without touching the circumference of any circle (including the moment you reach $(x_t,y_t)$). You can move in any direction you want. However, your speed is limited to $1$ unit per second. Please determine whether this is possible.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1\le n\le10^5$) β€” the number of circles. The next $n$ lines each contain two integers $x_i$, $y_i$ ($1\le x_i,y_i\le10^9$) β€” the center of each circle. The final line contains four integers $x_s$, $y_s$, $x_t$, $y_t$ ($1\le x_s,y_s,x_t,y_t\le10^9$) β€” the coordinates of the starting point and the goal, respectively. It is guaranteed that these $n+2$ points are distinct. It is guaranteed that the sum of $n$ over all testcases does not exceed $10^5$.
For each test case, output $\texttt{YES}$ if it is possible to reach the goal without touching the circle boundaries, and output $\texttt{NO}$ otherwise. You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt{yes}$, $\texttt{Yes}$, and $\texttt{YES}$ will be recognized as a positive response).
[ [ "7\n3\n2 5\n2 14\n10 13\n4 9 9 7\n3\n10 11\n6 9\n12 12\n14 13 4 8\n1\n5 7\n12 6 11 13\n2\n1000000000 2\n2 1000000000\n1 1 2 2\n1\n999999998 1000000000\n999999999 999999999 1 1\n1\n1000000000 1\n1 1000000000 1 1\n10\n989237121 2397081\n206669655 527238537\n522705783 380636165\n532545346 320061691\n207818728 199485303\n884520552 315781807\n992311437 802563521\n205138355 324818663\n223575704 395073023\n281560523 236279118\n216941610 572010615 323956540 794523071", "YES\nNO\nYES\nYES\nYES\nNO\nYES" ] ]
In the first test case, a feasible way of movement is as follows. ![](CDN_BASE_URL/235d765e20897c623b3ac974eceac134)
2002D1
https://codeforces.com/problemset/problem/2002/D1
DFS Checker (Easy Version)
1,900
[ "brute force", "data structures", "dfs and similar", "graphs", "hashing", "trees" ]
Div. 1 + 2
2,000
512
This is the easy version of the problem. In this version, the given tree is a perfect binary tree and the constraints on $n$ and $q$ are lower. You can make hacks only if both versions of the problem are solved. You are given a perfect binary tree$^\dagger$ consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, and the root is the vertex $1$. You are also given a permutation $p_1, p_2, \ldots, p_n$ of $[1,2,\ldots,n]$. You need to answer $q$ queries. For each query, you are given two integers $x$, $y$; you need to swap $p_x$ and $p_y$ and determine if $p_1, p_2, \ldots, p_n$ is a valid DFS order$^\ddagger$ of the given tree. Please note that the swaps are persistent through queries. $^\dagger$ A perfect binary tree is a tree with vertex $1$ as its root, with size $n=2^k-1$ for a positive integer $k$, and where the parent of each vertex $i$ ($1<i\le n$) is $\left\lfloor\frac{i}{2}\right\rfloor$. Thus, all leaves of this tree are at a distance $k - 1$ from the root. $^\ddagger$ A DFS order is found by calling the following $\texttt{dfs}$ function on the given tree. dfs_order = [] function dfs(v): append v to the back of dfs_order pick an arbitrary permutation s of children of v for child in s: dfs(child) dfs(1) Note that the DFS order is not unique.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains two integers $n$, $q$ ($3\le n\le 65\,535$, $2\le q\le 5 \cdot 10^4$) β€” the number of vertices in the tree and the number of queries. It is guaranteed that $n=2^k-1$ for a positive integer $k$. The next line contains $n-1$ integers $a_2,a_3,\ldots,a_n$ ($1\le a_i<i$) β€” the parent of each vertex in the given tree. It is guaranteed that $a_i=\left\lfloor\frac{i}{2}\right\rfloor$. The next line contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1\le p_i\le n$, all $p_i$ are distinct) β€” the initial permutation $p$. The next $q$ lines each contain two integers $x$, $y$ ($1\le x,y\le n,x\neq y$) β€” the positions of the elements to swap in the permutation. It is guaranteed that the sum of all $n$ does not exceed $65\,535$, and the sum of all $q$ does not exceed $5 \cdot 10^4$.
For each test case, print $q$ lines corresponding to the $q$ queries. For each query, output $\texttt{YES}$ if there is a DFS order that exactly equals the current permutation, and output $\texttt{NO}$ otherwise. You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt{yes}$, $\texttt{Yes}$ and $\texttt{YES}$ will be recognized as a positive response).
[ [ "2\n3 3\n1 1\n1 2 3\n2 3\n3 2\n1 3\n7 4\n1 1 2 2 3 3\n1 2 3 4 5 6 7\n3 5\n2 5\n3 7\n4 6", "YES\nYES\nNO\nYES\nNO\nNO\nYES" ] ]
In the first test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,3,2],[1,2,3],[3,2,1]$, respectively. The first two permutations are valid DFS orders; the third is not a DFS order. In the second test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,2,5,4,3,6,7],[1,3,5,4,2,6,7],[1,3,7,4,2,6,5],[1,3,7,6,2,4,5]$, respectively.
2002D2
https://codeforces.com/problemset/problem/2002/D2
DFS Checker (Hard Version)
2,300
[ "binary search", "data structures", "dfs and similar", "graphs", "hashing", "trees" ]
Div. 1 + 2
2,000
512
This is the hard version of the problem. In this version, you are given a generic tree and the constraints on $n$ and $q$ are higher. You can make hacks only if both versions of the problem are solved. You are given a rooted tree consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, and the root is the vertex $1$. You are also given a permutation $p_1, p_2, \ldots, p_n$ of $[1,2,\ldots,n]$. You need to answer $q$ queries. For each query, you are given two integers $x$, $y$; you need to swap $p_x$ and $p_y$ and determine if $p_1, p_2, \ldots, p_n$ is a valid DFS order$^\dagger$ of the given tree. Please note that the swaps are persistent through queries. $^\dagger$ A DFS order is found by calling the following $\texttt{dfs}$ function on the given tree. dfs_order = [] function dfs(v): append v to the back of dfs_order pick an arbitrary permutation s of children of v for child in s: dfs(child) dfs(1) Note that the DFS order is not unique.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains two integers $n$, $q$ ($2\le n\le 3\cdot 10^5$, $2\le q\le 10^5$) β€” the number of vertices in the tree and the number of queries. The next line contains $n-1$ integers $a_2,a_3,\ldots,a_n$ ($1\le a_i<i$) β€” the parent of each vertex in the given tree. The next line contains $n$ integers $p_1,p_2,\ldots,p_n$ ($1\le p_i\le n$, all $p_i$ are distinct) β€” the initial permutation $p$. The next $q$ lines each contain two integers $x$, $y$ ($1\le x,y\le n,x\neq y$) β€” the positions of the elements to swap in the permutation. It is guaranteed that the sum of all $n$ does not exceed $3\cdot 10^5$, and the sum of all $q$ does not exceed $10^5$.
For each test case, print $q$ lines corresponding to the $q$ queries. For each query, output $\texttt{YES}$ if there is a DFS order that exactly equals the current permutation, and output $\texttt{NO}$ otherwise. You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt{yes}$, $\texttt{Yes}$, and $\texttt{YES}$ will be recognized as a positive response).
[ [ "3\n3 3\n1 1\n1 2 3\n2 3\n3 2\n1 3\n7 4\n1 1 2 2 3 3\n1 2 3 4 5 6 7\n3 5\n2 5\n3 7\n4 6\n5 4\n1 1 3 4\n2 3 4 5 1\n5 1\n4 5\n3 4\n2 3", "YES\nYES\nNO\nYES\nNO\nNO\nYES\nYES\nNO\nNO\nYES" ] ]
In the first test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,3,2],[1,2,3],[3,2,1]$, respectively. The first two permutations are valid DFS orders; the third is not a DFS order. In the second test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,2,5,4,3,6,7],[1,3,5,4,2,6,7],[1,3,7,4,2,6,5],[1,3,7,6,2,4,5]$, respectively.
2002E
https://codeforces.com/problemset/problem/2002/E
Cosmic Rays
2,300
[ "brute force", "data structures", "dp" ]
Div. 1 + 2
2,000
512
Given an array of integers $s_1, s_2, \ldots, s_l$, every second, cosmic rays will cause all $s_i$ such that $i=1$ or $s_i\neq s_{i-1}$ to be deleted simultaneously, and the remaining parts will be concatenated together in order to form the new array $s_1, s_2, \ldots, s_{l'}$. Define the strength of an array as the number of seconds it takes to become empty. You are given an array of integers compressed in the form of $n$ pairs that describe the array left to right. Each pair $(a_i,b_i)$ represents $a_i$ copies of $b_i$, i.e. $\underbrace{b_i,b_i,\cdots,b_i}_{a_i\textrm{ times}}$. For each $i=1,2,\dots,n$, please find the strength of the sequence described by the first $i$ pairs.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1\le n\le3\cdot10^5$) β€” the length of sequence $a$. The next $n$ lines contain two integers each $a_i$, $b_i$ ($1\le a_i\le10^9,0\le b_i\le n$) β€” the pairs which describe the sequence. It is guaranteed that the sum of all $n$ does not exceed $3\cdot10^5$. It is guaranteed that for all $1\le i<n$, $b_i\neq b_{i+1}$ holds.
For each test case, print one line containing $n$ integers β€” the answer for each prefix of pairs.
[ [ "4\n4\n2 0\n1 1\n3 0\n5 1\n6\n4 6\n1 3\n4 6\n4 0\n7 6\n6 3\n7\n9 0\n7 1\n5 0\n7 1\n9 0\n1 1\n2 0\n10\n10 7\n4 9\n2 2\n7 9\n2 8\n8 5\n11 7\n15 5\n12 7\n4 0", "2 2 4 5 \n4 4 7 7 10 10 \n9 9 9 9 9 9 10 \n10 10 10 10 10 10 12 15 15 15" ] ]
In the first test case, for the prefix of length $4$, the changes will be $[0,0,1,0,0,0,1,1,1,1,1]\rightarrow[0,0,0,1,1,1,1]\rightarrow[0,0,1,1,1]\rightarrow[0,1,1]\rightarrow[1]\rightarrow[]$, so the array becomes empty after $5$ seconds. In the second test case, for the prefix of length $4$, the changes will be $[6,6,6,6,3,6,6,6,6,0,0,0,0]\rightarrow[6,6,6,6,6,6,0,0,0]\rightarrow[6,6,6,6,6,0,0]\rightarrow[6,6,6,6,0]\rightarrow[6,6,6]\rightarrow[6,6]\rightarrow[6]\rightarrow[]$, so the array becomes empty after $7$ seconds.
2002F1
https://codeforces.com/problemset/problem/2002/F1
Court Blue (Easy Version)
2,600
[ "brute force", "dfs and similar", "dp", "math", "number theory" ]
Div. 1 + 2
3,000
512
This is the easy version of the problem. In this version, $n=m$ and the time limit is lower. You can make hacks only if both versions of the problem are solved. In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, either Lelle or Flamm wins. Let $W_L$ and $W_F$ denote the number of wins of Lelle and Flamm, respectively. The Blue King considers a match to be successful if and only if: * after every round, $\gcd(W_L,W_F)\le 1$; * at the end of the match, $W_L\le n, W_F\le m$. Note that $\gcd(0,x)=\gcd(x,0)=x$ for every non-negative integer $x$. Lelle and Flamm can decide to stop the match whenever they want, and the final score of the performance is $l \cdot W_L + f \cdot W_F$. Please help Lelle and Flamm coordinate their wins and losses such that the performance is successful, and the total score of the performance is maximized.
The first line contains an integer $t$ ($1\leq t \leq 10^3$) β€” the number of test cases. The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$, $\bf{n=m}$): $n$, $m$ gives the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ determine the final score of the performance. Unusual additional constraint: it is guaranteed that, for each test, there are no pairs of test cases with the same pair of $n$, $m$.
For each test case, output a single integer β€” the maximum total score of a successful performance.
[ [ "8\n3 3 2 5\n4 4 1 4\n6 6 2 2\n7 7 2 3\n9 9 9 1\n2 2 1 4\n5 5 1 4\n8 8 6 7", "19\n17\n18\n33\n86\n9\n24\n86" ], [ "1\n20000000 20000000 1341 331", "33439999007" ], [ "2\n1984 1984 19 84\n9982 9982 44 35", "204143\n788403" ] ]
In the first test case, a possible performance is as follows: * Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Flamm wins, $\gcd(1,2)=1$. * Flamm wins, $\gcd(1,3)=1$. * Lelle wins, $\gcd(2,3)=1$. * Lelle and Flamm agree to stop the match. The final score is $2\cdot2+3\cdot5=19$. In the third test case, a possible performance is as follows: * Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Lelle wins, $\gcd(2,1)=1$. * Lelle wins, $\gcd(3,1)=1$. * Lelle wins, $\gcd(4,1)=1$. * Lelle wins, $\gcd(5,1)=1$. * Flamm wins, $\gcd(5,2)=1$. * Flamm wins, $\gcd(5,3)=1$. * Flamm wins, $\gcd(5,4)=1$. * Lelle and Flamm agree to stop the match. The final score is $5\cdot2+4\cdot2=18$. Note that Lelle and Flamm can stop the match even if neither of them has $n$ wins.
2002F2
https://codeforces.com/problemset/problem/2002/F2
Court Blue (Hard Version)
2,800
[ "brute force", "dp", "math", "number theory" ]
Div. 1 + 2
4,000
512
This is the hard version of the problem. In this version, it is not guaranteed that $n=m$, and the time limit is higher. You can make hacks only if both versions of the problem are solved. In the court of the Blue King, Lelle and Flamm are having a performance match. The match consists of several rounds. In each round, either Lelle or Flamm wins. Let $W_L$ and $W_F$ denote the number of wins of Lelle and Flamm, respectively. The Blue King considers a match to be successful if and only if: * after every round, $\gcd(W_L,W_F)\le 1$; * at the end of the match, $W_L\le n, W_F\le m$. Note that $\gcd(0,x)=\gcd(x,0)=x$ for every non-negative integer $x$. Lelle and Flamm can decide to stop the match whenever they want, and the final score of the performance is $l \cdot W_L + f \cdot W_F$. Please help Lelle and Flamm coordinate their wins and losses such that the performance is successful, and the total score of the performance is maximized.
The first line contains an integer $t$ ($1\leq t \leq 10^3$) β€” the number of test cases. The only line of each test case contains four integers $n$, $m$, $l$, $f$ ($2\leq n\leq m \leq 2\cdot 10^7$, $1\leq l,f \leq 10^9$): $n$, $m$ give the upper bound on the number of Lelle and Flamm's wins, $l$ and $f$ determine the final score of the performance. Unusual additional constraint: it is guaranteed that, for each test, there are no pairs of test cases with the same pair of $n$, $m$.
For each test case, output a single integer β€” the maximum total score of a successful performance.
[ [ "8\n3 4 2 5\n4 4 1 4\n6 6 2 2\n7 9 2 3\n8 9 9 1\n2 7 1 4\n5 9 1 4\n5 6 6 7", "22\n17\n18\n37\n77\n30\n41\n59" ], [ "2\n3082823 20000000 1341 331\n20000000 20000000 3 5", "10754065643\n159999991" ], [ "1\n139 1293 193 412", "559543" ] ]
In the first test case, a possible performance is as follows: * Flamm wins, $\gcd(0,1)=1$. * Lelle wins, $\gcd(1,1)=1$. * Flamm wins, $\gcd(1,2)=1$. * Flamm wins, $\gcd(1,3)=1$. * Flamm wins, $\gcd(1,4)=1$. * Lelle and Flamm agree to stop the match. The final score is $1\cdot2+4\cdot5=22$.
2002G
https://codeforces.com/problemset/problem/2002/G
Lattice Optimizing
3,400
[ "bitmasks", "brute force", "hashing", "meet-in-the-middle" ]
Div. 1 + 2
7,000
1,024
Consider a grid graph with $n$ rows and $n$ columns. Let the cell in row $x$ and column $y$ be $(x,y)$. There exists a directed edge from $(x,y)$ to $(x+1,y)$, with non-negative integer value $d_{x,y}$, for all $1\le x < n, 1\le y \le n$, and there also exists a directed edge from $(x,y)$ to $(x,y+1)$, with non-negative integer value $r_{x,y}$, for all $1\le x \le n, 1\le y < n$. Initially, you are at $(1,1)$, with an empty set $S$. You need to walk along the edges and eventually reach $(n,n)$. Whenever you pass an edge, its value will be inserted into $S$. Please maximize the MEX$^{\text{βˆ—}}$ of $S$ when you reach $(n,n)$. $^{\text{βˆ—}}$The MEX (minimum excluded) of an array is the smallest non- negative integer that does not belong to the array. For instance: * The MEX of $[2,2,1]$ is $0$, because $0$ does not belong to the array. * The MEX of $[3,1,0,1]$ is $2$, because $0$ and $1$ belong to the array, but $2$ does not. * The MEX of $[0,3,1,2]$ is $4$, because $0, 1, 2$, and $3$ belong to the array, but $4$ does not.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le100$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2\le n\le20$) β€” the number of rows and columns. Each of the next $n-1$ lines contains $n$ integers separated by single spaces β€” the matrix $d$ ($0\le d_{x,y}\le 2n-2$). Each of the next $n$ lines contains $n-1$ integers separated by single spaces β€” the matrix $r$ ($0\le r_{x,y}\le 2n-2$). It is guaranteed that the sum of all $n^3$ does not exceed $8000$.
For each test case, print a single integer β€” the maximum MEX of $S$ when you reach $(n,n)$.
[ [ "2\n3\n1 0 2\n0 1 3\n2 1\n0 3\n3 0\n3\n1 2 0\n0 1 2\n2 0\n1 2\n0 1", "3\n2" ], [ "1\n10\n16 7 3 15 9 17 1 15 9 0\n4 3 1 12 13 10 10 14 6 12\n3 1 3 9 5 16 0 12 7 12\n11 4 8 7 13 7 15 13 9 2\n2 3 9 9 4 12 17 7 10 15\n10 6 15 17 13 6 15 9 4 9\n13 3 3 14 1 2 10 10 12 16\n8 2 9 13 18 7 1 6 2 6\n15 12 2 6 0 0 13 3 7 17\n7 3 17 17 10 15 12 14 15\n4 3 3 17 3 13 11 16 6\n16 17 7 7 12 5 2 4 10\n18 9 9 3 5 9 1 16 7\n1 0 4 2 10 10 12 2 1\n4 14 15 16 15 5 8 4 18\n7 18 10 11 2 0 14 8 18\n2 17 6 0 9 6 13 5 11\n5 15 7 11 6 3 17 14 5\n1 3 16 16 13 1 0 13 11", "14" ] ]
In the first test case, the grid graph and one of the optimal paths are as follows: ![](CDN_BASE_URL/70956fd41a5289db10b3b0bb41d0efae) In the second test case, the grid graph and one of the optimal paths are as follows: ![](CDN_BASE_URL/c6a0ac2a80551ddd517e35658fa66438)
2002H
https://codeforces.com/problemset/problem/2002/H
Counting 101
3,500
[ "greedy" ]
Div. 1 + 2
10,100
1,010
It's been a long summer's day, with the constant chirping of cicadas and the heat which never seemed to end. Finally, it has drawn to a close. The showdown has passed, the gates are open, and only a gentle breeze is left behind. Your predecessors had taken their final bow; it's your turn to take the stage. Sorting through some notes that were left behind, you found a curious statement named Problem 101: * Given a positive integer sequence $a_1,a_2,\ldots,a_n$, you can operate on it any number of times. In an operation, you choose three consecutive elements $a_i,a_{i+1},a_{i+2}$, and merge them into one element $\max(a_i+1,a_{i+1},a_{i+2}+1)$. Please calculate the maximum number of operations you can do without creating an element greater than $m$. After some thought, you decided to propose the following problem, named Counting 101: * Given $n$ and $m$. For each $k=0,1,\ldots,\left\lfloor\frac{n-1}{2}\right\rfloor$, please find the number of integer sequences $a_1,a_2,\ldots,a_n$ with elements in $[1, m]$, such that when used as input for Problem 101, the answer is $k$. As the answer can be very large, please print it modulo $10^9+7$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^3$). The description of the test cases follows. The only line of each test case contains two integers $n$, $m$ ($1\le n\le 130$, $1\le m\le 30$).
For each test case, output $\left\lfloor\frac{n+1}{2}\right\rfloor$ numbers. The $i$-th number is the number of valid sequences such that when used as input for Problem 101, the answer is $i-1$, modulo $10^9+7$.
[ [ "2\n3 2\n10 10", "6 2 \n1590121 23399118 382293180 213020758 379696760" ] ]
In the first test case, there are $2^3=8$ candidate sequences. Among them, you can operate on $[1,2,1]$ and $[1,1,1]$ once; you cannot operate on the other $6$ sequences.
2003A
https://codeforces.com/problemset/problem/2003/A
Turtle and Good Strings
800
[ "greedy", "strings" ]
Div. 2
1,000
256
Turtle thinks a string $s$ is a good string if there exists a sequence of strings $t_1, t_2, \ldots, t_k$ ($k$ is an arbitrary integer) such that: * $k \ge 2$. * $s = t_1 + t_2 + \ldots + t_k$, where $+$ represents the concatenation operation. For example, $\texttt{abc} = \texttt{a} + \texttt{bc}$. * For all $1 \le i < j \le k$, the first character of $t_i$ isn't equal to the last character of $t_j$. Turtle is given a string $s$ consisting of lowercase Latin letters. Please tell him whether the string $s$ is a good string!
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 a single integer $n$ ($2 \le n \le 100$) β€” the length of the string. The second line of each test case contains a string $s$ of length $n$, consisting of lowercase Latin letters.
For each test case, output "YES" if the string $s$ is a good string, and "NO" 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.
[ [ "4\n2\naa\n3\naba\n4\nabcb\n12\nabcabcabcabc", "No\nnO\nYes\nYES" ] ]
In the first test case, the sequence of strings $\texttt{a}, \texttt{a}$ satisfies the condition $s = t_1 + t_2 + \ldots + t_k$, but the first character of $t_1$ is equal to the last character of $t_2$. It can be seen that there doesn't exist any sequence of strings which satisfies all of the conditions, so the answer is "NO". In the third test case, the sequence of strings $\texttt{ab}, \texttt{cb}$ satisfies all of the conditions. In the fourth test case, the sequence of strings $\texttt{abca}, \texttt{bcab}, \texttt{cabc}$ satisfies all of the conditions.
2003B
https://codeforces.com/problemset/problem/2003/B
Turtle and Piggy Are Playing a Game 2
800
[ "games", "greedy", "sortings" ]
Div. 2
1,000
256
Turtle and Piggy are playing a game on a sequence. They are given a sequence $a_1, a_2, \ldots, a_n$, and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.). The game goes as follows: * Let the current length of the sequence be $m$. If $m = 1$, the game ends. * If the game does not end and it's Turtle's turn, then Turtle must choose an integer $i$ such that $1 \le i \le m - 1$, set $a_i$ to $\max(a_i, a_{i + 1})$, and remove $a_{i + 1}$. * If the game does not end and it's Piggy's turn, then Piggy must choose an integer $i$ such that $1 \le i \le m - 1$, set $a_i$ to $\min(a_i, a_{i + 1})$, and remove $a_{i + 1}$. Turtle wants to maximize the value of $a_1$ in the end, while Piggy wants to minimize the value of $a_1$ in the end. Find the value of $a_1$ in the end if both players play optimally. You can refer to notes for further clarification.
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 10^5$) β€” the length of the sequence. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$) β€” the elements of the sequence $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
For each test case, output a single integer β€” the value of $a_1$ in the end if both players play optimally.
[ [ "5\n2\n1 2\n3\n1 1 2\n3\n1 2 3\n5\n3 1 2 2 3\n10\n10 2 5 2 7 9 2 5 10 7", "2\n1\n2\n2\n7" ] ]
In the first test case, initially $a = [1, 2]$. Turtle can only choose $i = 1$. Then he will set $a_1$ to $\max(a_1, a_2) = 2$ and remove $a_2$. The sequence $a$ becomes $[2]$. Then the length of the sequence becomes $1$, and the game will end. The value of $a_1$ is $2$, so you should output $2$. In the second test case, one of the possible game processes is as follows: * Initially $a = [1, 1, 2]$. * Turtle can choose $i = 2$. Then he will set $a_2$ to $\max(a_2, a_3) = 2$ and remove $a_3$. The sequence $a$ will become $[1, 2]$. * Piggy can choose $i = 1$. Then he will set $a_1$ to $\min(a_1, a_2) = 1$ and remove $a_2$. The sequence $a$ will become $[1]$. * The length of the sequence becomes $1$, so the game will end. The value of $a_1$ will be $1$ in the end. In the fourth test case, one of the possible game processes is as follows: * Initially $a = [3, 1, 2, 2, 3]$. * Turtle can choose $i = 4$. Then he will set $a_4$ to $\max(a_4, a_5) = 3$ and remove $a_5$. The sequence $a$ will become $[3, 1, 2, 3]$. * Piggy can choose $i = 3$. Then he will set $a_3$ to $\min(a_3, a_4) = 2$ and remove $a_4$. The sequence $a$ will become $[3, 1, 2]$. * Turtle can choose $i = 2$. Then he will set $a_2$ to $\max(a_2, a_3) = 2$ and remove $a_3$. The sequence $a$ will become $[3, 2]$. * Piggy can choose $i = 1$. Then he will set $a_1$ to $\min(a_1, a_2) = 2$ and remove $a_2$. The sequence $a$ will become $[2]$. * The length of the sequence becomes $1$, so the game will end. The value of $a_1$ will be $2$ in the end.
2003C
https://codeforces.com/problemset/problem/2003/C
Turtle and Good Pairs
1,200
[ "constructive algorithms", "greedy", "sortings", "strings" ]
Div. 2
2,000
256
Turtle gives you a string $s$, consisting of lowercase Latin letters. Turtle considers a pair of integers $(i, j)$ ($1 \le i < j \le n$) to be a pleasant pair if and only if there exists an integer $k$ such that $i \le k < j$ and both of the following two conditions hold: * $s_k \ne s_{k + 1}$; * $s_k \ne s_i$ or $s_{k + 1} \ne s_j$. Besides, Turtle considers a pair of integers $(i, j)$ ($1 \le i < j \le n$) to be a good pair if and only if $s_i = s_j$ or $(i, j)$ is a pleasant pair. Turtle wants to reorder the string $s$ so that the number of good pairs is maximized. Please help him!
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 length of the string. The second line of each test case contains a string $s$ of length $n$, consisting of lowercase Latin letters. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output the string $s$ after reordering so that the number of good pairs is maximized. If there are multiple answers, print any of them.
[ [ "5\n3\nabc\n5\nedddf\n6\nturtle\n8\npppppppp\n10\ncodeforces", "acb\nddedf\nurtlet\npppppppp\ncodeforces" ] ]
In the first test case, $(1, 3)$ is a good pair in the reordered string. It can be seen that we can't reorder the string so that the number of good pairs is greater than $1$. bac and cab can also be the answer. In the second test case, $(1, 2)$, $(1, 4)$, $(1, 5)$, $(2, 4)$, $(2, 5)$, $(3, 5)$ are good pairs in the reordered string. efddd can also be the answer.
2003D1
https://codeforces.com/problemset/problem/2003/D1
Turtle and a MEX Problem (Easy Version)
1,500
[ "greedy", "math" ]
Div. 2
2,000
256
The two versions are different problems. In this version of the problem, you can choose the same integer twice or more. You can make hacks only if both versions are solved. One day, Turtle was playing with $n$ sequences. Let the length of the $i$-th sequence be $l_i$. Then the $i$-th sequence was $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$. Piggy gave Turtle a problem to solve when Turtle was playing. The statement of the problem was: * There was a non-negative integer $x$ at first. Turtle would perform an arbitrary number (possibly zero) of operations on the integer. * In each operation, Turtle could choose an integer $i$ such that $1 \le i \le n$, and set $x$ to $\text{mex}^{\dagger}(x, a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i})$. * Turtle was asked to find the answer, which was the maximum value of $x$ after performing an arbitrary number of operations. Turtle solved the above problem without difficulty. He defined $f(k)$ as the answer to the above problem when the initial value of $x$ was $k$. Then Piggy gave Turtle a non-negative integer $m$ and asked Turtle to find the value of $\sum\limits_{i = 0}^m f(i)$ (i.e., the value of $f(0) + f(1) + \ldots + f(m)$). Unfortunately, he couldn't solve this problem. Please help him! $^{\dagger}\text{mex}(c_1, c_2, \ldots, c_k)$ is defined as the smallest non-negative integer $x$ which does not occur in the sequence $c$. For example, $\text{mex}(2, 2, 0, 3)$ is $1$, $\text{mex}(1, 2)$ is $0$.
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$ ($1 \le n \le 2 \cdot 10^5, 0 \le m \le 10^9$). Each of the following $n$ lines contains several integers. The first integer $l_i$ ($1 \le l_i \le 2 \cdot 10^5$) represents the length of the $i$-th sequence, and the following $l_i$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$ ($0 \le a_{i, j} \le 10^9$) represent the elements of the $i$-th sequence. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$, and the sum of $\sum l_i$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output a single integer β€” the value of $\sum\limits_{i = 0}^m f(i)$.
[ [ "6\n3 4\n2 0 2\n3 2 3 3\n4 7 0 1 5\n3 4\n5 0 2 0 4 11\n1 1\n5 1 3 0 3 3\n2 50\n2 1 2\n2 1 2\n1 1\n7 1 2 4 1 4 9 5\n4 114514\n2 2 2\n5 7 3 6 0 3\n3 0 1 1\n5 0 9 2 1 5\n5 1919810\n1 2\n2 324003 0\n3 1416324 2 1460728\n4 1312631 2 0 1415195\n5 1223554 192248 2 1492515 725556", "16\n20\n1281\n6\n6556785365\n1842836177961" ] ]
In the first test case, when $x$ is initially $2$, Turtle can choose $i = 3$ and set $x$ to $\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}) = \text{mex}(2, 7, 0, 1, 5) = 3$. It can be proved that Turtle can't make the value of $x$ greater than $3$, so $f(2) = 3$. It can be seen that $f(0) = 3$, $f(1) = 3$, $f(2) = 3$, $f(3) = 3$, and $f(4) = 4$. So $f(0) + f(1) + f(2) + f(3) + f(4) = 3 + 3 + 3 + 3 + 4 = 16$. In the second test case, when $x$ is initially $1$, Turtle can choose $i = 3$ and set $x$ to $\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}, a_{3, 5}) = \text{mex}(1, 1, 3, 0, 3, 3) = 2$, and choose $i = 3$ and set $x$ to $\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}, a_{3, 5}) = \text{mex}(2, 1, 3, 0, 3, 3) = 4$. It can be proved that Turtle can't make the value of $x$ greater than $4$, so $f(1) = 4$. It can be seen that $f(0) = 4$, $f(1) = 4$, $f(2) = 4$, $f(3) = 4$, and $f(4) = 4$. So $f(0) + f(1) + f(2) + f(3) + f(4) = 4 + 4 + 4 + 4 + 4 = 20$. In the fourth test case, it can be seen that $f(0) = 3$ and $f(1) = 3$. So $f(0) + f(1) = 3 + 3 = 6$.
2003D2
https://codeforces.com/problemset/problem/2003/D2
Turtle and a MEX Problem (Hard Version)
2,100
[ "dfs and similar", "dp", "graphs", "greedy", "implementation", "math" ]
Div. 2
2,000
256
The two versions are different problems. In this version of the problem, you can't choose the same integer twice or more. You can make hacks only if both versions are solved. One day, Turtle was playing with $n$ sequences. Let the length of the $i$-th sequence be $l_i$. Then the $i$-th sequence was $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$. Piggy gave Turtle a problem to solve when Turtle was playing. The statement of the problem was: * There was a non-negative integer $x$ at first. Turtle would perform an arbitrary number (possibly zero) of operations on the integer. * In each operation, Turtle could choose an integer $i$ such that $1 \le i \le n$ and $i$ wasn't chosen before, and set $x$ to $\text{mex}^{\dagger}(x, a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i})$. * Turtle was asked to find the answer, which was the maximum value of $x$ after performing an arbitrary number of operations. Turtle solved the above problem without difficulty. He defined $f(k)$ as the answer to the above problem when the initial value of $x$ was $k$. Then Piggy gave Turtle a non-negative integer $m$ and asked Turtle to find the value of $\sum\limits_{i = 0}^m f(i)$ (i.e., the value of $f(0) + f(1) + \ldots + f(m)$). Unfortunately, he couldn't solve this problem. Please help him! $^{\dagger}\text{mex}(c_1, c_2, \ldots, c_k)$ is defined as the smallest non-negative integer $x$ which does not occur in the sequence $c$. For example, $\text{mex}(2, 2, 0, 3)$ is $1$, $\text{mex}(1, 2)$ is $0$.
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$ ($1 \le n \le 2 \cdot 10^5, 0 \le m \le 10^9$). Each of the following $n$ lines contains several integers. The first integer $l_i$ ($1 \le l_i \le 2 \cdot 10^5$) represents the length of the $i$-th sequence, and the following $l_i$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, l_i}$ ($0 \le a_{i, j} \le 10^9$) represent the elements of the $i$-th sequence. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ and the sum of $\sum l_i$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output a single integer β€” the value of $\sum\limits_{i = 0}^m f(i)$.
[ [ "6\n3 4\n2 0 2\n3 2 3 3\n4 7 0 1 5\n3 4\n5 0 2 0 4 11\n1 1\n5 1 3 0 3 3\n2 50\n2 1 2\n2 1 2\n1 1\n7 1 2 4 1 4 9 5\n4 114514\n2 2 2\n5 7 3 6 0 3\n3 0 1 1\n5 0 9 2 1 5\n5 1919810\n1 2\n2 324003 0\n3 1416324 2 1460728\n4 1312631 2 0 1415195\n5 1223554 192248 2 1492515 725556", "16\n18\n1281\n4\n6556785365\n1842836177961" ] ]
In the first test case, when $x$ is initially $2$, Turtle can choose $i = 3$ and set $x$ to $\text{mex}(x, a_{3, 1}, a_{3, 2}, a_{3, 3}, a_{3, 4}) = \text{mex}(2, 7, 0, 1, 5) = 3$. It can be proved that Turtle can't make the value of $x$ greater than $3$, so $f(2) = 3$. It can be seen that $f(0) = 3$, $f(1) = 3$, $f(2) = 3$, $f(3) = 3$, and $f(4) = 4$. So $f(0) + f(1) + f(2) + f(3) + f(4) = 3 + 3 + 3 + 3 + 4 = 16$. In the second test case, when $x$ is initially $1$, Turtle can choose $i = 1$ and set $x$ to $\text{mex}(x, a_{1, 1}, a_{1, 2}, a_{1, 3}, a_{1, 4}, a_{1, 5}) = \text{mex}(1, 0, 2, 0, 4, 11) = 3$. It can be proved that Turtle can't make the value of $x$ greater than $3$, so $f(1) = 3$. It can be seen that $f(0) = 4$, $f(1) = 3$, $f(2) = 4$, $f(3) = 3$, and $f(4) = 4$. So $f(0) + f(1) + f(2) + f(3) + f(4) = 4 + 3 + 4 + 3 + 4 = 18$. In the fourth test case, it can be seen that $f(0) = 3$ and $f(1) = 1$. So $f(0) + f(1) = 3 + 1 = 4$.
2003E1
https://codeforces.com/problemset/problem/2003/E1
Turtle and Inversions (Easy Version)
2,600
[ "brute force", "divide and conquer", "dp", "greedy", "math" ]
Div. 2
2,000
512
This is an easy version of this problem. The differences between the versions are the constraint on $m$ and $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$ in the easy version. You can make hacks only if both versions of the problem are solved. Turtle gives you $m$ intervals $[l_1, r_1], [l_2, r_2], \ldots, [l_m, r_m]$. He thinks that a permutation $p$ is interesting if there exists an integer $k_i$ for every interval ($l_i \le k_i < r_i$), and if he lets $a_i = \max\limits_{j = l_i}^{k_i} p_j, b_i = \min\limits_{j = k_i + 1}^{r_i} p_j$ for every integer $i$ from $1$ to $m$, the following condition holds: $$\max\limits_{i = 1}^m a_i < \min\limits_{i = 1}^m b_i$$ Turtle wants you to calculate the maximum number of inversions of all interesting permutations of length $n$, or tell him if there is no interesting permutation. An inversion of a permutation $p$ is a pair of integers $(i, j)$ ($1 \le i < j \le n$) such that $p_i > p_j$.
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, m$ ($2 \le n \le 5 \cdot 10^3, 0 \le m \le \frac{n}{2}$) β€” the length of the permutation and the number of intervals. The $i$-th of the following $m$ lines contains two integers $l_i, r_i$ ($1 \le l_i < r_i \le n$) β€” the $i$-th interval. Additional constraint on the input in this version: $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$. It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^3$.
For each test case, if there is no interesting permutation, output a single integer $-1$. Otherwise, output a single integer β€” the maximum number of inversions.
[ [ "6\n2 0\n2 1\n1 2\n5 1\n2 4\n8 2\n1 4\n6 8\n7 2\n1 3\n4 7\n7 3\n1 2\n3 4\n5 6", "1\n0\n8\n21\n15\n15" ] ]
In the third test case, the interesting permutation with the maximum number of inversions is $[5, 2, 4, 3, 1]$. In the fourth test case, the interesting permutation with the maximum number of inversions is $[4, 8, 7, 6, 3, 2, 1, 5]$. In this case, we can let $[k_1, k_2] = [1, 7]$. In the fifth test case, the interesting permutation with the maximum number of inversions is $[4, 7, 6, 3, 2, 1, 5]$. In the sixth test case, the interesting permutation with the maximum number of inversions is $[4, 7, 3, 6, 2, 5, 1]$.
2003E2
https://codeforces.com/problemset/problem/2003/E2
Turtle and Inversions (Hard Version)
2,700
[ "brute force", "data structures", "divide and conquer", "dp", "greedy", "math", "two pointers" ]
Div. 2
2,000
512
This is a hard version of this problem. The differences between the versions are the constraint on $m$ and $r_i < l_{i + 1}$ holds for each $i$ from $1$ to $m - 1$ in the easy version. You can make hacks only if both versions of the problem are solved. Turtle gives you $m$ intervals $[l_1, r_1], [l_2, r_2], \ldots, [l_m, r_m]$. He thinks that a permutation $p$ is interesting if there exists an integer $k_i$ for every interval ($l_i \le k_i < r_i$), and if he lets $a_i = \max\limits_{j = l_i}^{k_i} p_j, b_i = \min\limits_{j = k_i + 1}^{r_i} p_j$ for every integer $i$ from $1$ to $m$, the following condition holds: $$\max\limits_{i = 1}^m a_i < \min\limits_{i = 1}^m b_i$$ Turtle wants you to calculate the maximum number of inversions of all interesting permutations of length $n$, or tell him if there is no interesting permutation. An inversion of a permutation $p$ is a pair of integers $(i, j)$ ($1 \le i < j \le n$) such that $p_i > p_j$.
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, m$ ($2 \le n \le 5 \cdot 10^3, 0 \le m \le 5 \cdot 10^3$) β€” the length of the permutation and the number of intervals. The $i$-th of the following $m$ lines contains two integers $l_i, r_i$ ($1 \le l_i < r_i \le n$) β€” the $i$-th interval. Note that there may exist identical intervals (i.e., there may exist two different indices $i, j$ such that $l_i = l_j$ and $r_i = r_j$). It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^3$ and the sum of $m$ over all test cases does not exceed $5 \cdot 10^3$.
For each test case, if there is no interesting permutation, output a single integer $-1$. Otherwise, output a single integer β€” the maximum number of inversions.
[ [ "8\n2 0\n2 1\n1 2\n5 1\n2 4\n8 3\n1 4\n2 5\n7 8\n7 2\n1 4\n4 7\n7 3\n1 2\n1 7\n3 7\n7 4\n1 3\n4 7\n1 3\n4 7\n7 3\n1 2\n3 4\n5 6", "1\n0\n8\n18\n-1\n-1\n15\n15" ] ]
In the third test case, the interesting permutation with the maximum number of inversions is $[5, 2, 4, 3, 1]$. In the fourth test case, the interesting permutation with the maximum number of inversions is $[4, 3, 8, 7, 6, 2, 1, 5]$. In this case, we can let $[k_1, k_2, k_3] = [2, 2, 7]$. In the fifth and the sixth test case, it can be proven that there is no interesting permutation. In the seventh test case, the interesting permutation with the maximum number of inversions is $[4, 7, 6, 3, 2, 1, 5]$. In this case, we can let $[k_1, k_2, k_3, k_4] = [1, 6, 1, 6]$. In the eighth test case, the interesting permutation with the maximum number of inversions is $[4, 7, 3, 6, 2, 5, 1]$.
2003F
https://codeforces.com/problemset/problem/2003/F
Turtle and Three Sequences
2,800
[ "brute force", "data structures", "dp", "greedy", "math", "probabilities", "two pointers" ]
Div. 2
3,000
256
Piggy gives Turtle three sequences $a_1, a_2, \ldots, a_n$, $b_1, b_2, \ldots, b_n$, and $c_1, c_2, \ldots, c_n$. Turtle will choose a subsequence of $1, 2, \ldots, n$ of length $m$, let it be $p_1, p_2, \ldots, p_m$. The subsequence should satisfy the following conditions: * $a_{p_1} \le a_{p_2} \le \cdots \le a_{p_m}$; * All $b_{p_i}$ for all indices $i$ are pairwise distinct, i.e., there don't exist two different indices $i$, $j$ such that $b_{p_i} = b_{p_j}$. Help him find the maximum value of $\sum\limits_{i = 1}^m c_{p_i}$, or tell him that it is impossible to choose a subsequence of length $m$ that satisfies the conditions above. Recall that 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) elements.
The first line contains two integers $n$ and $m$ ($1 \le n \le 3000$, $1 \le m \le 5$) β€” the lengths of the three sequences and the required length of the subsequence. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) β€” the elements of the sequence $a$. The third line contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le n$) β€” the elements of the sequence $b$. The fourth line contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^4$) β€” the elements of the sequence $c$.
Output a single integer β€” the maximum value of $\sum\limits_{i = 1}^m c_{p_i}$. If it is impossible to choose a subsequence of length $m$ that satisfies the conditions above, output $-1$.
[ [ "4 2\n2 3 4 2\n1 3 3 2\n1 4 2 3", "5" ], [ "7 3\n1 4 5 2 3 6 7\n1 2 2 1 1 3 2\n1 5 6 7 3 2 4", "13" ], [ "5 3\n1 2 3 4 5\n1 1 2 1 2\n5 4 3 2 1", "-1" ] ]
In the first example, we can choose $p = [1, 2]$, then $c_{p_1} + c_{p_2} = 1 + 4 = 5$. We can't choose $p = [2, 4]$ since $a_2 > a_4$, violating the first condition. We can't choose $p = [2, 3]$ either since $b_2 = b_3$, violating the second condition. We can choose $p = [1, 4]$, but $c_1 + c_4 = 4$, which isn't maximum. In the second example, we can choose $p = [4, 6, 7]$. In the third example, it is impossible to choose a subsequence of length $3$ that satisfies both of the conditions.
2004A
https://codeforces.com/problemset/problem/2004/A
Closest Point
800
[ "implementation", "math" ]
Div. 2
2,000
512
Consider a set of points on a line. The distance between two points $i$ and $j$ is $|i - j|$. The point $i$ from the set is the closest to the point $j$ from the set, if there is no other point $k$ in the set such that the distance from $j$ to $k$ is strictly less than the distance from $j$ to $i$. In other words, all other points from the set have distance to $j$ greater or equal to $|i - j|$. For example, consider a set of points $\\{1, 3, 5, 8\\}$: * for the point $1$, the closest point is $3$ (other points have distance greater than $|1-3| = 2$); * for the point $3$, there are two closest points: $1$ and $5$; * for the point $5$, the closest point is $3$ (but not $8$, since its distance is greater than $|3-5|$); * for the point $8$, the closest point is $5$. You are given a set of points. You have to add an integer point into this set in such a way that it is different from every existing point in the set, and it becomes the closest point to every point in the set. Is it possible?
The first line contains one integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. Each test case consists of two lines: * the first line contains one integer $n$ ($2 \le n \le 40$) β€” the number of points in the set; * the second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($1 \le x_1 < x_2 < \dots < x_n \le 100$) β€” the points from the set.
For each test case, print YES if it is possible to add a new point according to the conditions from the statement. Otherwise, print NO.
[ [ "3\n2\n3 8\n2\n5 6\n6\n1 2 3 4 5 10", "YES\nNO\nNO" ] ]
In the first example, the point $7$ will be the closest to both $3$ and $8$. In the second example, it is impossible to add an integer point so that it becomes the closest to both $5$ and $6$, and is different from both of them.
2004B
https://codeforces.com/problemset/problem/2004/B
Game with Doors
1,000
[ "brute force", "greedy" ]
Div. 2
2,000
256
There are $100$ rooms arranged in a row and $99$ doors between them; the $i$-th door connects rooms $i$ and $i+1$. Each door can be either locked or unlocked. Initially, all doors are unlocked. We say that room $x$ is reachable from room $y$ if all doors between them are unlocked. You know that: * Alice is in some room from the segment $[l, r]$; * Bob is in some room from the segment $[L, R]$; * Alice and Bob are in different rooms. However, you don't know the exact rooms they are in. You don't want Alice and Bob to be able to reach each other, so you are going to lock some doors to prevent that. What's the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments?
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 $l$ and $r$ ($1 \le l < r \le 100$) β€” the bounds of the segment of rooms where Alice is located. The second line of each test case contains two integers $L$ and $R$ ($1 \le L < R \le 100$) β€” the bounds of the segment of rooms where Bob is located.
For each test case, print a single integer β€” the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments.
[ [ "4\n1 2\n3 4\n2 5\n2 5\n3 7\n6 7\n4 5\n2 8", "1\n3\n2\n3" ] ]
In the first test case, it is sufficient to lock the door between rooms $2$ and $3$. In the second test case, the following doors have to be locked: $(2,3)$, $(3,4)$, $(4,5)$. In the third test case, the following doors have to be locked: $(5, 6)$ and $(6,7)$.
2004C
https://codeforces.com/problemset/problem/2004/C
Splitting Items
1,100
[ "games", "greedy", "sortings" ]
Div. 2
2,000
256
Alice and Bob have $n$ items they'd like to split between them, so they decided to play a game. All items have a cost, and the $i$-th item costs $a_i$. Players move in turns starting from Alice. In each turn, the player chooses one of the remaining items and takes it. The game goes on until no items are left. Let's say that $A$ is the total cost of items taken by Alice and $B$ is the total cost of Bob's items. The resulting score of the game then will be equal to $A - B$. Alice wants to maximize the score, while Bob wants to minimize it. Both Alice and Bob will play optimally. But the game will take place tomorrow, so today Bob can modify the costs a little. He can increase the costs $a_i$ of several (possibly none or all) items by an integer value (possibly, by the same value or by different values for each item). However, the total increase must be less than or equal to $k$. Otherwise, Alice may suspect something. Note that Bob can't decrease costs, only increase. What is the minimum possible score Bob can achieve?
The first line contains a single integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $0 \le k \le 10^9$) β€” the number of items and the maximum total increase Bob can make. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$) β€” the initial costs of the items. It's guaranteed that the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
For each test case, print a single integer β€” the minimum possible score $A - B$ after Bob increases the costs of several (possibly none or all) items.
[ [ "4\n2 5\n1 10\n3 0\n10 15 12\n4 6\n3 1 2 4\n2 4\n6 9", "4\n13\n0\n0" ] ]
In the first test case, Bob can increase $a_1$ by $5$, making costs equal to $[6, 10]$. Tomorrow, Alice will take $10$ and Bob will take $6$. The total score will be equal to $10 - 6 = 4$, and it's the minimum possible. In the second test case, Bob can't change costs. So the score will be equal to $(15 + 10) - 12 = 13$, since Alice will take $15$, Bob will take $12$, and Alice β€” $10$. In the third test case, Bob, for example, can increase $a_1$ by $1$, $a_2$ by $3$, and $a_3$ by $2$. The total change is equal to $1 + 3 + 2 \le 6$ and costs will be equal to $[4, 4, 4, 4]$. Obviously, the score will be equal to $(4 + 4) - (4 + 4) = 0$. In the fourth test case, Bob can increase $a_1$ by $3$, making costs equal to $[9, 9]$. The score will be equal to $9 - 9 = 0$.
2004D
https://codeforces.com/problemset/problem/2004/D
Colored Portals
1,600
[ "binary search", "brute force", "data structures", "graphs", "greedy", "implementation", "shortest paths" ]
Div. 2
2,000
256
There are $n$ cities located on a straight line. The cities are numbered from $1$ to $n$. Portals are used to move between cities. There are $4$ colors of portals: blue, green, red, and yellow. Each city has portals of two different colors. You can move from city $i$ to city $j$ if they have portals of the same color (for example, you can move between a "blue-red" city and a "blue- green" city). This movement costs $|i-j|$ coins. Your task is to answer $q$ independent queries: calculate the minimum cost to move from city $x$ to city $y$.
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 $q$ ($1 \le n, q \le 2 \cdot 10^5$) β€” the number of cities and the number of queries, respectively. The second line contains $n$ strings of the following types: BG, BR, BY, GR, GY, or RY; the $i$-th of them describes the portals located in the $i$-th city; the symbol B indicates that there is a blue portal in the city, G β€” green, R β€” red, and Y β€” yellow. The $j$-th of the next $q$ lines contains two integers $x_j$ and $y_j$ ($1 \le x_j, y_j \le n$) β€” the description of the $j$-th query. Additional constraints on the input: * the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$; * the sum of $q$ over all test cases does not exceed $2 \cdot 10^5$.
For each query, print a single integer β€” the minimum cost to move from city $x$ to city $y$ (or $-1$ if it is impossible).
[ [ "2\n4 5\nBR BR GY GR\n1 2\n3 1\n4 4\n1 4\n4 2\n2 1\nBG RY\n1 2", "1\n4\n0\n3\n2\n-1" ] ]
2004E
https://codeforces.com/problemset/problem/2004/E
Not a Nim Problem
2,100
[ "brute force", "games", "math", "number theory" ]
Div. 2
2,000
512
Two players, Alice and Bob, are playing a game. They have $n$ piles of stones, with the $i$-th pile initially containing $a_i$ stones. On their turn, a player can choose any pile of stones and take any positive number of stones from it, with one condition: * let the current number of stones in the pile be $x$. It is not allowed to take from the pile a number of stones $y$ such that the greatest common divisor of $x$ and $y$ is not equal to $1$. The player who cannot make a move loses. Both players play optimally (that is, if a player has a strategy that allows them to win, no matter how the opponent responds, they will win). Alice goes first. Determine who will win.
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Each test case consists of two lines: * the first line contains a single integer $n$ ($1 \le n \le 3 \cdot 10^5$); * the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^7$). Additional constraint on the input: the sum of $n$ across all test cases does not exceed $3 \cdot 10^5$.
For each test case, output Alice if Alice wins, or Bob if Bob wins.
[ [ "3\n3\n3 2 9\n4\n3 3 6 1\n5\n1 2 3 4 5", "Bob\nAlice\nBob" ] ]
2004F
https://codeforces.com/problemset/problem/2004/F
Make a Palindrome
2,600
[ "binary search", "brute force", "data structures", "greedy", "math" ]
Div. 2
5,000
512
You are given an array $a$ consisting of $n$ integers. Let the function $f(b)$ return the minimum number of operations needed to make an array $b$ a palindrome. The operations you can make are: * choose two adjacent elements $b_i$ and $b_{i+1}$, remove them, and replace them with a single element equal to $(b_i + b_{i + 1})$; * or choose an element $b_i > 1$, remove it, and replace it with two positive integers $x$ and $y$ ($x > 0$ and $y > 0$) such that $x + y = b_i$. For example, from an array $b=[2, 1, 3]$, you can obtain the following arrays in one operation: $[1, 1, 1, 3]$, $[2, 1, 1, 2]$, $[3, 3]$, $[2, 4]$, or $[2, 1, 2, 1]$. Calculate $\displaystyle \left(\sum_{1 \le l \le r \le n}{f(a[l..r])}\right)$, where $a[l..r]$ is the subarray of $a$ from index $l$ to index $r$, inclusive. In other words, find the sum of the values of the function $f$ for all subarrays of the array $a$.
The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$). The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$). Additional constraint on the input: the sum of $n$ over all test cases does not exceed $2000$.
For each test case, print a single integer β€” the sum of the values of the function $f$ for all subarrays of the array $a$.
[ [ "4\n3\n2 1 3\n4\n1 1 1 1\n5\n4 2 3 1 5\n4\n1 2 1 2", "3\n0\n14\n5" ] ]
2004G
https://codeforces.com/problemset/problem/2004/G
Substring Compression
3,200
[ "data structures", "dp", "matrices" ]
Div. 2
2,000
1,024
Let's define the operation of compressing a string $t$, consisting of at least $2$ digits from $1$ to $9$, as follows: * split it into an even number of non-empty substrings β€” let these substrings be $t_1, t_2, \dots, t_m$ (so, $t = t_1 + t_2 + \dots + t_m$, where $+$ is the concatenation operation); * write the string $t_2$ $t_1$ times, then the string $t_4$ $t_3$ times, and so on. For example, for a string "12345", one could do the following: split it into ("1", "23", "4", "5"), and write "235555". Let the function $f(t)$ for a string $t$ return the minimum length of the string that can be obtained as a result of that process. You are given a string $s$, consisting of $n$ digits from $1$ to $9$, and an integer $k$. Calculate the value of the function $f$ for all contiguous substrings of $s$ of length exactly $k$.
The first line contains two integers $n$ and $k$ ($2 \le k \le n \le 2 \cdot 10^5$). The second line contains the string $s$ ($|s| = n$), consisting only of digits from $1$ to $9$.
Output $n - k + 1$ integers β€” $f(s_{1,k}), f(s_{2,k+1}), \dots, f(s_{n - k + 1, n})$.
[ [ "4 4\n5999", "14" ], [ "10 3\n1111111111", "2 2 2 2 2 2 2 2" ], [ "11 4\n49998641312", "12 18 17 15 12 7 7 2" ] ]
2005A
https://codeforces.com/problemset/problem/2005/A
Simple Palindrome
900
[ "combinatorics", "constructive algorithms", "greedy", "math" ]
Div. 2
1,000
256
Narek has to spend 2 hours with some 2-year-old kids at the kindergarten. He wants to teach them competitive programming, and their first lesson is about palindromes. Narek found out that the kids only know the vowels of the English alphabet (the letters $\mathtt{a}$, $\mathtt{e}$, $\mathtt{i}$, $\mathtt{o}$, and $\mathtt{u}$), so Narek needs to make a string that consists of vowels only. After making the string, he'll ask the kids to count the number of subsequences that are palindromes. Narek wants to keep it simple, so he's looking for a string such that the amount of palindrome subsequences is minimal. Help Narek find a string of length $n$, consisting of lowercase English vowels only (letters $\mathtt{a}$, $\mathtt{e}$, $\mathtt{i}$, $\mathtt{o}$, and $\mathtt{u}$), which minimizes the amount of palindrome$^{\dagger}$ subsequences$^{\ddagger}$ in it. $^{\dagger}$ A string is called a palindrome if it reads the same from left to right and from right to left. $^{\ddagger}$ String $t$ is a subsequence of string $s$ if $t$ can be obtained from $s$ by removing several (possibly, zero or all) characters from $s$ and concatenating the remaining ones, without changing their order. For example, $\mathtt{odocs}$ is a subsequence of $\texttt{c}{\color{red}{\texttt{od}}}\texttt{ef}{\color{red}{\texttt{o}}}\texttt{r}{\color{red}{\texttt{c}}}\texttt{e}{\color{red}{\texttt{s}}}$.
The first line of the input contains a single integer $t$ ($1 \le t \le 100$) β€” the number of test cases. Subsequently, the description of each test case follows. The only line of each test case contains a single integer $n$ ($1 \le n \le 100$) β€” the size of the string.
For each test case, output any string of length $n$ that satisfies the above conditions.
[ [ "3\n2\n3\n6", "uo\niae\noeiiua" ] ]
In the first example, $\texttt{uo}$ has only three palindrome subsequences: $\texttt{u}$, $\texttt{o}$, and the empty string. It can be shown that there is no better answer. In the third example, $\texttt{oeiiua}$ has only eight palindrome subsequences: $\texttt{o}$, $\texttt{e}$, $\texttt{i}$, $\texttt{i}$, $\texttt{u}$, $\texttt{a}$, $\texttt{ii}$, and the empty string. It can be shown that there is no better answer.
2005B1
https://codeforces.com/problemset/problem/2005/B1
The Strict Teacher (Easy Version)
1,000
[ "greedy", "math", "sortings" ]
Div. 2
1,500
256
This is the easy version of the problem. The only differences between the two versions are the constraints on $m$ and $q$. In this version, $m=2$ and $q=1$. You can make hacks only if both versions of the problem are solved. Narek and Tsovak were busy preparing this round, so they have not managed to do their homework and decided to steal David's homework. Their strict teacher noticed that David has no homework and now wants to punish him. She hires other teachers to help her catch David. And now $m$ teachers together are chasing him. Luckily, the classroom is big, so David has many places to hide. The classroom can be represented as a one-dimensional line with cells from $1$ to $n$, inclusive. At the start, all $m$ teachers and David are in distinct cells. Then they make moves. During each move * David goes to an adjacent cell or stays at the current one. * Then, each of the $m$ teachers simultaneously goes to an adjacent cell or stays at the current one. This continues until David is caught. David is caught if any of the teachers (possibly more than one) is located in the same cell as David. Everyone sees others' moves, so they all act optimally. Your task is to find how many moves it will take for the teachers to catch David if they all act optimally. Acting optimally means the student makes his moves in a way that maximizes the number of moves the teachers need to catch him; and the teachers coordinate with each other to make their moves in a way that minimizes the number of moves they need to catch the student. Also, as Narek and Tsovak think this task is easy, they decided to give you $q$ queries on David's position. Note: this is the easy version, and you are given only one query.
In the first line of the input, you are given a single integer $t$ ($1 \le t \le 10^5$) β€” the number of test cases. The description of each test case follows. In the first line of each test case, you are given three integers $n$, $m$, and $q$ ($3 \le n \le 10^9$, $m=2$, $q=1$) β€” the number of cells on the line, the number of teachers, and the number of queries. In the second line of each test case, you are given $m$ distinct integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le n$) β€” the cell numbers of the teachers. In the third line of each test case, you are given $q$ integers $a_1, a_2, \ldots, a_q$ ($1 \le a_i \le n$) β€” David's cell number for every query. It is guaranteed that for any $i$, $j$ such that $1 \le i \le m$ and $1 \le j \le q$, $b_i \neq a_j$.
For each test case, output $q$ lines, the $i$-th of them containing the answer of the $i$-th query.
[ [ "3\n10 2 1\n1 4\n2\n8 2 1\n3 6\n1\n8 2 1\n3 6\n8", "1\n2\n2" ] ]
In the first example, the student can just stay at cell $2$. The teacher, initially located in cell $1$, can reach cell $2$ in one move. Therefore, the answer is $1$. In the second example, the student should just stay at cell $1$. The teacher, initially located in cell $3$, can reach cell $1$ in two moves. Therefore, the answer is $2$.
2005B2
https://codeforces.com/problemset/problem/2005/B2
The Strict Teacher (Hard Version)
1,200
[ "binary search", "greedy", "math", "sortings" ]
Div. 2
1,500
256
This is the hard version of the problem. The only differences between the two versions are the constraints on $m$ and $q$. In this version, $m, q \le 10^5$. You can make hacks only if both versions of the problem are solved. Narek and Tsovak were busy preparing this round, so they have not managed to do their homework and decided to steal David's homework. Their strict teacher noticed that David has no homework and now wants to punish him. She hires other teachers to help her catch David. And now $m$ teachers together are chasing him. Luckily, the classroom is big, so David has many places to hide. The classroom can be represented as a one-dimensional line with cells from $1$ to $n$, inclusive. At the start, all $m$ teachers and David are in distinct cells. Then they make moves. During each move * David goes to an adjacent cell or stays at the current one. * Then, each of the $m$ teachers simultaneously goes to an adjacent cell or stays at the current one. This continues until David is caught. David is caught if any of the teachers (possibly more than one) is located in the same cell as David. Everyone sees others' moves, so they all act optimally. Your task is to find how many moves it will take for the teachers to catch David if they all act optimally. Acting optimally means the student makes his moves in a way that maximizes the number of moves the teachers need to catch him; and the teachers coordinate with each other to make their moves in a way that minimizes the number of moves they need to catch the student. Also, as Narek and Tsovak think this task is easy, they decided to give you $q$ queries on David's position.
In the first line of the input, you are given a single integer $t$ ($1 \le t \le 10^5$) β€” the number of test cases. The description of each test case follows. In the first line of each test case, you are given three integers $n$, $m$, and $q$ ($3 \le n \le 10^9$, $1 \le m, q \le 10^5$) β€” the number of cells on the line, the number of teachers, and the number of queries. In the second line of each test case, you are given $m$ distinct integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le n$) β€” the cell numbers of the teachers. In the third line of each test case, you are given $q$ integers $a_1, a_2, \ldots, a_q$ ($1 \le a_i \le n$) β€” David's cell number for every query. It is guaranteed that for any $i$, $j$ such that $1 \le i \le m$ and $1 \le j \le q$, $b_i \neq a_j$. It is guaranteed that the sum of values of $m$ over all test cases does not exceed $2 \cdot 10^5$. It is guaranteed that the sum of values of $q$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output $q$ lines, the $i$-th of them containing the answer of the $i$-th query.
[ [ "2\n8 1 1\n6\n3\n10 3 3\n1 4 8\n2 3 10", "5\n1\n1\n2" ] ]
In the only query of the first example, the student can run to cell $1$. It will take the teacher five moves to reach from cell $6$ to cell $1$, so the answer is $5$. In the second query of the second example, the student can just stay at cell $3$. The teacher, initially located in cell $4$, can reach cell $3$ in one move. Therefore, the answer is $1$.
2005C
https://codeforces.com/problemset/problem/2005/C
Lazy Narek
1,800
[ "dp", "implementation", "strings" ]
Div. 2
2,000
256
Narek is too lazy to create the third problem of this contest. His friend Artur suggests that he should use ChatGPT. ChatGPT creates $n$ problems, each consisting of $m$ letters, so Narek has $n$ strings. To make the problem harder, he combines the problems by selecting some of the $n$ strings possibly none and concatenating them without altering their order. His chance of solving the problem is defined as $score_n - score_c$, where $score_n$ is Narek's score and $score_c$ is ChatGPT's score. Narek calculates $score_n$ by examining the selected string (he moves from left to right). He initially searches for the letter $\texttt{"n"}$, followed by $\texttt{"a"}$, $\texttt{"r"}$, $\texttt{"e"}$, and $\texttt{"k"}$. Upon finding all occurrences of these letters, he increments $score_n$ by $5$ and resumes searching for $\texttt{"n"}$ again (he doesn't go back, and he just continues from where he left off). After Narek finishes, ChatGPT scans through the array and increments $score_c$ by $1$ for each letter $\texttt{"n"}$, $\texttt{"a"}$, $\texttt{"r"}$, $\texttt{"e"}$, or $\texttt{"k"}$ that Narek fails to utilize (note that if Narek fails to complete the last occurrence by finding all of the $5$ letters, then all of the letters he used are counted in ChatGPT's score $score_c$, and Narek doesn't get any points if he doesn't finish finding all the 5 letters). Narek aims to maximize the value of $score_n - score_c$ by selecting the most optimal subset of the initial strings.
In the first line of the input, you're given a single integer $t$ ($1 \le t \le 10^5$), the number of test cases. Then the description of each test case follows. In the first line of each test case, you're given two integers $n, m$ ($1 \le n, m \le 10^3$), the number of strings and the length of each string. In the next $n$ lines, you're given $n$ strings, each having a length of $m$. The strings only contain lowercase letters of the English alphabet. The sum of values of $n \cdot m$ over all test cases does not exceed $10^6$.
For each test case, output a single integer: the maximal possible value of $score_n - score_c$.
[ [ "4\n5 2\nnn\naa\nrr\nee\nkk\n1 5\nnarek\n1 4\nnare\n5 7\nnrrarek\nnrnekan\nuuuuuuu\nppppppp\nnkarekz", "0\n5\n0\n7" ] ]
In the first test case, one of the optimal answers is when Narek doesn't choose any of the strings, so the answer is $0$. He can alternatively choose all the strings. In this case, the full string becomes "nnaarreekk". Narek can choose the first appearances of all letters and add $5$ to the score. His opponent will add $1$ for all second appearances, which will be $5$ in total. So the answer will be $5 - 5 = 0$. In the third test case, the only optimal answer is when Narek doesn't choose the string. Note that if he were to choose the string, he wouldn't be able to find the last letter "k", so his score would stay at $0$ instead of becoming $5$. Then ChatGPT would add $4$ for all of the $4$ letters, and the answer would become $0 - 4 = -4$. In the last test case, Narek needs to choose the first and the last strings. After putting these two next to each other, he gets "${\color{red}{n}}rr{\color{red}{a}}{\color{red}{r}}{\color{red}{e}}{\color{red}{k}}{\color{red}{n}}k{\color{red}{a}}{\color{red}{r}}{\color{red}{e}}{\color{red}{k}}{\color{blue}{z}}$". Narek can choose the letters marked with red and add $10$ to his score. Since the black colored letters Narek left behind are eligible for the opponent to claim (they are used in the word "narek"), the opponent adds all other letters to the score and gets a score of $3$. Therefore, the answer is $10 - 3 = 7$.
2005D
https://codeforces.com/problemset/problem/2005/D
Alter the GCD
2,400
[ "binary search", "brute force", "data structures", "divide and conquer", "implementation", "number theory" ]
Div. 2
4,000
256
You are given two arrays $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$. You must perform the following operation exactly once: * choose any indices $l$ and $r$ such that $1 \le l \le r \le n$; * swap $a_i$ and $b_i$ for all $i$ such that $l \leq i \leq r$. Find the maximum possible value of $\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)$ after performing the operation exactly once. Also find the number of distinct pairs $(l, r)$ which achieve the maximum value.
In the first line of the input, you are given a single integer $t$ ($1 \le t \le 10^5$), the number of test cases. Then the description of each test case follows. In the first line of each test case, you are given a single integer $n$ ($1 \le n \le 2 \cdot 10^5$), representing the number of integers in each array. In the next line, you are given $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the elements of the array $a$. In the last line, you are given $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \le b_i \le 10^9$) β€” the elements of the array $b$. The sum of values of $n$ over all test cases does not exceed $5 \cdot 10^5$.
For each test case, output a line with two integers: the maximum value of $\text{gcd}(a_1, a_2, \ldots, a_n) + \text{gcd}(b_1, b_2, \ldots, b_n)$ after performing the operation exactly once, and the number of ways.
[ [ "5\n8\n11 4 16 17 3 24 25 8\n8 10 4 21 17 18 25 21\n4\n6 4 24 13\n15 3 1 14\n2\n13 14\n5 8\n8\n20 17 15 11 21 10 3 7\n9 9 4 20 14 9 13 1\n2\n18 13\n15 20", "2 36\n3 2\n2 3\n2 36\n6 1" ] ]
In the first, third, and fourth test cases, there's no way to achieve a higher GCD than $1$ in any of the arrays, so the answer is $1 + 1 = 2$. Any pair $(l, r)$ achieves the same result; for example, in the first test case there are $36$ such pairs. In the last test case, you must choose $l = 1$, $r = 2$ to maximize the answer. In this case, the GCD of the first array is $5$, and the GCD of the second array is $1$, so the answer is $5 + 1 = 6$, and the number of ways is $1$.
2005E1
https://codeforces.com/problemset/problem/2005/E1
Subtangle Game (Easy Version)
2,100
[ "dp", "games", "greedy", "implementation" ]
Div. 2
2,000
256
This is the easy version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game. They have an array $a$ and a matrix $b$ of integers with $n$ rows and $m$ columns, numbered from $1$. The cell in the $i$-th row and the $j$-th column is $(i, j)$. They are looking for the elements of $a$ in turns; Tsovak starts first. Each time a player looks for a cell in the matrix containing the current element of $a$ (Tsovak looks for the first, then Narek looks for the second, etc.). Let's say a player has chosen the cell $(r, c)$. The next player has to choose his cell in the submatrix starting at $(r + 1, c + 1)$ and ending in $(n, m)$ (the submatrix can be empty if $r=n$ or $c=m$). If a player cannot find such a cell (or the remaining submatrix is empty) or the array ends (the previous player has chosen the last element), then he loses. Your task is to determine the winner if the players play optimally.
The first line of the input contains $t$ ($1 \le t \le 300$) – the number of test cases. The first line of each test case contains three integers $l$, $n$, and $m$ ($1 \le l, n, m \le 300$) – the size of the array and the sizes of the matrix. The second line contains $l$ integers $a_1, a_2, a_3, \ldots a_l$ ($1 \le a_i \le \min(7, n \cdot m)$) – the elements of the array $a$. The $i$-th of the last $n$ lines contains $m$ integers $b_{i,1}, b_{i,2}, b_{i,3}, \ldots b_{i,m}$ ($1 \le b_{i,j} \le \min(7, n \cdot m)$) – representing the $i$-th row of the matrix. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^5$. It is guaranteed that the sum of $l$ over all test cases does not exceed $300$.
You should output $t$ lines, the $i$-th of them containing a character representing the answer of the $i$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes).
[ [ "3\n2 2 3\n1 2\n1 3 5\n4 5 2\n2 2 4\n1 2\n1 1 3 2\n4 2 5 1\n2 4 2\n1 2\n3 4\n5 5\n5 5\n5 5", "N\nT\nN" ] ]
In the first example, Tsovak starts by looking for $1$. There is only one occurrence of $1$ at $(1,1)$, so he chooses it. Then Narek needs to look for $2$ in the submatrix of $(2, 2)$, which consists of just the last two elements: $5$ and $2$. He chooses $2$, and then Tsovak loses since the array has ended. In the second example, Tsovak needs to choose $1$. There is a $1$ at the cell $(n,m)$, so he chooses that cell. Then, since the submatrix of $(n + 1, m + 1)$ is empty, Narek cannot find $2$, so he loses.
2005E2
https://codeforces.com/problemset/problem/2005/E2
Subtangle Game (Hard Version)
2,500
[ "data structures", "dp", "games", "greedy", "implementation" ]
Div. 2
2,000
256
This is the hard version of the problem. The differences between the two versions are the constraints on all the variables. You can make hacks only if both versions of the problem are solved. Tsovak and Narek are playing a game. They have an array $a$ and a matrix $b$ of integers with $n$ rows and $m$ columns, numbered from $1$. The cell in the $i$-th row and the $j$-th column is $(i, j)$. They are looking for the elements of $a$ in turns; Tsovak starts first. Each time a player looks for a cell in the matrix containing the current element of $a$ (Tsovak looks for the first, then Narek looks for the second, etc.). Let's say a player has chosen the cell $(r, c)$. The next player has to choose his cell in the submatrix starting at $(r + 1, c + 1)$ and ending in $(n, m)$ (the submatrix can be empty if $r=n$ or $c=m$). If a player cannot find such a cell (or the remaining submatrix is empty) or the array ends (the previous player has chosen the last element), then he loses. Your task is to determine the winner if the players play optimally. Note: since the input is large, you may need to optimize input/output for this problem. For example, in C++, it is enough to use the following lines at the start of the main() function: int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); }
The first line of the input contains $t$ ($1 \le t \le 1500$) – the number of test cases. The first line of each test case contains three integers $l$, $n$, and $m$ ($1 \le l, n, m \le 1500$) – the size of the array and the sizes of the matrix. The second line contains $l$ integers $a_1, a_2, a_3, \ldots a_l$ ($1 \le a_i \le n \cdot m$) – the elements of the array $a$. The $i$-th of the last $n$ lines contains $m$ integers $b_{i,1}, b_{i,2}, b_{i,3}, \ldots b_{i,m}$ ($1 \le b_{i,j} \le n \cdot m$) – representing the $i$-th row of the matrix. It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $3 \cdot 10^6$. It is guaranteed that the sum of $l$ over all test cases does not exceed $1500$.
You should output $t$ lines, the $i$-th of them containing a character representing the answer of the $i$-th test case: "T" if Tsovak wins or "N", otherwise (without quotes).
[ [ "3\n2 2 3\n1 2\n1 3 6\n4 6 2\n2 2 4\n1 2\n1 1 3 2\n4 2 5 1\n2 4 2\n1 2\n3 4\n5 6\n7 8\n8 8", "N\nT\nN" ] ]
In the first example, Tsovak starts by looking for $1$. There is only one occurrence of $1$ at $(1,1)$, so he chooses it. Then Narek needs to look for $2$ in the submatrix of $(2, 2)$, which consists of just the last two elements: $6$ and $2$. He chooses $2$, and then Tsovak loses since the array has ended. In the second example, Tsovak needs to choose $1$. There is a $1$ at the cell $(n,m)$, so he chooses that cell. Then, since the submatrix of $(n + 1, m + 1)$ is empty, Narek cannot find $2$, so he loses.
2006A
https://codeforces.com/problemset/problem/2006/A
Iris and Game on the Tree
1,700
[ "constructive algorithms", "dfs and similar", "games", "graphs", "greedy", "trees" ]
Div. 1
2,000
256
Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$. Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the weight of the leaf is the difference between the number of occurrences of $\mathtt{10}$ and $\mathtt{01}$ substrings in it. Take the following tree as an example. Green vertices have a value of $\mathtt 1$ while white vertices have a value of $\mathtt 0$. ![](CDN_BASE_URL/712e8f1acc71d0401cf5eb23441e53a5) * Let's calculate the weight of the leaf $5$: the formed string is $\mathtt{10110}$. The number of occurrences of substring $\mathtt{10}$ is $2$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $2 - 1 = 1$. * Let's calculate the weight of the leaf $6$: the formed string is $\mathtt{101}$. The number of occurrences of substring $\mathtt{10}$ is $1$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $1 - 1 = 0$. The score of a tree is defined as the number of leaves with non-zero weight in the tree. But the values of some vertices haven't been decided and will be given to you as $\texttt{?}$. Filling the blanks would be so boring, so Iris is going to invite Dora to play a game. On each turn, one of the girls chooses any of the remaining vertices with value $\texttt{?}$ and changes its value to $\mathtt{0}$ or $\mathtt{1}$, with Iris going first. The game continues until there are no vertices with value $\mathtt{?}$ left in the tree. Iris aims to maximize the score of the tree, while Dora aims to minimize that. Assuming that both girls play optimally, please determine the final score of the tree.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 5\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 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's guaranteed that the given edges form a tree. The last line contains a string $s$ of length $n$. The $i$-th character of $s$ represents the value of vertex $i$. It's guaranteed that $s$ only contains characters $\mathtt{0}$, $\mathtt{1}$ and $\mathtt{?}$. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2\cdot 10^5$.
For each test case, output a single integer β€” the final score of the tree.
[ [ "6\n4\n1 2\n1 3\n4 1\n0101\n4\n1 2\n3 2\n2 4\n???0\n5\n1 2\n1 3\n2 4\n2 5\n?1?01\n6\n1 2\n2 3\n3 4\n5 3\n3 6\n?0????\n5\n1 2\n1 3\n1 4\n1 5\n11?1?\n2\n2 1\n??", "2\n1\n1\n2\n1\n0" ] ]
In the first test case, all the values of the vertices have been determined. There are three different paths from the root to a leaf: * From vertex $1$ to vertex $2$. The string formed by the path is $\mathtt{01}$, so the weight of the leaf is $0-1=-1$. * From vertex $1$ to vertex $3$. The string formed by the path is $\mathtt{00}$, so the weight of the leaf is $0-0=0$. * From vertex $1$ to vertex $4$. The string formed by the path is $\mathtt{01}$, so the weight of the leaf is $0-1=-1$. Thus, there are two leaves with non-zero weight, so the score of the tree is $2$. In the second test case, one of the sequences of optimal choices for the two players can be: * Iris chooses to change the value of the vertex $3$ to $\mathtt 1$. * Dora chooses to change the value of the vertex $1$ to $\mathtt 0$. * Iris chooses to change the value of the vertex $2$ to $\mathtt 0$. The final tree is as follows: ![](CDN_BASE_URL/c8e20d0cf352b7905e60a7b371f053d6) The only leaf with non-zero weight is $3$, so the score of the tree is $1$. Note that this may not be the only sequence of optimal choices for Iris and Dora.
2006B
https://codeforces.com/problemset/problem/2006/B
Iris and the Tree
1,800
[ "brute force", "data structures", "dfs and similar", "dsu", "math", "trees" ]
Div. 1
3,000
256
Given a rooted tree with the root at vertex $1$. For any vertex $i$ ($1 < i \leq n$) in the tree, there is an edge connecting vertices $i$ and $p_i$ ($1 \leq p_i < i$), with a weight equal to $t_i$. Iris does not know the values of $t_i$, but she knows that $\displaystyle\sum_{i=2}^n t_i = w$ and each of the $t_i$ is a non- negative integer. The vertices of the tree are numbered in a special way: the numbers of the vertices in each subtree are consecutive integers. In other words, the vertices of the tree are numbered in the order of a depth-first search. ![](CDN_BASE_URL/274244c032854fe172d47861e2eb9c02) The tree in this picture satisfies the condition. For example, in the subtree of vertex $2$, the vertex numbers are $2, 3, 4, 5$, which are consecutive integers. ![](CDN_BASE_URL/83174231191d329be697a6e3f67b5eb3) The tree in this picture does not satisfy the condition, as in the subtree of vertex $2$, the vertex numbers $2$ and $4$ are not consecutive integers. We define $\operatorname{dist}(u, v)$ as the length of the simple path between vertices $u$ and $v$ in the tree. Next, there will be $n - 1$ events: * Iris is given integers $x$ and $y$, indicating that $t_x = y$. After each event, Iris wants to know the maximum possible value of $\operatorname{dist}(i, i \bmod n + 1)$ independently for each $i$ ($1\le i\le n$). She only needs to know the sum of these $n$ values. Please help Iris quickly get the answers. Note that when calculating the maximum possible values of $\operatorname{dist}(i, i \bmod n + 1)$ and $\operatorname{dist}(j, j \bmod n + 1)$ for $i \ne j$, the unknown edge weights may be different.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 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 $w$ ($2 \le n \le 2 \cdot 10^5$, $0 \leq w \leq 10^{12}$) β€” the number of vertices in the tree and the sum of the edge weights. The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i < i$) β€” the description of the edges of the tree. Then follow $n-1$ lines indicating the events. Each line contains two integers $x$ and $y$ ($2 \leq x \leq n$, $0 \leq y \leq w$), indicating that $t_x = y$. It is guaranteed that all $x$ in the events are distinct. It is also guaranteed that the sum of all $y$ equals $w$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output one line containing $n-1$ integers, each representing the answer after each event.
[ [ "4\n2 1000000000000\n1\n2 1000000000000\n4 9\n1 1 1\n2 2\n4 4\n3 3\n6 100\n1 2 3 2 1\n6 17\n3 32\n2 4\n4 26\n5 21\n10 511\n1 2 2 4 2 1 1 8 8\n3 2\n6 16\n10 256\n9 128\n2 1\n5 8\n8 64\n4 4\n7 32", "2000000000000\n25 18 18\n449 302 247 200 200\n4585 4473 2681 1567 1454 1322 1094 1022 1022" ] ]
In the first test case, $\operatorname{dist}(1, 2) = \operatorname{dist}(2, 1) = t_2 = w = 10^{12}$, so $\operatorname{dist}(1, 2) + \operatorname{dist}(2, 1) = 2 \cdot 10^{12}$. In the second test case, the tree after Iris found out all $t_x$ is shown below: ![](CDN_BASE_URL/2f461ea7db7bb0cda4536a2a78190c2f) $\operatorname{dist}(1, 2) = t_2$, $\operatorname{dist}(2, 3) = t_2 + t_3$, $\operatorname{dist}(3, 4) = t_3 + t_4$, $\operatorname{dist}(4, 1) = t_4$. After the first event, she found out that $t_2 = 2$, so $\operatorname{dist}(1, 2) = 2$. At the same time: * $\operatorname{dist}(2, 3)$ is maximized if $t_3 = 7$, $t_4 = 0$. Then $\operatorname{dist}(2, 3) = 9$. * $\operatorname{dist}(3, 4)$ and $\operatorname{dist}(4, 1)$ are maximized if $t_3 = 0$, $t_4 = 7$. Then $\operatorname{dist}(3, 4) = \operatorname{dist}(4, 1) = 7$. Thus, the answer is $2 + 9 + 7 + 7 = 25$. After the second event, she found out that $t_4 = 4$, then $t_3 = w - t_2 - t_4 = 4$. $\operatorname{dist}(1, 2) = 2$, $\operatorname{dist}(2, 3) = 2 + 3 = 5$, $\operatorname{dist}(3, 4) = 3 + 4 = 7$, $\operatorname{dist}(4, 1) = 4$. Thus, the answer is $2 + 5 + 7 + 4 = 18$.
2006C
https://codeforces.com/problemset/problem/2006/C
Eri and Expanded Sets
2,300
[ "data structures", "divide and conquer", "math", "number theory", "two pointers" ]
Div. 1
3,000
512
Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $x\neq y$ from the set such that their average $\frac{x+y}2$ is still a positive integer and isn't contained in the set, and add it to the set. The integers $x$ and $y$ remain in the set. Let's call the set of integers consecutive if, after the elements are sorted, the difference between any pair of adjacent elements is $1$. For example, sets $\\{2\\}$, $\\{2, 5, 4, 3\\}$, $\\{5, 6, 8, 7\\}$ are consecutive, while $\\{2, 4, 5, 6\\}$, $\\{9, 7\\}$ are not. Eri likes consecutive sets. Suppose there is an array $b$, then Eri puts all elements in $b$ into the set. If after a finite number of operations described above, the set can become consecutive, the array $b$ will be called brilliant. Note that if the same integer appears in the array multiple times, we only put it into the set once, as a set always contains distinct positive integers. Eri has an array $a$ of $n$ positive integers. Please help him to count the number of pairs of integers $(l,r)$ such that $1 \leq l \leq r \leq n$ and the subarray $a_l, a_{l+1}, \ldots, a_r$ is brilliant.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 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 4 \cdot 10^5$) β€” length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots a_n$ ($1 \leq a_i \leq 10^9$) β€” the elements of the array $a$. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $4 \cdot 10^5$.
For each test case, output a single integer β€” the number of brilliant subarrays.
[ [ "6\n2\n2 2\n6\n1 3 6 10 15 21\n5\n6 30 18 36 9\n1\n1000000000\n6\n1 1 4 5 1 4\n12\n70 130 90 90 90 108 612 500 451 171 193 193", "3\n18\n5\n1\n18\n53" ] ]
In the first test case, the array $a = [2, 2]$ has $3$ subarrays: $[2]$, $[2]$, and $[2, 2]$. For all of them, the set only contains a single integer $2$, therefore it's always consecutive. All these subarrays are brilliant, so the answer is $3$. In the second test case, let's consider the subarray $[3, 6, 10]$. We can do operations as follows: $$\\{3,6,10\\} \xrightarrow{x=6,y=10} \\{3,6,8,10\\} \xrightarrow{x=6,y=8} \\{3,6,7,8,10\\} \xrightarrow{x=3,y=7} \\{3,5,6,7,8,10\\}$$ $$\xrightarrow{x=3,y=5} \\{3,4,5,6,7,8,10\\} \xrightarrow{x=8,y=10} \\{3,4,5,6,7,8,9,10\\}$$ $\\{3,4,5,6,7,8,9,10\\}$ is a consecutive set, so the subarray $[3, 6, 10]$ is brilliant.
2006D
https://codeforces.com/problemset/problem/2006/D
Iris and Adjacent Products
2,600
[ "data structures", "greedy", "implementation", "math" ]
Div. 1
3,000
256
Iris has just learned multiplication in her Maths lessons. However, since her brain is unable to withstand too complex calculations, she could not multiply two integers with the product greater than $k$ together. Otherwise, her brain may explode! Her teacher sets a difficult task every day as her daily summer holiday homework. Now she is given an array $a$ consisting of $n$ elements, and she needs to calculate the product of each two adjacent elements (that is, $a_1 \cdot a_2$, $a_2 \cdot a_3$, and so on). Iris wants her brain to work safely, and in order to do that, she would like to modify the array $a$ in such a way that $a_i \cdot a_{i + 1} \leq k$ holds for every $1 \leq i < n$. There are two types of operations she can perform: 1. She can rearrange the elements of the array $a$ in an arbitrary way. 2. She can select an arbitrary element of the array $a$ and change its value to an arbitrary integer from $1$ to $k$. Iris wants to minimize the number of operations of type $2$ that she uses. However, that's completely not the end of the summer holiday! Summer holiday lasts for $q$ days, and on the $i$-th day, Iris is asked to solve the Math homework for the subarray $b_{l_i}, b_{l_i + 1}, \ldots, b_{r_i}$. Help Iris and tell her the minimum number of type $2$ operations she needs to perform for each day. Note that the operations are independent for each day, i.e. the array $b$ is not changed.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 5\cdot 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains three integers $n$, $q$ and $k$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$, $1 \leq k \leq 10^6$) β€” the length of array $b$, the number of days, and the upper bound for the multiplication calculation. The second line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($1 \leq b_i \leq k$) β€” the elements of the array $b$. Then $q$ lines follow, the $i$-th of them contains two integers $l_i$ and $r_i$ ($1 \leq l_i < r_i \leq n$) β€” the boundaries of the subarray on the $i$-th day. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$, and the sum of $q$ over all test cases does not exceed $10^5$.
For each test, output a single line containing $q$ integers β€” the minimum number of operations of type $2$ needed for each day.
[ [ "5\n3 1 1\n1 1 1\n1 3\n3 2 10\n1 10 9\n1 3\n2 3\n5 4 2\n2 2 2 2 2\n1 2\n2 4\n2 5\n1 5\n6 5 10\n3 2 5 10 10 1\n1 4\n3 6\n1 6\n2 5\n5 6\n10 10 10\n10 9 8 7 6 5 4 3 2 1\n1 10\n1 9\n1 8\n1 7\n2 10\n3 10\n4 10\n5 10\n3 9\n6 8", "0 \n0 1 \n1 1 2 2 \n1 1 1 1 0 \n3 3 4 3 2 2 1 1 2 1" ] ]
In the first test case, as Iris can always multiply $1$ and $1$ together, no operations are needed, so the answer is $0$. In the second test case, the first day's homework is $[1, 10, 9]$. Iris can rearrange its elements to get $[9, 1, 10]$, so no operations of type $2$ are needed. The second day's homework is $[10, 9]$, and she can change one element to get the array $[1, 9]$, so one operation of type $2$ is needed.
2006E
https://codeforces.com/problemset/problem/2006/E
Iris's Full Binary Tree
3,100
[ "brute force", "data structures", "dfs and similar", "trees" ]
Div. 1
4,000
1,024
Iris likes full binary trees. Let's define the depth of a rooted tree as the maximum number of vertices on the simple paths from some vertex to the root. A full binary tree of depth $d$ is a binary tree of depth $d$ with exactly $2^d - 1$ vertices. Iris calls a tree a $d$-binary tree if some vertices and edges can be added to it to make it a full binary tree of depth $d$. Note that any vertex can be chosen as the root of a full binary tree. Since performing operations on large trees is difficult, she defines the binary depth of a tree as the minimum $d$ satisfying that the tree is $d$-binary. Specifically, if there is no integer $d \ge 1$ such that the tree is $d$-binary, the binary depth of the tree is $-1$. Iris now has a tree consisting of only vertex $1$. She wants to add $n - 1$ more vertices to form a larger tree. She will add the vertices one by one. When she adds vertex $i$ ($2 \leq i \leq n$), she'll give you an integer $p_i$ ($1 \leq p_i < i$), and add a new edge connecting vertices $i$ and $p_i$. Iris wants to ask you the binary depth of the tree formed by the first $i$ vertices for each $1 \le i \le n$. Can you tell her the answer?
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 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 5 \cdot 10^5$) β€” the final size of the tree. The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i < i$) β€” descriptions of all edges of the tree. It is guaranteed that the sum of $n$ over all test cases does not exceed $5 \cdot 10^5$.
For each test case output $n$ integers, $i$-th of them representing the binary depth of the tree formed by the first $i$ vertices.
[ [ "7\n3\n1 1\n6\n1 2 3 4 5\n7\n1 1 3 2 5 1\n10\n1 1 2 1 4 2 4 5 8\n10\n1 1 3 1 3 2 2 2 6\n20\n1 1 2 2 4 4 5 5 7 6 8 6 11 14 11 8 13 13 12\n25\n1 1 3 3 1 5 4 4 6 8 11 12 8 7 11 13 7 13 15 6 19 14 10 23", "1 2 2 \n1 2 2 3 3 4 \n1 2 2 3 3 4 4 \n1 2 2 3 3 3 4 4 5 5 \n1 2 2 3 3 4 4 4 -1 -1 \n1 2 2 3 3 4 4 4 4 5 5 5 5 6 6 6 6 6 6 7 \n1 2 2 3 3 4 4 4 4 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8" ] ]
In the first test case, the final tree is shown below: ![](CDN_BASE_URL/5045337022386beaeb8585d242e8d14a) * The tree consisting of the vertex $1$ has the binary depth $1$ (the tree itself is a full binary tree of depth $1$). * The tree consisting of the vertices $1$ and $2$ has the binary depth $2$ (we can add the vertex $3$ to make it a full binary tree of depth $2$). * The tree consisting of the vertices $1$, $2$ and $3$ has the binary depth $2$ (the tree itself is a full binary tree of depth $2$). In the second test case, the formed full binary tree after adding some vertices to the tree consisting of $n$ vertices is shown below (bolded vertices are added): ![](CDN_BASE_URL/47a803b975dced95812a171507c56a8b) The depth of the formed full binary tree is $4$. In the fifth test case, the final tree is shown below: ![](CDN_BASE_URL/ffe91eb3e0db4e64598df8a805ba1e4a) It can be proved that Iris can't form any full binary tree by adding vertices and edges, so the binary depth is $-1$.
2006F
https://codeforces.com/problemset/problem/2006/F
Dora's Paint
3,500
[ "brute force", "combinatorics", "constructive algorithms", "graphs", "implementation" ]
Div. 1
3,000
512
Sadly, Dora poured the paint when painting the class mural. Dora considers the mural as the matrix $b$ of size $n \times n$. Initially, $b_{i,j} = 0$ for all $1 \le i, j \le n$. Dora has only two brushes which have two different colors. In one operation, she can paint the matrix with one of two brushes: * The first brush has color $1$ on it and can paint one column of the matrix. That is, Dora chooses $1 \leq j \leq n$ and makes $b_{i,j} := 1$ for all $1 \leq i \leq n$; * The second brush has color $2$ on it and can paint one row of the matrix. That is, Dora chooses $1 \leq i \leq n$ and makes $b_{i,j} := 2$ for all $1 \leq j \leq n$. Dora paints the matrix so that the resulting matrix $b$ contains only $1$ and $2$. For a matrix $b$, let $f(b)$ denote the minimum number of operations needed to turn the initial matrix (containing only $0$) into $b$. The beauty of a matrix $b$ is the number of ways to paint the initial matrix in exactly $f(b)$ operations to turn it into $b$. If there's no way to turn the initial matrix into $b$, the beauty of $b$ is $0$. However, Dora made a uniformly random mistake; there's exactly one element different in the matrix $a$ given to you from the real matrix $b$. That is, there is exactly one pair $(i, j)$ such that $a_{i, j} = 3 - b_{i, j}$. Please help Dora compute the expected beauty of the real matrix $b$ modulo $998\,244\,353$ (all possible $n^2$ mistakes have equal probability). Since the size of the matrix is too large, Dora will only tell you the positions of $m$ elements of color $1$, and the remaining $n^2-m$ elements have color $2$.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 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 $m$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq m \leq \min(10^6, n^2)$) β€” the size of the matrix and the number of elements of color $1$. Then $m$ lines follow, each containing two positive integers $x_i$ and $y_i$ ($1 \leq x_i, y_i \leq n$) β€” denoting that $a_{x_i, y_i} = 1$. It is guaranteed that if $i \neq j$, then $(x_i, y_i) \neq (x_j, y_j)$. It is also guaranteed that the sum of $n$ over all test cases does not exceed $4\cdot10^5$, and the sum of $m$ over all test cases does not exceed $10^6$.
For each test case, output a single integer β€” the expected beauty of the real matrix $b$, modulo $998\,244\,353$.
[ [ "7\n2 2\n1 1\n1 2\n2 1\n1 1\n3 2\n1 1\n3 3\n6 0\n5 10\n1 1\n1 2\n1 3\n2 1\n2 3\n5 1\n5 2\n5 3\n5 4\n5 5\n3 5\n1 1\n1 3\n2 2\n3 1\n3 3\n4 3\n1 1\n2 3\n2 4", "1\n499122178\n665496236\n120\n79859554\n776412275\n1" ] ]
In the first test case, the matrix $a = \left[\begin{matrix}1&1\\\2&2\end{matrix}\right]$. Let's consider changing the element $(1,1)$ to calculate the answer. It can be proved that the minimum steps to paint the initial matrix into $\left[\begin{matrix}2&1\\\2&2\end{matrix}\right]$ is $3$. We can first paint the first row into color $2$, then paint the second column into color $1$, and finally paint the second row into color $2$. The process is listed below: $$\left[\begin{matrix}0&0\\\0&0\end{matrix}\right]\Rightarrow\left[\begin{matrix}2&2\\\0&0\end{matrix}\right]\Rightarrow\left[\begin{matrix}2&1\\\0&1\end{matrix}\right]\Rightarrow\left[\begin{matrix}2&1\\\2&2\end{matrix}\right]$$ It can be proved that this is the only way to paint the matrix in $3$ steps. So the beauty of the matrix $\left[\begin{matrix}2&1\\\2&2\end{matrix}\right]$ is $1$. Similarly, if any other element of the matrix is changed, the beauty is always $1$, so the expected beauty of the real matrix $b$ is $1$. In the second test case, the matrix $a = \left[\begin{matrix}1&2\\\2&2\end{matrix}\right]$. Let's consider changing the element $(2, 2)$ to calculate the answer. It can be proven that it's impossible to paint the initial matrix into $\left[\begin{matrix}1&2\\\2&1\end{matrix}\right]$, so its beauty is $0$. If any other element of the matrix is changed, the beauty is always $2$, so the expected beauty is $\frac{0 + 2 + 2 + 2}{4} = \frac{6}{4} \equiv 499\,122\,178 \pmod {998\,244\,353}$.
2007A
https://codeforces.com/problemset/problem/2007/A
Dora's Set
800
[ "greedy", "math", "number theory" ]
Div. 2
1,000
256
Dora has a set $s$ containing integers. In the beginning, she will put all integers in $[l, r]$ into the set $s$. That is, an integer $x$ is initially contained in the set if and only if $l \leq x \leq r$. Then she allows you to perform the following operations: * Select three distinct integers $a$, $b$, and $c$ from the set $s$, such that $\gcd(a, b) = \gcd(b, c) = \gcd(a, c) = 1^\dagger$. * Then, remove these three integers from the set $s$. What is the maximum number of operations you can perform? $^\dagger$Recall that $\gcd(x, y)$ means the [greatest common divisor](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers $x$ and $y$.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 500$) β€” the number of test cases. The description of the test cases follows. The only line of each test case contains two integers $l$ and $r$ ($1 \leq l \leq r \leq 1000$) β€” the range of integers in the initial set.
For each test case, output a single integer β€” the maximum number of operations you can perform.
[ [ "8\n1 3\n3 7\n10 21\n2 8\n51 60\n2 15\n10 26\n1 1000", "1\n1\n3\n1\n2\n3\n4\n250" ] ]
In the first test case, you can choose $a = 1$, $b = 2$, $c = 3$ in the only operation, since $\gcd(1, 2) = \gcd(2, 3) = \gcd(1, 3) = 1$, and then there are no more integers in the set, so no more operations can be performed. In the second test case, you can choose $a = 3$, $b = 5$, $c = 7$ in the only operation. In the third test case, you can choose $a = 11$, $b = 19$, $c = 20$ in the first operation, $a = 13$, $b = 14$, $c = 15$ in the second operation, and $a = 10$, $b = 17$, $c = 21$ in the third operation. After the three operations, the set $s$ contains the following integers: $12$, $16$, $18$. It can be proven that it's impossible to perform more than $3$ operations.
2007B
https://codeforces.com/problemset/problem/2007/B
Index and Maximum Value
900
[ "data structures", "greedy" ]
Div. 2
1,000
256
After receiving yet another integer array $a_1, a_2, \ldots, a_n$ at her birthday party, Index decides to perform some operations on it. Formally, there are $m$ operations that she is going to perform in order. Each of them belongs to one of the two types: * $\texttt{+ l r}$. Given two integers $l$ and $r$, for all $1 \leq i \leq n$ such that $l \leq a_i \leq r$, set $a_i := a_i + 1$. * $\texttt{- l r}$. Given two integers $l$ and $r$, for all $1 \leq i \leq n$ such that $l \leq a_i \leq r$, set $a_i := a_i - 1$. For example, if the initial array $a = [7, 1, 3, 4, 3]$, after performing the operation $\texttt{+} \space 2 \space 4$, the array $a = [7, 1, 4, 5, 4]$. Then, after performing the operation $\texttt{-} \space 1 \space 10$, the array $a = [6, 0, 3, 4, 3]$. Index is curious about the maximum value in the array $a$. Please help her find it after each of the $m$ operations.
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 two integers $n$ and $m$ ($1 \leq n \leq 10^5$, $1 \leq m \leq 10^5$) β€” the length of the array and the number of operations. 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 initial array $a$. Then $m$ lines follow, each line corresponds to the operation, in the following format: $\texttt{c l r}$ ($c \in \\{\texttt +, \texttt -\\}$, $l$ and $r$ are integers, $1 \leq l \leq r \leq 10^9$) β€” the description of the operation. Note that the elements $a_i$ may not satisfy $1\le a_i\le 10^9$ after some operations, as it is shown in the example. 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 $10^5$.
For each test case, output one single line containing $m$ integers, with the $i$-th of them describing the maximum value of the array after the $i$-th operation.
[ [ "5\n5 5\n1 2 3 2 1\n+ 1 3\n- 2 3\n+ 1 2\n+ 2 4\n- 6 8\n5 5\n1 3 3 4 5\n+ 1 4\n+ 2 3\n- 4 5\n- 3 3\n- 2 6\n5 5\n1 1 1 1 1\n+ 2 3\n- 4 5\n+ 1 6\n- 2 5\n+ 1 8\n1 1\n1\n- 1 1\n1 1\n1000000000\n+ 1000000000 1000000000", "4 4 4 5 5\n5 5 4 4 3\n1 1 2 1 2\n0\n1000000001" ] ]
In the first test case, the process of the operations is listed below: * After the first operation, the array becomes equal $[2,3,4,3,2]$. The maximum value is $4$. * After the second operation, the array becomes equal $[1,2,4,2,1]$. The maximum value is $4$. * After the third operation, the array becomes equal $[2,3,4,3,2]$. The maximum value is $4$. * After the fourth operation, the array becomes equal $[3,4,5,4,3]$. The maximum value is $5$. * After the fifth operation, the array becomes equal $[3,4,5,4,3]$. The maximum value is $5$. In the second test case, the process of the operations is listed below: * After the first operation, the array becomes equal $[2,4,4,5,5]$. The maximum value is $5$. * After the second operation, the array becomes equal $[3,4,4,5,5]$. The maximum value is $5$. * After the third operation, the array becomes equal $[3,3,3,4,4]$. The maximum value is $4$. * After the fourth operation, the array becomes equal $[2,2,2,4,4]$. The maximum value is $4$. * After the fifth operation, the array becomes equal $[1,1,1,3,3]$. The maximum value is $3$.
2007C
https://codeforces.com/problemset/problem/2007/C
Dora and C++
1,500
[ "math", "number theory" ]
Div. 2
2,000
256
Dora has just learned the programming language C++! However, she has completely misunderstood the meaning of C++. She considers it as two kinds of adding operations on the array $c$ with $n$ elements. Dora has two integers $a$ and $b$. In one operation, she can choose one of the following things to do. * Choose an integer $i$ such that $1 \leq i \leq n$, and increase $c_i$ by $a$. * Choose an integer $i$ such that $1 \leq i \leq n$, and increase $c_i$ by $b$. Note that $a$ and $b$ are constants, and they can be the same. Let's define a range of array $d$ as $\max(d_i) - \min(d_i)$. For instance, the range of the array $[1, 2, 3, 4]$ is $4 - 1 = 3$, the range of the array $[5, 2, 8, 2, 2, 1]$ is $8 - 1 = 7$, and the range of the array $[3, 3, 3]$ is $3 - 3 = 0$. After any number of operations (possibly, $0$), Dora calculates the range of the new array. You need to help Dora minimize this value, but since Dora loves exploring all by herself, you only need to tell her the minimized value.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains three integers $n$, $a$, and $b$ ($1 \leq n \leq 10^5$, $1 \leq a, b \leq 10^9$) β€” the length of the array $c$ and the constant values, respectively. The second line of each test case contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \leq c_i \leq 10^9$) β€” the initial elements of the array $c$. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
For each test case, output a single integer β€” the minimum possible range of the array after any number of operations.
[ [ "10\n4 5 5\n1 3 4 4\n4 2 3\n1 3 4 6\n4 7 7\n1 1 2 6\n3 15 9\n1 9 5\n3 18 12\n1 4 5\n7 27 36\n33 13 23 12 35 24 41\n10 6 9\n15 5 6 9 8 2 12 15 3 8\n2 1 1000000000\n1 1000000000\n6 336718728 709848696\n552806726 474775724 15129785 371139304 178408298 13106071\n6 335734893 671469786\n138885253 70095920 456876775 9345665 214704906 375508929", "3\n0\n3\n2\n3\n5\n1\n0\n17\n205359241" ] ]
In the first test case, we can increase $c_1 = 1$ by $a = 5$. The array $c$ will become $[6, 3, 4, 4]$, and the range is $3$. Note that there is more than one way to reach the answer. In the second test case, we can increase $c_1 = 1$ by $a = 2$ and then increase $c_1 = 3$ by $b = 3$. Also, we can increase $c_2 = 3$ by $b = 3$ and increase $c_3 = 4$ by $a = 2$. The array $c$ will become $[6, 6, 6, 6]$, and the range is $0$.
2007D
https://codeforces.com/problemset/problem/2007/D
Iris and Game on the Tree
1,700
[ "games", "graphs", "greedy", "trees" ]
Div. 2
2,000
256
Iris has a tree rooted at vertex $1$. Each vertex has a value of $\mathtt 0$ or $\mathtt 1$. Let's consider a leaf of the tree (the vertex $1$ is never considered a leaf) and define its weight. Construct a string formed by the values of the vertices on the path starting at the root and ending in this leaf. Then the weight of the leaf is the difference between the number of occurrences of $\mathtt{10}$ and $\mathtt{01}$ substrings in it. Take the following tree as an example. Green vertices have a value of $\mathtt 1$ while white vertices have a value of $\mathtt 0$. ![](CDN_BASE_URL/712e8f1acc71d0401cf5eb23441e53a5) * Let's calculate the weight of the leaf $5$: the formed string is $\mathtt{10110}$. The number of occurrences of substring $\mathtt{10}$ is $2$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $2 - 1 = 1$. * Let's calculate the weight of the leaf $6$: the formed string is $\mathtt{101}$. The number of occurrences of substring $\mathtt{10}$ is $1$, the number of occurrences of substring $\mathtt{01}$ is $1$, so the difference is $1 - 1 = 0$. The score of a tree is defined as the number of leaves with non-zero weight in the tree. But the values of some vertices haven't been decided and will be given to you as $\texttt{?}$. Filling the blanks would be so boring, so Iris is going to invite Dora to play a game. On each turn, one of the girls chooses any of the remaining vertices with value $\texttt{?}$ and changes its value to $\mathtt{0}$ or $\mathtt{1}$, with Iris going first. The game continues until there are no vertices with value $\mathtt{?}$ left in the tree. Iris aims to maximize the score of the tree, while Dora aims to minimize that. Assuming that both girls play optimally, please determine the final score of the tree.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 5\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 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's guaranteed that the given edges form a tree. The last line contains a string $s$ of length $n$. The $i$-th character of $s$ represents the value of vertex $i$. It's guaranteed that $s$ only contains characters $\mathtt{0}$, $\mathtt{1}$ and $\mathtt{?}$. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $2\cdot 10^5$.
For each test case, output a single integer β€” the final score of the tree.
[ [ "6\n4\n1 2\n1 3\n4 1\n0101\n4\n1 2\n3 2\n2 4\n???0\n5\n1 2\n1 3\n2 4\n2 5\n?1?01\n6\n1 2\n2 3\n3 4\n5 3\n3 6\n?0????\n5\n1 2\n1 3\n1 4\n1 5\n11?1?\n2\n2 1\n??", "2\n1\n1\n2\n1\n0" ] ]
In the first test case, all the values of the vertices have been determined. There are three different paths from the root to a leaf: * From vertex $1$ to vertex $2$. The string formed by the path is $\mathtt{01}$, so the weight of the leaf is $0-1=-1$. * From vertex $1$ to vertex $3$. The string formed by the path is $\mathtt{00}$, so the weight of the leaf is $0-0=0$. * From vertex $1$ to vertex $4$. The string formed by the path is $\mathtt{01}$, so the weight of the leaf is $0-1=-1$. Thus, there are two leaves with non-zero weight, so the score of the tree is $2$. In the second test case, one of the sequences of optimal choices for the two players can be: * Iris chooses to change the value of the vertex $3$ to $\mathtt 1$. * Dora chooses to change the value of the vertex $1$ to $\mathtt 0$. * Iris chooses to change the value of the vertex $2$ to $\mathtt 0$. The final tree is as follows: ![](CDN_BASE_URL/c8e20d0cf352b7905e60a7b371f053d6) The only leaf with non-zero weight is $3$, so the score of the tree is $1$. Note that this may not be the only sequence of optimal choices for Iris and Dora.
2007E
https://codeforces.com/problemset/problem/2007/E
Iris and the Tree
1,800
[ "brute force", "data structures", "dfs and similar", "math", "trees" ]
Div. 2
3,000
256
Given a rooted tree with the root at vertex $1$. For any vertex $i$ ($1 < i \leq n$) in the tree, there is an edge connecting vertices $i$ and $p_i$ ($1 \leq p_i < i$), with a weight equal to $t_i$. Iris does not know the values of $t_i$, but she knows that $\displaystyle\sum_{i=2}^n t_i = w$ and each of the $t_i$ is a non- negative integer. The vertices of the tree are numbered in a special way: the numbers of the vertices in each subtree are consecutive integers. In other words, the vertices of the tree are numbered in the order of a depth-first search. ![](CDN_BASE_URL/274244c032854fe172d47861e2eb9c02) The tree in this picture satisfies the condition. For example, in the subtree of vertex $2$, the vertex numbers are $2, 3, 4, 5$, which are consecutive integers. ![](CDN_BASE_URL/83174231191d329be697a6e3f67b5eb3) The tree in this picture does not satisfy the condition, as in the subtree of vertex $2$, the vertex numbers $2$ and $4$ are not consecutive integers. We define $\operatorname{dist}(u, v)$ as the length of the simple path between vertices $u$ and $v$ in the tree. Next, there will be $n - 1$ events: * Iris is given integers $x$ and $y$, indicating that $t_x = y$. After each event, Iris wants to know the maximum possible value of $\operatorname{dist}(i, i \bmod n + 1)$ independently for each $i$ ($1\le i\le n$). She only needs to know the sum of these $n$ values. Please help Iris quickly get the answers. Note that when calculating the maximum possible values of $\operatorname{dist}(i, i \bmod n + 1)$ and $\operatorname{dist}(j, j \bmod n + 1)$ for $i \ne j$, the unknown edge weights may be different.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 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 $w$ ($2 \le n \le 2 \cdot 10^5$, $0 \leq w \leq 10^{12}$) β€” the number of vertices in the tree and the sum of the edge weights. The second line of each test case contains $n - 1$ integers $p_2, p_3, \ldots, p_n$ ($1 \leq p_i < i$) β€” the description of the edges of the tree. Then follow $n-1$ lines indicating the events. Each line contains two integers $x$ and $y$ ($2 \leq x \leq n$, $0 \leq y \leq w$), indicating that $t_x = y$. It is guaranteed that all $x$ in the events are distinct. It is also guaranteed that the sum of all $y$ equals $w$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output one line containing $n-1$ integers, each representing the answer after each event.
[ [ "4\n2 1000000000000\n1\n2 1000000000000\n4 9\n1 1 1\n2 2\n4 4\n3 3\n6 100\n1 2 3 2 1\n6 17\n3 32\n2 4\n4 26\n5 21\n10 511\n1 2 2 4 2 1 1 8 8\n3 2\n6 16\n10 256\n9 128\n2 1\n5 8\n8 64\n4 4\n7 32", "2000000000000\n25 18 18\n449 302 247 200 200\n4585 4473 2681 1567 1454 1322 1094 1022 1022" ] ]
In the first test case, $\operatorname{dist}(1, 2) = \operatorname{dist}(2, 1) = t_2 = w = 10^{12}$, so $\operatorname{dist}(1, 2) + \operatorname{dist}(2, 1) = 2 \cdot 10^{12}$. In the second test case, the tree after Iris found out all $t_x$ is shown below: ![](CDN_BASE_URL/2f461ea7db7bb0cda4536a2a78190c2f) $\operatorname{dist}(1, 2) = t_2$, $\operatorname{dist}(2, 3) = t_2 + t_3$, $\operatorname{dist}(3, 4) = t_3 + t_4$, $\operatorname{dist}(4, 1) = t_4$. After the first event, she found out that $t_2 = 2$, so $\operatorname{dist}(1, 2) = 2$. At the same time: * $\operatorname{dist}(2, 3)$ is maximized if $t_3 = 7$, $t_4 = 0$. Then $\operatorname{dist}(2, 3) = 9$. * $\operatorname{dist}(3, 4)$ and $\operatorname{dist}(4, 1)$ are maximized if $t_3 = 0$, $t_4 = 7$. Then $\operatorname{dist}(3, 4) = \operatorname{dist}(4, 1) = 7$. Thus, the answer is $2 + 9 + 7 + 7 = 25$. After the second event, she found out that $t_4 = 4$, then $t_3 = w - t_2 - t_4 = 4$. $\operatorname{dist}(1, 2) = 2$, $\operatorname{dist}(2, 3) = 2 + 3 = 5$, $\operatorname{dist}(3, 4) = 3 + 4 = 7$, $\operatorname{dist}(4, 1) = 4$. Thus, the answer is $2 + 5 + 7 + 4 = 18$.
2007F
https://codeforces.com/problemset/problem/2007/F
Eri and Expanded Sets
2,300
[ "binary search", "data structures", "number theory", "two pointers" ]
Div. 2
3,000
512
Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers $x\neq y$ from the set such that their average $\frac{x+y}2$ is still a positive integer and isn't contained in the set, and add it to the set. The integers $x$ and $y$ remain in the set. Let's call the set of integers consecutive if, after the elements are sorted, the difference between any pair of adjacent elements is $1$. For example, sets $\\{2\\}$, $\\{2, 5, 4, 3\\}$, $\\{5, 6, 8, 7\\}$ are consecutive, while $\\{2, 4, 5, 6\\}$, $\\{9, 7\\}$ are not. Eri likes consecutive sets. Suppose there is an array $b$, then Eri puts all elements in $b$ into the set. If after a finite number of operations described above, the set can become consecutive, the array $b$ will be called brilliant. Note that if the same integer appears in the array multiple times, we only put it into the set once, as a set always contains distinct positive integers. Eri has an array $a$ of $n$ positive integers. Please help him to count the number of pairs of integers $(l,r)$ such that $1 \leq l \leq r \leq n$ and the subarray $a_l, a_{l+1}, \ldots, a_r$ is brilliant.
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 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 4 \cdot 10^5$) β€” length of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \ldots a_n$ ($1 \leq a_i \leq 10^9$) β€” the elements of the array $a$. It is guaranteed that the sum of $n$ over all test cases doesn't exceed $4 \cdot 10^5$.
For each test case, output a single integer β€” the number of brilliant subarrays.
[ [ "6\n2\n2 2\n6\n1 3 6 10 15 21\n5\n6 30 18 36 9\n1\n1000000000\n6\n1 1 4 5 1 4\n12\n70 130 90 90 90 108 612 500 451 171 193 193", "3\n18\n5\n1\n18\n53" ] ]
In the first test case, the array $a = [2, 2]$ has $3$ subarrays: $[2]$, $[2]$, and $[2, 2]$. For all of them, the set only contains a single integer $2$, therefore it's always consecutive. All these subarrays are brilliant, so the answer is $3$. In the second test case, let's consider the subarray $[3, 6, 10]$. We can do operations as follows: $$\\{3,6,10\\} \xrightarrow{x=6,y=10} \\{3,6,8,10\\} \xrightarrow{x=6,y=8} \\{3,6,7,8,10\\} \xrightarrow{x=3,y=7} \\{3,5,6,7,8,10\\}$$ $$\xrightarrow{x=3,y=5} \\{3,4,5,6,7,8,10\\} \xrightarrow{x=8,y=10} \\{3,4,5,6,7,8,9,10\\}$$ $\\{3,4,5,6,7,8,9,10\\}$ is a consecutive set, so the subarray $[3, 6, 10]$ is brilliant.
2008A
https://codeforces.com/problemset/problem/2008/A
Sakurako's Exam
800
[ "brute force", "constructive algorithms", "greedy", "math" ]
Div. 3
1,000
256
Today, Sakurako has a math exam. The teacher gave the array, consisting of $a$ ones and $b$ twos. In an array, Sakurako must place either a '+' or a '-' in front of each element so that the sum of all elements in the array equals $0$. Sakurako is not sure if it is possible to solve this problem, so determine whether there is a way to assign signs such that the sum of all elements in the array equals $0$.
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 two integers $a$ and $b$ ($0\le a,b<10$) β€” the number of '1's and the number of '2's in the array.
For each test case, output "Yes" if you can make the sum of the entire array equal to $0$, and "No" otherwise. You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
[ [ "5\n0 1\n0 3\n2 0\n2 3\n3 1", "NO\nNO\nYES\nYES\nNO" ] ]
1. $a=0$, $b=1$: This means the array is $[2]$ β€” it is impossible to add the signs '+' or '-' to get $0$ as a result; 2. $a=0$, $b=3$: This means the array is $[2, 2, 2]$ β€” it is impossible to add the signs '+' or '-' to get $0$ as a result; 3. $a=2$, $b=0$: This means the array is $[1, 1]$ β€” it is possible to add the signs '+' or '-' to get $0$ as a result ($+1-1=0$); 4. $a=2$, $b=3$: This means the array is $[1, 1, 2, 2, 2]$ β€” it is possible to add the signs '+' or '-' to get $0$ as a result ($+1+1-2-2+2=0$);
2008B
https://codeforces.com/problemset/problem/2008/B
Square or Not
800
[ "brute force", "math", "strings" ]
Div. 3
2,000
256
A beautiful binary matrix is a matrix that has ones on its edges and zeros inside. ![](CDN_BASE_URL/5df31946486165887b31c82158ed558d) Examples of four beautiful binary matrices. Today, Sakurako was playing with a beautiful binary matrix of size $r \times c$ and created a binary string $s$ by writing down all the rows of the matrix, starting from the first and ending with the $r$-th. More formally, the element from the matrix in the $i$-th row and $j$-th column corresponds to the $((i-1)*c+j)$-th element of the string. You need to check whether the beautiful matrix from which the string $s$ was obtained could be squared. In other words, you need to check whether the string $s$ could have been build from a square beautiful binary matrix (i.e., one where $r=c$).
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 a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the length of the string. The second line of each test case contains the string $s$ of length $n$. The string is always the result of writing out the strings of a beautiful matrix. It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
Print "Yes", if the original matrix could have been square, and "No" otherwise.
[ [ "5\n2\n11\n4\n1111\n9\n111101111\n9\n111111111\n12\n111110011111", "No\nYes\nYes\nNo\nNo" ] ]
For the second test case, string 1111 can be obtained from the matrix: $1$| $1$ ---|--- $1$| $1$ For the third test case, string 111101111 can be obtained from the matrix: $1$| $1$| $1$ ---|---|--- $1$| $0$| $1$ $1$| $1$| $1$ There is no square matrix in the fourth case, such that the string can be obtained from it.
2008C
https://codeforces.com/problemset/problem/2008/C
Longest Good Array
800
[ "binary search", "brute force", "math" ]
Div. 3
2,000
256
Today, Sakurako was studying arrays. An array $a$ of length $n$ is considered good if and only if: * the array $a$ is increasing, meaning $a_{i - 1} < a_i$ for all $2 \le i \le n$; * the differences between adjacent elements are increasing, meaning $a_i - a_{i-1} < a_{i+1} - a_i$ for all $2 \le i < n$. Sakurako has come up with boundaries $l$ and $r$ and wants to construct a good array of maximum length, where $l \le a_i \le r$ for all $a_i$. Help Sakurako find the maximum length of a good array for the given $l$ and $r$.
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The only line of each test case contains two integers $l$ and $r$ ($1\le l\le r\le 10^9$).
For each test case, output a single integer β€” the length of the longest good array Sakurako can form given $l$ and $r$.
[ [ "5\n1 2\n1 5\n2 2\n10 20\n1 1000000000", "2\n3\n1\n5\n44721" ] ]
For $l=1$ and $r=5$, one possible array could be $(1,2,5)$. It can be proven that an array of length $4$ does not exist for the given $l$ and $r$. For $l=2$ and $r=2$, the only possible array is $(2)$. For $l=10$ and $r=20$, the only possible array is $(10,11,13,16,20)$.
2008D
https://codeforces.com/problemset/problem/2008/D
Sakurako's Hobby
1,100
[ "dp", "dsu", "graphs", "math" ]
Div. 3
2,000
256
For a certain permutation $p$$^{\text{βˆ—}}$ Sakurako calls an integer $j$ reachable from an integer $i$ if it is possible to make $i$ equal to $j$ by assigning $i=p_i$ a certain number of times. If $p=[3,5,6,1,2,4]$, then, for example, $4$ is reachable from $1$, because: $i=1$ $\rightarrow$ $i=p_1=3$ $\rightarrow$ $i=p_3=6$ $\rightarrow$ $i=p_6=4$. Now $i=4$, so $4$ is reachable from $1$. Each number in the permutation is colored either black or white. Sakurako defines the function $F(i)$ as the number of black integers that are reachable from $i$. Sakurako is interested in $F(i)$ for each $1\le i\le n$, but calculating all values becomes very difficult, so she asks you, as her good friend, to compute this. $^{\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 (the number $2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$, but the array contains $4$).
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 a single integer $n$ ($1\le n\le 2\cdot 10^5$) β€” the number of elements in the array. The second line of each test case contains $n$ integers $p_1, p_2, \dots, p_n$ ($1\le p_i\le n$) β€” the elements of the permutation. The third line of each test case contains a string $s$ of length $n$, consisting of '0' and '1'. If $s_i=0$, then the number $p_i$ is colored black; if $s_i=1$, then the number $p_i$ is colored white. It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$.
For each test case, output $n$ integers $F(1), F(2), \dots, F(n)$.
[ [ "5\n1\n1\n0\n5\n1 2 4 5 3\n10101\n5\n5 4 1 3 2\n10011\n6\n3 5 6 1 2 4\n010000\n6\n1 2 3 4 5 6\n100110", "1 \n0 1 1 1 1 \n2 2 2 2 2 \n4 1 4 4 1 4 \n0 1 1 0 0 1" ] ]
2008E
https://codeforces.com/problemset/problem/2008/E
Alternating String
1,500
[ "brute force", "data structures", "dp", "greedy", "implementation", "strings" ]
Div. 3
2,000
256
Sakurako really loves alternating strings. She calls a string $s$ of lowercase Latin letters an alternating string if characters in the even positions are the same, if characters in the odd positions are the same, and the length of the string is even. For example, the strings 'abab' and 'gg' are alternating, while the strings 'aba' and 'ggwp' are not. As a good friend, you decided to gift such a string, but you couldn't find one. Luckily, you can perform two types of operations on the string: 1. Choose an index $i$ and delete the $i$-th character from the string, which will reduce the length of the string by $1$. This type of operation can be performed no more than $1$ time; 2. Choose an index $i$ and replace $s_i$ with any other letter. Since you are in a hurry, you need to determine the minimum number of operations required to make the string an alternating one.
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 a single number $n$ ($1 \le n\le 2\cdot 10^5$) β€” the length of the string. The second line of each test case contains a string $s$, consisting of lowercase Latin letters. It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
For each test case, output a single integer β€” the minimum number of operations required to turn the string $s$ into an alternating one.
[ [ "10\n1\na\n2\nca\n3\naab\n5\nababa\n6\nacdada\n9\nejibmyyju\n6\nbbccbc\n6\nabacba\n5\nbcbca\n5\ndcbdb", "1\n0\n1\n1\n2\n6\n2\n3\n1\n1" ] ]
For the string ababa, you can delete the first character to get baba, which is an alternating string. For the string acdada, you can change the first two characters to get dadada, which is an alternating string.
2008F
https://codeforces.com/problemset/problem/2008/F
Sakurako's Box
1,400
[ "combinatorics", "math", "number theory" ]
Div. 3
2,000
256
Sakurako has a box with $n$ balls. Each ball has it's value. She wants to bet with her friend that if the friend randomly picks two balls from the box (it could be two distinct balls, but they may have the same value), the product of their values will be the same as the number that Sakurako guessed. Since Sakurako has a PhD in probability, she knows that the best number to pick is [the expected value](http://tiny.cc/matozh_en), but she forgot how to calculate it. Help Sakurako and find the expected value of the product of two elements from the array. It can be shown that the expected value has the form $\frac{P}{Q}$, where $P$ and $Q$ are non-negative integers, and $Q \ne 0$. Report the value of $P \cdot Q^{-1}(\bmod 10^9+7)$.
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 a single integer $n$ ($2\le n\le 2\cdot 10^5$) β€” the number of elements in the array. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0\le a_i\le 10^9$) β€” the elements of the array. It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$.
For each test case, output the value of $P \cdot Q^{-1}(\bmod 10^9+7)$.
[ [ "3\n3\n3 2 3\n4\n2 2 2 4\n5\n1 2 3 4 5", "7\n6\n500000012" ] ]
For the first test, Sakurako's friend can pick these pairs of balls: $(a_1,a_2)$, $(a_1,a_3)$, $(a_2,a_3)$. Their products equal to $3\cdot 2=6$ , $3\cdot 3=9$ , $3\cdot 2=6$ respectively, so the expected value is $\frac{6+9+6}{3}=7$. For the second test, Sakurako's friend can pick these pairs of balls: $(a_1,a_2)$, $(a_1,a_3)$, $(a_1,a_4)$, $(a_2,a_3)$, $(a_2,a_4)$, $(a_3,a_4)$. Their products equal to $2\cdot 2=4$ , $2\cdot 2=4$ , $2\cdot 4=8$, $2\cdot 2=4$, $2\cdot 4=8$, $2\cdot 4=8$ respectively, so the expected value is $\frac{4+4+8+4+8+8}{6}=\frac{36}{6}=6$.
2008G
https://codeforces.com/problemset/problem/2008/G
Sakurako's Task
1,800
[ "binary search", "greedy", "math", "number theory" ]
Div. 3
2,000
256
Sakurako has prepared a task for you: She gives you an array of $n$ integers and allows you to choose $i$ and $j$ such that $i \neq j$ and $a_i \ge a_j$, and then assign $a_i = a_i - a_j$ or $a_i = a_i + a_j$. You can perform this operation any number of times for any $i$ and $j$, as long as they satisfy the conditions. Sakurako asks you what is the maximum possible value of $mex_k$$^{\text{βˆ—}}$ of the array after any number of operations. $^{\text{βˆ—}}$$mex_k$ is the $k$-th non-negative integer that is absent in the array. For example, $mex_1(\\{1,2,3 \\})=0$, since $0$ is the first element that is not in the array, and $mex_2(\\{0,2,4 \\})=3$, since $3$ is the second element that is not in the array.
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 n\le 2\cdot 10^5,1\le k\le 10^9$) β€” the number of elements in the array and the value $k$ for $mex_k$. The second line of each test case contains $n$ integers $a_1, a_2, \dots,a_n$ ($1\le a_i\le 10^9$) β€” the elements of the array. It is guaranteed that the sum of $n$ across all test cases does not exceed $2\cdot 10^5$.
For each test case, output the maximum $mex_k$ that can be achieved through the operations.
[ [ "6\n1 3\n3\n2 10\n1 1\n3 1\n1 2 3\n3 2\n1 2 4\n4 5\n2 2 2 16\n4 5\n2 2 2 3", "2\n11\n3\n4\n8\n8" ] ]
2008H
https://codeforces.com/problemset/problem/2008/H
Sakurako's Test
2,100
[ "binary search", "brute force", "greedy", "math", "number theory" ]
Div. 3
1,000
256
Sakurako will soon take a test. The test can be described as an array of integers $n$ and a task on it: Given an integer $x$, Sakurako can perform the following operation any number of times: * Choose an integer $i$ ($1\le i\le n$) such that $a_i\ge x$; * Change the value of $a_i$ to $a_i-x$. Using this operation any number of times, she must find the minimum possible median$^{\text{βˆ—}}$ of the array $a$. Sakurako knows the array but does not know the integer $x$. Someone let it slip that one of the $q$ values of $x$ will be in the next test, so Sakurako is asking you what the answer is for each such $x$. $^{\text{βˆ—}}$The median of an array of length $n$ is the element that stands in the middle of the sorted array (at the $\frac{n+2}{2}$-th position for even $n$, and at the $\frac{n+1}{2}$-th for odd)
The first line contains one 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 $q$ ($1\le n,q\le 10^5$) β€” the number of elements in the array and the number of queries. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1\le a_i\le n$) β€” the elements of the array. The following $q$ lines each contain one integer $x$ ($1\le x\le n$). It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. The same guarantee applies to the sum of $q$ across all test cases.
For each test case, output $q$ integers β€” the answer for each query.
[ [ "2\n5 5\n1 2 3 4 5\n1\n2\n3\n4\n5\n6 3\n1 2 6 4 1 3\n2\n1\n5", "0 1 1 1 2 \n1 0 2" ] ]
2013A
https://codeforces.com/problemset/problem/2013/A
Zhan's Blender
800
[ "constructive algorithms", "math" ]
Div. 2
1,000
256
Today, a club fair was held at "NSPhM". In order to advertise his pastry club, Zhan decided to demonstrate the power of his blender. To demonstrate the power of his blender, Zhan has $n$ fruits. The blender can mix up to $x$ fruits per second. In each second, Zhan can put up to $y$ fruits into the blender. After that, the blender will blend $\min(x, c)$ fruits, where $c$ is the number of fruits inside the blender. After blending, blended fruits are removed from the blender. Help Zhan determine the minimum amount of time required for Zhan to blend all fruits.
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 one integer $n$ ($0 \le n \le 10^9$) β€” the number of fruits Zhan has. The second line of each test case contains two integers $x$ and $y$ ($1 \le x, y \le 10^9$) β€” the number of fruits the blender can blend per second and the number of fruits Zhan can put into the blender per second.
For each testcase, output a single integer β€” the minimum number of seconds to blend all fruits.
[ [ "5\n5\n3 4\n3\n1 2\n6\n4 3\n100\n4 3\n9\n3 3", "2\n3\n2\n34\n3" ] ]
In the first example, you can first put $2$ fruits in the blender. After that, the blender will mix these $2$ fruits, and in the end, there will be $0$ fruits left in the blender. Then you can put $3$ fruits into the blender, after which the blender will mix these $3$ fruits. In the second example, you can put $1$ fruit into the blender $3$ times. In the third example, you can first put $3$ fruits into the blender, then add another $3$ fruits.
2013B
https://codeforces.com/problemset/problem/2013/B
Battle for Survive
900
[ "constructive algorithms", "greedy", "math" ]
Div. 2
1,000
256
Eralim, being the mafia boss, manages a group of $n$ fighters. Fighter $i$ has a rating of $a_i$. Eralim arranges a tournament of $n - 1$ battles, in each of which two not yet eliminated fighters $i$ and $j$ ($1 \le i < j \le n$) are chosen, and as a result of the battle, fighter $i$ is eliminated from the tournament, and the rating of fighter $j$ is reduced by the rating of fighter $i$. That is, $a_j$ is decreased by $a_i$. Note that fighter $j$'s rating can become negative. The fighters indexes do not change. Eralim wants to know what maximum rating the last remaining fighter can preserve if he chooses the battles optimally.
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 fighters. 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 ratings of the fighters. The sum of $n$ over all testcases does not exceed $2 \cdot 10^5$.
For each testcase, output a single integer β€” the maximum rating that the last remaining fighter can preserve.
[ [ "5\n2\n2 1\n3\n2 2 8\n4\n1 2 4 3\n5\n1 2 3 4 5\n5\n3 2 4 5 4", "-1\n8\n2\n7\n8" ] ]
In the first example, you can arrange a fight between fighters with indices $1$ and $2$, where the fighter with index $2$ will win. The rating of the last fighter, that is, the fighter with index $2$, will be $1 - 2 = -1$. In the second example, you can first conduct a fight between fighters with indices $1$ and $2$, where the fighter with index $2$ will win, and then conduct a fight between fighters with indices $2$ and $3$, where the fighter with index $3$ will win. The rating of the fighter with index $2$ after the first fight will be $2 - 2 = 0$. The rating of the fighter with index $3$ after the second fight will be $8 - 0 = 8$.
2013C
https://codeforces.com/problemset/problem/2013/C
Password Cracking
1,400
[ "constructive algorithms", "interactive", "strings" ]
Div. 2
2,000
256
Dimash learned that Mansur wrote something very unpleasant about him to a friend, so he decided to find out his password at all costs and discover what exactly he wrote. Believing in the strength of his password, Mansur stated that his password β€” is a binary string of length $n$. He is also ready to answer Dimash's questions of the following type: Dimash says a binary string $t$, and Mansur replies whether it is true that $t$ is a substring of his password. Help Dimash find out the password in no more than $2n$ operations; otherwise, Mansur will understand the trick and stop communicating with him.
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.
At the beginning of each test case, first read $n$ ($1 \le n \le 100$) β€” the size of the binary string. Then proceed to guessing it. To guess each string $s$, you can make no more than $2n$ queries of the following type: * "? t", where $t$ is a binary string such that ($1 \le |t| \le n$). In response to this query, you will receive $1$ if $t$ is a substring of $s$, and $0$ otherwise. Once you receive the answer, output a single string in the following format: * "! s", where $s$ is a binary string of size $n$. After that, proceed to solve the next test case. If you make an incorrect attempt or exceed the limit of $2n$ attempts, you will receive $-1$ instead of an answer and get the verdict Wrong answer. In this case, your program should terminate immediately to avoid undefined verdicts. After outputting the queries, do not forget to output a newline character and flush the output buffer. Otherwise, you will receive the verdict Solution "hung". 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 use hacks, use the following format of tests: The first line should contain a single integer $t$ ($1\le t\le 100$) β€” the number of test cases. The first line of each test case should contain a single number $n$ ($1 \le n \le 100$) β€” the length of the string. The second line should contain a binary string of size $n$.
[ [ "4\n3\n\n0\n\n0\n\n1\n\n4\n\n4\n\n2", "? 00\n\n? 000\n\n? 010\n\n! 010\n\n! 1100\n\n! 0110\n\n! 10" ] ]
In the first example, the string $010$ is given. Therefore, the answers to the queries are as follows: "? 00" $00$ is not a substring of $010$, so the answer is $0$. "? 000" $000$ is not a substring, so the answer is $0$. "? 010" $010$ is a substring, so the answer is $1$. In the second example, the string is $1100$, in the third $0110$, and in the fourth $10$.
2013D
https://codeforces.com/problemset/problem/2013/D
Minimize the Difference
1,900
[ "binary search", "greedy" ]
Div. 2
2,000
256
Zhan, tired after the contest, gave the only task that he did not solve during the contest to his friend, Sungat. However, he could not solve it either, so we ask you to try to solve this problem. You are given an array $a_1, a_2, \ldots, a_n$ of length $n$. We can perform any number (possibly, zero) of operations on the array. In one operation, we choose a position $i$ ($1 \leq i \leq n - 1$) and perform the following action: * $a_i := a_i - 1$, and $a_{i+1} := a_{i+1} + 1$. Find the minimum possible value of $\max(a_1, a_2, \ldots, a_n) - \min(a_1, a_2, \ldots, a_n)$.
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 an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$). The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^{12}$). The sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output a single integer: the minimum possible value of $\max(a_1, a_2, \ldots, a_n) - \min(a_1, a_2, \ldots, a_n)$.
[ [ "5\n1\n1\n3\n1 2 3\n4\n4 1 2 3\n4\n4 2 3 1\n5\n5 14 4 10 2", "0\n2\n1\n1\n3" ] ]
In the third testcase, you can perform the operation twice with $i = 1$. After that, the array is $a = [2, 3, 2, 3]$, and $\max(2, 3, 2, 3) - \min(2, 3, 2, 3) = 3 - 2 = 1$.
2013E
https://codeforces.com/problemset/problem/2013/E
Prefix GCD
2,200
[ "brute force", "dp", "greedy", "math", "number theory" ]
Div. 2
2,000
256
Since Mansur is tired of making legends, there will be no legends for this task. You are given an array of positive integer numbers $a_1, a_2, \ldots, a_n$. The elements of the array can be rearranged in any order. You need to find the smallest possible value of the expression $$\gcd(a_1) + \gcd(a_1, a_2) + \ldots + \gcd(a_1, a_2, \ldots, a_n),$$ where $\gcd(a_1, a_2, \ldots, a_n)$ denotes the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of $a_1, a_2, \ldots, a_n$.
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 number $n$ ($1 \le n \le 10^5$) β€” the size of the array. The second line of each test case contains $n$ numbers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^5$) β€” the initial array. The sum of $n$ over all test cases does not exceed $10^5$. The sum of $\max(a_1, a_2, \ldots, a_n)$ over all test cases does not exceed $10^5$.
For each test case, output a single number on a separate line β€” the answer to the problem.
[ [ "5\n3\n4 2 2\n2\n6 3\n3\n10 15 6\n5\n6 42 12 52 20\n4\n42 154 231 66", "6\n6\n9\n14\n51" ] ]
In the first test case, the elements can be rearranged as follows: $[2, 4, 2]$. Then the answer will be $\gcd(2) + \gcd(2, 4) + \gcd(2, 4, 2) = 2 + 2 + 2 = 6$. In the third test case, the elements can be rearranged as follows: $[6, 10, 15]$. Then the answer will be $\gcd(6) + \gcd(6, 10) + \gcd(6, 10, 15) = 6 + 2 + 1 = 9$.
2013F1
https://codeforces.com/problemset/problem/2013/F1
Game in Tree (Easy Version)
2,700
[ "binary search", "brute force", "data structures", "dp", "games", "greedy", "implementation", "trees" ]
Div. 2
4,000
256
This is the easy version of the problem. In this version, $\mathbf{u = v}$. You can make hacks only if both versions of the problem are solved. Alice and Bob are playing a fun game on a tree. This game is played on a tree with $n$ vertices, numbered from $1$ to $n$. Recall that a tree with $n$ vertices is an undirected connected graph with $n - 1$ edges. Alice and Bob take turns, with Alice going first. Each player starts at some vertex. On their turn, a player must move from the current vertex to a neighboring vertex that has not yet been visited by anyone. The first player who cannot make a move loses. You are given two vertices $u$ and $v$. Represent the simple path from vertex $u$ to $v$ as an array $p_1, p_2, p_3, \ldots, p_m$, where $p_1 = u$, $p_m = v$, and there is an edge between $p_i$ and $p_{i + 1}$ for all $i$ ($1 \le i < m$). You need to determine the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_j$ for each $j$ (where $1 \le j \le m$).
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. Each of the following $n - 1$ lines contains two integers $a$ and $b$ ($1 \le a, b \le n$), denoting an undirected edge between vertices $a$ and $b$. It is guaranteed that these edges form a tree. The last line of each test case contains two integers $u$ and $v$ ($2 \le u, v \le n$, $\mathbf{u = v}$). It is guaranteed that the path from $u$ to $v$ does not pass through vertex $1$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output $m$ lines. In the $i$-th line, print the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_i$. Print "Alice" (without quotes) if Alice wins, or "Bob" (without quotes) otherwise.
[ [ "3\n3\n1 2\n2 3\n2 2\n3\n1 2\n2 3\n3 3\n6\n1 2\n1 3\n2 4\n2 5\n1 6\n4 4", "Bob\nAlice\nAlice" ] ]
![](CDN_BASE_URL/6df79b98ba203b10924734a14ee457df) Tree from the first and second examples. In the first test case, the path will be ($2,2$). Bob starts at vertex $2$, Alice will not be able to move anywhere on her first turn and will lose. In the second test case, the path will be ($3,3$). Bob starts at vertex $3$, Alice will move to vertex $2$, and Bob will have no remaining vertices to visit and will lose.
2013F2
https://codeforces.com/problemset/problem/2013/F2
Game in Tree (Hard Version)
3,500
[ "binary search", "data structures", "trees" ]
Div. 2
4,000
256
This is the hard version of the problem. In this version, it is not guaranteed that $u = v$. You can make hacks only if both versions of the problem are solved. Alice and Bob are playing a fun game on a tree. This game is played on a tree with $n$ vertices, numbered from $1$ to $n$. Recall that a tree with $n$ vertices is an undirected connected graph with $n - 1$ edges. Alice and Bob take turns, with Alice going first. Each player starts at some vertex. On their turn, a player must move from the current vertex to a neighboring vertex that has not yet been visited by anyone. The first player who cannot make a move loses. You are given two vertices $u$ and $v$. Represent the simple path from vertex $u$ to $v$ as an array $p_1, p_2, p_3, \ldots, p_m$, where $p_1 = u$, $p_m = v$, and there is an edge between $p_i$ and $p_{i + 1}$ for all $i$ ($1 \le i < m$). You need to determine the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_j$ for each $j$ (where $1 \le j \le m$).
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. Each of the following $n - 1$ lines contains two integers $a$ and $b$ ($1 \le a, b \le n$), denoting an undirected edge between vertices $a$ and $b$. It is guaranteed that these edges form a tree. The last line of each test case contains two integers $u$ and $v$ ($2 \le u, v \le n$). It is guaranteed that the path from $u$ to $v$ does not pass through vertex $1$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output $m$ lines. In the $i$-th line, print the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_i$. Print "Alice" (without quotes) if Alice wins, or "Bob" (without quotes) otherwise.
[ [ "3\n3\n1 2\n2 3\n2 3\n6\n1 2\n1 3\n2 4\n2 5\n1 6\n4 5\n4\n1 2\n1 3\n2 4\n2 4", "Bob\nAlice\nAlice\nBob\nAlice\nBob\nAlice" ] ]
![](CDN_BASE_URL/45f5cd537988c3a64939e74c3b13efab) Tree from the first example. In the first test case, the path will be ($2,3$). If Bob starts at vertex $2$, Alice will not be able to move anywhere on her first turn and will lose. However, if Bob starts at vertex $3$, Alice will move to vertex $2$, and Bob will have no remaining vertices to visit and will lose.
2014A
https://codeforces.com/problemset/problem/2014/A
Robin Helps
800
[ "greedy", "implementation" ]
Div. 3
1,000
256
There is a little bit of the outlaw in everyone, and a little bit of the hero too. The heroic outlaw Robin Hood is famous for taking from the rich and giving to the poor. Robin encounters $n$ people starting from the $1$-st and ending with the $n$-th. The $i$-th person has $a_i$ gold. If $a_i \ge k$, Robin will take all $a_i$ gold, and if $a_i=0$, Robin will give $1$ gold if he has any. Robin starts with $0$ gold. Find out how many people Robin gives gold to.
The first line of the input contains a single integer $t$ ($1\leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$, $k$ ($1 \le n \le 50, 1 \le k \le 100$) β€” the number of people and the threshold at which Robin Hood takes the gold. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 100$) β€” the gold of each person.
For each test case, output a single integer, the number of people that will get gold from Robin Hood.
[ [ "4\n2 2\n2 0\n3 2\n3 0 0\n6 2\n0 3 0 0 0 0\n2 5\n5 4", "1\n2\n3\n0" ] ]
In the first test case, Robin takes $2$ gold from the first person and gives a gold to the second person. In the second test case, Robin takes $3$ gold and gives $1$ gold to each of the next $2$ people. In the third test case, Robin takes $3$ gold and so only gives gold to $3$ other people.
2014B
https://codeforces.com/problemset/problem/2014/B
Robin Hood and the Major Oak
800
[ "math" ]
Div. 3
1,000
256
In Sherwood, the trees are our shelter, and we are all children of the forest. The Major Oak in Sherwood is known for its majestic foliage, which provided shelter to Robin Hood and his band of merry men and women. The Major Oak grows $i^i$ new leaves in the $i$-th year. It starts with $1$ leaf in year $1$. Leaves last for $k$ years on the tree. In other words, leaves grown in year $i$ last between years $i$ and $i+k-1$ inclusive. Robin considers even numbers lucky. Help Robin determine whether the Major Oak will have an even number of leaves in year $n$.
The first line of the input contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Each test case consists of two integers $n$, $k$ ($1 \le n \le 10^9$, $1 \le k \le n$) β€” the requested year and the number of years during which the leaves remain.
For each test case, output one line, "YES" if in year $n$ the Major Oak will have an even number of leaves and "NO" 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.
[ [ "5\n1 1\n2 1\n2 2\n3 2\n4 4", "NO\nYES\nNO\nNO\nYES" ] ]
In the first test case, there is only $1$ leaf. In the second test case, $k=1$, so in the $2$-nd year there will be $2^2=4$ leaves. In the third test case, $k=2$, so in the $2$-nd year there will be $1+2^2=5$ leaves. In the fourth test case, $k=2$, so in the $3$-rd year there will be $2^2 + 3^3 = 4 + 27 = 31$ leaves.
2014C
https://codeforces.com/problemset/problem/2014/C
Robin Hood in Town
1,100
[ "binary search", "greedy", "math" ]
Div. 3
2,000
256
In Sherwood, we judge a man not by his wealth, but by his merit. Look around, the rich are getting richer, and the poor are getting poorer. We need to take from the rich and give to the poor. We need Robin Hood! There are $n$ people living in the town. Just now, the wealth of the $i$-th person was $a_i$ gold. But guess what? The richest person has found an extra pot of gold! More formally, find an $a_j=max(a_1, a_2, \dots, a_n)$, change $a_j$ to $a_j+x$, where $x$ is a non-negative integer number of gold found in the pot. If there are multiple maxima, it can be any one of them. A person is unhappy if their wealth is strictly less than half of the average wealth$^{\text{βˆ—}}$. If strictly more than half of the total population $n$ are unhappy, Robin Hood will appear by popular demand. Determine the minimum value of $x$ for Robin Hood to appear, or output $-1$ if it is impossible. $^{\text{βˆ—}}$The average wealth is defined as the total wealth divided by the total population $n$, that is, $\frac{\sum a_i}{n}$, the result is a real number.
The first line of input contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains an integer $n$ ($1 \le n \le 2\cdot10^5$) β€” the total population. 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 wealth of each person. It is guaranteed that the sum of $n$ across all test cases does not exceed $2 \cdot 10^5$.
For each test case, output one integer β€” the minimum number of gold that the richest person must find for Robin Hood to appear. If it is impossible, output $-1$ instead.
[ [ "6\n1\n2\n2\n2 19\n3\n1 3 20\n4\n1 2 3 4\n5\n1 2 3 4 5\n6\n1 2 1 1 1 25", "-1\n-1\n0\n15\n16\n0" ] ]
In the first test case, it is impossible for a single person to be unhappy. In the second test case, there is always $1$ happy person (the richest). In the third test case, no additional gold are required, so the answer is $0$. In the fourth test case, after adding $15$ gold, the average wealth becomes $\frac{25}{4}$, and half of this average is $\frac{25}{8}$, resulting in $3$ people being unhappy. In the fifth test case, after adding $16$ gold, the average wealth becomes $\frac{31}{5}$, resulting in $3$ people being unhappy.
2014D
https://codeforces.com/problemset/problem/2014/D
Robert Hood and Mrs Hood
1,400
[ "brute force", "data structures", "greedy", "sortings" ]
Div. 3
2,000
256
Impress thy brother, yet fret not thy mother. Robin's brother and mother are visiting, and Robin gets to choose the start day for each visitor. All days are numbered from $1$ to $n$. Visitors stay for $d$ continuous days, all of those $d$ days must be between day $1$ and $n$ inclusive. Robin has a total of $k$ risky 'jobs' planned. The $i$-th job takes place between days $l_i$ and $r_i$ inclusive, for $1 \le i \le k$. If a job takes place on any of the $d$ days, the visit overlaps with this job (the length of overlap is unimportant). Robin wants his brother's visit to overlap with the maximum number of distinct jobs, and his mother's the minimum. Find suitable start days for the visits of Robin's brother and mother. If there are multiple suitable days, choose the earliest one.
The first line of the input contains a single integer $t$ ($1\leq t \leq 10^4$) β€” the number of test cases. The first line of each test case consists of three integers $n$, $d$, $k$ ($1 \le n \le 10^5, 1 \le d, k \le n$) β€” the number of total days, duration of the visits, and the number of jobs. Then follow $k$ lines of each test case, each with two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le n$) β€” the start and end day of each job. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output two integers, the best starting days of Robin's brother and mother respectively. Both visits must fit between day $1$ and $n$ inclusive.
[ [ "6\n2 1 1\n1 2\n4 1 2\n1 2\n2 4\n7 2 3\n1 2\n1 3\n6 7\n5 1 2\n1 2\n3 5\n9 2 1\n2 8\n9 2 4\n7 9\n4 8\n1 3\n2 3", "1 1\n2 1\n1 4\n1 1\n1 1\n3 4" ] ]
In the first test case, the only job fills all $2$ days, both should visit on day $1$. In the second test case, day $2$ overlaps with $2$ jobs and day $1$ overlaps with only $1$. In the third test case, Robert visits for days $[1,2]$, Mrs. Hood visits for days $[4,5]$.
2014E
https://codeforces.com/problemset/problem/2014/E
Rendez-vous de Marian et Robin
1,800
[ "dfs and similar", "graphs", "shortest paths" ]
Div. 3
5,000
256
In the humble act of meeting, joy doth unfold like a flower in bloom. Absence makes the heart grow fonder. Marian sold her last ware at the Market at the same time Robin finished training at the Major Oak. They couldn't wait to meet, so they both start without delay. The travel network is represented as $n$ vertices numbered from $1$ to $n$ and $m$ edges. The $i$-th edge connects vertices $u_i$ and $v_i$, and takes $w_i$ seconds to travel (all $w_i$ are even). Marian starts at vertex $1$ (Market) and Robin starts at vertex $n$ (Major Oak). In addition, $h$ of the $n$ vertices each has a single horse available. Both Marian and Robin are capable riders, and could mount horses in no time (i.e. in $0$ seconds). Travel times are halved when riding. Once mounted, a horse lasts the remainder of the travel. Meeting must take place on a vertex (i.e. not on an edge). Either could choose to wait on any vertex. Output the earliest time Robin and Marian can meet. If vertices $1$ and $n$ are disconnected, output $-1$ as the meeting is cancelled.
The first line of the input contains a single integer $t$ ($1\leq t \leq 10^4$) β€” the number of test cases. The first line of each test case consists of three integers $n$, $m$, $h$ ($2 \le n \le 2 \cdot 10^5, \;1 \le m \le 2 \cdot 10^5, \; 1 \le h \le n$) β€” the number of vertices, the number of edges and the number of vertices that have a single horse. The second line of each test case contains $h$ distinct integers $a_1, a_2, \ldots, a_h$ ($1 \le a_i \le n$) β€” the vertices that have a single horse available. Then follow $m$ lines of each test case, each with three integers $u_i$, $v_i$, $w_i$ ($1\le u_i,v_i \le n, \; 2\le w_i \le 10^6$) β€” meaning that there is an edge between vertices $u_i$ and $v_i$ with travel cost $w_i$ seconds without a horse. There are no self loops or multiple edges. By good fortune, all $w_i$ are even integers. It is guaranteed that the sums of both $n$ and $m$ over all test cases do not exceed $2 \cdot 10^5$.
For each test case, output a single integer, the earliest time Robin and Marian can meet. If it is impossible for them to meet, output $-1$.
[ [ "6\n2 1 1\n1\n1 2 10\n3 1 2\n2 3\n1 2 10\n3 3 1\n2\n1 2 4\n1 3 10\n2 3 6\n4 3 2\n2 3\n1 2 10\n2 3 18\n3 4 16\n3 2 1\n2\n1 2 4\n1 3 16\n7 7 1\n3\n1 5 2\n2 6 12\n1 2 12\n6 4 8\n7 3 4\n6 3 4\n7 6 4", "5\n-1\n6\n19\n14\n12" ] ]
In the first test case, Marian rides from vertex $1$ to vertex $2$, Robin waits. In the second test case, vertices $1$ and $3$ are not connected. In the third test case, both Marian and Robin travel to vertex $2$ to meet. In the fourth test case, Marian travels to vertex $2$ without a horse, mounts the horse at vertex $2$ and rides to vertex $3$ to meet Robin. In the fifth test case, Marian travels to vertex $2$ without a horse, mounts the horse at vertex $2$ and rides back to vertex $1$ and then to vertex $3$. Robin waits.
2014F
https://codeforces.com/problemset/problem/2014/F
Sheriff's Defense
2,000
[ "dfs and similar", "dp", "greedy", "trees" ]
Div. 3
2,000
256
"Why, master," quoth Little John, taking the bags and weighing them in his hand, "here is the chink of gold." The folk hero Robin Hood has been troubling Sheriff of Nottingham greatly. Sheriff knows that Robin Hood is about to attack his camps and he wants to be prepared. Sheriff of Nottingham built the camps with strategy in mind and thus there are exactly $n$ camps numbered from $1$ to $n$ and $n-1$ trails, each connecting two camps. Any camp can be reached from any other camp. Each camp $i$ has initially $a_i$ gold. As it is now, all camps would be destroyed by Robin. Sheriff can strengthen a camp by subtracting exactly $c$ gold from each of its neighboring camps and use it to build better defenses for that camp. Strengthening a camp doesn't change its gold, only its neighbors' gold. A camp can have negative gold. After Robin Hood's attack, all camps that have been strengthened survive the attack, all others are destroyed. What's the maximum gold Sheriff can keep in his surviving camps after Robin Hood's attack if he strengthens his camps optimally? Camp $a$ is neighboring camp $b$ if and only if there exists a trail connecting $a$ and $b$. Only strengthened camps count towards the answer, as others are destroyed.
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Each test case begins with two integers $n$, $c$ ($1 \le n \le 2\cdot10^5, 1 \le c \le 10^9$) β€” the number of camps and the gold taken from each neighboring camp for strengthening. The second line of each test case contains $n$ integers $a_1,a_2,\dots,a_n$ ($-10^9 \le a_i \le 10^9$) β€” the initial gold of each camp. Then follow $n-1$ lines, each with integers $u$, $v$ ($1 \le u, v \le n$, $u \ne v$) β€” meaning that there is a trail between $u$ and $v$. The sum of $n$ over all test cases doesn't exceed $2\cdot10^5$. It is guaranteed that any camp is reachable from any other camp.
Output a single integer, the maximum gold Sheriff of Nottingham can keep in his surviving camps after Robin Hood's attack.
[ [ "5\n3 1\n2 3 1\n1 2\n2 3\n3 1\n3 6 3\n1 2\n2 3\n3 1\n-2 -3 -1\n1 2\n2 3\n6 1\n5 -4 3 6 7 3\n4 1\n5 1\n3 5\n3 6\n1 2\n8 1\n3 5 2 7 8 5 -3 -4\n7 3\n1 8\n4 3\n3 5\n7 6\n8 7\n2 1", "3\n8\n0\n17\n26" ] ]
In the first test case, it is optimal to strengthen the second base. The final gold at each base is $[1,3,0]$. In the second test case, it is optimal to strengthen all bases. The final gold at each base is $[2,4,2]$. In the third test case, it is optimal to not strengthen any base.
2014G
https://codeforces.com/problemset/problem/2014/G
Milky Days
2,200
[ "brute force", "data structures", "greedy", "implementation" ]
Div. 3
2,000
256
What is done is done, and the spoilt milk cannot be helped. Little John is as little as night is day β€” he was known to be a giant, at possibly $2.1$ metres tall. It has everything to do with his love for milk. His dairy diary has $n$ entries, showing that he acquired $a_i$ pints of fresh milk on day $d_i$. Milk declines in freshness with time and stays drinkable for a maximum of $k$ days. In other words, fresh milk acquired on day $d_i$ will be drinkable between days $d_i$ and $d_i+k-1$ inclusive. Every day, Little John drinks drinkable milk, up to a maximum of $m$ pints. In other words, if there are less than $m$ pints of milk, he will drink them all and not be satisfied; if there are at least $m$ pints of milk, he will drink exactly $m$ pints and be satisfied, and it's a milk satisfaction day. Little John always drinks the freshest drinkable milk first. Determine the number of milk satisfaction days for Little John.
The first line of the input contains a single integer $t$ ($1\leq t \leq 10^4$), the number of test cases. The first line of each test case consists of three integers $n$, $m$, $k$ ($1\le n$, $m$, $k \le 10^5$), the number of diary entries, the maximum pints needed for a milk satisfaction day, and the duration of milk's freshness. Then follow $n$ lines of each test case, each with two integers $d_i$ and $a_i$ ($1\le d_i$, $a_i \le 10^6$), the day on which the milk was acquired and the number of pints acquired. They are sorted in increasing values of $d_i$, and all values of $d_i$ are distinct. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output a single integer, the number of milk satisfaction days.
[ [ "6\n1 1 3\n1 5\n2 3 3\n1 5\n2 7\n4 5 2\n1 9\n2 6\n4 9\n5 6\n5 2 4\n4 7\n5 3\n7 1\n11 2\n12 1\n4 1 3\n5 10\n9 4\n14 8\n15 3\n5 5 5\n8 9\n10 7\n16 10\n21 5\n28 9", "3\n3\n4\n5\n10\n6" ] ]
In the first test case, $5$ pints of milk are good for $3$ days before spoiling. In the second test case, the following will happen: * On day $1$, he will receive $5$ pints of milk and drink $3$ of them (leaving $2$ pints from day $1$); * On day $2$, he will receive $7$ pints of milk and drink $3$ of them (leaving $2$ pints from day $1$ and $4$ pints from day $2$); * On day $3$, he will drink $3$ pints from day $2$ (leaving $2$ pints from day $1$ and $1$ pint from day $2$); * On day $4$, the milk acquired on day $1$ will spoil, and he will drink $1$ pint from day $2$ (no more milk is left).
2014H
https://codeforces.com/problemset/problem/2014/H
Robin Hood Archery
1,900
[ "data structures", "divide and conquer", "greedy", "hashing" ]
Div. 3
3,000
256
At such times archery was always the main sport of the day, for the Nottinghamshire yeomen were the best hand at the longbow in all merry England, but this year the Sheriff hesitated... Sheriff of Nottingham has organized a tournament in archery. It's the final round and Robin Hood is playing against Sheriff! There are $n$ targets in a row numbered from $1$ to $n$. When a player shoots target $i$, their score increases by $a_i$ and the target $i$ is destroyed. The game consists of turns and players alternate between whose turn it is. Robin Hood always starts the game, then Sheriff and so on. The game continues until all targets are destroyed. Both players start with score $0$. At the end of the game, the player with most score wins and the other player loses. If both players have the same score, it's a tie and no one wins or loses. In each turn, the player can shoot any target that wasn't shot before. Both play optimally to get the most score possible. Sheriff of Nottingham has a suspicion that he might lose the game! This cannot happen, you must help Sheriff. Sheriff will pose $q$ queries, each specifying $l$ and $r$. This means that the game would be played only with targets $l, l+1, \dots, r$, as others would be removed by Sheriff before the game starts. For each query $l$, $r$, determine whether the Sheriff can not lose the game when only considering the targets $l, l+1, \dots, r$.
The first line of input contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$, $q$ ($1 \le n,q \le 2\cdot10^5$) β€” the number of targets and the queries Sheriff will pose. 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 points for hitting each target. Then follow $q$ lines, each with two integers $l$ and $r$ ($1 \le l \le r \le n$) β€” the range of the targets that is considered for each query. It is guaranteed that the sum of both $n$ and $q$ across all test cases does not exceed $2 \cdot 10^5$.
For each query, output "YES", if the Sheriff does not lose the game when only considering the targets $l, l+1, \dots, r$, and "NO" 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.
[ [ "2\n3 3\n1 2 2\n1 2\n1 3\n2 3\n5 3\n2 1 2 1 1\n1 2\n1 3\n4 5", "NO\nNO\nYES\nNO\nNO\nYES" ] ]
2018A
https://codeforces.com/problemset/problem/2018/A
Cards Partition
1,600
[ "2-sat", "brute force", "greedy", "implementation", "math" ]
Div. 1
2,000
256
[DJ Genki vs Gram - Einherjar Joker](https://soundcloud.com/leon- hwang-368077289/einherjar-joker-dj-genki-vs-gram) β € You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them. There is also a shop which contains unlimited cards of each type. You have $k$ coins, so you can buy at most $k$ new cards in total, and the cards you buy can contain any integer between $\mathbf{1}$ and $\mathbf{n}$, inclusive. After buying the new cards, you must partition all your cards into decks, according to the following rules: * all the decks must have the same size; * there are no pairs of cards with the same value in the same deck. Find the maximum possible size of a deck after buying cards and partitioning them optimally.
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$, $k$ ($1 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^{16}$) β€” the number of distinct types of cards and the number of coins. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^{10}$, $\sum a_i \geq 1$) β€” the number of cards of type $i$ you have at the beginning, for each $1 \leq i \leq n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output a single integer: the maximum possible size of a deck if you operate optimally.
[ [ "9\n3 1\n3 2 2\n5 4\n2 6 1 2 4\n2 100\n1410065408 10000000000\n10 8\n7 4 6 6 9 3 10 2 8 7\n2 12\n2 2\n2 70\n0 1\n1 0\n1\n3 0\n2 1 2\n3 1\n0 3 3", "2\n3\n1\n7\n2\n2\n1\n1\n2" ] ]
In the first test case, you can buy one card with the number $1$, and your cards become $[1, 1, 1, 1, 2, 2, 3, 3]$. You can partition them into the decks $[1, 2], [1, 2], [1, 3], [1, 3]$: they all have size $2$, and they all contain distinct values. You can show that you cannot get a partition with decks of size greater than $2$, so the answer is $2$. In the second test case, you can buy two cards with the number $1$ and one card with the number $3$, and your cards become $[1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5]$, which can be partitioned into $[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 5], [2, 3, 5], [2, 4, 5]$. You can show that you cannot get a partition with decks of size greater than $3$, so the answer is $3$.
2018B
https://codeforces.com/problemset/problem/2018/B
Speedbreaker
1,900
[ "binary search", "data structures", "dp", "greedy", "implementation", "two pointers" ]
Div. 1
2,000
256
[Djjaner - Speedbreaker](https://soundcloud.com/luciano- ferrari-151560131/speedbreaker) β € There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right. * At time $1$, you conquer exactly one city, called the starting city. * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it. You win if, for each $i$, you conquer city $i$ at a time no later than $a_i$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win?
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$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of cities. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) β€” the deadlines for conquering the cities. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output a single integer: the number of starting cities that allow you to win.
[ [ "3\n6\n6 3 3 3 5 5\n6\n5 6 4 1 4 5\n9\n8 6 4 2 1 3 5 7 9", "3\n0\n1" ] ]
In the first test case, cities $2$, $3$, and $4$ are good starting cities. In the second test case, there are no good starting cities. In the third test case, the only good starting city is city $5$.
2018C
https://codeforces.com/problemset/problem/2018/C
Tree Pruning
1,700
[ "brute force", "dfs and similar", "greedy", "sortings", "trees" ]
Div. 1
3,000
256
[t+pazolite, ginkiha, Hommarju - Paved Garden](https://soundcloud.com/fractalex-gd/ginkiha-paved-garden-little) β € You are given a tree with $n$ nodes, rooted at node $1$. In this problem, a leaf is a non-root node with degree $1$. In one operation, you can remove a leaf and the edge adjacent to it (possibly, new leaves appear). What is the minimum number of operations that you have to perform to get a tree, also rooted at node $1$, where all the leaves are at the same distance from the root?
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$ ($3 \leq n \leq 5 \cdot 10^5$) β€” the number of nodes. Each of the next $n-1$ lines contains two integers $u$, $v$ ($1 \leq u, v \leq n$, $u \neq v$), describing an edge that connects $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 $5 \cdot 10^5$.
For each test case, output a single integer: the minimum number of operations needed to achieve your goal.
[ [ "3\n7\n1 2\n1 3\n2 4\n2 5\n4 6\n4 7\n7\n1 2\n1 3\n1 4\n2 5\n3 6\n5 7\n15\n12 9\n1 6\n6 14\n9 11\n8 7\n3 5\n13 5\n6 10\n13 15\n13 6\n14 12\n7 2\n8 1\n1 4", "2\n2\n5" ] ]
In the first two examples, the tree is as follows: ![](CDN_BASE_URL/754511fbdb88995aaa49733a1fe75dfc) In the first example, by removing edges $(1, 3)$ and $(2, 5)$, the resulting tree has all leaves (nodes $6$ and $7$) at the same distance from the root (node $1$), which is $3$. The answer is $2$, as it is the minimum number of edges that need to be removed to achieve the goal. In the second example, removing edges $(1, 4)$ and $(5, 7)$ results in a tree where all leaves (nodes $4$ and $5$) are at the same distance from the root (node $1$), which is $2$.
2018D
https://codeforces.com/problemset/problem/2018/D
Max Plus Min Plus Size
2,200
[ "data structures", "dp", "dsu", "greedy", "implementation", "matrices", "sortings" ]
Div. 1
2,000
256
[EnV - The Dusty Dragon Tavern](https://soundcloud.com/envyofficial/env-the- dusty-dragon-tavern) β € You are given an array $a_1, a_2, \ldots, a_n$ of positive integers. You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for $1 \leq i \leq n-1$, at least one of $a_i$ and $a_{i+1}$ must not be red). Your score is the maximum value of a red element, plus the minimum value of a red element, plus the number of red elements. Find the maximum score you can get.
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$ ($1 \le n \le 2 \cdot 10^5$) β€” the length 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 10^9$) β€” the given array. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.
[ [ "4\n3\n5 4 5\n3\n4 5 4\n10\n3 3 3 3 4 1 2 3 5 4\n10\n17 89 92 42 29 41 92 14 70 45", "12\n11\n12\n186" ] ]
In the first test case, you can color the array as follows: $[\color{red}{5}, 4, \color{red}{5}]$. Your score is $\max([5, 5]) + \min([5, 5]) + \text{size}([5, 5]) = 5+5+2 = 12$. This is the maximum score you can get. In the second test case, you can color the array as follows: $[4, \color{red}{5}, 4]$. Your score is $\max([5]) + \min([5]) + \text{size}([5]) = 5+5+1 = 11$. This is the maximum score you can get. In the third test case, you can color the array as follows: $[\color{red}{3}, 3, \color{red}{3}, 3, \color{red}{4}, 1, 2, \color{red}{3}, 5, \color{red}{4}]$. Your score is $\max([3, 3, 4, 3, 4]) + \min([3, 3, 4, 3, 4]) + \text{size}([3, 3, 4, 3, 4]) = 4+3+5 = 12$. This is the maximum score you can get.
2018E1
https://codeforces.com/problemset/problem/2018/E1
Complex Segments (Easy Version)
3,300
[ "binary search", "data structures", "divide and conquer", "dsu", "greedy", "math", "sortings" ]
Div. 1
6,000
256
[Ken Arai - COMPLEX](https://soundcloud.com/diatomichail2/complex) β € This is the easy version of the problem. In this version, the constraints on $n$ and the time limit are lower. You can make hacks only if both versions of the problem are solved. A set of (closed) segments is complex if it can be partitioned into some subsets such that * all the subsets have the same size; and * a pair of segments intersects if and only if the two segments are in the same subset. You are given $n$ segments $[l_1, r_1], [l_2, r_2], \ldots, [l_n, r_n]$. Find the maximum size of a complex subset of these segments.
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 a single integer $n$ ($1 \le n \le 2 \cdot 10^4$) β€” the number of segments. The second line of each test case contains $n$ integers $l_1, l_2, \ldots, l_n$ ($1 \le l_i \le 2n$) β€” the left endpoints of the segments. The third line of each test case contains $n$ integers $r_1, r_2, \ldots, r_n$ ($l_i \leq r_i \le 2n$) β€” the right endpoints of the segments. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^4$.
For each test case, output a single integer: the maximum size of a complex subset of the given segments.
[ [ "3\n3\n1 2 3\n5 4 6\n5\n1 2 3 6 8\n5 4 7 9 10\n5\n3 1 4 1 5\n7 2 6 5 10", "3\n4\n4" ] ]
In the first test case, all pairs of segments intersect, therefore it is optimal to form a single group containing all of the three segments. In the second test case, there is no valid partition for all of the five segments. A valid partition with four segments is the following: $\\{\\{ [1, 5], [2, 4] \\}, \\{ [6, 9], [8, 10] \\}\\}$. In the third test case, it is optimal to make a single group containing all the segments except the second.
2018E2
https://codeforces.com/problemset/problem/2018/E2
Complex Segments (Hard Version)
3,400
[ "binary search", "data structures", "divide and conquer", "dsu", "greedy", "math", "sortings" ]
Div. 1
13,000
256
[Ken Arai - COMPLEX](https://soundcloud.com/diatomichail2/complex) β € This is the hard version of the problem. In this version, the constraints on $n$ and the time limit are higher. You can make hacks only if both versions of the problem are solved. A set of (closed) segments is complex if it can be partitioned into some subsets such that * all the subsets have the same size; and * a pair of segments intersects if and only if the two segments are in the same subset. You are given $n$ segments $[l_1, r_1], [l_2, r_2], \ldots, [l_n, r_n]$. Find the maximum size of a complex subset of these segments.
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 a single integer $n$ ($1 \le n \le 3 \cdot 10^5$) β€” the number of segments. The second line of each test case contains $n$ integers $l_1, l_2, \ldots, l_n$ ($1 \le l_i \le 2n$) β€” the left endpoints of the segments. The third line of each test case contains $n$ integers $r_1, r_2, \ldots, r_n$ ($l_i \leq r_i \le 2n$) β€” the right endpoints of the segments. It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$.
For each test case, output a single integer: the maximum size of a complex subset of the given segments.
[ [ "3\n3\n1 2 3\n5 4 6\n5\n1 2 3 6 8\n5 4 7 9 10\n5\n3 1 4 1 5\n7 2 6 5 10", "3\n4\n4" ] ]
In the first test case, all pairs of segments intersect, therefore it is optimal to form a single group containing all of the three segments. In the second test case, there is no valid partition for all of the five segments. A valid partition with four segments is the following: $\\{\\{ [1, 5], [2, 4] \\}, \\{ [6, 9], [8, 10] \\}\\}$. In the third test case, it is optimal to make a single group containing all the segments except the second.
2018F1
https://codeforces.com/problemset/problem/2018/F1
Speedbreaker Counting (Easy Version)
2,900
[ "combinatorics", "dp", "greedy", "math" ]
Div. 1
2,000
1,024
[NightHawk22 - Isolation](https://soundcloud.com/vepium/nighthawk22-isolation- official-limbo-remix) β € This is the easy version of the problem. In the three versions, the constraints on $n$ and the time limit are different. You can make hacks only if all the versions of the problem are solved. This is the statement of Problem D1B: * There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right. * At time $1$, you conquer exactly one city, called the starting city. * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it. You win if, for each $i$, you conquer city $i$ at a time no later than $a_i$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win? For each $0 \leq k \leq n$, count the number of arrays of positive integers $a_1, a_2, \ldots, a_n$ such that * $1 \leq a_i \leq n$ for each $1 \leq i \leq n$; * the answer to Problem D1B is $k$. The answer can be very large, so you have to calculate it modulo a given prime $p$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 80$). The description of the test cases follows. The only line of each test case contains two integers $n$, $p$ ($1 \le n \le 80$, $10^8 \leq p \leq 10^9$, $p$ is prime) β€” the number of cities and the modulo. It is guaranteed that the sum of $n$ over all test cases does not exceed $80$.
For each test case, output $n+1$ integers: the $i$-th integer should be the number of arrays that satisfy the conditions for $k = i-1$.
[ [ "11\n1 998244353\n2 998244353\n3 998244353\n4 998244353\n5 998244353\n6 998244353\n7 998244353\n8 998244353\n9 998244353\n10 102275857\n10 999662017", "0 1 \n1 2 1 \n14 7 4 2 \n183 34 19 16 4 \n2624 209 112 120 48 12 \n42605 1546 793 992 468 216 36 \n785910 13327 6556 9190 4672 2880 864 144 \n16382863 130922 61939 94992 50100 36960 14256 4608 576 \n382823936 1441729 657784 1086596 583344 488700 216000 96480 23040 2880 \n20300780 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 \n944100756 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400" ] ]
In the first test case, * arrays with $1$ good starting city: $[1]$. In the second test case, * arrays with $0$ good starting cities: $[1, 1]$; * arrays with $1$ good starting city: $[1, 2]$, $[2, 1]$; * arrays with $2$ good starting cities: $[2, 2]$. In the third test case, * arrays with $0$ good starting cities: $[1, 1, 1]$, $[1, 1, 2]$, $[1, 1, 3]$, $[1, 2, 1]$, $[1, 2, 2]$, $[1, 3, 1]$, $[1, 3, 2]$, $[2, 1, 1]$, $[2, 1, 2]$, $[2, 2, 1]$, $[2, 2, 2]$, $[2, 3, 1]$, $[2, 3, 2]$, $[3, 1, 1]$; * arrays with $1$ good starting city: $[1, 2, 3]$, $[1, 3, 3]$, $[2, 1, 3]$, $[3, 1, 2]$, $[3, 1, 3]$, $[3, 2, 1]$, $[3, 3, 1]$; * arrays with $2$ good starting cities: $[2, 2, 3]$, $[2, 3, 3]$, $[3, 2, 2]$, $[3, 3, 2]$; * arrays with $3$ good starting cities: $[3, 2, 3]$, $[3, 3, 3]$.
2018F2
https://codeforces.com/problemset/problem/2018/F2
Speedbreaker Counting (Medium Version)
3,000
[ "dp", "greedy", "math" ]
Div. 1
10,000
1,024
[NightHawk22 - Isolation](https://soundcloud.com/vepium/nighthawk22-isolation- official-limbo-remix) β € This is the medium version of the problem. In the three versions, the constraints on $n$ and the time limit are different. You can make hacks only if all the versions of the problem are solved. This is the statement of Problem D1B: * There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right. * At time $1$, you conquer exactly one city, called the starting city. * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it. You win if, for each $i$, you conquer city $i$ at a time no later than $a_i$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win? For each $0 \leq k \leq n$, count the number of arrays of positive integers $a_1, a_2, \ldots, a_n$ such that * $1 \leq a_i \leq n$ for each $1 \leq i \leq n$; * the answer to Problem D1B is $k$. The answer can be very large, so you have to calculate it modulo a given prime $p$.
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 only line of each test case contains two integers $n$, $p$ ($1 \le n \le 500$, $10^8 \leq p \leq 10^9$, $p$ is prime) β€” the number of cities and the modulo. It is guaranteed that the sum of $n$ over all test cases does not exceed $500$.
For each test case, output $n+1$ integers: the $i$-th integer should be the number of arrays that satisfy the conditions for $k = i-1$.
[ [ "11\n1 998244353\n2 998244353\n3 998244353\n4 998244353\n5 998244353\n6 998244353\n7 998244353\n8 998244353\n9 998244353\n10 102275857\n10 999662017", "0 1 \n1 2 1 \n14 7 4 2 \n183 34 19 16 4 \n2624 209 112 120 48 12 \n42605 1546 793 992 468 216 36 \n785910 13327 6556 9190 4672 2880 864 144 \n16382863 130922 61939 94992 50100 36960 14256 4608 576 \n382823936 1441729 657784 1086596 583344 488700 216000 96480 23040 2880 \n20300780 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 \n944100756 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400" ] ]
In the first test case, * arrays with $1$ good starting city: $[1]$. In the second test case, * arrays with $0$ good starting cities: $[1, 1]$; * arrays with $1$ good starting city: $[1, 2]$, $[2, 1]$; * arrays with $2$ good starting cities: $[2, 2]$. In the third test case, * arrays with $0$ good starting cities: $[1, 1, 1]$, $[1, 1, 2]$, $[1, 1, 3]$, $[1, 2, 1]$, $[1, 2, 2]$, $[1, 3, 1]$, $[1, 3, 2]$, $[2, 1, 1]$, $[2, 1, 2]$, $[2, 2, 1]$, $[2, 2, 2]$, $[2, 3, 1]$, $[2, 3, 2]$, $[3, 1, 1]$; * arrays with $1$ good starting city: $[1, 2, 3]$, $[1, 3, 3]$, $[2, 1, 3]$, $[3, 1, 2]$, $[3, 1, 3]$, $[3, 2, 1]$, $[3, 3, 1]$; * arrays with $2$ good starting cities: $[2, 2, 3]$, $[2, 3, 3]$, $[3, 2, 2]$, $[3, 3, 2]$; * arrays with $3$ good starting cities: $[3, 2, 3]$, $[3, 3, 3]$.
2018F3
https://codeforces.com/problemset/problem/2018/F3
Speedbreaker Counting (Hard Version)
3,100
[ "dp", "greedy", "math" ]
Div. 1
10,000
1,024
[NightHawk22 - Isolation](https://soundcloud.com/vepium/nighthawk22-isolation- official-limbo-remix) β € This is the hard version of the problem. In the three versions, the constraints on $n$ and the time limit are different. You can make hacks only if all the versions of the problem are solved. This is the statement of Problem D1B: * There are $n$ cities in a row, numbered $1, 2, \ldots, n$ left to right. * At time $1$, you conquer exactly one city, called the starting city. * At time $2, 3, \ldots, n$, you can choose a city adjacent to the ones conquered so far and conquer it. You win if, for each $i$, you conquer city $i$ at a time no later than $a_i$. A winning strategy may or may not exist, also depending on the starting city. How many starting cities allow you to win? For each $0 \leq k \leq n$, count the number of arrays of positive integers $a_1, a_2, \ldots, a_n$ such that * $1 \leq a_i \leq n$ for each $1 \leq i \leq n$; * the answer to Problem D1B is $k$. The answer can be very large, so you have to calculate it modulo a given prime $p$.
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 3000$). The description of the test cases follows. The only line of each test case contains two integers $n$, $p$ ($1 \le n \le 3000$, $10^8 \leq p \leq 10^9$, $p$ is prime) β€” the number of cities and the modulo. It is guaranteed that the sum of $n$ over all test cases does not exceed $3000$.
For each test case, output $n+1$ integers: the $i$-th integer should be the number of arrays that satisfy the conditions for $k = i-1$.
[ [ "11\n1 998244353\n2 998244353\n3 998244353\n4 998244353\n5 998244353\n6 998244353\n7 998244353\n8 998244353\n9 998244353\n10 102275857\n10 999662017", "0 1 \n1 2 1 \n14 7 4 2 \n183 34 19 16 4 \n2624 209 112 120 48 12 \n42605 1546 793 992 468 216 36 \n785910 13327 6556 9190 4672 2880 864 144 \n16382863 130922 61939 94992 50100 36960 14256 4608 576 \n382823936 1441729 657784 1086596 583344 488700 216000 96480 23040 2880 \n20300780 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400 \n944100756 17572114 7751377 13641280 7376068 6810552 3269700 1785600 576000 144000 14400" ] ]
In the first test case, * arrays with $1$ good starting city: $[1]$. In the second test case, * arrays with $0$ good starting cities: $[1, 1]$; * arrays with $1$ good starting city: $[1, 2]$, $[2, 1]$; * arrays with $2$ good starting cities: $[2, 2]$. In the third test case, * arrays with $0$ good starting cities: $[1, 1, 1]$, $[1, 1, 2]$, $[1, 1, 3]$, $[1, 2, 1]$, $[1, 2, 2]$, $[1, 3, 1]$, $[1, 3, 2]$, $[2, 1, 1]$, $[2, 1, 2]$, $[2, 2, 1]$, $[2, 2, 2]$, $[2, 3, 1]$, $[2, 3, 2]$, $[3, 1, 1]$; * arrays with $1$ good starting city: $[1, 2, 3]$, $[1, 3, 3]$, $[2, 1, 3]$, $[3, 1, 2]$, $[3, 1, 3]$, $[3, 2, 1]$, $[3, 3, 1]$; * arrays with $2$ good starting cities: $[2, 2, 3]$, $[2, 3, 3]$, $[3, 2, 2]$, $[3, 3, 2]$; * arrays with $3$ good starting cities: $[3, 2, 3]$, $[3, 3, 3]$.
2019A
https://codeforces.com/problemset/problem/2019/A
Max Plus Size
800
[ "brute force", "dp", "greedy" ]
Div. 2
1,000
256
[EnV - Dynasty](https://soundcloud.com/envyofficial/env-dynasty) β € You are given an array $a_1, a_2, \ldots, a_n$ of positive integers. You can color some elements of the array red, but there cannot be two adjacent red elements (i.e., for $1 \leq i \leq n-1$, at least one of $a_i$ and $a_{i+1}$ must not be red). Your score is the maximum value of a red element plus the number of red elements. Find the maximum score you can get.
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 a single integer $n$ ($1 \le n \le 100$) β€” the length 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 1000$) β€” the given array.
For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.
[ [ "4\n3\n5 4 5\n3\n4 5 4\n10\n3 3 3 3 4 1 2 3 4 5\n9\n17 89 92 42 29 92 14 70 45", "7\n6\n10\n97" ] ]
In the first test case, you can color the array as follows: $[\color{red}{5}, 4, \color{red}{5}]$. Your score is $\max([5, 5]) + \text{size}([5, 5]) = 5+2 = 7$. This is the maximum score you can get. In the second test case, you can color the array as follows: $[\color{red}{4}, 5, \color{red}{4}]$. Your score is $\max([4, 4]) + \text{size}([4, 4]) = 4+2 = 6$. This is the maximum score you can get. In the third test case, you can color the array as follows: $[\color{red}{3}, 3, \color{red}{3}, 3, \color{red}{4}, 1, 2, \color{red}{3}, 4, \color{red}{5}]$. Your score is $\max([3, 3, 4, 3, 5]) + \text{size}([3, 3, 4, 3, 5]) = 5+5 = 10$. This is the maximum score you can get.
2019B
https://codeforces.com/problemset/problem/2019/B
All Pairs Segments
1,200
[ "implementation", "math" ]
Div. 2
1,500
256
[Shirobon - FOX](https://soundcloud.com/shirobon/fox?in=mart_207/sets/fav) β € You are given $n$ points on the $x$ axis, at increasing positive integer coordinates $x_1 < x_2 < \ldots < x_n$. For each pair $(i, j)$ with $1 \leq i < j \leq n$, you draw the segment $[x_i, x_j]$. The segments are closed, i.e., a segment $[a, b]$ contains the points $a, a+1, \ldots, b$. You are given $q$ queries. In the $i$-th query, you are given a positive integer $k_i$, and you have to determine how many points with integer coordinates are contained in exactly $k_i$ segments.
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$, $q$ ($2 \le n \le 10^5$, $1 \le q \le 10^5$) β€” the number of points and the number of queries. The second line of each test case contains $n$ integers $x_1, x_2, \ldots, x_n$ ($1 \leq x_1 < x_2 < \ldots < x_n \leq 10^9$) β€” the coordinates of the $n$ points. The third line of each test case contains $q$ integers $k_1, k_2, \ldots, k_q$ ($1 \leq k_i \leq 10^{18}$) β€” the parameters of the $q$ queries. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$, and the sum of $q$ over all test cases does not exceed $10^5$.
For each test case, output a single line with $q$ integers: the $i$-th integer is the answer to the $i$-th query.
[ [ "3\n2 2\n101 200\n2 1\n6 15\n1 2 3 5 6 7\n1 2 3 4 5 6 7 8 9 10 11 12 13 14 15\n5 8\n254618033 265675151 461318786 557391198 848083778\n6 9 15 10 6 9 4 4294967300", "0 100 \n0 0 0 0 2 0 0 0 3 0 2 0 0 0 0 \n291716045 0 0 0 291716045 0 301749698 0" ] ]
In the first example, you only draw the segment $[101, 200]$. No point is contained in exactly $2$ segments, and the $100$ points $101, 102, \ldots, 200$ are contained in exactly $1$ segment. In the second example, you draw $15$ segments: $[1, 2], [1, 3], [1, 5], [1, 6], [1, 7], [2, 3], [2, 5], [2, 6], [2, 7], [3, 5], [3, 6], [3, 7], [5, 6], [5, 7], [6, 7]$. Points $1, 7$ are contained in exactly $5$ segments; points $2, 4, 6$ are contained in exactly $9$ segments; points $3, 5$ are contained in exactly $11$ segments.
2019C
https://codeforces.com/problemset/problem/2019/C
Cards Partition
1,600
[ "greedy", "implementation", "math" ]
Div. 2
2,000
256
[DJ Genki vs Gram - Einherjar Joker](https://soundcloud.com/leon- hwang-368077289/einherjar-joker-dj-genki-vs-gram) β € You have some cards. An integer between $1$ and $n$ is written on each card: specifically, for each $i$ from $1$ to $n$, you have $a_i$ cards which have the number $i$ written on them. There is also a shop which contains unlimited cards of each type. You have $k$ coins, so you can buy at most $k$ new cards in total, and the cards you buy can contain any integer between $\mathbf{1}$ and $\mathbf{n}$, inclusive. After buying the new cards, you must partition all your cards into decks, according to the following rules: * all the decks must have the same size; * there are no pairs of cards with the same value in the same deck. Find the maximum possible size of a deck after buying cards and partitioning them optimally.
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$, $k$ ($1 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^{16}$) β€” the number of distinct types of cards and the number of coins. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^{10}$, $\sum a_i \geq 1$) β€” the number of cards of type $i$ you have at the beginning, for each $1 \leq i \leq n$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
For each test case, output a single integer: the maximum possible size of a deck if you operate optimally.
[ [ "9\n3 1\n3 2 2\n5 4\n2 6 1 2 4\n2 100\n1410065408 10000000000\n10 8\n7 4 6 6 9 3 10 2 8 7\n2 12\n2 2\n2 70\n0 1\n1 0\n1\n3 0\n2 1 2\n3 1\n0 3 3", "2\n3\n1\n7\n2\n2\n1\n1\n2" ] ]
In the first test case, you can buy one card with the number $1$, and your cards become $[1, 1, 1, 1, 2, 2, 3, 3]$. You can partition them into the decks $[1, 2], [1, 2], [1, 3], [1, 3]$: they all have size $2$, and they all contain distinct values. You can show that you cannot get a partition with decks of size greater than $2$, so the answer is $2$. In the second test case, you can buy two cards with the number $1$ and one card with the number $3$, and your cards become $[1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5]$, which can be partitioned into $[1, 2, 3], [1, 2, 4], [1, 2, 5], [1, 2, 5], [2, 3, 5], [2, 4, 5]$. You can show that you cannot get a partition with decks of size greater than $3$, so the answer is $3$.