problem_id
stringlengths 5
6
| url
stringlengths 48
49
| title
stringlengths 2
46
| rating
int64 800
3.5k
| tags
listlengths 1
11
| div
stringclasses 16
values | time_limit_ms
int64 1k
13k
| memory_limit_mb
int64 32
1.02k
| description
stringlengths 67
2.52k
| input
stringlengths 99
1.93k
β | output
stringlengths 47
1.35k
β | examples
listlengths 1
5
| note
stringlengths 0
1.68k
| prompt
stringlengths 505
6.96k
|
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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.
|
Title: Numeric String Template
time_limit_ms: 2000
memory_limit_mb: 256
Description: 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$.
Input: 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$.
Output: 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).
Examples:
input:
3
5
3 5 2 1 3
2
abfda
afbfa
2
1 2
3
ab
abc
aa
4
5 -3 5 -3
4
aaaa
bcbc
aba
cbcb
output:
YES
NO
YES
NO
NO
NO
YES
NO
YES
Note: The first test case is explained in the problem statement.
|
2030F
|
https://codeforces.com/problemset/problem/2030/F
|
Orangutan Approved Subarrays
| 2,400
|
[
"binary search",
"data structures",
"dp",
"greedy",
"implementation",
"two pointers"
] |
Div. 2
| 3,000
| 512
|
Suppose you have an array $b$. Initially, you also have a set $S$ that contains all distinct elements of $b$. The array $b$ is called orangutan-approved if it can be emptied by repeatedly performing the following operation:
* In one operation, select indices $l$ and $r$ ($1 \leq l \leq r \leq |b|$) such that $v = b_l = b_{l+1} = \ldots = b_r$ and $v$ is present in $S$. Remove $v$ from $S$, and simultaneously remove all $b_i$ such that $l \leq i \leq r$. Then, reindex the elements $b_{r+1}, b_{r+2}, \ldots$ as $b_l, b_{l+1}, \ldots$ accordingly.
You are given an array $a$ of length $n$ and $q$ queries.
Each query consists of two indices $l$ and $r$ ($1 \le l \le r \le n$), and you need to determine whether or not the subarray $a_{l}, a_{l+1}, \ldots, a_r$ is orangutan-approved.
|
The first line contains $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains integers $n$ and $q$ ($1 \leq n,q \leq 2 \cdot 10^5$) β the size of $a$ and the number of queries, respectively.
The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) β the elements of the array $a$.
The following $q$ lines contain two integers $l$ and $r$ β the endpoints of the subarray for each query ($1 \leq l \leq r \leq n$).
It is guaranteed that the sum of $n$ and $q$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each query, output "YES" (without quotes) if the subarray from $l$ to $r$ is orangutan-approved, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).
|
[
[
"3\n4 2\n1 2 2 1\n1 4\n1 3\n5 3\n1 2 1 2 1\n2 5\n3 5\n1 3\n8 4\n1 2 3 2 1 3 2 3\n1 5\n2 8\n3 5\n6 8",
"YES\nYES\nNO\nYES\nYES\nYES\nNO\nYES\nYES"
]
] |
In the first query of the first testcase, the answer is YES.
* Initially, $S=\\{1,2\\}$ and $b=[1,2,2,1]$ * Select $l=2$ and $r=3$. Since $b_2=b_3=2$ is in $S$, we may erase $b_2$ and $b_3$ from the array, as well as erasing $2$ from $S$. The set $S$ becomes $\\{1\\}$ and the array becomes $[1,1]$. * Select $l=1$ and $r=2$. Since $b_1=b_2=1$ is in $S$, we may erase $b_1$ and $b_2$ from the array, as well as erasing $1$ from $S$. The set $S$ becomes $\\{\\}$ and the array becomes $[]$. * Since the array is now empty, we can say the original array is orangutan-approved.
In the first query of the second testcase, the answer is NO, because it can be shown that the subarray $[2,1,2,1]$ cannot become empty through any sequence of valid operations.
|
Title: Orangutan Approved Subarrays
time_limit_ms: 3000
memory_limit_mb: 512
Description: Suppose you have an array $b$. Initially, you also have a set $S$ that contains all distinct elements of $b$. The array $b$ is called orangutan-approved if it can be emptied by repeatedly performing the following operation:
* In one operation, select indices $l$ and $r$ ($1 \leq l \leq r \leq |b|$) such that $v = b_l = b_{l+1} = \ldots = b_r$ and $v$ is present in $S$. Remove $v$ from $S$, and simultaneously remove all $b_i$ such that $l \leq i \leq r$. Then, reindex the elements $b_{r+1}, b_{r+2}, \ldots$ as $b_l, b_{l+1}, \ldots$ accordingly.
You are given an array $a$ of length $n$ and $q$ queries.
Each query consists of two indices $l$ and $r$ ($1 \le l \le r \le n$), and you need to determine whether or not the subarray $a_{l}, a_{l+1}, \ldots, a_r$ is orangutan-approved.
Input: The first line contains $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains integers $n$ and $q$ ($1 \leq n,q \leq 2 \cdot 10^5$) β the size of $a$ and the number of queries, respectively.
The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) β the elements of the array $a$.
The following $q$ lines contain two integers $l$ and $r$ β the endpoints of the subarray for each query ($1 \leq l \leq r \leq n$).
It is guaranteed that the sum of $n$ and $q$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each query, output "YES" (without quotes) if the subarray from $l$ to $r$ is orangutan-approved, and "NO" (without quotes) otherwise.
You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).
Examples:
input:
3
4 2
1 2 2 1
1 4
1 3
5 3
1 2 1 2 1
2 5
3 5
1 3
8 4
1 2 3 2 1 3 2 3
1 5
2 8
3 5
6 8
output:
YES
YES
NO
YES
YES
YES
NO
YES
YES
Note: In the first query of the first testcase, the answer is YES.
* Initially, $S=\\{1,2\\}$ and $b=[1,2,2,1]$ * Select $l=2$ and $r=3$. Since $b_2=b_3=2$ is in $S$, we may erase $b_2$ and $b_3$ from the array, as well as erasing $2$ from $S$. The set $S$ becomes $\\{1\\}$ and the array becomes $[1,1]$. * Select $l=1$ and $r=2$. Since $b_1=b_2=1$ is in $S$, we may erase $b_1$ and $b_2$ from the array, as well as erasing $1$ from $S$. The set $S$ becomes $\\{\\}$ and the array becomes $[]$. * Since the array is now empty, we can say the original array is orangutan-approved.
In the first query of the second testcase, the answer is NO, because it can be shown that the subarray $[2,1,2,1]$ cannot become empty through any sequence of valid operations.
|
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"
]
] |
Title: Robin Hood Archery
time_limit_ms: 3000
memory_limit_mb: 256
Description: 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$.
Input: 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$.
Output: 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.
Examples:
input:
2
3 3
1 2 2
1 2
1 3
2 3
5 3
2 1 2 1 1
1 2
1 3
4 5
output:
NO
NO
YES
NO
NO
YES
Note:
|
|
1995D
|
https://codeforces.com/problemset/problem/1995/D
|
Cases
| 2,300
|
[
"bitmasks",
"brute force",
"dp",
"strings"
] |
Div. 2
| 2,000
| 256
|
You're a linguist studying a mysterious ancient language. You know that
1. Its words consist only of the first $c$ letters of the Latin alphabet. 2. Each word has a case which can be unambiguously determined by its last letter (different letters correspond to different cases). For example, words "ABACABA" and "ABA" (if they exist) have the same case in this language because they both have the same ending 'A', whereas "ALICE" and "BOB" have different cases. If the language does not have a case corresponding to some letter, it means that the word cannot end with this letter. 3. The length of each word is $k$ or less.
You have a single text written in this language. Unfortunately, as the language is really ancient, spaces between words are missing and all letters are uppercase. You wonder what is the minimum number of cases the language can have. Can you find this out?
|
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) β the number of test cases. It is followed by descriptions of the test cases.
The first line of each test case contains three integers $n$, $c$, $k$ ($1 \le k \le n \le 2^{18}$, $1 \le c \le 18$) β the length of the text, the number of letters in the language, and the maximum length of the word.
The second line contains a string of $n$ characters β the text itself. Each character is one of the first $c$ uppercase letters of the Latin alphabet.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2^{18}$ and the sum of $2^c$ over all test cases does not exceed $2^{18}$.
|
For each test case, output a single line consisting of a single integer β the minimum number of cases in the language.
|
[
[
"7\n5 5 1\nABCDE\n3 1 2\nAAA\n3 2 2\nAAB\n10 2 2\nABABABABAB\n4 4 4\nDCBA\n1 17 1\nQ\n9 3 2\nABCABCABC",
"5\n1\n2\n1\n1\n1\n2"
]
] |
In the first test case, there must be five cases in the language (for each of the letters 'A', 'B', 'C', 'D', and 'E' there must be a case that has a corresponding ending).
In the fourth test case, one case with ending 'B' is sufficient.
|
Title: Cases
time_limit_ms: 2000
memory_limit_mb: 256
Description: You're a linguist studying a mysterious ancient language. You know that
1. Its words consist only of the first $c$ letters of the Latin alphabet. 2. Each word has a case which can be unambiguously determined by its last letter (different letters correspond to different cases). For example, words "ABACABA" and "ABA" (if they exist) have the same case in this language because they both have the same ending 'A', whereas "ALICE" and "BOB" have different cases. If the language does not have a case corresponding to some letter, it means that the word cannot end with this letter. 3. The length of each word is $k$ or less.
You have a single text written in this language. Unfortunately, as the language is really ancient, spaces between words are missing and all letters are uppercase. You wonder what is the minimum number of cases the language can have. Can you find this out?
Input: Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) β the number of test cases. It is followed by descriptions of the test cases.
The first line of each test case contains three integers $n$, $c$, $k$ ($1 \le k \le n \le 2^{18}$, $1 \le c \le 18$) β the length of the text, the number of letters in the language, and the maximum length of the word.
The second line contains a string of $n$ characters β the text itself. Each character is one of the first $c$ uppercase letters of the Latin alphabet.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2^{18}$ and the sum of $2^c$ over all test cases does not exceed $2^{18}$.
Output: For each test case, output a single line consisting of a single integer β the minimum number of cases in the language.
Examples:
input:
7
5 5 1
ABCDE
3 1 2
AAA
3 2 2
AAB
10 2 2
ABABABABAB
4 4 4
DCBA
1 17 1
Q
9 3 2
ABCABCABC
output:
5
1
2
1
1
1
2
Note: In the first test case, there must be five cases in the language (for each of the letters 'A', 'B', 'C', 'D', and 'E' there must be a case that has a corresponding ending).
In the fourth test case, one case with ending 'B' is sufficient.
|
1980D
|
https://codeforces.com/problemset/problem/1980/D
|
GCD-sequence
| 1,400
|
[
"greedy",
"implementation",
"math",
"number theory"
] |
Div. 3
| 2,000
| 256
|
GCD (Greatest Common Divisor) of two integers $x$ and $y$ is the maximum integer $z$ by which both $x$ and $y$ are divisible. For example, $GCD(36, 48) = 12$, $GCD(5, 10) = 5$, and $GCD(7,11) = 1$.
Kristina has an array $a$ consisting of exactly $n$ positive integers. She wants to count the GCD of each neighbouring pair of numbers to get a new array $b$, called GCD-sequence.
So, the elements of the GCD-sequence $b$ will be calculated using the formula $b_i = GCD(a_i, a_{i + 1})$ for $1 \le i \le n - 1$.
Determine whether it is possible to remove exactly one number from the array $a$ so that the GCD sequence $b$ is non-decreasing (i.e., $b_i \le b_{i+1}$ is always true).
For example, let Khristina had an array $a$ = [$20, 6, 12, 3, 48, 36$]. If she removes $a_4 = 3$ from it and counts the GCD-sequence of $b$, she gets:
* $b_1 = GCD(20, 6) = 2$ * $b_2 = GCD(6, 12) = 6$ * $b_3 = GCD(12, 48) = 12$ * $b_4 = GCD(48, 36) = 12$
The resulting GCD sequence $b$ = [$2,6,12,12$] is non-decreasing because $b_1 \le b_2 \le b_3 \le b_4$.
|
The first line of input data contains a single number $t$ ($1 \le t \le 10^4$) β he number of test cases in the test.
This is followed by the descriptions of the test cases.
The first line of each test case contains a single integer $n$ ($3 \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$ ($1 \le a_i \le 10^9$) β the elements of array $a$.
It is guaranteed that the sum of $n$ over all test case does not exceed $2 \cdot 10^5$.
|
For each test case, output a single line:
* "YES" if you can remove exactly one number from the array $a$ so that the GCD-sequence of $b$ is non-decreasing; * "NO" otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as a positive answer).
|
[
[
"12\n6\n20 6 12 3 48 36\n4\n12 6 3 4\n3\n10 12 3\n5\n32 16 8 4 2\n5\n100 50 2 10 20\n4\n2 4 8 1\n10\n7 4 6 2 4 5 1 4 2 8\n7\n5 9 6 8 5 9 2\n6\n11 14 8 12 9 3\n9\n5 7 3 10 6 3 12 6 3\n3\n4 2 4\n8\n1 6 11 12 6 12 3 6",
"YES\nNO\nYES\nNO\nYES\nYES\nNO\nYES\nYES\nYES\nYES\nYES"
]
] |
The first test case is explained in the problem statement.
|
Title: GCD-sequence
time_limit_ms: 2000
memory_limit_mb: 256
Description: GCD (Greatest Common Divisor) of two integers $x$ and $y$ is the maximum integer $z$ by which both $x$ and $y$ are divisible. For example, $GCD(36, 48) = 12$, $GCD(5, 10) = 5$, and $GCD(7,11) = 1$.
Kristina has an array $a$ consisting of exactly $n$ positive integers. She wants to count the GCD of each neighbouring pair of numbers to get a new array $b$, called GCD-sequence.
So, the elements of the GCD-sequence $b$ will be calculated using the formula $b_i = GCD(a_i, a_{i + 1})$ for $1 \le i \le n - 1$.
Determine whether it is possible to remove exactly one number from the array $a$ so that the GCD sequence $b$ is non-decreasing (i.e., $b_i \le b_{i+1}$ is always true).
For example, let Khristina had an array $a$ = [$20, 6, 12, 3, 48, 36$]. If she removes $a_4 = 3$ from it and counts the GCD-sequence of $b$, she gets:
* $b_1 = GCD(20, 6) = 2$ * $b_2 = GCD(6, 12) = 6$ * $b_3 = GCD(12, 48) = 12$ * $b_4 = GCD(48, 36) = 12$
The resulting GCD sequence $b$ = [$2,6,12,12$] is non-decreasing because $b_1 \le b_2 \le b_3 \le b_4$.
Input: The first line of input data contains a single number $t$ ($1 \le t \le 10^4$) β he number of test cases in the test.
This is followed by the descriptions of the test cases.
The first line of each test case contains a single integer $n$ ($3 \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$ ($1 \le a_i \le 10^9$) β the elements of array $a$.
It is guaranteed that the sum of $n$ over all test case does not exceed $2 \cdot 10^5$.
Output: For each test case, output a single line:
* "YES" if you can remove exactly one number from the array $a$ so that the GCD-sequence of $b$ is non-decreasing; * "NO" otherwise.
You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will all be recognized as a positive answer).
Examples:
input:
12
6
20 6 12 3 48 36
4
12 6 3 4
3
10 12 3
5
32 16 8 4 2
5
100 50 2 10 20
4
2 4 8 1
10
7 4 6 2 4 5 1 4 2 8
7
5 9 6 8 5 9 2
6
11 14 8 12 9 3
9
5 7 3 10 6 3 12 6 3
3
4 2 4
8
1 6 11 12 6 12 3 6
output:
YES
NO
YES
NO
YES
YES
NO
YES
YES
YES
YES
YES
Note: The first test case is explained in the problem statement.
|
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.
|
Title: Max Plus Size
time_limit_ms: 1000
memory_limit_mb: 256
Description: [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.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains 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.
Output: For each test case, output a single integer: the maximum possible score you can get after coloring some elements red according to the statement.
Examples:
input:
4
3
5 4 5
3
4 5 4
10
3 3 3 3 4 1 2 3 4 5
9
17 89 92 42 29 92 14 70 45
output:
7
6
10
97
Note: 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.
|
2022C
|
https://codeforces.com/problemset/problem/2022/C
|
Gerrymandering
| 1,800
|
[
"dp",
"implementation"
] |
Div. 2
| 2,000
| 256
|
We all steal a little bit. But I have only one hand, while my adversaries have two.
Γlvaro ObregΓ³n
Γlvaro and JosΓ© are the only candidates running for the presidency of Tepito, a rectangular grid of $2$ rows and $n$ columns, where each cell represents a house. It is guaranteed that $n$ is a multiple of $3$.
Under the voting system of Tepito, the grid will be split into districts, which consist of any $3$ houses that are connected$^{\text{β}}$. Each house will belong to exactly one district.
Each district will cast a single vote. The district will vote for Γlvaro or JosΓ© respectively if at least $2$ houses in that district select them. Therefore, a total of $\frac{2n}{3}$ votes will be cast.
As Γlvaro is the current president, he knows exactly which candidate each house will select. If Γlvaro divides the houses into districts optimally, determine the maximum number of votes he can get.
$^{\text{β}}$A set of cells is connected if there is a path between any $2$ cells that requires moving only up, down, left and right through cells in the set.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains one integer $n$ ($3 \le n \le 10^5$; $n$ is a multiple of $3$) β the number of columns of Tepito.
The following two lines each contain a string of length $n$. The $i$-th line contains the string $s_i$, consisting of the characters $\texttt{A}$ and $\texttt{J}$. If $s_{i,j}=\texttt{A}$, the house in the $i$-th row and $j$-th column will select Γlvaro. Otherwise if $s_{i,j}=\texttt{J}$, the house will select JosΓ©.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
|
For each test case, output a single integer β the maximum number of districts Γlvaro can win by optimally dividing the houses into districts.
|
[
[
"4\n3\nAAA\nAJJ\n6\nJAJAJJ\nJJAJAJ\n6\nAJJJAJ\nAJJAAA\n9\nAJJJJAJAJ\nJAAJJJJJA",
"2\n2\n3\n2"
]
] |
The image below showcases the optimal arrangement of districts Γlvaro can use for each test case in the example.

|
Title: Gerrymandering
time_limit_ms: 2000
memory_limit_mb: 256
Description: We all steal a little bit. But I have only one hand, while my adversaries have two.
Γlvaro ObregΓ³n
Γlvaro and JosΓ© are the only candidates running for the presidency of Tepito, a rectangular grid of $2$ rows and $n$ columns, where each cell represents a house. It is guaranteed that $n$ is a multiple of $3$.
Under the voting system of Tepito, the grid will be split into districts, which consist of any $3$ houses that are connected$^{\text{β}}$. Each house will belong to exactly one district.
Each district will cast a single vote. The district will vote for Γlvaro or JosΓ© respectively if at least $2$ houses in that district select them. Therefore, a total of $\frac{2n}{3}$ votes will be cast.
As Γlvaro is the current president, he knows exactly which candidate each house will select. If Γlvaro divides the houses into districts optimally, determine the maximum number of votes he can get.
$^{\text{β}}$A set of cells is connected if there is a path between any $2$ cells that requires moving only up, down, left and right through cells in the set.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains one integer $n$ ($3 \le n \le 10^5$; $n$ is a multiple of $3$) β the number of columns of Tepito.
The following two lines each contain a string of length $n$. The $i$-th line contains the string $s_i$, consisting of the characters $\texttt{A}$ and $\texttt{J}$. If $s_{i,j}=\texttt{A}$, the house in the $i$-th row and $j$-th column will select Γlvaro. Otherwise if $s_{i,j}=\texttt{J}$, the house will select JosΓ©.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
Output: For each test case, output a single integer β the maximum number of districts Γlvaro can win by optimally dividing the houses into districts.
Examples:
input:
4
3
AAA
AJJ
6
JAJAJJ
JJAJAJ
6
AJJJAJ
AJJAAA
9
AJJJJAJAJ
JAAJJJJJA
output:
2
2
3
2
Note: The image below showcases the optimal arrangement of districts Γlvaro can use for each test case in the example.

|
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$.
|
Title: Turtle and a MEX Problem (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: 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$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n, m$ ($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$.
Output: For each test case, output a single integer β the value of $\sum\limits_{i = 0}^m f(i)$.
Examples:
input:
6
3 4
2 0 2
3 2 3 3
4 7 0 1 5
3 4
5 0 2 0 4 11
1 1
5 1 3 0 3 3
2 50
2 1 2
2 1 2
1 1
7 1 2 4 1 4 9 5
4 114514
2 2 2
5 7 3 6 0 3
3 0 1 1
5 0 9 2 1 5
5 1919810
1 2
2 324003 0
3 1416324 2 1460728
4 1312631 2 0 1415195
5 1223554 192248 2 1492515 725556
output:
16
18
1281
4
6556785365
1842836177961
Note: 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$.
|
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:

In the second test case, the color of all cells must be pairwise different, so the answer is $5$.
|
Title: Distanced Coloring
time_limit_ms: 1000
memory_limit_mb: 256
Description: 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.
Input: 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.
Output: For each test case, print a single integer β the minimum number of colors needed to color the grid.
Examples:
input:
6
3 3 2
5 1 10000
7 3 4
3 2 7
8 9 6
2 5 4
output:
4
5
12
6
36
8
Note: In the first test case, one of the optimal constructions is:

In the second test case, the color of all cells must be pairwise different, so the answer is $5$.
|
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$.
|
Title: The Strict Teacher (Hard Version)
time_limit_ms: 1500
memory_limit_mb: 256
Description: 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.
Input: 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$.
Output: For each test case, output $q$ lines, the $i$-th of them containing the answer of the $i$-th query.
Examples:
input:
2
8 1 1
6
3
10 3 3
1 4 8
2 3 10
output:
5
1
1
2
Note: 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$.
|
2020A
|
https://codeforces.com/problemset/problem/2020/A
|
Find Minimum Operations
| 800
|
[
"bitmasks",
"brute force",
"greedy",
"math",
"number theory"
] |
Div. 2
| 1,000
| 256
|
You are given two integers $n$ and $k$.
In one operation, you can subtract any power of $k$ from $n$. Formally, in one operation, you can replace $n$ by $(n-k^x)$ for any non-negative integer $x$.
Find the minimum number of operations required to make $n$ equal to $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 only line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 10^9$).
|
For each test case, output the minimum number of operations on a new line.
|
[
[
"6\n5 2\n3 5\n16 4\n100 3\n6492 10\n10 1",
"2\n3\n1\n4\n21\n10"
]
] |
In the first test case, $n = 5$ and $k = 2$. We can perform the following sequence of operations:
1. Subtract $2^0 = 1$ from $5$. The current value of $n$ becomes $5 - 1 = 4$. 2. Subtract $2^2 = 4$ from $4$. The current value of $n$ becomes $4 - 4 = 0$.
It can be shown that there is no way to make $n$ equal to $0$ in less than $2$ operations. Thus, $2$ is the answer.
In the second test case, $n = 3$ and $k = 5$. We can perform the following sequence of operations:
1. Subtract $5^0 = 1$ from $3$. The current value of $n$ becomes $3 - 1 = 2$. 2. Subtract $5^0 = 1$ from $2$. The current value of $n$ becomes $2 - 1 = 1$. 3. Subtract $5^0 = 1$ from $1$. The current value of $n$ becomes $1 - 1 = 0$.
It can be shown that there is no way to make $n$ equal to $0$ in less than $3$ operations. Thus, $3$ is the answer.
|
Title: Find Minimum Operations
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given two integers $n$ and $k$.
In one operation, you can subtract any power of $k$ from $n$. Formally, in one operation, you can replace $n$ by $(n-k^x)$ for any non-negative integer $x$.
Find the minimum number of operations required to make $n$ equal to $0$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The only line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 10^9$).
Output: For each test case, output the minimum number of operations on a new line.
Examples:
input:
6
5 2
3 5
16 4
100 3
6492 10
10 1
output:
2
3
1
4
21
10
Note: In the first test case, $n = 5$ and $k = 2$. We can perform the following sequence of operations:
1. Subtract $2^0 = 1$ from $5$. The current value of $n$ becomes $5 - 1 = 4$. 2. Subtract $2^2 = 4$ from $4$. The current value of $n$ becomes $4 - 4 = 0$.
It can be shown that there is no way to make $n$ equal to $0$ in less than $2$ operations. Thus, $2$ is the answer.
In the second test case, $n = 3$ and $k = 5$. We can perform the following sequence of operations:
1. Subtract $5^0 = 1$ from $3$. The current value of $n$ becomes $3 - 1 = 2$. 2. Subtract $5^0 = 1$ from $2$. The current value of $n$ becomes $2 - 1 = 1$. 3. Subtract $5^0 = 1$ from $1$. The current value of $n$ becomes $1 - 1 = 0$.
It can be shown that there is no way to make $n$ equal to $0$ in less than $3$ operations. Thus, $3$ is the answer.
|
1985C
|
https://codeforces.com/problemset/problem/1985/C
|
Good Prefixes
| 1,000
|
[
"greedy"
] |
Div. 4
| 2,000
| 256
|
Alex thinks some array is good if there exists some element that can be represented as the sum of all other elements (the sum of all other elements is $0$ if there are no other elements). For example, the array $[1,6,3,2]$ is good since $1+3+2=6$. Furthermore, the array $[0]$ is also good. However, the arrays $[1,2,3,4]$ and $[1]$ are not good.
Alex has an array $a_1,a_2,\ldots,a_n$. Help him count the number of good non-empty prefixes of the array $a$. In other words, count the number of integers $i$ ($1 \le i \le n$) such that the length $i$ prefix (i.e. $a_1,a_2,\ldots,a_i$) is good.
|
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 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 $a_1,a_2,\ldots,a_n$ ($0 \le a_i \le 10^9$) β the elements of the 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 number of good non-empty prefixes of the array $a$.
|
[
[
"7\n1\n0\n1\n1\n4\n1 1 2 0\n5\n0 1 2 1 4\n7\n1 1 0 3 5 2 12\n7\n1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 294967296\n10\n0 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 589934592",
"1\n0\n3\n3\n4\n1\n2"
]
] |
In the fourth test case, the array has five prefixes:
* prefix $[0]$ is a good array, as mentioned in the statement; * prefix $[0, 1]$ is not a good array, since $0 \ne 1$; * prefix $[0, 1, 2]$ is not a good array, since $0 \ne 1 + 2$, $1 \ne 0 + 2$ and $2 \ne 0 + 1$; * prefix $[0, 1, 2, 1]$ is a good array, since $2 = 0 + 1 + 1$; * prefix $[0, 1, 2, 1, 4]$ is a good array, since $4 = 0 + 1 + 2 + 1$.
As you can see, three of them are good, so the answer is $3$.
|
Title: Good Prefixes
time_limit_ms: 2000
memory_limit_mb: 256
Description: Alex thinks some array is good if there exists some element that can be represented as the sum of all other elements (the sum of all other elements is $0$ if there are no other elements). For example, the array $[1,6,3,2]$ is good since $1+3+2=6$. Furthermore, the array $[0]$ is also good. However, the arrays $[1,2,3,4]$ and $[1]$ are not good.
Alex has an array $a_1,a_2,\ldots,a_n$. Help him count the number of good non-empty prefixes of the array $a$. In other words, count the number of integers $i$ ($1 \le i \le n$) such that the length $i$ prefix (i.e. $a_1,a_2,\ldots,a_i$) is good.
Input: 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 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 $a_1,a_2,\ldots,a_n$ ($0 \le a_i \le 10^9$) β the elements of the array.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output a single integer β the number of good non-empty prefixes of the array $a$.
Examples:
input:
7
1
0
1
1
4
1 1 2 0
5
0 1 2 1 4
7
1 1 0 3 5 2 12
7
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 294967296
10
0 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 589934592
output:
1
0
3
3
4
1
2
Note: In the fourth test case, the array has five prefixes:
* prefix $[0]$ is a good array, as mentioned in the statement; * prefix $[0, 1]$ is not a good array, since $0 \ne 1$; * prefix $[0, 1, 2]$ is not a good array, since $0 \ne 1 + 2$, $1 \ne 0 + 2$ and $2 \ne 0 + 1$; * prefix $[0, 1, 2, 1]$ is a good array, since $2 = 0 + 1 + 1$; * prefix $[0, 1, 2, 1, 4]$ is a good array, since $4 = 0 + 1 + 2 + 1$.
As you can see, three of them are good, so the answer is $3$.
|
1990F
|
https://codeforces.com/problemset/problem/1990/F
|
Polygonal Segments
| 2,800
|
[
"brute force",
"data structures",
"divide and conquer",
"dp",
"greedy",
"two pointers"
] |
Div. 2
| 8,000
| 512
|
You are given an array $a$ of size $n$.
A segment $[l, r](1 \le l < r \le n)$ is called a polygonal segment only if the following conditions hold:
* $(r-l+1) \geq 3$; * Considering $a_l, a_{l+1}, \ldots, a_r$ as side lengths, these sides can form a polygon with $(r-l+1)$ sides.
Process $q$ queries of two types:
* "1 l r": find the length of the longest segment among all polygonal segments $[l_0,r_0]$ satisfying $l \le l_0 \le r_0 \le r$. If there is no such polygonal segment, output $-1$ instead; * "2 i x": assign $a_i := x$.
|
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
For each test case:
* The first line of each testcase contains two integers $n$, $q$ ($4 \le n \le 2\cdot 10^5$, $1 \le q \le 10^5$); * The second line of each testcase contains $n$ integers $a_1,a_2,\ldots, a_n$ ($1 \le a_i \le 10^{12}$); * The following $q$ lines contain the description of queries. Each line is of one of two types: * "1 l r" ($1 \le l < r \le n$, $r-l+1\ge 3$); * "2 i x" ($1 \le i \le n$, $1 \le x \le 10^{12}$).
It is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$, and the sum of $q$ over all test cases will not exceed $10^5$.
|
For each query, if there is no suitable segment, output $-1$ in a new line. Otherwise, output the length of the longest segment satisfying the condition above in a new line.
|
[
[
"2\n5 6\n3 1 2 2 8\n1 1 3\n1 1 4\n1 1 5\n2 1 5\n1 1 4\n1 1 5\n4 10\n500000000000 500000000000 1000000000000 500000000000\n1 1 3\n1 2 4\n1 1 4\n2 1 499999999999\n2 3 999999999999\n1 1 3\n1 2 4\n1 1 4\n2 3 1000000000000\n1 1 3",
"-1\n4\n4\n3\n5\n-1\n-1\n4\n-1\n3\n4\n-1"
]
] |
In the first query of the first test case, there is no polygonal segment under the given condition. For example, considering segment $[1,3]$, you can not form a triangle with side lengths of $a_1=3$, $a_2=1$, and $a_3=2$.
In the second query of the first test case, the longest polygonal segment is $[1,4]$. You can form a quadrilateral with side lengths of $a_1=3$, $a_2=1$, $a_3=2$, and $a_4=2$.
 An example of a quadrilateral with side lengths of $3$, $1$, $2$, and $2$.
|
Title: Polygonal Segments
time_limit_ms: 8000
memory_limit_mb: 512
Description: You are given an array $a$ of size $n$.
A segment $[l, r](1 \le l < r \le n)$ is called a polygonal segment only if the following conditions hold:
* $(r-l+1) \geq 3$; * Considering $a_l, a_{l+1}, \ldots, a_r$ as side lengths, these sides can form a polygon with $(r-l+1)$ sides.
Process $q$ queries of two types:
* "1 l r": find the length of the longest segment among all polygonal segments $[l_0,r_0]$ satisfying $l \le l_0 \le r_0 \le r$. If there is no such polygonal segment, output $-1$ instead; * "2 i x": assign $a_i := x$.
Input: The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
For each test case:
* The first line of each testcase contains two integers $n$, $q$ ($4 \le n \le 2\cdot 10^5$, $1 \le q \le 10^5$); * The second line of each testcase contains $n$ integers $a_1,a_2,\ldots, a_n$ ($1 \le a_i \le 10^{12}$); * The following $q$ lines contain the description of queries. Each line is of one of two types: * "1 l r" ($1 \le l < r \le n$, $r-l+1\ge 3$); * "2 i x" ($1 \le i \le n$, $1 \le x \le 10^{12}$).
It is guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$, and the sum of $q$ over all test cases will not exceed $10^5$.
Output: For each query, if there is no suitable segment, output $-1$ in a new line. Otherwise, output the length of the longest segment satisfying the condition above in a new line.
Examples:
input:
2
5 6
3 1 2 2 8
1 1 3
1 1 4
1 1 5
2 1 5
1 1 4
1 1 5
4 10
500000000000 500000000000 1000000000000 500000000000
1 1 3
1 2 4
1 1 4
2 1 499999999999
2 3 999999999999
1 1 3
1 2 4
1 1 4
2 3 1000000000000
1 1 3
output:
-1
4
4
3
5
-1
-1
4
-1
3
4
-1
Note: In the first query of the first test case, there is no polygonal segment under the given condition. For example, considering segment $[1,3]$, you can not form a triangle with side lengths of $a_1=3$, $a_2=1$, and $a_3=2$.
In the second query of the first test case, the longest polygonal segment is $[1,4]$. You can form a quadrilateral with side lengths of $a_1=3$, $a_2=1$, $a_3=2$, and $a_4=2$.
 An example of a quadrilateral with side lengths of $3$, $1$, $2$, and $2$.
|
1997F
|
https://codeforces.com/problemset/problem/1997/F
|
Chips on a Line
| 2,700
|
[
"brute force",
"combinatorics",
"dp",
"greedy",
"math"
] |
Div. 2
| 5,000
| 512
|
You have $n$ chips, and you are going to place all of them in one of $x$ points, numbered from $1$ to $x$. There can be multiple chips in each point.
After placing the chips, you can perform the following four operations (in any order, any number of times):
* choose a chip in point $i \ge 3$, remove it and place two chips: one in $i-1$, one in $i-2$; * choose two chips in adjacent points $i$ and $i+1$, remove them and place a new chip in $i+2$; * choose a chip in point $1$ and move it to $2$; * choose a chip in point $2$ and move it to $1$.
Note that the coordinates of the chips you place during the operations cannot be less than $1$, but can be greater than $x$.
Denote the cost of chip placement as the minimum number of chips which can be present on the line after you perform these operations, starting from the placement you've chosen.
For example, the cost of placing two chips in points $3$ and one chip in point $5$ is $2$, because you can reduce the number of chips to $2$ as follows:
* choose a chip in point $3$, remove it, place a chip in $1$ and another chip in $2$; * choose the chips in points $2$ and $3$, remove them and place a chip in $4$; * choose the chips in points $4$ and $5$, remove them and place a chip in $6$.
You are given three integers $n$, $x$ and $m$. Calculate the number of placements of exactly $n$ chips in points from $1$ to $x$ having cost equal to $m$, and print it modulo $998244353$. Two placements are considered different if the number of chips in some point differs between these placements.
|
The only line contains three integers $n$, $x$ and $m$ ($1 \le m \le n \le 1000$; $2 \le x \le 10$).
|
Print one integer β the number of placements with cost equal to $m$, taken modulo $998244353$.
|
[
[
"2 3 1",
"5"
],
[
"42 10 5",
"902673363"
],
[
"1000 10 8",
"187821763"
]
] |
In the first example, there are five ways to place $2$ chips in points from $1$ to $3$ so that the cost is $1$:
* $(1, 1)$; * $(1, 2)$; * $(1, 3)$; * $(2, 2)$; * $(2, 3)$.
|
Title: Chips on a Line
time_limit_ms: 5000
memory_limit_mb: 512
Description: You have $n$ chips, and you are going to place all of them in one of $x$ points, numbered from $1$ to $x$. There can be multiple chips in each point.
After placing the chips, you can perform the following four operations (in any order, any number of times):
* choose a chip in point $i \ge 3$, remove it and place two chips: one in $i-1$, one in $i-2$; * choose two chips in adjacent points $i$ and $i+1$, remove them and place a new chip in $i+2$; * choose a chip in point $1$ and move it to $2$; * choose a chip in point $2$ and move it to $1$.
Note that the coordinates of the chips you place during the operations cannot be less than $1$, but can be greater than $x$.
Denote the cost of chip placement as the minimum number of chips which can be present on the line after you perform these operations, starting from the placement you've chosen.
For example, the cost of placing two chips in points $3$ and one chip in point $5$ is $2$, because you can reduce the number of chips to $2$ as follows:
* choose a chip in point $3$, remove it, place a chip in $1$ and another chip in $2$; * choose the chips in points $2$ and $3$, remove them and place a chip in $4$; * choose the chips in points $4$ and $5$, remove them and place a chip in $6$.
You are given three integers $n$, $x$ and $m$. Calculate the number of placements of exactly $n$ chips in points from $1$ to $x$ having cost equal to $m$, and print it modulo $998244353$. Two placements are considered different if the number of chips in some point differs between these placements.
Input: The only line contains three integers $n$, $x$ and $m$ ($1 \le m \le n \le 1000$; $2 \le x \le 10$).
Output: Print one integer β the number of placements with cost equal to $m$, taken modulo $998244353$.
Examples:
input:
2 3 1
output:
5
input:
42 10 5
output:
902673363
input:
1000 10 8
output:
187821763
Note: In the first example, there are five ways to place $2$ chips in points from $1$ to $3$ so that the cost is $1$:
* $(1, 1)$; * $(1, 2)$; * $(1, 3)$; * $(2, 2)$; * $(2, 3)$.
|
1990D
|
https://codeforces.com/problemset/problem/1990/D
|
Grid Puzzle
| 1,800
|
[
"bitmasks",
"brute force",
"dp",
"greedy",
"implementation"
] |
Div. 2
| 2,000
| 256
|
You are given an array $a$ of size $n$.
There is an $n \times n$ grid. In the $i$-th row, the first $a_i$ cells are black and the other cells are white. In other words, note $(i,j)$ as the cell in the $i$-th row and $j$-th column, cells $(i,1), (i,2), \ldots, (i,a_i)$ are black, and cells $(i,a_i+1), \ldots, (i,n)$ are white.
You can do the following operations any number of times in any order:
* Dye a $2 \times 2$ subgrid white; * Dye a whole row white. Note you can not dye a whole column white.
Find the minimum number of operations to dye all cells white.
|
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
For each test case:
* The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the size of the array $a$. * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq n$).
It's guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
|
For each test case, output a single integer β the minimum number of operations to dye all cells white.
|
[
[
"10\n1\n0\n4\n2 4 4 2\n4\n3 2 1 0\n3\n0 3 0\n3\n0 1 3\n3\n3 1 0\n4\n3 1 0 3\n4\n0 2 2 2\n6\n1 3 4 2 0 4\n8\n2 2 5 2 3 4 2 4",
"0\n3\n2\n1\n2\n2\n3\n2\n4\n6"
]
] |
In the first test case, you don't need to do any operation.
In the second test case, you can do:
* Dye $(1,1), (1,2), (2,1)$, and $(2,2)$ white; * Dye $(2,3), (2,4), (3,3)$, and $(3,4)$ white; * Dye $(3,1), (3,2), (4,1)$, and $(4,2)$ white.
It can be proven $3$ is the minimum number of operations.
In the third test case, you can do:
* Dye the first row white; * Dye $(2,1), (2,2), (3,1)$, and $(3,2)$ white.
It can be proven $2$ is the minimum number of operations.
|
Title: Grid Puzzle
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given an array $a$ of size $n$.
There is an $n \times n$ grid. In the $i$-th row, the first $a_i$ cells are black and the other cells are white. In other words, note $(i,j)$ as the cell in the $i$-th row and $j$-th column, cells $(i,1), (i,2), \ldots, (i,a_i)$ are black, and cells $(i,a_i+1), \ldots, (i,n)$ are white.
You can do the following operations any number of times in any order:
* Dye a $2 \times 2$ subgrid white; * Dye a whole row white. Note you can not dye a whole column white.
Find the minimum number of operations to dye all cells white.
Input: The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
For each test case:
* The first line contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the size of the array $a$. * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq n$).
It's guaranteed that the sum of $n$ over all test cases will not exceed $2 \cdot 10^5$.
Output: For each test case, output a single integer β the minimum number of operations to dye all cells white.
Examples:
input:
10
1
0
4
2 4 4 2
4
3 2 1 0
3
0 3 0
3
0 1 3
3
3 1 0
4
3 1 0 3
4
0 2 2 2
6
1 3 4 2 0 4
8
2 2 5 2 3 4 2 4
output:
0
3
2
1
2
2
3
2
4
6
Note: In the first test case, you don't need to do any operation.
In the second test case, you can do:
* Dye $(1,1), (1,2), (2,1)$, and $(2,2)$ white; * Dye $(2,3), (2,4), (3,3)$, and $(3,4)$ white; * Dye $(3,1), (3,2), (4,1)$, and $(4,2)$ white.
It can be proven $3$ is the minimum number of operations.
In the third test case, you can do:
* Dye the first row white; * Dye $(2,1), (2,2), (3,1)$, and $(3,2)$ white.
It can be proven $2$ is the minimum number of operations.
|
1997E
|
https://codeforces.com/problemset/problem/1997/E
|
Level Up
| 2,200
|
[
"binary search",
"brute force",
"data structures",
"divide and conquer",
"implementation"
] |
Div. 2
| 4,000
| 512
|
Monocarp is playing a computer game. He starts the game being level $1$. He is about to fight $n$ monsters, in order from $1$ to $n$. The level of the $i$-th monster is $a_i$.
For each monster in the given order, Monocarp's encounter goes as follows:
* if Monocarp's level is strictly higher than the monster's level, the monster flees (runs away); * otherwise, Monocarp fights the monster.
After every $k$-th fight with a monster (fleeing monsters do not count), Monocarp's level increases by $1$. So, his level becomes $2$ after $k$ monsters he fights, $3$ after $2k$ monsters, $4$ after $3k$ monsters, and so on.
You need to process $q$ queries of the following form:
* $i~x$: will Monocarp fight the $i$-th monster (or will this monster flee) if the parameter $k$ is equal to $x$?
|
The first line contains two integers $n$ and $q$ ($1 \le n, q \le 2 \cdot 10^5$) β the number of monsters and the number of queries.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) β the levels of the monsters.
In the $j$-th of the following $q$ lines, two integers $i$ and $x$ ($1 \le i, x \le n$) β the index of the monster and the number of fights required for a level up in the $j$-th query.
|
For each query, output "YES", if Monocarp will fight the $i$-th monster in this query, and "NO", if the $i$-th monster flees.
|
[
[
"4 16\n2 1 2 1\n1 1\n2 1\n3 1\n4 1\n1 2\n2 2\n3 2\n4 2\n1 3\n2 3\n3 3\n4 3\n1 4\n2 4\n3 4\n4 4",
"YES\nNO\nYES\nNO\nYES\nYES\nYES\nNO\nYES\nYES\nYES\nNO\nYES\nYES\nYES\nYES"
],
[
"7 15\n1 1 2 1 1 1 1\n5 3\n2 2\n2 2\n1 6\n5 1\n5 5\n7 7\n3 5\n7 4\n4 3\n2 5\n1 2\n5 6\n4 1\n6 1",
"NO\nYES\nYES\nYES\nNO\nYES\nYES\nYES\nNO\nNO\nYES\nYES\nYES\nNO\nNO"
]
] |
Title: Level Up
time_limit_ms: 4000
memory_limit_mb: 512
Description: Monocarp is playing a computer game. He starts the game being level $1$. He is about to fight $n$ monsters, in order from $1$ to $n$. The level of the $i$-th monster is $a_i$.
For each monster in the given order, Monocarp's encounter goes as follows:
* if Monocarp's level is strictly higher than the monster's level, the monster flees (runs away); * otherwise, Monocarp fights the monster.
After every $k$-th fight with a monster (fleeing monsters do not count), Monocarp's level increases by $1$. So, his level becomes $2$ after $k$ monsters he fights, $3$ after $2k$ monsters, $4$ after $3k$ monsters, and so on.
You need to process $q$ queries of the following form:
* $i~x$: will Monocarp fight the $i$-th monster (or will this monster flee) if the parameter $k$ is equal to $x$?
Input: The first line contains two integers $n$ and $q$ ($1 \le n, q \le 2 \cdot 10^5$) β the number of monsters and the number of queries.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) β the levels of the monsters.
In the $j$-th of the following $q$ lines, two integers $i$ and $x$ ($1 \le i, x \le n$) β the index of the monster and the number of fights required for a level up in the $j$-th query.
Output: For each query, output "YES", if Monocarp will fight the $i$-th monster in this query, and "NO", if the $i$-th monster flees.
Examples:
input:
4 16
2 1 2 1
1 1
2 1
3 1
4 1
1 2
2 2
3 2
4 2
1 3
2 3
3 3
4 3
1 4
2 4
3 4
4 4
output:
YES
NO
YES
NO
YES
YES
YES
NO
YES
YES
YES
NO
YES
YES
YES
YES
input:
7 15
1 1 2 1 1 1 1
5 3
2 2
2 2
1 6
5 1
5 5
7 7
3 5
7 4
4 3
2 5
1 2
5 6
4 1
6 1
output:
NO
YES
YES
YES
NO
YES
YES
YES
NO
NO
YES
YES
YES
NO
NO
Note:
|
|
1993F2
|
https://codeforces.com/problemset/problem/1993/F2
|
Dyn-scripted Robot (Hard Version)
| 2,800
|
[
"chinese remainder theorem",
"math",
"number theory"
] |
Div. 2
| 3,000
| 256
|
This is the hard version of the problem. The only difference is that in this version $k \le 10^{12}$. You can make hacks only if both versions of the problem are solved.
Given a $w \times h$ rectangle on the $Oxy$ plane, with points $(0, 0)$ at the bottom-left and $(w, h)$ at the top-right of the rectangle.
You also have a robot initially at point $(0, 0)$ and a script $s$ of $n$ characters. Each character is either L, R, U, or D, which tells the robot to move left, right, up, or down respectively.
The robot can only move inside the rectangle; otherwise, it will change the script $s$ as follows:
* If it tries to move outside a vertical border, it changes all L characters to R's (and vice versa, all R's to L's). * If it tries to move outside a horizontal border, it changes all U characters to D's (and vice versa, all D's to U's).
Then, it will execute the changed script starting from the character which it couldn't execute.
 An example of the robot's movement process, $s = \texttt{"ULULURD"}$
