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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
835 |
F
|
835F
|
F. Roads in the Kingdom
| 2,500 |
dfs and similar; dp; graphs; trees
|
In the Kingdom K., there are n towns numbered with integers from 1 to n. The towns are connected by n bi-directional roads numbered with integers from 1 to n. The i-th road connects the towns ui and vi and its length is li. There is no more than one road between two towns. Also, there are no roads that connect the towns with itself.Let's call the inconvenience of the roads the maximum of the shortest distances between all pairs of towns.Because of lack of money, it was decided to close down one of the roads so that after its removal it is still possible to reach any town from any other. You have to find the minimum possible inconvenience of the roads after closing down one of the roads.
|
The first line contains the integer n (3 β€ n β€ 2Β·105) β the number of towns and roads.The next n lines contain the roads description. The i-th from these lines contains three integers ui, vi, li (1 β€ ui, vi β€ n, 1 β€ li β€ 109) β the numbers of towns connected by the i-th road and the length of the i-th road. No road connects a town to itself, no two roads connect the same towns.It's guaranteed that it's always possible to close down one of the roads so that all the towns are still reachable from each other.
|
Print a single integer β the minimum possible inconvenience of the roads after the refusal from one of the roads.
|
Input: 31 2 42 3 51 3 1 | Output: 5
|
Expert
| 4 | 695 | 511 | 113 | 8 |
|
724 |
C
|
724C
|
C. Ray Tracing
| 1,800 |
greedy; hashing; implementation; math; number theory; sortings
|
There are k sensors located in the rectangular room of size n Γ m meters. The i-th sensor is located at point (xi, yi). All sensors are located at distinct points strictly inside the rectangle. Opposite corners of the room are located at points (0, 0) and (n, m). Walls of the room are parallel to coordinate axes.At the moment 0, from the point (0, 0) the laser ray is released in the direction of point (1, 1). The ray travels with a speed of meters per second. Thus, the ray will reach the point (1, 1) in exactly one second after the start.When the ray meets the wall it's reflected by the rule that the angle of incidence is equal to the angle of reflection. If the ray reaches any of the four corners, it immediately stops.For each sensor you have to determine the first moment of time when the ray will pass through the point where this sensor is located. If the ray will never pass through this point, print - 1 for such sensors.
|
The first line of the input contains three integers n, m and k (2 β€ n, m β€ 100 000, 1 β€ k β€ 100 000) β lengths of the room's walls and the number of sensors.Each of the following k lines contains two integers xi and yi (1 β€ xi β€ n - 1, 1 β€ yi β€ m - 1) β coordinates of the sensors. It's guaranteed that no two sensors are located at the same point.
|
Print k integers. The i-th of them should be equal to the number of seconds when the ray first passes through the point where the i-th sensor is located, or - 1 if this will never happen.
|
In the first sample, the ray will consequently pass through the points (0, 0), (1, 1), (2, 2), (3, 3). Thus, it will stop at the point (3, 3) after 3 seconds. In the second sample, the ray will consequently pass through the following points: (0, 0), (1, 1), (2, 2), (3, 3), (2, 4), (1, 3), (0, 2), (1, 1), (2, 0), (3, 1), (2, 2), (1, 3), (0, 4). The ray will stop at the point (0, 4) after 12 seconds. It will reflect at the points (3, 3), (2, 4), (0, 2), (2, 0) and (3, 1).
|
Input: 3 3 41 11 22 12 2 | Output: 1-1-12
|
Medium
| 6 | 937 | 348 | 187 | 7 |
1,838 |
B
|
1838B
|
B. Minimize Permutation Subarrays
| 1,100 |
constructive algorithms; math
|
You are given a permutation \(p\) of size \(n\). You want to minimize the number of subarrays of \(p\) that are permutations. In order to do so, you must perform the following operation exactly once: Select integers \(i\), \(j\), where \(1 \le i, j \le n\), then Swap \(p_i\) and \(p_j\). For example, if \(p = [5, 1, 4, 2, 3]\) and we choose \(i = 2\), \(j = 3\), the resulting array will be \([5, 4, 1, 2, 3]\). If instead we choose \(i = j = 5\), the resulting array will be \([5, 1, 4, 2, 3]\).Which choice of \(i\) and \(j\) will minimize the number of subarrays that are permutations?A permutation of length \(n\) 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).An array \(a\) is a subarray of an array \(b\) if \(a\) can be obtained from \(b\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2\cdot 10^5\)) β the size of the permutation.The next line of each test case contains \(n\) integers \(p_1, p_2, \ldots p_n\) (\(1 \le p_i \le n\), all \(p_i\) are distinct) β the elements of the permutation \(p\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output two integers \(i\) and \(j\) (\(1 \le i, j \le n\)) β the indices to swap in \(p\).If there are multiple solutions, print any of them.
|
For the first test case, there are four possible arrays after the swap: If we swap \(p_1\) and \(p_2\), we get the array \([2, 1, 3]\), which has 3 subarrays that are permutations (\([1]\), \([2, 1]\), \([2, 1, 3]\)). If we swap \(p_1\) and \(p_3\), we get the array \([3, 2, 1]\), which has 3 subarrays that are permutations (\([1]\), \([2, 1]\), \([3, 2, 1]\)). If we swap \(p_2\) and \(p_3\), we get the array \([1, 3, 2]\), which has 2 subarrays that are permutations (\([1]\), \([1, 3, 2]\)). If we swap any element with itself, we get the array \([1, 2, 3]\), which has 3 subarrays that are permutations (\([1]\), \([1, 2]\), \([1, 2, 3]\)). So the best swap to make is positions \(2\) and \(3\).For the third sample case, after we swap elements at positions \(2\) and \(5\), the resulting array is \([1, 4, 2, 5, 3]\). The only subarrays that are permutations are \([1]\) and \([1, 4, 2, 5, 3]\). We can show that this is minimal.
|
Input: 831 2 331 3 251 3 2 5 464 5 6 1 2 398 7 6 3 2 1 4 5 9107 10 5 1 9 8 3 2 6 4108 5 10 9 2 1 3 4 6 7102 3 5 7 10 1 8 6 4 9 | Output: 2 3 1 1 5 2 1 4 9 5 8 8 6 10 5 4
|
Easy
| 2 | 1,134 | 538 | 161 | 18 |
921 |
11
|
92111
|
11. Labyrinth-11
| 3,200 |
See the problem statement here: http://codeforces.com/contest/921/problem/01.
|
Master
| 0 | 77 | 0 | 0 | 9 |
|||||
587 |
F
|
587F
|
F. Duff is Mad
| 3,000 |
data structures; strings
|
Duff is mad at her friends. That's why she sometimes makes Malek to take candy from one of her friends for no reason! She has n friends. Her i-th friend's name is si (their names are not necessarily unique). q times, she asks Malek to take candy from her friends. She's angry, but also she acts with rules. When she wants to ask Malek to take candy from one of her friends, like k, she chooses two numbers l and r and tells Malek to take exactly candies from him/her, where occur(t, s) is the number of occurrences of string t in s.Malek is not able to calculate how many candies to take in each request from Duff. That's why she asked for your help. Please tell him how many candies to take in each request.
|
The first line of input contains two integers n and q (1 β€ n, q β€ 105).The next n lines contain the names. i-th of them contains an string si, consisting of lowercase English letters ().The next q lines contain the requests. Each of them contains three integers, l, r and k (says that Malek should take candies from Duff's k-th friend).
|
Print the answer to each request in one line.
|
Input: 5 5aababababababb1 5 43 5 41 5 21 5 31 4 1 | Output: 126371
|
Master
| 2 | 708 | 336 | 45 | 5 |
|
1,838 |
E
|
1838E
|
E. Count Supersequences
| 2,500 |
combinatorics; dp; math
|
You are given an array \(a\) of \(n\) integers, where all elements \(a_i\) lie in the range \([1, k]\). How many different arrays \(b\) of \(m\) integers, where all elements \(b_i\) lie in the range \([1, k]\), contain \(a\) as a subsequence? Two arrays are considered different if they differ in at least one position.A sequence \(x\) is a subsequence of a sequence \(y\) if \(x\) can be obtained from \(y\) by the deletion of several (possibly, zero or all) elements.Since the answer may be large, print it modulo \(10^9 + 7\).
|
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n\), \(m\), \(k\) (\(1 \le n \le 2\cdot 10^5\), \(n \le m \le 10^9\), \(1 \le k \le 10^9\)) β the size of \(a\), the size of \(b\), and the maximum value allowed in the arrays, respectively.The next line of each test case contains \(n\) integers \(a_1, a_2, \ldots a_n\) (\(1\le a_i \le k\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output a single integer β the number of suitable arrays \(b\), modulo \(10^9+7\).
|
For the first example, since \(k=1\), there is only one array of size \(m\) consisting of the integers \([1, k]\). This array (\([1, 1, \ldots, 1]\)) contains the original array as a subsequence, so the answer is 1.For the second example, the \(9\) arrays are \([1, 1, 2, 2]\), \([1, 2, 1, 2]\), \([1, 2, 2, 1]\), \([1, 2, 2, 2]\), \([1, 2, 2, 3]\), \([1, 2, 3, 2]\), \([1, 3, 2, 2]\), \([2, 1, 2, 2]\), \([3, 1, 2, 2]\).For the fourth example, since \(m=n\), the only array of size \(m\) that contains \(a\) as a subsequence is \(a\) itself.
|
Input: 71 1000000 113 4 31 2 25 7 81 2 3 4 16 6 1818 2 2 5 2 161 10 218 10 12345671 1 2 1 2 2 2 15 1000000000 1000000000525785549 816356460 108064697 194447117 725595511 | Output: 1 9 1079 1 1023 906241579 232432822
|
Expert
| 3 | 529 | 629 | 101 | 18 |
1,637 |
E
|
1637E
|
E. Best Pair
| 2,100 |
binary search; brute force; implementation
|
You are given an array \(a\) of length \(n\). Let \(cnt_x\) be the number of elements from the array which are equal to \(x\). Let's also define \(f(x, y)\) as \((cnt_x + cnt_y) \cdot (x + y)\).Also you are given \(m\) bad pairs \((x_i, y_i)\). Note that if \((x, y)\) is a bad pair, then \((y, x)\) is also bad.Your task is to find the maximum value of \(f(u, v)\) over all pairs \((u, v)\), such that \(u \neq v\), that this pair is not bad, and also that \(u\) and \(v\) each occur in the array \(a\). It is guaranteed that such a pair exists.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10\,000\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \le n \le 3 \cdot 10^5\), \(0 \le m \le 3 \cdot 10^5\)) β the length of the array and the number of bad pairs.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β elements of the array.The \(i\)-th of the next \(m\) lines contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i < y_i \le 10^9\)), which represent a bad pair. It is guaranteed that no bad pair occurs twice in the input. It is also guaranteed that \(cnt_{x_i} > 0\) and \(cnt_{y_i} > 0\).It is guaranteed that for each test case there is a pair of integers \((u, v)\), \(u \ne v\), that is not bad, and such that both of these numbers occur in \(a\).It is guaranteed that the total sum of \(n\) and the total sum of \(m\) don't exceed \(3 \cdot 10^5\).
|
For each test case print a single integer β the answer to the problem.
|
In the first test case \(3\), \(6\), \(7\) occur in the array. \(f(3, 6) = (cnt_3 + cnt_6) \cdot (3 + 6) = (3 + 2) \cdot (3 + 6) = 45\). But \((3, 6)\) is bad so we ignore it. \(f(3, 7) = (cnt_3 + cnt_7) \cdot (3 + 7) = (3 + 1) \cdot (3 + 7) = 40\). \(f(6, 7) = (cnt_6 + cnt_7) \cdot (6 + 7) = (2 + 1) \cdot (6 + 7) = 39\). The answer to the problem is \(\max(40, 39) = 40\).
|
Input: 36 16 3 6 7 3 33 62 03 47 41 2 2 3 1 5 11 53 51 32 5 | Output: 40 14 15
|
Hard
| 3 | 546 | 950 | 70 | 16 |
600 |
D
|
600D
|
D. Area of Two Circles' Intersection
| 2,000 |
geometry
|
You are given two circles. Find the area of their intersection.
|
The first line contains three integers x1, y1, r1 ( - 109 β€ x1, y1 β€ 109, 1 β€ r1 β€ 109) β the position of the center and the radius of the first circle.The second line contains three integers x2, y2, r2 ( - 109 β€ x2, y2 β€ 109, 1 β€ r2 β€ 109) β the position of the center and the radius of the second circle.
|
Print the area of the intersection of the circles. The answer will be considered correct if the absolute or relative error doesn't exceed 10 - 6.
|
Input: 0 0 46 0 4 | Output: 7.25298806364175601379
|
Hard
| 1 | 63 | 306 | 145 | 6 |
|
108 |
B
|
108B
|
B. Datatypes
| 1,400 |
math; sortings
|
Tattah's youngest brother, Tuftuf, is new to programming.Since his older brother is such a good programmer, his biggest dream is to outshine him. Tuftuf is a student at the German University in Cairo (GUC) where he learns to write programs in Gava.Today, Tuftuf was introduced to Gava's unsigned integer datatypes. Gava has n unsigned integer datatypes of sizes (in bits) a1, a2, ... an. The i-th datatype have size ai bits, so it can represent every integer between 0 and 2ai - 1 inclusive. Tuftuf is thinking of learning a better programming language. If there exists an integer x, such that x fits in some type i (in ai bits) and xΒ·x does not fit in some other type j (in aj bits) where ai < aj, then Tuftuf will stop using Gava.Your task is to determine Tuftuf's destiny.
|
The first line contains integer n (2 β€ n β€ 105) β the number of Gava's unsigned integer datatypes' sizes. The second line contains a single-space-separated list of n integers (1 β€ ai β€ 109) β sizes of datatypes in bits. Some datatypes may have equal sizes.
|
Print ""YES"" if Tuftuf will stop using Gava, and ""NO"" otherwise.
|
In the second example, x = 7 (1112) fits in 3 bits, but x2 = 49 (1100012) does not fit in 4 bits.
|
Input: 364 16 32 | Output: NO
|
Easy
| 2 | 775 | 256 | 67 | 1 |
1,817 |
D
|
1817D
|
D. Toy Machine
| 2,700 |
constructive algorithms; games; implementation
|
There is a toy machine with toys arranged in two rows of \(n\) cells each (\(n\) is odd). Initial state for \(n=9\). Initially, \(n-2\) toys are placed in the non-corner cells of the top row. The bottom row is initially empty, and its leftmost, rightmost, and central cells are blocked. There are \(4\) buttons to control the toy machine: left, right, up, and down marked by the letters L, R, U, and D correspondingly.When pressing L, R, U, or D, all the toys will be moved simultaneously in the corresponding direction and will only stop if they push into another toy, the wall or a blocked cell. Your goal is to move the \(k\)-th toy into the leftmost cell of the top row. The toys are numbered from \(1\) to \(n-2\) from left to right. Given \(n\) and \(k\), find a solution that uses at most \(1\,000\,000\) button presses.To test out the toy machine, a web page is available that lets you play the game in real time.
|
The first and only line contains two integers, \(n\) and \(k\) (\(5 \le n \le 100\,000\), \(n\) is odd, \(1 \le k \le n-2\)) β the number of cells in a row, and the index of the toy that has to be moved to the leftmost cell of the top row.
|
On a single line, output a description of the button presses as a string of at most \(1\,000\,000\) characters. The string should only contain the characters L, R, U, and D. The \(i\)-th character in the string is the \(i\)-th button that is pressed. After all the button presses are performed, the \(k\)-th toy should be in the leftmost cell of the top row.If there are multiple solutions, print any. The number of button presses does not have to be minimized.
|
In the first example, there will be \(5-2 = 3\) toys. The first toy needs to end up in the leftmost cell of the top row. The moves RDL will achieve this, see the picture for a better understanding. Another possible solution would be to do one button press L. Visualization of the moves for the first example.
|
Input: 5 1 | Output: RDL
|
Master
| 3 | 921 | 239 | 461 | 18 |
1,993 |
C
|
1993C
|
C. Light Switches
| 1,400 |
implementation; math
|
There is an apartment consisting of \(n\) rooms, each with its light initially turned off.To control the lights in these rooms, the owner of the apartment decided to install chips in the rooms so that each room has exactly one chip, and the chips are installed at different times. Specifically, these times are represented by the array \(a_1, a_2, \ldots, a_n\), where \(a_i\) is the time (in minutes) at which a chip is installed in the \(i\)-th room.As soon as a chip is installed, it changes the room's light status every \(k\) minutes β it turns on the light for \(k\) minutes, then turns it off for the next \(k\) minutes, then turns it back on for the next \(k\) minutes, and so on. In other words, the light status is changed by the chip at minute \(a_i\), \(a_i + k\), \(a_i + 2k\), \(a_i + 3k\), \(\ldots\) for the \(i\)-th room.What is the earliest moment when all rooms in the apartment have their lights turned on?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 2 \cdot 10^5\)) β the number of rooms in the apartment and the period of the chips.The second line contains \(n\) distinct integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the moments when the chips are installed.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, print a single integer β the answer to the question (in minutes). If there is no such moment that the lights are turned on in all the rooms, print \(-1\) instead.
|
In the first test case, all lights will be on by the minute \(5\) without any of them being turned off by the chips. The answer is \(5\).In the second test case, due to \(k=3\), the \(1\)-st light will be on at minutes \(2, 3, 4, 8, 9, 10, 14, \ldots\); meanwhile, the \(4\)-th light will be on at minutes \(5, 6, 7, 11, 12, 13, 17, \ldots\). These two sequences don't have any number in common, so they will never be on at the same time.In the third test case, it can be seen that the \(1\)-st and \(2\)-nd lights will be turned off at minutes \(6\) and \(7\), but the chips will turn them back on at minutes \(9\) and \(10\). The \(3\)-rd and \(4\)-th lights will also be on at minute \(10\), so the answer is \(10\).
|
Input: 94 42 3 4 54 32 3 4 54 33 4 8 93 36 2 11 117 514 34 6 25 46 7 176 540 80 99 60 90 506 564 40 50 68 70 102 11 1000000000 | Output: 5 -1 10 8 1 47 100 -1 -1
|
Easy
| 2 | 926 | 507 | 182 | 19 |
1,357 |
E2
|
1357E2
|
E2. Root of quantum Fourier transform
| 0 |
*special
|
Implement an operation that is equivalent to the operation QFT\(^{1/P}\), where QFT is the quantum Fourier transform. In other words, your operation, applied \(P\) times, should have the same effect as applying QFT. You can implement the required transformation up to a global phase.Your operation should take the following inputs: an integer \(P\) (\(2 \le P \le 8\)). a register of type LittleEndian - a wrapper type for an array of qubits that encodes an unsigned integer in little-endian format, with the least significant bit written first (corresponding to the array element with index 0). (If you need to, you can convert it to an array type using unwrap operator: let qubitArray = inputRegister!;.) The register will contain at most 7 qubits. The ""output"" of your solution is the state in which it left the input qubits.Your code should have the following signature (note that your operation should have Adjoint and Controlled variants defined for it; is Adj+Ctl in the operation signature will generate them automatically based on your code):namespace Solution { open Microsoft.Quantum.Arithmetic; open Microsoft.Quantum.Intrinsic; operation Solve (p : Int, inputRegister : LittleEndian) : Unit is Adj+Ctl { // your code here }}You can learn more about QFT in this kata. You are allowed to take advantage of library operations, including QFTLE.
|
Beginner
| 1 | 1,355 | 0 | 0 | 13 |
||||
1,353 |
A
|
1353A
|
A. Most Unstable Array
| 800 |
constructive algorithms; greedy; math
|
You are given two integers \(n\) and \(m\). You have to construct the array \(a\) of length \(n\) consisting of non-negative integers (i.e. integers greater than or equal to zero) such that the sum of elements of this array is exactly \(m\) and the value \(\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}|\) is the maximum possible. Recall that \(|x|\) is the absolute value of \(x\).In other words, you have to maximize the sum of absolute differences between adjacent (consecutive) elements. For example, if the array \(a=[1, 3, 2, 5, 5, 0]\) then the value above for this array is \(|1-3| + |3-2| + |2-5| + |5-5| + |5-0| = 2 + 1 + 3 + 0 + 5 = 11\). Note that this example doesn't show the optimal answer but it shows how the required value for some array is calculated.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. Then \(t\) test cases follow.The only line of the test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^9\)) β the length of the array and its sum correspondingly.
|
For each test case, print the answer β the maximum possible value of \(\sum\limits_{i=1}^{n-1} |a_i - a_{i+1}|\) for the array \(a\) consisting of \(n\) non-negative integers with the sum \(m\).
|
In the first test case of the example, the only possible array is \([100]\) and the answer is obviously \(0\).In the second test case of the example, one of the possible arrays is \([2, 0]\) and the answer is \(|2-0| = 2\).In the third test case of the example, one of the possible arrays is \([0, 2, 0, 3, 0]\) and the answer is \(|0-2| + |2-0| + |0-3| + |3-0| = 10\).
|
Input: 5 1 100 2 2 5 5 2 1000000000 1000000000 1000000000 | Output: 0 2 10 1000000000 2000000000
|
Beginner
| 3 | 811 | 284 | 194 | 13 |
509 |
D
|
509D
|
D. Restoring Numbers
| 2,200 |
constructive algorithms; math
|
Vasya had two arrays consisting of non-negative integers: a of size n and b of size m. Vasya chose a positive integer k and created an n Γ m matrix v using the following formula:Vasya wrote down matrix v on a piece of paper and put it in the table.A year later Vasya was cleaning his table when he found a piece of paper containing an n Γ m matrix w. He remembered making a matrix one day by the rules given above but he was not sure if he had found the paper with the matrix v from those days. Your task is to find out if the matrix w that you've found could have been obtained by following these rules and if it could, then for what numbers k, a1, a2, ..., an, b1, b2, ..., bm it is possible.
|
The first line contains integers n and m (1 β€ n, m β€ 100), separated by a space β the number of rows and columns in the found matrix, respectively. The i-th of the following lines contains numbers wi, 1, wi, 2, ..., wi, m (0 β€ wi, j β€ 109), separated by spaces β the elements of the i-th row of matrix w.
|
If the matrix w could not have been obtained in the manner described above, print ""NO"" (without quotes) in the single line of output.Otherwise, print four lines.In the first line print ""YES"" (without quotes).In the second line print an integer k (1 β€ k β€ 1018). Note that each element of table w should be in range between 0 and k - 1 inclusively.In the third line print n integers a1, a2, ..., an (0 β€ ai β€ 1018), separated by spaces.In the fourth line print m integers b1, b2, ..., bm (0 β€ bi β€ 1018), separated by spaces.
|
By we denote the remainder of integer division of b by c.It is guaranteed that if there exists some set of numbers k, a1, ..., an, b1, ..., bm, that you could use to make matrix w, then there also exists a set of numbers that meets the limits 1 β€ k β€ 1018, 1 β€ ai β€ 1018, 1 β€ bi β€ 1018 in the output format. In other words, these upper bounds are introduced only for checking convenience purposes.
|
Input: 2 31 2 32 3 4 | Output: YES10000000070 1 1 2 3
|
Hard
| 2 | 694 | 304 | 528 | 5 |
1,473 |
A
|
1473A
|
A. Replacing Elements
| 800 |
greedy; implementation; math; sortings
|
You have an array \(a_1, a_2, \dots, a_n\). All \(a_i\) are positive integers.In one step you can choose three distinct indices \(i\), \(j\), and \(k\) (\(i \neq j\); \(i \neq k\); \(j \neq k\)) and assign the sum of \(a_j\) and \(a_k\) to \(a_i\), i. e. make \(a_i = a_j + a_k\).Can you make all \(a_i\) lower or equal to \(d\) using the operation above any number of times (possibly, zero)?
|
The first line contains a single integer \(t\) (\(1 \le t \le 2000\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(d\) (\(3 \le n \le 100\); \(1 \le d \le 100\)) β the number of elements in the array \(a\) and the value \(d\).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 100\)) β the array \(a\).
|
For each test case, print YES, if it's possible to make all elements \(a_i\) less or equal than \(d\) using the operation above. Otherwise, print NO.You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer).
|
In the first test case, we can prove that we can't make all \(a_i \le 3\).In the second test case, all \(a_i\) are already less or equal than \(d = 4\).In the third test case, we can, for example, choose \(i = 5\), \(j = 1\), \(k = 2\) and make \(a_5 = a_1 + a_2 = 2 + 1 = 3\). Array \(a\) will become \([2, 1, 5, 3, 3]\).After that we can make \(a_3 = a_5 + a_2 = 3 + 1 = 4\). Array will become \([2, 1, 4, 3, 3]\) and all elements are less or equal than \(d = 4\).
|
Input: 3 5 3 2 3 2 5 4 3 4 2 4 4 5 4 2 1 5 3 6 | Output: NO YES YES
|
Beginner
| 4 | 392 | 382 | 263 | 14 |
543 |
E
|
543E
|
E. Listening to Music
| 3,200 |
constructive algorithms; data structures
|
Please note that the memory limit differs from the standard.You really love to listen to music. During the each of next s days you will listen to exactly m songs from the playlist that consists of exactly n songs. Let's number the songs from the playlist with numbers from 1 to n, inclusive. The quality of song number i is ai.On the i-th day you choose some integer v (li β€ v β€ ri) and listen to songs number v, v + 1, ..., v + m - 1. On the i-th day listening to one song with quality less than qi increases your displeasure by exactly one.Determine what minimum displeasure you can get on each of the s next days.
|
The first line contains two positive integers n, m (1 β€ m β€ n β€ 2Β·105). The second line contains n positive integers a1, a2, ..., an (0 β€ ai < 230) β the description of songs from the playlist. The next line contains a single number s (1 β€ s β€ 2Β·105) β the number of days that you consider.The next s lines contain three integers each li, ri, xi (1 β€ li β€ ri β€ n - m + 1; 0 β€ xi < 230) β the description of the parameters for the i-th day. In order to calculate value qi, you need to use formula: , where ansi is the answer to the problem for day i. Assume that ans0 = 0.
|
Print exactly s integers ans1, ans2, ..., anss, where ansi is the minimum displeasure that you can get on day i.
|
Input: 5 31 2 1 2 351 1 21 3 21 3 31 3 51 3 1 | Output: 20231
|
Master
| 2 | 616 | 571 | 112 | 5 |
|
273 |
E
|
273E
|
E. Dima and Game
| 2,600 |
dp; games
|
Dima and Anya love playing different games. Now Dima has imagined a new game that he wants to play with Anya.Dima writes n pairs of integers on a piece of paper (li, ri) (1 β€ li < ri β€ p). Then players take turns. On his turn the player can do the following actions: choose the number of the pair i (1 β€ i β€ n), such that ri - li > 2; replace pair number i by pair or by pair . Notation βxβ means rounding down to the closest integer. The player who can't make a move loses.Of course, Dima wants Anya, who will move first, to win. That's why Dima should write out such n pairs of integers (li, ri) (1 β€ li < ri β€ p), that if both players play optimally well, the first one wins. Count the number of ways in which Dima can do it. Print the remainder after dividing the answer by number 1000000007 (109 + 7).Two ways are considered distinct, if the ordered sequences of the written pairs are distinct.
|
The first line contains two integers n, p (1 β€ n β€ 1000, 1 β€ p β€ 109). The numbers are separated by a single space.
|
In a single line print the remainder after dividing the answer to the problem by number 1000000007 (109 + 7).
|
Input: 2 2 | Output: 0
|
Expert
| 2 | 899 | 115 | 109 | 2 |
|
2,119 |
C
|
2119C
|
C. A Good Problem
| 1,300 |
bitmasks; constructive algorithms; math
|
Juggernaut. - Lost Dream feat.ζεγ―γ You are given four positive integers \(n, l, r, k\). You need to find the lexicographically smallest\(^{\text{β}}\) array \(a\) of length \(n\), consisting of integers, such that: For every \(1 \leq i \leq n\), \(l \leq a_i \leq r\). \(a_1 \, \& \, a_2 \, \& \, \ldots \, \& \, a_n = a_1 \oplus a_2 \oplus \ldots \oplus a_n\), where \(\&\) denotes the bitwise AND operation and \(\oplus\) denotes the bitwise XOR operation. If no such array exists, output \(-1\). Otherwise, since the entire array might be too large to output, output \(a_k\) only.\(^{\text{β}}\)An array \(a\) is lexicographically smaller than an array \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a \ne b\); or in the first position where \(a\) and \(b\) differ, the array \(a\) has a smaller element than the corresponding element in \(b\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. Each test case contains four positive integers \(n,l,r,k\) (\(1 \le k \le n \le 10^{18}\), \(1 \le l \le r \le 10^{18}\)).
|
For each test case, output \(a_k\) or \(-1\) if no array meets the conditions.
|
In the first test case, the array \(a = [4]\). It can be proven that there is no array that meets the above requirements and has a smaller lexicographic order.In the second test case, the array \(a= [1,1,1]\). It can be proven that there is no array that meets the above requirements and has a smaller lexicographic order.In the third test case and the fourth test case, the array \(a = [6,6,8,8]\). It can be proven that there is no array that meets the above requirements and has a smaller lexicographic order.In the fifth test case and the sixth test case, it can be proven that there is no array that meets the above requirements.
|
Input: 91 4 4 13 1 3 34 6 9 24 6 9 34 6 7 42 5 5 12 3 6 2999999999999999999 1000000000000000000 1000000000000000000 9999999999999999991000000000000000000 1 999999999999999999 1000000000000000000 | Output: 4 1 6 8 -1 -1 -1 1000000000000000000 2
|
Easy
| 3 | 883 | 284 | 78 | 21 |
1,679 |
A
|
1679A
|
A. AvtoBus
| 900 |
brute force; greedy; math; number theory
|
Spring has come, and the management of the AvtoBus bus fleet has given the order to replace winter tires with summer tires on all buses.You own a small bus service business and you have just received an order to replace \(n\) tires. You know that the bus fleet owns two types of buses: with two axles (these buses have \(4\) wheels) and with three axles (these buses have \(6\) wheels).You don't know how many buses of which type the AvtoBus bus fleet owns, so you wonder how many buses the fleet might have. You have to determine the minimum and the maximum number of buses that can be in the fleet if you know that the total number of wheels for all buses is \(n\).
|
The first line contains an integer \(t\) (\(1 \le t \le 1\,000\)) β the number of test cases. The following lines contain description of test cases.The only line of each test case contains one integer \(n\) (\(1 \le n \le 10^{18}\)) β the total number of wheels for all buses.
|
For each test case print the answer in a single line using the following format.Print two integers \(x\) and \(y\) (\(1 \le x \le y\)) β the minimum and the maximum possible number of buses that can be in the bus fleet.If there is no suitable number of buses for the given \(n\), print the number \(-1\) as the answer.
|
In the first test case the total number of wheels is \(4\). It means that there is the only one bus with two axles in the bus fleet.In the second test case it's easy to show that there is no suitable number of buses with \(7\) wheels in total.In the third test case the total number of wheels is \(24\). The following options are possible: Four buses with three axles. Three buses with two axles and two buses with three axles. Six buses with two axles. So the minimum number of buses is \(4\) and the maximum number of buses is \(6\).
|
Input: 44724998244353998244352 | Output: 1 1 -1 4 6 166374058999707392 249561088499561088
|
Beginner
| 4 | 667 | 276 | 318 | 16 |
1,746 |
B
|
1746B
|
B. Rebellion
| 800 |
constructive algorithms; greedy; two pointers
|
You have an array \(a\) of size \(n\) consisting only of zeroes and ones. You can do the following operation: choose two indices \(1 \le i , j \le n\), \(i \ne j\), add \(a_{i}\) to \(a_{j}\), remove \(a_{i}\) from \(a\). Note that elements of \(a\) can become bigger than \(1\) after performing some operations. Also note that \(n\) becomes \(1\) less after the operation.What is the minimum number of operations needed to make \(a\) non-decreasing, i. e. that each element is not less than the previous element?
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains an integer \(n\) (\(1 \le n \le 10^5\)), the size of array \(a\).Next line contains \(n\) integers \(a_{1}, a_{2}, \ldots a_{n}\) (\(a_i\) is \(0\) or \(1\)), elements of array \(a\).It's guaranteed that sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case print a single integer, minimum number of operations needed to make \(a\) non-decreasing.
|
In the first test case, \(a\) is already non-decreasing, so you don't need to do any operations and the answer is \(0\).In the second test case, you can perform an operation for \(i = 1\) and \(j = 5\), so \(a\) will be equal to \([0, 0, 1, 2]\) and it becomes non-decreasing.In the third test case, you can perform an operation for \(i = 2\) and \(j = 1\), so \(a\) will be equal to \([1]\) and it becomes non-decreasing.
|
Input: 480 0 1 1 1 1 1 151 0 0 1 121 0111 1 0 0 1 0 0 1 1 1 0 | Output: 0 1 1 3
|
Beginner
| 3 | 513 | 472 | 108 | 17 |
1,633 |
E
|
1633E
|
E. Spanning Tree Queries
| 2,400 |
binary search; data structures; dfs and similar; dsu; graphs; greedy; math; sortings; trees
|
You are given a connected weighted undirected graph, consisting of \(n\) vertices and \(m\) edges.You are asked \(k\) queries about it. Each query consists of a single integer \(x\). For each query, you select a spanning tree in the graph. Let the weights of its edges be \(w_1, w_2, \dots, w_{n-1}\). The cost of a spanning tree is \(\sum \limits_{i=1}^{n-1} |w_i - x|\) (the sum of absolute differences between the weights and \(x\)). The answer to a query is the lowest cost of a spanning tree.The queries are given in a compressed format. The first \(p\) \((1 \le p \le k)\) queries \(q_1, q_2, \dots, q_p\) are provided explicitly. For queries from \(p+1\) to \(k\), \(q_j = (q_{j-1} \cdot a + b) \mod c\).Print the xor of answers to all queries.
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 50\); \(n - 1 \le m \le 300\)) β the number of vertices and the number of edges in the graph.Each of the next \(m\) lines contains a description of an undirected edge: three integers \(v\), \(u\) and \(w\) (\(1 \le v, u \le n\); \(v \neq u\); \(0 \le w \le 10^8\)) β the vertices the edge connects and its weight. Note that there might be multiple edges between a pair of vertices. The edges form a connected graph.The next line contains five integers \(p, k, a, b\) and \(c\) (\(1 \le p \le 10^5\); \(p \le k \le 10^7\); \(0 \le a, b \le 10^8\); \(1 \le c \le 10^8\)) β the number of queries provided explicitly, the total number of queries and parameters to generate the queries.The next line contains \(p\) integers \(q_1, q_2, \dots, q_p\) (\(0 \le q_j < c\)) β the first \(p\) queries.
|
Print a single integer β the xor of answers to all queries.
|
The queries in the first example are \(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0\). The answers are \(11, 9, 7, 3, 1, 5, 8, 7, 5, 7, 11\). The queries in the second example are \(3, 0, 2, 1, 6, 0, 3, 5, 4, 1\). The answers are \(14, 19, 15, 16, 11, 19, 14, 12, 13, 16\). The queries in the third example are \(75, 0, 0, \dots\). The answers are \(50, 150, 150, \dots\).
|
Input: 5 8 4 1 4 3 1 0 3 5 3 2 5 4 3 4 8 4 3 4 4 2 8 5 3 9 3 11 1 1 10 0 1 2 | Output: 4
|
Expert
| 9 | 751 | 858 | 59 | 16 |
1,869 |
A
|
1869A
|
A. Make It Zero
| 900 |
constructive algorithms
|
During Zhongkao examination, Reycloer met an interesting problem, but he cannot come up with a solution immediately. Time is running out! Please help him.Initially, you are given an array \(a\) consisting of \(n \ge 2\) integers, and you want to change all elements in it to \(0\).In one operation, you select two indices \(l\) and \(r\) (\(1\le l\le r\le n\)) and do the following: Let \(s=a_l\oplus a_{l+1}\oplus \ldots \oplus a_r\), where \(\oplus\) denotes the bitwise XOR operation; Then, for all \(l\le i\le r\), replace \(a_i\) with \(s\). You can use the operation above in any order at most \(8\) times in total.Find a sequence of operations, such that after performing the operations in order, all elements in \(a\) are equal to \(0\). It can be proven that the solution always exists.
|
The first line of input contains a single integer \(t\) (\(1\le t\le 500\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(2\le n\le 100\)) β the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(0\le a_i\le 100\)) β the elements of the array \(a\).
|
For each test case, in the first line output a single integer \(k\) (\(0\le k\le 8\)) β the number of operations you use.Then print \(k\) lines, in the \(i\)-th line output two integers \(l_i\) and \(r_i\) (\(1\le l_i\le r_i\le n\)) representing that you select \(l_i\) and \(r_i\) in the \(i\)-th operation. Note that you do not have to minimize \(k\). If there are multiple solutions, you may output any of them.
|
In the first test case, since \(1\oplus2\oplus3\oplus0=0\), after performing the operation on segment \([1,4]\), all the elements in the array are equal to \(0\).In the second test case, after the first operation, the array becomes equal to \([3,1,4,15,15,15,15,6]\), after the second operation, the array becomes equal to \([0,0,0,0,0,0,0,0]\).In the third test case: Operation\(a\) before\(a\) after\(1\)\([\underline{1,5},4,1,4,7]\)\(\rightarrow\)\([4,4,4,1,4,7]\)\(2\)\([4,4,\underline{4,1},4,7]\)\(\rightarrow\)\([4,4,5,5,4,7]\)\(3\)\([4,4,5,5,\underline{4,7}]\)\(\rightarrow\)\([4,4,5,5,3,3]\)\(4\)\([\underline{4,4,5},5,3,3]\)\(\rightarrow\)\([5,5,5,5,3,3]\)\(5\)\([5,5,5,\underline{5,3,3}]\)\(\rightarrow\)\([5,5,5,5,5,5]\)\(6\)\([\underline{5,5,5,5,5,5}]\)\(\rightarrow\)\([0,0,0,0,0,0]\) In the fourth test case, the initial array contains only \(0\), so we do not need to perform any operations with it.
|
Input: 641 2 3 083 1 4 1 5 9 2 661 5 4 1 4 750 0 0 0 071 1 9 9 0 1 83100 100 0 | Output: 1 1 4 2 4 7 1 8 6 1 2 3 4 5 6 1 3 4 6 1 6 0 4 1 2 6 7 3 4 6 7 1 1 2
|
Beginner
| 1 | 795 | 396 | 414 | 18 |
18 |
B
|
18B
|
B. Platforms
| 1,700 |
brute force; math
|
In one one-dimensional world there are n platforms. Platform with index k (platforms are numbered from 1) is a segment with coordinates [(k - 1)m, (k - 1)m + l], and l < m. Grasshopper Bob starts to jump along the platforms from point 0, with each jump he moves exactly d units right. Find out the coordinate of the point, where Bob will fall down. The grasshopper falls down, if he finds himself not on the platform, but if he finds himself on the edge of the platform, he doesn't fall down.
|
The first input line contains 4 integer numbers n, d, m, l (1 β€ n, d, m, l β€ 106, l < m) β respectively: amount of platforms, length of the grasshopper Bob's jump, and numbers m and l needed to find coordinates of the k-th platform: [(k - 1)m, (k - 1)m + l].
|
Output the coordinates of the point, where the grosshopper will fall down. Don't forget that if Bob finds himself on the platform edge, he doesn't fall down.
|
Input: 2 2 5 3 | Output: 4
|
Medium
| 2 | 492 | 258 | 157 | 0 |
|
1,313 |
A
|
1313A
|
A. Fast Food Restaurant
| 900 |
brute force; greedy; implementation
|
Tired of boring office work, Denis decided to open a fast food restaurant.On the first day he made \(a\) portions of dumplings, \(b\) portions of cranberry juice and \(c\) pancakes with condensed milk.The peculiarity of Denis's restaurant is the procedure of ordering food. For each visitor Denis himself chooses a set of dishes that this visitor will receive. When doing so, Denis is guided by the following rules: every visitor should receive at least one dish (dumplings, cranberry juice, pancakes with condensed milk are all considered to be dishes); each visitor should receive no more than one portion of dumplings, no more than one portion of cranberry juice and no more than one pancake with condensed milk; all visitors should receive different sets of dishes. What is the maximum number of visitors Denis can feed?
|
The first line contains an integer \(t\) (\(1 \le t \le 500\)) β the number of test cases to solve.Each of the remaining \(t\) lines contains integers \(a\), \(b\) and \(c\) (\(0 \leq a, b, c \leq 10\)) β the number of portions of dumplings, the number of portions of cranberry juice and the number of condensed milk pancakes Denis made.
|
For each test case print a single integer β the maximum number of visitors Denis can feed.
|
In the first test case of the example, Denis can feed the first visitor with dumplings, give the second a portion of cranberry juice, and give the third visitor a portion of cranberry juice and a pancake with a condensed milk.In the second test case of the example, the restaurant Denis is not very promising: he can serve no customers.In the third test case of the example, Denise can serve four visitors. The first guest will receive a full lunch of dumplings, a portion of cranberry juice and a pancake with condensed milk. The second visitor will get only dumplings. The third guest will receive a pancake with condensed milk, and the fourth guest will receive a pancake and a portion of dumplings. Please note that Denis hasn't used all of the prepared products, but is unable to serve more visitors.
|
Input: 71 2 10 0 09 1 72 2 32 3 23 2 24 4 4 | Output: 3045557
|
Beginner
| 3 | 824 | 337 | 90 | 13 |
400 |
A
|
400A
|
A. Inna and Choose Options
| 1,000 |
implementation
|
There always is something to choose from! And now, instead of ""Noughts and Crosses"", Inna choose a very unusual upgrade of this game. The rules of the game are given below:There is one person playing the game. Before the beginning of the game he puts 12 cards in a row on the table. Each card contains a character: ""X"" or ""O"". Then the player chooses two positive integers a and b (aΒ·b = 12), after that he makes a table of size a Γ b from the cards he put on the table as follows: the first b cards form the first row of the table, the second b cards form the second row of the table and so on, the last b cards form the last (number a) row of the table. The player wins if some column of the table contain characters ""X"" on all cards. Otherwise, the player loses.Inna has already put 12 cards on the table in a row. But unfortunately, she doesn't know what numbers a and b to choose. Help her win the game: print to her all the possible ways of numbers a, b that she can choose and win.
|
The first line of the input contains integer t (1 β€ t β€ 100). This value shows the number of sets of test data in the input. Next follows the description of each of the t tests on a separate line.The description of each test is a string consisting of 12 characters, each character is either ""X"", or ""O"". The i-th character of the string shows the character that is written on the i-th card from the start.
|
For each test, print the answer to the test on a single line. The first number in the line must represent the number of distinct ways to choose the pair a, b. Next, print on this line the pairs in the format axb. Print the pairs in the order of increasing first parameter (a). Separate the pairs in the line by whitespaces.
|
Input: 4OXXXOXOOXOOXOXOXOXOXOXOXXXXXXXXXXXXXOOOOOOOOOOOO | Output: 3 1x12 2x6 4x34 1x12 2x6 3x4 6x26 1x12 2x6 3x4 4x3 6x2 12x10
|
Beginner
| 1 | 996 | 409 | 323 | 4 |
|
814 |
E
|
814E
|
E. An unavoidable detour for home
| 2,600 |
combinatorics; dp; graphs; shortest paths
|
Those unwilling to return home from a long journey, will be affected by the oddity of the snail and lose their way. Mayoi, the oddity's carrier, wouldn't like this to happen, but there's nothing to do with this before a cure is figured out. For now, she would only like to know the enormous number of possibilities to be faced with if someone gets lost.There are n towns in the region, numbered from 1 to n. The town numbered 1 is called the capital. The traffic network is formed by bidirectional roads connecting pairs of towns. No two roads connect the same pair of towns, and no road connects a town with itself. The time needed to travel through each of the roads is the same. Lost travelers will not be able to find out how the towns are connected, but the residents can help them by providing the following facts: Starting from each town other than the capital, the shortest path (i.e. the path passing through the minimum number of roads) to the capital exists, and is unique; Let li be the number of roads on the shortest path from town i to the capital, then li β₯ li - 1 holds for all 2 β€ i β€ n; For town i, the number of roads connected to it is denoted by di, which equals either 2 or 3. You are to count the number of different ways in which the towns are connected, and give the answer modulo 109 + 7. Two ways of connecting towns are considered different if a pair (u, v) (1 β€ u, v β€ n) exists such there is a road between towns u and v in one of them but not in the other.
|
The first line of input contains a positive integer n (3 β€ n β€ 50) β the number of towns.The second line contains n space-separated integers d1, d2, ..., dn (2 β€ di β€ 3) β the number of roads connected to towns 1, 2, ..., n, respectively. It is guaranteed that the sum of di over all i is even.
|
Output one integer β the total number of different possible ways in which the towns are connected, modulo 109 + 7.
|
In the first example, the following structure is the only one to satisfy the constraints, the distances from towns 2, 3, 4 to the capital are all 1. In the second example, the following two structures satisfy the constraints.
|
Input: 43 2 3 2 | Output: 1
|
Expert
| 4 | 1,488 | 294 | 114 | 8 |
847 |
J
|
847J
|
J. Students Initiation
| 2,400 |
binary search; flows; graphs
|
Soon the first year students will be initiated into students at the University of Berland. The organizers of the initiation come up with a program for this holiday. In their opinion, it would be good if the first-year students presented small souvenirs to each other. When they voiced this idea to the first-year students, they found out the following: some pairs of the new students already know each other; each new student agrees to give souvenirs only to those with whom they are already familiar; each new student does not want to present too many souvenirs. The organizers have written down all the pairs of first-year friends who are familiar with each other and now want to determine for each new student, whom they should give souvenirs to. In their opinion, in each pair of familiar students exactly one student must present a souvenir to another student.First year students already decided to call the unluckiest the one who will have to present the greatest number of souvenirs. The organizers in return promised that the unluckiest will be unlucky to the minimum possible degree: of course, they will have to present the greatest number of souvenirs compared to the other students, but this number will be as small as possible.Organizers are very busy, and they asked you to determine for each pair of first-year friends who and to whom should present a souvenir.
|
The first line contains two integers n and m (1 β€ n β€ 5000, 0 β€ m β€ min(5000, nΒ·(n - 1) / 2)) β the number of the first year students and the number of pairs of the students that know each other. The students are numbered from 1 to n.Each of the following m lines contains two integers xi, yi (1 β€ xi, yi β€ n, xi β yi) β the students in each pair.It is guaranteed that each pair is present in the list exactly once. It is also guaranteed that if there is a pair (xi, yi) in the list, then there is no pair (yi, xi).
|
Print a single integer into the first line β the smallest number of souvenirs that the unluckiest student will have to present.Following should be m lines, each containing two integers β the students which are familiar with each other. The first number in the pair must be the student that will present the souvenir to the second student in the pair.Pairs can be printed in any order. If there are many solutions, print any of them.
|
Input: 5 42 11 32 32 5 | Output: 11 22 33 15 2
|
Expert
| 3 | 1,376 | 515 | 432 | 8 |
|
1,107 |
G
|
1107G
|
G. Vasya and Maximum Profit
| 2,400 |
binary search; constructive algorithms; data structures; dp; dsu
|
Vasya got really tired of these credits (from problem F) and now wants to earn the money himself! He decided to make a contest to gain a profit.Vasya has \(n\) problems to choose from. They are numbered from \(1\) to \(n\). The difficulty of the \(i\)-th problem is \(d_i\). Moreover, the problems are given in the increasing order by their difficulties. The difficulties of all tasks are pairwise distinct. In order to add the \(i\)-th problem to the contest you need to pay \(c_i\) burles to its author. For each problem in the contest Vasya gets \(a\) burles.In order to create a contest he needs to choose a consecutive subsegment of tasks.So the total earnings for the contest are calculated as follows: if Vasya takes problem \(i\) to the contest, he needs to pay \(c_i\) to its author; for each problem in the contest Vasya gets \(a\) burles; let \(gap(l, r) = \max\limits_{l \le i < r} (d_{i + 1} - d_i)^2\). If Vasya takes all the tasks with indices from \(l\) to \(r\) to the contest, he also needs to pay \(gap(l, r)\). If \(l = r\) then \(gap(l, r) = 0\). Calculate the maximum profit that Vasya can earn by taking a consecutive segment of tasks.
|
The first line contains two integers \(n\) and \(a\) (\(1 \le n \le 3 \cdot 10^5\), \(1 \le a \le 10^9\)) β the number of proposed tasks and the profit for a single problem, respectively.Each of the next \(n\) lines contains two integers \(d_i\) and \(c_i\) (\(1 \le d_i, c_i \le 10^9, d_i < d_{i+1}\)).
|
Print one integer β maximum amount of burles Vasya can earn.
|
Input: 5 10 1 15 5 3 6 11 7 2 11 22 | Output: 13
|
Expert
| 5 | 1,158 | 303 | 60 | 11 |
|
1,684 |
F
|
1684F
|
F. Diverse Segments
| 2,600 |
data structures; two pointers
|
You are given an array \(a\) of \(n\) integers. Also you are given \(m\) subsegments of that array. The left and the right endpoints of the \(j\)-th segment are \(l_j\) and \(r_j\) respectively.You are allowed to make no more than one operation. In that operation you choose any subsegment of the array \(a\) and replace each value on this segment with any integer (you are also allowed to keep elements the same).You have to apply this operation so that for the given \(m\) segments, the elements on each segment are distinct. More formally, for each \(1 \le j \le m\) all elements \(a_{l_{j}}, a_{l_{j}+1}, \ldots, a_{r_{j}-1}, a_{r_{j}}\) should be distinct.You don't want to use the operation on a big segment, so you have to find the smallest length of a segment, so that you can apply the operation to this segment and meet the above-mentioned conditions. If it is not needed to use this operation, the answer is \(0\).
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 2 \cdot 10^5\)) β the size of the array and the number of segments respectively.The next line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β the elements of \(a\).Each of the next \(m\) lines contains two integers \(l_j\), \(r_j\) (\(1 \le l_j \le r_j \le n\)) β the left and the right endpoints of the \(j\)-th segment.It's guaranteed that the sum of \(n\) and the sum of \(m\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case output a single integer β the smallest length of a segment you can apply an operation on making the elements on all given segments distinct. If it is not needed to use the operation, output \(0\).
|
In the first test case you can perform the operation on the segment \([1, 2]\) and make \(a = [5, 6, 2, 1, 3, 3, 5]\). Then the elements on each segment are distinct. On the segment \([1, 4]\) there are \([5, 6, 2, 1]\). On the segment \([4, 5]\) there are \([1, 3]\). On the segment \([2, 4]\) there are \([6, 2, 1, 3]\). This way on each of the given segments all elements are distinct. Also, it is impossible to change any single integer to make elements distinct on each segment. That is why the answer is \(2\).In the second test case the elements on each segment are already distinct, so we should not perform the operation.In the third test case we can replace the first \(5\) by \(1\). This way we will get \([1, 7, 5, 6]\) where all elements are distinct so on each given segment all elements are distinct.
|
Input: 57 31 1 2 1 3 3 51 44 52 45 210 1 6 14 14 52 44 55 7 5 62 21 32 43 33 47 32 2 2 7 8 2 24 44 45 51 11231 1 | Output: 2 0 1 0 0
|
Expert
| 2 | 925 | 729 | 215 | 16 |
1,505 |
C
|
1505C
|
C. Fibonacci Words
| 1,400 |
*special; implementation
|
The input consists of a single string of uppercase letters A-Z. The length of the string is between 1 and 10 characters, inclusive.
|
Output ""YES"" or ""NO"".
|
Input: HELP | Output: YES
|
Easy
| 2 | 0 | 131 | 25 | 15 |
||
47 |
B
|
47B
|
B. Coins
| 1,200 |
implementation
|
One day Vasya came across three Berland coins. They didn't have any numbers that's why Vasya didn't understand how their denominations differ. He supposed that if one coin is heavier than the other one, then it should be worth more. Vasya weighed all the three pairs of coins on pan balance scales and told you the results. Find out how the deminations of the coins differ or if Vasya has a mistake in the weighting results. No two coins are equal.
|
The input data contains the results of all the weighting, one result on each line. It is guaranteed that every coin pair was weighted exactly once. Vasya labelled the coins with letters Β«AΒ», Β«BΒ» and Β«CΒ». Each result is a line that appears as (letter)(> or < sign)(letter). For example, if coin ""A"" proved lighter than coin ""B"", the result of the weighting is A<B.
|
It the results are contradictory, print Impossible. Otherwise, print without spaces the rearrangement of letters Β«AΒ», Β«BΒ» and Β«CΒ» which represent the coins in the increasing order of their weights.
|
Input: A>BC<BA>C | Output: CBA
|
Easy
| 1 | 448 | 367 | 197 | 0 |
|
1,391 |
D
|
1391D
|
D. 505
| 2,000 |
bitmasks; brute force; constructive algorithms; dp; greedy; implementation
|
A binary matrix is called good if every even length square sub-matrix has an odd number of ones. Given a binary matrix \(a\) consisting of \(n\) rows and \(m\) columns, determine the minimum number of cells you need to change to make it good, or report that there is no way to make it good at all. All the terms above have their usual meanings β refer to the Notes section for their formal definitions.
|
The first line of input contains two integers \(n\) and \(m\) (\(1 \leq n \leq m \leq 10^6\) and \(n\cdot m \leq 10^6\)) β the number of rows and columns in \(a\), respectively. The following \(n\) lines each contain \(m\) characters, each of which is one of 0 and 1. If the \(j\)-th character on the \(i\)-th line is 1, then \(a_{i,j} = 1\). Similarly, if the \(j\)-th character on the \(i\)-th line is 0, then \(a_{i,j} = 0\).
|
Output the minimum number of cells you need to change to make \(a\) good, or output \(-1\) if it's not possible at all.
|
In the first case, changing \(a_{1,1}\) to \(0\) and \(a_{2,2}\) to \(1\) is enough. You can verify that there is no way to make the matrix in the second case good. Some definitions β A binary matrix is one in which every element is either \(1\) or \(0\). A sub-matrix is described by \(4\) parameters β \(r_1\), \(r_2\), \(c_1\), and \(c_2\); here, \(1 \leq r_1 \leq r_2 \leq n\) and \(1 \leq c_1 \leq c_2 \leq m\). This sub-matrix contains all elements \(a_{i,j}\) that satisfy both \(r_1 \leq i \leq r_2\) and \(c_1 \leq j \leq c_2\). A sub-matrix is, further, called an even length square if \(r_2-r_1 = c_2-c_1\) and \(r_2-r_1+1\) is divisible by \(2\).
|
Input: 3 3 101 001 110 | Output: 2
|
Hard
| 6 | 402 | 428 | 119 | 13 |
852 |
A
|
852A
|
A. Digits
| 2,500 |
brute force; implementation; math
|
John gave Jack a very hard problem. He wrote a very big positive integer A0 on a piece of paper. The number is less than 10200000 . In each step, Jack is allowed to put ' + ' signs in between some of the digits (maybe none) of the current number and calculate the sum of the expression. He can perform the same procedure on that sum and so on. The resulting sums can be labeled respectively by A1, A2 etc. His task is to get to a single digit number.The problem is that there is not much blank space on the paper. There are only three lines of space, so he can't perform more than three steps. Since he wants to fill up the paper completely, he will perform exactly three steps.Jack must not add leading zeros to intermediate results, but he can put ' + ' signs in front of digit 0. For example, if the current number is 1000100, 10 + 001 + 00 is a valid step, resulting in number 11.
|
First line contains a positive integer N (1 β€ N β€ 200000), representing the number of digits of A0.Second line contains a string of length N representing positive integer number A0. Each character is digit. There will be no leading zeros.
|
Output exactly three lines, the steps Jack needs to perform to solve the problem. You can output any sequence of steps which results in a single digit number (and is logically consistent).Every step consists of digits and ' + ' signs. Steps should not contain several ' + ' signs in a row, whitespaces, or ' + ' signs as the first or last character. They also need to be arithmetically consistent.Solution might not be unique. Output any of them in that case.
|
In the first sample, Jack can't put ' + ' signs anywhere, so he just writes 1 in each line and solves the problem. Here, solution is unique.In the second sample, Jack first puts ' + ' between every two consecutive digits, thus getting the result 5 + 8 + 0 + 6 = 19. He does the same on the second step, getting 1 + 9 = 10. Once more, he gets 1 + 0 = 1, so after three steps, the result is 1 and his solution is correct.
|
Input: 11 | Output: 111
|
Expert
| 3 | 884 | 238 | 459 | 8 |
1,141 |
F1
|
1141F1
|
F1. Same Sum Blocks (Easy)
| 1,900 |
greedy
|
This problem is given in two editions, which differ exclusively in the constraints on the number \(n\).You are given an array of integers \(a[1], a[2], \dots, a[n].\) A block is a sequence of contiguous (consecutive) elements \(a[l], a[l+1], \dots, a[r]\) (\(1 \le l \le r \le n\)). Thus, a block is defined by a pair of indices \((l, r)\).Find a set of blocks \((l_1, r_1), (l_2, r_2), \dots, (l_k, r_k)\) such that: They do not intersect (i.e. they are disjoint). Formally, for each pair of blocks \((l_i, r_i)\) and \((l_j, r_j\)) where \(i \neq j\) either \(r_i < l_j\) or \(r_j < l_i\). For each block the sum of its elements is the same. Formally, $$$\(a[l_1]+a[l_1+1]+\dots+a[r_1]=a[l_2]+a[l_2+1]+\dots+a[r_2]=\)\( \)\(\dots =\)\( \)\(a[l_k]+a[l_k+1]+\dots+a[r_k].\)\( The number of the blocks in the set is maximum. Formally, there does not exist a set of blocks \)(l_1', r_1'), (l_2', r_2'), \dots, (l_{k'}', r_{k'}')\( satisfying the above two requirements with \)k' > k$$$. The picture corresponds to the first example. Blue boxes illustrate blocks. Write a program to find such a set of blocks.
|
The first line contains integer \(n\) (\(1 \le n \le 50\)) β the length of the given array. The second line contains the sequence of elements \(a[1], a[2], \dots, a[n]\) (\(-10^5 \le a_i \le 10^5\)).
|
In the first line print the integer \(k\) (\(1 \le k \le n\)). The following \(k\) lines should contain blocks, one per line. In each line print a pair of indices \(l_i, r_i\) (\(1 \le l_i \le r_i \le n\)) β the bounds of the \(i\)-th block. You can print blocks in any order. If there are multiple answers, print any of them.
|
Input: 7 4 1 2 2 1 5 3 | Output: 3 7 7 2 3 4 5
|
Hard
| 1 | 1,106 | 199 | 326 | 11 |
|
2,006 |
C
|
2006C
|
C. Eri and Expanded Sets
| 2,300 |
data structures; divide and conquer; math; number theory; two pointers
|
Let there be a set that contains distinct positive integers. To expand the set to contain as many integers as possible, Eri can choose two integers \(x\neq y\) from the set such that their average \(\frac{x+y}2\) is still a positive integer and isn't contained in the set, and add it to the set. The integers \(x\) and \(y\) remain in the set.Let's call the set of integers consecutive if, after the elements are sorted, the difference between any pair of adjacent elements is \(1\). For example, sets \(\{2\}\), \(\{2, 5, 4, 3\}\), \(\{5, 6, 8, 7\}\) are consecutive, while \(\{2, 4, 5, 6\}\), \(\{9, 7\}\) are not.Eri likes consecutive sets. Suppose there is an array \(b\), then Eri puts all elements in \(b\) into the set. If after a finite number of operations described above, the set can become consecutive, the array \(b\) will be called brilliant.Note that if the same integer appears in the array multiple times, we only put it into the set once, as a set always contains distinct positive integers.Eri has an array \(a\) of \(n\) positive integers. Please help him to count the number of pairs of integers \((l,r)\) such that \(1 \leq l \leq r \leq n\) and the subarray \(a_l, a_{l+1}, \ldots, a_r\) is brilliant.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 4 \cdot 10^5\)) β length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots a_n\) (\(1 \leq a_i \leq 10^9\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(4 \cdot 10^5\).
|
For each test case, output a single integer β the number of brilliant subarrays.
|
In the first test case, the array \(a = [2, 2]\) has \(3\) subarrays: \([2]\), \([2]\), and \([2, 2]\). For all of them, the set only contains a single integer \(2\), therefore it's always consecutive. All these subarrays are brilliant, so the answer is \(3\).In the second test case, let's consider the subarray \([3, 6, 10]\). We can do operations as follows: $$$\(\{3,6,10\} \xrightarrow{x=6,y=10} \{3,6,8,10\} \xrightarrow{x=6,y=8} \{3,6,7,8,10\} \xrightarrow{x=3,y=7} \{3,5,6,7,8,10\}\)\( \)\(\xrightarrow{x=3,y=5} \{3,4,5,6,7,8,10\} \xrightarrow{x=8,y=10} \{3,4,5,6,7,8,9,10\}\)\( \)\{3,4,5,6,7,8,9,10\}\( is a consecutive set, so the subarray \)[3, 6, 10]$$$ is brilliant.
|
Input: 622 261 3 6 10 15 2156 30 18 36 91100000000061 1 4 5 1 41270 130 90 90 90 108 612 500 451 171 193 193 | Output: 3 18 5 1 18 53
|
Expert
| 5 | 1,224 | 546 | 80 | 20 |
33 |
B
|
33B
|
B. String Problem
| 1,800 |
shortest paths
|
Boy Valera likes strings. And even more he likes them, when they are identical. That's why in his spare time Valera plays the following game. He takes any two strings, consisting of lower case Latin letters, and tries to make them identical. According to the game rules, with each move Valera can change one arbitrary character Ai in one of the strings into arbitrary character Bi, but he has to pay for every move a particular sum of money, equal to Wi. He is allowed to make as many moves as he needs. Since Valera is a very economical boy and never wastes his money, he asked you, an experienced programmer, to help him answer the question: what minimum amount of money should Valera have to get identical strings.
|
The first input line contains two initial non-empty strings s and t, consisting of lower case Latin letters. The length of each string doesn't exceed 105. The following line contains integer n (0 β€ n β€ 500) β amount of possible changings. Then follow n lines, each containing characters Ai and Bi (lower case Latin letters) and integer Wi (0 β€ Wi β€ 100), saying that it's allowed to change character Ai into character Bi in any of the strings and spend sum of money Wi.
|
If the answer exists, output the answer to the problem, and the resulting string. Otherwise output -1 in the only line. If the answer is not unique, output any.
|
Input: uayduxxd3a x 8x y 13d c 3 | Output: 21uxyd
|
Medium
| 1 | 717 | 469 | 160 | 0 |
|
1,901 |
D
|
1901D
|
D. Yet Another Monster Fight
| 1,700 |
binary search; dp; greedy; implementation; math
|
Vasya is a sorcerer that fights monsters. Again. There are \(n\) monsters standing in a row, the amount of health points of the \(i\)-th monster is \(a_i\).Vasya is a very powerful sorcerer who knows many overpowered spells. In this fight, he decided to use a chain lightning spell to defeat all the monsters. Let's see how this spell works.Firstly, Vasya chooses an index \(i\) of some monster (\(1 \le i \le n\)) and the initial power of the spell \(x\). Then the spell hits monsters exactly \(n\) times, one hit per monster. The first target of the spell is always the monster \(i\). For every target except for the first one, the chain lightning will choose a random monster who was not hit by the spell and is adjacent to one of the monsters that already was hit. So, each monster will be hit exactly once. The first monster hit by the spell receives \(x\) damage, the second monster receives \((x-1)\) damage, the third receives \((x-2)\) damage, and so on.Vasya wants to show how powerful he is, so he wants to kill all the monsters with a single chain lightning spell. The monster is considered dead if the damage he received is not less than the amount of its health points. On the other hand, Vasya wants to show he doesn't care that much, so he wants to choose the minimum initial power of the spell \(x\) such that it kills all monsters, no matter which monster (among those who can get hit) gets hit on each step.Of course, Vasya is a sorcerer, but the amount of calculations required to determine the optimal spell setup is way above his possibilities, so you have to help him find the minimum spell power required to kill all the monsters.Note that Vasya chooses the initial target and the power of the spell, other things should be considered random and Vasya wants to kill all the monsters even in the worst possible scenario.
|
The first line of the input contains one integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the number of monsters.The second line of the input contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the amount of health points of the \(i\)-th monster.
|
Print one integer β the minimum spell power required to kill all the monsters if Vasya chooses the first target optimally, and the order of spell hits can be any possible within the given rules.
|
Input: 6 2 1 5 6 4 3 | Output: 8
|
Medium
| 5 | 1,843 | 285 | 194 | 19 |
|
1,348 |
E
|
1348E
|
E. Phoenix and Berries
| 2,400 |
brute force; dp; greedy; math
|
Phoenix is picking berries in his backyard. There are \(n\) shrubs, and each shrub has \(a_i\) red berries and \(b_i\) blue berries.Each basket can contain \(k\) berries. But, Phoenix has decided that each basket may only contain berries from the same shrub or berries of the same color (red or blue). In other words, all berries in a basket must be from the same shrub or/and have the same color.For example, if there are two shrubs with \(5\) red and \(2\) blue berries in the first shrub and \(2\) red and \(1\) blue berries in the second shrub then Phoenix can fill \(2\) baskets of capacity \(4\) completely: the first basket will contain \(3\) red and \(1\) blue berries from the first shrub; the second basket will contain the \(2\) remaining red berries from the first shrub and \(2\) red berries from the second shrub. Help Phoenix determine the maximum number of baskets he can fill completely!
|
The first line contains two integers \(n\) and \(k\) (\( 1\le n, k \le 500\)) β the number of shrubs and the basket capacity, respectively.The \(i\)-th of the next \(n\) lines contain two integers \(a_i\) and \(b_i\) (\(0 \le a_i, b_i \le 10^9\)) β the number of red and blue berries in the \(i\)-th shrub, respectively.
|
Output one integer β the maximum number of baskets that Phoenix can fill completely.
|
The first example is described above.In the second example, Phoenix can fill one basket fully using all the berries from the first (and only) shrub.In the third example, Phoenix cannot fill any basket completely because there are less than \(5\) berries in each shrub, less than \(5\) total red berries, and less than \(5\) total blue berries.In the fourth example, Phoenix can put all the red berries into baskets, leaving an extra blue berry behind.
|
Input: 2 4 5 2 2 1 | Output: 2
|
Expert
| 4 | 904 | 320 | 84 | 13 |
1,434 |
E
|
1434E
|
E. A Convex Game
| 3,500 |
dsu; games
|
Shikamaru and Asuma like to play different games, and sometimes they play the following: given an increasing list of numbers, they take turns to move. Each move consists of picking a number from the list.Assume the picked numbers are \(v_{i_1}\), \(v_{i_2}\), \(\ldots\), \(v_{i_k}\). The following conditions must hold: \(i_{j} < i_{j+1}\) for all \(1 \leq j \leq k-1\); \(v_{i_{j+1}} - v_{i_j} < v_{i_{j+2}} - v_{i_{j+1}}\) for all \(1 \leq j \leq k-2\). However, it's easy to play only one instance of game, so today Shikamaru and Asuma decided to play \(n\) simultaneous games. They agreed on taking turns as for just one game, Shikamaru goes first. At each turn, the player performs a valid move in any single game. The player who cannot move loses. Find out who wins, provided that both play optimally.
|
The first line contains the only integer \(n\) (\(1 \leq n \leq 1000\)) standing for the number of games Shikamaru and Asuma play at once. Next lines describe the games.Each description starts from a line with the only number \(m\) (\(m\geq 1\)) denoting the length of the number list. The second line contains the increasing space-separated sequence \(v_1\), \(v_2\), ..., \(v_m\) from the game (\(1 \leq v_{1} < v_{2} < ... < v_{m} \leq 10^{5}\)).The total length of all sequences doesn't exceed \(10^5\).
|
Print ""YES"" if Shikamaru can secure the victory, and ""NO"" otherwise.
|
In the first example Shikamaru can pick the last number, and Asuma cannot do anything because of the first constraint.In the second sample test Asuma can follow the symmetric strategy, repeating Shikamaru's moves in the other instance each time, and therefore win.
|
Input: 1 10 1 2 3 4 5 6 7 8 9 10 | Output: YES
|
Master
| 2 | 808 | 507 | 72 | 14 |
1,932 |
C
|
1932C
|
C. LR-remainders
| 1,400 |
brute force; data structures; implementation; math; two pointers
|
You are given an array \(a\) of length \(n\), a positive integer \(m\), and a string of commands of length \(n\). Each command is either the character 'L' or the character 'R'.Process all \(n\) commands in the order they are written in the string \(s\). Processing a command is done as follows: First, output the remainder of the product of all elements of the array \(a\) when divided by \(m\). Then, if the command is 'L', remove the leftmost element from the array \(a\), if the command is 'R', remove the rightmost element from the array \(a\). Note that after each move, the length of the array \(a\) decreases by \(1\), and after processing all commands, it will be empty.Write a program that will process all commands in the order they are written in the string \(s\) (from left to right).
|
The first line contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input. Then descriptions of \(t\) test cases follow.Each test case of the input is given by three lines.The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2\cdot10^5, 1 \le m \le 10^4\)) β the initial length of the array \(a\) and the value to take the remainder by.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^4\)) β the elements of the array \(a\).The third line contains a string \(s\) consisting of \(n\) characters 'L' and 'R'.It is guaranteed that the sum of the values of \(n\) for all test cases in a test does not exceed \(2\cdot10^5\).
|
For each test case, output \(n\) integers \(b_1, b_2, \dots, b_n\), where \(b_i\) is the remainder when dividing the product of all elements of the current state of the array \(a\) by \(m\) at the beginning of the execution of the \(i\)-th command.
|
In the first test case of the example: \(3 \cdot 1 \cdot 4 \cdot 2 \bmod 6 = 24 \bmod 6 = 0\); \(s_1 = \text{L}\), so we remove the first element and get the array \([1, 4, 2]\); \(1 \cdot 4 \cdot 2 \bmod 6 = 8 \bmod 6 = 2\); \(s_2 = \text{R}\), so we remove the last element and get the array \([1, 4]\); \(1 \cdot 4 \bmod 6 = 4 \bmod 6 = 4\); \(s_3 = \text{R}\), so we remove the last element and get the array \([1]\); \(1 \bmod 6 = 1\); \(s_4 = \text{L}\), so we remove the first element and get an empty array.
|
Input: 44 63 1 4 2LRRL5 11 1 1 1 1LLLLL6 81 2 3 4 5 6RLLLRR1 1000010000R | Output: 0 2 4 1 0 0 0 0 0 0 0 0 4 4 4 0
|
Easy
| 5 | 796 | 700 | 248 | 19 |
1,359 |
C
|
1359C
|
C. Mixing Water
| 1,700 |
binary search; math
|
There are two infinite sources of water: hot water of temperature \(h\); cold water of temperature \(c\) (\(c < h\)). You perform the following procedure of alternating moves: take one cup of the hot water and pour it into an infinitely deep barrel; take one cup of the cold water and pour it into an infinitely deep barrel; take one cup of the hot water \(\dots\) and so on \(\dots\) Note that you always start with the cup of hot water.The barrel is initially empty. You have to pour at least one cup into the barrel. The water temperature in the barrel is an average of the temperatures of the poured cups.You want to achieve a temperature as close as possible to \(t\). So if the temperature in the barrel is \(t_b\), then the absolute difference of \(t_b\) and \(t\) (\(|t_b - t|\)) should be as small as possible.How many cups should you pour into the barrel, so that the temperature in it is as close as possible to \(t\)? If there are multiple answers with the minimum absolute difference, then print the smallest of them.
|
The first line contains a single integer \(T\) (\(1 \le T \le 3 \cdot 10^4\)) β the number of testcases.Each of the next \(T\) lines contains three integers \(h\), \(c\) and \(t\) (\(1 \le c < h \le 10^6\); \(c \le t \le h\)) β the temperature of the hot water, the temperature of the cold water and the desired temperature in the barrel.
|
For each testcase print a single positive integer β the minimum number of cups required to be poured into the barrel to achieve the closest temperature to \(t\).
|
In the first testcase the temperature after \(2\) poured cups: \(1\) hot and \(1\) cold is exactly \(20\). So that is the closest we can achieve.In the second testcase the temperature after \(7\) poured cups: \(4\) hot and \(3\) cold is about \(29.857\). Pouring more water won't get us closer to \(t\) than that.In the third testcase the temperature after \(1\) poured cup: \(1\) hot is \(18\). That's exactly equal to \(t\).
|
Input: 3 30 10 20 41 15 30 18 13 18 | Output: 2 7 1
|
Medium
| 2 | 1,030 | 338 | 161 | 13 |
1,974 |
G
|
1974G
|
G. Money Buys Less Happiness Now
| 2,000 |
data structures; greedy; sortings
|
You can never buy enough happiness, so here we go again! In this version, you can only buy \(h_i = 1\) unit of happiness each month, but the number of months is hugely increased. We are in the realm of quantum happiness and time dilation.Being a physicist, Charlie likes to plan his life in simple and precise terms. For the next \(m\) months, starting with no money, Charlie will work hard and earn \(x\) pounds per month. For the \(i\)-th month \((1 \le i \le m)\), there'll be a single opportunity of paying cost \(c_i\) pounds to obtain one unit of happiness. You cannot buy more than one unit each month.Borrowing is not allowed. Money earned in the \(i\)-th month can only be spent in a later \(j\)-th month (\(j>i\)).Since physicists don't code, help Charlie find the maximum reachable units of happiness.
|
The first line of the input contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The first line of each test case contains two integers, \(m\) and \(x\) (\(1 \le m \le 2 \cdot 10^5\), \(1 \le x \le 10^3\)) β the total number of months and the monthly salary. The second line of each test case contains \(m\) integers \(c_1, c_2, \dots, c_m\) (\(1 \leq c_i \leq 10^3\)) β the cost of one unit of happiness for each month.It is guaranteed that sum of \(m\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output one integer β the maximal amount of happiness Charlie can get.
|
Input: 63 32 2 26 52 2 8 2 6 86 44 10 3 8 6 102 11 14 14 1 3 14 21 3 4 3 | Output: 2 4 3 1 2 1
|
Hard
| 3 | 812 | 523 | 89 | 19 |
|
1,553 |
H
|
1553H
|
H. XOR and Distance
| 2,900 |
bitmasks; divide and conquer; trees
|
You are given an array \(a\) consisting of \(n\) distinct elements and an integer \(k\). Each element in the array is a non-negative integer not exceeding \(2^k-1\).Let's define the XOR distance for a number \(x\) as the value of $$$\(f(x) = \min\limits_{i = 1}^{n} \min\limits_{j = i + 1}^{n} |(a_i \oplus x) - (a_j \oplus x)|,\)\(where \)\oplus\( denotes the bitwise XOR operation.For every integer \)x\( from \)0\( to \)2^k-1\(, you have to calculate \)f(x)$$$.
|
The first line contains two integers \(n\) and \(k\) (\(1 \le k \le 19\); \(2 \le n \le 2^k\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i \le 2^k-1\)). All these integers are distinct.
|
Print \(2^k\) integers. The \(i\)-th of them should be equal to \(f(i-1)\).
|
Consider the first example: for \(x = 0\), if we apply bitwise XOR to the elements of the array with \(x\), we get the array \([6, 0, 3]\), and the minimum absolute difference of two elements is \(3\); for \(x = 1\), if we apply bitwise XOR to the elements of the array with \(x\), we get the array \([7, 1, 2]\), and the minimum absolute difference of two elements is \(1\); for \(x = 2\), if we apply bitwise XOR to the elements of the array with \(x\), we get the array \([4, 2, 1]\), and the minimum absolute difference of two elements is \(1\); for \(x = 3\), if we apply bitwise XOR to the elements of the array with \(x\), we get the array \([5, 3, 0]\), and the minimum absolute difference of two elements is \(2\); for \(x = 4\), if we apply bitwise XOR to the elements of the array with \(x\), we get the array \([2, 4, 7]\), and the minimum absolute difference of two elements is \(2\); for \(x = 5\), if we apply bitwise XOR to the elements of the array with \(x\), we get the array \([3, 5, 6]\), and the minimum absolute difference of two elements is \(1\); for \(x = 6\), if we apply bitwise XOR to the elements of the array with \(x\), we get the array \([0, 6, 5]\), and the minimum absolute difference of two elements is \(1\); for \(x = 7\), if we apply bitwise XOR to the elements of the array with \(x\), we get the array \([1, 7, 4]\), and the minimum absolute difference of two elements is \(3\).
|
Input: 3 3 6 0 3 | Output: 3 1 1 2 2 1 1 3
|
Master
| 3 | 464 | 219 | 75 | 15 |
1,481 |
E
|
1481E
|
E. Sorting Books
| 2,500 |
data structures; dp; greedy
|
One day you wanted to read something, so you went to your bookshelf to grab some book. But when you saw how messy the bookshelf was you decided to clean it up first. There are \(n\) books standing in a row on the shelf, the \(i\)-th book has color \(a_i\).You'd like to rearrange the books to make the shelf look beautiful. The shelf is considered beautiful if all books of the same color are next to each other.In one operation you can take one book from any position on the shelf and move it to the right end of the shelf.What is the minimum number of operations you need to make the shelf beautiful?
|
The first line contains one integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) β the number of books.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β the book colors.
|
Output the minimum number of operations to make the shelf beautiful.
|
In the first example, we have the bookshelf \([1, 2, 2, 1, 3]\) and can, for example: take a book on position \(4\) and move to the right end: we'll get \([1, 2, 2, 3, 1]\); take a book on position \(1\) and move to the right end: we'll get \([2, 2, 3, 1, 1]\). In the second example, we can move the first book to the end of the bookshelf and get \([2,2,1,1,1]\).
|
Input: 5 1 2 2 1 3 | Output: 2
|
Expert
| 3 | 602 | 200 | 68 | 14 |
471 |
A
|
471A
|
A. MUH and Sticks
| 1,100 |
implementation
|
Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way: Four sticks represent the animal's legs, these sticks should have the same length. Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks. Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it.
|
The single line contains six space-separated integers li (1 β€ li β€ 9) β the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks.
|
If you can make a bear from the given set, print string ""Bear"" (without the quotes). If you can make an elephant, print string ""Elephant"" (wΔ±thout the quotes). If you can make neither a bear nor an elephant, print string ""Alien"" (without the quotes).
|
If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue.
|
Input: 4 2 5 4 4 4 | Output: Bear
|
Easy
| 1 | 896 | 193 | 256 | 4 |
1,598 |
D
|
1598D
|
D. Training Session
| 1,700 |
combinatorics; data structures; geometry; implementation; math
|
Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams.Monocarp has \(n\) problems that none of his students have seen yet. The \(i\)-th problem has a topic \(a_i\) (an integer from \(1\) to \(n\)) and a difficulty \(b_i\) (an integer from \(1\) to \(n\)). All problems are different, that is, there are no two tasks that have the same topic and difficulty at the same time.Monocarp decided to select exactly \(3\) problems from \(n\) problems for the problemset. The problems should satisfy at least one of two conditions (possibly, both): the topics of all three selected problems are different; the difficulties of all three selected problems are different. Your task is to determine the number of ways to select three problems for the problemset.
|
The first line contains a single integer \(t\) (\(1 \le t \le 50000\)) β the number of testcases.The first line of each testcase contains an integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of problems that Monocarp have.In the \(i\)-th of the following \(n\) lines, there are two integers \(a_i\) and \(b_i\) (\(1 \le a_i, b_i \le n\)) β the topic and the difficulty of the \(i\)-th problem.It is guaranteed that there are no two problems that have the same topic and difficulty at the same time.The sum of \(n\) over all testcases doesn't exceed \(2 \cdot 10^5\).
|
Print the number of ways to select three training problems that meet either of the requirements described in the statement.
|
In the first example, you can take the following sets of three problems: problems \(1\), \(2\), \(4\); problems \(1\), \(3\), \(4\); problems \(2\), \(3\), \(4\). Thus, the number of ways is equal to three.
|
Input: 2 4 2 4 3 4 2 1 1 3 5 1 5 2 4 3 3 4 2 5 1 | Output: 3 10
|
Medium
| 5 | 840 | 574 | 123 | 15 |
1,121 |
C
|
1121C
|
C. System Testing
| 1,600 |
implementation
|
Vasya likes taking part in Codeforces contests. When a round is over, Vasya follows all submissions in the system testing tab.There are \(n\) solutions, the \(i\)-th of them should be tested on \(a_i\) tests, testing one solution on one test takes \(1\) second. The solutions are judged in the order from \(1\) to \(n\). There are \(k\) testing processes which test solutions simultaneously. Each of them can test at most one solution at a time.At any time moment \(t\) when some testing process is not judging any solution, it takes the first solution from the queue and tests it on each test in increasing order of the test ids. Let this solution have id \(i\), then it is being tested on the first test from time moment \(t\) till time moment \(t + 1\), then on the second test till time moment \(t + 2\) and so on. This solution is fully tested at time moment \(t + a_i\), and after that the testing process immediately starts testing another solution.Consider some time moment, let there be exactly \(m\) fully tested solutions by this moment. There is a caption ""System testing: \(d\)%"" on the page with solutions, where \(d\) is calculated as$$$\(d = round\left(100\cdot\frac{m}{n}\right),\)\(where \)round(x) = \lfloor{x + 0.5}\rfloor\( is a function which maps every real to the nearest integer.Vasya calls a submission interesting if there is a time moment (possibly, non-integer) when the solution is being tested on some test \)q\(, and the caption says ""System testing: \)q$$$%"". Find the number of interesting solutions.Please note that in case when multiple processes attempt to take the first submission from the queue at the same moment (for instance, at the initial moment), the order they take the solutions does not matter.
|
The first line contains two positive integers \(n\) and \(k\) (\(1 \le n \le 1000\), \(1 \le k \le 100\)) standing for the number of submissions and the number of testing processes respectively.The second line contains \(n\) positive integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 150\)), where \(a_i\) is equal to the number of tests the \(i\)-th submission is to be run on.
|
Output the only integer β the number of interesting submissions.
|
Consider the first example. At time moment \(0\) both solutions start testing. At time moment \(49\) the first solution is fully tested, so at time moment \(49.5\) the second solution is being tested on the test \(50\), and the caption says ""System testing: \(50\)%"" (because there is one fully tested solution out of two). So, the second solution is interesting.Consider the second example. At time moment \(0\) the first and the second solutions start testing. At time moment \(32\) the first solution is fully tested, the third solution starts testing, the caption says ""System testing: \(25\)%"". At time moment \(32 + 24.5 = 56.5\) the third solutions is being tested on test \(25\), the caption is still the same, thus this solution is interesting. After that the third solution is fully tested at time moment \(32 + 33 = 65\), the fourth solution is fully tested at time moment \(65 + 1 = 66\). The captions becomes ""System testing: \(75\)%"", and at time moment \(74.5\) the second solution is being tested on test \(75\). So, this solution is also interesting. Overall, there are two interesting solutions.
|
Input: 2 2 49 100 | Output: 1
|
Medium
| 1 | 1,747 | 380 | 64 | 11 |
1,548 |
A
|
1548A
|
A. Web of Lies
| 1,400 |
brute force; graphs; greedy
|
When you play the game of thrones, you win, or you die. There is no middle ground.Cersei Lannister, A Game of Thrones by George R. R. MartinThere are \(n\) nobles, numbered from \(1\) to \(n\). Noble \(i\) has a power of \(i\). There are also \(m\) ""friendships"". A friendship between nobles \(a\) and \(b\) is always mutual.A noble is defined to be vulnerable if both of the following conditions are satisfied: the noble has at least one friend, and all of that noble's friends have a higher power. You will have to process the following three types of queries. Add a friendship between nobles \(u\) and \(v\). Remove a friendship between nobles \(u\) and \(v\). Calculate the answer to the following process. The process: all vulnerable nobles are simultaneously killed, and all their friendships end. Then, it is possible that new nobles become vulnerable. The process repeats itself until no nobles are vulnerable. It can be proven that the process will end in finite time. After the process is complete, you need to calculate the number of remaining nobles.Note that the results of the process are not carried over between queries, that is, every process starts with all nobles being alive!
|
The first line contains the integers \(n\) and \(m\) (\(1 \le n \le 2\cdot 10^5\), \(0 \le m \le 2\cdot 10^5\)) β the number of nobles and number of original friendships respectively.The next \(m\) lines each contain the integers \(u\) and \(v\) (\(1 \le u,v \le n\), \(u \ne v\)), describing a friendship. No friendship is listed twice.The next line contains the integer \(q\) (\(1 \le q \le 2\cdot {10}^{5}\)) β the number of queries. The next \(q\) lines contain the queries themselves, each query has one of the following three formats. \(1\) \(u\) \(v\) (\(1 \le u,v \le n\), \(u \ne v\)) β add a friendship between \(u\) and \(v\). It is guaranteed that \(u\) and \(v\) are not friends at this moment. \(2\) \(u\) \(v\) (\(1 \le u,v \le n\), \(u \ne v\)) β remove a friendship between \(u\) and \(v\). It is guaranteed that \(u\) and \(v\) are friends at this moment. \(3\) β print the answer to the process described in the statement.
|
For each type \(3\) query print one integer to a new line. It is guaranteed that there will be at least one type \(3\) query.
|
Consider the first example. In the first type 3 query, we have the diagram below.In the first round of the process, noble \(1\) is weaker than all of his friends (\(2\) and \(3\)), and is thus killed. No other noble is vulnerable in round 1. In round 2, noble \(3\) is weaker than his only friend, noble \(4\), and is therefore killed. At this point, the process ends, and the answer is \(2\). In the second type 3 query, the only surviving noble is \(4\). The second example consists of only one type \(3\) query. In the first round, two nobles are killed, and in the second round, one noble is killed. The final answer is \(1\), since only one noble survives.
|
Input: 4 3 2 1 1 3 3 4 4 3 1 2 3 2 3 1 3 | Output: 2 1
|
Easy
| 3 | 1,197 | 941 | 125 | 15 |
1,268 |
A
|
1268A
|
A. Long Beautiful Integer
| 1,700 |
constructive algorithms; greedy; implementation; strings
|
You are given an integer \(x\) of \(n\) digits \(a_1, a_2, \ldots, a_n\), which make up its decimal notation in order from left to right.Also, you are given a positive integer \(k < n\).Let's call integer \(b_1, b_2, \ldots, b_m\) beautiful if \(b_i = b_{i+k}\) for each \(i\), such that \(1 \leq i \leq m - k\).You need to find the smallest beautiful integer \(y\), such that \(y \geq x\).
|
The first line of input contains two integers \(n, k\) (\(2 \leq n \leq 200\,000, 1 \leq k < n\)): the number of digits in \(x\) and \(k\).The next line of input contains \(n\) digits \(a_1, a_2, \ldots, a_n\) (\(a_1 \neq 0\), \(0 \leq a_i \leq 9\)): digits of \(x\).
|
In the first line print one integer \(m\): the number of digits in \(y\).In the next line print \(m\) digits \(b_1, b_2, \ldots, b_m\) (\(b_1 \neq 0\), \(0 \leq b_i \leq 9\)): digits of \(y\).
|
Input: 3 2 353 | Output: 3 353
|
Medium
| 4 | 390 | 267 | 192 | 12 |
|
1,368 |
F
|
1368F
|
F. Lamps on a Circle
| 2,600 |
games; implementation; interactive; math
|
This is an interactive problem.John and his imaginary friend play a game. There are \(n\) lamps arranged in a circle. Lamps are numbered \(1\) through \(n\) in clockwise order, that is, lamps \(i\) and \(i + 1\) are adjacent for any \(i = 1, \ldots, n - 1\), and also lamps \(n\) and \(1\) are adjacent. Initially all lamps are turned off.John and his friend take turns, with John moving first. On his turn John can choose to terminate the game, or to make a move. To make a move, John can choose any positive number \(k\) and turn any \(k\) lamps of his choosing on. In response to this move, John's friend will choose \(k\) consecutive lamps and turn all of them off (the lamps in the range that were off before this move stay off). Note that the value of \(k\) is the same as John's number on his last move. For example, if \(n = 5\) and John have just turned three lamps on, John's friend may choose to turn off lamps \(1, 2, 3\), or \(2, 3, 4\), or \(3, 4, 5\), or \(4, 5, 1\), or \(5, 1, 2\).After this, John may choose to terminate or move again, and so on. However, John can not make more than \(10^4\) moves.John wants to maximize the number of lamps turned on at the end of the game, while his friend wants to minimize this number. Your task is to provide a strategy for John to achieve optimal result. Your program will play interactively for John against the jury's interactor program playing for John's friend.Suppose there are \(n\) lamps in the game. Let \(R(n)\) be the number of turned on lamps at the end of the game if both players act optimally. Your program has to terminate the game with at least \(R(n)\) turned on lamps within \(10^4\) moves. Refer to Interaction section below for interaction details.For technical reasons hacks for this problem are disabled.
|
When \(n = 3\), any John's move can be reversed, thus \(R(3) = 0\), and terminating the game immediately is correct.\(R(4) = 1\), and one strategy to achieve this result is shown in the second sample case.Blank lines in sample interactions are for clarity and should not be printed.
|
Input: 3 | Output: 0
|
Expert
| 4 | 1,784 | 0 | 0 | 13 |
||
297 |
D
|
297D
|
D. Color the Carpet
| 2,500 |
constructive algorithms
|
Even polar bears feel cold when lying on the ice. Therefore, a polar bear Alice is going to make a carpet. The carpet can be viewed as a grid with height h and width w. Then the grid is divided into h Γ w squares. Alice is going to assign one of k different colors to each square. The colors are numbered from 1 to k. She may choose not to use all of the colors.However, there are some restrictions. For every two adjacent squares (squares that shares an edge) x and y, there is a color constraint in one of the forms: color(x) = color(y), or color(x) β color(y). Example of the color constraints: Ideally, Alice wants to satisfy all color constraints. But again, life in the Arctic is hard. It is not always possible to satisfy all color constraints. Fortunately, she will still be happy if at least of the color constraints are satisfied. If she has 4 colors she can color the carpet in the following way: And she is happy because of the color constraints are satisfied, and . Your task is to help her color the carpet.
|
The first line contains three integers h, w, k (2 β€ h, w β€ 1000, 1 β€ k β€ wΒ·h).The next 2h - 1 lines describe the color constraints from top to bottom, left to right. They contain w - 1, w, w - 1, w, ..., w - 1 characters respectively. Each color constraint is represented by a character ""E"" or ""N"", where ""E"" means "" = "" and ""N"" means "" β "".The color constraints listed in the order they are depicted on the picture.
|
If there is a coloring that satisfies at least of the color constraints, print ""YES"" (without quotes) in the first line. In each of the next h lines, print w integers describing the coloring.Otherwise, print ""NO"" (without quotes).
|
Input: 3 4 4ENENNEENEEENENENN | Output: YES1 1 2 23 4 1 13 3 2 4
|
Expert
| 1 | 1,021 | 428 | 234 | 2 |
|
426 |
A
|
426A
|
A. Sereja and Mugs
| 800 |
implementation
|
Sereja showed an interesting game to his friends. The game goes like that. Initially, there is a table with an empty cup and n water mugs on it. Then all players take turns to move. During a move, a player takes a non-empty mug of water and pours all water from it into the cup. If the cup overfills, then we assume that this player lost.As soon as Sereja's friends heard of the game, they wanted to play it. Sereja, on the other hand, wanted to find out whether his friends can play the game in such a way that there are no losers. You are given the volumes of all mugs and the cup. Also, you know that Sereja has (n - 1) friends. Determine if Sereja's friends can play the game so that nobody loses.
|
The first line contains integers n and s (2 β€ n β€ 100; 1 β€ s β€ 1000) β the number of mugs and the volume of the cup. The next line contains n integers a1, a2, ..., an (1 β€ ai β€ 10). Number ai means the volume of the i-th mug.
|
In a single line, print ""YES"" (without the quotes) if his friends can play in the described manner, and ""NO"" (without the quotes) otherwise.
|
Input: 3 41 1 1 | Output: YES
|
Beginner
| 1 | 701 | 225 | 144 | 4 |
|
1,559 |
A
|
1559A
|
A. Mocha and Math
| 900 |
bitmasks; constructive algorithms; math
|
Mocha is a young girl from high school. She has learned so much interesting knowledge from her teachers, especially her math teacher. Recently, Mocha is learning about binary system and very interested in bitwise operation.This day, Mocha got a sequence \(a\) of length \(n\). In each operation, she can select an arbitrary interval \([l, r]\) and for all values \(i\) (\(0\leq i \leq r-l\)), replace \(a_{l+i}\) with \(a_{l+i} \,\&\, a_{r-i}\) at the same time, where \(\&\) denotes the bitwise AND operation. This operation can be performed any number of times.For example, if \(n=5\), the array is \([a_1,a_2,a_3,a_4,a_5]\), and Mocha selects the interval \([2,5]\), then the new array is \([a_1,a_2\,\&\, a_5, a_3\,\&\, a_4, a_4\,\&\, a_3, a_5\,\&\, a_2]\).Now Mocha wants to minimize the maximum value in the sequence. As her best friend, can you help her to get the answer?
|
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of test cases. Each test case consists of two lines.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the length of the sequence.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)).
|
For each test case, print one integer β the minimal value of the maximum value in the sequence.
|
In the first test case, Mocha can choose the interval \([1,2]\), then the sequence becomes \([ 0, 0]\), where the first element is \(1\,\&\,2\), and the second element is \(2\,\&\,1\).In the second test case, Mocha can choose the interval \([1,3]\), then the sequence becomes \([ 1,1,1]\), where the first element is \(1\,\&\,3\), the second element is \(1\,\&\,1\), and the third element is \(3\,\&\,1\).
|
Input: 4 2 1 2 3 1 1 3 4 3 11 3 7 5 11 7 15 3 7 | Output: 0 1 3 3
|
Beginner
| 3 | 879 | 399 | 95 | 15 |
1,875 |
C
|
1875C
|
C. Jellyfish and Green Apple
| 1,400 |
bitmasks; greedy; math; number theory
|
Jellyfish has \(n\) green apple pieces. Each green apple piece weighs \(1~\text{kg}\). Jellyfish wants to divide these green apple pieces equally among \(m\) people.Jellyfish has a magic knife. Each time Jellyfish can choose one piece of green apple and divide it into two smaller pieces, with each piece having half the weight of the original piece.Jellyfish wants to know the minimum number of operations needed such that she can divide the green apple pieces such that the total weight of apple pieces received by each person is the same.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \leq t \leq 2 \cdot 10^4\)). The description of the test cases follows.The first and only line of each test case contains two integers, \(n\) and \(m\) (\(1 \leq n, m \leq 10^9\)) β the number of the green apple pieces initially and the number of the people.
|
For each test case, output a single integer β the minimum number of operations required to divide all the green apples equally among the \(m\) people. If this cannot be achieved using a finite number of operations, output \(-1\) instead.
|
In the first test case, we do not need to divide the apple pieces. Each person will receive \(2\) pieces of \(1~\text{kg}\) and the total weight of apple pieces received by each person is \(2~\text{kg}\).In the second test case, it is impossible to divide the apple equally using a finite number of operations.In the third test case, we can divide two of the apple pieces of weight \(1~\text{kg}\), getting \(4\) apple pieces of weight \(0.5~\text{kg}\). Each person will receive \(1\) apple piece of weight \(0.5~\text{kg}\) and \(2\) apple pieces of weight \(1~\text{kg}\). The total weight of apple pieces received by each person is \(2.5~\text{kg}\).In the fourth test case, we first divide all \(3\) of the apple pieces of weight \(1~\text{kg}\), getting \(6\) pieces of weight \(0.5~\text{kg}\). Then, we further divide two of the apple pieces of weight \(0.5~\text{kg}\), getting \(4\) pieces of weight \(0.25~\text{kg}\). Each person will receive \(1\) apple piece of weight \(0.5~\text{kg}\) and \(1\) apple piece of weight \(0.25~\text{kg}\). The total weight of apples received by each person is \(0.75~\text{kg}\).
|
Input: 410 51 510 43 4 | Output: 0 -1 2 5
|
Easy
| 4 | 541 | 358 | 237 | 18 |
1,681 |
C
|
1681C
|
C. Double Sort
| 1,200 |
implementation; sortings
|
You are given two arrays \(a\) and \(b\), both consisting of \(n\) integers.In one move, you can choose two indices \(i\) and \(j\) (\(1 \le i, j \le n\); \(i \neq j\)) and swap \(a_i\) with \(a_j\) and \(b_i\) with \(b_j\). You have to perform the swap in both arrays.You are allowed to perform at most \(10^4\) moves (possibly, zero). Can you make both arrays sorted in a non-decreasing order at the end? If you can, print any sequence of moves that makes both arrays sorted.
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\)) β the number of testcases.The first line of each testcase contains a single integer \(n\) (\(2 \le n \le 100\)) β the number of elements in both arrays.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le n\)) β the first array.The third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le n\)) β the second array.
|
For each testcase, print the answer. If it's impossible to make both arrays sorted in a non-decreasing order in at most \(10^4\) moves, print -1. Otherwise, first, print the number of moves \(k\) \((0 \le k \le 10^4)\). Then print \(i\) and \(j\) for each move \((1 \le i, j \le n\); \(i \neq j)\).If there are multiple answers, then print any of them. You don't have to minimize the number of moves.
|
Input: 321 21 222 11 242 3 1 22 3 2 3 | Output: 0 -1 3 3 1 3 2 4 3
|
Easy
| 2 | 477 | 431 | 400 | 16 |
|
1,682 |
C
|
1682C
|
C. LIS or Reverse LIS?
| 1,400 |
constructive algorithms; greedy; implementation; math
|
You are given an array \(a\) of \(n\) positive integers. Let \(\text{LIS}(a)\) denote the length of longest strictly increasing subsequence of \(a\). For example, \(\text{LIS}([2, \underline{1}, 1, \underline{3}])\) = \(2\). \(\text{LIS}([\underline{3}, \underline{5}, \underline{10}, \underline{20}])\) = \(4\). \(\text{LIS}([3, \underline{1}, \underline{2}, \underline{4}])\) = \(3\). We define array \(a'\) as the array obtained after reversing the array \(a\) i.e. \(a' = [a_n, a_{n-1}, \ldots , a_1]\).The beauty of array \(a\) is defined as \(min(\text{LIS}(a),\text{LIS}(a'))\).Your task is to determine the maximum possible beauty of the array \(a\) if you can rearrange the array \(a\) arbitrarily.
|
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 a single integer \(n\) \((1 \leq n \leq 2\cdot 10^5)\) β the length of array \(a\).The second line of each test case contains \(n\) integers \(a_1,a_2, \ldots ,a_n\) \((1 \leq a_i \leq 10^9)\) β the elements of the array \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output a single integer β the maximum possible beauty of \(a\) after rearranging its elements arbitrarily.
|
In the first test case, \(a\) = \([6, 6, 6]\) and \(a'\) = \([6, 6, 6]\). \(\text{LIS}(a) = \text{LIS}(a')\) = \(1\). Hence the beauty is \(min(1, 1) = 1\).In the second test case, \(a\) can be rearranged to \([2, 5, 4, 5, 4, 2]\). Then \(a'\) = \([2, 4, 5, 4, 5, 2]\). \(\text{LIS}(a) = \text{LIS}(a') = 3\). Hence the beauty is \(3\) and it can be shown that this is the maximum possible beauty.In the third test case, \(a\) can be rearranged to \([1, 2, 3, 2]\). Then \(a'\) = \([2, 3, 2, 1]\). \(\text{LIS}(a) = 3\), \(\text{LIS}(a') = 2\). Hence the beauty is \(min(3, 2) = 2\) and it can be shown that \(2\) is the maximum possible beauty.
|
Input: 336 6 662 5 4 5 2 441 3 2 2 | Output: 1 3 2
|
Easy
| 4 | 707 | 541 | 126 | 16 |
429 |
D
|
429D
|
D. Tricky Function
| 2,200 |
data structures; divide and conquer; geometry
|
Iahub and Sorin are the best competitive programmers in their town. However, they can't both qualify to an important contest. The selection will be made with the help of a single problem. Blatnatalag, a friend of Iahub, managed to get hold of the problem before the contest. Because he wants to make sure Iahub will be the one qualified, he tells Iahub the following task.You're given an (1-based) array a with n elements. Let's define function f(i, j) (1 β€ i, j β€ n) as (i - j)2 + g(i, j)2. Function g is calculated by the following pseudo-code:int g(int i, int j) { int sum = 0; for (int k = min(i, j) + 1; k <= max(i, j); k = k + 1) sum = sum + a[k]; return sum;}Find a value mini β j f(i, j).Probably by now Iahub already figured out the solution to this problem. Can you?
|
The first line of input contains a single integer n (2 β€ n β€ 100000). Next line contains n integers a[1], a[2], ..., a[n] ( - 104 β€ a[i] β€ 104).
|
Output a single integer β the value of mini β j f(i, j).
|
Input: 41 0 0 -1 | Output: 1
|
Hard
| 3 | 776 | 144 | 56 | 4 |
|
450 |
A
|
450A
|
A. Jzzhu and Children
| 1,000 |
implementation
|
There are n children in Jzzhu's school. Jzzhu is going to give some candies to them. Let's number all the children from 1 to n. The i-th child wants to get at least ai candies.Jzzhu asks children to line up. Initially, the i-th child stands at the i-th place of the line. Then Jzzhu start distribution of the candies. He follows the algorithm: Give m candies to the first child of the line. If this child still haven't got enough candies, then the child goes to the end of the line, else the child go home. Repeat the first two steps while the line is not empty. Consider all the children in the order they go home. Jzzhu wants to know, which child will be the last in this order?
|
The first line contains two integers n, m (1 β€ n β€ 100; 1 β€ m β€ 100). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 100).
|
Output a single integer, representing the number of the last child.
|
Let's consider the first sample. Firstly child 1 gets 2 candies and go home. Then child 2 gets 2 candies and go to the end of the line. Currently the line looks like [3, 4, 5, 2] (indices of the children in order of the line). Then child 3 gets 2 candies and go home, and then child 4 gets 2 candies and goes to the end of the line. Currently the line looks like [5, 2, 4]. Then child 5 gets 2 candies and goes home. Then child 2 gets two candies and goes home, and finally child 4 gets 2 candies and goes home.Child 4 is the last one who goes home.
|
Input: 5 21 3 1 4 2 | Output: 4
|
Beginner
| 1 | 680 | 137 | 67 | 4 |
1,811 |
G2
|
1811G2
|
G2. Vlad and the Nice Paths (hard version)
| 2,200 |
binary search; combinatorics; data structures; dp; math; two pointers
|
This is hard version of the problem, it differs from the easy one only by constraints on \(n\) and \(k\).Vlad found a row of \(n\) tiles and the integer \(k\). The tiles are indexed from left to right and the \(i\)-th tile has the color \(c_i\). After a little thought, he decided what to do with it.You can start from any tile and jump to any number of tiles right, forming the path \(p\). Let's call the path \(p\) of length \(m\) nice if: \(p\) can be divided into blocks of length exactly \(k\), that is, \(m\) is divisible by \(k\); \(c_{p_1} = c_{p_2} = \ldots = c_{p_k}\); \(c_{p_{k+1}} = c_{p_{k+2}} = \ldots = c_{p_{2k}}\); \(\ldots\) \(c_{p_{m-k+1}} = c_{p_{m-k+2}} = \ldots = c_{p_{m}}\); Your task is to find the number of nice paths of maximum length. Since this number may be too large, print it modulo \(10^9 + 7\).
|
The first line of each test contains the integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 5000\)) β the number of tiles in a row and the length of the block.The second line of each test case contains \(n\) integers \(c_1, c_2, c_3, \dots, c_n\) (\(1 \le c_i \le n\)) β tile colors.It is guaranteed that the sum of \(n^2\) over all test cases does not exceed \(25 \cdot 10^6\).
|
Print \(t\) numbers, each of which is the answer to the corresponding test case β the number of nice paths of maximum length modulo \(10^9 + 7\).
|
In the first sample, it is impossible to make a nice path with a length greater than \(0\).In the second sample, we are interested in the following paths: \(1 \rightarrow 3 \rightarrow 4 \rightarrow 5\) \(2 \rightarrow 4 \rightarrow 5 \rightarrow 7\) \(1 \rightarrow 3 \rightarrow 5 \rightarrow 7\) \(1 \rightarrow 3 \rightarrow 4 \rightarrow 7\) In the third example, any path of length \(8\) is nice.
|
Input: 55 21 2 3 4 57 21 3 1 3 3 1 311 41 1 1 1 1 1 1 1 1 1 15 21 1 2 2 25 11 2 3 4 5 | Output: 1 4 165 3 1
|
Hard
| 6 | 830 | 495 | 145 | 18 |
457 |
E
|
457E
|
E. Flow Optimality
| 3,000 |
constructive algorithms; flows; math
|
There is a computer network consisting of n nodes numbered 1 through n. There are links in the network that connect pairs of nodes. A pair of nodes may have multiple links between them, but no node has a link to itself.Each link supports unlimited bandwidth (in either direction), however a link may only transmit in a single direction at any given time. The cost of sending data across a link is proportional to the square of the bandwidth. Specifically, each link has a positive weight, and the cost of sending data across the link is the weight times the square of the bandwidth.The network is connected (there is a series of links from any node to any other node), and furthermore designed to remain connected in the event of any single node failure.You needed to send data from node 1 to node n at a bandwidth of some positive number k. That is, you wish to assign a bandwidth to each link so that the bandwidth into a node minus the bandwidth out of a node is - k for node 1, k for node n, and 0 for all other nodes. The individual bandwidths do not need to be integers.Wishing to minimize the total cost, you drew a diagram of the network, then gave the task to an intern to solve. The intern claimed to have solved the task and written the optimal bandwidths on your diagram, but then spilled coffee on it, rendering much of it unreadable (including parts of the original diagram, and the value of k).From the information available, determine if the intern's solution may have been optimal. That is, determine if there exists a valid network, total bandwidth, and optimal solution which is a superset of the given information. Furthermore, determine the efficiency of the intern's solution (if possible), where efficiency is defined as total cost divided by total bandwidth.
|
Input will begin with two integers n and m (2 β€ n β€ 200000; 0 β€ m β€ 200000), the number of nodes and number of known links in the network, respectively. Following this are m lines with four integers each: f, t, w, b (1 β€ f β€ n; 1 β€ t β€ n; f β t; 1 β€ w β€ 100; 0 β€ b β€ 100). This indicates there is a link between nodes f and t with weight w and carrying b bandwidth. The direction of bandwidth is from f to t.
|
If the intern's solution is definitely not optimal, print ""BAD x"", where x is the first link in the input that violates the optimality of the solution. If the intern's solution may be optimal, print the efficiency of the solution if it can be determined rounded to the nearest integer, otherwise print ""UNKNOWN"".
|
Although the known weights and bandwidths happen to always be integers, the weights and bandwidths of the remaining links are not restricted to integers.
|
Input: 4 51 2 1 21 3 4 12 3 2 12 4 4 13 4 1 2 | Output: 6
|
Master
| 3 | 1,782 | 408 | 316 | 4 |
1,063 |
B
|
1063B
|
B. Labyrinth
| 1,800 |
graphs; shortest paths
|
You are playing some computer game. One of its levels puts you in a maze consisting of n lines, each of which contains m cells. Each cell either is free or is occupied by an obstacle. The starting cell is in the row r and column c. In one step you can move one square up, left, down or right, if the target cell is not occupied by an obstacle. You can't move beyond the boundaries of the labyrinth.Unfortunately, your keyboard is about to break, so you can move left no more than x times and move right no more than y times. There are no restrictions on the number of moves up and down since the keys used to move up and down are in perfect condition.Now you would like to determine for each cell whether there exists a sequence of moves that will put you from the starting cell to this particular one. How many cells of the board have this property?
|
The first line contains two integers n, m (1 β€ n, m β€ 2000) β the number of rows and the number columns in the labyrinth respectively.The second line contains two integers r, c (1 β€ r β€ n, 1 β€ c β€ m) β index of the row and index of the column that define the starting cell.The third line contains two integers x, y (0 β€ x, y β€ 109) β the maximum allowed number of movements to the left and to the right respectively.The next n lines describe the labyrinth. Each of them has length of m and consists only of symbols '.' and '*'. The j-th character of the i-th line corresponds to the cell of labyrinth at row i and column j. Symbol '.' denotes the free cell, while symbol '*' denotes the cell with an obstacle.It is guaranteed, that the starting cell contains no obstacles.
|
Print exactly one integer β the number of cells in the labyrinth, which are reachable from starting cell, including the starting cell itself.
|
Cells, reachable in the corresponding example, are marked with '+'.First example: +++..+***.+++***+++. Second example: .++..+*..++..++.
|
Input: 4 53 21 2......***....***.... | Output: 10
|
Medium
| 2 | 850 | 772 | 141 | 10 |
75 |
A
|
75A
|
A. Life Without Zeros
| 1,000 |
implementation
|
Can you imagine our life if we removed all zeros from it? For sure we will have many problems.In this problem we will have a simple example if we removed all zeros from our life, it's the addition operation. Let's assume you are given this equation a + b = c, where a and b are positive integers, and c is the sum of a and b. Now let's remove all zeros from this equation. Will the equation remain correct after removing all zeros?For example if the equation is 101 + 102 = 203, if we removed all zeros it will be 11 + 12 = 23 which is still a correct equation.But if the equation is 105 + 106 = 211, if we removed all zeros it will be 15 + 16 = 211 which is not a correct equation.
|
The input will consist of two lines, the first line will contain the integer a, and the second line will contain the integer b which are in the equation as described above (1 β€ a, b β€ 109). There won't be any leading zeros in both. The value of c should be calculated as c = a + b.
|
The output will be just one line, you should print ""YES"" if the equation will remain correct after removing all zeros, and print ""NO"" otherwise.
|
Input: 101102 | Output: YES
|
Beginner
| 1 | 682 | 281 | 148 | 0 |
|
1,662 |
D
|
1662D
|
D. Evolution of Weasels
| 0 |
greedy; implementation; strings
|
A wild basilisk just appeared at your doorstep. You are not entirely sure what a basilisk is and you wonder whether it evolved from your favorite animal, the weasel. How can you find out whether basilisks evolved from weasels? Certainly, a good first step is to sequence both of their DNAs. Then you can try to check whether there is a sequence of possible mutations from the DNA of the weasel to the DNA of the basilisk. Your friend Ron is a talented alchemist and has studied DNA sequences in many of his experiments. He has found out that DNA strings consist of the letters A, B and C and that single mutations can only remove or add substrings at any position in the string (a substring is a contiguous sequence of characters). The substrings that can be removed or added by a mutation are AA, BB, CC, ABAB or BCBC. During a sequence of mutations a DNA string may even become empty.Ron has agreed to sequence the DNA of the weasel and the basilisk for you, but finding out whether there is a sequence of possible mutations that leads from one to the other is too difficult for him, so you have to do it on your own.
|
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 100\)) β the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains a string \(u\) (\(1\le |u|\le 200\)) β the DNA of the weasel.The second line of each test case contains a string \(v\) (\(1\le |v|\le 200\)) β the DNA of the basilisk. The values \(|u|\), \(|v|\) denote the lengths of the strings \(u\) and \(v\). It is guaranteed that both strings \(u\) and \(v\) consist of the letters A, B and C.
|
For each test case, print YES if there is a sequence of mutations to get from \(u\) to \(v\) and NO otherwise.
|
Input: 8ABBCCAAABBBBCCCCAAABABBCBCABCCBA | Output: NO NO NO YES YES YES YES NO
|
Beginner
| 3 | 1,119 | 551 | 110 | 16 |
|
1,916 |
E
|
1916E
|
E. Happy Life in University
| 2,300 |
data structures; dfs and similar; greedy; trees
|
Egor and his friend Arseniy are finishing school this year and will soon enter university. And since they are very responsible guys, they have started preparing for admission already.First of all, they decided to take care of where they will live for the long four years of study, and after visiting the university's website, they found out that the university dormitory can be represented as a root tree with \(n\) vertices with the root at vertex \(1\). In the tree, each vertex represents a recreation with some type of activity \(a_i\). The friends need to choose \(2\) recreations (not necessarily different) in which they will settle. The guys are convinced that the more the value of the following function \(f(u, v) = diff(u, lca(u, v)) \cdot diff(v, lca(u, v))\), the more fun their life will be. Help Egor and Arseniy and find the maximum value of \(f(u, v)\) among all pairs of recreations!\(^{\dagger} diff(u, v)\) β the number of different activities listed on the simple path from vertex \(u\) to vertex \(v\).\(^{\dagger} lca(u, v)\) β a vertex \(p\) such that it is at the maximum distance from the root and is a parent of both vertex \(u\) and vertex \(v\).
|
Each test consists of several test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β the number of test cases. Then follows the description of the test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^{5}\)).The second line of each test case contains \({n - 1}\) integers \(p_2, p_3, \ldots,p_n\) (\(1 \le p_i \le i - 1\)), where \(p_i\) β the parent of vertex \(i\).The third line of each test case contains \({n}\) integers \(a_1, a_2, \ldots,a_n\) (\(1 \le a_i \le n\)), where \(a_i\) β the number of the activity located at vertex \(i\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3 \cdot 10^5\).
|
For each test case, output the maximum value of \(f(u, v)\) for all pairs of recreations \((u, v)\).
|
Consider the fourth test case. The tree has the following structure: All recreations are colored. The same colors mean that the activities in the recreations match. Consider the pair of vertices \((11, 12)\), \(lca(11, 12) = 1\). Write down all activities on the path from \(11\) to \(1\) β \([11, 5, 1, 11]\), among them there are \(3\) different activities, so \(diff(11, 1) = 3\). Also write down all activities on the path from \(12\) to \(1\) β \([7, 8, 2, 11]\), among them there are \(4\) different activities, so \(diff(12, 1) = 4\). We get that \(f(11, 12) = diff(12, 1) \cdot diff(11, 1) = 4 \cdot 3 = 12\), which is the answer for this tree. It can be shown that a better answer is impossible to obtain.
|
Input: 4211 271 1 2 2 3 36 5 2 3 6 5 6131 1 1 2 2 2 3 3 4 5 6 62 2 2 1 4 9 7 2 5 2 1 11 2121 1 1 2 2 3 4 4 7 7 611 2 1 11 12 8 5 8 8 5 11 7 | Output: 2 9 9 12
|
Expert
| 4 | 1,174 | 710 | 100 | 19 |
354 |
A
|
354A
|
A. Vasya and Robot
| 1,500 |
brute force; greedy; math
|
Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in such a way that the leftmost item has number 1, the rightmost item has number n. Each item has a weight, the i-th item weights wi kilograms.Vasya needs to collect all these items, however he won't do it by himself. He uses his brand new robot. The robot has two different arms β the left one and the right one. The robot can consecutively perform the following actions: Take the leftmost item with the left hand and spend wi Β· l energy units (wi is a weight of the leftmost item, l is some parameter). If the previous action was the same (left-hand), then the robot spends extra Ql energy units; Take the rightmost item with the right hand and spend wj Β· r energy units (wj is a weight of the rightmost item, r is some parameter). If the previous action was the same (right-hand), then the robot spends extra Qr energy units; Naturally, Vasya wants to program the robot in a way that the robot spends as little energy as possible. He asked you to solve this problem. Your task is to find the minimum number of energy units robot spends to collect all items.
|
The first line contains five integers n, l, r, Ql, Qr (1 β€ n β€ 105; 1 β€ l, r β€ 100; 1 β€ Ql, Qr β€ 104).The second line contains n integers w1, w2, ..., wn (1 β€ wi β€ 100).
|
In the single line print a single number β the answer to the problem.
|
Consider the first sample. As l = r, we can take an item in turns: first from the left side, then from the right one and last item from the left. In total the robot spends 4Β·42 + 4Β·99 + 4Β·3 = 576 energy units.The second sample. The optimal solution is to take one item from the right, then one item from the left and two items from the right. In total the robot spends (2Β·4) + (7Β·1) + (2Β·3) + (2Β·2 + 9) = 34 energy units.
|
Input: 3 4 4 19 142 3 99 | Output: 576
|
Medium
| 3 | 1,155 | 169 | 69 | 3 |
853 |
D
|
853D
|
D. Michael and Charging Stations
| 2,400 |
binary search; dp; greedy
|
Michael has just bought a new electric car for moving across city. Michael does not like to overwork, so each day he drives to only one of two his jobs.Michael's day starts from charging his electric car for getting to the work and back. He spends 1000 burles on charge if he goes to the first job, and 2000 burles if he goes to the second job.On a charging station he uses there is a loyalty program that involves bonus cards. Bonus card may have some non-negative amount of bonus burles. Each time customer is going to buy something for the price of x burles, he is allowed to pay an amount of y (0 β€ y β€ x) burles that does not exceed the bonus card balance with bonus burles. In this case he pays x - y burles with cash, and the balance on the bonus card is decreased by y bonus burles. If customer pays whole price with cash (i.e., y = 0) then 10% of price is returned back to the bonus card. This means that bonus card balance increases by bonus burles. Initially the bonus card balance is equal to 0 bonus burles.Michael has planned next n days and he knows how much does the charge cost on each of those days. Help Michael determine the minimum amount of burles in cash he has to spend with optimal use of bonus card. Assume that Michael is able to cover any part of the price with cash in any day. It is not necessary to spend all bonus burles at the end of the given period.
|
The first line of input contains a single integer n (1 β€ n β€ 300 000), the number of days Michael has planned.Next line contains n integers a1, a2, ..., an (ai = 1000 or ai = 2000) with ai denoting the charging cost at the day i.
|
Output the minimum amount of burles Michael has to spend.
|
In the first sample case the most optimal way for Michael is to pay for the first two days spending 3000 burles and get 300 bonus burles as return. After that he is able to pay only 700 burles for the third days, covering the rest of the price with bonus burles.In the second sample case the most optimal way for Michael is to pay the whole price for the first five days, getting 1000 bonus burles as return and being able to use them on the last day without paying anything in cash.
|
Input: 31000 2000 1000 | Output: 3700
|
Expert
| 3 | 1,384 | 229 | 57 | 8 |
1,532 |
B
|
1532B
|
B. Frog Jumping
| 0 |
*special; math
|
A frog is currently at the point \(0\) on a coordinate axis \(Ox\). It jumps by the following algorithm: the first jump is \(a\) units to the right, the second jump is \(b\) units to the left, the third jump is \(a\) units to the right, the fourth jump is \(b\) units to the left, and so on.Formally: if the frog has jumped an even number of times (before the current jump), it jumps from its current position \(x\) to position \(x+a\); otherwise it jumps from its current position \(x\) to position \(x-b\). Your task is to calculate the position of the frog after \(k\) jumps.But... One more thing. You are watching \(t\) different frogs so you have to answer \(t\) independent queries.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of queries.Each of the next \(t\) lines contain queries (one query per line).The query is described as three space-separated integers \(a, b, k\) (\(1 \le a, b, k \le 10^9\)) β the lengths of two types of jumps and the number of jumps, respectively.
|
Print \(t\) integers. The \(i\)-th integer should be the answer for the \(i\)-th query.
|
In the first query frog jumps \(5\) to the right, \(2\) to the left and \(5\) to the right so the answer is \(5 - 2 + 5 = 8\).In the second query frog jumps \(100\) to the right, \(1\) to the left, \(100\) to the right and \(1\) to the left so the answer is \(100 - 1 + 100 - 1 = 198\).In the third query the answer is \(1 - 10 + 1 - 10 + 1 = -17\).In the fourth query the answer is \(10^9 - 1 + 10^9 - 1 + 10^9 - 1 = 2999999997\).In the fifth query all frog's jumps are neutralized by each other so the answer is \(0\).The sixth query is the same as the fifth but without the last jump so the answer is \(1\).
|
Input: 6 5 2 3 100 1 4 1 10 5 1000000000 1 6 1 1 1000000000 1 1 999999999 | Output: 8 198 -17 2999999997 0 1
|
Beginner
| 2 | 688 | 340 | 87 | 15 |
1,886 |
E
|
1886E
|
E. I Wanna be the Team Leader
| 2,400 |
bitmasks; constructive algorithms; dp; greedy; math; sortings; two pointers
|
Monocarp is a team leader in a massive IT company.There are \(m\) projects his team of programmers has to complete, numbered from \(1\) to \(m\). The \(i\)-th project has a difficulty level \(b_i\).There are \(n\) programmers in the team, numbered from \(1\) to \(n\). The \(j\)-th programmer has a stress tolerance level \(a_j\).Monocarp wants to assign the programmers to the projects in such a way that: each programmer is assigned to no more than one project; each project has at least one programmer assigned to it; let \(k\) programmers be assigned to the \(i\)-th project; then all the assigned programmers have to have a stress tolerance level greater than or equal to \(\frac{b_i}{k}\). Help Monocarp to find a valid assignment. If there are multiple answers, print any of them.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le m \le 20\)) β the number of programmers and the number of projects.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)) β the stress tolerance level of each programmer.The third line contains \(m\) integers \(b_1, b_2, \dots, b_m\) (\(1 \le b_i \le 10^9\)) β the difficulty level of each project.
|
If there is no valid assignment, print ""NO"".Otherwise, in the first line, print ""YES"". In the \(i\)-th of the next \(m\) lines, print the list of the programmers assigned to the \(i\)-th project: first, the number of programmers, then their indices in an arbitrary order.If there are multiple answers, print any of them.
|
Input: 5 3 4 6 100 5 1 50 1 12 | Output: YES 1 3 1 5 3 2 4 1
|
Expert
| 7 | 787 | 425 | 324 | 18 |
|
725 |
F
|
725F
|
F. Family Photos
| 2,900 |
games; greedy
|
Alice and Bonnie are sisters, but they don't like each other very much. So when some old family photos were found in the attic, they started to argue about who should receive which photos. In the end, they decided that they would take turns picking photos. Alice goes first.There are n stacks of photos. Each stack contains exactly two photos. In each turn, a player may take only a photo from the top of one of the stacks.Each photo is described by two non-negative integers a and b, indicating that it is worth a units of happiness to Alice and b units of happiness to Bonnie. Values of a and b might differ for different photos.It's allowed to pass instead of taking a photo. The game ends when all photos are taken or both players pass consecutively.The players don't act to maximize their own happiness. Instead, each player acts to maximize the amount by which her happiness exceeds her sister's. Assuming both players play optimal, find the difference between Alice's and Bonnie's happiness. That is, if there's a perfectly-played game such that Alice has x happiness and Bonnie has y happiness at the end, you should print x - y.
|
The first line of input contains a single integer n (1 β€ n β€ 100 000) β the number of two-photo stacks. Then follow n lines, each describing one of the stacks. A stack is described by four space-separated non-negative integers a1, b1, a2 and b2, each not exceeding 109. a1 and b1 describe the top photo in the stack, while a2 and b2 describe the bottom photo in the stack.
|
Output a single integer: the difference between Alice's and Bonnie's happiness if both play optimally.
|
Input: 212 3 4 71 15 9 1 | Output: 1
|
Master
| 2 | 1,137 | 372 | 102 | 7 |
|
1,668 |
B
|
1668B
|
B. Social Distance
| 900 |
greedy; math; sortings
|
\(m\) chairs are arranged in a circle sequentially. The chairs are numbered from \(0\) to \(m-1\). \(n\) people want to sit in these chairs. The \(i\)-th of them wants at least \(a[i]\) empty chairs both on his right and left side. More formally, if the \(i\)-th person sits in the \(j\)-th chair, then no one else should sit in the following chairs: \((j-a[i]) \bmod m\), \((j-a[i]+1) \bmod m\), ... \((j+a[i]-1) \bmod m\), \((j+a[i]) \bmod m\).Decide if it is possible to sit down for all of them, under the given limitations.
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 5 \cdot 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(2 \leq n \leq 10^5\), \(1 \leq m \leq 10^9\)) β the number of people and the number of chairs.The next line contains \(n\) integers, \(a_1\), \(a_2\), ... \(a_n\) (\(1 \leq a_i \leq 10^9\)) β the minimum number of empty chairs, on both sides of the \(i\)-th person.It is guaranteed that the sum of \(n\) over all test cases will not exceed \(10^5\).
|
For each test case print ""YES"" (without quotes) if it is possible for everyone to sit down and fulfil the restrictions, and ""NO"" (without quotes) otherwise.You may print every letter in any case you want (so, for example, the strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will all be recognized as positive answers).
|
Test case \(1\): \(n>m\), so they can not sit down.Test case \(2\): the first person can sit \(2\)-nd and the second person can sit in the \(0\)-th chair. Both of them want at least \(1\) empty chair on both sides, chairs \(1\) and \(3\) are free, so this is a good solution.Test case \(3\): if the second person sits down somewhere, he needs \(2\) empty chairs, both on his right and on his left side, so it is impossible to find a place for the first person, because there are only \(5\) chairs.Test case \(4\): they can sit in the \(1\)-st, \(4\)-th, \(7\)-th chairs respectively.
|
Input: 6 3 2 1 1 1 2 4 1 1 2 5 2 1 3 8 1 2 1 4 12 1 2 1 3 4 19 1 2 1 3 | Output: NO YES NO YES NO YES
|
Beginner
| 3 | 528 | 616 | 320 | 16 |
409 |
C
|
409C
|
C. Magnum Opus
| 1,700 |
*special
|
Salve, mi amice.Et tu quidem de lapis philosophorum. Barba non facit philosophum. Labor omnia vincit. Non potest creatio ex nihilo. Necesse est partibus.Rp: I Aqua Fortis I Aqua Regia II Amalgama VII Minium IV VitriolMisce in vitro et Γ¦stus, et nil admirari. Festina lente, et nulla tenaci invia est via.Fac et spera,Vale,Nicolas Flamel
|
The first line of input contains several space-separated integers ai (0 β€ ai β€ 100).
|
Print a single integer.
|
Input: 2 4 6 8 10 | Output: 1
|
Medium
| 1 | 336 | 84 | 23 | 4 |
|
650 |
B
|
650B
|
B. Image Preview
| 1,900 |
binary search; brute force; dp; two pointers
|
Vasya's telephone contains n photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo n. Similarly, by swiping right from the last photo you reach photo 1. It takes a seconds to swipe from photo to adjacent.For each photo it is known which orientation is intended for it β horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes b second to change orientation of the photo.Vasya has T seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends b seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos.Help Vasya find the maximum number of photos he is able to watch during T seconds.
|
The first line of the input contains 4 integers n, a, b, T (1 β€ n β€ 5Β·105, 1 β€ a, b β€ 1000, 1 β€ T β€ 109) β the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo.Second line of the input contains a string of length n containing symbols 'w' and 'h'. If the i-th position of a string contains 'w', then the photo i should be seen in the horizontal orientation.If the i-th position of a string contains 'h', then the photo i should be seen in vertical orientation.
|
Output the only integer, the maximum number of photos Vasya is able to watch during those T seconds.
|
In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds.Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it.
|
Input: 4 2 3 10wwhw | Output: 2
|
Hard
| 4 | 1,085 | 549 | 100 | 6 |
1,301 |
F
|
1301F
|
F. Super Jaber
| 2,600 |
dfs and similar; graphs; implementation; shortest paths
|
Jaber is a superhero in a large country that can be described as a grid with \(n\) rows and \(m\) columns, where every cell in that grid contains a different city.Jaber gave every city in that country a specific color between \(1\) and \(k\). In one second he can go from the current city to any of the cities adjacent by the side or to any city with the same color as the current city color.Jaber has to do \(q\) missions. In every mission he will be in the city at row \(r_1\) and column \(c_1\), and he should help someone in the city at row \(r_2\) and column \(c_2\).Jaber wants your help to tell him the minimum possible time to go from the starting city to the finishing city for every mission.
|
The first line contains three integers \(n\), \(m\) and \(k\) (\(1 \leq n, m \leq 1000\), \(1 \leq k \leq min(40 , n \cdot m)\)) β the number of rows, columns and colors.Each of the next \(n\) lines contains \(m\) integers. In the \(i\)-th line, the \(j\)-th integer is \(a_{ij}\) (\(1 \leq a_{ij} \leq k\)), which is the color assigned to the city in the \(i\)-th row and \(j\)-th column.The next line contains one integer \(q\) (\(1 \leq q \leq 10^{5}\)) β the number of missions.For the next \(q\) lines, every line contains four integers \(r_1\), \(c_1\), \(r_2\), \(c_2\) (\(1 \leq r_1 , r_2 \leq n\), \(1 \leq c_1 , c_2 \leq m\)) β the coordinates of the starting and the finishing cities of the corresponding mission.It is guaranteed that for every color between \(1\) and \(k\) there is at least one city of that color.
|
For every mission print the minimum possible time to reach city at the cell \((r_2, c_2)\) starting from city at the cell \((r_1, c_1)\).
|
In the first example: mission \(1\): Jaber should go from the cell \((1,1)\) to the cell \((3,3)\) because they have the same colors, then from the cell \((3,3)\) to the cell \((3,4)\) because they are adjacent by side (two moves in total); mission \(2\): Jaber already starts in the finishing cell. In the second example: mission \(1\): \((1,1)\) \(\rightarrow\) \((1,2)\) \(\rightarrow\) \((2,2)\); mission \(2\): \((1,1)\) \(\rightarrow\) \((3,2)\) \(\rightarrow\) \((3,3)\) \(\rightarrow\) \((3,4)\); mission \(3\): \((1,1)\) \(\rightarrow\) \((3,2)\) \(\rightarrow\) \((3,3)\) \(\rightarrow\) \((2,4)\); mission \(4\): \((1,1)\) \(\rightarrow\) \((1,2)\) \(\rightarrow\) \((1,3)\) \(\rightarrow\) \((1,4)\) \(\rightarrow\) \((4,4)\).
|
Input: 3 4 5 1 2 1 3 4 4 5 5 1 2 1 3 2 1 1 3 4 2 2 2 2 | Output: 2 0
|
Expert
| 4 | 701 | 827 | 137 | 13 |
1,156 |
D
|
1156D
|
D. 0-1-Tree
| 2,200 |
dfs and similar; divide and conquer; dp; dsu; trees
|
You are given a tree (an undirected connected acyclic graph) consisting of \(n\) vertices and \(n - 1\) edges. A number is written on each edge, each number is either \(0\) (let's call such edges \(0\)-edges) or \(1\) (those are \(1\)-edges).Let's call an ordered pair of vertices \((x, y)\) (\(x \ne y\)) valid if, while traversing the simple path from \(x\) to \(y\), we never go through a \(0\)-edge after going through a \(1\)-edge. Your task is to calculate the number of valid pairs in the tree.
|
The first line contains one integer \(n\) (\(2 \le n \le 200000\)) β the number of vertices in the tree.Then \(n - 1\) lines follow, each denoting an edge of the tree. Each edge is represented by three integers \(x_i\), \(y_i\) and \(c_i\) (\(1 \le x_i, y_i \le n\), \(0 \le c_i \le 1\), \(x_i \ne y_i\)) β the vertices connected by this edge and the number written on it, respectively.It is guaranteed that the given edges form a tree.
|
Print one integer β the number of valid pairs of vertices.
|
The picture corresponding to the first example:
|
Input: 7 2 1 1 3 2 0 4 2 1 5 2 0 6 7 1 7 2 1 | Output: 34
|
Hard
| 5 | 501 | 436 | 58 | 11 |
1,017 |
A
|
1017A
|
A. The Rank
| 800 |
implementation
|
John Smith knows that his son, Thomas Smith, is among the best students in his class and even in his school. After the students of the school took the exams in English, German, Math, and History, a table of results was formed.There are \(n\) students, each of them has a unique id (from \(1\) to \(n\)). Thomas's id is \(1\). Every student has four scores correspond to his or her English, German, Math, and History scores. The students are given in order of increasing of their ids.In the table, the students will be sorted by decreasing the sum of their scores. So, a student with the largest sum will get the first place. If two or more students have the same sum, these students will be sorted by increasing their ids. Please help John find out the rank of his son.
|
The first line contains a single integer \(n\) (\(1 \le n \le 1000\)) β the number of students.Each of the next \(n\) lines contains four integers \(a_i\), \(b_i\), \(c_i\), and \(d_i\) (\(0\leq a_i, b_i, c_i, d_i\leq 100\)) β the grades of the \(i\)-th student on English, German, Math, and History. The id of the \(i\)-th student is equal to \(i\).
|
Print the rank of Thomas Smith. Thomas's id is \(1\).
|
In the first sample, the students got total scores: \(398\), \(400\), \(398\), \(379\), and \(357\). Among the \(5\) students, Thomas and the third student have the second highest score, but Thomas has a smaller id, so his rank is \(2\).In the second sample, the students got total scores: \(369\), \(240\), \(310\), \(300\), \(300\), and \(0\). Among the \(6\) students, Thomas got the highest score, so his rank is \(1\).
|
Input: 5100 98 100 100100 100 100 100100 100 99 9990 99 90 100100 98 60 99 | Output: 2
|
Beginner
| 1 | 769 | 350 | 53 | 10 |
1,152 |
F2
|
1152F2
|
F2. Neko Rules the Catniverse (Large Version)
| 3,000 |
bitmasks; dp; matrices
|
This problem is same as the previous one, but has larger constraints.Aki is playing a new video game. In the video game, he will control Neko, the giant cat, to fly between planets in the Catniverse.There are \(n\) planets in the Catniverse, numbered from \(1\) to \(n\). At the beginning of the game, Aki chooses the planet where Neko is initially located. Then Aki performs \(k - 1\) moves, where in each move Neko is moved from the current planet \(x\) to some other planet \(y\) such that: Planet \(y\) is not visited yet. \(1 \leq y \leq x + m\) (where \(m\) is a fixed constant given in the input) This way, Neko will visit exactly \(k\) different planets. Two ways of visiting planets are called different if there is some index \(i\) such that, the \(i\)-th planet visited in the first way is different from the \(i\)-th planet visited in the second way.What is the total number of ways to visit \(k\) planets this way? Since the answer can be quite large, print it modulo \(10^9 + 7\).
|
The only line contains three integers \(n\), \(k\) and \(m\) (\(1 \le n \le 10^9\), \(1 \le k \le \min(n, 12)\), \(1 \le m \le 4\)) β the number of planets in the Catniverse, the number of planets Neko needs to visit and the said constant \(m\).
|
Print exactly one integer β the number of different ways Neko can visit exactly \(k\) planets. Since the answer can be quite large, print it modulo \(10^9 + 7\).
|
In the first example, there are \(4\) ways Neko can visit all the planets: \(1 \rightarrow 2 \rightarrow 3\) \(2 \rightarrow 3 \rightarrow 1\) \(3 \rightarrow 1 \rightarrow 2\) \(3 \rightarrow 2 \rightarrow 1\) In the second example, there are \(9\) ways Neko can visit exactly \(2\) planets: \(1 \rightarrow 2\) \(2 \rightarrow 1\) \(2 \rightarrow 3\) \(3 \rightarrow 1\) \(3 \rightarrow 2\) \(3 \rightarrow 4\) \(4 \rightarrow 1\) \(4 \rightarrow 2\) \(4 \rightarrow 3\) In the third example, with \(m = 4\), Neko can visit all the planets in any order, so there are \(5! = 120\) ways Neko can visit all the planets.In the fourth example, Neko only visit exactly \(1\) planet (which is also the planet he initially located), and there are \(100\) ways to choose the starting planet for Neko.
|
Input: 3 3 1 | Output: 4
|
Master
| 3 | 994 | 245 | 161 | 11 |
2,060 |
F
|
2060F
|
F. Multiplicative Arrays
| 2,200 |
combinatorics; dp; number theory
|
You're given integers \(k\) and \(n\). For each integer \(x\) from \(1\) to \(k\), count the number of integer arrays \(a\) such that all of the following are satisfied: \(1 \leq |a| \leq n\) where \(|a|\) represents the length of \(a\). \(1 \leq a_i \leq k\) for all \(1 \leq i \leq |a|\). \(a_1 \times a_2 \times \dots \times a_{|a|}=x\) (i.e., the product of all elements is \(x\)). Note that two arrays \(b\) and \(c\) are different if either their lengths are different, or if there exists an index \(1 \leq i \leq |b|\) such that \(b_i\neq c_i\).Output the answer modulo \(998\,244\,353\).
|
The first line contains an integer \(t\) (\(1 \leq t\leq 10^3\)) β the number of independent test cases.The only line of each test case contains two integers \(k\) and \(n\) (\(1 \leq k \leq 10^5, 1\leq n \leq 9\cdot 10^8\)).It is guaranteed that the sum of \(k\) over all test cases does not exceed \(10^5\).
|
For each test case, output \(k\) space-separated integers on a new line: the number of arrays for \(x=1,2,\ldots,k\), modulo \(998\,244\,353\).
|
In the first test case, there are \(2\) arrays \(a\) with \(|a|\leq 2\) and the product of elements equal to \(1\): \([1]\) \([1,1]\) There are \(3\) arrays \(a\) with \(|a|\leq 2\) and the product of elements equal to \(2\): \([2]\) \([1,2]\) \([2,1]\)
|
Input: 32 24 310 6969420 | Output: 2 3 3 6 6 10 6969420 124188773 124188773 729965558 124188773 337497990 124188773 50981194 729965558 337497990
|
Hard
| 3 | 595 | 309 | 143 | 20 |
15 |
B
|
15B
|
B. Laser
| 1,800 |
math
|
Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of n Γ m cells and a robotic arm. Each cell of the field is a 1 Γ 1 square. The robotic arm has two lasers pointed at the field perpendicularly to its surface. At any one time lasers are pointed at the centres of some two cells. Since the lasers are on the robotic hand, their movements are synchronized β if you move one of the lasers by a vector, another one moves by the same vector.The following facts about the experiment are known: initially the whole field is covered with a chocolate bar of the size n Γ m, both lasers are located above the field and are active; the chocolate melts within one cell of the field at which the laser is pointed; all moves of the robotic arm should be parallel to the sides of the field, after each move the lasers should be pointed at the centres of some two cells; at any one time both lasers should be pointed at the field. Petya doesn't want to become a second Gordon Freeman. You are given n, m and the cells (x1, y1) and (x2, y2), where the lasers are initially pointed at (xi is a column number, yi is a row number). Rows are numbered from 1 to m from top to bottom and columns are numbered from 1 to n from left to right. You are to find the amount of cells of the field on which the chocolate can't be melted in the given conditions.
|
The first line contains one integer number t (1 β€ t β€ 10000) β the number of test sets. Each of the following t lines describes one test set. Each line contains integer numbers n, m, x1, y1, x2, y2, separated by a space (2 β€ n, m β€ 109, 1 β€ x1, x2 β€ n, 1 β€ y1, y2 β€ m). Cells (x1, y1) and (x2, y2) are distinct.
|
Each of the t lines of the output should contain the answer to the corresponding input test set.
|
Input: 24 4 1 1 3 34 3 1 1 2 2 | Output: 82
|
Medium
| 1 | 1,491 | 311 | 96 | 0 |
|
1,204 |
B
|
1204B
|
B. Mislove Has Lost an Array
| 900 |
greedy; math
|
Mislove had an array \(a_1\), \(a_2\), \(\cdots\), \(a_n\) of \(n\) positive integers, but he has lost it. He only remembers the following facts about it: The number of different numbers in the array is not less than \(l\) and is not greater than \(r\); For each array's element \(a_i\) either \(a_i = 1\) or \(a_i\) is even and there is a number \(\dfrac{a_i}{2}\) in the array.For example, if \(n=5\), \(l=2\), \(r=3\) then an array could be \([1,2,2,4,4]\) or \([1,1,1,1,2]\); but it couldn't be \([1,2,2,4,8]\) because this array contains \(4\) different numbers; it couldn't be \([1,2,2,3,3]\) because \(3\) is odd and isn't equal to \(1\); and it couldn't be \([1,1,2,2,16]\) because there is a number \(16\) in the array but there isn't a number \(\frac{16}{2} = 8\).According to these facts, he is asking you to count the minimal and the maximal possible sums of all elements in an array.
|
The only input line contains three integers \(n\), \(l\) and \(r\) (\(1 \leq n \leq 1\,000\), \(1 \leq l \leq r \leq \min(n, 20)\)) β an array's size, the minimal number and the maximal number of distinct elements in an array.
|
Output two numbers β the minimal and the maximal possible sums of all elements in an array.
|
In the first example, an array could be the one of the following: \([1,1,1,2]\), \([1,1,2,2]\) or \([1,2,2,2]\). In the first case the minimal sum is reached and in the last case the maximal sum is reached.In the second example, the minimal sum is reached at the array \([1,1,1,1,1]\), and the maximal one is reached at the array \([1,2,4,8,16]\).
|
Input: 4 2 2 | Output: 5 7
|
Beginner
| 2 | 896 | 226 | 91 | 12 |
1,010 |
C
|
1010C
|
C. Border
| 1,800 |
number theory
|
Astronaut Natasha arrived on Mars. She knows that the Martians are very poor aliens. To ensure a better life for the Mars citizens, their emperor decided to take tax from every tourist who visited the planet. Natasha is the inhabitant of Earth, therefore she had to pay the tax to enter the territory of Mars.There are \(n\) banknote denominations on Mars: the value of \(i\)-th banknote is \(a_i\). Natasha has an infinite number of banknotes of each denomination.Martians have \(k\) fingers on their hands, so they use a number system with base \(k\). In addition, the Martians consider the digit \(d\) (in the number system with base \(k\)) divine. Thus, if the last digit in Natasha's tax amount written in the number system with the base \(k\) is \(d\), the Martians will be happy. Unfortunately, Natasha does not know the Martians' divine digit yet.Determine for which values \(d\) Natasha can make the Martians happy.Natasha can use only her banknotes. Martians don't give her change.
|
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 100\,000\), \(2 \le k \le 100\,000\)) β the number of denominations of banknotes and the base of the number system on Mars.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)) β denominations of banknotes on Mars.All numbers are given in decimal notation.
|
On the first line output the number of values \(d\) for which Natasha can make the Martians happy.In the second line, output all these values in increasing order.Print all numbers in decimal notation.
|
Consider the first test case. It uses the octal number system.If you take one banknote with the value of \(12\), you will get \(14_8\) in octal system. The last digit is \(4_8\).If you take one banknote with the value of \(12\) and one banknote with the value of \(20\), the total value will be \(32\). In the octal system, it is \(40_8\). The last digit is \(0_8\).If you take two banknotes with the value of \(20\), the total value will be \(40\), this is \(50_8\) in the octal system. The last digit is \(0_8\).No other digits other than \(0_8\) and \(4_8\) can be obtained. Digits \(0_8\) and \(4_8\) could also be obtained in other ways.The second test case uses the decimal number system. The nominals of all banknotes end with zero, so Natasha can give the Martians only the amount whose decimal notation also ends with zero.
|
Input: 2 812 20 | Output: 20 4
|
Medium
| 1 | 991 | 361 | 200 | 10 |
1,312 |
G
|
1312G
|
G. Autocompletion
| 2,600 |
data structures; dfs and similar; dp
|
You are given a set of strings \(S\). Each string consists of lowercase Latin letters.For each string in this set, you want to calculate the minimum number of seconds required to type this string. To type a string, you have to start with an empty string and transform it into the string you want to type using the following actions: if the current string is \(t\), choose some lowercase Latin letter \(c\) and append it to the back of \(t\), so the current string becomes \(t + c\). This action takes \(1\) second; use autocompletion. When you try to autocomplete the current string \(t\), a list of all strings \(s \in S\) such that \(t\) is a prefix of \(s\) is shown to you. This list includes \(t\) itself, if \(t\) is a string from \(S\), and the strings are ordered lexicographically. You can transform \(t\) into the \(i\)-th string from this list in \(i\) seconds. Note that you may choose any string from this list you want, it is not necessarily the string you are trying to type. What is the minimum number of seconds that you have to spend to type each string from \(S\)?Note that the strings from \(S\) are given in an unusual way.
|
The first line contains one integer \(n\) (\(1 \le n \le 10^6\)).Then \(n\) lines follow, the \(i\)-th line contains one integer \(p_i\) (\(0 \le p_i < i\)) and one lowercase Latin character \(c_i\). These lines form some set of strings such that \(S\) is its subset as follows: there are \(n + 1\) strings, numbered from \(0\) to \(n\); the \(0\)-th string is an empty string, and the \(i\)-th string (\(i \ge 1\)) is the result of appending the character \(c_i\) to the string \(p_i\). It is guaranteed that all these strings are distinct.The next line contains one integer \(k\) (\(1 \le k \le n\)) β the number of strings in \(S\).The last line contains \(k\) integers \(a_1\), \(a_2\), ..., \(a_k\) (\(1 \le a_i \le n\), all \(a_i\) are pairwise distinct) denoting the indices of the strings generated by above-mentioned process that form the set \(S\) β formally, if we denote the \(i\)-th generated string as \(s_i\), then \(S = {s_{a_1}, s_{a_2}, \dots, s_{a_k}}\).
|
Print \(k\) integers, the \(i\)-th of them should be equal to the minimum number of seconds required to type the string \(s_{a_i}\).
|
In the first example, \(S\) consists of the following strings: ieh, iqgp, i, iqge, ier.
|
Input: 10 0 i 1 q 2 g 0 k 1 e 5 r 4 m 5 h 3 p 3 e 5 8 9 1 10 6 | Output: 2 4 1 3 3
|
Expert
| 3 | 1,144 | 973 | 132 | 13 |
734 |
F
|
734F
|
F. Anton and School
| 2,500 |
bitmasks; constructive algorithms; implementation; math
|
Anton goes to school, his favorite lessons are arraystudying. He usually solves all the tasks pretty fast, but this time the teacher gave him a complicated one: given two arrays b and c of length n, find array a, such that:where a and b means bitwise AND, while a or b means bitwise OR.Usually Anton is good in arraystudying, but this problem is too hard, so Anton asks you to help.
|
The first line of the input contains a single integers n (1 β€ n β€ 200 000) β the size of arrays b and c.The second line contains n integers bi (0 β€ bi β€ 109) β elements of the array b.Third line contains n integers ci (0 β€ ci β€ 109) β elements of the array c.
|
If there is no solution, print - 1.Otherwise, the only line of the output should contain n non-negative integers ai β elements of the array a. If there are multiple possible solutions, you may print any of them.
|
Input: 46 8 4 416 22 10 10 | Output: 3 5 1 1
|
Expert
| 4 | 382 | 259 | 211 | 7 |
|
434 |
D
|
434D
|
D. Nanami's Power Plant
| 2,900 |
flows
|
Nanami likes playing games, and is also really good at it. This day she was playing a new game which involved operating a power plant. Nanami's job is to control the generators in the plant and produce maximum output.There are n generators in the plant. Each generator should be set to a generating level. Generating level is an integer (possibly zero or negative), the generating level of the i-th generator should be between li and ri (both inclusive). The output of a generator can be calculated using a certain quadratic function f(x), where x is the generating level of the generator. Each generator has its own function, the function of the i-th generator is denoted as fi(x).However, there are m further restrictions to the generators. Let the generating level of the i-th generator be xi. Each restriction is of the form xu β€ xv + d, where u and v are IDs of two different generators and d is an integer.Nanami found the game tedious but giving up is against her creed. So she decided to have a program written to calculate the answer for her (the maximum total output of generators). Somehow, this became your job.
|
The first line contains two integers n and m (1 β€ n β€ 50; 0 β€ m β€ 100) β the number of generators and the number of restrictions.Then follow n lines, each line contains three integers ai, bi, and ci (|ai| β€ 10; |bi|, |ci| β€ 1000) β the coefficients of the function fi(x). That is, fi(x) = aix2 + bix + ci.Then follow another n lines, each line contains two integers li and ri ( - 100 β€ li β€ ri β€ 100).Then follow m lines, each line contains three integers ui, vi, and di (1 β€ ui, vi β€ n; ui β vi; |di| β€ 200), describing a restriction. The i-th restriction is xui β€ xvi + di.
|
Print a single line containing a single integer β the maximum output of all the generators. It is guaranteed that there exists at least one valid configuration.
|
In the first sample, f1(x) = x, f2(x) = x + 1, and f3(x) = x + 2, so we are to maximize the sum of the generating levels. The restrictions are x1 β€ x2, x2 β€ x3, and x3 β€ x1, which gives us x1 = x2 = x3. The optimal configuration is x1 = x2 = x3 = 2, which produces an output of 9.In the second sample, restrictions are equal to |xi - xi + 1| β€ 3 for 1 β€ i < n. One of the optimal configurations is x1 = 1, x2 = 4, x3 = 5, x4 = 8 and x5 = 7.
|
Input: 3 30 1 00 1 10 1 20 31 2-100 1001 2 02 3 03 1 0 | Output: 9
|
Master
| 1 | 1,123 | 575 | 160 | 4 |
924 |
F
|
924F
|
F. Minimal Subset Difference
| 3,200 |
dp
|
We call a positive integer x a k-beautiful integer if and only if it is possible to split the multiset of its digits in the decimal representation into two subsets such that the difference between the sum of digits in one subset and the sum of digits in the other subset is less than or equal to k. Each digit should belong to exactly one subset after the split.There are n queries for you. Each query is described with three integers l, r and k, which mean that you are asked how many integers x between l and r (inclusive) are k-beautiful.
|
The first line contains a single integer n (1 β€ n β€ 5Β·104), indicating the number of queries.Each of the next n lines describes a query, containing three integers l, r and k (1 β€ l β€ r β€ 1018, 0 β€ k β€ 9).
|
For each query print a single number β the answer to the query.
|
If 1 β€ x β€ 9, integer x is k-beautiful if and only if x β€ k.If 10 β€ x β€ 99, integer x = 10a + b is k-beautiful if and only if |a - b| β€ k, where a and b are integers between 0 and 9, inclusive.100 is k-beautiful if and only if k β₯ 1.
|
Input: 101 100 01 100 11 100 21 100 31 100 41 100 51 100 61 100 71 100 81 100 9 | Output: 92844587080889498100
|
Master
| 1 | 541 | 204 | 63 | 9 |
2,053 |
I1
|
2053I1
|
I1. Affectionate Arrays (Easy Version)
| 2,800 |
data structures; dp; greedy
|
You are the beginning of the letter, the development of a poem, and the end of a fairy tale.β ilem, Pinky PromiseThis is the easy version of the problem. The difference between the versions is that in this version, you need to compute the minimum length of the arrays. You can hack only if you solved all versions of this problem. Iris treasures an integer array \(a_1, a_2, \ldots, a_n\). She knows this array has an interesting property: the maximum absolute value of all elements is less than or equal to the sum of all elements, that is, \(\max(\lvert a_i\rvert) \leq \sum a_i\).Iris defines the boredom of an array as its maximum subarray\(^{\text{β}}\) sum. Iris's birthday is coming, and Victor is going to send her another array \(b_1, b_2, \ldots, b_m\) as a gift. For some seemingly obvious reasons, he decides the array \(b_1, b_2, \ldots, b_m\) should have the following properties. \(a_1, a_2, \ldots, a_n\) should be a subsequence\(^{\text{β }}\) of \(b_1, b_2, \ldots, b_m\). The two arrays have the same sum. That is, \(\sum\limits_{i=1}^n a_i = \sum\limits_{i=1}^m b_i\). The boredom of \(b_1, b_2, \ldots, b_m\) is the smallest possible. Among the arrays with the smallest boredom, the length of the array \(b\) (i.e., \(m\)) is the smallest possible. And in this case, Iris will understand his regard as soon as possible! Even constrained as above, there are still too many possible gifts. So Victor asks you to compute the value of \(\boldsymbol{m}\) of any array \(b_1, b_2, \ldots, b_m\) satisfying all the conditions above. He promises you: if you help him successfully, he will share a bit of Iris's birthday cake with you.Note: since the input is large, you may need to optimize it for this problem.For example, in C++, it is enough to use the following lines at the start of the main() function:int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr);}\(^{\text{β}}\)An array \(c\) is a subarray of an array \(d\) if \(c\) can be obtained from \(d\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. \(^{\text{β }}\)A sequence \(c\) is a subsequence of a sequence \(d\) if \(c\) can be obtained from \(d\) by the deletion of several (possibly, zero or all) element from arbitrary positions.
|
Each test contains multiple test cases. The first line of input contains an integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 3\cdot 10^6\)) β the length of the array \(a_1, a_2, \ldots, a_n\).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 initial array. It is guaranteed that \(\max(\lvert a_i\rvert) \leq \sum a_i\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(3\cdot 10^6\).
|
For each test case, output a single line containing an integer: the length \(m\) of a valid array \(b\).
|
In the first test case, \(a=[1, 2, 3, 4]\). The only array \(b\) which satisfies all the properties above is \([1, 2, 3, 4]\), so we should output \(4\).In the second test case, \(a=[2, -3, 2, 2]\). The possible arrays \(b\) are \([1, 2, -3, 2, -1, 2]\) and \([2, 1, -3, 2, -1, 2]\), so we should output \(6\).
|
Input: 441 2 3 442 -3 2 2102 -7 6 3 -1 4 2 -5 8 -4204 -2 4 3 -2 1 5 2 3 6 -5 -1 -4 -2 -3 5 -3 1 -4 1 | Output: 4 6 14 25
|
Master
| 3 | 2,339 | 620 | 104 | 20 |
1,619 |
D
|
1619D
|
D. New Year's Problem
| 1,800 |
binary search; greedy; sortings
|
Vlad has \(n\) friends, for each of whom he wants to buy one gift for the New Year.There are \(m\) shops in the city, in each of which he can buy a gift for any of his friends. If the \(j\)-th friend (\(1 \le j \le n\)) receives a gift bought in the shop with the number \(i\) (\(1 \le i \le m\)), then the friend receives \(p_{ij}\) units of joy. The rectangular table \(p_{ij}\) is given in the input.Vlad has time to visit at most \(n-1\) shops (where \(n\) is the number of friends). He chooses which shops he will visit and for which friends he will buy gifts in each of them.Let the \(j\)-th friend receive \(a_j\) units of joy from Vlad's gift. Let's find the value \(\alpha=\min\{a_1, a_2, \dots, a_n\}\). Vlad's goal is to buy gifts so that the value of \(\alpha\) is as large as possible. In other words, Vlad wants to maximize the minimum of the joys of his friends.For example, let \(m = 2\), \(n = 2\). Let the joy from the gifts that we can buy in the first shop: \(p_{11} = 1\), \(p_{12}=2\), in the second shop: \(p_{21} = 3\), \(p_{22}=4\).Then it is enough for Vlad to go only to the second shop and buy a gift for the first friend, bringing joy \(3\), and for the second β bringing joy \(4\). In this case, the value \(\alpha\) will be equal to \(\min\{3, 4\} = 3\)Help Vlad choose gifts for his friends so that the value of \(\alpha\) is as high as possible. Please note that each friend must receive one gift. Vlad can visit at most \(n-1\) shops (where \(n\) is the number of friends). In the shop, he can buy any number of gifts.
|
The first line of the input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the input.An empty line is written before each test case. Then there is a line containing integers \(m\) and \(n\) (\(2 \le n\), \(2 \le n \cdot m \le 10^5\)) separated by a space β the number of shops and the number of friends, where \(n \cdot m\) is the product of \(n\) and \(m\).Then \(m\) lines follow, each containing \(n\) numbers. The number in the \(i\)-th row of the \(j\)-th column \(p_{ij}\) (\(1 \le p_{ij} \le 10^9\)) is the joy of the product intended for friend number \(j\) in shop number \(i\).It is guaranteed that the sum of the values \(n \cdot m\) over all test cases in the test does not exceed \(10^5\).
|
Print \(t\) lines, each line must contain the answer to the corresponding test case β the maximum possible value of \(\alpha\), where \(\alpha\) is the minimum of the joys from a gift for all of Vlad's friends.
|
Input: 5 2 2 1 2 3 4 4 3 1 3 1 3 1 1 1 2 2 1 1 3 2 3 5 3 4 2 5 1 4 2 7 9 8 1 9 6 10 8 2 4 6 5 2 1 7 9 7 2 | Output: 3 2 4 8 2
|
Medium
| 3 | 1,552 | 734 | 210 | 16 |
|
474 |
C
|
474C
|
C. Captain Marmot
| 2,000 |
brute force; geometry
|
Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles.Initially, each mole i (1 β€ i β€ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible.Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi).A regiment is compact only if the position points of the 4 moles form a square with non-zero area.Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible.
|
The first line contains one integer n (1 β€ n β€ 100), the number of regiments.The next 4n lines contain 4 integers xi, yi, ai, bi ( - 104 β€ xi, yi, ai, bi β€ 104).
|
Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print ""-1"" (without quotes).
|
In the first regiment we can move once the second or the third mole.We can't make the second regiment compact.In the third regiment, from the last 3 moles we can move once one and twice another one.In the fourth regiment, we can move twice the first mole and once the third mole.
|
Input: 41 1 0 0-1 1 0 0-1 1 0 01 -1 0 01 1 0 0-2 1 0 0-1 1 0 01 -1 0 01 1 0 0-1 1 0 0-1 1 0 0-1 1 0 02 2 0 1-1 0 0 -23 0 0 -2-1 1 -2 0 | Output: 1-133
|
Hard
| 2 | 760 | 161 | 215 | 4 |
1,296 |
D
|
1296D
|
D. Fight with Monsters
| 1,500 |
greedy; sortings
|
There are \(n\) monsters standing in a row numbered from \(1\) to \(n\). The \(i\)-th monster has \(h_i\) health points (hp). You have your attack power equal to \(a\) hp and your opponent has his attack power equal to \(b\) hp.You and your opponent are fighting these monsters. Firstly, you and your opponent go to the first monster and fight it till his death, then you and your opponent go the second monster and fight it till his death, and so on. A monster is considered dead if its hp is less than or equal to \(0\).The fight with a monster happens in turns. You hit the monster by \(a\) hp. If it is dead after your hit, you gain one point and you both proceed to the next monster. Your opponent hits the monster by \(b\) hp. If it is dead after his hit, nobody gains a point and you both proceed to the next monster. You have some secret technique to force your opponent to skip his turn. You can use this technique at most \(k\) times in total (for example, if there are two monsters and \(k=4\), then you can use the technique \(2\) times on the first monster and \(1\) time on the second monster, but not \(2\) times on the first monster and \(3\) times on the second monster).Your task is to determine the maximum number of points you can gain if you use the secret technique optimally.
|
The first line of the input contains four integers \(n, a, b\) and \(k\) (\(1 \le n \le 2 \cdot 10^5, 1 \le a, b, k \le 10^9\)) β the number of monsters, your attack power, the opponent's attack power and the number of times you can use the secret technique.The second line of the input contains \(n\) integers \(h_1, h_2, \dots, h_n\) (\(1 \le h_i \le 10^9\)), where \(h_i\) is the health points of the \(i\)-th monster.
|
Print one integer β the maximum number of points you can gain if you use the secret technique optimally.
|
Input: 6 2 3 3 7 10 50 12 1 8 | Output: 5
|
Medium
| 2 | 1,298 | 421 | 104 | 12 |
|
201 |
E
|
201E
|
E. Thoroughly Bureaucratic Organization
| 2,600 |
binary search; combinatorics
|
Once n people simultaneously signed in to the reception at the recently opened, but already thoroughly bureaucratic organization (abbreviated TBO). As the organization is thoroughly bureaucratic, it can accept and cater for exactly one person per day. As a consequence, each of n people made an appointment on one of the next n days, and no two persons have an appointment on the same day.However, the organization workers are very irresponsible about their job, so none of the signed in people was told the exact date of the appointment. The only way to know when people should come is to write some requests to TBO.The request form consists of m empty lines. Into each of these lines the name of a signed in person can be written (it can be left blank as well). Writing a person's name in the same form twice is forbidden, such requests are ignored. TBO responds very quickly to written requests, but the reply format is of very poor quality β that is, the response contains the correct appointment dates for all people from the request form, but the dates are in completely random order. Responds to all requests arrive simultaneously at the end of the day (each response specifies the request that it answers).Fortunately, you aren't among these n lucky guys. As an observer, you have the following task β given n and m, determine the minimum number of requests to submit to TBO to clearly determine the appointment date for each person.
|
The first line contains a single integer t (1 β€ t β€ 1000) β the number of test cases. Each of the following t lines contains two integers n and m (1 β€ n, m β€ 109) β the number of people who have got an appointment at TBO and the number of empty lines in the request form, correspondingly.
|
Print t lines, each containing an answer for the corresponding test case (in the order they are given in the input) β the minimum number of requests to submit to TBO.
|
In the first sample, you need to submit three requests to TBO with three different names. When you learn the appointment dates of three people out of four, you can find out the fourth person's date by elimination, so you do not need a fourth request.In the second sample you need only two requests. Let's number the persons from 1 to 4 and mention persons 1 and 2 in the first request and persons 1 and 3 in the second request. It is easy to see that after that we can clearly determine each person's appointment date regardless of the answers obtained from TBO.In the fourth sample only one person signed up for an appointment. He doesn't need to submit any requests β his appointment date is tomorrow.
|
Input: 54 14 27 31 142 7 | Output: 323011
|
Expert
| 2 | 1,441 | 288 | 166 | 2 |
1,145 |
B
|
1145B
|
B. Kanban Numbers
| 0 |
*special; brute force
|
The input contains a single integer \(a\) (\(1 \le a \le 99\)).
|
Output ""YES"" or ""NO"".
|
Input: 5 | Output: YES
|
Beginner
| 2 | 0 | 63 | 25 | 11 |
||
1,835 |
E
|
1835E
|
E. Old Mobile
| 3,500 |
combinatorics; dp; probabilities
|
During the latest mission of the starship U.S.S. Coder, Captain Jan Bitovsky was accidentally teleported to the surface of an unknown planet. Trying to find his way back, Jan found an artifact from planet Earth's ancient civilization β a mobile device capable of interstellar calls created by Byterola. Unfortunately, there was another problem. Even though Jan, as a representative of humans, knew perfectly the old notation of the cell phone numbers, the symbols on the device's keyboard were completely worn down and invisible to the human eye. The old keyboards had exactly \(m + 1\) buttons, one for each digit from the base \(m\) numerical system, and one single backspace button allowing one to erase the last written digit (if nothing was written on the screen, then this button does nothing, but it's still counted as pressed).Jan would like to communicate with his crew. He needs to type a certain number (also from the base \(m\) numerical system, that is, digits from \(0\) to \(m - 1\)). He wants to know the expected number of button presses necessary to contact the U.S.S. Coder. Jan always chooses the most optimal buttons based on his current knowledge. Buttons are indistinguishable until pressed. Help him!
|
In the first line of input, there are two integer numbers \(n\) and \(m\) (\(1 \le n \le 10^6\), \(2 \le m \le 10^3\)) β the length of the number to U.S.S. Coder and the base of the numerical system.In the next and the last input line, there are \(n\) integers between \(0\) and \(m - 1\): the number to type in the base \(m\) numerical system.
|
Output the expected number of button presses modulo \(1\,000\,000\,007\).Formally, let \(M = 1\,000\,000\,007\). It can be shown that the answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
|
In the first example, two digits (\(0\) and \(1\)) and a backspace button are available on the keyboard. Jan has no way of knowing which one is which, so he presses a random one. With probability \(\frac{1}{3}\), he presses \(0\) and manages to type the crew's number. With probability \(\frac{1}{3}\), he presses backspace, and nothing happens. Then with probability \(\frac{1}{2}\) he manages to press \(0\) (finishing the process). Otherwise, with probability \(\frac{1}{2}\), he types \(1\), which he then needs to remove with backspace and hit the last button, which has to be \(0\). In this case, he needs \(4\) button presses.At last, he might press the \(1\) button first, also with probability \(\frac{1}{3}\). Then, if he presses the backspace with a chance of \(50\%\), he is all set and only needs to press the last button (3 presses in total). In the worst case, he would press the \(0\) button first and need to remove both with backspace, then finally type the number \(0\) (5 presses in total).We get the expected value of \(\frac{16}{6}\). The modular inverse of \(6\) modulo \(1\;000\;000\;007\) is \(166666668\), so \(16 \cdot 166666668 = 666666674 \mod \; 1\;000\;000\;007\)
|
Input: 1 2 0 | Output: 666666674
|
Master
| 3 | 1,224 | 344 | 433 | 18 |
1,004 |
C
|
1004C
|
C. Sonya and Robots
| 1,400 |
constructive algorithms; implementation
|
Since Sonya is interested in robotics too, she decided to construct robots that will read and recognize numbers.Sonya has drawn \(n\) numbers in a row, \(a_i\) is located in the \(i\)-th position. She also has put a robot at each end of the row (to the left of the first number and to the right of the last number). Sonya will give a number to each robot (they can be either same or different) and run them. When a robot is running, it is moving toward to another robot, reading numbers in the row. When a robot is reading a number that is equal to the number that was given to that robot, it will turn off and stay in the same position.Sonya does not want robots to break, so she will give such numbers that robots will stop before they meet. That is, the girl wants them to stop at different positions so that the first robot is to the left of the second one.For example, if the numbers \([1, 5, 4, 1, 3]\) are written, and Sonya gives the number \(1\) to the first robot and the number \(4\) to the second one, the first robot will stop in the \(1\)-st position while the second one in the \(3\)-rd position. In that case, robots will not meet each other. As a result, robots will not be broken. But if Sonya gives the number \(4\) to the first robot and the number \(5\) to the second one, they will meet since the first robot will stop in the \(3\)-rd position while the second one is in the \(2\)-nd position.Sonya understands that it does not make sense to give a number that is not written in the row because a robot will not find this number and will meet the other robot.Sonya is now interested in finding the number of different pairs that she can give to robots so that they will not meet. In other words, she wants to know the number of pairs (\(p\), \(q\)), where she will give \(p\) to the first robot and \(q\) to the second one. Pairs (\(p_i\), \(q_i\)) and (\(p_j\), \(q_j\)) are different if \(p_i\neq p_j\) or \(q_i\neq q_j\).Unfortunately, Sonya is busy fixing robots that broke after a failed launch. That is why she is asking you to find the number of pairs that she can give to robots so that they will not meet.
|
The first line contains a single integer \(n\) (\(1\leq n\leq 10^5\)) β the number of numbers in a row.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1\leq a_i\leq 10^5\)) β the numbers in a row.
|
Print one number β the number of possible pairs that Sonya can give to robots so that they will not meet.
|
In the first example, Sonya can give pairs (\(1\), \(1\)), (\(1\), \(3\)), (\(1\), \(4\)), (\(1\), \(5\)), (\(4\), \(1\)), (\(4\), \(3\)), (\(5\), \(1\)), (\(5\), \(3\)), and (\(5\), \(4\)).In the second example, Sonya can give pairs (\(1\), \(1\)), (\(1\), \(2\)), (\(1\), \(3\)), (\(2\), \(1\)), (\(2\), \(2\)), (\(2\), \(3\)), and (\(3\), \(2\)).
|
Input: 51 5 4 1 3 | Output: 9
|
Easy
| 2 | 2,136 | 217 | 105 | 10 |
605 |
B
|
605B
|
B. Lazy Student
| 1,700 |
constructive algorithms; data structures; graphs
|
Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph β something that will definitely never be useful in real life. He asked a girl sitting next to him to lend him some cheat papers for this questions and found there the following definition:The minimum spanning tree T of graph G is such a tree that it contains all the vertices of the original graph G, and the sum of the weights of its edges is the minimum possible among all such trees.Vladislav drew a graph with n vertices and m edges containing no loops and multiple edges. He found one of its minimum spanning trees and then wrote for each edge its weight and whether it is included in the found tree or not. Unfortunately, the piece of paper where the graph was painted is gone and the teacher is getting very angry and demands to see the original graph. Help Vladislav come up with a graph so that the information about the minimum spanning tree remains correct.
|
The first line of the input contains two integers n and m () β the number of vertices and the number of edges in the graph.Each of the next m lines describes an edge of the graph and consists of two integers aj and bj (1 β€ aj β€ 109, bj = {0, 1}). The first of these numbers is the weight of the edge and the second number is equal to 1 if this edge was included in the minimum spanning tree found by Vladislav, or 0 if it was not.It is guaranteed that exactly n - 1 number {bj} are equal to one and exactly m - n + 1 of them are equal to zero.
|
If Vladislav has made a mistake and such graph doesn't exist, print - 1.Otherwise print m lines. On the j-th line print a pair of vertices (uj, vj) (1 β€ uj, vj β€ n, uj β vj), that should be connected by the j-th edge. The edges are numbered in the same order as in the input. The graph, determined by these edges, must be connected, contain no loops or multiple edges and its edges with bj = 1 must define the minimum spanning tree. In case there are multiple possible solutions, print any of them.
|
Input: 4 52 13 14 01 15 0 | Output: 2 41 43 43 13 2
|
Medium
| 3 | 1,006 | 543 | 498 | 6 |
|
707 |
A
|
707A
|
A. Brain's Photos
| 800 |
implementation
|
Small, but very brave, mouse Brain was not accepted to summer school of young villains. He was upset and decided to postpone his plans of taking over the world, but to become a photographer instead.As you may know, the coolest photos are on the film (because you can specify the hashtag #film for such).Brain took a lot of colourful pictures on colored and black-and-white film. Then he developed and translated it into a digital form. But now, color and black-and-white photos are in one folder, and to sort them, one needs to spend more than one hour!As soon as Brain is a photographer not programmer now, he asks you to help him determine for a single photo whether it is colored or black-and-white.Photo can be represented as a matrix sized n Γ m, and each element of the matrix stores a symbol indicating corresponding pixel color. There are only 6 colors: 'C' (cyan) 'M' (magenta) 'Y' (yellow) 'W' (white) 'G' (grey) 'B' (black) The photo is considered black-and-white if it has only white, black and grey pixels in it. If there are any of cyan, magenta or yellow pixels in the photo then it is considered colored.
|
The first line of the input contains two integers n and m (1 β€ n, m β€ 100) β the number of photo pixel matrix rows and columns respectively.Then n lines describing matrix rows follow. Each of them contains m space-separated characters describing colors of pixels in a row. Each character in the line is one of the 'C', 'M', 'Y', 'W', 'G' or 'B'.
|
Print the ""#Black&White"" (without quotes), if the photo is black-and-white and ""#Color"" (without quotes), if it is colored, in the only line.
|
Input: 2 2C MY Y | Output: #Color
|
Beginner
| 1 | 1,120 | 345 | 145 | 7 |
|
2,110 |
F
|
2110F
|
F. Faculty
| 2,400 |
brute force; greedy; math; number theory
|
In 2077, after the world was enslaved by robots, the robots decided to implement an educational reform, and now the operation of taking the modulus is only taught in the faculty of ""Ancient World History"". Here is one of the entrance tasks for this faculty:We define the beauty of an array of positive integers \(b\) as the maximum \(f(b_i, b_j)\) over all pairs \(1 \leq i, j \leq n\), where \(f(x, y) = (x \bmod y) + (y \bmod x)\).Given an array of positive integers \(a\) of length \(n\), output \(n\) numbers, where the \(i\)-th number (\(1 \leq i \leq n\)) is the beauty of the array \(a_1, a_2, \ldots, a_i\).\(x \bmod y\) is the remainder of the division of \(x\) by \(y\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 10^6\)) β the size of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)).It is guaranteed that the sum of \(n\) across all test cases does not exceed \(10^6\).
|
For each test case, output \(n\) integers β the beauties of all prefixes of the array \(a\).
|
The beauty of the array \(3\) is \(0\).The beauty of the array \(3, 1\) is \(f(3, 1) = 1\).The beauty of the array \(3, 1, 4\) is \(f(3, 4) = 4\).The beauty of the array \(3, 1, 4, 1\) is \(f(4, 3) = 4\).The beauty of the array \(3, 1, 4, 1, 5\) is \(f(4, 5) = 5\).
|
Input: 253 1 4 1 575 11 11 4 2 1 10 | Output: 0 1 4 4 5 0 6 6 7 7 7 11
|
Expert
| 4 | 682 | 479 | 92 | 21 |
1,884 |
B
|
1884B
|
B. Haunted House
| 1,100 |
binary search; greedy; math; two pointers
|
You are given a number in binary representation consisting of exactly \(n\) bits, possibly, with leading zeroes. For example, for \(n = 5\) the number \(6\) will be given as \(00110\), and for \(n = 4\) the number \(9\) will be given as \(1001\).Let's fix some integer \(i\) such that \(1 \le i \le n\). In one operation you can swap any two adjacent bits in the binary representation. Your goal is to find the smallest number of operations you are required to perform to make the number divisible by \(2^i\), or say that it is impossible.Please note that for each \(1 \le i \le n\) you are solving the problem independently.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows.The first line of each test case contains one integer \(n\) (\(1 \le n \le 10^5\)) β the length of the binary representation of the number.The second line of each test case contains a string of length \(n\), consisting of \(0\) and \(1\), β the binary representation of the number.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, for each \(1 \le i \le n\) output the smallest number of operations required to make the number divisible by \(2^i\), or output \(-1\) if it is impossible.
|
In the first test case, we cannot swap any elements, and the number \(1\) is not divisible by \(2\).In the second test case, the initial number is \(1\). It is not divisible by \(2\), but if we perform the operation, then we obtain the number with binary representation \(10\), which is equal to \(2\) in decimal representation, thus, it is divisible by \(2\). But this number is not divisible by \(4\) and we cannot obtain any other number using the operations.In the third test case, the initial number is \(2\). For \(i = 1\) we do not have to perform any operations since the initial number is divisible by \(2\). For \(i = 2\) we can perform one operation swapping the first two bits (we would obtain \(100\) in binary representation, which corresponds to number \(4\)). There is no answer for \(i = 3\).
|
Input: 61120130105101017000011112001011000110 | Output: -1 1 -1 0 1 -1 1 3 -1 -1 -1 3 6 9 12 -1 -1 -1 0 2 4 6 10 15 20 -1 -1 -1 -1 -1
|
Easy
| 4 | 625 | 534 | 175 | 18 |
1,328 |
B
|
1328B
|
B. K-th Beautiful String
| 1,300 |
binary search; brute force; combinatorics; implementation; math
|
For the given integer \(n\) (\(n > 2\)) let's write down all the strings of length \(n\) which contain \(n-2\) letters 'a' and two letters 'b' in lexicographical (alphabetical) order.Recall that the string \(s\) of length \(n\) is lexicographically less than string \(t\) of length \(n\), if there exists such \(i\) (\(1 \le i \le n\)), that \(s_i < t_i\), and for any \(j\) (\(1 \le j < i\)) \(s_j = t_j\). The lexicographic comparison of strings is implemented by the operator < in modern programming languages.For example, if \(n=5\) the strings are (the order does matter): aaabb aabab aabba abaab ababa abbaa baaab baaba babaa bbaaa It is easy to show that such a list of strings will contain exactly \(\frac{n \cdot (n-1)}{2}\) strings.You are given \(n\) (\(n > 2\)) and \(k\) (\(1 \le k \le \frac{n \cdot (n-1)}{2}\)). Print the \(k\)-th string from the list.
|
The input contains one or more test cases.The first line contains one integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases in the test. Then \(t\) test cases follow.Each test case is written on the the separate line containing two integers \(n\) and \(k\) (\(3 \le n \le 10^5, 1 \le k \le \min(2\cdot10^9, \frac{n \cdot (n-1)}{2})\).The sum of values \(n\) over all test cases in the test doesn't exceed \(10^5\).
|
For each test case print the \(k\)-th string from the list of all described above strings of length \(n\). Strings in the list are sorted lexicographically (alphabetically).
|
Input: 7 5 1 5 2 5 8 5 10 3 1 3 2 20 100 | Output: aaabb aabab baaba bbaaa abb bab aaaaabaaaaabaaaaaaaa
|
Easy
| 5 | 867 | 424 | 173 | 13 |
|
540 |
D
|
540D
|
D. Bad Luck Island
| 1,900 |
dp; probabilities
|
The Bad Luck Island is inhabited by three kinds of species: r rocks, s scissors and p papers. At some moments of time two random individuals meet (all pairs of individuals can meet equiprobably), and if they belong to different species, then one individual kills the other one: a rock kills scissors, scissors kill paper, and paper kills a rock. Your task is to determine for each species what is the probability that this species will be the only one to inhabit this island after a long enough period of time.
|
The single line contains three integers r, s and p (1 β€ r, s, p β€ 100) β the original number of individuals in the species of rock, scissors and paper, respectively.
|
Print three space-separated real numbers: the probabilities, at which the rocks, the scissors and the paper will be the only surviving species, respectively. The answer will be considered correct if the relative or absolute error of each number doesn't exceed 10 - 9.
|
Input: 2 2 2 | Output: 0.333333333333 0.333333333333 0.333333333333
|
Hard
| 2 | 510 | 165 | 267 | 5 |
|
584 |
B
|
584B
|
B. Kolya and Tanya
| 1,500 |
combinatorics
|
Kolya loves putting gnomes at the circle table and giving them coins, and Tanya loves studying triplets of gnomes, sitting in the vertexes of an equilateral triangle.More formally, there are 3n gnomes sitting in a circle. Each gnome can have from 1 to 3 coins. Let's number the places in the order they occur in the circle by numbers from 0 to 3n - 1, let the gnome sitting on the i-th place have ai coins. If there is an integer i (0 β€ i < n) such that ai + ai + n + ai + 2n β 6, then Tanya is satisfied. Count the number of ways to choose ai so that Tanya is satisfied. As there can be many ways of distributing coins, print the remainder of this number modulo 109 + 7. Two ways, a and b, are considered distinct if there is index i (0 β€ i < 3n), such that ai β bi (that is, some gnome got different number of coins in these two ways).
|
A single line contains number n (1 β€ n β€ 105) β the number of the gnomes divided by three.
|
Print a single number β the remainder of the number of variants of distributing coins that satisfy Tanya modulo 109 + 7.
|
20 ways for n = 1 (gnome with index 0 sits on the top of the triangle, gnome 1 on the right vertex, gnome 2 on the left vertex):
|
Input: 1 | Output: 20
|
Medium
| 1 | 837 | 90 | 120 | 5 |
710 |
E
|
710E
|
E. Generate a String
| 2,000 |
dfs and similar; dp
|
zscoder wants to generate an input file for some programming competition problem.His input is a string consisting of n letters 'a'. He is too lazy to write a generator so he will manually generate the input in a text editor.Initially, the text editor is empty. It takes him x seconds to insert or delete a letter 'a' from the text file and y seconds to copy the contents of the entire text file, and duplicate it.zscoder wants to find the minimum amount of time needed for him to create the input file of exactly n letters 'a'. Help him to determine the amount of time needed to generate the input.
|
The only line contains three integers n, x and y (1 β€ n β€ 107, 1 β€ x, y β€ 109) β the number of letters 'a' in the input file and the parameters from the problem statement.
|
Print the only integer t β the minimum amount of time needed to generate the input file.
|
Input: 8 1 1 | Output: 4
|
Hard
| 2 | 598 | 171 | 88 | 7 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.