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,916 |
H2
|
1916H2
|
H2. Matrix Rank (Hard Version)
| 2,700 |
combinatorics; dp; math; matrices; string suffix structures
|
This is the hard version of the problem. The only differences between the two versions of this problem are the constraints on \(k\). You can make hacks only if all versions of the problem are solved.You are given integers \(n\), \(p\) and \(k\). \(p\) is guaranteed to be a prime number. For each \(r\) from \(0\) to \(k\), find the number of \(n \times n\) matrices \(A\) of the field\(^\dagger\) of integers modulo \(p\) such that the rank\(^\ddagger\) of \(A\) is exactly \(r\). Since these values are big, you are only required to output them modulo \(998\,244\,353\).\(^\dagger\) https://en.wikipedia.org/wiki/Field_(mathematics)\(^\ddagger\) https://en.wikipedia.org/wiki/Rank_(linear_algebra)
|
The first line of input contains three integers \(n\), \(p\) and \(k\) (\(1 \leq n \leq 10^{18}\), \(2 \leq p < 998\,244\,353\), \(0 \leq k \leq 5 \cdot 10^5\)).It is guaranteed that \(p\) is a prime number.
|
Output \(k+1\) integers, the answers for each \(r\) from \(0\) to \(k\).
|
Input: 3 2 3 | Output: 1 49 294 168
|
Master
| 5 | 699 | 207 | 72 | 19 |
|
1,189 |
B
|
1189B
|
B. Number Circle
| 1,100 |
greedy; math; sortings
|
You are given \(n\) numbers \(a_1, a_2, \ldots, a_n\). Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?For example, for the array \([1, 4, 5, 6, 7, 8]\), the arrangement on the left is valid, while arrangement on the right is not, as \(5\ge 4 + 1\) and \(8> 1 + 6\).
|
The first line contains a single integer \(n\) (\(3\le n \le 10^5\)) β the number of numbers.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \le 10^9\)) β the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).
|
If there is no solution, output ""NO"" in the first line. If there is a solution, output ""YES"" in the first line. In the second line output \(n\) numbers β elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.
|
One of the possible arrangements is shown in the first example: \(4< 2 + 3\);\(2 < 4 + 3\);\(3< 4 + 2\).One of the possible arrangements is shown in the second example.No matter how we arrange \(13, 8, 5\) in a circle in the third example, \(13\) will have \(8\) and \(5\) as neighbors, but \(13\ge 8 + 5\). There is no solution in the fourth example.
|
Input: 3 2 4 3 | Output: YES 4 2 3
|
Easy
| 3 | 340 | 277 | 409 | 11 |
1,934 |
D2
|
1934D2
|
D2. XOR Break β Game Version
| 2,400 |
bitmasks; games; greedy; interactive
|
This is an interactive problem.This is the game version of the problem. Note that the solution of this problem may or may not share ideas with the solution of the solo version. You can solve and get points for both versions independently.Alice and Bob are playing a game. The game starts with a positive integer \(n\), with players taking turns. On each turn of the game, the following sequence of events takes place: The player having the integer \(p\) breaks it into two integers \(p_{1}\) and \(p_{2}\), where \(0 \lt p_{1} \lt p\), \(0 \lt p_{2} \lt p\) and \(p_{1} \oplus p_{2} = p\). If no such \(p_{1}\), \(p_{2}\) exist, the player loses. Otherwise, the opponent does either select the integer \(p_{1}\) or \(p_{2}\). The game continues with the selected integer. The opponent will try to break it. As Alice, your goal is to win. You can execute a maximum of \(63\) break operations. You have the choice to play first or second. The system will act for Bob.Here \(\oplus\) denotes the bitwise XOR operation.
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The only line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^{18}\)) β the number the game starts with.
|
Explanation for the interaction.Interactor / BobAliceExplanation4\(t\)1\(n\) for the first test casesecondAlice chooses to go second0 0Bob says he cannot break \(p = 1\)3\(n\) for the second test casefirstAlice chooses to go first1 2Alice breaks \(p = 3\) into \(p_1 = 1\) and \(p_2 = 2\)0 0Bob says he cannot break \(p = 1\) or \(p = 2\)13\(n\) for the third test casefirstAlice chooses to go first10 7Alice breaks \(p = 13\) into \(p_1 = 10\) and \(p_2 = 7\)3 4Bob breaks \(p = 7\) into \(p_1 = 3\) and \(p_2 = 4\)1 2Alice breaks \(p = 3\) into \(p_1 = 1\) and \(p_2 = 2\)0 0Bob says he cannot break \(p = 1\) or \(p = 2\)777777770001\(n\) for the fourth test casefirstAlice chooses to go first777777770000 1Alice breaks \(p = 777\,777\,770\,001\) into \(p_1 = 777\,777\,770\,000\) and \(p_2 = 1\)0 0Bob says he cannot perform break operation.This table is for explanation only and does not reflect the actual behavior of the interactor. Note that in the last test case Bob could choose \(p_1\) and perform a break operation but he gave up.
|
Input: 4 1 0 0 3 0 0 13 3 4 0 0 777777770001 0 0 | Output: second first 2 1 first 10 7 1 2 first 777777770000 1
|
Expert
| 4 | 1,015 | 274 | 0 | 19 |
|
1,558 |
B
|
1558B
|
B. Up the Strip
| 1,900 |
brute force; dp; math; number theory; two pointers
|
Note that the memory limit in this problem is lower than in others.You have a vertical strip with \(n\) cells, numbered consecutively from \(1\) to \(n\) from top to bottom.You also have a token that is initially placed in cell \(n\). You will move the token up until it arrives at cell \(1\).Let the token be in cell \(x > 1\) at some moment. One shift of the token can have either of the following kinds: Subtraction: you choose an integer \(y\) between \(1\) and \(x-1\), inclusive, and move the token from cell \(x\) to cell \(x - y\). Floored division: you choose an integer \(z\) between \(2\) and \(x\), inclusive, and move the token from cell \(x\) to cell \(\lfloor \frac{x}{z} \rfloor\) (\(x\) divided by \(z\) rounded down). Find the number of ways to move the token from cell \(n\) to cell \(1\) using one or more shifts, and print it modulo \(m\). Note that if there are several ways to move the token from one cell to another in one shift, all these ways are considered distinct (check example explanation for a better understanding).
|
The only line contains two integers \(n\) and \(m\) (\(2 \le n \le 4 \cdot 10^6\); \(10^8 < m < 10^9\); \(m\) is a prime number) β the length of the strip and the modulo.
|
Print the number of ways to move the token from cell \(n\) to cell \(1\), modulo \(m\).
|
In the first test, there are three ways to move the token from cell \(3\) to cell \(1\) in one shift: using subtraction of \(y = 2\), or using division by \(z = 2\) or \(z = 3\).There are also two ways to move the token from cell \(3\) to cell \(1\) via cell \(2\): first subtract \(y = 1\), and then either subtract \(y = 1\) again or divide by \(z = 2\).Therefore, there are five ways in total.
|
Input: 3 998244353 | Output: 5
|
Hard
| 5 | 1,048 | 170 | 87 | 15 |
1,002 |
C1
|
1002C1
|
C1. Distinguish zero state and plus state with minimum error
| 1,700 |
*special
|
You are given a qubit which is guaranteed to be either in state or in state. Your task is to perform necessary operations and measurements to figure out which state it was and to return 0 if it was a state or 1 if it was state. The state of the qubit after the operations does not matter.Note that these states are not orthogonal, and thus can not be distinguished perfectly. In each test your solution will be called 1000 times, and your goal is to get a correct answer at least 80% of the times. In each test and states will be provided with 50% probability.You have to implement an operation which takes a qubit 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 (q : Qubit) : Int { body { // your code here } }}
|
Medium
| 1 | 847 | 0 | 0 | 10 |
||||
1,381 |
B
|
1381B
|
B. Unmerge
| 1,800 |
dp
|
Let \(a\) and \(b\) be two arrays of lengths \(n\) and \(m\), respectively, with no elements in common. We can define a new array \(\mathrm{merge}(a,b)\) of length \(n+m\) recursively as follows: If one of the arrays is empty, the result is the other array. That is, \(\mathrm{merge}(\emptyset,b)=b\) and \(\mathrm{merge}(a,\emptyset)=a\). In particular, \(\mathrm{merge}(\emptyset,\emptyset)=\emptyset\). If both arrays are non-empty, and \(a_1<b_1\), then \(\mathrm{merge}(a,b)=[a_1]+\mathrm{merge}([a_2,\ldots,a_n],b)\). That is, we delete the first element \(a_1\) of \(a\), merge the remaining arrays, then add \(a_1\) to the beginning of the result. If both arrays are non-empty, and \(a_1>b_1\), then \(\mathrm{merge}(a,b)=[b_1]+\mathrm{merge}(a,[b_2,\ldots,b_m])\). That is, we delete the first element \(b_1\) of \(b\), merge the remaining arrays, then add \(b_1\) to the beginning of the result. This algorithm has the nice property that if \(a\) and \(b\) are sorted, then \(\mathrm{merge}(a,b)\) will also be sorted. For example, it is used as a subroutine in merge-sort. For this problem, however, we will consider the same procedure acting on non-sorted arrays as well. For example, if \(a=[3,1]\) and \(b=[2,4]\), then \(\mathrm{merge}(a,b)=[2,3,1,4]\).A permutation is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).There is a permutation \(p\) of length \(2n\). Determine if there exist two arrays \(a\) and \(b\), each of length \(n\) and with no elements in common, so that \(p=\mathrm{merge}(a,b)\).
|
The first line contains a single integer \(t\) (\(1\le t\le 1000\)) β the number of test cases. Next \(2t\) lines contain descriptions of test cases. The first line of each test case contains a single integer \(n\) (\(1\le n\le 2000\)).The second line of each test case contains \(2n\) integers \(p_1,\ldots,p_{2n}\) (\(1\le p_i\le 2n\)). It is guaranteed that \(p\) is a permutation.It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2000\).
|
For each test case, output ""YES"" if there exist arrays \(a\), \(b\), each of length \(n\) and with no common elements, so that \(p=\mathrm{merge}(a,b)\). Otherwise, output ""NO"".
|
In the first test case, \([2,3,1,4]=\mathrm{merge}([3,1],[2,4])\).In the second test case, we can show that \([3,1,2,4]\) is not the merge of two arrays of length \(2\).In the third test case, \([3,2,6,1,5,7,8,4]=\mathrm{merge}([3,2,8,4],[6,1,5,7])\).In the fourth test case, \([1,2,3,4,5,6]=\mathrm{merge}([1,3,6],[2,4,5])\), for example.
|
Input: 6 2 2 3 1 4 2 3 1 2 4 4 3 2 6 1 5 7 8 4 3 1 2 3 4 5 6 4 6 1 3 7 4 5 8 2 6 4 3 2 5 1 11 9 12 8 6 10 7 | Output: YES NO YES YES NO NO
|
Medium
| 1 | 1,762 | 470 | 181 | 13 |
803 |
F
|
803F
|
F. Coprime Subsequences
| 2,000 |
bitmasks; combinatorics; number theory
|
Let's call a non-empty sequence of positive integers a1, a2... ak coprime if the greatest common divisor of all elements of this sequence is equal to 1.Given an array a consisting of n positive integers, find the number of its coprime subsequences. Since the answer may be very large, print it modulo 109 + 7.Note that two subsequences are considered different if chosen indices are different. For example, in the array [1, 1] there are 3 different subsequences: [1], [1] and [1, 1].
|
The first line contains one integer number n (1 β€ n β€ 100000).The second line contains n integer numbers a1, a2... an (1 β€ ai β€ 100000).
|
Print the number of coprime subsequences of a modulo 109 + 7.
|
In the first example coprime subsequences are: 1 1, 2 1, 3 1, 2, 3 2, 3 In the second example all subsequences are coprime.
|
Input: 31 2 3 | Output: 5
|
Hard
| 3 | 483 | 136 | 61 | 8 |
320 |
A
|
320A
|
A. Magic Numbers
| 900 |
brute force; greedy
|
A magic number is a number formed by concatenation of numbers 1, 14 and 144. We can use each of these numbers any number of times. Therefore 14144, 141414 and 1411 are magic numbers but 1444, 514 and 414 are not.You're given a number. Determine if it is a magic number or not.
|
The first line of input contains an integer n, (1 β€ n β€ 109). This number doesn't contain leading zeros.
|
Print ""YES"" if n is a magic number or print ""NO"" if it's not.
|
Input: 114114 | Output: YES
|
Beginner
| 2 | 276 | 104 | 65 | 3 |
|
2,024 |
A
|
2024A
|
A. Profitable Interest Rate
| 800 |
greedy; math
|
Alice has \(a\) coins. She can open a bank deposit called ""Profitable"", but the minimum amount required to open this deposit is \(b\) coins.There is also a deposit called ""Unprofitable"", which can be opened with any amount of coins. Alice noticed that if she opens the ""Unprofitable"" deposit with \(x\) coins, the minimum amount required to open the ""Profitable"" deposit decreases by \(2x\) coins. However, these coins cannot later be deposited into the ""Profitable"" deposit.Help Alice determine the maximum number of coins she can deposit into the ""Profitable"" deposit if she first deposits some amount of coins (possibly \(0\)) into the ""Unprofitable"" deposit. If Alice can never open the ""Profitable"" deposit, output \(0\).
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.A single line of each test case contains two integers \(a\) and \(b\) (\(1 \le a, b \le 10^9\)) β the number of coins Alice has and the initial minimum amount required to open the ""Profitable"" deposit.
|
For each test case, output a single integer β the maximum number of coins that Alice can deposit into the ""Profitable"" deposit. If Alice can never open the ""Profitable"" deposit, output \(0\).
|
In the first test case, \(a \ge b\), so Alice can immediately open the ""Profitable"" deposit with all \(10\) coins.In the second test case, Alice can open the ""Unprofitable"" deposit with \(2\) coins. Then she will have \(5\) coins left, but the minimum amount required to open the ""Profitable"" deposit will decrease by \(4\) coins, making it equal to \(5\) coins. Thus, Alice will be able to open the ""Profitable"" deposit with \(5\) coins.In the third test case, Alice will not be able to open the ""Profitable"" deposit.
|
Input: 510 57 95 1001 11 2 | Output: 10 5 0 1 0
|
Beginner
| 2 | 742 | 386 | 195 | 20 |
1,469 |
E
|
1469E
|
E. A Bit Similar
| 2,400 |
bitmasks; brute force; hashing; string suffix structures; strings; two pointers
|
Let's call two strings \(a\) and \(b\) (both of length \(k\)) a bit similar if they have the same character in some position, i. e. there exists at least one \(i \in [1, k]\) such that \(a_i = b_i\).You are given a binary string \(s\) of length \(n\) (a string of \(n\) characters 0 and/or 1) and an integer \(k\). Let's denote the string \(s[i..j]\) as the substring of \(s\) starting from the \(i\)-th character and ending with the \(j\)-th character (that is, \(s[i..j] = s_i s_{i + 1} s_{i + 2} \dots s_{j - 1} s_j\)).Let's call a binary string \(t\) of length \(k\) beautiful if it is a bit similar to all substrings of \(s\) having length exactly \(k\); that is, it is a bit similar to \(s[1..k], s[2..k+1], \dots, s[n-k+1..n]\).Your goal is to find the lexicographically smallest string \(t\) that is beautiful, or report that no such string exists. String \(x\) is lexicographically less than string \(y\) if either \(x\) is a prefix of \(y\) (and \(x \ne y\)), or there exists such \(i\) (\(1 \le i \le \min(|x|, |y|)\)), that \(x_i < y_i\), and for any \(j\) (\(1 \le j < i\)) \(x_j = y_j\).
|
The first line contains one integer \(q\) (\(1 \le q \le 10000\)) β the number of test cases. Each test case consists of two lines.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^6\)). The second line contains the string \(s\), consisting of \(n\) characters (each character is either 0 or 1).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, print the answer as follows: if it is impossible to construct a beautiful string, print one line containing the string NO (note: exactly in upper case, you can't print No, for example); otherwise, print two lines. The first line should contain the string YES (exactly in upper case as well); the second line β the lexicographically smallest beautiful string, consisting of \(k\) characters 0 and/or 1.
|
Input: 7 4 2 0110 4 2 1001 9 3 010001110 9 3 101110001 10 3 0101110001 10 10 1111111111 11 10 11111111110 | Output: YES 11 YES 00 YES 010 YES 101 NO YES 0000000001 YES 0000000010
|
Expert
| 6 | 1,101 | 424 | 421 | 14 |
|
690 |
E1
|
690E1
|
E1. Photographs (I)
| 1,800 |
The Human-Cow Confederation (HC2), led by Heidi, has built a base where people and cows can hide, guarded from zombie attacks. The entrance to the base is protected by an automated gate which performs a kind of a Turing test: it shows the entering creature a photograph and asks them whether the top and bottom halves of this photograph have been swapped or not. A person (or even a cow) will have no problem answering such questions; on the other hand, a zombie would just randomly smash one of the two buttons.The creature is asked a series of such questions. If at least 75% of them are answered correctly, the gate is unlocked; otherwise, a side door opens, beneath which a huge fan is spinning...Heidi is now building a robot army to fight the zombies, and she wants the robots to also be able to enter the base. You are tasked with programming them to distinguish the images. The first two images from the test set. The first picture has been rearranged, but not the second.
|
The first line of the input contains the number q of questions (1 β€ q β€ 220). After that, q questions follow, each of which in the format described below.The first line of every question contains two space-separated integers h and w (1 β€ h, w β€ 600) β the height (number of rows) and width (number of columns) of the photograph. (Most photographs are roughly 200 Γ 300.) After this, h lines follow, each describing a single row of the picture. The picture is monochrome (in shades of grey). Its i-th row is described by w space-separated integers aij (j = 1, ..., w), where aij is the brightness of the corresponding pixel (0 β€ aij < 256, where 0 is black and 255 is white).Each picture will be either a real-life photograph, or a real-life photograph which has been broken up into two pieces and rearranged. More precisely, in the latter case, the topmost rows have been moved to the bottom of the picture. It is guaranteed that h is even.There is only a single input file to be processed, called all.in, and it is downloadable from the online judge. You are also a given another input file, called sample.in, which contains the first 20 pictures from all.in; you are provided the correct answers for sample.in in sample.out. You are also given a directory easy_bmp, which contains the first 50 input photographs in the form of .bmp image files, as well as a directory easy_sample_original_bmp, which contains the first 20 images before rearrangement. Check the notes for the download links.
|
Your program should print q lines. The i-th line should contain your answer for the i-th question: YES if the photograph has been rearranged and NO otherwise. Your answers will be accepted if they all conform to this format and if at least 75% of them are correct.Because the input is rather huge, feel free to process it locally and submit just your precomputed answers (i.e., a program which just prints your output for the input file all.in).
|
The link to download all necessary files is http://assets.codeforces.com/files/690/easy_contestant_package.zip
|
Medium
| 0 | 980 | 1,492 | 445 | 6 |
||
2,027 |
D1
|
2027D1
|
D1. The Endspeaker (Easy Version)
| 1,700 |
binary search; dp; graphs; greedy; implementation; two pointers
|
This is the easy version of this problem. The only difference is that you only need to output the minimum total cost of operations in this version. You must solve both versions to be able to hack.You're given an array \(a\) of length \(n\), and an array \(b\) of length \(m\) (\(b_i > b_{i+1}\) for all \(1 \le i < m\)). Initially, the value of \(k\) is \(1\). Your aim is to make the array \(a\) empty by performing one of these two operations repeatedly: Type \(1\) β If the value of \(k\) is less than \(m\) and the array \(a\) is not empty, you can increase the value of \(k\) by \(1\). This does not incur any cost. Type \(2\) β You remove a non-empty prefix of array \(a\), such that its sum does not exceed \(b_k\). This incurs a cost of \(m - k\). You need to minimize the total cost of the operations to make array \(a\) empty. If it's impossible to do this through any sequence of operations, output \(-1\). Otherwise, output the minimum total cost of the operations.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 3 \cdot 10^5\), \(\boldsymbol{1 \le n \cdot m \le 3 \cdot 10^5}\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)).The third line of each test case contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(1 \le b_i \le 10^9\)).It is also guaranteed that \(b_i > b_{i+1}\) for all \(1 \le i < m\).It is guaranteed that the sum of \(\boldsymbol{n \cdot m}\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, if it's possible to make \(a\) empty, then output the minimum total cost of the operations.If there is no possible sequence of operations which makes \(a\) empty, then output a single integer \(-1\).
|
In the first test case, one optimal sequence of operations which yields a total cost of \(1\) is as follows: Perform an operation of type \(2\). Choose the prefix to be \([9]\). This incurs a cost of \(1\). Perform an operation of type \(1\). The value of \(k\) is now \(2\). This incurs no cost. Perform an operation of type \(2\). Choose the prefix to be \([3, 4]\). This incurs a cost of \(0\). Perform an operation of type \(2\). Choose the prefix to be \([3]\). This incurs a cost of \(0\). The array \(a\) is now empty, and the total cost of all operations is \(1\). In the second test case, it's impossible to remove any prefix of the array since \(a_1 > b_1\), so array \(a\) cannot be made empty by any sequence of operations.
|
Input: 54 29 3 4 311 71 22019 1810 22 5 2 1 10 3 2 9 9 617 910 112 2 2 2 2 2 2 2 2 220 18 16 14 12 10 8 6 4 2 11 61032 16 8 4 2 1 | Output: 1 -1 2 10 4
|
Medium
| 6 | 977 | 716 | 219 | 20 |
1,165 |
F1
|
1165F1
|
F1. Microtransactions (easy version)
| 2,000 |
binary search; greedy
|
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 1000\)) β 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 1000\)), 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 \(1000\).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 1000, 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
| 2 | 1,261 | 776 | 114 | 11 |
|
17 |
B
|
17B
|
B. Hierarchy
| 1,500 |
dfs and similar; dsu; greedy; shortest paths
|
Nick's company employed n people. Now Nick needs to build a tree hierarchy of Β«supervisor-surbodinateΒ» relations in the company (this is to say that each employee, except one, has exactly one supervisor). There are m applications written in the following form: Β«employee ai is ready to become a supervisor of employee bi at extra cost ciΒ». The qualification qj of each employee is known, and for each application the following is true: qai > qbi. Would you help Nick calculate the minimum cost of such a hierarchy, or find out that it is impossible to build it.
|
The first input line contains integer n (1 β€ n β€ 1000) β amount of employees in the company. The following line contains n space-separated numbers qj (0 β€ qj β€ 106)β the employees' qualifications. The following line contains number m (0 β€ m β€ 10000) β amount of received applications. The following m lines contain the applications themselves, each of them in the form of three space-separated numbers: ai, bi and ci (1 β€ ai, bi β€ n, 0 β€ ci β€ 106). Different applications can be similar, i.e. they can come from one and the same employee who offered to become a supervisor of the same person but at a different cost. For each application qai > qbi.
|
Output the only line β the minimum cost of building such a hierarchy, or -1 if it is impossible to build it.
|
In the first sample one of the possible ways for building a hierarchy is to take applications with indexes 1, 2 and 4, which give 11 as the minimum total cost. In the second sample it is impossible to build the required hierarchy, so the answer is -1.
|
Input: 47 2 3 141 2 52 4 13 4 11 3 5 | Output: 11
|
Medium
| 4 | 561 | 648 | 108 | 0 |
1,361 |
B
|
1361B
|
B. Johnny and Grandmaster
| 1,900 |
greedy; implementation; math; sortings
|
Johnny has just found the new, great tutorial: ""How to become a grandmaster?"". The tutorial tells many strange and unexpected for Johnny things, such as you have to be patient or that very important is solving many harder and harder problems. The boy has found an online judge with tasks divided by topics they cover. He has picked \(p^{k_i}\) problems from \(i\)-th category (\(p\) is his favorite number). He wants to solve them in two weeks (the patience condition is too hard for Johnny, so for simplicity, he looks only at easy tasks, which can be solved in such a period). Now our future grandmaster has to decide which topics to cover first and which the second week. Help him assign topics in such a way, that workload is balanced.Formally, given \(n\) numbers \(p^{k_i}\), the boy wants to divide them into two disjoint sets, minimizing the absolute difference between sums of numbers in each set. Find the minimal absolute difference. Output the result modulo \(10^{9}+7\).
|
Input consists of multiple test cases. The first line contains one integer \(t\) \((1 \leq t \leq 10^5)\) β the number of test cases. Each test case is described as follows:The first line contains two integers \(n\) and \(p\) \((1 \leq n, p \leq 10^6)\). The second line contains \(n\) integers \(k_i\) \((0 \leq k_i \leq 10^6)\).The sum of \(n\) over all test cases doesn't exceed \(10^6\).
|
Output one integer β the reminder of division the answer by \(1\,000\,000\,007\).
|
You have to minimize the difference, not it's remainder. For example, if the minimum difference is equal to \(2\), but there is also a distribution where the difference is \(10^9 + 8\), then the answer is \(2\), not \(1\).In the first test case of the example, there're the following numbers: \(4\), \(8\), \(16\), \(16\), and \(8\). We can divide them into such two sets: \({4, 8, 16}\) and \({8, 16}\). Then the difference between the sums of numbers in sets would be \(4\).
|
Input: 4 5 2 2 3 4 4 3 3 1 2 10 1000 4 5 0 1 1 100 1 8 89 | Output: 4 1 146981438 747093407
|
Hard
| 4 | 985 | 391 | 81 | 13 |
1,486 |
F
|
1486F
|
F. Pairs of Paths
| 2,600 |
combinatorics; data structures; dfs and similar; dp; trees
|
You are given a tree consisting of \(n\) vertices, and \(m\) simple vertex paths. Your task is to find how many pairs of those paths intersect at exactly one vertex. More formally you have to find the number of pairs \((i, j)\) \((1 \leq i < j \leq m)\) such that \(path_i\) and \(path_j\) have exactly one vertex in common.
|
First line contains a single integer \(n\) \((1 \leq n \leq 3 \cdot 10^5)\).Next \(n - 1\) lines describe the tree. Each line contains two integers \(u\) and \(v\) \((1 \leq u, v \leq n)\) describing an edge between vertices \(u\) and \(v\).Next line contains a single integer \(m\) \((1 \leq m \leq 3 \cdot 10^5)\).Next \(m\) lines describe paths. Each line describes a path by it's two endpoints \(u\) and \(v\) \((1 \leq u, v \leq n)\). The given path is all the vertices on the shortest path from \(u\) to \(v\) (including \(u\) and \(v\)).
|
Output a single integer β the number of pairs of paths that intersect at exactly one vertex.
|
The tree in the first example and paths look like this. Pairs \((1,4)\) and \((3,4)\) intersect at one vertex.In the second example all three paths contain the same single vertex, so all pairs \((1, 2)\), \((1, 3)\) and \((2, 3)\) intersect at one vertex.The third example is the same as the first example with two additional paths. Pairs \((1,4)\), \((1,5)\), \((2,5)\), \((3,4)\), \((3,5)\), \((3,6)\) and \((5,6)\) intersect at one vertex.
|
Input: 5 1 2 1 3 1 4 3 5 4 2 3 2 4 3 4 3 5 | Output: 2
|
Expert
| 5 | 324 | 544 | 92 | 14 |
1,611 |
C
|
1611C
|
C. Polycarp Recovers the Permutation
| 1,000 |
constructive algorithms
|
Polycarp wrote on a whiteboard an array \(p\) of length \(n\), which is a permutation of numbers from \(1\) to \(n\). In other words, in \(p\) each number from \(1\) to \(n\) occurs exactly once.He also prepared a resulting array \(a\), which is initially empty (that is, it has a length of \(0\)).After that, he did exactly \(n\) steps. Each step looked like this: Look at the leftmost and rightmost elements of \(p\), and pick the smaller of the two. If you picked the leftmost element of \(p\), append it to the left of \(a\); otherwise, if you picked the rightmost element of \(p\), append it to the right of \(a\). The picked element is erased from \(p\). Note that on the last step, \(p\) has a length of \(1\) and its minimum element is both leftmost and rightmost. In this case, Polycarp can choose what role the minimum element plays. In other words, this element can be added to \(a\) both on the left and on the right (at the discretion of Polycarp).Let's look at an example. Let \(n=4\), \(p=[3, 1, 4, 2]\). Initially \(a=[]\). Then: During the first step, the minimum is on the right (with a value of \(2\)), so after this step, \(p=[3,1,4]\) and \(a=[2]\) (he added the value \(2\) to the right). During the second step, the minimum is on the left (with a value of \(3\)), so after this step, \(p=[1,4]\) and \(a=[3,2]\) (he added the value \(3\) to the left). During the third step, the minimum is on the left (with a value of \(1\)), so after this step, \(p=[4]\) and \(a=[1,3,2]\) (he added the value \(1\) to the left). During the fourth step, the minimum is both left and right (this value is \(4\)). Let's say Polycarp chose the right option. After this step, \(p=[]\) and \(a=[1,3,2,4]\) (he added the value \(4\) to the right).Thus, a possible value of \(a\) after \(n\) steps could be \(a=[1,3,2,4]\).You are given the final value of the resulting array \(a\). Find any possible initial value for \(p\) that can result the given \(a\), or determine that there is no solution.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.Each test case consists of two lines. The first of them contains an integer \(n\) (\(1 \le n \le 2\cdot10^5\)) β the length of the array \(a\). The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β the elements of the array \(a\). All elements of the \(a\) array are distinct numbers.It is guaranteed that the sum of the values \(n\) over all test cases in the test does not exceed \(2\cdot10^5\).
|
Print \(t\) lines, each of the lines must contain the answer to the corresponding set of input data: numbers \(p_1, p_2, \dots, p_n\) β any of the possible initial values of the array \(p\), which will lead to the given array \(a\). All elements of \(p\) are distinct integers from \(1\) to \(n\). Thus, if there are several solutions, print any. If there is no solution, then print -1 on the line.
|
The first test case in the example is clarified in the main section of the problem statement. There may be other correct answers for this test set.In the second test case, \(n=1\). Thus, there is only one permutation that can be the answer: \(p=[1]\). Indeed, this is the answer to this test case.In the third test case of the example, no matter what permutation you take as \(p\), after applying the \(n\) steps, the result will differ from \(a=[1, 3, 5, 4, 2]\).
|
Input: 4 4 1 3 2 4 1 1 5 1 3 5 4 2 3 3 2 1 | Output: 3 1 4 2 1 -1 2 3 1
|
Beginner
| 1 | 1,998 | 548 | 398 | 16 |
140 |
F
|
140F
|
F. New Year Snowflake
| 2,600 |
geometry; sortings
|
As Gerald ..., in other words, on a New Year Eve Constantine prepared an unusual present for the Beautiful Lady. The present is the magic New Year snowflake that can make any dream come true.The New Year snowflake consists of tiny ice crystals, which can be approximately regarded as points on the plane. The beauty of the New Year snowflake is that it has a center of symmetry. This is a point such that for each crystal of the snowflake exists another crystal, symmetrical to it relative to that point. One of the crystals can be placed directly in the center of symmetry.While Constantine was choosing a snowflake among millions of other snowflakes, no less symmetrical and no less magical, then endured a difficult path through the drifts to the house of his mistress, while he was waiting with bated breath for a few long moments before the Beautiful Lady opens the door, some of the snowflake crystals melted and naturally disappeared. Constantine is sure that there were no more than k of such crystals, because he handled the snowflake very carefully. Now he is ready to demonstrate to the Beautiful Lady all the power of nanotechnology and restore the symmetry of snowflakes.You are given the coordinates of the surviving snowflake crystals, given in nanometers. Your task is to identify all possible positions of the original center of symmetry.
|
The first line contains two integers n and k (1 β€ n β€ 200 000, 0 β€ k β€ 10) β the number of the surviving snowflake crystals and the maximum number of melted crystals, correspondingly. Next n lines contain the coordinates of the crystals that are left in the following form: ""xi yi"". The coordinates are integers and do not exceed 5Β·108 in absolute value. All given points are different.
|
The first line contains an integer c β the number of possible symmetry centers. Next c lines should contain the centers' descriptions. Each symmetry center is described by a couple of coordinates ""x y"", separated by a space. Print the coordinates with absolute error not exceeding 10 - 6. You are allowed to print the symmetry centers in any order. All printed points should be different. If there exist an infinite number of possible symmetry centers, print the single number ""-1"".
|
Input: 4 00 00 11 01 1 | Output: 10.5 0.5
|
Expert
| 2 | 1,355 | 388 | 486 | 1 |
|
1,674 |
E
|
1674E
|
E. Breaking the Wall
| 2,000 |
binary search; brute force; constructive algorithms; greedy; math
|
Monocarp plays ""Rage of Empires II: Definitive Edition"" β a strategic computer game. Right now he's planning to attack his opponent in the game, but Monocarp's forces cannot enter the opponent's territory since the opponent has built a wall.The wall consists of \(n\) sections, aligned in a row. The \(i\)-th section initially has durability \(a_i\). If durability of some section becomes \(0\) or less, this section is considered broken.To attack the opponent, Monocarp needs to break at least two sections of the wall (any two sections: possibly adjacent, possibly not). To do this, he plans to use an onager β a special siege weapon. The onager can be used to shoot any section of the wall; the shot deals \(2\) damage to the target section and \(1\) damage to adjacent sections. In other words, if the onager shoots at the section \(x\), then the durability of the section \(x\) decreases by \(2\), and the durability of the sections \(x - 1\) and \(x + 1\) (if they exist) decreases by \(1\) each. Monocarp can shoot at any sections any number of times, he can even shoot at broken sections.Monocarp wants to calculate the minimum number of onager shots needed to break at least two sections. Help him!
|
The first line contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of sections.The second line contains the sequence of integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^6\)), where \(a_i\) is the initial durability of the \(i\)-th section.
|
Print one integer β the minimum number of onager shots needed to break at least two sections of the wall.
|
In the first example, it is possible to break the \(2\)-nd and the \(4\)-th section in \(10\) shots, for example, by shooting the third section \(10\) times. After that, the durabilities become \([20, 0, 10, 0, 20]\). Another way of doing it is firing \(5\) shots at the \(2\)-nd section, and another \(5\) shots at the \(4\)-th section. After that, the durabilities become \([15, 0, 20, 0, 15]\).In the second example, it is enough to shoot the \(2\)-nd section once. Then the \(1\)-st and the \(3\)-rd section will be broken.In the third example, it is enough to shoot the \(2\)-nd section twice (then the durabilities become \([5, 2, 4, 8, 5, 8]\)), and then shoot the \(3\)-rd section twice (then the durabilities become \([5, 0, 0, 6, 5, 8]\)). So, four shots are enough to break the \(2\)-nd and the \(3\)-rd section.
|
Input: 5 20 10 30 10 20 | Output: 10
|
Hard
| 5 | 1,209 | 263 | 105 | 16 |
939 |
A
|
939A
|
A. Love Triangle
| 800 |
graphs
|
As you could know there are no male planes nor female planes. However, each plane on Earth likes some other plane. There are n planes on Earth, numbered from 1 to n, and the plane with number i likes the plane with number fi, where 1 β€ fi β€ n and fi β i.We call a love triangle a situation in which plane A likes plane B, plane B likes plane C and plane C likes plane A. Find out if there is any love triangle on Earth.
|
The first line contains a single integer n (2 β€ n β€ 5000) β the number of planes.The second line contains n integers f1, f2, ..., fn (1 β€ fi β€ n, fi β i), meaning that the i-th plane likes the fi-th.
|
Output Β«YESΒ» if there is a love triangle consisting of planes on Earth. Otherwise, output Β«NOΒ».You can output any letter in lower case or in upper case.
|
In first example plane 2 likes plane 4, plane 4 likes plane 1, plane 1 likes plane 2 and that is a love triangle.In second example there are no love triangles.
|
Input: 52 4 5 1 3 | Output: YES
|
Beginner
| 1 | 419 | 199 | 152 | 9 |
1,630 |
A
|
1630A
|
A. And Matching
| 1,500 |
bitmasks; constructive algorithms
|
You are given a set of \(n\) (\(n\) is always a power of \(2\)) elements containing all integers \(0, 1, 2, \ldots, n-1\) exactly once.Find \(\frac{n}{2}\) pairs of elements such that: Each element in the set is in exactly one pair. The sum over all pairs of the bitwise AND of its elements must be exactly equal to \(k\). Formally, if \(a_i\) and \(b_i\) are the elements of the \(i\)-th pair, then the following must hold: $$$\(\sum_{i=1}^{n/2}{a_i \& b_i} = k,\)\( where \)\&\( denotes the bitwise AND operation. If there are many solutions, print any of them, if there is no solution, print \)-1$$$ instead.
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 400\)) β the number of test cases. Description of the test cases follows.Each test case consists of a single line with two integers \(n\) and \(k\) (\(4 \leq n \leq 2^{16}\), \(n\) is a power of \(2\), \(0 \leq k \leq n-1\)).The sum of \(n\) over all test cases does not exceed \(2^{16}\). All test cases in each individual input will be pairwise different.
|
For each test case, if there is no solution, print a single line with the integer \(-1\).Otherwise, print \(\frac{n}{2}\) lines, the \(i\)-th of them must contain \(a_i\) and \(b_i\), the elements in the \(i\)-th pair. If there are many solutions, print any of them. Print the pairs and the elements in the pairs in any order.
|
In the first test, \((0\&3)+(1\&2) = 0\).In the second test, \((0\&2)+(1\&3) = 1\).In the third test, \((0\&1)+(2\&3) = 2\).In the fourth test, there is no solution.
|
Input: 44 04 14 24 3 | Output: 0 3 1 2 0 2 1 3 0 1 2 3 -1
|
Medium
| 2 | 611 | 464 | 326 | 16 |
1,730 |
C
|
1730C
|
C. Minimum Notation
| 1,200 |
data structures; greedy; math; sortings
|
You have a string \(s\) consisting of digits from \(0\) to \(9\) inclusive. You can perform the following operation any (possibly zero) number of times: You can choose a position \(i\) and delete a digit \(d\) on the \(i\)-th position. Then insert the digit \(\min{(d + 1, 9)}\) on any position (at the beginning, at the end or in between any two adjacent digits). What is the lexicographically smallest string you can get by performing these operations?A string \(a\) is lexicographically smaller than a string \(b\) of the same length if and only if the following holds: in the first position where \(a\) and \(b\) differ, the string \(a\) has a smaller digit than the corresponding digit in \(b\).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then the test cases follow.Each test case consists of a single line that contains one string \(s\) (\(1 \le |s| \le 2 \cdot 10^5\)) β the string consisting of digits. Please note that \(s\) is just a string consisting of digits, so leading zeros are allowed.It is guaranteed that the sum of lengths of \(s\) over all test cases does not exceed \(2 \cdot 10^5\).
|
Print a single string β the minimum string that is possible to obtain.
|
In the first test case: Delete \(8\) and insert \(9\) at the end of the notation. The resulting notation is \(04299\). Delete \(4\) and insert \(5\) in the \(3\)-rd position of the notation. The resulting notation is \(02599\). Nothing needs to be done in the second and third test cases.
|
Input: 404829901314752277691991 | Output: 02599 9 01 111334567888999
|
Easy
| 4 | 700 | 459 | 70 | 17 |
356 |
C
|
356C
|
C. Compartments
| 2,100 |
combinatorics; constructive algorithms; greedy; implementation
|
A team of students from the city S is sent to the All-Berland Olympiad in Informatics. Traditionally, they go on the train. All students have bought tickets in one carriage, consisting of n compartments (each compartment has exactly four people). We know that if one compartment contain one or two students, then they get bored, and if one compartment contain three or four students, then the compartment has fun throughout the entire trip.The students want to swap with other people, so that no compartment with students had bored students. To swap places with another person, you need to convince him that it is really necessary. The students can not independently find the necessary arguments, so they asked a sympathetic conductor for help. The conductor can use her life experience to persuade any passenger to switch places with some student.However, the conductor does not want to waste time persuading the wrong people, so she wants to know what is the minimum number of people necessary to persuade her to change places with the students. Your task is to find the number. After all the swaps each compartment should either have no student left, or have a company of three or four students.
|
The first line contains integer n (1 β€ n β€ 106) β the number of compartments in the carriage. The second line contains n integers a1, a2, ..., an showing how many students ride in each compartment (0 β€ ai β€ 4). It is guaranteed that at least one student is riding in the train.
|
If no sequence of swapping seats with other people leads to the desired result, print number ""-1"" (without the quotes). In another case, print the smallest number of people you need to persuade to swap places.
|
Input: 51 2 2 4 3 | Output: 2
|
Hard
| 4 | 1,198 | 277 | 211 | 3 |
|
645 |
D
|
645D
|
D. Robot Rapping Results Report
| 1,800 |
binary search; dp; graphs
|
While Farmer John rebuilds his farm in an unfamiliar portion of Bovinia, Bessie is out trying some alternative jobs. In her new gig as a reporter, Bessie needs to know about programming competition results as quickly as possible. When she covers the 2016 Robot Rap Battle Tournament, she notices that all of the robots operate under deterministic algorithms. In particular, robot i will beat robot j if and only if robot i has a higher skill level than robot j. And if robot i beats robot j and robot j beats robot k, then robot i will beat robot k. Since rapping is such a subtle art, two robots can never have the same skill level.Given the results of the rap battles in the order in which they were played, determine the minimum number of first rap battles that needed to take place before Bessie could order all of the robots by skill level.
|
The first line of the input consists of two integers, the number of robots n (2 β€ n β€ 100 000) and the number of rap battles m ().The next m lines describe the results of the rap battles in the order they took place. Each consists of two integers ui and vi (1 β€ ui, vi β€ n, ui β vi), indicating that robot ui beat robot vi in the i-th rap battle. No two rap battles involve the same pair of robots.It is guaranteed that at least one ordering of the robots satisfies all m relations.
|
Print the minimum k such that the ordering of the robots by skill level is uniquely defined by the first k rap battles. If there exists more than one ordering that satisfies all m relations, output -1.
|
In the first sample, the robots from strongest to weakest must be (4, 2, 1, 3), which Bessie can deduce after knowing the results of the first four rap battles.In the second sample, both (1, 3, 2) and (3, 1, 2) are possible orderings of the robots from strongest to weakest after both rap battles.
|
Input: 4 52 11 32 34 24 3 | Output: 4
|
Medium
| 3 | 845 | 482 | 201 | 6 |
603 |
E
|
603E
|
E. Pastoral Oddities
| 3,000 |
data structures; divide and conquer; dsu; math; trees
|
In the land of Bovinia there are n pastures, but no paths connecting the pastures. Of course, this is a terrible situation, so Kevin Sun is planning to rectify it by constructing m undirected paths connecting pairs of distinct pastures. To make transportation more efficient, he also plans to pave some of these new paths.Kevin is very particular about certain aspects of path-paving. Since he loves odd numbers, he wants each pasture to have an odd number of paved paths connected to it. Thus we call a paving sunny if each pasture is incident to an odd number of paved paths. He also enjoys short paths more than long paths, so he would like the longest paved path to be as short as possible. After adding each path, Kevin wants to know if a sunny paving exists for the paths of Bovinia, and if at least one does, the minimum possible length of the longest path in such a paving. Note that ""longest path"" here means maximum-weight edge.
|
The first line contains two integers n (2 β€ n β€ 100 000) and m (1 β€ m β€ 300 000), denoting the number of pastures and paths, respectively. The next m lines each contain three integers ai, bi and li, describing the i-th path. The i-th path connects pastures ai and bi (1 β€ ai, bi β€ n; ai β bi) and has length li (1 β€ li β€ 109). Paths are given in the order in which they are constructed.
|
Output m lines. The i-th line should contain a single integer denoting the minimum possible length of the longest path (maximum-weight edge) in a sunny paving using only the first i paths. If Kevin cannot pave a set of paths so that each pasture is incident to an odd number of paved paths, output - 1.Note that the paving is only hypotheticalβyour answer after adding the i-th path should not be affected by any of your previous answers.
|
For the first sample, these are the paths that Kevin should pave after building the i-th path: No set of paths works. Paths 1 (length 4) and 2 (length 8). Paths 1 (length 4) and 2 (length 8). Paths 3 (length 2) and 4 (length 3). In the second sample, there never exists a paving that makes Kevin happy.
|
Input: 4 41 3 42 4 81 2 23 4 3 | Output: -1883
|
Master
| 5 | 940 | 386 | 438 | 6 |
100 |
C
|
100C
|
C. A+B
| 1,400 |
*special; implementation
|
Bijan is new to programming. He learned recently that programmers do not code every bit of their apps from scratch.For example they never write a code to sum two integers, because their languages have the ability to do the sum. But can they use it? Can an experienced coder who has attended more than 100 contests, sum two integers?Bijan is not sure about this. What's your opinion?
|
You are given two integers a and b, one per line (1 β€ a, b < 10500). These numbers will not have any leading zeros.
|
Write sum of the two integers. Do not put any leading zeros.
|
Input: 23 | Output: 5
|
Easy
| 2 | 382 | 115 | 60 | 1 |
|
214 |
A
|
214A
|
A. System of Equations
| 800 |
brute force
|
Furik loves math lessons very much, so he doesn't attend them, unlike Rubik. But now Furik wants to get a good mark for math. For that Ms. Ivanova, his math teacher, gave him a new task. Furik solved the task immediately. Can you?You are given a system of equations: You should count, how many there are pairs of integers (a, b) (0 β€ a, b) which satisfy the system.
|
A single line contains two integers n, m (1 β€ n, m β€ 1000) β the parameters of the system. The numbers on the line are separated by a space.
|
On a single line print the answer to the problem.
|
In the first sample the suitable pair is integers (3, 0). In the second sample the suitable pair is integers (3, 5). In the third sample there is no suitable pair.
|
Input: 9 3 | Output: 1
|
Beginner
| 1 | 365 | 140 | 49 | 2 |
1,942 |
H
|
1942H
|
H. Farmer John's Favorite Intern
| 3,500 |
data structures; dp; flows; trees
|
Peaches...β Ruby just won an internship position at Farmer John's farm by winning a coding competition! As the newly recruited intern, Ruby is tasked with maintaining Farmer John's peach tree, a tree consisting of \(n\) nodes rooted at node \(1\). Each node initially contains \(a_i = 0\) peaches, and there are two types of events that can happen: Growth event at some node \(x\): Ruby must choose either the parent of \(x\) or any node in the subtree of \(x\) and increase the amount of peaches it contains by one. Harvest event at some node \(x\): Ruby must choose a single node that is in the subtree of \(x\) and decrease the amount of peaches it contains by one. Note that this is not the same set of nodes as the growth event. Note that the subtree of \(x\) includes the node \(x\) as well.Ruby is also given an array \(b\) of length \(n\). The peach tree is deemed healthy if \(a_i \ge b_i\) for every node \(i\).Ruby is asked to perform \(q\) operations of two types: 1 x v β Perform \(v\) growth events on node \(x\). Ruby does not have to choose the same node to increase in every growth event. 2 x v β Perform \(v\) harvest events on node \(x\). Ruby does not have to choose the same node to decrease in every harvest event. For every prefix of operations, Ruby asks you to find if she can perform these operations in some order such that the resulting peach tree (at the end of these operations) is healthy. Note that Ruby can't perform a harvest event that makes any \(a_i\) negative.Every prefix is independent, meaning that for a given operation, Ruby may choose different nodes to perform events on for every prefix that contains that operation.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \le n, q \le 2 \cdot 10^5\)) β the size of the tree and the number of operations.The second line contains \(n - 1\) integers \(p_2, p_3, \ldots, p_n\) (\(1 \le p_i < i\)) β the parent of each node.The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(0 \le b_i \le 10^6\)) β the minimum number of peaches each node needs for the peach tree to be considered healthy.The next \(q\) lines describe the operations Ruby will perform. Each line contains three integers \(t\), \(x\), and \(v\) (\(1 \le t \le 2\), \(1 \le x \le n\), \(1 \le v \le 10^6\)). If \(t = 1\), this denotes that Ruby must perform \(v\) growth events on node \(x\). If \(t = 2\), this denotes that Ruby must perform \(v\) harvest events on node \(x\).It is guaranteed that the sum of \(n\) does not exceed \(2 \cdot 10^5\) and the sum of \(q\) does not exceed \(2 \cdot 10^5\)
|
For each test case, output \(q\) lines. The \(i\)-th line should contain ""YES"" if Ruby can make the peach tree healthy after performing operations \(1, 2, \ldots, i\) in some order. Otherwise, it should contain ""NO"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.
|
For the prefix containing operations \(1, 2, \ldots, 5\) in the first test case, Ruby may perform the operations in the following order: Ruby performs operation \(2\) and chooses to increase \(a_4\) by \(9\) and \(a_5\) by \(8\). Ruby performs operation \(1\) and chooses to increase \(a_1\) by \(5\), \(a_3\) by \(2\), \(a_6\) by \(4\), and \(a_8\) by \(3\). Ruby performs operation \(3\) and chooses to increase \(a_2\) by \(7\). Ruby performs operation \(4\) and chooses to decrease \(a_2\) by \(1\). Ruby performs operation \(5\) and chooses to increase \(a_7\) by \(1\).
|
Input: 28 81 1 1 4 3 6 65 6 2 9 8 4 1 31 3 141 4 171 2 72 2 11 6 12 1 10000001 4 9999991 3 110 201 1 1 2 5 2 4 7 2311353 270334 74853 385085 315501 183346 234819 417314 103862 4294371 1 8375411 10 9338761 1 5659581 4 7914552 3 850542 3 4409781 4 9810401 5 685222 1 8583052 4 1843081 4 9050812 8 5196262 2 2690901 1 430162 2 5176441 5 3557921 9 3192412 10 1254472 10 5238901 10 241045 | Output: NO NO YES NO YES NO NO YES NO NO NO YES YES NO NO YES YES NO YES YES NO YES NO NO YES YES NO NO
|
Master
| 4 | 1,661 | 1,031 | 381 | 19 |
1,657 |
C
|
1657C
|
C. Bracket Sequence Deletion
| 1,200 |
greedy; implementation
|
You are given a bracket sequence consisting of \(n\) characters '(' and/or )'. You perform several operations with it.During one operation, you choose the shortest prefix of this string (some amount of first characters of the string) that is good and remove it from the string.The prefix is considered good if one of the following two conditions is satisfied: this prefix is a regular bracket sequence; this prefix is a palindrome of length at least two. A bracket sequence is called regular if it is possible to obtain a correct arithmetic expression by inserting characters '+' and '1' into this sequence. For example, sequences (())(), () and (()(())) are regular, while )(, (() and (()))( are not.The bracket sequence is called palindrome if it reads the same back and forth. For example, the bracket sequences )), (( and )(() are palindromes, while bracket sequences (), )( and ))( are not palindromes.You stop performing the operations when it's not possible to find a good prefix. Your task is to find the number of operations you will perform on the given string and the number of remaining characters in the string.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The next \(2t\) lines describe test cases.The first line of the test case contains one integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) β the length of the bracket sequence.The second line of the test case contains \(n\) characters '(' and/or ')' β the bracket sequence itself.It is guaranteed that the sum of \(n\) over all test cases do not exceed \(5 \cdot 10^5\) (\(\sum n \le 5 \cdot 10^5\)).
|
For each test case, print two integers \(c\) and \(r\) β the number of operations you will perform on the given bracket sequence and the number of characters that remain in the string after performing all operations.
|
Input: 52()3())4((((5)((()6)((()( | Output: 1 0 1 1 2 0 1 0 1 1
|
Easy
| 2 | 1,172 | 499 | 216 | 16 |
|
327 |
C
|
327C
|
C. Magic Five
| 1,700 |
combinatorics; math
|
There is a long plate s containing n digits. Iahub wants to delete some digits (possibly none, but he is not allowed to delete all the digits) to form his ""magic number"" on the plate, a number that is divisible by 5. Note that, the resulting number may contain leading zeros.Now Iahub wants to count the number of ways he can obtain magic number, modulo 1000000007 (109 + 7). Two ways are different, if the set of deleted positions in s differs.Look at the input part of the statement, s is given in a special form.
|
In the first line you're given a string a (1 β€ |a| β€ 105), containing digits only. In the second line you're given an integer k (1 β€ k β€ 109). The plate s is formed by concatenating k copies of a together. That is n = |a|Β·k.
|
Print a single integer β the required number of ways modulo 1000000007 (109 + 7).
|
In the first case, there are four possible ways to make a number that is divisible by 5: 5, 15, 25 and 125.In the second case, remember to concatenate the copies of a. The actual plate is 1399013990.In the third case, except deleting all digits, any choice will do. Therefore there are 26 - 1 = 63 possible ways to delete digits.
|
Input: 12561 | Output: 4
|
Medium
| 2 | 517 | 224 | 81 | 3 |
31 |
A
|
31A
|
A. Worms Evolution
| 1,200 |
implementation
|
Professor Vasechkin is studying evolution of worms. Recently he put forward hypotheses that all worms evolve by division. There are n forms of worms. Worms of these forms have lengths a1, a2, ..., an. To prove his theory, professor needs to find 3 different forms that the length of the first form is equal to sum of lengths of the other two forms. Help him to do this.
|
The first line contains integer n (3 β€ n β€ 100) β amount of worm's forms. The second line contains n space-separated integers ai (1 β€ ai β€ 1000) β lengths of worms of each form.
|
Output 3 distinct integers i j k (1 β€ i, j, k β€ n) β such indexes of worm's forms that ai = aj + ak. If there is no such triple, output -1. If there are several solutions, output any of them. It possible that aj = ak.
|
Input: 51 2 3 5 7 | Output: 3 2 1
|
Easy
| 1 | 369 | 177 | 217 | 0 |
|
1,804 |
B
|
1804B
|
B. Vaccination
| 1,000 |
greedy; implementation
|
Ethan runs a vaccination station to help people combat the seasonal flu. He analyses the historical data in order to develop an optimal strategy for vaccine usage.Consider there are \(n\) patients coming to the station on a particular day. The \(i\)-th patient comes at the moment \(t_i\). We know that each of these patients can be asked to wait for no more than \(w\) time moments. That means the \(i\)-th patient can get vaccine at moments \(t_i, t_i + 1, \ldots, t_i + w\).Vaccines come in packs, each pack consists of \(k\) doses. Each patient needs exactly one dose. Packs are stored in a special fridge. After a pack is taken out of the fridge and opened, it can no longer be put back. The lifetime of the vaccine outside the fridge is \(d\) moments of time. Thus, if the pack was taken out of the fridge and opened at moment \(x\), its doses can be used to vaccinate patients at moments \(x, x + 1, \ldots, x + d\). At moment \(x + d + 1\) all the remaining unused doses of this pack are thrown away.Assume that the vaccination station has enough staff to conduct an arbitrary number of operations at every moment of time. What is the minimum number of vaccine packs required to vaccinate all \(n\) patients?
|
The first line of the input contains the number of test cases \(t\) (\(1 \leq t \leq 10^4\)). Then follow \(t\) descriptions of the test cases.The first line of each test case contains four integers \(n\), \(k\), \(d\) and \(w\) (\(1 \leq n, k \leq 2 \cdot 10^5\), \(0 \leq d, w \leq 10^6\)). They are the number of patients, the number of doses per vaccine pack, the number of moments of time the vaccine can live outside the fridge, and the number of moments of time each of the patients can wait, respectively.The second line of each test case contains a non-decreasing sequence \(t_1, t_2, \ldots, t_n\) (\(0 \leq t_1 \leq t_2 \leq \ldots \leq t_n \leq 10^6\)). The \(i\)-th element of this sequence is the moment when the \(i\)-th patient comes to the vaccination station.It is guaranteed that the sum of \(n\) over all test cases won't exceed \(2 \cdot 10^5\).
|
Output one integer, the minimum number of vaccine packs required to vaccinate all \(n\) patients.
|
In the first example, the first pack can be opened at moment \(1\) to vaccinate patient \(1\). The vaccine is durable enough to be used at moments \(2\) and \(3\) for patients \(2\) and \(3\), respectively. Then the staff needs to ask patients \(4\) and \(5\) to wait for moment \(13\). At moment \(13\) the staff opens the second vaccine pack and serves patients \(4\) and \(5\). Finally, the last patient comes at moment \(18\) and immediately gets the last dose of the second pack while it is still fine.In the second example, the vaccine should be used exactly at the moment it is taken out of the fridge. Moreover, all the patients want to be served at exactly the same moment they come. That means the staff needs to open two packs at moment \(3\) and use five doses on patients \(1\), \(2\), \(3\), \(4\), and \(5\). There will be three doses left ouf of these two packs but they can't be used for patient \(6\). When patient \(6\) comes at moment \(4\) the staff needs to open a new pack just to use only one dose out of it.
|
Input: 56 3 5 31 2 3 10 11 186 4 0 03 3 3 3 3 49 10 2 20 1 2 3 4 5 6 7 83 10 3 610 20 305 5 4 40 2 4 6 8 | Output: 2 3 2 3 1
|
Beginner
| 2 | 1,216 | 866 | 97 | 18 |
1,218 |
D
|
1218D
|
D. Xor Spanning Tree
| 2,400 |
divide and conquer; fft; graphs
|
In the galaxy far far away is the ancient interplanetary republic of Bubbleland, consisting of \(N\) planets. Between them, there are \(M\) bidirectional wormholes, each connecting a pair of planets. Bubbleland is a very centralized republic, having a capital planet Whiteplanet, from which any another planet can be reached using these wormholes. It is also guaranteed that no wormhole connects planet to itself and that no two different wormholes connect same pair of planets. We call a path that begins at one planet, visits other planets and each of them at most once and returns to starting point a tour. Interplanetary Safety Regulations guarantee that each planet belongs to at most one tour and that there are at most \(42\) tours.After many eons of usage, wormholes need to be repaired and each wormhole has the cost \(W_{i}\) which needs to be payed for reparation. Unfortunately, the Senate of Bubbleland is short on budget. Therefore, they have decided only to fix as many wormholes as they need in order to have all planets reachable from capital and to pay as little money as they have to for this repair. However the way in which the Senate calculates the cost is different. Cost of the set of reparations is binary xor of costs of each individual reparation, that is if reparations to be made have costs \(A_{1},A_{2},...,A_{k}\), the cost of entire set is \(A_{1} \oplus A_{2} \oplus ... \oplus A_{k}\).Now the Senate would like to know how much money do they have to pay and also the number of different ways to achieve that cost modulo \(1000000007\).
|
First line of input contains two numbers \(N (1 \leq N \leq 100.000)\), the number of planets and \(M (1 \leq M \leq 100.041)\), the number of wormholes. Following \(M\) lines contain three numbers \(U, V (1 \leq U \neq V \leq N)\) and \(W (1 \leq W \leq 100.000)\), meaning that there exists a wormhole connecting planets \(U\) and \(V\), with repair cost of \(W\).
|
Output two numbers, the smallest possible cost of entire reparation and the number of different valid reparations with that cost modulo \(1000000007\).
|
We can repair wormholes \(1\),\(2\),\(3\),\(5\) and \(6\), paying \(5 \oplus 1\oplus 2 \oplus 3 \oplus 4=1\), one can check that this is the cheapest repair in which all of the planets are connected and the only valid repair with that cost.
|
Input: 6 6 4 1 5 5 2 1 6 3 2 1 2 6 1 3 3 2 3 4 | Output: 1 1
|
Expert
| 3 | 1,570 | 366 | 151 | 12 |
1,656 |
C
|
1656C
|
C. Make Equal With Mod
| 1,200 |
constructive algorithms; math; number theory; sortings
|
You are given an array of \(n\) non-negative integers \(a_1, a_2, \ldots, a_n\). You can make the following operation: choose an integer \(x \geq 2\) and replace each number of the array by the remainder when dividing that number by \(x\), that is, for all \(1 \leq i \leq n\) set \(a_i\) to \(a_i \bmod x\).Determine if it is possible to make all the elements of the array equal by applying the operation zero or more times.
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 10^5\)) β the length of the array.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 10^9\)) where \(a_i\) is the \(i\)-th element of the array.The sum of \(n\) for all test cases is at most \(2 \cdot 10^5\).
|
For each test case, print a line with YES if you can make all elements of the list equal by applying the operation. Otherwise, print NO.You may print each letter in any case (for example, ""YES"", ""Yes"", ""yes"", ""yEs"" will all be recognized as a positive answer).
|
In the first test case, one can apply the operation with \(x = 3\) to obtain the array \([2, 2, 0, 2]\), and then apply the operation with \(x = 2\) to obtain \([0, 0, 0, 0]\).In the second test case, all numbers are already equal.In the fourth test case, applying the operation with \(x = 4\) results in the array \([1, 1, 1, 1]\).
|
Input: 442 5 6 831 1 154 1 7 0 845 9 17 5 | Output: YES YES NO YES
|
Easy
| 4 | 425 | 517 | 268 | 16 |
1,529 |
B
|
1529B
|
B. Sifid and Strange Subsequences
| 1,100 |
greedy; math; sortings
|
A sequence \((b_1, b_2, \ldots, b_k)\) is called strange, if the absolute difference between any pair of its elements is greater than or equal to the maximum element in the sequence. Formally speaking, it's strange if for every pair \((i, j)\) with \(1 \le i<j \le k\), we have \(|a_i-a_j|\geq MAX\), where \(MAX\) is the largest element of the sequence. In particular, any sequence of length at most \(1\) is strange.For example, the sequences \((-2021, -1, -1, -1)\) and \((-1, 0, 1)\) are strange, but \((3, 0, 1)\) is not, because \(|0 - 1| < 3\).Sifid has an array \(a\) of \(n\) integers. Sifid likes everything big, so among all the strange subsequences of \(a\), he wants to find the length of the longest one. Can you help him?A sequence \(c\) is a subsequence of an array \(d\) if \(c\) can be obtained from \(d\) by deletion of several (possibly, zero or all) elements.
|
The first line contains an integer \(t\) \((1\le t\le 10^4)\) β the number of test cases. The description of the test cases follows.The first line of each test case contains an integer \(n\) \((1\le n\le 10^5)\) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) \((-10^9\le a_i \le 10^9)\) β the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(10^5\).
|
For each test case output a single integer β the length of the longest strange subsequence of \(a\).
|
In the first test case, one of the longest strange subsequences is \((a_1, a_2, a_3, a_4)\)In the second test case, one of the longest strange subsequences is \((a_1, a_3, a_4, a_5, a_7)\).In the third test case, one of the longest strange subsequences is \((a_1, a_3, a_4, a_5)\).In the fourth test case, one of the longest strange subsequences is \((a_2)\).In the fifth test case, one of the longest strange subsequences is \((a_1, a_2, a_4)\).
|
Input: 6 4 -1 -2 0 0 7 -3 4 -2 0 -4 6 1 5 0 5 -3 2 -5 3 2 3 1 4 -3 0 2 0 6 -3 -2 -1 1 1 1 | Output: 4 5 4 1 3 4
|
Easy
| 3 | 880 | 473 | 100 | 15 |
277 |
C
|
277C
|
C. Game
| 2,400 |
games; implementation
|
Two players play the following game. Initially, the players have a knife and a rectangular sheet of paper, divided into equal square grid cells of unit size. The players make moves in turn, the player who can't make a move loses. In one move, a player can take the knife and cut the paper along any segment of the grid line (not necessarily from border to border). The part of the paper, that touches the knife at least once, is considered cut. There is one limit not to turn the game into an infinite cycle: each move has to cut the paper, that is the knife has to touch the part of the paper that is not cut before.Obviously, the game ends when the entire sheet is cut into 1 Γ 1 blocks. During the game, the pieces of the sheet are not allowed to move. It is also prohibited to cut along the border. The coordinates of the ends of each cut must be integers.You are given an n Γ m piece of paper, somebody has already made k cuts there. Your task is to determine who will win if the players start to play on this sheet. You can consider that both players play optimally well. If the first player wins, you also need to find the winning first move.
|
The first line contains three integers n, m, k (1 β€ n, m β€ 109, 0 β€ k β€ 105) β the sizes of the piece of paper and the number of cuts. Then follow k lines, each containing 4 integers xbi, ybi, xei, yei (0 β€ xbi, xei β€ n, 0 β€ ybi, yei β€ m) β the coordinates of the ends of the existing cuts. It is guaranteed that each cut has a non-zero length, is either vertical or horizontal and doesn't go along the sheet border.The cuts may intersect, overlap and even be the same. That is, it is not guaranteed that the cuts were obtained during any correct game.
|
If the second player wins, print ""SECOND"". Otherwise, in the first line print ""FIRST"", and in the second line print any winning move of the first player (the coordinates of the cut ends, follow input format to print them).
|
Input: 2 1 0 | Output: FIRST1 0 1 1
|
Expert
| 2 | 1,149 | 552 | 226 | 2 |
|
1,250 |
B
|
1250B
|
B. The Feast and the Bus
| 1,800 |
brute force; constructive algorithms; greedy; math
|
Employees of JebTrains are on their way to celebrate the 256-th day of the year! There are \(n\) employees and \(k\) teams in JebTrains. Each employee is a member of some (exactly one) team. All teams are numbered from \(1\) to \(k\). You are given an array of numbers \(t_1, t_2, \dots, t_n\) where \(t_i\) is the \(i\)-th employee's team number.JebTrains is going to rent a single bus to get employees to the feast. The bus will take one or more rides. A bus can pick up an entire team or two entire teams. If three or more teams take a ride together they may start a new project which is considered unacceptable. It's prohibited to split a team, so all members of a team should take the same ride.It is possible to rent a bus of any capacity \(s\). Such a bus can take up to \(s\) people on a single ride. The total cost of the rent is equal to \(s \cdot r\) burles where \(r\) is the number of rides. Note that it's impossible to rent two or more buses.Help JebTrains to calculate the minimum cost of the rent, required to get all employees to the feast, fulfilling all the conditions above.
|
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 5\cdot10^5, 1 \le k \le 8000\)) β the number of employees and the number of teams in JebTrains. The second line contains a sequence of integers \(t_1, t_2, \dots, t_n\), where \(t_i\) (\(1 \le t_i \le k\)) is the \(i\)-th employee's team number. Every team contains at least one employee.
|
Print the minimum cost of the rent.
|
Input: 6 3 3 1 2 3 2 3 | Output: 6
|
Medium
| 4 | 1,095 | 356 | 35 | 12 |
|
1,385 |
A
|
1385A
|
A. Three Pairwise Maximums
| 800 |
math
|
You are given three positive (i.e. strictly greater than zero) integers \(x\), \(y\) and \(z\).Your task is to find positive integers \(a\), \(b\) and \(c\) such that \(x = \max(a, b)\), \(y = \max(a, c)\) and \(z = \max(b, c)\), or determine that it is impossible to find such \(a\), \(b\) and \(c\).You have to answer \(t\) independent test cases. Print required \(a\), \(b\) and \(c\) in any (arbitrary) order.
|
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 only line of the test case contains three integers \(x\), \(y\), and \(z\) (\(1 \le x, y, z \le 10^9\)).
|
For each test case, print the answer: ""NO"" in the only line of the output if a solution doesn't exist; or ""YES"" in the first line and any valid triple of positive integers \(a\), \(b\) and \(c\) (\(1 \le a, b, c \le 10^9\)) in the second line. You can print \(a\), \(b\) and \(c\) in any order.
|
Input: 5 3 2 3 100 100 100 50 49 49 10 30 20 1 1000000000 1000000000 | Output: YES 3 2 1 YES 100 100 100 NO NO YES 1 1 1000000000
|
Beginner
| 1 | 413 | 251 | 298 | 13 |
|
1,396 |
B
|
1396B
|
B. Stoned Game
| 1,800 |
brute force; constructive algorithms; games; greedy
|
T is playing a game with his friend, HL.There are \(n\) piles of stones, the \(i\)-th pile initially has \(a_i\) stones. T and HL will take alternating turns, with T going first. In each turn, a player chooses a non-empty pile and then removes a single stone from it. However, one cannot choose a pile that has been chosen in the previous turn (the pile that was chosen by the other player, or if the current turn is the first turn then the player can choose any non-empty pile). The player who cannot choose a pile in his turn loses, and the game ends.Assuming both players play optimally, given the starting configuration of \(t\) games, determine the winner of each game.
|
The first line of the input contains a single integer \(t\) \((1 \le t \le 100)\) β the number of games. The description of the games follows. Each description contains two lines:The first line contains a single integer \(n\) \((1 \le n \le 100)\) β the number of piles.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) \((1 \le a_i \le 100)\).
|
For each game, print on a single line the name of the winner, ""T"" or ""HL"" (without quotes)
|
In the first game, T removes a single stone from the only pile in his first turn. After that, although the pile still contains \(1\) stone, HL cannot choose from this pile because it has been chosen by T in the previous turn. Therefore, T is the winner.
|
Input: 2 1 2 2 1 1 | Output: T HL
|
Medium
| 4 | 674 | 359 | 94 | 13 |
1,734 |
D
|
1734D
|
D. Slime Escape
| 1,800 |
data structures; dp; greedy; two pointers
|
You are playing a game called Slime Escape. The game takes place on a number line. Initially, there are \(n\) slimes. For all positive integers \(i\) where \(1 \le i \le n\), the \(i\)-th slime is located at position \(i\) and has health \(a_i\). You are controlling the slime at position \(k\). There are two escapes located at positions \(0\) and \(n+1\). Your goal is to reach any one of the two escapes by performing any number of game moves.In one game move, you move your slime to the left or right by one position. However, if there is another slime in the new position, you must absorb it. When absorbing a slime, the health of your slime would be increased by the health of the absorbed slime, then the absorbed slime would be removed from the game.Note that some slimes might have negative health, so your health would decrease when absorbing such slimes. You lose the game immediately if your slime has negative health at any moment during the game.Can you reach one of two escapes by performing any number of game moves, without ever losing the game?
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 20\,000\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains two positive integers \(n\), \(k\) (\(3 \leq n \leq 200\,000\), \(1 \leq k \leq n\)) β the number of slimes and the position of your slime.The second line of each test case contains \(n\) integers, \(a_1, a_2, \ldots, a_n\) (\(-10^9 \leq a_i \leq 10^9\)) β the health of the slimes.It is guaranteed that health of your slime is non-negative (\(a_k \geq 0\)), and all other slimes have non-zero health (\(a_i \ne 0\) for \(i \ne k\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(200\,000\).
|
For each test case, print ""YES"" (without quotes) if you can escape without losing, and ""NO"" (without quotes) otherwise.
|
In the first test case, you control the slime at position \(4\) with health \(6\). One way to escape is to absorb the slimes at positions \(5\), \(6\), and \(7\). Your slime escapes with \(0\) health at position \(8\).In the second test case, you control the slime with \(232\) health at position \(1\). Since your slime is already located next to the escape at position \(0\), you can move to it without absorbing any slime.In the third test case, it can be shown that your slime would always have a negative health before reaching any one of two escapes.In the fourth test case, you control the slime at position \(4\) with health \(6\). The following describes a possible sequence of moves to win: Absorb the slimes at positions \(5\), \(6\), and \(7\): your health becomes \(4\) after absorbing the slime with health \(-2\); becomes \(1\) after absorbing the slime with health \(-3\); and becomes \(7\) after absorbing the slime with health \(6\). Absorb the slimes at positions \(3\), and \(2\): your health becomes \(7-7+10=10\). Absorb the slime at position \(8\): your health becomes \(10-10=0\). Use the escape at position \(9\). Since your slime has maintained non-negative health at all times, you have won.
|
Input: 67 4-1 -2 -3 6 -2 -3 -13 1232 -500 -7007 4-1 -2 -4 6 -2 -4 -18 4-100 10 -7 6 -2 -3 6 -108 2-999 0 -2 3 4 5 6 77 37 3 3 4 2 1 1 | Output: YES YES NO YES NO YES
|
Medium
| 4 | 1,062 | 744 | 123 | 17 |
1,070 |
C
|
1070C
|
C. Cloud Computing
| 2,000 |
data structures; greedy
|
Buber is a Berland technology company that specializes in waste of investor's money. Recently Buber decided to transfer its infrastructure to a cloud. The company decided to rent CPU cores in the cloud for \(n\) consecutive days, which are numbered from \(1\) to \(n\). Buber requires \(k\) CPU cores each day.The cloud provider offers \(m\) tariff plans, the \(i\)-th tariff plan is characterized by the following parameters: \(l_i\) and \(r_i\) β the \(i\)-th tariff plan is available only on days from \(l_i\) to \(r_i\), inclusive, \(c_i\) β the number of cores per day available for rent on the \(i\)-th tariff plan, \(p_i\) β the price of renting one core per day on the \(i\)-th tariff plan. Buber can arbitrarily share its computing core needs between the tariff plans. Every day Buber can rent an arbitrary number of cores (from 0 to \(c_i\)) on each of the available plans. The number of rented cores on a tariff plan can vary arbitrarily from day to day.Find the minimum amount of money that Buber will pay for its work for \(n\) days from \(1\) to \(n\). If on a day the total number of cores for all available tariff plans is strictly less than \(k\), then this day Buber will have to work on fewer cores (and it rents all the available cores), otherwise Buber rents exactly \(k\) cores this day.
|
The first line of the input contains three integers \(n\), \(k\) and \(m\) (\(1 \le n,k \le 10^6, 1 \le m \le 2\cdot10^5\)) β the number of days to analyze, the desired daily number of cores, the number of tariff plans.The following \(m\) lines contain descriptions of tariff plans, one description per line. Each line contains four integers \(l_i\), \(r_i\), \(c_i\), \(p_i\) (\(1 \le l_i \le r_i \le n\), \(1 \le c_i, p_i \le 10^6\)), where \(l_i\) and \(r_i\) are starting and finishing days of the \(i\)-th tariff plan, \(c_i\) β number of cores, \(p_i\) β price of a single core for daily rent on the \(i\)-th tariff plan.
|
Print a single integer number β the minimal amount of money that Buber will pay.
|
Input: 5 7 31 4 5 31 3 5 22 5 10 1 | Output: 44
|
Hard
| 2 | 1,309 | 627 | 80 | 10 |
|
255 |
D
|
255D
|
D. Mr. Bender and Square
| 1,800 |
binary search; implementation; math
|
Mr. Bender has a digital table of size n Γ n, each cell can be switched on or off. He wants the field to have at least c switched on squares. When this condition is fulfilled, Mr Bender will be happy.We'll consider the table rows numbered from top to bottom from 1 to n, and the columns β numbered from left to right from 1 to n. Initially there is exactly one switched on cell with coordinates (x, y) (x is the row number, y is the column number), and all other cells are switched off. Then each second we switch on the cells that are off but have the side-adjacent cells that are on.For a cell with coordinates (x, y) the side-adjacent cells are cells with coordinates (x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1).In how many seconds will Mr. Bender get happy?
|
The first line contains four space-separated integers n, x, y, c (1 β€ n, c β€ 109; 1 β€ x, y β€ n; c β€ n2).
|
In a single line print a single integer β the answer to the problem.
|
Initially the first test has one painted cell, so the answer is 0. In the second test all events will go as is shown on the figure. .
|
Input: 6 4 3 1 | Output: 0
|
Medium
| 3 | 764 | 104 | 68 | 2 |
223 |
B
|
223B
|
B. Two Strings
| 1,900 |
data structures; dp; strings
|
A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is a string x = sk1sk2... sk|x| (1 β€ k1 < k2 < ... < k|x| β€ |s|).You've got two strings β s and t. Let's consider all subsequences of string s, coinciding with string t. Is it true that each character of string s occurs in at least one of these subsequences? In other words, is it true that for all i (1 β€ i β€ |s|), there is such subsequence x = sk1sk2... sk|x| of string s, that x = t and for some j (1 β€ j β€ |x|) kj = i.
|
The first line contains string s, the second line contains string t. Each line consists only of lowercase English letters. The given strings are non-empty, the length of each string does not exceed 2Β·105.
|
Print ""Yes"" (without the quotes), if each character of the string s occurs in at least one of the described subsequences, or ""No"" (without the quotes) otherwise.
|
In the first sample string t can occur in the string s as a subsequence in three ways: abab, abab and abab. In these occurrences each character of string s occurs at least once.In the second sample the 4-th character of the string s doesn't occur in any occurrence of string t.In the third sample there is no occurrence of string t in string s.
|
Input: ababab | Output: Yes
|
Hard
| 3 | 515 | 204 | 165 | 2 |
149 |
D
|
149D
|
D. Coloring Brackets
| 1,900 |
dp
|
Once Petya read a problem about a bracket sequence. He gave it much thought but didn't find a solution. Today you will face it.You are given string s. It represents a correct bracket sequence. A correct bracket sequence is the sequence of opening (""("") and closing ("")"") brackets, such that it is possible to obtain a correct mathematical expression from it, inserting numbers and operators between the brackets. For example, such sequences as ""(())()"" and ""()"" are correct bracket sequences and such sequences as "")()"" and ""(()"" are not.In a correct bracket sequence each bracket corresponds to the matching bracket (an opening bracket corresponds to the matching closing bracket and vice versa). For example, in a bracket sequence shown of the figure below, the third bracket corresponds to the matching sixth one and the fifth bracket corresponds to the fourth one. You are allowed to color some brackets in the bracket sequence so as all three conditions are fulfilled: Each bracket is either not colored any color, or is colored red, or is colored blue. For any pair of matching brackets exactly one of them is colored. In other words, for any bracket the following is true: either it or the matching bracket that corresponds to it is colored. No two neighboring colored brackets have the same color. Find the number of different ways to color the bracket sequence. The ways should meet the above-given conditions. Two ways of coloring are considered different if they differ in the color of at least one bracket. As the result can be quite large, print it modulo 1000000007 (109 + 7).
|
The first line contains the single string s (2 β€ |s| β€ 700) which represents a correct bracket sequence.
|
Print the only number β the number of ways to color the bracket sequence that meet the above given conditions modulo 1000000007 (109 + 7).
|
Let's consider the first sample test. The bracket sequence from the sample can be colored, for example, as is shown on two figures below. The two ways of coloring shown below are incorrect.
|
Input: (()) | Output: 12
|
Hard
| 1 | 1,602 | 104 | 138 | 1 |
1,419 |
B
|
1419B
|
B. Stairs
| 1,200 |
brute force; constructive algorithms; greedy; implementation; math
|
Jett is tired after destroying the town and she wants to have a rest. She likes high places, that's why for having a rest she wants to get high and she decided to craft staircases.A staircase is a squared figure that consists of square cells. Each staircase consists of an arbitrary number of stairs. If a staircase has \(n\) stairs, then it is made of \(n\) columns, the first column is \(1\) cell high, the second column is \(2\) cells high, \(\ldots\), the \(n\)-th column if \(n\) cells high. The lowest cells of all stairs must be in the same row.A staircase with \(n\) stairs is called nice, if it may be covered by \(n\) disjoint squares made of cells. All squares should fully consist of cells of a staircase. This is how a nice covered staircase with \(7\) stairs looks like: Find out the maximal number of different nice staircases, that can be built, using no more than \(x\) cells, in total. No cell can be used more than once.
|
The first line contains a single integer \(t\) \((1 \le t \le 1000)\) β the number of test cases.The description of each test case contains a single integer \(x\) \((1 \le x \le 10^{18})\) β the number of cells for building staircases.
|
For each test case output a single integer β the number of different nice staircases, that can be built, using not more than \(x\) cells, in total.
|
In the first test case, it is possible to build only one staircase, that consists of \(1\) stair. It's nice. That's why the answer is \(1\).In the second test case, it is possible to build two different nice staircases: one consists of \(1\) stair, and another consists of \(3\) stairs. This will cost \(7\) cells. In this case, there is one cell left, but it is not possible to use it for building any nice staircases, that have not been built yet. That's why the answer is \(2\).In the third test case, it is possible to build only one of two nice staircases: with \(1\) stair or with \(3\) stairs. In the first case, there will be \(5\) cells left, that may be used only to build a staircase with \(2\) stairs. This staircase is not nice, and Jett only builds nice staircases. That's why in this case the answer is \(1\). If Jett builds a staircase with \(3\) stairs, then there are no more cells left, so the answer is \(1\) again.
|
Input: 4 1 8 6 1000000000000000000 | Output: 1 2 1 30
|
Easy
| 5 | 939 | 235 | 147 | 14 |
446 |
E
|
446E
|
E. DZY Loves Bridges
| 3,100 |
math; matrices
|
DZY owns 2m islands near his home, numbered from 1 to 2m. He loves building bridges to connect the islands. Every bridge he builds takes one day's time to walk across.DZY has a strange rule of building the bridges. For every pair of islands u, v (u β v), he has built 2k different bridges connecting them, where (a|b means b is divisible by a). These bridges are bidirectional.Also, DZY has built some bridges connecting his home with the islands. Specifically, there are ai different bridges from his home to the i-th island. These are one-way bridges, so after he leaves his home he will never come back.DZY decides to go to the islands for sightseeing. At first he is at home. He chooses and walks across one of the bridges connecting with his home, and arrives at some island. After that, he will spend t day(s) on the islands. Each day, he can choose to stay and rest, or to walk to another island across the bridge. It is allowed to stay at an island for more than one day. It's also allowed to cross one bridge more than once.Suppose that right after the t-th day DZY stands at the i-th island. Let ans[i] be the number of ways for DZY to reach the i-th island after t-th day. Your task is to calculate ans[i] for each i modulo 1051131.
|
To avoid huge input, we use the following way to generate the array a. You are given the first s elements of array: a1, a2, ..., as. All the other elements should be calculated by formula: ai = (101Β·ai - s + 10007) mod 1051131 (s < i β€ 2m).The first line contains three integers m, t, s (1 β€ m β€ 25; 1 β€ t β€ 1018; 1 β€ s β€ min(2m, 105)).The second line contains s integers a1, a2, ..., as (1 β€ ai β€ 106).
|
To avoid huge output, you only need to output xor-sum of all the answers for all i modulo 1051131 (1 β€ i β€ 2m), i.e. (ans[1] mod 1051131) xor (ans[2] mod 1051131) xor... xor (ans[n] mod 1051131).
|
In the first sample, ans = [6, 7, 6, 6].If he wants to be at island 1 after one day, he has 6 different ways: home β> 1 -(stay)-> 1 home β> 2 β> 1 home β> 3 β> 1 home β> 3 β> 1 (note that there are two different bridges between 1 and 3) home β> 4 β> 1 home β> 4 β> 1 (note that there are two different bridges from home to 4) In the second sample, (a1, a2, a3, a4, a5, a6, a7, a8) = (389094, 705719, 547193, 653800, 947499, 17024, 416654, 861849), ans = [235771, 712729, 433182, 745954, 139255, 935785, 620229, 644335].
|
Input: 2 1 41 1 1 2 | Output: 1
|
Master
| 2 | 1,243 | 403 | 195 | 4 |
513 |
B2
|
513B2
|
B2. Permutations
| 1,800 |
bitmasks; divide and conquer; math
|
You are given a permutation p of numbers 1, 2, ..., n. Let's define f(p) as the following sum:Find the lexicographically m-th permutation of length n in the set of permutations having the maximum possible value of f(p).
|
The single line of input contains two integers n and m (1 β€ m β€ cntn), where cntn is the number of permutations of length n with maximum possible value of f(p).The problem consists of two 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 B1 (3 points), the constraint 1 β€ n β€ 8 will hold. In subproblem B2 (4 points), the constraint 1 β€ n β€ 50 will hold.
|
Output n number forming the required permutation.
|
In the first example, both permutations of numbers {1, 2} yield maximum possible f(p) which is equal to 4. Among them, (2, 1) comes second in lexicographical order.
|
Input: 2 2 | Output: 2 1
|
Medium
| 3 | 219 | 502 | 49 | 5 |
1,468 |
E
|
1468E
|
E. Four Segments
| 800 |
greedy
|
Monocarp wants to draw four line segments on a sheet of paper. He wants the \(i\)-th segment to have its length equal to \(a_i\) (\(1 \le i \le 4\)). These segments can intersect with each other, and each segment should be either horizontal or vertical.Monocarp wants to draw the segments in such a way that they enclose a rectangular space, and the area of that rectangular space should be maximum possible.For example, if Monocarp wants to draw four segments with lengths \(1\), \(2\), \(3\) and \(4\), he can do it the following way: Here, Monocarp has drawn segments \(AB\) (with length \(1\)), \(CD\) (with length \(2\)), \(BC\) (with length \(3\)) and \(EF\) (with length \(4\)). He got a rectangle \(ABCF\) with area equal to \(3\) that is enclosed by the segments. Calculate the maximum area of a rectangle Monocarp can enclose with four segments.
|
The first line contains one integer \(t\) (\(1 \le t \le 3 \cdot 10^4\)) β the number of test cases.Each test case consists of a single line containing four integers \(a_1\), \(a_2\), \(a_3\), \(a_4\) (\(1 \le a_i \le 10^4\)) β the lengths of the segments Monocarp wants to draw.
|
For each test case, print one integer β the maximum area of a rectangle Monocarp can enclose with four segments (it can be shown that the answer is always an integer).
|
The first test case of the example is described in the statement.For the second test case, Monocarp can draw the segments \(AB\), \(BC\), \(CD\) and \(DA\) as follows: Here, Monocarp has drawn segments \(AB\) (with length \(5\)), \(BC\) (with length \(5\)), \(CD\) (with length \(5\)) and \(DA\) (with length \(5\)). He got a rectangle \(ABCD\) with area equal to \(25\) that is enclosed by the segments.
|
Input: 4 1 2 3 4 5 5 5 5 3 1 4 1 100 20 20 100 | Output: 3 25 3 2000
|
Beginner
| 1 | 855 | 279 | 167 | 14 |
14 |
B
|
14B
|
B. Young Photographer
| 1,000 |
implementation
|
Among other things, Bob is keen on photography. Especially he likes to take pictures of sportsmen. That was the reason why he placed himself in position x0 of a long straight racetrack and got ready to take pictures. But the problem was that not all the runners passed him. The total amount of sportsmen, training at that racetrack, equals n. And each of them regularly runs distances within a particular segment of the racetrack, which is the same for each sportsman. For example, the first sportsman runs from position a1 to position b1, the second β from a2 to b2What is the minimum distance that Bob should move to have a chance to take pictures of each sportsman? Bob can take a picture of a sportsman, if he stands within the segment that this sportsman covers on the racetrack.
|
The first line of the input file contains integers n and x0 (1 β€ n β€ 100; 0 β€ x0 β€ 1000). The following n lines contain pairs of integers ai, bi (0 β€ ai, bi β€ 1000; ai β bi).
|
Output the required minimum distance in the same units as the positions on the racetrack. If there is no such a position, output -1.
|
Input: 3 30 714 24 6 | Output: 1
|
Beginner
| 1 | 784 | 174 | 132 | 0 |
|
1,271 |
E
|
1271E
|
E. Common Number
| 2,100 |
binary search; combinatorics; dp; math
|
At first, let's define function \(f(x)\) as follows: $$$\( \begin{matrix} f(x) & = & \left\{ \begin{matrix} \frac{x}{2} & \mbox{if } x \text{ is even} \\ x - 1 & \mbox{otherwise } \end{matrix} \right. \end{matrix} \)\(We can see that if we choose some value \)v\( and will apply function \)f\( to it, then apply \)f\( to \)f(v)\(, and so on, we'll eventually get \)1\(. Let's write down all values we get in this process in a list and denote this list as \)path(v)\(. For example, \)path(1) = [1]\(, \)path(15) = [15, 14, 7, 6, 3, 2, 1]\(, \)path(32) = [32, 16, 8, 4, 2, 1]\(.Let's write all lists \)path(x)\( for every \)x\( from \)1\( to \)n\(. The question is next: what is the maximum value \)y\( such that \)y\( is contained in at least \)k\( different lists \)path(x)\(?Formally speaking, you need to find maximum \)y\( such that \)\left| \{ x ~|~ 1 \le x \le n, y \in path(x) \} \right| \ge k$$$.
|
The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 10^{18}\)).
|
Print the only integer β the maximum value that is contained in at least \(k\) paths.
|
In the first example, the answer is \(5\), since \(5\) occurs in \(path(5)\), \(path(10)\) and \(path(11)\).In the second example, the answer is \(4\), since \(4\) occurs in \(path(4)\), \(path(5)\), \(path(8)\), \(path(9)\), \(path(10)\) and \(path(11)\).In the third example \(n = k\), so the answer is \(1\), since \(1\) is the only number occuring in all paths for integers from \(1\) to \(20\).
|
Input: 11 3 | Output: 5
|
Hard
| 4 | 903 | 85 | 85 | 12 |
213 |
C
|
213C
|
C. Relay Race
| 2,000 |
dp
|
Furik and Rubik take part in a relay race. The race will be set up on a large square with the side of n meters. The given square is split into n Γ n cells (represented as unit squares), each cell has some number.At the beginning of the race Furik stands in a cell with coordinates (1, 1), and Rubik stands in a cell with coordinates (n, n). Right after the start Furik runs towards Rubik, besides, if Furik stands at a cell with coordinates (i, j), then he can move to cell (i + 1, j) or (i, j + 1). After Furik reaches Rubik, Rubik starts running from cell with coordinates (n, n) to cell with coordinates (1, 1). If Rubik stands in cell (i, j), then he can move to cell (i - 1, j) or (i, j - 1). Neither Furik, nor Rubik are allowed to go beyond the boundaries of the field; if a player goes beyond the boundaries, he will be disqualified. To win the race, Furik and Rubik must earn as many points as possible. The number of points is the sum of numbers from the cells Furik and Rubik visited. Each cell counts only once in the sum.Print the maximum number of points Furik and Rubik can earn on the relay race.
|
The first line contains a single integer (1 β€ n β€ 300). The next n lines contain n integers each: the j-th number on the i-th line ai, j ( - 1000 β€ ai, j β€ 1000) is the number written in the cell with coordinates (i, j).
|
On a single line print a single number β the answer to the problem.
|
Comments to the second sample: The profitable path for Furik is: (1, 1), (1, 2), (2, 2), and for Rubik: (2, 2), (2, 1), (1, 1). Comments to the third sample: The optimal path for Furik is: (1, 1), (1, 2), (1, 3), (2, 3), (3, 3), and for Rubik: (3, 3), (3, 2), (2, 2), (2, 1), (1, 1). The figure to the sample: Furik's path is marked with yellow, and Rubik's path is marked with pink.
|
Input: 15 | Output: 5
|
Hard
| 1 | 1,112 | 220 | 67 | 2 |
1,949 |
H
|
1949H
|
H. Division Avoidance
| 3,100 |
greedy; math
|
A newly discovered organism can be represented as a set of cells on an infinite grid. There is a coordinate system on the grid such that each cell has two integer coordinates \(x\) and \(y\). A cell with coordinates \(x=a\) and \(y=b\) will be denoted as \((a, b)\).Initially, the organism consists of a single cell \((0, 0)\). Then zero or more divisions can happen. In one division, a cell \((a, b)\) is removed and replaced by two cells \((a+1, b)\) and \((a, b+1)\).For example, after the first division, the organism always consists of two cells \((1, 0)\) and \((0, 1)\), and after the second division, it is either the three cells \((2, 0)\), \((1, 1)\) and \((0, 1)\), or the three cells \((1, 0)\), \((1, 1)\) and \((0, 2)\).A division of a cell \((a, b)\) can only happen if the cells \((a+1, b)\) and \((a, b+1)\) are not yet part of the organism. For example, the cell \((1, 0)\) cannot divide if the organism currently consists of the three cells \((1, 0)\), \((1, 1)\) and \((0, 2)\), since the cell \((1, 1)\) that would be one of the results of this division is already part of the organism.You are given a set of forbidden cells \({(c_i, d_i)}\). Is it possible for the organism to contain none of those cells after zero or more divisions?
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10\,000\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 10^6\)) β the number of forbidden cells.The next \(n\) lines contain two integers each. The \(i\)-th of such lines contains \(c_i\) and \(d_i\) (\(0 \le c_i, d_i \le 10^9\)) β the coordinates of the \(i\)-th forbidden cell. It is guaranteed that all forbidden cells are distinct.It is guaranteed that the sum of values of \(n\) over all test cases does not exceed \(10^6\).
|
For each test case, print \(\texttt{YES}\) if it is possible for the organism to contain no forbidden cells after zero or more divisions. Otherwise, print \(\texttt{NO}\).
|
In the first test case, dividing the following cells in the following order creates an organism without any forbidden cells: \((0, 0)\), \((1, 0)\), \((1, 1)\), \((0, 1)\), \((2, 1)\), \((2, 2)\), \((1, 2)\), \((1, 1)\). The following picture demonstrates how the organism changes during this process:In the second test case, you can see that, surprisingly, any organism always has at least one cell in the \(0 \le x, y \le 3\) square, no matter how many divisions we do.
|
Input: 240 01 00 11 1160 00 10 20 31 01 11 21 32 02 12 22 33 03 13 23 3 | Output: YES NO
|
Master
| 2 | 1,256 | 630 | 171 | 19 |
1,514 |
A
|
1514A
|
A. Perfectly Imperfect Array
| 800 |
math; number theory
|
Given an array \(a\) of length \(n\), tell us whether it has a non-empty subsequence such that the product of its elements is not a perfect square.A sequence \(b\) is a subsequence of an array \(a\) if \(b\) can be obtained from \(a\) by deleting some (possibly zero) elements.
|
The first line contains an 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 an integer \(n\) (\(1 \le n \le 100\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1\), \(a_2\), \(\ldots\), \(a_{n}\) (\(1 \le a_i \le 10^4\)) β the elements of the array \(a\).
|
If there's a subsequence of \(a\) whose product isn't a perfect square, print ""YES"". Otherwise, print ""NO"".
|
In the first example, the product of the whole array (\(20\)) isn't a perfect square.In the second example, all subsequences have a perfect square product.
|
Input: 2 3 1 5 4 2 100 10000 | Output: YES NO
|
Beginner
| 2 | 277 | 403 | 111 | 15 |
908 |
E
|
908E
|
E. New Year and Entity Enumeration
| 2,500 |
bitmasks; combinatorics; dp; math
|
You are given an integer m.Let M = 2m - 1.You are also given a set of n integers denoted as the set T. The integers will be provided in base 2 as n binary strings of length m.A set of integers S is called ""good"" if the following hold. If , then . If , then All elements of S are less than or equal to M. Here, and refer to the bitwise XOR and bitwise AND operators, respectively.Count the number of good sets S, modulo 109 + 7.
|
The first line will contain two integers m and n (1 β€ m β€ 1 000, 1 β€ n β€ min(2m, 50)).The next n lines will contain the elements of T. Each line will contain exactly m zeros and ones. Elements of T will be distinct.
|
Print a single integer, the number of good sets modulo 109 + 7.
|
An example of a valid set S is {00000, 00101, 00010, 00111, 11000, 11010, 11101, 11111}.
|
Input: 5 3110100010111000 | Output: 4
|
Expert
| 4 | 429 | 215 | 63 | 9 |
1,442 |
B
|
1442B
|
B. Identify the Operations
| 1,800 |
combinatorics; data structures; dsu; greedy; implementation
|
We start with a permutation \(a_1, a_2, \ldots, a_n\) and with an empty array \(b\). We apply the following operation \(k\) times.On the \(i\)-th iteration, we select an index \(t_i\) (\(1 \le t_i \le n-i+1\)), remove \(a_{t_i}\) from the array, and append one of the numbers \(a_{t_i-1}\) or \(a_{t_i+1}\) (if \(t_i-1\) or \(t_i+1\) are within the array bounds) to the right end of the array \(b\). Then we move elements \(a_{t_i+1}, \ldots, a_n\) to the left in order to fill in the empty space.You are given the initial permutation \(a_1, a_2, \ldots, a_n\) and the resulting array \(b_1, b_2, \ldots, b_k\). All elements of an array \(b\) are distinct. Calculate the number of possible sequences of indices \(t_1, t_2, \ldots, t_k\) modulo \(998\,244\,353\).
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 100\,000\)), denoting the number of test cases, followed by a description of the test cases.The first line of each test case contains two integers \(n, k\) (\(1 \le k < n \le 200\,000\)): sizes of arrays \(a\) and \(b\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)): elements of \(a\). All elements of \(a\) are distinct.The third line of each test case contains \(k\) integers \(b_1, b_2, \ldots, b_k\) (\(1 \le b_i \le n\)): elements of \(b\). All elements of \(b\) are distinct.The sum of all \(n\) among all test cases is guaranteed to not exceed \(200\,000\).
|
For each test case print one integer: the number of possible sequences modulo \(998\,244\,353\).
|
\(\require{cancel}\)Let's denote as \(a_1 a_2 \ldots \cancel{a_i} \underline{a_{i+1}} \ldots a_n \rightarrow a_1 a_2 \ldots a_{i-1} a_{i+1} \ldots a_{n-1}\) an operation over an element with index \(i\): removal of element \(a_i\) from array \(a\) and appending element \(a_{i+1}\) to array \(b\).In the first example test, the following two options can be used to produce the given array \(b\): \(1 2 \underline{3} \cancel{4} 5 \rightarrow 1 \underline{2} \cancel{3} 5 \rightarrow 1 \cancel{2} \underline{5} \rightarrow 1 2\); \((t_1, t_2, t_3) = (4, 3, 2)\); \(1 2 \underline{3} \cancel{4} 5 \rightarrow \cancel{1} \underline{2} 3 5 \rightarrow 2 \cancel{3} \underline{5} \rightarrow 1 5\); \((t_1, t_2, t_3) = (4, 1, 2)\). In the second example test, it is impossible to achieve the given array no matter the operations used. That's because, on the first application, we removed the element next to \(4\), namely number \(3\), which means that it couldn't be added to array \(b\) on the second step.In the third example test, there are four options to achieve the given array \(b\): \(1 4 \cancel{7} \underline{3} 6 2 5 \rightarrow 1 4 3 \cancel{6} \underline{2} 5 \rightarrow \cancel{1} \underline{4} 3 2 5 \rightarrow 4 3 \cancel{2} \underline{5} \rightarrow 4 3 5\); \(1 4 \cancel{7} \underline{3} 6 2 5 \rightarrow 1 4 3 \cancel{6} \underline{2} 5 \rightarrow 1 \underline{4} \cancel{3} 2 5 \rightarrow 1 4 \cancel{2} \underline{5} \rightarrow 1 4 5\); \(1 4 7 \underline{3} \cancel{6} 2 5 \rightarrow 1 4 7 \cancel{3} \underline{2} 5 \rightarrow \cancel{1} \underline{4} 7 2 5 \rightarrow 4 7 \cancel{2} \underline{5} \rightarrow 4 7 5\); \(1 4 7 \underline{3} \cancel{6} 2 5 \rightarrow 1 4 7 \cancel{3} \underline{2} 5 \rightarrow 1 \underline{4} \cancel{7} 2 5 \rightarrow 1 4 \cancel{2} \underline{5} \rightarrow 1 4 5\);
|
Input: 3 5 3 1 2 3 4 5 3 2 5 4 3 4 3 2 1 4 3 1 7 4 1 4 7 3 6 2 5 3 2 4 5 | Output: 2 0 4
|
Medium
| 5 | 762 | 720 | 96 | 14 |
1,276 |
A
|
1276A
|
A. As Simple as One and Two
| 1,400 |
dp; greedy
|
You are given a non-empty string \(s=s_1s_2\dots s_n\), which consists only of lowercase Latin letters. Polycarp does not like a string if it contains at least one string ""one"" or at least one string ""two"" (or both at the same time) as a substring. In other words, Polycarp does not like the string \(s\) if there is an integer \(j\) (\(1 \le j \le n-2\)), that \(s_{j}s_{j+1}s_{j+2}=\)""one"" or \(s_{j}s_{j+1}s_{j+2}=\)""two"".For example: Polycarp does not like strings ""oneee"", ""ontwow"", ""twone"" and ""oneonetwo"" (they all have at least one substring ""one"" or ""two""), Polycarp likes strings ""oonnee"", ""twwwo"" and ""twnoe"" (they have no substrings ""one"" and ""two""). Polycarp wants to select a certain set of indices (positions) and remove all letters on these positions. All removals are made at the same time.For example, if the string looks like \(s=\)""onetwone"", then if Polycarp selects two indices \(3\) and \(6\), then ""onetwone"" will be selected and the result is ""ontwne"".What is the minimum number of indices (positions) that Polycarp needs to select to make the string liked? What should these positions be?
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input. Next, the test cases are given.Each test case consists of one non-empty string \(s\). Its length does not exceed \(1.5\cdot10^5\). The string \(s\) consists only of lowercase Latin letters.It is guaranteed that the sum of lengths of all lines for all input data in the test does not exceed \(1.5\cdot10^6\).
|
Print an answer for each test case in the input in order of their appearance.The first line of each answer should contain \(r\) (\(0 \le r \le |s|\)) β the required minimum number of positions to be removed, where \(|s|\) is the length of the given line. The second line of each answer should contain \(r\) different integers β the indices themselves for removal in any order. Indices are numbered from left to right from \(1\) to the length of the string. If \(r=0\), then the second line can be skipped (or you can print empty). If there are several answers, print any of them.
|
In the first example, answers are: ""onetwone"", ""testme"" β Polycarp likes it, there is nothing to remove, ""oneoneone"", ""twotwo"". In the second example, answers are: ""onetwonetwooneooonetwooo"", ""two"", ""one"", ""twooooo"", ""ttttwo"", ""ttwwoo"" β Polycarp likes it, there is nothing to remove, ""ooone"", ""onnne"" β Polycarp likes it, there is nothing to remove, ""oneeeee"", ""oneeeeeeetwooooo"".
|
Input: 4 onetwone testme oneoneone twotwo | Output: 2 6 3 0 3 4 1 7 2 1 4
|
Easy
| 2 | 1,150 | 425 | 579 | 12 |
1,282 |
D
|
1282D
|
D. Enchanted Artifact
| 2,300 |
constructive algorithms; interactive; strings
|
This is an interactive problem.After completing the last level of the enchanted temple, you received a powerful artifact of the 255th level. Do not rush to celebrate, because this artifact has a powerful rune that can be destroyed with a single spell \(s\), which you are going to find. We define the spell as some non-empty string consisting only of the letters a and b.At any time, you can cast an arbitrary non-empty spell \(t\), and the rune on the artifact will begin to resist. Resistance of the rune is the edit distance between the strings that specify the casted spell \(t\) and the rune-destroying spell \(s\).Edit distance of two strings \(s\) and \(t\) is a value equal to the minimum number of one-character operations of replacing, inserting and deleting characters in \(s\) to get \(t\). For example, the distance between ababa and aaa is \(2\), the distance between aaa and aba is \(1\), the distance between bbaba and abb is \(3\). The edit distance is \(0\) if and only if the strings are equal.It is also worth considering that the artifact has a resistance limit β if you cast more than \(n + 2\) spells, where \(n\) is the length of spell \(s\), the rune will be blocked.Thus, it takes \(n + 2\) or fewer spells to destroy the rune that is on your artifact. Keep in mind that the required destructive spell \(s\) must also be counted among these \(n + 2\) spells.Note that the length \(n\) of the rune-destroying spell \(s\) is not known to you in advance. It is only known that its length \(n\) does not exceed \(300\).
|
Input: 2 2 1 2 3 0 | Output: aaa aaab abba bba abaaa aabba
|
Expert
| 3 | 1,541 | 0 | 0 | 12 |
|||
89 |
E
|
89E
|
E. Fire and Ice
| 2,900 |
greedy
|
The Fire Lord attacked the Frost Kingdom. He has already got to the Ice Fortress, where the Snow Queen dwells. He arranged his army on a segment n in length not far from the city walls. And only the frost magician Solomon can save the Frost Kingdom. The n-long segment is located at a distance equal exactly to 1 from the castle walls. It can be imaginarily divided into unit segments. On some of the unit segments fire demons are located β no more than one demon per position. Each demon is characterised by his strength - by some positive integer. We can regard the fire demons being idle.Initially Solomon is positioned on the fortress wall. He can perform the following actions several times in a row: ""L"" β Solomon shifts one unit to the left. This movement cannot be performed on the castle wall. ""R"" β Solomon shifts one unit to the left. This movement cannot be performed if there's no ice block to the right. ""A"" β If there's nothing to the right of Solomon, then Solomon creates an ice block that immediately freezes to the block that Solomon is currently standing on. If there already is an ice block, then Solomon destroys it. At that the ice blocks to the right of the destroyed one can remain but they are left unsupported. Those ice blocks fall down.Solomon spends exactly a second on each of these actions.As the result of Solomon's actions, ice blocks' segments fall down. When an ice block falls on a fire demon, the block evaporates and the demon's strength is reduced by 1. When the demons' strength is equal to 0, the fire demon vanishes. The picture below shows how it happens. The ice block that falls on the position with no demon, breaks into lots of tiny pieces and vanishes without hurting anybody. Help Solomon destroy all the Fire Lord's army in minimum time.
|
The first line contains an integer n (1 β€ n β€ 1000). The next line contains n numbers, the i-th of them represents the strength of the fire demon standing of the i-th position, an integer from 1 to 100. If there's no demon on the i-th position, then the i-th number equals to 0. It is guaranteed that the input data have at least one fire demon.
|
Print a string of minimum length, containing characters ""L"", ""R"" and ""A"" β the succession of actions leading to the required result.If there are several possible answers, print any of them.
|
Input: 31 0 1 | Output: ARARARALLLA
|
Master
| 1 | 1,794 | 345 | 195 | 0 |
|
1,976 |
F
|
1976F
|
F. Remove Bridges
| 2,800 |
data structures; dfs and similar; dp; greedy; sortings; trees
|
You are given a rooted tree, consisting of \(n\) vertices, numbered from \(1\) to \(n\). Vertex \(1\) is the root. Additionally, the root only has one child.You are asked to add exactly \(k\) edges to the tree (possibly, multiple edges and/or edges already existing in the tree).Recall that a bridge is such an edge that, after you remove it, the number of connected components in the graph increases. So, initially, all edges of the tree are bridges.After \(k\) edges are added, some original edges of the tree are still bridges and some are not anymore. You want to satisfy two conditions: for every bridge, all tree edges in the subtree of the lower vertex of that bridge should also be bridges; the number of bridges is as small as possible. Solve the task for all values of \(k\) from \(1\) to \(n - 1\) and output the smallest number of bridges.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) β the number of vertices of the tree.Each of the next \(n - 1\) lines contain two integers \(v\) and \(u\) (\(1 \le v, u \le n\)) β the description of the edges of the tree. It's guaranteed that the given edges form a valid tree.Additional constraint on the input: the root (vertex \(1\)) has exactly one child.The sum of \(n\) over all testcases doesn't exceed \(3 \cdot 10^5\).
|
For each testcase, print \(n - 1\) integers. For each \(k\) from \(1\) to \(n - 1\) print the smallest number of bridges that can be left after you add \(k\) edges to the tree.
|
Input: 421 2124 105 1212 113 69 61 612 711 62 1110 910 881 22 32 43 53 64 74 851 22 33 44 5 | Output: 0 7 3 1 0 0 0 0 0 0 0 0 4 1 0 0 0 0 0 0 0 0 0
|
Master
| 6 | 851 | 570 | 176 | 19 |
|
1,450 |
G
|
1450G
|
G. Communism
| 3,500 |
bitmasks; dp; trees
|
Please pay attention to the unusual memory limit in this problem.In a parallel universe, Satan is called ""Trygub"". For that reason, the letters of his namesake were deleted from the alphabet in ancient times.The government has \(n\) workers standing in a row and numbered with integers from \(1\) to \(n\) from left to right. Their job categories can be represented as a string \(s\) of length \(n\), where the character \(s_i\) represents the job category of the \(i\)-th worker.A new law will be approved to increase the equality between the workers. The government decided to make everyone have the same job category by performing the following operation any number of times (possibly zero).There is a fixed rational parameter \(k=\frac ab\) describing how easy it is to convince the public, and it will be used to determine the success of an operation.In an operation, the government first selects a job category \(x\) with at least one worker at the current moment. Suppose \(i_1,\ldots, i_m\) (\(i_1<\ldots<i_m\)) are the positions of all the workers with job category \(x\). If \(k\cdot (i_m-i_1+1)\le m\), the government is able to choose any job category \(y\) with at least one worker at the current moment and change the job category of all workers with job category \(x\) to job category \(y\).If it is possible to make all workers have job category \(x\), we say that \(x\) is obtainable. Can you tell the government the set of obtainable job categories?
|
The first line contains three integers \(n, a, b\) (\(1 \le n \le 5000\), \(1\le a\le b\le 10^5\)) β the number of workers and the numerator and denominator of the parameter \(k\), respectively.The second line contains a string \(s\) of length \(n\), consisting of lowercase English characters β the job categories of each worker. The characters 't', 'r', 'y', 'g', 'u', and 'b' do not appear in the string \(s\).
|
Print an integer \(c\) equal to the number of obtainable job categories followed by \(c\) space-separated characters β the obtainable job categories sorted in the lexicographical order.
|
The first operation must select the job category 'i' because all other job categories cannot satisfy the condition, therefore 'i' is not obtainable.Below is showed how to obtain 'c', 'm', and 'o'. The square brackets denote the segment containing all workers of the selected category, the red color denotes this category and the blue color denotes the new category after the change. Get 'c': (\(\texttt{com}\color{red}{\texttt{[i]}}\texttt{com} \rightarrow \texttt{com}\color{#1E90FF}{\texttt{[o]}}\texttt{com}\)) (\(\texttt{c}\color{red}{\texttt{[o}}\texttt{m}\color{red}{\texttt{o}}\texttt{c}\color{red}{\texttt{o]}}\texttt{m} \rightarrow \texttt{c}\color{#1E90FF}{\texttt{[m}}\texttt{m}\color{#1E90FF}{\texttt{m}}\texttt{c}\color{#1E90FF}{\texttt{m]}}\texttt{m}\)) (\(\texttt{c}\color{red}{\texttt{[mmm}}\texttt{c}\color{red}{\texttt{mm]}} \rightarrow \texttt{c}\color{#1E90FF}{\texttt{[ccc}}\texttt{c}\color{#1E90FF}{\texttt{cc]}}\)) Get 'm': (\(\texttt{com}\color{red}{\texttt{[i]}}\texttt{com} \rightarrow \texttt{com}\color{#1E90FF}{\texttt{[o]}}\texttt{com}\)) (\(\texttt{c}\color{red}{\texttt{[o}}\texttt{m}\color{red}{\texttt{o}}\texttt{c}\color{red}{\texttt{o]}}\texttt{m} \rightarrow \texttt{c}\color{#1E90FF}{\texttt{[c}}\texttt{m}\color{#1E90FF}{\texttt{c}}\texttt{c}\color{#1E90FF}{\texttt{c]}}\texttt{m}\)) (\(\color{red}{\texttt{[cc}}\texttt{m}\color{red}{\texttt{ccc]}}\texttt{m} \rightarrow \color{#1E90FF}{\texttt{[mm}}\texttt{m}\color{#1E90FF}{\texttt{mmm]}}\texttt{m}\)) Get 'o': (\(\texttt{com}\color{red}{\texttt{[i]}}\texttt{com} \rightarrow \texttt{com}\color{#1E90FF}{\texttt{[c]}}\texttt{com}\)) (\(\color{red}{\texttt{[c}}\texttt{om}\color{red}{\texttt{cc]}}\texttt{om} \rightarrow \color{#1E90FF}{\texttt{[m}}\texttt{om}\color{#1E90FF}{\texttt{mm]}}\texttt{om}\)) (\(\color{red}{\texttt{[m}}\texttt{o}\color{red}{\texttt{mmm}}\texttt{o}\color{red}{\texttt{m]}} \rightarrow \color{#1E90FF}{\texttt{[o}}\texttt{o}\color{#1E90FF}{\texttt{ooo}}\texttt{o}\color{#1E90FF}{\texttt{o]}}\))
|
Input: 7 1 2 comicom | Output: 3 c m o
|
Master
| 3 | 1,469 | 413 | 185 | 14 |
1,055 |
A
|
1055A
|
A. Metro
| 900 |
graphs
|
Alice has a birthday today, so she invited home her best friend Bob. Now Bob needs to find a way to commute to the Alice's home.In the city in which Alice and Bob live, the first metro line is being built. This metro line contains \(n\) stations numbered from \(1\) to \(n\). Bob lives near the station with number \(1\), while Alice lives near the station with number \(s\). The metro line has two tracks. Trains on the first track go from the station \(1\) to the station \(n\) and trains on the second track go in reverse direction. Just after the train arrives to the end of its track, it goes to the depot immediately, so it is impossible to travel on it after that.Some stations are not yet open at all and some are only partially open β for each station and for each track it is known whether the station is closed for that track or not. If a station is closed for some track, all trains going in this track's direction pass the station without stopping on it.When the Bob got the information on opened and closed stations, he found that traveling by metro may be unexpectedly complicated. Help Bob determine whether he can travel to the Alice's home by metro or he should search for some other transport.
|
The first line contains two integers \(n\) and \(s\) (\(2 \le s \le n \le 1000\)) β the number of stations in the metro and the number of the station where Alice's home is located. Bob lives at station \(1\).Next lines describe information about closed and open stations.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(a_i = 0\) or \(a_i = 1\)). If \(a_i = 1\), then the \(i\)-th station is open on the first track (that is, in the direction of increasing station numbers). Otherwise the station is closed on the first track.The third line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(b_i = 0\) or \(b_i = 1\)). If \(b_i = 1\), then the \(i\)-th station is open on the second track (that is, in the direction of decreasing station numbers). Otherwise the station is closed on the second track.
|
Print ""YES"" (quotes for clarity) if Bob will be able to commute to the Alice's home by metro and ""NO"" (quotes for clarity) otherwise.You can print each letter in any case (upper or lower).
|
In the first example, all stations are opened, so Bob can simply travel to the station with number \(3\).In the second example, Bob should travel to the station \(5\) first, switch to the second track and travel to the station \(4\) then.In the third example, Bob simply can't enter the train going in the direction of Alice's home.
|
Input: 5 31 1 1 1 11 1 1 1 1 | Output: YES
|
Beginner
| 1 | 1,212 | 822 | 192 | 10 |
603 |
A
|
603A
|
A. Alternative Thinking
| 1,600 |
dp; greedy; math
|
Kevin has just recevied his disappointing results on the USA Identification of Cows Olympiad (USAICO) in the form of a binary string of length n. Each character of Kevin's string represents Kevin's score on one of the n questions of the olympiadβ'1' for a correctly identified cow and '0' otherwise.However, all is not lost. Kevin is a big proponent of alternative thinking and believes that his score, instead of being the sum of his points, should be the length of the longest alternating subsequence of his string. Here, we define an alternating subsequence of a string as a not-necessarily contiguous subsequence where no two consecutive elements are equal. For example, {0, 1, 0, 1}, {1, 0, 1}, and {1, 0, 1, 0} are alternating sequences, while {1, 0, 0} and {0, 1, 0, 1, 1} are not.Kevin, being the sneaky little puffball that he is, is willing to hack into the USAICO databases to improve his score. In order to be subtle, he decides that he will flip exactly one substringβthat is, take a contiguous non-empty substring of his score and change all '0's in that substring to '1's and vice versa. After such an operation, Kevin wants to know the length of the longest possible alternating subsequence that his string could have.
|
The first line contains the number of questions on the olympiad n (1 β€ n β€ 100 000).The following line contains a binary string of length n representing Kevin's results on the USAICO.
|
Output a single integer, the length of the longest possible alternating subsequence that Kevin can create in his string after flipping a single substring.
|
In the first sample, Kevin can flip the bolded substring '10000011' and turn his string into '10011011', which has an alternating subsequence of length 5: '10011011'.In the second sample, Kevin can flip the entire string and still have the same score.
|
Input: 810000011 | Output: 5
|
Medium
| 3 | 1,234 | 183 | 154 | 6 |
290 |
B
|
290B
|
B. QR code
| 1,500 |
*special; implementation
|
The input contains two integers a1, a2 (0 β€ ai β€ 32), separated by a single space.
|
Output a single integer.
|
Input: 1 1 | Output: 0
|
Medium
| 2 | 0 | 82 | 24 | 2 |
||
2,081 |
G1
|
2081G1
|
G1. Hard Formula
| 3,100 |
math; number theory
|
This is the easy version of the problem. The difference between the versions is that in this version, the limits on \(n\) and the time limit are smaller. You can hack only if you solved all versions of this problem. You are given an integer \(n\), and you need to compute \((\sum_{k=1}^n k\bmod\varphi(k))\bmod 2^{32}\), where \(\varphi(k)\) equals the number of positive integers no greater than \(k\) that are coprime with \(k\).
|
The only line contains a single integer \(n\) (\(1 \le n \le 10^{10})\).
|
Print a single integer, representing \((\sum_{k=1}^n k\bmod\varphi(k))\bmod 2^{32}\).
|
Input: 5 | Output: 2
|
Master
| 2 | 431 | 72 | 85 | 20 |
|
1,906 |
J
|
1906J
|
J. Count BFS Graph
| 2,100 |
combinatorics; dp
|
You are currently researching a graph traversal algorithm called the Breadth First Search (BFS). Suppose you have an input graph of \(N\) nodes (numbered from \(1\) to \(N\)). The graph is represented by an adjacency matrix \(M\), for which node \(u\) can traverse to node \(v\) if \(M_{u, v}\) is \(1\), otherwise it is \(0\). Your algorithm will output the order the nodes are visited in the BFS. The pseudocode of the algorithm is presented as follows. BFS(M[1..N][1..N]): let A be an empty array let Q be an empty queue append 1 to A push 1 to Q while Q is not empty: pop the front element of Q into u for v = 1 to N: if M[u][v] == 1 and v is not in A: append v to A push v to Q return ADuring your research, you are interested in the following problem. Given an array \(A\) such that \(A\) is a permutation of \(1\) to \(N\) and \(A_1 = 1\). How many simple undirected graph with \(N\) nodes and adjacency matrix \(M\) such that \(\text{BFS}(M) = A\)? Since the answer can be very large, calculate the answer modulo \(998\,244\,353\).A simple graph has no self-loop (\(M_{i, i} = 0\) for \(1 \leq i \leq N\)) and there is at most one edge that connects a pair of nodes. In an undirected graph, if node \(u\) is adjacent to node \(v\), then node \(v\) is also adjacent to node \(u\); formally, \(M_{u, v} = M_{v, u}\) for \(1 \leq u < v \leq N\).Two graphs are considered different if there is an edge that exists in one graph but not the other. In other words, two graphs are considered different if their adjacency matrices are different.
|
The first line consists of an integer \(N\) (\(2 \leq N \leq 5000\)).The second line consists of \(N\) integers \(A_i\). The array \(A\) is a permutation of \(1\) to \(N\) and \(A_1 = 1\).
|
Output an integer representing the number of simple undirected graphs with \(N\) nodes and adjacency matrix \(M\) such that \(\text{BFS}(M) = A\). Since the answer can be very large, output the answer modulo \(998\,244\,353\).
|
Explanation for the sample input/output #1The following illustration shows all graphs that satisfy the requirements. Explanation for the sample input/output #2The only graph that satisfies the requirements is a graph with two edges: one that connects nodes \(1\) and \(3\), and another one that connects nodes \(3\) and \(2\).
|
Input: 3 1 2 3 | Output: 3
|
Hard
| 2 | 1,544 | 188 | 226 | 19 |
452 |
C
|
452C
|
C. Magic Trick
| 2,100 |
combinatorics; math; probabilities
|
Alex enjoys performing magic tricks. He has a trick that requires a deck of n cards. He has m identical decks of n different cards each, which have been mixed together. When Alex wishes to perform the trick, he grabs n cards at random and performs the trick with those. The resulting deck looks like a normal deck, but may have duplicates of some cards.The trick itself is performed as follows: first Alex allows you to choose a random card from the deck. You memorize the card and put it back in the deck. Then Alex shuffles the deck, and pulls out a card. If the card matches the one you memorized, the trick is successful.You don't think Alex is a very good magician, and that he just pulls a card randomly from the deck. Determine the probability of the trick being successful if this is the case.
|
First line of the input consists of two integers n and m (1 β€ n, m β€ 1000), separated by space β number of cards in each deck, and number of decks.
|
On the only line of the output print one floating point number β probability of Alex successfully performing the trick. Relative or absolute error of your answer should not be higher than 10 - 6.
|
In the first sample, with probability Alex will perform the trick with two cards with the same value from two different decks. In this case the trick is guaranteed to succeed.With the remaining probability he took two different cards, and the probability of pulling off the trick is .The resulting probability is
|
Input: 2 2 | Output: 0.6666666666666666
|
Hard
| 3 | 801 | 147 | 195 | 4 |
175 |
A
|
175A
|
A. Robot Bicorn Attack
| 1,400 |
brute force; implementation
|
Vasya plays Robot Bicorn Attack.The game consists of three rounds. For each one a non-negative integer amount of points is given. The result of the game is the sum of obtained points. Vasya has already played three rounds and wrote obtained points one by one (without leading zeros) into the string s. Vasya decided to brag about his achievement to the friends. However, he has forgotten how many points he got for each round. The only thing he remembers is the string s.Help Vasya to find out what is the maximum amount of points he could get. Take into account that Vasya played Robot Bicorn Attack for the first time, so he could not get more than 1000000 (106) points for one round.
|
The only line of input contains non-empty string s obtained by Vasya. The string consists of digits only. The string length does not exceed 30 characters.
|
Print the only number β the maximum amount of points Vasya could get. If Vasya is wrong and the string could not be obtained according to the rules then output number -1.
|
In the first example the string must be split into numbers 1, 2 and 34.In the second example the string must be split into numbers 90, 0 and 0. In the third example the string is incorrect, because after splitting the string into 3 numbers number 00 or 09 will be obtained, but numbers cannot have leading zeroes.
|
Input: 1234 | Output: 37
|
Easy
| 2 | 686 | 154 | 170 | 1 |
903 |
D
|
903D
|
D. Almost Difference
| 2,200 |
data structures; math
|
Let's denote a function You are given an array a consisting of n integers. You have to calculate the sum of d(ai, aj) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
|
The first line contains one integer n (1 β€ n β€ 200000) β the number of elements in a.The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 109) β elements of the array.
|
Print one integer β the sum of d(ai, aj) over all pairs (i, j) such that 1 β€ i β€ j β€ n.
|
In the first example: d(a1, a2) = 0; d(a1, a3) = 2; d(a1, a4) = 0; d(a1, a5) = 2; d(a2, a3) = 0; d(a2, a4) = 0; d(a2, a5) = 0; d(a3, a4) = - 2; d(a3, a5) = 0; d(a4, a5) = 2.
|
Input: 51 2 3 1 3 | Output: 4
|
Hard
| 2 | 164 | 176 | 87 | 9 |
660 |
B
|
660B
|
B. Seating On Bus
| 1,000 |
implementation
|
Consider 2n rows of the seats in a bus. n rows of the seats on the left and n rows of the seats on the right. Each row can be filled by two people. So the total capacity of the bus is 4n.Consider that m (m β€ 4n) people occupy the seats in the bus. The passengers entering the bus are numbered from 1 to m (in the order of their entering the bus). The pattern of the seat occupation is as below:1-st row left window seat, 1-st row right window seat, 2-nd row left window seat, 2-nd row right window seat, ... , n-th row left window seat, n-th row right window seat.After occupying all the window seats (for m > 2n) the non-window seats are occupied:1-st row left non-window seat, 1-st row right non-window seat, ... , n-th row left non-window seat, n-th row right non-window seat.All the passengers go to a single final destination. In the final destination, the passengers get off in the given order.1-st row left non-window seat, 1-st row left window seat, 1-st row right non-window seat, 1-st row right window seat, ... , n-th row left non-window seat, n-th row left window seat, n-th row right non-window seat, n-th row right window seat. The seating for n = 9 and m = 36. You are given the values n and m. Output m numbers from 1 to m, the order in which the passengers will get off the bus.
|
The only line contains two integers, n and m (1 β€ n β€ 100, 1 β€ m β€ 4n) β the number of pairs of rows and the number of passengers.
|
Print m distinct integers from 1 to m β the order in which the passengers will get off the bus.
|
Input: 2 7 | Output: 5 1 6 2 7 3 4
|
Beginner
| 1 | 1,295 | 130 | 95 | 6 |
|
30 |
A
|
30A
|
A. Accounting
| 1,400 |
brute force; math
|
A long time ago in some far country lived king Copa. After the recent king's reform, he got so large powers that started to keep the books by himself.The total income A of his kingdom during 0-th year is known, as well as the total income B during n-th year (these numbers can be negative β it means that there was a loss in the correspondent year). King wants to show financial stability. To do this, he needs to find common coefficient X β the coefficient of income growth during one year. This coefficient should satisfy the equation:AΒ·Xn = B.Surely, the king is not going to do this job by himself, and demands you to find such number X.It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient X must be integers. The number X may be zero or negative.
|
The input contains three integers A, B, n (|A|, |B| β€ 1000, 1 β€ n β€ 10).
|
Output the required integer coefficient X, or Β«No solutionΒ», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them.
|
Input: 2 18 2 | Output: 3
|
Easy
| 2 | 842 | 72 | 179 | 0 |
|
1,519 |
D
|
1519D
|
D. Maximum Sum of Products
| 1,600 |
brute force; dp; implementation; math; two pointers
|
You are given two integer arrays \(a\) and \(b\) of length \(n\).You can reverse at most one subarray (continuous subsegment) of the array \(a\). Your task is to reverse such a subarray that the sum \(\sum\limits_{i=1}^n a_i \cdot b_i\) is maximized.
|
The first line contains one integer \(n\) (\(1 \le n \le 5000\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^7\)).The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le 10^7\)).
|
Print single integer β maximum possible sum after reversing at most one subarray (continuous subsegment) of \(a\).
|
In the first example, you can reverse the subarray \([4, 5]\). Then \(a = [2, 3, 2, 3, 1]\) and \(2 \cdot 1 + 3 \cdot 3 + 2 \cdot 2 + 3 \cdot 4 + 1 \cdot 2 = 29\).In the second example, you don't need to use the reverse operation. \(13 \cdot 2 + 37 \cdot 4 = 174\).In the third example, you can reverse the subarray \([3, 5]\). Then \(a = [1, 8, 3, 6, 7, 6]\) and \(1 \cdot 5 + 8 \cdot 9 + 3 \cdot 6 + 6 \cdot 8 + 7 \cdot 8 + 6 \cdot 6 = 235\).
|
Input: 5 2 3 2 1 3 1 3 2 4 2 | Output: 29
|
Medium
| 5 | 250 | 244 | 114 | 15 |
696 |
E
|
696E
|
E. ...Wait for it...
| 3,000 |
data structures; dsu; trees
|
Barney is searching for his dream girl. He lives in NYC. NYC has n junctions numbered from 1 to n and n - 1 roads connecting them. We will consider the NYC as a rooted tree with root being junction 1. m girls live in NYC, i-th of them lives along junction ci and her weight initially equals i pounds. Barney consider a girl x to be better than a girl y if and only if: girl x has weight strictly less than girl y or girl x and girl y have equal weights and index of girl x living junction index is strictly less than girl y living junction index, i.e. cx < cy. Thus for any two girls one of them is always better than another one.For the next q days, one event happens each day. There are two types of events: Barney goes from junction v to junction u. As a result he picks at most k best girls he still have not invited from junctions on his way and invites them to his house to test if one of them is his dream girl. If there are less than k not invited girls on his path, he invites all of them. Girls living along junctions in subtree of junction v (including v itself) put on some weight. As result, their weights increase by k pounds. Your task is for each event of first type tell Barney the indices of girls he will invite to his home in this event.
|
The first line of input contains three integers n, m and q (1 β€ n, m, q β€ 105) β the number of junctions in NYC, the number of girls living in NYC and the number of events respectively.The next n - 1 lines describes the roads. Each line contains two integers v and u (1 β€ v, u β€ n, v β u) meaning that there is a road connecting junctions v and u .The next line contains m integers c1, c2, ..., cm (1 β€ ci β€ n) β the girl's living junctions.The next q lines describe the events in chronological order. Each line starts with an integer t (1 β€ t β€ 2) β type of the event .If t = 1 then the line describes event of first type three integers v, u and k (1 β€ v, u, k β€ n) follow β the endpoints of Barney's path and the number of girls that he will invite at most.Otherwise the line describes event of second type and two integers v and k (1 β€ v β€ n, 1 β€ k β€ 109) follow β the root of the subtree and value by which all the girls' weights in the subtree should increase.
|
For each event of the first type, print number t and then t integers g1, g2, ..., gt in one line, meaning that in this event Barney will invite t girls whose indices are g1, ..., gt in the order from the best to the worst according to Barney's considerations.
|
For the first sample case: Description of events: Weights of girls in subtree of junction 4 increase by 3. These girls have IDs: 1, 3, 5, 4, 7. Barney goes from junction 2 to 1. Girls on his way have IDs 1, 2, 3, 5, 6, 7 with weights 4, 2, 6, 8, 6, 10 respectively. So, he invites girls 2 and 1. Barney goes from junction 4 to junction 2. Girls on his way has IDs 3, 5, 7 with weights 6, 8, 10 respectively. So he invites girl 3. Weight of girls in subtree of junction 2 increase by 10. There are no not invited girls, so nothing happens. Weight of girls in subtree of junction 1 increase by 10. These girls (all girls left) have IDs: 4, 5, 6, 7. Barney goes from junction 2 to junction 4. Girls on his way has IDs 5, 7 with weights 18, 20 respectively. So he invites girl 5. Barney goes from junction 2 to junction 3. There is no girl on his way. Weight of girls in subtree of junction 5 increase by 2. The only girl there is girl with ID 4. Weight of girls in subtree of junction 4 increase by 9. These girls have IDs: 4, 6, 7. Barney goes from junction 3 to junction 5. Only girl on his way is girl with ID 4. Barney goes from junction 1 to junction 2. Girls on his way has IDs 6, 7 with weights 16, 29 respectively.
|
Input: 5 7 113 52 34 31 44 1 4 5 4 1 42 4 31 2 1 21 4 2 12 2 102 1 101 2 4 11 2 3 42 5 22 4 91 3 5 21 1 2 3 | Output: 2 2 1 1 3 1 5 0 1 4 2 6 7
|
Master
| 3 | 1,257 | 965 | 259 | 6 |
1,263 |
C
|
1263C
|
C. Everyone is a Winner!
| 1,400 |
binary search; math; meet-in-the-middle; number theory
|
On the well-known testing system MathForces, a draw of \(n\) rating units is arranged. The rating will be distributed according to the following algorithm: if \(k\) participants take part in this event, then the \(n\) rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remain β it is not given to any of the participants.For example, if \(n = 5\) and \(k = 3\), then each participant will recieve an \(1\) rating unit, and also \(2\) rating units will remain unused. If \(n = 5\), and \(k = 6\), then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if \(n=5\), then the answer is equal to the sequence \(0, 1, 2, 5\). Each of the sequence values (and only them) can be obtained as \(\lfloor n/k \rfloor\) for some positive integer \(k\) (where \(\lfloor x \rfloor\) is the value of \(x\) rounded down): \(0 = \lfloor 5/7 \rfloor\), \(1 = \lfloor 5/5 \rfloor\), \(2 = \lfloor 5/2 \rfloor\), \(5 = \lfloor 5/1 \rfloor\).Write a program that, for a given \(n\), finds a sequence of all possible rating increments.
|
The first line contains integer number \(t\) (\(1 \le t \le 10\)) β the number of test cases in the input. Then \(t\) test cases follow.Each line contains an integer \(n\) (\(1 \le n \le 10^9\)) β the total number of the rating units being drawn.
|
Output the answers for each of \(t\) test cases. Each answer should be contained in two lines.In the first line print a single integer \(m\) β the number of different rating increment values that Vasya can get.In the following line print \(m\) integers in ascending order β the values of possible rating increments.
|
Input: 4 5 11 1 3 | Output: 4 0 1 2 5 6 0 1 2 3 5 11 2 0 1 3 0 1 3
|
Easy
| 4 | 1,369 | 246 | 315 | 12 |
|
1,356 |
A1
|
1356A1
|
A1. Distinguish I from X
| 0 |
*special
|
You are given an operation that implements a single-qubit unitary transformation: either the identity (I gate) or the X gate. The operation will have Adjoint and Controlled variants defined.Your task is to perform necessary operations and measurements to figure out which unitary it was and to return 0 if it was the I gate or 1 if it was the X gate. You are allowed to apply the given operation and its adjoint/controlled variants exactly once.You have to implement an operation which takes a single-qubit operation as an input and returns an integer. Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Intrinsic; operation Solve (unitary : (Qubit => Unit is Adj+Ctl)) : Int { // your code here }}
|
Beginner
| 1 | 737 | 0 | 0 | 13 |
||||
501 |
C
|
501C
|
C. Misha and Forest
| 1,500 |
constructive algorithms; data structures; greedy; sortings; trees
|
Let's define a forest as a non-directed acyclic graph (also without loops and parallel edges). One day Misha played with the forest consisting of n vertices. For each vertex v from 0 to n - 1 he wrote down two integers, degreev and sv, were the first integer is the number of vertices adjacent to vertex v, and the second integer is the XOR sum of the numbers of vertices adjacent to v (if there were no adjacent vertices, he wrote down 0). Next day Misha couldn't remember what graph he initially had. Misha has values degreev and sv left, though. Help him find the number of edges and the edges of the initial graph. It is guaranteed that there exists a forest that corresponds to the numbers written by Misha.
|
The first line contains integer n (1 β€ n β€ 216), the number of vertices in the graph.The i-th of the next lines contains numbers degreei and si (0 β€ degreei β€ n - 1, 0 β€ si < 216), separated by a space.
|
In the first line print number m, the number of edges of the graph.Next print m lines, each containing two distinct numbers, a and b (0 β€ a β€ n - 1, 0 β€ b β€ n - 1), corresponding to edge (a, b).Edges can be printed in any order; vertices of the edge can also be printed in any order.
|
The XOR sum of numbers is the result of bitwise adding numbers modulo 2. This operation exists in many modern programming languages. For example, in languages C++, Java and Python it is represented as ""^"", and in Pascal β as ""xor"".
|
Input: 32 31 01 0 | Output: 21 02 0
|
Medium
| 5 | 712 | 202 | 283 | 5 |
2,038 |
J
|
2038J
|
J. Waiting for...
| 800 |
greedy; implementation
|
Monocarp is waiting for a bus at the bus stop. Unfortunately, there are many people who want to ride a bus too.You are given a list of events of two types: B \(b_i\) β a bus with \(b_i\) free seats arrives at the stop; P \(p_i\) β \(p_i\) people arrive at the stop. These events are listed in a chronological order. When a bus arrives, the following happens. All people at the bus stop (except for Monocarp) try to enter the bus. If there are enough free seats for all of them, then they all enter the bus. Otherwise, some people remain at the bus stop (the number of people who enter the bus is equal to the number of free seats).If there is still at least one free seat after all people (except for Monocarp) enter the bus, then Monocarp can decide to enter this bus as well (but he might choose to wait for another bus). For each bus, you have to determine if it is possible for Monocarp to take that bus.
|
The first line contains one integer \(n\) \((1 \le n \le 10^3)\) β the number of events.Then, \(n\) lines follow. The \(i\)-th of them contains the description of the \(i\)-th event in one of the two following formats: B \(b_i\) (\(1 \le b_i \le 10^6\)) β a bus with \(b_i\) free seats arrives at the stop; P \(p_i\) (\(1 \le p_i \le 10^6\)) β \(p_i\) people arrive at the stop. Additional constraint on the input: there is at least one event of type B.
|
For each event of type B, print YES if it is possible for Monocarp to take the corresponding bus, or NO otherwise (case-insensitive).
|
Input: 10P 2P 5B 8P 14B 5B 9B 3P 2B 1B 2 | Output: YES NO NO YES NO YES
|
Beginner
| 2 | 908 | 453 | 133 | 20 |
|
550 |
C
|
550C
|
C. Divisibility by Eight
| 1,500 |
brute force; dp; math
|
You are given a non-negative integer n, its decimal representation consists of at most 100 digits and doesn't contain leading zeroes.Your task is to determine if it is possible in this case to remove some of the digits (possibly not remove any digit at all) so that the result contains at least one digit, forms a non-negative integer, doesn't have leading zeroes and is divisible by 8. After the removing, it is forbidden to rearrange the digits.If a solution exists, you should print it.
|
The single line of the input contains a non-negative integer n. The representation of number n doesn't contain any leading zeroes and its length doesn't exceed 100 digits.
|
Print ""NO"" (without quotes), if there is no such way to remove some digits from number n. Otherwise, print ""YES"" in the first line and the resulting number after removing digits from number n in the second line. The printed number must be divisible by 8.If there are multiple possible answers, you may print any of them.
|
Input: 3454 | Output: YES344
|
Medium
| 3 | 489 | 171 | 324 | 5 |
|
1,096 |
G
|
1096G
|
G. Lucky Tickets
| 2,400 |
divide and conquer; dp; fft
|
All bus tickets in Berland have their numbers. A number consists of \(n\) digits (\(n\) is even). Only \(k\) decimal digits \(d_1, d_2, \dots, d_k\) can be used to form ticket numbers. If \(0\) is among these digits, then numbers may have leading zeroes. For example, if \(n = 4\) and only digits \(0\) and \(4\) can be used, then \(0000\), \(4004\), \(4440\) are valid ticket numbers, and \(0002\), \(00\), \(44443\) are not.A ticket is lucky if the sum of first \(n / 2\) digits is equal to the sum of remaining \(n / 2\) digits. Calculate the number of different lucky tickets in Berland. Since the answer may be big, print it modulo \(998244353\).
|
The first line contains two integers \(n\) and \(k\) \((2 \le n \le 2 \cdot 10^5, 1 \le k \le 10)\) β the number of digits in each ticket number, and the number of different decimal digits that may be used. \(n\) is even.The second line contains a sequence of pairwise distinct integers \(d_1, d_2, \dots, d_k\) \((0 \le d_i \le 9)\) β the digits that may be used in ticket numbers. The digits are given in arbitrary order.
|
Print the number of lucky ticket numbers, taken modulo \(998244353\).
|
In the first example there are \(6\) lucky ticket numbers: \(1111\), \(1818\), \(1881\), \(8118\), \(8181\) and \(8888\).There is only one ticket number in the second example, it consists of \(20\) digits \(6\). This ticket number is lucky, so the answer is \(1\).
|
Input: 4 21 8 | Output: 6
|
Expert
| 3 | 651 | 423 | 69 | 10 |
335 |
B
|
335B
|
B. Palindrome
| 1,900 |
constructive algorithms; dp
|
Given a string s, determine if it contains any palindrome of length exactly 100 as a subsequence. If it has any, print any one of them. If it doesn't have any, print a palindrome that is a subsequence of s and is as long as possible.
|
The only line of the input contains one string s of length n (1 β€ n β€ 5Β·104) containing only lowercase English letters.
|
If s contains a palindrome of length exactly 100 as a subsequence, print any palindrome of length 100 which is a subsequence of s. If s doesn't contain any palindromes of length exactly 100, print a palindrome that is a subsequence of s and is as long as possible.If there exists multiple answers, you are allowed to print any of them.
|
A subsequence of a string is a string that can be derived from it by deleting some characters without changing the order of the remaining characters. A palindrome is a string that reads the same forward or backward.
|
Input: bbbabcbbb | Output: bbbcbbb
|
Hard
| 2 | 233 | 119 | 335 | 3 |
126 |
E
|
126E
|
E. Pills
| 2,900 |
brute force; flows
|
Doctor prescribed medicine to his patient. The medicine is represented by pills. Each pill consists of a shell and healing powder. The shell consists of two halves; each half has one of four colors β blue, red, white or yellow.The doctor wants to put 28 pills in a rectangular box 7 Γ 8 in size. Besides, each pill occupies exactly two neighboring cells and any cell contains exactly one half of a pill. Thus, the result is a four colored picture 7 Γ 8 in size.The doctor thinks that a patient will recover sooner if the picture made by the pills will be special. Unfortunately, putting the pills in the box so as to get the required picture is not a very easy task. That's why doctor asks you to help. Doctor has some amount of pills of each of 10 painting types. They all contain the same medicine, that's why it doesn't matter which 28 of them will be stored inside the box.Place the pills in the box so that the required picture was formed. If it is impossible to place the pills in the required manner, then place them so that the number of matching colors in all 56 cells in the final arrangement and the doctor's picture were maximum.
|
First 7 lines contain the doctor's picture. Each line contains 8 characters, each character can be ""B"", ""R"", ""W"" and ""Y"" that stands for blue, red, white and yellow colors correspondingly.Next four lines contain 10 numbers that stand for, correspondingly, the number of pills painted:""BY"" ""BW"" ""BR"" ""BB""""RY"" ""RW"" ""RR""""WY"" ""WW""""YY""Those numbers lie within range from 0 to 28 inclusively. It is guaranteed that the total number of pills in no less than 28.
|
Print on the first line the maximal number cells for which the colors match.Then print 13 lines each containing 15 characters β the pills' position in the optimal arrangement. The intersections of odd lines and odd columns should contain characters ""B"", ""R"", ""W"" and ""Y"". All other positions should contain characters ""."", ""-"" and ""|"". Use ""-"" and ""|"" to show which halves belong to one pill. See the samples for more clarification.If there are several possible solutions, print any of them.
|
Input: WWWBBWWWWWWBBWWWYYWBBWWWYYWBBWRRYYWBBWRRYYWBBWRRYYWBBWRR0 0 0 80 1 51 105 | Output: 53W.W.W.B.B.W.W.W|.|.|.|.|.|.|.|W.W.W.B.B.W.W.W...............Y.Y.W.B.B.W.W-W|.|.|.|.|.|....Y.Y.W.B.B.W.R.R............|.|Y.Y.W.B.B.R.R.R|.|.|.|.|.|....Y.Y.W.B.B.W.R.R............|.|Y-Y.B-B.B-B.R.R
|
Master
| 2 | 1,141 | 482 | 509 | 1 |
|
1,737 |
A
|
1737A
|
A. Ela Sorting Books
| 900 |
greedy; implementation; strings
|
Ela loves reading a lot, just like her new co-workers in DTL! On her first day after becoming an engineer in DTL, she is challenged by a co-worker to sort a heap of books into different compartments on the shelf.\(n\) books must be split into \(k\) compartments on the bookshelf (\(n\) is divisible by \(k\)). Each book is represented by a lowercase Latin letter from 'a' to 'y' inclusively, which is the beginning letter in the title of the book.Ela must stack exactly \(\frac{n}{k}\) books in each compartment. After the books are stacked, for each compartment indexed from \(1\) to \(k\), she takes the minimum excluded (MEX) letter of the multiset of letters formed by letters representing all books in that compartment, then combines the resulting letters into a string. The first letter of the resulting string is the MEX letter of the multiset of letters formed by the first compartment, the second letter of the resulting string is the MEX letter of the multiset of letters formed by the second compartment, ... and so on. Please note, under the constraint of this problem, MEX letter can always be determined for any multiset found in this problem because 'z' is not used.What is the lexicographically greatest resulting string possible that Ela can create?A string \(a\) is lexicographically greater than a string \(b\) if and only if one of the following holds: \(b\) is a prefix of \(a\), but \(b \ne a\); in the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears later in the alphabet than the corresponding letter in \(b\). The minimum excluded (MEX) letter of a multiset of letters is the letter that appears earliest in the alphabet and is not contained in the multiset. For example, if a multiset of letters contains \(7\) letters 'b', 'a', 'b', 'c', 'e', 'c', 'f' respectively, then the MEX letter of this compartment is 'd', because 'd' is not included in the multiset, and all letters comes before 'd' in the alphabet, namely 'a', 'b' and 'c', are included in the multiset.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le n \le 200\); \(1 \le k \le n\)). It is guaranteed that \(n\) is divisible by \(k\).The second line of each test case contains a string of \(n\) lowercase Latin letters from 'a' to 'y' inclusively. Each letter represents the starting letter of the title of a book in the initial heap.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(1000\).
|
For each test case, output a string of \(k\) letters which is the most optimal string that Ela can find.
|
In the first test case, the books can be divided into \(3\) compartments as below: the first compartment contains the books with indices \(1, 2, 3, 7\): \(multiset_1 = \{\)'c', 'a', 'b', 'd'\(\}\) \(\rightarrow\) \(MEX(multiset_1) =\) 'e' the second compartment contains the books with indices \(4, 5, 6, 9\) : \(multiset_2 = \{\)'c', 'c', 'a', 'b'\(\}\) \(\rightarrow\) \(MEX(multiset_2) =\) 'd' the third compartment contains the remaining books \(8, 10, 11, 12\) : \(multiset_3 = \{\) 'a', 'a', 'a', 'c'\(\}\) \(\rightarrow\) \(MEX(multiset_3) =\) 'b' Therefore, the answer is 'edb'. It can be proven that there is no way that Ela can arrange the books so that it results in a lexicographically greater string.
|
Input: 512 3cabccadabaac12 6cabccadabaac12 12cabccadabaac25 1abcdefghijklmnopqrstuvwxy10 5bcdxedbcfg | Output: edb ccbbba bbbbbaaaaaaa z aaaaa
|
Beginner
| 3 | 2,030 | 603 | 104 | 17 |
883 |
H
|
883H
|
H. Palindromic Cut
| 1,800 |
brute force; implementation; strings
|
Kolya has a string s of length n consisting of lowercase and uppercase Latin letters and digits.He wants to rearrange the symbols in s and cut it into the minimum number of parts so that each part is a palindrome and all parts have the same lengths. A palindrome is a string which reads the same backward as forward, such as madam or racecar.Your task is to help Kolya and determine the minimum number of palindromes of equal lengths to cut s into, if it is allowed to rearrange letters in s before cuttings.
|
The first line contains an integer n (1 β€ n β€ 4Β·105) β the length of string s.The second line contains a string s of length n consisting of lowercase and uppercase Latin letters and digits.
|
Print to the first line an integer k β minimum number of palindromes into which you can cut a given string.Print to the second line k strings β the palindromes themselves. Separate them by a space. You are allowed to print palindromes in arbitrary order. All of them should have the same length.
|
Input: 6aabaac | Output: 2aba aca
|
Medium
| 3 | 508 | 189 | 295 | 8 |
|
792 |
F
|
792F
|
F. Mages and Monsters
| 3,100 |
data structures; geometry
|
Vova plays a computer game known as Mages and Monsters. Vova's character is a mage. Though as he has just started, his character knows no spells.Vova's character can learn new spells during the game. Every spell is characterized by two values xi and yi β damage per second and mana cost per second, respectively. Vova doesn't have to use a spell for an integer amount of seconds. More formally, if he uses a spell with damage x and mana cost y for z seconds, then he will deal xΒ·z damage and spend yΒ·z mana (no rounding). If there is no mana left (mana amount is set in the start of the game and it remains the same at the beginning of every fight), then character won't be able to use any spells. It is prohibited to use multiple spells simultaneously.Also Vova can fight monsters. Every monster is characterized by two values tj and hj β monster kills Vova's character in tj seconds and has hj health points. Mana refills after every fight (or Vova's character revives with full mana reserve), so previous fights have no influence on further ones.Vova's character kills a monster, if he deals hj damage to it in no more than tj seconds using his spells (it is allowed to use more than one spell in a fight) and spending no more mana than he had at the beginning of the fight. If monster's health becomes zero exactly in tj seconds (it means that the monster and Vova's character kill each other at the same time), then Vova wins the fight.You have to write a program which can answer two types of queries: 1 x y β Vova's character learns new spell which deals x damage per second and costs y mana per second. 2 t h β Vova fights the monster which kills his character in t seconds and has h health points. Note that queries are given in a different form. Also remember that Vova's character knows no spells at the beginning of the game.For every query of second type you have to determine if Vova is able to win the fight with corresponding monster.
|
The first line contains two integer numbers q and m (2 β€ q β€ 105, 1 β€ m β€ 1012) β the number of queries and the amount of mana at the beginning of every fight.i-th of each next q lines contains three numbers ki, ai and bi (1 β€ ki β€ 2, 1 β€ ai, bi β€ 106). Using them you can restore queries this way: let j be the index of the last query of second type with positive answer (j = 0 if there were none of these). If ki = 1, then character learns spell with x = (ai + j) mod 106 + 1, y = (bi + j) mod 106 + 1. If ki = 2, then you have to determine if Vova is able to win the fight against monster with t = (ai + j) mod 106 + 1, h = (bi + j) mod 106 + 1.
|
For every query of second type print YES if Vova is able to win the fight with corresponding monster and NO otherwise.
|
In first example Vova's character at first learns the spell with 5 damage and 10 mana cost per second. Next query is a fight with monster which can kill character in 20 seconds and has 50 health points. Vova kills it in 10 seconds (spending 100 mana). Next monster has 52 health, so Vova can't deal that much damage with only 100 mana.
|
Input: 3 1001 4 92 19 492 19 49 | Output: YESNO
|
Master
| 2 | 1,950 | 648 | 118 | 7 |
2,014 |
A
|
2014A
|
A. Robin Helps
| 800 |
greedy; implementation
|
There is a little bit of the outlaw in everyone, and a little bit of the hero too.The heroic outlaw Robin Hood is famous for taking from the rich and giving to the poor.Robin encounters \(n\) people starting from the \(1\)-st and ending with the \(n\)-th. The \(i\)-th person has \(a_i\) gold. If \(a_i \ge k\), Robin will take all \(a_i\) gold, and if \(a_i=0\), Robin will give \(1\) gold if he has any. Robin starts with \(0\) gold.Find out how many people Robin gives gold to.
|
The first line of the input contains a single integer \(t\) (\(1\leq t \leq 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\), \(k\) (\(1 \le n \le 50, 1 \le k \le 100\)) β the number of people and the threshold at which Robin Hood takes the gold.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 100\)) β the gold of each person.
|
For each test case, output a single integer, the number of people that will get gold from Robin Hood.
|
In the first test case, Robin takes \(2\) gold from the first person and gives a gold to the second person.In the second test case, Robin takes \(3\) gold and gives \(1\) gold to each of the next \(2\) people.In the third test case, Robin takes \(3\) gold and so only gives gold to \(3\) other people.
|
Input: 42 22 03 23 0 06 20 3 0 0 0 02 55 4 | Output: 1 2 3 0
|
Beginner
| 2 | 480 | 427 | 101 | 20 |
234 |
C
|
234C
|
C. Weather
| 1,300 |
dp; implementation
|
Scientists say a lot about the problems of global warming and cooling of the Earth. Indeed, such natural phenomena strongly influence all life on our planet.Our hero Vasya is quite concerned about the problems. He decided to try a little experiment and observe how outside daily temperature changes. He hung out a thermometer on the balcony every morning and recorded the temperature. He had been measuring the temperature for the last n days. Thus, he got a sequence of numbers t1, t2, ..., tn, where the i-th number is the temperature on the i-th day.Vasya analyzed the temperature statistics in other cities, and came to the conclusion that the city has no environmental problems, if first the temperature outside is negative for some non-zero number of days, and then the temperature is positive for some non-zero number of days. More formally, there must be a positive integer k (1 β€ k β€ n - 1) such that t1 < 0, t2 < 0, ..., tk < 0 and tk + 1 > 0, tk + 2 > 0, ..., tn > 0. In particular, the temperature should never be zero. If this condition is not met, Vasya decides that his city has environmental problems, and gets upset.You do not want to upset Vasya. Therefore, you want to select multiple values of temperature and modify them to satisfy Vasya's condition. You need to know what the least number of temperature values needs to be changed for that.
|
The first line contains a single integer n (2 β€ n β€ 105) β the number of days for which Vasya has been measuring the temperature. The second line contains a sequence of n integers t1, t2, ..., tn (|ti| β€ 109) β the sequence of temperature values. Numbers ti are separated by single spaces.
|
Print a single integer β the answer to the given task.
|
Note to the first sample: there are two ways to change exactly one number so that the sequence met Vasya's condition. You can either replace the first number 1 by any negative number or replace the number -2 by any positive number.
|
Input: 4-1 1 -2 1 | Output: 1
|
Easy
| 2 | 1,362 | 289 | 54 | 2 |
1,324 |
D
|
1324D
|
D. Pair of Topics
| 1,400 |
binary search; data structures; sortings; two pointers
|
The next lecture in a high school requires two topics to be discussed. The \(i\)-th topic is interesting by \(a_i\) units for the teacher and by \(b_i\) units for the students.The pair of topics \(i\) and \(j\) (\(i < j\)) is called good if \(a_i + a_j > b_i + b_j\) (i.e. it is more interesting for the teacher).Your task is to find the number of good pairs of topics.
|
The first line of the input contains one integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of topics.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the interestingness of the \(i\)-th topic for the teacher.The third line of the input contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le 10^9\)), where \(b_i\) is the interestingness of the \(i\)-th topic for the students.
|
Print one integer β the number of good pairs of topic.
|
Input: 5 4 8 2 6 2 4 5 4 1 3 | Output: 7
|
Easy
| 4 | 369 | 467 | 54 | 13 |
|
1,249 |
D2
|
1249D2
|
D2. Too Many Segments (hard version)
| 1,800 |
data structures; greedy; sortings
|
The only difference between easy and hard versions is constraints.You are given \(n\) segments on the coordinate axis \(OX\). Segments can intersect, lie inside each other and even coincide. The \(i\)-th segment is \([l_i; r_i]\) (\(l_i \le r_i\)) and it covers all integer points \(j\) such that \(l_i \le j \le r_i\).The integer point is called bad if it is covered by strictly more than \(k\) segments.Your task is to remove the minimum number of segments so that there are no bad points at all.
|
The first line of the input contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β the number of segments and the maximum number of segments by which each integer point can be covered.The next \(n\) lines contain segments. The \(i\)-th line contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le 2 \cdot 10^5\)) β the endpoints of the \(i\)-th segment.
|
In the first line print one integer \(m\) (\(0 \le m \le n\)) β the minimum number of segments you need to remove so that there are no bad points.In the second line print \(m\) distinct integers \(p_1, p_2, \dots, p_m\) (\(1 \le p_i \le n\)) β indices of segments you remove in any order. If there are multiple answers, you can print any of them.
|
Input: 7 2 11 11 9 11 7 8 8 9 7 8 9 11 7 9 | Output: 3 4 6 7
|
Medium
| 3 | 498 | 386 | 346 | 12 |
|
316 |
D2
|
316D2
|
D2. PE Lesson
| 2,300 |
dp
|
Smart Beaver decided to be not only smart, but also a healthy beaver! And so he began to attend physical education classes at school X. In this school, physical education has a very creative teacher. One of his favorite warm-up exercises is throwing balls. Students line up. Each one gets a single ball in the beginning. The balls are numbered from 1 to n (by the demand of the inventory commission). Figure 1. The initial position for n = 5. After receiving the balls the students perform the warm-up exercise. The exercise takes place in a few throws. For each throw the teacher chooses any two arbitrary different students who will participate in it. The selected students throw their balls to each other. Thus, after each throw the students remain in their positions, and the two balls are swapped. Figure 2. The example of a throw. In this case there was a throw between the students, who were holding the 2-nd and the 4-th balls. Since the warm-up has many exercises, each of them can only continue for little time. Therefore, for each student we know the maximum number of throws he can participate in. For this lessons maximum number of throws will be 1 or 2.Note that after all phases of the considered exercise any ball can end up with any student. Smart Beaver decided to formalize it and introduced the concept of the ""ball order"". The ball order is a sequence of n numbers that correspond to the order of balls in the line. The first number will match the number of the ball of the first from the left student in the line, the second number will match the ball of the second student, and so on. For example, in figure 2 the order of the balls was (1, 2, 3, 4, 5), and after the throw it was (1, 4, 3, 2, 5). Smart beaver knows the number of students and for each student he knows the maximum number of throws in which he can participate. And now he is wondering: what is the number of distinct ways of ball orders by the end of the exercise.
|
The first line contains a single number n β the number of students in the line and the number of balls. The next line contains exactly n space-separated integers. Each number corresponds to a student in the line (the i-th number corresponds to the i-th from the left student in the line) and shows the number of throws he can participate in.The input limits for scoring 30 points are (subproblem D1): 1 β€ n β€ 10. The input limits for scoring 70 points are (subproblems D1+D2): 1 β€ n β€ 500. The input limits for scoring 100 points are (subproblems D1+D2+D3): 1 β€ n β€ 1000000.
|
The output should contain a single integer β the number of variants of ball orders after the warm up exercise is complete. As the number can be rather large, print it modulo 1000000007 (109 + 7).
|
Input: 51 2 2 1 2 | Output: 120
|
Expert
| 1 | 1,956 | 574 | 195 | 3 |
|
691 |
A
|
691A
|
A. Fashion in Berland
| 1,000 |
implementation
|
According to rules of the Berland fashion, a jacket should be fastened by all the buttons except only one, but not necessarily it should be the last one. Also if the jacket has only one button, it should be fastened, so the jacket will not swinging open.You are given a jacket with n buttons. Determine if it is fastened in a right way.
|
The first line contains integer n (1 β€ n β€ 1000) β the number of buttons on the jacket.The second line contains n integers ai (0 β€ ai β€ 1). The number ai = 0 if the i-th button is not fastened. Otherwise ai = 1.
|
In the only line print the word ""YES"" if the jacket is fastened in a right way. Otherwise print the word ""NO"".
|
Input: 31 0 1 | Output: YES
|
Beginner
| 1 | 336 | 211 | 114 | 6 |
|
2,075 |
E
|
2075E
|
E. XOR Matrix
| 2,500 |
bitmasks; combinatorics; dp; implementation; math
|
For two arrays \(a = [a_1, a_2, \dots, a_n]\) and \(b = [b_1, b_2, \dots, b_m]\), we define the XOR matrix \(X\) of size \(n \times m\), where for each pair \((i,j)\) (\(1 \le i \le n\); \(1 \le j \le m\)) it holds that \(X_{i,j} = a_i \oplus b_j\). The symbol \(\oplus\) denotes the bitwise XOR operation.You are given four integers \(n, m, A, B\). Count the number of such pairs of arrays \((a, b)\) such that: \(a\) consists of \(n\) integers, each of which is from \(0\) to \(A\); \(b\) consists of \(m\) integers, each of which is from \(0\) to \(B\); in the XOR matrix formed from these arrays, there are no more than two distinct values.
|
The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.Each test case consists of one line containing four integers \(n, m, A, B\) (\(2 \le n, m, A, B \le 2^{29} - 1\)).
|
For each test case, output one integer β the number of pairs of arrays \((a, b)\) that satisfy all three conditions. Since this number can be very large, output it modulo \(998244353\).
|
Input: 62 2 2 22 3 4 55 7 4 31337 42 1337 424 2 13 37536870902 536370902 536390912 466128231 | Output: 57 864 50360 439988899 112000 732195491
|
Expert
| 5 | 644 | 206 | 185 | 20 |
|
98 |
D
|
98D
|
D. Help Monks
| 2,500 |
constructive algorithms
|
In a far away kingdom is the famous Lio Shan monastery. Gods constructed three diamond pillars on the monastery's lawn long ago. Gods also placed on one pillar n golden disks of different diameters (in the order of the diameters' decreasing from the bottom to the top). Besides, gods commanded to carry all the disks from the first pillar to the third one according to the following rules: you can carry only one disk in one move; you cannot put a larger disk on a smaller one. There was no universal opinion concerning what is to happen after the gods' will is done: some people promised world peace and eternal happiness to everyone, whereas others predicted that the kingdom will face communi⦠(gee, what am I rambling about?) the Armageddon. However, as everybody knew that it was impossible to solve the problem in less than 2n - 1 moves and the lazy Lio Shan monks never even started to solve it, everyone lives peacefully even though the problem was never solved and nobody was afraid of the Armageddon.However, the monastery wasn't doing so well lately and the wise prior Ku Sean Sun had to cut some disks at the edges and use the gold for the greater good. Wouldn't you think that the prior is entitled to have an air conditioning system? Besides, staying in the monastery all year is sooo dull⦠One has to have a go at something new now and then, go skiing, for example⦠Ku Sean Sun realize how big a mistake he had made only after a while: after he cut the edges, the diameters of some disks got the same; that means that some moves that used to be impossible to make, were at last possible (why, gods never prohibited to put a disk on a disk of the same diameter). Thus, the possible Armageddon can come earlier than was initially planned by gods. Much earlier. So much earlier, in fact, that Ku Sean Sun won't even have time to ski all he wants or relax under the air conditioner.The wise prior could never let that last thing happen and he asked one very old and very wise witch PikiWedia to help him. May be she can determine the least number of moves needed to solve the gods' problem. However, the witch laid out her cards and found no answer for the prior. Then he asked you to help him.Can you find the shortest solution of the problem, given the number of disks and their diameters? Keep in mind that it is allowed to place disks of the same diameter one on the other one, however, the order in which the disks are positioned on the third pillar in the end should match the initial order of the disks on the first pillar.
|
The first line contains an integer n β the number of disks (1 β€ n β€ 20). The second line contains n integers di β the disks' diameters after Ku Sean Sun cut their edges. The diameters are given from the bottom to the top (1 β€ di β€ 20, besides, di β₯ di + 1 for any 1 β€ i < n).
|
Print on the first line number m β the smallest number of moves to solve the gods' problem. Print on the next m lines the description of moves: two space-separated positive integers si and ti that determine the number of the pillar from which the disk is moved and the number of pillar where the disk is moved, correspondingly (1 β€ si, ti β€ 3, si β ti).
|
Pay attention to the third test demonstrating that the order of disks should remain the same in the end, even despite the disks' same radius. If this condition was not necessary to fulfill, the gods' task could have been solved within a smaller number of moves (three β simply moving the three disks from the first pillar on the third one).
|
Input: 33 2 1 | Output: 71 31 23 21 32 12 31 3
|
Expert
| 1 | 2,541 | 275 | 353 | 0 |
1,045 |
H
|
1045H
|
H. Self-exploration
| 2,400 |
math
|
Being bored of exploring the Moon over and over again Wall-B decided to explore something he is made of β binary numbers. He took a binary number and decided to count how many times different substrings of length two appeared. He stored those values in \(c_{00}\), \(c_{01}\), \(c_{10}\) and \(c_{11}\), representing how many times substrings 00, 01, 10 and 11 appear in the number respectively. For example: \(10111100 \rightarrow c_{00} = 1, \ c_{01} = 1,\ c_{10} = 2,\ c_{11} = 3\) \(10000 \rightarrow c_{00} = 3,\ c_{01} = 0,\ c_{10} = 1,\ c_{11} = 0\) \(10101001 \rightarrow c_{00} = 1,\ c_{01} = 3,\ c_{10} = 3,\ c_{11} = 0\) \(1 \rightarrow c_{00} = 0,\ c_{01} = 0,\ c_{10} = 0,\ c_{11} = 0\)Wall-B noticed that there can be multiple binary numbers satisfying the same \(c_{00}\), \(c_{01}\), \(c_{10}\) and \(c_{11}\) constraints. Because of that he wanted to count how many binary numbers satisfy the constraints \(c_{xy}\) given the interval \([A, B]\). Unfortunately, his processing power wasn't strong enough to handle large intervals he was curious about. Can you help him? Since this number can be large print it modulo \(10^9 + 7\).
|
First two lines contain two positive binary numbers \(A\) and \(B\) (\(1 \leq A \leq B < 2^{100\,000}\)), representing the start and the end of the interval respectively. Binary numbers \(A\) and \(B\) have no leading zeroes.Next four lines contain decimal numbers \(c_{00}\), \(c_{01}\), \(c_{10}\) and \(c_{11}\) (\(0 \leq c_{00}, c_{01}, c_{10}, c_{11} \leq 100\,000\)) representing the count of two-digit substrings 00, 01, 10 and 11 respectively.
|
Output one integer number representing how many binary numbers in the interval \([A, B]\) satisfy the constraints mod \(10^9 + 7\).
|
Example 1: The binary numbers in the interval \([10,1001]\) are \(10,11,100,101,110,111,1000,1001\). Only number 110 satisfies the constraints: \(c_{00} = 0, c_{01} = 0, c_{10} = 1, c_{11} = 1\).Example 2: No number in the interval satisfies the constraints
|
Input: 1010010011 | Output: 1
|
Expert
| 1 | 1,147 | 451 | 131 | 10 |
2,129 |
C3
|
2129C3
|
C3. Interactive RBS (Hard Version)
| 2,300 |
binary search; bitmasks; constructive algorithms; dp; interactive
|
This is an interactive problem.This is the hard version of the problem. The only difference is the limit on the number of queries. You can make hacks only if all versions of the problem are solved.There is a hidden bracket sequence \(s\) of length \(n\), where \(s\) only contains \(\texttt{'('}\) and \(\texttt{')'}\). It is guaranteed that \(s\) contains at least one \(\texttt{'('}\) and one \(\texttt{')'}\).To find this bracket sequence, you can ask queries. Each query has the following form: you pick an integer \(k\) and arbitrary indices \(i_1, i_2, \ldots, i_k\) (\(1 \le k \le 1000\), \(1 \le i_1, i_2, \ldots, i_k \le n\)). Note that the indices can be equal. Next, you receive an integer \(f(s_{i_1}s_{i_2}\ldots s_{i_k})\) calculated by the jury.For a bracket sequence \(t\), \(f(t)\) is the number of non-empty regular bracket substrings in \(t\) (the substrings must be contiguous). For example, \(f(\texttt{""()())""}) = 3\).A bracket sequence is called regular if it can be constructed in the following way. The empty sequence \(\varnothing\) is regular. If the bracket sequence \(A\) is regular, then \(\mathtt{(}A\mathtt{)}\) is also regular. If the bracket sequences \(A\) and \(B\) are regular, then the concatenated sequence \(A B\) is also regular. For example, the sequences \(\texttt{""(())()""}\), \(\texttt{""()""}\) are regular, while \(\texttt{""(()""}\) and \(\texttt{""())(""}\) are not.Find the sequence \(s\) using no more than \(100\) queries.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 20\)). The description of the test cases follows.
|
In the first test case, the hidden bracket sequence is \(s=\texttt{"")((""}\).For the query ""? 4 1 2 3 3"", the jury returns \(0\) because \(f(s_{1}s_{2}s_{3}s_{3}) = f(\texttt{"")(((""}) = 0\).For the query ""? 2 2 1"", the jury returns \(1\) because \(f(s_{2}s_{1}) = f(\texttt{""()""}) = 1\).For the query ""? 2 3 1"", the jury returns \(1\) because \(f(s_{3}s_{1}) = f(\texttt{""()""}) = 1\).In the second test case, the hidden bracket sequence is \(s=\texttt{""()""}\).For the query ""? 4 1 2 1 2"", the jury returns \(3\) because \(f(s_{1}s_{2}s_{1}s_{2}) = f(\texttt{""()()""}) = 3\).Note that the example is only for understanding the statement and does not guarantee finding the unique bracket sequence \(s\).
|
Input: 2 3 0 1 1 2 3 | Output: ? 4 1 2 3 3 ? 2 2 1 ? 2 3 1 ! )(( ? 4 1 2 1 2 ! ()
|
Expert
| 5 | 1,478 | 159 | 0 | 21 |
|
1,299 |
E
|
1299E
|
E. So Mean
| 3,400 |
interactive; math
|
This problem is interactive.We have hidden a permutation \(p_1, p_2, \dots, p_n\) of numbers from \(1\) to \(n\) from you, where \(n\) is even. You can try to guess it using the following queries:\(?\) \(k\) \(a_1\) \(a_2\) \(\dots\) \(a_k\).In response, you will learn if the average of elements with indexes \(a_1, a_2, \dots, a_k\) is an integer. In other words, you will receive \(1\) if \(\frac{p_{a_1} + p_{a_2} + \dots + p_{a_k}}{k}\) is integer, and \(0\) otherwise. You have to guess the permutation. You can ask not more than \(18n\) queries.Note that permutations \([p_1, p_2, \dots, p_k]\) and \([n + 1 - p_1, n + 1 - p_2, \dots, n + 1 - p_k]\) are indistinguishable. Therefore, you are guaranteed that \(p_1 \le \frac{n}{2}\).Note that the permutation \(p\) is fixed before the start of the interaction and doesn't depend on your queries. In other words, interactor is not adaptive.Note that you don't have to minimize the number of queries.
|
The first line contains a single integer \(n\) (\(2 \le n \le 800\), \(n\) is even).
|
Input: 2 1 2 | Output: ? 1 2 ? 1 1 ! 1 2
|
Master
| 2 | 954 | 84 | 0 | 12 |
||
1,773 |
E
|
1773E
|
E. Easy Assembly
| 1,400 |
greedy; sortings
|
Emma loves playing with blocks. She has several cubic blocks of the same size that are numbered with distinct integers written on them. She assembles towers from those blocks by stacking them vertically. A configuration of her game is a set of towers that she has assembled from the blocks. Emma can perform two kinds of operations on a configuration of towers: Split any tower with more than one block in it by taking any number of blocks from the top of the tower and moving them to a new tower keeping their order, so that the top block of the old tower becomes the top block of the new tower. As a result of this operation, the number of towers increases by one. Combine any two towers by moving blocks from one tower on top of the other tower in the same order. As a result of this operation, the number of towers decreases by one.Emma wants to stack all the blocks into a single tower so that all blocks come in order sorted by the numbers β from the block with the minimal number at the top to the block with the maximal number at the bottom. Emma wants to do as little of splitting and combining operations as possible. Your task is to find the minimal number of operations she has to make and output how many splits and combines are needed.
|
The first line of the input file contains an integer \(n\) (\(1 \le n \le 10\,000\)) β the number of towers in the initial configuration. Next \(n\) lines describe towers. Each tower \(i\) is described by a line that starts with the number \(k_i\) (\(k_i \ge 1\); \(\sum_1^n{k_i} \le 10\,000\)) β the number of blocks in the tower, followed by \(k_i\) numbers \(b_{i,j}\) (\(1 \le b_{i,j} \le 10^9\)) β numbers written on the blocks in the \(i\)-th tower, listed from top to bottom. All block numbers listed in the input are different.
|
Output a line with two integers \(s\) and \(c\) β the number of split and combine operations Emma should make to get a single tower with blocks sorted by their numbers, so that the total number of operations is minimized.
|
The example needs the following operations (1 split and 2 combines).InitialSplit lastCombined 2nd onto 1stCombined 1st onto 2nd
|
Input: 2 3 3 5 8 2 9 2 | Output: 1 2
|
Easy
| 2 | 1,249 | 535 | 221 | 17 |
571 |
E
|
571E
|
E. Geometric Progressions
| 3,200 |
math
|
Geometric progression with the first element a and common ratio b is a sequence of numbers a, ab, ab2, ab3, ....You are given n integer geometric progressions. Your task is to find the smallest integer x, that is the element of all the given progressions, or else state that such integer does not exist.
|
The first line contains integer (1 β€ n β€ 100) β the number of geometric progressions. Next n lines contain pairs of integers a, b (1 β€ a, b β€ 109), that are the first element and the common ratio of the corresponding geometric progression.
|
If the intersection of all progressions is empty, then print - 1, otherwise print the remainder of the minimal positive integer number belonging to all progressions modulo 1000000007 (109 + 7).
|
In the second sample test one of the progressions contains only powers of two, the other one contains only powers of three.
|
Input: 22 24 1 | Output: 4
|
Master
| 1 | 303 | 239 | 193 | 5 |
612 |
C
|
612C
|
C. Replace To Make Regular Bracket Sequence
| 1,400 |
data structures; expression parsing; math
|
You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bracket {, but you can't replace it by ) or >.The following definition of a regular bracket sequence is well-known, so you can be familiar with it.Let's define a regular bracket sequence (RBS). Empty string is RBS. Let s1 and s2 be a RBS then the strings <s1>s2, {s1}s2, [s1]s2, (s1)s2 are also RBS.For example the string ""[[(){}]<>]"" is RBS, but the strings ""[)()"" and ""][()()"" are not.Determine the least number of replaces to make the string s RBS.
|
The only line contains a non empty string s, consisting of only opening and closing brackets of four kinds. The length of s does not exceed 106.
|
If it's impossible to get RBS from s print Impossible.Otherwise print the least number of replaces needed to get RBS from s.
|
Input: [<}){} | Output: 2
|
Easy
| 3 | 700 | 144 | 124 | 6 |
|
1,357 |
C2
|
1357C2
|
C2. Prepare superposition of basis states with the same parity
| 0 |
*special
|
You are given \(N\) qubits in the state \(|0...0 \rangle\), and an integer \(parity \in \{0, 1\}\). Your task is to prepare an equal superposition of all basis states that have the given parity of the number of \(1\)s in their binary notation, i.e., the basis states that have an even number of \(1\)s if \(parity = 0\) or the basis states that have an odd number of \(1\)s if \(parity = 1\).For example, for \(N = 2\) the required state would be \(\frac{1}{\sqrt{2}} \big( |00 \rangle + |11 \rangle)\) if \(parity = 0\). \(\frac{1}{\sqrt{2}} \big( |01 \rangle + |10 \rangle)\) if \(parity = 1\). You are not allowed to use any gates except the Pauli gates (X, Y and Z), the Hadamard gate and the controlled versions of those (you are allowed to use multiple qubits as controls in the controlled versions of gates). However, you are allowed to use measurements.You have to implement an operation which takes an array of \(N\) qubits and an integer as an input and has no output. The ""output"" of your solution is the state in which it left the input qubits.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Intrinsic; operation Solve (qs : Qubit[], parity : Int) : Unit { // your code here }}
|
Beginner
| 1 | 1,233 | 0 | 0 | 13 |
||||
1,773 |
G
|
1773G
|
G. Game of Questions
| 2,800 |
bitmasks; combinatorics; dp; fft; math; probabilities
|
Genie is taking part in an intellectual game. The game consists of \(n\) questions, and there are \(m\) participants numbered from \(1\) to \(m\). Genie is the participant number \(1\).For each question \(i\) and participant \(j\), it is known whether the participant will answer the question correctly or not.The goal of the game is to be the last participant staying in the game.The game is conducted as follows. First, all \(n\) questions get shuffled uniformly at random (all \(n!\) permutations are equally likely). Then, the questions are asked one by one. Each participant answers the question. If all participants still in the game answer the question correctly, or if all of them answer the question incorrectly, nothing happens. Otherwise, those participants who answer the question incorrectly lose and leave the game.After all \(n\) questions are asked, all participants who are still in the game are declared to be the winners.What is the probability that Genie will win the game?
|
The first line contains two integers \(n\) and \(m\) β the number of questions and the number of participants (\(1 \le n \le 2 \cdot 10^5\); \(2 \le m \le 17\)).The \(i\)-th of the next \(n\) lines contains \(m\) characters \(s_{i, 1}, s_{i, 2}, \ldots, s_{i, m}\). Character \(s_{i, j}\) is '1' if participant \(j\) answers question \(i\) correctly or '0' otherwise.
|
Print the probability that Genie will win the game. Your answer will be considered correct if its absolute or relative error does not exceed \(10^{-9}\).
|
In the first example, there is a single question and Genie will answer it correctly, thus winning the game (along with participants \(2\) and \(4\)).In the second example, one participant will leave after the first asked question, and another participant will leave after the second asked question. Each participant will win with probability \(\frac{1}{3}\).
|
Input: 1 5 11010 | Output: 1.0000000000000000
|
Master
| 6 | 993 | 367 | 153 | 17 |
1,532 |
D
|
1532D
|
D. Teams Forming
| 0 |
*special; sortings
|
There are \(n\) students in a university. The number of students is even. The \(i\)-th student has programming skill equal to \(a_i\). The coach wants to form \(\frac{n}{2}\) teams. Each team should consist of exactly two students, and each student should belong to exactly one team. Two students can form a team only if their skills are equal (otherwise they cannot understand each other and cannot form a team).Students can solve problems to increase their skill. One solved problem increases the skill by one.The coach wants to know the minimum total number of problems students should solve to form exactly \(\frac{n}{2}\) teams (i.e. each pair of students should form a team). Your task is to find this number.
|
The first line of the input contains one integer \(n\) (\(2 \le n \le 100\)) β the number of students. It is guaranteed that \(n\) is even.The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 100\)), where \(a_i\) is the skill of the \(i\)-th student.
|
Print one number β the minimum total number of problems students should solve to form exactly \(\frac{n}{2}\) teams.
|
In the first example the optimal teams will be: \((3, 4)\), \((1, 6)\) and \((2, 5)\), where numbers in brackets are indices of students. Then, to form the first team the third student should solve \(1\) problem, to form the second team nobody needs to solve problems and to form the third team the second student should solve \(4\) problems so the answer is \(1 + 4 = 5\).In the second example the first student should solve \(99\) problems to form a team with the second one.
|
Input: 6 5 10 2 3 14 5 | Output: 5
|
Beginner
| 2 | 715 | 293 | 116 | 15 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.