The script $s$ will be executed for $k$ times continuously. All changes to the string $s$ will be retained even when it is repeated. During this process, how many times will the robot move to the point $(0, 0)$ in total? Note that the initial position does NOT count.
|
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 four integers $n$, $k$, $w$, and $h$ ($1 \le n, w, h \le 10^6$; $1 \le k \le 10^{12}$).
The second line contains a single string $s$ of size $n$ ($s_i \in \\{\texttt{L}, \texttt{R}, \texttt{U}, \texttt{D}\\}$) β the script to be executed.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
|
For each test case, print a single integer β the number of times the robot reaches $(0, 0)$ when executing script $s$ for $k$ times continuously.
|
[
[
"6\n2 4 2 2\nUR\n4 2 1 1\nLLDD\n6 3 3 1\nRLRRRL\n5 6 3 3\nRUURD\n7 5 3 4\nRRDLUUU\n7 123456789999 3 2\nULULURD",
"1\n4\n3\n1\n1\n41152263332"
]
] |
In the first test case, the robot only moves up and right for the first two executions. After that, it occupies the position $(2, 2)$. For the next two executions, it moves down and left and finishes at $(0, 0)$. So the answer is $1$.
In the second test case, each time executing the script the robot visits the origin twice. And since $k=2$, it visits the origin $2 \cdot 2 = 4$ times overall.

In the third test case, the visualization is shown as below:

|
Title: Dyn-scripted Robot (Hard Version)
time_limit_ms: 3000
memory_limit_mb: 256
Description: This is the hard version of the problem. The only difference is that in this version $k \le 10^{12}$. You can make hacks only if both versions of the problem are solved.
Given a $w \times h$ rectangle on the $Oxy$ plane, with points $(0, 0)$ at the bottom-left and $(w, h)$ at the top-right of the rectangle.
You also have a robot initially at point $(0, 0)$ and a script $s$ of $n$ characters. Each character is either L, R, U, or D, which tells the robot to move left, right, up, or down respectively.
The robot can only move inside the rectangle; otherwise, it will change the script $s$ as follows:
* If it tries to move outside a vertical border, it changes all L characters to R's (and vice versa, all R's to L's). * If it tries to move outside a horizontal border, it changes all U characters to D's (and vice versa, all D's to U's).
Then, it will execute the changed script starting from the character which it couldn't execute.
 An example of the robot's movement process, $s = \texttt{"ULULURD"}$
The script $s$ will be executed for $k$ times continuously. All changes to the string $s$ will be retained even when it is repeated. During this process, how many times will the robot move to the point $(0, 0)$ in total? Note that the initial position does NOT count.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains four integers $n$, $k$, $w$, and $h$ ($1 \le n, w, h \le 10^6$; $1 \le k \le 10^{12}$).
The second line contains a single string $s$ of size $n$ ($s_i \in \\{\texttt{L}, \texttt{R}, \texttt{U}, \texttt{D}\\}$) β the script to be executed.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
Output: For each test case, print a single integer β the number of times the robot reaches $(0, 0)$ when executing script $s$ for $k$ times continuously.
Examples:
input:
6
2 4 2 2
UR
4 2 1 1
LLDD
6 3 3 1
RLRRRL
5 6 3 3
RUURD
7 5 3 4
RRDLUUU
7 123456789999 3 2
ULULURD
output:
1
4
3
1
1
41152263332
Note: In the first test case, the robot only moves up and right for the first two executions. After that, it occupies the position $(2, 2)$. For the next two executions, it moves down and left and finishes at $(0, 0)$. So the answer is $1$.
In the second test case, each time executing the script the robot visits the origin twice. And since $k=2$, it visits the origin $2 \cdot 2 = 4$ times overall.

In the third test case, the visualization is shown as below:

|
1975E
|
https://codeforces.com/problemset/problem/1975/E
|
Chain Queries
| 2,100
|
[
"binary search",
"data structures",
"dfs and similar",
"implementation",
"trees"
] |
Div. 1 + 2
| 2,000
| 256
|
You are given a tree of $n$ vertices numbered from $1$ to $n$. Initially, all vertices are colored white or black.
You are asked to perform $q$ queries:
* "u" β toggle the color of vertex $u$ (if it was white, change it to black and vice versa).
After each query, you should answer whether all the black vertices form a chain. That is, there exist two black vertices such that the simple path between them passes through all the black vertices and only the black vertices. Specifically, if there is only one black vertex, they form a chain. If there are no black vertices, they do not form a chain.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $q$ ($1\leq n,q\leq 2\cdot 10^5$).
The second line of each test case contains $n$ integers $c_1,c_2,\ldots,c_n$ ($c_i \in \\{ \mathtt{0}, \mathtt{1} \\}$) β the initial color of the vertices. $c_i$ denotes the color of vertex $i$ where $\mathtt{0}$ denotes the color white, and $\mathtt{1}$ denotes the color black.
Then $n - 1$ lines follow, each line contains two integers $x_i$ and $y_i$ ($1 \le x_i,y_i \le n$), indicating an edge between vertices $x_i$ and $y_i$. It is guaranteed that these edges form a tree.
The following $q$ lines each contain an integer $u_i$ ($1 \le u_i \le n$), indicating the color of vertex $u_i$ needs to be toggled.
It is guaranteed that the sum of $n$ and $q$ over all test cases respectively does not exceed $2\cdot 10^5$.
|
For each query, output "Yes" if the black vertices form a chain, and output "No" otherwise.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
|
[
[
"2\n2 1\n1 0\n1 2\n1\n5 4\n1 0 0 0 0\n1 2\n1 3\n1 5\n3 4\n4\n3\n2\n5",
"No\nNo\nYes\nYes\nNo"
],
[
"4\n5 3\n1 1 1 1 1\n3 5\n2 5\n3 4\n1 5\n1\n1\n1\n4 4\n0 0 0 0\n1 2\n2 3\n1 4\n1\n2\n3\n2\n1 1\n1\n1\n1 1\n0\n1",
"Yes\nNo\nYes\nYes\nYes\nYes\nNo\nNo\nYes"
]
] |
In the second test case, the color of the vertices are as follows:
The initial tree:

The first query toggles the color of vertex $4$:

The second query toggles the color of vertex $3$:

The third query toggles the color of vertex $2$:

The fourth query toggles the color of vertex $5$:

|
Title: Chain Queries
time_limit_ms: 2000
memory_limit_mb: 256
Description: You are given a tree of $n$ vertices numbered from $1$ to $n$. Initially, all vertices are colored white or black.
You are asked to perform $q$ queries:
* "u" β toggle the color of vertex $u$ (if it was white, change it to black and vice versa).
After each query, you should answer whether all the black vertices form a chain. That is, there exist two black vertices such that the simple path between them passes through all the black vertices and only the black vertices. Specifically, if there is only one black vertex, they form a chain. If there are no black vertices, they do not form a chain.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $q$ ($1\leq n,q\leq 2\cdot 10^5$).
The second line of each test case contains $n$ integers $c_1,c_2,\ldots,c_n$ ($c_i \in \\{ \mathtt{0}, \mathtt{1} \\}$) β the initial color of the vertices. $c_i$ denotes the color of vertex $i$ where $\mathtt{0}$ denotes the color white, and $\mathtt{1}$ denotes the color black.
Then $n - 1$ lines follow, each line contains two integers $x_i$ and $y_i$ ($1 \le x_i,y_i \le n$), indicating an edge between vertices $x_i$ and $y_i$. It is guaranteed that these edges form a tree.
The following $q$ lines each contain an integer $u_i$ ($1 \le u_i \le n$), indicating the color of vertex $u_i$ needs to be toggled.
It is guaranteed that the sum of $n$ and $q$ over all test cases respectively does not exceed $2\cdot 10^5$.
Output: For each query, output "Yes" if the black vertices form a chain, and output "No" otherwise.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
Examples:
input:
2
2 1
1 0
1 2
1
5 4
1 0 0 0 0
1 2
1 3
1 5
3 4
4
3
2
5
output:
No
No
Yes
Yes
No
input:
4
5 3
1 1 1 1 1
3 5
2 5
3 4
1 5
1
1
1
4 4
0 0 0 0
1 2
2 3
1 4
1
2
3
2
1 1
1
1
1 1
0
1
output:
Yes
No
Yes
Yes
Yes
Yes
No
No
Yes
Note: In the second test case, the color of the vertices are as follows:
The initial tree:

The first query toggles the color of vertex $4$:

The second query toggles the color of vertex $3$:

The third query toggles the color of vertex $2$:

The fourth query toggles the color of vertex $5$:

|
1977A
|
https://codeforces.com/problemset/problem/1977/A
|
Little Nikita
| 800
|
[
"math"
] |
Div. 2
| 1,000
| 256
|
The little boy Nikita was given some cubes as a present. He decided to build a tower out of them.
Initially, the tower doesn't have any cubes. In one move, Nikita either puts exactly $1$ cube on top of the tower or removes exactly $1$ cube from the top of the tower. Is it possible that after $n$ moves, the resulting tower has exactly $m$ cubes?
|
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. The description of the test cases follows.
The only line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 100$).
|
For each test case, output "Yes" (without quotes) if Nikita can obtain a tower with $m$ cubes, and "No" (without quotes) 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.
|
[
[
"3\n3 3\n2 4\n5 3",
"Yes\nNo\nYes"
]
] |
In the first test case, Nikita can put $1$ cube on top of the tower $3$ times in a row, so the answer is "Yes".
In the second test case, Nikita can only end up with either a tower with no blocks or a tower with $2$ blocks, so the answer is "No".
|
Title: Little Nikita
time_limit_ms: 1000
memory_limit_mb: 256
Description: The little boy Nikita was given some cubes as a present. He decided to build a tower out of them.
Initially, the tower doesn't have any cubes. In one move, Nikita either puts exactly $1$ cube on top of the tower or removes exactly $1$ cube from the top of the tower. Is it possible that after $n$ moves, the resulting tower has exactly $m$ cubes?
Input: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases. The description of the test cases follows.
The only line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 100$).
Output: For each test case, output "Yes" (without quotes) if Nikita can obtain a tower with $m$ cubes, and "No" (without quotes) 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.
Examples:
input:
3
3 3
2 4
5 3
output:
Yes
No
Yes
Note: In the first test case, Nikita can put $1$ cube on top of the tower $3$ times in a row, so the answer is "Yes".
In the second test case, Nikita can only end up with either a tower with no blocks or a tower with $2$ blocks, so the answer is "No".
|
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.
|
Title: Complex Segments (Hard Version)
time_limit_ms: 13000
memory_limit_mb: 256
Description: [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.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). The description of the test cases follows.
The first line of each test case contains 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$.
Output: For each test case, output a single integer: the maximum size of a complex subset of the given segments.
Examples:
input:
3
3
1 2 3
5 4 6
5
1 2 3 6 8
5 4 7 9 10
5
3 1 4 1 5
7 2 6 5 10
output:
3
4
4
Note: 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.
|
1996G
|
https://codeforces.com/problemset/problem/1996/G
|
Penacony
| 2,200
|
[
"brute force",
"data structures",
"graphs",
"greedy",
"hashing"
] |
Div. 3
| 3,000
| 512
|
On Penacony, The Land of the Dreams, there exists $n$ houses and $n$ roads. There exists a road between house $i$ and $i+1$ for all $1 \leq i \leq n-1$ and a road between house $n$ and house $1$. All roads are bidirectional. However, due to the crisis on Penacony, the overseeing family has gone into debt and may not be able to maintain all roads.
There are $m$ pairs of friendships between the residents of Penacony. If the resident living in house $a$ is friends with the resident living in house $b$, there must be a path between houses $a$ and $b$ through maintained roads.
What is the minimum number of roads that must be maintained?
|
The first line contains $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($3 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 2 \cdot 10^5$) β the number of houses and the number of friendships.
The next $m$ lines contain two integers $a$ and $b$ ($1 \leq a < b \leq n$) β the resident in house $a$ is friends with the resident in house $b$. It is guaranteed all ($a, b$) are distinct.
It is guaranteed the sum of $n$ and $m$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output an integer, the minimum number of roads that must be maintained.
|
[
[
"7\n8 3\n1 8\n2 7\n4 5\n13 4\n1 13\n2 12\n3 11\n4 10\n10 2\n2 3\n3 4\n10 4\n3 8\n5 10\n2 10\n4 10\n4 1\n1 3\n5 2\n3 5\n1 4\n5 2\n2 5\n1 3",
"4\n7\n2\n7\n2\n3\n3"
]
] |
For the first test case, the following roads must be maintained:
* $8 \leftarrow \rightarrow 1$ * $7 \leftarrow \rightarrow 8$ * $1 \leftarrow \rightarrow 2$ * $4 \leftarrow \rightarrow 5$
|
Title: Penacony
time_limit_ms: 3000
memory_limit_mb: 512
Description: On Penacony, The Land of the Dreams, there exists $n$ houses and $n$ roads. There exists a road between house $i$ and $i+1$ for all $1 \leq i \leq n-1$ and a road between house $n$ and house $1$. All roads are bidirectional. However, due to the crisis on Penacony, the overseeing family has gone into debt and may not be able to maintain all roads.
There are $m$ pairs of friendships between the residents of Penacony. If the resident living in house $a$ is friends with the resident living in house $b$, there must be a path between houses $a$ and $b$ through maintained roads.
What is the minimum number of roads that must be maintained?
Input: The first line contains $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains two integers $n$ and $m$ ($3 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 2 \cdot 10^5$) β the number of houses and the number of friendships.
The next $m$ lines contain two integers $a$ and $b$ ($1 \leq a < b \leq n$) β the resident in house $a$ is friends with the resident in house $b$. It is guaranteed all ($a, b$) are distinct.
It is guaranteed the sum of $n$ and $m$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output an integer, the minimum number of roads that must be maintained.
Examples:
input:
7
8 3
1 8
2 7
4 5
13 4
1 13
2 12
3 11
4 10
10 2
2 3
3 4
10 4
3 8
5 10
2 10
4 10
4 1
1 3
5 2
3 5
1 4
5 2
2 5
1 3
output:
4
7
2
7
2
3
3
Note: For the first test case, the following roads must be maintained:
* $8 \leftarrow \rightarrow 1$ * $7 \leftarrow \rightarrow 8$ * $1 \leftarrow \rightarrow 2$ * $4 \leftarrow \rightarrow 5$
|
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"
]
] |
Title: Not a Nim Problem
time_limit_ms: 2000
memory_limit_mb: 512
Description: 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.
Input: 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$.
Output: For each test case, output Alice if Alice wins, or Bob if Bob wins.
Examples:
input:
3
3
3 2 9
4
3 3 6 1
5
1 2 3 4 5
output:
Bob
Alice
Bob
Note:
|
|
2022E1
|
https://codeforces.com/problemset/problem/2022/E1
|
Billetes MX (Easy Version)
| 2,500
|
[
"2-sat",
"binary search",
"combinatorics",
"constructive algorithms",
"dfs and similar",
"dsu",
"graphs"
] |
Div. 2
| 2,000
| 512
|
This is the easy version of the problem. In this version, it is guaranteed that $q = 0$. You can make hacks only if both versions of the problem are solved.
An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:
* All elements of the grid are integers between $0$ and $2^{30}-1$, and * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.
However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.
For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.
|
The first line contains $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k \le 10^5$; $q = 0$) β the number of rows, the number of columns, the number of fixed cells, and the number of updates.
The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) β indicating that $G_{r, c}$ is assigned the integer $v$.
The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) β indicating that $G_{r, c}$ is assigned the integer $v$.
It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.
It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.
|
For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.
|
[
[
"2\n3 3 8 0\n2 1 6\n3 2 12\n1 2 6\n2 2 0\n1 3 10\n1 1 0\n2 3 12\n3 1 10\n2 5 2 0\n1 1 10\n1 2 30",
"1\n489373567"
]
] |
In the first test case of the example, we have the following grid:
$0$| $6$| $10$ ---|---|--- $6$| $0$| $12$ $10$| $12$| $?$ It can be proven that the only valid value for tile $(3, 3)$ is $0$.
|
Title: Billetes MX (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: This is the easy version of the problem. In this version, it is guaranteed that $q = 0$. You can make hacks only if both versions of the problem are solved.
An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if:
* All elements of the grid are integers between $0$ and $2^{30}-1$, and * For any subgrid, the XOR of the values at the corners is equal to $0$. Formally, for any four integers $i_1$, $i_2$, $j_1$, $j_2$ ($1 \le i_1 < i_2 \le p$; $1 \le j_1 < j_2 \le q$), $A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
There is a partially filled integer grid $G$ with $n$ rows and $m$ columns where only $k$ cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.
However, Monocarp thinks that this problem is too easy. Therefore, he will perform $q$ updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.
For each of the $q + 1$ states of the grid, the initial state and after each of the $q$ queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo $10^9+7$.
Input: The first line contains $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k \le 10^5$; $q = 0$) β the number of rows, the number of columns, the number of fixed cells, and the number of updates.
The following $k$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) β indicating that $G_{r, c}$ is assigned the integer $v$.
The following $q$ lines contain three integers $r$, $c$ and $v$ ($1 \le r \le n, 1 \le c \le m$; $0 \le v < 2^{30}$) β indicating that $G_{r, c}$ is assigned the integer $v$.
It is guaranteed that the pairs $(r,c)$ over all assignments are distinct.
It is guaranteed that the sum of $n$, $m$, $k$ and $q$ over all test cases does not exceed $10^5$ respectively.
Output: For each test case, output $q + 1$ lines. The $i$-th line of output should contain the answer of the $i$-th state of the grid modulo $10^9 + 7$.
Examples:
input:
2
3 3 8 0
2 1 6
3 2 12
1 2 6
2 2 0
1 3 10
1 1 0
2 3 12
3 1 10
2 5 2 0
1 1 10
1 2 30
output:
1
489373567
Note: In the first test case of the example, we have the following grid:
$0$| $6$| $10$ ---|---|--- $6$| $0$| $12$ $10$| $12$| $?$ It can be proven that the only valid value for tile $(3, 3)$ is $0$.
|
2027D1
|
https://codeforces.com/problemset/problem/2027/D1
|
The Endspeaker (Easy Version)
| 1,700
|
[
"binary search",
"dp",
"graphs",
"greedy",
"implementation",
"two pointers"
] |
Div. 2
| 2,000
| 256
|
This is the easy version of this problem. The only difference is that you only need to output the minimum total cost of operations in this version. You must solve both versions to be able to hack.
You're given an array $a$ of length $n$, and an array $b$ of length $m$ ($b_i > b_{i+1}$ for all $1 \le i < m$). Initially, the value of $k$ is $1$. Your aim is to make the array $a$ empty by performing one of these two operations repeatedly:
* Type $1$ β If the value of $k$ is less than $m$ and the array $a$ is not empty, you can increase the value of $k$ by $1$. This does not incur any cost. * Type $2$ β You remove a non-empty prefix of array $a$, such that its sum does not exceed $b_k$. This incurs a cost of $m - k$.
You need to minimize the total cost of the operations to make array $a$ empty. If it's impossible to do this through any sequence of operations, output $-1$. Otherwise, output the minimum total cost of the operations.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$, $\boldsymbol{1 \le n \cdot m \le 3 \cdot 10^5}$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le 10^9$).
It is also guaranteed that $b_i > b_{i+1}$ for all $1 \le i < m$.
It is guaranteed that the sum of $\boldsymbol{n \cdot m}$ over all test cases does not exceed $3 \cdot 10^5$.
|
For each test case, if it's possible to make $a$ empty, then output the minimum total cost of the operations.
If there is no possible sequence of operations which makes $a$ empty, then output a single integer $-1$.
|
[
[
"5\n4 2\n9 3 4 3\n11 7\n1 2\n20\n19 18\n10 2\n2 5 2 1 10 3 2 9 9 6\n17 9\n10 11\n2 2 2 2 2 2 2 2 2 2\n20 18 16 14 12 10 8 6 4 2 1\n1 6\n10\n32 16 8 4 2 1",
"1\n-1\n2\n10\n4"
]
] |
In the first test case, one optimal sequence of operations which yields a total cost of $1$ is as follows:
* Perform an operation of type $2$. Choose the prefix to be $[9]$. This incurs a cost of $1$. * Perform an operation of type $1$. The value of $k$ is now $2$. This incurs no cost. * Perform an operation of type $2$. Choose the prefix to be $[3, 4]$. This incurs a cost of $0$. * Perform an operation of type $2$. Choose the prefix to be $[3]$. This incurs a cost of $0$. * The array $a$ is now empty, and the total cost of all operations is $1$.
In the second test case, it's impossible to remove any prefix of the array since $a_1 > b_1$, so array $a$ cannot be made empty by any sequence of operations.
|
Title: The Endspeaker (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is the easy version of this problem. The only difference is that you only need to output the minimum total cost of operations in this version. You must solve both versions to be able to hack.
You're given an array $a$ of length $n$, and an array $b$ of length $m$ ($b_i > b_{i+1}$ for all $1 \le i < m$). Initially, the value of $k$ is $1$. Your aim is to make the array $a$ empty by performing one of these two operations repeatedly:
* Type $1$ β If the value of $k$ is less than $m$ and the array $a$ is not empty, you can increase the value of $k$ by $1$. This does not incur any cost. * Type $2$ β You remove a non-empty prefix of array $a$, such that its sum does not exceed $b_k$. This incurs a cost of $m - k$.
You need to minimize the total cost of the operations to make array $a$ empty. If it's impossible to do this through any sequence of operations, output $-1$. Otherwise, output the minimum total cost of the operations.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$, $\boldsymbol{1 \le n \cdot m \le 3 \cdot 10^5}$).
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$).
The third line of each test case contains $m$ integers $b_1, b_2, \ldots, b_m$ ($1 \le b_i \le 10^9$).
It is also guaranteed that $b_i > b_{i+1}$ for all $1 \le i < m$.
It is guaranteed that the sum of $\boldsymbol{n \cdot m}$ over all test cases does not exceed $3 \cdot 10^5$.
Output: For each test case, if it's possible to make $a$ empty, then output the minimum total cost of the operations.
If there is no possible sequence of operations which makes $a$ empty, then output a single integer $-1$.
Examples:
input:
5
4 2
9 3 4 3
11 7
1 2
20
19 18
10 2
2 5 2 1 10 3 2 9 9 6
17 9
10 11
2 2 2 2 2 2 2 2 2 2
20 18 16 14 12 10 8 6 4 2 1
1 6
10
32 16 8 4 2 1
output:
1
-1
2
10
4
Note: In the first test case, one optimal sequence of operations which yields a total cost of $1$ is as follows:
* Perform an operation of type $2$. Choose the prefix to be $[9]$. This incurs a cost of $1$. * Perform an operation of type $1$. The value of $k$ is now $2$. This incurs no cost. * Perform an operation of type $2$. Choose the prefix to be $[3, 4]$. This incurs a cost of $0$. * Perform an operation of type $2$. Choose the prefix to be $[3]$. This incurs a cost of $0$. * The array $a$ is now empty, and the total cost of all operations is $1$.
In the second test case, it's impossible to remove any prefix of the array since $a_1 > b_1$, so array $a$ cannot be made empty by any sequence of operations.
|
1991H
|
https://codeforces.com/problemset/problem/1991/H
|
Prime Split Game
| 3,300
|
[
"bitmasks",
"dp",
"fft",
"games",
"math",
"number theory"
] |
Div. 1 + 2
| 2,000
| 256
|
Alice and Bob are playing a game with $n$ piles of stones, where the $i$-th pile has $a_i$ stones. Players take turns making moves, with Alice going first.
On each move, the player does the following three-step process:
1. Choose an integer $k$ ($1 \leq k \leq \frac n 2$). Note that the value of $k$ can be different for different moves. 2. Remove $k$ piles of stones. 3. Choose another $k$ piles of stones and split each pile into two piles. The number of stones in each new pile must be a prime number.
The player who is unable to make a move loses.
Determine who will win if both players play optimally.
|
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of 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 piles of stones.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) β the number of stones in the piles.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output "Alice" (without quotes) if Alice wins and "Bob" (without quotes) otherwise.
You can output each letter in any case (upper or lower). For example, the strings "alIcE", "Alice", and "alice" will all be considered identical.
|
[
[
"4\n2\n2 1\n3\n3 5 7\n4\n4 6 8 10\n5\n8 8 8 8 8",
"Bob\nAlice\nAlice\nBob"
]
] |
In the first test case, there are $2$ piles of stones with $2$ and $1$ stones respectively. Since neither $1$ nor $2$ can be split into two prime numbers, Alice cannot make a move, so Bob wins.
In the second test case, there are $3$ piles of stones with $3$, $5$, and $7$ stones respectively. Alice can choose $k = 1$, remove the pile of $7$ stones, and then split the pile of $5$ stones into two piles of prime numbers of stones, $2$ and $3$. Then, the piles consist of $3$ piles of stones with $3$, $2$, and $3$ stones respectively, leaving Bob with no valid moves, so Alice wins.
In the third test case, there are $4$ piles of stones with $4$, $6$, $8$, and $10$ stones respectively. Alice can choose $k = 2$, removing two piles of $8$ and $10$ stones. She splits the pile of $4$ stones into two piles of prime numbers of stones, $2$ and $2$, and the pile of $6$ stones into two piles of $3$ and $3$ stones. Then, Bob has no valid moves, so Alice wins.
In the fourth test case, there are $5$ piles of stones, each containing $8$ stones. It can be shown that if both players play optimally, Bob will win.
|
Title: Prime Split Game
time_limit_ms: 2000
memory_limit_mb: 256
Description: Alice and Bob are playing a game with $n$ piles of stones, where the $i$-th pile has $a_i$ stones. Players take turns making moves, with Alice going first.
On each move, the player does the following three-step process:
1. Choose an integer $k$ ($1 \leq k \leq \frac n 2$). Note that the value of $k$ can be different for different moves. 2. Remove $k$ piles of stones. 3. Choose another $k$ piles of stones and split each pile into two piles. The number of stones in each new pile must be a prime number.
The player who is unable to make a move loses.
Determine who will win if both players play optimally.
Input: Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of 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 piles of stones.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 2 \cdot 10^5$) β the number of stones in the piles.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output "Alice" (without quotes) if Alice wins and "Bob" (without quotes) otherwise.
You can output each letter in any case (upper or lower). For example, the strings "alIcE", "Alice", and "alice" will all be considered identical.
Examples:
input:
4
2
2 1
3
3 5 7
4
4 6 8 10
5
8 8 8 8 8
output:
Bob
Alice
Alice
Bob
Note: In the first test case, there are $2$ piles of stones with $2$ and $1$ stones respectively. Since neither $1$ nor $2$ can be split into two prime numbers, Alice cannot make a move, so Bob wins.
In the second test case, there are $3$ piles of stones with $3$, $5$, and $7$ stones respectively. Alice can choose $k = 1$, remove the pile of $7$ stones, and then split the pile of $5$ stones into two piles of prime numbers of stones, $2$ and $3$. Then, the piles consist of $3$ piles of stones with $3$, $2$, and $3$ stones respectively, leaving Bob with no valid moves, so Alice wins.
In the third test case, there are $4$ piles of stones with $4$, $6$, $8$, and $10$ stones respectively. Alice can choose $k = 2$, removing two piles of $8$ and $10$ stones. She splits the pile of $4$ stones into two piles of prime numbers of stones, $2$ and $2$, and the pile of $6$ stones into two piles of $3$ and $3$ stones. Then, Bob has no valid moves, so Alice wins.
In the fourth test case, there are $5$ piles of stones, each containing $8$ stones. It can be shown that if both players play optimally, Bob will win.
|
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$.
|
Title: Dora and C++
time_limit_ms: 2000
memory_limit_mb: 256
Description: 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.
Input: 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$.
Output: For each test case, output a single integer β the minimum possible range of the array after any number of operations.
Examples:
input:
10
4 5 5
1 3 4 4
4 2 3
1 3 4 6
4 7 7
1 1 2 6
3 15 9
1 9 5
3 18 12
1 4 5
7 27 36
33 13 23 12 35 24 41
10 6 9
15 5 6 9 8 2 12 15 3 8
2 1 1000000000
1 1000000000
6 336718728 709848696
552806726 474775724 15129785 371139304 178408298 13106071
6 335734893 671469786
138885253 70095920 456876775 9345665 214704906 375508929
output:
3
0
3
2
3
5
1
0
17
205359241
Note: 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$.
|
1995E1
|
https://codeforces.com/problemset/problem/1995/E1
|
Let Me Teach You a Lesson (Easy Version)
| 2,700
|
[
"2-sat",
"data structures",
"dp",
"matrices",
"two pointers"
] |
Div. 2
| 2,000
| 256
|
This is the easy version of a problem. The only difference between an easy and a hard version is the constraints on $t$ and $n$. You can make hacks only if both versions of the problem are solved.
Arthur is giving a lesson to his famous $2 n$ knights. Like any other students, they're sitting at the desks in pairs, but out of habit in a circle. The knight $2 i - 1$ is sitting at the desk with the knight $2 i$.
Each knight has intelligence, which can be measured by an integer. Let's denote the intelligence of the $i$-th knight as $a_i$. Arthur wants the maximal difference in total intelligence over all pairs of desks to be as small as possible. More formally, he wants to minimize $\max\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i}) - \min\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i})$.
However, the Code of Chivalry only allows swapping the opposite knights in the circle, i.e., Arthur can simultaneously perform $a_i := a_{i + n}$, $a_{i + n} := a_i$ for any $1 \le i \le n$. Arthur can make any number of such swaps. What is the best result he can achieve?
|
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases. It is followed by descriptions of the test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$) β the number of desks.
The second line consists of $2n$ integers $a_1, a_2, \ldots, a_{2 n}$ ($1 \le a_i \le 10^9$) β the intelligence values of the knights.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$.
|
For each test case, output a single line containing one integer β the minimal difference Arthur can achieve.
|
[
[
"5\n2\n6 6 4 4\n1\n10 17\n3\n1 10 1 10 1 10\n3\n3 3 4 5 5 4\n5\n1 2 3 4 5 6 7 8 9 10",
"0\n0\n0\n2\n4"
]
] |
In the first test case, Arthur can swap the second and the fourth knights. Then the total intelligence at both desks will be $10$.
In the third test case, Arthur can make $0$ operations, which will result in the total intelligence of $11$ at each of the desks.
In the fourth test case, Arthur can swap knights with indices $2$ and $5$ and achieve the difference of $2$. It can be proven that he cannot improve his result any further.
|
Title: Let Me Teach You a Lesson (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is the easy version of a problem. The only difference between an easy and a hard version is the constraints on $t$ and $n$. You can make hacks only if both versions of the problem are solved.
Arthur is giving a lesson to his famous $2 n$ knights. Like any other students, they're sitting at the desks in pairs, but out of habit in a circle. The knight $2 i - 1$ is sitting at the desk with the knight $2 i$.
Each knight has intelligence, which can be measured by an integer. Let's denote the intelligence of the $i$-th knight as $a_i$. Arthur wants the maximal difference in total intelligence over all pairs of desks to be as small as possible. More formally, he wants to minimize $\max\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i}) - \min\limits_{1 \le i \le n} (a_{2 i - 1} + a_{2 i})$.
However, the Code of Chivalry only allows swapping the opposite knights in the circle, i.e., Arthur can simultaneously perform $a_i := a_{i + n}$, $a_{i + n} := a_i$ for any $1 \le i \le n$. Arthur can make any number of such swaps. What is the best result he can achieve?
Input: Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) β the number of test cases. It is followed by descriptions of the test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2000$) β the number of desks.
The second line consists of $2n$ integers $a_1, a_2, \ldots, a_{2 n}$ ($1 \le a_i \le 10^9$) β the intelligence values of the knights.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2000$.
Output: For each test case, output a single line containing one integer β the minimal difference Arthur can achieve.
Examples:
input:
5
2
6 6 4 4
1
10 17
3
1 10 1 10 1 10
3
3 3 4 5 5 4
5
1 2 3 4 5 6 7 8 9 10
output:
0
0
0
2
4
Note: In the first test case, Arthur can swap the second and the fourth knights. Then the total intelligence at both desks will be $10$.
In the third test case, Arthur can make $0$ operations, which will result in the total intelligence of $11$ at each of the desks.
In the fourth test case, Arthur can swap knights with indices $2$ and $5$ and achieve the difference of $2$. It can be proven that he cannot improve his result any further.
|
1994B
|
https://codeforces.com/problemset/problem/1994/B
|
Fun Game
| 1,100
|
[
"bitmasks",
"constructive algorithms",
"greedy",
"math"
] |
Div. 1 + 2
| 1,000
| 256
|
Vova really loves the [XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) operation (denoted as $\oplus$). Recently, when he was going to sleep, he came up with a fun game.
At the beginning of the game, Vova chooses two binary sequences $s$ and $t$ of length $n$ and gives them to Vanya. A binary sequence is a sequence consisting only of the numbers $0$ and $1$. Vanya can choose integers $l, r$ such that $1 \leq l \leq r \leq n$, and for all $l \leq i \leq r$ simultaneously replace $s_i$ with $s_i \oplus s_{i - l + 1}$, where $s_i$ is the $i$-th element of the sequence $s$.
In order for the game to be interesting, there must be a possibility to win. Vanya wins if, with an unlimited number of actions, he can obtain the sequence $t$ from the sequence $s$. Determine if the game will be interesting for the sequences $s$ and $t$.
|
Each test consists of multiple test cases. The first line contains an integer $q$ ($1 \le q \le 10^{4}$) β the number of test cases. Then follows the description of the test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the length of the sequences $s$ and $t$.
The second line of each test case contains a binary sequence $s$ of length $n$.
The third line of each test case contains a binary sequence $t$ of length $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 "Yes" if the game will be interesting, otherwise output "No".
You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
|
[
[
"6\n1\n0\n1\n7\n0110100\n0110100\n9\n100101010\n101111110\n4\n0011\n1011\n4\n0100\n0001\n8\n10110111\n01100000",
"NO\nYES\nYES\nNO\nYES\nYES"
]
] |
In the first test case, Vanya will not be able to change the sequence $s$ with the only possible action of choosing $l = r = 1$.
In the second test case, the sequences $s$ and $t$ are already equal.
In the third test case, Vanya can act as follows:
1. Choose $l = 3$ and $r = 5$, then $s$ will become $\mathtt{101101010}$. 2. Choose $l = 5$ and $r = 6$, then $s$ will become $\mathtt{101111010}$. 3. Choose $l = 7$ and $r = 7$, then $s$ will become $\mathtt{101111110}$.
|
Title: Fun Game
time_limit_ms: 1000
memory_limit_mb: 256
Description: Vova really loves the [XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) operation (denoted as $\oplus$). Recently, when he was going to sleep, he came up with a fun game.
At the beginning of the game, Vova chooses two binary sequences $s$ and $t$ of length $n$ and gives them to Vanya. A binary sequence is a sequence consisting only of the numbers $0$ and $1$. Vanya can choose integers $l, r$ such that $1 \leq l \leq r \leq n$, and for all $l \leq i \leq r$ simultaneously replace $s_i$ with $s_i \oplus s_{i - l + 1}$, where $s_i$ is the $i$-th element of the sequence $s$.
In order for the game to be interesting, there must be a possibility to win. Vanya wins if, with an unlimited number of actions, he can obtain the sequence $t$ from the sequence $s$. Determine if the game will be interesting for the sequences $s$ and $t$.
Input: Each test consists of multiple test cases. The first line contains an integer $q$ ($1 \le q \le 10^{4}$) β the number of test cases. Then follows the description of the test cases.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the length of the sequences $s$ and $t$.
The second line of each test case contains a binary sequence $s$ of length $n$.
The third line of each test case contains a binary sequence $t$ of length $n$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output "Yes" if the game will be interesting, otherwise output "No".
You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
Examples:
input:
6
1
0
1
7
0110100
0110100
9
100101010
101111110
4
0011
1011
4
0100
0001
8
10110111
01100000
output:
NO
YES
YES
NO
YES
YES
Note: In the first test case, Vanya will not be able to change the sequence $s$ with the only possible action of choosing $l = r = 1$.
In the second test case, the sequences $s$ and $t$ are already equal.
In the third test case, Vanya can act as follows:
1. Choose $l = 3$ and $r = 5$, then $s$ will become $\mathtt{101101010}$. 2. Choose $l = 5$ and $r = 6$, then $s$ will become $\mathtt{101111010}$. 3. Choose $l = 7$ and $r = 7$, then $s$ will become $\mathtt{101111110}$.
|
1997D
|
https://codeforces.com/problemset/problem/1997/D
|
Maximize the Root
| 1,500
|
[
"binary search",
"dfs and similar",
"dp",
"greedy",
"trees"
] |
Div. 2
| 3,000
| 256
|
You are given a rooted tree, consisting of $n$ vertices. The vertices in the tree are numbered from $1$ to $n$, and the root is the vertex $1$. The value $a_i$ is written at the $i$-th vertex.
You can perform the following operation any number of times (possibly zero): choose a vertex $v$ which has at least one child; increase $a_v$ by $1$; and decrease $a_u$ by $1$ for all vertices $u$ that are in the subtree of $v$ (except $v$ itself). However, after each operation, the values on all vertices should be non-negative.
Your task is to calculate the maximum possible value written at the root using the aforementioned operation.
|
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 vertices in the tree.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$) β the initial values written at vertices.
The third line contains $n-1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the parent of the $i$-th vertex in the tree. Vertex $1$ is the root.
Additional constraint on the input: the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
|
For each test case, print a single integer β the maximum possible value written at the root using the aforementioned operation.
|
[
[
"3\n4\n0 1 0 2\n1 1 3\n2\n3 0\n1\n5\n2 5 3 9 6\n3 1 5 2",
"1\n3\n6"
]
] |
In the first test case, the following sequence of operations is possible:
* perform the operation on $v=3$, then the values on the vertices will be $[0, 1, 1, 1]$; * perform the operation on $v=1$, then the values on the vertices will be $[1, 0, 0, 0]$.
|
Title: Maximize the Root
time_limit_ms: 3000
memory_limit_mb: 256
Description: You are given a rooted tree, consisting of $n$ vertices. The vertices in the tree are numbered from $1$ to $n$, and the root is the vertex $1$. The value $a_i$ is written at the $i$-th vertex.
You can perform the following operation any number of times (possibly zero): choose a vertex $v$ which has at least one child; increase $a_v$ by $1$; and decrease $a_u$ by $1$ for all vertices $u$ that are in the subtree of $v$ (except $v$ itself). However, after each operation, the values on all vertices should be non-negative.
Your task is to calculate the maximum possible value written at the root using the aforementioned operation.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$) β the initial values written at vertices.
The third line contains $n-1$ integers $p_2, p_3, \dots, p_n$ ($1 \le p_i \le n$), where $p_i$ is the parent of the $i$-th vertex in the tree. Vertex $1$ is the root.
Additional constraint on the input: the sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
Output: For each test case, print a single integer β the maximum possible value written at the root using the aforementioned operation.
Examples:
input:
3
4
0 1 0 2
1 1 3
2
3 0
1
5
2 5 3 9 6
3 1 5 2
output:
1
3
6
Note: In the first test case, the following sequence of operations is possible:
* perform the operation on $v=3$, then the values on the vertices will be $[0, 1, 1, 1]$; * perform the operation on $v=1$, then the values on the vertices will be $[1, 0, 0, 0]$.
|
2030E
|
https://codeforces.com/problemset/problem/2030/E
|
MEXimize the Score
| 2,200
|
[
"combinatorics",
"data structures",
"dp",
"greedy",
"implementation",
"math"
] |
Div. 2
| 2,000
| 256
|
Suppose we partition the elements of an array $b$ into any number $k$ of non-empty multisets $S_1, S_2, \ldots, S_k$, where $k$ is an arbitrary positive integer. Define the score of $b$ as the maximum value of $\operatorname{MEX}(S_1)$$^{\text{β}}$$ + \operatorname{MEX}(S_2) + \ldots + \operatorname{MEX}(S_k)$ over all possible partitions of $b$ for any integer $k$.
Envy is given an array $a$ of size $n$. Since he knows that calculating the score of $a$ is too easy for you, he instead asks you to calculate the sum of scores of all $2^n - 1$ non-empty subsequences of $a$.$^{\text{β }}$ Since this answer may be large, please output it modulo $998\,244\,353$.
$^{\text{β}}$$\operatorname{MEX}$ of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest non-negative integer $x$ that does not occur in the collection $c$. For example, $\operatorname{MEX}([0,1,2,2]) = 3$ and $\operatorname{MEX}([1,2,2]) = 0$
$^{\text{β }}$A sequence $x$ is a subsequence of a sequence $y$ if $x$ can be obtained from $y$ by deleting several (possibly, zero or all) elements.
|
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the length of $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < n$) β the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output the answer, modulo $998\,244\,353$.
|
[
[
"4\n3\n0 0 1\n4\n0 0 1 1\n5\n0 0 1 2 2\n4\n1 1 1 1",
"11\n26\n53\n0"
]
] |
In the first testcase, we must consider seven subsequences:
* $[0]$: The score is $1$. * $[0]$: The score is $1$. * $[1]$: The score is $0$. * $[0,0]$: The score is $2$. * $[0,1]$: The score is $2$. * $[0,1]$: The score is $2$. * $[0,0,1]$: The score is $3$.
The answer for the first testcase is $1+1+2+2+2+3=11$.
In the last testcase, all subsequences have a score of $0$.
|
Title: MEXimize the Score
time_limit_ms: 2000
memory_limit_mb: 256
Description: Suppose we partition the elements of an array $b$ into any number $k$ of non-empty multisets $S_1, S_2, \ldots, S_k$, where $k$ is an arbitrary positive integer. Define the score of $b$ as the maximum value of $\operatorname{MEX}(S_1)$$^{\text{β}}$$ + \operatorname{MEX}(S_2) + \ldots + \operatorname{MEX}(S_k)$ over all possible partitions of $b$ for any integer $k$.
Envy is given an array $a$ of size $n$. Since he knows that calculating the score of $a$ is too easy for you, he instead asks you to calculate the sum of scores of all $2^n - 1$ non-empty subsequences of $a$.$^{\text{β }}$ Since this answer may be large, please output it modulo $998\,244\,353$.
$^{\text{β}}$$\operatorname{MEX}$ of a collection of integers $c_1, c_2, \ldots, c_k$ is defined as the smallest non-negative integer $x$ that does not occur in the collection $c$. For example, $\operatorname{MEX}([0,1,2,2]) = 3$ and $\operatorname{MEX}([1,2,2]) = 0$
$^{\text{β }}$A sequence $x$ is a subsequence of a sequence $y$ if $x$ can be obtained from $y$ by deleting several (possibly, zero or all) elements.
Input: The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
The first line of each test case contains an integer $n$ ($1 \leq n \leq 2 \cdot 10^5$) β the length of $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i < n$) β the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output the answer, modulo $998\,244\,353$.
Examples:
input:
4
3
0 0 1
4
0 0 1 1
5
0 0 1 2 2
4
1 1 1 1
output:
11
26
53
0
Note: In the first testcase, we must consider seven subsequences:
* $[0]$: The score is $1$. * $[0]$: The score is $1$. * $[1]$: The score is $0$. * $[0,0]$: The score is $2$. * $[0,1]$: The score is $2$. * $[0,1]$: The score is $2$. * $[0,0,1]$: The score is $3$.
The answer for the first testcase is $1+1+2+2+2+3=11$.
In the last testcase, all subsequences have a score of $0$.
|
2025G
|
https://codeforces.com/problemset/problem/2025/G
|
Variable Damage
| 3,000
|
[
"data structures",
"flows"
] |
Div. 2
| 5,000
| 512
|
Monocarp is gathering an army to fight a dragon in a videogame.
The army consists of two parts: the heroes and the defensive artifacts. Each hero has one parameter β his health. Each defensive artifact also has one parameter β its durability.
Before the battle begins, Monocarp distributes artifacts to the heroes so that each hero receives at most one artifact.
The battle consists of rounds that proceed as follows:
* first, the dragon deals damage equal to $\frac{1}{a + b}$ (a real number without rounding) to each hero, where $a$ is the number of heroes alive and $b$ is the number of active artifacts; * after that, all heroes with health $0$ or less die; * finally, some artifacts are deactivated. An artifact with durability $x$ is deactivated when one of the following occurs: the hero holding the artifact either dies or receives $x$ total damage (from the start of the battle). If an artifact is not held by any hero, it is inactive from the beginning of the battle.
The battle ends when there are no heroes left alive.
Initially, the army is empty. There are $q$ queries: add a hero with health $x$ or an artifact with durability $y$ to the army. After each query, determine the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.
|
The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
In the $i$-th of the following $q$ lines, there are two integers $t_i$ and $v_i$ ($t_i \in \\{1, 2\\}$; $1 \le v_i \le 10^9$) β the type of the query and the value of the query parameter. If the type is $1$, a hero with health $v_i$ is added. If the type is $2$, an artifact with durability $v_i$ is added.
|
Print $q$ integers. After each query, output the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.
|
[
[
"3\n2 5\n1 4\n1 10",
"0\n8\n19"
],
[
"10\n1 9\n1 6\n2 4\n1 8\n1 3\n2 10\n1 3\n1 6\n1 10\n2 6",
"9\n15\n19\n27\n30\n39\n42\n48\n59\n65"
]
] |
Let's consider the first example.
* An artifact with durability $5$ is added. Since there are no heroes yet, the battle ends immediately. * A hero with health $4$ is added. Monocarp can give him an artifact with durability $5$. First, there are rounds in which the hero takes $\frac{1}{1 + 1} = \frac{1}{2}$ damage. After $8$ such rounds, a total of $4$ damage will have been dealt, and the hero will die, while the artifact will deactivate. There are no more heroes alive, so the battle ends after $8$ rounds. * A hero with health $10$ is added. Now let the artifact with durability $5$ be with this hero. Then, in the first $12$ rounds, the heroes will take $12 \cdot \frac{1}{2 + 1} = 4$ damage, and the first hero will die. The second hero has $6$ health left, and the artifact has $1$ durability. Now the damage is $\frac{1}{2}$, so after another $2$ rounds, the artifact will deactivate. The second hero has $5$ health left. After another $5$ rounds, the second hero will die. Therefore, the answer is $12 + 2 + 5 = 19$.
|
Title: Variable Damage
time_limit_ms: 5000
memory_limit_mb: 512
Description: Monocarp is gathering an army to fight a dragon in a videogame.
The army consists of two parts: the heroes and the defensive artifacts. Each hero has one parameter β his health. Each defensive artifact also has one parameter β its durability.
Before the battle begins, Monocarp distributes artifacts to the heroes so that each hero receives at most one artifact.
The battle consists of rounds that proceed as follows:
* first, the dragon deals damage equal to $\frac{1}{a + b}$ (a real number without rounding) to each hero, where $a$ is the number of heroes alive and $b$ is the number of active artifacts; * after that, all heroes with health $0$ or less die; * finally, some artifacts are deactivated. An artifact with durability $x$ is deactivated when one of the following occurs: the hero holding the artifact either dies or receives $x$ total damage (from the start of the battle). If an artifact is not held by any hero, it is inactive from the beginning of the battle.
The battle ends when there are no heroes left alive.
Initially, the army is empty. There are $q$ queries: add a hero with health $x$ or an artifact with durability $y$ to the army. After each query, determine the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.
Input: The first line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$) β the number of queries.
In the $i$-th of the following $q$ lines, there are two integers $t_i$ and $v_i$ ($t_i \in \\{1, 2\\}$; $1 \le v_i \le 10^9$) β the type of the query and the value of the query parameter. If the type is $1$, a hero with health $v_i$ is added. If the type is $2$, an artifact with durability $v_i$ is added.
Output: Print $q$ integers. After each query, output the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally.
Examples:
input:
3
2 5
1 4
1 10
output:
0
8
19
input:
10
1 9
1 6
2 4
1 8
1 3
2 10
1 3
1 6
1 10
2 6
output:
9
15
19
27
30
39
42
48
59
65
Note: Let's consider the first example.
* An artifact with durability $5$ is added. Since there are no heroes yet, the battle ends immediately. * A hero with health $4$ is added. Monocarp can give him an artifact with durability $5$. First, there are rounds in which the hero takes $\frac{1}{1 + 1} = \frac{1}{2}$ damage. After $8$ such rounds, a total of $4$ damage will have been dealt, and the hero will die, while the artifact will deactivate. There are no more heroes alive, so the battle ends after $8$ rounds. * A hero with health $10$ is added. Now let the artifact with durability $5$ be with this hero. Then, in the first $12$ rounds, the heroes will take $12 \cdot \frac{1}{2 + 1} = 4$ damage, and the first hero will die. The second hero has $6$ health left, and the artifact has $1$ durability. Now the damage is $\frac{1}{2}$, so after another $2$ rounds, the artifact will deactivate. The second hero has $5$ health left. After another $5$ rounds, the second hero will die. Therefore, the answer is $12 + 2 + 5 = 19$.
|
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.
|
Title: Robin Hood in Town
time_limit_ms: 2000
memory_limit_mb: 256
Description: 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.
Input: 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$.
Output: 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.
Examples:
input:
6
1
2
2
2 19
3
1 3 20
4
1 2 3 4
5
1 2 3 4 5
6
1 2 1 1 1 25
output:
-1
-1
0
15
16
0
Note: 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.
|
1987A
|
https://codeforces.com/problemset/problem/1987/A
|
Upload More RAM
| 800
|
[
"greedy",
"math"
] |
Div. 1 + 2
| 1,000
| 256
|
Oh no, the ForceCodes servers are running out of memory! Luckily, you can help them out by uploading some of your RAM!
You want to upload $n$ GBs of RAM. Every second, you will upload either $0$ or $1$ GB of RAM. However, there is a restriction on your network speed: in any $k$ consecutive seconds, you can upload only at most $1$ GB of RAM in total.
Find the minimum number of seconds needed to upload $n$ GBs of RAM!
|
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The first and only line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β the number of GBs that you want to upload and the length of the time window respectively.
|
For each test case, output a single integer β the minimum number of seconds needed to upload $n$ GBs of RAM.
|
[
[
"6\n5 1\n2 2\n2 3\n1 7\n11 5\n100 100",
"5\n3\n4\n1\n51\n9901"
]
] |
In the first test case, you can upload $1$ GB of RAM per second, so to upload $5$ GBs, you need $5$ seconds.
In the second test case, you can upload $1$ GB in the first second, $0$ GBs in the second second, and $1$ GB in the third second, which in total adds up to exactly $2$ GBs of uploaded RAM.
In the third test case, you can upload $1$ GB in the first second, $0$ GBs in the second second, $0$ GBs in the third second, and $1$ GB in the fourth second, which in total adds up to exactly $2$ GBs of uploaded RAM.
|
Title: Upload More RAM
time_limit_ms: 1000
memory_limit_mb: 256
Description: Oh no, the ForceCodes servers are running out of memory! Luckily, you can help them out by uploading some of your RAM!
You want to upload $n$ GBs of RAM. Every second, you will upload either $0$ or $1$ GB of RAM. However, there is a restriction on your network speed: in any $k$ consecutive seconds, you can upload only at most $1$ GB of RAM in total.
Find the minimum number of seconds needed to upload $n$ GBs of RAM!
Input: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The first and only line of each test case contains two integers $n$ and $k$ ($1 \le n, k \le 100$) β the number of GBs that you want to upload and the length of the time window respectively.
Output: For each test case, output a single integer β the minimum number of seconds needed to upload $n$ GBs of RAM.
Examples:
input:
6
5 1
2 2
2 3
1 7
11 5
100 100
output:
5
3
4
1
51
9901
Note: In the first test case, you can upload $1$ GB of RAM per second, so to upload $5$ GBs, you need $5$ seconds.
In the second test case, you can upload $1$ GB in the first second, $0$ GBs in the second second, and $1$ GB in the third second, which in total adds up to exactly $2$ GBs of uploaded RAM.
In the third test case, you can upload $1$ GB in the first second, $0$ GBs in the second second, $0$ GBs in the third second, and $1$ GB in the fourth second, which in total adds up to exactly $2$ GBs of uploaded RAM.
|
1982D
|
https://codeforces.com/problemset/problem/1982/D
|
Beauty of the mountains
| 1,700
|
[
"brute force",
"data structures",
"implementation",
"math",
"number theory"
] |
Div. 2
| 2,000
| 256
|
Nikita loves mountains and has finally decided to visit the Berlyand mountain range! The range was so beautiful that Nikita decided to capture it on a map. The map is a table of $n$ rows and $m$ columns, with each cell containing a non-negative integer representing the height of the mountain.
He also noticed that mountains come in two types:
* With snowy caps. * Without snowy caps.
Nikita is a very pragmatic person. He wants the sum of the heights of the mountains with snowy caps to be equal to the sum of the heights of the mountains without them. He has arranged with the mayor of Berlyand, Polikarp Polikarpovich, to allow him to transform the landscape.
Nikita can perform transformations on submatrices of size $k \times k$ as follows: he can add an integer constant $c$ to the heights of the mountains within this area, but the type of the mountain remains unchanged. Nikita can choose the constant $c$ independently for each transformation. Note that $c$ can be negative.
Before making the transformations, Nikita asks you to find out if it is possible to achieve equality of the sums, or if it is impossible. It doesn't matter at what cost, even if the mountains turn into canyons and have negative heights.
If only one type of mountain is represented on the map, then the sum of the heights of the other type of mountain is considered to be zero.
|
Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) β the number of test cases. This is followed by a description of test cases.
The first line of each test case contains three integers $n, m, k$ ($1 \le n, m \le 500, 1 \le k \le min(n, m)$).
The next $n$ lines of each test case contain $m$ integers $a_{i j}$ ($0 \le a_{i j} \le 10^{9}$) β the initial heights of the mountains.
The next $n$ binary strings of length $m$ for each test case determine the type of mountain, '$0$' β with snowy caps, '$1$' β without them.
It is guaranteed that the sum of $n \cdot m$ for all test cases does not exceed $250\,000$.
|
For each test case, output "YES" without quotes if it is possible to equalize the sums of the mountain heights, otherwise output "NO" without quotes. You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
|
[
[
"8\n3 3 2\n7 11 3\n4 2 3\n0 1 15\n100\n010\n000\n4 4 3\n123 413 24 233\n123 42 0 216\n22 1 1 53\n427 763 22 6\n0101\n1111\n1010\n0101\n3 3 2\n2 1 1\n1 1 2\n1 5 4\n010\n101\n010\n3 3 2\n2 1 1\n1 1 2\n1 5 3\n010\n101\n010\n3 4 3\n46 49 50 1\n19 30 23 12\n30 25 1 46\n1000\n0100\n0010\n5 4 4\n39 30 0 17\n22 42 30 13\n10 44 46 35\n12 19 9 39\n21 0 45 40\n1000\n1111\n0011\n0111\n1100\n2 2 2\n3 4\n6 7\n00\n00\n2 2 2\n0 0\n2 0\n01\n00",
"YES\nNO\nYES\nNO\nYES\nNO\nYES\nYES"
]
] |
The mountain array from the first test case looks like this:

