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,739 |
C
|
1739C
|
C. Card Game
| 1,500 |
combinatorics; constructive algorithms; dp; games
|
Consider a game with \(n\) cards (\(n\) is even). Each card has a number written on it, between \(1\) and \(n\). All numbers on the cards are different. We say that a card with number \(x\) is stronger than a card with number \(y\) if \(x > y\).Two players, Alex and Boris, play this game. In the beginning, each of them receives exactly \(\frac{n}{2}\) cards, so each card belongs to exactly one player. Then, they take turns. Alex goes first, then Boris, then Alex again, and so on.On a player's turn, he must play exactly one of his cards. Then, if the opponent doesn't have any cards stronger than the card played, the opponent loses, and the game ends. Otherwise, the opponent has to play a stronger card (exactly one card as well). These two cards are removed from the game, and the turn ends. If there are no cards left, the game ends in a draw; otherwise it's the opponent's turn.Consider all possible ways to distribute the cards between two players, so that each of them receives exactly half of the cards. You have to calculate three numbers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. You may assume that both players play optimally (i. e. if a player can win no matter how his opponent plays, he wins). Two ways to distribute the cards are different if there is at least one card such that, in one of these ways, it is given to Alex, and in the other way, it is given to Boris.For example, suppose \(n = 4\), Alex receives the cards \([2, 3]\), and Boris receives the cards \([1, 4]\). Then the game may go as follows: if Alex plays the card \(2\), then Boris has to respond with the card \(4\). Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is \(1\); he plays it, and Alex responds with the card \(3\). So, the game ends in a draw; if Alex plays the card \(3\), then Boris has to respond with the card \(4\). Then, Alex's turn ends, and Boris' turn starts. Boris has only one card left, which is \(1\); he plays it, and Alex responds with the card \(2\). So, the game ends in a draw. So, in this case, the game ends in a draw.
|
The first line contains one integer \(t\) (\(1 \le t \le 30\)) β the number of test cases.Then, \(t\) lines follow. The \(i\)-th line contains one even integer \(n\) (\(2 \le n \le 60\)).
|
For each test case, print three integers: the number of ways to distribute the cards so that Alex wins; the number of ways to distribute the cards so that Boris wins; the number of ways to distribute the cards so that the game ends in a draw. Since the answers can be large, print them modulo \(998244353\).
|
In the first test case, Alex wins if he receives the card \(2\) (he plays it, and Boris cannot respond). If Alex receives the card \(1\), the game ends in a draw.In the second test case: Alex wins if he receives the cards \([3, 4]\), \([2, 4]\) or \([1, 4]\); Boris wins if Alex receives the cards \([1, 2]\) or \([1, 3]\); the game ends in a draw if Alex receives the cards \([2, 3]\).
|
Input: 5246860 | Output: 1 0 1 3 2 1 12 7 1 42 27 1 341102826 248150916 1
|
Medium
| 4 | 2,236 | 187 | 307 | 17 |
1,463 |
F
|
1463F
|
F. Max Correct Set
| 3,100 |
bitmasks; dp; math
|
Let's call the set of positive integers \(S\) correct if the following two conditions are met: \(S \subseteq \{1, 2, \dots, n\}\); if \(a \in S\) and \(b \in S\), then \(|a-b| \neq x\) and \(|a-b| \neq y\). For the given values \(n\), \(x\), and \(y\), you have to find the maximum size of the correct set.
|
A single line contains three integers \(n\), \(x\) and \(y\) (\(1 \le n \le 10^9\); \(1 \le x, y \le 22\)).
|
Print one integer β the maximum size of the correct set.
|
Input: 10 2 5 | Output: 5
|
Master
| 3 | 306 | 107 | 56 | 14 |
|
125 |
C
|
125C
|
C. Hobbits' Party
| 1,600 |
constructive algorithms; greedy
|
Everyone knows that hobbits love to organize all sorts of parties and celebrations. There are n hobbits living in the Shire. They decided to organize the Greatest Party (GP) that would last for several days. Next day the hobbits wrote a guest list, some non-empty set containing all the inhabitants of the Shire. To ensure that everybody enjoy themselves and nobody gets bored, for any two days (say, days A and B) of the GP there existed at least one hobbit, invited to come on day A and on day B. However, to ensure that nobody has a row, for any three different days A, B, C there shouldn't be a hobbit invited on days A, B and C. The Shire inhabitants are keen on keeping the GP going for as long as possible. Your task is given number n, to indicate the GP's maximum duration and the guest lists for each day.
|
The first line contains an integer n (3 β€ n β€ 10000), representing the number of hobbits.
|
In the first output line print a number k β the maximum duration of GP in days. Then on k lines print the guest lists, (the guests should be separated by spaces). Print each guest list on the single line. Each list can contain an arbitrary positive number of hobbits. The hobbits are numbered with integers from 1 to n.
|
Input: 4 | Output: 31 2 1 3 2 3
|
Medium
| 2 | 814 | 89 | 319 | 1 |
|
1,176 |
D
|
1176D
|
D. Recover it!
| 1,800 |
dfs and similar; graphs; greedy; number theory; sortings
|
Authors guessed an array \(a\) consisting of \(n\) integers; each integer is not less than \(2\) and not greater than \(2 \cdot 10^5\). You don't know the array \(a\), but you know the array \(b\) which is formed from it with the following sequence of operations: Firstly, let the array \(b\) be equal to the array \(a\); Secondly, for each \(i\) from \(1\) to \(n\): if \(a_i\) is a prime number, then one integer \(p_{a_i}\) is appended to array \(b\), where \(p\) is an infinite sequence of prime numbers (\(2, 3, 5, \dots\)); otherwise (if \(a_i\) is not a prime number), the greatest divisor of \(a_i\) which is not equal to \(a_i\) is appended to \(b\); Then the obtained array of length \(2n\) is shuffled and given to you in the input. Here \(p_{a_i}\) means the \(a_i\)-th prime number. The first prime \(p_1 = 2\), the second one is \(p_2 = 3\), and so on.Your task is to recover any suitable array \(a\) that forms the given array \(b\). It is guaranteed that the answer exists (so the array \(b\) is obtained from some suitable array \(a\)). If there are multiple answers, you can print any.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of elements in \(a\).The second line of the input contains \(2n\) integers \(b_1, b_2, \dots, b_{2n}\) (\(2 \le b_i \le 2750131\)), where \(b_i\) is the \(i\)-th element of \(b\). \(2750131\) is the \(199999\)-th prime number.
|
In the only line of the output print \(n\) integers \(a_1, a_2, \dots, a_n\) (\(2 \le a_i \le 2 \cdot 10^5\)) in any order β the array \(a\) from which the array \(b\) can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any.
|
Input: 3 3 5 2 3 2 4 | Output: 3 4 2
|
Medium
| 5 | 1,103 | 325 | 293 | 11 |
|
1,967 |
A
|
1967A
|
A. Permutation Counting
| 1,400 |
binary search; greedy; implementation; math; sortings
|
You have some cards. An integer between \(1\) and \(n\) is written on each card: specifically, for each \(i\) from \(1\) to \(n\), you have \(a_i\) cards which have the number \(i\) written on them.There is also a shop which contains unlimited cards of each type. You have \(k\) coins, so you can buy \(k\) new cards in total, and the cards you buy can contain any integer between \(1\) and \(n\).After buying the new cards, you rearrange all your cards in a line. The score of a rearrangement is the number of (contiguous) subarrays of length \(n\) which are a permutation of \([1, 2, \ldots, n]\). What's the maximum score you can get?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\ (1\le t\le 100)\). The description of the test cases follows.The first line of each test case contains two integers \(n\), \(k\) (\(1\le n \le 2 \cdot 10^5\), \(0\le k \le 10^{12}\)) β the number of distinct types of cards and the number of coins.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^{12}\)) β the number of cards of type \(i\) you have at the beginning.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case, output a single line containing an integer: the maximum score you can get.
|
In the first test case, the final (and only) array we can get is \([1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]\) (including \(11\) single \(1\)s), which contains \(11\) subarrays consisting of a permutation of \([1]\).In the second test case, we can buy \(0\) cards of type \(1\) and \(4\) cards of type \(2\), and then we rearrange the cards as following: \([1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2]\). There are \(8\) subarrays equal to \([1, 2]\) and \(7\) subarrays equal to \([2, 1]\), which make a total of \(15\) subarrays which are a permutation of \([1, 2]\). It can also be proved that this is the maximum score we can get.In the third test case, one of the possible optimal rearrangements is \([3, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 3]\).
|
Input: 81 1012 48 43 46 1 83 97 6 25 36 6 7 4 69 77 6 1 7 6 2 4 3 310 101 3 1 2 1 9 3 5 7 59 85 8 7 5 1 3 2 9 8 | Output: 11 15 15 22 28 32 28 36
|
Easy
| 5 | 637 | 607 | 94 | 19 |
1,498 |
C
|
1498C
|
C. Planar Reflections
| 1,600 |
brute force; data structures; dp
|
Gaurang has grown up in a mystical universe. He is faced by \(n\) consecutive 2D planes. He shoots a particle of decay age \(k\) at the planes.A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age \(k-1\). If a particle has decay age equal to \(1\), it will NOT produce a copy.For example, if there are two planes and a particle is shot with decay age \(3\) (towards the right), the process is as follows: (here, \(D(x)\) refers to a single particle with decay age \(x\)) the first plane produces a \(D(2)\) to the left and lets \(D(3)\) continue on to the right; the second plane produces a \(D(2)\) to the left and lets \(D(3)\) continue on to the right; the first plane lets \(D(2)\) continue on to the left and produces a \(D(1)\) to the right; the second plane lets \(D(1)\) continue on to the right (\(D(1)\) cannot produce any copies). In total, the final multiset \(S\) of particles is \(\{D(3), D(2), D(2), D(1)\}\). (See notes for visual explanation of this test case.)Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset \(S\), given \(n\) and \(k\).Since the size of the multiset can be very large, you have to output it modulo \(10^9+7\).Note: Particles can go back and forth between the planes without colliding with each other.
|
The first line of the input contains the number of test cases \(t\) (\(1 \le t \le 100\)). Then, \(t\) lines follow, each containing two integers \(n\) and \(k\) (\(1 \le n, k \le 1000\)). Additionally, the sum of \(n\) over all test cases will not exceed \(1000\), and the sum of \(k\) over all test cases will not exceed \(1000\). All test cases in one test are different.
|
Output \(t\) integers. The \(i\)-th of them should be equal to the answer to the \(i\)-th test case.
|
Let us explain the first example with four test cases. Test case 1: (\(n = 2\), \(k = 3\)) is already explained in the problem statement.See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: (\(n = 2\), \(k = 2\)) is explained as follows: the first plane produces a \(D(1)\) to the left and lets \(D(2)\) continue on to the right; the second plane produces a \(D(1)\) to the left and lets \(D(2)\) continue on to the right; the first plane lets \(D(1)\) continue on to the left (\(D(1)\) cannot produce any copies).Total size of multiset obtained \(\{D(1), D(1), D(2)\}\) is equal to three.Test case 3: (\(n = 3\), \(k = 1\)), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one.Test case 4: (\(n = 1\), \(k = 3\)) there is only one plane. The particle produces a new copy to the left. The multiset \(\{D(2), D(3)\}\) is of size two.
|
Input: 4 2 3 2 2 3 1 1 3 | Output: 4 3 1 2
|
Medium
| 3 | 1,444 | 374 | 100 | 14 |
1,648 |
C
|
1648C
|
C. Tyler and Strings
| 1,900 |
combinatorics; data structures; implementation
|
While looking at the kitchen fridge, the little boy Tyler noticed magnets with symbols, that can be aligned into a string \(s\).Tyler likes strings, and especially those that are lexicographically smaller than another string, \(t\). After playing with magnets on the fridge, he is wondering, how many distinct strings can be composed out of letters of string \(s\) by rearranging them, so that the resulting string is lexicographically smaller than the string \(t\)? Tyler is too young, so he can't answer this question. The alphabet Tyler uses is very large, so for your convenience he has already replaced the same letters in \(s\) and \(t\) to the same integers, keeping that different letters have been replaced to different integers.We call a string \(x\) lexicographically smaller than a string \(y\) if one of the followings conditions is fulfilled: There exists such position of symbol \(m\) that is presented in both strings, so that before \(m\)-th symbol the strings are equal, and the \(m\)-th symbol of string \(x\) is smaller than \(m\)-th symbol of string \(y\). String \(x\) is the prefix of string \(y\) and \(x \neq y\). Because the answer can be too large, print it modulo \(998\,244\,353\).
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 200\,000\)) β the lengths of strings \(s\) and \(t\) respectively.The second line contains \(n\) integers \(s_1, s_2, s_3, \ldots, s_n\) (\(1 \le s_i \le 200\,000\)) β letters of the string \(s\).The third line contains \(m\) integers \(t_1, t_2, t_3, \ldots, t_m\) (\(1 \le t_i \le 200\,000\)) β letters of the string \(t\).
|
Print a single number β the number of strings lexicographically smaller than \(t\) that can be obtained by rearranging the letters in \(s\), modulo \(998\,244\,353\).
|
In the first example, the strings we are interested in are \([1\, 2\, 2]\) and \([2\, 1\, 2]\). The string \([2\, 2\, 1]\) is lexicographically larger than the string \([2\, 1\, 2\, 1]\), so we don't count it.In the second example, all strings count except \([4\, 3\, 2\, 1]\), so the answer is \(4! - 1 = 23\).In the third example, only the string \([1\, 1\, 1\, 2]\) counts.
|
Input: 3 4 1 2 2 2 1 2 1 | Output: 2
|
Hard
| 3 | 1,210 | 396 | 166 | 16 |
1,612 |
F
|
1612F
|
F. Armor and Weapons
| 2,800 |
brute force; dp; greedy; shortest paths
|
Monocarp plays a computer game. There are \(n\) different sets of armor and \(m\) different weapons in this game. If a character equips the \(i\)-th set of armor and wields the \(j\)-th weapon, their power is usually equal to \(i + j\); but some combinations of armor and weapons synergize well. Formally, there is a list of \(q\) ordered pairs, and if the pair \((i, j)\) belongs to this list, the power of the character equipped with the \(i\)-th set of armor and wielding the \(j\)-th weapon is not \(i + j\), but \(i + j + 1\).Initially, Monocarp's character has got only the \(1\)-st armor set and the \(1\)-st weapon. Monocarp can obtain a new weapon or a new set of armor in one hour. If he wants to obtain the \(k\)-th armor set or the \(k\)-th weapon, he must possess a combination of an armor set and a weapon that gets his power to \(k\) or greater. Of course, after Monocarp obtains a weapon or an armor set, he can use it to obtain new armor sets or weapons, but he can go with any of the older armor sets and/or weapons as well.Monocarp wants to obtain the \(n\)-th armor set and the \(m\)-th weapon. What is the minimum number of hours he has to spend on it?
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n, m \le 2 \cdot 10^5\)) β the number of armor sets and the number of weapons, respectively.The second line contains one integer \(q\) (\(0 \le q \le \min(2 \cdot 10^5, nm)\)) β the number of combinations that synergize well.Then \(q\) lines follow, the \(i\)-th line contains two integers \(a_i\) and \(b_i\) (\(1 \le a_i \le n\); \(1 \le b_i \le m\)) meaning that the \(a_i\)-th armor set synergizes well with the \(b_i\)-th weapon. All pairs \((a_i, b_i)\) are distinct.
|
Print one integer β the minimum number of hours Monocarp has to spend to obtain both the \(n\)-th armor set and the \(m\)-th weapon.
|
In the first example, Monocarp can obtain the strongest armor set and the strongest weapon as follows: Obtain the \(2\)-nd weapon using the \(1\)-st armor set and the \(1\)-st weapon; Obtain the \(3\)-rd armor set using the \(1\)-st armor set and the \(2\)-nd weapon; Obtain the \(4\)-th weapon using the \(3\)-rd armor set and the \(2\)-nd weapon. In the second example, Monocarp can obtain the strongest armor set and the strongest weapon as follows: Obtain the \(3\)-rd armor set using the \(1\)-st armor set and the \(1\)-st weapon (they synergize well, so Monocarp's power is not \(2\) but \(3\)); Obtain the \(4\)-th weapon using the \(3\)-rd armor set and the \(1\)-st weapon.
|
Input: 3 4 0 | Output: 3
|
Master
| 4 | 1,173 | 535 | 132 | 16 |
1,205 |
B
|
1205B
|
B. Shortest Cycle
| 1,900 |
bitmasks; brute force; graphs; shortest paths
|
You are given \(n\) integer numbers \(a_1, a_2, \dots, a_n\). Consider graph on \(n\) nodes, in which nodes \(i\), \(j\) (\(i\neq j\)) are connected if and only if, \(a_i\) AND \(a_j\neq 0\), where AND denotes the bitwise AND operation.Find the length of the shortest cycle in this graph or determine that it doesn't have cycles at all.
|
The first line contains one integer \(n\) \((1 \le n \le 10^5)\) β number of numbers.The second line contains \(n\) integer numbers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 10^{18}\)).
|
If the graph doesn't have any cycles, output \(-1\). Else output the length of the shortest cycle.
|
In the first example, the shortest cycle is \((9, 3, 6, 28)\).In the second example, the shortest cycle is \((5, 12, 9)\).The graph has no cycles in the third example.
|
Input: 4 3 6 28 9 | Output: 4
|
Hard
| 4 | 336 | 185 | 98 | 12 |
1,454 |
F
|
1454F
|
F. Array Partition
| 2,100 |
binary search; data structures; greedy; two pointers
|
You are given an array \(a\) consisting of \(n\) integers.Let \(min(l, r)\) be the minimum value among \(a_l, a_{l + 1}, \ldots, a_r\) and \(max(l, r)\) be the maximum value among \(a_l, a_{l + 1}, \ldots, a_r\).Your task is to choose three positive (greater than \(0\)) integers \(x\), \(y\) and \(z\) such that: \(x + y + z = n\); \(max(1, x) = min(x + 1, x + y) = max(x + y + 1, n)\). In other words, you have to split the array \(a\) into three consecutive non-empty parts that cover the whole array and the maximum in the first part equals the minimum in the second part and equals the maximum in the third part (or determine it is impossible to find such a partition).Among all such triples (partitions), you can choose any.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\) (\(3 \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, \ldots, a_n\) (\(1 \le a_i \le 10^9\)), 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: NO in the only line if there is no such partition of \(a\) that satisfies the conditions from the problem statement. Otherwise, print YES in the first line and three integers \(x\), \(y\) and \(z\) (\(x + y + z = n\)) in the second line.If there are several answers, you can print any.
|
Input: 6 11 1 2 3 3 3 4 4 3 4 2 1 8 2 9 1 7 3 9 4 1 9 2 1 4 2 4 3 3 1 2 7 4 2 1 1 4 1 4 5 1 1 1 1 1 7 4 3 4 3 3 3 4 | Output: YES 6 1 4 NO YES 2 5 2 YES 4 1 2 YES 1 1 3 YES 2 1 4
|
Hard
| 4 | 778 | 513 | 323 | 14 |
|
1,165 |
F2
|
1165F2
|
F2. Microtransactions (hard version)
| 2,000 |
binary search; greedy; implementation
|
The only difference between easy and hard versions is constraints.Ivan plays a computer game that contains some microtransactions to make characters look cooler. Since Ivan wants his character to be really cool, he wants to use some of these microtransactions β and he won't start playing until he gets all of them.Each day (during the morning) Ivan earns exactly one burle.There are \(n\) types of microtransactions in the game. Each microtransaction costs \(2\) burles usually and \(1\) burle if it is on sale. Ivan has to order exactly \(k_i\) microtransactions of the \(i\)-th type (he orders microtransactions during the evening).Ivan can order any (possibly zero) number of microtransactions of any types during any day (of course, if he has enough money to do it). If the microtransaction he wants to order is on sale then he can buy it for \(1\) burle and otherwise he can buy it for \(2\) burles.There are also \(m\) special offers in the game shop. The \(j\)-th offer \((d_j, t_j)\) means that microtransactions of the \(t_j\)-th type are on sale during the \(d_j\)-th day.Ivan wants to order all microtransactions as soon as possible. Your task is to calculate the minimum day when he can buy all microtransactions he want and actually start playing.
|
The first line of the input contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the number of types of microtransactions and the number of special offers in the game shop.The second line of the input contains \(n\) integers \(k_1, k_2, \dots, k_n\) (\(0 \le k_i \le 2 \cdot 10^5\)), where \(k_i\) is the number of copies of microtransaction of the \(i\)-th type Ivan has to order. It is guaranteed that sum of all \(k_i\) is not less than \(1\) and not greater than \(2 \cdot 10^5\).The next \(m\) lines contain special offers. The \(j\)-th of these lines contains the \(j\)-th special offer. It is given as a pair of integers \((d_j, t_j)\) (\(1 \le d_j \le 2 \cdot 10^5, 1 \le t_j \le n\)) and means that microtransactions of the \(t_j\)-th type are on sale during the \(d_j\)-th day.
|
Print one integer β the minimum day when Ivan can order all microtransactions he wants and actually start playing.
|
Input: 5 6 1 2 0 2 0 2 4 3 3 1 5 1 2 1 5 2 3 | Output: 8
|
Hard
| 3 | 1,261 | 808 | 114 | 11 |
|
1,896 |
H1
|
1896H1
|
H1. Cyclic Hamming (Easy Version)
| 3,400 |
brute force; divide and conquer; dp; fft; math; number theory
|
This is the easy version of the problem. The only difference between the two versions is the constraint on \(k\). You can make hacks only if all versions of the problem are solved.In this statement, all strings are \(0\)-indexed.For two strings \(a\), \(b\) of the same length \(p\), we define the following definitions: The hamming distance between \(a\) and \(b\), denoted as \(h(a, b)\), is defined as the number of positions \(i\) such that \(0 \le i < p\) and \(a_i \ne b_i\). \(b\) is a cyclic shift of \(a\) if there exists some \(0 \leq k < p\) such that \(b_{(i+k) \bmod p} = a_i\) for all \(0 \le i < p\). Here \(x \bmod y\) denotes the remainder from dividing \(x\) by \(y\). You are given two binary strings \(s\) and \(t\) of length \(2^{k+1}\) each. Both strings may contain missing characters (denoted by the character '?'). Your task is to count the number of ways to replace the missing characters in both strings with the characters '0' or '1' such that: Each string \(s\) and \(t\) contains exactly \(2^k\) occurrences of each character '0' and '1' \(h(s, c) \ge 2^k\) for all strings \(c\) that is a cyclic shift of \(t\). As the result can be very large, you should print the value modulo \(998\,244\,353\).
|
The first line of the input contains a single integer \(k\) (\(1 \le k \le 7\)).The second line of the input contains string \(s\) of size \(2^{k+1}\), consisting of the characters '0', '1' and '?'.The third line of the input contains string \(t\) of size \(2^{k+1}\), consisting of the characters '0', '1' and '?'.It is guaranteed that both strings \(s\) and \(t\) contains no more than \(2^k\) character '0' or '1'.
|
Print a single integer β the answer to the problem modulo \(998\,244\,353\).
|
In the first example, we can check that the condition \(h(s, c) \ge 2^k\) for all cyclic shift \(c\) of \(t\) is satisfied. In particular: for \(c = \mathtt{0101}\), \(h(s, c) = h(\mathtt{0110}, \mathtt{0101}) = 2 \ge 2^1\); for \(c = \mathtt{1010}\), \(h(s, c) = h(\mathtt{0110}, \mathtt{1010}) = 2 \ge 2^1\). In the second example, there exists a cycle shift \(c\) of \(t\) such that \(h(s, c) < 2^k\) (in particular, \(c = \mathtt{0011}\), and \(h(s, c) = h(\mathtt{0011}, \mathtt{0011}) = 0\)).In the third example, there are \(2\) possible ways to recover the missing characters: \(s = \mathtt{0101}\), \(t = \mathtt{0110}\); \(s = \mathtt{0011}\), \(t = \mathtt{0101}\). In the fourth example, there are \(3\) possible ways to recover the missing characters: \(s = \mathtt{00011110}\), \(t = \mathtt{01010101}\); \(s = \mathtt{00011011}\), \(t = \mathtt{01010101}\); \(s = \mathtt{00001111}\), \(t = \mathtt{01010101}\).
|
Input: 1 0011 0101 | Output: 1
|
Master
| 6 | 1,228 | 417 | 76 | 18 |
257 |
A
|
257A
|
A. Sockets
| 1,100 |
greedy; implementation; sortings
|
Vasya has got many devices that work on electricity. He's got n supply-line filters to plug the devices, the i-th supply-line filter has ai sockets.Overall Vasya has got m devices and k electrical sockets in his flat, he can plug the devices or supply-line filters directly. Of course, he can plug the supply-line filter to any other supply-line filter. The device (or the supply-line filter) is considered plugged to electricity if it is either plugged to one of k electrical sockets, or if it is plugged to some supply-line filter that is in turn plugged to electricity. What minimum number of supply-line filters from the given set will Vasya need to plug all the devices he has to electricity? Note that all devices and supply-line filters take one socket for plugging and that he can use one socket to plug either one device or one supply-line filter.
|
The first line contains three integers n, m, k (1 β€ n, m, k β€ 50) β the number of supply-line filters, the number of devices and the number of sockets that he can plug to directly, correspondingly. The second line contains n space-separated integers a1, a2, ..., an (1 β€ ai β€ 50) β number ai stands for the number of sockets on the i-th supply-line filter.
|
Print a single number β the minimum number of supply-line filters that is needed to plug all the devices to electricity. If it is impossible to plug all the devices even using all the supply-line filters, print -1.
|
In the first test case he can plug the first supply-line filter directly to electricity. After he plug it, he get 5 (3 on the supply-line filter and 2 remaining sockets for direct plugging) available sockets to plug. Thus, one filter is enough to plug 5 devices.One of the optimal ways in the second test sample is to plug the second supply-line filter directly and plug the fourth supply-line filter to one of the sockets in the second supply-line filter. Thus, he gets exactly 7 sockets, available to plug: one to plug to the electricity directly, 2 on the second supply-line filter, 4 on the fourth supply-line filter. There's no way he can plug 7 devices if he use one supply-line filter.
|
Input: 3 5 33 1 2 | Output: 1
|
Easy
| 3 | 856 | 356 | 214 | 2 |
1,761 |
F2
|
1761F2
|
F2. Anti-median (Hard Version)
| 3,500 |
combinatorics; dp; math
|
This is the hard version of the problem. The only difference between the two versions is the constraint on \(n\). You can make hacks only if all versions of the problem are solved.Let's call an array \(a\) of odd length \(2m+1\) (with \(m \ge 1\)) bad, if element \(a_{m+1}\) is equal to the median of this array. In other words, the array is bad if, after sorting it, the element at \(m+1\)-st position remains the same.Let's call a permutation \(p\) of integers from \(1\) to \(n\) anti-median, if every its subarray of odd length \(\ge 3\) is not bad.You are already given values of some elements of the permutation. Find the number of ways to set unknown values to obtain an anti-median permutation. As this number can be very large, find 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 description of test cases follows.The first line of each test case contains a single integer \(n\) \((2 \le n \le 10^6)\) β the length of the permutation.The second line of each test case contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le n\), or \(p_i = -1\)) β the elements of the permutation. If \(p_i \neq -1\), it's given, else it's unknown. It's guaranteed that if for some \(i \neq j\) holds \(p_i \neq -1, p_j \neq -1\), then \(p_i \neq p_j\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, output a single integer β the number of ways to set unknown values to obtain an anti-median permutation, modulo \(10^9+7\).
|
In the first test case, both \([1, 2]\) and \([2, 1]\) are anti-median.In the second test case, permutations \([1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2]\) are anti-median. The remaining two permutations, \([1, 2, 3]\), \([3, 2, 1]\), are bad arrays on their own, as their median, \(2\), is in their middle.In the third test case, \([1, 2, 3, 4]\) isn't anti-median, as it contains bad subarray \([1, 2, 3]\).In the fourth test case, the only anti-median array you can get is \([5, 6, 3, 4, 1, 2]\).
|
Input: 52-1 -13-1 -1 -141 2 3 46-1 -1 3 4 -1 -18-1 -1 -1 -1 -1 -1 -1 -1 | Output: 2 4 0 1 316
|
Master
| 3 | 764 | 653 | 143 | 17 |
840 |
B
|
840B
|
B. Leha and another game about graph
| 2,100 |
constructive algorithms; data structures; dfs and similar; dp; graphs
|
Leha plays a computer game, where is on each level is given a connected graph with n vertices and m edges. Graph can contain multiple edges, but can not contain self loops. Each vertex has an integer di, which can be equal to 0, 1 or - 1. To pass the level, he needs to find a Β«goodΒ» subset of edges of the graph or say, that it doesn't exist. Subset is called Β«goodΒ», if by by leaving only edges from this subset in the original graph, we obtain the following: for every vertex i, di = - 1 or it's degree modulo 2 is equal to di. Leha wants to pass the game as soon as possible and ask you to help him. In case of multiple correct answers, print any of them.
|
The first line contains two integers n, m (1 β€ n β€ 3Β·105, n - 1 β€ m β€ 3Β·105) β number of vertices and edges.The second line contains n integers d1, d2, ..., dn ( - 1 β€ di β€ 1) β numbers on the vertices.Each of the next m lines contains two integers u and v (1 β€ u, v β€ n) β edges. It's guaranteed, that graph in the input is connected.
|
Print - 1 in a single line, if solution doesn't exist. Otherwise in the first line k β number of edges in a subset. In the next k lines indexes of edges. Edges are numerated in order as they are given in the input, starting from 1.
|
In the first sample we have single vertex without edges. It's degree is 0 and we can not get 1.
|
Input: 1 01 | Output: -1
|
Hard
| 5 | 659 | 335 | 231 | 8 |
1,114 |
C
|
1114C
|
C. Trailing Loves (or L'oeufs?)
| 1,700 |
brute force; implementation; math; number theory
|
The number ""zero"" is called ""love"" (or ""l'oeuf"" to be precise, literally means ""egg"" in French), for example when denoting the zero score in a game of tennis. Aki is fond of numbers, especially those with trailing zeros. For example, the number \(9200\) has two trailing zeros. Aki thinks the more trailing zero digits a number has, the prettier it is.However, Aki believes, that the number of trailing zeros of a number is not static, but depends on the base (radix) it is represented in. Thus, he considers a few scenarios with some numbers and bases. And now, since the numbers he used become quite bizarre, he asks you to help him to calculate the beauty of these numbers.Given two integers \(n\) and \(b\) (in decimal notation), your task is to calculate the number of trailing zero digits in the \(b\)-ary (in the base/radix of \(b\)) representation of \(n\,!\) (factorial of \(n\)).
|
The only line of the input contains two integers \(n\) and \(b\) (\(1 \le n \le 10^{18}\), \(2 \le b \le 10^{12}\)).
|
Print an only integer β the number of trailing zero digits in the \(b\)-ary representation of \(n!\)
|
In the first example, \(6!_{(10)} = 720_{(10)} = 880_{(9)}\).In the third and fourth example, \(5!_{(10)} = 120_{(10)} = 1111000_{(2)}\).The representation of the number \(x\) in the \(b\)-ary base is \(d_1, d_2, \ldots, d_k\) if \(x = d_1 b^{k - 1} + d_2 b^{k - 2} + \ldots + d_k b^0\), where \(d_i\) are integers and \(0 \le d_i \le b - 1\). For example, the number \(720\) from the first example is represented as \(880_{(9)}\) since \(720 = 8 \cdot 9^2 + 8 \cdot 9 + 0 \cdot 1\).You can read more about bases here.
|
Input: 6 9 | Output: 1
|
Medium
| 4 | 897 | 116 | 100 | 11 |
246 |
E
|
246E
|
E. Blood Cousins Return
| 2,400 |
binary search; data structures; dfs and similar; dp; sortings
|
Polycarpus got hold of a family tree. The found tree describes the family relations of n people, numbered from 1 to n. Every person in this tree has at most one direct ancestor. Also, each person in the tree has a name, the names are not necessarily unique.We call the man with a number a a 1-ancestor of the man with a number b, if the man with a number a is a direct ancestor of the man with a number b.We call the man with a number a a k-ancestor (k > 1) of the man with a number b, if the man with a number b has a 1-ancestor, and the man with a number a is a (k - 1)-ancestor of the 1-ancestor of the man with a number b.In the tree the family ties do not form cycles. In other words there isn't a person who is his own direct or indirect ancestor (that is, who is an x-ancestor of himself, for some x, x > 0).We call a man with a number a the k-son of the man with a number b, if the man with a number b is a k-ancestor of the man with a number a.Polycarpus is very much interested in how many sons and which sons each person has. He took a piece of paper and wrote m pairs of numbers vi, ki. Help him to learn for each pair vi, ki the number of distinct names among all names of the ki-sons of the man with number vi.
|
The first line of the input contains a single integer n (1 β€ n β€ 105) β the number of people in the tree. Next n lines contain the description of people in the tree. The i-th line contains space-separated string si and integer ri (0 β€ ri β€ n), where si is the name of the man with a number i, and ri is either the number of the direct ancestor of the man with a number i or 0, if the man with a number i has no direct ancestor. The next line contains a single integer m (1 β€ m β€ 105) β the number of Polycarpus's records. Next m lines contain space-separated pairs of integers. The i-th line contains integers vi, ki (1 β€ vi, ki β€ n).It is guaranteed that the family relationships do not form cycles. The names of all people are non-empty strings, consisting of no more than 20 lowercase English letters.
|
Print m whitespace-separated integers β the answers to Polycarpus's records. Print the answers to the records in the order, in which the records occur in the input.
|
Input: 6pasha 0gerald 1gerald 1valera 2igor 3olesya 151 11 21 33 16 1 | Output: 22010
|
Expert
| 5 | 1,224 | 804 | 164 | 2 |
|
1,023 |
A
|
1023A
|
A. Single Wildcard Pattern Matching
| 1,200 |
brute force; implementation; strings
|
You are given two strings \(s\) and \(t\). The string \(s\) consists of lowercase Latin letters and at most one wildcard character '*', the string \(t\) consists only of lowercase Latin letters. The length of the string \(s\) equals \(n\), the length of the string \(t\) equals \(m\).The wildcard character '*' in the string \(s\) (if any) can be replaced with an arbitrary sequence (possibly empty) of lowercase Latin letters. No other character of \(s\) can be replaced with anything. If it is possible to replace a wildcard character '*' in \(s\) to obtain a string \(t\), then the string \(t\) matches the pattern \(s\).For example, if \(s=\)""aba*aba"" then the following strings match it ""abaaba"", ""abacaba"" and ""abazzzaba"", but the following strings do not match: ""ababa"", ""abcaaba"", ""codeforces"", ""aba1aba"", ""aba?aba"".If the given string \(t\) matches the given string \(s\), print ""YES"", otherwise print ""NO"".
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the length of the string \(s\) and the length of the string \(t\), respectively.The second line contains string \(s\) of length \(n\), which consists of lowercase Latin letters and at most one wildcard character '*'.The third line contains string \(t\) of length \(m\), which consists only of lowercase Latin letters.
|
Print ""YES"" (without quotes), if you can obtain the string \(t\) from the string \(s\). Otherwise print ""NO"" (without quotes).
|
In the first example a wildcard character '*' can be replaced with a string ""force"". So the string \(s\) after this replacement is ""codeforces"" and the answer is ""YES"".In the second example a wildcard character '*' can be replaced with an empty string. So the string \(s\) after this replacement is ""vkcup"" and the answer is ""YES"".There is no wildcard character '*' in the third example and the strings ""v"" and ""k"" are different so the answer is ""NO"".In the fourth example there is no such replacement of a wildcard character '*' that you can obtain the string \(t\) so the answer is ""NO"".
|
Input: 6 10code*scodeforces | Output: YES
|
Easy
| 3 | 938 | 406 | 130 | 10 |
1,592 |
A
|
1592A
|
A. Gamer Hemose
| 800 |
binary search; greedy; math; sortings
|
One day, Ahmed_Hossam went to Hemose and said ""Let's solve a gym contest!"". Hemose didn't want to do that, as he was playing Valorant, so he came up with a problem and told it to Ahmed to distract him. Sadly, Ahmed can't solve it... Could you help him?There is an Agent in Valorant, and he has \(n\) weapons. The \(i\)-th weapon has a damage value \(a_i\), and the Agent will face an enemy whose health value is \(H\).The Agent will perform one or more moves until the enemy dies.In one move, he will choose a weapon and decrease the enemy's health by its damage value. The enemy will die when his health will become less than or equal to \(0\). However, not everything is so easy: the Agent can't choose the same weapon for \(2\) times in a row.What is the minimum number of times that the Agent will need to use the weapons to kill the enemy?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) \((1 \leq t \leq 10^5)\). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(H\) \((2 \leq n \leq 10^3, 1 \leq H \leq 10^9)\) β the number of available weapons and the initial health value of the enemy.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 damage values of the weapons.It's guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print a single integer β the minimum number of times that the Agent will have to use the weapons to kill the enemy.
|
In the first test case, the Agent can use the second weapon, making health value of the enemy equal to \(4-7=-3\). \(-3 \le 0\), so the enemy is dead, and using weapon \(1\) time was enough.In the second test case, the Agent can use the first weapon first, and then the second one. After this, the health of enemy will drop to \(6-4-2 = 0\), meaning he would be killed after using weapons \(2\) times.In the third test case, the Agent can use the weapons in order (third, first, third), decreasing the health value of enemy to \(11 - 7 - 2 - 7 = -5\) after using the weapons \(3\) times. Note that we can't kill the enemy by using the third weapon twice, as even though \(11-7-7<0\), it's not allowed to use the same weapon twice in a row.
|
Input: 3 2 4 3 7 2 6 4 2 3 11 2 1 7 | Output: 1 2 3
|
Beginner
| 4 | 846 | 587 | 135 | 15 |
1,912 |
I
|
1912I
| 3,300 |
geometry; math; two pointers
|
Master
| 3 | 0 | 0 | 0 | 19 |
||||||
180 |
B
|
180B
|
B. Divisibility Rules
| 2,300 |
math; number theory
|
Vasya studies divisibility rules at school. Here are some of them: Divisibility by 2. A number is divisible by 2 if and only if its last digit is divisible by 2 or in other words, is even. Divisibility by 3. A number is divisible by 3 if and only if the sum of its digits is divisible by 3. Divisibility by 4. A number is divisible by 4 if and only if its last two digits form a number that is divisible by 4. Divisibility by 5. A number is divisible by 5 if and only if its last digit equals 5 or 0. Divisibility by 6. A number is divisible by 6 if and only if it is divisible by 2 and 3 simultaneously (that is, if the last digit is even and the sum of all digits is divisible by 3). Divisibility by 7. Vasya doesn't know such divisibility rule. Divisibility by 8. A number is divisible by 8 if and only if its last three digits form a number that is divisible by 8. Divisibility by 9. A number is divisible by 9 if and only if the sum of its digits is divisible by 9. Divisibility by 10. A number is divisible by 10 if and only if its last digit is a zero. Divisibility by 11. A number is divisible by 11 if and only if the sum of digits on its odd positions either equals to the sum of digits on the even positions, or they differ in a number that is divisible by 11.Vasya got interested by the fact that some divisibility rules resemble each other. In fact, to check a number's divisibility by 2, 4, 5, 8 and 10 it is enough to check fulfiling some condition for one or several last digits. Vasya calls such rules the 2-type rules.If checking divisibility means finding a sum of digits and checking whether the sum is divisible by the given number, then Vasya calls this rule the 3-type rule (because it works for numbers 3 and 9).If we need to find the difference between the sum of digits on odd and even positions and check whether the difference is divisible by the given divisor, this rule is called the 11-type rule (it works for number 11).In some cases we should divide the divisor into several factors and check whether rules of different types (2-type, 3-type or 11-type) work there. For example, for number 6 we check 2-type and 3-type rules, for number 66 we check all three types. Such mixed divisibility rules are called 6-type rules. And finally, there are some numbers for which no rule works: neither 2-type, nor 3-type, nor 11-type, nor 6-type. The least such number is number 7, so we'll say that in such cases the mysterious 7-type rule works, the one that Vasya hasn't discovered yet. Vasya's dream is finding divisibility rules for all possible numbers. He isn't going to stop on the decimal numbers only. As there are quite many numbers, ha can't do it all by himself. Vasya asked you to write a program that determines the divisibility rule type in the b-based notation for the given divisor d.
|
The first input line contains two integers b and d (2 β€ b, d β€ 100) β the notation system base and the divisor. Both numbers are given in the decimal notation.
|
On the first output line print the type of the rule in the b-based notation system, where the divisor is d: ""2-type"", ""3-type"", ""11-type"", ""6-type"" or ""7-type"". If there are several such types, print the one that goes earlier in the given sequence. If a number belongs to the 2-type, print on the second line the least number of the last b-based digits that we will need to use to check the divisibility.
|
The divisibility rule for number 3 in binary notation looks as follows: ""A number is divisible by 3 if and only if the sum of its digits that occupy the even places differs from the sum of digits that occupy the odd places, in a number that is divisible by 3"". That's an 11-type rule. For example, 2110 = 101012. For it the sum of digits on odd positions equals 1 + 1 + 1 = 3, an on even positions β 0 + 0 = 0. The rule works and the number is divisible by 3. In some notations a number can fit into the 3-type rule and the 11-type rule. In this case the correct answer is ""3-type"".
|
Input: 10 10 | Output: 2-type1
|
Expert
| 2 | 2,823 | 159 | 414 | 1 |
913 |
H
|
913H
|
H. Don't Exceed
| 3,400 |
math; probabilities
|
You generate real numbers s1, s2, ..., sn as follows: s0 = 0; si = si - 1 + ti, where ti is a real number chosen independently uniformly at random between 0 and 1, inclusive. You are given real numbers x1, x2, ..., xn. You are interested in the probability that si β€ xi is true for all i simultaneously.It can be shown that this can be represented as , where P and Q are coprime integers, and . Print the value of PΒ·Q - 1 modulo 998244353.
|
The first line contains integer n (1 β€ n β€ 30).The next n lines contain real numbers x1, x2, ..., xn, given with at most six digits after the decimal point (0 < xi β€ n).
|
Print a single integer, the answer to the problem.
|
In the first example, the sought probability is 1 since the sum of i real numbers which don't exceed 1 doesn't exceed i.In the second example, the probability is x1 itself.In the third example, the sought probability is 3 / 8.
|
Input: 41.0023.0000004.0 | Output: 1
|
Master
| 2 | 439 | 169 | 50 | 9 |
1,857 |
B
|
1857B
|
B. Maximum Rounding
| 1,100 |
greedy; implementation; math
|
Given a natural number \(x\). You can perform the following operation: choose a positive integer \(k\) and round \(x\) to the \(k\)-th digit Note that the positions are numbered from right to left, starting from zero. If the number has \(k\) digits, it is considered that the digit at the \(k\)-th position is equal to \(0\).The rounding is done as follows: if the digit at the \((k-1)\)-th position is greater than or equal to \(5\), then the digit at the \(k\)-th position is increased by \(1\), otherwise the digit at the \(k\)-th position remains unchanged (mathematical rounding is used). if before the operations the digit at the \(k\)-th position was \(9\), and it should be increased by \(1\), then we search for the least position \(k'\) (\(k'>k\)), where the digit at the \(k'\)-th position is less than \(9\) and add \(1\) to the digit at the \(k'\)-th position. Then we assign \(k=k'\). after that, all digits which positions are less than \(k\) are replaced with zeros.Your task is to make \(x\) as large as possible, if you can perform the operation as many times as you want.For example, if \(x\) is equal to \(3451\), then if you choose consecutively: \(k=1\), then after the operation \(x\) will become \(3450\) \(k=2\), then after the operation \(x\) will become \(3500\) \(k=3\), then after the operation \(x\) will become \(4000\) \(k=4\), then after the operation \(x\) will become \(0\) To maximize the answer, you need to choose \(k=2\) first, and then \(k=3\), then the number will become \(4000\).
|
The first line contains a single integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases.Each test case consists of positive integer \(x\) with a length of up to \(2 \cdot 10^5\). It is guaranteed that there are no leading zeros in the integer.It is guaranteed that the sum of the lengths of all integers \(x\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each set of input data, output the maximum possible value of \(x\) after the operations. The number should not have leading zeros in its representation.
|
In the first sample, it is better not to perform any operations.In the second sample, you can perform one operation and obtain \(10\).In the third sample, you can choose \(k=1\) or \(k=2\). In both cases the answer will be \(100\).
|
Input: 101599913198020444204456094741986040862016542130810467 | Output: 1 10 100 1000 2000 20444 21000 100000 420000 41000000000000000000
|
Easy
| 3 | 1,522 | 370 | 156 | 18 |
720 |
B
|
720B
|
B. Cactusophobia
| 2,400 |
dfs and similar; flows
|
Tree is a connected undirected graph that has no cycles. Edge cactus is a connected undirected graph without loops and parallel edges, such that each edge belongs to at most one cycle.Vasya has an edge cactus, each edge of this graph has some color.Vasya would like to remove the minimal number of edges in such way that his cactus turned to a tree. Vasya wants to make it in such a way that there were edges of as many different colors in the resulting tree, as possible. Help him to find how many different colors can the resulting tree have.
|
The first line contains two integers: n, m (2 β€ n β€ 10 000) β the number of vertices and the number of edges in Vasya's graph, respectively.The following m lines contain three integers each: u, v, c (1 β€ u, v β€ n, u β v, 1 β€ c β€ m) β the numbers of vertices connected by the corresponding edge, and its color. It is guaranteed that the described graph is indeed an edge cactus.
|
Output one integer: the maximal number of different colors that the resulting tree can have.
|
Input: 4 41 2 42 3 13 4 24 2 3 | Output: 3
|
Expert
| 2 | 544 | 377 | 92 | 7 |
|
2,038 |
N
|
2038N
|
N. Fixing the Expression
| 800 |
implementation
|
An expression is a string consisting of three characters, where the first and the last characters are digits (from \(0\) to \(9\)), and the middle character is a comparison symbol (<, = or >).An expression is true if the comparison symbol matches the digits (for example, if the first digit is strictly less than the last digit, the comparison symbol should be <).For example, the expressions 1<3, 4>2, 0=0 are true, while 5>5, 7<3 are not.You are given a string \(s\), which is an expression. Change as few characters as possible so that \(s\) becomes a true expression. Note that if \(s\) is already true, you should leave it as it is.
|
The first line contains one integer \(t\) (\(1 \le t \le 300\)) β the number of test cases.Each test case consists of one line containing the string \(s\) (\(|s| = 3\), the first and the last characters of \(s\) are digits, the second character is a comparison symbol).
|
For each test case, print a string consisting of \(3\) characters β a true expression which can be obtained by changing as few characters as possible in \(s\). If there are multiple answers, print any of them.
|
Input: 53<73>78=90=05<3 | Output: 3<7 8>7 8<9 0=0 0<3
|
Beginner
| 1 | 637 | 269 | 209 | 20 |
|
248 |
D
|
248D
|
D. Sweets for Everyone!
| 2,300 |
binary search; greedy; implementation
|
For he knew every Who down in Whoville beneath, Was busy now, hanging a mistletoe wreath. ""And they're hanging their stockings!"" he snarled with a sneer, ""Tomorrow is Christmas! It's practically here!""Dr. Suess, How The Grinch Stole ChristmasChristmas celebrations are coming to Whoville. Cindy Lou Who and her parents Lou Lou Who and Betty Lou Who decided to give sweets to all people in their street. They decided to give the residents of each house on the street, one kilogram of sweets. So they need as many kilos of sweets as there are homes on their street.The street, where the Lou Who family lives can be represented as n consecutive sections of equal length. You can go from any section to a neighbouring one in one unit of time. Each of the sections is one of three types: an empty piece of land, a house or a shop. Cindy Lou and her family can buy sweets in a shop, but no more than one kilogram of sweets in one shop (the vendors care about the residents of Whoville not to overeat on sweets).After the Lou Who family leave their home, they will be on the first section of the road. To get to this section of the road, they also require one unit of time. We can assume that Cindy and her mom and dad can carry an unlimited number of kilograms of sweets. Every time they are on a house section, they can give a kilogram of sweets to the inhabitants of the house, or they can simply move to another section. If the family have already given sweets to the residents of a house, they can't do it again. Similarly, if they are on the shop section, they can either buy a kilo of sweets in it or skip this shop. If they've bought a kilo of sweets in a shop, the seller of the shop remembered them and the won't sell them a single candy if they come again. The time to buy and give sweets can be neglected. The Lou Whos do not want the people of any house to remain without food.The Lou Whos want to spend no more than t time units of time to give out sweets, as they really want to have enough time to prepare for the Christmas celebration. In order to have time to give all the sweets, they may have to initially bring additional k kilos of sweets.Cindy Lou wants to know the minimum number of k kilos of sweets they need to take with them, to have time to give sweets to the residents of each house in their street.Your task is to write a program that will determine the minimum possible value of k.
|
The first line of the input contains two space-separated integers n and t (2 β€ n β€ 5Β·105, 1 β€ t β€ 109). The second line of the input contains n characters, the i-th of them equals ""H"" (if the i-th segment contains a house), ""S"" (if the i-th segment contains a shop) or ""."" (if the i-th segment doesn't contain a house or a shop). It is guaranteed that there is at least one segment with a house.
|
If there isn't a single value of k that makes it possible to give sweets to everybody in at most t units of time, print in a single line ""-1"" (without the quotes). Otherwise, print on a single line the minimum possible value of k.
|
In the first example, there are as many stores, as houses. If the family do not take a single kilo of sweets from home, in order to treat the inhabitants of the first house, they will need to make at least one step back, and they have absolutely no time for it. If they take one kilogram of sweets, they won't need to go back.In the second example, the number of shops is equal to the number of houses and plenty of time. Available at all stores passing out candy in one direction and give them when passing in the opposite direction.In the third example, the shops on the street are fewer than houses. The Lou Whos have to take the missing number of kilograms of sweets with them from home.
|
Input: 6 6HSHSHS | Output: 1
|
Expert
| 3 | 2,410 | 401 | 232 | 2 |
89 |
A
|
89A
|
A. Robbery
| 1,800 |
greedy
|
It is nighttime and Joe the Elusive got into the country's main bank's safe. The safe has n cells positioned in a row, each of them contains some amount of diamonds. Let's make the problem more comfortable to work with and mark the cells with positive numbers from 1 to n from the left to the right.Unfortunately, Joe didn't switch the last security system off. On the plus side, he knows the way it works.Every minute the security system calculates the total amount of diamonds for each two adjacent cells (for the cells between whose numbers difference equals 1). As a result of this check we get an n - 1 sums. If at least one of the sums differs from the corresponding sum received during the previous check, then the security system is triggered.Joe can move the diamonds from one cell to another between the security system's checks. He manages to move them no more than m times between two checks. One of the three following operations is regarded as moving a diamond: moving a diamond from any cell to any other one, moving a diamond from any cell to Joe's pocket, moving a diamond from Joe's pocket to any cell. Initially Joe's pocket is empty, and it can carry an unlimited amount of diamonds. It is considered that before all Joe's actions the system performs at least one check.In the morning the bank employees will come, which is why Joe has to leave the bank before that moment. Joe has only k minutes left before morning, and on each of these k minutes he can perform no more than m operations. All that remains in Joe's pocket, is considered his loot.Calculate the largest amount of diamonds Joe can carry with him. Don't forget that the security system shouldn't be triggered (even after Joe leaves the bank) and Joe should leave before morning.
|
The first line contains integers n, m and k (1 β€ n β€ 104, 1 β€ m, k β€ 109). The next line contains n numbers. The i-th number is equal to the amount of diamonds in the i-th cell β it is an integer from 0 to 105.
|
Print a single number β the maximum number of diamonds Joe can steal.
|
In the second sample Joe can act like this:The diamonds' initial positions are 4 1 3.During the first period of time Joe moves a diamond from the 1-th cell to the 2-th one and a diamond from the 3-th cell to his pocket.By the end of the first period the diamonds' positions are 3 2 2. The check finds no difference and the security system doesn't go off.During the second period Joe moves a diamond from the 3-rd cell to the 2-nd one and puts a diamond from the 1-st cell to his pocket.By the end of the second period the diamonds' positions are 2 3 1. The check finds no difference again and the security system doesn't go off.Now Joe leaves with 2 diamonds in his pocket.
|
Input: 2 3 12 3 | Output: 0
|
Medium
| 1 | 1,763 | 210 | 69 | 0 |
1,506 |
E
|
1506E
|
E. Restoring the Permutation
| 1,500 |
constructive algorithms; implementation
|
A permutation is a sequence of \(n\) integers from \(1\) to \(n\), in which all numbers occur exactly once. For example, \([1]\), \([3, 5, 2, 1, 4]\), \([1, 3, 2]\) are permutations, and \([2, 3, 2]\), \([4, 3, 1]\), \([0]\) are not.Polycarp was presented with a permutation \(p\) of numbers from \(1\) to \(n\). However, when Polycarp came home, he noticed that in his pocket, the permutation \(p\) had turned into an array \(q\) according to the following rule: \(q_i = \max(p_1, p_2, \ldots, p_i)\). Now Polycarp wondered what lexicographically minimal and lexicographically maximal permutations could be presented to him.An array \(a\) of length \(n\) is lexicographically smaller than an array \(b\) of length \(n\) if there is an index \(i\) (\(1 \le i \le n\)) such that the first \(i-1\) elements of arrays \(a\) and \(b\) are the same, and the \(i\)-th element of the array \(a\) is less than the \(i\)-th element of the array \(b\). For example, the array \(a=[1, 3, 2, 3]\) is lexicographically smaller than the array \(b=[1, 3, 4, 2]\).For example, if \(n=7\) and \(p=[3, 2, 4, 1, 7, 5, 6]\), then \(q=[3, 3, 4, 4, 7, 7, 7]\) and the following permutations could have been as \(p\) initially: \([3, 1, 4, 2, 7, 5, 6]\) (lexicographically minimal permutation); \([3, 1, 4, 2, 7, 6, 5]\); \([3, 2, 4, 1, 7, 5, 6]\); \([3, 2, 4, 1, 7, 6, 5]\) (lexicographically maximum permutation). For a given array \(q\), find the lexicographically minimal and lexicographically maximal permutations that could have been originally presented to Polycarp.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(q_1, q_2, \ldots, q_n\) (\(1 \le q_i \le n\)).It is guaranteed that the array \(q\) was obtained by applying the rule from the statement to some permutation \(p\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output two lines: on the first line output \(n\) integers β lexicographically minimal permutation that could have been originally presented to Polycarp; on the second line print \(n\) integers β lexicographically maximal permutation that could have been originally presented to Polycarp;
|
Input: 4 7 3 3 4 4 7 7 7 4 1 2 3 4 7 3 4 5 5 5 7 7 1 1 | Output: 3 1 4 2 7 5 6 3 2 4 1 7 6 5 1 2 3 4 1 2 3 4 3 4 5 1 2 7 6 3 4 5 2 1 7 6 1 1
|
Medium
| 2 | 1,550 | 501 | 307 | 15 |
|
178 |
B1
|
178B1
|
B1. Greedy Merchants
| 1,600 |
In ABBYY a wonderful Smart Beaver lives. This time, he began to study history. When he read about the Roman Empire, he became interested in the life of merchants.The Roman Empire consisted of n cities numbered from 1 to n. It also had m bidirectional roads numbered from 1 to m. Each road connected two different cities. Any two cities were connected by no more than one road.We say that there is a path between cities c1 and c2 if there exists a finite sequence of cities t1, t2, ..., tp (p β₯ 1) such that: t1 = c1 tp = c2 for any i (1 β€ i < p), cities ti and ti + 1 are connected by a road We know that there existed a path between any two cities in the Roman Empire.In the Empire k merchants lived numbered from 1 to k. For each merchant we know a pair of numbers si and li, where si is the number of the city where this merchant's warehouse is, and li is the number of the city where his shop is. The shop and the warehouse could be located in different cities, so the merchants had to deliver goods from the warehouse to the shop.Let's call a road important for the merchant if its destruction threatens to ruin the merchant, that is, without this road there is no path from the merchant's warehouse to his shop. Merchants in the Roman Empire are very greedy, so each merchant pays a tax (1 dinar) only for those roads which are important for him. In other words, each merchant pays di dinars of tax, where di (di β₯ 0) is the number of roads important for the i-th merchant.The tax collection day came in the Empire. The Smart Beaver from ABBYY is very curious by nature, so he decided to count how many dinars each merchant had paid that day. And now he needs your help.
|
The first input line contains two integers n and m, separated by a space, n is the number of cities, and m is the number of roads in the empire.The following m lines contain pairs of integers ai, bi (1 β€ ai, bi β€ n, ai β bi), separated by a space β the numbers of cities connected by the i-th road. It is guaranteed that any two cities are connected by no more than one road and that there exists a path between any two cities in the Roman Empire.The next line contains a single integer k β the number of merchants in the empire.The following k lines contain pairs of integers si, li (1 β€ si, li β€ n), separated by a space, β si is the number of the city in which the warehouse of the i-th merchant is located, and li is the number of the city in which the shop of the i-th merchant is located.The input limitations for getting 20 points are: 1 β€ n β€ 200 1 β€ m β€ 200 1 β€ k β€ 200 The input limitations for getting 50 points are: 1 β€ n β€ 2000 1 β€ m β€ 2000 1 β€ k β€ 2000 The input limitations for getting 100 points are: 1 β€ n β€ 105 1 β€ m β€ 105 1 β€ k β€ 105
|
Print exactly k lines, the i-th line should contain a single integer di β the number of dinars that the i-th merchant paid.
|
The given sample is illustrated in the figure below. Let's describe the result for the first merchant. The merchant's warehouse is located in city 1 and his shop is in city 5. Let us note that if either road, (1, 2) or (2, 3) is destroyed, there won't be any path between cities 1 and 5 anymore. If any other road is destroyed, the path will be preserved. That's why for the given merchant the answer is 2.
|
Input: 7 81 22 33 44 55 65 73 54 741 52 42 64 7 | Output: 2120
|
Medium
| 0 | 1,676 | 1,052 | 123 | 1 |
|
1,773 |
B
|
1773B
|
B. BinCoin
| 2,200 |
binary search; divide and conquer; hashing; implementation; probabilities; trees
|
There are \(n\) employees in the BinCoin company numbered from \(1\) to \(n\). The subordination structure in this company is a rooted tree. In other words: There is one CEO in the company β the main boss. Each other employee has exactly one direct superior. There are no cycles in the subordination structure. Moreover, due to the inexplicable love of the CEO of BinCoin for all the binary stuff, the subordination structure in the company is a binary rooted tree. That means each employee is directly superior to exactly zero or two other employees.In the CEO's opinion, working in this company is almost as dangerous as in mines. So, employees should sign the waiver of claims sometimes. This process happens in the following way. Initially, CEO takes the journal, then recursively the following procedure is performed: If an employee that holds the journal does not have any subordinates, they sign the waiver in the journal and give it back to their superior. The procedure stops if that was the CEO, who has no superior. Otherwise they choose one of two of their direct subordinates uniformly at random and give the journal to one of them; when they get the journal back, they sign it; and then they give it to another direct subordinate; when they get it back again, they give it back to their superior. The procedure stops if that was the CEO, who has no superior. All random choices are independent.One day, the CEO realized that they could not remember the subordination tree. Fortunately, they have the journal with \(k\) records. Each record is a sequence of employees in the order they've signed in a journal.Help CEO restore the subordination tree.
|
The first line contains two integers \(n\) and \(k\) β the number of employees and the number of records in the journal (\(1 \le n \le 999\); \(50 \le k \le 100\)).Each of the next \(k\) lines contains a permutation of integers from \(1\) to \(n\) β the order of employees in the corresponding record.It is guaranteed that the input was obtained as described in the statement with a real random choice.
|
Output \(n\) integers \(p_i\). If \(i\) is a CEO, then \(p_i\) should be \(-1\). Otherwise, \(p_i\) should be the index of the direct superior of \(i\)-th employee.Your output should correspond to a binary rooted tree. If there are several trees satisfying the input, you can output any one of them.
|
In order to fit on the page, several consecutive lines in the examples were joined into one. The real inputs follow the input description.
|
Input: 3 50 1 2 3 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 1 2 3 1 2 3 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 3 2 1 1 2 3 1 2 3 3 2 1 1 2 3 1 2 3 1 2 3 1 2 3 3 2 1 1 2 3 3 2 1 3 2 1 1 2 3 3 2 1 1 2 3 3 2 1 3 2 1 1 2 3 1 2 3 3 2 1 1 2 3 3 2 1 3 2 1 3 2 1 1 2 3 1 2 3 3 2 1 3 2 1 | Output: 2 -1 2
|
Hard
| 6 | 1,662 | 402 | 299 | 17 |
1,601 |
A
|
1601A
|
A. Array Elimination
| 1,300 |
bitmasks; greedy; math; number theory
|
You are given array \(a_1, a_2, \ldots, a_n\), consisting of non-negative integers.Let's define operation of ""elimination"" with integer parameter \(k\) (\(1 \leq k \leq n\)) as follows: Choose \(k\) distinct array indices \(1 \leq i_1 < i_2 < \ldots < i_k \le n\). Calculate \(x = a_{i_1} ~ \& ~ a_{i_2} ~ \& ~ \ldots ~ \& ~ a_{i_k}\), where \(\&\) denotes the bitwise AND operation (notes section contains formal definition). Subtract \(x\) from each of \(a_{i_1}, a_{i_2}, \ldots, a_{i_k}\); all other elements remain untouched. Find all possible values of \(k\), such that it's possible to make all elements of array \(a\) equal to \(0\) using a finite number of elimination operations with parameter \(k\). It can be proven that exists at least one possible \(k\) for any array \(a\).Note that you firstly choose \(k\) and only after that perform elimination operations with value \(k\) you've chosen initially.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). Description of the test cases follows.The first line of each test case contains one integer \(n\) (\(1 \leq n \leq 200\,000\)) β the length of array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i < 2^{30}\)) β array \(a\) itself.It's guaranteed that the sum of \(n\) over all test cases doesn't exceed \(200\,000\).
|
For each test case, print all values \(k\), such that it's possible to make all elements of \(a\) equal to \(0\) in a finite number of elimination operations with the given parameter \(k\).Print them in increasing order.
|
In the first test case: If \(k = 1\), we can make four elimination operations with sets of indices \(\{1\}\), \(\{2\}\), \(\{3\}\), \(\{4\}\). Since \(\&\) of one element is equal to the element itself, then for each operation \(x = a_i\), so \(a_i - x = a_i - a_i = 0\). If \(k = 2\), we can make two elimination operations with, for example, sets of indices \(\{1, 3\}\) and \(\{2, 4\}\): \(x = a_1 ~ \& ~ a_3\) \(=\) \(a_2 ~ \& ~ a_4\) \(=\) \(4 ~ \& ~ 4 = 4\). For both operations \(x = 4\), so after the first operation \(a_1 - x = 0\) and \(a_3 - x = 0\), and after the second operation β \(a_2 - x = 0\) and \(a_4 - x = 0\). If \(k = 3\), it's impossible to make all \(a_i\) equal to \(0\). After performing the first operation, we'll get three elements equal to \(0\) and one equal to \(4\). After that, all elimination operations won't change anything, since at least one chosen element will always be equal to \(0\). If \(k = 4\), we can make one operation with set \(\{1, 2, 3, 4\}\), because \(x = a_1 ~ \& ~ a_2 ~ \& ~ a_3 ~ \& ~ a_4\) \(= 4\). In the second test case, if \(k = 2\) then we can make the following elimination operations: Operation with indices \(\{1, 3\}\): \(x = a_1 ~ \& ~ a_3\) \(=\) \(13 ~ \& ~ 25 = 9\). \(a_1 - x = 13 - 9 = 4\) and \(a_3 - x = 25 - 9 = 16\). Array \(a\) will become equal to \([4, 7, 16, 19]\). Operation with indices \(\{3, 4\}\): \(x = a_3 ~ \& ~ a_4\) \(=\) \(16 ~ \& ~ 19 = 16\). \(a_3 - x = 16 - 16 = 0\) and \(a_4 - x = 19 - 16 = 3\). Array \(a\) will become equal to \([4, 7, 0, 3]\). Operation with indices \(\{2, 4\}\): \(x = a_2 ~ \& ~ a_4\) \(=\) \(7 ~ \& ~ 3 = 3\). \(a_2 - x = 7 - 3 = 4\) and \(a_4 - x = 3 - 3 = 0\). Array \(a\) will become equal to \([4, 4, 0, 0]\). Operation with indices \(\{1, 2\}\): \(x = a_1 ~ \& ~ a_2\) \(=\) \(4 ~ \& ~ 4 = 4\). \(a_1 - x = 4 - 4 = 0\) and \(a_2 - x = 4 - 4 = 0\). Array \(a\) will become equal to \([0, 0, 0, 0]\). Formal definition of bitwise AND:Let's define bitwise AND (\(\&\)) as follows. Suppose we have two non-negative integers \(x\) and \(y\), let's look at their binary representations (possibly, with leading zeroes): \(x_k \dots x_2 x_1 x_0\) and \(y_k \dots y_2 y_1 y_0\). Here, \(x_i\) is the \(i\)-th bit of number \(x\), and \(y_i\) is the \(i\)-th bit of number \(y\). Let \(r = x ~ \& ~ y\) is a result of operation \(\&\) on number \(x\) and \(y\). Then binary representation of \(r\) will be \(r_k \dots r_2 r_1 r_0\), where:$$$\( r_i = \begin{cases} 1, ~ \text{if} ~ x_i = 1 ~ \text{and} ~ y_i = 1 \\ 0, ~ \text{if} ~ x_i = 0 ~ \text{or} ~ y_i = 0 \end{cases} \)$$$
|
Input: 5 4 4 4 4 4 4 13 7 25 19 6 3 5 3 1 7 1 1 1 5 0 0 0 0 0 | Output: 1 2 4 1 2 1 1 1 2 3 4 5
|
Easy
| 4 | 917 | 493 | 220 | 16 |
1,662 |
I
|
1662I
|
I. Ice Cream Shop
| 0 |
brute force; implementation; sortings
|
On a beach there are \(n\) huts in a perfect line, hut \(1\) being at the left and hut \(i+1\) being \(100\) meters to the right of hut \(i\), for all \(1 \le i \le n - 1\). In hut \(i\) there are \(p_i\) people.There are \(m\) ice cream sellers, also aligned in a perfect line with all the huts. The \(i\)-th ice cream seller has their shop \(x_i\) meters to the right of the first hut. All ice cream shops are at distinct locations, but they may be at the same location as a hut.You want to open a new ice cream shop and you wonder what the best location for your shop is. You can place your ice cream shop anywhere on the beach (not necessarily at an integer distance from the first hut) as long as it is aligned with the huts and the other ice cream shops, even if there is already another ice cream shop or a hut at that location. You know that people would come to your shop only if it is strictly closer to their hut than any other ice cream shop.If every person living in the huts wants to buy exactly one ice cream, what is the maximum number of ice creams that you can sell if you place the shop optimally?
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 200\,000\), \(1 \le m \le 200\,000\)) β the number of huts and the number of ice cream sellers.The second line contains \(n\) integers \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le 10^9\)) β the number of people in each hut.The third line contains \(m\) integers \(x_1, x_2, \ldots, x_m\) (\(0 \le x_i \le 10^9\), \(x_i \ne x_j\) for \(i \ne j\)) β the location of each ice cream shop.
|
Print the maximum number of ice creams that can be sold by choosing optimally the location of the new shop.
|
In the first sample, you can place the shop (coloured orange in the picture below) \(150\) meters to the right of the first hut (for example) so that it is the closest shop to the first two huts, which have \(2\) and \(5\) people, for a total of \(7\) sold ice creams. In the second sample, you can place the shop \(170\) meters to the right of the first hut (for example) so that it is the closest shop to the last two huts, which have \(7\) and \(8\) people, for a total of \(15\) sold ice creams.
|
Input: 3 1 2 5 6 169 | Output: 7
|
Beginner
| 3 | 1,116 | 450 | 107 | 16 |
1,466 |
E
|
1466E
|
E. Apollo versus Pan
| 1,800 |
bitmasks; brute force; math
|
Only a few know that Pan and Apollo weren't only battling for the title of the GOAT musician. A few millenniums later, they also challenged each other in math (or rather in fast calculations). The task they got to solve is the following:Let \(x_1, x_2, \ldots, x_n\) be the sequence of \(n\) non-negative integers. Find this value: $$$\(\sum_{i=1}^n \sum_{j=1}^n \sum_{k=1}^n (x_i \, \& \, x_j) \cdot (x_j \, | \, x_k)\)\(Here \)\&\( denotes the bitwise and, and \)|\( denotes the bitwise or.Pan and Apollo could solve this in a few seconds. Can you do it too? For convenience, find the answer modulo \)10^9 + 7$$$.
|
The first line of the input contains a single integer \(t\) (\(1 \leq t \leq 1\,000\)) denoting the number of test cases, then \(t\) test cases follow.The first line of each test case consists of a single integer \(n\) (\(1 \leq n \leq 5 \cdot 10^5\)), the length of the sequence. The second one contains \(n\) non-negative integers \(x_1, x_2, \ldots, x_n\) (\(0 \leq x_i < 2^{60}\)), elements of the sequence.The sum of \(n\) over all test cases will not exceed \(5 \cdot 10^5\).
|
Print \(t\) lines. The \(i\)-th line should contain the answer to the \(i\)-th text case.
|
Input: 8 2 1 7 3 1 2 4 4 5 5 5 5 5 6 2 2 1 0 1 0 1 1 6 1 12 123 1234 12345 123456 5 536870912 536870911 1152921504606846975 1152921504606846974 1152921504606846973 | Output: 128 91 1600 505 0 1 502811676 264880351
|
Medium
| 3 | 615 | 481 | 89 | 14 |
|
1,475 |
A
|
1475A
|
A. Odd Divisor
| 900 |
math; number theory
|
You are given an integer \(n\). Check if \(n\) has an odd divisor, greater than one (does there exist such a number \(x\) (\(x > 1\)) that \(n\) is divisible by \(x\) and \(x\) is odd).For example, if \(n=6\), then there is \(x=3\). If \(n=4\), then such a number does not exist.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then \(t\) test cases follow.Each test case contains one integer \(n\) (\(2 \le n \le 10^{14}\)).Please note, that the input for some test cases won't fit into \(32\)-bit integer type, so you should use at least \(64\)-bit integer type in your programming language.
|
For each test case, output on a separate line: ""YES"" if \(n\) has an odd divisor, greater than one; ""NO"" otherwise. You can output ""YES"" and ""NO"" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
|
Input: 6 2 3 4 5 998244353 1099511627776 | Output: NO YES NO YES YES NO
|
Beginner
| 2 | 279 | 358 | 246 | 14 |
|
1,616 |
G
|
1616G
|
G. Just Add an Edge
| 3,500 |
dfs and similar; dp; graphs
|
You are given a directed acyclic graph with \(n\) vertices and \(m\) edges. For all edges \(a \to b\) in the graph, \(a < b\) holds.You need to find the number of pairs of vertices \(x\), \(y\), such that \(x > y\) and after adding the edge \(x \to y\) to the graph, it has a Hamiltonian path.
|
The first line of input contains one integer \(t\) (\(1 \leq t \leq 5\)): the number of test cases.The next lines contains the descriptions of the test cases.In the first line you are given two integers \(n\) and \(m\) (\(1 \leq n \leq 150\,000\), \(0 \leq m \leq \min(150\,000, \frac{n(n-1)}{2})\)): the number of vertices and edges in the graph. Each of the next \(m\) lines contains two integers \(a\), \(b\) (\(1 \leq a < b \leq n\)), specifying an edge \(a \to b\) in the graph. No edge \(a \to b\) appears more than once.
|
For each test case, print one integer: the number of pairs of vertices \(x\), \(y\), \(x > y\), such that after adding the edge \(x \to y\) to the graph, it has a Hamiltonian path.
|
In the first example, any edge \(x \to y\) such that \(x > y\) is valid, because there already is a path \(1 \to 2 \to 3\).In the second example only the edge \(4 \to 1\) is valid. There is a path \(3 \to 4 \to 1 \to 2\) if this edge is added.In the third example you can add edges \(2 \to 1\), \(3 \to 1\), \(4 \to 1\), \(4 \to 2\).
|
Input: 3 3 2 1 2 2 3 4 3 1 2 3 4 1 4 4 4 1 3 1 4 2 3 3 4 | Output: 3 1 4
|
Master
| 3 | 293 | 527 | 180 | 16 |
1,346 |
A
|
1346A
|
A. Color Revolution
| 1,000 |
*special; math
|
Hmm, how long has it been since the last color revolution? 5 years?! It's totally the time to make a new one!So the general idea is the following. Division \(1\) should have \(n_1\) participants. Division \(2\) should have \(n_2\) and be exactly \(k\) times bigger than division \(1\) (\(n_2 = k \cdot n_1\)). Division \(3\) should have \(n_3 = k \cdot n_2\) participants. Finally, division \(4\) should have \(n_4 = k \cdot n_3\) participants.There are \(n\) participants on Codeforces in total. So \(n_1 + n_2 + n_3 + n_4\) should be exactly equal to \(n\).You know the values of \(n\) and \(k\). You also know that \(n\) and \(k\) are chosen in such a way that there exist values \(n_1, n_2, n_3\) and \(n_4\) such that all the conditions are satisfied.What will be the number of participants in each division (\(n_1, n_2, n_3\) and \(n_4\)) after the revolution?
|
The first line contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of testcases.Each of the next \(t\) lines contains two integers \(n\) and \(k\) (\(4 \le n \le 10^9\); \(1 \le k \le 500\)) β the total number of participants on Codeforces and the size multiplier for the corresponding testcase. In each testcase, \(n\) and \(k\) are chosen in such a way that the answer exists.
|
For each testcase print four integers \(n_1, n_2, n_3\) and \(n_4\) such that \(n_2 = k \cdot n_1\), \(n_3 = k \cdot n_2\), \(n_4 = k \cdot n_3\) and \(n_1 + n_2 + n_3 + n_4 = n\).
|
Input: 4 40 3 1200 7 320802005 400 4 1 | Output: 1 3 9 27 3 21 147 1029 5 2000 800000 320000000 1 1 1 1
|
Beginner
| 2 | 866 | 394 | 180 | 13 |
|
1,080 |
C
|
1080C
|
C. Masha and two friends
| 1,500 |
implementation
|
Recently, Masha was presented with a chessboard with a height of \(n\) and a width of \(m\).The rows on the chessboard are numbered from \(1\) to \(n\) from bottom to top. The columns are numbered from \(1\) to \(m\) from left to right. Therefore, each cell can be specified with the coordinates \((x,y)\), where \(x\) is the column number, and \(y\) is the row number (do not mix up).Let us call a rectangle with coordinates \((a,b,c,d)\) a rectangle lower left point of which has coordinates \((a,b)\), and the upper right one β \((c,d)\).The chessboard is painted black and white as follows: An example of a chessboard. Masha was very happy with the gift and, therefore, invited her friends Maxim and Denis to show off. The guys decided to make her a treat β they bought her a can of white and a can of black paint, so that if the old board deteriorates, it can be repainted. When they came to Masha, something unpleasant happened: first, Maxim went over the threshold and spilled white paint on the rectangle \((x_1,y_1,x_2,y_2)\). Then after him Denis spilled black paint on the rectangle \((x_3,y_3,x_4,y_4)\).To spill paint of color \(color\) onto a certain rectangle means that all the cells that belong to the given rectangle become \(color\). The cell dyeing is superimposed on each other (if at first some cell is spilled with white paint and then with black one, then its color will be black).Masha was shocked! She drove away from the guests and decided to find out how spoiled the gift was. For this, she needs to know the number of cells of white and black color. Help her find these numbers!
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) β the number of test cases.Each of them is described in the following format:The first line contains two integers \(n\) and \(m\) (\(1 \le n,m \le 10^9\)) β the size of the board.The second line contains four integers \(x_1\), \(y_1\), \(x_2\), \(y_2\) (\(1 \le x_1 \le x_2 \le m, 1 \le y_1 \le y_2 \le n\)) β the coordinates of the rectangle, the white paint was spilled on.The third line contains four integers \(x_3\), \(y_3\), \(x_4\), \(y_4\) (\(1 \le x_3 \le x_4 \le m, 1 \le y_3 \le y_4 \le n\)) β the coordinates of the rectangle, the black paint was spilled on.
|
Output \(t\) lines, each of which contains two numbers β the number of white and black cells after spilling paint, respectively.
|
Explanation for examples:The first picture of each illustration shows how the field looked before the dyes were spilled. The second picture of each illustration shows how the field looked after Maxim spoiled white dye (the rectangle on which the dye was spilled is highlighted with red). The third picture in each illustration shows how the field looked after Denis spoiled black dye (the rectangle on which the dye was spilled is highlighted with red).In the first test, the paint on the field changed as follows: In the second test, the paint on the field changed as follows: In the third test, the paint on the field changed as follows: In the fourth test, the paint on the field changed as follows: In the fifth test, the paint on the field changed as follows:
|
Input: 5 2 2 1 1 2 2 1 1 2 2 3 4 2 2 3 2 3 1 4 3 1 5 1 1 5 1 3 1 5 1 4 4 1 1 4 2 1 3 4 4 3 4 1 2 4 2 2 1 3 3 | Output: 0 4 3 9 2 3 8 8 4 8
|
Medium
| 1 | 1,607 | 640 | 128 | 10 |
5 |
C
|
5C
|
C. Longest Regular Bracket Sequence
| 1,900 |
constructive algorithms; data structures; dp; greedy; sortings; strings
|
This is yet another problem dealing with regular bracket sequences.We should remind you that a bracket sequence is called regular, if by inserting Β«+Β» and Β«1Β» into it we can get a correct mathematical expression. For example, sequences Β«(())()Β», Β«()Β» and Β«(()(()))Β» are regular, while Β«)(Β», Β«(()Β» and Β«(()))(Β» are not. You are given a string of Β«(Β» and Β«)Β» characters. You are to find its longest substring that is a regular bracket sequence. You are to find the number of such substrings as well.
|
The first line of the input file contains a non-empty string, consisting of Β«(Β» and Β«)Β» characters. Its length does not exceed 106.
|
Print the length of the longest substring that is a regular bracket sequence, and the number of such substrings. If there are no such substrings, write the only line containing ""0 1"".
|
Input: )((())))(()()) | Output: 6 2
|
Hard
| 6 | 497 | 131 | 185 | 0 |
|
1,091 |
H
|
1091H
|
H. New Year and the Tricolore Recreation
| 3,200 |
games
|
Alice and Bob play a game on a grid with \(n\) rows and infinitely many columns. In each row, there are three tokens, blue, white and red one. Before the game starts and after every move, the following two conditions must hold: Any two tokens are not in the same cell. In each row, the blue token is to the left of the white token, and the red token is to the right of the white token. First, they pick a positive integer \(f\), whose value is valid for the whole game. Second, the starting player is chosen and makes his or her first turn. Then players take alternating turns. The player who is unable to make a move loses. During a move, a player first selects an integer \(k\) that is either a prime number or a product of two (not necessarily distinct) primes. The smallest possible values of \(k\) are thus \(2, 3, 4, 5, 6, 7, 9, 10, 11, 13, 14, 15, 17, 19, \dots\). Furthermore, \(k\) must not be equal to the previously picked integer \(f\). Each turn, a move is performed in exactly one of the rows.If it is Alice's turn, she chooses a single blue token and moves it \(k\) cells to the right. Alternatively, she may move both the blue and the white token in the same row by the same amount \(k\) to the right.On the other hand, Bob selects a single red token and moves it \(k\) cells to the left. Similarly, he may also move the white and the red token in the corresponding row by \(k\) to the left.Note that Alice may never move a red token, while Bob may never move a blue one. Remember that after a move, the two conditions on relative positions of the tokens must still hold. Both players play optimally. Given the initial state of the board, determine who wins for two games: if Alice starts and if Bob starts.
|
The first line contains a two integers \(n\) and \(f\) (\(1 \leq n \leq 10^5\), \(2 \leq f \leq 2 \cdot 10^5\)) β the number of rows and the forbidden move, respectively.Each of the next \(n\) lines contains three integers \(b_i\), \(w_i\), \(r_i\) (\(-10^5 \leq b_i < w_i < r_i \leq 10^5\)) β the number of column in which the blue, white and red token lies in the \(i\)-th row, respectively.
|
Output two lines. The first line should contain the name of the winner when Alice starts, and the second line should contain the name of the winner when Bob starts.
|
The first example is as follows:When Alice starts, she can win by moving the blue and white token to right by \(2\) cells, getting into position \(2~5~9\). Regardless of what Bob does, Alice will have one more move and then the game is over. For instance, he can move both the red and white token by \(2\) cells to the left, reaching state \(2~3~7\). Alice can then move blue and white token by \(2\) to move into \(4~5~7\), where no more moves are possible.If Bob starts, he gains enough advantage to win. For instance, he may move the red token by \(3\) to the left, getting into position \(0~3~6\). Alice can, for example, move the blue token by \(2\), which is countered by Bob by moving the red token by \(2\). The game ends in position \(2~3~4\). In the second example, it is forbidden to move by \(2\), but this doesn't stop Alice from winning! She can move the blue and white token by \(4\), getting into position \(4~7~9\). Now Bob has no move, since moving by \(2\) is forbidden.
|
Input: 1 6 0 3 9 | Output: Alice Bob
|
Master
| 1 | 1,723 | 393 | 164 | 10 |
681 |
C
|
681C
|
C. Heap Operations
| 1,600 |
constructive algorithms; data structures; greedy
|
Petya has recently learned data structure named ""Binary heap"".The heap he is now operating with allows the following operations: put the given number into the heap; get the value of the minimum element in the heap; extract the minimum element from the heap; Thus, at any moment of time the heap contains several integers (possibly none), some of them might be equal.In order to better learn this data structure Petya took an empty heap and applied some operations above to it. Also, he carefully wrote down all the operations and their results to his event log, following the format: insert x β put the element with value x in the heap; getMin x β the value of the minimum element contained in the heap was equal to x; removeMin β the minimum element was extracted from the heap (only one instance, if there were many). All the operations were correct, i.e. there was at least one element in the heap each time getMin or removeMin operations were applied.While Petya was away for a lunch, his little brother Vova came to the room, took away some of the pages from Petya's log and used them to make paper boats.Now Vova is worried, if he made Petya's sequence of operations inconsistent. For example, if one apply operations one-by-one in the order they are written in the event log, results of getMin operations might differ from the results recorded by Petya, and some of getMin or removeMin operations may be incorrect, as the heap is empty at the moment they are applied.Now Vova wants to add some new operation records to the event log in order to make the resulting sequence of operations correct. That is, the result of each getMin operation is equal to the result in the record, and the heap is non-empty when getMin ad removeMin are applied. Vova wants to complete this as fast as possible, as the Petya may get back at any moment. He asks you to add the least possible number of operation records to the current log. Note that arbitrary number of operations may be added at the beginning, between any two other operations, or at the end of the log.
|
The first line of the input contains the only integer n (1 β€ n β€ 100 000) β the number of the records left in Petya's journal.Each of the following n lines describe the records in the current log in the order they are applied. Format described in the statement is used. All numbers in the input are integers not exceeding 109 by their absolute value.
|
The first line of the output should contain a single integer m β the minimum possible number of records in the modified sequence of operations.Next m lines should contain the corrected sequence of records following the format of the input (described in the statement), one per line and in the order they are applied. All the numbers in the output should be integers not exceeding 109 by their absolute value.Note that the input sequence of operations must be the subsequence of the output sequence.It's guaranteed that there exists the correct answer consisting of no more than 1 000 000 operations.
|
In the first sample, after number 3 is inserted into the heap, the minimum number is 3. To make the result of the first getMin equal to 4 one should firstly remove number 3 from the heap and then add number 4 into the heap.In the second sample case number 1 is inserted two times, so should be similarly removed twice.
|
Input: 2insert 3getMin 4 | Output: 4insert 3removeMininsert 4getMin 4
|
Medium
| 3 | 2,059 | 350 | 599 | 6 |
1,497 |
D
|
1497D
|
D. Genius
| 2,500 |
bitmasks; dp; graphs; number theory
|
Please note the non-standard memory limit.There are \(n\) problems numbered with integers from \(1\) to \(n\). \(i\)-th problem has the complexity \(c_i = 2^i\), tag \(tag_i\) and score \(s_i\).After solving the problem \(i\) it's allowed to solve problem \(j\) if and only if \(\text{IQ} < |c_i - c_j|\) and \(tag_i \neq tag_j\). After solving it your \(\text{IQ}\) changes and becomes \(\text{IQ} = |c_i - c_j|\) and you gain \(|s_i - s_j|\) points.Any problem can be the first. You can solve problems in any order and as many times as you want.Initially your \(\text{IQ} = 0\). Find the maximum number of points that can be earned.
|
The first line contains a single integer \(t\) \((1 \le t \le 100)\) β the number of test cases. The first line of each test case contains an integer \(n\) \((1 \le n \le 5000)\) β the number of problems.The second line of each test case contains \(n\) integers \(tag_1, tag_2, \ldots, tag_n\) \((1 \le tag_i \le n)\) β tags of the problems.The third line of each test case contains \(n\) integers \(s_1, s_2, \ldots, s_n\) \((1 \le s_i \le 10^9)\) β scores of the problems.It's guaranteed that sum of \(n\) over all test cases does not exceed \(5000\).
|
For each test case print a single integer β the maximum number of points that can be earned.
|
In the first test case optimal sequence of solving problems is as follows: \(1 \rightarrow 2\), after that total score is \(5\) and \(\text{IQ} = 2\) \(2 \rightarrow 3\), after that total score is \(10\) and \(\text{IQ} = 4\) \(3 \rightarrow 1\), after that total score is \(20\) and \(\text{IQ} = 6\) \(1 \rightarrow 4\), after that total score is \(35\) and \(\text{IQ} = 14\) In the second test case optimal sequence of solving problems is as follows: \(1 \rightarrow 2\), after that total score is \(5\) and \(\text{IQ} = 2\) \(2 \rightarrow 3\), after that total score is \(10\) and \(\text{IQ} = 4\) \(3 \rightarrow 4\), after that total score is \(15\) and \(\text{IQ} = 8\) \(4 \rightarrow 1\), after that total score is \(35\) and \(\text{IQ} = 14\) In the third test case optimal sequence of solving problems is as follows: \(1 \rightarrow 3\), after that total score is \(17\) and \(\text{IQ} = 6\) \(3 \rightarrow 4\), after that total score is \(35\) and \(\text{IQ} = 8\) \(4 \rightarrow 2\), after that total score is \(42\) and \(\text{IQ} = 12\)
|
Input: 5 4 1 2 3 4 5 10 15 20 4 1 2 1 2 5 10 15 20 4 2 2 4 1 2 8 19 1 2 1 1 6 9 1 1 666 | Output: 35 30 42 0 0
|
Expert
| 4 | 634 | 553 | 92 | 14 |
1,215 |
B
|
1215B
|
B. The Number of Products
| 1,400 |
combinatorics; dp; implementation
|
You are given a sequence \(a_1, a_2, \dots, a_n\) consisting of \(n\) non-zero integers (i.e. \(a_i \ne 0\)). You have to calculate two following values: the number of pairs of indices \((l, r)\) \((l \le r)\) such that \(a_l \cdot a_{l + 1} \dots a_{r - 1} \cdot a_r\) is negative; the number of pairs of indices \((l, r)\) \((l \le r)\) such that \(a_l \cdot a_{l + 1} \dots a_{r - 1} \cdot a_r\) is positive;
|
The first line contains one integer \(n\) \((1 \le n \le 2 \cdot 10^{5})\) β the number of elements in the sequence.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) \((-10^{9} \le a_i \le 10^{9}; a_i \neq 0)\) β the elements of the sequence.
|
Print two integers β the number of subsegments with negative product and the number of subsegments with positive product, respectively.
|
Input: 5 5 -3 3 -1 1 | Output: 8 7
|
Easy
| 3 | 411 | 257 | 135 | 12 |
|
1,228 |
E
|
1228E
|
E. Another Filling the Grid
| 2,300 |
combinatorics; dp; math
|
You have \(n \times n\) square grid and an integer \(k\). Put an integer in each cell while satisfying the conditions below. All numbers in the grid should be between \(1\) and \(k\) inclusive. Minimum number of the \(i\)-th row is \(1\) (\(1 \le i \le n\)). Minimum number of the \(j\)-th column is \(1\) (\(1 \le j \le n\)). Find the number of ways to put integers in the grid. Since the answer can be very large, find the answer modulo \((10^{9} + 7)\). These are the examples of valid and invalid grid when \(n=k=2\).
|
The only line contains two integers \(n\) and \(k\) (\(1 \le n \le 250\), \(1 \le k \le 10^{9}\)).
|
Print the answer modulo \((10^{9} + 7)\).
|
In the first example, following \(7\) cases are possible. In the second example, make sure you print the answer modulo \((10^{9} + 7)\).
|
Input: 2 2 | Output: 7
|
Expert
| 3 | 521 | 98 | 41 | 12 |
622 |
D
|
622D
|
D. Optimal Number Permutation
| 1,900 |
constructive algorithms
|
You have array a that contains all integers from 1 to n twice. You can arbitrary permute any numbers in a.Let number i be in positions xi, yi (xi < yi) in the permuted array a. Let's define the value di = yi - xi β the distance between the positions of the number i. Permute the numbers in array a to minimize the value of the sum .
|
The only line contains integer n (1 β€ n β€ 5Β·105).
|
Print 2n integers β the permuted array a that minimizes the value of the sum s.
|
Input: 2 | Output: 1 1 2 2
|
Hard
| 1 | 332 | 49 | 79 | 6 |
|
1,990 |
F
|
1990F
|
F. Polygonal Segments
| 2,800 |
brute force; data structures; divide and conquer; dp; greedy; two pointers
|
You are given an array \(a\) of size \(n\).A segment \([l, r](1 \le l < r \le n)\) is called a polygonal segment only if the following conditions hold: \((r-l+1) \geq 3\); Considering \(a_l, a_{l+1}, \ldots, a_r\) as side lengths, these sides can form a polygon with \((r-l+1)\) sides. Process \(q\) queries of two types: ""1 l r"": find the length of the longest segment among all polygonal segments \([l_0,r_0]\) satisfying \(l \le l_0 \le r_0 \le r\). If there is no such polygonal segment, output \(-1\) instead; ""2 i x"": assign \(a_i := x\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.For each test case: The first line of each testcase contains two integers \(n\), \(q\) (\(4 \le n \le 2\cdot 10^5\), \(1 \le q \le 10^5\)); The second line of each testcase contains \(n\) integers \(a_1,a_2,\ldots, a_n\) (\(1 \le a_i \le 10^{12}\)); The following \(q\) lines contain the description of queries. Each line is of one of two types: ""1 l r"" (\(1 \le l < r \le n\), \(r-l+1\ge 3\)); ""2 i x"" (\(1 \le i \le n\), \(1 \le x \le 10^{12}\)). It is guaranteed that the sum of \(n\) over all test cases will not exceed \(2 \cdot 10^5\), and the sum of \(q\) over all test cases will not exceed \(10^5\).
|
For each query, if there is no suitable segment, output \(-1\) in a new line. Otherwise, output the length of the longest segment satisfying the condition above in a new line.
|
In the first query of the first test case, there is no polygonal segment under the given condition. For example, considering segment \([1,3]\), you can not form a triangle with side lengths of \(a_1=3\), \(a_2=1\), and \(a_3=2\).In the second query of the first test case, the longest polygonal segment is \([1,4]\). You can form a quadrilateral with side lengths of \(a_1=3\), \(a_2=1\), \(a_3=2\), and \(a_4=2\). An example of a quadrilateral with side lengths of \(3\), \(1\), \(2\), and \(2\).
|
Input: 25 63 1 2 2 81 1 31 1 41 1 52 1 51 1 41 1 54 10500000000000 500000000000 1000000000000 5000000000001 1 31 2 41 1 42 1 4999999999992 3 9999999999991 1 31 2 41 1 42 3 10000000000001 1 3 | Output: -1 4 4 3 5 -1 -1 4 -1 3 4 -1
|
Master
| 6 | 548 | 705 | 175 | 19 |
344 |
B
|
344B
|
B. Simple Molecules
| 1,200 |
brute force; graphs; math
|
Mad scientist Mike is busy carrying out experiments in chemistry. Today he will attempt to join three atoms into one molecule.A molecule consists of atoms, with some pairs of atoms connected by atomic bonds. Each atom has a valence number β the number of bonds the atom must form with other atoms. An atom can form one or multiple bonds with any other atom, but it cannot form a bond with itself. The number of bonds of an atom in the molecule must be equal to its valence number. Mike knows valence numbers of the three atoms. Find a molecule that can be built from these atoms according to the stated rules, or determine that it is impossible.
|
The single line of the input contains three space-separated integers a, b and c (1 β€ a, b, c β€ 106) β the valence numbers of the given atoms.
|
If such a molecule can be built, print three space-separated integers β the number of bonds between the 1-st and the 2-nd, the 2-nd and the 3-rd, the 3-rd and the 1-st atoms, correspondingly. If there are multiple solutions, output any of them. If there is no solution, print ""Impossible"" (without the quotes).
|
The first sample corresponds to the first figure. There are no bonds between atoms 1 and 2 in this case.The second sample corresponds to the second figure. There is one or more bonds between each pair of atoms.The third sample corresponds to the third figure. There is no solution, because an atom cannot form bonds with itself.The configuration in the fourth figure is impossible as each atom must have at least one atomic bond.
|
Input: 1 1 2 | Output: 0 1 1
|
Easy
| 3 | 645 | 141 | 312 | 3 |
1,446 |
E
|
1446E
|
E. Long Recovery
| 3,500 |
constructive algorithms; dfs and similar
|
A patient has been infected with an unknown disease. His body can be seen as an infinite grid of triangular cells which looks as follows: Two cells are neighboring if they share a side. Therefore, each cell (\(x\), \(y\)) has exactly three neighbors: (\(x+1\), \(y\)) (\(x-1\), \(y\)) (\(x+1\), \(y-1\)) if \(x\) is even and (\(x-1\), \(y+1\)) otherwise. Initially some cells are infected, all the others are healthy. The process of recovery begins. Each second, for exactly one cell (even though there might be multiple cells that could change its state) one of the following happens: A healthy cell with at least \(2\) infected neighbors also becomes infected. An infected cell with at least \(2\) healthy neighbors also becomes healthy. If no such cell exists, the process of recovery stops. Patient is considered recovered if the process of recovery has stopped and all the cells are healthy.We're interested in a worst-case scenario: is it possible that the patient never recovers, or if it's not possible, what is the maximum possible duration of the recovery process?
|
The first line contains one integer \(n\) \((1 \leq n \leq 250000)\) β the number of infected cells. The \(i\)-th of the next \(n\) lines contains two space-separated integers \(x_i\) and \(y_i\) \((0 \leq x_i, y_i < 500)\), meaning that cell \((x_i, y_i)\) is infected. All cells \((x_i, y_i)\) are distinct, and all other cells are considered healthy.
|
If it is possible that the organism never fully recovers from the disease, print SICK. Otherwise, you should print RECOVERED and in the next line an integer \(k\) β the longest possible recovery period, modulo \(998244353\).
|
For the first testcase, the following drawings describe the longest possible recovery process. It can be proven that there are no recovery periods of length \(5\) or longer, and the organism always recovers in this testcase. \(\hspace{40pt} \downarrow\) \(\hspace{40pt} \downarrow\) \(\hspace{40pt} \downarrow\) \(\hspace{40pt} \downarrow\)\(\hspace{15pt}\) RECOVEREDFor the second testcase, it is possible for the cells \((2, 0)\), \((2, 1)\), \((0, 1)\) to become infected. After that, no cell can change its state, so the answer is SICK, as not all of the cells are healthy.
|
Input: 4 0 0 1 0 2 0 0 1 | Output: RECOVERED 4
|
Master
| 2 | 1,074 | 353 | 224 | 14 |
1,767 |
E
|
1767E
|
E. Algebra Flash
| 2,500 |
bitmasks; brute force; dp; graphs; math; meet-in-the-middle; trees
|
Algebra Flash 2.2 has just been released!Changelog: New gamemode! Thank you for the continued support of the game! Huh, is that it? Slightly disappointed, you boot up the game and click on the new gamemode. It says ""Colored platforms"".There are \(n\) platforms, numbered from \(1\) to \(n\), placed one after another. There are \(m\) colors available in the game, numbered from \(1\) to \(m\). The \(i\)-th platform is colored \(c_i\).You start on the platform \(1\) and want to reach platform \(n\). In one move, you can jump from some platform \(i\) to platforms \(i + 1\) or \(i + 2\).All platforms are initially deactivated (including platforms \(1\) and \(n\)). For each color \(j\), you can pay \(x_j\) coins to activate all platforms of that color.You want to activate some platforms so that you could start on an activated platform \(1\), jump through some activated platforms and reach an activated platform \(n\).What's the smallest amount of coins you can spend to achieve that?
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 3 \cdot 10^5\); \(1 \le m \le 40\)) β the number of platforms and the number of colors, respectively.The second line contains \(n\) integers \(c_1, c_2, \dots, c_n\) (\(1 \le c_i \le m\)) β the colors of the platforms.The third line contains \(m\) integers \(x_1, x_2, \dots, x_m\) (\(1 \le x_i \le 10^7\)) β the cost of activating all platforms of each color.
|
Print the smallest amount of coins you can spend to activate some platforms so that you could start on an activated platform \(1\), jump through some activated platforms and reach an activated platform \(n\).
|
Input: 5 3 1 3 2 3 1 1 10 100 | Output: 11
|
Expert
| 7 | 991 | 428 | 208 | 17 |
|
1,297 |
F
|
1297F
|
F. Movie Fan
| 0 |
*special; data structures; greedy; implementation; sortings
|
Recently, Polycarp has been a fan of cinema novelties and is trying not to miss them!In the near future, \(n\) new movies will be released: the \(i\)-th of them will be airing from the day \(a_i\) and to the day \(b_i\). This means that if Polycarp wants to watch the \(i\)-th movie in the cinema, he must do so in the period from \(a_i\) to \(b_i\) inclusive.If perhaps Polycarp will not have the opportunity to watch a movie in a cinema, he can then do it after day \(b_i\) by watching it using an online service. Of course, this is an undesirable outcome for Polycarp because the whole world will have time to discuss this movie on social networks!Polycarp can watch no more than \(m\) movies per day. Help Polycarp find a movie-watching schedule such that every movie will be watched in the cinema. If such a schedule does not exist, then Polycarp wants to watch movies so that: for each movie that he doesn't have time to watch in the cinema, we will find the number of days between the end of its airing and the day when Polycarpus watches the movie, the maximum of the values from the previous point should be as small as possible.
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input. The following are descriptions of the \(t\) test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\), \(1 \le m \le 10^9\)) β the number of movies and the maximum number of movies that Polycarp can view per day.In the next \(n\) lines, the movies themselves are described, one per line, by a pair of integers \(a_i\), \(b_i\) (\(1 \le a_i \le b_i \le 10^9\)) β the first and last airing days for the \(i\)-th movie.It is guaranteed that the sum of the values \(n\) for all test cases in the input does not exceed \(2 \cdot 10^5\).
|
Print \(t\) answers to given test cases in the order in which they appear in the input: the \(i\)-th answer should consist of two lines. Print the integer \(d\) in the first line of each test case answer: \(d=0\), if there is a schedule such that all movies are watched during airing, \(d>0\), if such a schedule does not exist β in this case, \(d\) is equal to the minimum value of maximum among all the watching ""delays"" after the end of airing. In the second line of the answer to each test case, print \(n\) positive integers \(t_1, t_2, \dots, t_n\), where \(t_i\) is the number of the day when Polycarp needs to watch the \(i\)-th movie in the optimal schedule.If there are several answers, print any of them.
|
Input: 3 7 2 1 2 1 3 2 2 2 3 1 1 2 3 1 2 5 3 1 1 1 1 1 1 1 1 1 1 6 1 13 13 31 31 25 25 12 12 14 14 10 10 | Output: 1 1 3 2 3 1 4 2 1 1 1 1 2 2 0 13 31 25 12 14 10
|
Beginner
| 5 | 1,138 | 690 | 717 | 12 |
|
1,365 |
D
|
1365D
|
D. Solve The Maze
| 1,700 |
constructive algorithms; dfs and similar; dsu; graphs; greedy; implementation; shortest paths
|
Vivek has encountered a problem. He has a maze that can be represented as an \(n \times m\) grid. Each of the grid cells may represent the following: Empty β '.' Wall β '#' Good person β 'G' Bad person β 'B' The only escape from the maze is at cell \((n, m)\).A person can move to a cell only if it shares a side with their current cell and does not contain a wall. Vivek wants to block some of the empty cells by replacing them with walls in such a way, that all the good people are able to escape, while none of the bad people are able to. A cell that initially contains 'G' or 'B' cannot be blocked and can be travelled through.Help him determine if there exists a way to replace some (zero or more) empty cells with walls to satisfy the above conditions.It is guaranteed that the cell \((n,m)\) is empty. Vivek can also block this cell.
|
The first line contains one integer \(t\) \((1 \le t \le 100)\) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\), \(m\) \((1 \le n, m \le 50)\) β the number of rows and columns in the maze.Each of the next \(n\) lines contain \(m\) characters. They describe the layout of the maze. If a character on a line equals '.', the corresponding cell is empty. If it equals '#', the cell has a wall. 'G' corresponds to a good person and 'B' corresponds to a bad person.
|
For each test case, print ""Yes"" if there exists a way to replace some empty cells with walls to satisfy the given conditions. Otherwise print ""No""You may print every letter in any case (upper or lower).
|
For the first and second test cases, all conditions are already satisfied.For the third test case, there is only one empty cell \((2,2)\), and if it is replaced with a wall then the good person at \((1,2)\) will not be able to escape.For the fourth test case, the good person at \((1,1)\) cannot escape.For the fifth test case, Vivek can block the cells \((2,3)\) and \((2,2)\).For the last test case, Vivek can block the destination cell \((2, 2)\).
|
Input: 6 1 1 . 1 2 G. 2 2 #B G. 2 3 G.# B#. 3 3 #B. #.. GG. 2 2 #B B. | Output: Yes Yes No No Yes Yes
|
Medium
| 7 | 840 | 542 | 206 | 13 |
6 |
D
|
6D
|
D. Lizards and Basements 2
| 2,600 |
brute force; dp
|
This is simplified version of the problem used on the original contest. The original problem seems to have too difiicult solution. The constraints for input data have been reduced.Polycarp likes to play computer role-playing game Β«Lizards and BasementsΒ». At the moment he is playing it as a magician. At one of the last levels he has to fight the line of archers. The only spell with which he can damage them is a fire ball. If Polycarp hits the i-th archer with his fire ball (they are numbered from left to right), the archer loses a health points. At the same time the spell damages the archers adjacent to the i-th (if any) β they lose b (1 β€ b < a β€ 10) health points each.As the extreme archers (i.e. archers numbered 1 and n) are very far, the fire ball cannot reach them. Polycarp can hit any other archer with his fire ball.The amount of health points for each archer is known. An archer will be killed when this amount is less than 0. What is the minimum amount of spells Polycarp can use to kill all the enemies?Polycarp can throw his fire ball into an archer if the latter is already killed.
|
The first line of the input contains three integers n, a, b (3 β€ n β€ 10; 1 β€ b < a β€ 10). The second line contains a sequence of n integers β h1, h2, ..., hn (1 β€ hi β€ 15), where hi is the amount of health points the i-th archer has.
|
In the first line print t β the required minimum amount of fire balls.In the second line print t numbers β indexes of the archers that Polycarp should hit to kill all the archers in t shots. All these numbers should be between 2 and n - 1. Separate numbers with spaces. If there are several solutions, output any of them. Print numbers in any order.
|
Input: 3 2 12 2 2 | Output: 32 2 2
|
Expert
| 2 | 1,103 | 233 | 349 | 0 |
|
1,419 |
A
|
1419A
|
A. Digit Game
| 900 |
games; greedy; implementation
|
Everyone knows that agents in Valorant decide, who will play as attackers, and who will play as defenders. To do that Raze and Breach decided to play \(t\) matches of a digit game...In each of \(t\) matches of the digit game, a positive integer is generated. It consists of \(n\) digits. The digits of this integer are numerated from \(1\) to \(n\) from the highest-order digit to the lowest-order digit. After this integer is announced, the match starts.Agents play in turns. Raze starts. In one turn an agent can choose any unmarked digit and mark it. Raze can choose digits on odd positions, but can not choose digits on even positions. Breach can choose digits on even positions, but can not choose digits on odd positions. The match ends, when there is only one unmarked digit left. If the single last digit is odd, then Raze wins, else Breach wins.It can be proved, that before the end of the match (for every initial integer with \(n\) digits) each agent has an ability to make a turn, i.e. there is at least one unmarked digit, that stands on a position of required parity.For each of \(t\) matches find out, which agent wins, if both of them want to win and play optimally.
|
First line of input contains an integer \(t\) \((1 \le t \le 100)\) β the number of matches.The first line of each match description contains an integer \(n\) \((1 \le n \le 10^3)\) β the number of digits of the generated number.The second line of each match description contains an \(n\)-digit positive integer without leading zeros.
|
For each match print \(1\), if Raze wins, and \(2\), if Breach wins.
|
In the first match no one can make a turn, the only digit left is \(2\), it's even, so Breach wins.In the second match the only digit left is \(3\), it's odd, so Raze wins.In the third match Raze can mark the last digit, after that Breach can only mark \(0\). \(1\) will be the last digit left, it's odd, so Raze wins.In the fourth match no matter how Raze plays, Breach can mark \(9\), and in the end there will be digit \(0\). It's even, so Breach wins.
|
Input: 4 1 2 1 3 3 102 4 2069 | Output: 2 1 1 2
|
Beginner
| 3 | 1,182 | 334 | 68 | 14 |
1,938 |
B
|
1938B
| 3,000 |
Master
| 0 | 0 | 0 | 0 | 19 |
|||||||
687 |
E
|
687E
|
E. TOF
| 2,900 |
dfs and similar; graphs
|
Today Pari gave Arya a cool graph problem. Arya wrote a non-optimal solution for it, because he believes in his ability to optimize non-optimal solutions. In addition to being non-optimal, his code was buggy and he tried a lot to optimize it, so the code also became dirty! He keeps getting Time Limit Exceeds and he is disappointed. Suddenly a bright idea came to his mind!Here is how his dirty code looks like:dfs(v){ set count[v] = count[v] + 1 if(count[v] < 1000) { foreach u in neighbors[v] { if(visited[u] is equal to false) { dfs(u) } break } } set visited[v] = true}main(){ input the digraph() TOF() foreach 1<=i<=n { set count[i] = 0 , visited[i] = false } foreach 1 <= v <= n { if(visited[v] is equal to false) { dfs(v) } } ... // And do something cool and magical but we can't tell you what!}He asks you to write the TOF function in order to optimize the running time of the code with minimizing the number of calls of the dfs function. The input is a directed graph and in the TOF function you have to rearrange the edges of the graph in the list neighbors for each vertex. The number of calls of dfs function depends on the arrangement of neighbors of each vertex.
|
The first line of the input contains two integers n and m (1 β€ n, m β€ 5000) β the number of vertices and then number of directed edges in the input graph.Each of the next m lines contains a pair of integers ui and vi (1 β€ ui, vi β€ n), meaning there is a directed edge in the input graph. You may assume that the graph won't contain any self-loops and there is at most one edge between any unordered pair of vertices.
|
Print a single integer β the minimum possible number of dfs calls that can be achieved with permuting the edges.
|
Input: 3 31 22 33 1 | Output: 2998
|
Master
| 2 | 1,177 | 416 | 112 | 6 |
|
2,040 |
E
|
2040E
|
E. Control of Randomness
| 2,100 |
combinatorics; dfs and similar; dp; graphs; greedy; math; probabilities; trees
|
You are given a tree with \(n\) vertices.Let's place a robot in some vertex \(v \ne 1\), and suppose we initially have \(p\) coins. Consider the following process, where in the \(i\)-th step (starting from \(i = 1\)): If \(i\) is odd, the robot moves to an adjacent vertex in the direction of vertex \(1\); Else, \(i\) is even. You can either pay one coin (if there are some left) and then the robot moves to an adjacent vertex in the direction of vertex \(1\), or not pay, and then the robot moves to an adjacent vertex chosen uniformly at random. The process stops as soon as the robot reaches vertex \(1\). Let \(f(v, p)\) be the minimum possible expected number of steps in the process above if we spend our coins optimally.Answer \(q\) queries, in the \(i\)-th of which you have to find the value of \(f(v_i, p_i)\), modulo\(^{\text{β}}\) \(998\,244\,353\).\(^{\text{β}}\) Formally, let \(M = 998\,244\,353\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(2 \le n \le 2 \cdot 10^3\); \(1 \le q \le 2 \cdot 10^3\)) β the number of vertices in the tree and the number of queries.The next \(n - 1\) lines contain the edges of the tree, one edge per line. The \(i\)-th line contains two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\); \(u_i \neq v_i\)), denoting the edge between the nodes \(u_i\) and \(v_i\).The next \(q\) lines contain two integers \(v_i\) and \(p_i\) (\(2 \le v_i \le n\); \(0 \le p_i \le n\)).It's guaranteed that the given edges form a tree.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10 ^ 3\).It is guaranteed that the sum of \(q\) over all test cases does not exceed \(2 \cdot 10 ^ 3\).
|
For each test case, print \(q\) integers: the values of \(f(v_i, p_i)\) modulo \(998\,244\,353\).Formally, let \(M = 998\,244\,353\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
|
The tree in the first test case: In the first query, the expected value is equal to \(1\), since the robot starts moving from vertex \(2\) to vertex \(1\) in the first step and the process stops.Let's calculate the expected value in the second query (\(x\) is the number of steps): \(P(x < 2) = 0\), the distance to vertex \(1\) is \(2\) and the robot cannot reach it in fewer steps. \(P(x = 2) = \frac{1}{3}\), since there is only one sequence of steps leading to \(x = 2\). This is \(3 \rightarrow_{1} 2 \rightarrow_{0.33} 1\) with probability \(1 \cdot \frac{1}{3}\). \(P(x \bmod 2 = 1) = 0\), since the robot can reach vertex \(1\) by only taking an even number of steps. \(P(x = 4) = \frac{2}{9}\): possible paths \(3 \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.33} 1\). \(P(x = 6) = \frac{4}{27}\): possible paths \(3 \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.67} [3, 4] \rightarrow_{1} 2 \rightarrow_{0.33} 1\). \(P(x = i \cdot 2) = \frac{2^{i - 1}}{3^i}\) in the general case. As a result, \(f(v, p) = \sum\limits_{i=1}^{\infty}{i \cdot 2 \cdot \frac{2^{i - 1}}{3^i}} = 6\).The tree in the second test case:
|
Input: 24 41 22 32 42 03 04 03 112 101 22 32 41 55 66 76 86 98 1010 1110 126 09 010 011 03 17 110 112 112 211 12 | Output: 1 6 6 2 4 9 8 15 2 3 6 9 5 5
|
Hard
| 8 | 1,235 | 935 | 454 | 20 |
556 |
B
|
556B
|
B. Case of Fake Numbers
| 1,100 |
brute force; implementation
|
Andrewid the Android is a galaxy-famous detective. He is now investigating a case of frauds who make fake copies of the famous Stolp's gears, puzzles that are as famous as the Rubik's cube once was.Its most important components are a button and a line of n similar gears. Each gear has n teeth containing all numbers from 0 to n - 1 in the counter-clockwise order. When you push a button, the first gear rotates clockwise, then the second gear rotates counter-clockwise, the the third gear rotates clockwise an so on.Besides, each gear has exactly one active tooth. When a gear turns, a new active tooth is the one following after the current active tooth according to the direction of the rotation. For example, if n = 5, and the active tooth is the one containing number 0, then clockwise rotation makes the tooth with number 1 active, or the counter-clockwise rotating makes the tooth number 4 active.Andrewid remembers that the real puzzle has the following property: you can push the button multiple times in such a way that in the end the numbers on the active teeth of the gears from first to last form sequence 0, 1, 2, ..., n - 1. Write a program that determines whether the given puzzle is real or fake.
|
The first line contains integer n (1 β€ n β€ 1000) β the number of gears.The second line contains n digits a1, a2, ..., an (0 β€ ai β€ n - 1) β the sequence of active teeth: the active tooth of the i-th gear contains number ai.
|
In a single line print ""Yes"" (without the quotes), if the given Stolp's gears puzzle is real, and ""No"" (without the quotes) otherwise.
|
In the first sample test when you push the button for the first time, the sequence of active teeth will be 2 2 1, when you push it for the second time, you get 0 1 2.
|
Input: 31 0 0 | Output: Yes
|
Easy
| 2 | 1,213 | 223 | 138 | 5 |
2,093 |
B
|
2093B
|
B. Expensive Number
| 900 |
greedy; math
|
The cost of a positive integer \(n\) is defined as the result of dividing the number \(n\) by the sum of its digits.For example, the cost of the number \(104\) is \(\frac{104}{1 + 0 + 4} = 20.8\), and the cost of the number \(111\) is \(\frac{111}{1 + 1 + 1} = 37\).You are given a positive integer \(n\) that does not contain leading zeros. You can remove any number of digits from the number \(n\) (including none) so that the remaining number contains at least one digit and is strictly greater than zero. The remaining digits cannot be rearranged. As a result, you may end up with a number that has leading zeros.For example, you are given the number \(103554\). If you decide to remove the digits \(1\), \(4\), and one digit \(5\), you will end up with the number \(035\), whose cost is \(\frac{035}{0 + 3 + 5} = 4.375\).What is the minimum number of digits you need to remove from the number so that its cost becomes the minimum possible?
|
The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The only line of each test case contains a positive integer \(n\) (\(1 \leq n < 10^{100}\)) without leading zeros.
|
For each test case, output one integer on a new line β the number of digits that need to be removed from the number so that its cost becomes minimal.
|
Input: 4666137001020307 | Output: 2 4 3 0
|
Beginner
| 2 | 944 | 207 | 149 | 20 |
|
1,780 |
A
|
1780A
|
A. Hayato and School
| 800 |
constructive algorithms; greedy
|
Today Hayato came home from school with homework.In the assignment, Hayato was given an array \(a\) of length \(n\). The task was to find \(3\) numbers in this array whose sum is odd. At school, he claimed that there are such \(3\) numbers, but Hayato was not sure, so he asked you for help.Answer if there are such three numbers, and if so, output indices \(i\), \(j\), and \(k\) such that \(a_i + a_j + a_k\) is odd.The odd numbers are integers that are not divisible by \(2\): \(1\), \(3\), \(5\), and so on.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.For each test case, the first line contains one integer \(n\) (\(3 \le n \le 300\)) β the length of \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^5\)) β the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case, in the first line print one word ""YES"" (without quotes) if there are \(3\) numbers with an odd sum or ""NO"" (without quotes) if there are no such \(3\) numbers.If the answer exists, then on the second line print \(3\) distinct integers \(i, j, k\) (\(1 \le i, j, k \le n\)) β the indices of the numbers. If there are several answers, output any.
|
In the first test case, there is one way to choose \(3\) numbers, and since \(1 + 1 + 1 = 3\), this triple is fine for us.In the second test case, you need to choose the numbers \(1, 2, 2\), since \(1 + 2 + 2 = 5\).In the third test case, there is one way to choose three numbers, but \(1 + 2 + 3 = 6\) is an even number, so the required triple does not exist.In the fifth test case, no matter what three numbers we choose, their sum is even.
|
Input: 631 1 141 1 2 231 2 351 4 5 1 242 6 2 455 6 3 2 1 | Output: YES 1 2 3 YES 3 4 1 NO YES 1 3 4 NO YES 1 3 5
|
Beginner
| 2 | 511 | 402 | 368 | 17 |
334 |
B
|
334B
|
B. Eight Point Sets
| 1,400 |
sortings
|
Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines and three distinct integer horizontal straight lines, except for the average of these nine points. In other words, there must be three integers x1, x2, x3 and three more integers y1, y2, y3, such that x1 < x2 < x3, y1 < y2 < y3 and the eight point set consists of all points (xi, yj) (1 β€ i, j β€ 3), except for point (x2, y2).You have a set of eight points. Find out if Gerald can use this set?
|
The input consists of eight lines, the i-th line contains two space-separated integers xi and yi (0 β€ xi, yi β€ 106). You do not have any other conditions for these points.
|
In a single line print word ""respectable"", if the given set of points corresponds to Gerald's decency rules, and ""ugly"" otherwise.
|
Input: 0 00 10 21 01 22 02 12 2 | Output: respectable
|
Easy
| 1 | 576 | 171 | 134 | 3 |
|
1,810 |
G
|
1810G
|
G. The Maximum Prefix
| 3,200 |
dp
|
You're going to generate an array \(a\) with a length of at most \(n\), where each \(a_{i}\) equals either \(1\) or \(-1\).You generate this array in the following way. First, you choose some integer \(k\) (\(1\le k \le n\)), which decides the length of \(a\). Then, for each \(i\) (\(1\le i \le k\)), you set \(a_{i} = 1\) with probability \(p_{i}\), otherwise set \(a_{i} = -1\) (with probability \(1 - p_{i}\)). After the array is generated, you calculate \(s_{i} = a_{1} + a_{2} + a_{3}+ \ldots + a_{i}\). Specially, \(s_{0} = 0\). Then you let \(S\) equal to \(\displaystyle \max_{i=0}^{k}{s_{i}}\). That is, \(S\) is the maximum prefix sum of the array \(a\).You are given \(n+1\) integers \(h_{0} , h_{1}, \ldots ,h_{n}\). The score of an array \(a\) with maximum prefix sum \(S\) is \(h_{S}\). Now, for each \(k\), you want to know the expected score for an array of length \(k\) modulo \(10^9+7\).
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 5000\)) β the number of test cases. Their description follows.The first line contains an integer \(n\) (\(1\le n \le 5000\)).Then for the following \(n\) lines, each line contains two integers \(x_{i}\) and \(y_{i}\) (\(0 \le x_{i} < 10^9 + 7\), \(1\le y_{i} < 10^9 + 7\), \(x_{i} \le y_{i}\)), indicating \(p_{i} = \frac{x_{i}}{y_{i}}\).The next line contains \(n+1\) integers \(h_{0},h_{1}, \ldots, h_{n}\) (\(0 \le h_{i} < 10^9 + 7\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5000\).
|
For each test case, output \(n\) integers in one single line, the \(i\)-th of which denotes the expected score for an array of length \(i\), modulo \(10^9 + 7\).Formally, let \(M = 10^9 + 7\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
|
In the first test case, if we choose \(k=1\), there are \(2\) possible arrays with equal probabilities: \([1]\) and \([-1]\). The \(S\) values for them are \(1\) and \(0\). So the expected score is \(\frac{1}{2}h_{0} + \frac{1}{2}h_{1} = \frac{3}{2}\). If we choose \(k=2\), there are \(4\) possible arrays with equal probabilities: \([1,1]\), \([1,-1]\), \([-1,1]\), \([-1,-1]\), and the \(S\) values for them are \(2,1,0,0\). So the expected score is \(\frac{1}{2}h_{0} + \frac{1}{4}h_{1} + \frac{1}{4}h_{2} = \frac{7}{4}\).In the second test case, no matter what the \(S\) value is, the score is always \(1\), so the expected score is always \(1\).
|
Input: 421 21 21 2 331 31 45 51 1 1 132 54 60 24 3 2 155 65 71 61 34 79 0 4 5 2 4 | Output: 500000005 750000007 1 1 1 200000005 333333339 333333339 500000005 880952391 801587311 781746041 789304620
|
Master
| 1 | 906 | 624 | 513 | 18 |
1,951 |
I
|
1951I
|
I. Growing Trees
| 3,200 |
binary search; constructive algorithms; flows; graphs; greedy
|
wowaka ft. Hatsune Miku - Ura-Omote LoversΰΆYou are given an undirected connected simple graph with \(n\) nodes and \(m\) edges, where edge \(i\) connects node \(u_i\) and \(v_i\), with two positive parameters \(a_i\) and \(b_i\) attached to it. Additionally, you are also given an integer \(k\).A non-negative array \(x\) with size \(m\) is called a \(k\)-spanning-tree generator if it satisfies the following: Consider the undirected multigraph with \(n\) nodes where edge \(i\) is cloned \(x_i\) times (i.e. there are \(x_i\) edges connecting \(u_i\) and \(v_i\)). It is possible to partition the edges of this graph into \(k\) spanning trees, where each edge belongs to exactly one spanning tree\(^\dagger\). The cost of such array \(x\) is defined as \(\sum_{i = 1}^m a_i x_i^2 + b_i x_i\). Find the minimum cost of a \(k\)-spanning-tree generator.\(^\dagger\) A spanning tree of a (multi)graph is a subset of the graph's edges that form a tree connecting all vertices of the graph.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 500\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\), and \(k\) (\(2 \le n \le 50, n - 1 \le m \le \min(50, \frac{n(n - 1)}{2}), 1 \le k \le 10^7\)) β the number of nodes in the graph, the number of edges in the graph, and the parameter for the \(k\)-spanning-tree generator.Each of the next \(m\) lines of each test case contains four integers \(u_i\), \(v_i\), \(a_i\), and \(b_i\) (\(1 \le u_i, v_i \le n, u_i \neq v_i, 1 \le a_i, b_i \le 1000\)) β the endpoints of the edge \(i\) and its two parameters. It is guaranteed that the graph is simple and connected.It is guaranteed that the sum of \(n^2\) and the sum of \(m^2\) over all test cases does not exceed \(2500\).
|
For each test case, output a single integer: the minimum cost of a \(k\)-spanning-tree generator.
|
In the first test case, a valid \(1\)-spanning-tree generator is \(x = [1, 1, 1, 1, 0]\), as indicated by the following figure. The cost of this generator is \((1^2 \cdot 5 + 1 \cdot 5) + (1^2 \cdot 5 + 1 \cdot 7) + (1^2 \cdot 6 + 1 \cdot 2) + (1^2 \cdot 3 + 1 \cdot 5) + (0^2 \cdot 4 + 0 \cdot 9) = 38\). It can be proven that no other generator has a lower cost. The \(1\)-spanning-tree partition of \(x = [1, 1, 1, 1, 0]\) In the second test case, a valid \(3\)-spanning-tree generator is \(x = [2, 3, 2, 2, 3]\), as indicated by the following figure. The cost of this generator is \((2^2 \cdot 5 + 2 \cdot 5) + (3^2 \cdot 5 + 3 \cdot 7) + (2^2 \cdot 6 + 2 \cdot 2) + (2^2 \cdot 3 + 2 \cdot 5) + (3^2 \cdot 4 + 3 \cdot 9) = 191\). It can be proven that no other generator has a lower cost. The \(3\)-spanning-tree partition of \(x = [2, 3, 2, 2, 3]\)
|
Input: 45 5 14 3 5 52 1 5 72 4 6 25 3 3 52 5 2 95 5 34 3 5 52 1 5 72 4 6 25 3 3 52 5 2 92 1 100000001 2 1000 100010 15 107 1 7 65 8 6 64 8 2 24 3 10 910 8 3 44 6 6 15 4 1 39 3 4 38 3 9 97 5 10 32 1 3 46 1 6 42 5 7 310 7 2 18 2 6 8 | Output: 38 191 100000010000000000 2722
|
Master
| 5 | 986 | 863 | 97 | 19 |
518 |
A
|
518A
|
A. Vitaly and Strings
| 1,600 |
constructive algorithms; strings
|
Vitaly is a diligent student who never missed a lesson in his five years of studying in the university. He always does his homework on time and passes his exams in time. During the last lesson the teacher has provided two strings s and t to Vitaly. The strings have the same length, they consist of lowercase English letters, string s is lexicographically smaller than string t. Vitaly wondered if there is such string that is lexicographically larger than string s and at the same is lexicographically smaller than string t. This string should also consist of lowercase English letters and have the length equal to the lengths of strings s and t. Let's help Vitaly solve this easy problem!
|
The first line contains string s (1 β€ |s| β€ 100), consisting of lowercase English letters. Here, |s| denotes the length of the string.The second line contains string t (|t| = |s|), consisting of lowercase English letters.It is guaranteed that the lengths of strings s and t are the same and string s is lexicographically less than string t.
|
If the string that meets the given requirements doesn't exist, print a single string ""No such string"" (without the quotes).If such string exists, print it. If there are multiple valid strings, you may print any of them.
|
String s = s1s2... sn is said to be lexicographically smaller than t = t1t2... tn, if there exists such i, that s1 = t1, s2 = t2, ... si - 1 = ti - 1, si < ti.
|
Input: ac | Output: b
|
Medium
| 2 | 690 | 340 | 221 | 5 |
1,673 |
A
|
1673A
|
A. Subtle Substring Subtraction
| 800 |
games; greedy; strings
|
Alice and Bob are playing a game with strings. There will be \(t\) rounds in the game. In each round, there will be a string \(s\) consisting of lowercase English letters. Alice moves first and both the players take alternate turns. Alice is allowed to remove any substring of even length (possibly empty) and Bob is allowed to remove any substring of odd length from \(s\).More formally, if there was a string \(s = s_1s_2 \ldots s_k\) the player can choose a substring \(s_ls_{l+1} \ldots s_{r-1}s_r\) with length of corresponding parity and remove it. After that the string will become \(s = s_1 \ldots s_{l-1}s_{r+1} \ldots s_k\).After the string becomes empty, the round ends and each player calculates his/her score for this round. The score of a player is the sum of values of all characters removed by him/her. The value of \(\texttt{a}\) is \(1\), the value of \(\texttt{b}\) is \(2\), the value of \(\texttt{c}\) is \(3\), \(\ldots\), and the value of \(\texttt{z}\) is \(26\). The player with higher score wins the round. For each round, determine the winner and the difference between winner's and loser's scores. Assume that both players play optimally to maximize their score. It can be proved that a draw is impossible.
|
The first line of input contains a single integer \(t\) (\(1\leq t\leq 5\cdot 10^4\)) denoting the number of rounds.Each of the next \(t\) lines contain a single string \(s\) (\(1\leq |s|\leq 2\cdot 10^5\)) consisting of lowercase English letters, denoting the string used for the round. Here \(|s|\) denotes the length of the string \(s\).It is guaranteed that the sum of \(|s|\) over all rounds does not exceed \(2\cdot 10^5\).
|
For each round, print a single line containing a string and an integer. If Alice wins the round, the string must be ""Alice"". If Bob wins the round, the string must be ""Bob"". The integer must be the difference between their scores assuming both players play optimally.
|
For the first round, \(\texttt{""aba""}\xrightarrow{\texttt{Alice}}\texttt{""}{\color{red}{\texttt{ab}}}\texttt{a""}\xrightarrow{} \texttt{""a""}\xrightarrow{\texttt{Bob}}\texttt{""}{\color{red}{\texttt{a}}}\texttt{""}\xrightarrow{}\texttt{""""}\). Alice's total score is \(1+2=3\). Bob's total score is \(1\).For the second round, \(\texttt{""abc""}\xrightarrow{\texttt{Alice}}\texttt{""a}{\color{red}{\texttt{bc}}}\texttt{""}\xrightarrow{} \texttt{""a""}\xrightarrow{\texttt{Bob}}\texttt{""}{\color{red}{\texttt{a}}}\texttt{""}\xrightarrow{}\texttt{""""}\). Alice's total score is \(2+3=5\). Bob's total score is \(1\).For the third round, \(\texttt{""cba""}\xrightarrow{\texttt{Alice}}\texttt{""}{\color{red}{\texttt{cb}}}\texttt{a""}\xrightarrow{} \texttt{""a""}\xrightarrow{\texttt{Bob}}\texttt{""}{\color{red}{\texttt{a}}}\texttt{""}\xrightarrow{}\texttt{""""}\). Alice's total score is \(3+2=5\). Bob's total score is \(1\).For the fourth round, \(\texttt{""n""}\xrightarrow{\texttt{Alice}}\texttt{""n""}\xrightarrow{} \texttt{""n""}\xrightarrow{\texttt{Bob}}\texttt{""}{\color{red}{\texttt{n}}}\texttt{""}\xrightarrow{}\texttt{""""}\). Alice's total score is \(0\). Bob's total score is \(14\).For the fifth round, \(\texttt{""codeforces""}\xrightarrow{\texttt{Alice}}\texttt{""}{\color{red}{\texttt{codeforces}}}\texttt{""}\xrightarrow{} \texttt{""""}\). Alice's total score is \(3+15+4+5+6+15+18+3+5+19=93\). Bob's total score is \(0\).
|
Input: 5abaabccbancodeforces | Output: Alice 2 Alice 4 Alice 4 Bob 14 Alice 93
|
Beginner
| 3 | 1,234 | 429 | 271 | 16 |
2,060 |
G
|
2060G
|
G. Bugged Sort
| 2,400 |
dp; greedy; sortings
|
Today, Alice has given Bob arrays for him to sort in increasing order again! At this point, no one really knows how many times she has done this.Bob is given two sequences \(a\) and \(b\), both of length \(n\). All integers in the range from \(1\) to \(2n\) appear exactly once in either \(a\) or \(b\). In other words, the concatenated\(^{\text{β}}\) sequence \(a+b\) is a permutation\(^{\text{β }}\) of length \(2n\).Bob must sort both sequences in increasing order at the same time using Alice's swap function. Alice's swap function is implemented as follows: Given two indices \(i\) and \(j\) (\(i \neq j\)), it swaps \(a_i\) with \(b_j\), and swaps \(b_i\) with \(a_j\). Given sequences \(a\) and \(b\), please determine if both sequences can be sorted in increasing order simultaneously after using Alice's swap function any number of times.\(^{\text{β}}\)The concatenated sequence \(a+b\) denotes the sequence \([a_1, a_2, a_3, \ldots , b_1, b_2, b_3, \ldots]\).\(^{\text{β }}\)A permutation of length \(m\) contains all integers from \(1\) to \(m\) in some order.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)).The second line of each test case contains \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le 2n\)).The third line of each test case contains \(b_1,b_2,\ldots,b_n\) (\(1 \le b_i \le 2n\)).It is guaranteed that all integers in the range \([1,2n]\) appear exactly once in either \(a\) or \(b\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
If it is possible to sort both sequences simultaneously, print ""YES"" on a new line. Otherwise, print ""NO"" on a new line.You can output the answer in any case. For example, the strings ""yEs"", ""yes"", and ""Yes"" will also be recognized as positive responses.
|
In the first test case, it can be shown that it is impossible.In the second test case, Bob can perform one operation with indices \(i=1\) and \(j=2\). The arrays become \([3,4,5]\) and \([1,2,6]\) respectively. Both arrays are now sorted.
|
Input: 532 1 34 6 532 1 54 3 641 6 4 35 2 8 745 3 7 18 6 4 275 1 9 12 3 13 72 4 11 14 6 10 8 | Output: NO YES NO YES YES
|
Expert
| 3 | 1,069 | 630 | 264 | 20 |
375 |
C
|
375C
|
C. Circling Round Treasures
| 2,600 |
bitmasks; shortest paths
|
You have a map as a rectangle table. Each cell of the table is either an obstacle, or a treasure with a certain price, or a bomb, or an empty cell. Your initial position is also given to you.You can go from one cell of the map to a side-adjacent one. At that, you are not allowed to go beyond the borders of the map, enter the cells with treasures, obstacles and bombs. To pick the treasures, you need to build a closed path (starting and ending in the starting cell). The closed path mustn't contain any cells with bombs inside. Let's assume that the sum of the treasures' values that are located inside the closed path equals v, and besides, you've made k single moves (from one cell to another) while you were going through the path, then such path brings you the profit of v - k rubles.Your task is to build a closed path that doesn't contain any bombs and brings maximum profit.Note that the path can have self-intersections. In order to determine if a cell lies inside a path or not, use the following algorithm: Assume that the table cells are points on the plane (the table cell on the intersection of the i-th column and the j-th row is point (i, j)). And the given path is a closed polyline that goes through these points. You need to find out if the point p of the table that is not crossed by the polyline lies inside the polyline. Let's draw a ray that starts from point p and does not intersect other points of the table (such ray must exist). Let's count the number of segments of the polyline that intersect the painted ray. If this number is odd, we assume that point p (and consequently, the table cell) lie inside the polyline (path). Otherwise, we assume that it lies outside.
|
The first line contains two integers n and m (1 β€ n, m β€ 20) β the sizes of the table. Next n lines each contains m characters β the description of the table. The description means the following: character ""B"" is a cell with a bomb; character ""S"" is the starting cell, you can assume that it's empty; digit c (1-8) is treasure with index c; character ""."" is an empty cell; character ""#"" is an obstacle. Assume that the map has t treasures. Next t lines contain the prices of the treasures. The i-th line contains the price of the treasure with index i, vi ( - 200 β€ vi β€ 200). It is guaranteed that the treasures are numbered from 1 to t. It is guaranteed that the map has not more than 8 objects in total. Objects are bombs and treasures. It is guaranteed that the map has exactly one character ""S"".
|
Print a single integer β the maximum possible profit you can get.
|
In the first example the answer will look as follows. In the second example the answer will look as follows. In the third example you cannot get profit.In the fourth example you cannot get profit as you cannot construct a closed path with more than one cell.
|
Input: 4 4.....S1.........10 | Output: 2
|
Expert
| 2 | 1,696 | 810 | 65 | 3 |
231 |
B
|
231B
|
B. Magic, Wizardry and Wonders
| 1,500 |
constructive algorithms; greedy
|
Vasya the Great Magician and Conjurer loves all kinds of miracles and wizardry. In one wave of a magic wand he can turn an object into something else. But, as you all know, there is no better magic in the Universe than the magic of numbers. That's why Vasya adores math and spends a lot of time turning some numbers into some other ones.This morning he has n cards with integers lined up in front of him. Each integer is not less than 1, but not greater than l. When Vasya waves his magic wand, two rightmost cards vanish from the line and a new card magically appears in their place. It contains the difference between the left and the right numbers on the two vanished cards. Vasya was very interested to know what would happen next, and so he waved with his magic wand on and on, until the table had a single card left.Suppose that Vasya originally had the following cards: 4, 1, 1, 3 (listed from left to right). Then after the first wave the line would be: 4, 1, -2, and after the second one: 4, 3, and after the third one the table would have a single card with number 1.Please note that in spite of the fact that initially all the numbers on the cards were not less than 1 and not greater than l, the numbers on the appearing cards can be anything, no restrictions are imposed on them.It is now evening. Vasya is very tired and wants to return everything back, but does not remember which cards he had in the morning. He only remembers that there were n cards, they contained integers from 1 to l, and after all magical actions he was left with a single card containing number d.Help Vasya to recover the initial set of cards with numbers.
|
The single line contains three space-separated integers: n (2 β€ n β€ 100) β the initial number of cards on the table, d (|d| β€ 104) β the number on the card that was left on the table after all the magical actions, and l (1 β€ l β€ 100) β the limits for the initial integers.
|
If Vasya is mistaken, that is, if there doesn't exist a set that meets the requirements given in the statement, then print a single number -1, otherwise print the sought set containing n integers from 1 to l. Separate the integers by spaces. Print the integers in the order, in which they were written on the cards from left to right. If there are several suitable sets of numbers, you can print any of them.
|
Input: 3 3 2 | Output: 2 1 2
|
Medium
| 2 | 1,646 | 272 | 408 | 2 |
|
33 |
A
|
33A
|
A. What is for dinner?
| 1,200 |
greedy; implementation
|
In one little known, but very beautiful country called Waterland, lives a lovely shark Valerie. Like all the sharks, she has several rows of teeth, and feeds on crucians. One of Valerie's distinguishing features is that while eating one crucian she uses only one row of her teeth, the rest of the teeth are ""relaxing"".For a long time our heroine had been searching the sea for crucians, but a great misfortune happened. Her teeth started to ache, and she had to see the local dentist, lobster Ashot. As a professional, Ashot quickly relieved Valerie from her toothache. Moreover, he managed to determine the cause of Valerie's developing caries (for what he was later nicknamed Cap).It turned that Valerie eats too many crucians. To help Valerie avoid further reoccurrence of toothache, Ashot found for each Valerie's tooth its residual viability. Residual viability of a tooth is a value equal to the amount of crucians that Valerie can eat with this tooth. Every time Valerie eats a crucian, viability of all the teeth used for it will decrease by one. When the viability of at least one tooth becomes negative, the shark will have to see the dentist again. Unhappy, Valerie came back home, where a portion of crucians was waiting for her. For sure, the shark couldn't say no to her favourite meal, but she had no desire to go back to the dentist. That's why she decided to eat the maximum amount of crucians from the portion but so that the viability of no tooth becomes negative. As Valerie is not good at mathematics, she asked you to help her to find out the total amount of crucians that she can consume for dinner.We should remind you that while eating one crucian Valerie uses exactly one row of teeth and the viability of each tooth from this row decreases by one.
|
The first line contains three integers n, m, k (1 β€ m β€ n β€ 1000, 0 β€ k β€ 106) β total amount of Valerie's teeth, amount of tooth rows and amount of crucians in Valerie's portion for dinner. Then follow n lines, each containing two integers: r (1 β€ r β€ m) β index of the row, where belongs the corresponding tooth, and c (0 β€ c β€ 106) β its residual viability.It's guaranteed that each tooth row has positive amount of teeth.
|
In the first line output the maximum amount of crucians that Valerie can consume for dinner.
|
Input: 4 3 182 31 23 62 3 | Output: 11
|
Easy
| 2 | 1,776 | 425 | 92 | 0 |
|
1,811 |
G1
|
1811G1
|
G1. Vlad and the Nice Paths (easy version)
| 2,100 |
combinatorics; dp; math
|
This is an easy version of the problem, it differs from the hard one only by constraints on \(n\) and \(k\).Vlad found a row of \(n\) tiles and the integer \(k\). The tiles are indexed from left to right and the \(i\)-th tile has the color \(c_i\). After a little thought, he decided what to do with it.You can start from any tile and jump to any number of tiles right, forming the path \(p\). Let's call the path \(p\) of length \(m\) nice if: \(p\) can be divided into blocks of length exactly \(k\), that is, \(m\) is divisible by \(k\); \(c_{p_1} = c_{p_2} = \ldots = c_{p_k}\); \(c_{p_{k+1}} = c_{p_{k+2}} = \ldots = c_{p_{2k}}\); \(\ldots\) \(c_{p_{m-k+1}} = c_{p_{m-k+2}} = \ldots = c_{p_{m}}\); Your task is to find the number of nice paths of maximum length. Since this number may be too large, print it modulo \(10^9 + 7\).
|
The first line of each test contains the integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 100\)) β the number of tiles in a row and the length of the block.The second line of each test case contains \(n\) integers \(c_1, c_2, c_3, \dots, c_n\) (\(1 \le c_i \le n\)) β tile colors.It is guaranteed that the sum of \(n^3\) over all test cases does not exceed \(5 \cdot 10^6\).
|
Print \(t\) numbers, each of which is the answer to the corresponding test case β the number of nice paths of maximum length modulo \(10^9 + 7\).
|
In the first sample, it is impossible to make a nice path with a length greater than \(0\).In the second sample, we are interested in the following paths: \(1 \rightarrow 3 \rightarrow 4 \rightarrow 5\) \(2 \rightarrow 4 \rightarrow 5 \rightarrow 7\) \(1 \rightarrow 3 \rightarrow 5 \rightarrow 7\) \(1 \rightarrow 3 \rightarrow 4 \rightarrow 7\) In the third example, any path of length \(8\) is nice.
|
Input: 55 21 2 3 4 57 21 3 1 3 3 1 311 41 1 1 1 1 1 1 1 1 1 15 21 1 2 2 25 11 2 3 4 5 | Output: 1 4 165 3 1
|
Hard
| 3 | 833 | 493 | 145 | 18 |
1,366 |
C
|
1366C
|
C. Palindromic Paths
| 1,500 |
greedy; math
|
You are given a matrix with \(n\) rows (numbered from \(1\) to \(n\)) and \(m\) columns (numbered from \(1\) to \(m\)). A number \(a_{i, j}\) is written in the cell belonging to the \(i\)-th row and the \(j\)-th column, each number is either \(0\) or \(1\).A chip is initially in the cell \((1, 1)\), and it will be moved to the cell \((n, m)\). During each move, it either moves to the next cell in the current row, or in the current column (if the current cell is \((x, y)\), then after the move it can be either \((x + 1, y)\) or \((x, y + 1)\)). The chip cannot leave the matrix.Consider each path of the chip from \((1, 1)\) to \((n, m)\). A path is called palindromic if the number in the first cell is equal to the number in the last cell, the number in the second cell is equal to the number in the second-to-last cell, and so on.Your goal is to change the values in the minimum number of cells so that every path is palindromic.
|
The first line contains one integer \(t\) (\(1 \le t \le 200\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n, m \le 30\)) β the dimensions of the matrix.Then \(n\) lines follow, the \(i\)-th line contains \(m\) integers \(a_{i, 1}\), \(a_{i, 2}\), ..., \(a_{i, m}\) (\(0 \le a_{i, j} \le 1\)).
|
For each test case, print one integer β the minimum number of cells you have to change so that every path in the matrix is palindromic.
|
The resulting matrices in the first three test cases: \(\begin{pmatrix} 1 & 1\\ 0 & 1 \end{pmatrix}\) \(\begin{pmatrix} 0 & 0 & 0\\ 0 & 0 & 0 \end{pmatrix}\) \(\begin{pmatrix} 1 & 0 & 1 & 1 & 1 & 1 & 1\\ 0 & 1 & 1 & 0 & 1 & 1 & 0\\ 1 & 1 & 1 & 1 & 1 & 0 & 1 \end{pmatrix}\)
|
Input: 4 2 2 1 1 0 1 2 3 1 1 0 1 0 0 3 7 1 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 1 1 1 0 1 3 5 1 0 1 0 0 1 1 1 1 0 0 0 1 0 0 | Output: 0 3 4 4
|
Medium
| 2 | 937 | 357 | 135 | 13 |
1,248 |
B
|
1248B
|
B. Grow The Tree
| 900 |
greedy; math; sortings
|
Gardener Alexey teaches competitive programming to high school students. To congratulate Alexey on the Teacher's Day, the students have gifted him a collection of wooden sticks, where every stick has an integer length. Now Alexey wants to grow a tree from them.The tree looks like a polyline on the plane, consisting of all sticks. The polyline starts at the point \((0, 0)\). While constructing the polyline, Alexey will attach sticks to it one by one in arbitrary order. Each stick must be either vertical or horizontal (that is, parallel to \(OX\) or \(OY\) axis). It is not allowed for two consecutive sticks to be aligned simultaneously horizontally or simultaneously vertically. See the images below for clarification.Alexey wants to make a polyline in such a way that its end is as far as possible from \((0, 0)\). Please help him to grow the tree this way.Note that the polyline defining the form of the tree may have self-intersections and self-touches, but it can be proved that the optimal answer does not contain any self-intersections or self-touches.
|
The first line contains an integer \(n\) (\(1 \le n \le 100\,000\)) β the number of sticks Alexey got as a present.The second line contains \(n\) integers \(a_1, \ldots, a_n\) (\(1 \le a_i \le 10\,000\)) β the lengths of the sticks.
|
Print one integer β the square of the largest possible distance from \((0, 0)\) to the tree end.
|
The following pictures show optimal trees for example tests. The squared distance in the first example equals \(5 \cdot 5 + 1 \cdot 1 = 26\), and in the second example \(4 \cdot 4 + 2 \cdot 2 = 20\).
|
Input: 3 1 2 3 | Output: 26
|
Beginner
| 3 | 1,064 | 232 | 96 | 12 |
1,275 |
C
|
1275C
|
C. #define ΠΠ°Π΄Π°ΡΠ° B ...
| 0 |
*special
|
ΠΠΎΠΌΠΏΠ°Π½ΠΈΡ Β«ΠΠΠΎΠ½ΡΠ°ΠΊΡΠ΅Β» Π°ΠΊΡΠΈΠ²Π½ΠΎ ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅Ρ ΡΠ·ΡΠΊΠΈ C/C++ Π΄Π»Ρ ΡΠ°Π·ΡΠ°Π±ΠΎΡΠΊΠΈ Π΄Π²ΠΈΠΆΠΊΠΎΠ². Π‘ΡΠ°ΡΡΠΈΠΉ ΡΠ°Π·ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΠ°ΡΡ β Π±ΠΎΠ»ΡΡΠΎΠΉ Π»ΡΠ±ΠΈΡΠ΅Π»Ρ ΡΠ·ΡΠΊΠ° C, Π²Π΅Π΄Ρ ΡΠΎΠ»ΡΠΊΠΎ Π² Π½Π΅ΠΌ ΠΌΠΎΠΆΠ½ΠΎ ΠΏΠΎΠ»Π½ΠΎΡΡΡΡ ΡΠ°ΡΠΊΡΡΡΡ ΠΏΠΎΡΠ΅Π½ΡΠΈΠ°Π» Define-Oriented Programming. Π ΡΠ·ΡΠΊΠ΅ C Π΄ΠΈΡΠ΅ΠΊΡΠΈΠ²Π° #define ΠΏΠΎΠ·Π²ΠΎΠ»ΡΠ΅Ρ ΡΠ΄Π΅Π»Π°ΡΡ ΠΏΡΡΠΌΡΡ ΠΏΠΎΠ΄ΡΡΠ°Π½ΠΎΠ²ΠΊΡ ΠΊΡΡΠΊΠ° ΠΊΠΎΠ΄Π° Π² Π»ΡΠ±ΠΎΠ΅ ΠΌΠ΅ΡΡΠΎ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ. ΠΠ°ΠΏΡΠΈΠΌΠ΅Ρ, ΠΏΡΠΈ ΠΈΡΠΏΠΎΠ»Π½Π΅Π½ΠΈΠΈ ΡΠ»Π΅Π΄ΡΡΡΠ΅Π³ΠΎ ΠΊΠΎΠ΄Π° Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ \(v\) Π±ΡΠ΄Π΅Ρ ΡΠ°Π²Π½ΠΎ \(11\) (Π² ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ \(v\) Π·Π°ΠΏΠΈΡΠ΅ΡΡΡ ΡΠ΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΡΠ°ΠΆΠ΅Π½ΠΈΡ \(3 + 4 \cdot 2\)).#define add(x) + x#define mul(y) * yint v = 3 add(4) mul(2);ΠΠ΅Π΄Π°Π²Π½ΠΎ ΠΠ°ΡΡ Π½Π°ΠΏΠΈΡΠ°Π» Π½Π΅Π±ΠΎΠ»ΡΡΡΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ, ΠΊΠΎΡΠΎΡΠ°Ρ Π·Π°ΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΌΠ°ΡΡΠΈΠ² Π±ΠΎΠ»ΡΡΠΎΠΉ Π΄Π»ΠΈΠ½Ρ. ΠΡΠΎΠ³ΡΠ°ΠΌΠΌΠ° Π²ΡΠ³Π»ΡΠ΄ΠΈΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ:#define A0(x) x,#define A1(x) A0(x) A0(x + 1) A0(x + 3) A0(x + 4)#define A2(x) A1(x) A1(x + 1) A1(x + 3) A1(x + 4)#define A3(x) A2(x) A2(x + 1) A2(x + 3) A2(x + 4)#define A4(x) A3(x) A3(x + 1) A3(x + 3) A3(x + 4)...#define A24(x) A23(x) A23(x + 1) A23(x + 3) A23(x + 4)#define A25(x) A24(x) A24(x + 1) A24(x + 3) A24(x + 4)const long long values[1125899906842624] = { A25(0) };Π ΡΠΎΠΆΠ°Π»Π΅Π½ΠΈΡ, Π΅Π³ΠΎ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠ° Π½Π΅ ΠΊΠΎΠΌΠΏΠΈΠ»ΠΈΡΡΠ΅ΡΡΡ Π² ΡΠΈΠ»Ρ Π½Π΅ΡΠΎΠ²Π΅ΡΡΠ΅Π½ΡΡΠ²Π° Π΅Π³ΠΎ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ°, Π½ΠΎ Π΅ΠΌΡ ΠΎΡΠ΅Π½Ρ ΠΈΠ½ΡΠ΅ΡΠ΅ΡΠ½ΠΎ Π·Π½Π°ΡΡ, ΠΊΠ°ΠΊΠΈΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π»Π΅ΠΆΠ°Π»ΠΈ Π±Ρ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ \(values\), Π΅ΡΠ»ΠΈ Π±Ρ Π΅ΠΌΡ ΡΠ΄Π°Π»ΠΎΡΡ ΡΠΊΠΎΠΌΠΏΠΈΠ»ΠΈΡΠΎΠ²Π°ΡΡ ΠΈ Π·Π°ΠΏΡΡΡΠΈΡΡ ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΡ. ΠΠΎΠΌΠΎΠ³ΠΈΡΠ΅ Π΅ΠΌΡ ΡΡΠΎ ΡΠ·Π½Π°ΡΡ.
|
Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π΄Π°Π½ΠΎ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ \(n\) (\(1 \le n \le 1\,000\)) β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² ΠΌΠ°ΡΡΠΈΠ²Π°, Π·Π½Π°ΡΠ΅Π½ΠΈΡ ΠΊΠΎΡΠΎΡΡΡ
ΠΈΠ½ΡΠ΅ΡΠ΅ΡΡΡΡ ΠΠ°ΡΡ.Π‘Π»Π΅Π΄ΡΡΡΠΈΠ΅ \(n\) ΡΡΡΠΎΠΊ ΡΠΎΠ΄Π΅ΡΠΆΠ°Ρ \(n\) ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» \(pos_i\) (\(0 \le pos_i \le 1\,125\,899\,906\,842\,623\)) β ΠΏΠΎΠ·ΠΈΡΠΈΠΈ, Π΄Π»Ρ ΠΊΠΎΡΠΎΡΡΡ
Π½ΡΠΆΠ½ΠΎ ΡΠ·Π½Π°ΡΡ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅.
|
ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ \(n\) ΡΡΡΠΎΠΊ. Π \(i\)-ΠΉ ΡΡΡΠΎΠΊΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΡΠΎΠ΄Π΅ΡΠΆΠ°ΡΡΡΡ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΌΠ°ΡΡΠΈΠ²Π° Π½Π° ΠΏΠΎΠ·ΠΈΡΠΈΠΈ \(pos_i\).
|
ΠΠ°ΡΠ°Π»ΠΎ ΡΡΠΎΠ³ΠΎ ΠΌΠ°ΡΡΠΈΠ²Π° Π²ΡΠ³Π»ΡΠ΄ΠΈΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ:const long long values[1125899906842624] = { 0, 1, 3, 4, 1, 2, 4, ... };
|
Input: 5 0 1 3 5 8 | Output: 0 1 4 2 3
|
Beginner
| 1 | 1,264 | 297 | 105 | 12 |
1,310 |
A
|
1310A
|
A. Recommendations
| 1,700 |
data structures; greedy; sortings
|
VK news recommendation system daily selects interesting publications of one of \(n\) disjoint categories for each user. Each publication belongs to exactly one category. For each category \(i\) batch algorithm selects \(a_i\) publications.The latest A/B test suggests that users are reading recommended publications more actively if each category has a different number of publications within daily recommendations. The targeted algorithm can find a single interesting publication of \(i\)-th category within \(t_i\) seconds. What is the minimum total time necessary to add publications to the result of batch algorithm execution, so all categories have a different number of publications? You can't remove publications recommended by the batch algorithm.
|
The first line of input consists of single integer \(n\) β the number of news categories (\(1 \le n \le 200\,000\)).The second line of input consists of \(n\) integers \(a_i\) β the number of publications of \(i\)-th category selected by the batch algorithm (\(1 \le a_i \le 10^9\)).The third line of input consists of \(n\) integers \(t_i\) β time it takes for targeted algorithm to find one new publication of category \(i\) (\(1 \le t_i \le 10^5)\).
|
Print one integer β the minimal required time for the targeted algorithm to get rid of categories with the same size.
|
In the first example, it is possible to find three publications of the second type, which will take 6 seconds.In the second example, all news categories contain a different number of publications.
|
Input: 5 3 7 9 7 8 5 2 5 7 5 | Output: 6
|
Medium
| 3 | 755 | 452 | 117 | 13 |
430 |
A
|
430A
|
A. Points and Segments (easy)
| 1,600 |
constructive algorithms; sortings
|
Iahub isn't well prepared on geometry problems, but he heard that this year there will be a lot of geometry problems on the IOI selection camp. Scared, Iahub locked himself in the basement and started thinking of new problems of this kind. One of them is the following.Iahub wants to draw n distinct points and m segments on the OX axis. He can draw each point with either red or blue. The drawing is good if and only if the following requirement is met: for each segment [li, ri] consider all the red points belong to it (ri points), and all the blue points belong to it (bi points); each segment i should satisfy the inequality |ri - bi| β€ 1.Iahub thinks that point x belongs to segment [l, r], if inequality l β€ x β€ r holds.Iahub gives to you all coordinates of points and segments. Please, help him to find any good drawing.
|
The first line of input contains two integers: n (1 β€ n β€ 100) and m (1 β€ m β€ 100). The next line contains n space-separated integers x1, x2, ..., xn (0 β€ xi β€ 100) β the coordinates of the points. The following m lines contain the descriptions of the m segments. Each line contains two integers li and ri (0 β€ li β€ ri β€ 100) β the borders of the i-th segment.It's guaranteed that all the points are distinct.
|
If there is no good drawing for a given test, output a single integer -1. Otherwise output n integers, each integer must be 0 or 1. The i-th number denotes the color of the i-th point (0 is red, and 1 is blue).If there are multiple good drawings you can output any of them.
|
Input: 3 33 7 141 56 1011 15 | Output: 0 0 0
|
Medium
| 2 | 828 | 409 | 273 | 4 |
|
71 |
D
|
71D
|
D. Solitaire
| 2,200 |
brute force; implementation
|
Vasya has a pack of 54 cards (52 standard cards and 2 distinct jokers). That is all he has at the moment. Not to die from boredom, Vasya plays Solitaire with them.Vasya lays out nm cards as a rectangle n Γ m. If there are jokers among them, then Vasya should change them with some of the rest of 54 - nm cards (which are not layed out) so that there were no jokers left. Vasya can pick the cards to replace the jokers arbitrarily. Remember, that each card presents in pack exactly once (i. e. in a single copy). Vasya tries to perform the replacements so that the solitaire was solved.Vasya thinks that the solitaire is solved if after the jokers are replaced, there exist two non-overlapping squares 3 Γ 3, inside each of which all the cards either have the same suit, or pairwise different ranks.Determine by the initial position whether the solitaire can be solved or not. If it can be solved, show the way in which it is possible.
|
The first line contains integers n and m (3 β€ n, m β€ 17, n Γ m β€ 52). Next n lines contain m words each. Each word consists of two letters. The jokers are defined as ""J1"" and ""J2"" correspondingly. For the rest of the cards, the first letter stands for the rank and the second one β for the suit. The possible ranks are: ""2"", ""3"", ""4"", ""5"", ""6"", ""7"", ""8"", ""9"", ""T"", ""J"", ""Q"", ""K"" and ""A"". The possible suits are: ""C"", ""D"", ""H"" and ""S"". All the cards are different.
|
If the Solitaire can be solved, print on the first line ""Solution exists."" without the quotes. On the second line print in what way the jokers can be replaced. Three variants are possible: ""There are no jokers."", if there are no jokers in the input data. ""Replace Jx with y."", if there is one joker. x is its number, and y is the card it should be replaced with. ""Replace J1 with x and J2 with y."", if both jokers are present in the input data. x and y here represent distinct cards with which one should replace the first and the second jokers correspondingly.On the third line print the coordinates of the upper left corner of the first square 3 Γ 3 in the format ""Put the first square to (r, c)."", where r and c are the row and the column correspondingly. In the same manner print on the fourth line the coordinates of the second square 3 Γ 3 in the format ""Put the second square to (r, c)."".If there are several solutions to that problem, print any of them.If there are no solutions, print of the single line ""No solution."" without the quotes.See the samples to understand the output format better.
|
The pretests cover all the possible output formats.
|
Input: 4 62S 3S 4S 7S 8S AS5H 6H 7H 5S TC AC8H 9H TH 7C 8C 9C2D 2C 3C 4C 5C 6C | Output: No solution.
|
Hard
| 2 | 934 | 501 | 1,116 | 0 |
431 |
E
|
431E
|
E. Chemistry Experiment
| 2,200 |
binary search; data structures; ternary search
|
One day two students, Grisha and Diana, found themselves in the university chemistry lab. In the lab the students found n test tubes with mercury numbered from 1 to n and decided to conduct an experiment.The experiment consists of q steps. On each step, one of the following actions occurs: Diana pours all the contents from tube number pi and then pours there exactly xi liters of mercury. Let's consider all the ways to add vi liters of water into the tubes; for each way let's count the volume of liquid (water and mercury) in the tube with water with maximum amount of liquid; finally let's find the minimum among counted maximums. That is the number the students want to count. At that, the students don't actually pour the mercury. They perform calculations without changing the contents of the tubes. Unfortunately, the calculations proved to be too complex and the students asked you to help them. Help them conduct the described experiment.
|
The first line contains two integers n and q (1 β€ n, q β€ 105) β the number of tubes ans the number of experiment steps. The next line contains n space-separated integers: h1, h2, ..., hn (0 β€ hi β€ 109), where hi is the volume of mercury in the Ρ-th tube at the beginning of the experiment.The next q lines contain the game actions in the following format: A line of form ""1 pi xi"" means an action of the first type (1 β€ pi β€ n; 0 β€ xi β€ 109). A line of form ""2 vi"" means an action of the second type (1 β€ vi β€ 1015). It is guaranteed that there is at least one action of the second type. It is guaranteed that all numbers that describe the experiment are integers.
|
For each action of the second type print the calculated value. The answer will be considered correct if its relative or absolute error doesn't exceed 10 - 4.
|
Input: 3 31 2 02 21 2 12 3 | Output: 1.500001.66667
|
Hard
| 3 | 949 | 668 | 157 | 4 |
|
512 |
E
|
512E
|
E. Fox And Polygon
| 2,900 |
constructive algorithms; divide and conquer
|
Fox Ciel just designed a puzzle game called ""Polygon""! It is played using triangulations of a regular n-edge polygon. The goal is to transform one triangulation to another by some tricky rules.Triangulation of an n-edge poylgon is a set of n - 3 diagonals satisfying the condition that no two diagonals share a common internal point.For example, the initial state of the game may look like (a) in above figure. And your goal may look like (c). In each step you can choose a diagonal inside the polygon (but not the one of edges of the polygon) and flip this diagonal. Suppose you are going to flip a diagonal a β b. There always exist two triangles sharing a β b as a side, let's denote them as a β b β c and a β b β d. As a result of this operation, the diagonal a β b is replaced by a diagonal c β d. It can be easily proven that after flip operation resulting set of diagonals is still a triangulation of the polygon.So in order to solve above case, you may first flip diagonal 6 β 3, it will be replaced by diagonal 2 β 4. Then you flip diagonal 6 β 4 and get figure (c) as result.Ciel just proved that for any starting and destination triangulations this game has a solution. She wants you to solve it in no more than 20 000 steps for any puzzle satisfying n β€ 1000.
|
The first line contain an integer n (4 β€ n β€ 1000), number of edges of the regular polygon. Then follows two groups of (n - 3) lines describing the original triangulation and goal triangulation.Description of each triangulation consists of (n - 3) lines. Each line contains 2 integers ai and bi (1 β€ ai, bi β€ n), describing a diagonal ai β bi.It is guaranteed that both original and goal triangulations are correct (i. e. no two diagonals share a common internal point in both of these triangulations).
|
First, output an integer k (0 β€ k β€ 20, 000): number of steps.Then output k lines, each containing 2 integers ai and bi: the endpoints of a diagonal you are going to flip at step i. You may output ai and bi in any order.If there are several possible solutions, output any of them.
|
Sample test 2 is discussed above and shown on the picture.
|
Input: 41 32 4 | Output: 11 3
|
Master
| 2 | 1,273 | 502 | 280 | 5 |
1,002 |
B3
|
1002B3
|
B3. Distinguish four 2-qubit states
| 1,600 |
*special
|
You are given 2 qubits which are guaranteed to be in one of the four orthogonal states: Your task is to perform necessary operations and measurements to figure out which state it was and to return the index of that state (0 for , 1 for etc.). The state of the qubits after the operations does not matter.You have to implement an operation which takes an array of 2 qubits as an input and returns an integer. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (qs : Qubit[]) : Int { body { // your code here } }}
|
Medium
| 1 | 607 | 0 | 0 | 10 |
||||
1,686 |
A
|
1686A
|
A. Everything Everywhere All But One
| 800 |
greedy
|
You are given an array of \(n\) integers \(a_1, a_2, \ldots, a_n\). After you watched the amazing film ""Everything Everywhere All At Once"", you came up with the following operation.In one operation, you choose \(n-1\) elements of the array and replace each of them with their arithmetic mean (which doesn't have to be an integer). For example, from the array \([1, 2, 3, 1]\) we can get the array \([2, 2, 2, 1]\), if we choose the first three elements, or we can get the array \([\frac{4}{3}, \frac{4}{3}, 3, \frac{4}{3}]\), if we choose all elements except the third.Is it possible to make all elements of the array equal by performing a finite number of such operations?
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 200\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 50\)) β the number of integers.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 100\)).
|
For each test case, if it is possible to make all elements equal after some number of operations, output \(\texttt{YES}\). Otherwise, output \(\texttt{NO}\).You can output \(\texttt{YES}\) and \(\texttt{NO}\) in any case (for example, strings \(\texttt{yEs}\), \(\texttt{yes}\), \(\texttt{Yes}\) will be recognized as a positive response).
|
In the first test case, all elements are already equal.In the second test case, you can choose all elements except the third, their average is \(\frac{1 + 2 + 4 + 5}{4} = 3\), so the array will become \([3, 3, 3, 3, 3]\).It's possible to show that it's impossible to make all elements equal in the third and fourth test cases.
|
Input: 4342 42 4251 2 3 4 544 3 2 1324 2 22 | Output: YES YES NO NO
|
Beginner
| 1 | 675 | 371 | 339 | 16 |
1,676 |
D
|
1676D
|
D. X-Sum
| 1,000 |
brute force; greedy; implementation
|
Timur's grandfather gifted him a chessboard to practice his chess skills. This chessboard is a grid \(a\) with \(n\) rows and \(m\) columns with each cell having a non-negative integer written on it. Timur's challenge is to place a bishop on the board such that the sum of all cells attacked by the bishop is maximal. The bishop attacks in all directions diagonally, and there is no limit to the distance which the bishop can attack. Note that the cell on which the bishop is placed is also considered attacked. Help him find the maximal sum he can get.
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains the integers \(n\) and \(m\) (\(1 \le n \le 200\), \(1 \leq m \leq 200\)).The following \(n\) lines contain \(m\) integers each, the \(j\)-th element of the \(i\)-th line \(a_{ij}\) is the number written in the \(j\)-th cell of the \(i\)-th row \((0\leq a_{ij} \leq 10^6)\)It is guaranteed that the sum of \(n\cdot m\) over all test cases does not exceed \(4\cdot10^4\).
|
For each test case output a single integer, the maximum sum over all possible placements of the bishop.
|
For the first test case here the best sum is achieved by the bishop being in this position:
|
Input: 44 41 2 2 12 4 2 42 2 3 12 4 2 42 1103 31 1 11 1 11 1 13 30 1 11 0 11 1 0 | Output: 20 1 5 3
|
Beginner
| 3 | 553 | 561 | 103 | 16 |
276 |
D
|
276D
|
D. Little Girl and Maximum XOR
| 1,700 |
bitmasks; dp; greedy; implementation; math
|
A little girl loves problems on bitwise operations very much. Here's one of them.You are given two integers l and r. Let's consider the values of for all pairs of integers a and b (l β€ a β€ b β€ r). Your task is to find the maximum value among all considered ones.Expression means applying bitwise excluding or operation to integers x and y. The given operation exists in all modern programming languages, for example, in languages C++ and Java it is represented as ""^"", in Pascal β as ""xor"".
|
The single line contains space-separated integers l and r (1 β€ l β€ r β€ 1018).Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
In a single line print a single integer β the maximum value of for all pairs of integers a, b (l β€ a β€ b β€ r).
|
Input: 1 2 | Output: 3
|
Medium
| 5 | 494 | 224 | 110 | 2 |
|
46 |
E
|
46E
|
E. Comb
| 1,900 |
data structures; dp
|
Having endured all the hardships, Lara Croft finally found herself in a room with treasures. To her surprise she didn't find golden mountains there. Lara looked around and noticed on the floor a painted table n Γ m panels in size with integers written on the panels. There also was a huge number of stones lying by the wall. On the pillar near the table Lara found a guidance note which said that to get hold of the treasures one has to choose some non-zero number of the first panels in each row of the table and put stones on all those panels to push them down. After that she will receive a number of golden coins equal to the sum of numbers written on the chosen panels. Lara quickly made up her mind on how to arrange the stones and was about to start when she noticed an addition to the note in small font below. According to the addition, for the room ceiling not to crush and smash the adventurer, the chosen panels should form a comb. It was explained that the chosen panels form a comb when the sequence c1, c2, ..., cn made from the quantities of panels chosen in each table line satisfies the following property: c1 > c2 < c3 > c4 < ..., i.e. the inequation mark interchanges between the neighboring elements. Now Lara is bewildered and doesn't know what to do. Help her to determine the largest number of coins she can get and survive at the same time.
|
The first line contains a pair of integers n, m (2 β€ n, m β€ 1500). Next n lines contain m integers each β that is the table itself. The absolute value of the numbers in the table does not exceed 10000.
|
Print the single number β the maximum number of coins Lara can get.
|
Input: 2 2-1 21 3 | Output: 2
|
Hard
| 2 | 1,365 | 201 | 67 | 0 |
|
83 |
C
|
83C
|
C. Track
| 2,400 |
graphs; greedy; shortest paths
|
You already know that Valery's favorite sport is biathlon. Due to your help, he learned to shoot without missing, and his skills are unmatched at the shooting range. But now a smaller task is to be performed, he should learn to complete the path fastest.The track's map is represented by a rectangle n Γ m in size divided into squares. Each square is marked with a lowercase Latin letter (which means the type of the plot), with the exception of the starting square (it is marked with a capital Latin letters S) and the terminating square (it is marked with a capital Latin letter T). The time of movement from one square to another is equal to 1 minute. The time of movement within the cell can be neglected. We can move from the cell only to side-adjacent ones, but it is forbidden to go beyond the map edges. Also the following restriction is imposed on the path: it is not allowed to visit more than k different types of squares (squares of one type can be visited an infinite number of times). Squares marked with S and T have no type, so they are not counted. But S must be visited exactly once β at the very beginning, and T must be visited exactly once β at the very end.Your task is to find the path from the square S to the square T that takes minimum time. Among all shortest paths you should choose the lexicographically minimal one. When comparing paths you should lexicographically represent them as a sequence of characters, that is, of plot types.
|
The first input line contains three integers n, m and k (1 β€ n, m β€ 50, nΒ·m β₯ 2, 1 β€ k β€ 4). Then n lines contain the map. Each line has the length of exactly m characters and consists of lowercase Latin letters and characters S and T. It is guaranteed that the map contains exactly one character S and exactly one character T.Pretest 12 is one of the maximal tests for this problem.
|
If there is a path that satisfies the condition, print it as a sequence of letters β the plot types. Otherwise, print ""-1"" (without quotes). You shouldn't print the character S in the beginning and T in the end.Note that this sequence may be empty. This case is present in pretests. You can just print nothing or print one ""End of line""-character. Both will be accepted.
|
Input: 5 3 2SbacccaaccccabT | Output: bcccc
|
Expert
| 3 | 1,463 | 383 | 374 | 0 |
|
2,095 |
A
|
2095A
|
A. Piecing It Together
| 0 |
*special; string suffix structures
|
Beginner
| 2 | 0 | 0 | 0 | 20 |
|||||
773 |
C
|
773C
|
C. Prairie Partition
| 2,200 |
binary search; constructive algorithms; greedy; math
|
It can be shown that any positive integer x can be uniquely represented as x = 1 + 2 + 4 + ... + 2k - 1 + r, where k and r are integers, k β₯ 0, 0 < r β€ 2k. Let's call that representation prairie partition of x.For example, the prairie partitions of 12, 17, 7 and 1 are: 12 = 1 + 2 + 4 + 5,17 = 1 + 2 + 4 + 8 + 2,7 = 1 + 2 + 4,1 = 1. Alice took a sequence of positive integers (possibly with repeating elements), replaced every element with the sequence of summands in its prairie partition, arranged the resulting numbers in non-decreasing order and gave them to Borys. Now Borys wonders how many elements Alice's original sequence could contain. Find all possible options!
|
The first line contains a single integer n (1 β€ n β€ 105) β the number of numbers given from Alice to Borys.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 1012; a1 β€ a2 β€ ... β€ an) β the numbers given from Alice to Borys.
|
Output, in increasing order, all possible values of m such that there exists a sequence of positive integers of length m such that if you replace every element with the summands in its prairie partition and arrange the resulting numbers in non-decreasing order, you will get the sequence given in the input.If there are no such values of m, output a single integer -1.
|
In the first example, Alice could get the input sequence from [6, 20] as the original sequence.In the second example, Alice's original sequence could be either [4, 5] or [3, 3, 3].
|
Input: 81 1 2 2 3 4 5 8 | Output: 2
|
Hard
| 4 | 673 | 235 | 368 | 7 |
1,776 |
G
|
1776G
|
G. Another Wine Tasting Event
| 2,100 |
combinatorics; constructive algorithms; math; strings
|
After the first successful edition, Gabriella has been asked to organize a second wine tasting event. There will be \(2n - 1\) bottles of wine arranged in a row, each of which is either red wine or white wine.This time, Gabriella has already chosen the type and order of all the bottles. The types of the wines are represented by a string \(s\) of length \(2n - 1\). For each \(1 \le i \le 2n - 1\), it holds that \(s_i = \texttt{R}\) if the \(i\)-th bottle is red wine, and \(s_i = \texttt{W}\) if the \(i\)-th bottle is white wine.Exactly \(n\) critics have been invited to attend. The critics are numbered from \(1\) to \(n\). Just like last year, each critic \(j\) wants to taste an interval of wines, that is, the bottles at positions \(a_j, \, a_j + 1, \, \dots, \, b_j\) for some \(1 \le a_j \le b_j \le 2n - 1\). Moreover, they have the following additional requirements: each of them wants to taste at least \(n\) wines, that is, it must hold that \(b_j - a_j + 1 \ge n\); no two critics must taste exactly the same wines, that is, if \(j \ne k\) it must hold that \(a_j \ne a_k\) or \(b_j \ne b_k\). Gabriella knows that, since the event is held in a coastal region of Italy, critics are especially interested in the white wines, and don't care much about the red ones. (Indeed, white wine is perfect to accompany seafood.) Thus, to ensure fairness, she would like that all critics taste the same number of white wines.Help Gabriella find an integer \(x\) (with \(0 \le x \le 2n - 1\)) such that there exists a valid assignment of intervals to critics where each critic tastes exactly \(x\) white wines. It can be proved that at least one such \(x\) always exists.
|
The first line contains the integer \(n\) (\(1 \le n \le 10^6\)) β where \(2n - 1\) is the number of bottles, and \(n\) is the number of critics.The second line contains a string \(s\) of length \(2n - 1\) that represents the arrangement of the wines β the \(i\)-th character of \(s\) (\(1 \le i \le 2n - 1\)) is \(\texttt{R}\) for a red wine and \(\texttt{W}\) for a white wine.
|
Print an integer \(x\) β the number of white wines that each critic will taste.It can be proved that at least one solution exists. If multiple solutions exist, any of them will be accepted.
|
In the first sample, there are \(5\) critics and \(2 \cdot 5 - 1 = 9\) bottles of wine. A possible set of intervals that makes each critic taste \(2\) white wines is the following: \([2, 6],\) \([1, 6],\) \([4, 8],\) \([1, 5],\) \([3, 7]\). Note that all intervals contain at least \(5\) bottles.In the second sample, there is \(1\) critic and \(2 \cdot 1 - 1 = 1\) bottle of wine. The only possible interval is \([1, 1]\), which gives \(x = 0\).
|
Input: 5 RWWRRRWWW | Output: 2
|
Hard
| 4 | 1,674 | 379 | 189 | 17 |
490 |
B
|
490B
|
B. Queue
| 1,500 |
dsu; implementation
|
During the lunch break all n Berland State University students lined up in the food court. However, it turned out that the food court, too, has a lunch break and it temporarily stopped working.Standing in a queue that isn't being served is so boring! So, each of the students wrote down the number of the student ID of the student that stands in line directly in front of him, and the student that stands in line directly behind him. If no one stands before or after a student (that is, he is the first one or the last one), then he writes down number 0 instead (in Berland State University student IDs are numerated from 1).After that, all the students went about their business. When they returned, they found out that restoring the queue is not such an easy task.Help the students to restore the state of the queue by the numbers of the student ID's of their neighbors in the queue.
|
The first line contains integer n (2 β€ n β€ 2Β·105) β the number of students in the queue. Then n lines follow, i-th line contains the pair of integers ai, bi (0 β€ ai, bi β€ 106), where ai is the ID number of a person in front of a student and bi is the ID number of a person behind a student. The lines are given in the arbitrary order. Value 0 is given instead of a neighbor's ID number if the neighbor doesn't exist.The ID numbers of all students are distinct. It is guaranteed that the records correspond too the queue where all the students stand in some order.
|
Print a sequence of n integers x1, x2, ..., xn β the sequence of ID numbers of all the students in the order they go in the queue from the first student to the last one.
|
The picture illustrates the queue for the first sample.
|
Input: 492 310 731 07 141 | Output: 92 7 31 141
|
Medium
| 2 | 885 | 563 | 169 | 4 |
388 |
B
|
388B
|
B. Fox and Minimal path
| 1,900 |
bitmasks; constructive algorithms; graphs; implementation; math
|
Fox Ciel wants to write a task for a programming contest. The task is: ""You are given a simple undirected graph with n vertexes. Each its edge has unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2.""Same with some writers, she wants to make an example with some certain output: for example, her birthday or the number of her boyfriend. Can you help her to make a test case with answer equal exactly to k?
|
The first line contains a single integer k (1 β€ k β€ 109).
|
You should output a graph G with n vertexes (2 β€ n β€ 1000). There must be exactly k shortest paths between vertex 1 and vertex 2 of the graph.The first line must contain an integer n. Then adjacency matrix G with n rows and n columns must follow. Each element of the matrix must be 'N' or 'Y'. If Gij is 'Y', then graph G has a edge connecting vertex i and vertex j. Consider the graph vertexes are numbered from 1 to n.The graph must be undirected and simple: Gii = 'N' and Gij = Gji must hold. And there must be at least one path between vertex 1 and vertex 2. It's guaranteed that the answer exists. If there multiple correct answers, you can output any of them.
|
In first example, there are 2 shortest paths: 1-3-2 and 1-4-2.In second example, there are 9 shortest paths: 1-3-6-2, 1-3-7-2, 1-3-8-2, 1-4-6-2, 1-4-7-2, 1-4-8-2, 1-5-6-2, 1-5-7-2, 1-5-8-2.
|
Input: 2 | Output: 4NNYYNNYYYYNNYYNN
|
Hard
| 5 | 448 | 57 | 665 | 3 |
1,912 |
A
|
1912A
|
A. Accumulator Apex
| 1,900 |
data structures; implementation; sortings
|
Allyn is playing a new strategy game called ""Accumulator Apex"". In this game, Allyn is given the initial value of an integer \(x\), referred to as the accumulator, and \(k\) lists of integers. Allyn can make multiple turns. On each turn, Allyn can withdraw the leftmost element from any non-empty list and add it to the accumulator \(x\) if the resulting \(x\) is non-negative. Allyn can end the game at any moment. The goal of the game is to get the largest possible value of the accumulator \(x\). Please help Allyn find the largest possible value of the accumulator \(x\) they can get in this game.
|
The first line of the input contains two integers \(x\) and \(k\) (\(0 \leq x \leq 10^9, 1 \leq k \leq 10^5\)) β the initial value of the accumulator \(x\) and the number of lists. The next \(k\) lines contain the description of lists: an integer \(l_i\) (\(l_i \ge 1\)) followed on the same line by \(l_i\) elements of the list in the order from left to right. Each element of lists does not exceed \(10^9\) by the absolute value, and the total size of all lists does not exceed \(10^5\).
|
The sole line of the output should contain the largest value of the accumulator \(x\) Allyn can get.
|
In the first input, we start with \(x = 1\). Then, we can take the first integer from the first list and get \(x = 0\) β adding the next integer \(2\) from the first list we get \(x = 2\). After that, we can add the integers from the second list and obtain \(x = 3\). Finally, we can add the integers from the third list and obtain \(x = 4\).In the second input, we can add the first integer from the second list and get \(x = 2\). Then, by adding the elements from the first list, we get \(x = 4\). We cannot add more integers to increase \(x\).
|
Input: 1 32 -1 22 -2 32 -3 4 | Output: 4
|
Hard
| 3 | 603 | 489 | 100 | 19 |
1,638 |
F
|
1638F
|
F. Two Posters
| 3,200 |
brute force; data structures; greedy; two pointers
|
You want to advertise your new business, so you are going to place two posters on a billboard in the city center. The billboard consists of \(n\) vertical panels of width \(1\) and varying integer heights, held together by a horizontal bar. The \(i\)-th of the \(n\) panels has height \(h_i\). Initially, all panels hang down from the bar (their top edges lie on it), but before placing the two posters, you are allowed to move each panel up by any integer length, as long as it is still connected to the bar (its bottom edge lies below or on it).After the moves are done, you will place two posters: one below the bar and one above it. They are not allowed to go over the bar and they must be positioned completely inside of the panels.What is the maximum total area the two posters can cover together if you make the optimal moves? Note that you can also place a poster of \(0\) area. This case is equivalent to placing a single poster.
|
The first line of input contains one integer \(n\) (\(1 \le n \le 10^4\)) β the number of vertical panels.The second line of input contains \(n\) integers \(h_1, h_2, ..., h_n\) (\(1 \le h_i \le 10^{12}\)) β the heights of the \(n\) vertical panels.
|
Print a single integer β the maximum total area the two posters can cover together.
|
In the first sample test, we can choose an upper poster with area \(12\) and a lower poster of area \(6\) as in the image below. In the second sample test, we can cover the whole billboard using a single poster.
|
Input: 6 2 2 3 5 4 5 | Output: 18
|
Master
| 4 | 938 | 249 | 83 | 16 |
513 |
G2
|
513G2
|
G2. Inversions problem
| 2,400 |
dp; probabilities
|
You are given a permutation of n numbers p1, p2, ..., pn. We perform k operations of the following type: choose uniformly at random two indices l and r (l β€ r) and reverse the order of the elements pl, pl + 1, ..., pr. Your task is to find the expected value of the number of inversions in the resulting permutation.
|
The first line of input contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 109). The next line contains n integers p1, p2, ..., pn β the given permutation. All pi are different and in range from 1 to n.The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem G1 (3 points), the constraints 1 β€ n β€ 6, 1 β€ k β€ 4 will hold. In subproblem G2 (5 points), the constraints 1 β€ n β€ 30, 1 β€ k β€ 200 will hold. In subproblem G3 (16 points), the constraints 1 β€ n β€ 100, 1 β€ k β€ 109 will hold.
|
Output the answer with absolute or relative error no more than 1e - 9.
|
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability , the interval will consist of a single element and the permutation will not be altered. With probability we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to .
|
Input: 3 11 2 3 | Output: 0.833333333333333
|
Expert
| 2 | 316 | 655 | 70 | 5 |
498 |
C
|
498C
|
C. Array and Operations
| 2,100 |
flows; graph matchings; number theory
|
You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m good pairs of integers (i1, j1), (i2, j2), ..., (im, jm). Each good pair (ik, jk) meets the following conditions: ik + jk is an odd number and 1 β€ ik < jk β€ n.In one operation you can perform a sequence of actions: take one of the good pairs (ik, jk) and some integer v (v > 1), which divides both numbers a[ik] and a[jk]; divide both numbers by v, i. e. perform the assignments: and . Determine the maximum number of operations you can sequentially perform on the given array. Note that one pair may be used several times in the described operations.
|
The first line contains two space-separated integers n, m (2 β€ n β€ 100, 1 β€ m β€ 100).The second line contains n space-separated integers a[1], a[2], ..., a[n] (1 β€ a[i] β€ 109) β the description of the array.The following m lines contain the description of good pairs. The k-th line contains two space-separated integers ik, jk (1 β€ ik < jk β€ n, ik + jk is an odd number).It is guaranteed that all the good pairs are distinct.
|
Output the answer for the problem.
|
Input: 3 28 3 81 22 3 | Output: 0
|
Hard
| 3 | 648 | 425 | 34 | 4 |
|
1,842 |
I
|
1842I
|
I. Tenzing and Necklace
| 3,500 |
divide and conquer; dp; greedy
|
bright, sunny and innocent......Tenzing has a beautiful necklace. The necklace consists of \(n\) pearls numbered from \(1\) to \(n\) with a string connecting pearls \(i\) and \((i \text{ mod } n)+1\) for all \(1 \leq i \leq n\).One day Tenzing wants to cut the necklace into several parts by cutting some strings. But for each connected part of the necklace, there should not be more than \(k\) pearls. The time needed to cut each string may not be the same. Tenzing needs to spend \(a_i\) minutes cutting the string between pearls \(i\) and \((i \text{ mod } n)+1\).Tenzing wants to know the minimum time in minutes to cut the necklace such that each connected part will not have more than \(k\) pearls.
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. The description of test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(2\leq n\leq 5\cdot 10^5\), \(1\leq k <n\)).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1\leq a_i\leq 10^9\)).It is guaranteed that the sum of \(n\) of all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case, output the minimum total time in minutes required.
|
In the first test case, the necklace will be cut into \(3\) parts: \([1,2][3,4][5]\), so the total time is \(3\).In the second test case, the necklace will be cut into \(3\) parts: \([5,1][2][3,4]\), Tenzing will cut the strings connecting \((1,2), (2,3)\) and \((4,5)\), so the total time is \(a_1+a_2+a_4=7\).
|
Input: 45 21 1 1 1 15 21 2 3 4 56 34 2 5 1 3 310 32 5 6 5 2 1 7 9 7 2 | Output: 3 7 5 15
|
Master
| 3 | 704 | 498 | 70 | 18 |
937 |
A
|
937A
|
A. Olympiad
| 800 |
implementation; sortings
|
The recent All-Berland Olympiad in Informatics featured n participants with each scoring a certain amount of points.As the head of the programming committee, you are to determine the set of participants to be awarded with diplomas with respect to the following criteria: At least one participant should get a diploma. None of those with score equal to zero should get awarded. When someone is awarded, all participants with score not less than his score should also be awarded. Determine the number of ways to choose a subset of participants that will receive the diplomas.
|
The first line contains a single integer n (1 β€ n β€ 100) β the number of participants.The next line contains a sequence of n integers a1, a2, ..., an (0 β€ ai β€ 600) β participants' scores.It's guaranteed that at least one participant has non-zero score.
|
Print a single integer β the desired number of ways.
|
There are three ways to choose a subset in sample case one. Only participants with 3 points will get diplomas. Participants with 2 or 3 points will get diplomas. Everyone will get a diploma! The only option in sample case two is to award everyone.Note that in sample case three participants with zero scores cannot get anything.
|
Input: 41 3 3 2 | Output: 3
|
Beginner
| 2 | 573 | 253 | 52 | 9 |
887 |
B
|
887B
|
B. Cubes for Masha
| 1,300 |
brute force; implementation
|
Absent-minded Masha got set of n cubes for her birthday.At each of 6 faces of each cube, there is exactly one digit from 0 to 9. Masha became interested what is the largest natural x such she can make using her new cubes all integers from 1 to x.To make a number Masha can rotate her cubes and put them in a row. After that, she looks at upper faces of cubes from left to right and reads the number.The number can't contain leading zeros. It's not required to use all cubes to build a number.Pay attention: Masha can't make digit 6 from digit 9 and vice-versa using cube rotations.
|
In first line integer n is given (1 β€ n β€ 3) β the number of cubes, Masha got for her birthday.Each of next n lines contains 6 integers aij (0 β€ aij β€ 9) β number on j-th face of i-th cube.
|
Print single integer β maximum number x such Masha can make any integers from 1 to x using her cubes or 0 if Masha can't make even 1.
|
In the first test case, Masha can build all numbers from 1 to 87, but she can't make 88 because there are no two cubes with digit 8.
|
Input: 30 1 2 3 4 56 7 8 9 0 12 3 4 5 6 7 | Output: 87
|
Easy
| 2 | 581 | 189 | 133 | 8 |
1,996 |
G
|
1996G
|
G. Penacony
| 2,200 |
brute force; data structures; graphs; greedy; hashing
|
On Penacony, The Land of the Dreams, there exists \(n\) houses and \(n\) roads. There exists a road between house \(i\) and \(i+1\) for all \(1 \leq i \leq n-1\) and a road between house \(n\) and house \(1\). All roads are bidirectional. However, due to the crisis on Penacony, the overseeing family has gone into debt and may not be able to maintain all roads.There are \(m\) pairs of friendships between the residents of Penacony. If the resident living in house \(a\) is friends with the resident living in house \(b\), there must be a path between houses \(a\) and \(b\) through maintained roads. What is the minimum number of roads that must be maintained?
|
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(3 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 2 \cdot 10^5\)) β the number of houses and the number of friendships.The next \(m\) lines contain two integers \(a\) and \(b\) (\(1 \leq a < b \leq n\)) β the resident in house \(a\) is friends with the resident in house \(b\). It is guaranteed all (\(a, b\)) are distinct.It is guaranteed the sum of \(n\) and \(m\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output an integer, the minimum number of roads that must be maintained.
|
For the first test case, the following roads must be maintained: \(8 \leftarrow \rightarrow 1\) \(7 \leftarrow \rightarrow 8\) \(1 \leftarrow \rightarrow 2\) \(4 \leftarrow \rightarrow 5\)
|
Input: 78 31 82 74 513 41 132 123 114 1010 22 33 410 43 85 102 104 104 11 35 23 51 45 22 51 3 | Output: 4 7 2 7 2 3 3
|
Hard
| 5 | 662 | 568 | 91 | 19 |
1,555 |
E
|
1555E
|
E. Boring Segments
| 2,100 |
data structures; sortings; trees; two pointers
|
You are given \(n\) segments on a number line, numbered from \(1\) to \(n\). The \(i\)-th segments covers all integer points from \(l_i\) to \(r_i\) and has a value \(w_i\).You are asked to select a subset of these segments (possibly, all of them). Once the subset is selected, it's possible to travel between two integer points if there exists a selected segment that covers both of them. A subset is good if it's possible to reach point \(m\) starting from point \(1\) in arbitrary number of moves.The cost of the subset is the difference between the maximum and the minimum values of segments in it. Find the minimum cost of a good subset.In every test there exists at least one good subset.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 3 \cdot 10^5\); \(2 \le m \le 10^6\)) β the number of segments and the number of integer points.Each of the next \(n\) lines contains three integers \(l_i\), \(r_i\) and \(w_i\) (\(1 \le l_i < r_i \le m\); \(1 \le w_i \le 10^6\)) β the description of the \(i\)-th segment.In every test there exists at least one good subset.
|
Print a single integer β the minimum cost of a good subset.
|
Input: 5 12 1 5 5 3 4 10 4 10 6 11 12 5 10 12 3 | Output: 3
|
Hard
| 4 | 694 | 392 | 59 | 15 |
|
1,102 |
C
|
1102C
|
C. Doors Breaking and Repairing
| 1,200 |
games
|
You are policeman and you are playing a game with Slavik. The game is turn-based and each turn consists of two phases. During the first phase you make your move and during the second phase Slavik makes his move.There are \(n\) doors, the \(i\)-th door initially has durability equal to \(a_i\).During your move you can try to break one of the doors. If you choose door \(i\) and its current durability is \(b_i\) then you reduce its durability to \(max(0, b_i - x)\) (the value \(x\) is given).During Slavik's move he tries to repair one of the doors. If he chooses door \(i\) and its current durability is \(b_i\) then he increases its durability to \(b_i + y\) (the value \(y\) is given). Slavik cannot repair doors with current durability equal to \(0\).The game lasts \(10^{100}\) turns. If some player cannot make his move then he has to skip it.Your goal is to maximize the number of doors with durability equal to \(0\) at the end of the game. You can assume that Slavik wants to minimize the number of such doors. What is the number of such doors in the end if you both play optimally?
|
The first line of the input contains three integers \(n\), \(x\) and \(y\) (\(1 \le n \le 100\), \(1 \le x, y \le 10^5\)) β the number of doors, value \(x\) and value \(y\), respectively.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^5\)), where \(a_i\) is the initial durability of the \(i\)-th door.
|
Print one integer β the number of doors with durability equal to \(0\) at the end of the game, if you and Slavik both play optimally.
|
Clarifications about the optimal strategy will be ignored.
|
Input: 6 3 2 2 3 1 3 4 2 | Output: 6
|
Easy
| 1 | 1,093 | 352 | 133 | 11 |
846 |
E
|
846E
|
E. Chemistry in Berland
| 2,300 |
dfs and similar; greedy; trees
|
Igor is a post-graduate student of chemistry faculty in Berland State University (BerSU). He needs to conduct a complicated experiment to write his thesis, but laboratory of BerSU doesn't contain all the materials required for this experiment.Fortunately, chemical laws allow material transformations (yes, chemistry in Berland differs from ours). But the rules of transformation are a bit strange.Berland chemists are aware of n materials, numbered in the order they were discovered. Each material can be transformed into some other material (or vice versa). Formally, for each i (2 β€ i β€ n) there exist two numbers xi and ki that denote a possible transformation: ki kilograms of material xi can be transformed into 1 kilogram of material i, and 1 kilogram of material i can be transformed into 1 kilogram of material xi. Chemical processing equipment in BerSU allows only such transformation that the amount of resulting material is always an integer number of kilograms.For each i (1 β€ i β€ n) Igor knows that the experiment requires ai kilograms of material i, and the laboratory contains bi kilograms of this material. Is it possible to conduct an experiment after transforming some materials (or none)?
|
The first line contains one integer number n (1 β€ n β€ 105) β the number of materials discovered by Berland chemists.The second line contains n integer numbers b1, b2... bn (1 β€ bi β€ 1012) β supplies of BerSU laboratory.The third line contains n integer numbers a1, a2... an (1 β€ ai β€ 1012) β the amounts required for the experiment.Then n - 1 lines follow. j-th of them contains two numbers xj + 1 and kj + 1 that denote transformation of (j + 1)-th material (1 β€ xj + 1 β€ j, 1 β€ kj + 1 β€ 109).
|
Print YES if it is possible to conduct an experiment. Otherwise print NO.
|
Input: 31 2 33 2 11 11 1 | Output: YES
|
Expert
| 3 | 1,208 | 494 | 73 | 8 |
|
1,704 |
H2
|
1704H2
|
H2. Game of AI (hard version)
| 3,500 |
combinatorics; fft; math
|
This is the hard version of this problem. The difference between easy and hard versions is the constraint on \(k\) and the time limit. Notice that you need to calculate the answer for all positive integers \(n \in [1,k]\) in this version. You can make hacks only if both versions of the problem are solved.Cirno is playing a war simulator game with \(n\) towers (numbered from \(1\) to \(n\)) and \(n\) bots (numbered from \(1\) to \(n\)). The \(i\)-th tower is initially occupied by the \(i\)-th bot for \(1 \le i \le n\).Before the game, Cirno first chooses a permutation \(p = [p_1, p_2, \ldots, p_n]\) of length \(n\) (A permutation of length \(n\) is an array of length \(n\) where each integer between \(1\) and \(n\) appears exactly once). After that, she can choose a sequence \(a = [a_1, a_2, \ldots, a_n]\) (\(1 \le a_i \le n\) and \(a_i \ne i\) for all \(1 \le i \le n\)).The game has \(n\) rounds of attacks. In the \(i\)-th round, if the \(p_i\)-th bot is still in the game, it will begin its attack, and as the result the \(a_{p_i}\)-th tower becomes occupied by the \(p_i\)-th bot; the bot that previously occupied the \(a_{p_i}\)-th tower will no longer occupy it. If the \(p_i\)-th bot is not in the game, nothing will happen in this round.After each round, if a bot doesn't occupy any towers, it will be eliminated and leave the game. Please note that no tower can be occupied by more than one bot, but one bot can occupy more than one tower during the game.At the end of the game, Cirno will record the result as a sequence \(b = [b_1, b_2, \ldots, b_n]\), where \(b_i\) is the number of the bot that occupies the \(i\)-th tower at the end of the game.However, as a mathematics master, she wants you to solve the following counting problem instead of playing games:Count the number of different pairs of sequences \(a\), \(b\) from all possible choices of sequence \(a\) and permutation \(p\).Calculate the answers for all \(n\) such that \(1 \le n \le k\). Since these numbers may be large, output them modulo \(M\).
|
The only line contains two positive integers \(k\) and \(M\) (\(1\le k\le 10^5\), \(2\le M\le 10^9\) ). It is guaranteed that \(2^{18}\) is a divisor of \(M-1\) and \(M\) is a prime number.
|
Output \(k\) lines, where the \(i\)-th line contains a non-negative integer, which is the answer for \(n=i\) modulo \(M\).
|
For \(n=1\), no valid sequence \(a\) exists. We regard the answer as \(0\).For \(n=2\), there is only one possible array \(a\): \([2, 1]\). For array \(a\) is \([2, 1]\) and permutation \(p\) is \([1, 2]\), the sequence \(b\) will be \([1, 1]\) after all rounds have finished. The details for each rounds: In the first round, the first bot will begin its attack and successfully capture the tower \(2\). After this round, the second bot will be eliminated and leave the game as all of its towers are occupied by other bots. In the second round, the second bot is not in the game. For array \(a\) is \([2, 1]\) and permutation \(p\) is \([2, 1]\), the sequence \(b\) will be \([2, 2]\) after all rounds have finished. The details for each rounds: In the first round, the second bot will begin its attack and successfully capture the tower \(1\). After this round, the first bot will be eliminated and leave the game as all of its towers are occupied by other bots. In the second round, the first bot is not in the game. So the number of different pairs of sequences \((a,b)\) is \(2\) (\([2, 1]\), \([1, 1]\) and \([2, 1]\), \([2, 2]\)) for \(n=2\).
|
Input: 8 998244353 | Output: 0 2 24 360 6800 153150 4057452 123391016
|
Master
| 3 | 2,036 | 189 | 122 | 17 |
2,002 |
D2
|
2002D2
|
D2. DFS Checker (Hard Version)
| 2,300 |
binary search; data structures; dfs and similar; graphs; hashing; trees
|
This is the hard version of the problem. In this version, you are given a generic tree and the constraints on \(n\) and \(q\) are higher. You can make hacks only if both versions of the problem are solved.You are given a rooted tree consisting of \(n\) vertices. The vertices are numbered from \(1\) to \(n\), and the root is the vertex \(1\). You are also given a permutation \(p_1, p_2, \ldots, p_n\) of \([1,2,\ldots,n]\).You need to answer \(q\) queries. For each query, you are given two integers \(x\), \(y\); you need to swap \(p_x\) and \(p_y\) and determine if \(p_1, p_2, \ldots, p_n\) is a valid DFS order\(^\dagger\) of the given tree.Please note that the swaps are persistent through queries.\(^\dagger\) A DFS order is found by calling the following \(\texttt{dfs}\) function on the given tree.dfs_order = []function dfs(v): append v to the back of dfs_order pick an arbitrary permutation s of children of v for child in s: dfs(child)dfs(1)Note that the DFS order is not unique.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\le t\le10^4\)). The description of the test cases follows. The first line of each test case contains two integers \(n\), \(q\) (\(2\le n\le 3\cdot 10^5\), \(2\le q\le 10^5\)) β the number of vertices in the tree and the number of queries.The next line contains \(n-1\) integers \(a_2,a_3,\ldots,a_n\) (\(1\le a_i<i\)) β the parent of each vertex in the given tree.The next line contains \(n\) integers \(p_1,p_2,\ldots,p_n\) (\(1\le p_i\le n\), all \(p_i\) are distinct) β the initial permutation \(p\).The next \(q\) lines each contain two integers \(x\), \(y\) (\(1\le x,y\le n,x\neq y\)) β the positions of the elements to swap in the permutation.It is guaranteed that the sum of all \(n\) does not exceed \(3\cdot 10^5\), and the sum of all \(q\) does not exceed \(10^5\).
|
For each test case, print \(q\) lines corresponding to the \(q\) queries. For each query, output \(\texttt{YES}\) if there is a DFS order that exactly equals the current permutation, and output \(\texttt{NO}\) otherwise.You can output \(\texttt{Yes}\) and \(\texttt{No}\) in any case (for example, strings \(\texttt{yEs}\), \(\texttt{yes}\), \(\texttt{Yes}\), and \(\texttt{YES}\) will be recognized as a positive response).
|
In the first test case, the permutation \(p_1, p_2, \ldots, p_n\) after each modification is \([1,3,2],[1,2,3],[3,2,1]\), respectively. The first two permutations are valid DFS orders; the third is not a DFS order.In the second test case, the permutation \(p_1, p_2, \ldots, p_n\) after each modification is \([1,2,5,4,3,6,7],[1,3,5,4,2,6,7],[1,3,7,4,2,6,5],[1,3,7,6,2,4,5]\), respectively.
|
Input: 33 31 11 2 32 33 21 37 41 1 2 2 3 31 2 3 4 5 6 73 52 53 74 65 41 1 3 42 3 4 5 15 14 53 42 3 | Output: YES YES NO YES NO NO YES YES NO NO YES
|
Expert
| 6 | 992 | 876 | 424 | 20 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.