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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
87 |
D
|
87D
|
D. Beautiful Road
| 2,300 |
dfs and similar; dp; dsu; graphs; implementation; sortings; trees
|
A long time ago in some country in Asia were civil wars.Each of n cities wanted to seize power. That's why sometimes one city gathered an army and sent it to campaign against another city.Road making was difficult, so the country had few roads, exactly n - 1. Also you could reach any city from any other city going on those roads.Even during the war the Oriental people remain spiritually rich and appreciate the beauty of nature. And to keep the memory of this great crusade for the centuries to come, they planted one beautiful tree by the road on which the army spent most time. The Oriental people love nature, that's why if there were several such roads, then one tree was planted by each of them.Recently, when the records of the war were found, it became clear that each city attacked each other one exactly once. There were exactly n(n - 1) attacks in total. Everyone has been wondering what road after those wars became the most beautiful, that is, by which road they planted the largest number of beautiful trees.
|
The first line contains an integer n (2 β€ n β€ 105), which represents the number of cities. Next n - 1 lines contain three integers each: the numbers of cities ai, bi (1 β€ ai, bi β€ n), connected by the i-th road and the number of days di the army spends to go on it (1 β€ di β€ 109). The lengths of several roads may coincide.
|
Print on the first line two integers β the number of beautiful trees on the most beautiful road and the number of the most beautiful roads. Print on the second line the list of the most beautiful roads in the sorted order by the numbers' increasing. The roads are numbered from 1 to n - 1 in the order in which they are given in the input data.Please, do not use %lld specificator to write 64-bit integers in C++. It is preferred to use the cout stream (also you may use the %I64d specificator).
|
Input: 22 1 5 | Output: 2 11
|
Expert
| 7 | 1,024 | 323 | 495 | 0 |
|
1,033 |
E
|
1033E
|
E. Hidden Bipartite Graph
| 2,800 |
binary search; constructive algorithms; dfs and similar; graphs; interactive
|
Bob has a simple undirected connected graph (without self-loops and multiple edges). He wants to learn whether his graph is bipartite (that is, you can paint all vertices of the graph into two colors so that there is no edge connecting two vertices of the same color) or not. As he is not very good at programming, he asked Alice for help. He does not want to disclose his graph to Alice, but he agreed that Alice can ask him some questions about the graph.The only question that Alice can ask is the following: she sends \(s\) β a subset of vertices of the original graph. Bob answers with the number of edges that have both endpoints in \(s\). Since he doesn't want Alice to learn too much about the graph, he allows her to ask no more than \(20000\) questions. Furthermore, he suspects that Alice might introduce false messages to their communication channel, so when Alice finally tells him whether the graph is bipartite or not, she also needs to provide a proof β either the partitions themselves or a cycle of odd length.Your task is to help Alice to construct the queries, find whether the graph is bipartite.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 600\)) β the number of vertices in Bob's graph.
|
In the first case, Alice learns that there are \(4\) edges in the whole graph. Over the course of the next three queries, she learns that vertex \(1\) has two neighbors: \(3\) and \(4\). She then learns that while vertex \(2\) is adjacent to \(4\), the vertex \(3\) isn't adjacent to \(4\). There is only one option for the remaining edge, and that is \((2, 3)\). This means that the graph is a cycle on four vertices, with \((1, 2)\) being one partition and \((3, 4)\) being the second. Here, it would be also valid to output ""3 4"" on the second line.In the second case, we also have a graph on four vertices and four edges. In the second query, Alice learns that there are three edges among vertices \((1, 2, 4)\). The only way this could possibly happen is that those form a triangle. As the triangle is not bipartite, Alice can report it as a proof. Notice that she does not learn where the fourth edge is, but she is able to answer Bob correctly anyway.
|
Input: 4401110 | Output: ? 4 1 2 3 4? 21 2? 21 3? 21 4? 22 4? 23 4Y 21 2
|
Master
| 5 | 1,117 | 111 | 0 | 10 |
|
123 |
A
|
123A
|
A. Prime Permutation
| 1,300 |
implementation; number theory; strings
|
You are given a string s, consisting of small Latin letters. Let's denote the length of the string as |s|. The characters in the string are numbered starting from 1. Your task is to find out if it is possible to rearrange characters in string s so that for any prime number p β€ |s| and for any integer i ranging from 1 to |s| / p (inclusive) the following condition was fulfilled sp = sp Γ i. If the answer is positive, find one way to rearrange the characters.
|
The only line contains the initial string s, consisting of small Latin letters (1 β€ |s| β€ 1000).
|
If it is possible to rearrange the characters in the string so that the above-mentioned conditions were fulfilled, then print in the first line ""YES"" (without the quotes) and print on the second line one of the possible resulting strings. If such permutation is impossible to perform, then print the single string ""NO"".
|
In the first sample any of the six possible strings will do: ""abc"", ""acb"", ""bac"", ""bca"", ""cab"" or ""cba"".In the second sample no letter permutation will satisfy the condition at p = 2 (s2 = s4).In the third test any string where character ""y"" doesn't occupy positions 2, 3, 4, 6 will be valid.
|
Input: abc | Output: YESabc
|
Easy
| 3 | 461 | 96 | 323 | 1 |
1,725 |
A
|
1725A
|
A. Accumulation of Dominoes
| 800 |
math
|
Pak Chanek has a grid that has \(N\) rows and \(M\) columns. Each row is numbered from \(1\) to \(N\) from top to bottom. Each column is numbered from \(1\) to \(M\) from left to right.Each tile in the grid contains a number. The numbers are arranged as follows: Row \(1\) contains integers from \(1\) to \(M\) from left to right. Row \(2\) contains integers from \(M+1\) to \(2 \times M\) from left to right. Row \(3\) contains integers from \(2 \times M+1\) to \(3 \times M\) from left to right. And so on until row \(N\). A domino is defined as two different tiles in the grid that touch by their sides. A domino is said to be tight if and only if the two numbers in the domino have a difference of exactly \(1\). Count the number of distinct tight dominoes in the grid.Two dominoes are said to be distinct if and only if there exists at least one tile that is in one domino, but not in the other.
|
The only line contains two integers \(N\) and \(M\) (\(1 \leq N, M \leq 10^9\)) β the number of rows and columns in the grid.
|
An integer representing the number of distinct tight dominoes in the grid.
|
The picture below is the grid that Pak Chanek has in the first example. The picture below is an example of a tight domino in the grid.
|
Input: 3 4 | Output: 9
|
Beginner
| 1 | 900 | 125 | 74 | 17 |
825 |
B
|
825B
|
B. Five-In-a-Row
| 1,600 |
brute force; implementation
|
Alice and Bob play 5-in-a-row game. They have a playing field of size 10 Γ 10. In turns they put either crosses or noughts, one at a time. Alice puts crosses and Bob puts noughts.In current match they have made some turns and now it's Alice's turn. She wonders if she can put cross in such empty cell that she wins immediately.Alice wins if some crosses in the field form line of length not smaller than 5. This line can be horizontal, vertical and diagonal.
|
You are given matrix 10 Γ 10 (10 lines of 10 characters each) with capital Latin letters 'X' being a cross, letters 'O' being a nought and '.' being an empty cell. The number of 'X' cells is equal to the number of 'O' cells and there is at least one of each type. There is at least one empty cell.It is guaranteed that in the current arrangement nobody has still won.
|
Print 'YES' if it's possible for Alice to win in one turn by putting cross in some empty cell. Otherwise print 'NO'.
|
Input: XX.XX..........OOOO................................................................................. | Output: YES
|
Medium
| 2 | 458 | 367 | 116 | 8 |
|
2,050 |
F
|
2050F
|
F. Maximum modulo equality
| 1,700 |
data structures; divide and conquer; math; number theory
|
You are given an array \(a\) of length \(n\) and \(q\) queries \(l\), \(r\).For each query, find the maximum possible \(m\), such that all elements \(a_l\), \(a_{l+1}\), ..., \(a_r\) are equal modulo \(m\). In other words, \(a_l \bmod m = a_{l+1} \bmod m = \dots = a_r \bmod m\), where \(a \bmod b\) β is the remainder of division \(a\) by \(b\). In particular, when \(m\) can be infinite, print \(0\).
|
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\), \(q\) (\(1 \le n, q \le 2\cdot 10^5\)) β the length of the array and the number of queries.The second line of each test case contains \(n\) integers \(a_i\) (\(1 \le a_i \le 10^9\)) β the elements of the array.In the following \(q\) lines of each test case, two integers \(l\), \(r\) are provided (\(1 \le l \le r \le n\)) β the range of the query.It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2\cdot 10^5\), and the sum of \(q\) does not exceed \(2\cdot 10^5\).
|
For each query, output the maximum value \(m\) described in the statement.
|
In the first query of the first sample, \(6 \bmod 3 = 3 \bmod 3 = 0\). It can be shown that for greater \(m\), the required condition will not be fulfilled.In the third query of the first sample, \(14 \bmod 4 = 2 \bmod 4 = 6 \bmod 4 = 2\). It can be shown that for greater \(m\), the required condition will not be fulfilled.
|
Input: 35 55 14 2 6 34 51 42 43 51 11 171 13 21 7 82 31 2 | Output: 3 1 4 1 0 0 1 6
|
Medium
| 4 | 402 | 654 | 74 | 20 |
1,236 |
C
|
1236C
|
C. Labs
| 1,300 |
constructive algorithms; greedy; implementation
|
In order to do some research, \(n^2\) labs are built on different heights of a mountain. Let's enumerate them with integers from \(1\) to \(n^2\), such that the lab with the number \(1\) is at the lowest place, the lab with the number \(2\) is at the second-lowest place, \(\ldots\), the lab with the number \(n^2\) is at the highest place.To transport water between the labs, pipes are built between every pair of labs. A pipe can transport at most one unit of water at a time from the lab with the number \(u\) to the lab with the number \(v\) if \(u > v\).Now the labs need to be divided into \(n\) groups, each group should contain exactly \(n\) labs. The labs from different groups can transport water to each other. The sum of units of water that can be sent from a group \(A\) to a group \(B\) is equal to the number of pairs of labs (\(u, v\)) such that the lab with the number \(u\) is from the group \(A\), the lab with the number \(v\) is from the group \(B\) and \(u > v\). Let's denote this value as \(f(A,B)\) (i.e. \(f(A,B)\) is the sum of units of water that can be sent from a group \(A\) to a group \(B\)).For example, if \(n=3\) and there are \(3\) groups \(X\), \(Y\) and \(Z\): \(X = \{1, 5, 6\}, Y = \{2, 4, 9\}\) and \(Z = \{3, 7, 8\}\). In this case, the values of \(f\) are equal to: \(f(X,Y)=4\) because of \(5 \rightarrow 2\), \(5 \rightarrow 4\), \(6 \rightarrow 2\), \(6 \rightarrow 4\), \(f(X,Z)=2\) because of \(5 \rightarrow 3\), \(6 \rightarrow 3\), \(f(Y,X)=5\) because of \(2 \rightarrow 1\), \(4 \rightarrow 1\), \(9 \rightarrow 1\), \(9 \rightarrow 5\), \(9 \rightarrow 6\), \(f(Y,Z)=4\) because of \(4 \rightarrow 3\), \(9 \rightarrow 3\), \(9 \rightarrow 7\), \(9 \rightarrow 8\), \(f(Z,X)=7\) because of \(3 \rightarrow 1\), \(7 \rightarrow 1\), \(7 \rightarrow 5\), \(7 \rightarrow 6\), \(8 \rightarrow 1\), \(8 \rightarrow 5\), \(8 \rightarrow 6\), \(f(Z,Y)=5\) because of \(3 \rightarrow 2\), \(7 \rightarrow 2\), \(7 \rightarrow 4\), \(8 \rightarrow 2\), \(8 \rightarrow 4\). Please, divide labs into \(n\) groups with size \(n\), such that the value \(\min f(A,B)\) over all possible pairs of groups \(A\) and \(B\) (\(A \neq B\)) is maximal.In other words, divide labs into \(n\) groups with size \(n\), such that minimum number of the sum of units of water that can be transported from a group \(A\) to a group \(B\) for every pair of different groups \(A\) and \(B\) (\(A \neq B\)) as big as possible.Note, that the example above doesn't demonstrate an optimal division, but it demonstrates how to calculate the values \(f\) for some division.If there are many optimal divisions, you can find any.
|
The only line contains one number \(n\) (\(2 \leq n \leq 300\)).
|
Output \(n\) lines:In the \(i\)-th line print \(n\) numbers, the numbers of labs of the \(i\)-th group, in any order you want.If there are multiple answers, that maximize the minimum number of the sum of units of water that can be transported from one group the another, you can print any.
|
In the first test we can divide \(9\) labs into groups \(\{2, 8, 5\}, \{9, 3, 4\}, \{7, 6, 1\}\).From the first group to the second group we can transport \(4\) units of water (\(8 \rightarrow 3, 8 \rightarrow 4, 5 \rightarrow 3, 5 \rightarrow 4\)).From the first group to the third group we can transport \(5\) units of water (\(2 \rightarrow 1, 8 \rightarrow 7, 8 \rightarrow 6, 8 \rightarrow 1, 5 \rightarrow 1\)).From the second group to the first group we can transport \(5\) units of water (\(9 \rightarrow 2, 9 \rightarrow 8, 9 \rightarrow 5, 3 \rightarrow 2, 4 \rightarrow 2\)).From the second group to the third group we can transport \(5\) units of water (\(9 \rightarrow 7, 9 \rightarrow 6, 9 \rightarrow 1, 3 \rightarrow 1, 4 \rightarrow 1\)).From the third group to the first group we can transport \(4\) units of water (\(7 \rightarrow 2, 7 \rightarrow 5, 6 \rightarrow 2, 6 \rightarrow 5\)).From the third group to the second group we can transport \(4\) units of water (\(7 \rightarrow 3, 7 \rightarrow 4, 6 \rightarrow 3, 6 \rightarrow 4\)).The minimal number of the sum of units of water, that can be transported from one group to another is equal to \(4\). It can be proved, that it is impossible to make a better division.
|
Input: 3 | Output: 2 8 5 9 3 4 7 6 1
|
Easy
| 3 | 2,645 | 64 | 289 | 12 |
981 |
A
|
981A
|
A. Antipalindrome
| 900 |
brute force; implementation; strings
|
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings ""kek"", ""abacaba"", ""r"" and ""papicipap"" are palindromes, while the strings ""abb"" and ""iq"" are not.A substring \(s[l \ldots r]\) (\(1 \leq l \leq r \leq |s|\)) of a string \(s = s_{1}s_{2} \ldots s_{|s|}\) is the string \(s_{l}s_{l + 1} \ldots s_{r}\).Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word \(s\) is changed into its longest substring that is not a palindrome. If all the substrings of \(s\) are palindromes, she skips the word at all.Some time ago Ann read the word \(s\). What is the word she changed it into?
|
The first line contains a non-empty string \(s\) with length at most \(50\) characters, containing lowercase English letters only.
|
If there is such a substring in \(s\) that is not a palindrome, print the maximum length of such a substring. Otherwise print \(0\).Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
|
""mew"" is not a palindrome, so the longest substring of it that is not a palindrome, is the string ""mew"" itself. Thus, the answer for the first example is \(3\).The string ""uffuw"" is one of the longest non-palindrome substrings (of length \(5\)) of the string ""wuffuw"", so the answer for the second example is \(5\).All substrings of the string ""qqqqqqqq"" consist of equal characters so they are palindromes. This way, there are no non-palindrome substrings. Thus, the answer for the third example is \(0\).
|
Input: mew | Output: 3
|
Beginner
| 3 | 759 | 130 | 236 | 9 |
107 |
E
|
107E
|
E. Darts
| 2,700 |
geometry; probabilities
|
The night after the graduation ceremony graduate students of German University in Cairo (GUC) are playing darts. As there's no real dart board available, the photographs of members of the GUC upper management are being used.So, n rectangular photos are placed on the wall. They can overlap arbitrary and even coincide. The photos are not necessarily placed horizontally or vertically, they could also be rotated before being pinned to the wall.The score of one dart throw is simply the number of photos the dart went through.Fatma has made a throw but her score was not recorded. She only remembers that she did make it into at least one photo.Assuming that the probability distribution of the throw is equal across the whole wall, what would be the expectation of Fatma's score?
|
The first line of input contains integer n (1 β€ n β€ 500) β the number of photos on the wall. Then follow n lines describing the photos, each containing 8 single-space-separated integers (coordinates of 4 vertices): x1, y1, x2, y2, x3, y3, x4, y4. Each photo is a rectangle with a nonzero area. The coordinates are integers, not exceeding 104 by absolute value. The coordinates of the rectangle are given in either clockwise or counterclockwise order.
|
Print the expected score of the throw. The answer will be accepted if it has absolute or relative error not exceeding 10 - 6.
|
Input: 10 0 0 2 2 2 2 0 | Output: 1.0000000000
|
Master
| 2 | 779 | 450 | 125 | 1 |
|
981 |
B
|
981B
|
B. Businessmen Problems
| 1,000 |
sortings
|
Two famous competing companies ChemForces and TopChemist decided to show their sets of recently discovered chemical elements on an exhibition. However they know that no element should be present in the sets of both companies.In order to avoid this representatives of both companies decided to make an agreement on the sets the companies should present. The sets should be chosen in the way that maximizes the total income of the companies.All elements are enumerated with integers. The ChemForces company has discovered \(n\) distinct chemical elements with indices \(a_1, a_2, \ldots, a_n\), and will get an income of \(x_i\) Berland rubles if the \(i\)-th element from this list is in the set of this company.The TopChemist company discovered \(m\) distinct chemical elements with indices \(b_1, b_2, \ldots, b_m\), and it will get an income of \(y_j\) Berland rubles for including the \(j\)-th element from this list to its set.In other words, the first company can present any subset of elements from \(\{a_1, a_2, \ldots, a_n\}\) (possibly empty subset), the second company can present any subset of elements from \(\{b_1, b_2, \ldots, b_m\}\) (possibly empty subset). There shouldn't be equal elements in the subsets.Help the representatives select the sets in such a way that no element is presented in both sets and the total income is the maximum possible.
|
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the number of elements discovered by ChemForces.The \(i\)-th of the next \(n\) lines contains two integers \(a_i\) and \(x_i\) (\(1 \leq a_i \leq 10^9\), \(1 \leq x_i \leq 10^9\)) β the index of the \(i\)-th element and the income of its usage on the exhibition. It is guaranteed that all \(a_i\) are distinct.The next line contains a single integer \(m\) (\(1 \leq m \leq 10^5\)) β the number of chemicals invented by TopChemist.The \(j\)-th of the next \(m\) lines contains two integers \(b_j\) and \(y_j\), (\(1 \leq b_j \leq 10^9\), \(1 \leq y_j \leq 10^9\)) β the index of the \(j\)-th element and the income of its usage on the exhibition. It is guaranteed that all \(b_j\) are distinct.
|
Print the maximum total income you can obtain by choosing the sets for both companies in such a way that no element is presented in both sets.
|
In the first example ChemForces can choose the set (\(3, 7\)), while TopChemist can choose (\(1, 2, 4\)). This way the total income is \((10 + 2) + (4 + 4 + 4) = 24\).In the second example ChemForces can choose the only element \(10^9\), while TopChemist can choose (\(14, 92, 35\)). This way the total income is \((239) + (15 + 65 + 89) = 408\).
|
Input: 31 27 23 1041 42 43 44 4 | Output: 24
|
Beginner
| 1 | 1,365 | 767 | 142 | 9 |
1,910 |
G
|
1910G
|
G. Pool Records
| 2,700 |
*special; greedy
|
Alice and Bob are swimming in the pool under the guidance of their instructor Monocarp.The pool can be represented as a segment on the OX-axis from \(0\) to \(50\). Both Alice and Bob started at moment \(0\) at point \(0\) with positive real speeds \(v_A\) and \(v_B\) correspondingly.Both Alice and Bob swim from \(0\) to point \(50\), then instantly turn back and swim from \(50\) to \(0\). At \(0\) they turn back again and swim to \(50\) and so on.Monocarp logged all valuable info about Alice and Bob, including moments of time when they met at the same point (Alice and Bob swim on parallel lanes, so they don't bother each other). Let's name that moments of time as sequence \(t_1, t_2, \dots, t_n\).Due to some incident, Monocarp lost almost all data he recorded, and all he has now is array \(t\). Monocarp remembers that Alice and Bob had distinct positive real swimming speeds \(v_A\) and \(v_B\) (\(v_A > 0\); \(v_B > 0\); \(v_A \neq v_B\)) and that sequence \(t\) contains the first \(n\) meeting moments.But looking at sequence \(t\) he noticed that all \(t_i\) are integer values, and now he doubts is sequence \(t\) valid or there are some errors in it. Help Monocarp to check array \(t\)!
|
The first line contains a single integer \(c\) (\(1 \le c \le 10^4\)) β the number of test cases. Then \(c\) cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the size of array \(t\).The second line of each test case contains \(n\) integers \(t_1, t_2, \dots, t_n\) (\(1 \le t_1 < t_2 < \dots < t_n \le 10^9\)) β the meeting moments in the increasing order.It's guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print VALID if the array \(t\) is a valid array, or (in other words) exist such real values \(v_A\) and \(v_B\) (\(v_A, v_B > 0\); \(v_A \neq v_B\)) that array \(t\) contains the first \(n\) meeting moments of Alice and Bob in the pool.Otherwise, print INVALID.You can output the answer in any case (upper or lower). For example, the strings ""vALid"", ""valid"", ""Valid"", and ""VALID"" will be recognized as positive responses.
|
In the first test case, imagine a situation: \(v_A = 1\) and \(v_B = \frac{29}{21}\). Then, at moment \(42\) Alice and Bob will be at point \(42\).In the second test case, imagine a situation: \(v_A = \frac{175}{6}\) and \(v_B = \frac{25}{6}\). Then, at moment \(3\) Alice and Bob will meet at point \(12.5\), at \(4\) they'll meet at point \(\frac{50}{3}\), at moment \(6\) they'll meet at \(25\) and at moment \(8\) β at point \(\frac{100}{3}\). Since it's exactly the first \(4\) meeting moments. Array \(t\) may be valid.In the third test case, one of the possible situations is \(v_A = 25\) and \(v_B = 75\).
|
Input: 714243 4 6 851 2 3 4 53999999998 999999999 100000000054 6 8 12 1646 11 12 14210 30 | Output: VALID VALID VALID INVALID VALID INVALID INVALID
|
Master
| 2 | 1,205 | 508 | 450 | 19 |
1,109 |
E
|
1109E
|
E. Sasha and a Very Easy Test
| 2,700 |
data structures; number theory
|
Egor likes math, and not so long ago he got the highest degree of recognition in the math community β Egor became a red mathematician. In this regard, Sasha decided to congratulate Egor and give him a math test as a present. This test contains an array \(a\) of integers of length \(n\) and exactly \(q\) queries. Queries were of three types: ""1 l r x"" β multiply each number on the range from \(l\) to \(r\) by \(x\). ""2 p x"" β divide the number at the position \(p\) by \(x\) (divisibility guaranteed). ""3 l r"" β find the sum of all elements on the range from \(l\) to \(r\). The sum can be big, so Sasha asked Egor to calculate the sum modulo some integer \(mod\). But since Egor is a red mathematician, he doesn't have enough time to solve such easy tasks, at the same time he doesn't want to anger Sasha, that's why he asked you to help and to find answers for all queries of the \(3\)-rd type.
|
The first line contains two integers \(n\) and \(mod\) (\(1 \le n \le 10^5\), \(2 \le mod \le 10^9 + 9\)) β the size of the array and the number \(mod\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^5\)) β the array itself.The third line contains one integer \(q\)(\(1 \le q \le 10^5\)) β the number of queries.Next \(q\) lines satisfy one of the following formats: 1 l r x (\(1 \le l \le r \le n\), \(1 \le x \le 10^5\)), means that you must multiply each number on the range from \(l\) to \(r\) by \(x\). 2 p x (\(1 \le p \le n\), \(1 \le x \le 10^5\)), means that you must divide number at the position \(p\) by \(x\) (divisibility guaranteed). 3 l r (\(1 \le l \le r \le n\)), means that you must find the sum of elements on the range from \(l\) to \(r\). It is guaranteed that there is at least one query of the \(3\)-rd type.
|
For each query of the \(3\)-rd type print the answer on a new line modulo \(mod\).
|
The first example:Inital array is \([4, 1, 2, 3, 5]\) In the first query, you must calculate the sum of the whole array, it's equal to \((4 + 1 + 2 + 3 + 5) \bmod 100 = 15 \bmod 100 = 15\) In the second query, you must multiply each number on the range from \(2\) to \(3\) by \(6\). The resulting array will be \([4, 6, 12, 3, 5]\) In the third query, you must calculate the sum on the range from \(1\) to \(2\), it's equal to \((4 + 6) \bmod 100 = 10 \bmod 100 = 10\) In the fourth query, you must multiply each number on the range from \(1\) to \(5\) by \(1\). Multiplication by \(1\) doesn't affect the array. In the fifth query, you must calculate the sum on the range from \(2\) to \(4\), it's equal to \((6 + 12 + 3) \bmod 100 = 21 \bmod 100 = 21\) The second example:Inital array is \([4, 1, 2, 3, 5]\) In the first query, you must calculate the sum of the whole array, it's equal to \((4 + 1 + 2 + 3 + 5) \bmod 2 = 15 \bmod 2 = 1\) In the second query, you must multiply each number on the range from \(2\) to \(3\) by \(6\). The resulting array will be \([4, 6, 12, 3, 5]\) In the third query, you must calculate the sum on the range from \(1\) to \(2\), it's equal to \((4 + 6) \bmod 2 = 10 \bmod 2 = 0\) In the fourth query, you must multiply each number on the range from \(1\) to \(5\) by \(1\). Multiplication by \(1\) doesn't affect the array. In the fifth query, you must calculate the sum on the range from \(2\) to \(4\), it's equal to \((6 + 12 + 3) \bmod 2 = 21 \bmod 2 = 1\) In the sixth query, you must divide number at the position \(3\) by \(4\). \(\frac{12}{4}=3\), so the array will be \([4, 6, 3, 3, 5]\). In the seventh, query you must calculate the sum on the range form \(3\) to \(4\), it's equal to \((3 + 3) \bmod 2 = 6 \bmod 2 = 0\)
|
Input: 5 100 4 1 2 3 5 5 3 1 5 1 2 3 6 3 1 2 1 1 5 1 3 2 4 | Output: 15 10 21
|
Master
| 2 | 905 | 871 | 82 | 11 |
1,203 |
E
|
1203E
|
E. Boxers
| 1,500 |
greedy; sortings
|
There are \(n\) boxers, the weight of the \(i\)-th boxer is \(a_i\). Each of them can change the weight by no more than \(1\) before the competition (the weight cannot become equal to zero, that is, it must remain positive). Weight is always an integer number.It is necessary to choose the largest boxing team in terms of the number of people, that all the boxers' weights in the team are different (i.e. unique).Write a program that for given current values β\(a_i\) will find the maximum possible number of boxers in a team.It is possible that after some change the weight of some boxer is \(150001\) (but no more).
|
The first line contains an integer \(n\) (\(1 \le n \le 150000\)) β the number of boxers. The next line contains \(n\) integers \(a_1, a_2, \dots, a_n\), where \(a_i\) (\(1 \le a_i \le 150000\)) is the weight of the \(i\)-th boxer.
|
Print a single integer β the maximum possible number of people in a team.
|
In the first example, boxers should not change their weights β you can just make a team out of all of them.In the second example, one boxer with a weight of \(1\) can be increased by one (get the weight of \(2\)), one boxer with a weight of \(4\) can be reduced by one, and the other can be increased by one (resulting the boxers with a weight of \(3\) and \(5\), respectively). Thus, you can get a team consisting of boxers with weights of \(5, 4, 3, 2, 1\).
|
Input: 43 2 4 1 | Output: 4
|
Medium
| 2 | 617 | 231 | 73 | 12 |
86 |
C
|
86C
|
C. Genetic engineering
| 2,500 |
dp; string suffix structures; trees
|
""Multidimensional spaces are completely out of style these days, unlike genetics problems"" β thought physicist Woll and changed his subject of study to bioinformatics. Analysing results of sequencing he faced the following problem concerning DNA sequences. We will further think of a DNA sequence as an arbitrary string of uppercase letters ""A"", ""C"", ""G"" and ""T"" (of course, this is a simplified interpretation).Let w be a long DNA sequence and s1, s2, ..., sm β collection of short DNA sequences. Let us say that the collection filters w iff w can be covered with the sequences from the collection. Certainly, substrings corresponding to the different positions of the string may intersect or even cover each other. More formally: denote by |w| the length of w, let symbols of w be numbered from 1 to |w|. Then for each position i in w there exist pair of indices l, r (1 β€ l β€ i β€ r β€ |w|) such that the substring w[l ... r] equals one of the elements s1, s2, ..., sm of the collection.Woll wants to calculate the number of DNA sequences of a given length filtered by a given collection, but he doesn't know how to deal with it. Help him! Your task is to find the number of different DNA sequences of length n filtered by the collection {si}.Answer may appear very large, so output it modulo 1000000009.
|
First line contains two integer numbers n and m (1 β€ n β€ 1000, 1 β€ m β€ 10) β the length of the string and the number of sequences in the collection correspondently. Next m lines contain the collection sequences si, one per line. Each si is a nonempty string of length not greater than 10. All the strings consist of uppercase letters ""A"", ""C"", ""G"", ""T"". The collection may contain identical strings.
|
Output should contain a single integer β the number of strings filtered by the collection modulo 1000000009 (109 + 9).
|
In the first sample, a string has to be filtered by ""A"". Clearly, there is only one such string: ""AA"".In the second sample, there exist exactly two different strings satisfying the condition (see the pictures below).
|
Input: 2 1A | Output: 1
|
Expert
| 3 | 1,315 | 407 | 118 | 0 |
1,383 |
E
|
1383E
|
E. Strange Operation
| 2,800 |
combinatorics; data structures; dp
|
Koa the Koala has a binary string \(s\) of length \(n\). Koa can perform no more than \(n-1\) (possibly zero) operations of the following form:In one operation Koa selects positions \(i\) and \(i+1\) for some \(i\) with \(1 \le i < |s|\) and sets \(s_i\) to \(max(s_i, s_{i+1})\). Then Koa deletes position \(i+1\) from \(s\) (after the removal, the remaining parts are concatenated).Note that after every operation the length of \(s\) decreases by \(1\).How many different binary strings can Koa obtain by doing no more than \(n-1\) (possibly zero) operations modulo \(10^9+7\) (\(1000000007\))?
|
The only line of input contains binary string \(s\) (\(1 \le |s| \le 10^6\)). For all \(i\) (\(1 \le i \le |s|\)) \(s_i = 0\) or \(s_i = 1\).
|
On a single line print the answer to the problem modulo \(10^9+7\) (\(1000000007\)).
|
In the first sample Koa can obtain binary strings: \(0\), \(00\) and \(000\).In the second sample Koa can obtain binary strings: \(1\), \(01\), \(11\), \(011\), \(101\) and \(0101\). For example: to obtain \(01\) from \(0101\) Koa can operate as follows: \(0101 \rightarrow 0(10)1 \rightarrow 011 \rightarrow 0(11) \rightarrow 01\). to obtain \(11\) from \(0101\) Koa can operate as follows: \(0101 \rightarrow (01)01 \rightarrow 101 \rightarrow 1(01) \rightarrow 11\). Parentheses denote the two positions Koa selected in each operation.
|
Input: 000 | Output: 3
|
Master
| 3 | 596 | 141 | 84 | 13 |
1,950 |
C
|
1950C
|
C. Clock Conversion
| 800 |
implementation; math
|
Given the time in 24-hour format, output the equivalent time in 12-hour format. 24-hour format divides the day into 24 hours from \(00\) to \(23\), each of which has 60 minutes from \(00\) to \(59\). 12-hour format divides the day into two halves: the first half is \(\mathrm{AM}\), and the second half is \(\mathrm{PM}\). In each half, the hours are numbered in the order \(12, 01, 02, 03, \dots, 11\). Each hour has 60 minutes numbered from \(00\) to \(59\).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 1440\)) β the number of test cases.The only line of each test case contains a string \(s\) of length \(5\) with format hh:mm representing a valid time in the 24-hour format. hh represents the hour from \(00\) to \(23\), and mm represents the minute from \(00\) to \(59\).The input will always be a valid time in 24-hour format.
|
For each test case, output two strings separated by a space (""hh:mm AM"" or ""hh:mm PM""), which are the 12-hour equivalent to the time provided in the test case (without quotes).You should output the time exactly as indicated; in particular, you should not remove leading zeroes.
|
Input: 1109:4118:0612:1400:5900:0014:3401:0119:0711:5912:0021:37 | Output: 09:41 AM 06:06 PM 12:14 PM 12:59 AM 12:00 AM 02:34 PM 01:01 AM 07:07 PM 11:59 AM 12:00 PM 09:37 PM
|
Beginner
| 2 | 460 | 391 | 281 | 19 |
|
2,048 |
F
|
2048F
|
F. Kevin and Math Class
| 2,500 |
brute force; data structures; divide and conquer; dp; implementation; math; trees
|
Kevin is a student from Eversleeping Town, currently attending a math class where the teacher is giving him division exercises.On the board, there are two rows of positive integers written, each containing \( n \) numbers. The first row is \( a_1, a_2, \ldots, a_n \), and the second row is \( b_1, b_2, \ldots, b_n \). For each division exercise, Kevin can choose any segment \( [l, r] \) and find the smallest value \( x \) among \( b_l, b_{l+1}, \ldots, b_r \). He will then modify each \( a_i \) for \( l \leq i \leq r \) to be the ceiling of \( a_i \) divided by \( x \).Formally, he selects two integers \( 1 \leq l \leq r \leq n \), sets \( x = \min_{l \leq i \leq r} b_i \), and changes all \( a_i \) for \( l \leq i \leq r \) to \( \lceil \frac{a_i}{x} \rceil \).Kevin can leave class and go home when all \( a_i \) become \( 1 \). He is eager to leave and wants to know the minimum number of division exercises required to achieve this.
|
Each test contains multiple test cases. The first line contains the number of test cases \( t \) (\( 1 \le t \le 10^4 \)).The first line of each test case contains an integer \( n \) (\( 1 \le n \leq 2 \cdot 10^5 \)) β the length of the sequence \( a \) and \( b \).The second line of each test case contains \( n \) integers \( a_1, a_2, \ldots, a_n \) (\( 1 \le a_i \le 10^{18} \)) β the first row of integers on the board.The third line of each test case contains \( n \) integers \( b_1, b_2, \ldots, b_n \) (\( 2 \le b_i \le 10^{18} \)) β the second row of integers on the board.It is guaranteed that the sum of \( n \) over all test cases doesn't exceed \( 2 \cdot 10^5 \).
|
For each test case, output one integer β the minimum number of division exercises required to leave class.
|
For the first test case: \( [{\color{red}{5,4}},2]\xrightarrow[\min(b_1,b_2)=3]{\text{operate segment }[1,2]}[{\color{red}{2,2,2}}]\xrightarrow[\min(b_1,b_2,b_3)=2]{\text{operate segment }[1,3]}[1,1,1] \).For the second test case: \( [{\color{red}{3,6,1}},3,2]\xrightarrow[\min(b_1,b_2,b_3)=3]{\text{operate segment }[1,3]}[1,{\color{red}{2,1,3}},2]\xrightarrow[\min(b_2,b_3,b_4)=2]{\text{operate segment }[2,4]}[1,1,1,{\color{red}{2,2}}]\xrightarrow[\min(b_4,b_5)=2]{\text{operate segment }[4,5]}[1,1,1,1,1] \).
|
Input: 335 4 26 3 253 6 1 3 23 5 3 2 268 3 3 7 5 83 2 3 4 2 3 | Output: 2 3 3
|
Expert
| 7 | 946 | 679 | 106 | 20 |
1,394 |
A
|
1394A
|
A. Boboniu Chats with Du
| 1,800 |
dp; greedy; sortings; two pointers
|
Have you ever used the chat application QQ? Well, in a chat group of QQ, administrators can muzzle a user for days.In Boboniu's chat group, there's a person called Du Yi who likes to make fun of Boboniu every day.Du will chat in the group for \(n\) days. On the \(i\)-th day: If Du can speak, he'll make fun of Boboniu with fun factor \(a_i\). But after that, he may be muzzled depending on Boboniu's mood. Otherwise, Du won't do anything. Boboniu's mood is a constant \(m\). On the \(i\)-th day: If Du can speak and \(a_i>m\), then Boboniu will be angry and muzzle him for \(d\) days, which means that Du won't be able to speak on the \(i+1, i+2, \cdots, \min(i+d,n)\)-th days. Otherwise, Boboniu won't do anything. The total fun factor is the sum of the fun factors on the days when Du can speak.Du asked you to find the maximum total fun factor among all possible permutations of \(a\).
|
The first line contains three integers \(n\), \(d\) and \(m\) (\(1\le d\le n\le 10^5,0\le m\le 10^9\)).The next line contains \(n\) integers \(a_1, a_2, \ldots,a_n\) (\(0\le a_i\le 10^9\)).
|
Print one integer: the maximum total fun factor among all permutations of \(a\).
|
In the first example, you can set \(a'=[15, 5, 8, 10, 23]\). Then Du's chatting record will be: Make fun of Boboniu with fun factor \(15\). Be muzzled. Be muzzled. Make fun of Boboniu with fun factor \(10\). Make fun of Boboniu with fun factor \(23\). Thus the total fun factor is \(48\).
|
Input: 5 2 11 8 10 15 23 5 | Output: 48
|
Medium
| 4 | 889 | 189 | 80 | 13 |
1,051 |
C
|
1051C
|
C. Vasya and Multisets
| 1,500 |
brute force; dp; greedy; implementation; math
|
Vasya has a multiset \(s\) consisting of \(n\) integer numbers. Vasya calls some number \(x\) nice if it appears in the multiset exactly once. For example, multiset \(\{1, 1, 2, 3, 3, 3, 4\}\) contains nice numbers \(2\) and \(4\).Vasya wants to split multiset \(s\) into two multisets \(a\) and \(b\) (one of which may be empty) in such a way that the quantity of nice numbers in multiset \(a\) would be the same as the quantity of nice numbers in multiset \(b\) (the quantity of numbers to appear exactly once in multiset \(a\) and the quantity of numbers to appear exactly once in multiset \(b\)).
|
The first line contains a single integer \(n~(2 \le n \le 100)\).The second line contains \(n\) integers \(s_1, s_2, \dots s_n~(1 \le s_i \le 100)\) β the multiset \(s\).
|
If there exists no split of \(s\) to satisfy the given requirements, then print ""NO"" in the first line.Otherwise print ""YES"" in the first line.The second line should contain a string, consisting of \(n\) characters. \(i\)-th character should be equal to 'A' if the \(i\)-th element of multiset \(s\) goes to multiset \(a\) and 'B' if if the \(i\)-th element of multiset \(s\) goes to multiset \(b\). Elements are numbered from \(1\) to \(n\) in the order they are given in the input.If there exist multiple solutions, then print any of them.
|
Input: 43 5 7 1 | Output: YESBABA
|
Medium
| 5 | 600 | 170 | 545 | 10 |
|
652 |
B
|
652B
|
B. z-sort
| 1,000 |
sortings
|
A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold: ai β₯ ai - 1 for all even i, ai β€ ai - 1 for all odd i > 1. For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isnβt z-sorted.Can you make the array z-sorted?
|
The first line contains a single integer n (1 β€ n β€ 1000) β the number of elements in the array a.The second line contains n integers ai (1 β€ ai β€ 109) β the elements of the array a.
|
If it's possible to make the array a z-sorted print n space separated integers ai β the elements after z-sort. Otherwise print the only word ""Impossible"".
|
Input: 41 2 2 1 | Output: 1 2 1 2
|
Beginner
| 1 | 318 | 182 | 156 | 6 |
|
670 |
F
|
670F
|
F. Restore a Number
| 2,300 |
brute force; constructive algorithms; strings
|
Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k β the number of digits in n. Magically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty substring of n (a substring of n is a sequence of consecutive digits of the number n).Vasya knows that there may be more than one way to restore the number n. Your task is to find the smallest possible initial integer n. Note that decimal representation of number n contained no leading zeroes, except the case the integer n was equal to zero itself (in this case a single digit 0 was used).
|
The first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1 000 000.The second line contains the substring of n which Vasya remembers. This string can contain leading zeroes. It is guaranteed that the input data is correct, and the answer always exists.
|
Print the smalles integer n which Vasya could pass to Kate.
|
Input: 003512021 | Output: 30021
|
Expert
| 3 | 703 | 313 | 59 | 6 |
|
1,462 |
E1
|
1462E1
|
E1. Close Tuples (easy version)
| 1,500 |
binary search; combinatorics; math; sortings; two pointers
|
This is the easy version of this problem. The only difference between easy and hard versions is the constraints on \(k\) and \(m\) (in this version \(k=2\) and \(m=3\)). Also, in this version of the problem, you DON'T NEED to output the answer by modulo.You are given a sequence \(a\) of length \(n\) consisting of integers from \(1\) to \(n\). The sequence may contain duplicates (i.e. some elements can be equal).Find the number of tuples of \(m = 3\) elements such that the maximum number in the tuple differs from the minimum by no more than \(k = 2\). Formally, you need to find the number of triples of indices \(i < j < z\) such that$$$\(\max(a_i, a_j, a_z) - \min(a_i, a_j, a_z) \le 2.\)\(For example, if \)n=4\( and \)a=[1,2,4,3]\(, then there are two such triples (\)i=1, j=2, z=4\( and \)i=2, j=3, z=4\(). If \)n=4\( and \)a=[1,1,1,1]$$$, then all four possible triples are suitable.
|
The first line contains a single integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β the number of test cases. Then \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the sequence \(a\).The next line contains \(n\) integers \(a_1, a_2,\ldots, a_n\) (\(1 \le a_i \le n\)) β the sequence \(a\).It is guaranteed that the sum of \(n\) for all test cases does not exceed \(2 \cdot 10^5\).
|
Output \(t\) answers to the given test cases. Each answer is the required number of triples of elements, such that the maximum value in the triple differs from the minimum by no more than \(2\). Note that in difference to the hard version of the problem, you don't need to output the answer by modulo. You must output the exact value of the answer.
|
Input: 4 4 1 2 4 3 4 1 1 1 1 1 1 10 5 6 1 3 2 9 8 1 2 4 | Output: 2 4 0 15
|
Medium
| 5 | 894 | 457 | 348 | 14 |
|
1,826 |
A
|
1826A
|
A. Trust Nobody
| 1,200 |
brute force; greedy; implementation; sortings
|
There is a group of \(n\) people. Some of them might be liars, who always tell lies. Other people always tell the truth. The \(i\)-th person says ""There are at least \(l_i\) liars amongst us"". Determine if what people are saying is contradictory, or if it is possible. If it is possible, output the number of liars in the group. If there are multiple possible answers, output any one of them.
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 100\)).The second line of each test case contains \(n\) integers \(l_i\) (\(0 \leq l_i \leq n\)) β the number said by the \(i\)-th person.It's guaranteed that the sum of all \(n\) does not exceed \(10^4\).
|
For each test case output a single integer. If what people are saying is contradictory, output \(-1\). Otherwise, output the number of liars in the group. If there are multiple possible answers, output any one of them.
|
In the first example, the only possible answer is that the second person is a liar, so the answer is \(1\) liar.In the second example, it can be proven that we can't choose the liars so that all the requirements are satisfied.In the third example, everybody tells the truth, so the answer is \(0\) liars.
|
Input: 721 222 220 0111055 5 3 3 565 3 6 6 3 5 | Output: 1 -1 0 -1 0 3 4
|
Easy
| 4 | 394 | 386 | 218 | 18 |
814 |
A
|
814A
|
A. An abandoned sentiment from past
| 900 |
constructive algorithms; greedy; implementation; sortings
|
A few years ago, Hitagi encountered a giant crab, who stole the whole of her body weight. Ever since, she tried to avoid contact with others, for fear that this secret might be noticed.To get rid of the oddity and recover her weight, a special integer sequence is needed. Hitagi's sequence has been broken for a long time, but now Kaiki provides an opportunity.Hitagi's sequence a has a length of n. Lost elements in it are denoted by zeros. Kaiki provides another sequence b, whose length k equals the number of lost elements in a (i.e. the number of zeros). Hitagi is to replace each zero in a with an element from b so that each element in b should be used exactly once. Hitagi knows, however, that, apart from 0, no integer occurs in a and b more than once in total.If the resulting sequence is not an increasing sequence, then it has the power to recover Hitagi from the oddity. You are to determine whether this is possible, or Kaiki's sequence is just another fake. In other words, you should detect whether it is possible to replace each zero in a with an integer from b so that each integer from b is used exactly once, and the resulting sequence is not increasing.
|
The first line of input contains two space-separated positive integers n (2 β€ n β€ 100) and k (1 β€ k β€ n) β the lengths of sequence a and b respectively.The second line contains n space-separated integers a1, a2, ..., an (0 β€ ai β€ 200) β Hitagi's broken sequence with exactly k zero elements.The third line contains k space-separated integers b1, b2, ..., bk (1 β€ bi β€ 200) β the elements to fill into Hitagi's sequence.Input guarantees that apart from 0, no integer occurs in a and b more than once in total.
|
Output ""Yes"" if it's possible to replace zeros in a with elements in b and make the resulting sequence not increasing, and ""No"" otherwise.
|
In the first sample: Sequence a is 11, 0, 0, 14. Two of the elements are lost, and the candidates in b are 5 and 4. There are two possible resulting sequences: 11, 5, 4, 14 and 11, 4, 5, 14, both of which fulfill the requirements. Thus the answer is ""Yes"". In the second sample, the only possible resulting sequence is 2, 3, 5, 8, 9, 10, which is an increasing sequence and therefore invalid.
|
Input: 4 211 0 0 145 4 | Output: Yes
|
Beginner
| 4 | 1,174 | 508 | 142 | 8 |
107 |
B
|
107B
|
B. Basketball Team
| 1,600 |
combinatorics; dp; math; probabilities
|
As a German University in Cairo (GUC) student and a basketball player, Herr Wafa was delighted once he heard the news. GUC is finally participating in the Annual Basketball Competition (ABC). A team is to be formed of n players, all of which are GUC students. However, the team might have players belonging to different departments. There are m departments in GUC, numbered from 1 to m. Herr Wafa's department has number h. For each department i, Herr Wafa knows number si β how many students who play basketball belong to this department.Herr Wafa was also able to guarantee a spot on the team, using his special powers. But since he hates floating-point numbers, he needs your help at finding the probability that he will have at least one teammate belonging to his department. Note that every possible team containing Herr Wafa is equally probable. Consider all the students different from each other.
|
The first line contains three integers n, m and h (1 β€ n β€ 100, 1 β€ m β€ 1000, 1 β€ h β€ m) β the number of players on the team, the number of departments in GUC and Herr Wafa's department, correspondingly. The second line contains a single-space-separated list of m integers si (1 β€ si β€ 100), denoting the number of students in the i-th department. Note that sh includes Herr Wafa.
|
Print the probability that Herr Wafa will have at least one teammate from his department. If there is not enough basketball players in GUC to participate in ABC, print -1. The answer will be accepted if it has absolute or relative error not exceeding 10 - 6.
|
In the first example all 3 players (2 from department 1 and 1 from department 2) must be chosen for the team. Both players from Wafa's departments will be chosen, so he's guaranteed to have a teammate from his department.In the second example, there are not enough players.In the third example, there are three possibilities to compose the team containing Herr Wafa. In two of them the other player from Herr Wafa's department is part of the team.
|
Input: 3 2 12 1 | Output: 1
|
Medium
| 4 | 904 | 380 | 258 | 1 |
1,939 |
A
|
1939A
| 0 |
*special; constructive algorithms; dp; geometry; interactive
|
Beginner
| 5 | 0 | 0 | 0 | 19 |
||||||
954 |
A
|
954A
|
A. Diagonal Walking
| 800 |
implementation
|
Mikhail walks on a 2D plane. He can go either up or right. You are given a sequence of Mikhail's moves. He thinks that this sequence is too long and he wants to make it as short as possible.In the given sequence moving up is described by character U and moving right is described by character R. Mikhail can replace any pair of consecutive moves RU or UR with a diagonal move (described as character D). After that, he can go on and do some other replacements, until there is no pair of consecutive moves RU or UR left.Your problem is to print the minimum possible length of the sequence of moves after the replacements.
|
The first line of the input contains one integer n (1 β€ n β€ 100) β the length of the sequence. The second line contains the sequence consisting of n characters U and R.
|
Print the minimum possible length of the sequence of moves after all replacements are done.
|
In the first test the shortened sequence of moves may be DUD (its length is 3).In the second test the shortened sequence of moves can be UUDRRRDUDDUUU (its length is 13).
|
Input: 5RUURU | Output: 3
|
Beginner
| 1 | 620 | 168 | 91 | 9 |
524 |
B
|
524B
|
B. Π€ΠΎΡΠΎ Π½Π° ΠΏΠ°ΠΌΡΡΡ - 2 (round version)
| 1,700 |
dp; greedy
|
ΠΡΠΎΡΠ»ΠΎ ΠΌΠ½ΠΎΠ³ΠΎ Π»Π΅Ρ, ΠΈ Π½Π° Π²Π΅ΡΠ΅ΡΠΈΠ½ΠΊΠ΅ ΡΠ½ΠΎΠ²Π° Π²ΡΡΡΠ΅ΡΠΈΠ»ΠΈΡΡ n Π΄ΡΡΠ·Π΅ΠΉ. Π‘ ΠΌΠΎΠΌΠ΅Π½ΡΠ° ΠΏΠΎΡΠ»Π΅Π΄Π½Π΅ΠΉ Π²ΡΡΡΠ΅ΡΠΈ ΡΠ΅Ρ
Π½ΠΈΠΊΠ° ΡΠ°Π³Π½ΡΠ»Π° Π΄Π°Π»Π΅ΠΊΠΎ Π²ΠΏΠ΅ΡΡΠ΄, ΠΏΠΎΡΠ²ΠΈΠ»ΠΈΡΡ ΡΠΎΡΠΎΠ°ΠΏΠΏΠ°ΡΠ°ΡΡ Ρ Π°Π²ΡΠΎΡΠΏΡΡΠΊΠΎΠΌ, ΠΈ ΡΠ΅ΠΏΠ΅ΡΡ Π½Π΅ ΡΡΠ΅Π±ΡΠ΅ΡΡΡ, ΡΡΠΎΠ±Ρ ΠΎΠ΄ΠΈΠ½ ΠΈΠ· Π΄ΡΡΠ·Π΅ΠΉ ΡΡΠΎΡΠ» Ρ ΡΠΎΡΠΎΠ°ΠΏΠΏΠ°ΡΠ°ΡΠΎΠΌ, ΠΈ, ΡΠ΅ΠΌ ΡΠ°ΠΌΡΠΌ, ΠΎΠΊΠ°Π·ΡΠ²Π°Π»ΡΡ Π½Π΅ Π·Π°ΠΏΠ΅ΡΠ°ΡΠ»ΡΠ½Π½ΡΠΌ Π½Π° ΡΠ½ΠΈΠΌΠΊΠ΅.Π£ΠΏΡΠΎΡΠ΅Π½Π½ΠΎ ΠΏΡΠΎΡΠ΅ΡΡ ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΡΠΎΠ²Π°Π½ΠΈΡ ΠΌΠΎΠΆΠ½ΠΎ ΠΎΠΏΠΈΡΠ°ΡΡ ΡΠ»Π΅Π΄ΡΡΡΠΈΠΌ ΠΎΠ±ΡΠ°Π·ΠΎΠΌ. ΠΠ° ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΠΈ ΠΊΠ°ΠΆΠ΄ΡΠΉ ΠΈΠ· Π΄ΡΡΠ·Π΅ΠΉ Π·Π°Π½ΠΈΠΌΠ°Π΅Ρ ΠΏΡΡΠΌΠΎΡΠ³ΠΎΠ»ΡΠ½ΠΈΠΊ ΠΈΠ· ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ: Π² ΡΡΠΎΡΡΠ΅ΠΌ ΠΏΠΎΠ»ΠΎΠΆΠ΅Π½ΠΈΠΈ i-ΠΉ ΠΈΠ· Π½ΠΈΡ
Π·Π°Π½ΠΈΠΌΠ°Π΅Ρ ΠΏΡΡΠΌΠΎΡΠ³ΠΎΠ»ΡΠ½ΠΈΠΊ ΡΠΈΡΠΈΠ½Ρ wi ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ ΠΈ Π²ΡΡΠΎΡΡ hi ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ. ΠΠΎ ΡΠ°ΠΊΠΆΠ΅, ΠΏΡΠΈ ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΡΠΎΠ²Π°Π½ΠΈΠΈ ΠΊΠ°ΠΆΠ΄ΡΠΉ ΡΠ΅Π»ΠΎΠ²Π΅ΠΊ ΠΌΠΎΠΆΠ΅Ρ Π»Π΅ΡΡ, ΠΈ ΡΠΎΠ³Π΄Π° ΠΎΠ½ Π±ΡΠ΄Π΅Ρ Π·Π°Π½ΠΈΠΌΠ°ΡΡ ΠΏΡΡΠΌΠΎΡΠ³ΠΎΠ»ΡΠ½ΠΈΠΊ ΡΠΈΡΠΈΠ½Ρ hi ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ ΠΈ Π²ΡΡΠΎΡΡ wi ΠΏΠΈΠΊΡΠ΅Π»Π΅ΠΉ.ΠΠ±ΡΠ°Ρ ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΡ Π±ΡΠ΄Π΅Ρ ΠΈΠΌΠ΅ΡΡ ΡΠ°Π·ΠΌΠ΅ΡΡ W Γ H, Π³Π΄Π΅ W β ΡΡΠΌΠΌΠ°ΡΠ½Π°Ρ ΡΠΈΡΠΈΠ½Π° Π²ΡΠ΅Ρ
ΠΏΡΡΠΌΠΎΡΠ³ΠΎΠ»ΡΠ½ΠΈΠΊΠΎΠ²-Π»ΡΠ΄Π΅ΠΉ, Π° H β ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½Π°Ρ ΠΈΠ· Π²ΡΡΠΎΡ. ΠΡΡΠ·ΡΡ Ρ
ΠΎΡΡΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»ΠΈΡΡ, ΠΊΠ°ΠΊΡΡ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΡΡ ΠΏΠ»ΠΎΡΠ°Π΄Ρ ΠΌΠΎΠΆΠ΅Ρ ΠΈΠΌΠ΅ΡΡ ΠΎΠ±ΡΠ°Ρ ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΡ. ΠΠΎΠΌΠΎΠ³ΠΈΡΠ΅ ΠΈΠΌ Π² ΡΡΠΎΠΌ.
|
Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ ΡΠ»Π΅Π΄ΡΠ΅Ρ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ n (1 β€ n β€ 1000) β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π΄ΡΡΠ·Π΅ΠΉ.Π ΠΏΠΎΡΠ»Π΅Π΄ΡΡΡΠΈΡ
n ΡΡΡΠΎΠΊΠ°Ρ
ΡΠ»Π΅Π΄ΡΡΡ ΠΏΠΎ Π΄Π²Π° ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ»Π° wi, hi (1 β€ wi, hi β€ 1000), ΠΎΠ±ΠΎΠ·Π½Π°ΡΠ°ΡΡΠΈΠ΅ ΡΠ°Π·ΠΌΠ΅ΡΡ ΠΏΡΡΠΌΠΎΡΠ³ΠΎΠ»ΡΠ½ΠΈΠΊΠ°, ΡΠΎΠΎΡΠ²Π΅ΡΡΡΠ²ΡΡΡΠ΅Π³ΠΎ i-ΠΌΡ ΠΈΠ· Π΄ΡΡΠ·Π΅ΠΉ.
|
ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ Π΅Π΄ΠΈΠ½ΡΡΠ²Π΅Π½Π½ΠΎΠ΅ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ, ΡΠ°Π²Π½ΠΎΠ΅ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠΉ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠΉ ΠΏΠ»ΠΎΡΠ°Π΄ΠΈ ΡΠΎΡΠΎΠ³ΡΠ°ΡΠΈΠΈ, Π²ΠΌΠ΅ΡΠ°ΡΡΠ΅ΠΉ Π²ΡΠ΅Ρ
Π΄ΡΡΠ·Π΅ΠΉ.
|
Input: 310 120 230 3 | Output: 180
|
Medium
| 2 | 871 | 221 | 106 | 5 |
|
488 |
A
|
488A
|
A. Giga Tower
| 1,100 |
brute force
|
Giga Tower is the tallest and deepest building in Cyberland. There are 17 777 777 777 floors, numbered from - 8 888 888 888 to 8 888 888 888. In particular, there is floor 0 between floor - 1 and floor 1. Every day, thousands of tourists come to this place to enjoy the wonderful view. In Cyberland, it is believed that the number ""8"" is a lucky number (that's why Giga Tower has 8 888 888 888 floors above the ground), and, an integer is lucky, if and only if its decimal notation contains at least one digit ""8"". For example, 8, - 180, 808 are all lucky while 42, - 10 are not. In the Giga Tower, if you write code at a floor with lucky floor number, good luck will always be with you (Well, this round is #278, also lucky, huh?).Tourist Henry goes to the tower to seek good luck. Now he is at the floor numbered a. He wants to find the minimum positive integer b, such that, if he walks b floors higher, he will arrive at a floor with a lucky number.
|
The only line of input contains an integer a ( - 109 β€ a β€ 109).
|
Print the minimum b in a line.
|
For the first sample, he has to arrive at the floor numbered 180.For the second sample, he will arrive at 8.Note that b should be positive, so the answer for the third sample is 10, not 0.
|
Input: 179 | Output: 1
|
Easy
| 1 | 957 | 64 | 30 | 4 |
505 |
B
|
505B
|
B. Mr. Kitayuta's Colorful Graph
| 1,400 |
dfs and similar; dp; dsu; graphs
|
Mr. Kitayuta has just bought an undirected graph consisting of n vertices and m edges. The vertices of the graph are numbered from 1 to n. Each edge, namely edge i, has a color ci, connecting vertex ai and bi.Mr. Kitayuta wants you to process the following q queries.In the i-th query, he gives you two integers β ui and vi.Find the number of the colors that satisfy the following condition: the edges of that color connect vertex ui and vertex vi directly or indirectly.
|
The first line of the input contains space-separated two integers β n and m (2 β€ n β€ 100, 1 β€ m β€ 100), denoting the number of the vertices and the number of the edges, respectively.The next m lines contain space-separated three integers β ai, bi (1 β€ ai < bi β€ n) and ci (1 β€ ci β€ m). Note that there can be multiple edges between two vertices. However, there are no multiple edges of the same color between two vertices, that is, if i β j, (ai, bi, ci) β (aj, bj, cj).The next line contains a integer β q (1 β€ q β€ 100), denoting the number of the queries.Then follows q lines, containing space-separated two integers β ui and vi (1 β€ ui, vi β€ n). It is guaranteed that ui β vi.
|
For each query, print the answer in a separate line.
|
Let's consider the first sample. The figure above shows the first sample. Vertex 1 and vertex 2 are connected by color 1 and 2. Vertex 3 and vertex 4 are connected by color 3. Vertex 1 and vertex 4 are not connected by any single color.
|
Input: 4 51 2 11 2 22 3 12 3 32 4 331 23 41 4 | Output: 210
|
Easy
| 4 | 471 | 679 | 52 | 5 |
1,781 |
H1
|
1781H1
|
H1. Window Signals (easy version)
| 3,200 |
This is the easy version of the problem. In this version, the constraints on \(h\) and \(w\) are lower.A house at the sea has \(h\) floors, all of the same height. The side of the house facing the sea has \(w\) windows at equal distances from each other on every floor. Thus, the windows are positioned in cells of a rectangular grid of size \(h \times w\).In every window, the light can be turned either on or off, except for the given \(k\) (at most \(2\)) windows. In these \(k\) windows the light can not be turned on, because it is broken.In the dark, we can send a signal to a ship at sea using a configuration of lights turned on and off. However, the ship can not see the position of the lights with respect to the house. Thus, if one configuration of windows with lights on can be transformed into another using parallel translation, these configurations are considered equal. Note that only parallel translation is allowed, but neither rotations nor flips are. Moreover, a configuration without any light at all is not considered valid.Find how many different signals the ship can receive and print this number modulo \(998\,244\,353\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.The first line of each test case contains three integers \(h\), \(w\), and \(k\) (\(1 \le h, w \le 40\); \(0 \le k \le \min(h \cdot w, 2)\)) β the height of the house, the number of windows on each floor, and the number of windows with broken lights.If \(k > 0\), each of the next \(k\) lines contains two integers \(r_i\) and \(c_i\) (\(1 \le r_i \le h\); \(1 \le c_i \le w\)) β the floor number and the window number with the broken light. The floors are numbered \(1\) to \(h\) from bottom to top, and the windows are numbered \(1\) to \(w\) from left to right. If \(k = 2\), then either \(r_1 \ne r_2\) or \(c_1 \ne c_2\).It is guaranteed that the sum of \(h \cdot w\) over all test cases does not exceed \(1600\).
|
For each test case, print a single integer β the number of signals the ship can receive, modulo \(998\,244\,353\).
|
In the first test case, the ship can receive four different signals: all lights on; lights on in two neighboring windows; lights on in the leftmost and the rightmost windows; or light on in any single window.
|
Input: 101 3 02 2 03 2 04 5 012 7 01 1 11 11 3 11 23 4 13 42 3 21 12 14 5 22 34 2 | Output: 4 10 44 954368 34903934 0 2 1696 10 253144
|
Master
| 0 | 1,146 | 878 | 114 | 17 |
|
432 |
C
|
432C
|
C. Prime Swaps
| 1,800 |
greedy; sortings
|
You have an array a[1], a[2], ..., a[n], containing distinct integers from 1 to n. Your task is to sort this array in increasing order with the following operation (you may need to apply it multiple times): choose two indexes, i and j (1 β€ i < j β€ n; (j - i + 1) is a prime number); swap the elements on positions i and j; in other words, you are allowed to apply the following sequence of assignments: tmp = a[i], a[i] = a[j], a[j] = tmp (tmp is a temporary variable). You do not need to minimize the number of used operations. However, you need to make sure that there are at most 5n operations.
|
The first line contains integer n (1 β€ n β€ 105). The next line contains n distinct integers a[1], a[2], ..., a[n] (1 β€ a[i] β€ n).
|
In the first line, print integer k (0 β€ k β€ 5n) β the number of used operations. Next, print the operations. Each operation must be printed as ""i j"" (1 β€ i < j β€ n; (j - i + 1) is a prime).If there are multiple answers, you can print any of them.
|
Input: 33 2 1 | Output: 11 3
|
Medium
| 2 | 597 | 129 | 248 | 4 |
|
1,239 |
F
|
1239F
|
F. Swiper, no swiping!
| 3,400 |
graphs; implementation
|
I'm the Map, I'm the Map! I'm the MAP!!!MapIn anticipation of new adventures Boots wanted to do a good deed. After discussion with the Map and Backpack, they decided to gift Dora a connected graph. After a long search, Boots chose \(t\) graph's variants, which Dora might like. However fox Swiper wants to spoil his plan.The Swiper knows, that Dora now is only able to count up to \(3\), so he has came up with a following idea. He wants to steal some non-empty set of vertices, so that the Dora won't notice the loss. He has decided to steal some non-empty set of vertices, so that after deletion of the stolen vertices and edges adjacent to them, every remaining vertex wouldn't change it's degree modulo \(3\). The degree of a vertex is the number of edges it is adjacent to. It would've been suspicious to steal all the vertices, so Swiper needs another plan.Boots are sure, that the crime can not be allowed. However they are afraid, that they won't be able to handle this alone. So Boots decided to ask for your help. Please determine for every graph's variant whether the Swiper can perform the theft or not.
|
The first line contains a single integer \(t\) (\(1 \le t \le 100\,000\)) β the number of graph variants.The first line of each variant contains integers \(n\), \(m\) (\(1 \le n \le 500\,000\), \(0 \le m \le 500\,000\)), the number of vertexes and edges in the graph.Then \(m\) lines follow, each containing integers \(a_i\), \(b_i\) (\(1 \le a_i, b_i \le n\)), the indices of the vertices connected with a corresponding edge.It's guaranteed, that the graph is connected and doesn't contain multiple edges or self-loops.It's guaranteed, that the sum of \(n\) over all variants is at most \(500\,000\) and that the sum of \(m\) over all variants is at most \(500\,000\).Descriptions of graph's variants are separated with an empty line.
|
For each variant: In case the answer exists, print ""Yes"" and then the answer itself.The first line should contain an integer \(c\) (\(1 < c < n\)), the number of vertices the Crook can steal, without Dora noticing the loss. On the next line print \(c\) distinct integers, the indices of the graph's vertices in arbitrary order. Otherwise print ""No"". In case there are several correct ways to steal the vertices, print any of them.Please note, that it's not required to maximize the number of stolen vertices.
|
The picture below shows the third variant from the example test. The set of the vertices the Crook can steal is denoted with bold.
|
Input: 3 3 3 1 2 2 3 3 1 6 6 1 2 1 3 2 3 2 5 2 6 2 4 8 12 1 2 1 3 2 3 1 4 4 5 5 1 3 6 3 7 3 8 6 1 7 1 8 1 | Output: No Yes 3 4 5 6 Yes 3 6 7 8
|
Master
| 2 | 1,115 | 735 | 512 | 12 |
837 |
A
|
837A
|
A. Text Volume
| 800 |
implementation
|
You are given a text of single-space separated words, consisting of small and capital Latin letters.Volume of the word is number of capital letters in the word. Volume of the text is maximum volume of all words in the text.Calculate the volume of the given text.
|
The first line contains one integer number n (1 β€ n β€ 200) β length of the text.The second line contains text of single-space separated words s1, s2, ..., si, consisting only of small and capital Latin letters.
|
Print one integer number β volume of text.
|
In the first example there is only one word, there are 5 capital letters in it.In the second example all of the words contain 0 capital letters.
|
Input: 7NonZERO | Output: 5
|
Beginner
| 1 | 262 | 210 | 42 | 8 |
518 |
C
|
518C
|
C. Anya and Smartphone
| 1,600 |
constructive algorithms; data structures; implementation
|
Anya has bought a new smartphone that uses Berdroid operating system. The smartphone menu has exactly n applications, each application has its own icon. The icons are located on different screens, one screen contains k icons. The icons from the first to the k-th one are located on the first screen, from the (k + 1)-th to the 2k-th ones are on the second screen and so on (the last screen may be partially empty).Initially the smartphone menu is showing the screen number 1. To launch the application with the icon located on the screen t, Anya needs to make the following gestures: first she scrolls to the required screen number t, by making t - 1 gestures (if the icon is on the screen t), and then make another gesture β press the icon of the required application exactly once to launch it.After the application is launched, the menu returns to the first screen. That is, to launch the next application you need to scroll through the menu again starting from the screen number 1.All applications are numbered from 1 to n. We know a certain order in which the icons of the applications are located in the menu at the beginning, but it changes as long as you use the operating system. Berdroid is intelligent system, so it changes the order of the icons by moving the more frequently used icons to the beginning of the list. Formally, right after an application is launched, Berdroid swaps the application icon and the icon of a preceding application (that is, the icon of an application on the position that is smaller by one in the order of menu). The preceding icon may possibly be located on the adjacent screen. The only exception is when the icon of the launched application already occupies the first place, in this case the icon arrangement doesn't change.Anya has planned the order in which she will launch applications. How many gestures should Anya make to launch the applications in the planned order? Note that one application may be launched multiple times.
|
The first line of the input contains three numbers n, m, k (1 β€ n, m, k β€ 105) β the number of applications that Anya has on her smartphone, the number of applications that will be launched and the number of icons that are located on the same screen.The next line contains n integers, permutation a1, a2, ..., an β the initial order of icons from left to right in the menu (from the first to the last one), ai β is the id of the application, whose icon goes i-th in the menu. Each integer from 1 to n occurs exactly once among ai.The third line contains m integers b1, b2, ..., bm(1 β€ bi β€ n) β the ids of the launched applications in the planned order. One application may be launched multiple times.
|
Print a single number β the number of gestures that Anya needs to make to launch all the applications in the desired order.
|
In the first test the initial configuration looks like (123)(456)(78), that is, the first screen contains icons of applications 1, 2, 3, the second screen contains icons 4, 5, 6, the third screen contains icons 7, 8. After application 7 is launched, we get the new arrangement of the icons β (123)(457)(68). To launch it Anya makes 3 gestures. After application 8 is launched, we get configuration (123)(457)(86). To launch it Anya makes 3 gestures. After application 1 is launched, the arrangement of icons in the menu doesn't change. To launch it Anya makes 1 gesture.In total, Anya makes 7 gestures.
|
Input: 8 3 31 2 3 4 5 6 7 87 8 1 | Output: 7
|
Medium
| 3 | 1,974 | 701 | 123 | 5 |
2,094 |
B
|
2094B
|
B. Bobritto Bandito
| 800 |
brute force; constructive algorithms
|
In Bobritto Bandito's home town of residence, there are an infinite number of houses on an infinite number line, with houses at \(\ldots, -2, -1, 0, 1, 2, \ldots\). On day \(0\), he started a plague by giving an infection to the unfortunate residents of house \(0\). Each succeeding day, the plague spreads to exactly one healthy household that is next to an infected household. It can be shown that each day the infected houses form a continuous segment.Let the segment starting at the \(l\)-th house and ending at the \(r\)-th house be denoted as \([l, r]\). You know that after \(n\) days, the segment \([l, r]\) became infected. Find any such segment \([l', r']\) that could have been infected on the \(m\)-th day (\(m \le n\)).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of independent test cases.The only line of each test case contains four integers \(n\), \(m\), \(l\), and \(r\) (\(1 \leq m\leq n \leq 2000, -n \leq l \leq 0 \leq r \leq n, r-l=n\)).
|
For each test case, output two integers \(l'\) and \(r'\) on a new line. If there are multiple solutions, output any.
|
In the first test case, it is possible that on the \(1\)-st, \(2\)-nd, and \(3\)-rd days the interval of houses affected is \([-1,0]\), \([-1,1]\), \([-2,1]\). Therefore, \([-1,1]\) is a valid output.
|
Input: 44 2 -2 24 1 0 43 3 -1 29 8 -6 3 | Output: -1 1 0 1 -1 2 -5 3
|
Beginner
| 2 | 732 | 260 | 117 | 20 |
1,535 |
F
|
1535F
|
F. String Distance
| 3,000 |
binary search; brute force; data structures; hashing; implementation; strings
|
Suppose you are given two strings \(a\) and \(b\). You can apply the following operation any number of times: choose any contiguous substring of \(a\) or \(b\), and sort the characters in it in non-descending order. Let \(f(a, b)\) the minimum number of operations you have to apply in order to make them equal (or \(f(a, b) = 1337\) if it is impossible to make \(a\) and \(b\) equal using these operations).For example: \(f(\text{ab}, \text{ab}) = 0\); \(f(\text{ba}, \text{ab}) = 1\) (in one operation, we can sort the whole first string); \(f(\text{ebcda}, \text{ecdba}) = 1\) (in one operation, we can sort the substring of the second string starting from the \(2\)-nd character and ending with the \(4\)-th character); \(f(\text{a}, \text{b}) = 1337\). You are given \(n\) strings \(s_1, s_2, \dots, s_k\) having equal length. Calculate \(\sum \limits_{i = 1}^{n} \sum\limits_{j = i + 1}^{n} f(s_i, s_j)\).
|
The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of strings.Then \(n\) lines follow, each line contains one of the strings \(s_i\), consisting of lowercase Latin letters. \(|s_1| = |s_2| = \ldots = |s_n|\), and \(n \cdot |s_1| \le 2 \cdot 10^5\). All these strings are pairwise distinct.
|
Print one integer: \(\sum \limits_{i = 1}^{n} \sum\limits_{j = i + 1}^{n} f(s_i, s_j)\).
|
Input: 4 zzz bac abc acb | Output: 4015
|
Master
| 6 | 911 | 324 | 88 | 15 |
|
333 |
D
|
333D
|
D. Characteristics of Rectangles
| 2,100 |
binary search; bitmasks; brute force; implementation; sortings
|
Gerald found a table consisting of n rows and m columns. As a prominent expert on rectangular tables, he immediately counted the table's properties, that is, the minimum of the numbers in the corners of the table (minimum of four numbers). However, he did not like the final value β it seemed to be too small. And to make this value larger, he decided to crop the table a little: delete some columns on the left and some on the right, as well as some rows from the top and some from the bottom. Find what the maximum property of the table can be after such cropping. Note that the table should have at least two rows and at least two columns left in the end. The number of cropped rows or columns from each of the four sides can be zero.
|
The first line contains two space-separated integers n and m (2 β€ n, m β€ 1000). The following n lines describe the table. The i-th of these lines lists the space-separated integers ai, 1, ai, 2, ..., ai, m (0 β€ ai, j β€ 109) β the m numbers standing in the i-th row of the table.
|
Print the answer to the problem.
|
In the first test case Gerald cannot crop the table β table contains only two rows and only two columns.In the second test case if we'll crop the table, the table will contain zero in some corner cell. Also initially it contains two zeros in the corner cells, so the answer is 0.
|
Input: 2 21 23 4 | Output: 1
|
Hard
| 5 | 737 | 278 | 32 | 3 |
1,284 |
D
|
1284D
|
D. New Year and Conference
| 2,100 |
binary search; data structures; hashing; sortings
|
Filled with optimism, Hyunuk will host a conference about how great this new year will be!The conference will have \(n\) lectures. Hyunuk has two candidate venues \(a\) and \(b\). For each of the \(n\) lectures, the speaker specified two time intervals \([sa_i, ea_i]\) (\(sa_i \le ea_i\)) and \([sb_i, eb_i]\) (\(sb_i \le eb_i\)). If the conference is situated in venue \(a\), the lecture will be held from \(sa_i\) to \(ea_i\), and if the conference is situated in venue \(b\), the lecture will be held from \(sb_i\) to \(eb_i\). Hyunuk will choose one of these venues and all lectures will be held at that venue.Two lectures are said to overlap if they share any point in time in common. Formally, a lecture held in interval \([x, y]\) overlaps with a lecture held in interval \([u, v]\) if and only if \(\max(x, u) \le \min(y, v)\).We say that a participant can attend a subset \(s\) of the lectures if the lectures in \(s\) do not pairwise overlap (i.e. no two lectures overlap). Note that the possibility of attending may depend on whether Hyunuk selected venue \(a\) or venue \(b\) to hold the conference.A subset of lectures \(s\) is said to be venue-sensitive if, for one of the venues, the participant can attend \(s\), but for the other venue, the participant cannot attend \(s\).A venue-sensitive set is problematic for a participant who is interested in attending the lectures in \(s\) because the participant cannot be sure whether the lecture times will overlap. Hyunuk will be happy if and only if there are no venue-sensitive sets. Determine whether Hyunuk will be happy.
|
The first line contains an integer \(n\) (\(1 \le n \le 100\,000\)), the number of lectures held in the conference.Each of the next \(n\) lines contains four integers \(sa_i\), \(ea_i\), \(sb_i\), \(eb_i\) (\(1 \le sa_i, ea_i, sb_i, eb_i \le 10^9\), \(sa_i \le ea_i, sb_i \le eb_i\)).
|
Print ""YES"" if Hyunuk will be happy. Print ""NO"" otherwise.You can print each letter in any case (upper or lower).
|
In second example, lecture set \(\{1, 3\}\) is venue-sensitive. Because participant can't attend this lectures in venue \(a\), but can attend in venue \(b\).In first and third example, venue-sensitive set does not exist.
|
Input: 2 1 2 3 6 3 4 7 8 | Output: YES
|
Hard
| 4 | 1,588 | 284 | 117 | 12 |
799 |
A
|
799A
|
A. Carrot Cakes
| 1,100 |
brute force; implementation
|
In some game by Playrix it takes t minutes for an oven to bake k carrot cakes, all cakes are ready at the same moment t minutes after they started baking. Arkady needs at least n cakes to complete a task, but he currently don't have any. However, he has infinitely many ingredients and one oven. Moreover, Arkady can build one more similar oven to make the process faster, it would take d minutes to build the oven. While the new oven is being built, only old one can bake cakes, after the new oven is built, both ovens bake simultaneously. Arkady can't build more than one oven.Determine if it is reasonable to build the second oven, i.e. will it decrease the minimum time needed to get n cakes or not. If the time needed with the second oven is the same as with one oven, then it is unreasonable.
|
The only line contains four integers n, t, k, d (1 β€ n, t, k, d β€ 1 000) β the number of cakes needed, the time needed for one oven to bake k cakes, the number of cakes baked at the same time, the time needed to build the second oven.
|
If it is reasonable to build the second oven, print ""YES"". Otherwise print ""NO"".
|
In the first example it is possible to get 8 cakes in 12 minutes using one oven. The second oven can be built in 5 minutes, so after 6 minutes the first oven bakes 4 cakes, the second oven bakes 4 more ovens after 11 minutes. Thus, it is reasonable to build the second oven. In the second example it doesn't matter whether we build the second oven or not, thus it takes 12 minutes to bake 8 cakes in both cases. Thus, it is unreasonable to build the second oven.In the third example the first oven bakes 11 cakes in 3 minutes, that is more than needed 10. It is unreasonable to build the second oven, because its building takes more time that baking the needed number of cakes using the only oven.
|
Input: 8 6 4 5 | Output: YES
|
Easy
| 2 | 798 | 234 | 84 | 7 |
1,658 |
C
|
1658C
|
C. Shinju and the Lost Permutation
| 1,700 |
constructive algorithms; math
|
Shinju loves permutations very much! Today, she has borrowed a permutation \(p\) from Juju to play with.The \(i\)-th cyclic shift of a permutation \(p\) is a transformation on the permutation such that \(p = [p_1, p_2, \ldots, p_n] \) will now become \( p = [p_{n-i+1}, \ldots, p_n, p_1,p_2, \ldots, p_{n-i}]\).Let's define the power of permutation \(p\) as the number of distinct elements in the prefix maximums array \(b\) of the permutation. The prefix maximums array \(b\) is the array of length \(n\) such that \(b_i = \max(p_1, p_2, \ldots, p_i)\). For example, the power of \([1, 2, 5, 4, 6, 3]\) is \(4\) since \(b=[1,2,5,5,6,6]\) and there are \(4\) distinct elements in \(b\).Unfortunately, Shinju has lost the permutation \(p\)! The only information she remembers is an array \(c\), where \(c_i\) is the power of the \((i-1)\)-th cyclic shift of the permutation \(p\). She's also not confident that she remembers it correctly, so she wants to know if her memory is good enough.Given the array \(c\), determine if there exists a permutation \(p\) that is consistent with \(c\). You do not have to construct the permutation \(p\).A permutation is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array) and \([1,3, 4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 5 \cdot 10^3\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains \(n\) integers \(c_1,c_2,\ldots,c_n\) (\(1 \leq c_i \leq n\)). It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, print ""YES"" if there is a permutation \(p\) exists that satisfies the array \(c\), and ""NO"" otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive response).
|
In the first test case, the permutation \([1]\) satisfies the array \(c\).In the second test case, the permutation \([2,1]\) satisfies the array \(c\).In the fifth test case, the permutation \([5, 1, 2, 4, 6, 3]\) satisfies the array \(c\). Let's see why this is true. The zeroth cyclic shift of \(p\) is \([5, 1, 2, 4, 6, 3]\). Its power is \(2\) since \(b = [5, 5, 5, 5, 6, 6]\) and there are \(2\) distinct elements β \(5\) and \(6\). The first cyclic shift of \(p\) is \([3, 5, 1, 2, 4, 6]\). Its power is \(3\) since \(b=[3,5,5,5,5,6]\). The second cyclic shift of \(p\) is \([6, 3, 5, 1, 2, 4]\). Its power is \(1\) since \(b=[6,6,6,6,6,6]\). The third cyclic shift of \(p\) is \([4, 6, 3, 5, 1, 2]\). Its power is \(2\) since \(b=[4,6,6,6,6,6]\). The fourth cyclic shift of \(p\) is \([2, 4, 6, 3, 5, 1]\). Its power is \(3\) since \(b = [2, 4, 6, 6, 6, 6]\). The fifth cyclic shift of \(p\) is \([1, 2, 4, 6, 3, 5]\). Its power is \(4\) since \(b = [1, 2, 4, 6, 6, 6]\). Therefore, \(c = [2, 3, 1, 2, 3, 4]\).In the third, fourth, and sixth testcases, we can show that there is no permutation that satisfies array \(c\).
|
Input: 61121 222 261 2 4 6 3 562 3 1 2 3 433 2 1 | Output: YES YES NO NO YES NO
|
Medium
| 2 | 1,447 | 422 | 275 | 16 |
431 |
D
|
431D
|
D. Random Task
| 2,100 |
binary search; bitmasks; combinatorics; dp; math
|
One day, after a difficult lecture a diligent student Sasha saw a graffitied desk in the classroom. She came closer and read: ""Find such positive integer n, that among numbers n + 1, n + 2, ..., 2Β·n there are exactly m numbers which binary representation contains exactly k digits one"".The girl got interested in the task and she asked you to help her solve it. Sasha knows that you are afraid of large numbers, so she guaranteed that there is an answer that doesn't exceed 1018.
|
The first line contains two space-separated integers, m and k (0 β€ m β€ 1018; 1 β€ k β€ 64).
|
Print the required number n (1 β€ n β€ 1018). If there are multiple answers, print any of them.
|
Input: 1 1 | Output: 1
|
Hard
| 5 | 481 | 89 | 93 | 4 |
|
226 |
E
|
226E
|
E. Noble Knight's Path
| 2,900 |
data structures; trees
|
In Berland each feudal owns exactly one castle and each castle belongs to exactly one feudal.Each feudal, except one (the King) is subordinate to another feudal. A feudal can have any number of vassals (subordinates).Some castles are connected by roads, it is allowed to move along the roads in both ways. Two castles have a road between them if and only if the owner of one of these castles is a direct subordinate to the other owner.Each year exactly one of these two events may happen in Berland. The barbarians attacked castle c. The interesting fact is, the barbarians never attacked the same castle twice throughout the whole Berlandian history. A noble knight sets off on a journey from castle a to castle b (provided that on his path he encounters each castle not more than once). Let's consider the second event in detail. As the journey from a to b is not short, then the knight might want to stop at a castle he encounters on his way to have some rest. However, he can't stop at just any castle: his nobility doesn't let him stay in the castle that has been desecrated by the enemy's stench. A castle is desecrated if and only if it has been attacked after the year of y. So, the knight chooses the k-th castle he encounters, starting from a (castles a and b aren't taken into consideration), that hasn't been attacked in years from y + 1 till current year.The knights don't remember which castles were attacked on what years, so he asked the court scholar, aka you to help them. You've got a sequence of events in the Berland history. Tell each knight, in what city he should stop or else deliver the sad news β that the path from city a to city b has less than k cities that meet his requirements, so the knight won't be able to rest.
|
The first input line contains integer n (2 β€ n β€ 105) β the number of feudals. The next line contains n space-separated integers: the i-th integer shows either the number of the i-th feudal's master, or a 0, if the i-th feudal is the King. The third line contains integer m (1 β€ m β€ 105) β the number of queries.Then follow m lines that describe the events. The i-th line (the lines are indexed starting from 1) contains the description of the event that occurred in year i. Each event is characterised by type ti (1 β€ ti β€ 2). The description of the first type event looks as two space-separated integers ti ci (ti = 1; 1 β€ ci β€ n), where ci is the number of the castle that was attacked by the barbarians in the i-th year. The description of the second type contains five space-separated integers: ti ai bi ki yi (ti = 2; 1 β€ ai, bi, ki β€ n; ai β bi; 0 β€ yi < i), where ai is the number of the castle from which the knight is setting off, bi is the number of the castle to which the knight is going, ki and yi are the k and y from the second event's description.You can consider the feudals indexed from 1 to n. It is guaranteed that there is only one king among the feudals. It is guaranteed that for the first type events all values ci are different.
|
For each second type event print an integer β the number of the castle where the knight must stay to rest, or -1, if he will have to cover the distance from ai to bi without a rest. Separate the answers by whitespaces.Print the answers in the order, in which the second type events are given in the input.
|
In the first sample there is only castle 2 on the knight's way from castle 1 to castle 3. When the knight covers the path 1 - 3 for the first time, castle 2 won't be desecrated by an enemy and the knight will stay there. In the second year the castle 2 will become desecrated, so the knight won't have anywhere to stay for the next two years (as finding a castle that hasn't been desecrated from years 1 and 2, correspondingly, is important for him). In the fifth year the knight won't consider the castle 2 desecrated, so he will stay there again.
|
Input: 30 1 252 1 3 1 01 22 1 3 1 02 1 3 1 12 1 3 1 2 | Output: 2-1-12
|
Master
| 2 | 1,747 | 1,254 | 305 | 2 |
1,353 |
E
|
1353E
|
E. K-periodic Garland
| 1,900 |
brute force; dp; greedy
|
You are given a garland consisting of \(n\) lamps. States of the lamps are represented by the string \(s\) of length \(n\). The \(i\)-th character of the string \(s_i\) equals '0' if the \(i\)-th lamp is turned off or '1' if the \(i\)-th lamp is turned on. You are also given a positive integer \(k\).In one move, you can choose one lamp and change its state (i.e. turn it on if it is turned off and vice versa).The garland is called \(k\)-periodic if the distance between each pair of adjacent turned on lamps is exactly \(k\). Consider the case \(k=3\). Then garlands ""00010010"", ""1001001"", ""00010"" and ""0"" are good but garlands ""00101001"", ""1000001"" and ""01001100"" are not. Note that the garland is not cyclic, i.e. the first turned on lamp is not going after the last turned on lamp and vice versa.Your task is to find the minimum number of moves you need to make to obtain \(k\)-periodic garland from the given one.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 25~ 000\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains two integers \(n\) and \(k\) (\(1 \le n \le 10^6; 1 \le k \le n\)) β the length of \(s\) and the required period. The second line of the test case contains the string \(s\) consisting of \(n\) characters '0' and '1'.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\) (\(\sum n \le 10^6\)).
|
For each test case, print the answer β the minimum number of moves you need to make to obtain \(k\)-periodic garland from the given one.
|
Input: 6 9 2 010001010 9 3 111100000 7 4 1111111 10 3 1001110101 1 1 1 1 1 0 | Output: 1 2 5 4 0 0
|
Hard
| 3 | 982 | 501 | 136 | 13 |
|
1,142 |
D
|
1142D
|
D. Foreigner
| 2,800 |
dp
|
Traveling around the world you noticed that many shop owners raise prices to inadequate values if the see you are a foreigner.You define inadequate numbers as follows: all integers from \(1\) to \(9\) are inadequate; for an integer \(x \ge 10\) to be inadequate, it is required that the integer \(\lfloor x / 10 \rfloor\) is inadequate, but that's not the only condition. Let's sort all the inadequate integers. Let \(\lfloor x / 10 \rfloor\) have number \(k\) in this order. Then, the integer \(x\) is inadequate only if the last digit of \(x\) is strictly less than the reminder of division of \(k\) by \(11\). Here \(\lfloor x / 10 \rfloor\) denotes \(x/10\) rounded down.Thus, if \(x\) is the \(m\)-th in increasing order inadequate number, and \(m\) gives the remainder \(c\) when divided by \(11\), then integers \(10 \cdot x + 0, 10 \cdot x + 1 \ldots, 10 \cdot x + (c - 1)\) are inadequate, while integers \(10 \cdot x + c, 10 \cdot x + (c + 1), \ldots, 10 \cdot x + 9\) are not inadequate.The first several inadequate integers are \(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 21, 30, 31, 32 \ldots\). After that, since \(4\) is the fourth inadequate integer, \(40, 41, 42, 43\) are inadequate, while \(44, 45, 46, \ldots, 49\) are not inadequate; since \(10\) is the \(10\)-th inadequate number, integers \(100, 101, 102, \ldots, 109\) are all inadequate. And since \(20\) is the \(11\)-th inadequate number, none of \(200, 201, 202, \ldots, 209\) is inadequate.You wrote down all the prices you have seen in a trip. Unfortunately, all integers got concatenated in one large digit string \(s\) and you lost the bounds between the neighboring integers. You are now interested in the number of substrings of the resulting string that form an inadequate number. If a substring appears more than once at different positions, all its appearances are counted separately.
|
The only line contains the string \(s\) (\(1 \le |s| \le 10^5\)), consisting only of digits. It is guaranteed that the first digit of \(s\) is not zero.
|
In the only line print the number of substrings of \(s\) that form an inadequate number.
|
In the first example the inadequate numbers in the string are \(1, 2, 4, 21, 40, 402\). In the second example the inadequate numbers in the string are \(1\) and \(10\), and \(1\) appears twice (on the first and on the second positions).
|
Input: 4021 | Output: 6
|
Master
| 1 | 1,865 | 152 | 88 | 11 |
1,956 |
E2
|
1956E2
|
E2. Nene vs. Monsters (Hard Version)
| 2,700 |
brute force; greedy; implementation; math
|
This is the hard version of the problem. The only difference between the versions is the constraints on \(a_i\). You can make hacks only if both versions of the problem are solved.Nene is fighting with \(n\) monsters, located in a circle. These monsters are numbered from \(1\) to \(n\), and the \(i\)-th (\(1 \le i \le n\)) monster's current energy level is \(a_i\).Since the monsters are too strong, Nene decided to fight with them using the Attack Your Neighbour spell. When Nene uses this spell, the following actions happen in the following order one by one: The \(1\)-st monster attacks the \(2\)-nd monster; The \(2\)-nd monster attacks the \(3\)-rd monster; \(\ldots\) The \((n-1)\)-th monster attacks the \(n\)-th monster; The \(n\)-th monster attacks the \(1\)-st monster. When the monster with energy level \(x\) attacks the monster with the energy level \(y\), the energy level of the defending monster becomes \(\max(0, y-x)\) (the energy level of the attacking monster remains equal to \(x\)).Nene is going to use this spell \(10^{100}\) times and deal with the monsters that will still have a non-zero energy level herself. She wants you to determine which monsters will have a non-zero energy level once she will use the described spell \(10^{100}\) times.
|
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 test cases follows.The first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of monsters.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) β the current energy levels of monsters.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, in the first line output an integer \(m\) β the number of monsters with non-zero energy level after \(10^{100}\) uses of the spell; in the second line of output \(m\) integers \(i_1,i_2,\ldots,i_m\) (\(1 \le i_1 < i_2 < \ldots < i_m \le n\)) β the indices of these monsters in the increasing order. If \(m=0\), you may either output an empty line or don't output it.
|
In the first test case, the following actions happen during the first \(3\) uses of the spell in this order: Nene uses the Attack Your Neighbour spell for the first time; the \(1\)-st monster attacks the \(2\)-nd monster, after the attack the energy level of the \(2\)-nd monster becomes equal to \(\max(0, 5-2)=3\); the \(2\)-nd monster attacks the \(3\)-rd monster, after the attack the energy level of the \(3\)-rd monster becomes equal to \(\max(0, 3-3)=0\); the \(3\)-rd monster attacks the \(1\)-st monster, after the attack the energy level of the \(1\)-st monster becomes equal to \(\max(0, 2-0)=2\); Nene uses the Attack Your Neighbour spell for the second time; the \(1\)-st monster attacks the \(2\)-nd monster, after the attack the energy level of the \(2\)-nd monster becomes equal to \(\max(0, 3-2)=1\); the \(2\)-nd monster attacks the \(3\)-rd monster, after the attack the energy level of the \(3\)-rd monster becomes equal to \(\max(0, 0-1)=0\); the \(3\)-rd monster attacks the \(1\)-st monster, after the attack the energy level of the \(1\)-st monster becomes equal to \(\max(0, 2-0)=2\); Nene uses the Attack Your Neighbour spell for the third time; the \(1\)-st monster attacks the \(2\)-nd monster, after the attack the energy level of the \(2\)-nd monster becomes equal to \(\max(0, 1-2)=0\); the \(2\)-nd monster attacks the \(3\)-rd monster, after the attack the energy level of the \(3\)-rd monster becomes equal to \(\max(0, 0-0)=0\); the \(3\)-rd monster attacks the \(1\)-st monster, after the attack the energy level of the \(1\)-st monster becomes equal to \(\max(0, 2-0)=2\). After each of the next uses of the spell, energy levels of monsters do not change. Thus, only the \(1\)-st monster has a non-zero energy level in the end.In the second test case, both monsters initially have zero energy level.
|
Input: 532 5 320 041 5 7 244 2 1 2131 1 4 5 1 4 1 9 1 9 8 1 0 | Output: 1 1 0 1 1 2 1 3 6 1 3 6 8 10 12
|
Master
| 4 | 1,272 | 483 | 386 | 19 |
173 |
B
|
173B
|
B. Chamber of Secrets
| 1,800 |
dfs and similar; shortest paths
|
""The Chamber of Secrets has been opened again"" β this news has spread all around Hogwarts and some of the students have been petrified due to seeing the basilisk. Dumbledore got fired and now Harry is trying to enter the Chamber of Secrets. These aren't good news for Lord Voldemort. The problem is, he doesn't want anybody to be able to enter the chamber. The Dark Lord is going to be busy sucking life out of Ginny.The Chamber of Secrets is an n Γ m rectangular grid in which some of the cells are columns. A light ray (and a basilisk's gaze) passes through the columns without changing its direction. But with some spell we can make a column magic to reflect the light ray (or the gaze) in all four directions when it receives the ray. This is shown in the figure below. The left light ray passes through a regular column, and the right ray β through the magic column. The basilisk is located at the right side of the lower right cell of the grid and is looking to the left (in the direction of the lower left cell). According to the legend, anyone who meets a basilisk's gaze directly dies immediately. But if someone meets a basilisk's gaze through a column, this person will get petrified. We know that the door to the Chamber is located on the left side of the upper left corner of the grid and anyone who wants to enter will look in the direction of its movement (in the direction of the upper right cell) from that position. This figure illustrates the first sample test. Given the dimensions of the chamber and the location of regular columns, Lord Voldemort has asked you to find the minimum number of columns that we need to make magic so that anyone who wants to enter the chamber would be petrified or just declare that it's impossible to secure the chamber.
|
The first line of the input contains two integer numbers n and m (2 β€ n, m β€ 1000). Each of the next n lines contains m characters. Each character is either ""."" or ""#"" and represents one cell of the Chamber grid. It's ""."" if the corresponding cell is empty and ""#"" if it's a regular column.
|
Print the minimum number of columns to make magic or -1 if it's impossible to do.
|
The figure above shows the first sample test. In the first sample we should make both columns magic. The dragon figure represents the basilisk and the binoculars represent the person who will enter the Chamber of secrets. The black star shows the place where the person will be petrified. Yellow lines represent basilisk gaze moving through columns.
|
Input: 3 3.#.....#. | Output: 2
|
Medium
| 2 | 1,774 | 298 | 81 | 1 |
109 |
A
|
109A
|
A. Lucky Sum of Digits
| 1,000 |
brute force; implementation
|
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.Petya wonders eagerly what minimum lucky number has the sum of digits equal to n. Help him cope with the task.
|
The single line contains an integer n (1 β€ n β€ 106) β the sum of digits of the required lucky number.
|
Print on the single line the result β the minimum lucky number, whose sum of digits equals n. If such number does not exist, print -1.
|
Input: 11 | Output: 47
|
Beginner
| 2 | 328 | 101 | 134 | 1 |
|
566 |
A
|
566A
|
A. Matching Names
| 2,300 |
dfs and similar; strings; trees
|
Teachers of one programming summer school decided to make a surprise for the students by giving them names in the style of the ""Hobbit"" movie. Each student must get a pseudonym maximally similar to his own name. The pseudonym must be a name of some character of the popular saga and now the teachers are busy matching pseudonyms to student names.There are n students in a summer school. Teachers chose exactly n pseudonyms for them. Each student must get exactly one pseudonym corresponding to him. Let us determine the relevance of a pseudonym b to a student with name a as the length of the largest common prefix a and b. We will represent such value as . Then we can determine the quality of matching of the pseudonyms to students as a sum of relevances of all pseudonyms to the corresponding students.Find the matching between students and pseudonyms with the maximum quality.
|
The first line contains number n (1 β€ n β€ 100 000) β the number of students in the summer school.Next n lines contain the name of the students. Each name is a non-empty word consisting of lowercase English letters. Some names can be repeating.The last n lines contain the given pseudonyms. Each pseudonym is a non-empty word consisting of small English letters. Some pseudonyms can be repeating.The total length of all the names and pseudonyms doesn't exceed 800 000 characters.
|
In the first line print the maximum possible quality of matching pseudonyms to students.In the next n lines describe the optimal matching. Each line must have the form a b (1 β€ a, b β€ n), that means that the student who was number a in the input, must match to the pseudonym number b in the input.The matching should be a one-to-one correspondence, that is, each student and each pseudonym should occur exactly once in your output. If there are several optimal answers, output any.
|
The first test from the statement the match looks as follows: bill β bilbo (lcp = 3) galya β galadriel (lcp = 3) gennady β gendalf (lcp = 3) toshik β torin (lcp = 2) boris β smaug (lcp = 0)
|
Input: 5gennadygalyaborisbilltoshikbilbotoringendalfsmauggaladriel | Output: 114 12 51 35 23 4
|
Expert
| 3 | 882 | 478 | 481 | 5 |
1,519 |
E
|
1519E
|
E. Off by One
| 2,700 |
constructive algorithms; dfs and similar; geometry; graphs; sortings; trees
|
There are \(n\) points on an infinite plane. The \(i\)-th point has coordinates \((x_i, y_i)\) such that \(x_i > 0\) and \(y_i > 0\). The coordinates are not necessarily integer.In one move you perform the following operations: choose two points \(a\) and \(b\) (\(a \neq b\)); move point \(a\) from \((x_a, y_a)\) to either \((x_a + 1, y_a)\) or \((x_a, y_a + 1)\); move point \(b\) from \((x_b, y_b)\) to either \((x_b + 1, y_b)\) or \((x_b, y_b + 1)\); remove points \(a\) and \(b\). However, the move can only be performed if there exists a line that passes through the new coordinates of \(a\), new coordinates of \(b\) and \((0, 0)\). Otherwise, the move can't be performed and the points stay at their original coordinates \((x_a, y_a)\) and \((x_b, y_b)\), respectively.The numeration of points does not change after some points are removed. Once the points are removed, they can't be chosen in any later moves. Note that you have to move both points during the move, you can't leave them at their original coordinates.What is the maximum number of moves you can perform? What are these moves?If there are multiple answers, you can print any of them.
|
The first line contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the number of points.The \(i\)-th of the next \(n\) lines contains four integers \(a_i, b_i, c_i, d_i\) (\(1 \le a_i, b_i, c_i, d_i \le 10^9\)). The coordinates of the \(i\)-th point are \(x_i = \frac{a_i}{b_i}\) and \(y_i = \frac{c_i}{d_i}\).
|
In the first line print a single integer \(c\) β the maximum number of moves you can perform.Each of the next \(c\) lines should contain a description of a move: two integers \(a\) and \(b\) (\(1 \le a, b \le n\), \(a \neq b\)) β the points that are removed during the current move. There should be a way to move points \(a\) and \(b\) according to the statement so that there's a line that passes through the new coordinates of \(a\), the new coordinates of \(b\) and \((0, 0)\). No removed point can be chosen in a later move.If there are multiple answers, you can print any of them. You can print the moves and the points in the move in the arbitrary order.
|
Here are the points and the moves for the ones that get chosen for the moves from the first example:
|
Input: 7 4 1 5 1 1 1 1 1 3 3 3 3 1 1 4 1 6 1 1 1 5 1 4 1 6 1 1 1 | Output: 3 1 6 2 4 5 7
|
Master
| 6 | 1,158 | 323 | 660 | 15 |
1,302 |
I
|
1302I
|
I. Deja vu
| 0 |
This is an unusual problem in an unusual contest, here is the announcement: http://codeforces.com/blog/entry/73543You run a string shop. During a day your customers want to buy strings of certain lengths and sometimes satisfying other properties.Today your first customer asked you if you have a string of length \(k\). In fact, you have a string of length \(n\) and can cut a string of length \(k\) starting at any position out of it. You wonder how many distinct values this string can equal.Please note that in your shop strings are over an alphabet of size \(2\).
|
The first line contains two integers \(n\) and \(k\) (\(1\leq k\leq n\leq 200\,000\)). The second line contains a binary string \(s\) of length \(n\).
|
In the only line print the number of distinct substrings of length \(k\) of the string \(s\).
|
The second sample test represents the de Bruijn sequence of order 3.
|
Input: 5 2 01101 | Output: 3
|
Beginner
| 0 | 567 | 150 | 93 | 13 |
|
2,013 |
A
|
2013A
|
A. Zhan's Blender
| 800 |
constructive algorithms; math
|
Today, a club fair was held at ""NSPhM"". In order to advertise his pastry club, Zhan decided to demonstrate the power of his blender.To demonstrate the power of his blender, Zhan has \(n\) fruits.The blender can mix up to \(x\) fruits per second.In each second, Zhan can put up to \(y\) fruits into the blender. After that, the blender will blend \(\min(x, c)\) fruits, where \(c\) is the number of fruits inside the blender. After blending, blended fruits are removed from the blender.Help Zhan determine the minimum amount of time required for Zhan to blend all fruits.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). The description of the test cases follows.The first line of each test case contains one integer \(n\) (\(0 \le n \le 10^9\)) β the number of fruits Zhan has.The second line of each test case contains two integers \(x\) and \(y\) (\(1 \le x, y \le 10^9\)) β the number of fruits the blender can blend per second and the number of fruits Zhan can put into the blender per second.
|
For each testcase, output a single integer β the minimum number of seconds to blend all fruits.
|
In the first example, you can first put \(2\) fruits in the blender. After that, the blender will mix these \(2\) fruits, and in the end, there will be \(0\) fruits left in the blender. Then you can put \(3\) fruits into the blender, after which the blender will mix these \(3\) fruits.In the second example, you can put \(1\) fruit into the blender \(3\) times.In the third example, you can first put \(3\) fruits into the blender, then add another \(3\) fruits.
|
Input: 553 431 264 31004 393 3 | Output: 2 3 2 34 3
|
Beginner
| 2 | 572 | 496 | 95 | 20 |
1,315 |
C
|
1315C
|
C. Restoring Permutation
| 1,200 |
greedy
|
You are given a sequence \(b_1, b_2, \ldots, b_n\). Find the lexicographically minimal permutation \(a_1, a_2, \ldots, a_{2n}\) such that \(b_i = \min(a_{2i-1}, a_{2i})\), or determine that it is impossible.
|
Each test contains one or more test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)).The first line of each test case consists of one integer \(n\) β the number of elements in the sequence \(b\) (\(1 \le n \le 100\)).The second line of each test case consists of \(n\) different integers \(b_1, \ldots, b_n\) β elements of the sequence \(b\) (\(1 \le b_i \le 2n\)).It is guaranteed that the sum of \(n\) by all test cases doesn't exceed \(100\).
|
For each test case, if there is no appropriate permutation, print one number \(-1\).Otherwise, print \(2n\) integers \(a_1, \ldots, a_{2n}\) β required lexicographically minimal permutation of numbers from \(1\) to \(2n\).
|
Input: 5 1 1 2 4 1 3 4 1 3 4 2 3 4 5 5 1 5 7 2 8 | Output: 1 2 -1 4 5 1 2 3 6 -1 1 3 5 6 7 9 2 4 8 10
|
Easy
| 1 | 207 | 480 | 222 | 13 |
|
2,056 |
F2
|
2056F2
|
F2. Xor of Median (Hard Version)
| 3,000 |
bitmasks; brute force; combinatorics; dp; math
|
This is the hard version of the problem. The difference between the versions is that in this version, the constraints on \(t\), \(k\), and \(m\) are higher. You can hack only if you solved all versions of this problem. A sequence \(a\) of \(n\) integers is called good if the following condition holds: Let \(\text{cnt}_x\) be the number of occurrences of \(x\) in sequence \(a\). For all pairs \(0 \le i < j < m\), at least one of the following has to be true: \(\text{cnt}_i = 0\), \(\text{cnt}_j = 0\), or \(\text{cnt}_i \le \text{cnt}_j\). In other words, if both \(i\) and \(j\) are present in sequence \(a\), then the number of occurrences of \(i\) in \(a\) is less than or equal to the number of occurrences of \(j\) in \(a\). You are given integers \(n\) and \(m\). Calculate the value of the bitwise XOR of the median\(^{\text{β}}\) of all good sequences \(a\) of length \(n\) with \(0\le a_i < m\). Note that the value of \(n\) can be very large, so you are given its binary representation instead.\(^{\text{β}}\)The median of a sequence \(a\) of length \(n\) is defined as the \(\left\lfloor\frac{n + 1}{2}\right\rfloor\)-th smallest value in the sequence.
|
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 two integers \(k\) and \(m\) (\(1 \le k \le 2 \cdot 10^5\), \(1 \le m \le 10^9\)) β the number of bits in \(n\) and the upper bound on the elements in sequence \(a\).The second line of each test case contains a binary string of length \(k\) β the binary representation of \(n\) with no leading zeros.It is guaranteed that the sum of \(k\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output a single integer representing the bitwise XOR of the median of all good sequences \(a\) of length \(n\) where \(0\le a_i < m\).
|
In the first example, \(n = 10_2 = 2\) and \(m = 3\). All possible sequences with elements less than \(m\) are: \([0, 0]\), \([0, 1]\), \([0, 2]\), \([1, 0]\), \([1, 1]\), \([1, 2]\), \([2, 0]\), \([2, 1]\), \([2, 2]\). All of them are good, so the answer is: \(0 \oplus 0 \oplus 0 \oplus 0 \oplus 1 \oplus 1 \oplus 0 \oplus 1 \oplus 2 = 3\).In the second example, \(n = 11_2 = 3\) and \(m = 3\). Some good sequences are \([2, 2, 2]\), \([1, 0, 1]\), and \([2, 0, 1]\). However, a sequence \([2, 0, 0]\) is not good, because \(\text{cnt}_0 = 2\), \(\text{cnt}_2 = 1\). Therefore, if we set \(i = 0\) and \(j = 2\), \(i < j\) holds, but \(\text{cnt}_i \le \text{cnt}_j\) does not.
|
Input: 62 3102 3115 1111017 9110101117 34110010100010100101 10000000001 | Output: 3 2 0 8 32 0
|
Master
| 5 | 1,167 | 595 | 154 | 20 |
2,023 |
F
|
2023F
|
F. Hills and Pits
| 3,500 |
data structures; greedy; math; matrices
|
In a desert city with a hilly landscape, the city hall decided to level the road surface by purchasing a dump truck. The road is divided into \(n\) sections, numbered from \(1\) to \(n\) from left to right. The height of the surface in the \(i\)-th section is equal to \(a_i\). If the height of the \(i\)-th section is greater than \(0\), then the dump truck must take sand from the \(i\)-th section of the road, and if the height of the \(i\)-th section is less than \(0\), the dump truck must fill the pit in the \(i\)-th section of the road with sand. It is guaranteed that the initial heights are not equal to \(0\).When the dump truck is in the \(i\)-th section of the road, it can either take away \(x\) units of sand, in which case the height of the surface in the \(i\)-th section will decrease by \(x\), or it can fill in \(x\) units of sand (provided that it currently has at least \(x\) units of sand in its bed), in which case the height of the surface in the \(i\)-th section of the road will increase by \(x\).The dump truck can start its journey from any section of the road. Moving to an adjacent section on the left or right takes \(1\) minute, and the time for loading and unloading sand can be neglected. The dump truck has an infinite capacity and is initially empty.You need to find the minimum time required for the dump truck to level the sand so that the height in each section becomes equal to \(0\). Note that after all movements, the dump truck may still have sand left in its bed. You need to solve this problem independently for the segments numbered from \(l_i\) to \(r_i\). Sand outside the segment cannot be used.
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(1 \le n, q \le 3 \cdot 10^5\)) β the number of sections and the number of queries.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^9 \le a_i \le 10^9\), \(a_i \neq 0\)) β the initial height in each section.The \(i\)-th of the following \(q\) lines contains two integers \(l_i\) and \(r_i\) (\(1 \le l_i \le r_i \le n\)) β the boundaries of the segment of sections for which the minimum time needs to be determined.It is guaranteed that the sum of \(n\) over all test cases and the sum of \(q\) over all test cases do not exceed \(3 \cdot 10^5\).
|
For each query, output the minimum time required to level the sand in the segment \([l_i, r_i]\), or \(-1\) if it is impossible.
|
In the first test case, \(179\) units of sand need to be added to the only section. However, there is nowhere to take it from, so this is impossible.In the second test case: In the first query, the dump truck can start its journey at the second section. It can take \(2\) units of sand, after which the height in the second section will become \(0\). Then the dump truck can move to the third section. It can pour \(1\) unit of sand there, after which the height in the third section will become \(0\). Then the dump truck can move to the fourth section. There it can take \(3\) units of sand, after which the height in the fourth section will become \(0\). In total, the dump truck will spend \(2\) minutes on movements. In the second query, the dump truck can start its journey at the fourth section. It can take \(3\) units of sand, after which the height in the fourth section will become \(0\). Then the dump truck can move to the fifth section. It can pour \(1\) unit of sand there, after which the height in the fifth section will become \(0\). Then the dump truck can move back to the fourth section and then to the third. It can pour \(1\) unit of sand there, after which the height in the third section will become \(0\). Then the dump truck can move to the second section. It can take \(2\) units of sand. Then it can move to the first section. It can pour \(2\) units of sand there, after which the height in the first section will become \(0\). In total, the dump truck will spend \(5\) minutes on movements. In the third query, the dump truck will not be able to make the height in each section equal to \(0\).
|
Input: 51 1-1791 15 3-2 2 -1 3 -12 41 51 37 11 1 1 -4 1 1 11 77 22 -2 2 -2 1 2 -11 72 74 41000000000 1000000000 999999999 -10000000002 43 42 31 3 | Output: -1 2 5 -1 8 6 6 2 -1 1 2
|
Master
| 4 | 1,645 | 844 | 128 | 20 |
1,706 |
B
|
1706B
|
B. Making Towers
| 1,100 |
dp; greedy; math
|
You have a sequence of \(n\) colored blocks. The color of the \(i\)-th block is \(c_i\), an integer between \(1\) and \(n\).You will place the blocks down in sequence on an infinite coordinate grid in the following way. Initially, you place block \(1\) at \((0, 0)\). For \(2 \le i \le n\), if the \((i - 1)\)-th block is placed at position \((x, y)\), then the \(i\)-th block can be placed at one of positions \((x + 1, y)\), \((x - 1, y)\), \((x, y + 1)\) (but not at position \((x, y - 1)\)), as long no previous block was placed at that position. A tower is formed by \(s\) blocks such that they are placed at positions \((x, y), (x, y + 1), \ldots, (x, y + s - 1)\) for some position \((x, y)\) and integer \(s\). The size of the tower is \(s\), the number of blocks in it. A tower of color \(r\) is a tower such that all blocks in it have the color \(r\).For each color \(r\) from \(1\) to \(n\), solve the following problem independently: Find the maximum size of a tower of color \(r\) that you can form by placing down the blocks according to the rules.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains \(n\) integers \(c_1, c_2, \ldots, c_n\) (\(1 \le c_i \le n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output \(n\) integers. The \(r\)-th of them should be the maximum size of an tower of color \(r\) you can form by following the given rules. If you cannot form any tower of color \(r\), the \(r\)-th integer should be \(0\).
|
In the first test case, one of the possible ways to form a tower of color \(1\) and size \(3\) is: place block \(1\) at position \((0, 0)\); place block \(2\) to the right of block \(1\), at position \((1, 0)\); place block \(3\) above block \(2\), at position \((1, 1)\); place block \(4\) to the left of block \(3\), at position \((0, 1)\); place block \(5\) to the left of block \(4\), at position \((-1, 1)\); place block \(6\) above block \(5\), at position \((-1, 2)\); place block \(7\) to the right of block \(6\), at position \((0, 2)\). The blocks at positions \((0, 0)\), \((0, 1)\), and \((0, 2)\) all have color \(1\), forming an tower of size \(3\).In the second test case, note that the following placement is not valid, since you are not allowed to place block \(6\) under block \(5\): It can be shown that it is impossible to form a tower of color \(4\) and size \(3\).
|
Input: 671 2 3 1 2 3 164 2 2 2 4 41155 4 5 3 563 3 3 1 3 381 2 3 4 4 3 2 1 | Output: 3 2 2 0 0 0 0 0 3 0 2 0 0 1 0 0 1 1 1 1 0 4 0 0 0 2 2 2 2 0 0 0 0
|
Easy
| 3 | 1,062 | 384 | 243 | 17 |
153 |
B
|
153B
|
B. Binary notation
| 1,800 |
*special
|
You are given a positive integer n. Output its binary notation.
|
The only line of input data contains an integer n (1 β€ n β€ 106).
|
Output the binary notation of n (without any leading zeros).
|
In the first example 5 = 1 * 22 + 0 * 21 + 1 * 20.
|
Input: 5 | Output: 101
|
Medium
| 1 | 63 | 64 | 60 | 1 |
2,072 |
D
|
2072D
|
D. For Wizards, the Exam Is Easy, but I Couldn't Handle It
| 1,300 |
brute force; greedy; implementation
|
Akito got tired of being a simple locksmith at a bank, so he decided to enroll in the Magical Academy and become the best wizard in the world! However, to enroll, he needed to solve a single problem on the exam, which the ambitious hero could not manage.In the problem, he was given an array \(a\) of length \(n\). He needed to minimize the number of inversions\(^{\text{β}}\) in the array after applying the spell exactly once. The spell was simple; to apply it, Akito had to choose two numbers \(l\) and \(r\) such that \(1 \le l \le r \le n\) and perform a cyclic shift of the subarray from \(l\) to \(r\) one position to the left.More formally, Akito selects the subarray \([l, r]\) and modifies the array as follows: From the original array \([a_1, a_2, \ldots, a_{l - 1}, \mathbf{ a_l }, \mathbf{ a_{l + 1} } , \mathbf{ \ldots }, \mathbf{ a_{r - 1} }, \mathbf{ a_r }, a_{r + 1}, \ldots, a_{n - 1}, a_n]\), he obtains the array \([a_1, a_2, \ldots, a_{l - 1}, \mathbf{ a_{l + 1} }, \mathbf{ a_{l + 2} }, \mathbf{ \ldots }, \mathbf{ a_{r - 1} }, \mathbf{ a_{r} }, \mathbf{ a_{l} }, a_{r + 1}, \ldots, a_{n - 1}, a_{n}]\). Akito is eager to start his studies, but he still hasn't passed the exam. Help him enroll and solve the problem!\(^{\text{β}}\)An inversion in an array \(b\) of length \(m\) is defined as a pair of indices \((i, j)\) such that \(1 \le i < j \le m\) and \(b_i > b_j\). For example, in the array \(b = [3, 1, 4, 1, 5]\), the inversions are the pairs of indices \((1, 2)\), \((1, 4)\), \((3, 4)\).
|
The first line of input contains a number \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.In the first line of each test case, there is a number \(n\) (\(1 \le n \le 2000\)) β the length of the array \(a\).In the second line of each test case, there are \(n\) numbers \(a_i\) (\(1 \le a_i \le 2000\)) β the elements of the array \(a\).It is guaranteed that the sum of \(n^2\) across all test cases does not exceed \(4 \cdot 10^6\).
|
For each test case, output two numbers \(l\) and \(r\) (\(1 \le l \le r \le n\)) β the boundaries of the subarray that should be chosen so that after applying the spell, the number of inversions in the array is minimized.If there are multiple suitable pairs of boundaries, you may output any of them.
|
In the first example, the array \([1, 4, 3, 2, 5, 3, 3]\) will turn into the array \([1, 3, 2, 5, 3, 3, 4]\). The inversions in it are \((2, 3)\), \((4, 5)\), \((4, 6)\) and \((4, 7)\). It can be shown that it is not possible to achieve fewer than \(4\) inversions.In the second example, the array \([1, 4, 3, 2, 5, 3]\) will turn into \([1, 3, 2, 4, 5, 3]\). The inversions in it are \((2, 3)\), \((4, 6)\), and \((5, 6)\). The pair \(l = 2\) and \(r = 6\) also works, then the array will turn into \([1, 3, 2, 5, 3, 4]\), which also has \(3\) inversions β \((2, 3)\), \((4, 5)\), and \((4, 6)\). It can be shown that it is not possible to achieve fewer than \(3\) inversions.In the fourth example, choosing \(l = 4\) and \(r = 6\) transforms the array into \([1, 1, 1, 1, 1, 5, 5, 6, 7, 8]\). It is sorted, and therefore, there are no inversions.In the last example, the array is initially sorted, so any operation on a segment of length at least \(2\) will only increase the number of inversions.
|
Input: 971 4 3 2 5 3 361 4 3 2 5 387 6 5 8 4 3 2 1101 1 1 5 1 1 5 6 7 821337 6942 1 2 13998 244 35331 2 191 1 2 3 5 8 13 21 34 | Output: 2 7 2 4 1 8 4 6 1 2 1 4 1 3 2 3 5 5
|
Easy
| 3 | 1,520 | 439 | 300 | 20 |
2,001 |
B
|
2001B
|
B. Generate Permutation
| 800 |
constructive algorithms
|
There is an integer sequence \(a\) of length \(n\), where each element is initially \(-1\).Misuki has two typewriters where the first one writes letters from left to right, with a pointer initially pointing to \(1\), and another writes letters from right to left with a pointer initially pointing to \(n\).Misuki would choose one of the typewriters and use it to perform the following operations until \(a\) becomes a permutation of \([1, 2, \ldots, n]\) write number: write the minimum positive integer that isn't present in the array \(a\) to the element \(a_i\), \(i\) is the position where the pointer points at. Such operation can be performed only when \(a_i = -1\). carriage return: return the pointer to its initial position (i.e. \(1\) for the first typewriter, \(n\) for the second) move pointer: move the pointer to the next position, let \(i\) be the position the pointer points at before this operation, if Misuki is using the first typewriter, \(i := i + 1\) would happen, and \(i := i - 1\) otherwise. Such operation can be performed only if after the operation, \(1 \le i \le n\) holds. Your task is to construct any permutation \(p\) of length \(n\), such that the minimum number of carriage return operations needed to make \(a = p\) is the same no matter which typewriter Misuki is using.
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 500\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β the length of the permutation.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a line of \(n\) integers, representing the permutation \(p\) of length \(n\) such that the minimum number of carriage return operations needed to make \(a = p\) is the same no matter which typewriter Misuki is using, or \(-1\) if it is impossible to do so.If there are multiple valid permutations, you can output any of them.
|
In the first testcase, it's possible to make \(a = p = [1]\) using \(0\) carriage return operations.In the second testcase, it is possible to make \(a = p = [1, 2]\) with the minimal number of carriage returns as follows:If Misuki is using the first typewriter: Write number: write \(1\) to \(a_1\), \(a\) becomes \([1, -1]\) Move pointer: move the pointer to the next position. (i.e. \(2\)) Write number: write \(2\) to \(a_2\), \(a\) becomes \([1, 2]\) If Misuki is using the second typewriter: Move pointer: move the pointer to the next position. (i.e. \(1\)) Write number: write \(1\) to \(a_1\), \(a\) becomes \([1, -1]\) Carriage return: return the pointer to \(2\). Write number: write \(2\) to \(a_2\), \(a\) becomes \([1, 2]\) It can be proven that the minimum number of carriage returns needed to transform \(a\) into \(p\) when using the first typewriter is \(0\) and it is \(1\) when using the second one, so this permutation is not valid.Similarly, \(p = [2, 1]\) is also not valid, so there is no solution for \(n = 2\).In the third testcase, it is possibile to make \(a = p = [3, 1, 2]\) with \(1\) carriage return with both the first and the second typewriter. It can be proven that, for both typewriters, it is impossible to write \(p\) with \(0\) carriage returns.With the first typewriter it is possible to: Move pointer: move the pointer to the next position. (i.e. \(2\)) Write number: write \(1\) to \(a_2\), \(a\) becomes \([-1, 1, -1]\) Move pointer: move the pointer to the next position. (i.e. \(3\)) Write number: write \(2\) to \(a_3\), \(a\) becomes \([-1, 1, 2]\) Carriage return: return the pointer to \(1\). Write number: write \(3\) to \(a_1\), \(a\) becomes \([3,1,2]\) With the second typewriter it is possible to: Move pointer: move the pointer to the next position. (i.e. \(2\)) Write number: write \(1\) to \(a_2\), \(a\) becomes \([-1, 1, -1]\) Carriage return: return the pointer to \(3\). Write number: write \(2\) to \(a_3\), \(a\) becomes \([-1, 1, 2]\) Move pointer: move the pointer to the next position. (i.e. \(2\)) Move pointer: move the pointer to the next position. (i.e. \(1\)) Write number: write \(3\) to \(a_1\), \(a\) becomes \([3, 1, 2]\)
|
Input: 3 1 2 3 | Output: 1 -1 3 1 2
|
Beginner
| 1 | 1,307 | 408 | 352 | 20 |
1,627 |
A
|
1627A
|
A. Not Shading
| 800 |
constructive algorithms; implementation
|
There is a grid with \(n\) rows and \(m\) columns. Some cells are colored black, and the rest of the cells are colored white.In one operation, you can select some black cell and do exactly one of the following: color all cells in its row black, or color all cells in its column black. You are given two integers \(r\) and \(c\). Find the minimum number of operations required to make the cell in row \(r\) and column \(c\) black, or determine that it is impossible.
|
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains four integers \(n\), \(m\), \(r\), and \(c\) (\(1 \leq n, m \leq 50\); \(1 \leq r \leq n\); \(1 \leq c \leq m\)) β the number of rows and the number of columns in the grid, and the row and column of the cell you need to turn black, respectively.Then \(n\) lines follow, each containing \(m\) characters. Each of these characters is either 'B' or 'W' β a black and a white cell, respectively.
|
For each test case, if it is impossible to make the cell in row \(r\) and column \(c\) black, output \(-1\). Otherwise, output a single integer β the minimum number of operations required to make the cell in row \(r\) and column \(c\) black.
|
The first test case is pictured below. We can take the black cell in row \(1\) and column \(2\), and make all cells in its row black. Therefore, the cell in row \(1\) and column \(4\) will become black. In the second test case, the cell in row \(2\) and column \(1\) is already black.In the third test case, it is impossible to make the cell in row \(2\) and column \(2\) black.The fourth test case is pictured below. We can take the black cell in row \(2\) and column \(2\) and make its column black. Then, we can take the black cell in row \(1\) and column \(2\) and make its row black. Therefore, the cell in row \(1\) and column \(1\) will become black.
|
Input: 93 5 1 4WBWWWBBBWBWWBBB4 3 2 1BWWBBWWBBWWB2 3 2 2WWWWWW2 2 1 1WWWB5 9 5 9WWWWWWWWWWBWBWBBBWWBBBWWBWWWBWBWBBBWWWWWWWWWW1 1 1 1B1 1 1 1W1 2 1 1WB2 1 1 1WB | Output: 1 0 -1 2 2 0 -1 1 1
|
Beginner
| 2 | 465 | 611 | 241 | 16 |
11 |
C
|
11C
|
C. How Many Squares?
| 2,200 |
implementation
|
You are given a 0-1 rectangular matrix. What is the number of squares in it? A square is a solid square frame (border) with linewidth equal to 1. A square should be at least 2 Γ 2. We are only interested in two types of squares: squares with each side parallel to a side of the matrix; squares with each side parallel to a diagonal of the matrix. For example the following matrix contains only one square of the first type: 0000000 0111100 0100100 0100100 0111100The following matrix contains only one square of the second type:00000000010000010100000100000000000Regardless of type, a square must contain at least one 1 and can't touch (by side or corner) any foreign 1. Of course, the lengths of the sides of each square should be equal.How many squares are in the given matrix?
|
The first line contains integer t (1 β€ t β€ 10000), where t is the number of test cases in the input. Then test cases follow. Each case starts with a line containing integers n and m (2 β€ n, m β€ 250), where n is the number of rows and m is the number of columns. The following n lines contain m characters each (0 or 1).The total number of characters in all test cases doesn't exceed 106 for any input file.
|
You should output exactly t lines, with the answer to the i-th test case on the i-th line.
|
Input: 28 8000100010010100001000100100000100100010000101000110100111100001110 101111111000100000100010110010001011001010100000110110010010101010101000100100100010000010001111111000 | Output: 12
|
Hard
| 1 | 779 | 406 | 90 | 0 |
|
820 |
B
|
820B
|
B. Mister B and Angle in Polygon
| 1,300 |
constructive algorithms; geometry; math
|
On one quiet day all of sudden Mister B decided to draw angle a on his field. Aliens have already visited his field and left many different geometric figures on it. One of the figures is regular convex n-gon (regular convex polygon with n sides).That's why Mister B decided to use this polygon. Now Mister B must find three distinct vertices v1, v2, v3 such that the angle (where v2 is the vertex of the angle, and v1 and v3 lie on its sides) is as close as possible to a. In other words, the value should be minimum possible.If there are many optimal solutions, Mister B should be satisfied with any of them.
|
First and only line contains two space-separated integers n and a (3 β€ n β€ 105, 1 β€ a β€ 180) β the number of vertices in the polygon and the needed angle, in degrees.
|
Print three space-separated integers: the vertices v1, v2, v3, which form . If there are multiple optimal solutions, print any of them. The vertices are numbered from 1 to n in clockwise order.
|
In first sample test vertices of regular triangle can create only angle of 60 degrees, that's why every possible angle is correct.Vertices of square can create 45 or 90 degrees angles only. That's why in second sample test the angle of 45 degrees was chosen, since |45 - 67| < |90 - 67|. Other correct answers are: ""3 1 2"", ""3 2 4"", ""4 2 3"", ""4 3 1"", ""1 3 4"", ""1 4 2"", ""2 4 1"", ""4 1 3"", ""3 1 4"", ""3 4 2"", ""2 4 3"", ""2 3 1"", ""1 3 2"", ""1 2 4"", ""4 2 1"".In third sample test, on the contrary, the angle of 90 degrees was chosen, since |90 - 68| < |45 - 68|. Other correct answers are: ""2 1 4"", ""3 2 1"", ""1 2 3"", ""4 3 2"", ""2 3 4"", ""1 4 3"", ""3 4 1"".
|
Input: 3 15 | Output: 1 2 3
|
Easy
| 3 | 609 | 166 | 193 | 8 |
1,969 |
B
|
1969B
|
B. Shifts and Sorting
| 1,000 |
constructive algorithms; greedy
|
Let's define a cyclic shift of some string \(s\) as a transformation from \(s_1 s_2 \dots s_{n-1} s_{n}\) into \(s_{n} s_1 s_2 \dots s_{n-1}\). In other words, you take one last character \(s_n\) and place it before the first character while moving all other characters to the right.You are given a binary string \(s\) (a string consisting of only 0-s and/or 1-s).In one operation, you can choose any substring \(s_l s_{l+1} \dots s_r\) (\(1 \le l < r \le |s|\)) and cyclically shift it. The cost of such operation is equal to \(r - l + 1\) (or the length of the chosen substring).You can perform the given operation any number of times. What is the minimum total cost to make \(s\) sorted in non-descending order?
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first and only line of each test case contains a binary string \(s\) (\(2 \le |s| \le 2 \cdot 10^5\); \(s_i \in\) {0, 1}) β the string you need to sort.Additional constraint on the input: the sum of lengths of strings over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print the single integer β the minimum total cost to make string sorted using operation above any number of times.
|
In the first test case, you can choose the whole string and perform a cyclic shift: 10 \(\rightarrow\) 01. The length of the substring is \(2\), so the cost is \(2\).In the second test case, the string is already sorted, so you don't need to perform any operations.In the third test case, one of the optimal strategies is the next: choose substring \([1, 3]\): 11000 \(\rightarrow\) 01100; choose substring \([2, 4]\): 01100 \(\rightarrow\) 00110; choose substring \([3, 5]\): 00110 \(\rightarrow\) 00011. The total cost is \(3 + 3 + 3 = 9\).
|
Input: 51000001100010101101101001 | Output: 2 0 9 5 11
|
Beginner
| 2 | 714 | 371 | 134 | 19 |
1,198 |
C
|
1198C
|
C. Matching vs Independent Set
| 2,000 |
constructive algorithms; graphs; greedy; sortings
|
You are given a graph with \(3 \cdot n\) vertices and \(m\) edges. You are to find a matching of \(n\) edges, or an independent set of \(n\) vertices.A set of edges is called a matching if no two edges share an endpoint.A set of vertices is called an independent set if no two vertices are connected with an edge.
|
The first line contains a single integer \(T \ge 1\) β the number of graphs you need to process. The description of \(T\) graphs follows.The first line of description of a single graph contains two integers \(n\) and \(m\), where \(3 \cdot n\) is the number of vertices, and \(m\) is the number of edges in the graph (\(1 \leq n \leq 10^{5}\), \(0 \leq m \leq 5 \cdot 10^{5}\)).Each of the next \(m\) lines contains two integers \(v_i\) and \(u_i\) (\(1 \leq v_i, u_i \leq 3 \cdot n\)), meaning that there is an edge between vertices \(v_i\) and \(u_i\).It is guaranteed that there are no self-loops and no multiple edges in the graph.It is guaranteed that the sum of all \(n\) over all graphs in a single test does not exceed \(10^{5}\), and the sum of all \(m\) over all graphs in a single test does not exceed \(5 \cdot 10^{5}\).
|
Print your answer for each of the \(T\) graphs. Output your answer for a single graph in the following format.If you found a matching of size \(n\), on the first line print ""Matching"" (without quotes), and on the second line print \(n\) integers β the indices of the edges in the matching. The edges are numbered from \(1\) to \(m\) in the input order.If you found an independent set of size \(n\), on the first line print ""IndSet"" (without quotes), and on the second line print \(n\) integers β the indices of the vertices in the independent set.If there is no matching and no independent set of the specified size, print ""Impossible"" (without quotes).You can print edges and vertices in any order.If there are several solutions, print any. In particular, if there are both a matching of size \(n\), and an independent set of size \(n\), then you should print exactly one of such matchings or exactly one of such independent sets.
|
The first two graphs are same, and there are both a matching of size 1 and an independent set of size 1. Any of these matchings and independent sets is a correct answer.The third graph does not have a matching of size 2, however, there is an independent set of size 2. Moreover, there is an independent set of size 5: 2 3 4 5 6. However such answer is not correct, because you are asked to find an independent set (or matching) of size exactly \(n\).The fourth graph does not have an independent set of size 2, but there is a matching of size 2.
|
Input: 4 1 2 1 3 1 2 1 2 1 3 1 2 2 5 1 2 3 1 1 4 5 1 1 6 2 15 1 2 1 3 1 4 1 5 1 6 2 3 2 4 2 5 2 6 3 4 3 5 3 6 4 5 4 6 5 6 | Output: Matching 2 IndSet 1 IndSet 2 4 Matching 1 15
|
Hard
| 4 | 313 | 832 | 937 | 11 |
2,032 |
C
|
2032C
|
C. Trinity
| 1,400 |
binary search; math; sortings; two pointers
|
You are given an array \(a\) of \(n\) elements \(a_1, a_2, \ldots, a_n\).You can perform the following operation any number (possibly \(0\)) of times: Choose two integers \(i\) and \(j\), where \(1 \le i, j \le n\), and assign \(a_i := a_j\). Find the minimum number of operations required to make the array \(a\) satisfy the condition: For every pairwise distinct triplet of indices \((x, y, z)\) (\(1 \le x, y, z \le n\), \(x \ne y\), \(y \ne z\), \(x \ne z\)), there exists a non-degenerate triangle with side lengths \(a_x\), \(a_y\) and \(a_z\), i.e. \(a_x + a_y > a_z\), \(a_y + a_z > a_x\) and \(a_z + a_x > a_y\).
|
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of elements in the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^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 minimum number of operations required.
|
In the first test case, one of the possible series of operations would be: Assign \(a_1 := a_4 = 4\). The array will become \([4, 2, 3, 4, 5, 6, 7]\). Assign \(a_2 := a_5 = 5\). The array will become \([4, 5, 3, 4, 5, 6, 7]\). Assign \(a_7 := a_1 = 4\). The array will become \([4, 5, 3, 4, 5, 6, 4]\). It can be proven that any triplet of elements with pairwise distinct indices in the final array forms a non-degenerate triangle, and there is no possible answer using less than \(3\) operations.In the second test case, we can assign \(a_1 := a_2 = 3\) to make the array \(a = [3, 3, 2]\).In the third test case, since \(3\), \(4\) and \(5\) are valid side lengths of a triangle, we don't need to perform any operation to the array.
|
Input: 471 2 3 4 5 6 731 3 234 5 3159 3 8 1 6 5 3 8 2 1 4 2 9 4 7 | Output: 3 1 0 8
|
Easy
| 4 | 621 | 558 | 88 | 20 |
1,217 |
D
|
1217D
|
D. Coloring Edges
| 2,100 |
constructive algorithms; dfs and similar; graphs
|
You are given a directed graph with \(n\) vertices and \(m\) directed edges without self-loops or multiple edges.Let's denote the \(k\)-coloring of a digraph as following: you color each edge in one of \(k\) colors. The \(k\)-coloring is good if and only if there no cycle formed by edges of same color.Find a good \(k\)-coloring of given digraph with minimum possible \(k\).
|
The first line contains two integers \(n\) and \(m\) (\(2 \le n \le 5000\), \(1 \le m \le 5000\)) β the number of vertices and edges in the digraph, respectively.Next \(m\) lines contain description of edges β one per line. Each edge is a pair of integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \ne v\)) β there is directed edge from \(u\) to \(v\) in the graph.It is guaranteed that each ordered pair \((u, v)\) appears in the list of edges at most once.
|
In the first line print single integer \(k\) β the number of used colors in a good \(k\)-coloring of given graph.In the second line print \(m\) integers \(c_1, c_2, \dots, c_m\) (\(1 \le c_i \le k\)), where \(c_i\) is a color of the \(i\)-th edge (in order as they are given in the input).If there are multiple answers print any of them (you still have to minimize \(k\)).
|
Input: 4 5 1 2 1 3 3 4 2 4 1 4 | Output: 1 1 1 1 1 1
|
Hard
| 3 | 375 | 459 | 372 | 12 |
|
1,675 |
F
|
1675F
|
F. Vlad and Unfinished Business
| 1,800 |
dfs and similar; dp; greedy; trees
|
Vlad and Nastya live in a city consisting of \(n\) houses and \(n-1\) road. From each house, you can get to the other by moving only along the roads. That is, the city is a tree.Vlad lives in a house with index \(x\), and Nastya lives in a house with index \(y\). Vlad decided to visit Nastya. However, he remembered that he had postponed for later \(k\) things that he has to do before coming to Nastya. To do the \(i\)-th thing, he needs to come to the \(a_i\)-th house, things can be done in any order. In \(1\) minute, he can walk from one house to another if they are connected by a road.Vlad does not really like walking, so he is interested what is the minimum number of minutes he has to spend on the road to do all things and then come to Nastya. Houses \(a_1, a_2, \dots, a_k\) he can visit in any order. He can visit any house multiple times (if he wants).
|
The first line of input contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of input test cases. There is an empty line before each test case.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 houses and things, respectively.The second line of each test case contains two integers \(x\) and \(y\) (\(1 \le x, y \le n\)) β indices of the houses where Vlad and Nastya live, respectively.The third line of each test case contains \(k\) integers \(a_1, a_2, \dots, a_k\) (\(1 \le a_i \le n\)) β indices of houses Vlad need to come to do things.The following \(n-1\) lines contain description of city, each line contains two integers \(v_j\) and \(u_j\) (\(1 \le u_j, v_j \le n\)) β indices of houses connected by road \(j\).It is guaranteed that the sum of \(n\) for all cases does not exceed \(2\cdot10^5\).
|
Output \(t\) lines, each of which contains the answer to the corresponding test case of input. As an answer output single integer β the minimum number of minutes Vlad needs on the road to do all the things and come to Nastya.
|
Tree and best path for the first test case: \(1 \rightarrow 2 \rightarrow 1 \rightarrow 3\) Tree and best path for the second test case: \(3 \rightarrow 1 \rightarrow 3 \rightarrow 5 \rightarrow 2 \rightarrow 5 \rightarrow 6 \rightarrow 5\) Tree and best path for the third test case: \(3 \rightarrow 5 \rightarrow 2\)
|
Input: 33 11 321 31 26 43 51 6 2 11 33 43 55 65 26 23 25 31 33 43 55 65 2 | Output: 3 7 2
|
Medium
| 4 | 867 | 886 | 225 | 16 |
1,223 |
G
|
1223G
|
G. Wooden Raft
| 3,200 |
binary search; math; number theory
|
Suppose you are stuck on a desert island. The only way to save yourself is to craft a wooden raft and go to the sea. Fortunately, you have a hand-made saw and a forest nearby. Moreover, you've already cut several trees and prepared it to the point that now you have \(n\) logs and the \(i\)-th log has length \(a_i\).The wooden raft you'd like to build has the following structure: \(2\) logs of length \(x\) and \(x\) logs of length \(y\). Such raft would have the area equal to \(x \cdot y\). Both \(x\) and \(y\) must be integers since it's the only way you can measure the lengths while being on a desert island. And both \(x\) and \(y\) must be at least \(2\) since the raft that is one log wide is unstable.You can cut logs in pieces but you can't merge two logs in one. What is the maximum area of the raft you can craft?
|
The first line contains the only integer \(n\) (\(1 \le n \le 5 \cdot 10^5\)) β the number of logs you have.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(2 \le a_i \le 5 \cdot 10^5\)) β the corresponding lengths of the logs.It's guaranteed that you can always craft at least \(2 \times 2\) raft.
|
Print the only integer β the maximum area of the raft you can craft.
|
In the first example, you can cut the log of the length \(9\) in \(5\) parts: \(2 + 2 + 2 + 2 + 1\). Now you can build \(2 \times 2\) raft using \(2\) logs of length \(x = 2\) and \(x = 2\) logs of length \(y = 2\).In the second example, you can cut \(a_4 = 18\) into two pieces \(9 + 9\) and \(a_8 = 28\) in three pieces \(10 + 9 + 9\). Now you can make \(10 \times 9\) raft using \(2\) logs of length \(10\) and \(10\) logs of length \(9\).
|
Input: 1 9 | Output: 4
|
Master
| 3 | 828 | 317 | 68 | 12 |
1,533 |
D
|
1533D
|
D. String Searching
| 0 |
*special; hashing
|
You are given an array \(s\) consisting of \(n\) different strings. Each string consists of \(m\) lowercase Latin letters.You have to respond to \(q\) queries. Each query contains a string \(t\) of length \(m+1\). Count the number of indices \(i\), such that the string \(t\) can be obtained from the string \(s_i\), if it is allowed to insert one letter in an arbitrary position.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n \le 10^5\); \(1 \le m \le 10\)) β the number of strings in the array and the length of each string.The following \(n\) lines contain strings \(s_i\). All of the given strings are different.The next line contains a single integer \(q\) (\(1 \le q \le 10^5\)) β the number of queries.The following \(q\) lines contain query strings \(t\) of length \(m + 1\).
|
For each query, print the number of indices \(i\), such that a string from the query can be obtained from the string \(s_i\), if it is allowed to insert one letter in an arbitrary position.
|
Explanation of the first test of the example: the string a can be transformed into aa by inserting one letter; both strings a and c can be transformed into ca by inserting one letter; neither a nor c can be transformed into mm by inserting one letter; c can be transformed into cf by inserting one letter.
|
Input: 2 1 a c 4 aa ca mm cf | Output: 1 2 0 1
|
Beginner
| 2 | 380 | 420 | 189 | 15 |
1,740 |
G
|
1740G
|
G. Dangerous Laser Power
| 3,100 |
constructive algorithms; dsu; sortings
|
Pak Chanek has an \(n \times m\) grid of portals. The portal on the \(i\)-th row and \(j\)-th column is denoted as portal \((i,j)\). The portals \((1,1)\) and \((n,m)\) are on the north-west and south-east corner of the grid respectively.The portal \((i,j)\) has two settings: Type \(t_{i,j}\), which is either \(0\) or \(1\). Strength \(s_{i,j}\), which is an integer between \(1\) and \(10^9\) inclusive. Each portal has \(4\) faces labelled with integers \(0,1,2,3\), which correspond to the north, east, south, and west direction respectively. When a laser enters face \(k\) of portal \((i, j)\) with speed \(x_\text{in}\), it leaves the portal going out of face \((k+2+t_{i,j}) \bmod 4\) with speed \(x_\text{out} = \max(x_\text{in},s_{i,j})\). The portal also has to consume \(x_\text{out} - x_\text{in}\) units of energy. Pak Chanek is very bored today. He will shoot \(4nm\) lasers with an initial speed of \(1\), one into each face of each portal. Each laser will travel throughout this grid of portals until it moves outside the grid or it has passed through \(10^{100}\) portals.At the end, Pak Chanek thinks that a portal is good if and only if the total energy consumed by that portal modulo \(2\) is equal to its type. Given the strength settings of all portals, find a way to assign the type settings of each portal such that the number of good portals is maximised.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 1000\)) β the number of rows and columns in the grid.The \(i\)-th of the next \(n\) lines contains \(m\) integers, with the \(j\)-th integer being \(s_{i,j}\) (\(1 \leq s_{i,j} \leq 10^9\)) β the strength of portal \((i, j)\).
|
Print \(n\) lines with each line containing a string of length \(m\) consisting of characters \(0\) or \(1\) representing the type settings. The \(j\)-th character in the \(i\)-th string is the type setting of portal \((i, j)\).If there are multiple solutions, you can output any of them.
|
In the first example, let's consider the laser Pak Chanek shoots into face \(1\) of portal \((2, 2)\). The laser travels as follows: The laser enters face \(1\) of portal \((2, 2)\) with speed \(1\). It leaves the portal going out of face \(3\) with speed \(5\). Portal \((2, 2)\) consumes \(4\) units of energy. The laser enters face \(1\) of portal \((2, 1)\) with speed \(5\). It leaves the portal going out of face \(0\) with speed \(6\). Portal \((2, 1)\) consumes \(1\) units of energy. The laser enters face \(2\) of portal \((1, 1)\) with speed \(6\). It leaves the portal going out of face \(1\) with speed \(8\). Portal \((1, 1)\) consumes \(2\) units of energy. The laser enters face \(3\) of portal \((1, 2)\) with speed \(8\). It leaves the portal going out of face \(2\) with speed \(8\). Portal \((1, 2)\) consumes \(0\) units of energy. The laser enters face \(0\) of portal \((2, 2)\) with speed \(8\). It leaves the portal going out of face \(2\) with speed \(8\). Portal \((2, 2)\) consumes \(0\) units of energy. The illustration of the travel of the laser above is as follows. As an example, consider portal \((2, 3)\). We can calculate that the total energy consumed by that portal in the end will be \(32\). Since \(32 \bmod 2 = 0\) and \(t_{2,3} = 0\), then it is a good portal.
|
Input: 2 3 8 8 2 6 5 7 | Output: 110 100
|
Master
| 3 | 1,381 | 297 | 288 | 17 |
1,408 |
B
|
1408B
|
B. Arrays Sum
| 1,400 |
constructive algorithms; greedy; math
|
You are given a non-decreasing array of non-negative integers \(a_1, a_2, \ldots, a_n\). Also you are given a positive integer \(k\).You want to find \(m\) non-decreasing arrays of non-negative integers \(b_1, b_2, \ldots, b_m\), such that: The size of \(b_i\) is equal to \(n\) for all \(1 \leq i \leq m\). For all \(1 \leq j \leq n\), \(a_j = b_{1, j} + b_{2, j} + \ldots + b_{m, j}\). In the other word, array \(a\) is the sum of arrays \(b_i\). The number of different elements in the array \(b_i\) is at most \(k\) for all \(1 \leq i \leq m\). Find the minimum possible value of \(m\), or report that there is no possible \(m\).
|
The first line contains one integer \(t\) (\(1 \leq t \leq 100\)): the number of test cases.The first line of each test case contains two integers \(n\), \(k\) (\(1 \leq n \leq 100\), \(1 \leq k \leq n\)).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_1 \leq a_2 \leq \ldots \leq a_n \leq 100\), \(a_n > 0\)).
|
For each test case print a single integer: the minimum possible value of \(m\). If there is no such \(m\), print \(-1\).
|
In the first test case, there is no possible \(m\), because all elements of all arrays should be equal to \(0\). But in this case, it is impossible to get \(a_4 = 1\) as the sum of zeros.In the second test case, we can take \(b_1 = [3, 3, 3]\). \(1\) is the smallest possible value of \(m\).In the third test case, we can take \(b_1 = [0, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2]\) and \(b_2 = [0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2]\). It's easy to see, that \(a_i = b_{1, i} + b_{2, i}\) for all \(i\) and the number of different elements in \(b_1\) and in \(b_2\) is equal to \(3\) (so it is at most \(3\)). It can be proven that \(2\) is the smallest possible value of \(m\).
|
Input: 6 4 1 0 0 0 1 3 1 3 3 3 11 3 0 1 2 2 3 3 3 4 4 4 4 5 3 1 2 3 4 5 9 4 2 2 3 5 7 11 13 13 17 10 7 0 1 1 2 3 3 4 5 5 6 | Output: -1 1 2 2 2 1
|
Easy
| 3 | 633 | 340 | 120 | 14 |
29 |
C
|
29C
|
C. Mail Stamps
| 1,700 |
data structures; dfs and similar; graphs; implementation
|
One day Bob got a letter in an envelope. Bob knows that when Berland's post officers send a letter directly from city Β«AΒ» to city Β«BΒ», they stamp it with Β«A BΒ», or Β«B AΒ». Unfortunately, often it is impossible to send a letter directly from the city of the sender to the city of the receiver, that's why the letter is sent via some intermediate cities. Post officers never send a letter in such a way that the route of this letter contains some city more than once. Bob is sure that the post officers stamp the letters accurately.There are n stamps on the envelope of Bob's letter. He understands that the possible routes of this letter are only two. But the stamps are numerous, and Bob can't determine himself none of these routes. That's why he asks you to help him. Find one of the possible routes of the letter.
|
The first line contains integer n (1 β€ n β€ 105) β amount of mail stamps on the envelope. Then there follow n lines with two integers each β description of the stamps. Each stamp is described with indexes of the cities between which a letter is sent. The indexes of cities are integers from 1 to 109. Indexes of all the cities are different. Every time the letter is sent from one city to another, exactly one stamp is put on the envelope. It is guaranteed that the given stamps correspond to some valid route from some city to some other city.
|
Output n + 1 numbers β indexes of cities in one of the two possible routes of the letter.
|
Input: 21 100100 2 | Output: 2 100 1
|
Medium
| 4 | 815 | 543 | 89 | 0 |
|
717 |
A
|
717A
|
A. Festival Organization
| 2,900 |
combinatorics; math; number theory
|
The Prodiggers are quite a cool band and for this reason, they have been the surprise guest at the ENTER festival for the past 80 years. At the beginning of their careers, they werenβt so successful, so they had to spend time digging channels to earn money; hence the name. Anyway, they like to tour a lot and have surprising amounts of energy to do extremely long tours. However, they hate spending two consecutive days without having a concert, so they would like to avoid it.A tour is defined by a sequence of concerts and days-off. You need to count in how many ways The Prodiggers can select k different tours of the same length between l and r.For example if k = 2, l = 1 and r = 2, if we define concert day as {1} and day-off as {0}, here are all possible tours: {0}, {1}, {00}, {01}, {10}, {11}. But tour 00 can not be selected because it has 2 days-off in a row. Now, we need to count in how many ways we can select k = 2 tours of the same length in range [1;2]. Here they are: {0,1}; {01,10}; {01,11}; {10,11}.Since their schedule is quite busy, they want you to tell them in how many ways can do that, modulo 1 000 000 007 (109 + 7).
|
The first line of the input contains three integers k, l and r (1 β€ k β€ 200, 1 β€ l β€ r β€ 1018).
|
Output a single number: the number of ways to select k different tours of the same length, modulo 1 000 000 007.
|
Input: 1 1 2 | Output: 5
|
Master
| 3 | 1,144 | 95 | 112 | 7 |
|
991 |
A
|
991A
|
A. If at first you don't succeed...
| 1,000 |
implementation
|
Each student eagerly awaits the day he would pass the exams successfully. Thus, Vasya was ready to celebrate, but, alas, he didn't pass it. However, many of Vasya's fellow students from the same group were more successful and celebrated after the exam.Some of them celebrated in the BugDonalds restaurant, some of them β in the BeaverKing restaurant, the most successful ones were fast enough to celebrate in both of restaurants. Students which didn't pass the exam didn't celebrate in any of those restaurants and elected to stay home to prepare for their reexamination. However, this quickly bored Vasya and he started checking celebration photos on the Kilogramm. He found out that, in total, BugDonalds was visited by \(A\) students, BeaverKing β by \(B\) students and \(C\) students visited both restaurants. Vasya also knows that there are \(N\) students in his group.Based on this info, Vasya wants to determine either if his data contradicts itself or, if it doesn't, how many students in his group didn't pass the exam. Can you help him so he won't waste his valuable preparation time?
|
The first line contains four integers β \(A\), \(B\), \(C\) and \(N\) (\(0 \leq A, B, C, N \leq 100\)).
|
If a distribution of \(N\) students exists in which \(A\) students visited BugDonalds, \(B\) β BeaverKing, \(C\) β both of the restaurants and at least one student is left home (it is known that Vasya didn't pass the exam and stayed at home), output one integer β amount of students (including Vasya) who did not pass the exam. If such a distribution does not exist and Vasya made a mistake while determining the numbers \(A\), \(B\), \(C\) or \(N\) (as in samples 2 and 3), output \(-1\).
|
The first sample describes following situation: \(5\) only visited BugDonalds, \(5\) students only visited BeaverKing, \(5\) visited both of them and \(5\) students (including Vasya) didn't pass the exam.In the second sample \(2\) students only visited BugDonalds and \(2\) only visited BeaverKing, but that means all \(4\) students in group passed the exam which contradicts the fact that Vasya didn't pass meaning that this situation is impossible.The third sample describes a situation where \(2\) students visited BugDonalds but the group has only \(1\) which makes it clearly impossible.
|
Input: 10 10 5 20 | Output: 5
|
Beginner
| 1 | 1,094 | 103 | 489 | 9 |
1,177 |
B
|
1177B
|
B. Digits Sequence (Hard Edition)
| 1,800 |
binary search; divide and conquer; implementation
|
Let's write all the positive integer numbers one after another from \(1\) without any delimiters (i.e. as a single string). It will be the infinite sequence starting with 123456789101112131415161718192021222324252627282930313233343536...Your task is to print the \(k\)-th digit of this sequence.
|
The first and only line contains integer \(k\) (\(1 \le k \le 10^{12}\)) β the position to process (\(1\)-based index).
|
Print the \(k\)-th digit of the resulting infinite sequence.
|
Input: 7 | Output: 7
|
Medium
| 3 | 295 | 119 | 60 | 11 |
|
802 |
A
|
802A
|
A. Heidi and Library (easy)
| 1,800 |
greedy
|
Your search for Heidi is over β you finally found her at a library, dressed up as a human. In fact, she has spent so much time there that she now runs the place! Her job is to buy books and keep them at the library so that people can borrow and read them. There are n different books, numbered 1 through n.We will look at the library's operation during n consecutive days. Heidi knows in advance that on the i-th day (1 β€ i β€ n) precisely one person will come to the library, request to borrow the book ai, read it in a few hours, and return the book later on the same day.Heidi desperately wants to please all her guests, so she will make sure to always have the book ai available in the library on the i-th day. During the night before the i-th day, she has the option of going to the bookstore (which operates at nights to avoid competition with the library) and buying any book for the price of 1 CHF. Of course, if she already has a book at the library, she does not need to buy it again. Initially, the library contains no books.There is a problem, though. The capacity of the library is k β this means that at any time, there can be at most k books at the library. If buying a new book would cause Heidi to have more than k books, she must first get rid of some book that she already has, in order to make room for the new book. If she later needs a book that she got rid of, she will need to buy that book again.You are given k and the sequence of requests for books a1, a2, ..., an. What is the minimum cost (in CHF) of buying new books to satisfy all the requests?
|
The first line of input will contain two integers n and k (1 β€ n, k β€ 80). The second line will contain n integers a1, a2, ..., an (1 β€ ai β€ n) β the sequence of book requests.
|
On a single line print the minimum cost of buying books at the store so as to satisfy all requests.
|
In the first test case, Heidi is able to keep all books forever. Therefore, she only needs to buy the book 1 before the first day and the book 2 before the second day.In the second test case, she can only keep one book at a time. Therefore she will need to buy new books on the first, second and fourth day.In the third test case, before buying book 3 on the third day, she must decide which of the books 1 and 2 she should get rid of. Of course, she should keep the book 1, which will be requested on the fourth day.
|
Input: 4 801 2 2 1 | Output: 2
|
Medium
| 1 | 1,574 | 176 | 99 | 8 |
620 |
B
|
620B
|
B. Grandfather Dovletβs calculator
| 1,000 |
implementation
|
Once Max found an electronic calculator from his grandfather Dovlet's chest. He noticed that the numbers were written with seven-segment indicators (https://en.wikipedia.org/wiki/Seven-segment_display). Max starts to type all the values from a to b. After typing each number Max resets the calculator. Find the total number of segments printed on the calculator.For example if a = 1 and b = 3 then at first the calculator will print 2 segments, then β 5 segments and at last it will print 5 segments. So the total number of printed segments is 12.
|
The only line contains two integers a, b (1 β€ a β€ b β€ 106) β the first and the last number typed by Max.
|
Print the only integer a β the total number of printed segments.
|
Input: 1 3 | Output: 12
|
Beginner
| 1 | 547 | 104 | 64 | 6 |
|
808 |
B
|
808B
|
B. Average Sleep Time
| 1,300 |
data structures; implementation; math
|
It's been almost a week since Polycarp couldn't get rid of insomnia. And as you may already know, one week in Berland lasts k days!When Polycarp went to a doctor with his problem, the doctor asked him about his sleeping schedule (more specifically, the average amount of hours of sleep per week). Luckily, Polycarp kept records of sleep times for the last n days. So now he has a sequence a1, a2, ..., an, where ai is the sleep time on the i-th day.The number of records is so large that Polycarp is unable to calculate the average value by himself. Thus he is asking you to help him with the calculations. To get the average Polycarp is going to consider k consecutive days as a week. So there will be n - k + 1 weeks to take into consideration. For example, if k = 2, n = 3 and a = [3, 4, 7], then the result is .You should write a program which will calculate average sleep times of Polycarp over all weeks.
|
The first line contains two integer numbers n and k (1 β€ k β€ n β€ 2Β·105).The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 105).
|
Output average sleeping time over all weeks. The answer is considered to be correct if its absolute or relative error does not exceed 10 - 6. In particular, it is enough to output real number with at least 6 digits after the decimal point.
|
In the third example there are n - k + 1 = 7 weeks, so the answer is sums of all weeks divided by 7.
|
Input: 3 23 4 7 | Output: 9.0000000000
|
Easy
| 3 | 910 | 146 | 239 | 8 |
2,117 |
F
|
2117F
|
F. Wildflower
| 1,800 |
combinatorics; dfs and similar; trees
|
Yousef has a rooted tree\(^{\text{β}}\) consisting of exactly \(n\) vertices, which is rooted at vertex \(1\). You would like to give Yousef an array \(a\) of length \(n\), where each \(a_i\) \((1 \le i \le n)\) can either be \(1\) or \(2\).Let \(s_u\) denote the sum of \(a_v\) where vertex \(v\) is in the subtree\(^{\text{β }}\) of vertex \(u\). Yousef considers the tree special if all the values in \(s\) are pairwise distinct (i.e., all subtree sums are unique).Your task is to help Yousef count the number of different arrays \(a\) that result in the tree being special. Two arrays \(b\) and \(c\) are different if there exists an index \(i\) such that \(b_i \neq c_i\).As the result can be very large, you should print it modulo \(10^9 + 7\).\(^{\text{β}}\)A tree is a connected undirected graph with \(n - 1\) edges.\(^{\text{β }}\)The subtree of a vertex \(v\) is the set of all vertices that pass through \(v\) on a simple path to the root. Note that vertex \(v\) is also included in the set.
|
The first line contains an integer \(t\) \((1 \le t \le 10^4)\) β the number of test cases.Each test case consists of several lines. The first line of the test case contains an integer \(n\) \((2 \le n \le 2 \cdot 10^5)\) β the number of vertices in the tree.Then \(nβ1\) lines follow, each of them contains two integers \(u\) and \(v\) \((1 \le u,v \le n, u \ne v)\) which describe a pair of vertices connected by an edge. It is guaranteed that the given graph is a tree and has no loops or multiple edges.It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(2 \cdot 10^5\).
|
For each test case, print one integer \(x\) β the number of different arrays \(a\) that result in the tree being special, modulo \(10^9 + 7\).
|
The tree given in the fifth test case:
|
Input: 721 281 22 33 82 44 55 66 7101 22 33 44 55 64 77 84 99 1071 44 23 23 52 66 771 22 33 43 54 66 775 74 61 61 32 66 753 41 21 32 5 | Output: 4 24 0 16 48 0 4
|
Medium
| 3 | 1,001 | 598 | 142 | 21 |
1,398 |
D
|
1398D
|
D. Colored Rectangles
| 1,800 |
dp; greedy; sortings
|
You are given three multisets of pairs of colored sticks: \(R\) pairs of red sticks, the first pair has length \(r_1\), the second pair has length \(r_2\), \(\dots\), the \(R\)-th pair has length \(r_R\); \(G\) pairs of green sticks, the first pair has length \(g_1\), the second pair has length \(g_2\), \(\dots\), the \(G\)-th pair has length \(g_G\); \(B\) pairs of blue sticks, the first pair has length \(b_1\), the second pair has length \(b_2\), \(\dots\), the \(B\)-th pair has length \(b_B\); You are constructing rectangles from these pairs of sticks with the following process: take a pair of sticks of one color; take a pair of sticks of another color different from the first one; add the area of the resulting rectangle to the total area. Thus, you get such rectangles that their opposite sides are the same color and their adjacent sides are not the same color.Each pair of sticks can be used at most once, some pairs can be left unused. You are not allowed to split a pair into independent sticks.What is the maximum area you can achieve?
|
The first line contains three integers \(R\), \(G\), \(B\) (\(1 \le R, G, B \le 200\)) β the number of pairs of red sticks, the number of pairs of green sticks and the number of pairs of blue sticks.The second line contains \(R\) integers \(r_1, r_2, \dots, r_R\) (\(1 \le r_i \le 2000\)) β the lengths of sticks in each pair of red sticks.The third line contains \(G\) integers \(g_1, g_2, \dots, g_G\) (\(1 \le g_i \le 2000\)) β the lengths of sticks in each pair of green sticks.The fourth line contains \(B\) integers \(b_1, b_2, \dots, b_B\) (\(1 \le b_i \le 2000\)) β the lengths of sticks in each pair of blue sticks.
|
Print the maximum possible total area of the constructed rectangles.
|
In the first example you can construct one of these rectangles: red and green with sides \(3\) and \(5\), red and blue with sides \(3\) and \(4\) and green and blue with sides \(5\) and \(4\). The best area of them is \(4 \times 5 = 20\).In the second example the best rectangles are: red/blue \(9 \times 8\), red/blue \(5 \times 5\), green/blue \(2 \times 1\). So the total area is \(72 + 25 + 2 = 99\).In the third example the best rectangles are: red/green \(19 \times 8\) and red/blue \(20 \times 11\). The total area is \(152 + 220 = 372\). Note that you can't construct more rectangles because you are not allowed to have both pairs taken to be the same color.
|
Input: 1 1 1 3 5 4 | Output: 20
|
Medium
| 3 | 1,054 | 624 | 68 | 13 |
364 |
C
|
364C
|
C. Beautiful Set
| 2,300 |
brute force; number theory
|
We'll call a set of positive integers a beautiful if the following condition fulfills: for any prime p, if , then . In other words, if one number from the set is divisible by prime p, then at least half of numbers from the set is divisible by p.Your task is to find any beautiful set, where the number of elements is equal to k and each element doesn't exceed 2k2.
|
The first line contains integer k (10 β€ k β€ 5000) that shows how many numbers the required beautiful set should have.
|
In the first line print k space-separated integers that are a beautiful set. If there are multiple such sets, you are allowed to print any of them.
|
Input: 10 | Output: 16 18 24 27 36 48 54 72 108 144
|
Expert
| 2 | 364 | 117 | 147 | 3 |
|
1,668 |
A
|
1668A
|
A. Direction Change
| 800 |
implementation; math
|
You are given a grid with \(n\) rows and \(m\) columns. Rows and columns are numbered from \(1\) to \(n\), and from \(1\) to \(m\). The intersection of the \(a\)-th row and \(b\)-th column is denoted by \((a, b)\). Initially, you are standing in the top left corner \((1, 1)\). Your goal is to reach the bottom right corner \((n, m)\).You can move in four directions from \((a, b)\): up to \((a-1, b)\), down to \((a+1, b)\), left to \((a, b-1)\) or right to \((a, b+1)\).You cannot move in the same direction in two consecutive moves, and you cannot leave the grid. What is the minimum number of moves to reach \((n, m)\)?
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)) β the number of the test cases. The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10^9\)) β the size of the grid.
|
For each test case, print a single integer: \(-1\) if it is impossible to reach \((n, m)\) under the given conditions, otherwise the minimum number of moves.
|
Test case \(1\): \(n=1\), \(m=1\), and initially you are standing in \((1, 1)\) so \(0\) move is required to reach \((n, m) = (1, 1)\).Test case \(2\): you should go down to reach \((2, 1)\).Test case \(3\): it is impossible to reach \((1, 3)\) without moving right two consecutive times, or without leaving the grid.Test case \(4\): an optimal moving sequence could be: \((1, 1) \to (1, 2) \to (2, 2) \to (2, 1) \to (3, 1) \to (3, 2) \to (4, 2)\). It can be proved that this is the optimal solution. So the answer is \(6\).
|
Input: 61 12 11 34 24 610 5 | Output: 0 1 -1 6 10 17
|
Beginner
| 2 | 623 | 307 | 157 | 16 |
1,980 |
D
|
1980D
|
D. GCD-sequence
| 1,400 |
greedy; implementation; math; number theory
|
GCD (Greatest Common Divisor) of two integers \(x\) and \(y\) is the maximum integer \(z\) by which both \(x\) and \(y\) are divisible. For example, \(GCD(36, 48) = 12\), \(GCD(5, 10) = 5\), and \(GCD(7,11) = 1\).Kristina has an array \(a\) consisting of exactly \(n\) positive integers. She wants to count the GCD of each neighbouring pair of numbers to get a new array \(b\), called GCD-sequence. So, the elements of the GCD-sequence \(b\) will be calculated using the formula \(b_i = GCD(a_i, a_{i + 1})\) for \(1 \le i \le n - 1\).Determine whether it is possible to remove exactly one number from the array \(a\) so that the GCD sequence \(b\) is non-decreasing (i.e., \(b_i \le b_{i+1}\) is always true).For example, let Khristina had an array \(a\) = [\(20, 6, 12, 3, 48, 36\)]. If she removes \(a_4 = 3\) from it and counts the GCD-sequence of \(b\), she gets: \(b_1 = GCD(20, 6) = 2\) \(b_2 = GCD(6, 12) = 6\) \(b_3 = GCD(12, 48) = 12\) \(b_4 = GCD(48, 36) = 12\) The resulting GCD sequence \(b\) = [\(2,6,12,12\)] is non-decreasing because \(b_1 \le b_2 \le b_3 \le b_4\).
|
The first line of input data contains a single number \(t\) (\(1 \le t \le 10^4\)) β he number of test cases in the test.This is followed by the descriptions of the test cases.The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2 \cdot 10^5\)) β the number of elements in the array \(a\).The second line of each test case contains exactly \(n\) integers \(a_i\) (\(1 \le a_i \le 10^9\)) β the elements of array \(a\).It is guaranteed that the sum of \(n\) over all test case does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single line: ""YES"" if you can remove exactly one number from the array \(a\) so that the GCD-sequence of \(b\) is non-decreasing; ""NO"" otherwise. You can output the answer in any case (for example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will all be recognized as a positive answer).
|
The first test case is explained in the problem statement.
|
Input: 12620 6 12 3 48 36412 6 3 4310 12 3532 16 8 4 25100 50 2 10 2042 4 8 1107 4 6 2 4 5 1 4 2 875 9 6 8 5 9 2611 14 8 12 9 395 7 3 10 6 3 12 6 334 2 481 6 11 12 6 12 3 6 | Output: YES NO YES NO YES YES NO YES YES YES YES YES
|
Easy
| 4 | 1,082 | 536 | 327 | 19 |
1,992 |
C
|
1992C
|
C. Gorilla and Permutation
| 900 |
constructive algorithms; math
|
Gorilla and Noobish_Monk found three numbers \(n\), \(m\), and \(k\) (\(m < k\)). They decided to construct a permutation\(^{\dagger}\) of length \(n\).For the permutation, Noobish_Monk came up with the following function: \(g(i)\) is the sum of all the numbers in the permutation on a prefix of length \(i\) that are not greater than \(m\). Similarly, Gorilla came up with the function \(f\), where \(f(i)\) is the sum of all the numbers in the permutation on a prefix of length \(i\) that are not less than \(k\). A prefix of length \(i\) is a subarray consisting of the first \(i\) elements of the original array.For example, if \(n = 5\), \(m = 2\), \(k = 5\), and the permutation is \([5, 3, 4, 1, 2]\), then: \(f(1) = 5\), because \(5 \ge 5\); \(g(1) = 0\), because \(5 > 2\); \(f(2) = 5\), because \(3 < 5\); \(g(2) = 0\), because \(3 > 2\); \(f(3) = 5\), because \(4 < 5\); \(g(3) = 0\), because \(4 > 2\); \(f(4) = 5\), because \(1 < 5\); \(g(4) = 1\), because \(1 \le 2\); \(f(5) = 5\), because \(2 < 5\); \(g(5) = 1 + 2 = 3\), because \(2 \le 2\). Help them find a permutation for which the value of \(\left(\sum_{i=1}^n f(i) - \sum_{i=1}^n g(i)\right)\) is maximized.\(^{\dagger}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in any order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (as \(2\) appears twice in the array) and \([1,3,4]\) is also not a permutation (as \(n=3\), but \(4\) appears in the array).
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The only line of each case contains three integers \(n\), \(m\), \(k\) (\(2\le n \le 10^5\); \(1 \le m < k \le n\)) β the size of the permutation to be constructed and two integers.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output the permutation β a set of numbers that satisfies the conditions of the problem. If there are multiple solutions, output any of them.
|
In the first example, \(\left(\sum_{i=1}^n f(i) - \sum_{i=1}^n g(i)\right) = 5 \cdot 5 - (0 \cdot 3 + 1 + 3) = 25 - 4 = 21\)
|
Input: 35 2 53 1 310 3 8 | Output: 5 3 4 1 2 3 2 1 10 9 8 4 7 5 6 1 2 3
|
Beginner
| 2 | 1,516 | 370 | 160 | 19 |
390 |
E
|
390E
|
E. Inna and Large Sweet Matrix
| 0 |
Inna loves sweets very much. That's why she wants to play the ""Sweet Matrix"" game with Dima and Sereja. But Sereja is a large person, so the game proved small for him. Sereja suggested playing the ""Large Sweet Matrix"" game.The ""Large Sweet Matrix"" playing field is an n Γ m matrix. Let's number the rows of the matrix from 1 to n, and the columns β from 1 to m. Let's denote the cell in the i-th row and j-th column as (i, j). Each cell of the matrix can contain multiple candies, initially all cells are empty. The game goes in w moves, during each move one of the two following events occurs: Sereja chooses five integers x1, y1, x2, y2, v (x1 β€ x2, y1 β€ y2) and adds v candies to each matrix cell (i, j) (x1 β€ i β€ x2; y1 β€ j β€ y2). Sereja chooses four integers x1, y1, x2, y2 (x1 β€ x2, y1 β€ y2). Then he asks Dima to calculate the total number of candies in cells (i, j) (x1 β€ i β€ x2; y1 β€ j β€ y2) and he asks Inna to calculate the total number of candies in the cells of matrix (p, q), which meet the following logical criteria: (p < x1 OR p > x2) AND (q < y1 OR q > y2). Finally, Sereja asks to write down the difference between the number Dima has calculated and the number Inna has calculated (D - I). Unfortunately, Sereja's matrix is really huge. That's why Inna and Dima aren't coping with the calculating. Help them!
|
The first line of the input contains three integers n, m and w (3 β€ n, m β€ 4Β·106; 1 β€ w β€ 105).The next w lines describe the moves that were made in the game. A line that describes an event of the first type contains 6 integers: 0, x1, y1, x2, y2 and v (1 β€ x1 β€ x2 β€ n; 1 β€ y1 β€ y2 β€ m; 1 β€ v β€ 109). A line that describes an event of the second type contains 5 integers: 1, x1, y1, x2, y2 (2 β€ x1 β€ x2 β€ n - 1; 2 β€ y1 β€ y2 β€ m - 1). It is guaranteed that the second type move occurs at least once. It is guaranteed that a single operation will not add more than 109 candies.Be careful, the constraints are very large, so please use optimal data structures. Max-tests will be in pretests.
|
For each second type move print a single integer on a single line β the difference between Dima and Inna's numbers.
|
Note to the sample. After the first query the matrix looks as: 2220022200 0000000000After the second one it is: 222002550003300 00000After the third one it is: 22201255010330100001For the fourth query, Dima's sum equals 5 + 0 + 3 + 0 = 8 and Inna's sum equals 4 + 1 + 0 + 1 = 6. The answer to the query equals 8 - 6 = 2. For the fifth query, Dima's sum equals 0 and Inna's sum equals 18 + 2 + 0 + 1 = 21. The answer to the query is 0 - 21 = -21.
|
Input: 4 5 50 1 1 2 3 20 2 2 3 3 30 1 5 4 5 11 2 3 3 41 3 4 3 4 | Output: 2-21
|
Beginner
| 0 | 1,333 | 689 | 115 | 3 |
|
675 |
A
|
675A
|
A. Infinite Sequence
| 1,100 |
math
|
Vasya likes everything infinite. Now he is studying the properties of a sequence s, such that its first element is equal to a (s1 = a), and the difference between any two neighbouring elements is equal to c (si - si - 1 = c). In particular, Vasya wonders if his favourite integer b appears in this sequence, that is, there exists a positive integer i, such that si = b. Of course, you are the person he asks for a help.
|
The first line of the input contain three integers a, b and c ( - 109 β€ a, b, c β€ 109) β the first element of the sequence, Vasya's favorite number and the difference between any two neighbouring elements of the sequence, respectively.
|
If b appears in the sequence s print ""YES"" (without quotes), otherwise print ""NO"" (without quotes).
|
In the first sample, the sequence starts from integers 1, 4, 7, so 7 is its element.In the second sample, the favorite integer of Vasya is equal to the first element of the sequence.In the third sample all elements of the sequence are greater than Vasya's favorite integer.In the fourth sample, the sequence starts from 0, 50, 100, and all the following elements are greater than Vasya's favorite integer.
|
Input: 1 7 3 | Output: YES
|
Easy
| 1 | 419 | 235 | 103 | 6 |
925 |
A
|
925A
|
A. Stairs and Elevators
| 1,600 |
binary search
|
In the year of \(30XX\) participants of some world programming championship live in a single large hotel. The hotel has \(n\) floors. Each floor has \(m\) sections with a single corridor connecting all of them. The sections are enumerated from \(1\) to \(m\) along the corridor, and all sections with equal numbers on different floors are located exactly one above the other. Thus, the hotel can be represented as a rectangle of height \(n\) and width \(m\). We can denote sections with pairs of integers \((i, j)\), where \(i\) is the floor, and \(j\) is the section number on the floor.The guests can walk along the corridor on each floor, use stairs and elevators. Each stairs or elevator occupies all sections \((1, x)\), \((2, x)\), \(\ldots\), \((n, x)\) for some \(x\) between \(1\) and \(m\). All sections not occupied with stairs or elevators contain guest rooms. It takes one time unit to move between neighboring sections on the same floor or to move one floor up or down using stairs. It takes one time unit to move up to \(v\) floors in any direction using an elevator. You can assume you don't have to wait for an elevator, and the time needed to enter or exit an elevator is negligible.You are to process \(q\) queries. Each query is a question ""what is the minimum time needed to go from a room in section \((x_1, y_1)\) to a room in section \((x_2, y_2)\)?""
|
The first line contains five integers \(n, m, c_l, c_e, v\) (\(2 \leq n, m \leq 10^8\), \(0 \leq c_l, c_e \leq 10^5\), \(1 \leq c_l + c_e \leq m - 1\), \(1 \leq v \leq n - 1\)) β the number of floors and section on each floor, the number of stairs, the number of elevators and the maximum speed of an elevator, respectively.The second line contains \(c_l\) integers \(l_1, \ldots, l_{c_l}\) in increasing order (\(1 \leq l_i \leq m\)), denoting the positions of the stairs. If \(c_l = 0\), the second line is empty.The third line contains \(c_e\) integers \(e_1, \ldots, e_{c_e}\) in increasing order, denoting the elevators positions in the same format. It is guaranteed that all integers \(l_i\) and \(e_i\) are distinct.The fourth line contains a single integer \(q\) (\(1 \leq q \leq 10^5\)) β the number of queries.The next \(q\) lines describe queries. Each of these lines contains four integers \(x_1, y_1, x_2, y_2\) (\(1 \leq x_1, x_2 \leq n\), \(1 \leq y_1, y_2 \leq m\)) β the coordinates of starting and finishing sections for the query. It is guaranteed that the starting and finishing sections are distinct. It is also guaranteed that these sections contain guest rooms, i. e. \(y_1\) and \(y_2\) are not among \(l_i\) and \(e_i\).
|
Print \(q\) integers, one per line β the answers for the queries.
|
In the first query the optimal way is to go to the elevator in the 5-th section in four time units, use it to go to the fifth floor in two time units and go to the destination in one more time unit.In the second query it is still optimal to use the elevator, but in the third query it is better to use the stairs in the section 2.
|
Input: 5 6 1 1 32531 1 5 61 3 5 43 3 5 3 | Output: 754
|
Medium
| 1 | 1,376 | 1,245 | 65 | 9 |
288 |
C
|
288C
|
C. Polo the Penguin and XOR operation
| 1,700 |
implementation; math
|
Little penguin Polo likes permutations. But most of all he likes permutations of integers from 0 to n, inclusive.For permutation p = p0, p1, ..., pn, Polo has defined its beauty β number .Expression means applying the operation of bitwise excluding ""OR"" to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is represented as ""^"" and in Pascal β as ""xor"".Help him find among all permutations of integers from 0 to n the permutation with the maximum beauty.
|
The single line contains a positive integer n (1 β€ n β€ 106).
|
In the first line print integer m the maximum possible beauty. In the second line print any permutation of integers from 0 to n with the beauty equal to m.If there are several suitable permutations, you are allowed to print any of them.
|
Input: 4 | Output: 200 2 1 4 3
|
Medium
| 2 | 528 | 60 | 236 | 2 |
|
994 |
A
|
994A
|
A. Fingerprints
| 800 |
implementation
|
You are locked in a room with a door that has a keypad with 10 keys corresponding to digits from 0 to 9. To escape from the room, you need to enter a correct code. You also have a sequence of digits.Some keys on the keypad have fingerprints. You believe the correct code is the longest not necessarily contiguous subsequence of the sequence you have that only contains digits with fingerprints on the corresponding keys. Find such code.
|
The first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 10\)) representing the number of digits in the sequence you have and the number of keys on the keypad that have fingerprints.The next line contains \(n\) distinct space-separated integers \(x_1, x_2, \ldots, x_n\) (\(0 \le x_i \le 9\)) representing the sequence.The next line contains \(m\) distinct space-separated integers \(y_1, y_2, \ldots, y_m\) (\(0 \le y_i \le 9\)) β the keys with fingerprints.
|
In a single line print a space-separated sequence of integers representing the code. If the resulting sequence is empty, both printing nothing and printing a single line break is acceptable.
|
In the first example, the only digits with fingerprints are \(1\), \(2\) and \(7\). All three of them appear in the sequence you know, \(7\) first, then \(1\) and then \(2\). Therefore the output is 7 1 2. Note that the order is important, and shall be the same as the order in the original sequence.In the second example digits \(0\), \(1\), \(7\) and \(9\) have fingerprints, however only \(0\) and \(1\) appear in the original sequence. \(1\) appears earlier, so the output is 1 0. Again, the order is important.
|
Input: 7 33 5 7 1 6 2 81 2 7 | Output: 7 1 2
|
Beginner
| 1 | 436 | 473 | 190 | 9 |
1,920 |
F2
|
1920F2
|
F2. Smooth Sailing (Hard Version)
| 3,000 |
binary search; data structures; dsu; geometry; graphs; trees
|
The only difference between the two versions of this problem is the constraint on \(q\). You can make hacks only if both versions of the problem are solved.Thomas is sailing around an island surrounded by the ocean. The ocean and island can be represented by a grid with \(n\) rows and \(m\) columns. The rows are numbered from \(1\) to \(n\) from top to bottom, and the columns are numbered from \(1\) to \(m\) from left to right. The position of a cell at row \(r\) and column \(c\) can be represented as \((r, c)\). Below is an example of a valid grid. Example of a valid grid There are three types of cells: island, ocean and underwater volcano. Cells representing the island are marked with a '#', cells representing the ocean are marked with a '.', and cells representing an underwater volcano are marked with a 'v'. It is guaranteed that there is at least one island cell and at least one underwater volcano cell. It is also guaranteed that the set of all island cells forms a single connected component\(^{\dagger}\) and the set of all ocean cells and underwater volcano cells forms a single connected component. Additionally, it is guaranteed that there are no island cells at the edge of the grid (that is, at row \(1\), at row \(n\), at column \(1\), and at column \(m\)).Define a round trip starting from cell \((x, y)\) as a path Thomas takes which satisfies the following conditions: The path starts and ends at \((x, y)\). If Thomas is at cell \((i, j)\), he can go to cells \((i+1, j)\), \((i-1, j)\), \((i, j-1)\), and \((i, j+1)\) as long as the destination cell is an ocean cell or an underwater volcano cell and is still inside the grid. Note that it is allowed for Thomas to visit the same cell multiple times in the same round trip. The path must go around the island and fully encircle it. Some path \(p\) fully encircles the island if it is impossible to go from an island cell to a cell on the grid border by only traveling to adjacent on a side or diagonal cells without visiting a cell on path \(p\). In the image below, the path starting from \((2, 2)\), going to \((1, 3)\), and going back to \((2, 2)\) the other way does not fully encircle the island and is not considered a round trip. Example of a path that does not fully encircle the island The safety of a round trip is the minimum Manhattan distance\(^{\ddagger}\) from a cell on the round trip to an underwater volcano (note that the presence of island cells does not impact this distance).You have \(q\) queries. A query can be represented as \((x, y)\) and for every query, you want to find the maximum safety of a round trip starting from \((x, y)\). It is guaranteed that \((x, y)\) is an ocean cell or an underwater volcano cell.\(^{\dagger}\)A set of cells forms a single connected component if from any cell of this set it is possible to reach any other cell of this set by moving only through the cells of this set, each time going to a cell with a common side.\(^{\ddagger}\)Manhattan distance between cells \((r_1, c_1)\) and \((r_2, c_2)\) is equal to \(|r_1 - r_2| + |c_1 - c_2|\).
|
The first line contains three integers \(n\), \(m\), and \(q\) (\(3 \leq n, m \leq 10^5\), \(9 \leq n \cdot m \leq 3 \cdot 10^5\), \(1 \leq q \leq 3 \cdot 10^5\)) β the number of rows and columns of the grid and the number of queries. Each of the following \(n\) lines contains \(m\) characters describing the cells of the grid. The character '#' denotes an island cell, '.' denotes an ocean cell, and 'v' denotes an underwater volcano cell.It is guaranteed that there is at least one island cell and at least one underwater volcano cell. It is guaranteed that the set of all island cells forms a single connected component and the set of all ocean cells and underwater volcano cells forms a single connected component. Also, it is guaranteed that there are no island cells at the edge of the grid (that is, at the row \(1\), at the row \(n\), at the column \(1\), and at the column \(m\)).The following \(q\) lines describe the queries. Each of these lines contains two integers \(x\) and \(y\) (\(1 \leq x \leq n\), \(1 \leq y \leq m\)) denoting a round trip starting from \((x, y)\).It is guaranteed that \((x, y)\) is an ocean cell or an underwater volcano cell.
|
For each query, output a single integer β the maximum safety of a round trip starting from the specified position.
|
For the first example, the image below shows an optimal round trip starting from \((1, 1)\). The round trip has a safety of \(3\) as the minimum Manhattan distance from a cell on the round trip to an underwater volcano is \(3\). Example of an optimal round trip For the fourth example, remember that it is allowed for Thomas to visit the same cell multiple times in the same round trip. For example, doing so is necessary for the round trip starting from \((7, 6)\).
|
Input: 9 9 3 ......... ......... ....###.. ...v#.... ..###.... ...##...v ...##.... ......... v........ 1 1 9 1 5 7 | Output: 3 0 3
|
Master
| 6 | 3,081 | 1,166 | 114 | 19 |
413 |
B
|
413B
|
B. Spyke Chatting
| 1,300 |
implementation
|
The R2 company has n employees working for it. The work involves constant exchange of ideas, sharing the stories of success and upcoming challenging. For that, R2 uses a famous instant messaging program Spyke.R2 has m Spyke chats just to discuss all sorts of issues. In each chat, some group of employees exchanges messages daily. An employee can simultaneously talk in multiple chats. If some employee is in the k-th chat, he can write messages to this chat and receive notifications about messages from this chat. If an employee writes a message in the chat, all other participants of the chat receive a message notification.The R2 company is conducting an audit. Now the specialists study effective communication between the employees. For this purpose, they have a chat log and the description of chat structure. You, as one of audit specialists, are commissioned to write a program that will use this data to determine the total number of message notifications received by each employee.
|
The first line contains three space-separated integers n, m and k (2 β€ n β€ 2Β·104; 1 β€ m β€ 10; 1 β€ k β€ 2Β·105) β the number of the employees, the number of chats and the number of events in the log, correspondingly. Next n lines contain matrix a of size n Γ m, consisting of numbers zero and one. The element of this matrix, recorded in the j-th column of the i-th line, (let's denote it as aij) equals 1, if the i-th employee is the participant of the j-th chat, otherwise the element equals 0. Assume that the employees are numbered from 1 to n and the chats are numbered from 1 to m.Next k lines contain the description of the log events. The i-th line contains two space-separated integers xi and yi (1 β€ xi β€ n; 1 β€ yi β€ m) which mean that the employee number xi sent one message to chat number yi. It is guaranteed that employee number xi is a participant of chat yi. It is guaranteed that each chat contains at least two employees.
|
Print in the single line n space-separated integers, where the i-th integer shows the number of message notifications the i-th employee receives.
|
Input: 3 4 51 1 1 11 0 1 11 1 0 01 13 11 32 43 2 | Output: 3 3 1
|
Easy
| 1 | 992 | 936 | 145 | 4 |
|
1,860 |
C
|
1860C
|
C. Game on Permutation
| 1,400 |
data structures; dp; games; greedy
|
Alice and Bob are playing a game. They have a permutation \(p\) of size \(n\) (a permutation of size \(n\) is an array of size \(n\) where each element from \(1\) to \(n\) occurs exactly once). They also have a chip, which can be placed on any element of the permutation.Alice and Bob make alternating moves: Alice makes the first move, then Bob makes the second move, then Alice makes the third move, and so on. During the first move, Alice chooses any element of the permutation and places the chip on that element. During each of the next moves, the current player has to move the chip to any element that is simultaneously to the left and strictly less than the current element (i. e. if the chip is on the \(i\)-th element, it can be moved to the \(j\)-th element if \(j < i\) and \(p_j < p_i\)). If a player cannot make a move (it is impossible to move the chip according to the rules of the game), that player wins the game.Let's say that the \(i\)-th element of the permutation is lucky if the following condition holds: if Alice places the chip on the \(i\)-th element during her first move, she can win the game no matter how Bob plays (i. e. she has a winning strategy). You have to calculate the number of lucky elements in the permutation.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 3 \cdot 10^5\)) β the number of elements in the permutation.The second line contains \(n\) integers \(p_1, p_2, \dots, p_n\) (\(1 \le p_i \le n\)). All \(p_i\) are distinct. The sum of \(n\) over all test cases doesn't exceed \(3 \cdot 10^5\).
|
For each test case, print a single integer β the number of lucky elements in the permutation.
|
In the first test case of the example, the \(3\)-rd element of the permutation is lucky.In the second test case of the example, there are no lucky elements.In the third test case of the example, the \(2\)-nd element of the permutation is lucky.In the fourth test case of the example, the \(3\)-rd and the \(4\)-th element of the permutation are lucky.
|
Input: 432 1 322 131 2 342 1 4 3 | Output: 1 0 1 2
|
Easy
| 4 | 1,252 | 420 | 93 | 18 |
1,857 |
E
|
1857E
|
E. Power of Points
| 1,500 |
math; sortings
|
You are given \(n\) points with integer coordinates \(x_1,\dots x_n\), which lie on a number line.For some integer \(s\), we construct segments [\(s,x_1\)], [\(s,x_2\)], \(\dots\), [\(s,x_n\)]. Note that if \(x_i<s\), then the segment will look like [\(x_i,s\)]. The segment [\(a, b\)] covers all integer points \(a, a+1, a+2, \dots, b\).We define the power of a point \(p\) as the number of segments that intersect the point with coordinate \(p\), denoted as \(f_p\).Your task is to compute \(\sum\limits_{p=1}^{10^9}f_p\) for each \(s \in \{x_1,\dots,x_n\}\), i.e., the sum of \(f_p\) for all integer points from \(1\) to \(10^9\).For example, if the initial coordinates are \([1,2,5,7,1]\) and we choose \(s=5\), then the segments will be: \([1,5]\),\([2,5]\),\([5,5]\),\([5,7]\),\([1,5]\). And the powers of the points will be: \(f_1=2, f_2=3, f_3=3, f_4=3, f_5=5, f_6=1, f_7=1, f_8=0, \dots, f_{10^9}=0\). Their sum is \(2+3+3+3+5+1+1=18\).
|
The first line contains an integer \(t\) (\(1\le t\le 10^4\)) β the number of test cases.The first line of each test case contains an integer \(n\) (\(1 \le n \le 2\cdot 10^5\)) β the number of points.The second line contains \(n\) integers \(x_1,x_2 \dots x_n\) (\(1 \le x_i \le 10^9\)) β the coordinates of the points.It is guaranteed that the sum of the values of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output \(n\) integers, where the \(i\)-th integer is equal to the sum of the powers of all points for \(s=x_i\).
|
In the first test case we first choose \(s=x_1=1\), then the following segments are formed: \([1,1]\),\([1,4]\),\([1,3]\). The powers of the points will be as follows: \(f_1=3, f_2=2, f_3=2, f_4=1, f_5=0 \dots\) The sum of powers of the points: \(3+2+2+1+0+\dots+0=8\). After that we choose \(s=x_2=4\). Then there will be such segments: \([1,4]\),\([4,4]\),\([3,4]\), and powers of the points are \(f_1=1, f_2=1, f_3=2, f_4=3\). At the end we take \(s=x_3=3\) and the segments look like this: \([1,3]\),\([3,4]\),\([3,3]\), the powers of the points are \(f_1=1, f_2=1, f_3=3, f_4=1\).
|
Input: 331 4 351 2 5 7 141 10 100 1000 | Output: 8 7 6 16 15 18 24 16 1111 1093 1093 2893
|
Medium
| 2 | 945 | 425 | 132 | 18 |
513 |
G1
|
513G1
|
G1. Inversions problem
| 1,800 |
brute force; dfs and similar; dp; meet-in-the-middle
|
You are given a permutation of n numbers p1, p2, ..., pn. We perform k operations of the following type: choose uniformly at random two indices l and r (l β€ r) and reverse the order of the elements pl, pl + 1, ..., pr. Your task is to find the expected value of the number of inversions in the resulting permutation.
|
The first line of input contains two integers n and k (1 β€ n β€ 100, 1 β€ k β€ 109). The next line contains n integers p1, p2, ..., pn β the given permutation. All pi are different and in range from 1 to n.The problem consists of three subproblems. The subproblems have different constraints on the input. You will get some score for the correct submission of the subproblem. The description of the subproblems follows. In subproblem G1 (3 points), the constraints 1 β€ n β€ 6, 1 β€ k β€ 4 will hold. In subproblem G2 (5 points), the constraints 1 β€ n β€ 30, 1 β€ k β€ 200 will hold. In subproblem G3 (16 points), the constraints 1 β€ n β€ 100, 1 β€ k β€ 109 will hold.
|
Output the answer with absolute or relative error no more than 1e - 9.
|
Consider the first sample test. We will randomly pick an interval of the permutation (1, 2, 3) (which has no inversions) and reverse the order of its elements. With probability , the interval will consist of a single element and the permutation will not be altered. With probability we will inverse the first two elements' order and obtain the permutation (2, 1, 3) which has one inversion. With the same probability we might pick the interval consisting of the last two elements which will lead to the permutation (1, 3, 2) with one inversion. Finally, with probability the randomly picked interval will contain all elements, leading to the permutation (3, 2, 1) with 3 inversions. Hence, the expected number of inversions is equal to .
|
Input: 3 11 2 3 | Output: 0.833333333333333
|
Medium
| 4 | 316 | 655 | 70 | 5 |
1,575 |
H
|
1575H
|
H. Holiday Wall Ornaments
| 2,200 |
dp; strings
|
The Winter holiday will be here soon. Mr. Chanek wants to decorate his house's wall with ornaments. The wall can be represented as a binary string \(a\) of length \(n\). His favorite nephew has another binary string \(b\) of length \(m\) (\(m \leq n\)).Mr. Chanek's nephew loves the non-negative integer \(k\). His nephew wants exactly \(k\) occurrences of \(b\) as substrings in \(a\). However, Mr. Chanek does not know the value of \(k\). So, for each \(k\) (\(0 \leq k \leq n - m + 1\)), find the minimum number of elements in \(a\) that have to be changed such that there are exactly \(k\) occurrences of \(b\) in \(a\).A string \(s\) occurs exactly \(k\) times in \(t\) if there are exactly \(k\) different pairs \((p,q)\) such that we can obtain \(s\) by deleting \(p\) characters from the beginning and \(q\) characters from the end of \(t\).
|
The first line contains two integers \(n\) and \(m\) (\(1 \leq m \leq n \leq 500\)) β size of the binary string \(a\) and \(b\) respectively.The second line contains a binary string \(a\) of length \(n\).The third line contains a binary string \(b\) of length \(m\).
|
Output \(n - m + 2\) integers β the \((k+1)\)-th integer denotes the minimal number of elements in \(a\) that have to be changed so there are exactly \(k\) occurrences of \(b\) as a substring in \(a\).
|
For \(k = 0\), to make the string \(a\) have no occurrence of 101, you can do one character change as follows.100101011 \(\rightarrow\) 100100011For \(k = 1\), you can also change a single character.100101011 \(\rightarrow\) 100001011For \(k = 2\), no changes are needed.
|
Input: 9 3 100101011 101 | Output: 1 1 0 1 6 -1 -1 -1
|
Hard
| 2 | 849 | 266 | 201 | 15 |
2,068 |
H
|
2068H
|
H. Statues
| 2,700 |
constructive algorithms; greedy; math
|
The mayor of a city wants to place \(n\) statues at intersections around the city. The intersections in the city are at all points \((x, y)\) with integer coordinates. Distances between intersections are measured using Manhattan distance, defined as follows: $$$\( \text{distance}((x_1, y_1), (x_2, y_2)) = |x_1 - x_2| + |y_1 - y_2|. \)\(The city council has provided the following requirements for the placement of the statues: The first statue is placed at \)(0, 0)\(; The \)n\(-th statue is placed at \)(a, b)\(; For \)i = 1, \dots, n-1\(, the distance between the \)i\(-th statue and the \)(i+1)\(-th statue is \)d_i\(. It is allowed to place multiple statues at the same intersection.Help the mayor find a valid arrangement of the \)n$$$ statues, or determine that it does not exist.
|
The first line contains an integer \(n\) (\(3 \le n \le 50\)) β the number of statues.The second line contains two integers \(a\) and \(b\) (\(0 \le a, b \le 10^9\)) β the coordinates of the intersection where the \(n\)-th statue must be placed.The third line contains \(n-1\) integers \(d_1, \dots, d_{n-1}\) (\(0 \le d_i \le 10^9\)) β the distance between the \(i\)-th statue and the \((i+1)\)-th statue.
|
Print \(\texttt{YES}\) if there is a valid arrangement of the \(n\) statues. Otherwise, print \(\texttt{NO}\).If there is a valid arrangement, print a valid arrangement in the following \(n\) lines. The \(i\)-th of these lines must contain two integers \(x_i\) and \(y_i\) β the coordinates of the intersection where the \(i\)-th statue is placed. You can print any valid arrangement if multiple exist.
|
In the first sample, there is no valid arrangement of the 3 statues.In the second sample, the sample output is shown in the following picture. Note that this is not the only valid arrangement of the 4 statues.
|
Input: 35 89 0 | Output: NO
|
Master
| 3 | 788 | 406 | 402 | 20 |
784 |
C
|
784C
|
C. INTERCALC
| 1,800 |
*special; implementation
|
DO YOU EXPECT ME TO FIND THIS OUT?WHAT BASE AND/XOR LANGUAGE INCLUDES string?DON'T BYTE OF MORE THAN YOU CAN CHEWYOU CAN ONLY DISTORT THE LARGEST OF MATHEMATICS SO FARSAYING ""ABRACADABRA"" WITHOUT A MAGIC AND WON'T DO YOU ANY GOODTHE LAST STACK RUPTURES. ALL DIE. OH, THE EMBARRASSMENT!I HAVE NO ARRAY AND I MUST SCREAMELEMENTS MAY NOT BE STORED IN WEST HYPERSPACE
|
The first line of input data contains a single integer n (1 β€ n β€ 10).The second line of input data contains n space-separated integers ai (1 β€ ai β€ 11).
|
Output a single integer.
|
Input: 42 5 3 1 | Output: 4
|
Medium
| 2 | 365 | 153 | 24 | 7 |
|
621 |
B
|
621B
|
B. Wet Shark and Bishops
| 1,300 |
combinatorics; implementation
|
Today, Wet Shark is given n bishops on a 1000 by 1000 grid. Both rows and columns of the grid are numbered from 1 to 1000. Rows are numbered from top to bottom, while columns are numbered from left to right.Wet Shark thinks that two bishops attack each other if they share the same diagonal. Note, that this is the only criteria, so two bishops may attack each other (according to Wet Shark) even if there is another bishop located between them. Now Wet Shark wants to count the number of pairs of bishops that attack each other.
|
The first line of the input contains n (1 β€ n β€ 200 000) β the number of bishops.Each of next n lines contains two space separated integers xi and yi (1 β€ xi, yi β€ 1000) β the number of row and the number of column where i-th bishop is positioned. It's guaranteed that no two bishops share the same position.
|
Output one integer β the number of pairs of bishops which attack each other.
|
In the first sample following pairs of bishops attack each other: (1, 3), (1, 5), (2, 3), (2, 4), (3, 4) and (3, 5). Pairs (1, 2), (1, 4), (2, 5) and (4, 5) do not attack each other because they do not share the same diagonal.
|
Input: 51 11 53 35 15 5 | Output: 6
|
Easy
| 2 | 529 | 308 | 76 | 6 |
1,482 |
B
|
1482B
|
B. Restore Modulo
| 1,500 |
implementation; math
|
For the first place at the competition, Alex won many arrays of integers and was assured that these arrays are very expensive. After the award ceremony Alex decided to sell them. There is a rule in arrays pawnshop: you can sell array only if it can be compressed to a generator.This generator takes four non-negative numbers \(n\), \(m\), \(c\), \(s\). \(n\) and \(m\) must be positive, \(s\) non-negative and for \(c\) it must be true that \(0 \leq c < m\). The array \(a\) of length \(n\) is created according to the following rules: \(a_1 = s \bmod m\), here \(x \bmod y\) denotes remainder of the division of \(x\) by \(y\); \(a_i = (a_{i-1} + c) \bmod m\) for all \(i\) such that \(1 < i \le n\). For example, if \(n = 5\), \(m = 7\), \(c = 4\), and \(s = 10\), then \(a = [3, 0, 4, 1, 5]\).Price of such an array is the value of \(m\) in this generator.Alex has a question: how much money he can get for each of the arrays. Please, help him to understand for every array whether there exist four numbers \(n\), \(m\), \(c\), \(s\) that generate this array. If yes, then maximize \(m\).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^5\)) β the number of arrays.The first line of array description contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β the size of this array.The second line of array description contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 10^9\) ) β elements of the array. It is guaranteed that the sum of array sizes does not exceed \(10^5\).
|
For every array print: \(-1\), if there are no such four numbers that generate this array; \(0\), if \(m\) can be arbitrary large; the maximum value \(m\) and any appropriate \(c\) (\(0 \leq c < m\)) in other cases.
|
Input: 6 6 1 9 17 6 14 3 3 4 2 2 3 7 3 4 3 2 2 4 5 0 1000000000 0 1000000000 0 2 1 1 | Output: 19 8 -1 -1 -1 2000000000 1000000000 0
|
Medium
| 2 | 1,091 | 423 | 215 | 14 |
|
1,712 |
C
|
1712C
|
C. Sort Zero
| 1,100 |
greedy; sortings
|
An array is sorted if it has no inversionsA Young BoyYou are given an array of \(n\) positive integers \(a_1,a_2,\ldots,a_n\). In one operation you do the following: Choose any integer \(x\). For all \(i\) such that \(a_i = x\), do \(a_i := 0\) (assign \(0\) to \(a_i\)). Find the minimum number of operations required to sort the array in non-decreasing order.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The second line of each test case contains \(n\) positive integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le n\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case print one integer β the minimum number of operations required to sort the array in non-decreasing order.
|
In the first test case, you can choose \(x = 3\) for the operation, the resulting array is \([0, 0, 2]\).In the second test case, you can choose \(x = 1\) for the first operation and \(x = 3\) for the second operation, the resulting array is \([0, 0, 0, 0]\).
|
Input: 533 3 241 3 1 354 1 5 3 242 4 1 211 | Output: 1 2 4 3 0
|
Easy
| 2 | 361 | 441 | 123 | 17 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.