Initially, the sum of the heights of the mountains with snowy caps is $11 + 3 + 4 + 3 + 0 + 1 + 15 = 37$, and without them is $7 + 2 = 9$.
To equalize these sums, we can perform two transformations:
First transformation:

Note that the constant $c$ can be negative.
After the first transformation, the mountain array looks like this:

Second transformation:

As a result, the mountain array looks like this:

The sum of the heights of the mountains with snowy caps is $17 + 9 + 9 - 16 - 20 - 19 + 15 = -5$, and without them is $7 - 12 = -5$, thus the answer is YES.
|
Title: Beauty of the mountains
time_limit_ms: 2000
memory_limit_mb: 256
Description: Nikita loves mountains and has finally decided to visit the Berlyand mountain range! The range was so beautiful that Nikita decided to capture it on a map. The map is a table of $n$ rows and $m$ columns, with each cell containing a non-negative integer representing the height of the mountain.
He also noticed that mountains come in two types:
* With snowy caps. * Without snowy caps.
Nikita is a very pragmatic person. He wants the sum of the heights of the mountains with snowy caps to be equal to the sum of the heights of the mountains without them. He has arranged with the mayor of Berlyand, Polikarp Polikarpovich, to allow him to transform the landscape.
Nikita can perform transformations on submatrices of size $k \times k$ as follows: he can add an integer constant $c$ to the heights of the mountains within this area, but the type of the mountain remains unchanged. Nikita can choose the constant $c$ independently for each transformation. Note that $c$ can be negative.
Before making the transformations, Nikita asks you to find out if it is possible to achieve equality of the sums, or if it is impossible. It doesn't matter at what cost, even if the mountains turn into canyons and have negative heights.
If only one type of mountain is represented on the map, then the sum of the heights of the other type of mountain is considered to be zero.
Input: Each test consists of several test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) β the number of test cases. This is followed by a description of test cases.
The first line of each test case contains three integers $n, m, k$ ($1 \le n, m \le 500, 1 \le k \le min(n, m)$).
The next $n$ lines of each test case contain $m$ integers $a_{i j}$ ($0 \le a_{i j} \le 10^{9}$) β the initial heights of the mountains.
The next $n$ binary strings of length $m$ for each test case determine the type of mountain, '$0$' β with snowy caps, '$1$' β without them.
It is guaranteed that the sum of $n \cdot m$ for all test cases does not exceed $250\,000$.
Output: For each test case, output "YES" without quotes if it is possible to equalize the sums of the mountain heights, otherwise output "NO" without quotes. You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).
Examples:
input:
8
3 3 2
7 11 3
4 2 3
0 1 15
100
010
000
4 4 3
123 413 24 233
123 42 0 216
22 1 1 53
427 763 22 6
0101
1111
1010
0101
3 3 2
2 1 1
1 1 2
1 5 4
010
101
010
3 3 2
2 1 1
1 1 2
1 5 3
010
101
010
3 4 3
46 49 50 1
19 30 23 12
30 25 1 46
1000
0100
0010
5 4 4
39 30 0 17
22 42 30 13
10 44 46 35
12 19 9 39
21 0 45 40
1000
1111
0011
0111
1100
2 2 2
3 4
6 7
00
00
2 2 2
0 0
2 0
01
00
output:
YES
NO
YES
NO
YES
NO
YES
YES
Note: The mountain array from the first test case looks like this:

Initially, the sum of the heights of the mountains with snowy caps is $11 + 3 + 4 + 3 + 0 + 1 + 15 = 37$, and without them is $7 + 2 = 9$.
To equalize these sums, we can perform two transformations:
First transformation:

Note that the constant $c$ can be negative.
After the first transformation, the mountain array looks like this:

Second transformation:

As a result, the mountain array looks like this:

The sum of the heights of the mountains with snowy caps is $17 + 9 + 9 - 16 - 20 - 19 + 15 = -5$, and without them is $7 - 12 = -5$, thus the answer is YES.
|
1972D1
|
https://codeforces.com/problemset/problem/1972/D1
|
Reverse Card (Easy Version)
| 1,400
|
[
"brute force",
"math",
"number theory"
] |
Div. 2
| 2,000
| 256
|
The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.
You are given two positive integers $n$, $m$.
Calculate the number of ordered pairs $(a, b)$ satisfying the following conditions:
* $1\le a\le n$, $1\le b\le m$; * $a+b$ is a multiple of $b \cdot \gcd(a,b)$.
|
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,m\le 2 \cdot 10^6$).
It is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \cdot 10^6$.
|
For each test case, print a single integer: the number of valid pairs.
|
[
[
"6\n1 1\n2 3\n3 5\n10 8\n100 1233\n1000000 1145141",
"1\n3\n4\n14\n153\n1643498"
]
] |
In the first test case, only $(1,1)$ satisfies the conditions.
In the fourth test case, $(1,1),(2,1),(2,2),(3,1),(4,1),(5,1),(6,1),(6,2),(6,3),(7,1),(8,1),(9,1),(10,1),(10,2)$ satisfy the conditions.
|
Title: Reverse Card (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: The two versions are different problems. You may want to read both versions. You can make hacks only if both versions are solved.
You are given two positive integers $n$, $m$.
Calculate the number of ordered pairs $(a, b)$ satisfying the following conditions:
* $1\le a\le n$, $1\le b\le m$; * $a+b$ is a multiple of $b \cdot \gcd(a,b)$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n$, $m$ ($1\le n,m\le 2 \cdot 10^6$).
It is guaranteed that neither the sum of $n$ nor the sum of $m$ over all test cases exceeds $2 \cdot 10^6$.
Output: For each test case, print a single integer: the number of valid pairs.
Examples:
input:
6
1 1
2 3
3 5
10 8
100 1233
1000000 1145141
output:
1
3
4
14
153
1643498
Note: In the first test case, only $(1,1)$ satisfies the conditions.
In the fourth test case, $(1,1),(2,1),(2,2),(3,1),(4,1),(5,1),(6,1),(6,2),(6,3),(7,1),(8,1),(9,1),(10,1),(10,2)$ satisfy the conditions.
|
1987E
|
https://codeforces.com/problemset/problem/1987/E
|
Wonderful Tree!
| 2,000
|
[
"brute force",
"data structures",
"dfs and similar",
"dsu",
"greedy",
"trees"
] |
Div. 1 + 2
| 2,000
| 256
|
God's Blessing on This ArrayForces!
A Random Pebble
You are given a tree with $n$ vertices, rooted at vertex $1$. The $i$-th vertex has an integer $a_i$ written on it.
Let $L$ be the set of all direct children$^{\text{β}}$ of $v$. A tree is called wonderful, if for all vertices $v$ where $L$ is not empty, $$a_v \le \sum_{u \in L}{a_u}.$$ In one operation, you choose any vertex $v$ and increase $a_v$ by $1$.
Find the minimum number of operations needed to make the given tree wonderful!
$^{\text{β}}$ Vertex $u$ is called a direct child of vertex $v$ if:
* $u$ and $v$ are connected by an edge, and * $v$ is on the (unique) path from $u$ to the root of the tree.
|
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 1000$) β 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 \le n \le 5000$) β the number of vertices in the tree.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) β the values initially written on the vertices.
The third line of each test case contains $n - 1$ integers $p_2, p_3 , \ldots, p_n$ ($1 \le p_i < i$), indicating that there is an edge from vertex $p_i$ to vertex $i$. 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 $5000$.
|
For each test case, output a single integer β the minimum number of operations needed to make the tree wonderful.
|
[
[
"4\n5\n9 3 4 1 2\n1 1 3 3\n2\n5 3\n1\n2\n36 54\n1\n3\n0 0 0\n1 2",
"3\n2\n0\n0"
]
] |
The tree in the first test case:

You can apply the operation once on vertex $5$ and twice on vertex $2$ to get a wonderful tree.
In the second test case, you can apply the operation twice on vertex $2$ to get a wonderful tree.
In the third and fourth test cases, the tree is already wonderful, so you don't need to apply any operations.
|
Title: Wonderful Tree!
time_limit_ms: 2000
memory_limit_mb: 256
Description: God's Blessing on This ArrayForces!
A Random Pebble
You are given a tree with $n$ vertices, rooted at vertex $1$. The $i$-th vertex has an integer $a_i$ written on it.
Let $L$ be the set of all direct children$^{\text{β}}$ of $v$. A tree is called wonderful, if for all vertices $v$ where $L$ is not empty, $$a_v \le \sum_{u \in L}{a_u}.$$ In one operation, you choose any vertex $v$ and increase $a_v$ by $1$.
Find the minimum number of operations needed to make the given tree wonderful!
$^{\text{β}}$ Vertex $u$ is called a direct child of vertex $v$ if:
* $u$ and $v$ are connected by an edge, and * $v$ is on the (unique) path from $u$ to the root of the tree.
Input: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 1000$) β 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 \le n \le 5000$) β the number of vertices in the tree.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \le a_i \le 10^9$) β the values initially written on the vertices.
The third line of each test case contains $n - 1$ integers $p_2, p_3 , \ldots, p_n$ ($1 \le p_i < i$), indicating that there is an edge from vertex $p_i$ to vertex $i$. 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 $5000$.
Output: For each test case, output a single integer β the minimum number of operations needed to make the tree wonderful.
Examples:
input:
4
5
9 3 4 1 2
1 1 3 3
2
5 3
1
2
36 54
1
3
0 0 0
1 2
output:
3
2
0
0
Note: The tree in the first test case:

