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 purchase in such a way that there's no change β the total worth of provided coins is exactly \(m\). He can use both regular and fancy coins. However, he wants to spend as little fancy coins as possible.What's the smallest total number of fancy coins he can use to make a purchase?
|
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 coin and the amounts of regular coins of both types, respectively.
|
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 \(11\) regular coins worth \(1\) burle, for example. Notice that Monocarp doesn't have to minimize the total number of used coins. That way he uses \(0\) fancy coins.In the third testcase, Monocarp can use \(5\) regular coins worth \(1\) burle and \(1\) regular coin worth \(3\) burles. That will get him to \(8\) total burles when he needs \(11\). So, \(1\) fancy coin worth \(3\) burles is enough.
|
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, Chouti was satisfied. He stood back and decided to find something fun with these bricks. After some counting, he found there are \(k\) bricks with a color different from the color of the brick on its left (the first brick is not counted, for sure).So as usual, he needs your help in counting how many ways could he paint the bricks. Two ways of painting bricks are different if there is at least one brick painted in different colors in these two ways. Because the answer might be quite big, you only need to output the number of ways modulo \(998\,244\,353\).
|
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 gift to or \(1 \le f_i \le n\) if the \(i\)-th friend wants to give the gift to the friend \(f_i\).You want to fill in the unknown values (\(f_i = 0\)) in such a way that each friend gives exactly one gift and receives exactly one gift and there is no friend who gives the gift to himself. It is guaranteed that the initial information isn't contradictory.If there are several answers, you can print any.
|
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 doesn't know whom he wants to give the gift to or \(1 \le f_i \le n\) if the \(i\)-th friend wants to give the gift to the friend \(f_i\). It is also guaranteed that there is at least two values \(f_i = 0\).
|
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 gift and there is no friend who gives the gift to himself.If there are several answers, you can print any.
|
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 token ended on the bonus, then you are allowed to make another turn with any other token. You can use different bonuses in any order. The same bonus can be used an unlimited number of times. Bonuses do not move during the game.There can be several tokens in one vertex at the same time, but initially there is no more than one token in each vertex.The vertex with number \(1\) is the finish vertex, and your task is to determine whether it is possible to hit it with any token by making turns with the tiles according to the rules described above. If a token is initially located at the vertex of \(1\), then the game is considered already won. The finish line is in black, the bonuses are in red, the chips are in grey. For example, for a given graph, you can reach the finish line with a chip from the \(8\)th vertex by making the following sequence of turns: Move from the \(8\)-th vertex to the \(6\)-th. Move from the \(7\)-th vertex to the \(5\)-th. Move from the \(6\)-th vertex to the \(4\)-th. Move from the \(5\)-th vertex to the \(6\)-th. Move from the \(4\)-th vertex to the \(2\)-nd. Move from the \(6\)-th vertex to the \(4\)-th. Move from the \(2\)-nd vertex to the \(1\)-st vertex, which is the finish.
|
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 number of vertices and edges in the graph, respectively.The second line of the description of each test case contains two integers \(p\) and \(b\) (\(1 \le p \le n, 0 \le b \le n\)) β the number of tokens and bonuses, respectively.The third line of the description of each test case contains \(p\) different integers from \(1\) to \(n\) β the indices of the vertices in which the tokens are located.The fourth line of the description of each input data set contains \(b\) different integers from \(1\) to \(n\) β the indices of the vertices in which the bonuses are located. Note that the value of \(b\) can be equal to \(0\). In this case, this line is empty.There can be both a token and a bonus in one vertex at the same time.The next \(m\) lines of the description of each test case contain two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\), \(u_i \ne v_i\)) β vertices connected by the \(i\)-th edge. There is at most one edge between each pair of vertices. The given graph is connected, that is, from any vertex you can get to any one by moving along the edges.The test cases are separated by an empty string.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\). Similarly, it is guaranteed that the sum of \(m\) over all input data sets does not exceed \(2 \cdot 10^5\).
|
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 sixth test case, the token is initially at node number \(1\), so we win immediately.
|
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 integers \(a\) and \(b\) are divisible by \(d\).For example: \(\text{\)gcdSum\(}(762) = gcd(762, 7 + 6 + 2)=gcd(762,15) = 3\).Given an integer \(n\), find the smallest integer \(x \ge n\) such that \(\text{\)gcdSum\(}(x) > 1\).
|
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(31, 3 + 1) = gcd(31,\ 4) = 1\).\(\text{\)gcdSum\(}(32) = gcd(32, 3 + 2) = gcd(32,\ 5) = 1\).\(\text{\)gcdSum\(}(33) = gcd(33, 3 + 3) = gcd(33,\ 6) = 3\).So the smallest number \(\ge 31\) whose \(gcdSum\) \(> 1\) is \(33\).Test case 3: \(\ n = 75\): \(\text{\)gcdSum\(}(75) = gcd(75, 7 + 5) = gcd(75,\ 12) = 3\).The \(\text{\)gcdSum\(}\) of \(75\) is already \(> 1\). Hence, it is the answer.
|
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 Vasya owes some money to this bank.There is only one type of operations available: transfer some amount of money from any bank to account in any neighbouring bank. There are no restrictions on the size of the sum being transferred or balance requirements to perform this operation.Vasya doesn't like to deal with large numbers, so he asks you to determine the minimum number of operations required to change the balance of each bank account to zero. It's guaranteed, that this is possible to achieve, that is, the total balance of Vasya in all banks is equal to zero.
|
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 bank; transfer 3 from the second bank to the third; transfer 6 from the third bank to the fourth.
|
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|\) \(+\) \(|b_1 - b_2| + |b_2 - b_3| + \dots + |b_{n-1} - b_n|\) (in other words, \(\sum\limits_{i=1}^{n - 1}{\left(|a_i - a_{i+1}| + |b_i - b_{i+1}|\right)}\)) you can achieve after performing several (possibly, zero) operations?
|
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, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the array \(a\).The third line of each test case contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le 10^9\)) β the array \(b\).
|
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 + |4 - 5| + |6 - 7| + \dots + |9 - 10|\) \(= 4 + 4 = 8\).In the third test case, we can, for example, swap \(a_5\) and \(b_5\).
|
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 \(k\)-th one β of \(k\) emotes, the \(k+1\)-th one β of \(k-1\) emotes, ..., and the last one β of one emote.For example, the emote triangle for \(k=3\) consists of \(5\) messages: Of course, most of the channels have auto moderation. Auto moderator of the current chat will ban you right after you spam at least \(x\) emotes in succession (you can assume you are the only user in the chat). Now you are interested β how many messages will you write before getting banned? Or maybe you will not get banned at all (i.e. will write all \(2k-1\) messages and complete your emote triangle successfully)? Note that if you get banned as a result of writing a message, this message is also counted.You have to answer \(t\) independent test cases.
|
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, and since \(1 + 2 + 3 + 4 \ge 7\), you get banned after that. In the third test case, you write one message containing exactly \(1\) emote. It doesn't get you banned, since \(1 < 2\), but you have already finished posting your emote triangle. So you wrote one message successfully. In the fourth test case, you write four messages containing \(1\), \(2\), \(3\) and \(2\) emotes respectively, and since \(1 + 2 + 3 + 2 \ge 7\), you get banned after that. In the fifth test case, you write three messages containing \(1\), \(2\) and \(1\) emote respectively. It doesn't get you banned, since \(1 + 2 + 1 < 5\), but you have already finished posting your emote triangle. So you wrote three messages successfully. In the sixth test case, since \(x = 1\), you get banned as soon as you send your first message. The seventh test case is too large to analyze, so we'll skip it.
|
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 indices \(i\), \(j\), and \(k\) (\(1 \leq i, j, k \leq n\)) and apply \(i \to j \to k \to i\) cycle to the array \(a\). It simultaneously places \(a_i\) on position \(j\), \(a_j\) on position \(k\), and \(a_k\) on position \(i\), without changing any other element.For example, if \(a\) is \([10, 50, 20, 30, 40, 60]\) and he chooses \(i = 2\), \(j = 1\), \(k = 5\), then the array becomes \([\underline{50}, \underline{40}, 20, 30, \underline{10}, 60]\).Petya can apply arbitrary number of \(3\)-cycles (possibly, zero). You are to determine if Petya can sort his array \(a\), i. e. make it non-decreasing.
|
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 test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
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 obviously for Sharic who loves hunting and fish is for Matroskin, but for whom was a new video game console meant? Every one of the three friends claimed that the present is for him and nearly quarreled. Uncle Fyodor had an idea how to solve the problem justly: they should suppose that the console was sent to all three of them and play it in turns. Everybody got relieved but then yet another burning problem popped up β who will play first? This time Matroskin came up with a brilliant solution, suggesting the most fair way to find it out: play rock-paper-scissors together. The rules of the game are very simple. On the count of three every player shows a combination with his hand (or paw). The combination corresponds to one of three things: a rock, scissors or paper. Some of the gestures win over some other ones according to well-known rules: the rock breaks the scissors, the scissors cut the paper, and the paper gets wrapped over the stone. Usually there are two players. Yet there are three friends, thatβs why they decided to choose the winner like that: If someone shows the gesture that wins over the other two players, then that player wins. Otherwise, another game round is required. Write a program that will determine the winner by the gestures they have shown.
|
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 smallest. If two cubes show the same value, they can go in any order.After sorting, Dmitry removed the first \(k\) cubes. Then he became interested in whether he removed his favorite cube (note that its position could have changed after sorting).For example, if \(n=5\), \(f=2\), \(a = [4, \color{green}3, 3, 2, 3]\) (the favorite cube is highlighted in green), and \(k = 2\), the following could have happened: After sorting \(a=[4, \color{green}3, 3, 3, 2]\), since the favorite cube ended up in the second position, it will be removed. After sorting \(a=[4, 3, \color{green}3, 3, 2]\), since the favorite cube ended up in the third position, it will not be removed.
|
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 cube, and the number of removed cubes, respectively.The second line of each test case description contains \(n\) integers \(a_i\) (\(1 \le a_i \le 100\)) β the values shown on the cubes.
|
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 first and the second line respectively (coordinates can be negative): the point \(A\) is responsible for the position of the laser, and the point \(B\) β for the direction of the laser ray. The laser ray is a ray starting at \(A\) and directed at \(B\) which will reflect from the sides of the tube (it doesn't matter if there are any sensors at a reflection point or not). A sensor will only register the ray if the ray hits exactly at the position of the sensor. Examples of laser rays. Note that image contains two examples. The \(3\) sensors (denoted by black bold points on the tube sides) will register the blue ray but only \(2\) will register the red. Calculate the maximum number of sensors which can register your ray if you choose points \(A\) and \(B\) on the first and the second lines respectively.
|
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 ascending order.The third line contains two integers \(m\) and \(y_2\) (\(1 \le m \le 10^5\), \(y_1 < y_2 \le 10^9\)) β number of sensors on the second line and its \(y\) coordinate. The fourth line contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(0 \le b_i \le 10^9\)) β \(x\) coordinates of the sensors on the second line in the ascending order.
|
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 is the maximum score you can acquire after \(k\) operations?
|
The first line contains \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains \(n\) and \(k\) (\(1 \leq n \leq 2 \cdot 10^5, 1 \leq k \leq 10^9\)) β the length of the arrays and the number of operations you can perform.The following line contains \(n\) integers \(a_1, a_2, ... a_n\) (\(1 \leq a_i \leq 10^9\)).The following line contains \(n\) integers \(b_1, b_2, ... b_n\) (\(1 \leq b_i \leq 10^9\)).It is guaranteed that the sum of \(n\) for all test cases does not exceed \(2 \cdot 10^5\).
|
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 company got hold of the records of a part of an online meeting of one successful team. The director watched the record and wanted to talk to the team leader. But how can he tell who the leader is? The director logically supposed that the leader is the person who is present at any conversation during a chat meeting. In other words, if at some moment of time at least one person is present on the meeting, then the leader is present on the meeting.You are the assistant director. Given the 'user logged on'/'user logged off' messages of the meeting in the chronological order, help the director determine who can be the leader. Note that the director has the record of only a continuous part of the meeting (probably, it's not the whole meeting).
|
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 with number id (1 β€ id β€ n) has logged off from the meeting. Assume that all the people of the team are numbered from 1 to n and the messages are given in the chronological order. It is guaranteed that the given sequence is the correct record of a continuous part of the meeting. It is guaranteed that no two log on/log off events occurred simultaneously.
|
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 Misha has already chosen the subject for his thesis: determining by two dwarven genomes, whether they belong to the same race. Two dwarves belong to the same race if we can swap two characters in the first dwarf's genome and get the second dwarf's genome as a result. Help Dwarf Misha and find out whether two gnomes belong to the same race or not.
|
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 correspond to the genomes are different. The given genomes may have different length.
|
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 is a segment of the x-axis. No two slices of cheese overlap, but their sides can touch. They form one connected shape. Note that we can arrange them in any order (the leftmost slice of cheese is not necessarily the first slice of cheese). Also note that we can rotate each slice of cheese in any way as long as all conditions still hold.Find the minimum possible perimeter of the constructed shape.
|
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 of cheese Pak Chanek has.The \(i\)-th of the next \(n\) lines of each test case contains two integers \(a_i\) and \(b_i\) (\(1 \leq a_i,b_i \leq 10^9\)) β the dimensions of the \(i\)-th slice of cheese.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a line 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 though the perimeter of the shape above is \(24\), it does not satisfy all conditions of the problem. The bottom edge of the \(1 \times 1\) slice of cheese is not a segment of the x-axis.In the second 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+2+2+3+2+3+2+2+2+4=24\). It can be shown that we cannot get a smaller perimeter.
|
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\) is positive, and \(A_1<A_2<\ldots<A_N\)) \(Y=C_1^{D_1}\times C_2^{D_2}\times\ldots\times C_M^{D_M}\) (each \(C_j\) is prime, each \(D_j\) is positive, and \(C_1<C_2<\ldots<C_M\)) Chaneka ponders about these two integers for too long throughout the trip, so Chaneka's friend commands her ""Gece, deh!"" (move fast) in order to not be late for the exam.Because of that command, Chaneka comes up with a problem, how many pairs of positive integers \(p\) and \(q\) such that \(\text{LCM}(p, q) = X\) and \(\text{GCD}(p, q) = Y\). Since the answer can be very big, output the answer modulo \(998\,244\,353\).Notes: \(\text{LCM}(p, q)\) is the smallest positive integer that is simultaneously divisible by \(p\) and \(q\). \(\text{GCD}(p, q)\) is the biggest positive integer that simultaneously divides \(p\) and \(q\).
|
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 factorisation of \(X\).The third line contains \(N\) integers \(B_1, B_2, B_3, \ldots, B_N\) (\(1 \leq B_i \leq 10^5\)) β the exponents in the prime factorisation of \(X\).The fourth line contains a single integer \(M\) (\(1 \leq M \leq 10^5\)) β the number of distinct primes in the prime factorisation of \(Y\).The fifth line contains \(M\) integers \(C_1, C_2, C_3, \ldots, C_M\) (\(2 \leq C_1 < C_2 < \ldots < C_M \leq 2 \cdot 10^6\); each \(C_j\) is prime) β the primes in the prime factorisation of \(Y\).The sixth line contains \(M\) integers \(D_1, D_2, D_3, \ldots, D_M\) (\(1 \leq D_j \leq 10^5\)) β the exponents in the prime factorisation of \(Y\).
|
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=2940\), \(q=21\) In the third example, the integers are as follows: \(X=2^1\times5^1=10\) \(Y=2^1\times3^1=6\) There is no pair \(p\) and \(q\) that simultaneously satisfies \(\text{LCM}(p,q)=10\) and \(\text{GCD}(p,q)=6\).
|
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 round down).You win if you guess the current number with no more than \(10\) queries.
|
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\), \(\lfloor\frac{x}{n}\rfloor = 1\).
|
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 rests, and he slides down \(b\) meters. If on the \(n\)-th day, the snail reaches position \(h\) for the first time (that is, the top of the tree), he will finish climbing, and we say that the snail spends \(n\) days climbing the tree. Note that on the last day of climbing, the snail doesn't necessarily climb up \(a\) meters, in case his distance to the top is smaller than \(a\).Unfortunately, you don't know the exact tree height \(h\) at first, but you know that \(h\) is a positive integer. There are \(q\) events of two kinds. Event of type \(1\): a snail with attributes \(a\), \(b\) comes and claims that he spent \(n\) days climbing the tree. If this message contradicts previously adopted information (i. e. there is no tree for which all previously adopted statements and this one are true), ignore it. Otherwise, adopt it. Event of type \(2\): a snail with attributes \(a\), \(b\) comes and asks you how many days he will spend if he climbs the tree. You can only give the answer based on the information you have adopted so far. If you cannot determine the answer precisely, report that. You need to deal with all the events in order.
|
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 first integer of each line is either \(1\) or \(2\), denoting the event type.If the event type is \(1\), then three integers \(a\), \(b\), and \(n\) (\(1\le a,b,n \le 10^9\), \(a>b\)) follow.If the event type is \(2\), then two integers \(a\) and \(b\) (\(1\le a,b \le 10^9\), \(a>b\)) follow.It is guaranteed that the sum of \(q\) over all test cases does not exceed \(2\cdot 10^5\).
|
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 determine it, output \(-1\).
|
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\). During the night of the \(1\)st day: slides down \(1\) meters, now at position \(3\). During the daylight hours of the \(2\)nd day: climbs up \(4\) meters, now at position \(7\) (reaches the top). In the third test case, the second snail's message contradicts the first snail's, because the second snail says he spent \(3\) days, and he can climb at most \(1+1+2=4\) meters in the first \(3\) days. However, the first snail only needs \(1\) day to climb \(4\) meters.
|
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\) to \(c + a_i\). Option \(2\): set \(c\) to \(|c + a_i|\), where \(|x|\) is the absolute value of \(x\). Let the maximum final value of \(c\) after the procedure described above be equal to \(k\). Find \(k\).
|
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 10^9\)).The sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
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 case, it is optimal to wait until the end to set \(c\) to its absolute value, resulting in an answer of \(6\).
|
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 to use an odd number of streets that together form a cycle. That is, for a junction \(S\) and an even positive integer \(k\), there is a sequence of junctions \(S, s_1, \ldots, s_k, S\) such that there are streets connecting (a) \(S\) and \(s_1\), (b) \(s_k\) and \(S\), and (c) \(s_{i-1}\) and \(s_i\) for each \(i = 2, \ldots, k\).However, the police are controlling the streets of Gotham City. On each day \(i\), they monitor a different subset of all streets with consecutive numbers \(j\): \(l_i \leq j \leq r_i\). These monitored streets cannot be a part of Joker's plan, of course. Unfortunately for the police, Joker has spies within the Gotham City Police Department; they tell him which streets are monitored on which day. Now Joker wants to find out, for some given number of days, whether he can execute his evil plan. On such a day there must be a cycle of streets, consisting of an odd number of streets which are not monitored on that day.
|
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 numbers \(u\) and \(v\) (\(u \neq v\)), saying that street \(j\) connects these two junctions. It is guaranteed that any two junctions are connected by at most one street. The following \(Q\) lines contain two integers \(l_i\) and \(r_i\), saying that all streets \(j\) with \(l_i \leq j \leq r_i\) are checked by the police on day \(i\) (\(1 \leq i \leq Q\)).
|
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. For example, one possible Latin square is shown below. $$$\(\begin{bmatrix} \texttt{A} & \texttt{B} & \texttt{C} \\ \texttt{C} & \texttt{A} & \texttt{B} \\ \texttt{B} & \texttt{C} & \texttt{A} \\ \end{bmatrix}\)\(You are given a Latin square, but one of the letters was replaced with a question mark \)\texttt{?}$$$. Find the letter that was replaced.
|
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 is a Latin square with exactly one of the letters replaced with a question mark \(\texttt{?}\).
|
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} & \color{red}{\texttt{B}} \\ \texttt{A} & \texttt{B} & \texttt{C} \\ \end{bmatrix} \quad \begin{bmatrix} \color{red}{\texttt{C}} & \texttt{A} & \texttt{B} \\ \texttt{B} & \texttt{C} & \texttt{A} \\ \texttt{A} & \texttt{B} & \texttt{C} \\ \end{bmatrix}\)$$$
|
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 bidirectional paths from some craters to others that are safe for ""Moonwalking"". Each of those direct paths is colored in one color and there is unique path between each two craters. Goal of the game is to find two craters such that given array of colors appears most times as continuous subarray on the path between those two craters (overlapping appearances should be counted).To help your favorite team win, you should make a program that, given the map, answers the queries of the following type: For two craters and array of colors answer how many times given array appears as continuous subarray on the path from the first crater to the second.Colors are represented as lowercase English alphabet letters.
|
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\).Next line contains integer \(Q\) \((1 \leq Q \leq 10^5)\) β number of queries.Next \(Q\) lines contain three values \(u, v\) \((1 \leq u, v \leq N)\) and \(S\) \((|S| \leq 100)\), where \(u\) and \(v\) are the two cratersfor which you should find how many times array of colors \(S\) (represented as string) appears on the path from \(u\) to \(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)\) : \(y\), or \(x\) : \((y + 1)\).While watching a soccer game, Dima was distracted by very important matters, and after some time, he returned to watching the game. Dima remembers the score right before he was distracted, and the score right after he returned. Given these two scores, he wonders the following question. Is it possible that, while Dima was not watching the game, the teams never had an equal score?It is guaranteed that at neither of the two time points Dima remembers the teams had equal scores. However, it is possible that the score did not change during his absence.Help Dima and answer the question!
|
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 score before Dima was distracted.The second line of each test case contains two integers \(x_{2}, y_{2}\) (\(x_{1} \le x_{2} \le 10^{9}\), \(y_{1} \le y_{2} \le 10^{9}\), \(x_{2} \neq y_{2}\)) β the score when Dima returned.
|
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, there is a moment when the teams have an equal score, so the answer is NO.In the third test case, one of the possible developments is: \(1\) : \(2\) \(1\) : \(3\) \(2\) : \(3\) \(2\) : \(4\) \(2\) : \(5\) \(3\) : \(5\) \(4\) : \(5\) In this scenario, there was no time when the score was equal, so the answer is YES.
|
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: from top to bottom, from left to right. First, the numbers in the first column was written (from top to bottom) and so on.Print the k-th number on the paper.
|
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 p is an odd prime number, the functional equation states that for some function . (This equation should hold for any integer x in the range 0 to p - 1, inclusive.)It turns out that f can actually be many different functions. Instead of finding a solution, Kevin wants you to count the number of distinct functions f that satisfy this equation. Since the answer may be very large, you should print your result modulo 109 + 7.
|
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 distinct vertices (in one out of two possible directions).
|
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 from v to u, and av, u = 1 if there is one. As the output graph has to be a tournament, following equalities must be satisfied: av, u + au, v = 1 for each v, u (1 β€ v, u β€ n; v β u); av, v = 0 for each v (1 β€ v β€ n).
|
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, b)\) denotes the greatest common divisor of \(a\) and \(b\), and \(\operatorname{lcm}(c, d)\) denotes the least common multiple of \(c\) and \(d\).
|
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) = \operatorname{lcm}(2, 1) = 2\), \(2 + 4 + 2 + 1 = 9\).In the fifth test case \(\gcd(3, 5) = \operatorname{lcm}(1, 1) = 1\), \(3 + 5 + 1 + 1 = 10\).
|
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 same vertex twice and doesn't contain the same edge twice.Find any simple cycle in this graph in which the weight of the lightest edge is minimal.
|
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 and the number of edges.The next \(m\) lines of the test case contain three integers \(u\), \(v\), and \(w\) (\(1 \le u, v \le n\), \(u \ne v\), \(1 \le w \le 10^6\)) β vertices \(u\) and \(v\) are connected by an edge of weight \(w\).It is guaranteed that there is at most one edge between each pair of vertices. Note that under the given constraints, there is always at least one simple cycle in the graph.It is guaranteed that the sum of the values of \(m\) for all test cases does not exceed \(2 \cdot 10^5\).
|
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, as under the given constraints, there is always at least one simple cycle in the graph.
|
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 in hands, while all the other vertices move down so that the tree becomes rooted at the chosen vertex. After that Timofey brings the tree to a trash can.Timofey doesn't like it when many colors are mixing together. A subtree annoys him if there are vertices of different color in it. Timofey wants to find a vertex which he should take in hands so that there are no subtrees that annoy him. He doesn't consider the whole tree as a subtree since he can't see the color of the root vertex.A subtree of some vertex is a subgraph containing that vertex and all its descendants.Your task is to determine if there is a vertex, taking which in hands Timofey wouldn't be annoyed.
|
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, ..., cn (1 β€ ci β€ 105), denoting the colors of the vertices.
|
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 game starts, \(M\) different cells (numbered from \(1\) to \(M\)) are chosen: at cell \((R_i, C_i)\), you add \(A_i\) stones on it. You then give Anda and Kamu an integer \(K\) and commence the game.Anda and Kamu will take turns alternately with Anda taking the first turn. A player on their turn will do the following. Choose a cell \((r, c)\) with at least one stone on it. Remove at least one but at most \(K\) stones from the chosen cell. For each cell \((x, y)\) such that \(r + 1 \leq x \leq \min(N, r + K)\) and \(c \leq y \leq c + x - r\), add zero or more stones but at most \(K\) stones to cell \((x, y)\). The following illustrations show all the possible cells in which you can add stones for \(K = 3\). You choose the cell \((2, 1)\) for the left illustration and the cell \((4, 3)\) for the right illustration. A player who is unable to complete their turn (because there are no more stones on the grid) will lose the game, and the opposing player wins. Determine who will win the game if both players play optimally.
|
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\) lines consists of three integers \(R_i\) \(C_i\) \(A_i\) (\(1 \leq C_i \leq R_i \leq N; 1 \leq A_1 \leq 10^9\)). The pairs \((R_i, C_i)\) are distinct.The sum of \(M\) across all test cases does not exceed \(200\,000\).
|
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 are removed by Kamu, Anda can remove all the remaining stones at \((2, 1)\) and win the game.For the second case, Kamu can always mirror whatever move made by Anda until Anda can no longer complete their turn.
|
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 driver. Its functional part consists of two rails with sliding endpoints of a piece of stretching rope. For the sake of simplicity we can consider this as a pair of parallel segments in a plane with the rope segment, whose endpoints we are free to choose as any points on these rails segments. The algorithmic part of the mechanism detects each particular raindrop and predicts when and where it reaches the plane. At this exact moment the rope segment must contain the raindrop point (so the rope adsorbs the raindrop).You are given the initial position of the rope endpoints and all information about raindrops. You are to choose the minimal possible speed \(v\) of the endpoints sliding (both endpoints can slide in any direction along their segments independently of each other) in such a way that it is possible to catch all raindrops moving both endpoints with speed not greater than \(v\), or find out that it's impossible no matter how high the speed is.
|
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 \(e_2\), meaning that the initial (that is, at the moment \(t = 0\)) positions of the endpoints are \((e_1, 0)\) and \((e_2, h)\) (\(0\le e_1, e_2\le w\)).The \(i\)-th of the following \(n\) lines contains three integers \(t_i\), \(x_i\) and \(y_i\) (\(1\le t_i\le 10^5\), \(0\le x_i \le w\), \(0 < y_i < h\)) meaning that the \(i\)-th raindrop touches the plane at the point \((x_i, y_i)\) at the time moment \(t_i\). It is guaranteed that \(t_i \le t_{i+1}\) for all valid \(i\).
|
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 answer be \(b\). Your answer is considered correct if \(\frac{|a - b|}{\max{(1, |b|)}} \le 10^{-4}\).
|
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 to another. Now Gerasim wants to check his hypothesis. The good thing is that chef Gerasim always pour the same number of milliliters of juice to all cups in the royal kitchen. Having thoroughly measured the juice in each cup, Gerasim asked you to write a program that will determine from which cup juice was poured to which one; otherwise, the program should determine that this time the pages set the table diligently.To simplify your task we shall consider the cups to be bottomless so that the juice never overfills a cup and pours out, however much it can be. Besides, by some strange reason in a far away kingdom one can only pour to a cup or from one cup to another an integer number of milliliters of juice.
|
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 the juice was poured (the cups are numbered with consecutive positive integers starting from one in the order in which the cups are described in the input data), b represents the number of the cup into which the juice was poured. Finally, if the given juice's volumes cannot be obtained using no more than one pouring (for example, the pages poured the juice from one cup to another more than once or the royal kitchen maids poured the juice into the cups incorrectly), print ""Unrecoverable configuration."" (without the quotes).
|
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 prefix of the next column (and there are no icons outside this figure). In other words, some amount of first columns will be filled with icons and, possibly, some amount of first cells of the next (after the last full column) column will be also filled with icons (and all the icons on the desktop belong to this figure). This is pretty much the same as the real life icons arrangement.In one move, you can take one icon and move it to any empty cell in the desktop.Ivan loves to add some icons to his desktop and remove them from it, so he is asking you to answer \(q\) queries: what is the minimum number of moves required to make the desktop good after adding/removing one icon?Note that queries are permanent and change the state of the desktop.
|
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 them contains \(m\) characters '.' and '*' β the description of the \(i\)-th row of the desktop.The next \(q\) lines describe queries. The \(i\)-th of them contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i \le n; 1 \le y_i \le m\)) β the position of the cell which changes its state (if this cell contained the icon before, then this icon is removed, otherwise an icon appears in this cell).
|
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 Berlyatov. The \(i\)-th route is going from the district \(a_i\) to the district \(b_i\). There is one courier on each route and the courier will always choose the cheapest (minimum by total cost) path from the district \(a_i\) to the district \(b_i\) to deliver products.The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).You can make at most one road to have cost zero (i.e. you choose at most one road and change its cost with \(0\)).Let \(d(x, y)\) be the cheapest cost of travel between districts \(x\) and \(y\).Your task is to find the minimum total courier routes cost you can achieve, if you optimally select the some road and change its cost with \(0\). In other words, you have to find the minimum possible value of \(\sum\limits_{i = 1}^{k} d(a_i, b_i)\) after applying the operation described above optimally.
|
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 three integers \(x_i\), \(y_i\) and \(w_i\) (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\); \(1 \le w_i \le 1000\)), where \(x_i\) and \(y_i\) are districts the \(i\)-th road connects and \(w_i\) is its cost. It is guaranteed that there is some path between each pair of districts, so the city is connected. It is also guaranteed that there is at most one road between each pair of districts.The next \(k\) lines describe courier routes. The \(i\)-th route is given as two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le n\)) β the districts of the \(i\)-th route. The route can go from the district to itself, some couriers routes can coincide (and you have to count them independently).
|
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 graph are integers and do not exceed \(S\). The graph has exactly one minimum spanning tree. The minimum spanning tree of the graph is the given tree. Two graphs are considered different if their sets of edges are different, taking into account the weights of the edges.The answer can be large, output it modulo \(998244353\).
|
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 \(i\)-th line contains three integers \(u_i\), \(v_i\), and \(w_i\) (\(1\le u_i,v_i\le n, u_i \ne v_i, 1\le w_i\le S\)) β an edge in the tree with weight \(w_i\).It is guaranteed that the sum of \(n\) for all tests does not exceed \(2\cdot 10^5\).
|
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 coloring suitable if it is beautiful and there is no rectangle of the single color, consisting of at least \(k\) tiles.Your task is to count the number of suitable colorings of the board of the given size.Since the answer can be very large, print it modulo \(998244353\).
|
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 of beautiful colorings of a board of size \(2 \times 2\) are the following:
|
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 than 2. For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2).Your task is to check whether Limak can choose three balls that satisfy conditions above.
|
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: Choose balls with sizes 3, 4 and 5. Choose balls with sizes 972, 970, 971.
|
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 Elsie, and it came in the form of a regular polygon with \(n\) sides. The vertices of the cake are numbered from \(1\) to \(n\) clockwise. You and Bessie are going to choose some of those vertices to cut non-intersecting diagonals into the cake. In other words, the endpoints of the diagonals must be part of the chosen vertices.Bessie would only like to give out pieces of cake which result in a triangle to keep consistency. The size of the pieces doesn't matter, and the whole cake does not have to be separated into all triangles (other shapes are allowed in the cake, but those will not be counted). Bessie has already chosen \(x\) of those vertices that can be used to form diagonals. She wants you to choose no more than \(y\) other vertices such that the number of triangular pieces of cake she can give out is maximized.What is the maximum number of triangular pieces of cake Bessie can give out?
|
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 Bessie has chosen, and the maximum number of other vertices you can choose.The second line consists of \(x\) distinct integers from \(1\) to \(n\), representing the vertices Bessie has chosen.It is guaranteed the sum of \(x\) over all test cases does not exceed \(2 \cdot 10^5\).
|
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 represent triangles that are counted.
|
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 array \(a\) of length \(k\) is the array \([a_1, a_2, \dots, a_k]\) (\(0 \le k \le n\)).The array \(b\) of length \(m\) is called good, if you can obtain a non-decreasing array \(c\) (\(c_1 \le c_2 \le \dots \le c_{m}\)) from it, repeating the following operation \(m\) times (initially, \(c\) is empty): select either the first or the last element of \(b\), remove it from \(b\), and append it to the end of the array \(c\). For example, if we do \(4\) operations: take \(b_1\), then \(b_{m}\), then \(b_{m-1}\) and at last \(b_2\), then \(b\) becomes \([b_3, b_4, \dots, b_{m-3}]\) and \(c =[b_1, b_{m}, b_{m-1}, b_2]\).Consider the following example: \(b = [1, 2, 3, 4, 4, 2, 1]\). This array is good because we can obtain non-decreasing array \(c\) from it by the following sequence of operations: take the first element of \(b\), so \(b = [2, 3, 4, 4, 2, 1]\), \(c = [1]\); take the last element of \(b\), so \(b = [2, 3, 4, 4, 2]\), \(c = [1, 1]\); take the last element of \(b\), so \(b = [2, 3, 4, 4]\), \(c = [1, 1, 2]\); take the first element of \(b\), so \(b = [3, 4, 4]\), \(c = [1, 1, 2, 2]\); take the first element of \(b\), so \(b = [4, 4]\), \(c = [1, 1, 2, 2, 3]\); take the last element of \(b\), so \(b = [4]\), \(c = [1, 1, 2, 2, 3, 4]\); take the only element of \(b\), so \(b = []\), \(c = [1, 1, 2, 2, 3, 4, 4]\) β \(c\) is non-decreasing. Note that the array consisting of one element is good.Print the length of the shortest prefix of \(a\) to delete (erase), to make \(a\) to be a good array. Note that the required length can be \(0\).You have to answer \(t\) independent test cases.
|
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, a_2, \dots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)), where \(a_i\) is the \(i\)-th element of \(a\).It is guaranteed that the sum of \(n\) does not exceed \(2 \cdot 10^5\) (\(\sum n \le 2 \cdot 10^5\)).
|
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 fewer number of first elements, the result will not be good.
|
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 \le 10^9\)). It is guaranteed that the points are distinct.It is guaranteed that the sum of values \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
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 decided to use the protective force field generating system to protect the colony against possible trouble.The system works as follows: the surface contains a number of generators of the field (they can also be considered as points). The active range of each generator is a circle of radius r centered at the location of the generator (the boundary of the circle is also included in the range). After the system is activated, it stretches the protective force field only over the part of the surface, which is within the area of all generators' activity. That is, the protected part is the intersection of the generators' active ranges.The number of generators available to the colonists is not limited, but the system of field generation consumes a lot of energy. More precisely, the energy consumption does not depend on the number of generators, but it is directly proportional to the area, which is protected by the field. Also, it is necessary that all the existing buildings are located within the protected area.Determine the smallest possible area of the protected part of the surface containing all the buildings.
|
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 yi (|xi|, |yi| β€ 50000) β coordinates of the i-th building. It is guaranteed that no two buildings are located at the same point, and no two different buildings are located closer than 1.It is guaranteed that there exists a circle with radius r that contains all the buildings.
|
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 sample is shown on the picture below.
|
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 has 20 triangular faces. All five kinds of polyhedrons are shown on the picture below: Anton has a collection of n polyhedrons. One day he decided to know, how many faces his polyhedrons have in total. Help Anton and find this number!
|
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-th polyhedron in Anton's collection is a tetrahedron. ""Cube"" (without quotes), if the i-th polyhedron in Anton's collection is a cube. ""Octahedron"" (without quotes), if the i-th polyhedron in Anton's collection is an octahedron. ""Dodecahedron"" (without quotes), if the i-th polyhedron in Anton's collection is a dodecahedron. ""Icosahedron"" (without quotes), if the i-th polyhedron in Anton's collection is an icosahedron.
|
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 guitar produces a note, let's denote it as aij. We know that Dima's guitar can produce k distinct notes. It is possible that some notes can be produced in multiple ways. In other words, it is possible that aij = apq at (i, j) β (p, q).Dima has already written a song β a sequence of s notes. In order to play the song, you need to consecutively produce the notes from the song on the guitar. You can produce each note in any available way. Dima understood that there are many ways to play a song and he wants to play it so as to make the song look as complicated as possible (try to act like Cobein).We'll represent a way to play a song as a sequence of pairs (xi, yi) (1 β€ i β€ s), such that the xi-th string on the yi-th fret produces the i-th note from the song. The complexity of moving between pairs (x1, y1) and (x2, y2) equals + . The complexity of a way to play a song is the maximum of complexities of moving between adjacent pairs.Help Dima determine the maximum complexity of the way to play his song! The guy's gotta look cool!
|
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 the input contains s integers qi (1 β€ qi β€ k) β the sequence of notes of the song.
|
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 1-ancestor, and a is a (k - 1)-ancestor of b's 1-ancestor. Family relationships don't form cycles in the found tree. In other words, there is no person who is his own ancestor, directly or indirectly (that is, who is an x-ancestor for himself, for some x, x > 0).Let's call two people x and y (x β y) p-th cousins (p > 0), if there is person z, who is a p-ancestor of x and a p-ancestor of y.Polycarpus wonders how many counsins and what kinds of them everybody has. He took a piece of paper and wrote m pairs of integers vi, pi. Help him to calculate the number of pi-th cousins that person vi has, for each pair vi, pi.
|
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 third line contains a single number m (1 β€ m β€ 105) β the number of family relationship queries Polycarus has. Next m lines contain pairs of space-separated integers. The i-th line contains numbers vi, pi (1 β€ vi, pi β€ n).
|
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 a position without blocks.All subsets that you choose should be different (unique).You need to remove all blocks in the array using at most \(n+1\) operations. It can be proved that the answer always exists.
|
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 binary strings should be distinct (unique) and \(a_i\) should be equal to the sum of \(s_i\) among all chosen binary strings.If there are multiple possible answers, you can print any.It can be proved that an answer always exists.
|
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 from others.
|
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 the list (one command can be changed several times). How far from the starting point can the turtle move after it follows all the commands of the modified list?
|
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 between the most loaded server and the least loaded server as small as possible. In other words you want to minimize expression ma - mb, where a is the most loaded server and b is the least loaded one.In one second you can reassign a single task. Thus in one second you can choose any pair of servers and move a single task from one server to another.Write a program to find the minimum number of seconds needed to balance the load of servers.
|
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 a task from server #4 to server #1 (the sequence m becomes: 2 2 3 3 5); then move task from server #5 to server #1 (the sequence m becomes: 3 2 3 3 4); then move task from server #5 to server #2 (the sequence m becomes: 3 3 3 3 3). The above sequence is one of several possible ways to balance the load of servers in three seconds.
|
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 operation, you can do one of the following: decrease the number on the clock by \(1\), or swap two digits (you can choose which digits to swap, and they don't have to be adjacent). Your task is to determine the minimum number of operations needed to make the clock show \(0\).
|
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, \ldots, s_n\) (\(0 \le s_1, s_2, \ldots, s_n \le 9\)) β the number on the clock.Note: If the number is smaller than \(10^{n-1}\) the clock will show leading zeroes.
|
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 hexagon edge should pass through any of the triangles).You are allowed to add straight lines parallel to the edges of the hexagons. Given \(n\), what is the minimum number of lines you need to add to create at least \(n\) equilateral triangles as described? Adding two red lines results in two new yellow equilateral triangles.
|
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 parallel to one of the coordinate axes, which connects the edges of the bar. More formally, each break goes along the line x = xc or y = yc, where xc and yc are integers. It should divide one part of the bar into two non-empty parts. After Bob breaks some part into two parts, he breaks the resulting parts separately and independently from each other. Also he doesn't move the parts of the bar. Bob made n breaks and wrote them down in his notebook in arbitrary order. At the end he got n + 1 parts. Now he wants to calculate their areas. Bob is lazy, so he asks you to do this task.
|
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, or yi, 1 = yi, 2). Breaks are given in arbitrary order.It is guaranteed that the set of breaks is correct, i.e. there is some order of the given breaks that each next break divides exactly one part of the bar into two non-empty parts.
|
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 right, that is transform [l; r] to either segment [l - 1; r], or to segment [l; r + 1].The value of a set of segments that consists of n segments [l1; r1], [l2; r2], ..., [ln; rn] is the number of integers x, such that there is integer j, for which the following inequality holds, lj β€ x β€ rj.Find the minimum number of moves needed to make the value of the set of Polo's segments divisible by k.
|
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 inequality holds, min(ri, rj) < max(li, lj).
|
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 special, because it can be written as \(4^0 + 4^2 = 1 + 16 = 17\), but \(9\) is not.Theofanis asks you to help him find the \(k\)-th special number if they are sorted in increasing order. Since this number may be too large, output it modulo \(10^9+7\).
|
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 Chanek goes to the local fish shop. The shop provides \(n\) clownfish numbered from \(1\) to \(n\), with clownfish \(i\) having a size of \(a_i\). Initially, every clownfish in the store does not have an assigned gender, but has the ability to be assigned to two possible clownfish genders, female or male.The store has a procedure which Pak Chanek should follow to buy clownfish. The shop owner will point at each clownfish sequentially from \(1\) to \(n\) and for each clownfish, she asks Pak Chanek whether to buy it or not. Each time Pak Chanek is asked, he must declare whether to buy the currently asked clownfish or not, before the shop owner moves on to the next clownfish. If Pak Chanek declares to buy the currently asked clownfish, he must also declare the gender to be assigned to that clownfish immediately. When assigning the gender for the currently asked clownfish, the following conditions must be satisfied: If Pak Chanek assigns it to be female and he has bought a female clownfish before, then the size of the current one must be exactly \(1\) bigger than the last female one. If Pak Chanek assigns it to be male and he has bought a male clownfish before, then the size of the current one must be exactly \(1\) smaller than the last male one. Pak Chanek wants to buy exactly \(k\) clownfish such that: There is at least one female clownfish and one male clownfish. Among the \(k\) clownfish Pak Chanek buys, the mean size of the female clownfish is equal to the mean size of the male clownfish. Let \(l\) and \(r\) respectively be the minimum and maximum index of a clownfish Pak Chanek buys. What is the minimum possible value of \(r-l\)?
|
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 as male. Buy clownfish \(8\) and assign it as female. Do not buy clownfish \(9\). Then, we get that: The mean size among the \(2\) female clownfish Pak Chanek buys is \(\frac{2+3}{2} = 2.5\). The mean size among the \(4\) male clownfish Pak Chanek buys is \(\frac{4+3+2+1}{4} = 2.5\). \(l=2\), \(r=8\), \(r-l=6\). There are no better strategies.
|
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 obtained MEX values. What is the maximum bitwise XOR that can be obtained?The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance: The MEX of \([2,2,1]\) is \(0\), because \(0\) does not belong to the array. The MEX of \([3,1,0,1]\) is \(2\), because \(0\) and \(1\) belong to the array, but \(2\) does not. The MEX of \([0,3,1,2]\) is \(4\), because \(0\), \(1\), \(2\) and \(3\) belong to the array, but \(4\) does not.
|
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 \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq n\)) β the array \(a\).It is guaranteed that the sum of all \(n\) values across all test cases does not exceed \(5000\).
|
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, 2, 4, 3]) = 5\), therefore, the XOR is \(5 \oplus 3=6\).In the third test case, the maximum XOR is \(7\) if we partition the array into segments \([1, 0]\) and \([7, 1, 2, 0, 2, 4, 3]\): \(\operatorname{MEX}([1, 0]) = 2\), \(\operatorname{MEX}([7, 1, 2, 0, 2, 4, 3]) = 5\), therefore, the XOR is \(5 \oplus 2 = 7\).
|
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 contain a traffic light; each has a position \(p_i\) and an initial delay \(d_i\) for which \(d_i < k\). The \(i\)-th traffic light works the following way: it shows red at the \(l \cdot k + d_i\)-th second, where \(l\) is an integer, it shows green otherwise. At second \(0\), you are initially positioned at some cell on the strip, facing the positive direction. At each second, you perform the following actions in order: If the current cell contains a red traffic light, you turn around. Move one cell in the direction you are currently facing. You are given \(q\) different starting positions. For each one, determine whether you will eventually leave the strip within \(10^{100}\) seconds.
|
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 number of traffic lights and the length of the period.The second line of each test case contains \(n\) integers \(p_1, p_2, \ldots p_n\) (\(1 \le p_1 < p_2 < \ldots < p_n \le 10^{15}\)) β the positions of the traffic lights.The third line of each test case contains \(n\) integers \(d_1, d_2, \ldots d_n\) (\(0 \le d_i < k\)) β the delays of the traffic lights.The fourth line of each test case contains one integer \(q\) (\(\mathbf{1 \le q \le 2\cdot10^5}\)) β the number of queries.The fifth line of each test case contains \(q\) integers \(a_1, a_2, \ldots, a_q\) (\(1 \leq a_i \leq 10^{15}\)) β the starting positions.It is guaranteed that the sum of \(n\) and \(q\) over all test cases does not exceed \(\mathbf{2\cdot10^5}\).
|
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 spaceships to attack these bases.The galaxy can be represented as an undirected graph with \(n\) planets (nodes) and \(m\) wormholes (edges), each connecting two planets.A total of \(s\) empire spaceships and \(b\) rebel bases are located at different planets in the galaxy.Each spaceship is given a location \(x\), denoting the index of the planet on which it is located, an attacking strength \(a\), and a certain amount of fuel \(f\).Each base is given a location \(x\), and a defensive strength \(d\).A spaceship can attack a base if both of these conditions hold: the spaceship's attacking strength is greater or equal than the defensive strength of the base the spaceship's fuel is greater or equal to the shortest distance, computed as the number of wormholes, between the spaceship's planet and the base's planet Vader is very particular about his attacking formations. He requires that each spaceship is to attack at most one base and that each base is to be attacked by at most one spaceship.Vader knows that the rebels have hidden \(k\) gold in each base, so he will assign the spaceships to attack bases in such a way that maximizes the number of bases attacked.Therefore, for each base that is attacked, the rebels lose \(k\) gold.However, the rebels have the ability to create any number of dummy bases. With the Doctor's help, these bases would exist beyond space and time, so all spaceship can reach them and attack them. Moreover, a dummy base is designed to seem irresistible: that is, it will always be attacked by some spaceship.Of course, dummy bases do not contain any gold, but creating such a dummy base costs \(h\) gold.What is the minimum gold the rebels can lose if they create an optimal number of dummy bases?
|
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 four integers \(s\), \(b\), \(k\) and \(h\) (\(1 \leq s\), \(b \leq 1000\), \(0 \leq k\), \(h \leq 10^9\)), the number of spaceships, the number of bases, the cost of having a base attacked, and the cost of creating a dummy base, respectively.The next \(s\) lines contain three integers \(x\), \(a\), \(f\) (\(1 \leq x \leq n\), \(0 \leq a\), \(f \leq 10^9\)), denoting the location, attack, and fuel of the spaceship.The next \(b\) lines contain two integers \(x\), \(d\) (\(1 \leq x \leq n\), \(0 \leq d \leq 10^9\)), denoting the location and defence of the base.
|
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 branches will begin to fall off the tree. It is clear, that if a vertex falls off the tree, then its entire subtree will fall off too. In addition, the root will remain on the tree. Formally: the tree will have some connected subset of vertices containing the root.After that, the fruits will grow on the tree (only at those vertices which remain). Exactly \(x\) fruits will grow in the root. The number of fruits in each remaining vertex will be not less than the sum of the numbers of fruits in the remaining sons of this vertex. It is allowed, that some vertices will not have any fruits.Natasha wondered how many tree configurations can be after the described changes. Since this number can be very large, output it modulo \(998244353\).Two configurations of the resulting tree are considered different if one of these two conditions is true: they have different subsets of remaining vertices; they have the same subset of remaining vertices, but there is a vertex in this subset where they have a different amount of fruits.
|
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 of the tree.It is guaranteed that the input data describes a correct binary tree with the root at the vertex \(1\).
|
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 \(2\), there are \(2\) fruits at the vertex \(3\); there are no fruits at the vertex \(2\), there is no vertex \(3\); there are no fruits at the vertex \(2\), there are no fruits at the vertex \(3\); there are no fruits at the vertex \(2\), there is \(1\) fruit at the vertex \(3\); there are no fruits at the vertex \(2\), there are \(2\) fruits at the vertex \(3\); there is \(1\) fruit at the vertex \(2\), there is no vertex \(3\); there is \(1\) fruit at the vertex \(2\), there are no fruits at the vertex \(3\); there is \(1\) fruit at the vertex \(2\), there is \(1\) fruit at the vertex \(3\); there are \(2\) fruits at the vertex \(2\), there is no vertex \(3\); there are \(2\) fruits at the vertex \(2\), there are no fruits at the vertex \(3\). Consider the second example. There are \(5\) fruits at the vertex \(1\). The following \(7\) options are possible: there is no vertex \(2\); there are no fruits at the vertex \(2\); there is \(1\) fruit at the vertex \(2\); there are \(2\) fruits at the vertex \(2\); there are \(3\) fruits at the vertex \(2\); there are \(4\) fruits at the vertex \(2\); there are \(5\) fruits at the vertex \(2\).
|
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 all the cities of the province, but there are no roads between provinces. Unlike other turn-based strategies, in FreeDiv a player has the opportunity to build tunnels between cities. The tunnels are two-way roads along which one can move armies undetected by the enemy. However, no more than one tunnel can be connected to each city. As for Vasya, he wants to build a network of tunnels so that any pair of cities in his state were reachable by some path consisting of roads and a tunnels. But at that no more than k tunnels are connected to each province (otherwise, the province will be difficult to keep in case other provinces are captured by enemy armies).Vasya discovered that maybe he will not be able to build such a network for the current condition of the state. Maybe he'll have first to build several roads between cities in different provinces to merge the provinces. Your task is to determine the minimum number of roads Vasya needs to build so that it was possible to build the required network of tunnels in the resulting state.
|
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 build additional road between cities 1 and 2 and build two tunnels between cities 1 and 3, 2 and 4 after that.
|
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 cannot rotate these figures.Your task is to say if you can clear the whole field by placing such figures.More formally, the problem can be described like this:The following process occurs while at least one \(a_i\) is greater than \(0\): You place one figure \(2 \times 1\) (choose some \(i\) from \(1\) to \(n\) and replace \(a_i\) with \(a_i + 2\)); then, while all \(a_i\) are greater than zero, replace each \(a_i\) with \(a_i - 1\). And your task is to determine if it is possible to clear the whole field (i.e. finish the described process), choosing the places for new figures properly.You have to answer \(t\) independent test cases.
|
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\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 100\)), where \(a_i\) is the initial height of the \(i\)-th column of the Tetris field.
|
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 second column and after the second step of the process, the field becomes \([0, 0, 0]\).And the second test case of the example field is shown below:It can be shown that you cannot do anything to end the process.In the third test case of the example, you first place the figure in the second column after the second step of the process, the field becomes \([0, 2]\). Then place the figure in the first column and after the second step of the process, the field becomes \([0, 0]\).In the fourth test case of the example, place the figure in the first column, then the field becomes \([102]\) after the first step of the process, and then the field becomes \([0]\) after the second step of the process.
|
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 generator of \(8\) since we can perform the following operations: \(3 \xrightarrow{d = 3} 6 \xrightarrow{d = 2} 8\); \(4\) is a generator of \(10\) since we can perform the following operations: \(4 \xrightarrow{d = 4} 8 \xrightarrow{d = 2} 10\); \(5\) is not a generator of \(6\) since we cannot transform \(5\) into \(6\) with the operation above. Now, Kevin gives you an array \(a\) consisting of \(n\) pairwise distinct integers (\(a_i\ge 2\)).You have to find an integer \(x\ge 2\) such that for each \(1\le i\le n\), \(x\) is a generator of \(a_i\), or determine that such an integer does not exist.
|
Each test contains multiple test cases. The first line of the input contains a single integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1\le n\le 10^5\)) β the length of the array \(a\).The second line contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(2\le a_i\le 4\cdot 10^5\)) β the elements in the array \(a\). It is guaranteed that the elements are pairwise distinct.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
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\) is a generator of \(10\), since we can perform the following operations: \(2 \xrightarrow{d = 2} 4 \xrightarrow{d = 2} 6 \xrightarrow{d = 2} 8 \xrightarrow{d = 2} 10\). In the second test case, it can be proven that it is impossible to find a common generator of the four integers.
|
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 worker doesn't want to work with the one whose skill is very different. In other words, the i-th worker won't work with those whose skill is less than li, and with those whose skill is more than ri.Pavel understands that the game of his dream isn't too hard to develop, so the worker with any skill will be equally useful. That's why he wants to pick a team of the maximum possible size. Help him pick such team.
|
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 skill value of the workers that the i-th worker can work with.
|
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 smallest number of pairs of vertices!
|
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^5, \frac{n \cdot (n - 1)}{2})\)) β the number of vertices in the graph and the number of edges.Each of the next \(m\) lines contains two integers \(u\) and \(v\) (\(1 \leq u, v \leq n, u \neq v\)), indicating that there is an undirected edge in the graph between vertices \(u\) and \(v\).It is guaranteed that the given graph is connected and without multiple edges.It is guaranteed that the sum of \(n\) and the sum of \(m\) over all sets of input data does not exceed \(2 \cdot 10^5\).
|
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 this initially. We will remove the edge \((3, 4)\) and then the only reachable pairs of vertices will be \((1, 2)\), \((1, 3)\), \((2, 3)\), \((4, 5)\), \((4, 6)\), \((5, 6)\). In the sixth set of input data, the graph looks like this initially. After removing the edge \((2, 4)\), the graph will look like this. Thus, there will be \(21\) pairs of reachable vertices.
|
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 in the i-th restaurant. If time ti exceeds the time k that the coach has given for the lunch break, then the Rabbits' joy from lunching in this restaurant will equal fi - (ti - k). Otherwise, the Rabbits get exactly fi units of joy.Your task is to find the value of the maximum joy the Rabbits can get from the lunch, depending on the restaurant. The Rabbits must choose exactly one restaurant to lunch in. Note that the joy value isn't necessarily a positive value.
|
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 characteristics of the i-th restaurant.
|
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\) by either \(a_i\) or \(a_j\).Determine whether \(a\) is beautiful.\(^\dagger\) \(x\) is divisible by \(y\) if there exists an integer \(z\) such that \(x = y \cdot z\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\leq t\leq 500\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3\leq n\leq 10^5\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1\leq a_i \leq 10^9\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output ""Yes"" if array \(a\) is beautiful, and output ""No"" otherwise.You can output ""Yes"" and ""No"" in any case (for example, strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive response).
|
In the first test case, any two numbers in the array are coprime, so the answer is ""No"".In the second test case, we can pick \(i=2\) and \(j=1\). Since every number in the array is divisible by \(a_i = 1\), the answer is ""Yes"".In the third test case, we can pick \(i=3\) and \(j=5\). \(2\) and \(4\) is divisible by \(a_i = 2\) while \(3\), \(6\) and \(12\) is divisible by \(a_j = 3\), so the answer is ""Yes"".
|
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, but not happily ever after: one day a vicious dragon attacked the kingdom and stole Victoria. The King was full of grief, yet he gathered his noble knights and promised half of his kingdom and Victoria's hand in marriage to the one who will save the girl from the infernal beast.Having travelled for some time, the knights found the dragon's lair and all of them rushed there to save Victoria. Each knight spat on the dragon once and, as the dragon had quite a fragile and frail heart, his heart broke and poor beast died. As for the noble knights, they got Victoria right to the King and started brawling as each one wanted the girl's hand in marriage.The problem was that all the noble knights were equally noble and equally handsome, and Victoria didn't want to marry any of them anyway. Then the King (and he was a very wise man and didn't want to hurt anybody's feelings) decided to find out who will get his daughter randomly, i.e. tossing a coin. However, there turned out to be n noble knights and the coin only has two sides. The good thing is that when a coin is tossed, the coin falls on each side with equal probability. The King got interested how to pick one noble knight using this coin so that all knights had equal probability of being chosen (the probability in that case should always be equal to 1 / n). First the King wants to know the expected number of times he will need to toss a coin to determine the winner. Besides, while tossing the coin, the King should follow the optimal tossing strategy (i.e. the strategy that minimizes the expected number of tosses). Help the King in this challenging task.
|
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 at time 0.The elevator takes exactly 1 second to move down exactly 1 floor and negligible time to pick up passengers. Genos is given a list detailing when and on which floor passengers arrive. Please determine how long in seconds it will take Genos to bring all passengers to floor 0.
|
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 number i.
|
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. Pick up passenger 1.8. Go to floor 0: takes 2 seconds.This gives a total of 2 + 2 + 4 + 1 + 2 = 11 seconds.
|
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\( and \)j\( (\)1 \leq i < j \leq n\() and swap the values of \)b_i\( and \)b_j\(. Help him find the maximum possible absolute beauty of the array \)b$$$ after performing at most one swap.
|
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 each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1\leq a_i\leq 10^9\)) β the array \(a\).The third line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1\leq b_i\leq 10^9\)) β the array \(b\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output 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\). These are all the possible outcomes, hence the answer is \(2\).In the third test case, it is optimal for Kirill to not perform the swap. Similarly to the previous test case, the answer is \(2\).In the fourth test case, no matter what Kirill does, the absolute beauty of \(b\) remains equal to \(16\).
|
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 that is parallel to the Oy axis, equals h. The second rectangle can be obtained by rotating the first rectangle relative to the origin of coordinates by angle Ξ±. Your task is to find the area of the region which belongs to both given rectangles. This region is shaded in the picture.
|
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 Elements of Harmony is in constant movement. According to the ancient book, the energy of vertex u in time i (ei[u]) equals to: Here b[] is the transformation coefficient β an array of m + 1 integers and f(u, v) is the number of ones in the binary representation of number (u xor v).Given the transformation coefficient and the energy distribution at time 0 (e0[]). Help Twilight Sparkle predict the energy distribution at time t (et[]). The answer can be quite large, so output it modulo p.
|
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-rated. It means that all rating changes must be divided by two.There are two conditions though: For each participant \(i\), their modified rating change \(b_i\) must be integer, and as close to \(\frac{a_i}{2}\) as possible. It means that either \(b_i = \lfloor \frac{a_i}{2} \rfloor\) or \(b_i = \lceil \frac{a_i}{2} \rceil\). In particular, if \(a_i\) is even, \(b_i = \frac{a_i}{2}\). Here \(\lfloor x \rfloor\) denotes rounding down to the largest integer not greater than \(x\), and \(\lceil x \rceil\) denotes rounding up to the smallest integer not smaller than \(x\). The modified rating changes must be perfectly balanced β their sum must be equal to \(0\). Can you help with that?
|
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, print any. We can show that a solution exists for any valid input.
|
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 phase). When a team loses a game, it is eliminated, and each game results in elimination of one team (there are no ties). After that, only \(2^{k-1}\) teams remain. If only one team remains, it is declared the champion; otherwise, \(2^{k-2}\) games are played: in the first one of them, the winner of the game ""\(1\) vs \(2\)"" plays against the winner of the game ""\(3\) vs \(4\)"", then the winner of the game ""\(5\) vs \(6\)"" plays against the winner of the game ""\(7\) vs \(8\)"", and so on. This process repeats until only one team remains.After the tournament ends, the teams are assigned places according to the tournament phase when they were eliminated. In particular: the winner of the tournament gets place \(1\); the team eliminated in the finals gets place \(2\); both teams eliminated in the semifinals get place \(3\); all teams eliminated in the quarterfinals get place \(5\); all teams eliminated in the 1/8 finals get place \(9\), and so on. For example, this picture describes one of the possible ways the tournament can go with \(k = 3\), and the resulting places of the teams: After a tournament which was conducted by the aforementioned rules ended, its results were encoded in the following way. Let \(p_i\) be the place of the \(i\)-th team in the tournament. The hash value of the tournament \(h\) is calculated as \(h = (\sum \limits_{i=1}^{2^k} i \cdot A^{p_i}) \bmod 998244353\), where \(A\) is some given integer.Unfortunately, due to a system crash, almost all tournament-related data was lost. The only pieces of data that remain are the values of \(k\), \(A\) and \(h\). You are asked to restore the resulting placing of the teams in the tournament, if it is possible at all.
|
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 tournament could go, and note that the initial structure of the tournament is fixed (for example, teams \(1\) and \(2\) always play in the first phase of the tournament against each other). If there are multiple ways to restore the places of the teams which are consistent with the data you have, print any of them.
|
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 placing since teams \(1\) and \(2\) play against each other in the semifinals, so they cannot get two first places.
|
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 points can be divided into pairs so that each point will belong to exactly one pair; in each pair the point with the lesser abscissa will be marked with a small English letter and the point with the larger abscissa will be marked with the same large English letter; if we built a square on each pair, the pair's points will be the square's opposite points and the segment between them will be the square's diagonal, then among the resulting squares there won't be any intersecting or touching ones. Little Petya erased some small and all large letters marking the points. Now Sereja wonders how many ways are there to return the removed letters so that the points were marked beautifully.
|
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 them). It is guaranteed that the input string doesn't contain letter ""x"".
|
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 and up in one move). The one who cannot make a move loses.Burenka makes the first move, the players take turns. Burenka really wants to win the game, but she is too lazy to come up with a strategy, so you are invited to solve the difficult task of finding it. Name the winner of the game (it is believed that Burenka and Tonya are masters of playing with chips, so they always move in the optimal way). Chip's starting cell is green, the only cell from which chip can't move is red. if the chip is in the yellow cell, then blue cells are all options to move the chip in one move.
|
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 \times 5\) cells, and Tonya is the first player. In such game the second player wins, so in the original one Burenka will win.
|
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 couldn't get why the required minimum is 6 people. ""Well, that's the same for five people, too!"" β he kept on repeating in his mind. β ""Let's take, say, Max, Ilya, Vova β here, they all know each other! And now let's add Dima and Oleg to Vova β none of them is acquainted with each other! Now, that math is just rubbish!""Igor K. took 5 friends of his and wrote down who of them is friends with whom. Now he wants to check whether it is true for the five people that among them there are either three pairwise acquainted or three pairwise not acquainted people.
|
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 acquaintances is described exactly once. The acquaintance relation is symmetrical, i.e. if x is acquainted with y, then y is also acquainted with x.
|
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 block, he gets the integer 12. Wet Shark then takes this number modulo x. Please, tell him how many ways he can choose one digit from each block so that he gets exactly k as the final result. As this number may be too large, print it modulo 109 + 7.Note, that the number of ways to choose some digit in the block is equal to the number of it's occurrences. For example, there are 3 ways to choose digit 5 from block 3 5 6 7 8 9 5 1 1 1 1 5.
|
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 give the digits contained in each block.
|
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 an array \(a\) if \(b\) can be obtained from \(a\) by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. In particular, an array is a subarray of itself.
|
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 queries.The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(a_i\) is \(1\) or \(2\)) β the elements of array \(a\).Each of the following \(q\) lines of each test case contains some integers. The first integer \(\mathrm{op}\) is either \(1\) or \(2\). If \(\mathrm{op}\) is \(1\), it is followed by one integer \(s\) (\(1 \leq s \leq 2n\)). If \(\mathrm{op}\) is \(2\), it is followed by two integers \(i\) and \(v\) (\(1 \leq i \leq n\), \(v\) is \(1\) or \(2\)). It is guaranteed that the sum of \(n\) and the sum of \(q\) over all test cases both do not exceed \(10^5\).
|
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 array \(a\) becomes \([2,1,2,2,2]\). The answer for the fifth query is ""YES"" because \(a_2+a_3+a_4+a_5=1+2+2+2=7\).
|
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 decided to change the design of the fence he had built, by cutting his top connected part so that the fence remained good. The cut part should consist of only the upper parts of the boards, while the adjacent parts must be interconnected (share a non-zero length before cutting out of the fence).You, as Vasily's curious neighbor, will count the number of possible ways to cut exactly one part as is described above. Two ways to cut a part are called distinct, if for the remaining fences there is such i, that the height of the i-th boards vary.As Vasily's fence can be very high and long, get the remainder after dividing the required number of ways by 1 000 000 007 (109 + 7).
|
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 positive integer \(k\) at most \(n\), and passes the ball to his \(k\)-th neighbour in the direction of increasing ids, that person passes the ball to his \(k\)-th neighbour in the same direction, and so on until the person with the id \(1\) gets the ball back. When he gets it back, people do not pass the ball any more.For instance, if \(n = 6\) and \(k = 4\), the ball is passed in order \([1, 5, 3, 1]\). Consider the set of all people that touched the ball. The fun value of the game is the sum of the ids of people that touched it. In the above example, the fun value would be \(1 + 5 + 3 = 9\).Find and report the set of possible fun values for all choices of positive integer \(k\). It can be shown that under the constraints of the problem, the ball always gets back to the \(1\)-st player after finitely many steps, and there are no more than \(10^5\) possible fun values for given \(n\).
|
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 achieved for instance for \(k = 16\), \(8\), \(4\), \(10\) and \(11\), respectively.
|
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 barrier, and she couldn't enter this barrier in a short time and destroy them. Therefore, all that can be done is to save any unfortunate person on the street from the ghosts.The pedestrian street can be represented as a one-dimensional coordinate system. There is one person hanging out on the pedestrian street. At the time \(0\) he is at coordinate \(x\), moving with a speed of \(1\) unit per second. In particular, at time \(i\) the person will be at coordinate \(x+i\).The ghosts are going to cast \(n\) curses on the street. The \(i\)-th curse will last from time \(tl_i-1+10^{-18}\) to time \(tr_i+1-10^{-18}\) (exclusively) and will kill people with coordinates from \(l_i-1+10^{-18}\) to \(r_i+1-10^{-18}\) (exclusively). Formally that means, that the person, whose coordinate is between \((l_i-1+10^{-18},r_i+1-10^{-18})\) in the time range \((tl_i-1+10^{-18},tr_i+1-10^{-18})\) will die.To save the person on the street, AquaMoon can stop time at any moment \(t\), and then move the person from his current coordinate \(x\) to any coordinate \(y\) (\(t\), \(x\) and \(y\) are not necessarily integers). The movement costs AquaMoon \(|x-y|\) energy. The movement is continuous, so if there exists some cursed area between points \(x\) and \(y\) at time \(t\), the person will die too.AquaMoon wants to know what is the minimum amount of energy she needs to spend in order to save the person on the street from all \(n\) curses. But she is not good at programming. As her friend, can you help her?
|
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 10^6\), \(1\le l_i\le r_i\le 10^6\)).
|
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 substring as you wish).Calculate the number of different strings which can be obtained from \(s\) by performing this operation at most once.
|
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 00110, and reorder its characters to get 11000; to obtain 1100101, you can take the substring from the \(5\)-th character to the \(7\)-th character, which is 110, and reorder its characters to get 101. In the second example, \(k = 0\) so you can only choose the substrings consisting only of 0 characters. Reordering them doesn't change the string at all, so the only string you can obtain is 10010.
|
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 that \(a_i + a_j + a_k = a_l\).Determine if \(a\) is 3SUM-closed.
|
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^9\)) β the elements of the array.It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output ""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-closed.In the third test case, \(a_i + a_j + a_k = 0\) for all distinct \(i\), \(j\), \(k\), and \(0\) is an element of the array, so the array is 3SUM-closed.
|
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 number of pairs \((l,r)\) (\(1 \leq l \leq r \leq n\)) that are good.
|
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^5\)) β the length of the array \(a\) and the integer \(k\).The second line of each test case contains \(n\) space-separated integers \(a_1,a_2,\ldots,a_n\) (\(1 \leq a_i \leq 10^5\)) β representing the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
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, 3, 4\) which satisfy the given conditions.
|
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. Twilight Sparkle wanted to track the path of Nightmare Moon. Unfortunately, she didn't know the exact path. What she knew is the parity of the number of times that each place Nightmare Moon visited. Can you help Twilight Sparkle to restore any path that is consistent with this information?Ponyville can be represented as an undirected graph (vertices are places, edges are roads between places) without self-loops and multi-edges. The path can start and end at any place (also it can be empty). Each place can be visited multiple times. The path must not visit more than 4n places.
|
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, ..., xn (0 β€ xi β€ 1) β the parity of the number of times that each place must be visited. If xi = 0, then the i-th place must be visited even number of times, else it must be visited odd number of times.
|
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-loops, therefore any two neighbouring places in the path must be distinct.If there is no required path, output -1. If there multiple possible paths, you can output any of them.
|
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 \(v\).You are asked to remove some edges from the graph. Let the new degrees be \(\mathit{in'}_v\) and \(\mathit{out'}_v\).You are only allowed to remove the edges if the following conditions hold for every vertex \(v\): \(\mathit{in'}_v < \mathit{in}_v\) or \(\mathit{in'}_v = \mathit{in}_v = 0\); \(\mathit{out'}_v < \mathit{out}_v\) or \(\mathit{out'}_v = \mathit{out}_v = 0\). Let's call a set of vertices \(S\) cute if for each pair of vertices \(v\) and \(u\) (\(v \neq u\)) such that \(v \in S\) and \(u \in S\), there exists a path either from \(v\) to \(u\) or from \(u\) to \(v\) over the non-removed edges.What is 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\)?
|
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 edges form a valid directed acyclic graph. There are no multiple edges.
|
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 still connected directly by an edge, so there is a path between them.In the second example, there are no edges. Since all \(\mathit{in}_v\) and \(\mathit{out}_v\) are equal to \(0\), leaving a graph with zero edges is allowed. There are \(5\) cute sets, each contains a single vertex. Thus, the maximum size is \(1\).In the third example, you can remove edges \((7, 1)\), \((2, 4)\), \((1, 3)\) and \((6, 2)\). The maximum cute set will be \(S = \{7, 3, 2\}\). You can remove edge \((7, 3)\) as well, and the answer won't change.Here is the picture of the graph from the third example:
|
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]\). Your task is to say 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.
|
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 labels of vertices it connects \((1 \le u_i, v_i \le n, u_i \ne v_i\)).It is guaranteed that the given edges form a tree.The next \(m\) lines describe queries. The \(i\)-th line describes the \(i\)-th query and starts with the integer \(k_i\) (\(1 \le k_i \le n\)) β the number of vertices in the current query. Then \(k_i\) integers follow: \(v_i[1], v_i[2], \dots, v_i[k_i]\) (\(1 \le v_i[j] \le n\)), where \(v_i[j]\) is the \(j\)-th vertex of the \(i\)-th query.It is guaranteed that all vertices in a single query are distinct.It is guaranteed that the sum of \(k_i\) does not exceed \(2 \cdot 10^5\) (\(\sum\limits_{i=1}^{m} k_i \le 2 \cdot 10^5\)).
|
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\) which also belongs to this path.The second query is \([2, 4, 6]\). The answer is ""YES"" as you can choose the path to the vertex \(u=2\). Then the vertex \(4\) has distance \(1\) to the vertex \(1\) which belongs to this path and the vertex \(6\) has distance \(1\) to the vertex \(2\) which belongs to this path.The third query is \([2, 1, 5]\). The answer is ""YES"" as you can choose the path to the vertex \(u=5\) and all vertices of the query belong to this path.The fourth query is \([4, 8, 2]\). The answer is ""YES"" as you can choose the path to the vertex \(u=9\) so vertices \(2\) and \(4\) both have distance \(1\) to the vertex \(1\) which belongs to this path and the vertex \(8\) has distance \(1\) to the vertex \(7\) which belongs to this path.The fifth and the sixth queries both have answer ""NO"" because you cannot choose suitable vertex \(u\).
|
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.You can perform the following operation any number of times (possibly zero): choose an element of the array and replace it with any integer.Your task is to calculate the minimum number of aforementioned operation in order for all the subarrays of the array \(a\) to be unique.
|
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 sum of \(n\) over all test cases doesn't exceed \(3 \cdot 10^5\).
|
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 from any vessel and pour it into the other vessel. Note that the mass of water poured in one move does not have to be an integer.What is the minimum number of moves required to make the masses of water in the vessels equal? Note that you cannot perform any actions other than the described moves.
|
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 the capacity of the cup, respectively.
|
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 first vessel will contain \(17 - 3 = 14\) grams of water, and the second vessel will contain \(4 + 3 = 7\) grams. Pour \(2\) grams of water from the first vessel into the second one. After this move, the first vessel will contain \(14 - 2 = 12\) grams of water, and the second vessel will contain \(7 + 2 = 9\) grams. Finally, pour \(1.5\) grams of water from the first vessel into the second one. After this move, the first vessel will contain \(12 - 1.5 = 10.5\) grams of water, and the second vessel will contain \(9 + 1.5 = 10.5\) grams.Note that this is not the only way to equalize the vessels in \(3\) moves, but there is no way to do it in \(2\) moves.In the third example test case, the vessels initially contain the same amount of water, so no moves are needed. The answer is \(0\).
|
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\) characters are 0-s.In one operation, you can replace one 0 with 1.Calculate the number of unique strings you can get using no more than \(k\) operations. Since the answer may be too large, print it modulo \(10^9 + 7\).
|
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 three hands β hour, minute, and second. Time froze, and clocks now show the time h hours, m minutes, s seconds.Last time Misha talked with the coordinator at t1 o'clock, so now he stands on the number t1 on the clock face. The contest should be ready by t2 o'clock. In the terms of paradox it means that Misha has to go to number t2 somehow. Note that he doesn't have to move forward only: in these circumstances time has no direction.Clock hands are very long, and Misha cannot get round them. He also cannot step over as it leads to the collapse of space-time. That is, if hour clock points 12 and Misha stands at 11 then he cannot move to 1 along the top arc. He has to follow all the way round the clock center (of course, if there are no other hands on his way).Given the hands' positions, t1, and t2, find if Misha can prepare the contest on time (or should we say on space?). That is, find if he can move from t1 to t2 by the clock face.
|
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)\), \((w, d)\), and \((0, d)\).A laptop is standing on the floor at point \((a, b)\). A projector is hanging on the ceiling right above point \((f, g)\). Vika wants to connect the laptop and the projector with a cable in such a way that the cable always goes along the walls, ceiling, or floor (i. e. does not go inside the cuboid). Additionally, the cable should always run parallel to one of the cuboid's edges (i. e. it can not go diagonally).What is the minimum length of a cable that can connect the laptop to the projector? Illustration for the first test case. One of the optimal ways to put the cable is shown in green.
|
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 four integers \(a\), \(b\), \(f\), \(g\) (\(0 < a, f < w\); \(0 < b, g < d\)): the laptop is located on the floor at point \((a, b)\), while the projector is hanging on the ceiling right above point \((f, g)\).
|
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 the graph \(x_1, x_2, \ldots, x_k\) a simple cycle, if \(k \geq 3\) and all pairs of nodes \(x_1\) and \(x_2\), \(x_2\) and \(x_3\), \(\ldots\), \(x_{k-1}\) and \(x_k\), \(x_k\) and \(x_1\) are connected with edges. Edges \((x_1, x_2)\), \((x_2, x_3)\), \(\ldots\), \((x_{k-1}, x_k)\), \((x_k, x_1)\) lies on this simple cycle.There are so many cactuses, so it seems hard to destroy them. But Alice has magic. When she uses the magic, every node of the cactus will be removed independently with the probability \(\frac{1}{2}\). When a node is removed, the edges connected to it are also removed.Now Alice wants to test her magic. She has picked a cactus with \(n\) nodes and \(m\) edges. Let \(X[S]\) (where \(S\) is a subset of the removed nodes) be the number of connected components in the remaining graph after removing nodes of set \(S\). Before she uses magic, she wants to know the variance of random variable \(X\), if all nodes of the graph have probability \(\frac{1}{2}\) to be removed and all \(n\) of these events are independent. By the definition the variance is equal to \(E[(X - E[X])^2]\), where \(E[X]\) is the expected value of \(X\). Help her and calculate this value by modulo \(10^9+7\).Formally, let \(M = 10^9 + 7\) (a prime number). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, find such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
|
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 there is an edge between the nodes \(u\) and \(v\).It is guaranteed that there are no loops and multiple edges in the graph and the given graph is cactus.
|
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}{8}+(1-\frac{7}{8})^2\times\frac{7}{8} = (\frac{7}{8})^2\times\frac{1}{8}+(\frac{1}{8})^2\times\frac{7}{8} = \frac{7}{64}\).In the second sample, the answer is \(\frac{1}{4}\).
|
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 n\) and all cubes have distinct colors. Each cube occupies exactly one cell. Also, there is a designated cell for each cube on the board, the puzzle is to place each cube on its place. The cubes are fragile, so in one operation you only can move one cube onto one of four neighboring by side cells, if only it is empty. Vasya wants to be careful, so each operation takes exactly one second. Vasya used to train hard for VK Cup Final, so he can focus his attention on the puzzle for at most \(3\) hours, that is \(10800\) seconds. Help Vasya find such a sequence of operations that all cubes will be moved onto their designated places, and Vasya won't lose his attention.
|
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 guaranteed that all initial positions are distinct and all designated places are distinct, however, it is possible that some initial positions coincide with some final positions.
|
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\) is the new position of the cube. The cells \(x_1, y_1\) and \(x_2, y_2\) should have a common side, the cell \(x_2, y_2\) should be empty before the operation.We can show that there always exists at least one solution. If there are multiple solutions, print any of them.
|
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 resulting word must be prefix of at least one string from the group. A player loses if he cannot move.Andrew and Alex decided to play this game k times. The player who is the loser of the i-th game makes the first move in the (i + 1)-th game. Guys decided that the winner of all games is the player who wins the last (k-th) game. Andrew and Alex already started the game. Fedor wants to know who wins the game if both players will play optimally. Help him.
|
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 operations (possibly, zero). Two arrays \(a\) and \(b\) of length \(n\) are called equal if \(a_i = b_i\) for all integers \(i\) from \(1\) to \(n\).
|
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 line of each test case contains \(n\) integers \(a_1, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of the array \(a\).The third line of each test case contains \(n\) integers \(b_1, \ldots, b_n\) (\(1 \le b_i \le 10^9\)) β the elements of the array \(b\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output ""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,5,5,5]\). After that, you can apply operations in order to elements with indices \(5,4,4,3,1\) and already get an array \([6,5,6,7,6]\).
|
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 find any suitable division, or determine that it does not exist.\(^{\dagger}\)A division of an array into \(k\) subsegments is defined as \(k\) pairs of integers \((l_1, r_1), (l_2, r_2), \ldots, (l_k, r_k)\) such that \(l_i \le r_i\) and for each \(1 \le j \le k - 1\), \(l_{j + 1} = r_j + 1\), and also \(l_1 = 1\) and \(r_k = n\). These pairs represent the subsegments themselves.\(^{\ddagger}\operatorname{MEX}\) of an array is the smallest non-negative integer that does not belong to the array.For example: \(\operatorname{MEX}\) of the array \([2, 2, 1]\) is \(0\), because \(0\) does not belong to the array. \(\operatorname{MEX}\) of the array \([3, 1, 0, 1]\) is \(2\), because \(0\) and \(1\) belong to the array, but \(2\) does not. \(\operatorname{MEX}\) of the array \([0, 3, 1, 2]\) is \(4\), because \(0\), \(1\), \(2\), and \(3\) belong to the array, but \(4\) does not.
|
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 of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i < n\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output 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\) and \(r_i\) (\(1 \le l_i \le r_i \le n\)) β the boundaries of the \(i\)-th subsegment.The following conditions must be satisfied: For all \(1 \le j \le k - 1\), \(l_{j + 1} = r_j + 1\); \(l_1 = 1\), \(r_k = n\).If there are multiple possible solutions, output any of them.
|
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\) belongs to the subsegment, but \(1\) does not. In the second test case, it can be proven that the required division does not exist.In the third test case, the array \(a\) can be divided into \(3\) subsegments with boundaries \([1, 3]\), \([4, 5]\), \([6, 8]\): \(\operatorname{MEX}\) of the first subsegment \([0, 1, 7]\) is \(2\), as \(0\) and \(1\) belong to the subsegment, but \(2\) does not. \(\operatorname{MEX}\) of the second subsegment \([1, 0]\) is \(2\), as \(0\) and \(1\) belong to the subsegment, but \(2\) does not. \(\operatorname{MEX}\) of the third subsegment \([1, 0, 3]\) is \(2\), as \(0\) and \(1\) belong to the subsegment, but \(2\) does not.
|
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.