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
2001E1
https://codeforces.com/problemset/problem/2001/E1
Deterministic Heap (Easy Version)
2,400
[ "combinatorics", "dp", "math", "trees" ]
Div. 2
3,000
512
This is the easy version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $n$ and $t$. You can make hacks only if both versions of the problem are solved. Consider a perfect binary tree with size $2^n - 1$, with nodes numbered from $1$ ...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows. The first line of each test case contains three integers $n, k, p$ ($1 \le n, k \le 500$, $10^8 \le p \le 10^9$, $p$ is a prime). It is guaranteed that the sum o...
For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation $\mathrm{add}$ exactly $k$ times, modulo $p$.
[ [ "7\n1 13 998244353\n2 1 998244353\n3 2 998244853\n3 3 998244353\n3 4 100000037\n4 2 100000039\n4 3 100000037", "1\n2\n12\n52\n124\n32\n304" ], [ "1\n500 500 100000007", "76297230" ], [ "6\n87 63 100000037\n77 77 100000039\n100 200 998244353\n200 100 998244353\n32 59 998244853\n1 1 9982...
For the first testcase, there is only one way to generate $a$, and such sequence is a deterministic max-heap, so the answer is $1$. For the second testcase, if we choose $v = 1$ and do the operation, we would have $a = [1, 0, 0]$, and since $a_2 = a_3$, we can choose either of them when doing the first $\mathrm{pop}$ ...
Title: Deterministic Heap (Easy Version) time_limit_ms: 3000 memory_limit_mb: 512 Description: This is the easy version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on $n$ and $t$. You can make hacks only if both versions of the problem...
1972B
https://codeforces.com/problemset/problem/1972/B
Coin Games
900
[ "games" ]
Div. 2
1,000
256
There are $n$ coins on the table forming a circle, and each coin is either facing up or facing down. Alice and Bob take turns to play the following game, and Alice goes first. In each operation, the player chooses a facing-up coin, removes the coin, and flips the two coins that are adjacent to it. If (before the opera...
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 only one positive integer $n$ ($1 \leq n \leq 100$), representing the number of the coins. A string $s$ of length $n$ fol...
For each test case, print "YES" if Alice will win the game, 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.
[ [ "3\n5\nUUDUD\n5\nUDDUD\n2\nUU", "YES\nNO\nNO" ] ]
In the first test case, the game may go as follows. * Alice chooses the first coin and $s$ becomes "DDUU". * Bob chooses the last coin and $s$ becomes "UDD". * Alice chooses the first coin and $s$ becomes "UU". * Bob chooses the first coin and $s$ becomes "U". * Alice chooses the only coin and $s$ become...
Title: Coin Games time_limit_ms: 1000 memory_limit_mb: 256 Description: There are $n$ coins on the table forming a circle, and each coin is either facing up or facing down. Alice and Bob take turns to play the following game, and Alice goes first. In each operation, the player chooses a facing-up coin, removes the coi...
1986G1
https://codeforces.com/problemset/problem/1986/G1
Permutation Problem (Simple Version)
2,200
[ "binary search", "brute force", "combinatorics", "data structures", "math", "number theory" ]
Div. 3
3,000
128
This is a simple version of the problem. The only difference is that in this version $n \leq 10^5$ and the sum of $n$ for all sets of input data does not exceed $10^5$. You are given a permutation $p$ of length $n$. Calculate the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is divisible by $i ...
Each test consists of multiple sets of input data. The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of sets of input data. Then follows their description. The first line of each set of input data contains a single integer $n$ ($1 \leq n \leq 10^5$) β€” the length of the permutation $p$. ...
For each set of input data, output the number of index pairs $1 \leq i < j \leq n$ such that $p_i \cdot p_j$ is divisible by $i \cdot j$ without remainder.
[ [ "6\n1\n1\n2\n1 2\n3\n2 3 1\n5\n2 4 1 3 5\n12\n8 9 7 12 1 10 6 3 2 4 11 5\n15\n1 2 4 6 8 10 12 14 3 9 15 5 7 11 13", "0\n1\n1\n3\n9\n3" ] ]
In the first set of input data, there are no index pairs, as the size of the permutation is $1$. In the second set of input data, there is one index pair $(1, 2)$ and it is valid. In the third set of input data, the index pair $(1, 2)$ is valid. In the fourth set of input data, the index pairs $(1, 2)$, $(1, 5)$, an...
Title: Permutation Problem (Simple Version) time_limit_ms: 3000 memory_limit_mb: 128 Description: This is a simple version of the problem. The only difference is that in this version $n \leq 10^5$ and the sum of $n$ for all sets of input data does not exceed $10^5$. You are given a permutation $p$ of length $n$. Calcu...
2030G2
https://codeforces.com/problemset/problem/2030/G2
The Destruction of the Universe (Hard Version)
3,100
[ "combinatorics", "math" ]
Div. 2
4,000
512
This is the hard version of the problem. In this version, $n \leq 10^6$. You can only make hacks if both versions of the problem are solved. Orangutans are powerful beingsβ€”so powerful that they only need $1$ unit of time to destroy every vulnerable planet in the universe! There are $n$ planets in the universe. Each p...
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 10^6$) β€” the number of planets in the universe. The following $n$ lines contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq n$) β€” the initial i...
For each test case, output an integer β€” the sum of scores to destroy all non- empty subsets of the planets in the universe, modulo $998\,244\,353$.
[ [ "3\n3\n1 1\n2 3\n3 3\n4\n1 4\n2 3\n2 4\n1 1\n5\n1 2\n2 3\n3 4\n4 5\n1 5", "5\n6\n24" ] ]
In the first testcase, there are seven non-empty subsets of planets we must consider: * For each of the subsets $\\{[1,1]\\}, \\{[2,3]\\}, \\{[3,3]\\}$, the score is $0$. * For the subset $\\{[2,3], [3,3]\\}$, the score is $0$, because the point $3$ is already contained in both planets' interval of vulnerability....
Title: The Destruction of the Universe (Hard Version) time_limit_ms: 4000 memory_limit_mb: 512 Description: This is the hard version of the problem. In this version, $n \leq 10^6$. You can only make hacks if both versions of the problem are solved. Orangutans are powerful beingsβ€”so powerful that they only need $1$ uni...
2004C
https://codeforces.com/problemset/problem/2004/C
Splitting Items
1,100
[ "games", "greedy", "sortings" ]
Div. 2
2,000
256
Alice and Bob have $n$ items they'd like to split between them, so they decided to play a game. All items have a cost, and the $i$-th item costs $a_i$. Players move in turns starting from Alice. In each turn, the player chooses one of the remaining items and takes it. The game goes on until no items are left. Let's s...
The first line contains a single integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. Then $t$ cases follow. The first line of each test case contains two integers $n$ and $k$ ($2 \le n \le 2 \cdot 10^5$; $0 \le k \le 10^9$) β€” the number of items and the maximum total increase Bob can make. The second line of...
For each test case, print a single integer β€” the minimum possible score $A - B$ after Bob increases the costs of several (possibly none or all) items.
[ [ "4\n2 5\n1 10\n3 0\n10 15 12\n4 6\n3 1 2 4\n2 4\n6 9", "4\n13\n0\n0" ] ]
In the first test case, Bob can increase $a_1$ by $5$, making costs equal to $[6, 10]$. Tomorrow, Alice will take $10$ and Bob will take $6$. The total score will be equal to $10 - 6 = 4$, and it's the minimum possible. In the second test case, Bob can't change costs. So the score will be equal to $(15 + 10) - 12 = 13...
Title: Splitting Items time_limit_ms: 2000 memory_limit_mb: 256 Description: Alice and Bob have $n$ items they'd like to split between them, so they decided to play a game. All items have a cost, and the $i$-th item costs $a_i$. Players move in turns starting from Alice. In each turn, the player chooses one of the rem...
1986A
https://codeforces.com/problemset/problem/1986/A
X Axis
800
[ "brute force", "geometry", "math", "sortings" ]
Div. 3
2,000
256
You are given three points with integer coordinates $x_1$, $x_2$, and $x_3$ on the $X$ axis ($1 \leq x_i \leq 10$). You can choose any point with an integer coordinate $a$ on the $X$ axis. Note that the point $a$ may coincide with $x_1$, $x_2$, or $x_3$. Let $f(a)$ be the total distance from the given points to the poi...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 10^3$) β€” the number of test cases. Then follows their descriptions. The single line of each test case contains three integers $x_1$, $x_2$, and $x_3$ ($1 \leq x_i \leq 10$) β€” the coordinates of the points.
For each test case, output the smallest value of $f(a)$.
[ [ "8\n1 1 1\n1 5 9\n8 2 8\n10 9 3\n2 1 1\n2 4 1\n7 3 5\n1 9 4", "0\n8\n6\n7\n1\n3\n4\n8" ] ]
In the first test case, the smallest value of $f(a)$ is achieved when $a = 1$: $f(1) = |1 - 1| + |1 - 1| + |1 - 1| = 0$. In the second test case, the smallest value of $f(a)$ is achieved when $a = 5$: $f(5) = |1 - 5| + |5 - 5| + |9 - 5| = 8$. In the third test case, the smallest value of $f(a)$ is achieved when $a = ...
Title: X Axis time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given three points with integer coordinates $x_1$, $x_2$, and $x_3$ on the $X$ axis ($1 \leq x_i \leq 10$). You can choose any point with an integer coordinate $a$ on the $X$ axis. Note that the point $a$ may coincide with $x_1$, $x_2$, or $x_3...
1995E2
https://codeforces.com/problemset/problem/1995/E2
Let Me Teach You a Lesson (Hard Version)
2,900
[ "data structures", "dp", "matrices", "two pointers" ]
Div. 2
2,000
256
This is the hard 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...
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 a single integer $n$ ($1 \le n \le 100\,000$) β€” the number of desks. The second line co...
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 ind...
Title: Let Me Teach You a Lesson (Hard Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: This is the hard 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 l...
2005B1
https://codeforces.com/problemset/problem/2005/B1
The Strict Teacher (Easy Version)
1,000
[ "greedy", "math", "sortings" ]
Div. 2
1,500
256
This is the easy version of the problem. The only differences between the two versions are the constraints on $m$ and $q$. In this version, $m=2$ and $q=1$. You can make hacks only if both versions of the problem are solved. Narek and Tsovak were busy preparing this round, so they have not managed to do their homework...
In the first line of the input, you are given a single integer $t$ ($1 \le t \le 10^5$) β€” the number of test cases. The description of each test case follows. In the first line of each test case, you are given three integers $n$, $m$, and $q$ ($3 \le n \le 10^9$, $m=2$, $q=1$) β€” the number of cells on the line, the nu...
For each test case, output $q$ lines, the $i$-th of them containing the answer of the $i$-th query.
[ [ "3\n10 2 1\n1 4\n2\n8 2 1\n3 6\n1\n8 2 1\n3 6\n8", "1\n2\n2" ] ]
In the first example, the student can just stay at cell $2$. The teacher, initially located in cell $1$, can reach cell $2$ in one move. Therefore, the answer is $1$. In the second example, the student should just stay at cell $1$. The teacher, initially located in cell $3$, can reach cell $1$ in two moves. Therefore,...
Title: The Strict Teacher (Easy Version) time_limit_ms: 1500 memory_limit_mb: 256 Description: This is the easy version of the problem. The only differences between the two versions are the constraints on $m$ and $q$. In this version, $m=2$ and $q=1$. You can make hacks only if both versions of the problem are solved. ...
2033D
https://codeforces.com/problemset/problem/2033/D
Kousuke's Assignment
1,300
[ "data structures", "dp", "dsu", "greedy", "math" ]
Div. 3
2,000
256
After a trip with Sakurako, Kousuke was very scared because he forgot about his programming assignment. In this assignment, the teacher gave him an array $a$ of $n$ integers and asked him to calculate the number of non- overlapping segments of the array $a$, such that each segment is considered beautiful. A segment $[...
The first line of input contains the number $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Each test case consists of $2$ lines. * The first line contains one integer $n$ ($1 \le n \le 10^5$) β€” the length of the array. * The second line contains $n$ integers $a_i$ ($-10^5 \le a_i \le 10^5$) β€” the elements of...
For each test case, output a single integer: the maximum number of non- overlapping beautiful segments.
[ [ "3\n5\n2 1 -3 2 1\n7\n12 -4 4 43 -3 -5 8\n6\n0 -4 0 3 0 1", "1\n2\n3" ] ]
Title: Kousuke's Assignment time_limit_ms: 2000 memory_limit_mb: 256 Description: After a trip with Sakurako, Kousuke was very scared because he forgot about his programming assignment. In this assignment, the teacher gave him an array $a$ of $n$ integers and asked him to calculate the number of non- overlapping segmen...
2022E2
https://codeforces.com/problemset/problem/2022/E2
Billetes MX (Hard Version)
2,600
[ "binary search", "combinatorics", "data structures", "dsu", "graphs" ]
Div. 2
2,000
512
This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved. An integer grid $A$ with $p$ rows and $q$ columns is called beautiful if: * All elements of the grid are integers between $0$ and $2^{30}-1$, and * Fo...
The first line contains $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains four integers $n$, $m$, $k$ and $q$ ($2 \le n, m \le 10^5$; $0 \le k, q \leq 10^5$) β€” the number of rows, the number of columns, the number of fixed cells, and the number of updates. The following $k...
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$.
[ [ "3\n3 3 8 1\n2 1 6\n3 2 12\n1 2 6\n2 2 0\n1 3 10\n1 1 0\n2 3 12\n3 1 10\n3 3 1\n2 5 2 0\n1 1 10\n1 2 30\n2 5 0 2\n1 1 10\n1 2 30", "1\n0\n489373567\n651321892\n769740174\n489373567" ] ]
In the first test case of the example, we initially have the following grid: $0$| $6$| $10$ ---|---|--- $6$| $0$| $12$ $10$| $12$| $?$ It can be proven that the only valid value for tile $(3, 3)$ is $0$, so the first answer is $1$. For the second query, the grid does not satisfy the condition, and thus the ...
Title: Billetes MX (Hard Version) time_limit_ms: 2000 memory_limit_mb: 512 Description: This is the hard version of the problem. In this version, it is guaranteed that $q \leq 10^5$. You can make hacks only if both versions of the problem are solved. An integer grid $A$ with $p$ rows and $q$ columns is called beautifu...
2020D
https://codeforces.com/problemset/problem/2020/D
Connect the Dots
1,800
[ "brute force", "dp", "dsu", "graphs", "math", "trees" ]
Div. 2
2,000
512
One fine evening, Alice sat down to play the classic game "Connect the Dots", but with a twist. To play the game, Alice draws a straight line and marks $n$ points on it, indexed from $1$ to $n$. Initially, there are no arcs between the points, so they are all disjoint. After that, Alice performs $m$ operations of the ...
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 two integers $n$ and $m$ ($1 \le n \le 2 \cdot 10^5$, $1 \le m \le 2 \cdot 10^5$). The $i$-th of the following $m$ lin...
For each test case, output the number of connected components.
[ [ "3\n10 2\n1 2 4\n2 2 4\n100 1\n19 2 4\n100 3\n1 2 5\n7 2 6\n17 2 31", "2\n96\n61" ] ]
In the first test case, there are $n = 10$ points. The first operation joins the points $1$, $3$, $5$, $7$, and $9$. The second operation joins the points $2$, $4$, $6$, $8$, and $10$. There are thus two connected components: $\\{1, 3, 5, 7, 9\\}$ and $\\{2, 4, 6, 8, 10\\}$. In the second test case, there are $n = 100...
Title: Connect the Dots time_limit_ms: 2000 memory_limit_mb: 512 Description: One fine evening, Alice sat down to play the classic game "Connect the Dots", but with a twist. To play the game, Alice draws a straight line and marks $n$ points on it, indexed from $1$ to $n$. Initially, there are no arcs between the point...
2003A
https://codeforces.com/problemset/problem/2003/A
Turtle and Good Strings
800
[ "greedy", "strings" ]
Div. 2
1,000
256
Turtle thinks a string $s$ is a good string if there exists a sequence of strings $t_1, t_2, \ldots, t_k$ ($k$ is an arbitrary integer) such that: * $k \ge 2$. * $s = t_1 + t_2 + \ldots + t_k$, where $+$ represents the concatenation operation. For example, $\texttt{abc} = \texttt{a} + \texttt{bc}$. * For all $...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 100$) β€” the length of the string. The second line of each test case contains a strin...
For each test case, output "YES" if the string $s$ is a good string, and "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
[ [ "4\n2\naa\n3\naba\n4\nabcb\n12\nabcabcabcabc", "No\nnO\nYes\nYES" ] ]
In the first test case, the sequence of strings $\texttt{a}, \texttt{a}$ satisfies the condition $s = t_1 + t_2 + \ldots + t_k$, but the first character of $t_1$ is equal to the last character of $t_2$. It can be seen that there doesn't exist any sequence of strings which satisfies all of the conditions, so the answer ...
Title: Turtle and Good Strings time_limit_ms: 1000 memory_limit_mb: 256 Description: Turtle thinks a string $s$ is a good string if there exists a sequence of strings $t_1, t_2, \ldots, t_k$ ($k$ is an arbitrary integer) such that: * $k \ge 2$. * $s = t_1 + t_2 + \ldots + t_k$, where $+$ represents the concatenat...
1993C
https://codeforces.com/problemset/problem/1993/C
Light Switches
1,400
[ "implementation", "math" ]
Div. 2
2,000
256
There is an apartment consisting of $n$ rooms, each with its light initially turned off. To control the lights in these rooms, the owner of the apartment decided to install chips in the rooms so that each room has exactly one chip, and the chips are installed at different times. Specifically, these times are represent...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 2 \cdot 10^5$) β€” the number of rooms in the apartment and the period of the chips. The second line contains $n$ distinct integers $a_1, a...
For each test case, print a single integer β€” the answer to the question (in minutes). If there is no such moment that the lights are turned on in all the rooms, print $-1$ instead.
[ [ "9\n4 4\n2 3 4 5\n4 3\n2 3 4 5\n4 3\n3 4 8 9\n3 3\n6 2 1\n1 1\n1\n7 5\n14 34 6 25 46 7 17\n6 5\n40 80 99 60 90 50\n6 5\n64 40 50 68 70 10\n2 1\n1 1000000000", "5\n-1\n10\n8\n1\n47\n100\n-1\n-1" ] ]
In the first test case, all lights will be on by the minute $5$ without any of them being turned off by the chips. The answer is $5$. In the second test case, due to $k=3$, the $1$-st light will be on at minutes $2, 3, 4, 8, 9, 10, 14, \ldots$; meanwhile, the $4$-th light will be on at minutes $5, 6, 7, 11, 12, 13, 17...
Title: Light Switches time_limit_ms: 2000 memory_limit_mb: 256 Description: There is an apartment consisting of $n$ rooms, each with its light initially turned off. To control the lights in these rooms, the owner of the apartment decided to install chips in the rooms so that each room has exactly one chip, and the chi...
1971H
https://codeforces.com/problemset/problem/1971/H
Β±1
2,100
[ "2-sat", "dfs and similar", "graphs" ]
Div. 4
2,000
256
Bob has a grid with $3$ rows and $n$ columns, each of which contains either $a_i$ or $-a_i$ for some integer $1 \leq i \leq n$. For example, one possible grid for $n=4$ is shown below: $$\begin{bmatrix} a_1 & -a_2 & -a_3 & -a_2 \\\ -a_4 & a_4 & -a_1 & -a_3 \\\ a_1 & a_2 & -a_2 & a_4 \end{bmatrix}$$ Alice and Bob play...
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2 \leq n \leq 500$) β€” the number of columns of Bob's grid. The next three lines each contain $n$ integers, the $i$-th of which contains $g_{i,1}, g_{i,2}, \d...
For each test case, output "YES" (without quotes) if Alice can win, 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).
[ [ "4\n4\n1 -2 -3 -2\n-4 4 -1 -3\n1 2 -2 4\n2\n1 2\n-1 -2\n2 -2\n5\n1 2 3 4 5\n-2 3 -4 -5 -1\n3 -5 1 2 2\n6\n1 3 -6 2 5 2\n1 3 -2 -3 -6 -5\n-2 -1 -3 2 3 1", "YES\nNO\nYES\nNO" ] ]
The first test case is described in the statement. In the second test case, Bob's grid is as follows: $$\begin{bmatrix} a_1 & a_2 \\\ -a_1 & -a_2 \\\ a_2 & -a_2 \end{bmatrix}$$ For the last column to have $1$ in the middle row when sorted, Alice must pick $a_2 = -1$. However, it is then impossible to choose $a_1$ su...
Title: Β±1 time_limit_ms: 2000 memory_limit_mb: 256 Description: Bob has a grid with $3$ rows and $n$ columns, each of which contains either $a_i$ or $-a_i$ for some integer $1 \leq i \leq n$. For example, one possible grid for $n=4$ is shown below: $$\begin{bmatrix} a_1 & -a_2 & -a_3 & -a_2 \\\ -a_4 & a_4 & -a_1 & -a_...
1980E
https://codeforces.com/problemset/problem/1980/E
Permutation of Rows and Columns
1,600
[ "constructive algorithms", "data structures", "greedy", "hashing", "implementation", "math", "matrices", "sortings" ]
Div. 3
3,000
256
You have been given a matrix $a$ of size $n$ by $m$, containing a permutation of integers from $1$ to $n \cdot m$. A permutation of $n$ integers is an array containing all numbers from $1$ to $n$ exactly once. For example, the arrays $[1]$, $[2, 1, 3]$, $[5, 4, 3, 2, 1]$ are permutations, while the arrays $[1, 1]$, $[...
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 description contains $2$ integers $n$ and $m$ ($1 \le n, m \le n \cdot m \le 2 \cdot 10^5$) β€” the sizes of the matrix. The next $n$ lines contain $m$ inte...
For each test case, output "YES" if the second matrix can be obtained from the first, and "NO" otherwise. You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
[ [ "7\n1 1\n1\n1\n2 2\n1 2\n3 4\n4 3\n2 1\n2 2\n1 2\n3 4\n4 3\n1 2\n3 4\n1 5 9 6\n12 10 4 8\n7 11 3 2\n1 5 9 6\n12 10 4 8\n7 11 3 2\n3 3\n1 5 9\n6 4 2\n3 8 7\n9 5 1\n2 4 6\n7 8 3\n2 3\n1 2 6\n5 4 3\n6 1 2\n3 4 5\n1 5\n5 1 2 3 4\n4 2 5 1 3", "YES\nYES\nNO\nYES\nYES\nNO\nYES" ] ]
In the second example, the original matrix looks like this: $ \begin{pmatrix} 1 & 2 \\\ 3 & 4 \end{pmatrix} $ By swapping rows $1$ and $2$, it becomes: $ \begin{pmatrix} 3 & 4 \\\ 1 & 2 \end{pmatrix} $ By swapping columns $1$ and $2$, it becomes equal to matrix $b$: $ \begin{pmatrix} 4 & 3 \\\ 2 & 1 \end{pmatrix} ...
Title: Permutation of Rows and Columns time_limit_ms: 3000 memory_limit_mb: 256 Description: You have been given a matrix $a$ of size $n$ by $m$, containing a permutation of integers from $1$ to $n \cdot m$. A permutation of $n$ integers is an array containing all numbers from $1$ to $n$ exactly once. For example, the...
2014F
https://codeforces.com/problemset/problem/2014/F
Sheriff's Defense
2,000
[ "dfs and similar", "dp", "greedy", "trees" ]
Div. 3
2,000
256
"Why, master," quoth Little John, taking the bags and weighing them in his hand, "here is the chink of gold." The folk hero Robin Hood has been troubling Sheriff of Nottingham greatly. Sheriff knows that Robin Hood is about to attack his camps and he wants to be prepared. Sheriff of Nottingham built the camps with st...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Each test case begins with two integers $n$, $c$ ($1 \le n \le 2\cdot10^5, 1 \le c \le 10^9$) β€” the number of camps and the gold taken from each neighboring camp for strengthening. The second line of each test case contains ...
Output a single integer, the maximum gold Sheriff of Nottingham can keep in his surviving camps after Robin Hood's attack.
[ [ "5\n3 1\n2 3 1\n1 2\n2 3\n3 1\n3 6 3\n1 2\n2 3\n3 1\n-2 -3 -1\n1 2\n2 3\n6 1\n5 -4 3 6 7 3\n4 1\n5 1\n3 5\n3 6\n1 2\n8 1\n3 5 2 7 8 5 -3 -4\n7 3\n1 8\n4 3\n3 5\n7 6\n8 7\n2 1", "3\n8\n0\n17\n26" ] ]
In the first test case, it is optimal to strengthen the second base. The final gold at each base is $[1,3,0]$. In the second test case, it is optimal to strengthen all bases. The final gold at each base is $[2,4,2]$. In the third test case, it is optimal to not strengthen any base.
Title: Sheriff's Defense time_limit_ms: 2000 memory_limit_mb: 256 Description: "Why, master," quoth Little John, taking the bags and weighing them in his hand, "here is the chink of gold." The folk hero Robin Hood has been troubling Sheriff of Nottingham greatly. Sheriff knows that Robin Hood is about to attack his ca...
1971G
https://codeforces.com/problemset/problem/1971/G
XOUR
1,700
[ "data structures", "dsu", "sortings" ]
Div. 4
2,000
256
You are given an array $a$ consisting of $n$ nonnegative integers. You can swap the elements at positions $i$ and $j$ if $a_i~\mathsf{XOR}~a_j < 4$, where $\mathsf{XOR}$ is the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). Find the lexicographically smallest array that can be made with...
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 2\cdot10^5$) β€” the length of the array. The second line of each test case contains $n$ integers $a_i$ ($0 \leq a_i \leq 10^9$) β€” the elements o...
For each test case, output $n$ integers β€” the lexicographically smallest array that can be made with any number of swaps.
[ [ "4\n4\n1 0 3 2\n5\n2 7 1 5 6\n8\n1 2 1 2 1 2 1 2\n4\n16 4 1 64", "0 1 2 3 \n1 5 2 6 7 \n1 1 1 1 2 2 2 2 \n16 4 1 64" ] ]
For the first test case, you can swap any two elements, so we can produce the sorted array. For the second test case, you can swap $2$ and $1$ (their $\mathsf{XOR}$ is $3$), $7$ and $5$ (their $\mathsf{XOR}$ is $2$), and $7$ and $6$ (their $\mathsf{XOR}$ is $1$) to get the lexicographically smallest array.
Title: XOUR time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given an array $a$ consisting of $n$ nonnegative integers. You can swap the elements at positions $i$ and $j$ if $a_i~\mathsf{XOR}~a_j < 4$, where $\mathsf{XOR}$ is the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR)...
1976F
https://codeforces.com/problemset/problem/1976/F
Remove Bridges
2,800
[ "data structures", "dfs and similar", "dp", "greedy", "sortings", "trees" ]
Div. 2
2,000
256
You are given a rooted tree, consisting of $n$ vertices, numbered from $1$ to $n$. Vertex $1$ is the root. Additionally, the root only has one child. You are asked to add exactly $k$ edges to the tree (possibly, multiple edges and/or edges already existing in the tree). Recall that a bridge is such an edge that, afte...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of testcases. The first line of each testcase contains a single integer $n$ ($2 \le n \le 3 \cdot 10^5$) β€” the number of vertices of the tree. Each of the next $n - 1$ lines contain two integers $v$ and $u$ ($1 \le v, u \le n$) β€” the descr...
For each testcase, print $n - 1$ integers. For each $k$ from $1$ to $n - 1$ print the smallest number of bridges that can be left after you add $k$ edges to the tree.
[ [ "4\n2\n1 2\n12\n4 10\n5 12\n12 11\n3 6\n9 6\n1 6\n12 7\n11 6\n2 11\n10 9\n10 8\n8\n1 2\n2 3\n2 4\n3 5\n3 6\n4 7\n4 8\n5\n1 2\n2 3\n3 4\n4 5", "0 \n7 3 1 0 0 0 0 0 0 0 0 \n4 1 0 0 0 0 0 \n0 0 0 0" ] ]
Title: Remove Bridges time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a rooted tree, consisting of $n$ vertices, numbered from $1$ to $n$. Vertex $1$ is the root. Additionally, the root only has one child. You are asked to add exactly $k$ edges to the tree (possibly, multiple edges and/or edges alr...
2027E1
https://codeforces.com/problemset/problem/2027/E1
Bit Game (Easy Version)
2,800
[ "bitmasks", "brute force", "games", "math" ]
Div. 2
2,000
256
This is the easy version of this problem. The only difference is that you need to output the winner of the game in this version, and the number of stones in each pile are fixed. You must solve both versions to be able to hack. Alice and Bob are playing a familiar game where they take turns removing stones from $n$ pil...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows. The first line of each test case contains $n$ ($1 \le n \le 10^4$) β€” the number of piles. The second line of each test case contains $n$ integers $a_1, a_2, \ld...
Print a single line with the winner's name. If Alice wins, print "Alice", otherwise print "Bob" (without quotes).
[ [ "7\n2\n1 6\n10 7\n3\n10 8 15\n25 4 14\n4\n8 32 65 64\n7 45 126 94\n3\n20 40 1\n23 55 1\n5\n12345 9876 86419 8641 1\n6789 54321 7532 97532 1\n2\n20 64\n44 61\n3\n57 109 55\n69 90 85", "Bob\nBob\nBob\nBob\nBob\nAlice\nAlice" ] ]
In the first test case, neither player can take any stones from the first pile since there is no value of $d$ satisfying the conditions. For the second pile, to begin with, Alice can remove between $1$ and $6$ stones. No matter which move Alice performs, Bob can remove the rest of the stones on his turn. After Bob's mo...
Title: Bit Game (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 need to output the winner of the game in this version, and the number of stones in each pile are fixed. You must solve both versions to be able to hack. Alice a...
2029C
https://codeforces.com/problemset/problem/2029/C
New Rating
1,700
[ "binary search", "data structures", "dp", "greedy" ]
Div. 1 + 2
2,000
512
Hello, Codeforces Forcescode! Kevin used to be a participant of Codeforces. Recently, the KDOI Team has developed a new Online Judge called Forcescode. Kevin has participated in $n$ contests on Forcescode. In the $i$-th contest, his performance rating is $a_i$. Now he has hacked into the backend of Forcescode and ...
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 5\cdot 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1\le n\le 3\cdot 10^5$) β€” the number of contests. The second l...
For each test case, output a single integer β€” the maximum possible rating after the recalculation if Kevin chooses the interval optimally.
[ [ "5\n6\n1 2 3 4 5 6\n7\n1 2 1 1 1 3 4\n1\n1\n9\n9 9 8 2 4 4 3 5 3\n10\n1 2 3 4 1 3 2 1 1 10", "5\n4\n0\n4\n5" ] ]
In the first test case, Kevin must skip at least one contest. If he chooses any interval of length $1$, his rating after the recalculation will be equal to $5$. In the second test case, Kevin's optimal choice is to select the interval $[3,5]$. During the recalculation, his rating changes as follows: $$ 0 \xrightarrow...
Title: New Rating time_limit_ms: 2000 memory_limit_mb: 512 Description: Hello, Codeforces Forcescode! Kevin used to be a participant of Codeforces. Recently, the KDOI Team has developed a new Online Judge called Forcescode. Kevin has participated in $n$ contests on Forcescode. In the $i$-th contest, his performance...
1991F
https://codeforces.com/problemset/problem/1991/F
Triangle Formation
2,200
[ "brute force", "greedy", "implementation", "math", "sortings" ]
Div. 1 + 2
5,000
256
You are given $n$ sticks, numbered from $1$ to $n$. The length of the $i$-th stick is $a_i$. You need to answer $q$ queries. In each query, you are given two integers $l$ and $r$ ($1 \le l < r \le n$, $r - l + 1 \ge 6$). Determine whether it is possible to choose $6$ distinct sticks from the sticks numbered $l$ to $r$...
The first line contains two integers $n$ and $q$ ($6 \le n \le 10^5$, $1 \le q \le 10^5$) β€” the number of sticks and the number of queries respectively. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” $a_i$ denotes the length of the $i$-th stick. Each of the following $q$ lines ...
For each query, output "YES" (without quotes) if it is possible to form $2$ triangles, 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.
[ [ "10 5\n5 2 2 10 4 10 6 1 5 3\n1 6\n2 7\n2 8\n5 10\n4 10", "YES\nNO\nYES\nNO\nYES" ] ]
In the first query, the lengths of the sticks are $[5, 2, 2, 10, 4, 10]$. Two sets of sticks $[2, 4, 5]$ and $[2, 10, 10]$ can be selected to form $2$ non-degenerate triangles. In the second query, the lengths of the sticks are $[2, 2, 10, 4, 10, 6]$. It can be shown that it is impossible to form $2$ non-degenerate tr...
Title: Triangle Formation time_limit_ms: 5000 memory_limit_mb: 256 Description: You are given $n$ sticks, numbered from $1$ to $n$. The length of the $i$-th stick is $a_i$. You need to answer $q$ queries. In each query, you are given two integers $l$ and $r$ ($1 \le l < r \le n$, $r - l + 1 \ge 6$). Determine whether ...
1984B
https://codeforces.com/problemset/problem/1984/B
Large Addition
1,100
[ "implementation", "math" ]
Div. 1 + 2
1,000
256
A digit is large if it is between $5$ and $9$, inclusive. A positive integer is large if all of its digits are large. You are given an integer $x$. Can it be the sum of two large positive integers with the same number of digits?
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The only line of each test case contains a single integer $x$ ($10 \leq x \leq 10^{18}$).
For each test case, output $\texttt{YES}$ if $x$ satisfies the condition, and $\texttt{NO}$ otherwise. You can output $\texttt{YES}$ and $\texttt{NO}$ in any case (for example, strings $\texttt{yES}$, $\texttt{yes}$, and $\texttt{Yes}$ will be recognized as a positive response).
[ [ "11\n1337\n200\n1393938\n1434\n98765432123456789\n11111111111111111\n420\n1984\n10\n69\n119", "YES\nNO\nYES\nYES\nNO\nYES\nNO\nYES\nYES\nNO\nNO" ] ]
In the first test case, we can have $658 + 679 = 1337$. In the second test case, it can be shown that no numbers of equal length and only consisting of large digits can add to $200$. In the third test case, we can have $696\,969 + 696\,969 = 1\,393\,938$. In the fourth test case, we can have $777 + 657 = 1434$.
Title: Large Addition time_limit_ms: 1000 memory_limit_mb: 256 Description: A digit is large if it is between $5$ and $9$, inclusive. A positive integer is large if all of its digits are large. You are given an integer $x$. Can it be the sum of two large positive integers with the same number of digits? Input: The fi...
1997B
https://codeforces.com/problemset/problem/1997/B
Make Three Regions
1,100
[ "constructive algorithms", "two pointers" ]
Div. 2
2,000
256
There is a grid, consisting of $2$ rows and $n$ columns. Each cell of the grid is either free or blocked. A free cell $y$ is reachable from a free cell $x$ if at least one of these conditions holds: * $x$ and $y$ share a side; * there exists a free cell $z$ such that $z$ is reachable from $x$ and $y$ is reachabl...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) β€” the number of columns. The $i$-th of the next two lines contains a description of the $i$-th row of the grid β€” the string $s_i$, c...
For each test case, print a single integer β€” the number of cells such that the number of connected regions becomes $3$ if this cell is blocked.
[ [ "4\n8\n.......x\n.x.xx...\n2\n..\n..\n3\nxxx\nxxx\n9\n..x.x.x.x\nx.......x", "1\n0\n0\n2" ] ]
In the first test case, if the cell $(1, 3)$ is blocked, the number of connected regions becomes $3$ (as shown in the picture from the statement).
Title: Make Three Regions time_limit_ms: 2000 memory_limit_mb: 256 Description: There is a grid, consisting of $2$ rows and $n$ columns. Each cell of the grid is either free or blocked. A free cell $y$ is reachable from a free cell $x$ if at least one of these conditions holds: * $x$ and $y$ share a side; * ther...
1993B
https://codeforces.com/problemset/problem/1993/B
Parity and Sum
1,100
[ "constructive algorithms", "greedy" ]
Div. 2
1,000
256
Given an array $a$ of $n$ positive integers. In one operation, you can pick any pair of indexes $(i, j)$ such that $a_i$ and $a_j$ have distinct parity, then replace the smaller one with the sum of them. More formally: * If $a_i < a_j$, replace $a_i$ with $a_i + a_j$; * Otherwise, replace $a_j$ with $a_i + a_j$....
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 second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) β€” the elements of array $a$. It is guaranteed...
For each test case, output a single integer β€” the minimum number of operations required.
[ [ "7\n5\n1 3 5 7 9\n4\n4 4 4 4\n3\n2 3 4\n4\n3 2 2 8\n6\n4 3 6 1 2 1\n6\n3 6 1 2 1 2\n5\n999999996 999999997 999999998 999999999 1000000000", "0\n0\n2\n4\n3\n3\n3" ] ]
In the first test case, all integers already have the same parity. Therefore, no operation is needed. In the third test case, we can perform two operations $(1, 2)$ and $(1, 3)$. The array $a$ transforms as follows: $a = [\color{red}2, \color{red}3, 4] \longrightarrow [\color{red}5, 3, \color{red}4] \longrightarrow [5...
Title: Parity and Sum time_limit_ms: 1000 memory_limit_mb: 256 Description: Given an array $a$ of $n$ positive integers. In one operation, you can pick any pair of indexes $(i, j)$ such that $a_i$ and $a_j$ have distinct parity, then replace the smaller one with the sum of them. More formally: * If $a_i < a_j$, rep...
2014D
https://codeforces.com/problemset/problem/2014/D
Robert Hood and Mrs Hood
1,400
[ "brute force", "data structures", "greedy", "sortings" ]
Div. 3
2,000
256
Impress thy brother, yet fret not thy mother. Robin's brother and mother are visiting, and Robin gets to choose the start day for each visitor. All days are numbered from $1$ to $n$. Visitors stay for $d$ continuous days, all of those $d$ days must be between day $1$ and $n$ inclusive. Robin has a total of $k$ risky...
The first line of the input contains a single integer $t$ ($1\leq t \leq 10^4$) β€” the number of test cases. The first line of each test case consists of three integers $n$, $d$, $k$ ($1 \le n \le 10^5, 1 \le d, k \le n$) β€” the number of total days, duration of the visits, and the number of jobs. Then follow $k$ lines...
For each test case, output two integers, the best starting days of Robin's brother and mother respectively. Both visits must fit between day $1$ and $n$ inclusive.
[ [ "6\n2 1 1\n1 2\n4 1 2\n1 2\n2 4\n7 2 3\n1 2\n1 3\n6 7\n5 1 2\n1 2\n3 5\n9 2 1\n2 8\n9 2 4\n7 9\n4 8\n1 3\n2 3", "1 1\n2 1\n1 4\n1 1\n1 1\n3 4" ] ]
In the first test case, the only job fills all $2$ days, both should visit on day $1$. In the second test case, day $2$ overlaps with $2$ jobs and day $1$ overlaps with only $1$. In the third test case, Robert visits for days $[1,2]$, Mrs. Hood visits for days $[4,5]$.
Title: Robert Hood and Mrs Hood time_limit_ms: 2000 memory_limit_mb: 256 Description: Impress thy brother, yet fret not thy mother. Robin's brother and mother are visiting, and Robin gets to choose the start day for each visitor. All days are numbered from $1$ to $n$. Visitors stay for $d$ continuous days, all of tho...
1979A
https://codeforces.com/problemset/problem/1979/A
Guess the Maximum
800
[ "brute force", "greedy", "implementation" ]
Div. 2
1,000
256
Alice and Bob came up with a rather strange game. They have an array of integers $a_1, a_2,\ldots, a_n$. Alice chooses a certain integer $k$ and tells it to Bob, then the following happens: * Bob chooses two integers $i$ and $j$ ($1 \le i < j \le n$), and then finds the maximum among the integers $a_i, a_{i + 1},\ld...
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$ ($2 \le n \le 5 \cdot 10^4$) β€” the number of elements in the array. The sec...
For each test case, output one integer β€” the maximum integer $k$ at which Alice is guaranteed to win.
[ [ "6\n4\n2 4 1 7\n5\n1 2 3 4 5\n2\n1 1\n3\n37 8 16\n5\n10 10 10 10 9\n10\n3 12 9 5 2 3 2 9 8 2", "3\n1\n0\n15\n9\n2" ] ]
In the first test case, all possible subsegments that Bob can choose look as follows: $[2, 4], [2, 4, 1], [2, 4, 1, 7], [4, 1], [4, 1, 7], [1, 7]$. The maximums on the subsegments are respectively equal to $4, 4, 7, 4, 7, 7$. It can be shown that $3$ is the largest integer such that any of the maximums will be strictly...
Title: Guess the Maximum time_limit_ms: 1000 memory_limit_mb: 256 Description: Alice and Bob came up with a rather strange game. They have an array of integers $a_1, a_2,\ldots, a_n$. Alice chooses a certain integer $k$ and tells it to Bob, then the following happens: * Bob chooses two integers $i$ and $j$ ($1 \le i...
1976B
https://codeforces.com/problemset/problem/1976/B
Increase/Decrease/Copy
1,100
[ "greedy", "implementation" ]
Div. 2
2,000
256
You are given two integer arrays: array $a$ of length $n$ and array $b$ of length $n+1$. You can perform the following operations any number of times in any order: * choose any element of the array $a$ and increase it by $1$; * choose any element of the array $a$ and decrease it by $1$; * choose any element o...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. Each test case consists of three lines: * the first line contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$); * the second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^9$); * the third ...
For each test case, print a single integer β€” the minimum number of operations (possibly zero) required to transform the array $a$ into the array $b$.
[ [ "3\n1\n2\n1 3\n2\n3 3\n3 3 3\n4\n4 2 1 2\n2 1 5 2 3", "3\n1\n8" ] ]
In the first example, you can transform $a$ into $b$ as follows: $[2] \rightarrow [2, 2] \rightarrow [1, 2] \rightarrow [1, 3]$.
Title: Increase/Decrease/Copy time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given two integer arrays: array $a$ of length $n$ and array $b$ of length $n+1$. You can perform the following operations any number of times in any order: * choose any element of the array $a$ and increase it by $1$; * c...
2027A
https://codeforces.com/problemset/problem/2027/A
Rectangle Arrangement
800
[ "geometry", "implementation", "math" ]
Div. 2
1,000
256
You are coloring an infinite square grid, in which all cells are initially white. To do this, you are given $n$ stamps. Each stamp is a rectangle of width $w_i$ and height $h_i$. You will use each stamp exactly once to color a rectangle of the same size as the stamp on the grid in black. You cannot rotate the stamp, a...
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 $i$-th of the next $n$ lines contains two integers $w_i$ and $h_i$ ($1 \l...
For each test case, output a single integer β€” the minimum sum of the perimeters of the connected regions of black squares you can obtain after all the stamps have been used.
[ [ "5\n5\n1 5\n2 4\n3 3\n4 2\n5 1\n3\n2 2\n1 1\n1 2\n1\n3 2\n3\n100 100\n100 100\n100 100\n4\n1 4\n2 3\n1 5\n3 2", "20\n8\n10\n400\n16" ] ]
In the first test case, the stamps can be used as shown on the left. Each stamp is highlighted in its own color for clarity. ![](CDN_BASE_URL/877686333ced56f689851506a90eefb8) After all these stamps are used, there is one black region (as shown on the right), and its perimeter is $20$. It can be shown that there is n...
Title: Rectangle Arrangement time_limit_ms: 1000 memory_limit_mb: 256 Description: You are coloring an infinite square grid, in which all cells are initially white. To do this, you are given $n$ stamps. Each stamp is a rectangle of width $w_i$ and height $h_i$. You will use each stamp exactly once to color a rectangle...
2020F
https://codeforces.com/problemset/problem/2020/F
Count Leaves
2,900
[ "dp", "math", "number theory" ]
Div. 2
4,000
256
Let $n$ and $d$ be positive integers. We build the the divisor tree $T_{n,d}$ as follows: * The root of the tree is a node marked with number $n$. This is the $0$-th layer of the tree. * For each $i$ from $0$ to $d - 1$, for each vertex of the $i$-th layer, do the following. If the current vertex is marked with $...
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 three integers $n$, $k$, and $d$ ($1 \le n \le 10^9$, $1 \le k,d \le 10^5$). It is guaranteed that the sum of $n$ over ...
For each test case, output $\sum\limits_{i=1}^{n} f(i^k,d)$, modulo $10^9+7$.
[ [ "3\n6 1 1\n1 3 3\n10 1 2", "14\n1\n53" ] ]
In the first test case, $n = 6$, $k = 1$, and $d = 1$. Thus, we need to find the total number of leaves in the divisor trees $T_{1,1}$, $T_{2,1}$, $T_{3,1}$, $T_{4,1}$, $T_{5,1}$, $T_{6,1}$. * $T_{1,1}$ has only one leaf, which is marked with $1$. * $T_{2,1}$ has two leaves, marked with $1$ and $2$. * $T_{3,1}...
Title: Count Leaves time_limit_ms: 4000 memory_limit_mb: 256 Description: Let $n$ and $d$ be positive integers. We build the the divisor tree $T_{n,d}$ as follows: * The root of the tree is a node marked with number $n$. This is the $0$-th layer of the tree. * For each $i$ from $0$ to $d - 1$, for each vertex of ...
2022B
https://codeforces.com/problemset/problem/2022/B
Kar Salesman
1,300
[ "binary search", "greedy", "math" ]
Div. 2
1,000
256
Karel is a salesman in a car dealership. The dealership has $n$ different models of cars. There are $a_i$ cars of the $i$-th model. Karel is an excellent salesperson and can convince customers to buy up to $x$ cars (of Karel's choice), as long as the cars are from different models. Determine the minimum number of ...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 5 \cdot 10^5$; $1 \le x \le 10$) β€” the number of different models of cars and th...
For each test case, output the minimum possible number of customers needed to sell all the cars.
[ [ "4\n3 2\n3 1 2\n3 3\n2 1 3\n5 3\n2 2 1 9 2\n7 4\n2 5 3 3 5 2 5", "3\n3\n9\n7" ] ]
For the first case, Karel only needs to lure in $3$ customers. He will convince the customers to buy the following models of cars: * Customer $1$ buys $2$ cars with model $1$ and $3$. * Customer $2$ buys $2$ cars with model $1$ and $2$. * Customer $3$ buys $2$ cars with model $1$ and $3$. For the second case...
Title: Kar Salesman time_limit_ms: 1000 memory_limit_mb: 256 Description: Karel is a salesman in a car dealership. The dealership has $n$ different models of cars. There are $a_i$ cars of the $i$-th model. Karel is an excellent salesperson and can convince customers to buy up to $x$ cars (of Karel's choice), as long as...
1997C
https://codeforces.com/problemset/problem/1997/C
Even Positions
1,100
[ "constructive algorithms", "data structures", "greedy" ]
Div. 2
2,000
256
Monocarp had a regular bracket sequence $s$ of length $n$ ($n$ is even). He even came up with his own way to calculate its cost. He knows that in a regular bracket sequence (RBS), each opening bracket is paired up with the corresponding closing bracket. So he decided to calculate the cost of RBS as the sum of distance...
The first line contains a single integer $t$ ($1 \le t \le 5000$) β€” the number of test cases. Next $t$ cases follow. The first line of each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$; $n$ is even) β€” the length of string $s$. The second line of each test case contains a string $s$ of length $n...
For each test case, print one integer β€” the minimum cost of the regular bracket sequence that can be obtained from $s$ by replacing '_'-s with brackets.
[ [ "4\n6\n_(_)_)\n2\n_)\n8\n_)_)_)_)\n8\n_(_)_(_)", "5\n1\n4\n8" ] ]
In the first test case, it's optimal to make $s$ equal to (())(). The cost of $s$ will be equal to $3 + 1 + 1 = 5$. In the second test case, the only option is to make $s$ equal to () with cost $1$. In the third test case, the only possible RBS is ()()()() with cost $1 + 1 + 1 + 1 = 4$. In the fourth test case, it's...
Title: Even Positions time_limit_ms: 2000 memory_limit_mb: 256 Description: Monocarp had a regular bracket sequence $s$ of length $n$ ($n$ is even). He even came up with his own way to calculate its cost. He knows that in a regular bracket sequence (RBS), each opening bracket is paired up with the corresponding closin...
2002D2
https://codeforces.com/problemset/problem/2002/D2
DFS Checker (Hard Version)
2,300
[ "binary search", "data structures", "dfs and similar", "graphs", "hashing", "trees" ]
Div. 1 + 2
2,000
512
This is the hard version of the problem. In this version, you are given a generic tree and the constraints on $n$ and $q$ are higher. You can make hacks only if both versions of the problem are solved. You are given a rooted tree consisting of $n$ vertices. The vertices are numbered from $1$ to $n$, and the root is th...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains two integers $n$, $q$ ($2\le n\le 3\cdot 10^5$, $2\le q\le 10^5$) β€” the number of vertices in the tree and the number of q...
For each test case, print $q$ lines corresponding to the $q$ queries. For each query, output $\texttt{YES}$ if there is a DFS order that exactly equals the current permutation, and output $\texttt{NO}$ otherwise. You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt...
[ [ "3\n3 3\n1 1\n1 2 3\n2 3\n3 2\n1 3\n7 4\n1 1 2 2 3 3\n1 2 3 4 5 6 7\n3 5\n2 5\n3 7\n4 6\n5 4\n1 1 3 4\n2 3 4 5 1\n5 1\n4 5\n3 4\n2 3", "YES\nYES\nNO\nYES\nNO\nNO\nYES\nYES\nNO\nNO\nYES" ] ]
In the first test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,3,2],[1,2,3],[3,2,1]$, respectively. The first two permutations are valid DFS orders; the third is not a DFS order. In the second test case, the permutation $p_1, p_2, \ldots, p_n$ after each modification is $[1,2,5,4,3,6,7]...
Title: DFS Checker (Hard Version) time_limit_ms: 2000 memory_limit_mb: 512 Description: This is the hard version of the problem. In this version, you are given a generic tree and the constraints on $n$ and $q$ are higher. You can make hacks only if both versions of the problem are solved. You are given a rooted tree c...
2000G
https://codeforces.com/problemset/problem/2000/G
Call During the Journey
2,100
[ "binary search", "brute force", "graphs", "greedy", "shortest paths" ]
Div. 3
4,000
256
You live in a city consisting of $n$ intersections and $m$ streets connecting some pairs of intersections. You can travel in either direction on each street. No two streets connect the same pair of intersections, and no street connects an intersection to itself. You can reach any intersection from any other, possibly p...
The first line contains an integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The following are the descriptions of the test cases. The first line of each test case contains two integers $n$, $m$ ($2 \le n \le 10^5, 1 \le m \le 10^5$) β€” the number of intersections and streets in the city. The second line of...
For each test case, output a single integer β€” the latest time you can leave home to have time to talk on the phone and not be late for the event. If you cannot reach the event on time, output -1.
[ [ "7\n5 5\n100 20 80\n1 5 30 100\n1 2 20 50\n2 3 20 50\n3 4 20 50\n4 5 20 50\n2 1\n100 50 60\n1 2 55 110\n4 4\n100 40 60\n1 2 30 100\n2 4 30 100\n1 3 20 50\n3 4 20 50\n3 3\n100 80 90\n1 2 1 10\n2 3 10 50\n1 3 20 21\n3 2\n58 55 57\n2 1 1 3\n2 3 3 4\n2 1\n12 9 10\n2 1 6 10\n5 5\n8 5 6\n2 1 1 8\n2 3 4 8\n4 2 2 4\n...
Title: Call During the Journey time_limit_ms: 4000 memory_limit_mb: 256 Description: You live in a city consisting of $n$ intersections and $m$ streets connecting some pairs of intersections. You can travel in either direction on each street. No two streets connect the same pair of intersections, and no street connects...
1992D
https://codeforces.com/problemset/problem/1992/D
Test of Love
1,200
[ "dp", "greedy", "implementation" ]
Div. 3
2,000
256
ErnKor is ready to do anything for Julen, even to swim through crocodile- infested swamps. We decided to test this love. ErnKor will have to swim across a river with a width of $1$ meter and a length of $n$ meters. The river is very cold. Therefore, in total (that is, throughout the entire swim from $0$ to $n+1$) ErnK...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains three numbers $n, m, k$ ($0 \le k \le 2 \cdot 10^5$, $1 \le n \le 2 \cdot 10^5$, $1 \le m \le 10$) β€” the length of the river, the distance ErnKor can jump, and the number of meters E...
For each test case, output "YES" if ErnKor can pass the test, and output "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.
[ [ "6\n6 2 0\nLWLLLW\n6 1 1\nLWLLLL\n6 1 1\nLWLLWL\n6 2 15\nLWLLCC\n6 10 0\nCCCCCC\n6 6 1\nWCCCCW", "YES\nYES\nNO\nNO\nYES\nYES" ] ]
Let's consider examples: * First example: We jump from the shore to the first log ($0 \rightarrow 1$), from the first log to the second ($1 \rightarrow 3$), from the second to the fourth ($3 \rightarrow 5$), and from the last log to the shore ($5 \rightarrow 7$). So, we have $0 \rightarrow 1 \rightarrow 3 \rightarro...
Title: Test of Love time_limit_ms: 2000 memory_limit_mb: 256 Description: ErnKor is ready to do anything for Julen, even to swim through crocodile- infested swamps. We decided to test this love. ErnKor will have to swim across a river with a width of $1$ meter and a length of $n$ meters. The river is very cold. Theref...
1985A
https://codeforces.com/problemset/problem/1985/A
Creating Words
800
[ "implementation", "strings" ]
Div. 4
1,000
256
Matthew is given two strings $a$ and $b$, both of length $3$. He thinks it's particularly funny to create two new words by swapping the first character of $a$ with the first character of $b$. He wants you to output $a$ and $b$ after the swap. Note that the new words may not necessarily be different.
The first line contains $t$ ($1 \leq t \leq 100$) β€” the number of test cases. The first and only line of each test case contains two space-separated strings, $a$ and $b$, both of length $3$. The strings only contain lowercase Latin letters.
For each test case, after the swap, output $a$ and $b$, separated by a space.
[ [ "6\nbit set\ncat dog\nhot dog\nuwu owo\ncat cat\nzzz zzz", "sit bet\ndat cog\ndot hog\nowu uwo\ncat cat\nzzz zzz" ] ]
Title: Creating Words time_limit_ms: 1000 memory_limit_mb: 256 Description: Matthew is given two strings $a$ and $b$, both of length $3$. He thinks it's particularly funny to create two new words by swapping the first character of $a$ with the first character of $b$. He wants you to output $a$ and $b$ after the swap. ...
1991A
https://codeforces.com/problemset/problem/1991/A
Maximize the Last Element
800
[ "greedy", "implementation" ]
Div. 1 + 2
1,000
256
You are given an array $a$ of $n$ integers, where $n$ is odd. In one operation, you will remove two adjacent elements from the array $a$, and then concatenate the remaining parts of the array. For example, given the array $[4,7,4,2,9]$, we can obtain the arrays $[4,2,9]$ and $[4,7,9]$ by the operations $[\underline{4,...
Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 99$; $n$ is odd) β€” the length of the array $a$. The second line of e...
For each test case, output a single integer β€” the maximum possible value of the remaining element in $a$.
[ [ "4\n1\n6\n3\n1 3 2\n5\n4 7 4 2 9\n7\n3 1 4 1 5 9 2", "6\n2\n9\n5" ] ]
In the first test case, the array $a$ is $[6]$. Since there is only one element, no operations are needed. The maximum possible value of the remaining element is $6$. In the second test case, the array $a$ is $[1, 3, 2]$. We can remove the first two elements $[\underline{1, 3}, 2] \to [2]$, or remove the last two elem...
Title: Maximize the Last Element time_limit_ms: 1000 memory_limit_mb: 256 Description: You are given an array $a$ of $n$ integers, where $n$ is odd. In one operation, you will remove two adjacent elements from the array $a$, and then concatenate the remaining parts of the array. For example, given the array $[4,7,4,2,...
2035F
https://codeforces.com/problemset/problem/2035/F
Tree Operations
2,500
[ "binary search", "brute force", "dfs and similar", "dp", "trees" ]
Div. 1 + 2
4,000
256
This really says a lot about our society. One day, a turtle gives you a tree with $n$ nodes rooted at node $x$. Each node has an initial nonnegative value; the $i$-th node has starting value $a_i$. You want to make the values of all nodes equal to $0$. To do so, you will perform a series of operations on the tree, wh...
The first line contains a single integer $t$ ($1\le t\le 100$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $x$ ($1 \le n \le 2000$, $1 \le x \le n$) β€” the number of nodes and the root of the tree. The second line of each test case contains $n$ integers $a_1, a_2, \ldots,...
For each test case, output a single integer denoting the minimum amount of operations needed to make all nodes $0$. If it's impossible to make all nodes $0$, output $-1$.
[ [ "5\n2 1\n1 2\n1 2\n3 2\n2 1 3\n2 1\n3 2\n4 1\n1 1 0 1\n1 2\n2 3\n1 4\n12 6\n14 4 5 6 12 9 5 11 6 2 1 12\n3 9\n10 6\n6 12\n4 3\n3 1\n5 11\n9 7\n5 6\n1 8\n2 8\n5 1\n1 1\n0", "3\n6\n5\n145\n0" ] ]
In the first test case, you can make the following valid sequence of operations: * For operation $1$, decrease the value of node $1$. This is valid because $(((1 - 1) \bmod n) + 1) = 1$, and node $1$ is in the subtree of node $1$. * For operation $2$, decrease the value of node $2$. This is valid because $(((2 - ...
Title: Tree Operations time_limit_ms: 4000 memory_limit_mb: 256 Description: This really says a lot about our society. One day, a turtle gives you a tree with $n$ nodes rooted at node $x$. Each node has an initial nonnegative value; the $i$-th node has starting value $a_i$. You want to make the values of all nodes eq...
2004D
https://codeforces.com/problemset/problem/2004/D
Colored Portals
1,600
[ "binary search", "brute force", "data structures", "graphs", "greedy", "implementation", "shortest paths" ]
Div. 2
2,000
256
There are $n$ cities located on a straight line. The cities are numbered from $1$ to $n$. Portals are used to move between cities. There are $4$ colors of portals: blue, green, red, and yellow. Each city has portals of two different colors. You can move from city $i$ to city $j$ if they have portals of the same color ...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $q$ ($1 \le n, q \le 2 \cdot 10^5$) β€” the number of cities and the number of queries, respectively. The second line contains $n$ strings of the following types: ...
For each query, print a single integer β€” the minimum cost to move from city $x$ to city $y$ (or $-1$ if it is impossible).
[ [ "2\n4 5\nBR BR GY GR\n1 2\n3 1\n4 4\n1 4\n4 2\n2 1\nBG RY\n1 2", "1\n4\n0\n3\n2\n-1" ] ]
Title: Colored Portals time_limit_ms: 2000 memory_limit_mb: 256 Description: There are $n$ cities located on a straight line. The cities are numbered from $1$ to $n$. Portals are used to move between cities. There are $4$ colors of portals: blue, green, red, and yellow. Each city has portals of two different colors. Y...
2027C
https://codeforces.com/problemset/problem/2027/C
Add Zeros
1,500
[ "brute force", "data structures", "dfs and similar", "dp", "graphs", "greedy" ]
Div. 2
3,000
256
You're given an array $a$ initially containing $n$ integers. In one operation, you must do the following: * Choose a position $i$ such that $1 < i \le |a|$ and $a_i = |a| + 1 - i$, where $|a|$ is the current size of the array. * Append $i - 1$ zeros onto the end of $a$. After performing this operation as many t...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows. The first line of each test case contains $n$ ($1 \le n \le 3 \cdot 10^5$) β€” the length of the array $a$. The second line of each test case contains $n$ integer...
For each test case, output a single integer β€” the maximum possible length of $a$ after performing some sequence of operations.
[ [ "4\n5\n2 4 6 2 5\n5\n5 4 4 5 1\n4\n6 8 2 3\n1\n1", "10\n11\n10\n1" ] ]
In the first test case, we can first choose $i = 4$, since $a_4 = 5 + 1 - 4 = 2$. After this, the array becomes $[2, 4, 6, 2, 5, 0, 0, 0]$. We can then choose $i = 3$ since $a_3 = 8 + 1 - 3 = 6$. After this, the array becomes $[2, 4, 6, 2, 5, 0, 0, 0, 0, 0]$, which has a length of $10$. It can be shown that no sequence...
Title: Add Zeros time_limit_ms: 3000 memory_limit_mb: 256 Description: You're given an array $a$ initially containing $n$ integers. In one operation, you must do the following: * Choose a position $i$ such that $1 < i \le |a|$ and $a_i = |a| + 1 - i$, where $|a|$ is the current size of the array. * Append $i - 1$...
2020E
https://codeforces.com/problemset/problem/2020/E
Expected Power
2,000
[ "bitmasks", "dp", "math", "probabilities" ]
Div. 2
4,000
256
You are given an array of $n$ integers $a_1,a_2,\ldots,a_n$. You are also given an array $p_1, p_2, \ldots, p_n$. Let $S$ denote the random multiset (i. e., it may contain equal elements) constructed as follows: * Initially, $S$ is empty. * For each $i$ from $1$ to $n$, insert $a_i$ into $S$ with probability $\f...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$). The second line of each test case contains $n$ integers $a_1,a_2,\l...
For each test case, output the expected value of $(f(S))^2$, modulo $10^9 + 7$.
[ [ "4\n2\n1 2\n5000 5000\n2\n1 1\n1000 2000\n6\n343 624 675 451 902 820\n6536 5326 7648 2165 9430 5428\n1\n1\n10000", "500000007\n820000006\n280120536\n1" ] ]
In the first test case, $a = [1, 2]$ and each element is inserted into $S$ with probability $\frac{1}{2}$, since $p_1 = p_2 = 5000$ and $\frac{p_i}{10^4} = \frac{1}{2}$. Thus, there are $4$ outcomes for $S$, each happening with the same probability of $\frac{1}{4}$: * $S = \varnothing$. In this case, $f(S) = 0$, $(f...
Title: Expected Power time_limit_ms: 4000 memory_limit_mb: 256 Description: You are given an array of $n$ integers $a_1,a_2,\ldots,a_n$. You are also given an array $p_1, p_2, \ldots, p_n$. Let $S$ denote the random multiset (i. e., it may contain equal elements) constructed as follows: * Initially, $S$ is empty. ...
2030G1
https://codeforces.com/problemset/problem/2030/G1
The Destruction of the Universe (Easy Version)
2,900
[ "combinatorics", "greedy", "math" ]
Div. 2
4,000
512
This is the easy version of the problem. In this version, $n \leq 5000$. You can only make hacks if both versions of the problem are solved. Orangutans are powerful beingsβ€”so powerful that they only need $1$ unit of time to destroy every vulnerable planet in the universe! There are $n$ planets in the universe. Each p...
The first line contains an integer $t$ ($1 \leq t \leq 1000$) β€” the number of test cases. The first line of each test case contains an integer $n$ ($1 \leq n \leq 5000$) β€” the number of planets in the universe. The following $n$ lines contain two integers $l_i$ and $r_i$ ($1 \leq l_i \leq r_i \leq n$) β€” the initial i...
For each test case, output an integer β€” the sum of scores to destroy all non- empty subsets of the planets in the universe, modulo $998\,244\,353$.
[ [ "3\n3\n1 1\n2 3\n3 3\n4\n1 4\n2 3\n2 4\n1 1\n5\n1 2\n2 3\n3 4\n4 5\n1 5", "5\n6\n24" ] ]
In the first testcase, there are seven non-empty subsets of planets we must consider: * For each of the subsets $\\{[1,1]\\}, \\{[2,3]\\}, \\{[3,3]\\}$, the score is $0$. * For the subset $\\{[2,3], [3,3]\\}$, the score is $0$, because the point $3$ is already contained in both planets' interval of vulnerability....
Title: The Destruction of the Universe (Easy Version) time_limit_ms: 4000 memory_limit_mb: 512 Description: This is the easy version of the problem. In this version, $n \leq 5000$. You can only make hacks if both versions of the problem are solved. Orangutans are powerful beingsβ€”so powerful that they only need $1$ uni...
2008G
https://codeforces.com/problemset/problem/2008/G
Sakurako's Task
1,800
[ "binary search", "greedy", "math", "number theory" ]
Div. 3
2,000
256
Sakurako has prepared a task for you: She gives you an array of $n$ integers and allows you to choose $i$ and $j$ such that $i \neq j$ and $a_i \ge a_j$, and then assign $a_i = a_i - a_j$ or $a_i = a_i + a_j$. You can perform this operation any number of times for any $i$ and $j$, as long as they satisfy the condition...
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1\le n\le 2\cdot 10^5,1\le k\le 10^9$) β€” the number of elements in the array and the value $k$ for $mex_k$. The second line of each test case contains $n$ ...
For each test case, output the maximum $mex_k$ that can be achieved through the operations.
[ [ "6\n1 3\n3\n2 10\n1 1\n3 1\n1 2 3\n3 2\n1 2 4\n4 5\n2 2 2 16\n4 5\n2 2 2 3", "2\n11\n3\n4\n8\n8" ] ]
Title: Sakurako's Task time_limit_ms: 2000 memory_limit_mb: 256 Description: Sakurako has prepared a task for you: She gives you an array of $n$ integers and allows you to choose $i$ and $j$ such that $i \neq j$ and $a_i \ge a_j$, and then assign $a_i = a_i - a_j$ or $a_i = a_i + a_j$. You can perform this operation a...
1999C
https://codeforces.com/problemset/problem/1999/C
Showering
800
[ "greedy", "implementation" ]
Div. 4
2,000
256
As a computer science student, Alex faces a hard challenge β€” showering. He tries to shower daily, but despite his best efforts there are always challenges. He takes $s$ minutes to shower and a day only has $m$ minutes! He already has $n$ tasks planned for the day. Task $i$ is represented as an interval $(l_i$, $r_i)$,...
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains three integers $n$, $s$, and $m$ ($1 \leq n \leq 2 \cdot 10^5$; $1 \leq s, m \leq 10^9$) β€” the number of time intervals Alex already has planned, the amount of time Alex takes to ta...
For each test case output "YES" (without quotes) if Alex can take a shower for that given test case, and "NO" (also without quotes) otherwise. You can output "YES" and "NO" in any case (for example, strings "yEs", "yes", and "Yes" will be recognized as a positive response).
[ [ "4\n3 3 10\n3 5\n6 8\n9 10\n3 3 10\n1 2\n3 5\n6 7\n3 3 10\n1 2\n3 5\n6 8\n3 4 10\n1 2\n6 7\n8 9", "YES\nYES\nNO\nYES" ] ]
Title: Showering time_limit_ms: 2000 memory_limit_mb: 256 Description: As a computer science student, Alex faces a hard challenge β€” showering. He tries to shower daily, but despite his best efforts there are always challenges. He takes $s$ minutes to shower and a day only has $m$ minutes! He already has $n$ tasks plan...
1985H2
https://codeforces.com/problemset/problem/1985/H2
Maximize the Largest Component (Hard Version)
2,200
[ "data structures", "dfs and similar", "dp", "dsu", "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...
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 e...
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\n7\n11\n16\n22\n36" ] ]
In the fourth test case, it is optimal for Alex to set all cells in row $4$ and column $2$ to be '#'. Doing so will lead to the largest connected component of '#' having a size of $16$. In the fifth test case, it is optimal for Alex to set all cells in row $2$ and column $4$ to be '#'. Doing so will lead to the larges...
Title: Maximize the Largest Component (Hard 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 a...
2032A
https://codeforces.com/problemset/problem/2032/A
Circuit
800
[ "greedy", "implementation", "math", "number theory" ]
Div. 2
1,000
256
Alice has just crafted a circuit with $n$ lights and $2n$ switches. Each component (a light or a switch) has two states: on or off. The lights and switches are arranged in a way that: * Each light is connected to exactly two switches. * Each switch is connected to exactly one light. It's unknown which light each ...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 500$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 50$) β€” the number of lights in the circuit. The second line of...
For each test case, output two integers β€” the minimum and maximum number of lights, respectively, that can be turned on.
[ [ "5\n1\n0 0\n1\n0 1\n1\n1 1\n3\n0 0 1 0 1 0\n3\n0 1 1 1 0 0", "0 0\n1 1\n0 0\n0 2\n1 3" ] ]
In the first test case, there is only one light in the circuit, and no switch is on, so the light is certainly off. In the second test case, there is only one light in the circuit, but one switch connected to it is on, so the light is on. In the third test case, there is only one light in the circuit, and both switch...
Title: Circuit time_limit_ms: 1000 memory_limit_mb: 256 Description: Alice has just crafted a circuit with $n$ lights and $2n$ switches. Each component (a light or a switch) has two states: on or off. The lights and switches are arranged in a way that: * Each light is connected to exactly two switches. * Each swi...
1989A
https://codeforces.com/problemset/problem/1989/A
Catch the Coin
800
[ "implementation" ]
Div. 2
2,000
256
Monocarp visited a retro arcade club with arcade cabinets. There got curious about the "Catch the Coin" cabinet. The game is pretty simple. The screen represents a coordinate grid such that: * the X-axis is directed from left to right; * the Y-axis is directed from bottom to top; * the center of the screen ha...
The first line contains a single integer $n$ ($1 \le n \le 500$) β€” the number of coins. In the $i$-th of the next $n$ lines, two integers $x_i$ and $y_i$ ($-50 \le x_i, y_i \le 50$) are written β€” the coordinates of the $i$-th coin. The coordinates of all coins are different. No coin is located at $(0, 0)$.
For each coin, print "YES" if Monocarp can collect it. Otherwise, print "NO".
[ [ "5\n24 42\n-2 -1\n-1 -2\n0 -50\n15 0", "YES\nYES\nNO\nNO\nYES" ] ]
Pay attention to the second coin in the example. Monocarp can first move from $(0, 0)$ to $(-1, -1)$. Then the coin falls $1$ down and ends up at $(-2, -2)$. Finally, Monocarp moves to $(-2, -2)$ and collects the coin.
Title: Catch the Coin time_limit_ms: 2000 memory_limit_mb: 256 Description: Monocarp visited a retro arcade club with arcade cabinets. There got curious about the "Catch the Coin" cabinet. The game is pretty simple. The screen represents a coordinate grid such that: * the X-axis is directed from left to right; *...
2008F
https://codeforces.com/problemset/problem/2008/F
Sakurako's Box
1,400
[ "combinatorics", "math", "number theory" ]
Div. 3
2,000
256
Sakurako has a box with $n$ balls. Each ball has it's value. She wants to bet with her friend that if the friend randomly picks two balls from the box (it could be two distinct balls, but they may have the same value), the product of their values will be the same as the number that Sakurako guessed. Since Sakurako has...
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The first line of each test case contains a single integer $n$ ($2\le n\le 2\cdot 10^5$) β€” the number of elements in the array. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($0\le a_i\le 10^...
For each test case, output the value of $P \cdot Q^{-1}(\bmod 10^9+7)$.
[ [ "3\n3\n3 2 3\n4\n2 2 2 4\n5\n1 2 3 4 5", "7\n6\n500000012" ] ]
For the first test, Sakurako's friend can pick these pairs of balls: $(a_1,a_2)$, $(a_1,a_3)$, $(a_2,a_3)$. Their products equal to $3\cdot 2=6$ , $3\cdot 3=9$ , $3\cdot 2=6$ respectively, so the expected value is $\frac{6+9+6}{3}=7$. For the second test, Sakurako's friend can pick these pairs of balls: $(a_1,a_2)$, $...
Title: Sakurako's Box time_limit_ms: 2000 memory_limit_mb: 256 Description: Sakurako has a box with $n$ balls. Each ball has it's value. She wants to bet with her friend that if the friend randomly picks two balls from the box (it could be two distinct balls, but they may have the same value), the product of their valu...
2000A
https://codeforces.com/problemset/problem/2000/A
Primary Task
800
[ "implementation", "math", "strings" ]
Div. 3
1,000
256
Dmitry wrote down $t$ integers on the board, and that is good. He is sure that he lost an important integer $n$ among them, and that is bad. The integer $n$ had the form $\text{10^x}$ ($x \ge 2$), where the symbol '$\text{^}$' denotes exponentiation.. Something went wrong, and Dmitry missed the symbol '$\text{^}$' whe...
The first line of the input contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of integers on the board. The next $t$ lines each contain an integer $a$ ($1 \le a \le 10000$) β€” the next integer from the board.
For each integer on the board, output "YES" if it could have been the important integer and "NO" otherwise. You may output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.
[ [ "7\n100\n1010\n101\n105\n2033\n1019\n1002", "NO\nYES\nNO\nYES\nNO\nYES\nNO" ] ]
Title: Primary Task time_limit_ms: 1000 memory_limit_mb: 256 Description: Dmitry wrote down $t$ integers on the board, and that is good. He is sure that he lost an important integer $n$ among them, and that is bad. The integer $n$ had the form $\text{10^x}$ ($x \ge 2$), where the symbol '$\text{^}$' denotes exponentia...
1992A
https://codeforces.com/problemset/problem/1992/A
Only Pluses
800
[ "brute force", "constructive algorithms", "greedy", "math", "sortings" ]
Div. 3
1,000
256
Kmes has written three integers $a$, $b$ and $c$ in order to remember that he has to give Noobish_Monk $a \times b \times c$ bananas. Noobish_Monk has found these integers and decided to do the following at most $5$ times: * pick one of these integers; * increase it by $1$. For example, if $a = 2$, $b = 3$ and...
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 and only line of each test case contains three integers $a$, $b$ and $c$ ($1 \le a, b, c \le 10$) β€” Kmes's integers.
For each test case, output a single integer β€” the maximum amount of bananas Noobish_Monk can get.
[ [ "2\n2 3 4\n10 1 10", "100\n600" ] ]
Title: Only Pluses time_limit_ms: 1000 memory_limit_mb: 256 Description: Kmes has written three integers $a$, $b$ and $c$ in order to remember that he has to give Noobish_Monk $a \times b \times c$ bananas. Noobish_Monk has found these integers and decided to do the following at most $5$ times: * pick one of these ...
1995C
https://codeforces.com/problemset/problem/1995/C
Squaring
1,800
[ "brute force", "constructive algorithms", "greedy", "implementation", "math", "number theory" ]
Div. 2
2,000
256
ikrpprpp found an array $a$ consisting of integers. He likes justice, so he wants to make $a$ fair β€” that is, make it non-decreasing. To do that, he can perform an act of justice on an index $1 \le i \le n$ of the array, which will replace $a_i$ with $a_i ^ 2$ (the element at position $i$ with its square). For example,...
First line contains an integer $t$ ($1 \le t \le 1000$) β€” the number of test cases. It is followed by the description of test cases. For each test case, the first line contains an integer $n$ β€” size of the array $a$. The second line contains $n$ ($1 \le n \le 2 \cdot 10 ^5$) integers $a_1, a_2,\ldots, a_n$ ($1 \le a_i...
For each testcase, print an integer β€” minimum number of acts of justice required to make the array $a$ non-decreasing. If it is impossible to do that, print $-1$.
[ [ "7\n3\n1 2 3\n2\n3 2\n3\n3 1 5\n4\n1 1 2 3\n3\n4 3 2\n9\n16 2 4 2 256 2 4 2 8\n11\n10010 10009 10008 10007 10006 10005 10004 10003 10002 10001 10000", "0\n1\n-1\n0\n3\n15\n55" ] ]
In the first test case, there's no need to perform acts of justice. The array is fair on its own! In the third test case, it can be proven that the array cannot become non- decreasing. In the fifth test case, ikrpprppp can perform an act of justice on index 3, then an act of justice on index 2, and finally yet anothe...
Title: Squaring time_limit_ms: 2000 memory_limit_mb: 256 Description: ikrpprpp found an array $a$ consisting of integers. He likes justice, so he wants to make $a$ fair β€” that is, make it non-decreasing. To do that, he can perform an act of justice on an index $1 \le i \le n$ of the array, which will replace $a_i$ with...
2002C
https://codeforces.com/problemset/problem/2002/C
Black Circles
1,200
[ "brute force", "geometry", "greedy", "math" ]
Div. 1 + 2
2,000
256
There are $n$ circles on a two-dimensional plane. The $i$-th circle is centered at $(x_i,y_i)$. Initially, all circles have a radius of $0$. The circles' radii increase at a rate of $1$ unit per second. You are currently at $(x_s,y_s)$; your goal is to reach $(x_t,y_t)$ without touching the circumference of any circl...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1\le n\le10^5$) β€” the number of circles. The next $n$ lines each contain two integers $x_i$, $y_i$...
For each test case, output $\texttt{YES}$ if it is possible to reach the goal without touching the circle boundaries, and output $\texttt{NO}$ otherwise. You can output $\texttt{Yes}$ and $\texttt{No}$ in any case (for example, strings $\texttt{yEs}$, $\texttt{yes}$, $\texttt{Yes}$, and $\texttt{YES}$ will be recogniz...
[ [ "7\n3\n2 5\n2 14\n10 13\n4 9 9 7\n3\n10 11\n6 9\n12 12\n14 13 4 8\n1\n5 7\n12 6 11 13\n2\n1000000000 2\n2 1000000000\n1 1 2 2\n1\n999999998 1000000000\n999999999 999999999 1 1\n1\n1000000000 1\n1 1000000000 1 1\n10\n989237121 2397081\n206669655 527238537\n522705783 380636165\n532545346 320061691\n207818728 19...
In the first test case, a feasible way of movement is as follows. ![](CDN_BASE_URL/235d765e20897c623b3ac974eceac134)
Title: Black Circles time_limit_ms: 2000 memory_limit_mb: 256 Description: There are $n$ circles on a two-dimensional plane. The $i$-th circle is centered at $(x_i,y_i)$. Initially, all circles have a radius of $0$. The circles' radii increase at a rate of $1$ unit per second. You are currently at $(x_s,y_s)$; your g...
2030A
https://codeforces.com/problemset/problem/2030/A
A Gift From Orangutan
800
[ "constructive algorithms", "greedy", "math", "sortings" ]
Div. 2
1,000
256
While exploring the jungle, you have bumped into a rare orangutan with a bow tie! You shake hands with the orangutan and offer him some food and water. In return... The orangutan has gifted you an array $a$ of length $n$. Using $a$, you will construct two arrays $b$ and $c$, both containing $n$ elements, in the follow...
The first line contains $t$ ($1 \leq t \leq 100$) β€” the number of test cases. The first line of each test case contains an integer $n$ ($1 \leq n \leq 1000$) β€” the number of elements in $a$. The following line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq 1000$) β€” the elements of the array $a$. It ...
For each test case, output the maximum score that you can get.
[ [ "3\n1\n69\n3\n7 6 5\n5\n1 1 1 2 2", "0\n4\n4" ] ]
In the first test case, there is no other way to rearrange $a$. So, $b = [69]$ and $c = [69]$. The only possible score is $69 - 69 = 0$. In the second test case, you can rearrange $a$ as $[7, 5, 6]$. Here, $b = [7, 5, 5]$ and $c = [7, 7, 7]$. The score in this case is $(7 - 7) + (7 - 5) + (7 - 5) = 4$. It can be shown...
Title: A Gift From Orangutan time_limit_ms: 1000 memory_limit_mb: 256 Description: While exploring the jungle, you have bumped into a rare orangutan with a bow tie! You shake hands with the orangutan and offer him some food and water. In return... The orangutan has gifted you an array $a$ of length $n$. Using $a$, you...
1996B
https://codeforces.com/problemset/problem/1996/B
Scale
800
[ "greedy", "implementation" ]
Div. 3
2,000
256
Tina has a square grid with $n$ rows and $n$ columns. Each cell in the grid is either $0$ or $1$. Tina wants to reduce the grid by a factor of $k$ ($k$ is a divisor of $n$). To do this, Tina splits the grid into $k \times k$ nonoverlapping blocks of cells such that every cell belongs to exactly one block. Tina then r...
The first line contains $t$ ($1 \leq t \leq 100$) – the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \leq n \leq 1000$, $1 \le k \le n$, $k$ is a divisor of $n$) β€” the number of rows and columns of the grid, and the factor that Tina wants to reduce the grid by. Each of ...
For each test case, output the grid reduced by a factor of $k$ on a new line.
[ [ "4\n4 4\n0000\n0000\n0000\n0000\n6 3\n000111\n000111\n000111\n111000\n111000\n111000\n6 2\n001100\n001100\n111111\n111111\n110000\n110000\n8 1\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111\n11111111", "0\n01\n10\n010\n111\n100\n11111111\n11111111\n11111111\n11111111\n11111111\n1111...
Title: Scale time_limit_ms: 2000 memory_limit_mb: 256 Description: Tina has a square grid with $n$ rows and $n$ columns. Each cell in the grid is either $0$ or $1$. Tina wants to reduce the grid by a factor of $k$ ($k$ is a divisor of $n$). To do this, Tina splits the grid into $k \times k$ nonoverlapping blocks of ce...
2006D
https://codeforces.com/problemset/problem/2006/D
Iris and Adjacent Products
2,600
[ "data structures", "greedy", "implementation", "math" ]
Div. 1
3,000
256
Iris has just learned multiplication in her Maths lessons. However, since her brain is unable to withstand too complex calculations, she could not multiply two integers with the product greater than $k$ together. Otherwise, her brain may explode! Her teacher sets a difficult task every day as her daily summer holiday ...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 5\cdot 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains three integers $n$, $q$ and $k$ ($2 \leq n \leq 10^5$, $1 \leq q \leq 10^5$, $1 \leq k \...
For each test, output a single line containing $q$ integers β€” the minimum number of operations of type $2$ needed for each day.
[ [ "5\n3 1 1\n1 1 1\n1 3\n3 2 10\n1 10 9\n1 3\n2 3\n5 4 2\n2 2 2 2 2\n1 2\n2 4\n2 5\n1 5\n6 5 10\n3 2 5 10 10 1\n1 4\n3 6\n1 6\n2 5\n5 6\n10 10 10\n10 9 8 7 6 5 4 3 2 1\n1 10\n1 9\n1 8\n1 7\n2 10\n3 10\n4 10\n5 10\n3 9\n6 8", "0 \n0 1 \n1 1 2 2 \n1 1 1 1 0 \n3 3 4 3 2 2 1 1 2 1" ] ]
In the first test case, as Iris can always multiply $1$ and $1$ together, no operations are needed, so the answer is $0$. In the second test case, the first day's homework is $[1, 10, 9]$. Iris can rearrange its elements to get $[9, 1, 10]$, so no operations of type $2$ are needed. The second day's homework is $[10, 9...
Title: Iris and Adjacent Products time_limit_ms: 3000 memory_limit_mb: 256 Description: Iris has just learned multiplication in her Maths lessons. However, since her brain is unable to withstand too complex calculations, she could not multiply two integers with the product greater than $k$ together. Otherwise, her brai...
2033B
https://codeforces.com/problemset/problem/2033/B
Sakurako and Water
900
[ "brute force", "constructive algorithms", "greedy" ]
Div. 3
2,000
256
During her journey with Kosuke, Sakurako and Kosuke found a valley that can be represented as a matrix of size $n \times n$, where at the intersection of the $i$-th row and the $j$-th column is a mountain with a height of $a_{i,j}$. If $a_{i,j} < 0$, then there is a lake there. Kosuke is very afraid of water, so Sakur...
The first line contains a single integer $t$ ($1 \le t \le 200$) β€” the number of test cases. Each test case is described as follows: * The first line of each test case consists of a single number $n$ ($1 \le n \le 500$). * Each of the following $n$ lines consists of $n$ integers separated by spaces, which corres...
For each test case, output the minimum number of times Sakurako will have to use her magic so that all lakes disappear.
[ [ "4\n1\n1\n2\n-1 2\n3 0\n3\n1 2 3\n-2 1 -1\n0 0 -1\n5\n1 1 -1 -1 3\n-3 1 4 4 -4\n-1 -1 3 0 -5\n4 5 3 -3 -1\n3 1 -3 -1 5", "0\n1\n4\n19" ] ]
Title: Sakurako and Water time_limit_ms: 2000 memory_limit_mb: 256 Description: During her journey with Kosuke, Sakurako and Kosuke found a valley that can be represented as a matrix of size $n \times n$, where at the intersection of the $i$-th row and the $j$-th column is a mountain with a height of $a_{i,j}$. If $a_{...
2036B
https://codeforces.com/problemset/problem/2036/B
Startup
800
[ "greedy", "sortings" ]
Div. 3
2,000
256
Arseniy came up with another business plan β€” to sell soda from a vending machine! For this, he purchased a machine with $n$ shelves, as well as $k$ bottles, where the $i$-th bottle is characterized by the brand index $b_i$ and the cost $c_i$. You can place any number of bottles on each shelf, but all bottles on the sa...
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 $k$ ($1 \le n, k \le 2 \cdot 10^5$), where $n$ is the number of shelves in the machine, and $k$ is the number of bottles available to Arseniy. The next $k$ lines cont...
For each test case, output one integer β€” the maximum amount that Arseniy can earn.
[ [ "4\n3 3\n2 6\n2 7\n1 15\n1 3\n2 6\n2 7\n1 15\n6 2\n1 7\n2 5\n190000 1\n1 1000", "28\n15\n12\n1000" ] ]
In the first test case, Arseniy has $3$ shelves in the vending machine. He can place, for example, two bottles of the brand $2$ on the first shelf and a bottle of the brand $1$ on the second shelf. Then the total cost of the bottles would be $6 + 7 + 15 = 28$. In the second test case, he has only one shelf. It is not ...
Title: Startup time_limit_ms: 2000 memory_limit_mb: 256 Description: Arseniy came up with another business plan β€” to sell soda from a vending machine! For this, he purchased a machine with $n$ shelves, as well as $k$ bottles, where the $i$-th bottle is characterized by the brand index $b_i$ and the cost $c_i$. You can...
1998E1
https://codeforces.com/problemset/problem/1998/E1
Eliminating Balls With Merging (Easy Version)
2,200
[ "binary search", "brute force", "data structures", "divide and conquer", "greedy" ]
Div. 2
4,000
512
Drink water. β€” Sun Tzu, The Art of Becoming a Healthy Programmer This is the easy version of the problem. The only difference is that $x=n$ in this version. You must solve both versions to be able to hack. You are given two integers $n$ and $x$ ($x=n$). There are $n$ balls lined up in a row, numbered from $1$ to $n$...
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 $x$ ($1 \leq n \leq 2 \cdot 10^5; x = n$) β€” the number of balls and the smallest index $i$ for which you need to find $f(i)$. The second line of each test case contains $a_1, a...
For each test case, output $n-x+1$ space separated integers on a new line, where the $j$-th integer should represent $f(x+j-1)$.
[ [ "3\n5 5\n1 2 3 2 1\n7 7\n4 5 1 2 1 4 5\n11 11\n1 2 3 1 1 9 3 2 4 1 3", "3\n4\n4" ] ]
In the first test case, you are required to calculate $f(5)$. It can be shown that after $4$ operations, $S$ can contain $2$, $3$, or $4$. The following shows the operations required to make $S = \\{4\\}$. * Initially, $S = \\{1, 2, 3, 4, 5\\}$ and $a = [1, 2, 3, 2, 1]$. * Choose $l = 1$. Naturally, $r = 2$. Sinc...
Title: Eliminating Balls With Merging (Easy Version) time_limit_ms: 4000 memory_limit_mb: 512 Description: Drink water. β€” Sun Tzu, The Art of Becoming a Healthy Programmer This is the easy version of the problem. The only difference is that $x=n$ in this version. You must solve both versions to be able to hack. You ...
1983E
https://codeforces.com/problemset/problem/1983/E
I Love Balls
2,300
[ "combinatorics", "math", "probabilities" ]
Div. 2
2,000
256
Alice and Bob are playing a game. There are $n$ balls, out of which $k$ are special. Each ball has a value associated with it. The players play turn by turn. In each turn, the player randomly picks a ball and adds the value of the ball to their score, which is $0$ at the beginning of the game. The selected ball is rem...
There are multiple test cases. The first line of the input contains an integer $t$, the number of test cases ($1 \le t \le 2 \cdot 10^5$). Each test case description is on a new line. The first line of the test case contains two integers $n$ and $k$ in the respective order separated by a space ($1 \le k \le n \le 4 \c...
Output two integers per test case in a new line, the expected score of Alice and the expected score of Bob modulo $10^9+7$.
[ [ "1\n5 2\n10 20 5 15 25", "45 30" ], [ "5\n1 1\n732507\n2 2\n5817860 5398510\n5 1\n2122894 4951549 2750585 7821535 3214167\n8 4\n1405323 5069867 6883092 6972029 328406 2478975 7628890 9973340\n4 2\n9662050 3566134 3996473 9872255", "732507 0\n11216370 0\n810642660 210218077\n722402997 318336932...
In the first test case, Alice's expected score is $45$, and Bob's is $30$ at the end of the game.
Title: I Love Balls time_limit_ms: 2000 memory_limit_mb: 256 Description: Alice and Bob are playing a game. There are $n$ balls, out of which $k$ are special. Each ball has a value associated with it. The players play turn by turn. In each turn, the player randomly picks a ball and adds the value of the ball to their ...
1978D
https://codeforces.com/problemset/problem/1978/D
Elections
1,600
[ "data structures", "greedy", "implementation", "math" ]
Div. 2
2,000
256
Elections are taking place in Berland. There are $n$ candidates participating in the elections, numbered from $1$ to $n$. The $i$-th candidate has $a_i$ fans who will vote for him. Additionally, there are $c$ people who are undecided about their favorite candidate, let's call them undecided. Undecided people will vote ...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$ and $c$ ($1 \le n \le 2 \cdot 10^5$, $0 \le c \le 10^9$) β€” the number ...
For each test case, output $n$ integers, the $i$-th of which should be equal to the minimum number of candidates that need to be excluded from the elections for candidate number $i$ to win.
[ [ "5\n3 1\n2 0 3\n2 3\n0 10\n5 3\n5 4 3 2 1\n4 5\n3 10 7 1\n6 0\n2 2 2 3 3 3", "0 1 2\n1 0\n0 1 2 3 4\n1 0 2 3\n1 1 2 0 4 5" ] ]
In the first test case: * If all candidates are allowed, candidate number $1$ will receive $3$ votes ($1$ undecided person will vote for him), candidate number $2$ will receive $0$ votes, and candidate number $3$ will receive $3$ votes. Therefore, candidate number $1$ wins (he received the same number of votes as ca...
Title: Elections time_limit_ms: 2000 memory_limit_mb: 256 Description: Elections are taking place in Berland. There are $n$ candidates participating in the elections, numbered from $1$ to $n$. The $i$-th candidate has $a_i$ fans who will vote for him. Additionally, there are $c$ people who are undecided about their fav...
1975D
https://codeforces.com/problemset/problem/1975/D
Paint the Tree
1,700
[ "brute force", "dfs and similar", "dp", "greedy", "shortest paths", "trees" ]
Div. 1 + 2
2,000
256
378QAQ has a tree with $n$ vertices. Initially, all vertices are white. There are two chess pieces called $P_A$ and $P_B$ on the tree. $P_A$ and $P_B$ are initially located on vertices $a$ and $b$ respectively. In one step, 378QAQ will do the following in order: 1. Move $P_A$ to a neighboring vertex. If the target ...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\leq t\leq 10^4$). The description of the test cases follows. The first line of each test case contains one integer $n$ ($1\leq n\leq 2\cdot 10^5$). The second line of each test case contains two integers $a$ and $b$ ($1\l...
For each test case, output the minimum number of steps to paint all vertices blue.
[ [ "3\n2\n1 2\n1 2\n5\n1 2\n1 2\n1 3\n1 4\n1 5\n8\n5 4\n7 1\n1 5\n1 8\n8 3\n7 2\n8 6\n3 4", "2\n8\n13" ] ]
In the first test case, 378QAQ can paint all vertices blue in the following order: * Initially, $P_A$ is located on the vertex $1$, and $P_B$ is located on the vertex $2$. The vertex $1$ is painted red and the vertex $2$ is white. * 378QAQ moves $P_A$ to the vertex $2$ and paints it red. Then 378QAQ moves $P_B$ t...
Title: Paint the Tree time_limit_ms: 2000 memory_limit_mb: 256 Description: 378QAQ has a tree with $n$ vertices. Initially, all vertices are white. There are two chess pieces called $P_A$ and $P_B$ on the tree. $P_A$ and $P_B$ are initially located on vertices $a$ and $b$ respectively. In one step, 378QAQ will do the ...
1989C
https://codeforces.com/problemset/problem/1989/C
Two Movies
1,400
[ "greedy", "math" ]
Div. 2
2,000
256
A movie company has released $2$ movies. These $2$ movies were watched by $n$ people. For each person, we know their attitude towards the first movie (liked it, neutral, or disliked it) and towards the second movie. If a person is asked to leave a review for the movie, then: * if that person liked the movie, they w...
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 second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($-1 \le a_i \le 1$), where $a_i$ is equal to $-1$ if the first movie ...
For each test case, print a single integer β€” the maximum possible rating of the company, if for each person, choose which movie to leave a review on.
[ [ "4\n2\n-1 1\n-1 -1\n1\n-1\n-1\n5\n0 -1 1 0 1\n-1 1 0 0 1\n4\n-1 -1 -1 1\n-1 1 1 1", "0\n-1\n1\n1" ] ]
Title: Two Movies time_limit_ms: 2000 memory_limit_mb: 256 Description: A movie company has released $2$ movies. These $2$ movies were watched by $n$ people. For each person, we know their attitude towards the first movie (liked it, neutral, or disliked it) and towards the second movie. If a person is asked to leave a...
2029F
https://codeforces.com/problemset/problem/2029/F
Palindrome Everywhere
2,500
[ "constructive algorithms", "greedy" ]
Div. 1 + 2
2,000
512
You are given a cycle with $n$ vertices numbered from $0$ to $n-1$. For each $0\le i\le n-1$, there is an undirected edge between vertex $i$ and vertex $((i+1)\bmod n)$ with the color $c_i$ ($c_i=\texttt{R}$ or $\texttt{B}$). Determine whether the following condition holds for every pair of vertices $(i,j)$ ($0\le i<j...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^5$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $n$ ($3\leq n\leq10^6$) β€” the number of vertices in the cycle. The second line c...
For each test case, print "YES" (without quotes) if there is a palindrome route between any pair of nodes, and "NO" (without quotes) otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.
[ [ "7\n5\nRRRRR\n5\nRRRRB\n5\nRBBRB\n6\nRBRBRB\n6\nRRBBRB\n5\nRBRBR\n12\nRRBRRBRRBRRB", "YES\nYES\nYES\nNO\nNO\nYES\nNO" ] ]
In the first test case, it is easy to show that there is a palindrome route between any two vertices. In the second test case, for any two vertices, there exists a palindrome route with only red edges. In the third test case, the cycle is as follows: $0\color{red}{\overset{\texttt{R}}{\longleftrightarrow}}1\color{blu...
Title: Palindrome Everywhere time_limit_ms: 2000 memory_limit_mb: 512 Description: You are given a cycle with $n$ vertices numbered from $0$ to $n-1$. For each $0\le i\le n-1$, there is an undirected edge between vertex $i$ and vertex $((i+1)\bmod n)$ with the color $c_i$ ($c_i=\texttt{R}$ or $\texttt{B}$). Determine ...
2024A
https://codeforces.com/problemset/problem/2024/A
Profitable Interest Rate
800
[ "greedy", "math" ]
Div. 2
1,000
256
Alice has $a$ coins. She can open a bank deposit called "Profitable", but the minimum amount required to open this deposit is $b$ coins. There is also a deposit called "Unprofitable", which can be opened with any amount of coins. Alice noticed that if she opens the "Unprofitable" deposit with $x$ coins, the minimum am...
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. A single line of each test case contains two integers $a$ and $b$ ($1 \le a, b \le 10^9$) β€” the number of coins Alice has and the initial m...
For each test case, output a single integer β€” the maximum number of coins that Alice can deposit into the "Profitable" deposit. If Alice can never open the "Profitable" deposit, output $0$.
[ [ "5\n10 5\n7 9\n5 100\n1 1\n1 2", "10\n5\n0\n1\n0" ] ]
In the first test case, $a \ge b$, so Alice can immediately open the "Profitable" deposit with all $10$ coins. In the second test case, Alice can open the "Unprofitable" deposit with $2$ coins. Then she will have $5$ coins left, but the minimum amount required to open the "Profitable" deposit will decrease by $4$ coin...
Title: Profitable Interest Rate time_limit_ms: 1000 memory_limit_mb: 256 Description: Alice has $a$ coins. She can open a bank deposit called "Profitable", but the minimum amount required to open this deposit is $b$ coins. There is also a deposit called "Unprofitable", which can be opened with any amount of coins. Ali...
1990A
https://codeforces.com/problemset/problem/1990/A
Submission Bait
900
[ "brute force", "games", "greedy", "sortings" ]
Div. 2
1,000
256
Alice and Bob are playing a game in an array $a$ of size $n$. They take turns to do operations, with Alice starting first. The player who can not operate will lose. At first, a variable $mx$ is set to $0$. In one operation, a player can do: * Choose an index $i$ ($1 \le i \le n$) such that $a_{i} \geq mx$ and set ...
The first line contains an integer $t$ ($1 \leq t \leq 10^3$) β€” the number of test cases. For each test case: * The first line contains an integer $n$ ($2 \leq n \leq 50$) β€” the size of the array. * The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \leq a_i \leq n$) β€” the elements of the array.
For each test case, if Alice has a winning strategy, output "YES". 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.
[ [ "5\n2\n2 1\n2\n1 1\n3\n3 3 3\n4\n3 3 4 4\n4\n1 2 2 2", "YES\nNO\nYES\nNO\nYES" ] ]
In the first test case, Alice can choose $i=1$ since $a_1=2 \ge mx=0$. After Alice's operation, $a=[0,1]$ and $mx=2$. Bob can not do any operation. Alice wins. In the second test case, Alice doesn't have a winning strategy. For example, if Alice chooses $i=1$, after Alice's operation: $a=[0,1]$ and $mx=1$. Then, Bob...
Title: Submission Bait time_limit_ms: 1000 memory_limit_mb: 256 Description: Alice and Bob are playing a game in an array $a$ of size $n$. They take turns to do operations, with Alice starting first. The player who can not operate will lose. At first, a variable $mx$ is set to $0$. In one operation, a player can do: ...
1973A
https://codeforces.com/problemset/problem/1973/A
Chess For Three
900
[ "brute force", "dp", "implementation", "math" ]
Div. 2
1,000
256
Three friends gathered to play a few games of chess together. In every game, two of them play against each other. The winner gets $2$ points while the loser gets $0$, and in case of a draw, both players get $1$ point each. Note that the same pair of players could have played any non-negative number of times (possibly ...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows. The first line of each test case contains three integers $p_1$, $p_2$ and $p_3$ ($0 \leq p_1 \leq p_2 \leq p_3 \leq 30$) β€” the scores of the three players, sorted...
For each testcase, print one number β€” the maximum possible number of draws that could've happened, or $-1$ if the scores aren't consistent with any valid set of games and results.
[ [ "7\n0 0 0\n0 1 1\n1 1 1\n1 1 2\n3 3 3\n3 4 5\n1 1 10", "0\n1\n-1\n2\n-1\n6\n2" ] ]
In the first example, no games were played at all, so no draws could occur either. For the second example, exactly one game occurred between the second and the third player and it ended in draw, so the answer is $1$. It's easy to see that there's no set of games achieving the scores in third example, so the answer fo...
Title: Chess For Three time_limit_ms: 1000 memory_limit_mb: 256 Description: Three friends gathered to play a few games of chess together. In every game, two of them play against each other. The winner gets $2$ points while the loser gets $0$, and in case of a draw, both players get $1$ point each. Note that the same ...
1972A
https://codeforces.com/problemset/problem/1972/A
Contest Proposal
800
[ "brute force", "greedy", "two pointers" ]
Div. 2
1,000
256
A contest contains $n$ problems and the difficulty of the $i$-th problem is expected to be at most $b_i$. There are already $n$ problem proposals and the difficulty of the $i$-th problem is $a_i$. Initially, both $a_1, a_2, \ldots, a_n$ and $b_1, b_2, \ldots, b_n$ are sorted in non-decreasing order. Some of the proble...
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 only one positive integer $n$ ($1 \leq n \leq 100$), representing the number of problems. The second line of each test ca...
For each test case, print an integer as your answer in a new line.
[ [ "2\n6\n1000 1400 2000 2000 2200 2700\n800 1200 1500 1800 2200 3000\n6\n4 5 6 7 8 9\n1 2 3 4 5 6", "2\n3" ] ]
In the first test case: * Propose a problem with difficulty $w=800$ and $a$ becomes $[800,1000,1400,2000,2000,2200]$. * Propose a problem with difficulty $w=1800$ and $a$ becomes $[800,1000,1400,1800,2000,2000]$. It can be proved that it's impossible to reach the goal by proposing fewer new problems. In the se...
Title: Contest Proposal time_limit_ms: 1000 memory_limit_mb: 256 Description: A contest contains $n$ problems and the difficulty of the $i$-th problem is expected to be at most $b_i$. There are already $n$ problem proposals and the difficulty of the $i$-th problem is $a_i$. Initially, both $a_1, a_2, \ldots, a_n$ and $...
2007B
https://codeforces.com/problemset/problem/2007/B
Index and Maximum Value
900
[ "data structures", "greedy" ]
Div. 2
1,000
256
After receiving yet another integer array $a_1, a_2, \ldots, a_n$ at her birthday party, Index decides to perform some operations on it. Formally, there are $m$ operations that she is going to perform in order. Each of them belongs to one of the two types: * $\texttt{+ l r}$. Given two integers $l$ and $r$, for all...
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 2 \cdot 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1 \leq n \leq 10^5$, $1 \leq m \leq 10^5$) β€” the length of t...
For each test case, output one single line containing $m$ integers, with the $i$-th of them describing the maximum value of the array after the $i$-th operation.
[ [ "5\n5 5\n1 2 3 2 1\n+ 1 3\n- 2 3\n+ 1 2\n+ 2 4\n- 6 8\n5 5\n1 3 3 4 5\n+ 1 4\n+ 2 3\n- 4 5\n- 3 3\n- 2 6\n5 5\n1 1 1 1 1\n+ 2 3\n- 4 5\n+ 1 6\n- 2 5\n+ 1 8\n1 1\n1\n- 1 1\n1 1\n1000000000\n+ 1000000000 1000000000", "4 4 4 5 5\n5 5 4 4 3\n1 1 2 1 2\n0\n1000000001" ] ]
In the first test case, the process of the operations is listed below: * After the first operation, the array becomes equal $[2,3,4,3,2]$. The maximum value is $4$. * After the second operation, the array becomes equal $[1,2,4,2,1]$. The maximum value is $4$. * After the third operation, the array becomes equa...
Title: Index and Maximum Value time_limit_ms: 1000 memory_limit_mb: 256 Description: After receiving yet another integer array $a_1, a_2, \ldots, a_n$ at her birthday party, Index decides to perform some operations on it. Formally, there are $m$ operations that she is going to perform in order. Each of them belongs to...
2013F2
https://codeforces.com/problemset/problem/2013/F2
Game in Tree (Hard Version)
3,500
[ "binary search", "data structures", "trees" ]
Div. 2
4,000
256
This is the hard version of the problem. In this version, it is not guaranteed that $u = v$. You can make hacks only if both versions of the problem are solved. Alice and Bob are playing a fun game on a tree. This game is played on a tree with $n$ vertices, numbered from $1$ to $n$. Recall that a tree with $n$ vertice...
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$ ...
For each test case, output $m$ lines. In the $i$-th line, print the winner of the game if Alice starts at vertex $1$ and Bob starts at vertex $p_i$. Print "Alice" (without quotes) if Alice wins, or "Bob" (without quotes) otherwise.
[ [ "3\n3\n1 2\n2 3\n2 3\n6\n1 2\n1 3\n2 4\n2 5\n1 6\n4 5\n4\n1 2\n1 3\n2 4\n2 4", "Bob\nAlice\nAlice\nBob\nAlice\nBob\nAlice" ] ]
![](CDN_BASE_URL/45f5cd537988c3a64939e74c3b13efab) Tree from the first example. In the first test case, the path will be ($2,3$). If Bob starts at vertex $2$, Alice will not be able to move anywhere on her first turn and will lose. However, if Bob starts at vertex $3$, Alice will move to vertex $2$, and Bob will have...
Title: Game in Tree (Hard Version) time_limit_ms: 4000 memory_limit_mb: 256 Description: This is the hard version of the problem. In this version, it is not guaranteed that $u = v$. You can make hacks only if both versions of the problem are solved. Alice and Bob are playing a fun game on a tree. This game is played o...
2004B
https://codeforces.com/problemset/problem/2004/B
Game with Doors
1,000
[ "brute force", "greedy" ]
Div. 2
2,000
256
There are $100$ rooms arranged in a row and $99$ doors between them; the $i$-th door connects rooms $i$ and $i+1$. Each door can be either locked or unlocked. Initially, all doors are unlocked. We say that room $x$ is reachable from room $y$ if all doors between them are unlocked. You know that: * Alice is in some...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $l$ and $r$ ($1 \le l < r \le 100$) β€” the bounds of the segment of rooms where Alice is located. The second line of each test case contains two integers $L$ and $R$ ($1 ...
For each test case, print a single integer β€” the smallest number of doors you have to lock so that Alice and Bob cannot meet, regardless of their starting positions inside the given segments.
[ [ "4\n1 2\n3 4\n2 5\n2 5\n3 7\n6 7\n4 5\n2 8", "1\n3\n2\n3" ] ]
In the first test case, it is sufficient to lock the door between rooms $2$ and $3$. In the second test case, the following doors have to be locked: $(2,3)$, $(3,4)$, $(4,5)$. In the third test case, the following doors have to be locked: $(5, 6)$ and $(6,7)$.
Title: Game with Doors time_limit_ms: 2000 memory_limit_mb: 256 Description: There are $100$ rooms arranged in a row and $99$ doors between them; the $i$-th door connects rooms $i$ and $i+1$. Each door can be either locked or unlocked. Initially, all doors are unlocked. We say that room $x$ is reachable from room $y$ ...
1972F
https://codeforces.com/problemset/problem/1972/F
Long Way to be Non-decreasing
2,800
[ "binary search", "dfs and similar", "graphs", "implementation" ]
Div. 2
4,000
512
Little R is a magician who likes non-decreasing arrays. She has an array of length $n$, initially as $a_1, \ldots, a_n$, in which each element is an integer between $[1, m]$. She wants it to be non-decreasing, i.e., $a_1 \leq a_2 \leq \ldots \leq a_n$. To do this, she can perform several magic tricks. Little R has a f...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1\le t\le 10^4$). The description of the test cases follows. The first line of each test case contains two integers $n$ and $m$ ($1\leq n \leq 10^6$, $1 \leq m \leq 10^6$) β€” the length of the initial array and the range of t...
For each test case, output a single integer: the minimum number of tricks needed, or $-1$ if it is impossible to make $a_1, \ldots, a_n$ non- decreasing.
[ [ "3\n5 8\n1 6 3 7 1\n2 3 5 8 7 1 5 6\n3 3\n1 3 2\n2 1 3\n10 10\n2 8 5 4 8 4 1 5 10 10\n6 7 2 6 3 4 1 1 3 5", "3\n-1\n3" ] ]
In the first case, the initial array $a_1, \ldots, a_n$ is $[1, 6, 3, 7, 1]$. You can choose $S$ as follows: * first trick: $S = [2, 4, 5]$, $a = [1, 1, 3, 5, 2]$; * second trick: $S = [5]$, $a = [1, 1, 3, 5, 3]$; * third trick: $S = [5]$, $a = [1, 1, 3, 5, 5]$. So it is possible to make $a_1, \ldots, a_n$ n...
Title: Long Way to be Non-decreasing time_limit_ms: 4000 memory_limit_mb: 512 Description: Little R is a magician who likes non-decreasing arrays. She has an array of length $n$, initially as $a_1, \ldots, a_n$, in which each element is an integer between $[1, m]$. She wants it to be non-decreasing, i.e., $a_1 \leq a_2...
2035E
https://codeforces.com/problemset/problem/2035/E
Monster
2,300
[ "binary search", "brute force", "constructive algorithms", "greedy", "implementation", "math", "ternary search" ]
Div. 1 + 2
2,000
256
Man, this Genshin boss is so hard. Good thing they have a top-up of $6$ coins for only $ \$4.99$. I should be careful and spend no more than I need to, lest my mom catches me... You are fighting a monster with $z$ health using a weapon with $d$ damage. Initially, $d=0$. You can perform the following operations. * I...
The first line contains a single integer $t$ ($1\le t\le 100$) β€” the number of test cases. The only line of each test case contains 4 integers $x$, $y$, $z$, and $k$ ($1\leq x, y, z, k\leq 10^8$) β€” the first operation's cost, the second operation's cost, the monster's health, and the limitation on the first operation.
For each test case, output the minimum number of coins needed to defeat the monster.
[ [ "4\n2 3 5 5\n10 20 40 5\n1 60 100 10\n60 1 100 10", "12\n190\n280\n160" ] ]
In the first test case, $x = 2$, $y = 3$, $z = 5$, and $k = 5$. Here's a strategy that achieves the lowest possible cost of $12$ coins: * Increase damage by $1$, costing $2$ coins. * Increase damage by $1$, costing $2$ coins. * Increase damage by $1$, costing $2$ coins. * Attack the monster, dealing $3$ dam...
Title: Monster time_limit_ms: 2000 memory_limit_mb: 256 Description: Man, this Genshin boss is so hard. Good thing they have a top-up of $6$ coins for only $ \$4.99$. I should be careful and spend no more than I need to, lest my mom catches me... You are fighting a monster with $z$ health using a weapon with $d$ damag...
1982E
https://codeforces.com/problemset/problem/1982/E
Number of k-good subarrays
2,300
[ "bitmasks", "brute force", "combinatorics", "divide and conquer", "dp", "math", "meet-in-the-middle" ]
Div. 2
2,000
256
Let $bit(x)$ denote the number of ones in the binary representation of a non-negative integer $x$. A subarray of an array is called $k$-good if it consists only of numbers with no more than $k$ ones in their binary representation, i.e., a subarray $(l, r)$ of array $a$ is good if for any $i$ such that $l \le i \le r$ ...
Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \le t \le 10^{4}$) β€” the number of test cases. The following lines describe the test cases. The single line of each test case contains two integers $n$, $k$ ($1 \le n \le 10^{18}, 1 \le k \le 60$).
For each test case, output a single integer β€” the number of $k$-good subarrays modulo $10^{9} + 7$.
[ [ "10\n6 1\n16 2\n1 1\n3 1\n31 3\n14 1\n1337 5\n100000 20\n795569939321040850 56\n576460752303423268 59", "7\n35\n1\n6\n155\n8\n7323\n49965\n741136395\n66679884" ] ]
For the first test case $a = [0, 1, 2, 3, 4, 5]$, $k = 1$. To find the answer, let's write all the numbers in binary representation: $$a = [\color{green}{000}, \color{green}{001}, \color{green}{010}, \color{red}{011}, \color{green}{100}, \color{red}{101}]$$ From this, it can be seen that the numbers $3$ and $5$ have...
Title: Number of k-good subarrays time_limit_ms: 2000 memory_limit_mb: 256 Description: Let $bit(x)$ denote the number of ones in the binary representation of a non-negative integer $x$. A subarray of an array is called $k$-good if it consists only of numbers with no more than $k$ ones in their binary representation, ...
1972D2
https://codeforces.com/problemset/problem/1972/D2
Reverse Card (Hard Version)
2,200
[ "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$; * $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...
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", "0\n1\n1\n6\n423\n5933961" ] ]
In the first test case, no pair satisfies the conditions. In the fourth test case, $(2,2),(3,6),(4,4),(6,3),(6,6),(8,8)$ satisfy the conditions.
Title: Reverse Card (Hard 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)$ satisf...
1981A
https://codeforces.com/problemset/problem/1981/A
Turtle and Piggy Are Playing a Game
800
[ "brute force", "greedy", "math" ]
Div. 2
1,000
256
Turtle and Piggy are playing a number game. First, Turtle will choose an integer $x$, such that $l \le x \le r$, where $l, r$ are given. It's also guaranteed that $2l \le r$. Then, Piggy will keep doing the following operation until $x$ becomes $1$: * Choose an integer $p$ such that $p \ge 2$ and $p \mid x$ (i.e. ...
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 $l, r$ ($1 \le l \le r \le 10^9, 2l \le r$) β€” The range where Turtle can choose the integer from.
For each test case, output a single integer β€” the maximum score.
[ [ "5\n2 4\n3 6\n2 15\n6 22\n114514 1919810", "2\n2\n3\n4\n20" ] ]
In the first test case, Turtle can choose an integer $x$, such that $2 \le x \le 4$. He can choose $x = 4$. Then Piggy can choose $p = 2$ for $2$ times. After that, $x$ will become $1$, and the score will be $2$, which is maximized. In the second test case, Turtle can choose an integer $3 \le x \le 6$. He can choose $...
Title: Turtle and Piggy Are Playing a Game time_limit_ms: 1000 memory_limit_mb: 256 Description: Turtle and Piggy are playing a number game. First, Turtle will choose an integer $x$, such that $l \le x \le r$, where $l, r$ are given. It's also guaranteed that $2l \le r$. Then, Piggy will keep doing the following oper...
1996D
https://codeforces.com/problemset/problem/1996/D
Fun
1,500
[ "binary search", "brute force", "combinatorics", "math", "number theory" ]
Div. 3
2,000
256
Counting is Fun! β€” satyam343 Given two integers $n$ and $x$, find the number of triplets ($a,b,c$) of positive integers such that $ab + ac + bc \le n$ and $a + b + c \le x$. Note that order matters (e.g. ($1, 1, 2$) and ($1, 2, 1$) are treated as different) and $a$, $b$, $c$ must be strictly greater than $0$.
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. Each test case contains two integers $n$ and $x$ ($1 \leq n,x \leq 10^6$). It is guaranteed that the sum of $n$ over all test cases does not exceed $10^6$ and that the sum of $x$ over all test cases does not exceed $10^6$...
Output a single integer β€” the number of triplets ($a,b,c$) of positive integers such that $ab + ac + bc \le n$ and $a + b + c \le x$.
[ [ "4\n7 4\n10 5\n7 1000\n900000 400000", "4\n10\n7\n1768016938" ] ]
In the first test case, the triplets are ($1, 1, 1$), ($1, 1, 2$), ($1, 2, 1$), and ($2, 1, 1$). In the second test case, the triplets are ($1, 1, 1$), ($1, 1, 2$), ($1, 1, 3$), ($1, 2, 1$), ($1, 2, 2$), ($1, 3, 1$), ($2, 1, 1$), ($2, 1, 2$), ($2, 2, 1$), and ($3, 1, 1$).
Title: Fun time_limit_ms: 2000 memory_limit_mb: 256 Description: Counting is Fun! β€” satyam343 Given two integers $n$ and $x$, find the number of triplets ($a,b,c$) of positive integers such that $ab + ac + bc \le n$ and $a + b + c \le x$. Note that order matters (e.g. ($1, 1, 2$) and ($1, 2, 1$) are treated as diffe...
2013A
https://codeforces.com/problemset/problem/2013/A
Zhan's Blender
800
[ "constructive algorithms", "math" ]
Div. 2
1,000
256
Today, a club fair was held at "NSPhM". In order to advertise his pastry club, Zhan decided to demonstrate the power of his blender. To demonstrate the power of his blender, Zhan has $n$ fruits. The blender can mix up to $x$ fruits per second. In each second, Zhan can put up to $y$ fruits into the blender. After tha...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). The description of the test cases follows. The first line of each test case contains one integer $n$ ($0 \le n \le 10^9$) β€” the number of fruits Zhan has. The second line of each test case contains two i...
For each testcase, output a single integer β€” the minimum number of seconds to blend all fruits.
[ [ "5\n5\n3 4\n3\n1 2\n6\n4 3\n100\n4 3\n9\n3 3", "2\n3\n2\n34\n3" ] ]
In the first example, you can first put $2$ fruits in the blender. After that, the blender will mix these $2$ fruits, and in the end, there will be $0$ fruits left in the blender. Then you can put $3$ fruits into the blender, after which the blender will mix these $3$ fruits. In the second example, you can put $1$ fru...
Title: Zhan's Blender time_limit_ms: 1000 memory_limit_mb: 256 Description: Today, a club fair was held at "NSPhM". In order to advertise his pastry club, Zhan decided to demonstrate the power of his blender. To demonstrate the power of his blender, Zhan has $n$ fruits. The blender can mix up to $x$ fruits per second...
1996C
https://codeforces.com/problemset/problem/1996/C
Sort
1,200
[ "dp", "greedy", "sortings", "strings" ]
Div. 3
5,000
256
You are given two strings $a$ and $b$ of length $n$. Then, you are (forced against your will) to answer $q$ queries. For each query, you are given a range bounded by $l$ and $r$. In one operation, you can choose an integer $i$ ($l \leq i \leq r$) and set $a_i = x$ where $x$ is any character you desire. Output the mini...
The first line contains $t$ ($1 \leq t \leq 1000$) – the number of test cases. The first line of each test case contains two integers $n$ and $q$ ($1 \leq n, q \leq 2 \cdot 10^5$) – the length of both strings and the number of queries. The following line contains $a$ of length $n$. It is guaranteed $a$ only contains ...
For each query, output an integer, the minimum number of operations you need to perform in a new line.
[ [ "3\n5 3\nabcde\nedcba\n1 5\n1 4\n3 3\n4 2\nzzde\nazbe\n1 3\n1 4\n6 3\nuwuwuw\nwuwuwu\n2 4\n1 3\n1 6", "0\n1\n0\n2\n2\n1\n1\n0" ] ]
For the first query, $\texttt{sorted(a[1..5])} =$ abcde and $\texttt{sorted(b[1..5])} =$ abcde, so no operations are necessary. For the second query, you need to set $a_1 = $ e. Then, $\texttt{sorted(a[1..4])} = \texttt{sorted(b[1..4])} = $ bcde.
Title: Sort time_limit_ms: 5000 memory_limit_mb: 256 Description: You are given two strings $a$ and $b$ of length $n$. Then, you are (forced against your will) to answer $q$ queries. For each query, you are given a range bounded by $l$ and $r$. In one operation, you can choose an integer $i$ ($l \leq i \leq r$) and se...
1984D
https://codeforces.com/problemset/problem/1984/D
''a'' String Problem
2,000
[ "brute force", "hashing", "implementation", "math", "string suffix structures", "strings" ]
Div. 1 + 2
2,000
256
You are given a string $s$ consisting of lowercase Latin characters. Count the number of nonempty strings $t \neq$ "$\texttt{a}$" such that it is possible to partition$^{\dagger}$ $s$ into some substrings satisfying the following conditions: * each substring either equals $t$ or "$\texttt{a}$", and * at least one...
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The only line of each test case contains a string $s$ consisting of lowercase Latin characters ($2 \leq |s| \leq 2 \cdot 10^5$). The sum of $|s|$ over all test cases does not exceed $3 \cdot 10^5$.
For each test case, output a single integer β€” the number of nonempty strings $t \neq$ "$\texttt{a}$" that satisfy all constraints.
[ [ "8\naaaaa\nbaba\ncabacb\naaabaaa\nbitset\nab\nabbaaaabbb\nyearnineteeneightyfour", "4\n4\n1\n16\n1\n2\n3\n1" ] ]
In the first test case, $t$ can be "$\texttt{aa}$", "$\texttt{aaa}$", "$\texttt{aaaa}$", or the full string. In the second test case, $t$ can be "$\texttt{b}$", "$\texttt{bab}$", "$\texttt{ba}$", or the full string. In the third test case, the only such $t$ is the full string.
Title: ''a'' String Problem time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a string $s$ consisting of lowercase Latin characters. Count the number of nonempty strings $t \neq$ "$\texttt{a}$" such that it is possible to partition$^{\dagger}$ $s$ into some substrings satisfying the following conditio...
1988D
https://codeforces.com/problemset/problem/1988/D
The Omnipotent Monster Killer
2,000
[ "brute force", "dfs and similar", "dp", "trees" ]
Div. 2
3,000
512
You, the monster killer, want to kill a group of monsters. The monsters are on a tree with $n$ vertices. On vertex with number $i$ ($1\le i\le n$), there is a monster with $a_i$ attack points. You want to battle with monsters for $10^{100}$ rounds. In each round, the following happens in order: 1. All living monste...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). 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 second line of each test case contains $n$ integers $a_1,\ldots,a_n$ ($1\le ...
For each test case, print one integer: the minimum possible health decrement.
[ [ "3\n1\n1000000000000\n5\n47 15 32 29 23\n1 2\n1 3\n2 4\n2 5\n7\n8 10 2 3 5 7 4\n1 2\n1 4\n3 2\n5 3\n6 2\n7 5", "1000000000000\n193\n57" ] ]
In the first test case, an optimal sequence of operations would be: * In the first round: first, receive the attack from the monster on vertex $1$, so your health decreases by $10^{12}$. Then kill the monster on vertex $1$. * In the second round to the $10^{100}$-th round: all monsters have been killed, so nothin...
Title: The Omnipotent Monster Killer time_limit_ms: 3000 memory_limit_mb: 512 Description: You, the monster killer, want to kill a group of monsters. The monsters are on a tree with $n$ vertices. On vertex with number $i$ ($1\le i\le n$), there is a monster with $a_i$ attack points. You want to battle with monsters for...
1989D
https://codeforces.com/problemset/problem/1989/D
Smithing Skill
1,900
[ "brute force", "data structures", "dp", "greedy", "math", "sortings", "two pointers" ]
Div. 2
3,000
256
You are playing a famous computer game (that just works) where you have various skills you can level up. Today, you focused on the "Smithing" skill. Your tactic is obvious: forging weapons from ingots and then melting them back to return the materials partially. For simplicity, every time you create an item, you get $1...
The first line contains two integers $n$ and $m$ ($1 \le n, m \le 10^6$) β€” the number of weapon classes and metal types. The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^6$), where $a_i$ is the number of ingots you need to forge one weapon of the $i$-th class. The third line contains $n...
Print one integer β€” the maximum total experience points you can gain by repeatedly forging and melting weapons.
[ [ "5 3\n9 6 7 5 5\n8 4 5 1 2\n10 4 7", "12" ], [ "3 4\n10 20 20\n0 0 0\n9 10 19 20", "8" ], [ "1 5\n3\n1\n1000000000 1000000000 1000000000 1000000000 1000000000", "4999999990" ] ]
In the first example, you can do the following: 1. craft one weapon of the $1$-st class from the $1$-st type of metal, spending $9$ ingots; 2. melt that weapon, returning $8$ ingots of the $1$-st metal type; 3. again, craft and melt one weapon of the $1$-st class from the $1$-st metal type; 4. craft and mel...
Title: Smithing Skill time_limit_ms: 3000 memory_limit_mb: 256 Description: You are playing a famous computer game (that just works) where you have various skills you can level up. Today, you focused on the "Smithing" skill. Your tactic is obvious: forging weapons from ingots and then melting them back to return the ma...
2035A
https://codeforces.com/problemset/problem/2035/A
Sliding
800
[ "implementation", "math" ]
Div. 1 + 2
1,000
256
Red was ejected. They were not the imposter. There are $n$ rows of $m$ people. Let the position in the $r$-th row and the $c$-th column be denoted by $(r, c)$. Number each person starting from $1$ in row-major order, i.e., the person numbered $(r-1)\cdot m+c$ is initially at $(r,c)$. The person at $(r, c)$ decides to...
The first line contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The only line of each testcase contains $4$ integers $n$, $m$, $r$, and $c$ ($1\le r\le n\le 10^6$, $1 \le c \le m \le 10^6$), where $n$ is the number of rows, $m$ is the number of columns, and $(r,c)$ is the position where the...
For each test case, output a single integer denoting the sum of the Manhattan distances.
[ [ "4\n2 3 1 2\n2 2 2 1\n1 1 1 1\n1000000 1000000 1 1", "6\n1\n0\n1999998000000" ] ]
For the first test case, the person numbered $2$ leaves, and the distances of the movements of the person numbered $3$, $4$, $5$, and $6$ are $1$, $3$, $1$, and $1$, respectively. So the answer is $1+3+1+1=6$. For the second test case, the person numbered $3$ leaves, and the person numbered $4$ moves. The answer is $1...
Title: Sliding time_limit_ms: 1000 memory_limit_mb: 256 Description: Red was ejected. They were not the imposter. There are $n$ rows of $m$ people. Let the position in the $r$-th row and the $c$-th column be denoted by $(r, c)$. Number each person starting from $1$ in row-major order, i.e., the person numbered $(r-1)\...
1993E
https://codeforces.com/problemset/problem/1993/E
Xor-Grid Problem
2,700
[ "bitmasks", "constructive algorithms", "dp", "implementation" ]
Div. 2
5,000
256
Given a matrix $a$ of size $n \times m$, each cell of which contains a non-negative integer. The integer lying at the intersection of the $i$-th row and the $j$-th column of the matrix is called $a_{i,j}$. Let's define $f(i)$ and $g(j)$ as the [XOR](https://en.wikipedia.org/wiki/Exclusive_or) of all integers in the $i...
The first line contains a single integer $t$ ($1 \le t \le 250$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 15$) β€” the number of rows and columns of $a$, respectively. The next $n$ lines, each containing $m$ integers $a_{i,1}, a_{i,2}, \ldots, a_{i,...
For each test case, print a single integer $b$ β€” the smallest possible $\textit{beauty}$ of the matrix.
[ [ "4\n1 2\n1 3\n2 3\n0 1 0\n5 4 4\n2 3\n0 2 4\n4 5 1\n3 3\n1 2 3\n4 5 6\n7 8 9", "1\n3\n13\n24" ] ]
Let's denote $r(i)$ as the first type operation applied on the $i$-th row, and $c(j)$ as the second type operation applied on the $j$-th column. In the first test case, you can apply an operation $c(1)$, which assigns $a_{1,1} := 1 \oplus 3 = 2$. Then, we'll receive this matrix: 2| 3 ---|--- In the second test...
Title: Xor-Grid Problem time_limit_ms: 5000 memory_limit_mb: 256 Description: Given a matrix $a$ of size $n \times m$, each cell of which contains a non-negative integer. The integer lying at the intersection of the $i$-th row and the $j$-th column of the matrix is called $a_{i,j}$. Let's define $f(i)$ and $g(j)$ as t...
1984F
https://codeforces.com/problemset/problem/1984/F
Reconstruction
2,500
[ "brute force", "dp", "math" ]
Div. 1 + 2
2,000
256
There is a hidden array $a_1, a_2, \ldots, a_n$ of length $n$ whose elements are integers between $-m$ and $m$, inclusive. You are given an array $b_1, b_2, \ldots, b_n$ of length $n$ and a string $s$ of length $n$ consisting of the characters $\texttt{P}$, $\texttt{S}$, and $\texttt{?}$. For each $i$ from $1$ to $n$...
The first line contains a single integer $t$ ($1 \leq t \leq 10^3$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($2 \leq n \leq 2 \cdot 10^3$, $2 \leq m \leq 10^{9}$) β€” the length of the hidden array $a_1, a_2, \ldots, a_n$ and the maximum absolute value of an element...
For each test case, output a single integer β€” the number of ways to replace all $\texttt{?}$ in $s$ with either $\texttt{P}$ or $\texttt{S}$ that result in the existence of a valid array $a_1, a_2, \ldots, a_n$, modulo $998\,244\,353$.
[ [ "6\n4 10\nPSPP\n1 9 8 10\n4 1000000000\n????\n1 1 1 4000000000\n8 1000000000\n?P?SSP?P\n-857095623 -1424391899 -851974476 673437144 471253851 -543483033 364945701 -178537332\n4 7\nPPSS\n4 2 1 3\n9 20\n?????????\n1 2 3 4 5 6 7 8 9\n3 1000000000\nP??\n-145463248 -974068460 -1287458396", "1\n0\n2\n1\n14\n1" ...
In the first test case, we can see that the following array satisfies all constraints, thus the answer is $1$: 1. $\texttt{P}$ β€” ${[\color{red}{\textbf{1}},3,4,2]}$: sum of $1$. 2. $\texttt{S}$ β€” ${[1,\color{red}{\textbf{3},4,2}]}$: sum of $9$. 3. $\texttt{P}$ β€” ${[\color{red}{1,3,\textbf{4}},2]}$: sum of $8$....
Title: Reconstruction time_limit_ms: 2000 memory_limit_mb: 256 Description: There is a hidden array $a_1, a_2, \ldots, a_n$ of length $n$ whose elements are integers between $-m$ and $m$, inclusive. You are given an array $b_1, b_2, \ldots, b_n$ of length $n$ and a string $s$ of length $n$ consisting of the characters...
2003B
https://codeforces.com/problemset/problem/2003/B
Turtle and Piggy Are Playing a Game 2
800
[ "games", "greedy", "sortings" ]
Div. 2
1,000
256
Turtle and Piggy are playing a game on a sequence. They are given a sequence $a_1, a_2, \ldots, a_n$, and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.). The game goes as follows: * Let the current length of the sequence be...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows. The first line of each test case contains a single integer $n$ ($2 \le n \le 10^5$) β€” the length of the sequence. The second line of each test case contains $n$...
For each test case, output a single integer β€” the value of $a_1$ in the end if both players play optimally.
[ [ "5\n2\n1 2\n3\n1 1 2\n3\n1 2 3\n5\n3 1 2 2 3\n10\n10 2 5 2 7 9 2 5 10 7", "2\n1\n2\n2\n7" ] ]
In the first test case, initially $a = [1, 2]$. Turtle can only choose $i = 1$. Then he will set $a_1$ to $\max(a_1, a_2) = 2$ and remove $a_2$. The sequence $a$ becomes $[2]$. Then the length of the sequence becomes $1$, and the game will end. The value of $a_1$ is $2$, so you should output $2$. In the second test ca...
Title: Turtle and Piggy Are Playing a Game 2 time_limit_ms: 1000 memory_limit_mb: 256 Description: Turtle and Piggy are playing a game on a sequence. They are given a sequence $a_1, a_2, \ldots, a_n$, and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turt...
1996F
https://codeforces.com/problemset/problem/1996/F
Bomb
1,900
[ "binary search", "greedy", "math" ]
Div. 3
2,000
256
Sparkle gives you two arrays $a$ and $b$ of length $n$. Initially, your score is $0$. In one operation, you can choose an integer $i$ and add $a_i$ to your score. Then, you must set $a_i$ = $\max(0, a_i - b_i)$. You only have time to perform $k$ operations before Sparkle sets off a nuclear bomb! What is the maximum sc...
The first line contains $t$ ($1 \leq t \leq 1000$) β€” the number of test cases. The first line of each test case contains $n$ and $k$ ($1 \leq n \leq 2 \cdot 10^5, 1 \leq k \leq 10^9$) β€” the length of the arrays and the number of operations you can perform. The following line contains $n$ integers $a_1, a_2, ... a_n$ ...
For each test case, output an integer, the maximum score you can acquire after $k$ operations.
[ [ "5\n3 4\n5 6 7\n2 3 4\n5 9\n32 52 68 64 14\n18 14 53 24 8\n5 1000\n1 2 3 4 5\n5 4 3 2 1\n1 1000000\n1000000\n1\n10 6\n3 3 5 10 6 8 6 8 7 7\n6 1 7 4 1 1 8 9 3 1", "21\n349\n27\n500000500000\n47" ] ]
Title: Bomb time_limit_ms: 2000 memory_limit_mb: 256 Description: Sparkle gives you two arrays $a$ and $b$ of length $n$. Initially, your score is $0$. In one operation, you can choose an integer $i$ and add $a_i$ to your score. Then, you must set $a_i$ = $\max(0, a_i - b_i)$. You only have time to perform $k$ operati...
2025C
https://codeforces.com/problemset/problem/2025/C
New Game
1,300
[ "binary search", "brute force", "greedy", "implementation", "sortings", "two pointers" ]
Div. 2
2,000
512
There's a new game Monocarp wants to play. The game uses a deck of $n$ cards, where the $i$-th card has exactly one integer $a_i$ written on it. At the beginning of the game, on the first turn, Monocarp can take any card from the deck. During each subsequent turn, Monocarp can take exactly one card that has either the...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $k$ ($1 \le k \le n \le 200\,000$) β€” the number of cards in the deck and the maximum number of distinct numbers that can be written on the cards that Monocarp tak...
For each test case, print the maximum number of cards that Monocarp can take from the deck during the game, given that on the first turn he can take any card from the deck.
[ [ "4\n10 2\n5 2 4 3 4 3 4 5 3 2\n5 1\n10 11 10 11 10\n9 3\n4 5 4 4 6 5 4 4 6\n3 2\n1 3 1", "6\n3\n9\n2" ] ]
In the first example, Monocarp needs to take any of the cards with the number $3$. On the next two turns, he needs to take the two remaining cards with the number $3$. On the next three turns, he needs to take three cards with the number $4$. After that, Monocarp will not be able to take any more cards from the deck, a...
Title: New Game time_limit_ms: 2000 memory_limit_mb: 512 Description: There's a new game Monocarp wants to play. The game uses a deck of $n$ cards, where the $i$-th card has exactly one integer $a_i$ written on it. At the beginning of the game, on the first turn, Monocarp can take any card from the deck. During each s...
2029H
https://codeforces.com/problemset/problem/2029/H
Message Spread
3,500
[ "bitmasks", "combinatorics", "dp" ]
Div. 1 + 2
12,000
1,024
Given is an undirected graph with $n$ vertices and $m$ edges. Each edge connects two vertices $(u, v)$ and has a probability of $\frac{p}{q}$ of appearing each day. Initially, vertex $1$ has a message. At the end of the day, a vertex has a message if and only if itself or at least one of the vertices adjacent to it ha...
The first line contains two integers $n$ and $m$ ($1\leq n\leq 21$, $n-1\leq m\leq\frac{n(n-1)}{2}$). Then $m$ lines follow, each containing four integers $u$, $v$, $p$, and $q$ ($1\leq u\neq v\leq n$, $1\leq p<q<998\,244\,353$, $\gcd(p,q)=1$) β€” there is an undirected edge between $u$ and $v$, and it has a probability...
Print a single integer in the only line of the output β€” the expected number of days, modulo $998\,244\,353$. Formally, let $M = 998\,244\,353$. It can be shown that the exact answer can be expressed as an irreducible fraction $\frac{p}{q}$, where $p$ and $q$ are integers and $q \not \equiv 0 \pmod{M}$. Output the inte...
[ [ "2 1\n1 2 1 10", "10" ], [ "3 3\n1 2 1 2\n1 3 1 2\n2 3 1 2", "887328316" ], [ "1 0", "0" ], [ "5 8\n1 2 1 11\n1 3 2 11\n1 4 3 11\n1 5 4 11\n2 4 5 11\n2 5 6 11\n3 4 7 11\n4 5 8 11", "469993557" ], [ "21 22\n1 2 3 4\n2 3 4 5\n3 4 5 6\n5 6 7 8\n6 7 8 9\n7 8 9 10\n8...
In the first test, the answer is equal to the expected number of days before the only edge in the graph first appears, and that is $\frac{1}{0.1}=10$. In the second test, the answer is equal to $\frac{20}{9}$ before it is taken modulo $998\,244\,353$. In the third test, the only vertex already has the message, so the...
Title: Message Spread time_limit_ms: 12000 memory_limit_mb: 1024 Description: Given is an undirected graph with $n$ vertices and $m$ edges. Each edge connects two vertices $(u, v)$ and has a probability of $\frac{p}{q}$ of appearing each day. Initially, vertex $1$ has a message. At the end of the day, a vertex has a m...
1974G
https://codeforces.com/problemset/problem/1974/G
Money Buys Less Happiness Now
2,000
[ "data structures", "greedy", "sortings" ]
Div. 3
2,000
256
You can never buy enough happiness, so here we go again! In this version, you can only buy $h_i = 1$ unit of happiness each month, but the number of months is hugely increased. We are in the realm of quantum happiness and time dilation. Being a physicist, Charlie likes to plan his life in simple and precise terms. Fo...
The first line of the input contains $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains two integers, $m$ and $x$ ($1 \le m \le 2 \cdot 10^5$, $1 \le x \le 10^3$) β€” the total number of months and the monthly salary. The second line of each test case contains $m$ integers ...
For each test case, output one integer β€” the maximal amount of happiness Charlie can get.
[ [ "6\n3 3\n2 2 2\n6 5\n2 2 8 2 6 8\n6 4\n4 10 3 8 6 10\n2 1\n1 1\n4 1\n4 1 3 1\n4 2\n1 3 4 3", "2\n4\n3\n1\n2\n1" ] ]
Title: Money Buys Less Happiness Now time_limit_ms: 2000 memory_limit_mb: 256 Description: You can never buy enough happiness, so here we go again! In this version, you can only buy $h_i = 1$ unit of happiness each month, but the number of months is hugely increased. We are in the realm of quantum happiness and time di...
2000D
https://codeforces.com/problemset/problem/2000/D
Right Left Wrong
1,200
[ "greedy", "implementation", "two pointers" ]
Div. 3
2,000
256
Vlad found a strip of $n$ cells, numbered from left to right from $1$ to $n$. In the $i$-th cell, there is a positive integer $a_i$ and a letter $s_i$, where all $s_i$ are either 'L' or 'R'. Vlad invites you to try to score the maximum possible points by performing any (possibly zero) number of operations. In one ope...
The first line contains one integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains one integer $n$ ($2 \le n \le 2 \cdot 10^5$) β€” the length of the strip. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le 10^5$) β€” the numbers...
For each test case, output one integer β€” the maximum possible number of points that can be scored.
[ [ "4\n6\n3 5 1 4 3 2\nLRLLLR\n2\n2 8\nLR\n2\n3 9\nRL\n5\n1 2 3 4 5\nLRLRR", "18\n10\n0\n22" ] ]
Title: Right Left Wrong time_limit_ms: 2000 memory_limit_mb: 256 Description: Vlad found a strip of $n$ cells, numbered from left to right from $1$ to $n$. In the $i$-th cell, there is a positive integer $a_i$ and a letter $s_i$, where all $s_i$ are either 'L' or 'R'. Vlad invites you to try to score the maximum possi...
1982C
https://codeforces.com/problemset/problem/1982/C
Boring Day
1,200
[ "binary search", "data structures", "dp", "greedy", "two pointers" ]
Div. 2
2,000
256
On another boring day, Egor got bored and decided to do something. But since he has no friends, he came up with a game to play. Egor has a deck of $n$ cards, the $i$-th card from the top has a number $a_i$ written on it. Egor wants to play a certain number of rounds until the cards run out. In each round, he takes a n...
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 the test cases. The first line of each test case contains three integers $n$, $l$, and $r$ ($1 \le n \le 10^{5}$, $1 \le l \le r \le 10^9$). The seco...
For each test case, output a single number β€” the maximum number of rounds Egor can win.
[ [ "8\n5 3 10\n2 1 11 3 7\n10 1 5\n17 8 12 11 7 11 21 13 10 8\n3 4 5\n3 4 2\n8 12 25\n10 7 5 13 8 9 12 7\n2 3 3\n5 2\n9 7 9\n2 10 5 1 3 7 6 2 3\n1 8 10\n9\n5 5 6\n1 4 2 6 4", "3\n0\n1\n4\n0\n3\n1\n2" ] ]
In the first test case, Egor can win $3$ rounds: * In the first round, take the top $2$ cards with values $2$ and $1$ and win, as their sum is $3$. After this, the deck will look like this: $[11, 3, 7]$. * In the second round, take the top card and lose, as its value $11$ is greater than $r = 10$. After this, the...
Title: Boring Day time_limit_ms: 2000 memory_limit_mb: 256 Description: On another boring day, Egor got bored and decided to do something. But since he has no friends, he came up with a game to play. Egor has a deck of $n$ cards, the $i$-th card from the top has a number $a_i$ written on it. Egor wants to play a certa...
1995B1
https://codeforces.com/problemset/problem/1995/B1
Bouquet (Easy Version)
1,100
[ "binary search", "brute force", "greedy", "sortings", "two pointers" ]
Div. 2
1,500
512
This is the easy version of the problem. The only difference is that in this version, the flowers are specified by enumeration. A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of $n$ flowers in the store, each of which is characterized by the number of petals, and a ...
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 o...
For each test case, output a single integer β€” the maximum possible number of petals in the bouquet that the girl can assemble while meeting all the conditions listed above.
[ [ "5\n5 10\n1 1 2 2 3\n8 20\n4 2 7 5 6 1 1 1\n8 100000\n239 30 610 122 24 40 8 2\n11 13\n2 4 11 1 1 2 3 5 4 3 2\n8 1033\n206 206 206 207 207 207 207 1000", "7\n13\n610\n13\n1033" ] ]
In the first test case, you can assemble a bouquet with $(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 third test case, you can assemble a bouquet with only one flower of any type, so the answer is $610$. In the fourth test case, you c...
Title: Bouquet (Easy Version) time_limit_ms: 1500 memory_limit_mb: 512 Description: This is the easy version of the problem. The only difference is that in this version, the flowers are specified by enumeration. A girl is preparing for her birthday and wants to buy the most beautiful bouquet. There are a total of $n$ ...
2030C
https://codeforces.com/problemset/problem/2030/C
A TRUE Battle
1,100
[ "brute force", "games", "greedy" ]
Div. 2
2,000
256
Alice and Bob are playing a game. There is a list of $n$ booleans, each of which is either true or false, given as a binary string $^{\text{βˆ—}}$ of length $n$ (where $\texttt{1}$ represents true, and $\texttt{0}$ represents false). Initially, there are no operators between the booleans. Alice and Bob will take alterna...
The first line contains $t$ ($1 \leq t \leq 10^4$) β€” the number of test cases. The first line of each test case contains an integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) β€” the length of the string. The second line contains a binary string of length $n$, consisting of characters $\texttt{0}$ and $\texttt{1}$ β€” the list o...
For each testcase, output "YES" (without quotes) if Alice wins, 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).
[ [ "5\n2\n11\n3\n010\n12\n101111111100\n10\n0111111011\n8\n01000010", "YES\nNO\nYES\nYES\nNO" ] ]
In the first testcase, Alice can place and between the two booleans. The game ends as there are no other places to place operators, and Alice wins because true and true is true. In the second testcase, Alice can place or between the middle true and the left false. Bob can place and between the middle true and the righ...
Title: A TRUE Battle time_limit_ms: 2000 memory_limit_mb: 256 Description: Alice and Bob are playing a game. There is a list of $n$ booleans, each of which is either true or false, given as a binary string $^{\text{βˆ—}}$ of length $n$ (where $\texttt{1}$ represents true, and $\texttt{0}$ represents false). Initially, th...
1978E
https://codeforces.com/problemset/problem/1978/E
Computing Machine
2,000
[ "brute force", "data structures", "dp", "greedy", "implementation" ]
Div. 2
2,000
256
Sasha has two binary strings $s$ and $t$ of the same length $n$, consisting of the characters 0 and 1. There is also a computing machine that can perform two types of operations on binary strings $a$ and $b$ of the same length $k$: 1. If $a_{i} = a_{i + 2} =$ 0, then you can assign $b_{i + 1} :=$ 1 ($1 \le i \le k ...
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 \le n \le 2 \cdot 10^5$) β€” the length of the strings $s$ and $t$. The...
For each test case, output $q$ integers β€” the answers to all queries.
[ [ "3\n4\n1111\n0000\n2\n1 2\n2 4\n4\n1010\n1101\n2\n1 3\n1 4\n6\n010101\n011010\n5\n2 3\n1 6\n2 5\n4 4\n3 6", "2\n3\n2\n3\n1\n4\n3\n1\n2" ] ]
In the first test case: * In the first query, $a =$ 11, so the maximum number of 1 characters is $2$. * In the second query, $a =$ 111, so the maximum number of 1 characters is $3$. In the second test case: * In the first query, $a =$ 101 and $b =$ 110. No operations can be performed, so the maximum number o...
Title: Computing Machine time_limit_ms: 2000 memory_limit_mb: 256 Description: Sasha has two binary strings $s$ and $t$ of the same length $n$, consisting of the characters 0 and 1. There is also a computing machine that can perform two types of operations on binary strings $a$ and $b$ of the same length $k$: 1. If...
1971F
https://codeforces.com/problemset/problem/1971/F
Circle Perimeter
1,600
[ "binary search", "brute force", "dfs and similar", "geometry", "implementation", "math" ]
Div. 4
1,000
256
Given an integer $r$, find the number of lattice points that have a Euclidean distance from $(0, 0)$ greater than or equal to $r$ but strictly less than $r+1$. A lattice point is a point with integer coordinates. The Euclidean distance from $(0, 0)$ to the point $(x,y)$ is $\sqrt{x^2 + y^2}$.
The first line contains a single integer $t$ ($1 \leq t \leq 1000$) β€” the number of test cases. The only line of each test case contains a single integer $r$ ($1 \leq r \leq 10^5$). The sum of $r$ over all test cases does not exceed $10^5$.
For each test case, output a single integer β€” the number of lattice points that have an Euclidean distance $d$ from $(0, 0)$ such that $r \leq d < r+1$.
[ [ "6\n1\n2\n3\n4\n5\n1984", "8\n16\n20\n24\n40\n12504" ] ]
The points for the first three test cases are shown below. ![](CDN_BASE_URL/798fdc9e24a49612fd77928f7fc973f8)
Title: Circle Perimeter time_limit_ms: 1000 memory_limit_mb: 256 Description: Given an integer $r$, find the number of lattice points that have a Euclidean distance from $(0, 0)$ greater than or equal to $r$ but strictly less than $r+1$. A lattice point is a point with integer coordinates. The Euclidean distance from ...
2028A
https://codeforces.com/problemset/problem/2028/A
Alice's Adventures in ''Chess''
800
[ "brute force", "implementation", "math", "strings" ]
Div. 2
1,000
256
Alice is trying to meet up with the Red Queen in the countryside! Right now, Alice is at position $(0, 0)$, and the Red Queen is at position $(a, b)$. Alice can only move in the four cardinal directions (north, east, south, west). More formally, if Alice is at the point $(x, y)$, she will do one of the following: *...
Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 500$). The description of the test cases follows. The first line of each test case contains three integers $n$, $a$, $b$ ($1 \le n$, $a$, $b \le 10$) β€” the length of the string and the initial coordinates of the ...
For each test case, output a single string "YES" or "NO" (without the quotes) denoting whether Alice will eventually meet the Red Queen. 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.
[ [ "6\n2 2 2\nNE\n3 2 2\nNNE\n6 2 1\nNNEESW\n6 10 10\nNNEESW\n3 4 2\nNEE\n4 5 5\nNEWS", "YES\nNO\nYES\nYES\nYES\nNO" ] ]
In the first test case, Alice follows the path $(0,0) \xrightarrow[\texttt{N}]{} (0,1) \xrightarrow[\texttt{E}]{} (1,1) \xrightarrow[\texttt{N}]{} (1,2) \xrightarrow[\texttt{E}]{} (2,2)$. In the second test case, Alice can never reach the Red Queen.
Title: Alice's Adventures in ''Chess'' time_limit_ms: 1000 memory_limit_mb: 256 Description: Alice is trying to meet up with the Red Queen in the countryside! Right now, Alice is at position $(0, 0)$, and the Red Queen is at position $(a, b)$. Alice can only move in the four cardinal directions (north, east, south, wes...
2026B
https://codeforces.com/problemset/problem/2026/B
Black Cells
1,300
[ "binary search", "brute force", "constructive algorithms", "greedy" ]
Div. 2
2,000
256
You are given a strip divided into cells, numbered from left to right from $0$ to $10^{18}$. Initially, all cells are white. You can perform the following operation: choose two white cells $i$ and $j$, such that $i \ne j$ and $|i - j| \le k$, and paint them black. A list $a$ is given. All cells from this list must be...
The first line contains a single integer $t$ ($1 \le t \le 500$) β€” 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$ ($0 < a_i < 10^{18}$; $a_i < a_{i + 1}$). Additional constraint on the input: ...
For each test case, print a single integer β€” the minimum value of $k$ for which it is possible to paint all the given cells black.
[ [ "4\n2\n1 2\n1\n7\n3\n2 4 9\n5\n1 5 8 10 13", "1\n1\n2\n3" ] ]
In the first example, with $k=1$, it is possible to paint the cells $(1, 2)$. In the second example, with $k=1$, it is possible to paint the cells $(7, 8)$. In the third example, with $k=2$, it is possible to paint the cells $(2, 4)$ and $(8, 9)$. In the fourth example, with $k=3$, it is possible to paint the cells ...
Title: Black Cells time_limit_ms: 2000 memory_limit_mb: 256 Description: You are given a strip divided into cells, numbered from left to right from $0$ to $10^{18}$. Initially, all cells are white. You can perform the following operation: choose two white cells $i$ and $j$, such that $i \ne j$ and $|i - j| \le k$, and...
2029B
https://codeforces.com/problemset/problem/2029/B
Replacement
1,100
[ "games", "greedy", "strings" ]
Div. 1 + 2
1,000
256
You have a binary string$^{\text{βˆ—}}$ $s$ of length $n$, and Iris gives you another binary string $r$ of length $n-1$. Iris is going to play a game with you. During the game, you will perform $n-1$ operations on $s$. In the $i$-th operation ($1 \le i \le n-1$): * First, you choose an index $k$ such that $1\le k\le ...
Each test contains multiple test cases. The first line of the input contains a single integer $t$ ($1\le t\le 10^4$) β€” the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($2\le n\le 10^5$) β€” the length of $s$. The second line contains the bi...
For each test case, print "YES" (without quotes) if you can win the game, 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.
[ [ "6\n2\n11\n0\n2\n01\n1\n4\n1101\n001\n6\n111110\n10000\n6\n010010\n11010\n8\n10010010\n0010010", "NO\nYES\nYES\nNO\nYES\nNO" ] ]
In the first test case, you cannot perform the first operation. Thus, you lose the game. In the second test case, you can choose $k=1$ in the only operation, and after that, $s$ becomes equal to $\mathtt{1}$. Thus, you win the game. In the third test case, you can perform the following operations: $\mathtt{1}\underli...
Title: Replacement time_limit_ms: 1000 memory_limit_mb: 256 Description: You have a binary string$^{\text{βˆ—}}$ $s$ of length $n$, and Iris gives you another binary string $r$ of length $n-1$. Iris is going to play a game with you. During the game, you will perform $n-1$ operations on $s$. In the $i$-th operation ($1 \...
2003D1
https://codeforces.com/problemset/problem/2003/D1
Turtle and a MEX Problem (Easy Version)
1,500
[ "greedy", "math" ]
Div. 2
2,000
256
The two versions are different problems. In this version of the problem, you can choose the same integer twice or more. You can make hacks only if both versions are solved. One day, Turtle was playing with $n$ sequences. Let the length of the $i$-th sequence be $l_i$. Then the $i$-th sequence was $a_{i, 1}, a_{i, 2}, ...
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 i...
For each test case, output a single integer β€” the value of $\sum\limits_{i = 0}^m f(i)$.
[ [ "6\n3 4\n2 0 2\n3 2 3 3\n4 7 0 1 5\n3 4\n5 0 2 0 4 11\n1 1\n5 1 3 0 3 3\n2 50\n2 1 2\n2 1 2\n1 1\n7 1 2 4 1 4 9 5\n4 114514\n2 2 2\n5 7 3 6 0 3\n3 0 1 1\n5 0 9 2 1 5\n5 1919810\n1 2\n2 324003 0\n3 1416324 2 1460728\n4 1312631 2 0 1415195\n5 1223554 192248 2 1492515 725556", "16\n20\n1281\n6\n6556785365\n1...
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...
Title: Turtle and a MEX Problem (Easy Version) time_limit_ms: 2000 memory_limit_mb: 256 Description: The two versions are different problems. In this version of the problem, you can choose the same integer twice or more. You can make hacks only if both versions are solved. One day, Turtle was playing with $n$ sequence...
1987B
https://codeforces.com/problemset/problem/1987/B
K-Sort
1,000
[ "greedy" ]
Div. 1 + 2
1,000
256
You are given an array of integers $a$ of length $n$. You can apply the following operation any number of times (maybe, zero): * First, choose an integer $k$ such that $1 \le k \le n$ and pay $k + 1$ coins. * Then, choose exactly $k$ indices such that $1 \le i_1 < i_2 < \ldots < i_k \le n$. * Then, for each $...
Each test contains multiple test cases. The first line of input contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) β€” the length of the array $a$. The second line o...
For each test case, output a single integer β€” the minimum number of coins needed to make $a$ non-decreasing.
[ [ "5\n3\n1 7 9\n5\n2 1 4 7 6\n4\n1 3 2 4\n1\n179\n9\n344 12 37 60 311 613 365 328 675", "0\n3\n2\n0\n1821" ] ]
In the first test case, $a$ is already sorted, so you don't have to spend any coins. In the second test case, the optimal sequence of operations is: * Choose $k = 2$ and the indices $2$ and $5$: $[ 2, \color{red}{1}, 4, 7, \color{red}{6} ] \rightarrow [2, 2, 4, 7, 7]$. This costs $3$ coins. It can be proven that ...
Title: K-Sort time_limit_ms: 1000 memory_limit_mb: 256 Description: You are given an array of integers $a$ of length $n$. You can apply the following operation any number of times (maybe, zero): * First, choose an integer $k$ such that $1 \le k \le n$ and pay $k + 1$ coins. * Then, choose exactly $k$ indices suc...
2035G1
https://codeforces.com/problemset/problem/2035/G1
Go Learn! (Easy Version)
3,300
[ "dp", "trees" ]
Div. 1 + 2
5,000
256
The differences between the easy and hard versions are the constraints on $n$ and the sum of $n$. In this version, $n \leq 3000$ and the sum of $n$ does not exceed $10^4$. You can only make hacks if both versions are solved. Well, well, well, let's see how Bessie is managing her finances. She seems to be in the trench...
The first line contains a single integer $t$ ($1 \le t \le 10^4$) β€” the number of test cases. The first line of each test case contains two integers $n$ and $m$ ($1 \leq m \leq n \leq 3000$) denoting the number of the array and the number of tests. The following $m$ lines each contain two integers, describing the tes...
For each test case, output two integers, $r$ β€” the minimum of tests removed so that there exists an array so that all remaining tests are correct, and the number of arrays such that it is possible to remove $r$ tests to make all remaining tests correct modulo $998\,244\,353$.
[ [ "2\n5 4\n1 1\n2 2\n4 3\n5 4\n5 4\n5 4\n2 5\n1 2\n3 3", "0 1\n1 3" ], [ "2\n6 6\n1 3\n2 5\n3 1\n4 2\n5 4\n6 6\n30 8\n19 22\n6 12\n12 1\n28 27\n3 4\n14 25\n29 14\n11 15", "3 78\n3 839271911" ] ]
Consider the first example. In the first test case, the array $[1,2,2,3,4]$ satisfies all $m$ tests, so the minimum number of tests Bessie has to remove is $0$. Note that this is also the only array that satisfies all $m$ tests. In the second test case, the minimum number of tests Bessie has to remove is $1$. The onl...
Title: Go Learn! (Easy Version) time_limit_ms: 5000 memory_limit_mb: 256 Description: The differences between the easy and hard versions are the constraints on $n$ and the sum of $n$. In this version, $n \leq 3000$ and the sum of $n$ does not exceed $10^4$. You can only make hacks if both versions are solved. Well, we...