You can apply the operation once on vertex $5$ and twice on vertex $2$ to get a wonderful tree.
In the second test case, you can apply the operation twice on vertex $2$ to get a wonderful tree.
In the third and fourth test cases, the tree is already wonderful, so you don't need to apply any operations.
|
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$.
|
Title: Expected Median
time_limit_ms: 3000
memory_limit_mb: 256
Description: 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.
Input: 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$.
Output: For each test case, print the sum modulo $10^9 + 7$.
Examples:
input:
8
4 3
1 0 0 1
5 1
1 1 1 1 1
5 5
0 1 0 1 0
6 3
1 0 1 0 1 1
4 3
1 0 1 1
5 3
1 0 1 1 0
2 1
0 0
34 17
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 1
output:
2
5
0
16
4
7
0
333606206
Note: 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$.
|
1985H1
|
https://codeforces.com/problemset/problem/1985/H1
|
Maximize the Largest Component (Easy Version)
| 1,700
|
[
"brute force",
"data structures",
"dfs and similar",
"dsu",
"graphs",
"implementation"
] |
Div. 4
| 2,000
| 512
|
Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. The only difference between the two versions is the operation.
Alex has a grid with $n$ rows and $m$ columns consisting of '.' and '#' characters. A set of '#' cells forms a connected component if from any cell in this set, it is possible to reach any other cell in this set by only moving to another cell in the set that shares a common side. The size of a connected component is the number of cells in the set.
In one operation, Alex selects any row $r$ ($1 \le r \le n$) or any column $c$ ($1 \le c \le m$), then sets every cell in row $r$ or column $c$ to be '#'. Help Alex find the maximum possible size of the largest connected component of '#' cells that he can achieve after performing the operation at most once.
|
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$ and $m$ ($1 \le n \cdot m \le 10^6$) β the number of rows and columns of the grid.
The next $n$ lines each contain $m$ characters. Each character is either '.' or '#'.
It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$.
|
For each test case, output a single integer β the maximum possible size of a connected component of '#' cells that Alex can achieve.
|
[
[
"6\n1 1\n.\n4 2\n..\n#.\n#.\n.#\n3 5\n.#.#.\n..#..\n.#.#.\n5 5\n#...#\n....#\n#...#\n.....\n...##\n6 6\n.#..#.\n#..#..\n.#...#\n#.#.#.\n.#.##.\n###..#\n6 8\n..#....#\n.####.#.\n###.#..#\n.##.#.##\n.#.##.##\n#..##.#.",
"1\n6\n9\n11\n15\n30"
]
] |
In the second test case, it is optimal for Alex to set all cells in column $2$ to be '#'. Doing so will lead to the largest connected component of '#' having a size of $6$.
In the third test case, it is optimal for Alex to set all cells in row $2$ to be '#'. Doing so will lead to the largest connected component of '#' having a size of $9$.
In the fourth test case, it is optimal for Alex to set all cells in row $4$ to be '#'. Doing so will lead to the largest connected component of '#' having a size of $11$.
|
Title: Maximize the Largest Component (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: Easy and hard versions are actually different problems, so read statements of both problems completely and carefully. The only difference between the two versions is the operation.
Alex has a grid with $n$ rows and $m$ columns consisting of '.' and '#' characters. A set of '#' cells forms a connected component if from any cell in this set, it is possible to reach any other cell in this set by only moving to another cell in the set that shares a common side. The size of a connected component is the number of cells in the set.
In one operation, Alex selects any row $r$ ($1 \le r \le n$) or any column $c$ ($1 \le c \le m$), then sets every cell in row $r$ or column $c$ to be '#'. Help Alex find the maximum possible size of the largest connected component of '#' cells that he can achieve after performing the operation at most once.
Input: 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$ and $m$ ($1 \le n \cdot m \le 10^6$) β the number of rows and columns of the grid.
The next $n$ lines each contain $m$ characters. Each character is either '.' or '#'.
It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $10^6$.
Output: For each test case, output a single integer β the maximum possible size of a connected component of '#' cells that Alex can achieve.
Examples:
input:
6
1 1
.
4 2
..
#.
#.
.#
3 5
.#.#.
..#..
.#.#.
5 5
#...#
....#
#...#
.....
...##
6 6
.#..#.
#..#..
.#...#
#.#.#.
.#.##.
###..#
6 8
..#....#
.####.#.
###.#..#
.##.#.##
.#.##.##
#..##.#.
output:
1
6
9
11
15
30
Note: In the second test case, it is optimal for Alex to set all cells in column $2$ to be '#'. Doing so will lead to the largest connected component of '#' having a size of $6$.
In the third test case, it is optimal for Alex to set all cells in row $2$ to be '#'. Doing so will lead to the largest connected component of '#' having a size of $9$.
In the fourth test case, it is optimal for Alex to set all cells in row $4$ to be '#'. Doing so will lead to the largest connected component of '#' having a size of $11$.
|
1996E
|
https://codeforces.com/problemset/problem/1996/E
|
Decode
| 1,600
|
[
"combinatorics",
"data structures",
"implementation",
"math"
] |
Div. 3
| 2,000
| 256
|
In a desperate attempt to obtain your waifu favorite character, you have hacked into the source code of the game. After days of struggling, you finally find the binary string that encodes the gacha system of the game. In order to decode it, you must first solve the following problem.
You are given a binary string $s$ of length $n$. For each pair of integers $(l, r)$ $(1 \leq l \leq r \leq n)$, count the number of pairs $(x, y)$ $(l \leq x \leq y \leq r)$ such that the amount of $\mathtt{0}$ equals the amount of $\mathtt{1}$ in the substring $s_xs_{x+1}...s_y$.
Output the sum of counts over all possible $(l, r)$ modulo $10^9+7$.
|
The first line contains $t$ ($1 \leq t \leq 1000$) β the number of test cases.
Each test case contains a binary string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$). It is guaranteed $s$ only contains characters $\mathtt{0}$ and $\mathtt{1}$.
It is guaranteed the sum of $|s|$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output an integer, the answer modulo $10^9+7$.
|
[
[
"4\n0000\n01010101\n1100111001\n11000000111",
"0\n130\n147\n70"
]
] |
Title: Decode
time_limit_ms: 2000
memory_limit_mb: 256
Description: In a desperate attempt to obtain your waifu favorite character, you have hacked into the source code of the game. After days of struggling, you finally find the binary string that encodes the gacha system of the game. In order to decode it, you must first solve the following problem.
You are given a binary string $s$ of length $n$. For each pair of integers $(l, r)$ $(1 \leq l \leq r \leq n)$, count the number of pairs $(x, y)$ $(l \leq x \leq y \leq r)$ such that the amount of $\mathtt{0}$ equals the amount of $\mathtt{1}$ in the substring $s_xs_{x+1}...s_y$.
Output the sum of counts over all possible $(l, r)$ modulo $10^9+7$.
Input: The first line contains $t$ ($1 \leq t \leq 1000$) β the number of test cases.
Each test case contains a binary string $s$ ($1 \leq |s| \leq 2 \cdot 10^5$). It is guaranteed $s$ only contains characters $\mathtt{0}$ and $\mathtt{1}$.
It is guaranteed the sum of $|s|$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output an integer, the answer modulo $10^9+7$.
Examples:
input:
4
0000
01010101
1100111001
11000000111
output:
0
130
147
70
Note:
|
|
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$.
|
Title: Make All Equal
time_limit_ms: 1000
memory_limit_mb: 256
Description: 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.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows.
The first line of each test case contains 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$.
Output: For each test case, output a single line containing an integer: the minimum number of operations needed to make all elements in $a$ equal.
Examples:
input:
7
1
1
3
1 2 3
3
1 2 2
5
5 4 3 2 1
6
1 1 2 2 3 3
8
8 7 6 3 8 7 6 3
6
1 1 4 5 1 4
output:
0
2
1
4
4
6
3
Note: 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$.
|
2022A
|
https://codeforces.com/problemset/problem/2022/A
|
Bus to PΓ©njamo
| 800
|
[
"constructive algorithms",
"greedy",
"implementation",
"math"
] |
Div. 2
| 1,000
| 256
|
Ya vamos llegando a PΓ©eeenjamoo β«β«β«
There are $n$ families travelling to PΓ©njamo to witness Mexico's largest- ever "walking a chicken on a leash" marathon. The $i$-th family has $a_i$ family members. All families will travel using a single bus consisting of $r$ rows with $2$ seats each.
A person is considered happy if:
* Another family member is seated in the same row as them, or * They are sitting alone in their row (with an empty seat next to them).
Determine the maximum number of happy people in an optimal seating arrangement. Note that everyone must be seated in the bus.
It is guaranteed that all family members will fit on the bus. Formally, it is guaranteed that $\displaystyle\sum_{i=1}^{n}a_i \le 2r$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $r$ ($1 \le n \le 100$; $1 \le r \le 500$) β the number of families and the number of rows in the bus.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10$) β the number of family members in each family.
|
For each test case, output the maximum number of happy people in an optimal seating arrangement.
|
[
[
"4\n3 3\n2 3 1\n3 3\n2 2 2\n4 5\n1 1 2 2\n4 5\n3 1 1 3",
"4\n6\n6\n6"
]
] |
In the first test case, the two members of the first family can sit together in the first row, while the two members of the second family can sit together in the second row. The remaining member of the second family can sit in the third row along with a member of the third family. This seating arrangement is shown below, where the $4$ happy people are colored green.
$\color{green}{1}$| $\color{green}{1}$ ---|--- $\color{green}{2}$| $\color{green}{2}$ $2$| $3$ In the second test case, a possible seating arrangement with $6$ happy people is shown below.
$\color{green}{3}$| $\color{green}{3}$ ---|--- $\color{green}{1}$| $\color{green}{1}$ $\color{green}{2}$| $\color{green}{2}$ In the third test case, a possible seating arrangement with $6$ happy people is shown below.
$\color{green}{4}$| $\color{green}{4}$ ---|--- $\color{green}{}$| $\color{green}{2}$ $\color{green}{3}$| $\color{green}{3}$ $\color{green}{1}$| $\color{green}{}$ $\color{green}{}$| $\color{green}{}$
|
Title: Bus to PΓ©njamo
time_limit_ms: 1000
memory_limit_mb: 256
Description: Ya vamos llegando a PΓ©eeenjamoo β«β«β«
There are $n$ families travelling to PΓ©njamo to witness Mexico's largest- ever "walking a chicken on a leash" marathon. The $i$-th family has $a_i$ family members. All families will travel using a single bus consisting of $r$ rows with $2$ seats each.
A person is considered happy if:
* Another family member is seated in the same row as them, or * They are sitting alone in their row (with an empty seat next to them).
Determine the maximum number of happy people in an optimal seating arrangement. Note that everyone must be seated in the bus.
It is guaranteed that all family members will fit on the bus. Formally, it is guaranteed that $\displaystyle\sum_{i=1}^{n}a_i \le 2r$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $r$ ($1 \le n \le 100$; $1 \le r \le 500$) β the number of families and the number of rows in the bus.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10$) β the number of family members in each family.
Output: For each test case, output the maximum number of happy people in an optimal seating arrangement.
Examples:
input:
4
3 3
2 3 1
3 3
2 2 2
4 5
1 1 2 2
4 5
3 1 1 3
output:
4
6
6
6
Note: In the first test case, the two members of the first family can sit together in the first row, while the two members of the second family can sit together in the second row. The remaining member of the second family can sit in the third row along with a member of the third family. This seating arrangement is shown below, where the $4$ happy people are colored green.
$\color{green}{1}$| $\color{green}{1}$ ---|--- $\color{green}{2}$| $\color{green}{2}$ $2$| $3$ In the second test case, a possible seating arrangement with $6$ happy people is shown below.
$\color{green}{3}$| $\color{green}{3}$ ---|--- $\color{green}{1}$| $\color{green}{1}$ $\color{green}{2}$| $\color{green}{2}$ In the third test case, a possible seating arrangement with $6$ happy people is shown below.
$\color{green}{4}$| $\color{green}{4}$ ---|--- $\color{green}{}$| $\color{green}{2}$ $\color{green}{3}$| $\color{green}{3}$ $\color{green}{1}$| $\color{green}{}$ $\color{green}{}$| $\color{green}{}$
|
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.
|
Title: Dora's Set
time_limit_ms: 1000
memory_limit_mb: 256
Description: 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$.
Input: 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.
Output: For each test case, output a single integer β the maximum number of operations you can perform.
Examples:
input:
8
1 3
3 7
10 21
2 8
51 60
2 15
10 26
1 1000
output:
1
1
3
1
2
3
4
250
Note: 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.
|
1987F1
|
https://codeforces.com/problemset/problem/1987/F1
|
Interesting Problem (Easy Version)
| 2,500
|
[
"dp"
] |
Div. 1 + 2
| 2,000
| 256
|
This is the easy version of the problem. The only difference between the two versions is the constraint on $n$. You can make hacks only if both versions of the problem are solved.
You are given an array of integers $a$ of length $n$.
In one operation, you will perform the following two-step process:
1. Choose an index $i$ such that $1 \le i < |a|$ and $a_i = i$. 2. Remove $a_i$ and $a_{i+1}$ from the array and concatenate the remaining parts.
Find the maximum number of times that you can perform the operation above.
|
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 100$) β 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 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 the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $100$.
|
For each test case, output a single integer β the maximum number of times that you can perform the operation.
|
[
[
"6\n5\n1 5 3 2 4\n8\n2 1 3 4 5 6 7 8\n3\n1 2 3\n4\n1 2 4 4\n5\n4 4 1 3 5\n1\n1",
"2\n3\n1\n2\n0\n0"
]
] |
In the first test case, one possible optimal sequence of operations is $[ 1, 5, \color{red}{3}, \color{red}{2}, 4 ] \rightarrow [\color{red}{1}, \color{red}{5}, 4] \rightarrow [4]$.
In the third test case, one possible optimal sequence of operations is $[1, \color{red}{2}, \color{red}{3}] \rightarrow [1]$.
|
Title: Interesting Problem (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is the easy version of the problem. The only difference between the two versions is the constraint on $n$. You can make hacks only if both versions of the problem are solved.
You are given an array of integers $a$ of length $n$.
In one operation, you will perform the following two-step process:
1. Choose an index $i$ such that $1 \le i < |a|$ and $a_i = i$. 2. Remove $a_i$ and $a_{i+1}$ from the array and concatenate the remaining parts.
Find the maximum number of times that you can perform the operation above.
Input: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 100$) β 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 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 the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $100$.
Output: For each test case, output a single integer β the maximum number of times that you can perform the operation.
Examples:
input:
6
5
1 5 3 2 4
8
2 1 3 4 5 6 7 8
3
1 2 3
4
1 2 4 4
5
4 4 1 3 5
1
1
output:
2
3
1
2
0
0
Note: In the first test case, one possible optimal sequence of operations is $[ 1, 5, \color{red}{3}, \color{red}{2}, 4 ] \rightarrow [\color{red}{1}, \color{red}{5}, 4] \rightarrow [4]$.
In the third test case, one possible optimal sequence of operations is $[1, \color{red}{2}, \color{red}{3}] \rightarrow [1]$.
|
1980G
|
https://codeforces.com/problemset/problem/1980/G
|
Yasya and the Mysterious Tree
| 2,300
|
[
"bitmasks",
"data structures",
"dfs and similar",
"graphs",
"greedy",
"strings",
"trees"
] |
Div. 3
| 2,500
| 512
|
Yasya was walking in the forest and accidentally found a tree with $n$ vertices. A tree is a connected undirected graph with no cycles.
Next to the tree, the girl found an ancient manuscript with $m$ queries written on it. The queries can be of two types.
The first type of query is described by the integer $y$. The weight of each edge in the tree is replaced by the [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng) of the weight of that edge and the integer $y$.
The second type is described by the vertex $v$ and the integer $x$. Yasya chooses a vertex $u$ ($1 \le u \le n$, $u \neq v$) and mentally draws a bidirectional edge of weight $x$ from $v$ to $u$ in the tree.
Then Yasya finds a simple cycle in the resulting graph and calculates the [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng) of all the edges in it. She wants to choose a vertex $u$ such that the calculated value is maximum. This calculated value will be the answer to the query. It can be shown that such a cycle exists and is unique under the given constraints (independent of the choice of $u$). If an edge between $v$ and $u$ already existed, a simple cycle is the path $v \to u \to v$.
Note that the second type of query is performed mentally, meaning the tree does not change in any way after it.
Help Yasya answer all the queries.
|
The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The descriptions of the test cases follow.
The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m \le 2 \cdot 10^5$) β the number of vertices in the tree and the number of queries.
The next $n - 1$ lines of each test case contain three integers $v$, $u$, $w$ ($1 \le v, u \le n$, $1 \le w \le 10^9$) β the ends of some edge in the tree and its weight.
It is guaranteed that the given set of edges forms a tree.
The next $m$ lines of each test case describe the queries:
* ^ $y$ ($1 \le y \le 10^9$) β parameter of the first type query; * ? $v$ $x$ ($1 \le v \le n$, $1 \le x \le 10^9$) β parameters of the second type query.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. The same is guaranteed for $m$.
|
For each test case, output the answers to the queries of the second type.
|
[
[
"2\n3 7\n1 2 1\n3 1 8\n^ 5\n? 2 9\n^ 1\n? 1 10\n^ 6\n? 3 1\n? 2 9\n5 6\n1 2 777\n3 2 2812\n4 1 16\n5 3 1000000000\n^ 4\n? 3 123\n? 5 1000000000\n^ 1000000000\n? 1 908070\n? 2 1",
"13 15 11 10 \n1000000127 2812 999756331 999999756"
],
[
"3\n8 4\n8 6 3\n6 3 4\n2 5 4\n7 6 2\n7 1 10\n4 1 4\n5 1 2\n^ 4\n^ 7\n? 7 8\n? 4 10\n5 6\n3 1 4\n2 3 9\n4 3 6\n5 2 10\n? 5 7\n^ 1\n^ 8\n? 4 10\n? 1 9\n? 3 6\n4 2\n2 1 4\n4 3 5\n2 3 4\n^ 13\n? 1 10",
"14 13 \n13 8 11 11 \n10"
]
] |
Title: Yasya and the Mysterious Tree
time_limit_ms: 2500
memory_limit_mb: 512
Description: Yasya was walking in the forest and accidentally found a tree with $n$ vertices. A tree is a connected undirected graph with no cycles.
Next to the tree, the girl found an ancient manuscript with $m$ queries written on it. The queries can be of two types.
The first type of query is described by the integer $y$. The weight of each edge in the tree is replaced by the [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng) of the weight of that edge and the integer $y$.
The second type is described by the vertex $v$ and the integer $x$. Yasya chooses a vertex $u$ ($1 \le u \le n$, $u \neq v$) and mentally draws a bidirectional edge of weight $x$ from $v$ to $u$ in the tree.
Then Yasya finds a simple cycle in the resulting graph and calculates the [bitwise exclusive OR](http://tiny.cc/xor_wiki_eng) of all the edges in it. She wants to choose a vertex $u$ such that the calculated value is maximum. This calculated value will be the answer to the query. It can be shown that such a cycle exists and is unique under the given constraints (independent of the choice of $u$). If an edge between $v$ and $u$ already existed, a simple cycle is the path $v \to u \to v$.
Note that the second type of query is performed mentally, meaning the tree does not change in any way after it.
Help Yasya answer all the queries.
Input: The first line contains an integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The descriptions of the test cases follow.
The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 2 \cdot 10^5$, $1 \le m \le 2 \cdot 10^5$) β the number of vertices in the tree and the number of queries.
The next $n - 1$ lines of each test case contain three integers $v$, $u$, $w$ ($1 \le v, u \le n$, $1 \le w \le 10^9$) β the ends of some edge in the tree and its weight.
It is guaranteed that the given set of edges forms a tree.
The next $m$ lines of each test case describe the queries:
* ^ $y$ ($1 \le y \le 10^9$) β parameter of the first type query; * ? $v$ $x$ ($1 \le v \le n$, $1 \le x \le 10^9$) β parameters of the second type query.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. The same is guaranteed for $m$.
Output: For each test case, output the answers to the queries of the second type.
Examples:
input:
2
3 7
1 2 1
3 1 8
^ 5
? 2 9
^ 1
? 1 10
^ 6
? 3 1
? 2 9
5 6
1 2 777
3 2 2812
4 1 16
5 3 1000000000
^ 4
? 3 123
? 5 1000000000
^ 1000000000
? 1 908070
? 2 1
output:
13 15 11 10
1000000127 2812 999756331 999999756
input:
3
8 4
8 6 3
6 3 4
2 5 4
7 6 2
7 1 10
4 1 4
5 1 2
^ 4
^ 7
? 7 8
? 4 10
5 6
3 1 4
2 3 9
4 3 6
5 2 10
? 5 7
^ 1
^ 8
? 4 10
? 1 9
? 3 6
4 2
2 1 4
4 3 5
2 3 4
^ 13
? 1 10
output:
14 13
13 8 11 11
10
Note:
|
|
1975F
|
https://codeforces.com/problemset/problem/1975/F
|
Set
| 2,600
|
[
"bitmasks",
"brute force",
"combinatorics",
"dfs and similar",
"divide and conquer",
"dp",
"math"
] |
Div. 1 + 2
| 2,000
| 512
|
Define the binary encoding of a finite set of natural numbers $T \subseteq \\{0,1,2,\ldots\\}$ as $f(T) = \sum\limits_{i \in T} 2^i$. For example, $f(\\{0,2\\}) = 2^0 + 2^2 = 5$ and $f(\\{\\}) = 0$. Notice that $f$ is a bijection from all such sets to all non-negative integers. As such, $f^{-1}$ is also defined.
You are given an integer $n$ along with $2^n-1$ sets $V_1,V_2,\ldots,V_{2^n-1}$.
Find all sets $S$ that satisfy the following constraint:
* $S \subseteq \\{0,1,\ldots,n-1\\}$. Note that $S$ can be empty. * For all non-empty subsets $T \subseteq \\{0,1,\ldots,n-1\\}$, $|S \cap T| \in V_{f(T)}$.
Due to the large input and output, both input and output will be given in terms of binary encodings of the sets.
|
The first line of input contains a single integer $n$ ($1 \leq n \leq 20$).
The second line of input contains $2^n-1$ integers $v_1,v_2,\ldots,v_{2^n-1}$ ($0 \leq v_i < 2^{n+1}$) β the sets $V_i$ given in their binary encoding where $V_i = f^{-1}(v_i)$.
|
The first line of output should contain an integer $k$ indicating the number of possible $S$.
In the following $k$ lines, you should output $f(S)$ for all possible $S$ in increasing order.
|
[
[
"3\n15 15 15 15 15 15 12",
"4\n3\n5\n6\n7"
],
[
"5\n63 63 63 63 6 63 63 63 63 63 63 5 63 63 63 63 63 63 8 63 63 63 63 2 63 63 63 63 63 63 63",
"1\n19"
]
] |
In the first test case, one possible $S$ is $f^{-1}(3) = \\{0,1\\}$. All the non-empty subsets $T \subseteq \\{0,1,2\\}$ and the corresponding $|S \cap T|$, $f(T)$ and $V_f(T)$ are as follows:
$T$| $|S\cap T|$| $f(T)$| $V_{f(T)}$ ---|---|---|--- $\\{0\\}$| $1$| $1$| $\\{0,1,2,3\\}$ $\\{1\\}$| $1$| $2$| $\\{0,1,2,3\\}$ $\\{2\\}$| $0$| $4$| $\\{0,1,2,3\\}$ $\\{0,1\\}$| $2$| $3$| $\\{0,1,2,3\\}$ $\\{0,2\\}$| $1$| $5$| $\\{0,1,2,3\\}$ $\\{1,2\\}$| $1$| $6$| $\\{0,1,2,3\\}$ $\\{0,1,2\\}$| $2$| $7$| $\\{2,3\\}$
|
Title: Set
time_limit_ms: 2000
memory_limit_mb: 512
Description: Define the binary encoding of a finite set of natural numbers $T \subseteq \\{0,1,2,\ldots\\}$ as $f(T) = \sum\limits_{i \in T} 2^i$. For example, $f(\\{0,2\\}) = 2^0 + 2^2 = 5$ and $f(\\{\\}) = 0$. Notice that $f$ is a bijection from all such sets to all non-negative integers. As such, $f^{-1}$ is also defined.
You are given an integer $n$ along with $2^n-1$ sets $V_1,V_2,\ldots,V_{2^n-1}$.
Find all sets $S$ that satisfy the following constraint:
* $S \subseteq \\{0,1,\ldots,n-1\\}$. Note that $S$ can be empty. * For all non-empty subsets $T \subseteq \\{0,1,\ldots,n-1\\}$, $|S \cap T| \in V_{f(T)}$.
Due to the large input and output, both input and output will be given in terms of binary encodings of the sets.
Input: The first line of input contains a single integer $n$ ($1 \leq n \leq 20$).
The second line of input contains $2^n-1$ integers $v_1,v_2,\ldots,v_{2^n-1}$ ($0 \leq v_i < 2^{n+1}$) β the sets $V_i$ given in their binary encoding where $V_i = f^{-1}(v_i)$.
Output: The first line of output should contain an integer $k$ indicating the number of possible $S$.
In the following $k$ lines, you should output $f(S)$ for all possible $S$ in increasing order.
Examples:
input:
3
15 15 15 15 15 15 12
output:
4
3
5
6
7
input:
5
63 63 63 63 6 63 63 63 63 63 63 5 63 63 63 63 63 63 8 63 63 63 63 2 63 63 63 63 63 63 63
output:
1
19
Note: In the first test case, one possible $S$ is $f^{-1}(3) = \\{0,1\\}$. All the non-empty subsets $T \subseteq \\{0,1,2\\}$ and the corresponding $|S \cap T|$, $f(T)$ and $V_f(T)$ are as follows:
$T$| $|S\cap T|$| $f(T)$| $V_{f(T)}$ ---|---|---|--- $\\{0\\}$| $1$| $1$| $\\{0,1,2,3\\}$ $\\{1\\}$| $1$| $2$| $\\{0,1,2,3\\}$ $\\{2\\}$| $0$| $4$| $\\{0,1,2,3\\}$ $\\{0,1\\}$| $2$| $3$| $\\{0,1,2,3\\}$ $\\{0,2\\}$| $1$| $5$| $\\{0,1,2,3\\}$ $\\{1,2\\}$| $1$| $6$| $\\{0,1,2,3\\}$ $\\{0,1,2\\}$| $2$| $7$| $\\{2,3\\}$
|
1974B
|
https://codeforces.com/problemset/problem/1974/B
|
Symmetric Encoding
| 800
|
[
"implementation",
"sortings",
"strings"
] |
Div. 3
| 2,000
| 256
|
Polycarp has a string $s$, which consists of lowercase Latin letters. He encodes this string using the following algorithm:
* first, he constructs a new auxiliary string $r$, which consists of all distinct letters of the string $s$, written in alphabetical order; * then the encoding happens as follows: each character in the string $s$ is replaced by its symmetric character from the string $r$ (the first character of the string $r$ will be replaced by the last, the second by the second from the end, and so on).
For example, encoding the string $s$="codeforces" happens as follows:
* the string $r$ is obtained as "cdefors"; * the first character $s_1$='c' is replaced by 's'; * the second character $s_2$='o' is replaced by 'e'; * the third character $s_3$='d' is replaced by 'r'; * ... * the last character $s_{10}$='s' is replaced by 'c'.
 The string $r$ and replacements for $s$="codeforces".
Thus, the result of encoding the string $s$="codeforces" is the string "serofedsoc".
Write a program that performs decoding β that is, restores the original string $s$ from the encoding result.
|
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 length of the string $b$.
The second line of each test case contains a string $b$ of length $n$, consisting of lowercase Latin letters β the result of encoding the original string $s$.
It is guaranteed that the sum of the values of $n$ over all test cases in the test does not exceed $2 \cdot 10^5$.
|
For each test case, output the string $s$ from which the encoding result $b$ was obtained.
|
[
[
"5\n10\nserofedsoc\n3\nttf\n9\ntlrhgmaoi\n1\nw\n15\nhnndledmnhlttin",
"codeforces\nfft\nalgorithm\nw\nmeetinthemiddle"
]
] |
Title: Symmetric Encoding
time_limit_ms: 2000
memory_limit_mb: 256
Description: Polycarp has a string $s$, which consists of lowercase Latin letters. He encodes this string using the following algorithm:
* first, he constructs a new auxiliary string $r$, which consists of all distinct letters of the string $s$, written in alphabetical order; * then the encoding happens as follows: each character in the string $s$ is replaced by its symmetric character from the string $r$ (the first character of the string $r$ will be replaced by the last, the second by the second from the end, and so on).
For example, encoding the string $s$="codeforces" happens as follows:
* the string $r$ is obtained as "cdefors"; * the first character $s_1$='c' is replaced by 's'; * the second character $s_2$='o' is replaced by 'e'; * the third character $s_3$='d' is replaced by 'r'; * ... * the last character $s_{10}$='s' is replaced by 'c'.
 The string $r$ and replacements for $s$="codeforces".
Thus, the result of encoding the string $s$="codeforces" is the string "serofedsoc".
Write a program that performs decoding β that is, restores the original string $s$ from the encoding result.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β the length of the string $b$.
The second line of each test case contains a string $b$ of length $n$, consisting of lowercase Latin letters β the result of encoding the original string $s$.
It is guaranteed that the sum of the values of $n$ over all test cases in the test does not exceed $2 \cdot 10^5$.
Output: For each test case, output the string $s$ from which the encoding result $b$ was obtained.
Examples:
input:
5
10
serofedsoc
3
ttf
9
tlrhgmaoi
1
w
15
hnndledmnhlttin
output:
codeforces
fft
algorithm
w
meetinthemiddle
Note:
|
|
2036D
|
https://codeforces.com/problemset/problem/2036/D
|
I Love 1543
| 1,300
|
[
"brute force",
"implementation",
"matrices"
] |
Div. 3
| 2,000
| 256
|
One morning, Polycarp woke up and realized that $1543$ is the most favorite number in his life.
The first thing that Polycarp saw that day as soon as he opened his eyes was a large wall carpet of size $n$ by $m$ cells; $n$ and $m$ are even integers. Each cell contains one of the digits from $0$ to $9$.
Polycarp became curious about how many times the number $1543$ would appear in all layers$^{\text{β}}$ of the carpet when traversed clockwise.
$^{\text{β}}$The first layer of a carpet of size $n \times m$ is defined as a closed strip of length $2 \cdot (n+m-2)$ and thickness of $1$ element, surrounding its outer part. Each subsequent layer is defined as the first layer of the carpet obtained by removing all previous layers from the original carpet.
|
The first line of the input contains a single integer $t$ ($1 \leq t \leq 100$) β the number of test cases. The following lines describe the test cases.
The first line of each test case contains a pair of numbers $n$ and $m$ ($2 \leq n, m \leq 10^3$, $n, m$ β even integers).
This is followed by $n$ lines of length $m$, consisting of digits from $0$ to $9$ β the description of the carpet.
It is guaranteed that the sum of $n \cdot m$ across all test cases does not exceed $10^6$.
|
For each test case, output a single number β the total number of times $1543$ appears in all layers of the carpet in the order of traversal clockwise.
|
[
[
"8\n2 4\n1543\n7777\n2 4\n7154\n8903\n2 4\n3451\n8888\n2 2\n54\n13\n2 2\n51\n43\n2 6\n432015\n512034\n4 4\n5431\n1435\n5518\n7634\n6 4\n5432\n1152\n4542\n2432\n2302\n5942",
"1\n1\n0\n1\n0\n2\n2\n2"
]
] |
 Occurrences of $1543$ in the seventh example. Different layers are colored in different colors.
|
Title: I Love 1543
time_limit_ms: 2000
memory_limit_mb: 256
Description: One morning, Polycarp woke up and realized that $1543$ is the most favorite number in his life.
The first thing that Polycarp saw that day as soon as he opened his eyes was a large wall carpet of size $n$ by $m$ cells; $n$ and $m$ are even integers. Each cell contains one of the digits from $0$ to $9$.
Polycarp became curious about how many times the number $1543$ would appear in all layers$^{\text{β}}$ of the carpet when traversed clockwise.
$^{\text{β}}$The first layer of a carpet of size $n \times m$ is defined as a closed strip of length $2 \cdot (n+m-2)$ and thickness of $1$ element, surrounding its outer part. Each subsequent layer is defined as the first layer of the carpet obtained by removing all previous layers from the original carpet.
Input: The first line of the input contains a single integer $t$ ($1 \leq t \leq 100$) β the number of test cases. The following lines describe the test cases.
The first line of each test case contains a pair of numbers $n$ and $m$ ($2 \leq n, m \leq 10^3$, $n, m$ β even integers).
This is followed by $n$ lines of length $m$, consisting of digits from $0$ to $9$ β the description of the carpet.
It is guaranteed that the sum of $n \cdot m$ across all test cases does not exceed $10^6$.
Output: For each test case, output a single number β the total number of times $1543$ appears in all layers of the carpet in the order of traversal clockwise.
Examples:
input:
8
2 4
1543
7777
2 4
7154
8903
2 4
3451
8888
2 2
54
13
2 2
51
43
2 6
432015
512034
4 4
5431
1435
5518
7634
6 4
5432
1152
4542
2432
2302
5942
output:
1
1
0
1
0
2
2
2
Note:  Occurrences of $1543$ in the seventh example. Different layers are colored in different colors.
|
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:

* 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):

The depth of the formed full binary tree is $4$.
In the fifth test case, the final tree is shown below:

It can be proved that Iris can't form any full binary tree by adding vertices and edges, so the binary depth is $-1$.
|
Title: Iris's Full Binary Tree
time_limit_ms: 4000
memory_limit_mb: 1024
Description: 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?
Input: 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$.
Output: For each test case output $n$ integers, $i$-th of them representing the binary depth of the tree formed by the first $i$ vertices.
Examples:
input:
7
3
1 1
6
1 2 3 4 5
7
1 1 3 2 5 1
10
1 1 2 1 4 2 4 5 8
10
1 1 3 1 3 2 2 2 6
20
1 1 2 2 4 4 5 5 7 6 8 6 11 14 11 8 13 13 12
25
1 1 3 3 1 5 4 4 6 8 11 12 8 7 11 13 7 13 15 6 19 14 10 23
output:
1 2 2
1 2 2 3 3 4
1 2 2 3 3 4 4
1 2 2 3 3 3 4 4 5 5
1 2 2 3 3 4 4 4 -1 -1
1 2 2 3 3 4 4 4 4 5 5 5 5 6 6 6 6 6 6 7
1 2 2 3 3 4 4 4 4 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8
Note: In the first test case, the final tree is shown below:

* 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):

The depth of the formed full binary tree is $4$.
In the fifth test case, the final tree is shown below:

It can be proved that Iris can't form any full binary tree by adding vertices and edges, so the binary depth is $-1$.
|
1988A
|
https://codeforces.com/problemset/problem/1988/A
|
Split the Multiset
| 900
|
[
"brute force",
"greedy",
"implementation",
"math"
] |
Div. 2
| 1,000
| 512
|
A multiset is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. For example, $\\{2,2,4\\}$ is a multiset.
You have a multiset $S$. Initially, the multiset contains only one positive integer $n$. That is, $S=\\{n\\}$. Additionally, there is a given positive integer $k$.
In one operation, you can select any positive integer $u$ in $S$ and remove one copy of $u$ from $S$. Then, insert no more than $k$ positive integers into $S$ so that the sum of all inserted integers is equal to $u$.
Find the minimum number of operations to make $S$ contain $n$ ones.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows.
The only line of each testcase contains two integers $n,k$ ($1\le n\le 1000,2\le k\le 1000$).
|
For each testcase, print one integer, which is the required answer.
|
[
[
"4\n1 5\n5 2\n6 3\n16 4",
"0\n4\n3\n5"
]
] |
For the first test case, initially $S=\\{1\\}$, already satisfying the requirement. Therefore, we need zero operations.
For the second test case, initially $S=\\{5\\}$. We can apply the following operations:
* Select $u=5$, remove $u$ from $S$, and insert $2,3$ into $S$. Now, $S=\\{2,3\\}$. * Select $u=2$, remove $u$ from $S$, and insert $1,1$ into $S$. Now, $S=\\{1,1,3\\}$. * Select $u=3$, remove $u$ from $S$, and insert $1,2$ into $S$. Now, $S=\\{1,1,1,2\\}$. * Select $u=2$, remove $u$ from $S$, and insert $1,1$ into $S$. Now, $S=\\{1,1,1,1,1\\}$.
Using $4$ operations in total, we achieve the goal.
For the third test case, initially $S=\\{6\\}$. We can apply the following operations:
* Select $u=6$, remove $u$ from $S$, and insert $1,2,3$ into $S$. Now, $S=\\{1,2,3\\}$. * Select $u=2$, remove $u$ from $S$, and insert $1,1$ into $S$. Now, $S=\\{1,1,1,3\\}$. * Select $u=3$, remove $u$ from $S$, and insert $1,1,1$ into $S$. Now, $S=\\{1,1,1,1,1,1\\}$.
Using $3$ operations in total, we achieve the goal.
For the fourth test case, initially $S=\\{16\\}$. We can apply the following operations:
* Select $u=16$, remove $u$ from $S$, and insert $4,4,4,4$ into $S$. Now, $S=\\{4,4,4,4\\}$. * Repeat for $4$ times: select $u=4$, remove $u$ from $S$, and insert $1,1,1,1$ into $S$.
Using $5$ operations in total, we achieve the goal.
|
Title: Split the Multiset
time_limit_ms: 1000
memory_limit_mb: 512
Description: A multiset is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. For example, $\\{2,2,4\\}$ is a multiset.
You have a multiset $S$. Initially, the multiset contains only one positive integer $n$. That is, $S=\\{n\\}$. Additionally, there is a given positive integer $k$.
In one operation, you can select any positive integer $u$ in $S$ and remove one copy of $u$ from $S$. Then, insert no more than $k$ positive integers into $S$ so that the sum of all inserted integers is equal to $u$.
Find the minimum number of operations to make $S$ contain $n$ ones.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows.
The only line of each testcase contains two integers $n,k$ ($1\le n\le 1000,2\le k\le 1000$).
Output: For each testcase, print one integer, which is the required answer.
Examples:
input:
4
1 5
5 2
6 3
16 4
output:
0
4
3
5
Note: For the first test case, initially $S=\\{1\\}$, already satisfying the requirement. Therefore, we need zero operations.
For the second test case, initially $S=\\{5\\}$. We can apply the following operations:
* Select $u=5$, remove $u$ from $S$, and insert $2,3$ into $S$. Now, $S=\\{2,3\\}$. * Select $u=2$, remove $u$ from $S$, and insert $1,1$ into $S$. Now, $S=\\{1,1,3\\}$. * Select $u=3$, remove $u$ from $S$, and insert $1,2$ into $S$. Now, $S=\\{1,1,1,2\\}$. * Select $u=2$, remove $u$ from $S$, and insert $1,1$ into $S$. Now, $S=\\{1,1,1,1,1\\}$.
Using $4$ operations in total, we achieve the goal.
For the third test case, initially $S=\\{6\\}$. We can apply the following operations:
* Select $u=6$, remove $u$ from $S$, and insert $1,2,3$ into $S$. Now, $S=\\{1,2,3\\}$. * Select $u=2$, remove $u$ from $S$, and insert $1,1$ into $S$. Now, $S=\\{1,1,1,3\\}$. * Select $u=3$, remove $u$ from $S$, and insert $1,1,1$ into $S$. Now, $S=\\{1,1,1,1,1,1\\}$.
Using $3$ operations in total, we achieve the goal.
For the fourth test case, initially $S=\\{16\\}$. We can apply the following operations:
* Select $u=16$, remove $u$ from $S$, and insert $4,4,4,4$ into $S$. Now, $S=\\{4,4,4,4\\}$. * Repeat for $4$ times: select $u=4$, remove $u$ from $S$, and insert $1,1,1,1$ into $S$.
Using $5$ operations in total, we achieve the goal.
|
2020B
|
https://codeforces.com/problemset/problem/2020/B
|
Brightness Begins
| 1,200
|
[
"binary search",
"math"
] |
Div. 2
| 1,000
| 256
|
Imagine you have $n$ light bulbs numbered $1, 2, \ldots, n$. Initially, all bulbs are on. To flip the state of a bulb means to turn it off if it used to be on, and to turn it on otherwise.
Next, you do the following:
* for each $i = 1, 2, \ldots, n$, flip the state of all bulbs $j$ such that $j$ is divisible by $i^\dagger$.
After performing all operations, there will be several bulbs that are still on. Your goal is to make this number exactly $k$.
Find the smallest suitable $n$ such that after performing the operations there will be exactly $k$ bulbs on. We can show that an answer always exists.
$^\dagger$ An integer $x$ is divisible by $y$ if there exists an integer $z$ such that $x = y\cdot z$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The only line of each test case contains a single integer $k$ ($1 \le k \le 10^{18}$).
|
For each test case, output $n$ β the minimum number of bulbs.
|
[
[
"3\n1\n3\n8",
"2\n5\n11"
]
] |
In the first test case, the minimum number of bulbs is $2$. Let's denote the state of all bulbs with an array, where $1$ corresponds to a turned on bulb, and $0$ corresponds to a turned off bulb. Initially, the array is $[1, 1]$.
* After performing the operation with $i = 1$, the array becomes $[\underline{0}, \underline{0}]$. * After performing the operation with $i = 2$, the array becomes $[0, \underline{1}]$.
In the end, there are $k = 1$ bulbs on. We can also show that the answer cannot be less than $2$.
In the second test case, the minimum number of bulbs is $5$. Initially, the array is $[1, 1, 1, 1, 1]$.
* After performing the operation with $i = 1$, the array becomes $[\underline{0}, \underline{0}, \underline{0}, \underline{0}, \underline{0}]$. * After performing the operation with $i = 2$, the array becomes $[0, \underline{1}, 0, \underline{1}, 0]$. * After performing the operation with $i = 3$, the array becomes $[0, 1, \underline{1}, 1, 0]$. * After performing the operation with $i = 4$, the array becomes $[0, 1, 1, \underline{0}, 0]$. * After performing the operation with $i = 5$, the array becomes $[0, 1, 1, 0, \underline{1}]$.
In the end, there are $k = 3$ bulbs on. We can also show that the answer cannot be smaller than $5$.
|
Title: Brightness Begins
time_limit_ms: 1000
memory_limit_mb: 256
Description: Imagine you have $n$ light bulbs numbered $1, 2, \ldots, n$. Initially, all bulbs are on. To flip the state of a bulb means to turn it off if it used to be on, and to turn it on otherwise.
Next, you do the following:
* for each $i = 1, 2, \ldots, n$, flip the state of all bulbs $j$ such that $j$ is divisible by $i^\dagger$.
After performing all operations, there will be several bulbs that are still on. Your goal is to make this number exactly $k$.
Find the smallest suitable $n$ such that after performing the operations there will be exactly $k$ bulbs on. We can show that an answer always exists.
$^\dagger$ An integer $x$ is divisible by $y$ if there exists an integer $z$ such that $x = y\cdot z$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The only line of each test case contains a single integer $k$ ($1 \le k \le 10^{18}$).
Output: For each test case, output $n$ β the minimum number of bulbs.
Examples:
input:
3
1
3
8
output:
2
5
11
Note: In the first test case, the minimum number of bulbs is $2$. Let's denote the state of all bulbs with an array, where $1$ corresponds to a turned on bulb, and $0$ corresponds to a turned off bulb. Initially, the array is $[1, 1]$.
* After performing the operation with $i = 1$, the array becomes $[\underline{0}, \underline{0}]$. * After performing the operation with $i = 2$, the array becomes $[0, \underline{1}]$.
In the end, there are $k = 1$ bulbs on. We can also show that the answer cannot be less than $2$.
In the second test case, the minimum number of bulbs is $5$. Initially, the array is $[1, 1, 1, 1, 1]$.
* After performing the operation with $i = 1$, the array becomes $[\underline{0}, \underline{0}, \underline{0}, \underline{0}, \underline{0}]$. * After performing the operation with $i = 2$, the array becomes $[0, \underline{1}, 0, \underline{1}, 0]$. * After performing the operation with $i = 3$, the array becomes $[0, 1, \underline{1}, 1, 0]$. * After performing the operation with $i = 4$, the array becomes $[0, 1, 1, \underline{0}, 0]$. * After performing the operation with $i = 5$, the array becomes $[0, 1, 1, 0, \underline{1}]$.
In the end, there are $k = 3$ bulbs on. We can also show that the answer cannot be smaller than $5$.
|
1992G
|
https://codeforces.com/problemset/problem/1992/G
|
Ultra-Meow
| 2,000
|
[
"combinatorics",
"dp",
"math"
] |
Div. 3
| 2,500
| 256
|
K1o0n gave you an array $a$ of length $n$, consisting of numbers $1, 2, \ldots, n$. Accept it? Of course! But what to do with it? Of course, calculate $\text{MEOW}(a)$.
Let $\text{MEX}(S, k)$ be the $k$-th positive (strictly greater than zero) integer in ascending order that is not present in the set $S$. Denote $\text{MEOW}(a)$ as the sum of $\text{MEX}(b, |b| + 1)$, over all distinct subsets $b$ of the array $a$.
Examples of $\text{MEX}(S, k)$ values for sets:
* $\text{MEX}(\\{3,2\\}, 1) = 1$, because $1$ is the first positive integer not present in the set; * $\text{MEX}(\\{4,2,1\\}, 2) = 5$, because the first two positive integers not present in the set are $3$ and $5$; * $\text{MEX}(\\{\\}, 4) = 4$, because there are no numbers in the empty set, so the first $4$ positive integers not present in it are $1, 2, 3, 4$.
|
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
In a single line of each test case, an integer $n$ ($1 \le n \le 5000$) is entered, the size of the array of gifted numbers.
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $25 \cdot 10^6$.
|
For each test case, output a single number β $\text{MEOW}(a)$. Since it may be very large, output it modulo $10^9 + 7$.
|
[
[
"5\n2\n3\n4999\n5\n1",
"12\n31\n354226409\n184\n4"
]
] |
Title: Ultra-Meow
time_limit_ms: 2500
memory_limit_mb: 256
Description: K1o0n gave you an array $a$ of length $n$, consisting of numbers $1, 2, \ldots, n$. Accept it? Of course! But what to do with it? Of course, calculate $\text{MEOW}(a)$.
Let $\text{MEX}(S, k)$ be the $k$-th positive (strictly greater than zero) integer in ascending order that is not present in the set $S$. Denote $\text{MEOW}(a)$ as the sum of $\text{MEX}(b, |b| + 1)$, over all distinct subsets $b$ of the array $a$.
Examples of $\text{MEX}(S, k)$ values for sets:
* $\text{MEX}(\\{3,2\\}, 1) = 1$, because $1$ is the first positive integer not present in the set; * $\text{MEX}(\\{4,2,1\\}, 2) = 5$, because the first two positive integers not present in the set are $3$ and $5$; * $\text{MEX}(\\{\\}, 4) = 4$, because there are no numbers in the empty set, so the first $4$ positive integers not present in it are $1, 2, 3, 4$.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
In a single line of each test case, an integer $n$ ($1 \le n \le 5000$) is entered, the size of the array of gifted numbers.
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $25 \cdot 10^6$.
Output: For each test case, output a single number β $\text{MEOW}(a)$. Since it may be very large, output it modulo $10^9 + 7$.
Examples:
input:
5
2
3
4999
5
1
output:
12
31
354226409
184
4
Note:
|
|
1974F
|
https://codeforces.com/problemset/problem/1974/F
|
Cutting Game
| 1,900
|
[
"binary search",
"brute force",
"data structures",
"implementation",
"sortings",
"two pointers"
] |
Div. 3
| 3,000
| 256
|
Alice and Bob were playing a game again. They have a grid of size $a \times b$ ($1 \le a, b \le 10^9$), on which there are $n$ chips, with at most one chip in each cell. The cell at the intersection of the $x$-th row and the $y$-th column has coordinates $(x, y)$.
Alice made the first move, and the players took turns. On each move, a player could cut several (but not all) rows or columns from the beginning or end of the remaining grid and earn a point for each chip that was on the cut part of the grid. Each move can be described by the character 'U', 'D', 'L', or 'R' and an integer $k$:
* If the character is 'U', then the first $k$ remaining rows will be cut; * If the character is 'D', then the last $k$ remaining rows will be cut; * If the character is 'L', then the first $k$ remaining columns will be cut; * If the character is 'R', then the last $k$ remaining columns will be cut.
Based on the initial state of the grid and the players' moves, determine the number of points earned by Alice and Bob, respectively.
|
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 four integers $a$, $b$, $n$, and $m$ ($2 \le a, b \le 10^9$, $1 \le n, m \le 2 \cdot 10^5$) β the dimensions of the grid, the number of chips, and the number of moves.
Each of the next $n$ lines contain two integers $x_i$ and $y_i$ ($1 \le x_i \le a$, $1 \le y_i \le b$) β the coordinates of the chips. All pairs of coordinates are distinct.
Each of the next $m$ lines contain a character $c_j$ and an integer $k_j$ β the description of the $j$-th move. It is guaranteed that $k$ is less than the number of rows/columns in the current grid. In other words, a player cannot cut the entire remaining grid on their move.
It is guaranteed that the sum of the values of $n$ across all test cases in the test does not exceed $2 \cdot 10^5$. It is guaranteed that the sum of the values of $m$ across all test cases in the test does not exceed $2 \cdot 10^5$.
|
For each test case, output two integers β the number of points earned by Alice and Bob, respectively.
|
[
[
"6\n4 4 3 2\n4 1\n3 3\n2 4\nD 2\nR 1\n4 4 3 3\n4 1\n3 2\n2 3\nD 1\nL 1\nU 2\n3 5 3 2\n1 3\n2 2\n3 3\nR 2\nR 2\n6 4 4 2\n1 4\n2 3\n5 3\n1 1\nR 1\nU 1\n9 3 2 1\n6 1\n3 3\nD 8\n10 10 2 5\n7 5\n9 1\nR 1\nL 2\nD 1\nU 4\nD 1",
"2 1\n2 0\n0 3\n1 1\n2 0\n0 1"
]
] |
Below is the game from the first example:

