contest_id
int32
1
2.13k
index
stringclasses
62 values
problem_id
stringlengths
2
6
title
stringlengths
0
67
rating
int32
0
3.5k
tags
stringlengths
0
139
statement
stringlengths
0
6.96k
input_spec
stringlengths
0
2.32k
output_spec
stringlengths
0
1.52k
note
stringlengths
0
5.06k
sample_tests
stringlengths
0
1.02k
difficulty_category
stringclasses
6 values
tag_count
int8
0
11
statement_length
int32
0
6.96k
input_spec_length
int16
0
2.32k
output_spec_length
int16
0
1.52k
contest_year
int16
0
21
1,860
B
1860B
B. Fancy Coins
1,200
binary search; brute force; greedy; math
Monocarp is going to make a purchase with cost of exactly \(m\) burles.He has two types of coins, in the following quantities: coins worth \(1\) burle: \(a_1\) regular coins and infinitely many fancy coins; coins worth \(k\) burles: \(a_k\) regular coins and infinitely many fancy coins. Monocarp wants to make his purch...
The first line contains a single integer \(t\) (\(1 \le t \le 3 \cdot 10^4\)) β€” the number of testcases.The only line of each testcase contains four integers \(m, k, a_1\) and \(a_k\) (\(1 \le m \le 10^8\); \(2 \le k \le 10^8\); \(0 \le a_1, a_k \le 10^8\)) β€” the cost of the purchase, the worth of the second type of co...
For each testcase, print a single integer β€” the smallest total number of fancy coins Monocarp can use to make a purchase.
In the first testcase, there are no regular coins of either type. Monocarp can use \(2\) fancy coins worth \(1\) burle and \(3\) fancy coins worth \(3\) (since \(k=3\)) burles to get \(11\) total burles with \(5\) total fancy coins.In the second testcase, Monocarp has a lot of regular coins of both types. He can use \(...
Input: 411 3 0 011 3 20 2011 3 6 1100000000 2 0 0 | Output: 5 0 1 50000000
Easy
4
595
384
121
18
1,081
C
1081C
C. Colorful Bricks
1,500
combinatorics; dp; math
On his free time, Chouti likes doing some housework. He has got one new task, paint some bricks in the yard.There are \(n\) bricks lined in a row on the ground. Chouti has got \(m\) paint buckets of different colors at hand, so he painted each brick in one of those \(m\) colors.Having finished painting all bricks, Chou...
The first and only line contains three integers \(n\), \(m\) and \(k\) (\(1 \leq n,m \leq 2000, 0 \leq k \leq n-1\)) β€” the number of bricks, the number of colors, and the number of bricks, such that its color differs from the color of brick to the left of it.
Print one integer β€” the number of ways to color bricks modulo \(998\,244\,353\).
In the first example, since \(k=0\), the color of every brick should be the same, so there will be exactly \(m=3\) ways to color the bricks.In the second example, suppose the two colors in the buckets are yellow and lime, the following image shows all \(4\) possible colorings.
Input: 3 3 0 | Output: 3
Medium
3
876
259
80
10
1,283
C
1283C
C. Friends and Gifts
1,500
constructive algorithms; data structures; math
There are \(n\) friends who want to give gifts for the New Year to each other. Each friend should give exactly one gift and receive exactly one gift. The friend cannot give the gift to himself.For each friend the value \(f_i\) is known: it is either \(f_i = 0\) if the \(i\)-th friend doesn't know whom he wants to give ...
The first line of the input contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the number of friends.The second line of the input contains \(n\) integers \(f_1, f_2, \dots, f_n\) (\(0 \le f_i \le n\), \(f_i \ne i\), all \(f_i \ne 0\) are distinct), where \(f_i\) is the either \(f_i = 0\) if the \(i\)-th friend...
Print \(n\) integers \(nf_1, nf_2, \dots, nf_n\), where \(nf_i\) should be equal to \(f_i\) if \(f_i \ne 0\) or the number of friend whom the \(i\)-th friend wants to give the gift to. All values \(nf_i\) should be distinct, \(nf_i\) cannot be equal to \(i\). Each friend gives exactly one gift and receives exactly one ...
Input: 5 5 0 0 2 4 | Output: 5 3 1 2 4
Medium
3
728
528
426
12
1,790
G
1790G
G. Tokens on Graph
2,300
constructive algorithms; dfs and similar; graphs; shortest paths
You are given an undirected connected graph, some vertices of which contain tokens and/or bonuses. Consider a game involving one player β€” you.You can move tokens according to the following rules: At the beginning of the game, you can make exactly one turn: move any token to any adjacent vertex. If the movement of the t...
The first line of input data contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” number of test cases in the test. The descriptions of the test cases follow.The first line of the description of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(0 \le m \le 2 \cdot 10^5\)) β€” the n...
For each test case, print YES in a separate line if you can reach the finish with some token, and NO otherwise.You can output YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive response).
The first test case is explained in the statement. In the second test case, there is only one token which can make only one turn, and it cannot reach the finish. In the third test case, the token can reach the finish line in \(1\) turn. In the fourth test case, you need to make just one turn from \(2\) to \(1\). In the...
Input: 68 102 47 82 4 5 61 22 32 43 43 54 65 65 76 87 85 41 1531 22 33 44 52 11 021 24 31 223 41 22 32 45 43 25 3 42 41 22 33 44 51 01 111 | Output: YES NO YES YES YES YES
Expert
4
1,537
1,644
240
17
1,498
A
1498A
A. GCD Sum
800
brute force; math
The \(\text{\)gcdSum\(}\) of a positive integer is the \(gcd\) of that integer with its sum of digits. Formally, \(\text{\)gcdSum\(}(x) = gcd(x, \text{ sum of digits of } x)\) for a positive integer \(x\). \(gcd(a, b)\) denotes the greatest common divisor of \(a\) and \(b\) β€” the largest integer \(d\) such that both in...
The first line of input contains one integer \(t\) \((1 \le t \le 10^4)\) β€” the number of test cases. Then \(t\) lines follow, each containing a single integer \(n\) \((1 \le n \le 10^{18})\).All test cases in one test are different.
Output \(t\) lines, where the \(i\)-th line is a single integer containing the answer to the \(i\)-th test case.
Let us explain the three test cases in the sample.Test case 1: \(n = 11\): \(\text{\)gcdSum\(}(11) = gcd(11, 1 + 1) = gcd(11,\ 2) = 1\).\(\text{\)gcdSum\(}(12) = gcd(12, 1 + 2) = gcd(12,\ 3) = 3\).So the smallest number \(\ge 11\) whose \(gcdSum\) \(> 1\) is \(12\).Test case 2: \(n = 31\): \(\text{\)gcdSum\(}(31) = gcd...
Input: 3 11 31 75 | Output: 12 33 75
Beginner
2
545
233
112
14
675
C
675C
C. Money Transfers
2,100
constructive algorithms; data structures; greedy; sortings
There are n banks in the city where Vasya lives, they are located in a circle, such that any two banks are neighbouring if their indices differ by no more than 1. Also, bank 1 and bank n are neighbours if n > 1. No bank is a neighbour of itself.Vasya has an account in each bank. Its balance may be negative, meaning Vas...
The first line of the input contains a single integer n (1 ≀ n ≀ 100 000) β€” the number of banks.The second line contains n integers ai ( - 109 ≀ ai ≀ 109), the i-th of them is equal to the initial balance of the account in the i-th bank. It's guaranteed that the sum of all ai is equal to 0.
Print the minimum number of operations required to change balance in each bank to zero.
In the first sample, Vasya may transfer 5 from the first bank to the third.In the second sample, Vasya may first transfer 1 from the third bank to the second, and then 1 from the second to the first.In the third sample, the following sequence provides the optimal answer: transfer 1 from the first bank to the second ban...
Input: 35 0 -5 | Output: 1
Hard
4
884
291
87
6
1,661
A
1661A
A. Array Balancing
800
greedy; math
You are given two arrays of length \(n\): \(a_1, a_2, \dots, a_n\) and \(b_1, b_2, \dots, b_n\).You can perform the following operation any number of times: Choose integer index \(i\) (\(1 \le i \le n\)); Swap \(a_i\) and \(b_i\). What is the minimum possible sum \(|a_1 - a_2| + |a_2 - a_3| + \dots + |a_{n-1} - a_n|\) ...
The first line contains a single integer \(t\) (\(1 \le t \le 4000\)) β€” the number of test cases. Then, \(t\) test cases follow.The first line of each test case contains the single integer \(n\) (\(2 \le n \le 25\)) β€” the length of arrays \(a\) and \(b\).The second line of each test case contains \(n\) integers \(a_1, ...
For each test case, print one integer β€” the minimum possible sum \(\sum\limits_{i=1}^{n-1}{\left(|a_i - a_{i+1}| + |b_i - b_{i+1}|\right)}\).
In the first test case, we can, for example, swap \(a_3\) with \(b_3\) and \(a_4\) with \(b_4\). We'll get arrays \(a = [3, 3, 3, 3]\) and \(b = [10, 10, 10, 10]\) with sum \(3 \cdot |3 - 3| + 3 \cdot |10 - 10| = 0\).In the second test case, arrays already have minimum sum (described above) equal to \(|1 - 2| + \dots +...
Input: 343 3 10 1010 10 3 351 2 3 4 56 7 8 9 10672 101 108 108 111 4410 87 111 114 108 100 | Output: 0 8 218
Beginner
2
547
506
141
16
1,612
C
1612C
C. Chat Ban
1,300
binary search; math
You are a usual chat user on the most famous streaming platform. Of course, there are some moments when you just want to chill and spam something.More precisely, you want to spam the emote triangle of size \(k\). It consists of \(2k-1\) messages. The first message consists of one emote, the second one β€” of two emotes, ...
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The next \(t\) lines describe test cases.The only line of the test case contains integers \(k\) and \(x\) (\(1 \le k \le 10^9; 1 \le x \le 10^{18}\)).
For each test case, print the number of messages you will write before getting banned for the corresponding values \(k\) and \(x\).
Let's analyze the test cases of the example. In the first test case, you write three messages containing \(1\), \(2\) and \(3\) emotes respectively, and since \(1 + 2 + 3 \ge 6\), you get banned after that. In the second test case, you write four messages containing \(1\), \(2\), \(3\) and \(4\) emotes respectively, an...
Input: 7 4 6 4 7 1 2 3 7 2 5 100 1 1000000000 923456789987654321 | Output: 3 4 1 4 3 1 1608737403
Easy
2
1,068
256
131
16
1,585
D
1585D
D. Yet Another Sorting Problem
1,900
data structures; math
Petya has an array of integers \(a_1, a_2, \ldots, a_n\). He only likes sorted arrays. Unfortunately, the given array could be arbitrary, so Petya wants to sort it.Petya likes to challenge himself, so he wants to sort array using only \(3\)-cycles. More formally, in one operation he can pick \(3\) pairwise distinct ind...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 5 \cdot 10^5\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 5 \cdot 10^5\)) β€” the length of the array \(a\).The second line of each...
For each test case, print ""YES"" (without quotes) if Petya can sort the array \(a\) using \(3\)-cycles, and ""NO"" (without quotes) otherwise. You can print each letter in any case (upper or lower).
In the \(6\)-th test case Petya can use the \(3\)-cycle \(1 \to 3 \to 2 \to 1\) to sort the array.In the \(7\)-th test case Petya can apply \(1 \to 3 \to 2 \to 1\) and make \(a = [1, 4, 2, 3]\). Then he can apply \(2 \to 4 \to 3 \to 2\) and finally sort the array.
Input: 7 1 1 2 2 2 2 2 1 3 1 2 3 3 2 1 3 3 3 1 2 4 2 1 4 3 | Output: YES YES NO YES NO YES YES
Hard
2
924
497
199
15
48
A
48A
A. Rock-paper-scissors
900
implementation; schedules
Uncle Fyodor, Matroskin the Cat and Sharic the Dog live their simple but happy lives in Prostokvashino. Sometimes they receive parcels from Uncle Fyodor’s parents and sometimes from anonymous benefactors, in which case it is hard to determine to which one of them the package has been sent. A photographic rifle is obvio...
The first input line contains the name of the gesture that Uncle Fyodor showed, the second line shows which gesture Matroskin showed and the third line shows Sharic’s gesture.
Print ""F"" (without quotes) if Uncle Fyodor wins. Print ""M"" if Matroskin wins and ""S"" if Sharic wins. If it is impossible to find the winner, print ""?"".
Input: rockrockrock | Output: ?
Beginner
2
1,597
175
159
0
1,980
B
1980B
B. Choosing Cubes
800
sortings; sortings
Dmitry has \(n\) cubes, numbered from left to right from \(1\) to \(n\). The cube with index \(f\) is his favorite.Dmitry threw all the cubes on the table, and the \(i\)-th cube showed the value \(a_i\) (\(1 \le a_i \le 100\)). After that, he arranged the cubes in non-increasing order of their values, from largest to s...
The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. Then follow the descriptions of the test cases.The first line of each test case description contains three integers \(n\), \(f\), and \(k\) (\(1 \le f, k \le n \le 100\)) β€” the number of cubes, the index of Dmitry's favorite cub...
For each test case, output one line β€” ""YES"" if the cube will be removed in all cases, ""NO"" if it will not be removed in any case, ""MAYBE"" if it may be either removed or left.You can output the answer in any case. For example, the strings ""YES"", ""nO"", ""mAyBe"" will be accepted as answers.
Input: 125 2 24 3 3 2 35 5 34 2 1 3 55 5 25 2 4 1 35 5 51 2 5 4 35 5 43 1 2 4 55 5 54 3 2 1 56 5 31 2 3 1 2 310 1 11 1 1 1 1 1 1 1 1 11 1 1425 2 32 2 1 1 22 1 12 15 3 13 3 2 3 2 | Output: MAYBE YES NO YES YES YES MAYBE MAYBE YES YES YES NO
Beginner
2
987
503
299
19
1,041
F
1041F
F. Ray in the tube
2,500
data structures; divide and conquer; dp; math
You are given a tube which is reflective inside represented as two non-coinciding, but parallel to \(Ox\) lines. Each line has some special integer points β€” positions of sensors on sides of the tube.You are going to emit a laser ray in the tube. To do so, you have to choose two integer points \(A\) and \(B\) on the fir...
The first line contains two integers \(n\) and \(y_1\) (\(1 \le n \le 10^5\), \(0 \le y_1 \le 10^9\)) β€” number of sensors on the first line and its \(y\) coordinate.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) β€” \(x\) coordinates of the sensors on the first line in the asce...
Print the only integer β€” the maximum number of sensors which can register the ray.
One of the solutions illustrated on the image by pair \(A_2\) and \(B_2\).
Input: 3 11 5 61 33 | Output: 3
Expert
4
1,129
667
82
10
1,996
F
1996F
F. Bomb
1,900
binary search; greedy; math
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 i...
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, ...
For each test case, output an integer, the maximum score you can acquire after \(k\) operations.
Input: 53 45 6 72 3 45 932 52 68 64 1418 14 53 24 85 10001 2 3 4 55 4 3 2 11 10000001000000110 63 3 5 10 6 8 6 8 7 76 1 7 4 1 1 8 9 3 1 | Output: 21 349 27 500000500000 47
Hard
3
379
540
96
19
420
B
420B
B. Online Meeting
1,800
implementation
Nearly each project of the F company has a whole team of developers working on it. They often are in different rooms of the office in different cities and even countries. To keep in touch and track the results of the project, the F company conducts shared online meetings in a Spyke chat.One day the director of the F co...
The first line contains integers n and m (1 ≀ n, m ≀ 105) β€” the number of team participants and the number of messages. Each of the next m lines contains a message in the format: '+ id': the record means that the person with number id (1 ≀ id ≀ n) has logged on to the meeting. '- id': the record means that the person w...
In the first line print integer k (0 ≀ k ≀ n) β€” how many people can be leaders. In the next line, print k integers in the increasing order β€” the numbers of the people who can be leaders.If the data is such that no member of the team can be a leader, print a single number 0.
Input: 5 4+ 1+ 2- 2- 1 | Output: 41 3 4 5
Medium
1
1,064
674
274
4
186
A
186A
A. Comparing Strings
1,100
implementation; strings
Some dwarves that are finishing the StUDY (State University for Dwarven Youngsters) Bachelor courses, have been told ""no genome, no degree"". That means that all dwarves should write a thesis on genome. Dwarven genome is far from simple. It is represented by a string that consists of lowercase Latin letters.Dwarf Mish...
The first line contains the first dwarf's genome: a non-empty string, consisting of lowercase Latin letters.The second line contains the second dwarf's genome: a non-empty string, consisting of lowercase Latin letters.The number of letters in each genome doesn't exceed 105. It is guaranteed that the strings that corres...
Print ""YES"", if the dwarves belong to the same race. Otherwise, print ""NO"".
First example: you can simply swap two letters in string ""ab"". So we get ""ba"". Second example: we can't change string ""aa"" into string ""ab"", because ""aa"" does not contain letter ""b"".
Input: abba | Output: YES
Easy
2
664
399
79
1
1,740
B
1740B
B. Jumbo Extra Cheese 2
800
geometry; greedy; sortings
Pak Chanek has \(n\) two-dimensional slices of cheese. The \(i\)-th slice of cheese can be represented as a rectangle of dimensions \(a_i \times b_i\). We want to arrange them on the two-dimensional plane such that: Each edge of each cheese is parallel to either the x-axis or the y-axis. The bottom edge of each cheese ...
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 2 \cdot 10^4\)) β€” the number of test cases. The following lines contain the description of each test case.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) β€” the number of slices o...
For each test case, output a line containing an integer representing the minimum possible perimeter of the constructed shape.
In the first test case, a way of getting the minimum possible perimeter is to arrange the slices of cheese as follows.We can calculate that the perimeter of the constructed shape is \(2+5+1+1+1+1+3+1+5+1+2+3=26\). It can be shown that we cannot get a smaller perimeter.Consider the following invalid arrangement.Even tho...
Input: 344 14 51 12 332 42 62 312 65 | Output: 26 24 134
Beginner
3
718
613
125
17
1,866
B
1866B
B. Battling with Numbers
1,400
combinatorics; math; number theory
On the trip to campus during the mid semester exam period, Chaneka thinks of two positive integers \(X\) and \(Y\). Since the two integers can be very big, both are represented using their prime factorisations, such that: \(X=A_1^{B_1}\times A_2^{B_2}\times\ldots\times A_N^{B_N}\) (each \(A_i\) is prime, each \(B_i\) i...
The first line contains a single integer \(N\) (\(1 \leq N \leq 10^5\)) β€” the number of distinct primes in the prime factorisation of \(X\).The second line contains \(N\) integers \(A_1, A_2, A_3, \ldots, A_N\) (\(2 \leq A_1 < A_2 < \ldots < A_N \leq 2 \cdot 10^6\); each \(A_i\) is prime) β€” the primes in the prime fact...
An integer representing the number of pairs of positive integers \(p\) and \(q\) such that \(\text{LCM}(p, q) = X\) and \(\text{GCD}(p, q) = Y\), modulo \(998\,244\,353\).
In the first example, the integers are as follows: \(X=2^2\times3^1\times5^1\times7^2=2940\) \(Y=3^1\times7^1=21\) The following are all possible pairs of \(p\) and \(q\): \(p=21\), \(q=2940\) \(p=84\), \(q=735\) \(p=105\), \(q=588\) \(p=147\), \(q=420\) \(p=420\), \(q=147\) \(p=588\), \(q=105\) \(p=735\), \(q=84\) \(p...
Input: 4 2 3 5 7 2 1 1 2 2 3 7 1 1 | Output: 8
Easy
3
1,133
976
171
18
1,624
F
1624F
F. Interacdive Problem
2,000
binary search; constructive algorithms; interactive
This problem is interactive.We decided to play a game with you and guess the number \(x\) (\(1 \le x < n\)), where you know the number \(n\).You can make queries like this: + c: this command assigns \(x = x + c\) (\(1 \le c < n\)) and then returns you the value \(\lfloor\frac{x}{n}\rfloor\) (\(x\) divide by \(n\) and r...
In the first sample initially \(x = 2\). After the first query \(x = 3\), \(\lfloor\frac{x}{n}\rfloor = 1\).In the second sample also initially \(x = 2\). After the first query \(x = 3\), \(\lfloor\frac{x}{n}\rfloor = 0\). After the second query \(x = 4\), \(\lfloor\frac{x}{n}\rfloor = 0\). After the third query \(x=5\...
Input: 3 1 | Output: + 1 ! 3
Hard
3
404
0
0
16
1,810
D
1810D
D. Climbing the Tree
1,700
binary search; math
The snails are climbing a tree. The tree height is \(h\) meters, and snails start at position \(0\).Each snail has two attributes \(a\) and \(b\) (\(a > b\)). Starting from the \(1\)-st day, one snail climbs the tree like this: during the daylight hours of the day, he climbs up \(a\) meters; during the night, the snail...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Then follows their description.The first line of each test case contains one integer \(q\) (\(1\le q \le 2\cdot 10^5\)) β€” the number of events.For the following \(q\) lines, the firs...
For each test case, output \(q\) integers in one line, one for each event, in order. Specifically, for each event of type \(1\), if you adopt the message, output \(1\); if you ignore it, output \(0\); for each event of type \(2\), output an integer denoting the number of days that the snail will spend. If you cannot de...
In the first test case, we can determine \(h=7\) through the first message, so we know the second snail and the third snail need to spend \(2\) and \(5\) days respectively to reach the top.Let's show how the second snail climbs: During the daylight hours of the \(1\)st day: climbs up \(4\) meters, now at position \(4\)...
Input: 531 3 2 52 4 12 3 231 6 5 12 3 12 6 231 4 2 21 2 1 32 10 291 7 3 61 2 1 82 5 11 10 9 71 8 1 21 10 5 81 10 7 72 7 41 9 4 291 2 1 61 8 5 61 4 2 72 9 11 5 1 41 5 2 71 7 1 91 9 1 42 10 8 | Output: 1 2 5 1 -1 1 1 0 1 1 0 -1 0 0 0 1 8 0 1 0 0 1 0 0 0 0 1
Medium
2
1,469
700
346
18
1,984
C1
1984C1
C1. Magnitude (Easy Version)
1,300
dp; greedy; math
The two versions of the problem are different. You may want to read both versions. You can make hacks only if both versions are solved.You are given an array \(a\) of length \(n\). Start with \(c = 0\). Then, for each \(i\) from \(1\) to \(n\) (in increasing order) do exactly one of the following: Option \(1\): set \(c...
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\) (\(2 \leq n \leq 2 \cdot 10^5\)).The second line of each case contains \(n\) integers \(a_1\), \(a_2\), \(a_3\), \(\ldots\), \(a_n\) (\(-10^9 \leq a_i \leq ...
For each test case, output a single integer β€” the value of \(k\).
In the first test case, if we set \(c\) to its absolute value every time we add to it, we end up with \(6\). It can be shown that this is the maximum result.In the second test case, taking the absolute value will never change anything, so we can just sum the array without doing anything to get \(24\).In the third test ...
Input: 5410 -9 -3 481 4 3 4 1 4 3 43-1 -2 -34-1000000000 1000000000 1000000000 100000000041 9 8 4 | Output: 6 24 6 4000000000 22
Easy
3
530
398
65
19
440
C
440C
C. One-Based Arithmetic
1,800
brute force; dfs and similar; divide and conquer
Prof. Vasechkin wants to represent positive integer n as a sum of addends, where each addends is an integer number containing only 1s. For example, he can represent 121 as 121=111+11+–1. Help him to find the least number of digits 1 in such sum.
The first line of the input contains integer n (1 ≀ n < 1015).
Print expected minimal number of digits 1.
Input: 121 | Output: 6
Medium
3
245
62
42
4
1,386
C
1386C
C. Joker
2,800
*special; bitmasks; data structures; divide and conquer; dsu
Joker returns to Gotham City to execute another evil plan. In Gotham City, there are \(N\) street junctions (numbered from \(1\) to \(N\)) and \(M\) streets (numbered from \(1\) to \(M\)). Each street connects two distinct junctions, and two junctions are connected by at most one street.For his evil plan, Joker needs t...
The first line of the input contains three integers \(N\), \(M\), and \(Q\) (\(1 \leq N, M, Q \leq 200\,000\)): the number of junctions, the number of streets, and the number of days to be investigated. The following \(M\) lines describe the streets. The \(j\)-th of these lines (\(1 \le j \le M\)) contains two junction...
Your output is to contain \(Q\) lines. Line \(i\) (\(1 \leq i \leq Q\)) contains ""YES"" if Joker can execute his plan on day \(i\), or ""NO"" otherwise.
The graph in the example test:
Input: 6 8 2 1 3 1 5 1 6 2 5 2 6 3 4 3 5 5 6 4 8 4 7 | Output: NO YES
Master
5
1,273
681
153
13
1,915
B
1915B
B. Not Quite Latin Square
800
bitmasks; brute force; implementation
A Latin square is a \(3 \times 3\) grid made up of the letters \(\texttt{A}\), \(\texttt{B}\), and \(\texttt{C}\) such that: in each row, the letters \(\texttt{A}\), \(\texttt{B}\), and \(\texttt{C}\) each appear once, and in each column, the letters \(\texttt{A}\), \(\texttt{B}\), and \(\texttt{C}\) each appear once. ...
The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 108\)) β€” the number of testcases.Each test case contains three lines, each consisting of three characters, representing the Latin square. Each character is one of \(\texttt{A}\), \(\texttt{B}\), \(\texttt{C}\), or \(\texttt{?}\).Each test case...
For each test case, output the letter that was replaced.
The correct Latin squares for the three test cases are shown below:$$$\(\begin{bmatrix} \texttt{A} & \texttt{B} & \texttt{C} \\ \texttt{C} & \color{red}{\texttt{A}} & \texttt{B} \\ \texttt{B} & \texttt{C} & \texttt{A} \\ \end{bmatrix} \quad \begin{bmatrix} \texttt{B} & \texttt{C} & \texttt{A} \\ \texttt{C} & \texttt{A}...
Input: 3ABCC?BBCABCACA?ABC?ABBCAABC | Output: A B C
Beginner
3
671
416
56
19
1,045
J
1045J
J. Moonwalk challenge
2,600
data structures; strings; trees
Since astronauts from BubbleCup XI mission finished their mission on the Moon and are big fans of famous singer, they decided to spend some fun time before returning to the Earth and hence created a so called ""Moonwalk challenge"" game.Teams of astronauts are given the map of craters on the Moon and direct bidirection...
In the first line, integer \(N\) \((2 \leq N \leq 10^5)\) β€” number of craters on the Moon. Craters are numerated with numbers \(1\) to \(N\).In next \(N-1\) lines, three values \(u, v, L\) \((1 \leq u, v \leq N, L \in \{a, ..., z\})\) β€” denoting that there is a direct path with color \(L\) between craters \(u\) and \(v...
For each query output one number that represents number of occurrences of array S on the path from \(u\) to \(v\).
Input: 62 3 g3 4 n5 3 o6 1 n1 2 d71 6 n6 4 dg6 4 n2 5 og1 2 d6 5 go2 3 g | Output: 1120111
Expert
3
1,022
671
114
10
1,982
A
1982A
A. Soccer
800
greedy; implementation; math; sortings
Dima loves watching soccer. In such a game, the score on the scoreboard is represented as \(x\) : \(y\), where \(x\) is the number of goals of the first team, and \(y\) is the number of goals of the second team. At any given time, only one team can score a goal, so the score \(x\) : \(y\) can change to either \((x + 1)...
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. Then follows the description of the test cases.The first line of each test case contains two integers \(x_{1}, y_{1}\) (\(0 \le x_{1}, y_{1} \le 10^{9}\), \(x_{1} \neq y_{1}\)) β€” the s...
For each test case, output ""YES"" without quotes if it is possible, that the teams never had a tie while Dima was away, otherwise output ""NO"" without quotes.You can output each letter in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as a positive answer).
In the first test case, the score before Dima left was \(1\) : \(0\). When he leaves, the first team scores several goals in a row until the score becomes \(5\) : \(0\), so the answer is YES.In the second test case, the score could only change as follows: \(1\) : \(2\) \(2\) : \(2\) \(3\) : \(2\) In this scenario, ther...
Input: 61 05 01 23 21 24 51 24 31 21 2998244353 01000000000 999999999 | Output: YES NO YES NO YES YES
Beginner
4
942
543
305
19
64
C
64C
C. Table
1,600
*special; greedy; implementation; math
The integer numbers from 1 to nm was put into rectangular table having n rows and m columns. The numbers was put from left to right, from top to bottom, i.e. the first row contains integers 1, 2, ..., m, the second β€” m + 1, m + 2, ..., 2 * m and so on.After it these numbers was written on the paper in another order: fr...
The only line in the input contains three integer numbers n, m and k (1 ≀ n, m ≀ 20000, 1 ≀ k ≀ nm).
Print the required number.
Input: 3 4 11 | Output: 8
Medium
4
475
100
26
0
603
B
603B
B. Moodular Arithmetic
1,800
combinatorics; dfs and similar; dsu; math; number theory
As behooves any intelligent schoolboy, Kevin Sun is studying psycowlogy, cowculus, and cryptcowgraphy at the Bovinia State University (BGU) under Farmer Ivan. During his Mathematics of Olympiads (MoO) class, Kevin was confronted with a weird functional equation and needs your help. For two fixed integers k and p, where...
The input consists of two space-separated integers p and k (3 ≀ p ≀ 1 000 000, 0 ≀ k ≀ p - 1) on a single line. It is guaranteed that p is an odd prime number.
Print a single integer, the number of distinct functions f modulo 109 + 7.
In the first sample, p = 3 and k = 2. The following functions work: f(0) = 0, f(1) = 1, f(2) = 2. f(0) = 0, f(1) = 2, f(2) = 1. f(0) = f(1) = f(2) = 0.
Input: 3 2 | Output: 3
Medium
5
745
159
74
6
323
B
323B
B. Tournament-graph
2,200
constructive algorithms; graphs
In this problem you have to build tournament graph, consisting of n vertices, such, that for any oriented pair of vertices (v, u) (v β‰  u) there exists a path from vertex v to vertex u consisting of no more then two edges.A directed graph without self-loops is a tournament, if there is exactly one edge between any two d...
The first line contains an integer n (3 ≀ n ≀ 1000), the number of the graph's vertices.
Print -1 if there is no graph, satisfying the described conditions.Otherwise, print n lines with n integers in each. The numbers should be separated with spaces. That is adjacency matrix a of the found tournament. Consider the graph vertices to be numbered with integers from 1 to n. Then av, u = 0, if there is no edge ...
Input: 3 | Output: 0 1 00 0 11 0 0
Hard
2
377
88
536
3
1,665
A
1665A
A. GCD vs LCM
800
constructive algorithms; math
You are given a positive integer \(n\). You have to find \(4\) positive integers \(a, b, c, d\) such that \(a + b + c + d = n\), and \(\gcd(a, b) = \operatorname{lcm}(c, d)\).If there are several possible answers you can output any of them. It is possible to show that the answer always exists.In this problem \(\gcd(a, ...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Description of the test cases follows.Each test case contains a single line with integer \(n\) (\(4 \le n \le 10^9\)) β€” the sum of \(a\), \(b\), \(c\), and \(d\).
For each test case output \(4\) positive integers \(a\), \(b\), \(c\), \(d\) such that \(a + b + c + d = n\) and \(\gcd(a, b) = \operatorname{lcm}(c, d)\).
In the first test case \(\gcd(1, 1) = \operatorname{lcm}(1, 1) = 1\), \(1 + 1 + 1 + 1 = 4\).In the second test case \(\gcd(2, 2) = \operatorname{lcm}(2, 1) = 2\), \(2 + 2 + 2 + 1 = 7\).In the third test case \(\gcd(2, 2) = \operatorname{lcm}(2, 2) = 2\), \(2 + 2 + 2 + 2 = 8\).In the fourth test case \(\gcd(2, 4) = \ope...
Input: 5478910 | Output: 1 1 1 1 2 2 2 1 2 2 2 2 2 4 2 1 3 5 1 1
Beginner
2
467
303
155
16
1,927
F
1927F
F. Microcycle
1,900
data structures; dfs and similar; dsu; graphs; greedy; implementation; sortings; trees
Given an undirected weighted graph with \(n\) vertices and \(m\) edges. There is at most one edge between each pair of vertices in the graph, and the graph does not contain loops (edges from a vertex to itself). The graph is not necessarily connected.A cycle in the graph is called simple if it doesn't pass through the ...
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. Then follow the descriptions of the test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(3 \le n \le m \le \min(\frac{n\cdot(n - 1)}{2}, 2 \cdot 10^5)\)) β€” the size of the graph...
For each test case, output a pair of numbers \(b\) and \(k\), where: \(b\) β€” the minimum weight of the edge in the found cycle, \(k\) β€” the number of vertices in the found cycle. On the next line, output \(k\) numbers from \(1\) to \(n\) β€” the vertices of the cycle in traversal order.Note that the answer always exists,...
Input: 56 61 2 12 3 13 1 14 5 15 6 16 4 16 61 2 102 3 83 1 54 5 1005 6 406 4 36 151 2 45 2 86 1 76 3 106 5 13 2 84 3 45 3 62 6 65 4 54 1 36 4 54 2 13 1 71 5 54 62 3 21 3 101 4 13 4 72 4 51 2 24 52 1 103 1 34 2 61 4 72 3 3 | Output: 1 3 1 2 3 3 3 6 4 5 1 5 4 2 1 6 3 1 4 1 4 3 2 3 3 2 3 1
Hard
8
466
834
408
19
763
A
763A
A. Timofey and a tree
1,600
dfs and similar; dp; dsu; graphs; implementation; trees
Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so that the i-th vertex gets color ci.Now it's time for Timofey birthday, and his mother asked him to remove the tree. Timofey removes the tree in the following way: he takes some vertex ...
The first line contains single integer n (2 ≀ n ≀ 105) β€” the number of vertices in the tree.Each of the next n - 1 lines contains two integers u and v (1 ≀ u, v ≀ n, u β‰  v), denoting there is an edge between vertices u and v. It is guaranteed that the given graph is a tree.The next line contains n integers c1, c2, ...,...
Print ""NO"" in a single line, if Timofey can't take the tree in such a way that it doesn't annoy him.Otherwise print ""YES"" in the first line. In the second line print the index of the vertex which Timofey should take in hands. If there are multiple answers, print any of them.
Input: 41 22 33 41 2 1 1 | Output: YES2
Medium
6
991
376
279
7
2,045
F
2045F
F. Grid Game 3-angle
3,000
games; math
Your friends, Anda and Kamu decide to play a game called Grid Game and ask you to become the gamemaster. As the gamemaster, you set up a triangular grid of size \(N\). The grid has \(N\) rows (numbered from \(1\) to \(N\)). Row \(r\) has \(r\) cells; the \(c\)-th cell of row \(r\) is denoted as \((r, c)\). Before the g...
This problem is a multi-case problem. The first line consists of an integer \(T\) (\(1 \leq T \leq 100\)) that represents the number of test cases.Each test case starts with a single line consisting of three integers \(N\) \(M\) \(K\) (\(1 \leq N \leq 10^9; 1 \leq M, K \leq 200\,000\)). Then, each of the next \(M\) lin...
For each case, output a string in a single line representing the player who will win the game if both players play optimally. Output Anda if Anda, the first player, wins. Otherwise, output Kamu.
Explanation for the sample input/output #1For the first case, during the first turn, Anda will remove all the stones from cell \((1, 1)\) and then add three stones at \((2, 1)\). The only cell with stones left is now cell \((2, 1)\) with five stones, so Kamu must remove stones from that cell. No matter how many stones ...
Input: 3 2 2 4 1 1 3 2 1 2 100 2 1 4 1 10 4 4 10 10 5 2 1 1 4 3 1 2 4 2 5 2 2 1 5 3 4 | Output: Anda Kamu Anda
Master
2
1,350
536
194
20
1,071
E
1071E
E. Rain Protection
3,500
binary search; geometry
A lot of people dream of convertibles (also often called cabriolets). Some of convertibles, however, don't have roof at all, and are vulnerable to rain. This is why Melon Ask, the famous inventor, decided to create a rain protection mechanism for convertibles.The workplace of the mechanism is a part of plane just above...
The first line contains three integers \(n\), \(w\) and \(h\) (\(1 \le n \le 10^5\), \(1\le w, h \le 10^3\)), meaning that there are \(n\) raindrops, and two rails are represented as segments connecting \((0, 0)\) and \((w, 0)\) and connecting \((0, h)\) and \((w, h)\).The second line contains two integers \(e_1\) and ...
If it is impossible to catch all raindrops, print \(-1\).Otherwise, print the least possible maximum speed of the rope endpoints for which it is possible to catch them all. Your answer is considered correct if the absolute or relative error doesn't exceed \(10^{-4}\).Formally, let your answer be \(a\), and the jury's a...
That is how one can act in the first sample test:Here is the same for the second:
Input: 3 5 50 01 1 42 2 43 3 4 | Output: 1.0000000019
Master
2
1,287
801
420
10
99
B
99B
B. Help Chef Gerasim
1,300
implementation; sortings
In a far away kingdom young pages help to set the table for the King. As they are terribly mischievous, one needs to keep an eye on the control whether they have set everything correctly. This time the royal chef Gerasim had the impression that the pages have played a prank again: they had poured the juice from one cup...
The first line contains integer n β€” the number of cups on the royal table (1 ≀ n ≀ 1000). Next n lines contain volumes of juice in each cup β€” non-negative integers, not exceeding 104.
If the pages didn't pour the juice, print ""Exemplary pages."" (without the quotes). If you can determine the volume of juice poured during exactly one juice pouring, print ""v ml. from cup #a to cup #b."" (without the quotes), where v represents the volume of poured juice, a represents the number of the cup from which...
Input: 5270250250230250 | Output: 20 ml. from cup #4 to cup #1.
Easy
2
1,036
183
851
0
1,674
F
1674F
F. Desktop Rearrangement
1,800
data structures; greedy; implementation
Your friend Ivan asked you to help him rearrange his desktop. The desktop can be represented as a rectangle matrix of size \(n \times m\) consisting of characters '.' (empty cell of the desktop) and '*' (an icon).The desktop is called good if all its icons are occupying some prefix of full columns and, possibly, the pr...
The first line of the input contains three integers \(n\), \(m\) and \(q\) (\(1 \le n, m \le 1000; 1 \le q \le 2 \cdot 10^5\)) β€” the number of rows in the desktop, the number of columns in the desktop and the number of queries, respectively.The next \(n\) lines contain the description of the desktop. The \(i\)-th of th...
Print \(q\) integers. The \(i\)-th of them should be the minimum number of moves required to make the desktop good after applying the first \(i\) queries.
Input: 4 4 8 ..** .*.. *... ...* 1 3 2 3 3 1 2 3 3 4 4 3 2 3 2 2 | Output: 3 4 4 3 4 5 5 5
Medium
3
1,067
714
154
16
1,433
G
1433G
G. Reducing Delivery Cost
2,100
brute force; graphs; shortest paths
You are a mayor of Berlyatov. There are \(n\) districts and \(m\) two-way roads between them. The \(i\)-th road connects districts \(x_i\) and \(y_i\). The cost of travelling along this road is \(w_i\). There is some path between each pair of districts, so the city is connected.There are \(k\) delivery routes in Berlya...
The first line of the input contains three integers \(n\), \(m\) and \(k\) (\(2 \le n \le 1000\); \(n - 1 \le m \le min(1000, \frac{n(n-1)}{2})\); \(1 \le k \le 1000\)) β€” the number of districts, the number of roads and the number of courier routes.The next \(m\) lines describe roads. The \(i\)-th road is given as thre...
Print one integer β€” the minimum total courier routes cost you can achieve (i.e. the minimum value \(\sum\limits_{i=1}^{k} d(a_i, b_i)\), where \(d(x, y)\) is the cheapest cost of travel between districts \(x\) and \(y\)) if you can make some (at most one) road cost zero.
The picture corresponding to the first example:There, you can choose either the road \((2, 4)\) or the road \((4, 6)\). Both options lead to the total cost \(22\).The picture corresponding to the second example:There, you can choose the road \((3, 4)\). This leads to the total cost \(13\).
Input: 6 5 2 1 2 5 2 3 7 2 4 4 4 5 2 4 6 8 1 6 5 3 | Output: 22
Hard
3
1,208
1,003
271
14
1,857
G
1857G
G. Counting Graphs
2,000
combinatorics; divide and conquer; dsu; graphs; greedy; sortings; trees
Given a tree consisting of \(n\) vertices. A tree is a connected undirected graph without cycles. Each edge of the tree has its weight, \(w_i\).Your task is to count the number of different graphs that satisfy all four conditions: The graph does not have self-loops and multiple edges. The weights on the edges of the gr...
The first line contains an integer \(t\) (\(1\le t\le 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(S\) (\(2 \le n \le 2 \cdot 10^5, 1\le S\le 10^9\)) β€” the number of vertices and the upper bound of the weights.Then follow \(n-1\) lines describing the tree, the \(...
For each test, output the number of different graphs that satisfy the conditions, modulo \(998244353\).
In the first sample, there is only one graph, which is the given tree.In the second samle, the given tree looks like this: All possible graphs for the second sample are shown below, the minimum spanning tree is highlighted in red:
Input: 42 51 2 44 51 2 22 3 43 4 35 61 2 31 3 23 4 63 5 110 2001 2 32 3 333 4 2001 5 1325 6 15 7 297 8 1877 9 207 10 4 | Output: 1 8 80 650867886
Hard
7
644
565
103
18
1,027
E
1027E
E. Inverse Coloring
2,100
combinatorics; dp; math
You are given a square board, consisting of \(n\) rows and \(n\) columns. Each tile in it should be colored either white or black.Let's call some coloring beautiful if each pair of adjacent rows are either the same or different in every position. The same condition should be held for the columns as well.Let's call some...
A single line contains two integers \(n\) and \(k\) (\(1 \le n \le 500\), \(1 \le k \le n^2\)) β€” the number of rows and columns of the board and the maximum number of tiles inside the rectangle of the single color, respectively.
Print a single integer β€” the number of suitable colorings of the board of the given size modulo \(998244353\).
Board of size \(1 \times 1\) is either a single black tile or a single white tile. Both of them include a rectangle of a single color, consisting of \(1\) tile.Here are the beautiful colorings of a board of size \(2 \times 2\) that don't include rectangles of a single color, consisting of at least \(3\) tiles: The rest...
Input: 1 1 | Output: 0
Hard
3
592
228
110
10
653
A
653A
A. Bear and Three Balls
900
brute force; implementation; sortings
Limak is a little polar bear. He has n balls, the i-th ball has size ti.Limak wants to give one ball to each of his three friends. Giving gifts isn't easy β€” there are two rules Limak must obey to make friends happy: No two friends can get balls of the same size. No two friends can get balls of sizes that differ by more...
The first line of the input contains one integer n (3 ≀ n ≀ 50) β€” the number of balls Limak has.The second line contains n integers t1, t2, ..., tn (1 ≀ ti ≀ 1000) where ti denotes the size of the i-th ball.
Print ""YES"" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print ""NO"" (without quotes).
In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17.In the second sample, there is no way to give gifts to three friends without breaking the rules.In the third sample, there is even more than one way to choose balls: ...
Input: 418 55 16 17 | Output: YES
Beginner
3
707
207
175
6
1,942
C1
1942C1
C1. Bessie's Birthday Cake (Easy Version)
1,300
geometry; greedy; math
Proof Geometric Construction Can Solve All Love Affairs - manbo-pβ €This is the easy version of the problem. The only difference between the two versions is the constraint on \(y\). In this version \(y = 0\). You can make hacks only if both versions are solved.Bessie has received a birthday cake from her best friend Elsi...
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 consists of three integers, \(n\), \(x\), and \(y\) (\(4 \leq n \leq 10^9\), \(2 \leq x \leq \min(n, 2 \cdot 10^5)\), \(y = 0\)) β€” the number of sides of the polygon, number of vertices Be...
For each test case, output a single integer: the maximum number of non-intersecting triangular pieces of cake she can give out.
In test cases \(1\), \(2\) and \(3\), you can get \(2\), \(6\) and \(2\) non-intersecting triangular pieces of cake, respectively. A possible construction is shown in the following pictures:The green dots represent vertices that can be used, the blue lines represent diagonals that are drawn, and the red numbers represe...
Input: 38 4 01 6 2 58 8 01 3 2 5 4 6 7 84 2 01 3 | Output: 2 6 2
Easy
3
1,221
597
127
19
1,385
C
1385C
C. Make It Good
1,200
greedy
You are given an array \(a\) consisting of \(n\) integers. You have to find the length of the smallest (shortest) prefix of elements you need to erase from \(a\) to make it a good array. Recall that the prefix of the array \(a=[a_1, a_2, \dots, a_n]\) is a subarray consisting several first elements: the prefix of the a...
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β€” the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the length of \(a\). The second line of the test case contains \(n\) integers \(a_1, ...
For each test case, print the answer: the length of the shortest prefix of elements you need to erase from \(a\) to make it a good array.
In the first test case of the example, the array \(a\) is already good, so we don't need to erase any prefix.In the second test case of the example, the initial array \(a\) is not good. Let's erase first \(4\) elements of \(a\), the result is \([4, 5, 2]\). The resulting array is good. You can prove that if you erase f...
Input: 5 4 1 2 3 4 7 4 3 3 8 4 5 2 3 1 1 1 7 1 3 1 4 5 3 2 5 5 4 3 2 3 | Output: 0 4 0 2 3
Easy
1
1,931
521
137
13
1,470
F
1470F
F. Strange Covering
3,500
divide and conquer
You are given \(n\) points on a plane. Please find the minimum sum of areas of two axis-aligned rectangles, such that each point is contained in at least one of these rectangles.Note that the chosen rectangles can be degenerate. Rectangle contains all the points that lie inside it or on its boundary.
The first line contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β€” 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 points.The following \(n\) lines contain the coordinates of the points \(x_i\) and \(y_i\) (\(0 \le x_i, y_i...
For each test case print one integer β€” the minimum sum of areas.
In the first two test cases the answer consists of 2 degenerate rectangles. In the third test case one of the possible answers consists of two rectangles \(1 \times 1\) with bottom left corners \((0,0)\) and \((9,9)\).
Input: 3 2 9 1 1 6 2 8 10 0 7 4 0 0 1 1 9 9 10 10 | Output: 0 0 2
Master
1
301
477
64
14
154
E
154E
E. Martian Colony
3,000
geometry
The first ship with the Earth settlers landed on Mars. The colonists managed to build n necessary structures on the surface of the planet (which can be regarded as a plane, and the construction can be regarded as points on it). But one day the scanners recorded suspicious activity on the outskirts of the colony. It was...
The first line contains two integers n and r (1 ≀ n ≀ 105, 1 ≀ r ≀ 50000) β€” the number of buildings and the active ranges of the generators, correspondingly.Next n lines contains the buildings' coordinates. The i + 1-th (1 ≀ i ≀ n) line contains two real numbers with at most three digits after the decimal point xi and ...
Print the single real number β€” the minimum area of the protected part containing all the buildings. The answer is accepted if absolute or relative error doesn't exceed 10 - 4.
In the first sample the given radius equals the radius of the circle circumscribed around the given points. That's why the circle that corresponds to it is the sought area. The answer is 25Ο€.In the second sample the area nearly coincides with the square which has vertexes in the given points.The area for the third samp...
Input: 3 50.00 0.0000.0 8.006 8.00 | Output: 78.5398163397
Master
1
1,443
597
175
1
785
A
785A
A. Anton and Polyhedrons
800
implementation; strings
Anton's favourite geometric figures are regular polyhedrons. Note that there are five kinds of regular polyhedrons: Tetrahedron. Tetrahedron has 4 triangular faces. Cube. Cube has 6 square faces. Octahedron. Octahedron has 8 triangular faces. Dodecahedron. Dodecahedron has 12 pentagonal faces. Icosahedron. Icosahedron ...
The first line of the input contains a single integer n (1 ≀ n ≀ 200 000) β€” the number of polyhedrons in Anton's collection.Each of the following n lines of the input contains a string si β€” the name of the i-th polyhedron in Anton's collection. The string can look like this: ""Tetrahedron"" (without quotes), if the i-t...
Output one number β€” the total number of faces in all the polyhedrons in Anton's collection.
In the first sample Anton has one icosahedron, one cube, one tetrahedron and one dodecahedron. Icosahedron has 20 faces, cube has 6 faces, tetrahedron has 4 faces and dodecahedron has 12 faces. In total, they have 20 + 6 + 4 + 12 = 42 faces.
Input: 4IcosahedronCubeTetrahedronDodecahedron | Output: 42
Beginner
2
554
748
91
7
366
E
366E
E. Dima and Magic Guitar
2,200
brute force; implementation; math
Dima loves Inna very much. He decided to write a song for her. Dima has a magic guitar with n strings and m frets. Dima makes the guitar produce sounds like that: to play a note, he needs to hold one of the strings on one of the frets and then pull the string. When Dima pulls the i-th string holding it on the j-th fret...
The first line of the input contains four integers n, m, k and s (1 ≀ n, m ≀ 2000, 1 ≀ k ≀ 9, 2 ≀ s ≀ 105). Then follow n lines, each containing m integers aij (1 ≀ aij ≀ k). The number in the i-th row and the j-th column (aij) means a note that the guitar produces on the i-th string and the j-th fret.The last line of ...
In a single line print a single number β€” the maximum possible complexity of the song.
Input: 4 6 5 73 1 2 2 3 13 2 2 2 5 54 2 2 2 5 33 2 2 1 4 32 3 1 4 1 5 1 | Output: 8
Hard
3
1,363
402
85
3
208
E
208E
E. Blood Cousins
2,100
binary search; data structures; dfs and similar; trees
Polycarpus got hold of a family relationship tree. The tree describes family relationships of n people, numbered 1 through n. Each person in the tree has no more than one parent.Let's call person a a 1-ancestor of person b, if a is the parent of b.Let's call person a a k-ancestor (k > 1) of person b, if person b has a ...
The first input line contains a single integer n (1 ≀ n ≀ 105) β€” the number of people in the tree. The next line contains n space-separated integers r1, r2, ..., rn, where ri (1 ≀ ri ≀ n) is the number of person i's parent or 0, if person i has no parent. It is guaranteed that family relationships don't form cycles.The...
Print m space-separated integers β€” the answers to Polycarpus' queries. Print the answers to the queries in the order, in which the queries occur in the input.
Input: 60 1 1 0 4 471 11 22 12 24 15 16 1 | Output: 0 0 1 0 0 1 1
Hard
4
941
543
158
2
1,227
G
1227G
G. Not Same
2,600
constructive algorithms
You are given an integer array \(a_1, a_2, \dots, a_n\), where \(a_i\) represents the number of blocks at the \(i\)-th position. It is guaranteed that \(1 \le a_i \le n\). In one operation you can choose a subset of indices of the given array and remove one block in each of these indices. You can't remove a block from ...
The first line contains a single integer \(n\) (\(1 \le n \le 10^3\)) β€” length of the given array.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β€” numbers of blocks at positions \(1, 2, \dots, n\).
In the first line print an integer \(op\) (\(0 \le op \le n+1\)).In each of the following \(op\) lines, print a binary string \(s\) of length \(n\). If \(s_i=\)'0', it means that the position \(i\) is not in the chosen subset. Otherwise, \(s_i\) should be equal to '1' and the position \(i\) is in the chosen subset.All ...
In the first example, the number of blocks decrease like that:\(\lbrace 5,5,5,5,5 \rbrace \to \lbrace 4,4,4,4,4 \rbrace \to \lbrace 4,3,3,3,3 \rbrace \to \lbrace 3,3,2,2,2 \rbrace \to \lbrace 2,2,2,1,1 \rbrace \to \lbrace 1,1,1,1,0 \rbrace \to \lbrace 0,0,0,0,0 \rbrace\). And we can note that each operation differs fro...
Input: 5 5 5 5 5 5 | Output: 6 11111 01111 10111 11011 11101 11110
Expert
1
527
237
549
12
132
C
132C
C. Logo Turtle
1,800
dp
A lot of people associate Logo programming language with turtle graphics. In this case the turtle moves along the straight line and accepts commands ""T"" (""turn around"") and ""F"" (""move 1 unit forward"").You are given a list of commands that will be given to the turtle. You have to change exactly n commands from t...
The first line of input contains a string commands β€” the original list of commands. The string commands contains between 1 and 100 characters, inclusive, and contains only characters ""T"" and ""F"".The second line contains an integer n (1 ≀ n ≀ 50) β€” the number of commands you have to change in the list.
Output the maximum distance from the starting point to the ending point of the turtle's path. The ending point of the turtle's path is turtle's coordinate after it follows all the commands of the modified list.
In the first example the best option is to change the second command (""T"") to ""F"" β€” this way the turtle will cover a distance of 2 units.In the second example you have to change two commands. One of the ways to cover maximal distance of 6 units is to change the fourth command and first or last one.
Input: FT1 | Output: 2
Medium
1
479
306
210
1
609
C
609C
C. Load Balancing
1,500
implementation; math
In the school computer room there are n servers which are responsible for processing several computing tasks. You know the number of scheduled tasks for each server: there are mi tasks assigned to the i-th server.In order to balance the load for each server, you want to reassign some tasks to make the difference betwee...
The first line contains positive number n (1 ≀ n ≀ 105) β€” the number of the servers. The second line contains the sequence of non-negative integers m1, m2, ..., mn (0 ≀ mi ≀ 2Β·104), where mi is the number of tasks assigned to the i-th server.
Print the minimum number of seconds required to balance the load.
In the first example two seconds are needed. In each second, a single task from server #2 should be moved to server #1. After two seconds there should be 3 tasks on server #1 and 4 tasks on server #2.In the second example the load is already balanced.A possible sequence of task movements for the third example is: move ...
Input: 21 6 | Output: 2
Medium
2
757
242
65
6
1,573
A
1573A
A. Countdown
800
greedy
You are given a digital clock with \(n\) digits. Each digit shows an integer from \(0\) to \(9\), so the whole clock shows an integer from \(0\) to \(10^n-1\). The clock will show leading zeroes if the number is smaller than \(10^{n-1}\).You want the clock to show \(0\) with as few operations as possible. In an operati...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)).The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β€” number of digits on the clock.The second line of each test case contains a string of \(n\) digits \(s_1, s_2, \ld...
For each test case, print one integer: the minimum number of operations needed to make the clock show \(0\).
In the first example, it's optimal to just decrease the number \(7\) times.In the second example, we can first swap the first and last position and then decrease the number by \(1\).In the third example, the clock already shows \(0\), so we don't have to perform any operations.
Input: 7 3 007 4 1000 5 00000 3 103 4 2020 9 123456789 30 001678294039710047203946100020 | Output: 7 2 0 5 6 53 115
Beginner
1
589
481
108
15
1,670
D
1670D
D. Very Suspicious
1,700
binary search; brute force; geometry; greedy; implementation; math
Sehr Sus is an infinite hexagonal grid as pictured below, controlled by MennaFadali, ZerooCool and Hosssam.They love equilateral triangles and want to create \(n\) equilateral triangles on the grid by adding some straight lines. The triangles must all be empty from the inside (in other words, no straight line or hexago...
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases. Then \(t\) test cases follow.Each test case contains a single integer \(n\) (\(1 \le n \le 10^{9}\)) β€” the required number of equilateral triangles.
For each test case, print the minimum number of lines needed to have \(n\) or more equilateral triangles.
In the first and second test cases only 2 lines are needed. After adding the first line, no equilateral triangles will be created no matter where it is added. But after adding the second line, two more triangles will be created at once. In the third test case, the minimum needed is 3 lines as shown below.
Input: 41234567 | Output: 2 2 3 83
Medium
6
641
246
105
16
31
D
31D
D. Chocolate
2,000
dfs and similar; implementation
Bob has a rectangular chocolate bar of the size W Γ— H. He introduced a cartesian coordinate system so that the point (0, 0) corresponds to the lower-left corner of the bar, and the point (W, H) corresponds to the upper-right corner. Bob decided to split the bar into pieces by breaking it. Each break is a segment parall...
The first line contains 3 integers W, H and n (1 ≀ W, H, n ≀ 100) β€” width of the bar, height of the bar and amount of breaks. Each of the following n lines contains four integers xi, 1, yi, 1, xi, 2, yi, 2 β€” coordinates of the endpoints of the i-th break (0 ≀ xi, 1 ≀ xi, 2 ≀ W, 0 ≀ yi, 1 ≀ yi, 2 ≀ H, or xi, 1 = xi, 2, ...
Output n + 1 numbers β€” areas of the resulting parts in the increasing order.
Input: 2 2 21 0 1 20 1 1 1 | Output: 1 1 2
Hard
2
898
554
76
0
289
A
289A
A. Polo the Penguin and Segments
1,100
brute force; implementation
Little penguin Polo adores integer segments, that is, pairs of integers [l; r] (l ≀ r). He has a set that consists of n integer segments: [l1; r1], [l2; r2], ..., [ln; rn]. We know that no two segments of this set intersect. In one move Polo can either widen any segment of the set 1 unit to the left or 1 unit to the ri...
The first line contains two integers n and k (1 ≀ n, k ≀ 105). Each of the following n lines contain a segment as a pair of integers li and ri ( - 105 ≀ li ≀ ri ≀ 105), separated by a space.It is guaranteed that no two segments intersect. In other words, for any two integers i, j (1 ≀ i < j ≀ n) the following inequalit...
In a single line print a single integer β€” the answer to the problem.
Input: 2 31 23 4 | Output: 2
Easy
2
714
355
68
2
1,594
B
1594B
B. Special Numbers
1,100
bitmasks; math
Theofanis really likes sequences of positive integers, thus his teacher (Yeltsa Kcir) gave him a problem about a sequence that consists of only special numbers.Let's call a positive number special if it can be written as a sum of different non-negative powers of \(n\). For example, for \(n = 4\) number \(17\) is specia...
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.The first and only line of each test case contains two integers \(n\) and \(k\) (\(2 \le n \le 10^9\); \(1 \le k \le 10^9\)).
For each test case, print one integer β€” the \(k\)-th special number in increasing order modulo \(10^9+7\).
For \(n = 3\) the sequence is \([1,3,4,9...]\)
Input: 3 3 4 2 12 105 564 | Output: 9 12 3595374
Easy
2
566
222
106
15
1,876
F
1876F
F. Indefinite Clownfish
3,500
binary search; graphs
Pak Chanek has just bought an empty fish tank and he has been dreaming to fill it with his favourite kind of fish, clownfish. Pak Chanek likes clownfish because of their ability to change their genders on demand. Because of the size of his fish tank, Pak Chanek wants to buy exactly \(k\) clownfish to fill the tank.Pak ...
The first line contains two integers \(n\) and \(k\) (\(2 \leq k \leq n \leq 2\cdot10^5\)) β€” the number of clownfish in the shop and the number of clownfish Pak Chanek has to buy.The second line contains \(n\) integers \(a_1,a_2,a_3,\ldots,a_n\) (\(1\leq a_i\leq n\)) β€” the size of each clownfish.
An integer representing the minimum possible value of \(r-l\). If it is impossible to buy exactly \(k\) clownfish and satisfy the problem's condition, print \(-1\).
In the first example, Pak Chanek can do the following: Do not buy clownfish \(1\). Buy clownfish \(2\) and assign it as male. Buy clownfish \(3\) and assign it as female. Buy clownfish \(4\) and assign it as male. Buy clownfish \(5\) and assign it as male. Do not buy clownfish \(6\). Buy clownfish \(7\) and assign it a...
Input: 9 6 2 4 2 3 2 4 1 3 4 | Output: 6
Master
2
1,979
297
164
18
1,870
E
1870E
E. Another MEX Problem
2,300
bitmasks; brute force; dp; shortest paths
You are given an array of integers \(a\) of size \(n\). You can choose a set of non-overlapping subarrays of the given array (note that some elements may be not included in any subarray, this is allowed). For each selected subarray, calculate the MEX of its elements, and then calculate the bitwise XOR of all the obtain...
The first line contains an integer \(t\) (\(1 \leq t \leq 5000\)) β€” the number of test cases. This is followed by the description of the test cases.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 5000\)) β€” the size of the array \(a\).The second line of each test case contains \(n\) integers ...
For each test case, output a single number β€” the maximum possible XOR of the MEX values of the selected subarrays.
In the first test case, the maximum XOR is \(2\) if we take the entire array, \(\operatorname{MEX}([1, 0]) = 2\).In the second test case, the maximum XOR is \(6\) if we partition the array into segments \([1, 2, 0]\) and \([7, 1, 2, 0, 2, 4, 3]\): \(\operatorname{MEX}([1, 2, 0]) = 3\), \(\operatorname{MEX}([7, 1, 2, 0,...
Input: 421 0101 2 0 7 1 2 0 2 4 3102 1 0 7 1 2 0 2 4 331 2 1 | Output: 2 6 7 0
Expert
4
803
485
114
18
2,118
D2
2118D2
D2. Red Light, Green Light (Hard version)
2,200
binary search; brute force; data structures; dfs and similar; dp; graphs; implementation; math; number theory
This is the hard version of the problem. The only difference is the constraint on \(k\) and the total sum of \(n\) and \(q\) across all test cases. You can make hacks only if both versions of the problem are solved.You are given a strip of length \(10^{15}\) and a constant \(k\). There are exactly \(n\) cells that cont...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 2\cdot10^5\)). The description of the test cases follows. The first line of each test case contains two integers \(n\), \(k\) (\(\mathbf{1 \le n \le 2\cdot10^5}\) and \(\mathbf{1 \le k \le 10^{15}}\)) β€” the num...
For each test case, output \(q\) lines. Each line should contain ""YES"" if you will eventually leave the strip 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.
In the first test case, the following happens at starting positions \(1\), \(2\), and \(3\): And the following in the second test case at starting position \(2\):
Input: 42 21 41 031 2 39 41 2 3 4 5 6 7 8 93 2 1 0 1 3 3 1 152 5 6 7 84 21 2 3 40 0 0 041 2 3 43 41 2 33 1 131 2 3 | Output: YES NO YES YES YES YES NO NO YES YES NO NO YES NO YES
Hard
9
1,011
1,048
295
21
1,184
B2
1184B2
B2. The Doctor Meets Vader (Medium)
2,200
flows; graph matchings; graphs; shortest paths; sortings
Thanks to the Doctor's help, the rebels managed to steal enough gold to launch a full-scale attack on the Empire! However, Darth Vader is looking for revenge and wants to take back his gold.The rebels have hidden the gold in various bases throughout the galaxy. Darth Vader and the Empire are looking to send out their s...
The first line contains two integers \(n\) and \(m\) (\(1 \leq n \leq 100\), \(0 \leq m \leq 10000\)), the number of nodes and the number of edges, respectively.The next \(m\) lines contain two integers \(u\) and \(v\) (\(1 \leq u\), \(v \leq n\)) denoting an undirected edge between the two nodes.The next line contains...
Print a single integer, the minimum cost in terms of gold.
One way to minimize the cost is to build \(4\) dummy bases, for a total cost of \(4 \times 3 = 12\).One empire spaceship will be assigned to attack each of these dummy bases, resulting in zero actual bases attacked.
Input: 6 7 1 2 2 3 3 4 4 6 6 5 4 4 3 6 4 2 7 3 1 10 2 3 8 2 5 1 0 6 5 4 3 7 5 2 | Output: 12
Hard
5
2,057
887
58
11
1,010
F
1010F
F. Tree
3,400
fft; graphs; trees
The Main Martian Tree grows on Mars. It is a binary tree (a rooted tree, with no more than two sons at each vertex) with \(n\) vertices, where the root vertex has the number \(1\). Its fruits are the Main Martian Fruits. It's summer now, so this tree does not have any fruit yet.Autumn is coming soon, and leaves and bra...
The first line contains two integers: \(n\) and \(x\) (\(1 \le n \le 10^5\), \(0 \le x \le 10^{18}\)) β€” the size of the tree and the number of fruits in the root.The \(i\)-th of the following \((n-1)\) lines contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le n\)) β€” vertices connected by the \(i\)-th edge o...
Print one number β€” the number of configurations of the resulting tree modulo \(998244353\).
Consider the first example. There are \(2\) fruits at the vertex \(1\). The following \(13\) options are possible: there is no vertex \(2\), there is no vertex \(3\); there is no vertex \(2\), there are no fruits at the vertex \(3\); there is no vertex \(2\), there is \(1\) fruit at the vertex \(3\); there is no vertex...
Input: 3 21 21 3 | Output: 13
Master
3
1,346
434
91
10
73
D
73D
D. FreeDiv
2,200
dfs and similar; graphs; greedy
Vasya plays FreeDiv. In this game he manages a huge state, which has n cities and m two-way roads between them. Unfortunately, not from every city you can reach any other one moving along these roads. Therefore Vasya decided to divide the state into provinces so that in every province, one could reach from every city a...
The first line contains three integers n, m and k (1 ≀ n, k ≀ 106, 0 ≀ m ≀ 106). Each of the next m lines contains two integers. They are the numbers of cities connected by a corresponding road. No road connects city to itself and there is at most one road between each pair of cities.
Print a single number, the minimum number of additional roads.
In the first example only one province exists, so it is not necessary to build any tunnels or roads.In the second example two provinces exist. It is possible to merge the provinces by building a tunnel between cities 1 and 3.In the third example at least one additional road is necessary. For example it is possible to b...
Input: 3 3 21 22 33 1 | Output: 0
Hard
3
1,363
285
62
0
1,324
A
1324A
A. Yet Another Tetris Problem
900
implementation; number theory
You are given some Tetris field consisting of \(n\) columns. The initial height of the \(i\)-th column of the field is \(a_i\) blocks. On top of these columns you can place only figures of size \(2 \times 1\) (i.e. the height of this figure is \(2\) blocks and the width of this figure is \(1\) block). Note that you can...
The first line of the input contains one integer \(t\) (\(1 \le t \le 100\)) β€” the number of test cases.The next \(2t\) lines describe test cases. The first line of the test case contains one integer \(n\) (\(1 \le n \le 100\)) β€” the number of columns in the Tetris field. The second line of the test case contains \(n\)...
For each test case, print the answer β€” ""YES"" (without quotes) if you can clear the whole Tetris field and ""NO"" otherwise.
The first test case of the example field is shown below:Gray lines are bounds of the Tetris field. Note that the field has no upper bound.One of the correct answers is to first place the figure in the first column. Then after the second step of the process, the field becomes \([2, 0, 2]\). Then place the figure in the ...
Input: 4 3 1 1 3 4 1 1 2 1 2 11 11 1 100 | Output: YES NO YES YES
Beginner
2
958
459
125
13
2,029
E
2029E
E. Common Generator
2,100
brute force; constructive algorithms; math; number theory
For two integers \(x\) and \(y\) (\(x,y\ge 2\)), we will say that \(x\) is a generator of \(y\) if and only if \(x\) can be transformed to \(y\) by performing the following operation some number of times (possibly zero): Choose a divisor \(d\) (\(d\ge 2\)) of \(x\), then increase \(x\) by \(d\). For example, \(3\) is a...
Each test contains multiple test cases. The first line of the input contains a single integer \(t\) (\(1\le t\le 10^4\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1\le n\le 10^5\)) β€” the length of the array \(a\).The second line...
For each test case, output a single integer \(x\) β€” the integer you found. Print \(-1\) if there does not exist a valid \(x\).If there are multiple answers, you may output any of them.
In the first test case, for \(x=2\): \(2\) is a generator of \(8\), since we can perform the following operations: \(2 \xrightarrow{d = 2} 4 \xrightarrow{d = 4} 8\); \(2\) is a generator of \(9\), since we can perform the following operations: \(2 \xrightarrow{d = 2} 4 \xrightarrow{d = 2} 6 \xrightarrow{d = 3} 9\). \(2...
Input: 438 9 1042 3 4 52147 15453 6 8 25 100000 | Output: 2 -1 7 3
Hard
4
926
574
184
20
377
D
377D
D. Developing Game
2,400
data structures
Pavel is going to make a game of his dream. However, he knows that he can't make it on his own so he founded a development company and hired n workers of staff. Now he wants to pick n workers from the staff who will be directly responsible for developing a game.Each worker has a certain skill level vi. Besides, each wo...
The first line contains a single integer n (1 ≀ n ≀ 105) β€” the number of workers Pavel hired.Each of the following n lines contains three space-separated integers li, vi, ri (1 ≀ li ≀ vi ≀ ri ≀ 3Β·105) β€” the minimum skill value of the workers that the i-th worker can work with, the i-th worker's skill and the maximum sk...
In the first line print a single integer m β€” the number of workers Pavel must pick for developing the game.In the next line print m space-separated integers β€” the numbers of the workers in any order.If there are multiple optimal solutions, print any of them.
Input: 42 8 91 4 73 6 85 8 10 | Output: 31 3 4
Expert
1
730
380
258
3
1,986
F
1986F
F. Non-academic Problem
1,900
dfs and similar; graphs; trees
You are given a connected undirected graph, the vertices of which are numbered with integers from \(1\) to \(n\). Your task is to minimize the number of pairs of vertices \(1 \leq u < v \leq n\) between which there exists a path in this graph. To achieve this, you can remove exactly one edge from the graph.Find the sma...
Each test consists of several 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 two integers \(n\) and \(m\) (\(2 \leq n \leq 10^5\), \(n - 1 \leq m \leq \min(10...
For each set of input data, output the smallest number of pairs of reachable vertices, if exactly one edge can be removed.
In the first set of input data, we will remove the single edge \((1, 2)\) and the only pair of vertices \((1, 2)\) will become unreachable from each other.In the second set of input data, no matter which edge we remove, all vertices will be reachable from each other.In the fourth set of input data, the graph looks like...
Input: 62 11 23 31 22 31 35 51 21 33 44 55 36 71 21 32 33 44 54 65 65 51 21 32 32 43 510 121 21 32 32 44 55 66 77 43 88 99 1010 8 | Output: 0 3 4 6 6 21
Hard
3
354
808
122
19
276
A
276A
A. Lunch Rush
900
implementation
Having written another programming contest, three Rabbits decided to grab some lunch. The coach gave the team exactly k time units for the lunch break.The Rabbits have a list of n restaurants to lunch in: the i-th restaurant is characterized by two integers fi and ti. Value ti shows the time the Rabbits need to lunch i...
The first line contains two space-separated integers β€” n (1 ≀ n ≀ 104) and k (1 ≀ k ≀ 109) β€” the number of restaurants in the Rabbits' list and the time the coach has given them to lunch, correspondingly. Each of the next n lines contains two space-separated integers β€” fi (1 ≀ fi ≀ 109) and ti (1 ≀ ti ≀ 109) β€” the char...
In a single line print a single integer β€” the maximum joy value that the Rabbits will get from the lunch.
Input: 2 53 34 5 | Output: 4
Beginner
1
785
355
105
2
1,975
B
1975B
B. 378QAQ and Mocha's Array
1,000
brute force; greedy; math; sortings
Mocha likes arrays, so before her departure, 378QAQ gave her an array \(a\) consisting of \(n\) positive integers as a gift.Mocha thinks that \(a\) is beautiful if there exist two numbers \(i\) and \(j\) (\(1\leq i,j\leq n\), \(i\neq j\)) such that for all \(k\) (\(1 \leq k \leq n\)), \(a_k\) is divisible\(^\dagger\) b...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\leq t\leq 500\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3\leq n\leq 10^5\)) β€” the length of the array \(a\).The second line of each test case contai...
For each test case, output ""Yes"" if array \(a\) is beautiful, and output ""No"" otherwise.You can output ""Yes"" and ""No"" in any case (for example, strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive response).
In the first test case, any two numbers in the array are coprime, so the answer is ""No"".In the second test case, we can pick \(i=2\) and \(j=1\). Since every number in the array is divisible by \(a_i = 1\), the answer is ""Yes"".In the third test case, we can pick \(i=3\) and \(j=5\). \(2\) and \(4\) is divisible by ...
Input: 437 3 857 1 9 3 554 12 2 6 357 49 9 3 1000000000 | Output: No Yes Yes No
Beginner
4
487
505
241
19
98
B
98B
B. Help King
2,200
implementation; probabilities; trees
This is the modification of the problem used during the official round. Unfortunately, author's solution of the original problem appeared wrong, so the problem was changed specially for the archive.Once upon a time in a far away kingdom lived the King. The King had a beautiful daughter, Victoria. They lived happily, bu...
The first line contains a single integer n from the problem's statement (1 ≀ n ≀ 10000).
Print the sought expected number of tosses as an irreducible fraction in the following form: ""a/b"" (without the quotes) without leading zeroes.
Input: 2 | Output: 1/1
Hard
3
1,943
88
145
0
608
A
608A
A. Saitama Destroys Hotel
1,000
implementation; math
Saitama accidentally destroyed a hotel again. To repay the hotel company, Genos has volunteered to operate an elevator in one of its other hotels. The elevator is special β€” it starts on the top floor, can only move down, and has infinite capacity. Floors are numbered from 0 to s and elevator initially starts on floor s...
The first line of input contains two integers n and s (1 ≀ n ≀ 100, 1 ≀ s ≀ 1000) β€” the number of passengers and the number of the top floor respectively.The next n lines each contain two space-separated integers fi and ti (1 ≀ fi ≀ s, 1 ≀ ti ≀ 1000) β€” the floor and the time of arrival in seconds for the passenger numb...
Print a single integer β€” the minimum amount of time in seconds needed to bring all the passengers to floor 0.
In the first sample, it takes at least 11 seconds to bring all passengers to floor 0. Here is how this could be done:1. Move to floor 5: takes 2 seconds.2. Pick up passenger 3.3. Move to floor 3: takes 2 seconds.4. Wait for passenger 2 to arrive: takes 4 seconds.5. Pick up passenger 2.6. Go to floor 2: takes 1 second.7...
Input: 3 72 13 85 2 | Output: 11
Beginner
2
605
325
109
6
1,898
D
1898D
D. Absolute Beauty
1,900
greedy; math
Kirill has two integer arrays \(a_1,a_2,\ldots,a_n\) and \(b_1,b_2,\ldots,b_n\) of length \(n\). He defines the absolute beauty of the array \(b\) as $$$\(\sum_{i=1}^{n} |a_i - b_i|.\)\( Here, \)|x|\( denotes the absolute value of \)x\(.Kirill can perform the following operation at most once: select two indices \)i\( a...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10\,000\)). The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2\leq n\leq 2\cdot 10^5\)) β€” the length of the arrays \(a\) and \(b\).The second line of e...
For each test case, output one integer β€” the maximum possible absolute beauty of the array \(b\) after no more than one swap.
In the first test case, each of the possible swaps does not change the array \(b\).In the second test case, the absolute beauty of the array \(b\) without performing the swap is \(|1-1| + |2-2| = 0\). After swapping the first and the second element in the array \(b\), the absolute beauty becomes \(|1-2| + |2-1| = 2\). ...
Input: 631 3 53 3 321 21 221 22 141 2 3 45 6 7 8101 8 2 5 3 5 3 1 1 32 9 2 4 8 2 3 5 3 1347326 6958 3586533587 35863 59474 | Output: 4 2 2 16 31 419045
Hard
2
504
644
125
18
280
A
280A
A. Rectangle Puzzle
2,000
geometry
You are given two rectangles on a plane. The centers of both rectangles are located in the origin of coordinates (meaning the center of the rectangle's symmetry). The first rectangle's sides are parallel to the coordinate axes: the length of the side that is parallel to the Ox axis, equals w, the length of the side tha...
The first line contains three integers w, h, Ξ± (1 ≀ w, h ≀ 106; 0 ≀ Ξ± ≀ 180). Angle Ξ± is given in degrees.
In a single line print a real number β€” the area of the region which belongs to both given rectangles.The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 6.
The second sample has been drawn on the picture above.
Input: 1 1 45 | Output: 0.828427125
Hard
1
600
106
195
2
453
D
453D
D. Little Pony and Elements of Harmony
3,000
dp; matrices
The Elements of Harmony are six supernatural artifacts representing subjective aspects of harmony. They are arguably the most powerful force in Equestria. The inside of Elements of Harmony can be seen as a complete graph with n vertices labeled from 0 to n - 1, where n is a power of two, equal to 2m. The energy in Elem...
The first line contains three integers m, t and p (1 ≀ m ≀ 20; 0 ≀ t ≀ 1018; 2 ≀ p ≀ 109). The following line contains n (n = 2m) integers e0[i] (1 ≀ e0[i] ≀ 109; 0 ≀ i < n). The next line contains m + 1 integers b[i] (0 ≀ b[i] ≀ 109; 0 ≀ i ≀ m).
Output n lines, the i-th line must contain a single integer et[i] modulo p.
Input: 2 2 100004 1 2 30 1 0 | Output: 146614
Master
2
807
246
75
4
1,237
A
1237A
A. Balanced Rating Changes
1,000
implementation; math
Another Codeforces Round has just finished! It has gathered \(n\) participants, and according to the results, the expected rating change of participant \(i\) is \(a_i\). These rating changes are perfectly balanced β€” their sum is equal to \(0\).Unfortunately, due to minor technical glitches, the round is declared semi-r...
The first line contains a single integer \(n\) (\(2 \le n \le 13\,845\)), denoting the number of participants.Each of the next \(n\) lines contains a single integer \(a_i\) (\(-336 \le a_i \le 1164\)), denoting the rating change of the \(i\)-th participant.The sum of all \(a_i\) is equal to \(0\).
Output \(n\) integers \(b_i\), each denoting the modified rating change of the \(i\)-th participant in order of input.For any \(i\), it must be true that either \(b_i = \lfloor \frac{a_i}{2} \rfloor\) or \(b_i = \lceil \frac{a_i}{2} \rceil\). The sum of all \(b_i\) must be equal to \(0\).If there are multiple solutions...
In the first example, \(b_1 = 5\), \(b_2 = -3\) and \(b_3 = -2\) is another correct solution.In the second example there are \(6\) possible solutions, one of them is shown in the example output.
Input: 3 10 -5 -5 | Output: 5 -2 -3
Beginner
2
1,009
298
388
12
1,569
E
1569E
E. Playoff Restoration
2,600
bitmasks; brute force; hashing; implementation; meet-in-the-middle
\(2^k\) teams participate in a playoff tournament. The tournament consists of \(2^k - 1\) games. They are held as follows: first of all, the teams are split into pairs: team \(1\) plays against team \(2\), team \(3\) plays against team \(4\) (exactly in this order), and so on (so, \(2^{k-1}\) games are played in that p...
The only line contains three integers \(k\), \(A\) and \(h\) (\(1 \le k \le 5\); \(100 \le A \le 10^8\); \(0 \le h \le 998244352\)).
If it is impossible to find any placing of the teams that is consistent with the data you have, print one integer \(-1\).Otherwise, print \(2^k\) integers, where \(i\)-th integer should be equal to \(p_i\) (the place of the \(i\)-th team). Note that your answer should be consistent with one of the possible ways the tou...
The tournament for the first example is described in the statement.For the third example, the placing \([1, 2, 3, 3]\) (team \(1\) gets place \(1\), team \(2\) gets place \(2\), teams \(3\) and \(4\) get place \(3\)) could result in a hash value of \(7020100\) with \(A = 100\), but no tournament can result in such plac...
Input: 3 1337 75275197 | Output: 5 3 5 2 1 5 5 3
Expert
5
2,031
132
632
15
314
E
314E
E. Sereja and Squares
2,900
dp
Sereja painted n points on the plane, point number i (1 ≀ i ≀ n) has coordinates (i, 0). Then Sereja marked each point with a small or large English letter. Sereja don't like letter ""x"", so he didn't use it to mark points. Sereja thinks that the points are marked beautifully if the following conditions holds: all poi...
The first line contains integer n the number of points (1 ≀ n ≀ 105). The second line contains a sequence consisting of n small English letters and question marks β€” the sequence of letters, that mark points, in order of increasing x-coordinate of points. Question marks denote the points without letters (Petya erased th...
In a single line print the answer to the problem modulo 4294967296. If there is no way to return the removed letters, print number 0.Please, do not write the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier.
Input: 4a??? | Output: 50
Master
1
1,005
393
282
3
1,719
A
1719A
A. Chip Game
800
games; math
Burenka and Tonya are playing an old Buryat game with a chip on a board of \(n \times m\) cells.At the beginning of the game, the chip is located in the lower left corner of the board. In one move, the player can move the chip to the right or up by any odd number of cells (but you cannot move the chip both to the right...
The first line contains one integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. The following is a description of the input data sets.The only line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10^9\)) β€” the dimensions of the game board.
For each test case print a single line β€” the name of the winner of the game (""Burenka"" or ""Tonya"").
In the first case, Burenka has no move, so Tonya wins.In the second case, Burenka can move \(3\) cells to the right, after which Tony will not be able to make a move, which means that Burenka wins.In the third case, Burenka can move \(5\) squares to the right. Then we can say that we have a game on a board of \(1 \time...
Input: 61 11 45 62 26 3999999999 1000000000 | Output: Tonya Burenka Burenka Tonya Burenka Burenka
Beginner
2
900
282
103
17
94
B
94B
B. Friends
1,300
graphs; implementation; math
One day Igor K. stopped programming and took up math. One late autumn evening he was sitting at a table reading a book and thinking about something. The following statement caught his attention: ""Among any six people there are either three pairwise acquainted people or three pairwise unacquainted people""Igor just cou...
The first line contains an integer m (0 ≀ m ≀ 10), which is the number of relations of acquaintances among the five friends of Igor's.Each of the following m lines contains two integers ai and bi (1 ≀ ai, bi ≀ 5;ai β‰  bi), where (ai, bi) is a pair of acquainted people. It is guaranteed that each pair of the acquaintance...
Print ""FAIL"", if among those five people there are no either three pairwise acquainted or three pairwise unacquainted people. Otherwise print ""WIN"".
Input: 41 32 31 45 3 | Output: WIN
Easy
3
881
456
152
0
621
E
621E
E. Wet Shark and Blocks
2,000
dp; matrices
There are b blocks of digits. Each one consisting of the same n digits, which are given to you in the input. Wet Shark must choose exactly one digit from each block and concatenate all of those digits together to form one large integer. For example, if he chooses digit 1 from the first block and digit 2 from the second...
The first line of the input contains four space-separated integers, n, b, k and x (2 ≀ n ≀ 50 000, 1 ≀ b ≀ 109, 0 ≀ k < x ≀ 100, x β‰₯ 2) β€” the number of digits in one block, the number of blocks, interesting remainder modulo x and modulo x itself.The next line contains n space separated integers ai (1 ≀ ai ≀ 9), that gi...
Print the number of ways to pick exactly one digit from each blocks, such that the resulting integer equals k modulo x.
In the second sample possible integers are 22, 26, 62 and 66. None of them gives the remainder 1 modulo 2.In the third sample integers 11, 13, 21, 23, 31 and 33 have remainder 1 modulo 2. There is exactly one way to obtain each of these integers, so the total answer is 6.
Input: 12 1 5 103 5 6 7 8 9 5 1 1 1 1 5 | Output: 3
Hard
2
761
358
119
6
1,520
B
1520B
B. Ordinary Numbers
800
brute force; math; number theory
Let's call a positive integer \(n\) ordinary if in the decimal notation all its digits are the same. For example, \(1\), \(2\) and \(99\) are ordinary numbers, but \(719\) and \(2021\) are not ordinary numbers.For a given number \(n\), find the number of ordinary numbers among the numbers from \(1\) to \(n\).
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.Each test case is characterized by one integer \(n\) (\(1 \le n \le 10^9\)).
For each test case output the number of ordinary numbers among numbers from \(1\) to \(n\).
Input: 6 1 2 3 4 5 100 | Output: 1 2 3 4 5 18
Beginner
3
310
171
91
15
1,896
D
1896D
D. Ones and Twos
1,700
binary search; data structures; divide and conquer; math; two pointers
You are given a \(1\)-indexed array \(a\) of length \(n\) where each element is \(1\) or \(2\).Process \(q\) queries of the following two types: ""1 s"": check if there exists a subarray\(^{\dagger}\) of \(a\) whose sum equals to \(s\). ""2 i v"": change \(a_i\) to \(v\). \(^{\dagger}\) An array \(b\) is a subarray of ...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(1\le n,q\le 10^5\)) β€” the length of array \(a\) and the number of ...
For each query with \(\mathrm{op}=1\), output ""YES"" in one line if there exists a subarray of \(a\) whose sum is equals to \(s\), 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.
Consider the first example: The answer for the first query is ""YES"" because \(a_1+a_2+a_3=2+1+2=5\). The answer for the second query is ""YES"" because \(a_1+a_2+a_3+a_4=2+1+2+1=6\). The answer for the third query is ""NO"" because we cannot find any subarray of \(a\) whose sum is \(7\). After the fourth query, the a...
Input: 25 52 1 2 1 21 51 61 72 4 21 73 22 2 21 61 5 | Output: YES YES NO YES YES NO
Medium
5
553
924
317
18
889
E
889E
E. Mod Mod Mod
3,000
binary search; dp; math
You are given a sequence of integers a1, a2, ..., an. Let , and for 1 ≀ i < n. Here, denotes the modulus operation. Find the maximum value of f(x, 1) over all nonnegative integers x.
The first line contains a single integer n (1 ≀ n ≀ 200000) β€” the length of the sequence.The second lines contains n integers a1, a2, ..., an (1 ≀ ai ≀ 1013) β€” the elements of the sequence.
Output a single integer β€” the maximum value of f(x, 1) over all nonnegative integers x.
In the first example you can choose, for example, x = 19.In the second example you can choose, for example, x = 3 or x = 2.
Input: 210 5 | Output: 13
Master
3
182
189
87
8
659
G
659G
G. Fence Divercity
2,300
combinatorics; dp; number theory
Long ago, Vasily built a good fence at his country house. Vasily calls a fence good, if it is a series of n consecutively fastened vertical boards of centimeter width, the height of each in centimeters is a positive integer. The house owner remembers that the height of the i-th board to the left is hi.Today Vasily deci...
The first line contains integer n (1 ≀ n ≀ 1 000 000) β€” the number of boards in Vasily's fence.The second line contains n space-separated numbers h1, h2, ..., hn (1 ≀ hi ≀ 109), where hi equals the height of the i-th board to the left.
Print the remainder after dividing r by 1 000 000 007, where r is the number of ways to cut exactly one connected part so that the part consisted of the upper parts of the boards and the remaining fence was good.
From the fence from the first example it is impossible to cut exactly one piece so as the remaining fence was good.All the possible variants of the resulting fence from the second sample look as follows (the grey shows the cut out part):
Input: 21 1 | Output: 0
Expert
3
995
235
212
6
1,562
A
1562A
A. The Miracle and the Sleeper
800
greedy; math
You are given two integers \(l\) and \(r\), \(l\le r\). Find the largest possible value of \(a \bmod b\) over all pairs \((a, b)\) of integers for which \(r\ge a \ge b \ge l\).As a reminder, \(a \bmod b\) is a remainder we get when dividing \(a\) by \(b\). For example, \(26 \bmod 8 = 2\).
Each test contains multiple test cases.The first line contains one positive integer \(t\) \((1\le t\le 10^4)\), denoting the number of test cases. Description of the test cases follows.The only line of each test case contains two integers \(l\), \(r\) (\(1\le l \le r \le 10^9\)).
For every test case, output the largest possible value of \(a \bmod b\) over all pairs \((a, b)\) of integers for which \(r\ge a \ge b \ge l\).
In the first test case, the only allowed pair is \((a, b) = (1, 1)\), for which \(a \bmod b = 1 \bmod 1 = 0\).In the second test case, the optimal choice is pair \((a, b) = (1000000000, 999999999)\), for which \(a \bmod b = 1\).
Input: 4 1 1 999999999 1000000000 8 26 1 999999999 | Output: 0 1 12 499999999
Beginner
2
289
280
143
15
1,091
C
1091C
C. New Year and the Sphere Transmission
1,400
math; number theory
There are \(n\) people sitting in a circle, numbered from \(1\) to \(n\) in the order in which they are seated. That is, for all \(i\) from \(1\) to \(n-1\), the people with id \(i\) and \(i+1\) are adjacent. People with id \(n\) and \(1\) are adjacent as well.The person with id \(1\) initially has a ball. He picks a p...
The only line consists of a single integer \(n\) (\(2 \leq n \leq 10^9\)) β€” the number of people playing with the ball.
Suppose the set of all fun values is \(f_1, f_2, \dots, f_m\).Output a single line containing \(m\) space separated integers \(f_1\) through \(f_m\) in increasing order.
In the first sample, we've already shown that picking \(k = 4\) yields fun value \(9\), as does \(k = 2\). Picking \(k = 6\) results in fun value of \(1\). For \(k = 3\) we get fun value \(5\) and with \(k = 1\) or \(k = 5\) we get \(21\). In the second sample, the values \(1\), \(10\), \(28\), \(64\) and \(136\) are a...
Input: 6 | Output: 1 5 9 21
Easy
2
1,217
119
169
10
1,545
E2
1545E2
E2. AquaMoon and Time Stop (hard version)
3,500
data structures; dp
Note that the differences between easy and hard versions are the constraints on \(n\) and the time limit. You can make hacks only if both versions are solved.AquaMoon knew through foresight that some ghosts wanted to curse tourists on a pedestrian street. But unfortunately, this time, these ghosts were hiding in a barr...
The first line contains a single integer \(n\) (\(1\le n\le 2 \cdot 10^5\)) β€” the number of curses.The next line contains a single integer \(x\) (\(1\le x\le 10^6\)) β€” the initial coordinate of the person.The following \(n\) lines contain four integers \(tl_i\), \(tr_i\), \(l_i\), \(r_i\) each (\(1\le tl_i\le tr_i\le 1...
Print a single integer β€” the minimum energy which AquaMoon needs to spent, rounded up to the nearest integer (in case there are two nearest integers you should round the answer to the highest of them).
Input: 2 1 1 2 1 2 2 3 2 3 | Output: 2
Master
2
1,823
356
201
15
1,622
D
1622D
D. Shuffle
2,000
combinatorics; math; two pointers
You are given a binary string (i. e. a string consisting of characters 0 and/or 1) \(s\) of length \(n\). You can perform the following operation with the string \(s\) at most once: choose a substring (a contiguous subsequence) of \(s\) having exactly \(k\) characters 1 in it, and shuffle it (reorder the characters in ...
The first line contains two integers \(n\) and \(k\) (\(2 \le n \le 5000\); \(0 \le k \le n\)).The second line contains the string \(s\) of length \(n\), consisting of characters 0 and/or 1.
Print one integer β€” the number of different strings which can be obtained from \(s\) by performing the described operation at most once. Since the answer can be large, output it modulo \(998244353\).
Some strings you can obtain in the first example: to obtain 0110110, you can take the substring from the \(1\)-st character to the \(4\)-th character, which is 1100, and reorder its characters to get 0110; to obtain 1111000, you can take the substring from the \(3\)-rd character to the \(7\)-th character, which is 0011...
Input: 7 2 1100110 | Output: 16
Hard
3
464
190
199
16
1,698
C
1698C
C. 3SUM Closure
1,300
brute force; data structures
You are given an array \(a\) of length \(n\). The array is called 3SUM-closed if for all distinct indices \(i\), \(j\), \(k\), the sum \(a_i + a_j + a_k\) is an element of the array. More formally, \(a\) is 3SUM-closed if for all integers \(1 \leq i < j < k \leq n\), there exists some integer \(1 \leq l \leq n\) such t...
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\) (\(3 \leq n \leq 2 \cdot 10^5\)) β€” the length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \leq a_i \leq 10^...
For each test case, output ""YES"" (without quotes) if \(a\) is 3SUM-closed 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).
In the first test case, there is only one triple where \(i=1\), \(j=2\), \(k=3\). In this case, \(a_1 + a_2 + a_3 = 0\), which is an element of the array (\(a_2 = 0\)), so the array is 3SUM-closed.In the second test case, \(a_1 + a_4 + a_5 = -1\), which is not an element of the array. Therefore, the array is not 3SUM-c...
Input: 43-1 0 151 -2 -2 1 -360 0 0 0 0 04-1 2 -3 4 | Output: YES NO YES NO
Easy
2
384
447
254
16
1,762
F
1762F
F. Good Pairs
2,600
binary search; data structures; dp
You are given an array \(a\) consisting of \(n\) integers and an integer \(k\).A pair \((l,r)\) is good if there exists a sequence of indices \(i_1, i_2, \dots, i_m\) such that \(i_1=l\) and \(i_m=r\); \(i_j < i_{j+1}\) for all \(1 \leq j < m\); and \(|a_{i_j}-a_{i_{j+1}}| \leq k\) for all \(1 \leq j < m\). Find the nu...
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains two space-separated integers \(n\) and \(k\) (\(1 \leq n \leq 5 \cdot 10^5\); \(0 \leq k \leq 10...
For each test case, print the number of good pairs.
In the first test case, good pairs are \((1,1)\), \((1,2)\), \((1,3)\), \((2,2)\), \((2,3)\), and \((3,3)\).In the second test case, good pairs are \((1,1)\), \((1,3)\), \((1,4)\), \((2,2)\), \((2,3)\), \((2,4)\), \((3,3)\), \((3,4)\) and \((4,4)\). Pair \((1,4)\) is good because there exists a sequence of indices \(1,...
Input: 43 01 1 14 24 8 6 86 47 2 5 8 3 820 23110 57 98 14 20 1 60 82 108 37 82 73 8 46 38 35 106 115 58 112 | Output: 6 9 18 92
Expert
3
387
627
51
17
453
C
453C
C. Little Pony and Summer Sun Celebration
2,200
constructive algorithms; dfs and similar; graphs
Twilight Sparkle learnt that the evil Nightmare Moon would return during the upcoming Summer Sun Celebration after one thousand years of imprisonment on the moon. She tried to warn her mentor Princess Celestia, but the princess ignored her and sent her to Ponyville to check on the preparations for the celebration. Twil...
The first line contains two integers n and m (2 ≀ n ≀ 105; 0 ≀ m ≀ 105) β€” the number of places and the number of roads in Ponyville. Each of the following m lines contains two integers ui, vi (1 ≀ ui, vi ≀ n; ui β‰  vi), these integers describe a road between places ui and vi.The next line contains n integers: x1, x2, .....
Output the number of visited places k in the first line (0 ≀ k ≀ 4n). Then output k integers β€” the numbers of places in the order of path. If xi = 0, then the i-th place must appear in the path even number of times, else i-th place must appear in the path odd number of times. Note, that given road system has no self-lo...
Input: 3 21 22 31 1 1 | Output: 31 2 3
Hard
3
898
521
494
4
1,674
G
1674G
G. Remove Directed Edges
2,000
dfs and similar; dp; graphs
You are given a directed acyclic graph, consisting of \(n\) vertices and \(m\) edges. The vertices are numbered from \(1\) to \(n\). There are no multiple edges and self-loops.Let \(\mathit{in}_v\) be the number of incoming edges (indegree) and \(\mathit{out}_v\) be the number of outgoing edges (outdegree) of vertex \(...
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\); \(0 \le m \le 2 \cdot 10^5\)) β€” the number of vertices and the number of edges of the graph.Each of the next \(m\) lines contains two integers \(v\) and \(u\) (\(1 \le v, u \le n\); \(v \neq u\)) β€” the description of an edge.The given e...
Print a single integer β€” the maximum possible size of a cute set \(S\) after you remove some edges from the graph and both indegrees and outdegrees of all vertices either decrease or remain equal to \(0\).
In the first example, you can remove edges \((1, 2)\) and \((2, 3)\). \(\mathit{in} = [0, 1, 2]\), \(\mathit{out} = [2, 1, 0]\). \(\mathit{in'} = [0, 0, 1]\), \(\mathit{out'} = [1, 0, 0]\). You can see that for all \(v\) the conditions hold. The maximum cute set \(S\) is formed by vertices \(1\) and \(3\). They are sti...
Input: 3 3 1 2 2 3 1 3 | Output: 2
Hard
3
1,123
390
205
16
1,328
E
1328E
E. Tree Queries
1,900
dfs and similar; graphs; trees
You are given a rooted tree consisting of \(n\) vertices numbered from \(1\) to \(n\). The root of the tree is a vertex number \(1\).A tree is a connected undirected graph with \(n-1\) edges.You are given \(m\) queries. The \(i\)-th query consists of the set of \(k_i\) distinct vertices \(v_i[1], v_i[2], \dots, v_i[k_i...
The first line of the input contains two integers \(n\) and \(m\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le m \le 2 \cdot 10^5\)) β€” the number of vertices in the tree and the number of queries.Each of the next \(n-1\) lines describes an edge of the tree. Edge \(i\) is denoted by two integers \(u_i\) and \(v_i\), the label...
For each query, print the answer β€” ""YES"", if there is a path from the root to some vertex \(u\) such that each of the given \(k\) vertices is either belongs to this path or has the distance \(1\) to some vertex of this path and ""NO"" otherwise.
The picture corresponding to the example:Consider the queries.The first query is \([3, 8, 9, 10]\). The answer is ""YES"" as you can choose the path from the root \(1\) to the vertex \(u=10\). Then vertices \([3, 9, 10]\) belong to the path from \(1\) to \(10\) and the vertex \(8\) has distance \(1\) to the vertex \(7\...
Input: 10 6 1 2 1 3 1 4 2 5 2 6 3 7 7 8 7 9 9 10 4 3 8 9 10 3 2 4 6 3 2 1 5 3 4 8 2 2 6 10 3 5 4 7 | Output: YES YES YES YES NO NO
Hard
3
527
970
247
13
1,969
E
1969E
E. Unique Array
2,400
binary search; data structures; divide and conquer; dp; greedy
You are given an integer array \(a\) of length \(n\). A subarray of \(a\) is one of its contiguous subsequences (i. e. an array \([a_l, a_{l+1}, \dots, a_r]\) for some integers \(l\) and \(r\) such that \(1 \le l < r \le n\)). Let's call a subarray unique if there is an integer that occurs exactly once in the subarray....
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 3 \cdot 10^5\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)).Additional constraint on the input: the ...
For each test case, print a single integer β€” the minimum number of aforementioned operation in order for all the subarrays of the array \(a\) to be unique.
In the second test case, you can replace the \(1\)-st and the \(3\)-rd element, for example, like this: \([3, 4, 1, 4]\).In the third test case, you can replace the \(4\)-th element, for example, like this: \([3, 1, 2, 3, 2]\).
Input: 432 1 244 4 4 453 1 2 1 251 3 2 1 2 | Output: 0 2 1 0
Expert
5
596
385
155
19
1,872
A
1872A
A. Two Vessels
800
brute force; greedy; math
You have two vessels with water. The first vessel contains \(a\) grams of water, and the second vessel contains \(b\) grams of water. Both vessels are very large and can hold any amount of water.You also have an empty cup that can hold up to \(c\) grams of water.In one move, you can scoop up to \(c\) grams of water fro...
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.Each test case consists of a single line containing three integers \(a\), \(b\), and \(c\) (\(1 \le a, b, c \le 100\)) β€” the mass of water in the vessels and t...
For each test case, output a single number β€” the minimum number of moves required to make the masses of water in the vessels equal. It can be shown, that it is always possible.
In the first test case, only one move is enough: if we pour \(2\) grams of water from the second vessel into the first one, both vessels will contain \(5\) grams of water.In the second example test case, three moves are enough: Pour \(3\) grams of water from the first vessel into the second one. After this move, the fi...
Input: 63 7 217 4 317 17 117 21 1001 100 197 4 3 | Output: 1 3 0 1 50 16
Beginner
3
613
357
176
18
1,954
F
1954F
F. Unique Strings
3,100
combinatorics; dp; math
Let's say that two strings \(a\) and \(b\) are equal if you can get the string \(b\) by cyclically shifting string \(a\). For example, the strings 0100110 and 1100100 are equal, while 1010 and 1100 are not.You are given a binary string \(s\) of length \(n\). Its first \(c\) characters are 1-s, and its last \(n - c\) ch...
The first and only line contains three integers \(n\), \(c\) and \(k\) (\(1 \le n \le 3000\); \(1 \le c \le n\); \(0 \le k \le n - c\)) β€” the length of string \(s\), the length of prefix of 1-s and the maximum number of operations.
Print the single integer β€” the number of unique strings you can achieve performing no more than \(k\) operations, modulo \(10^9 + 7\).
In the first test case, the only possible string is 1.In the second test case, the possible strings are: 100, 110, and 111. String 101 is equal to 110, so we don't count it.In the third test case, the possible strings are: 10000, 11000, 10100. String 10010 is equal to 10100, and 10001 is equal to 11000.
Input: 1 1 0 | Output: 1
Master
3
536
231
134
19
868
B
868B
B. Race Against Time
1,400
implementation
Have you ever tried to explain to the coordinator, why it is eight hours to the contest and not a single problem has been prepared yet? Misha had. And this time he has a really strong excuse: he faced a space-time paradox! Space and time replaced each other.The entire universe turned into an enormous clock face with th...
Five integers h, m, s, t1, t2 (1 ≀ h ≀ 12, 0 ≀ m, s ≀ 59, 1 ≀ t1, t2 ≀ 12, t1 β‰  t2).Misha's position and the target time do not coincide with the position of any hand.
Print ""YES"" (quotes for clarity), if Misha can prepare the contest on time, and ""NO"" otherwise.You can print each character either upper- or lowercase (""YeS"" and ""yes"" are valid when the answer is ""YES"").
The three examples are shown on the pictures below from left to right. The starting position of Misha is shown with green, the ending position is shown with pink. Note that the positions of the hands on the pictures are not exact, but are close to the exact and the answer is the same.
Input: 12 30 45 3 11 | Output: NO
Easy
1
1,262
167
214
8
1,781
A
1781A
A. Parallel Projection
800
geometry; math
Vika's house has a room in a shape of a rectangular parallelepiped (also known as a rectangular cuboid). Its floor is a rectangle of size \(w \times d\), and the ceiling is right above at the constant height of \(h\). Let's introduce a coordinate system on the floor so that its corners are at points \((0, 0)\), \((w, 0...
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains three integers \(w\), \(d\), and \(h\) (\(2 \le w, d, h \le 1000\)) β€” the size of the room.The second line contains f...
For each test case, print a single integer β€” the minimum length of the cable connecting the laptop and the projector that runs only along the walls, floor, and ceiling parallel to cuboid's edges.
The picture in the statement illustrates the first test case.
Input: 555 20 2923 10 18 320 10 51 5 2 515 15 47 13 10 102 1000 21 1 1 99910 4 107 1 2 1 | Output: 47 8 14 1002 17
Beginner
2
948
529
195
17
1,236
F
1236F
F. Alice and the Cactus
3,000
dfs and similar; graphs; math; probabilities
Alice recently found some cactuses growing near her house! After several months, more and more cactuses appeared and soon they blocked the road. So Alice wants to clear them.A cactus is a connected undirected graph. No edge of this graph lies on more than one simple cycle. Let's call a sequence of different nodes of th...
The first line contains two integers \(n\) and \(m\), separated by space (\(1 \leq n \leq 5 \cdot 10^5, n - 1 \leq m \leq 5 \cdot 10^5\)) β€” the number of nodes and edges in the cactus.The following \(m\) lines contain two numbers \(u\) and \(v\) each, separated by space (\(1 \leq u, v \leq n, u \neq v\)) meaning that t...
Print one integer β€” the variance of the number of connected components in the remaining graph, after removing a set of nodes such that each node has probability \(\frac{1}{2}\) to be removed and all these events are independent. This value should be found by modulo \(10^9+7\).
In the first sample, the answer is \(\frac{7}{64}\). If all nodes are removed the value of \(X\) is equal to \(0\), otherwise, it is equal to \(1\). So, the expected value of \(X\) is equal to \(0\times\frac{1}{8}+1\times\frac{7}{8}=\frac{7}{8}\). So, the variance of \(X\) is equal to \((0 - \frac{7}{8})^2\times\frac{1...
Input: 3 3 1 2 2 3 1 3 | Output: 984375007
Master
4
1,895
473
277
12
1,025
E
1025E
E. Colored Cubes
2,700
constructive algorithms; implementation; matrices
Vasya passes all exams! Despite expectations, Vasya is not tired, moreover, he is ready for new challenges. However, he does not want to work too hard on difficult problems.Vasya remembered that he has a not-so-hard puzzle: \(m\) colored cubes are placed on a chessboard of size \(n \times n\). The fact is that \(m \leq...
The first line contains two integers \(n\) and \(m\) (\(1 \leq m \leq n \leq 50\)).Each of the next \(m\) lines contains two integers \(x_i\), \(y_i\) (\(1 \leq x_i, y_i \leq n\)), the initial positions of the cubes.The next \(m\) lines describe the designated places for the cubes in the same format and order. It is gu...
In the first line print a single integer \(k\) (\(0 \le k \leq 10800\)) β€” the number of operations Vasya should make.In each of the next \(k\) lines you should describe one operation: print four integers \(x_1\), \(y_1\), \(x_2\), \(y_2\), where \(x_1, y_1\) is the position of the cube Vasya should move, and \(x_2, y_2...
In the fourth example the printed sequence of movements (shown on the picture below) is valid, but not shortest. There is a solution in \(3\) operations.
Input: 2 11 12 2 | Output: 21 1 1 21 2 2 2
Master
3
991
496
593
10
455
B
455B
B. A Lot of Games
1,900
dfs and similar; dp; games; implementation; strings; trees
Andrew, Fedor and Alex are inventive guys. Now they invent the game with strings for two players.Given a group of n non-empty strings. During the game two players build the word together, initially the word is empty. The players move in turns. On his step player must add a single letter in the end of the word, the resu...
The first line contains two integers, n and k (1 ≀ n ≀ 105; 1 ≀ k ≀ 109).Each of the next n lines contains a single non-empty string from the given group. The total length of all strings from the group doesn't exceed 105. Each string of the group consists only of lowercase English letters.
If the player who moves first wins, print ""First"", otherwise print ""Second"" (without the quotes).
Input: 2 3ab | Output: First
Hard
6
772
290
101
4
1,717
C
1717C
C. Madoka and Formal Statement
1,300
greedy
Given an array of integer \(a_1, a_2, \ldots, a_n\). In one operation you can make \(a_i := a_i + 1\) if \(i < n\) and \(a_i \leq a_{i + 1}\), or \(i = n\) and \(a_i \leq a_1\).You need to check whether the array \(a_1, a_2, \ldots, a_n\) can become equal to the array \(b_1, b_2, \ldots, b_n\) in some number of operati...
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 4 \cdot 10^4\)) β€” the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) – the length of the array.The second ...
For each test case, output ""YES"" if you can get the array \(b\), otherwise output ""NO"".You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as positive answer).
In the first test case, the array \(a\) is already equal to the array \(b\).In the second test case, we can't get the array \(b\), because to do this we need to decrease \(a_1\).In the fifth test case, we can apply operations in order to the elements with indices \(4, 3, 3,2,2,2,1,1,1,1\), and then get the array \([5,5...
Input: 531 2 51 2 522 21 343 4 1 26 4 2 532 4 14 5 351 2 3 4 56 5 6 7 6 | Output: YES NO NO NO YES
Easy
1
462
676
221
17
1,935
B
1935B
B. Informatics in MAC
1,200
constructive algorithms
In the Master's Assistance Center, Nyam-Nyam was given a homework assignment in informatics.There is an array \(a\) of length \(n\), and you want to divide it into \(k > 1\) subsegments\(^{\dagger}\) in such a way that the \(\operatorname{MEX} ^{\ddagger}\) on each subsegment is equal to the same integer.Help Nyam-Nyam...
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 10^5\)) β€” the length of the array \(a\).The second line...
For each test case, output a single integer \(-1\) if a suitable division does not exist.Otherwise, on the first line, output an integer \(k\) (\(2 \le k \le n\)) β€” the number of subsegments in the division.Then output \(k\) lines β€” the division into subsegments. The \(i\)-th line should contain two integers \(l_i\) an...
In the first test case, the array \(a\) can be divided into \(2\) subsegments with boundaries \([1, 1]\) and \([2, 2]\): \(\operatorname{MEX}\) of the first subsegment \([0]\) is \(1\), as \(0\) belongs to the subsegment, but \(1\) does not. \(\operatorname{MEX}\) of the second subsegment \([0]\) is \(1\), as \(0\) bel...
Input: 520 050 1 2 3 480 1 7 1 0 1 0 332 2 240 1 2 0 | Output: 2 1 1 2 2 -1 3 1 3 4 5 6 8 3 1 1 2 2 3 3 -1
Easy
1
1,208
527
590
19