On her turn, Alice cut $2$ rows from the bottom and scored $2$ points, then Bob cut $1$ column from the right and scored one point. Note that if Bob had cut $1$ row from the bottom, he would have also scored $1$ point.
|
Title: Cutting Game
time_limit_ms: 3000
memory_limit_mb: 256
Description: Alice and Bob were playing a game again. They have a grid of size $a \times b$ ($1 \le a, b \le 10^9$), on which there are $n$ chips, with at most one chip in each cell. The cell at the intersection of the $x$-th row and the $y$-th column has coordinates $(x, y)$.
Alice made the first move, and the players took turns. On each move, a player could cut several (but not all) rows or columns from the beginning or end of the remaining grid and earn a point for each chip that was on the cut part of the grid. Each move can be described by the character 'U', 'D', 'L', or 'R' and an integer $k$:
* If the character is 'U', then the first $k$ remaining rows will be cut; * If the character is 'D', then the last $k$ remaining rows will be cut; * If the character is 'L', then the first $k$ remaining columns will be cut; * If the character is 'R', then the last $k$ remaining columns will be cut.
Based on the initial state of the grid and the players' moves, determine the number of points earned by Alice and Bob, respectively.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
The first line of each test case contains four integers $a$, $b$, $n$, and $m$ ($2 \le a, b \le 10^9$, $1 \le n, m \le 2 \cdot 10^5$) β the dimensions of the grid, the number of chips, and the number of moves.
Each of the next $n$ lines contain two integers $x_i$ and $y_i$ ($1 \le x_i \le a$, $1 \le y_i \le b$) β the coordinates of the chips. All pairs of coordinates are distinct.
Each of the next $m$ lines contain a character $c_j$ and an integer $k_j$ β the description of the $j$-th move. It is guaranteed that $k$ is less than the number of rows/columns in the current grid. In other words, a player cannot cut the entire remaining grid on their move.
It is guaranteed that the sum of the values of $n$ across all test cases in the test does not exceed $2 \cdot 10^5$. It is guaranteed that the sum of the values of $m$ across all test cases in the test does not exceed $2 \cdot 10^5$.
Output: For each test case, output two integers β the number of points earned by Alice and Bob, respectively.
Examples:
input:
6
4 4 3 2
4 1
3 3
2 4
D 2
R 1
4 4 3 3
4 1
3 2
2 3
D 1
L 1
U 2
3 5 3 2
1 3
2 2
3 3
R 2
R 2
6 4 4 2
1 4
2 3
5 3
1 1
R 1
U 1
9 3 2 1
6 1
3 3
D 8
10 10 2 5
7 5
9 1
R 1
L 2
D 1
U 4
D 1
output:
2 1
2 0
0 3
1 1
2 0
0 1
Note: Below is the game from the first example:

On her turn, Alice cut $2$ rows from the bottom and scored $2$ points, then Bob cut $1$ column from the right and scored one point. Note that if Bob had cut $1$ row from the bottom, he would have also scored $1$ point.
|
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$.
|
Title: Battle for Survive
time_limit_ms: 1000
memory_limit_mb: 256
Description: 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.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of 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$.
Output: For each testcase, output a single integer β the maximum rating that the last remaining fighter can preserve.
Examples:
input:
5
2
2 1
3
2 2 8
4
1 2 4 3
5
1 2 3 4 5
5
3 2 4 5 4
output:
-1
8
2
7
8
Note: 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$.
|
1975B
|
https://codeforces.com/problemset/problem/1975/B
|
378QAQ and Mocha's Array
| 1,000
|
[
"brute force",
"greedy",
"math",
"sortings"
] |
Div. 1 + 2
| 1,000
| 256
|
Mocha likes arrays, so before her departure, 378QAQ gave her an array $a$ consisting of $n$ positive integers as a gift.
Mocha thinks that $a$ is beautiful if there exist two numbers $i$ and $j$ ($1\leq i,j\leq n$, $i\neq j$) such that for all $k$ ($1 \leq k \leq n$), $a_k$ is divisible$^\dagger$ by either $a_i$ or $a_j$.
Determine whether $a$ is beautiful.
$^\dagger$ $x$ is divisible by $y$ if there exists an integer $z$ such that $x = y \cdot z$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 500$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($3\leq n\leq 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq a_i \leq 10^9$) β the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
|
For each test case, output "Yes" if array $a$ is beautiful, and output "No" otherwise.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
|
[
[
"4\n3\n7 3 8\n5\n7 1 9 3 5\n5\n4 12 2 6 3\n5\n7 49 9 3 1000000000",
"No\nYes\nYes\nNo"
]
] |
In the first test case, any two numbers in the array are coprime, so the answer is "No".
In the second test case, we can pick $i=2$ and $j=1$. Since every number in the array is divisible by $a_i = 1$, the answer is "Yes".
In the third test case, we can pick $i=3$ and $j=5$. $2$ and $4$ is divisible by $a_i = 2$ while $3$, $6$ and $12$ is divisible by $a_j = 3$, so the answer is "Yes".
|
Title: 378QAQ and Mocha's Array
time_limit_ms: 1000
memory_limit_mb: 256
Description: Mocha likes arrays, so before her departure, 378QAQ gave her an array $a$ consisting of $n$ positive integers as a gift.
Mocha thinks that $a$ is beautiful if there exist two numbers $i$ and $j$ ($1\leq i,j\leq n$, $i\neq j$) such that for all $k$ ($1 \leq k \leq n$), $a_k$ is divisible$^\dagger$ by either $a_i$ or $a_j$.
Determine whether $a$ is beautiful.
$^\dagger$ $x$ is divisible by $y$ if there exists an integer $z$ such that $x = y \cdot z$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 500$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($3\leq n\leq 10^5$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1,a_2,\ldots,a_n$ ($1\leq a_i \leq 10^9$) β the elements of the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
Output: For each test case, output "Yes" if array $a$ is beautiful, and output "No" otherwise.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
Examples:
input:
4
3
7 3 8
5
7 1 9 3 5
5
4 12 2 6 3
5
7 49 9 3 1000000000
output:
No
Yes
Yes
No
Note: In the first test case, any two numbers in the array are coprime, so the answer is "No".
In the second test case, we can pick $i=2$ and $j=1$. Since every number in the array is divisible by $a_i = 1$, the answer is "Yes".
In the third test case, we can pick $i=3$ and $j=5$. $2$ and $4$ is divisible by $a_i = 2$ while $3$, $6$ and $12$ is divisible by $a_j = 3$, so the answer is "Yes".
|
2025A
|
https://codeforces.com/problemset/problem/2025/A
|
Two Screens
| 800
|
[
"binary search",
"greedy",
"strings",
"two pointers"
] |
Div. 2
| 2,000
| 512
|
There are two screens which can display sequences of uppercase Latin letters. Initially, both screens display nothing.
In one second, you can do one of the following two actions:
* choose a screen and an uppercase Latin letter, and append that letter to the end of the sequence displayed on that screen; * choose a screen and copy the sequence from it to the other screen, overwriting the sequence that was displayed on the other screen.
You have to calculate the minimum number of seconds you have to spend so that the first screen displays the sequence $s$, and the second screen displays the sequence $t$.
|
The first line contains one integer $q$ ($1 \le q \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains the string $s$, and the second line contains the string $t$ ($1 \le |s|, |t| \le 100$). Both strings consist of uppercase Latin letters.
|
For each test case, print one integer β the minimum possible number of seconds you have to spend so that the first screen displays the sequence $s$, and the second screen displays the sequence $t$.
|
[
[
"3\nGARAGE\nGARAGEFORSALE\nABCDE\nAABCD\nTRAINING\nDRAINING",
"14\n10\n16"
]
] |
In the first test case, the following sequence of actions is possible:
* spend $6$ seconds to write the sequence GARAGE on the first screen; * copy the sequence from the first screen to the second screen; * spend $7$ seconds to complete the sequence on the second screen by writing FORSALE.
In the second test case, the following sequence of actions is possible:
* spend $1$ second to write the sequence A on the second screen; * copy the sequence from the second screen to the first screen; * spend $4$ seconds to complete the sequence on the first screen by writing BCDE; * spend $4$ seconds to complete the sequence on the second screen by writing ABCD.
In the third test case, the fastest way to display the sequences is to type both of them character by character without copying, and this requires $16$ seconds.
|
Title: Two Screens
time_limit_ms: 2000
memory_limit_mb: 512
Description: There are two screens which can display sequences of uppercase Latin letters. Initially, both screens display nothing.
In one second, you can do one of the following two actions:
* choose a screen and an uppercase Latin letter, and append that letter to the end of the sequence displayed on that screen; * choose a screen and copy the sequence from it to the other screen, overwriting the sequence that was displayed on the other screen.
You have to calculate the minimum number of seconds you have to spend so that the first screen displays the sequence $s$, and the second screen displays the sequence $t$.
Input: The first line contains one integer $q$ ($1 \le q \le 500$) β the number of test cases.
Each test case consists of two lines. The first line contains the string $s$, and the second line contains the string $t$ ($1 \le |s|, |t| \le 100$). Both strings consist of uppercase Latin letters.
Output: For each test case, print one integer β the minimum possible number of seconds you have to spend so that the first screen displays the sequence $s$, and the second screen displays the sequence $t$.
Examples:
input:
3
GARAGE
GARAGEFORSALE
ABCDE
AABCD
TRAINING
DRAINING
output:
14
10
16
Note: In the first test case, the following sequence of actions is possible:
* spend $6$ seconds to write the sequence GARAGE on the first screen; * copy the sequence from the first screen to the second screen; * spend $7$ seconds to complete the sequence on the second screen by writing FORSALE.
In the second test case, the following sequence of actions is possible:
* spend $1$ second to write the sequence A on the second screen; * copy the sequence from the second screen to the first screen; * spend $4$ seconds to complete the sequence on the first screen by writing BCDE; * spend $4$ seconds to complete the sequence on the second screen by writing ABCD.
In the third test case, the fastest way to display the sequences is to type both of them character by character without copying, and this requires $16$ seconds.
|
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:
 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$.
|
Title: Photoshoot for Gorillas
time_limit_ms: 2000
memory_limit_mb: 256
Description: 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.
Input: 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$.
Output: For each test case, output a single integer β the maximum spectacle of a suitable arrangement.
Examples:
input:
5
3 4 2
9
1 1 1 1 1 1 1 1 1
2 1 1
2
5 7
20 15 7
9
4 1 4 5 6 1 1000000000 898 777
1984 1 1
4
5 4 1499 2004
9 5 5
6
6 7 14 16 16 6
output:
21
12
49000083104
3512
319
Note: In the first test case of the first input set, the spectacle of the following sub-squares is summed:
 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$.
|
1988E
|
https://codeforces.com/problemset/problem/1988/E
|
Range Minimum Sum
| 2,300
|
[
"binary search",
"brute force",
"data structures",
"divide and conquer",
"implementation"
] |
Div. 2
| 4,000
| 512
|
For an array $[a_1,a_2,\ldots,a_n]$ of length $n$, define $f(a)$ as the sum of the minimum element over all subsegments. That is, $$f(a)=\sum_{l=1}^n\sum_{r=l}^n \min_{l\le i\le r}a_i.$$
A permutation is a sequence of integers from $1$ to $n$ of length $n$ containing each number exactly once. You are given a permutation $[a_1,a_2,\ldots,a_n]$. For each $i$, solve the following problem independently:
* Erase $a_i$ from $a$, concatenating the remaining parts, resulting in $b = [a_1,a_2,\ldots,a_{i-1},\;a_{i+1},\ldots,a_{n}]$. * Calculate $f(b)$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows.
The first line of each test case contains an integer $n$ ($1\le n\le 5\cdot 10^5$).
The second line of each test case contains $n$ distinct integers $a_1,\ldots,a_n$ ($1\le a_i\le n$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
|
For each test case, print one line containing $n$ integers. The $i$-th integer should be the answer when erasing $a_i$.
|
[
[
"4\n1\n1\n3\n3 1 2\n5\n4 2 1 5 3\n8\n8 1 4 6 7 3 5 2",
"0 \n4 7 5 \n19 21 27 17 19 \n79 100 72 68 67 80 73 80"
]
] |
In the second test case, $a=[3,1,2]$.
* When removing $a_1$, $b=[1,2]$. $f(b)=1+2+\min\\{1,2\\}=4$. * When removing $a_2$, $b=[3,2]$. $f(b)=3+2+\min\\{3,2\\}=7$. * When removing $a_3$, $b=[3,1]$. $f(b)=3+1+\min\\{3,1\\}=5$.
|
Title: Range Minimum Sum
time_limit_ms: 4000
memory_limit_mb: 512
Description: For an array $[a_1,a_2,\ldots,a_n]$ of length $n$, define $f(a)$ as the sum of the minimum element over all subsegments. That is, $$f(a)=\sum_{l=1}^n\sum_{r=l}^n \min_{l\le i\le r}a_i.$$
A permutation is a sequence of integers from $1$ to $n$ of length $n$ containing each number exactly once. You are given a permutation $[a_1,a_2,\ldots,a_n]$. For each $i$, solve the following problem independently:
* Erase $a_i$ from $a$, concatenating the remaining parts, resulting in $b = [a_1,a_2,\ldots,a_{i-1},\;a_{i+1},\ldots,a_{n}]$. * Calculate $f(b)$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). Description of the test cases follows.
The first line of each test case contains an integer $n$ ($1\le n\le 5\cdot 10^5$).
The second line of each test case contains $n$ distinct integers $a_1,\ldots,a_n$ ($1\le a_i\le n$).
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
Output: For each test case, print one line containing $n$ integers. The $i$-th integer should be the answer when erasing $a_i$.
Examples:
input:
4
1
1
3
3 1 2
5
4 2 1 5 3
8
8 1 4 6 7 3 5 2
output:
0
4 7 5
19 21 27 17 19
79 100 72 68 67 80 73 80
Note: In the second test case, $a=[3,1,2]$.
* When removing $a_1$, $b=[1,2]$. $f(b)=1+2+\min\\{1,2\\}=4$. * When removing $a_2$, $b=[3,2]$. $f(b)=3+2+\min\\{3,2\\}=7$. * When removing $a_3$, $b=[3,1]$. $f(b)=3+1+\min\\{3,1\\}=5$.
|
1996A
|
https://codeforces.com/problemset/problem/1996/A
|
Legs
| 800
|
[
"binary search",
"math",
"ternary search"
] |
Div. 3
| 2,000
| 256
|
It's another beautiful day on Farmer John's farm.
After Farmer John arrived at his farm, he counted $n$ legs. It is known only chickens and cows live on the farm, and a chicken has $2$ legs while a cow has $4$.
What is the minimum number of animals Farmer John can have on his farm assuming he counted the legs of all animals?
|
The first line contains single integer $t$ ($1 \leq t \leq 10^3$) β the number of test cases.
Each test case contains an integer $n$ ($2 \leq n \leq 2 \cdot 10^3$, $n$ is even).
|
For each test case, output an integer, the minimum number of animals Farmer John can have on his farm.
|
[
[
"3\n2\n6\n8",
"1\n2\n2"
]
] |
Title: Legs
time_limit_ms: 2000
memory_limit_mb: 256
Description: It's another beautiful day on Farmer John's farm.
After Farmer John arrived at his farm, he counted $n$ legs. It is known only chickens and cows live on the farm, and a chicken has $2$ legs while a cow has $4$.
What is the minimum number of animals Farmer John can have on his farm assuming he counted the legs of all animals?
Input: The first line contains single integer $t$ ($1 \leq t \leq 10^3$) β the number of test cases.
Each test case contains an integer $n$ ($2 \leq n \leq 2 \cdot 10^3$, $n$ is even).
Output: For each test case, output an integer, the minimum number of animals Farmer John can have on his farm.
Examples:
input:
3
2
6
8
output:
1
2
2
Note:
|
|
2028E
|
https://codeforces.com/problemset/problem/2028/E
|
Alice's Adventures in the Rabbit Hole
| 2,300
|
[
"combinatorics",
"dfs and similar",
"dp",
"games",
"greedy",
"math",
"probabilities",
"trees"
] |
Div. 2
| 2,000
| 256
|
Alice is at the bottom of the rabbit hole! The rabbit hole can be modeled as a tree$^{\text{β}}$ which has an exit at vertex $1$, and Alice starts at some vertex $v$. She wants to get out of the hole, but unfortunately, the Queen of Hearts has ordered her execution.
Each minute, a fair coin is flipped. If it lands heads, Alice gets to move to an adjacent vertex of her current location, and otherwise, the Queen of Hearts gets to pull Alice to an adjacent vertex of the Queen's choosing. If Alice ever ends up on any of the non-root leaves$^{\text{β }}$ of the tree, Alice loses.
Assuming both of them move optimally, compute the probability that Alice manages to escape for every single starting vertex $1\le v\le n$. Since these probabilities can be very small, output them modulo $998\,244\,353$.
Formally, let $M = 998\,244\,353$. It can be shown that the exact answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.
$^{\text{β}}$A tree is a connected simple graph which has $n$ vertices and $n-1$ edges.
$^{\text{β }}$A leaf is a vertex that is connected to exactly one edge.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$) β the number of vertices in the tree.
The $i$-th of the next $n - 1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$ and $x_i \neq y_i$) β the edges of the tree. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
|
For each test case, output $n$ integers on one line β the probabilities of Alice escaping starting from vertex $1, 2, \ldots, n$. Since these probabilities can be very small, output them modulo $998\,244\,353$.
|
[
[
"2\n5\n1 2\n1 3\n2 4\n3 5\n9\n1 2\n2 3\n4 5\n5 6\n7 8\n8 9\n2 4\n5 7",
"1 499122177 499122177 0 0 \n1 499122177 0 332748118 166374059 0 443664157 720954255 0"
]
] |
For the first test case:
1. Alice escapes from the root (vertex $1$) by definition with probability $1$. 2. Alice immediately loses from vertices $4$ and $5$ since they are leaves. 3. From the other two vertices, Alice escapes with probability $\frac 12$ since the Queen will pull her to the leaves.
|
Title: Alice's Adventures in the Rabbit Hole
time_limit_ms: 2000
memory_limit_mb: 256
Description: Alice is at the bottom of the rabbit hole! The rabbit hole can be modeled as a tree$^{\text{β}}$ which has an exit at vertex $1$, and Alice starts at some vertex $v$. She wants to get out of the hole, but unfortunately, the Queen of Hearts has ordered her execution.
Each minute, a fair coin is flipped. If it lands heads, Alice gets to move to an adjacent vertex of her current location, and otherwise, the Queen of Hearts gets to pull Alice to an adjacent vertex of the Queen's choosing. If Alice ever ends up on any of the non-root leaves$^{\text{β }}$ of the tree, Alice loses.
Assuming both of them move optimally, compute the probability that Alice manages to escape for every single starting vertex $1\le v\le n$. Since these probabilities can be very small, output them modulo $998\,244\,353$.
Formally, let $M = 998\,244\,353$. It can be shown that the exact answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.
$^{\text{β}}$A tree is a connected simple graph which has $n$ vertices and $n-1$ edges.
$^{\text{β }}$A leaf is a vertex that is connected to exactly one edge.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$) β the number of vertices in the tree.
The $i$-th of the next $n - 1$ lines contains two integers $x_i$ and $y_i$ ($1 \le x_i, y_i \le n$ and $x_i \neq y_i$) β the edges of the tree. It is guaranteed that the given edges form a tree.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2\cdot 10^5$.
Output: For each test case, output $n$ integers on one line β the probabilities of Alice escaping starting from vertex $1, 2, \ldots, n$. Since these probabilities can be very small, output them modulo $998\,244\,353$.
Examples:
input:
2
5
1 2
1 3
2 4
3 5
9
1 2
2 3
4 5
5 6
7 8
8 9
2 4
5 7
output:
1 499122177 499122177 0 0
1 499122177 0 332748118 166374059 0 443664157 720954255 0
Note: For the first test case:
1. Alice escapes from the root (vertex $1$) by definition with probability $1$. 2. Alice immediately loses from vertices $4$ and $5$ since they are leaves. 3. From the other two vertices, Alice escapes with probability $\frac 12$ since the Queen will pull her to the leaves.
|
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 .$$
|
Title: Triple Operations
time_limit_ms: 1000
memory_limit_mb: 256
Description: 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.
Input: 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$).
Output: For each test case, output a single integer β the minimum number of operations needed to make all numbers on the board equal $0$.
Examples:
input:
4
1 3
2 4
199999 200000
19 84
output:
5
6
36
263
Note: 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 .$$
|
1983B
|
https://codeforces.com/problemset/problem/1983/B
|
Corner Twist
| 1,200
|
[
"constructive algorithms",
"greedy",
"implementation",
"math"
] |
Div. 2
| 1,000
| 256
|
You are given two grids of numbers $a$ and $b$, with $n$ rows and $m$ columns. All the values in the grid are $0$, $1$ or $2$.
You can perform the following operation on $a$ any number of times:
* Pick any subrectangle in the grid with length and width $\ge 2$. You are allowed to choose the entire grid as a subrectangle. * The subrectangle has four corners. Take any pair of diagonally opposite corners of the chosen subrectangle and add $1$ to their values modulo $3$. * For the pair of corners not picked, add $2$ to their values modulo $3$.
Note that the operation only changes the values of the corners of the picked subrectangle.
Is it possible to convert the grid $a$ into grid $b$ by applying the above operation any number of times (possibly zero)?
|
The first line contains an integer $t$, the number of testcases ($1 \le t \le 250$).
For each testcase:
The first line contains two integers $n$ and $m$, the number of rows and columns in the grid ($2 \le n,m \le 500$).
Each of the next n lines contain m characters β the $j$-th character of the $i$-th line represents $a_{i,j}$.
Each of the next n lines contain m characters β the $j$-th character of the $i$-th line represents $b_{i,j}$ ($0 \le a_{i,j}, b_{i,j} \le 2$).
It is guaranteed that the sum of $n$ over all test cases and the sum of $m$ over all test cases do not exceed $500$.
|
For each test case print "YES" (without quotes) if it is possible to convert grid $a$ into grid $b$ and "NO" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
|
[
[
"7\n3 3\n000\n000\n000\n111\n111\n111\n4 4\n0000\n0000\n0000\n0000\n2100\n1200\n0012\n0021\n4 4\n1020\n1200\n1210\n0000\n0000\n1200\n2200\n0000\n3 3\n012\n012\n012\n010\n111\n011\n8 8\n00000000\n00000000\n00000000\n00000000\n00000000\n00000000\n00000000\n10000000\n00000000\n01200000\n02010000\n00102000\n00020100\n00001020\n00000210\n10000000\n2 7\n0000000\n0000000\n2220111\n0111222\n2 7\n0000000\n0100010\n2220111\n1210202",
"YES\nYES\nYES\nNO\nYES\nNO\nYES"
]
] |
In the first testcase, grid $a$ can be converted into $b$ in the following manner:
$\begin{matrix}\fbox{0} & 0 & \fbox{0}\\\ 0 & 0 & 0\\\ \fbox{0} & 0 & \fbox{0}\end{matrix} \Rightarrow \begin{matrix}1 & 0 & 2\\\ 0 & \fbox{0} & \fbox{0}\\\ 2 & \fbox{0} & \fbox{1}\end{matrix} \Rightarrow \begin{matrix}1 & 0 & 2\\\ \fbox{0} & \fbox{1} & 2\\\ \fbox{2} & \fbox{2} & 2\end{matrix} \Rightarrow \begin{matrix}1 & \fbox{0} & \fbox{2}\\\ 1 & 0 & 2\\\ 1 & \fbox{0} & \fbox{2}\end{matrix} \Rightarrow \begin{matrix}1 & 1 & 1\\\ 1 & \fbox{0} & \fbox{2}\\\ 1 & \fbox{2} & \fbox{0}\end{matrix} \Rightarrow \begin{matrix}1 & 1 & 1\\\ 1 & 1 & 1\\\ 1 & 1 & 1\end{matrix}$
Here, in each operation, the top-right and bottom-left corners highlighted by a box are incremented by $2$ modulo $3$, while the top-left and bottom-right corners are incremented by $1$ modulo $3$.
In the fourth testcase, it can be proven that it is not possible to convert grid $a$ into grid $b$ using the above-mentioned operations any number of times.
|
Title: Corner Twist
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given two grids of numbers $a$ and $b$, with $n$ rows and $m$ columns. All the values in the grid are $0$, $1$ or $2$.
You can perform the following operation on $a$ any number of times:
* Pick any subrectangle in the grid with length and width $\ge 2$. You are allowed to choose the entire grid as a subrectangle. * The subrectangle has four corners. Take any pair of diagonally opposite corners of the chosen subrectangle and add $1$ to their values modulo $3$. * For the pair of corners not picked, add $2$ to their values modulo $3$.
Note that the operation only changes the values of the corners of the picked subrectangle.
Is it possible to convert the grid $a$ into grid $b$ by applying the above operation any number of times (possibly zero)?
Input: The first line contains an integer $t$, the number of testcases ($1 \le t \le 250$).
For each testcase:
The first line contains two integers $n$ and $m$, the number of rows and columns in the grid ($2 \le n,m \le 500$).
Each of the next n lines contain m characters β the $j$-th character of the $i$-th line represents $a_{i,j}$.
Each of the next n lines contain m characters β the $j$-th character of the $i$-th line represents $b_{i,j}$ ($0 \le a_{i,j}, b_{i,j} \le 2$).
It is guaranteed that the sum of $n$ over all test cases and the sum of $m$ over all test cases do not exceed $500$.
Output: For each test case print "YES" (without quotes) if it is possible to convert grid $a$ into grid $b$ and "NO" (without quotes) otherwise.
You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
Examples:
input:
7
3 3
000
000
000
111
111
111
4 4
0000
0000
0000
0000
2100
1200
0012
0021
4 4
1020
1200
1210
0000
0000
1200
2200
0000
3 3
012
012
012
010
111
011
8 8
00000000
00000000
00000000
00000000
00000000
00000000
00000000
10000000
00000000
01200000
02010000
00102000
00020100
00001020
00000210
10000000
2 7
0000000
0000000
2220111
0111222
2 7
0000000
0100010
2220111
1210202
output:
YES
YES
YES
NO
YES
NO
YES
Note: In the first testcase, grid $a$ can be converted into $b$ in the following manner:
$\begin{matrix}\fbox{0} & 0 & \fbox{0}\\\ 0 & 0 & 0\\\ \fbox{0} & 0 & \fbox{0}\end{matrix} \Rightarrow \begin{matrix}1 & 0 & 2\\\ 0 & \fbox{0} & \fbox{0}\\\ 2 & \fbox{0} & \fbox{1}\end{matrix} \Rightarrow \begin{matrix}1 & 0 & 2\\\ \fbox{0} & \fbox{1} & 2\\\ \fbox{2} & \fbox{2} & 2\end{matrix} \Rightarrow \begin{matrix}1 & \fbox{0} & \fbox{2}\\\ 1 & 0 & 2\\\ 1 & \fbox{0} & \fbox{2}\end{matrix} \Rightarrow \begin{matrix}1 & 1 & 1\\\ 1 & \fbox{0} & \fbox{2}\\\ 1 & \fbox{2} & \fbox{0}\end{matrix} \Rightarrow \begin{matrix}1 & 1 & 1\\\ 1 & 1 & 1\\\ 1 & 1 & 1\end{matrix}$
Here, in each operation, the top-right and bottom-left corners highlighted by a box are incremented by $2$ modulo $3$, while the top-left and bottom-right corners are incremented by $1$ modulo $3$.
In the fourth testcase, it can be proven that it is not possible to convert grid $a$ into grid $b$ using the above-mentioned operations any number of times.
|
1995B2
|
https://codeforces.com/problemset/problem/1995/B2
|
Bouquet (Hard Version)
| 1,700
|
[
"binary search",
"data structures",
"greedy",
"math",
"sortings",
"two pointers"
] |
Div. 2
| 1,500
| 256
|
This is the hard version of the problem. The only difference is that in this version, instead of listing the number of petals for each flower, the number of petals and the quantity of flowers in the store is set for all types of flowers.
A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of $n$ different types of flowers in the store, each of which is characterized by the number of petals and the quantity of this type of flower. A flower with $k$ petals costs $k$ coins. The girl has decided that the difference in the number of petals between any two flowers she will use to decorate her cake should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has $m$ coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet?
|
Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) β the number of test cases. This is followed by descriptions of the test cases.
The first line of each test case contains two integers $n$, $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le 10^{18}$) β the number of types of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains $n$ different integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the number of petals of the $i$-th flower type in the store (for different indexes $i \neq j$, it must be $a_i \neq a_j$). The third line of each test case contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^9$), where $c_i$ is the quantity of the $i$-th flower type in the store.
The sum of $n$ over all test cases does not exceed $2 \cdot {10}^5$.
|
For each test case, print one integer β the maximum possible number of petals in a bouquet that a girl can collect, observing all the conditions listed above.
|
[
[
"7\n3 10\n1 2 3\n2 2 1\n3 1033\n206 207 1000\n3 4 1\n6 20\n4 2 7 5 6 1\n1 2 1 3 1 7\n8 100000\n239 30 610 122 24 40 8 2\n12 13123 112 1456 124 100 123 10982\n6 13\n2 4 11 1 3 5\n2 2 1 2 2 1\n8 10330\n206 210 200 201 198 199 222 1000\n9 10 11 12 13 14 15 16\n2 10000000000\n11 12\n87312315 753297050",
"7\n1033\n19\n99990\n13\n10000\n9999999999"
]
] |
In the first test case, some valid bouquets are $(1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2)$. The maximum over all valid bouquets not greater than $10$ is $7$ for $(2, 2, 3)$. In the second test case, you can assemble a valid bouquet with $(206, 206, 207, 207, 207)$ with a sum of $1033$, which is the maximum number of petals the girl can buy. In the third test case, you can assemble a valid bouquet with $(5, 5, 5, 4)$ with a sum of $19$. It can be seen that no valid bouquet can have $20$ petals.
|
Title: Bouquet (Hard Version)
time_limit_ms: 1500
memory_limit_mb: 256
Description: This is the hard version of the problem. The only difference is that in this version, instead of listing the number of petals for each flower, the number of petals and the quantity of flowers in the store is set for all types of flowers.
A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of $n$ different types of flowers in the store, each of which is characterized by the number of petals and the quantity of this type of flower. A flower with $k$ petals costs $k$ coins. The girl has decided that the difference in the number of petals between any two flowers she will use to decorate her cake should not exceed one. At the same time, the girl wants to assemble a bouquet with the maximum possible number of petals. Unfortunately, she only has $m$ coins, and she cannot spend more. What is the maximum total number of petals she can assemble in the bouquet?
Input: Each test consists of several test cases. The first line contains a single integer $t$ ($1 \le t \le 10\,000$) β the number of test cases. This is followed by descriptions of the test cases.
The first line of each test case contains two integers $n$, $m$ ($1 \le n \le 2 \cdot 10^5, 1 \le m \le 10^{18}$) β the number of types of flowers in the store and the number of coins the girl possesses, respectively. The second line of each test case contains $n$ different integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$), where $a_i$ is the number of petals of the $i$-th flower type in the store (for different indexes $i \neq j$, it must be $a_i \neq a_j$). The third line of each test case contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 10^9$), where $c_i$ is the quantity of the $i$-th flower type in the store.
The sum of $n$ over all test cases does not exceed $2 \cdot {10}^5$.
Output: For each test case, print one integer β the maximum possible number of petals in a bouquet that a girl can collect, observing all the conditions listed above.
Examples:
input:
7
3 10
1 2 3
2 2 1
3 1033
206 207 1000
3 4 1
6 20
4 2 7 5 6 1
1 2 1 3 1 7
8 100000
239 30 610 122 24 40 8 2
12 13123 112 1456 124 100 123 10982
6 13
2 4 11 1 3 5
2 2 1 2 2 1
8 10330
206 210 200 201 198 199 222 1000
9 10 11 12 13 14 15 16
2 10000000000
11 12
87312315 753297050
output:
7
1033
19
99990
13
10000
9999999999
Note: In the first test case, some valid bouquets are $(1, 1, 2, 2), (2, 2, 3), (1, 1), (2, 2)$. The maximum over all valid bouquets not greater than $10$ is $7$ for $(2, 2, 3)$. In the second test case, you can assemble a valid bouquet with $(206, 206, 207, 207, 207)$ with a sum of $1033$, which is the maximum number of petals the girl can buy. In the third test case, you can assemble a valid bouquet with $(5, 5, 5, 4)$ with a sum of $19$. It can be seen that no valid bouquet can have $20$ petals.
|
1985B
|
https://codeforces.com/problemset/problem/1985/B
|
Maximum Multiple Sum
| 800
|
[
"brute force",
"math",
"number theory"
] |
Div. 4
| 1,000
| 256
|
Given an integer $n$, find an integer $x$ such that:
* $2 \leq x \leq n$. * The sum of multiples of $x$ that are less than or equal to $n$ is maximized. Formally, $x + 2x + 3x + \dots + kx$ where $kx \leq n$ is maximized over all possible values of $x$.
|
The first line contains $t$ ($1 \leq t \leq 100$) β the number of test cases.
Each test case contains a single integer $n$ ($2 \leq n \leq 100$).
|
For each test case, output an integer, the optimal value of $x$. It can be shown there is only one unique answer.
|
[
[
"2\n3\n15",
"3\n2"
]
] |
For $n = 3$, the possible values of $x$ are $2$ and $3$. The sum of all multiples of $2$ less than or equal to $n$ is just $2$, and the sum of all multiples of $3$ less than or equal to $n$ is $3$. Therefore, $3$ is the optimal value of $x$.
For $n = 15$, the optimal value of $x$ is $2$. The sum of all multiples of $2$ less than or equal to $n$ is $2 + 4 + 6 + 8 + 10 + 12 + 14 = 56$, which can be proven to be the maximal over all other possible values of $x$.
|
Title: Maximum Multiple Sum
time_limit_ms: 1000
memory_limit_mb: 256
Description: Given an integer $n$, find an integer $x$ such that:
* $2 \leq x \leq n$. * The sum of multiples of $x$ that are less than or equal to $n$ is maximized. Formally, $x + 2x + 3x + \dots + kx$ where $kx \leq n$ is maximized over all possible values of $x$.
Input: The first line contains $t$ ($1 \leq t \leq 100$) β the number of test cases.
Each test case contains a single integer $n$ ($2 \leq n \leq 100$).
Output: For each test case, output an integer, the optimal value of $x$. It can be shown there is only one unique answer.
Examples:
input:
2
3
15
output:
3
2
Note: For $n = 3$, the possible values of $x$ are $2$ and $3$. The sum of all multiples of $2$ less than or equal to $n$ is just $2$, and the sum of all multiples of $3$ less than or equal to $n$ is $3$. Therefore, $3$ is the optimal value of $x$.
For $n = 15$, the optimal value of $x$ is $2$. The sum of all multiples of $2$ less than or equal to $n$ is $2 + 4 + 6 + 8 + 10 + 12 + 14 = 56$, which can be proven to be the maximal over all other possible values of $x$.
|
1985G
|
https://codeforces.com/problemset/problem/1985/G
|
D-Function
| 1,600
|
[
"combinatorics",
"math",
"number theory"
] |
Div. 4
| 2,000
| 256
|
Let $D(n)$ represent the sum of digits of $n$. For how many integers $n$ where $10^{l} \leq n < 10^{r}$ satisfy $D(k \cdot n) = k \cdot D(n)$? Output the answer modulo $10^9+7$.
|
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
Each test case contains three integers $l$, $r$, and $k$ ($0 \leq l < r \leq 10^9$, $1 \leq k \leq 10^9$).
|
For each test case, output an integer, the answer, modulo $10^9 + 7$.
|
[
[
"6\n0 1 4\n0 2 7\n1 2 1\n1 2 3\n582 74663 3\n0 3 1",
"2\n3\n90\n12\n974995667\n999"
]
] |
For the first test case, the only values of $n$ that satisfy the condition are $1$ and $2$.
For the second test case, the only values of $n$ that satisfy the condition are $1$, $10$, and $11$.
For the third test case, all values of $n$ between $10$ inclusive and $100$ exclusive satisfy the condition.
|
Title: D-Function
time_limit_ms: 2000
memory_limit_mb: 256
Description: Let $D(n)$ represent the sum of digits of $n$. For how many integers $n$ where $10^{l} \leq n < 10^{r}$ satisfy $D(k \cdot n) = k \cdot D(n)$? Output the answer modulo $10^9+7$.
Input: The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
Each test case contains three integers $l$, $r$, and $k$ ($0 \leq l < r \leq 10^9$, $1 \leq k \leq 10^9$).
Output: For each test case, output an integer, the answer, modulo $10^9 + 7$.
Examples:
input:
6
0 1 4
0 2 7
1 2 1
1 2 3
582 74663 3
0 3 1
output:
2
3
90
12
974995667
999
Note: For the first test case, the only values of $n$ that satisfy the condition are $1$ and $2$.
For the second test case, the only values of $n$ that satisfy the condition are $1$, $10$, and $11$.
For the third test case, all values of $n$ between $10$ inclusive and $100$ exclusive satisfy the condition.
|
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$.
|
Title: Minimize the Difference
time_limit_ms: 2000
memory_limit_mb: 256
Description: 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)$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows.
The first line of each test case contains 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$.
Output: 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)$.
Examples:
input:
5
1
1
3
1 2 3
4
4 1 2 3
4
4 2 3 1
5
5 14 4 10 2
output:
0
2
1
1
3
Note: 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$.
|
2021D
|
https://codeforces.com/problemset/problem/2021/D
|
Boss, Thirsty
| 2,500
|
[
"dp",
"greedy",
"implementation"
] |
Div. 2
| 2,000
| 256
|
Pak Chanek has a friend who runs a drink stall in a canteen. His friend will sell drinks for $n$ days, numbered from day $1$ to day $n$. There are also $m$ types of drinks, numbered from $1$ to $m$.
The profit gained from selling a drink on a particular day can vary. On day $i$, the projected profit from selling drink of type $j$ is $A_{i, j}$. Note that $A_{i, j}$ can be negative, meaning that selling the drink would actually incur a loss.
Pak Chanek wants to help his friend plan the sales over the $n$ days. On day $i$, Pak Chanek must choose to sell at least one type of drink. Furthermore, the types of drinks sold on a single day must form a subarray. In other words, in each day, Pak Chanek will select $i$ and $j$ such that $1 \leq i \leq j \leq m$. Then all types of drinks between $i$ and $j$ (inclusive) will be sold.
However, to ensure that customers from the previous day keep returning, the selection of drink types sold on day $i$ ($i>1$) must meet the following conditions:
* At least one drink type sold on day $i$ must also have been sold on day $i-1$. * At least one drink type sold on day $i$ must not have been sold on day $i-1$.
The daily profit is the sum of the profits from all drink types sold on that day. The total profit from the sales plan is the sum of the profits over $n$ days. What is the maximum total profit that can be achieved if Pak Chanek plans the sales optimally?
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1 \leq n \leq 2 \cdot 10^5$; $3 \leq m \leq 2 \cdot 10^5$; $n \cdot m \leq 2 \cdot 10^5$) β the number of rows and columns in a grid.
The next $n$ lines of each test case contain $m$ integers each, where the $i$-th line contains the integers $A_{i,1} A_{i,2}, \ldots, A_{i,m}$ ($-10^9 \leq A_{i,j} \leq 10^9$) β project profits of each drink type on the $i$-th day.
It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$.
|
For each test case, output a single integer: the maximum profit that Pak Chanek can achieve.
|
[
[
"1\n3 6\n79 20 49 5 -1000 500\n-105 9 109 24 -98 -499\n14 47 12 39 23 50",
"475"
]
] |
Here is Pak Chanek's optimal plan:
| |  ---|---|--- * On day $1$, Pak Chanek sells drink types $1$ to $3$. Generating a profit of $79+20+49 = 148$. * On day $2$, Pak Chanek sells drink types $2$ to $4$. Generating a profit of $9+109+24 = 142$ * On day $3$, Pak Chanek sells drink types $1$ to $6$. Generating a profit of $185$.
So, the total profit of Pak Chanek's plan is $148 + 142 + 185 = 475$.
|
Title: Boss, Thirsty
time_limit_ms: 2000
memory_limit_mb: 256
Description: Pak Chanek has a friend who runs a drink stall in a canteen. His friend will sell drinks for $n$ days, numbered from day $1$ to day $n$. There are also $m$ types of drinks, numbered from $1$ to $m$.
The profit gained from selling a drink on a particular day can vary. On day $i$, the projected profit from selling drink of type $j$ is $A_{i, j}$. Note that $A_{i, j}$ can be negative, meaning that selling the drink would actually incur a loss.
Pak Chanek wants to help his friend plan the sales over the $n$ days. On day $i$, Pak Chanek must choose to sell at least one type of drink. Furthermore, the types of drinks sold on a single day must form a subarray. In other words, in each day, Pak Chanek will select $i$ and $j$ such that $1 \leq i \leq j \leq m$. Then all types of drinks between $i$ and $j$ (inclusive) will be sold.
However, to ensure that customers from the previous day keep returning, the selection of drink types sold on day $i$ ($i>1$) must meet the following conditions:
* At least one drink type sold on day $i$ must also have been sold on day $i-1$. * At least one drink type sold on day $i$ must not have been sold on day $i-1$.
The daily profit is the sum of the profits from all drink types sold on that day. The total profit from the sales plan is the sum of the profits over $n$ days. What is the maximum total profit that can be achieved if Pak Chanek plans the sales optimally?
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows.
The first line of each test case contains two integers $n$ and $m$ ($1 \leq n \leq 2 \cdot 10^5$; $3 \leq m \leq 2 \cdot 10^5$; $n \cdot m \leq 2 \cdot 10^5$) β the number of rows and columns in a grid.
The next $n$ lines of each test case contain $m$ integers each, where the $i$-th line contains the integers $A_{i,1} A_{i,2}, \ldots, A_{i,m}$ ($-10^9 \leq A_{i,j} \leq 10^9$) β project profits of each drink type on the $i$-th day.
It is guaranteed that the sum of $n \cdot m$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output a single integer: the maximum profit that Pak Chanek can achieve.
Examples:
input:
1
3 6
79 20 49 5 -1000 500
-105 9 109 24 -98 -499
14 47 12 39 23 50
output:
475
Note: Here is Pak Chanek's optimal plan:
| |  ---|---|--- * On day $1$, Pak Chanek sells drink types $1$ to $3$. Generating a profit of $79+20+49 = 148$. * On day $2$, Pak Chanek sells drink types $2$ to $4$. Generating a profit of $9+109+24 = 142$ * On day $3$, Pak Chanek sells drink types $1$ to $6$. Generating a profit of $185$.
So, the total profit of Pak Chanek's plan is $148 + 142 + 185 = 475$.
|
1975I
|
https://codeforces.com/problemset/problem/1975/I
|
Mind Bloom
| 3,500
|
[
"dp"
] |
Div. 1 + 2
| 5,000
| 1,024
|
This is the way it always was.
This is the way it always will be.
All will be forgotten again soon...
Jellyfish is playing a one-player card game called "Slay the Spire". There are $n$ cards in total numbered from $1$ to $n$. The $i$-th card has power $c_i$.
There is a binary string $s$ of length $n$. If $s_i = \texttt{0}$, the $i$-th card is initially in the draw pile. If $s_i = \texttt{1}$, the $i$-th card is initially in Jellyfish's hand.
Jellyfish will repeat the following process until either her hand or the draw pile is empty.
1. Let $x$ be the power of the card with the largest power in her hand. 2. Place a single card with power $x$ back into the draw pile. 3. Randomly draw $x$ cards from the draw pile. All subsets of $x$ cards from the draw pile have an equal chance of being drawn. If there are fewer than $x$ cards in the draw pile, Jellyfish will draw all cards.
At the end of this process, find the probability that Jellyfish can empty the draw pile, modulo $1\,000\,000\,007$.
Formally, let $M=1\,000\,000\,007$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 100$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 120$) β the number of cards.
The second line of each test case contains $n$ integers $c_1,c_2,\ldots,c_n$ ($0 \leq c_i \leq n$) β the powers of the cards. It is guaranteed that $c_1 \leq c_2 \leq \ldots \leq c_n$.
The third line of each test case contains a binary string $s$ of length $n$. If $s_i = \texttt{0}$, the $i$-th card is initially in the draw pile. If $s_i = \texttt{1}$, the $i$-th card is initially in Jellyfish's hand.
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $120^2$.
|
For each test case, output the probability that Jellyfish can empty the draw pile modulo $1\,000\,000\,007$.
|
[
[
"4\n5\n0 1 1 1 2\n00100\n3\n2 3 3\n000\n10\n0 0 0 0 0 0 0 1 1 1\n1111011111\n20\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 2 3 3 4\n00000000001000101010",
"500000004\n0\n0\n675898154"
]
] |
In the first test case, Jellyfish will keep playing cards with power $1$ until Jellyfish draws a card with power $0$ or power $2$. If Jellyfish draws a card with power $0$, she will eventually empty her hand. If Jellyfish draws a card with power $2$, she will eventually empty the draw pile. Since there is an equal chance of drawing $0$ or $2$, the answer is $\frac{1}{2}$, and $2 \cdot 500\,000\,004 \equiv 1 \pmod {10^9+7}$
|
Title: Mind Bloom
time_limit_ms: 5000
memory_limit_mb: 1024
Description: This is the way it always was.
This is the way it always will be.
All will be forgotten again soon...
Jellyfish is playing a one-player card game called "Slay the Spire". There are $n$ cards in total numbered from $1$ to $n$. The $i$-th card has power $c_i$.
There is a binary string $s$ of length $n$. If $s_i = \texttt{0}$, the $i$-th card is initially in the draw pile. If $s_i = \texttt{1}$, the $i$-th card is initially in Jellyfish's hand.
Jellyfish will repeat the following process until either her hand or the draw pile is empty.
1. Let $x$ be the power of the card with the largest power in her hand. 2. Place a single card with power $x$ back into the draw pile. 3. Randomly draw $x$ cards from the draw pile. All subsets of $x$ cards from the draw pile have an equal chance of being drawn. If there are fewer than $x$ cards in the draw pile, Jellyfish will draw all cards.
At the end of this process, find the probability that Jellyfish can empty the draw pile, modulo $1\,000\,000\,007$.
Formally, let $M=1\,000\,000\,007$. It can be shown that the answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the integer equal to $p \cdot q^{-1} \bmod M$. In other words, output such an integer $x$ that $0 \le x < M$ and $x \cdot q \equiv p \pmod{M}$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 100$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 120$) β the number of cards.
The second line of each test case contains $n$ integers $c_1,c_2,\ldots,c_n$ ($0 \leq c_i \leq n$) β the powers of the cards. It is guaranteed that $c_1 \leq c_2 \leq \ldots \leq c_n$.
The third line of each test case contains a binary string $s$ of length $n$. If $s_i = \texttt{0}$, the $i$-th card is initially in the draw pile. If $s_i = \texttt{1}$, the $i$-th card is initially in Jellyfish's hand.
It is guaranteed that the sum of $n^2$ over all test cases does not exceed $120^2$.
Output: For each test case, output the probability that Jellyfish can empty the draw pile modulo $1\,000\,000\,007$.
Examples:
input:
4
5
0 1 1 1 2
00100
3
2 3 3
000
10
0 0 0 0 0 0 0 1 1 1
1111011111
20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 2 3 3 4
00000000001000101010
output:
500000004
0
0
675898154
Note: In the first test case, Jellyfish will keep playing cards with power $1$ until Jellyfish draws a card with power $0$ or power $2$. If Jellyfish draws a card with power $0$, she will eventually empty her hand. If Jellyfish draws a card with power $2$, she will eventually empty the draw pile. Since there is an equal chance of drawing $0$ or $2$, the answer is $\frac{1}{2}$, and $2 \cdot 500\,000\,004 \equiv 1 \pmod {10^9+7}$
|
2021E1
|
https://codeforces.com/problemset/problem/2021/E1
|
Digital Village (Easy Version)
| 2,300
|
[
"brute force",
"data structures",
"dfs and similar",
"dp",
"dsu",
"fft",
"graphs",
"greedy",
"implementation",
"math",
"trees"
] |
Div. 2
| 2,000
| 256
|
This is the easy version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved.
Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$.
There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to.
For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, $p$ ($2 \le n \le 400$; $n-1 \le m \le 400$; $1 \le p \le n$) β the number of houses, the number of cables and the number of houses that need internet.
The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) β the houses that need internet. It is guaranteed that all elements of $s$ are distinct.
The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) β the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph.
It is guaranteed that the sum of $n^3$ and the sum of $m^3$ do not exceed $10^8$.
|
For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.
|
[
[
"2\n9 8 5\n2 5 6 8 9\n1 2 1\n1 3 2\n3 4 10\n4 5 3\n4 6 5\n1 7 10\n7 8 4\n7 9 2\n3 3 2\n3 1\n1 2 1\n2 3 3\n1 3 2",
"34 19 9 4 0 0 0 0 0\n2 0 0"
]
] |
In the first test case for $k=3$, a possible optimal solution is to install servers at vertices $2$, $6$ and $8$ and obtain the following latency:
* $\text{latency}(2) = 0$ * $\text{latency}(5) = \max(3, 5) = 5$ * $\text{latency}(6) = 0$ * $\text{latency}(8) = 0$ * $\text{latency}(9) = \max(2, 4) = 4$
So the total latency is $9$.
|
Title: Digital Village (Easy Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is the easy version of the problem. In the three versions, the constraints on $n$ and $m$ are different. You can make hacks only if all the versions of the problem are solved.
Pak Chanek is setting up internet connections for the village of Khuntien. The village can be represented as a connected simple graph with $n$ houses and $m$ internet cables connecting house $u_i$ and house $v_i$, each with a latency of $w_i$.
There are $p$ houses that require internet. Pak Chanek can install servers in at most $k$ of the houses. The houses that need internet will then be connected to one of the servers. However, since each cable has its latency, the latency experienced by house $s_i$ requiring internet will be the maximum latency of the cables between that house and the server it is connected to.
For each $k = 1,2,\ldots,n$, help Pak Chanek determine the minimum total latency that can be achieved for all the houses requiring internet.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.
The first line of each test case contains three integers $n$, $m$, $p$ ($2 \le n \le 400$; $n-1 \le m \le 400$; $1 \le p \le n$) β the number of houses, the number of cables and the number of houses that need internet.
The second line of each test case contains $p$ integers $s_1, s_2, \ldots, s_p$ ($1 \le s_i \le n$) β the houses that need internet. It is guaranteed that all elements of $s$ are distinct.
The $i$-th of the next $m$ lines of each test case contains three integers $u_i$, $v_i$, and $w_i$ ($1 \le u_i < v_i \le n$; $1 \le w_i \le 10^9$) β the internet cable connecting house $u_i$ and house $v_i$ with latency of $w_i$. It is guaranteed that the given edges form a connected simple graph.
It is guaranteed that the sum of $n^3$ and the sum of $m^3$ do not exceed $10^8$.
Output: For each test case, output $n$ integers: the minimum total latency that can be achieved for all the houses requiring internet for each $k = 1,2,\ldots,n$.
Examples:
input:
2
9 8 5
2 5 6 8 9
1 2 1
1 3 2
3 4 10
4 5 3
4 6 5
1 7 10
7 8 4
7 9 2
3 3 2
3 1
1 2 1
2 3 3
1 3 2
output:
34 19 9 4 0 0 0 0 0
2 0 0
Note: In the first test case for $k=3$, a possible optimal solution is to install servers at vertices $2$, $6$ and $8$ and obtain the following latency:
* $\text{latency}(2) = 0$ * $\text{latency}(5) = \max(3, 5) = 5$ * $\text{latency}(6) = 0$ * $\text{latency}(8) = 0$ * $\text{latency}(9) = \max(2, 4) = 4$
So the total latency is $9$.
|
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]$.
|
Title: Turtle and Inversions (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 512
Description: 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$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). The description of the test cases follows.
The first line of each test case contains two integers $n, 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$.
Output: 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.
Examples:
input:
8
2 0
2 1
1 2
5 1
2 4
8 3
1 4
2 5
7 8
7 2
1 4
4 7
7 3
1 2
1 7
3 7
7 4
1 3
4 7
1 3
4 7
7 3
1 2
3 4
5 6
output:
1
0
8
18
-1
-1
15
15
Note: 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]$.
|
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"
]
] |
Title: Make a Palindrome
time_limit_ms: 5000
memory_limit_mb: 512
Description: 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$.
Input: 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$.
Output: For each test case, print a single integer β the sum of the values of the function $f$ for all subarrays of the array $a$.
Examples:
input:
4
3
2 1 3
4
1 1 1 1
5
4 2 3 1 5
4
1 2 1 2
output:
3
0
14
5
Note:
|
|
2025D
|
https://codeforces.com/problemset/problem/2025/D
|
Attribute Checks
| 1,800
|
[
"brute force",
"data structures",
"dp",
"implementation",
"math",
"two pointers"
] |
Div. 2
| 2,500
| 512
|
Imagine a game where you play as a character that has two attributes: "Strength" and "Intelligence", that are at zero level initially.
During the game, you'll acquire $m$ attribute points that allow you to increase your attribute levels β one point will increase one of the attributes by one level. But sometimes, you'll encounter a so-called "Attribute Checks": if your corresponding attribute is high enough, you'll pass it; otherwise, you'll fail it.
Spending some time, you finally prepared a list which contains records of all points you got and all checks you've met. And now you're wondering: what is the maximum number of attribute checks you can pass in a single run if you'd spend points wisely?
Note that you can't change the order of records.
|
The first line contains two integers $n$ and $m$ ($1 \le m \le 5000$; $m < n \le 2 \cdot 10^6$) β the number of records in the list and the total number of points you'll get during the game.
The second line contains $n$ integers $r_1, r_2, \dots, r_n$ ($-m \le r_i \le m$), where $r_i$ encodes the $i$-th record:
* If $r_i = 0$, then the $i$-th record is an acquiring one attribute point. You can spend to level up either Strength or Intelligence; * If $r_i > 0$, then it's an Intelligence check: if your Intelligence level is greater than or equal to $|r_i|$, you pass. * If $r_i < 0$, then it's a Strength check: if your Strength level is greater than or equal to $|r_i|$, you pass.
Additional constraint on the input: the sequence $r_1, r_2, \dots, r_n$ contains exactly $m$ elements equal to $0$.
|
Print one integer β the maximum number of checks you can pass.
|
[
[
"10 5\n0 1 0 2 0 -3 0 -4 0 -5",
"3"
],
[
"3 1\n1 -1 0",
"0"
],
[
"9 3\n0 0 1 0 2 -3 -2 -2 1",
"4"
]
] |
In the first test, it's optimal to spend each point in Strength, so you'll fail $2$ Intelligence checks but pass $3$ Strength checks.
In the second test, you'll fail both checks, since the first point you get comes after the checks.
In the third test, one of the optimal strategies is:
1. spend the first point on Intelligence; 2. spend the second point on Strength; 3. spend the third point on Strength;
As a result, you'll pass $2$ Intelligence checks $r_3$ and $r_9$ and $2$ Strength checks $r_7$ and $r_8$.
|
Title: Attribute Checks
time_limit_ms: 2500
memory_limit_mb: 512
Description: Imagine a game where you play as a character that has two attributes: "Strength" and "Intelligence", that are at zero level initially.
During the game, you'll acquire $m$ attribute points that allow you to increase your attribute levels β one point will increase one of the attributes by one level. But sometimes, you'll encounter a so-called "Attribute Checks": if your corresponding attribute is high enough, you'll pass it; otherwise, you'll fail it.
Spending some time, you finally prepared a list which contains records of all points you got and all checks you've met. And now you're wondering: what is the maximum number of attribute checks you can pass in a single run if you'd spend points wisely?
Note that you can't change the order of records.
Input: The first line contains two integers $n$ and $m$ ($1 \le m \le 5000$; $m < n \le 2 \cdot 10^6$) β the number of records in the list and the total number of points you'll get during the game.
The second line contains $n$ integers $r_1, r_2, \dots, r_n$ ($-m \le r_i \le m$), where $r_i$ encodes the $i$-th record:
* If $r_i = 0$, then the $i$-th record is an acquiring one attribute point. You can spend to level up either Strength or Intelligence; * If $r_i > 0$, then it's an Intelligence check: if your Intelligence level is greater than or equal to $|r_i|$, you pass. * If $r_i < 0$, then it's a Strength check: if your Strength level is greater than or equal to $|r_i|$, you pass.
Additional constraint on the input: the sequence $r_1, r_2, \dots, r_n$ contains exactly $m$ elements equal to $0$.
Output: Print one integer β the maximum number of checks you can pass.
Examples:
input:
10 5
0 1 0 2 0 -3 0 -4 0 -5
output:
3
input:
3 1
1 -1 0
output:
0
input:
9 3
0 0 1 0 2 -3 -2 -2 1
output:
4
Note: In the first test, it's optimal to spend each point in Strength, so you'll fail $2$ Intelligence checks but pass $3$ Strength checks.
In the second test, you'll fail both checks, since the first point you get comes after the checks.
In the third test, one of the optimal strategies is:
1. spend the first point on Intelligence; 2. spend the second point on Strength; 3. spend the third point on Strength;
As a result, you'll pass $2$ Intelligence checks $r_3$ and $r_9$ and $2$ Strength checks $r_7$ and $r_8$.
|
2028B
|
https://codeforces.com/problemset/problem/2028/B
|
Alice's Adventures in Permuting
| 1,400
|
[
"binary search",
"implementation",
"math"
] |
Div. 2
| 1,000
| 256
|
Alice mixed up the words transmutation and permutation! She has an array $a$ specified via three integers $n$, $b$, $c$: the array $a$ has length $n$ and is given via $a_i = b\cdot (i - 1) + c$ for $1\le i\le n$. For example, if $n=3$, $b=2$, and $c=1$, then $a=[2 \cdot 0 + 1, 2 \cdot 1 + 1, 2 \cdot 2 + 1] = [1, 3, 5]$.
Now, Alice really enjoys permutations of $[0, \ldots, n-1]$$^{\text{β}}$ and would like to transform $a$ into a permutation. In one operation, Alice replaces the maximum element of $a$ with the $\operatorname{MEX}$$^{\text{β }}$ of $a$. If there are multiple maximum elements in $a$, Alice chooses the leftmost one to replace.
Can you help Alice figure out how many operations she has to do for $a$ to become a permutation for the first time? If it is impossible, you should report it.
$^{\text{β}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $0$ to $n-1$ in arbitrary order. Please note, this is slightly different from the usual definition of a permutation. For example, $[1,2,0,4,3]$ is a permutation, but $[0,1,1]$ is not a permutation ($1$ appears twice in the array), and $[0,2,3]$ is also not a permutation ($n=3$ but there is $3$ in the array).
$^{\text{β }}$The $\operatorname{MEX}$ of an array is the smallest non- negative integer that does not belong to the array. For example, the $\operatorname{MEX}$ of $[0, 3, 1, 3]$ is $2$ and the $\operatorname{MEX}$ of $[5]$ is $0$.
|
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 only line of each test case contains three integers $n$, $b$, $c$ ($1\le n\le 10^{18}$; $0\le b$, $c\le 10^{18}$) β the parameters of the array.
|
For each test case, if the array can never become a permutation, output $-1$. Otherwise, output the minimum number of operations for the array to become a permutation.
|
[
[
"7\n10 1 0\n1 2 3\n100 2 1\n3 0 1\n3 0 0\n1000000000000000000 0 0\n1000000000000000000 1000000000000000000 1000000000000000000",
"0\n1\n50\n2\n-1\n-1\n1000000000000000000"
]
] |
In the first test case, the array is already $[0, 1, \ldots, 9]$, so no operations are required.
In the third test case, the starting array is $[1, 3, 5, \ldots, 199]$. After the first operation, the $199$ gets transformed into a $0$. In the second operation, the $197$ gets transformed into a $2$. If we continue this, it will take exactly $50$ operations to get the array $[0, 1, 2, 3, \ldots, 99]$.
In the fourth test case, two operations are needed: $[1,1,1] \to [0,1,1] \to [0,2,1]$.
In the fifth test case, the process is $[0,0,0] \to [1,0,0] \to [2,0,0] \to [1,0,0] \to [2,0,0]$. This process repeats forever, so the array is never a permutation and the answer is $-1$.
|
Title: Alice's Adventures in Permuting
time_limit_ms: 1000
memory_limit_mb: 256
Description: Alice mixed up the words transmutation and permutation! She has an array $a$ specified via three integers $n$, $b$, $c$: the array $a$ has length $n$ and is given via $a_i = b\cdot (i - 1) + c$ for $1\le i\le n$. For example, if $n=3$, $b=2$, and $c=1$, then $a=[2 \cdot 0 + 1, 2 \cdot 1 + 1, 2 \cdot 2 + 1] = [1, 3, 5]$.
Now, Alice really enjoys permutations of $[0, \ldots, n-1]$$^{\text{β}}$ and would like to transform $a$ into a permutation. In one operation, Alice replaces the maximum element of $a$ with the $\operatorname{MEX}$$^{\text{β }}$ of $a$. If there are multiple maximum elements in $a$, Alice chooses the leftmost one to replace.
Can you help Alice figure out how many operations she has to do for $a$ to become a permutation for the first time? If it is impossible, you should report it.
$^{\text{β}}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $0$ to $n-1$ in arbitrary order. Please note, this is slightly different from the usual definition of a permutation. For example, $[1,2,0,4,3]$ is a permutation, but $[0,1,1]$ is not a permutation ($1$ appears twice in the array), and $[0,2,3]$ is also not a permutation ($n=3$ but there is $3$ in the array).
$^{\text{β }}$The $\operatorname{MEX}$ of an array is the smallest non- negative integer that does not belong to the array. For example, the $\operatorname{MEX}$ of $[0, 3, 1, 3]$ is $2$ and the $\operatorname{MEX}$ of $[5]$ is $0$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$). The description of the test cases follows.
The only line of each test case contains three integers $n$, $b$, $c$ ($1\le n\le 10^{18}$; $0\le b$, $c\le 10^{18}$) β the parameters of the array.
Output: For each test case, if the array can never become a permutation, output $-1$. Otherwise, output the minimum number of operations for the array to become a permutation.
Examples:
input:
7
10 1 0
1 2 3
100 2 1
3 0 1
3 0 0
1000000000000000000 0 0
1000000000000000000 1000000000000000000 1000000000000000000
output:
0
1
50
2
-1
-1
1000000000000000000
Note: In the first test case, the array is already $[0, 1, \ldots, 9]$, so no operations are required.
In the third test case, the starting array is $[1, 3, 5, \ldots, 199]$. After the first operation, the $199$ gets transformed into a $0$. In the second operation, the $197$ gets transformed into a $2$. If we continue this, it will take exactly $50$ operations to get the array $[0, 1, 2, 3, \ldots, 99]$.
In the fourth test case, two operations are needed: $[1,1,1] \to [0,1,1] \to [0,2,1]$.
In the fifth test case, the process is $[0,0,0] \to [1,0,0] \to [2,0,0] \to [1,0,0] \to [2,0,0]$. This process repeats forever, so the array is never a permutation and the answer is $-1$.
|
2026D
|
https://codeforces.com/problemset/problem/2026/D
|
Sums of Segments
| 1,900
|
[
"binary search",
"data structures",
"dp",
"implementation",
"math"
] |
Div. 2
| 4,000
| 512
|
You are given a sequence of integers $[a_1, a_2, \dots, a_n]$. Let $s(l,r)$ be the sum of elements from $a_l$ to $a_r$ (i. e. $s(l,r) = \sum\limits_{i=l}^{r} a_i$).
Let's construct another sequence $b$ of size $\frac{n(n+1)}{2}$ as follows: $b = [s(1,1), s(1,2), \dots, s(1,n), s(2,2), s(2,3), \dots, s(2,n), s(3,3), \dots, s(n,n)]$.
For example, if $a = [1, 2, 5, 10]$, then $b = [1, 3, 8, 18, 2, 7, 17, 5, 15, 10]$.
You are given $q$ queries. During the $i$-th query, you are given two integers $l_i$ and $r_i$, and you have to calculate $\sum \limits_{j=l_i}^{r_i} b_j$.
|
The first line contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10 \le a_i \le 10$).
The third line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$).
Then $q$ lines follow, the $i$-th of them contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le \frac{n(n+1)}{2}$).
|
Print $q$ integers, the $i$-th of which should be equal to $\sum \limits_{j=l_i}^{r_i} b_j$.
|
[
[
"4\n1 2 5 10\n15\n1 1\n1 2\n1 3\n1 4\n1 5\n1 10\n5 10\n6 10\n2 8\n3 4\n3 10\n3 8\n5 6\n5 5\n1 8",
"1\n4\n12\n30\n32\n86\n56\n54\n60\n26\n82\n57\n9\n2\n61"
]
] |
Title: Sums of Segments
time_limit_ms: 4000
memory_limit_mb: 512
Description: You are given a sequence of integers $[a_1, a_2, \dots, a_n]$. Let $s(l,r)$ be the sum of elements from $a_l$ to $a_r$ (i. e. $s(l,r) = \sum\limits_{i=l}^{r} a_i$).
Let's construct another sequence $b$ of size $\frac{n(n+1)}{2}$ as follows: $b = [s(1,1), s(1,2), \dots, s(1,n), s(2,2), s(2,3), \dots, s(2,n), s(3,3), \dots, s(n,n)]$.
For example, if $a = [1, 2, 5, 10]$, then $b = [1, 3, 8, 18, 2, 7, 17, 5, 15, 10]$.
You are given $q$ queries. During the $i$-th query, you are given two integers $l_i$ and $r_i$, and you have to calculate $\sum \limits_{j=l_i}^{r_i} b_j$.
Input: The first line contains one integer $n$ ($1 \le n \le 3 \cdot 10^5$).
The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-10 \le a_i \le 10$).
The third line contains one integer $q$ ($1 \le q \le 3 \cdot 10^5$).
Then $q$ lines follow, the $i$-th of them contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le \frac{n(n+1)}{2}$).
Output: Print $q$ integers, the $i$-th of which should be equal to $\sum \limits_{j=l_i}^{r_i} b_j$.
Examples:
input:
4
1 2 5 10
15
1 1
1 2
1 3
1 4
1 5
1 10
5 10
6 10
2 8
3 4
3 10
3 8
5 6
5 5
1 8
output:
1
4
12
30
32
86
56
54
60
26
82
57
9
2
61
Note:
|
|
2036F
|
https://codeforces.com/problemset/problem/2036/F
|
XORificator 3000
| 1,900
|
[
"bitmasks",
"dp",
"number theory",
"two pointers"
] |
Div. 3
| 1,000
| 256
|
Alice has been giving gifts to Bob for many years, and she knows that what he enjoys the most is performing [bitwise XOR](http://tiny.cc/xor_wiki_eng) of interesting integers. Bob considers a positive integer $x$ to be interesting if it satisfies $x \not\equiv k (\bmod 2^i)$. Therefore, this year for his birthday, she gifted him a super-powerful "XORificator 3000", the latest model.
Bob was very pleased with the gift, as it allowed him to instantly compute the XOR of all interesting integers in any range from $l$ to $r$, inclusive. After all, what else does a person need for happiness? Unfortunately, the device was so powerful that at one point it performed XOR with itself and disappeared. Bob was very upset, and to cheer him up, Alice asked you to write your version of the "XORificator".
|
The first line of input contains a single integer $t$ $(1 \leq t \leq 10^4)$ β the number of XOR queries on the segment. The following $t$ lines contain the queries, each consisting of the integers $l$, $r$, $i$, $k$ $(1 \leq l \leq r \leq 10^{18}$, $0 \leq i \leq 30$, $0 \leq k < 2^i)$.
|
For each query, output a single integer β the XOR of all integers $x$ in the range $[l, r]$ such that $x \not\equiv k \mod 2^i$.
|
[
[
"6\n1 3 1 0\n2 28 3 7\n15 43 1 0\n57 2007 1 0\n1010 1993 2 2\n1 1000000000 30 1543",
"2\n2\n13\n0\n4\n1000000519"
]
] |
In the first query, the interesting integers in the range $[1, 3]$ are $1$ and $3$, so the answer will be $1 \oplus 3 = 2$.
|
Title: XORificator 3000
time_limit_ms: 1000
memory_limit_mb: 256
Description: Alice has been giving gifts to Bob for many years, and she knows that what he enjoys the most is performing [bitwise XOR](http://tiny.cc/xor_wiki_eng) of interesting integers. Bob considers a positive integer $x$ to be interesting if it satisfies $x \not\equiv k (\bmod 2^i)$. Therefore, this year for his birthday, she gifted him a super-powerful "XORificator 3000", the latest model.
Bob was very pleased with the gift, as it allowed him to instantly compute the XOR of all interesting integers in any range from $l$ to $r$, inclusive. After all, what else does a person need for happiness? Unfortunately, the device was so powerful that at one point it performed XOR with itself and disappeared. Bob was very upset, and to cheer him up, Alice asked you to write your version of the "XORificator".
Input: The first line of input contains a single integer $t$ $(1 \leq t \leq 10^4)$ β the number of XOR queries on the segment. The following $t$ lines contain the queries, each consisting of the integers $l$, $r$, $i$, $k$ $(1 \leq l \leq r \leq 10^{18}$, $0 \leq i \leq 30$, $0 \leq k < 2^i)$.
Output: For each query, output a single integer β the XOR of all integers $x$ in the range $[l, r]$ such that $x \not\equiv k \mod 2^i$.
Examples:
input:
6
1 3 1 0
2 28 3 7
15 43 1 0
57 2007 1 0
1010 1993 2 2
1 1000000000 30 1543
output:
2
2
13
0
4
1000000519
Note: In the first query, the interesting integers in the range $[1, 3]$ are $1$ and $3$, so the answer will be $1 \oplus 3 = 2$.
|
1975G
|
https://codeforces.com/problemset/problem/1975/G
|
Zimpha Fan Club
| 3,000
|
[
"fft",
"greedy",
"math",
"strings"
] |
Div. 1 + 2
| 12,000
| 512
|
One day, Zimpha casually came up with a problem. As a member of "Zimpha fan club", you decided to solve that problem. You are given two strings $s$ and $t$ of length $n$ and $m$, respectively. Both strings only consist of lowercase English letters, - and *.
You need to replace all occurrences of * and -, observing the following rules:
* For each -, you must replace it with any lowercase English letter. * For each *, you must replace it with a string of any (possibly, zero) length which only consists of lowercase English letters.
Note that you can replace two different instances of - with different characters. You can also replace each two different instances of * with different strings.
Suppose $s$ and $t$ have been transformed into $s'$ and $t'$. Now you're wondering if there's a replacement that makes $s'=t'$.
|
The first line of input contains two integers $n$ and $m$ ($1 \leq n, m \leq 2 \cdot 10^6$) β the length of the strings $s$ and $t$, respectively.
The second line contains the string $s$ of length $n$. It is guaranteed that $s$ only consists of lowercase English letters, - and *.
The third line contains the string $t$ of length $m$. It is guaranteed that $t$ only consists of lowercase English letters, - and *.
|
For each test case, output "Yes" if there is a replacement that makes $s'=t'$, and output "No" otherwise.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
|
[
[
"10 10\njustmonika\nj-stsayori",
"No"
],
[
"7 8\nttk-wxx\n*tt-l-xx",
"Yes"
],
[
"13 11\nasoulwangziji\n-soulg*z-y-",
"No"
],
[
"7 3\nabc*cba\na*c",
"No"
],
[
"20 18\nbulijiojio-dibuliduo\n*li*ji-*ox*i*-du*-",
"Yes"
]
] |
In the second test case, we can transform both strings into ttklwxx. In $s$, - will be replaced with l. In $t$, * will be replaced by the empty string with the first and second - will be replaced with k and w respectively.
In the fifth test case, we can transform both strings into bulijiojioxdibuliduo.
|
Title: Zimpha Fan Club
time_limit_ms: 12000
memory_limit_mb: 512
Description: One day, Zimpha casually came up with a problem. As a member of "Zimpha fan club", you decided to solve that problem. You are given two strings $s$ and $t$ of length $n$ and $m$, respectively. Both strings only consist of lowercase English letters, - and *.
You need to replace all occurrences of * and -, observing the following rules:
* For each -, you must replace it with any lowercase English letter. * For each *, you must replace it with a string of any (possibly, zero) length which only consists of lowercase English letters.
Note that you can replace two different instances of - with different characters. You can also replace each two different instances of * with different strings.
Suppose $s$ and $t$ have been transformed into $s'$ and $t'$. Now you're wondering if there's a replacement that makes $s'=t'$.
Input: The first line of input contains two integers $n$ and $m$ ($1 \leq n, m \leq 2 \cdot 10^6$) β the length of the strings $s$ and $t$, respectively.
The second line contains the string $s$ of length $n$. It is guaranteed that $s$ only consists of lowercase English letters, - and *.
The third line contains the string $t$ of length $m$. It is guaranteed that $t$ only consists of lowercase English letters, - and *.
Output: For each test case, output "Yes" if there is a replacement that makes $s'=t'$, and output "No" otherwise.
You can output "Yes" and "No" in any case (for example, strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive response).
Examples:
input:
10 10
justmonika
j-stsayori
output:
No
input:
7 8
ttk-wxx
*tt-l-xx
output:
Yes
input:
13 11
asoulwangziji
-soulg*z-y-
output:
No
input:
7 3
abc*cba
a*c
output:
No
input:
20 18
bulijiojio-dibuliduo
*li*ji-*ox*i*-du*-
output:
Yes
Note: In the second test case, we can transform both strings into ttklwxx. In $s$, - will be replaced with l. In $t$, * will be replaced by the empty string with the first and second - will be replaced with k and w respectively.
In the fifth test case, we can transform both strings into bulijiojioxdibuliduo.
|
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.
|
Title: Robin Hood and the Major Oak
time_limit_ms: 1000
memory_limit_mb: 256
Description: 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$.
Input: 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.
Output: 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.
Examples:
input:
5
1 1
2 1
2 2
3 2
4 4
output:
NO
YES
NO
NO
YES
Note: 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.
|
2026C
|
https://codeforces.com/problemset/problem/2026/C
|
Action Figures
| 1,500
|
[
"binary search",
"brute force",
"constructive algorithms",
"data structures",
"greedy",
"implementation"
] |
Div. 2
| 2,500
| 512
|
There is a shop that sells action figures near Monocarp's house. A new set of action figures will be released shortly; this set contains $n$ figures, the $i$-th figure costs $i$ coins and is available for purchase from day $i$ to day $n$.
For each of the $n$ days, Monocarp knows whether he can visit the shop.
Every time Monocarp visits the shop, he can buy any number of action figures which are sold in the shop (of course, he cannot buy an action figure that is not yet available for purchase). If Monocarp buys at least two figures during the same day, he gets a discount equal to the cost of the most expensive figure he buys (in other words, he gets the most expensive of the figures he buys for free).
Monocarp wants to buy exactly one $1$-st figure, one $2$-nd figure, ..., one $n$-th figure from the set. He cannot buy the same figure twice. What is the minimum amount of money he has to spend?
|
The first line contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
Each test case consists of two lines:
* the first line contains one integer $n$ ($1 \le n \le 4 \cdot 10^5$) β the number of figures in the set (and the number of days); * the second line contains a string $s$ ($|s| = n$, each $s_i$ is either 0 or 1). If Monocarp can visit the shop on the $i$-th day, then $s_i$ is 1; otherwise, $s_i$ is 0.
Additional constraints on the input:
* in each test case, $s_n$ is 1, so Monocarp is always able to buy all figures during the $n$-th day; * the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$.
|
For each test case, print one integer β the minimum amount of money Monocarp has to spend.
|
[
[
"4\n1\n1\n6\n101101\n7\n1110001\n5\n11111",
"1\n8\n18\n6"
]
] |
In the first test case, Monocarp buys the $1$-st figure on the $1$-st day and spends $1$ coin.
In the second test case, Monocarp can buy the $1$-st and the $3$-rd figure on the $3$-rd day, the $2$-nd and the $4$-th figure on the $4$-th day, and the $5$-th and the $6$-th figure on the $6$-th day. Then, he will spend $1+2+5=8$ coins.
In the third test case, Monocarp can buy the $2$-nd and the $3$-rd figure on the $3$-rd day, and all other figures on the $7$-th day. Then, he will spend $1+2+4+5+6 = 18$ coins.
|
Title: Action Figures
time_limit_ms: 2500
memory_limit_mb: 512
Description: There is a shop that sells action figures near Monocarp's house. A new set of action figures will be released shortly; this set contains $n$ figures, the $i$-th figure costs $i$ coins and is available for purchase from day $i$ to day $n$.
For each of the $n$ days, Monocarp knows whether he can visit the shop.
Every time Monocarp visits the shop, he can buy any number of action figures which are sold in the shop (of course, he cannot buy an action figure that is not yet available for purchase). If Monocarp buys at least two figures during the same day, he gets a discount equal to the cost of the most expensive figure he buys (in other words, he gets the most expensive of the figures he buys for free).
Monocarp wants to buy exactly one $1$-st figure, one $2$-nd figure, ..., one $n$-th figure from the set. He cannot buy the same figure twice. What is the minimum amount of money he has to spend?
Input: The first line contains one integer $t$ ($1 \le t \le 10^4$) β the number of test cases.
Each test case consists of two lines:
* the first line contains one integer $n$ ($1 \le n \le 4 \cdot 10^5$) β the number of figures in the set (and the number of days); * the second line contains a string $s$ ($|s| = n$, each $s_i$ is either 0 or 1). If Monocarp can visit the shop on the $i$-th day, then $s_i$ is 1; otherwise, $s_i$ is 0.
Additional constraints on the input:
* in each test case, $s_n$ is 1, so Monocarp is always able to buy all figures during the $n$-th day; * the sum of $n$ over all test cases does not exceed $4 \cdot 10^5$.
Output: For each test case, print one integer β the minimum amount of money Monocarp has to spend.
Examples:
input:
4
1
1
6
101101
7
1110001
5
11111
output:
1
8
18
6
Note: In the first test case, Monocarp buys the $1$-st figure on the $1$-st day and spends $1$ coin.
In the second test case, Monocarp can buy the $1$-st and the $3$-rd figure on the $3$-rd day, the $2$-nd and the $4$-th figure on the $4$-th day, and the $5$-th and the $6$-th figure on the $6$-th day. Then, he will spend $1+2+5=8$ coins.
In the third test case, Monocarp can buy the $2$-nd and the $3$-rd figure on the $3$-rd day, and all other figures on the $7$-th day. Then, he will spend $1+2+4+5+6 = 18$ coins.
|
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"
]
] |
Title: Sakurako's Test
time_limit_ms: 1000
memory_limit_mb: 256
Description: 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)
Input: 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.
Output: For each test case, output $q$ integers β the answer for each query.
Examples:
input:
2
5 5
1 2 3 4 5
1
2
3
4
5
6 3
1 2 6 4 1 3
2
1
5
output:
0 1 1 1 2
1 0 2
Note:
|
|
1988B
|
https://codeforces.com/problemset/problem/1988/B
|
Make Majority
| 900
|
[
"greedy",
"implementation"
] |
Div. 2
| 1,500
| 512
|
You are given a sequence $[a_1,\ldots,a_n]$ where each element $a_i$ is either $0$ or $1$. You can apply several (possibly zero) operations to the sequence. In each operation, you select two integers $1\le l\le r\le |a|$ (where $|a|$ is the current length of $a$) and replace $[a_l,\ldots,a_r]$ with a single element $x$, where $x$ is the majority of $[a_l,\ldots,a_r]$.
Here, the majority of a sequence consisting of $0$ and $1$ is defined as follows: suppose there are $c_0$ zeros and $c_1$ ones in the sequence, respectively.
* If $c_0\ge c_1$, the majority is $0$. * If $c_0<c_1$, the majority is $1$.
For example, suppose $a=[1,0,0,0,1,1]$. If we select $l=1,r=2$, the resulting sequence will be $[0,0,0,1,1]$. If we select $l=4,r=6$, the resulting sequence will be $[1,0,0,1]$.
Determine if you can make $a=[1]$ with a finite number of operations.
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 4\cdot 10^4$). Description of the test cases follows.
The first line of each testcase contains one integer $n$ ($1\le n\le 2\cdot 10^5$).
The second line of each testcase contains a string consisting of $0$ and $1$, describing the sequence $a$.
It's guaranteed that the sum of $n$ over all testcases does not exceed $2\cdot 10^5$.
|
For each testcase, if it's possible to make $a=[1]$, print YES. Otherwise, print NO. 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\n0\n1\n1\n2\n01\n9\n100000001\n9\n000011000",
"No\nYes\nNo\nYes\nNo"
]
] |
In the fourth testcase of the example, initially $a=[1,0,0,0,0,0,0,0,1]$. A valid sequence of operations is:
1. Select $l=2,r=8$ and apply the operation. $a$ becomes $[1,0,1]$. 2. Select $l=1,r=3$ and apply the operation. $a$ becomes $[1]$.
|
Title: Make Majority
time_limit_ms: 1500
memory_limit_mb: 512
Description: You are given a sequence $[a_1,\ldots,a_n]$ where each element $a_i$ is either $0$ or $1$. You can apply several (possibly zero) operations to the sequence. In each operation, you select two integers $1\le l\le r\le |a|$ (where $|a|$ is the current length of $a$) and replace $[a_l,\ldots,a_r]$ with a single element $x$, where $x$ is the majority of $[a_l,\ldots,a_r]$.
Here, the majority of a sequence consisting of $0$ and $1$ is defined as follows: suppose there are $c_0$ zeros and $c_1$ ones in the sequence, respectively.
* If $c_0\ge c_1$, the majority is $0$. * If $c_0<c_1$, the majority is $1$.
For example, suppose $a=[1,0,0,0,1,1]$. If we select $l=1,r=2$, the resulting sequence will be $[0,0,0,1,1]$. If we select $l=4,r=6$, the resulting sequence will be $[1,0,0,1]$.
Determine if you can make $a=[1]$ with a finite number of operations.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 4\cdot 10^4$). Description of the test cases follows.
The first line of each testcase contains one integer $n$ ($1\le n\le 2\cdot 10^5$).
The second line of each testcase contains a string consisting of $0$ and $1$, describing the sequence $a$.
It's guaranteed that the sum of $n$ over all testcases does not exceed $2\cdot 10^5$.
Output: For each testcase, if it's possible to make $a=[1]$, print YES. Otherwise, print NO. 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.
Examples:
input:
5
1
0
1
1
2
01
9
100000001
9
000011000
output:
No
Yes
No
Yes
No
Note: In the fourth testcase of the example, initially $a=[1,0,0,0,0,0,0,0,1]$. A valid sequence of operations is:
1. Select $l=2,r=8$ and apply the operation. $a$ becomes $[1,0,1]$. 2. Select $l=1,r=3$ and apply the operation. $a$ becomes $[1]$.
|
2032F
|
https://codeforces.com/problemset/problem/2032/F
|
Peanuts
| 2,700
|
[
"combinatorics",
"dp",
"games",
"math"
] |
Div. 2
| 4,000
| 256
|
Having the magical beanstalk, Jack has been gathering a lot of peanuts lately. Eventually, he has obtained $n$ pockets of peanuts, conveniently numbered $1$ to $n$ from left to right. The $i$-th pocket has $a_i$ peanuts.
Jack and his childhood friend Alice decide to play a game around the peanuts. First, Alice divides the pockets into some boxes; each box will have a non- zero number of consecutive pockets, and each pocket will, obviously, belong to exactly one box. At the same time, Alice does not change the order of the boxes, that is, the boxes are numbered in ascending order of the indices of the pockets in them.
After that, Alice and Jack will take turns alternately, with Alice going first.
At each turn, the current player will remove a positive number of peanuts from exactly one pocket which belongs to the leftmost non-empty box (i.e., the leftmost box containing at least one non-empty pocket). In other words, if we number the boxes from left to right, then each player can only pick peanuts from the pocket in the $j$-th box ($j \ge 2$) only if the $(j - 1)$-th box has no peanuts left. The player who cannot make a valid move loses.
Alice is sure she will win since she has the advantage of dividing the pockets into boxes herself. Thus, she wanted to know how many ways there are for her to divide the peanuts into boxes at the start of the game so that she will win, assuming both players play optimally. Can you help her with the calculation?
As the result can be very large, output it modulo $998\,244\,353$.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^6$) β the number of pockets.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β the number of peanuts in each pocket.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
|
For each test case, output a single integer β the number of ways for Alice to divide the pockets into boxes at the start of the game to guarantee her win, assuming both players play optimally, modulo $998\,244\,353$.
|
[
[
"5\n3\n1 2 3\n4\n1 2 3 1\n5\n1 1 1 1 1\n2\n1 1\n10\n1 2 3 4 5 6 7 8 9 10",
"1\n4\n16\n0\n205"
]
] |
In the first test case, the only way for Alice to win is to divide the pockets into two boxes as follows: $([1, 2], [3])$ (the first box contains the first two pockets and the second box contains the third pocket). Alice wins by taking both peanuts from the second pocket, leaving Jack with $([1], [3])$. Jack is forced to take the only peanut left in the first box, which allows Alice to take the remaining ones in the second box.
In the second test case, the winning divisions for Alice are $([1], [2, 3, 1])$, $([1, 2, 3, 1])$, $([1, 2], [3], [1])$, and $([1, 2], [3, 1])$.
In the third test case, Alice always wins no matter how she divides the pockets into boxes.
In the fourth test case, Alice always loses no matter how she divides the pockets into boxes.
|
Title: Peanuts
time_limit_ms: 4000
memory_limit_mb: 256
Description: Having the magical beanstalk, Jack has been gathering a lot of peanuts lately. Eventually, he has obtained $n$ pockets of peanuts, conveniently numbered $1$ to $n$ from left to right. The $i$-th pocket has $a_i$ peanuts.
Jack and his childhood friend Alice decide to play a game around the peanuts. First, Alice divides the pockets into some boxes; each box will have a non- zero number of consecutive pockets, and each pocket will, obviously, belong to exactly one box. At the same time, Alice does not change the order of the boxes, that is, the boxes are numbered in ascending order of the indices of the pockets in them.
After that, Alice and Jack will take turns alternately, with Alice going first.
At each turn, the current player will remove a positive number of peanuts from exactly one pocket which belongs to the leftmost non-empty box (i.e., the leftmost box containing at least one non-empty pocket). In other words, if we number the boxes from left to right, then each player can only pick peanuts from the pocket in the $j$-th box ($j \ge 2$) only if the $(j - 1)$-th box has no peanuts left. The player who cannot make a valid move loses.
Alice is sure she will win since she has the advantage of dividing the pockets into boxes herself. Thus, she wanted to know how many ways there are for her to divide the peanuts into boxes at the start of the game so that she will win, assuming both players play optimally. Can you help her with the calculation?
As the result can be very large, output it modulo $998\,244\,353$.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^6$) β the number of pockets.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β the number of peanuts in each pocket.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$.
Output: For each test case, output a single integer β the number of ways for Alice to divide the pockets into boxes at the start of the game to guarantee her win, assuming both players play optimally, modulo $998\,244\,353$.
Examples:
input:
5
3
1 2 3
4
1 2 3 1
5
1 1 1 1 1
2
1 1
10
1 2 3 4 5 6 7 8 9 10
output:
1
4
16
0
205
Note: In the first test case, the only way for Alice to win is to divide the pockets into two boxes as follows: $([1, 2], [3])$ (the first box contains the first two pockets and the second box contains the third pocket). Alice wins by taking both peanuts from the second pocket, leaving Jack with $([1], [3])$. Jack is forced to take the only peanut left in the first box, which allows Alice to take the remaining ones in the second box.
In the second test case, the winning divisions for Alice are $([1], [2, 3, 1])$, $([1, 2, 3, 1])$, $([1, 2], [3], [1])$, and $([1, 2], [3, 1])$.
In the third test case, Alice always wins no matter how she divides the pockets into boxes.
In the fourth test case, Alice always loses no matter how she divides the pockets into boxes.
|
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.
|
Title: Complex Segments (Easy Version)
time_limit_ms: 6000
memory_limit_mb: 256
Description: [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.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^3$). The description of the test cases follows.
The first line of each test case contains 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$.
Output: For each test case, output a single integer: the maximum size of a complex subset of the given segments.
Examples:
input:
3
3
1 2 3
5 4 6
5
1 2 3 6 8
5 4 7 9 10
5
3 1 4 1 5
7 2 6 5 10
output:
3
4
4
Note: 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.
|
1989F
|
https://codeforces.com/problemset/problem/1989/F
|
Simultaneous Coloring
| 3,000
|
[
"dfs and similar",
"divide and conquer",
"graphs"
] |
Div. 2
| 6,000
| 512
|
You are given a matrix, consisting of $n$ rows and $m$ columns.
You can perform two types of actions on it:
* paint the entire column in blue; * paint the entire row in red.
Note that you cannot choose which color to paint the row or column.
In one second, you can perform either one action or multiple actions at the same time. If you perform one action, it will be free. If you perform $k > 1$ actions at the same time, it will cost $k^2$ coins. When multiple actions are performed at the same time, for each cell affected by actions of both types, the color can be chosen independently.
You are asked to process $q$ queries. Before each query, all cells become colorless. Initially, there are no restrictions on the color of any cells. In the $i$-th query, a restriction of the following form is added:
* $x_i~y_i~c_i$ β the cell in row $x_i$ in column $y_i$ should be painted in color $c_i$.
Thus, after $i$ queries, there are $i$ restrictions on the required colors of the matrix cells. After each query, output the minimum cost of painting the matrix according to the restrictions.
|
The first line contains three integers $n, m$ and $q$ ($1 \le n, m, q \le 2 \cdot 10^5$) β the size of the matrix and the number of queries.
In the $i$-th of the next $q$ lines, two integers $x_i, y_i$ and a character $c_i$ ($1 \le x_i \le n$; $1 \le y_i \le m$; $c_i \in$ {'R', 'B'}, where 'R' means red, and 'B' means blue) β description of the $i$-th restriction. The cells in all queries are pairwise distinct.
|
Print $q$ integers β after each query, output the minimum cost of painting the matrix according to the restrictions.
|
[
[
"2 2 4\n1 1 R\n2 2 R\n1 2 B\n2 1 B",
"0\n0\n0\n16"
],
[
"3 5 10\n1 1 B\n2 5 B\n2 2 B\n2 3 R\n2 1 B\n3 2 R\n3 3 B\n1 2 R\n1 3 B\n3 1 B",
"0\n0\n0\n0\n0\n0\n16\n16\n25\n25"
]
] |
Title: Simultaneous Coloring
time_limit_ms: 6000
memory_limit_mb: 512
Description: You are given a matrix, consisting of $n$ rows and $m$ columns.
You can perform two types of actions on it:
* paint the entire column in blue; * paint the entire row in red.
Note that you cannot choose which color to paint the row or column.
In one second, you can perform either one action or multiple actions at the same time. If you perform one action, it will be free. If you perform $k > 1$ actions at the same time, it will cost $k^2$ coins. When multiple actions are performed at the same time, for each cell affected by actions of both types, the color can be chosen independently.
You are asked to process $q$ queries. Before each query, all cells become colorless. Initially, there are no restrictions on the color of any cells. In the $i$-th query, a restriction of the following form is added:
* $x_i~y_i~c_i$ β the cell in row $x_i$ in column $y_i$ should be painted in color $c_i$.
Thus, after $i$ queries, there are $i$ restrictions on the required colors of the matrix cells. After each query, output the minimum cost of painting the matrix according to the restrictions.
Input: The first line contains three integers $n, m$ and $q$ ($1 \le n, m, q \le 2 \cdot 10^5$) β the size of the matrix and the number of queries.
In the $i$-th of the next $q$ lines, two integers $x_i, y_i$ and a character $c_i$ ($1 \le x_i \le n$; $1 \le y_i \le m$; $c_i \in$ {'R', 'B'}, where 'R' means red, and 'B' means blue) β description of the $i$-th restriction. The cells in all queries are pairwise distinct.
Output: Print $q$ integers β after each query, output the minimum cost of painting the matrix according to the restrictions.
Examples:
input:
2 2 4
1 1 R
2 2 R
1 2 B
2 1 B
output:
0
0
0
16
input:
3 5 10
1 1 B
2 5 B
2 2 B
2 3 R
2 1 B
3 2 R
3 3 B
1 2 R
1 3 B
3 1 B
output:
0
0
0
0
0
0
16
16
25
25
Note:
|
|
2021A
|
https://codeforces.com/problemset/problem/2021/A
|
Meaning Mean
| 800
|
[
"data structures",
"greedy",
"math",
"sortings"
] |
Div. 2
| 1,000
| 256
|
Pak Chanek has an array $a$ of $n$ positive integers. Since he is currently learning how to calculate the floored average of two numbers, he wants to practice it on his array $a$.
While the array $a$ has at least two elements, Pak Chanek will perform the following three-step operation:
1. Pick two different indices $i$ and $j$ ($1 \leq i, j \leq |a|$; $i \neq j$), note that $|a|$ denotes the current size of the array $a$. 2. Append $\lfloor \frac{a_i+a_j}{2} \rfloor$$^{\text{β}}$ to the end of the array. 3. Remove elements $a_i$ and $a_j$ from the array and concatenate the remaining parts of the array.
For example, suppose that $a=[5,4,3,2,1,1]$. If we choose $i=1$ and $j=5$, the resulting array will be $a=[4,3,2,1,3]$. If we choose $i=4$ and $j=3$, the resulting array will be $a=[5,4,1,1,2]$.
After all operations, the array will consist of a single element $x$. Find the maximum possible value of $x$ if Pak Chanek performs the operations optimally.
$^{\text{β}}$$\lfloor x \rfloor$ denotes the floor function of $x$, which is the greatest integer that is less than or equal to $x$. For example, $\lfloor 6 \rfloor = 6$, $\lfloor 2.5 \rfloor=2$, $\lfloor -3.6 \rfloor=-4$ and $\lfloor \pi \rfloor=3$
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 50$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β the elements of the array $a$.
Do note that the sum of $n$ over all test cases is not bounded.
|
For each test case, output a single integer: the maximum possible value of $x$ after all numbers have been picked.
|
[
[
"3\n5\n1 7 8 4 5\n3\n2 6 5\n5\n5 5 5 5 5",
"6\n4\n5"
]
] |
In the first test case, the array is initially $a=[1,7,8,4,5]$. Pak Chanek will perform the following operations:
1. Pick $i=1$ and $j=2$, then $a=[8,4,5,4]$. 2. Pick $i=3$ and $j=2$, then $a=[8,4,4]$. 3. Pick $i=2$ and $j=3$, then $a=[8,4]$. 4. Pick $i=1$ and $j=2$, then $a=[6]$.
After all the operations, the array consists of a single element $x=6$. It can be proven that there is no series of operations that results in $x$ greater than $6$ in the end.
|
Title: Meaning Mean
time_limit_ms: 1000
memory_limit_mb: 256
Description: Pak Chanek has an array $a$ of $n$ positive integers. Since he is currently learning how to calculate the floored average of two numbers, he wants to practice it on his array $a$.
While the array $a$ has at least two elements, Pak Chanek will perform the following three-step operation:
1. Pick two different indices $i$ and $j$ ($1 \leq i, j \leq |a|$; $i \neq j$), note that $|a|$ denotes the current size of the array $a$. 2. Append $\lfloor \frac{a_i+a_j}{2} \rfloor$$^{\text{β}}$ to the end of the array. 3. Remove elements $a_i$ and $a_j$ from the array and concatenate the remaining parts of the array.
For example, suppose that $a=[5,4,3,2,1,1]$. If we choose $i=1$ and $j=5$, the resulting array will be $a=[4,3,2,1,3]$. If we choose $i=4$ and $j=3$, the resulting array will be $a=[5,4,1,1,2]$.
After all operations, the array will consist of a single element $x$. Find the maximum possible value of $x$ if Pak Chanek performs the operations optimally.
$^{\text{β}}$$\lfloor x \rfloor$ denotes the floor function of $x$, which is the greatest integer that is less than or equal to $x$. For example, $\lfloor 6 \rfloor = 6$, $\lfloor 2.5 \rfloor=2$, $\lfloor -3.6 \rfloor=-4$ and $\lfloor \pi \rfloor=3$
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5000$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 50$) β the length of the array $a$.
The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β the elements of the array $a$.
Do note that the sum of $n$ over all test cases is not bounded.
Output: For each test case, output a single integer: the maximum possible value of $x$ after all numbers have been picked.
Examples:
input:
3
5
1 7 8 4 5
3
2 6 5
5
5 5 5 5 5
output:
6
4
5
Note: In the first test case, the array is initially $a=[1,7,8,4,5]$. Pak Chanek will perform the following operations:
1. Pick $i=1$ and $j=2$, then $a=[8,4,5,4]$. 2. Pick $i=3$ and $j=2$, then $a=[8,4,4]$. 3. Pick $i=2$ and $j=3$, then $a=[8,4]$. 4. Pick $i=1$ and $j=2$, then $a=[6]$.
After all the operations, the array consists of a single element $x=6$. It can be proven that there is no series of operations that results in $x$ greater than $6$ in the end.
|
2036A
|
https://codeforces.com/problemset/problem/2036/A
|
Quintomania
| 800
|
[
"implementation"
] |
Div. 3
| 1,000
| 256
|
Boris Notkin composes melodies. He represents them as a sequence of notes, where each note is encoded as an integer from $0$ to $127$ inclusive. The interval between two notes $a$ and $b$ is equal to $|a - b|$ semitones.
Boris considers a melody perfect if the interval between each two adjacent notes is either $5$ semitones or $7$ semitones.
After composing his latest melodies, he enthusiastically shows you his collection of works. Help Boris Notkin understand whether his melodies are perfect.
|
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of melodies.
Each melody is described by two lines.
The first line contains an integer $n$ ($2 \leq n \leq 50$) β the number of notes in the melody.
The second line contains $n$ integers $a_{1}, a_{2}, \dots, a_{n}$ ($0 \leq a_{i} \leq 127$) β the notes of the melody.
|
For each melody, output "YES", if it is perfect; otherwise, output "NO".
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.
|
[
[
"8\n2\n114 109\n2\n17 10\n3\n76 83 88\n8\n38 45 38 80 85 92 99 106\n5\n63 58 65 58 65\n8\n117 124 48 53 48 43 54 49\n5\n95 102 107 114 121\n10\n72 77 82 75 70 75 68 75 68 75",
"YES\nYES\nYES\nNO\nYES\nNO\nYES\nYES"
]
] |
Title: Quintomania
time_limit_ms: 1000
memory_limit_mb: 256
Description: Boris Notkin composes melodies. He represents them as a sequence of notes, where each note is encoded as an integer from $0$ to $127$ inclusive. The interval between two notes $a$ and $b$ is equal to $|a - b|$ semitones.
Boris considers a melody perfect if the interval between each two adjacent notes is either $5$ semitones or $7$ semitones.
After composing his latest melodies, he enthusiastically shows you his collection of works. Help Boris Notkin understand whether his melodies are perfect.
Input: The first line contains an integer $t$ ($1 \leq t \leq 1000$) β the number of melodies.
Each melody is described by two lines.
The first line contains an integer $n$ ($2 \leq n \leq 50$) β the number of notes in the melody.
The second line contains $n$ integers $a_{1}, a_{2}, \dots, a_{n}$ ($0 \leq a_{i} \leq 127$) β the notes of the melody.
Output: For each melody, output "YES", if it is perfect; otherwise, output "NO".
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.
Examples:
input:
8
2
114 109
2
17 10
3
76 83 88
8
38 45 38 80 85 92 99 106
5
63 58 65 58 65
8
117 124 48 53 48 43 54 49
5
95 102 107 114 121
10
72 77 82 75 70 75 68 75 68 75
output:
YES
YES
YES
NO
YES
NO
YES
YES
Note:
|
|
1999A
|
https://codeforces.com/problemset/problem/1999/A
|
A+B Again?
| 800
|
[
"implementation",
"math"
] |
Div. 4
| 1,000
| 256
|
Given a two-digit positive integer $n$, find the sum of its digits.
|
The first line contains an integer $t$ ($1 \leq t \leq 90$) β the number of test cases.
The only line of each test case contains a single two-digit positive integer $n$ ($10 \leq n \leq 99$).
|
For each test case, output a single integer β the sum of the digits of $n$.
|
[
[
"8\n77\n21\n40\n34\n19\n84\n10\n99",
"14\n3\n4\n7\n10\n12\n1\n18"
]
] |
Title: A+B Again?
time_limit_ms: 1000
memory_limit_mb: 256
Description: Given a two-digit positive integer $n$, find the sum of its digits.
Input: The first line contains an integer $t$ ($1 \leq t \leq 90$) β the number of test cases.
The only line of each test case contains a single two-digit positive integer $n$ ($10 \leq n \leq 99$).
Output: For each test case, output a single integer β the sum of the digits of $n$.
Examples:
input:
8
77
21
40
34
19
84
10
99
output:
14
3
4
7
10
12
1
18
Note:
|
|
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"
]
] |
 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.
|
Title: Game in Tree (Easy Version)
time_limit_ms: 4000
memory_limit_mb: 256
Description: 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$).
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) β the number of vertices in the tree.
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$.
Output: 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.
Examples:
input:
3
3
1 2
2 3
2 2
3
1 2
2 3
3 3
6
1 2
1 3
2 4
2 5
1 6
4 4
output:
Bob
Alice
Alice
Note:  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.
|
1988F
|
https://codeforces.com/problemset/problem/1988/F
|
Heartbeat
| 3,000
|
[
"combinatorics",
"dp",
"fft",
"math"
] |
Div. 2
| 5,000
| 512
|
For an array $u_1, u_2, \ldots, u_n$, define
* a prefix maximum as an index $i$ such that $u_i>u_j$ for all $j<i$; * a suffix maximum as an index $i$ such that $u_i>u_j$ for all $j>i$; * an ascent as an index $i$ ($i>1$) such that $u_i>u_{i-1}$.
You are given three cost arrays: $[a_1, a_2, \ldots, a_n]$, $[b_1, b_2, \ldots, b_n]$, and $[c_0, c_1, \ldots, c_{n-1}]$.
Define the cost of an array that has $x$ prefix maximums, $y$ suffix maximums, and $z$ ascents as $a_x\cdot b_y\cdot c_z$.
Let the sum of costs of all permutations of $1,2,\ldots,n$ be $f(n)$. Find $f(1)$, $f(2)$, ..., $f(n)$ modulo $998\,244\,353$.
|
The first line contains an integer $n$ ($1\le n\le 700$).
The second line contains $n$ integers $a_1,\ldots,a_n$ ($0\le a_i<998\,244\,353$).
The third line contains $n$ integers $b_1,\ldots,b_n$ ($0\le b_i<998\,244\,353$).
The fourth line contains $n$ integers $c_0,\ldots,c_{n-1}$ ($0\le c_i<998\,244\,353$).
|
Print $n$ integers: the $i$-th one is $f(i)$ modulo $998\,244\,353$.
|
[
[
"3\n1 1 1\n1 1 1\n1 1 1",
"1 2 6"
],
[
"3\n1 2 3\n2 3 1\n3 1 2",
"6 13 34"
],
[
"5\n1 4 2 5 3\n2 5 1 3 4\n300000000 100000000 500000000 400000000 200000000",
"600000000 303511294 612289529 324650937 947905622"
]
] |
In the second example:
* Consider permutation $[1,2,3]$. Indices $1,2,3$ are prefix maximums. Index $3$ is the only suffix maximum. Indices $2,3$ are ascents. In conclusion, it has $3$ prefix maximums, $1$ suffix maximums, and $2$ ascents. Therefore, its cost is $a_3b_1c_2=12$. * Permutation $[1,3,2]$ has $2$ prefix maximums, $2$ suffix maximums, and $1$ ascent. Its cost is $6$. * Permutation $[2,1,3]$ has $2$ prefix maximums, $1$ suffix maximum, and $1$ ascent. Its cost is $4$. * Permutation $[2,3,1]$ has $2$ prefix maximums, $2$ suffix maximums, and $1$ ascent. Its cost is $6$. * Permutation $[3,1,2]$ has $1$ prefix maximum, $2$ suffix maximums, and $1$ ascent. Its cost is $3$. * Permutation $[3,2,1]$ has $1$ prefix maximum, $3$ suffix maximums, and $0$ ascents. Its cost is $3$.
The sum of all permutations' costs is $34$, so $f(3)=34$.
|
Title: Heartbeat
time_limit_ms: 5000
memory_limit_mb: 512
Description: For an array $u_1, u_2, \ldots, u_n$, define
* a prefix maximum as an index $i$ such that $u_i>u_j$ for all $j<i$; * a suffix maximum as an index $i$ such that $u_i>u_j$ for all $j>i$; * an ascent as an index $i$ ($i>1$) such that $u_i>u_{i-1}$.
You are given three cost arrays: $[a_1, a_2, \ldots, a_n]$, $[b_1, b_2, \ldots, b_n]$, and $[c_0, c_1, \ldots, c_{n-1}]$.
Define the cost of an array that has $x$ prefix maximums, $y$ suffix maximums, and $z$ ascents as $a_x\cdot b_y\cdot c_z$.
Let the sum of costs of all permutations of $1,2,\ldots,n$ be $f(n)$. Find $f(1)$, $f(2)$, ..., $f(n)$ modulo $998\,244\,353$.
Input: The first line contains an integer $n$ ($1\le n\le 700$).
The second line contains $n$ integers $a_1,\ldots,a_n$ ($0\le a_i<998\,244\,353$).
The third line contains $n$ integers $b_1,\ldots,b_n$ ($0\le b_i<998\,244\,353$).
The fourth line contains $n$ integers $c_0,\ldots,c_{n-1}$ ($0\le c_i<998\,244\,353$).
Output: Print $n$ integers: the $i$-th one is $f(i)$ modulo $998\,244\,353$.
Examples:
input:
3
1 1 1
1 1 1
1 1 1
output:
1 2 6
input:
3
1 2 3
2 3 1
3 1 2
output:
6 13 34
input:
5
1 4 2 5 3
2 5 1 3 4
300000000 100000000 500000000 400000000 200000000
output:
600000000 303511294 612289529 324650937 947905622
Note: In the second example:
* Consider permutation $[1,2,3]$. Indices $1,2,3$ are prefix maximums. Index $3$ is the only suffix maximum. Indices $2,3$ are ascents. In conclusion, it has $3$ prefix maximums, $1$ suffix maximums, and $2$ ascents. Therefore, its cost is $a_3b_1c_2=12$. * Permutation $[1,3,2]$ has $2$ prefix maximums, $2$ suffix maximums, and $1$ ascent. Its cost is $6$. * Permutation $[2,1,3]$ has $2$ prefix maximums, $1$ suffix maximum, and $1$ ascent. Its cost is $4$. * Permutation $[2,3,1]$ has $2$ prefix maximums, $2$ suffix maximums, and $1$ ascent. Its cost is $6$. * Permutation $[3,1,2]$ has $1$ prefix maximum, $2$ suffix maximums, and $1$ ascent. Its cost is $3$. * Permutation $[3,2,1]$ has $1$ prefix maximum, $3$ suffix maximums, and $0$ ascents. Its cost is $3$.
The sum of all permutations' costs is $34$, so $f(3)=34$.
|
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:

In the second test case, the grid graph and one of the optimal paths are as follows:

|
Title: Lattice Optimizing
time_limit_ms: 7000
memory_limit_mb: 1024
Description: 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.
Input: 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$.
Output: For each test case, print a single integer β the maximum MEX of $S$ when you reach $(n,n)$.
Examples:
input:
2
3
1 0 2
0 1 3
2 1
0 3
3 0
3
1 2 0
0 1 2
2 0
1 2
0 1
output:
3
2
input:
1
10
16 7 3 15 9 17 1 15 9 0
4 3 1 12 13 10 10 14 6 12
3 1 3 9 5 16 0 12 7 12
11 4 8 7 13 7 15 13 9 2
2 3 9 9 4 12 17 7 10 15
10 6 15 17 13 6 15 9 4 9
13 3 3 14 1 2 10 10 12 16
8 2 9 13 18 7 1 6 2 6
15 12 2 6 0 0 13 3 7 17
7 3 17 17 10 15 12 14 15
4 3 3 17 3 13 11 16 6
16 17 7 7 12 5 2 4 10
18 9 9 3 5 9 1 16 7
1 0 4 2 10 10 12 2 1
4 14 15 16 15 5 8 4 18
7 18 10 11 2 0 14 8 18
2 17 6 0 9 6 13 5 11
5 15 7 11 6 3 17 14 5
1 3 16 16 13 1 0 13 11
output:
14
Note: In the first test case, the grid graph and one of the optimal paths are as follows:

In the second test case, the grid graph and one of the optimal paths are as follows:

|
1998C
|
https://codeforces.com/problemset/problem/1998/C
|
Perform Operations to Maximize Score
| 1,900
|
[
"binary search",
"brute force",
"constructive algorithms",
"greedy",
"implementation"
] |
Div. 2
| 3,000
| 256
|
I see satyam343. I'm shaking. Please more median problems this time. I love those. Please satyam343 we believe in you.
β satyam343's biggest fan
You are given an array $a$ of length $n$ and an integer $k$. You are also given a binary array $b$ of length $n$.
You can perform the following operation at most $k$ times:
* Select an index $i$ ($1 \leq i \leq n$) such that $b_i = 1$. Set $a_i = a_i + 1$ (i.e., increase $a_i$ by $1$).
Your score is defined to be $\max\limits_{i = 1}^{n} \left( a_i + \operatorname{median}(c_i) \right)$, where $c_i$ denotes the array of length $n-1$ that you get by deleting $a_i$ from $a$. In other words, your score is the maximum value of $a_i + \operatorname{median}(c_i)$ over all $i$ from $1$ to $n$.
Find the maximum score that you can achieve if you perform the operations optimally.
For an arbitrary array $p$, $\operatorname{median}(p)$ is defined as the $\left\lfloor \frac{|p|+1}{2} \right\rfloor$-th smallest element of $p$. For example, $\operatorname{median} \left( [3,2,1,3] \right) = 2$ and $\operatorname{median} \left( [6,2,4,5,1] \right) = 4$.
|
The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
Each test case begins with two integers $n$ and $k$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^9$) β the length of the $a$ and the number of operations you can perform.
The following line contains $n$ space separated integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β denoting the array $a$.
The following line contains $n$ space separated integers $b_1, b_2, \ldots, b_n$ ($b_i$ is $0$ or $1$) β denoting the array $b$.
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 maximum value of score you can get on a new line.
|
[
[
"8\n2 10\n3 3\n1 1\n3 10\n3 3 3\n0 0 0\n4 4\n2 1 5 1\n0 1 0 1\n5 4\n7 5 2 5 4\n0 0 1 0 1\n5 1\n5 15 15 2 11\n1 0 0 1 1\n5 2\n10 11 4 10 15\n1 1 0 1 0\n4 4\n1 1 2 5\n1 1 0 0\n2 1000000000\n1000000000 1000000000\n1 1",
"16\n6\n8\n13\n21\n26\n8\n3000000000"
]
] |
For the first test case, it is optimal to perform $5$ operations on both elements so $a = [8,8]$. So, the maximum score we can achieve is $\max(8 + \operatorname{median}[8], 8 + \operatorname{median}[8]) = 16$, as $c_1 = [a_2] = [8]$. It can be proven that you cannot get a better score.
For the second test case, you are not able to perform operations on any elements, so $a$ remains $[3,3,3]$. So, the maximum score we can achieve is $3 + \operatorname{median}[3, 3] = 6$, as $c_1 = [a_2, a_3] = [3, 3]$. It can be proven that you cannot get a better score.
|
Title: Perform Operations to Maximize Score
time_limit_ms: 3000
memory_limit_mb: 256
Description: I see satyam343. I'm shaking. Please more median problems this time. I love those. Please satyam343 we believe in you.
β satyam343's biggest fan
You are given an array $a$ of length $n$ and an integer $k$. You are also given a binary array $b$ of length $n$.
You can perform the following operation at most $k$ times:
* Select an index $i$ ($1 \leq i \leq n$) such that $b_i = 1$. Set $a_i = a_i + 1$ (i.e., increase $a_i$ by $1$).
Your score is defined to be $\max\limits_{i = 1}^{n} \left( a_i + \operatorname{median}(c_i) \right)$, where $c_i$ denotes the array of length $n-1$ that you get by deleting $a_i$ from $a$. In other words, your score is the maximum value of $a_i + \operatorname{median}(c_i)$ over all $i$ from $1$ to $n$.
Find the maximum score that you can achieve if you perform the operations optimally.
For an arbitrary array $p$, $\operatorname{median}(p)$ is defined as the $\left\lfloor \frac{|p|+1}{2} \right\rfloor$-th smallest element of $p$. For example, $\operatorname{median} \left( [3,2,1,3] \right) = 2$ and $\operatorname{median} \left( [6,2,4,5,1] \right) = 4$.
Input: The first line contains an integer $t$ ($1 \leq t \leq 10^4$) β the number of test cases.
Each test case begins with two integers $n$ and $k$ ($2 \leq n \leq 2 \cdot 10^5$, $0 \leq k \leq 10^9$) β the length of the $a$ and the number of operations you can perform.
The following line contains $n$ space separated integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 10^9$) β denoting the array $a$.
The following line contains $n$ space separated integers $b_1, b_2, \ldots, b_n$ ($b_i$ is $0$ or $1$) β denoting the array $b$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
Output: For each test case, output the maximum value of score you can get on a new line.
Examples:
input:
8
2 10
3 3
1 1
3 10
3 3 3
0 0 0
4 4
2 1 5 1
0 1 0 1
5 4
7 5 2 5 4
0 0 1 0 1
5 1
5 15 15 2 11
1 0 0 1 1
5 2
10 11 4 10 15
1 1 0 1 0
4 4
1 1 2 5
1 1 0 0
2 1000000000
1000000000 1000000000
1 1
output:
16
6
8
13
21
26
8
3000000000
Note: For the first test case, it is optimal to perform $5$ operations on both elements so $a = [8,8]$. So, the maximum score we can achieve is $\max(8 + \operatorname{median}[8], 8 + \operatorname{median}[8]) = 16$, as $c_1 = [a_2] = [8]$. It can be proven that you cannot get a better score.
For the second test case, you are not able to perform operations on any elements, so $a$ remains $[3,3,3]$. So, the maximum score we can achieve is $3 + \operatorname{median}[3, 3] = 6$, as $c_1 = [a_2, a_3] = [3, 3]$. It can be proven that you cannot get a better score.
|
1987F2
|
https://codeforces.com/problemset/problem/1987/F2
|
Interesting Problem (Hard Version)
| 2,600
|
[
"dp"
] |
Div. 1 + 2
| 2,000
| 256
|
This is the hard version of the problem. The only difference between the two versions is the constraint on $n$. You can make hacks only if both versions of the problem are solved.
You are given an array of integers $a$ of length $n$.
In one operation, you will perform the following two-step process:
1. Choose an index $i$ such that $1 \le i < |a|$ and $a_i = i$. 2. Remove $a_i$ and $a_{i+1}$ from the array and concatenate the remaining parts.
Find the maximum number of times that you can perform the operation above.
|
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 100$) β 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 800$) β 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 the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $800$.
|
For each test case, output a single integer β the maximum number of times that you can perform the operation.
|
[
[
"6\n5\n1 5 3 2 4\n8\n2 1 3 4 5 6 7 8\n3\n1 2 3\n4\n1 2 4 4\n5\n4 4 1 3 5\n1\n1",
"2\n3\n1\n2\n0\n0"
]
] |
In the first test case, one possible optimal sequence of operations is $[ 1, 5, \color{red}{3}, \color{red}{2}, 4 ] \rightarrow [\color{red}{1}, \color{red}{5}, 4] \rightarrow [4]$.
In the third test case, one possible optimal sequence of operations is $[1, \color{red}{2}, \color{red}{3}] \rightarrow [1]$.
|
Title: Interesting Problem (Hard Version)
time_limit_ms: 2000
memory_limit_mb: 256
Description: This is the hard version of the problem. The only difference between the two versions is the constraint on $n$. You can make hacks only if both versions of the problem are solved.
You are given an array of integers $a$ of length $n$.
In one operation, you will perform the following two-step process:
1. Choose an index $i$ such that $1 \le i < |a|$ and $a_i = i$. 2. Remove $a_i$ and $a_{i+1}$ from the array and concatenate the remaining parts.
Find the maximum number of times that you can perform the operation above.
Input: Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 100$) β 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 800$) β 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 the array $a$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $800$.
Output: For each test case, output a single integer β the maximum number of times that you can perform the operation.
Examples:
input:
6
5
1 5 3 2 4
8
2 1 3 4 5 6 7 8
3
1 2 3
4
1 2 4 4
5
4 4 1 3 5
1
1
output:
2
3
1
2
0
0
Note: In the first test case, one possible optimal sequence of operations is $[ 1, 5, \color{red}{3}, \color{red}{2}, 4 ] \rightarrow [\color{red}{1}, \color{red}{5}, 4] \rightarrow [4]$.
In the third test case, one possible optimal sequence of operations is $[1, \color{red}{2}, \color{red}{3}] \rightarrow [1]$.
|
1979B
|
https://codeforces.com/problemset/problem/1979/B
|
XOR Sequences
| 1,000
|
[
"bitmasks",
"greedy"
] |
Div. 2
| 1,000
| 256
|
You are given two distinct non-negative integers $x$ and $y$. Consider two infinite sequences $a_1, a_2, a_3, \ldots$ and $b_1, b_2, b_3, \ldots$, where
* $a_n = n \oplus x$; * $b_n = n \oplus y$.
Here, $x \oplus y$ denotes the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) operation of integers $x$ and $y$.
For example, with $x = 6$, the first $8$ elements of sequence $a$ will look as follows: $[7, 4, 5, 2, 3, 0, 1, 14, \ldots]$. Note that the indices of elements start with $1$.
Your task is to find the length of the longest common subsegment$^\dagger$ of sequences $a$ and $b$. In other words, find the maximum integer $m$ such that $a_i = b_j, a_{i + 1} = b_{j + 1}, \ldots, a_{i + m - 1} = b_{j + m - 1}$ for some $i, j \ge 1$.
$^\dagger$A subsegment of sequence $p$ is a sequence $p_l,p_{l+1},\ldots,p_r$, where $1 \le l \le r$.
|
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The only line of each test case contains two integers $x$ and $y$ ($0 \le x, y \le 10^9, x \neq y$) β the parameters of the sequences.
|
For each test case, output a single integer β the length of the longest common subsegment.
|
[
[
"4\n0 1\n12 4\n57 37\n316560849 14570961",
"1\n8\n4\n33554432"
]
] |
In the first test case, the first $7$ elements of sequences $a$ and $b$ are as follows:
$a = [1, 2, 3, 4, 5, 6, 7,\ldots]$
$b = [0, 3, 2, 5, 4, 7, 6,\ldots]$
It can be shown that there isn't a positive integer $k$ such that the sequence $[k, k + 1]$ occurs in $b$ as a subsegment. So the answer is $1$.
In the third test case, the first $20$ elements of sequences $a$ and $b$ are as follows:
$a = [56, 59, 58, 61, 60, 63, 62, 49, 48, 51, 50, 53, 52, 55, 54, \textbf{41, 40, 43, 42}, 45, \ldots]$
$b = [36, 39, 38, 33, 32, 35, 34, 45, 44, 47, 46, \textbf{41, 40, 43, 42}, 53, 52, 55, 54, 49, \ldots]$
It can be shown that one of the longest common subsegments is the subsegment $[41, 40, 43, 42]$ with a length of $4$.
|
Title: XOR Sequences
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given two distinct non-negative integers $x$ and $y$. Consider two infinite sequences $a_1, a_2, a_3, \ldots$ and $b_1, b_2, b_3, \ldots$, where
* $a_n = n \oplus x$; * $b_n = n \oplus y$.
Here, $x \oplus y$ denotes the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) operation of integers $x$ and $y$.
For example, with $x = 6$, the first $8$ elements of sequence $a$ will look as follows: $[7, 4, 5, 2, 3, 0, 1, 14, \ldots]$. Note that the indices of elements start with $1$.
Your task is to find the length of the longest common subsegment$^\dagger$ of sequences $a$ and $b$. In other words, find the maximum integer $m$ such that $a_i = b_j, a_{i + 1} = b_{j + 1}, \ldots, a_{i + m - 1} = b_{j + m - 1}$ for some $i, j \ge 1$.
$^\dagger$A subsegment of sequence $p$ is a sequence $p_l,p_{l+1},\ldots,p_r$, where $1 \le l \le r$.
Input: Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) β the number of test cases. The description of the test cases follows.
The only line of each test case contains two integers $x$ and $y$ ($0 \le x, y \le 10^9, x \neq y$) β the parameters of the sequences.
Output: For each test case, output a single integer β the length of the longest common subsegment.
Examples:
input:
4
0 1
12 4
57 37
316560849 14570961
output:
1
8
4
33554432
Note: In the first test case, the first $7$ elements of sequences $a$ and $b$ are as follows:
$a = [1, 2, 3, 4, 5, 6, 7,\ldots]$
$b = [0, 3, 2, 5, 4, 7, 6,\ldots]$
It can be shown that there isn't a positive integer $k$ such that the sequence $[k, k + 1]$ occurs in $b$ as a subsegment. So the answer is $1$.
In the third test case, the first $20$ elements of sequences $a$ and $b$ are as follows:
$a = [56, 59, 58, 61, 60, 63, 62, 49, 48, 51, 50, 53, 52, 55, 54, \textbf{41, 40, 43, 42}, 45, \ldots]$
$b = [36, 39, 38, 33, 32, 35, 34, 45, 44, 47, 46, \textbf{41, 40, 43, 42}, 53, 52, 55, 54, 49, \ldots]$
It can be shown that one of the longest common subsegments is the subsegment $[41, 40, 43, 42]$ with a length of $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.
|
Title: Longest Max Min Subsequence
time_limit_ms: 2000
memory_limit_mb: 256
Description: 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$.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 5 \cdot 10^4$). The description of the test cases follows.
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$.
Output: 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$.
Examples:
input:
4
4
3 2 1 3
4
1 1 1 1
9
3 2 1 3 2 1 3 2 1
1
1
output:
3
3 2 1
1
1
3
3 1 2
1
1
input:
10
2
1 2
10
5 2 1 7 9 7 2 5 5 2
2
1 2
10
2 2 8 7 7 9 8 1 9 6
9
9 1 7 5 8 5 6 4 1
3
3 3 3
6
1 6 4 4 6 5
6
3 4 4 5 3 3
10
4 1 4 5 4 5 10 1 5 1
7
1 2 1 3 2 4 6
output:
2
1 2
5
5 1 9 7 2
2
1 2
6
2 7 9 8 1 6
7
9 1 7 5 8 6 4
1
3
4
1 4 6 5
3
4 5 3
4
5 4 10 1
5
2 1 3 4 6
Note: 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.
|
2029A
|
https://codeforces.com/problemset/problem/2029/A
|
Set
| 800
|
[
"binary search",
"greedy",
"math"
] |
Div. 1 + 2
| 1,000
| 256
|
You are given a positive integer $k$ and a set $S$ of all integers from $l$ to $r$ (inclusive).
You can perform the following two-step operation any number of times (possibly zero):
1. First, choose a number $x$ from the set $S$, such that there are at least $k$ multiples of $x$ in $S$ (including $x$ itself); 2. Then, remove $x$ from $S$ (note that nothing else is removed).
Find the maximum possible number of operations that can be performed.
|
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 10^4$) β the number of test cases. The description of test cases follows.
The only line of each test case contains three integers $l$, $r$, and $k$ ($1\le l\le r\leq 10^9$, $1\leq k\le r-l+1$) β the minimum integer in $S$, the maximum integer in $S$, and the parameter $k$.
|
For each test case, output a single integer β the maximum possible number of operations that can be performed.
|
[
[
"8\n3 9 2\n4 9 1\n7 9 2\n2 10 2\n154 220 2\n147 294 2\n998 24435 3\n1 1000000000 2",
"2\n6\n0\n4\n0\n1\n7148\n500000000"
]
] |
In the first test case, initially, $S = \\{3,4,5,6,7,8,9\\}$. One possible optimal sequence of operations is:
1. Choose $x = 4$ for the first operation, since there are two multiples of $4$ in $S$: $4$ and $8$. $S$ becomes equal to $\\{3,5,6,7,8,9\\}$; 2. Choose $x = 3$ for the second operation, since there are three multiples of $3$ in $S$: $3$, $6$, and $9$. $S$ becomes equal to $\\{5,6,7,8,9\\}$.
In the second test case, initially, $S=\\{4,5,6,7,8,9\\}$. One possible optimal sequence of operations is:
1. Choose $x = 5$, $S$ becomes equal to $\\{4,6,7,8,9\\}$; 2. Choose $x = 6$, $S$ becomes equal to $\\{4,7,8,9\\}$; 3. Choose $x = 4$, $S$ becomes equal to $\\{7,8,9\\}$; 4. Choose $x = 8$, $S$ becomes equal to $\\{7,9\\}$; 5. Choose $x = 7$, $S$ becomes equal to $\\{9\\}$; 6. Choose $x = 9$, $S$ becomes equal to $\\{\\}$.
In the third test case, initially, $S=\\{7,8,9\\}$. For each $x$ in $S$, no multiple of $x$ other than $x$ itself can be found in $S$. Since $k = 2$, you can perform no operations.
In the fourth test case, initially, $S=\\{2,3,4,5,6,7,8,9,10\\}$. One possible optimal sequence of operations is:
1. Choose $x = 2$, $S$ becomes equal to $\\{3,4,5,6,7,8,9,10\\}$; 2. Choose $x = 4$, $S$ becomes equal to $\\{3,5,6,7,8,9,10\\}$; 3. Choose $x = 3$, $S$ becomes equal to $\\{5,6,7,8,9,10\\}$; 4. Choose $x = 5$, $S$ becomes equal to $\\{6,7,8,9,10\\}$.
|
Title: Set
time_limit_ms: 1000
memory_limit_mb: 256
Description: You are given a positive integer $k$ and a set $S$ of all integers from $l$ to $r$ (inclusive).
You can perform the following two-step operation any number of times (possibly zero):
1. First, choose a number $x$ from the set $S$, such that there are at least $k$ multiples of $x$ in $S$ (including $x$ itself); 2. Then, remove $x$ from $S$ (note that nothing else is removed).
Find the maximum possible number of operations that can be performed.
Input: Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 10^4$) β the number of test cases. The description of test cases follows.
The only line of each test case contains three integers $l$, $r$, and $k$ ($1\le l\le r\leq 10^9$, $1\leq k\le r-l+1$) β the minimum integer in $S$, the maximum integer in $S$, and the parameter $k$.
Output: For each test case, output a single integer β the maximum possible number of operations that can be performed.
Examples:
input:
8
3 9 2
4 9 1
7 9 2
2 10 2
154 220 2
147 294 2
998 24435 3
1 1000000000 2
output:
2
6
0
4
0
1
7148
500000000
Note: In the first test case, initially, $S = \\{3,4,5,6,7,8,9\\}$. One possible optimal sequence of operations is:
1. Choose $x = 4$ for the first operation, since there are two multiples of $4$ in $S$: $4$ and $8$. $S$ becomes equal to $\\{3,5,6,7,8,9\\}$; 2. Choose $x = 3$ for the second operation, since there are three multiples of $3$ in $S$: $3$, $6$, and $9$. $S$ becomes equal to $\\{5,6,7,8,9\\}$.
In the second test case, initially, $S=\\{4,5,6,7,8,9\\}$. One possible optimal sequence of operations is:
1. Choose $x = 5$, $S$ becomes equal to $\\{4,6,7,8,9\\}$; 2. Choose $x = 6$, $S$ becomes equal to $\\{4,7,8,9\\}$; 3. Choose $x = 4$, $S$ becomes equal to $\\{7,8,9\\}$; 4. Choose $x = 8$, $S$ becomes equal to $\\{7,9\\}$; 5. Choose $x = 7$, $S$ becomes equal to $\\{9\\}$; 6. Choose $x = 9$, $S$ becomes equal to $\\{\\}$.
In the third test case, initially, $S=\\{7,8,9\\}$. For each $x$ in $S$, no multiple of $x$ other than $x$ itself can be found in $S$. Since $k = 2$, you can perform no operations.
In the fourth test case, initially, $S=\\{2,3,4,5,6,7,8,9,10\\}$. One possible optimal sequence of operations is:
1. Choose $x = 2$, $S$ becomes equal to $\\{3,4,5,6,7,8,9,10\\}$; 2. Choose $x = 4$, $S$ becomes equal to $\\{3,5,6,7,8,9,10\\}$; 3. Choose $x = 3$, $S$ becomes equal to $\\{5,6,7,8,9,10\\}$; 4. Choose $x = 5$, $S$ becomes equal to $\\{6,7,8,9,10\\}$.
|
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.
|
Title: All Pairs Segments
time_limit_ms: 1500
memory_limit_mb: 256
Description: [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.
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
The first line of each test case contains two integers $n$, $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$.
Output: For each test case, output a single line with $q$ integers: the $i$-th integer is the answer to the $i$-th query.
Examples:
input:
3
2 2
101 200
2 1
6 15
1 2 3 5 6 7
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
5 8
254618033 265675151 461318786 557391198 848083778
6 9 15 10 6 9 4 4294967300
output:
0 100
0 0 0 0 2 0 0 0 3 0 2 0 0 0 0
291716045 0 0 0 291716045 0 301749698 0
Note: 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.
|
1976E
|
https://codeforces.com/problemset/problem/1976/E
|
Splittable Permutations
| 2,500
|
[
"combinatorics",
"data structures",
"dfs and similar",
"greedy",
"math",
"trees"
] |
Div. 2
| 2,000
| 512
|
Initially, we had one array, which was a permutation of size $n$ (an array of size $n$ where each integer from $1$ to $n$ appears exactly once).
We performed $q$ operations. During the $i$-th operation, we did the following:
* choose any array we have with at least $2$ elements; * split it into two non-empty arrays (prefix and suffix); * write two integers $l_i$ and $r_i$, where $l_i$ is the maximum element in the left part which we get after the split, and $r_i$ is the maximum element in the right part; * remove the array we've chosen from the pool of arrays we can use, and add the two resulting parts into the pool.
For example, suppose the initial array was $[6, 3, 4, 1, 2, 5]$, and we performed the following operations:
1. choose the array $[6, 3, 4, 1, 2, 5]$ and split it into $[6, 3]$ and $[4, 1, 2, 5]$. Then we write $l_1 = 6$ and $r_1 = 5$, and the arrays we have are $[6, 3]$ and $[4, 1, 2, 5]$; 2. choose the array $[4, 1, 2, 5]$ and split it into $[4, 1, 2]$ and $[5]$. Then we write $l_2 = 4$ and $r_2 = 5$, and the arrays we have are $[6, 3]$, $[4, 1, 2]$ and $[5]$; 3. choose the array $[4, 1, 2]$ and split it into $[4]$ and $[1, 2]$. Then we write $l_3 = 4$ and $r_3 = 2$, and the arrays we have are $[6, 3]$, $[4]$, $[1, 2]$ and $[5]$.
You are given two integers $n$ and $q$, and two sequences $[l_1, l_2, \dots, l_q]$ and $[r_1, r_2, \dots, r_q]$. A permutation of size $n$ is called valid if we can perform $q$ operations and produce the given sequences $[l_1, l_2, \dots, l_q]$ and $[r_1, r_2, \dots, r_q]$.
Calculate the number of valid permutations.
|
The first line contains two integers $n$ and $q$ ($1 \le q < n \le 3 \cdot 10^5$).
The second line contains $q$ integers $l_1, l_2, \dots, l_q$ ($1 \le l_i \le n$).
The third line contains $q$ integers $r_1, r_2, \dots, r_q$ ($1 \le r_i \le n$).
Additional constraint on the input: there exists at least one permutation which can produce the given sequences $[l_1, l_2, \dots, l_q]$ and $[r_1, r_2, \dots, r_q]$.
|
Print one integer β the number of valid permutations, taken modulo $998244353$.
|
[
[
"6 3\n6 4 4\n5 5 2",
"30"
],
[
"10 1\n10\n9",
"1814400"
],
[
"4 1\n2\n4",
"8"
]
] |
Title: Splittable Permutations
time_limit_ms: 2000
memory_limit_mb: 512
Description: Initially, we had one array, which was a permutation of size $n$ (an array of size $n$ where each integer from $1$ to $n$ appears exactly once).
We performed $q$ operations. During the $i$-th operation, we did the following:
* choose any array we have with at least $2$ elements; * split it into two non-empty arrays (prefix and suffix); * write two integers $l_i$ and $r_i$, where $l_i$ is the maximum element in the left part which we get after the split, and $r_i$ is the maximum element in the right part; * remove the array we've chosen from the pool of arrays we can use, and add the two resulting parts into the pool.
For example, suppose the initial array was $[6, 3, 4, 1, 2, 5]$, and we performed the following operations:
1. choose the array $[6, 3, 4, 1, 2, 5]$ and split it into $[6, 3]$ and $[4, 1, 2, 5]$. Then we write $l_1 = 6$ and $r_1 = 5$, and the arrays we have are $[6, 3]$ and $[4, 1, 2, 5]$; 2. choose the array $[4, 1, 2, 5]$ and split it into $[4, 1, 2]$ and $[5]$. Then we write $l_2 = 4$ and $r_2 = 5$, and the arrays we have are $[6, 3]$, $[4, 1, 2]$ and $[5]$; 3. choose the array $[4, 1, 2]$ and split it into $[4]$ and $[1, 2]$. Then we write $l_3 = 4$ and $r_3 = 2$, and the arrays we have are $[6, 3]$, $[4]$, $[1, 2]$ and $[5]$.
You are given two integers $n$ and $q$, and two sequences $[l_1, l_2, \dots, l_q]$ and $[r_1, r_2, \dots, r_q]$. A permutation of size $n$ is called valid if we can perform $q$ operations and produce the given sequences $[l_1, l_2, \dots, l_q]$ and $[r_1, r_2, \dots, r_q]$.
Calculate the number of valid permutations.
Input: The first line contains two integers $n$ and $q$ ($1 \le q < n \le 3 \cdot 10^5$).
The second line contains $q$ integers $l_1, l_2, \dots, l_q$ ($1 \le l_i \le n$).
The third line contains $q$ integers $r_1, r_2, \dots, r_q$ ($1 \le r_i \le n$).
Additional constraint on the input: there exists at least one permutation which can produce the given sequences $[l_1, l_2, \dots, l_q]$ and $[r_1, r_2, \dots, r_q]$.
Output: Print one integer β the number of valid permutations, taken modulo $998244353$.
Examples:
input:
6 3
6 4 4
5 5 2
output:
30
input:
10 1
10
9
output:
1814400
input:
4 1
2
4
output:
8
Note:
|
|
2025B
|
https://codeforces.com/problemset/problem/2025/B
|
Binomial Coefficients, Kind Of
| 1,100
|
[
"combinatorics",
"dp",
"math"
] |
Div. 2
| 2,000
| 512
|
Recently, akshiM met a task that needed binomial coefficients to solve. He wrote a code he usually does that looked like this:
for (int n = 0; n < N; n++) { // loop over n from 0 to N-1 (inclusive) C[n][0] = 1; C[n][n] = 1; for (int k = 1; k < n; k++) // loop over k from 1 to n-1 (inclusive) C[n][k] = C[n][k - 1] + C[n - 1][k - 1]; }
Unfortunately, he made an error, since the right formula is the following:
C[n][k] = C[n - 1][k] + C[n - 1][k - 1]
But his team member keblidA is interested in values that were produced using the wrong formula. Please help him to calculate these coefficients for $t$ various pairs $(n_i, k_i)$. Note that they should be calculated according to the first (wrong) formula.
Since values $C[n_i][k_i]$ may be too large, print them modulo $10^9 + 7$.
|
The first line contains a single integer $t$ ($1 \le t \le 10^5$) β the number of pairs. Next, $t$ pairs are written in two lines.
The second line contains $t$ integers $n_1, n_2, \dots, n_t$ ($2 \le n_i \le 10^5$).
The third line contains $t$ integers $k_1, k_2, \dots, k_t$ ($1 \le k_i < n_i$).
|
Print $t$ integers $C[n_i][k_i]$ modulo $10^9 + 7$.
|
[
[
"7\n2 5 5 100000 100000 100000 100000\n1 2 3 1 33333 66666 99999",
"2\n4\n8\n2\n326186014\n984426998\n303861760"
]
] |
Title: Binomial Coefficients, Kind Of
time_limit_ms: 2000
memory_limit_mb: 512
Description: Recently, akshiM met a task that needed binomial coefficients to solve. He wrote a code he usually does that looked like this:
for (int n = 0; n < N; n++) { // loop over n from 0 to N-1 (inclusive) C[n][0] = 1; C[n][n] = 1; for (int k = 1; k < n; k++) // loop over k from 1 to n-1 (inclusive) C[n][k] = C[n][k - 1] + C[n - 1][k - 1]; }
Unfortunately, he made an error, since the right formula is the following:
C[n][k] = C[n - 1][k] + C[n - 1][k - 1]
But his team member keblidA is interested in values that were produced using the wrong formula. Please help him to calculate these coefficients for $t$ various pairs $(n_i, k_i)$. Note that they should be calculated according to the first (wrong) formula.
Since values $C[n_i][k_i]$ may be too large, print them modulo $10^9 + 7$.
Input: The first line contains a single integer $t$ ($1 \le t \le 10^5$) β the number of pairs. Next, $t$ pairs are written in two lines.
The second line contains $t$ integers $n_1, n_2, \dots, n_t$ ($2 \le n_i \le 10^5$).
The third line contains $t$ integers $k_1, k_2, \dots, k_t$ ($1 \le k_i < n_i$).
Output: Print $t$ integers $C[n_i][k_i]$ modulo $10^9 + 7$.
Examples:
input:
7
2 5 5 100000 100000 100000 100000
1 2 3 1 33333 66666 99999
output:
2
4
8
2
326186014
984426998
303861760
Note:
|
|
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.
|
Title: Turtle and Three Sequences
time_limit_ms: 3000
memory_limit_mb: 256
Description: 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.
Input: 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: 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$.
Examples:
input:
4 2
2 3 4 2
1 3 3 2
1 4 2 3
output:
5
input:
7 3
1 4 5 2 3 6 7
1 2 2 1 1 3 2
1 5 6 7 3 2 4
output:
13
input:
5 3
1 2 3 4 5
1 1 2 1 2
5 4 3 2 1
output:
-1
Note: 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.
|
1983D
|
https://codeforces.com/problemset/problem/1983/D
|
Swap Dilemma
| 1,700
|
[
"constructive algorithms",
"data structures",
"divide and conquer",
"greedy",
"math",
"sortings"
] |
Div. 2
| 1,000
| 256
|
Given two arrays of distinct positive integers $a$ and $b$ of length $n$, we would like to make both the arrays the same. Two arrays $x$ and $y$ of length $k$ are said to be the same when for all $1 \le i \le k$, $x_i = y_i$.
Now in one move, you can choose some index $l$ and $r$ in $a$ ($l \le r$) and swap $a_l$ and $a_r$, then choose some $p$ and $q$ ($p \le q$) in $b$ such that $r-l=q-p$ and swap $b_p$ and $b_q$.
Is it possible to make both arrays the same?
|
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2 \cdot 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 10^5$) β the length of the arrays $a$ and $b$.
The second line of each test case contains $n$ distinct integers $a_1,a_2,a_3,\ldots,a_n$ ($1 \le a_i \le 2 \cdot 10^5$) β the integers in the array $a$.
The third line of each test case contains $n$ distinct integers $b_1,b_2,b_3,\ldots,b_n$ ($1 \le b_i \le 2 \cdot 10^5$) β the integers in the array $b$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
|
For each testcase, print "YES" if the arrays $a$ and $b$ can be made the same. Otherwise, print "NO". 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.
|
[
[
"6\n4\n1 2 3 4\n1 2 3 4\n5\n1 3 4 2 5\n7 1 2 5 4\n4\n1 2 3 4\n4 3 2 1\n3\n1 2 3\n1 3 2\n5\n1 5 7 1000 4\n4 1 7 5 1000\n3\n1 4 2\n1 3 2",
"YES\nNO\nYES\nNO\nNO\nNO"
]
] |
In the first testcase, you don't need to perform any operations since the arrays are same.
In the second testcase, it can be proven there exists no way to make the arrays same.
In the third testcase, one of the ways to make the arrays same is to first choose $l=1$, $r=3$, $p=1$, $q=3$ then choose $l=1$, $r=2$, $p=3$, $q=4$.
|
Title: Swap Dilemma
time_limit_ms: 1000
memory_limit_mb: 256
Description: Given two arrays of distinct positive integers $a$ and $b$ of length $n$, we would like to make both the arrays the same. Two arrays $x$ and $y$ of length $k$ are said to be the same when for all $1 \le i \le k$, $x_i = y_i$.
Now in one move, you can choose some index $l$ and $r$ in $a$ ($l \le r$) and swap $a_l$ and $a_r$, then choose some $p$ and $q$ ($p \le q$) in $b$ such that $r-l=q-p$ and swap $b_p$ and $b_q$.
Is it possible to make both arrays the same?
Input: Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 2 \cdot 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 10^5$) β the length of the arrays $a$ and $b$.
The second line of each test case contains $n$ distinct integers $a_1,a_2,a_3,\ldots,a_n$ ($1 \le a_i \le 2 \cdot 10^5$) β the integers in the array $a$.
The third line of each test case contains $n$ distinct integers $b_1,b_2,b_3,\ldots,b_n$ ($1 \le b_i \le 2 \cdot 10^5$) β the integers in the array $b$.
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
Output: For each testcase, print "YES" if the arrays $a$ and $b$ can be made the same. Otherwise, print "NO". 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.
Examples:
input:
6
4
1 2 3 4
1 2 3 4
5
1 3 4 2 5
7 1 2 5 4
4
1 2 3 4
4 3 2 1
3
1 2 3
1 3 2
5
1 5 7 1000 4
4 1 7 5 1000
3
1 4 2
1 3 2
output:
YES
NO
YES
NO
NO
NO
Note: In the first testcase, you don't need to perform any operations since the arrays are same.
In the second testcase, it can be proven there exists no way to make the arrays same.
In the third testcase, one of the ways to make the arrays same is to first choose $l=1$, $r=3$, $p=1$, $q=3$ then choose $l=1$, $r=2$, $p=3$, $q=4$.
|
1995A
|
https://codeforces.com/problemset/problem/1995/A
|
Diagonals
| 800
|
[
"brute force",
"greedy",
"implementation",
"math"
] |
Div. 2
| 1,000
| 256
|
Vitaly503 is given a checkered board with a side of $n$ and $k$ chips. He realized that all these $k$ chips need to be placed on the cells of the board (no more than one chip can be placed on a single cell).
Let's denote the cell in the $i$-th row and $j$-th column as $(i ,j)$. A diagonal is the set of cells for which the value $i + j$ is the same. For example, cells $(3, 1)$, $(2, 2)$, and $(1, 3)$ lie on the same diagonal, but $(1, 2)$ and $(2, 3)$ do not. A diagonal is called occupied if it contains at least one chip.
Determine what is the minimum possible number of occupied diagonals among all placements of $k$ chips.
|
Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of sets of input data. Then follow the descriptions of the sets of input data.
The only line of each set of input data contains two integers $n$, $k$ ($1 \le n \le 100, 0 \le k \le n^2$) β the side of the checkered board and the number of available chips, respectively.
|
For each set of input data, output a single integer β the minimum number of occupied diagonals with at least one chip that he can get after placing all $k$ chips.
|
[
[
"7\n1 0\n2 2\n2 3\n2 4\n10 50\n100 239\n3 9",
"0\n1\n2\n3\n6\n3\n5"
]
] |
In the first test case, there are no chips, so 0 diagonals will be occupied. In the second test case, both chips can be placed on diagonal $(2, 1), (1, 2)$, so the answer is 1. In the third test case, 3 chips can't be placed on one diagonal, but placing them on $(1, 2), (2, 1), (1, 1)$ makes 2 diagonals occupied. In the 7th test case, chips will occupy all 5 diagonals in any valid placing.
|
Title: Diagonals
time_limit_ms: 1000
memory_limit_mb: 256
Description: Vitaly503 is given a checkered board with a side of $n$ and $k$ chips. He realized that all these $k$ chips need to be placed on the cells of the board (no more than one chip can be placed on a single cell).
Let's denote the cell in the $i$-th row and $j$-th column as $(i ,j)$. A diagonal is the set of cells for which the value $i + j$ is the same. For example, cells $(3, 1)$, $(2, 2)$, and $(1, 3)$ lie on the same diagonal, but $(1, 2)$ and $(2, 3)$ do not. A diagonal is called occupied if it contains at least one chip.
Determine what is the minimum possible number of occupied diagonals among all placements of $k$ chips.
Input: Each test consists of several sets of input data. The first line contains a single integer $t$ ($1 \le t \le 500$) β the number of sets of input data. Then follow the descriptions of the sets of input data.
The only line of each set of input data contains two integers $n$, $k$ ($1 \le n \le 100, 0 \le k \le n^2$) β the side of the checkered board and the number of available chips, respectively.
Output: For each set of input data, output a single integer β the minimum number of occupied diagonals with at least one chip that he can get after placing all $k$ chips.
Examples:
input:
7
1 0
2 2
2 3
2 4
10 50
100 239
3 9
output:
0
1
2
3
6
3
5
Note: In the first test case, there are no chips, so 0 diagonals will be occupied. In the second test case, both chips can be placed on diagonal $(2, 1), (1, 2)$, so the answer is 1. In the third test case, 3 chips can't be placed on one diagonal, but placing them on $(1, 2), (2, 1), (1, 1)$ makes 2 diagonals occupied. In the 7th test case, chips will occupy all 5 diagonals in any valid placing.
|
1971E
|
https://codeforces.com/problemset/problem/1971/E
|
Find the Car
| 1,500
|
[
"binary search",
"math",
"sortings"
] |
Div. 4
| 3,000
| 256
|
Timur is in a car traveling on the number line from point $0$ to point $n$. The car starts moving from point $0$ at minute $0$.
There are $k+1$ signs on the line at points $0, a_1, a_2, \dots, a_k$, and Timur knows that the car will arrive there at minutes $0, b_1, b_2, \dots, b_k$, respectively. The sequences $a$ and $b$ are strictly increasing with $a_k = n$.

Between any two adjacent signs, the car travels with a constant speed. Timur has $q$ queries: each query will be an integer $d$, and Timur wants you to output how many minutes it takes the car to reach point $d$, rounded down to the nearest integer.
|
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$, $k$, and $q$, ($k \leq n \leq 10^9$; $1 \leq k, q \leq 10^5$) β the final destination, the number of points Timur knows the time for, and the number of queries respectively.
The second line of each test case contains $k$ integers $a_i$ ($1 \leq a_i \leq n$; $a_i < a_{i+1}$ for every $1 \leq i \leq k-1$; $a_k = n$).
The third line of each test case contains $k$ integers $b_i$ ($1 \leq b_i \leq 10^9$; $b_i < b_{i+1}$ for every $1 \leq i \leq k-1$).
Each of the following $q$ lines contains a single integer $d$ ($0 \leq d \leq n$) β the distance that Timur asks the minutes passed for.
The sum of $k$ over all test cases doesn't exceed $10^5$, and the sum of $q$ over all test cases doesn't exceed $10^5$.
|
For each query, output a single integer β the number of minutes passed until the car reaches the point $d$, rounded down.
|
[
[
"4\n10 1 3\n10\n10\n0\n6\n7\n10 2 4\n4 10\n4 7\n6\n4\n2\n7\n1000000000 1 1\n1000000000\n1000000000\n99999999\n6 1 3\n6\n5\n2\n6\n5",
"0 6 7 \n5 4 2 5 \n99999999 \n1 5 4"
]
] |
For the first test case, the car goes from point $0$ to point $10$ in $10$ minutes, so the speed is $1$ unit per minute and:
* At point $0$, the time will be $0$ minutes. * At point $6$, the time will be $6$ minutes. * At point $7$, the time will be $7$ minutes.
For the second test case, between points $0$ and $4$, the car travels at a speed of $1$ unit per minute and between $4$ and $10$ with a speed of $2$ units per minute and:
* At point $6$, the time will be $5$ minutes. * At point $4$, the time will be $4$ minutes. * At point $2$, the time will be $2$ minutes. * At point $7$, the time will be $5.5$ minutes, so the answer is $5$.
For the fourth test case, the car travels with $1.2$ units per minute, so the answers to the queries are:
* At point $2$, the time will be $1.66\dots$ minutes, so the answer is $1$. * At point $6$, the time will be $5$ minutes. * At point $5$, the time will be $4.16\dots$ minutes, so the answer is $4$.
|
Title: Find the Car
time_limit_ms: 3000
memory_limit_mb: 256
Description: Timur is in a car traveling on the number line from point $0$ to point $n$. The car starts moving from point $0$ at minute $0$.
There are $k+1$ signs on the line at points $0, a_1, a_2, \dots, a_k$, and Timur knows that the car will arrive there at minutes $0, b_1, b_2, \dots, b_k$, respectively. The sequences $a$ and $b$ are strictly increasing with $a_k = n$.

Between any two adjacent signs, the car travels with a constant speed. Timur has $q$ queries: each query will be an integer $d$, and Timur wants you to output how many minutes it takes the car to reach point $d$, rounded down to the nearest integer.
Input: 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$, $k$, and $q$, ($k \leq n \leq 10^9$; $1 \leq k, q \leq 10^5$) β the final destination, the number of points Timur knows the time for, and the number of queries respectively.
The second line of each test case contains $k$ integers $a_i$ ($1 \leq a_i \leq n$; $a_i < a_{i+1}$ for every $1 \leq i \leq k-1$; $a_k = n$).
The third line of each test case contains $k$ integers $b_i$ ($1 \leq b_i \leq 10^9$; $b_i < b_{i+1}$ for every $1 \leq i \leq k-1$).
Each of the following $q$ lines contains a single integer $d$ ($0 \leq d \leq n$) β the distance that Timur asks the minutes passed for.
The sum of $k$ over all test cases doesn't exceed $10^5$, and the sum of $q$ over all test cases doesn't exceed $10^5$.
Output: For each query, output a single integer β the number of minutes passed until the car reaches the point $d$, rounded down.
Examples:
input:
4
10 1 3
10
10
0
6
7
10 2 4
4 10
4 7
6
4
2
7
1000000000 1 1
1000000000
1000000000
99999999
6 1 3
6
5
2
6
5
output:
0 6 7
5 4 2 5
99999999
1 5 4
Note: For the first test case, the car goes from point $0$ to point $10$ in $10$ minutes, so the speed is $1$ unit per minute and:
* At point $0$, the time will be $0$ minutes. * At point $6$, the time will be $6$ minutes. * At point $7$, the time will be $7$ minutes.
For the second test case, between points $0$ and $4$, the car travels at a speed of $1$ unit per minute and between $4$ and $10$ with a speed of $2$ units per minute and:
* At point $6$, the time will be $5$ minutes. * At point $4$, the time will be $4$ minutes. * At point $2$, the time will be $2$ minutes. * At point $7$, the time will be $5.5$ minutes, so the answer is $5$.
For the fourth test case, the car travels with $1.2$ units per minute, so the answers to the queries are:
* At point $2$, the time will be $1.66\dots$ minutes, so the answer is $1$. * At point $6$, the time will be $5$ minutes. * At point $5$, the time will be $4.16\dots$ minutes, so the answer is $4$.
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.