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
513
C
513C
C. Second price auction
2,000
bitmasks; probabilities
Nowadays, most of the internet advertisements are not statically linked to a web page. Instead, what will be shown to the person opening a web page is determined within 100 milliseconds after the web page is opened. Usually, multiple companies compete for each ad slot on the web page in an auction. Each of them receives a request with details about the user, web page and ad slot and they have to respond within those 100 milliseconds with a bid they would pay for putting an advertisement on that ad slot. The company that suggests the highest bid wins the auction and gets to place its advertisement. If there are several companies tied for the highest bid, the winner gets picked at random.However, the company that won the auction does not have to pay the exact amount of its bid. In most of the cases, a second-price auction is used. This means that the amount paid by the company is equal to the maximum of all the other bids placed for this ad slot.Let's consider one such bidding. There are n companies competing for placing an ad. The i-th of these companies will bid an integer number of microdollars equiprobably randomly chosen from the range between Li and Ri, inclusive. In the other words, the value of the i-th company bid can be any integer from the range [Li, Ri] with the same probability. Determine the expected value that the winner will have to pay in a second-price auction.
The first line of input contains an integer number n (2 ≀ n ≀ 5). n lines follow, the i-th of them containing two numbers Li and Ri (1 ≀ Li ≀ Ri ≀ 10000) describing the i-th company's bid preferences.This problem doesn't have subproblems. You will get 8 points for the correct submission.
Output the answer with absolute or relative error no more than 1e - 9.
Consider the first example. The first company bids a random integer number of microdollars in range [4, 7]; the second company bids between 8 and 10, and the third company bids 5 microdollars. The second company will win regardless of the exact value it bids, however the price it will pay depends on the value of first company's bid. With probability 0.5 the first company will bid at most 5 microdollars, and the second-highest price of the whole auction will be 5. With probability 0.25 it will bid 6 microdollars, and with probability 0.25 it will bid 7 microdollars. Thus, the expected value the second company will have to pay is 0.5Β·5 + 0.25Β·6 + 0.25Β·7 = 5.75.
Input: 34 78 105 5 | Output: 5.7500000000
Hard
2
1,399
288
70
5
1,935
F
1935F
F. Andrey's Tree
2,800
binary search; constructive algorithms; data structures; dfs and similar; dsu; greedy; implementation; trees
Master Andrey loves trees\(^{\dagger}\) very much, so he has a tree consisting of \(n\) vertices.But it's not that simple. Master Timofey decided to steal one vertex from the tree. If Timofey stole vertex \(v\) from the tree, then vertex \(v\) and all edges with one end at vertex \(v\) are removed from the tree, while the numbers of other vertices remain unchanged. To prevent Andrey from getting upset, Timofey decided to make the resulting graph a tree again. To do this, he can add edges between any vertices \(a\) and \(b\), but when adding such an edge, he must pay \(|a - b|\) coins to the Master's Assistance Center.Note that the resulting tree does not contain vertex \(v\).Timofey has not yet decided which vertex \(v\) he will remove from the tree, so he wants to know for each vertex \(1 \leq v \leq n\), the minimum number of coins needed to be spent to make the graph a tree again after removing vertex \(v\), as well as which edges need to be added.\(^{\dagger}\)A tree is an undirected connected graph without cycles.
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\) (\(5 \le n \le 2\cdot10^5\)) β€” the number of vertices in Andrey's tree.The next \(n - 1\) lines contain a description of the tree's edges. The \(i\)-th of these lines contains two integers \(u_i\) and \(v_i\) (\(1 \le u_i, v_i \le n\)) β€” the numbers of vertices connected by the \(i\)-th edge.It is guaranteed that the given edges form a tree.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
For each test case, output the answer in the following format:For each vertex \(v\) (in the order from \(1\) to \(n\)), in the first line output two integers \(w\) and \(m\) β€” the minimum number of coins that need to be spent to make the graph a tree again after removing vertex \(v\), and the number of added edges.Then output \(m\) lines, each containing two integers \(a\) and \(b\) (\(1 \le a, b \le n, a \ne v, b \ne v\), \(a \ne b\)) β€” the ends of the added edge.If there are multiple ways to add edges, you can output any solution with the minimum cost.
In the first test case:Consider the removal of vertex \(4\): The optimal solution would be to add an edge from vertex \(5\) to vertex \(3\). Then we will spend \(|5 - 3| = 2\) coins.In the third test case:Consider the removal of vertex \(1\): The optimal solution would be: Add an edge from vertex \(2\) to vertex \(3\), spending \(|2 - 3| = 1\) coin. Add an edge from vertex \(3\) to vertex \(4\), spending \(|3 - 4| = 1\) coin. Add an edge from vertex \(4\) to vertex \(5\), spending \(|4 - 5| = 1\) coin. Then we will spend a total of \(1 + 1 + 1 = 3\) coins.Consider the removal of vertex \(2\): No edges need to be added, as the graph will remain a tree after removing the vertex.
Input: 351 31 44 53 254 24 33 55 152 11 51 41 3 | Output: 1 1 3 4 0 0 1 1 1 2 2 1 3 5 0 0 0 0 0 0 1 1 1 2 1 1 1 2 1 1 1 2 3 3 2 3 4 5 3 4 0 0 0 0 0 0 0 0
Master
8
1,034
681
560
19
1,023
G
1023G
G. Pisces
3,400
data structures; flows; trees
A group of researchers are studying fish population in a natural system of lakes and rivers. The system contains \(n\) lakes connected by \(n - 1\) rivers. Each river has integer length (in kilometers) and can be traversed in both directions. It is possible to travel between any pair of lakes by traversing the rivers (that is, the network of lakes and rivers form a tree).There is an unknown number of indistinguishable fish living in the lakes. On day \(1\), fish can be at arbitrary lakes. Fish can travel between lakes by swimming the rivers. Each fish can swim a river \(l\) kilometers long in any direction in \(l\) days. Further, each fish can stay any number of days in any particular lake it visits. No fish ever appear or disappear from the lake system. Each lake can accomodate any number of fish at any time.The researchers made several observations. The \(j\)-th of these observations is ""on day \(d_j\) there were at least \(f_j\) distinct fish in the lake \(p_j\)"". Help the researchers in determining the smallest possible total number of fish living in the lake system that doesn't contradict the observations.
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β€” the number of lakes in the system.The next \(n - 1\) lines describe the rivers. The \(i\)-th of these lines contains three integers \(u_i\), \(v_i\), \(l_i\) (\(1 \leq u_i, v_i \leq n\), \(u_i \neq v_i\), \(1 \leq l_i \leq 10^3\)) β€” \(1\)-based indices of lakes connected by the \(i\)-th river, and the length of this river.The next line contains a single integer \(k\) (\(1 \leq k \leq 10^5\)) β€” the number of observations.The next \(k\) lines describe the observations. The \(j\)-th of these lines contains three integers \(d_j\), \(f_j\), \(p_j\) (\(1 \leq d_j \leq 10^8\), \(1 \leq f_j \leq 10^4\), \(1 \leq p_j \leq n\)) β€” the day, the number of fish, and the lake index of the \(j\)-th observation. No two observations happen on the same day at the same lake simultaneously.
Print one integer β€” the smallest total number of fish not contradicting the observations.
In the first example, there could be one fish swimming through lakes \(2\), \(1\), and \(4\), and the second fish swimming through lakes \(3\), \(1\), and \(2\).In the second example a single fish can not possibly be part of all observations simultaneously, but two fish swimming \(2 \to 1 \to 4\) and \(3 \to 1 \to 5\) can.In the third example one fish can move from lake \(1\) to lake \(5\), others staying in their lakes during all time: two fish in lake \(4\), six fish in lake \(5\), one fish in lake \(3\). The system of lakes is shown on the picture.
Input: 41 2 11 3 11 4 151 1 21 1 32 2 13 1 43 1 2 | Output: 2
Master
3
1,130
854
89
10
40
E
40E
E. Number Table
2,500
combinatorics
As it has been found out recently, all the Berland's current economical state can be described using a simple table n Γ— m in size. n β€” the number of days in each Berland month, m β€” the number of months. Thus, a table cell corresponds to a day and a month of the Berland's year. Each cell will contain either 1, or -1, which means the state's gains in a particular month, on a particular day. 1 corresponds to profits, -1 corresponds to losses. It turned out important for successful development to analyze the data on the state of the economy of the previous year, however when the treasurers referred to the archives to retrieve the data, it turned out that the table had been substantially damaged. In some table cells the number values had faded and were impossible to be deciphered. It is known that the number of cells in which the data had been preserved is strictly less than max(n, m). However, there is additional information β€” the product of the numbers in each line and column equaled -1. Your task is to find out how many different tables may conform to the preserved data. As the answer to the task can be quite large, you have to find it modulo p.
The first line contains integers n and m (1 ≀ n, m ≀ 1000). The second line contains the integer k (0 ≀ k < max(n, m)) β€” the number of cells in which the data had been preserved. The next k lines contain the data on the state of the table in the preserved cells. Each line is of the form ""a b c"", where a (1 ≀ a ≀ n) β€” the number of the table row, b (1 ≀ b ≀ m) β€” the number of the column, c β€” the value containing in the cell (1 or -1). They are numbered starting from 1. It is guaranteed that no two lines with same a and b values exist. The last line contains an integer p (2 ≀ p ≀ 109 + 7).
Print the number of different tables that could conform to the preserved data modulo p.
Input: 2 20100 | Output: 2
Expert
1
1,161
596
87
0
989
D
989D
D. A Shade of Moonlight
2,500
binary search; geometry; math; sortings; two pointers
Gathering darkness shrouds the woods and the world. The moon sheds its light on the boat and the river.""To curtain off the moonlight should be hardly possible; the shades present its mellow beauty and restful nature."" Intonates Mino.""See? The clouds are coming."" Kanno gazes into the distance.""That can't be better,"" Mino turns to Kanno. The sky can be seen as a one-dimensional axis. The moon is at the origin whose coordinate is \(0\).There are \(n\) clouds floating in the sky. Each cloud has the same length \(l\). The \(i\)-th initially covers the range of \((x_i, x_i + l)\) (endpoints excluded). Initially, it moves at a velocity of \(v_i\), which equals either \(1\) or \(-1\).Furthermore, no pair of clouds intersect initially, that is, for all \(1 \leq i \lt j \leq n\), \(\lvert x_i - x_j \rvert \geq l\).With a wind velocity of \(w\), the velocity of the \(i\)-th cloud becomes \(v_i + w\). That is, its coordinate increases by \(v_i + w\) during each unit of time. Note that the wind can be strong and clouds can change their direction.You are to help Mino count the number of pairs \((i, j)\) (\(i < j\)), such that with a proper choice of wind velocity \(w\) not exceeding \(w_\mathrm{max}\) in absolute value (possibly negative and/or fractional), the \(i\)-th and \(j\)-th clouds both cover the moon at the same future moment. This \(w\) doesn't need to be the same across different pairs.
The first line contains three space-separated integers \(n\), \(l\), and \(w_\mathrm{max}\) (\(1 \leq n \leq 10^5\), \(1 \leq l, w_\mathrm{max} \leq 10^8\)) β€” the number of clouds, the length of each cloud and the maximum wind speed, respectively.The \(i\)-th of the following \(n\) lines contains two space-separated integers \(x_i\) and \(v_i\) (\(-10^8 \leq x_i \leq 10^8\), \(v_i \in \{-1, 1\}\)) β€” the initial position and the velocity of the \(i\)-th cloud, respectively.The input guarantees that for all \(1 \leq i \lt j \leq n\), \(\lvert x_i - x_j \rvert \geq l\).
Output one integer β€” the number of unordered pairs of clouds such that it's possible that clouds from each pair cover the moon at the same future moment with a proper choice of wind velocity \(w\).
In the first example, the initial positions and velocities of clouds are illustrated below. The pairs are: \((1, 3)\), covering the moon at time \(2.5\) with \(w = -0.4\); \((1, 4)\), covering the moon at time \(3.5\) with \(w = -0.6\); \((1, 5)\), covering the moon at time \(4.5\) with \(w = -0.7\); \((2, 5)\), covering the moon at time \(2.5\) with \(w = -2\). Below is the positions of clouds at time \(2.5\) with \(w = -0.4\). At this moment, the \(1\)-st and \(3\)-rd clouds both cover the moon. In the second example, the only pair is \((1, 4)\), covering the moon at time \(15\) with \(w = 0\).Note that all the times and wind velocities given above are just examples among infinitely many choices.
Input: 5 1 2-2 12 13 -15 -17 -1 | Output: 4
Expert
5
1,412
573
197
9
864
A
864A
A. Fair Game
1,000
implementation; sortings
Petya and Vasya decided to play a game. They have n cards (n is an even number). A single integer is written on each card.Before the game Petya will choose an integer and after that Vasya will choose another integer (different from the number that Petya chose). During the game each player takes all the cards with number he chose. For example, if Petya chose number 5 before the game he will take all cards on which 5 is written and if Vasya chose number 10 before the game he will take all cards on which 10 is written.The game is considered fair if Petya and Vasya can take all n cards, and the number of cards each player gets is the same.Determine whether Petya and Vasya can choose integer numbers before the game so that the game is fair.
The first line contains a single integer n (2 ≀ n ≀ 100) β€” number of cards. It is guaranteed that n is an even number.The following n lines contain a sequence of integers a1, a2, ..., an (one integer per line, 1 ≀ ai ≀ 100) β€” numbers written on the n cards.
If it is impossible for Petya and Vasya to choose numbers in such a way that the game will be fair, print ""NO"" (without quotes) in the first line. In this case you should not print anything more.In the other case print ""YES"" (without quotes) in the first line. In the second line print two distinct integers β€” number that Petya should choose and the number that Vasya should choose to make the game fair. If there are several solutions, print any of them.
In the first example the game will be fair if, for example, Petya chooses number 11, and Vasya chooses number 27. Then the will take all cards β€” Petya will take cards 1 and 4, and Vasya will take cards 2 and 3. Thus, each of them will take exactly two cards.In the second example fair game is impossible because the numbers written on the cards are equal, but the numbers that Petya and Vasya should choose should be distinct.In the third example it is impossible to take all cards. Petya and Vasya can take at most five cards β€” for example, Petya can choose number 10 and Vasya can choose number 20. But for the game to be fair it is necessary to take 6 cards.
Input: 411272711 | Output: YES11 27
Beginner
2
745
257
459
8
477
E
477E
E. Dreamoon and Notepad
3,100
data structures
Dreamoon has just created a document of hard problems using notepad.exe. The document consists of n lines of text, ai denotes the length of the i-th line. He now wants to know what is the fastest way to move the cursor around because the document is really long.Let (r, c) be a current cursor position, where r is row number and c is position of cursor in the row. We have 1 ≀ r ≀ n and 0 ≀ c ≀ ar.We can use following six operations in notepad.exe to move our cursor assuming the current cursor position is at (r, c): up key: the new cursor position (nr, nc) = (max(r - 1, 1), min(anr, c)) down key: the new cursor position (nr, nc) = (min(r + 1, n), min(anr, c)) left key: the new cursor position (nr, nc) = (r, max(0, c - 1)) right key: the new cursor position (nr, nc) = (r, min(anr, c + 1)) HOME key: the new cursor position (nr, nc) = (r, 0) END key: the new cursor position (nr, nc) = (r, ar) You're given the document description (n and sequence ai) and q queries from Dreamoon. Each query asks what minimal number of key presses is needed to move the cursor from (r1, c1) to (r2, c2).
The first line contains an integer n(1 ≀ n ≀ 400, 000) β€” the number of lines of text. The second line contains n integers a1, a2, ..., an(1 ≀ ai ≀ 108).The third line contains an integer q(1 ≀ q ≀ 400, 000). Each of the next q lines contains four integers r1, c1, r2, c2 representing a query (1 ≀ r1, r2 ≀ n, 0 ≀ c1 ≀ ar1, 0 ≀ c2 ≀ ar2).
For each query print the result of the query.
In the first sample, the first query can be solved with keys: HOME, right.The second query can be solved with keys: down, down, down, END, down.The third query can be solved with keys: down, END, down.The fourth query can be solved with keys: END, down.
Input: 91 3 5 3 1 3 5 3 143 5 3 13 3 7 31 0 3 36 0 7 3 | Output: 2532
Master
1
1,093
337
45
4
1,703
A
1703A
A. YES or YES?
800
brute force; implementation; strings
There is a string \(s\) of length \(3\), consisting of uppercase and lowercase English letters. Check if it is equal to ""YES"" (without quotes), where each letter can be in any case. For example, ""yES"", ""Yes"", ""yes"" are all allowable.
The first line of the input contains an integer \(t\) (\(1 \leq t \leq 10^3\)) β€” the number of testcases.The description of each test consists of one line containing one string \(s\) consisting of three characters. Each character of \(s\) is either an uppercase or lowercase English letter.
For each test case, output ""YES"" (without quotes) if \(s\) satisfies the condition, and ""NO"" (without quotes) otherwise.You can output ""YES"" and ""NO"" in any case (for example, strings ""yES"", ""yes"" and ""Yes"" will be recognized as a positive response).
The first five test cases contain the strings ""YES"", ""yES"", ""yes"", ""Yes"", ""YeS"". All of these are equal to ""YES"", where each character is either uppercase or lowercase.
Input: 10YESyESyesYesYeSNooorZyEzYasXES | Output: YES YES YES YES YES NO NO NO NO NO
Beginner
3
241
290
264
17
659
D
659D
D. Bicycle Race
1,500
geometry; implementation; math
Maria participates in a bicycle race.The speedway takes place on the shores of Lake Lucerne, just repeating its contour. As you know, the lake shore consists only of straight sections, directed to the north, south, east or west.Let's introduce a system of coordinates, directing the Ox axis from west to east, and the Oy axis from south to north. As a starting position of the race the southernmost point of the track is selected (and if there are several such points, the most western among them). The participants start the race, moving to the north. At all straight sections of the track, the participants travel in one of the four directions (north, south, east or west) and change the direction of movement only in bends between the straight sections. The participants, of course, never turn back, that is, they do not change the direction of movement from north to south or from east to west (or vice versa).Maria is still young, so she does not feel confident at some turns. Namely, Maria feels insecure if at a failed or untimely turn, she gets into the water. In other words, Maria considers the turn dangerous if she immediately gets into the water if it is ignored.Help Maria get ready for the competition β€” determine the number of dangerous turns on the track.
The first line of the input contains an integer n (4 ≀ n ≀ 1000) β€” the number of straight sections of the track.The following (n + 1)-th line contains pairs of integers (xi, yi) ( - 10 000 ≀ xi, yi ≀ 10 000). The first of these points is the starting position. The i-th straight section of the track begins at the point (xi, yi) and ends at the point (xi + 1, yi + 1).It is guaranteed that: the first straight section is directed to the north; the southernmost (and if there are several, then the most western of among them) point of the track is the first point; the last point coincides with the first one (i.e., the start position); any pair of straight sections of the track has no shared points (except for the neighboring ones, they share exactly one point); no pair of points (except for the first and last one) is the same; no two adjacent straight sections are directed in the same direction or in opposite directions.
Print a single integer β€” the number of dangerous turns on the track.
The first sample corresponds to the picture: The picture shows that you can get in the water under unfortunate circumstances only at turn at the point (1, 1). Thus, the answer is 1.
Input: 60 00 11 11 22 22 00 0 | Output: 1
Medium
3
1,272
927
68
6
542
D
542D
D. Superhero's Job
2,600
dfs and similar; dp; hashing; math; number theory
It's tough to be a superhero. And it's twice as tough to resist the supervillain who is cool at math. Suppose that you're an ordinary Batman in an ordinary city of Gotham. Your enemy Joker mined the building of the city administration and you only have several minutes to neutralize the charge. To do that you should enter the cancel code on the bomb control panel.However, that mad man decided to give you a hint. This morning the mayor found a playing card under his pillow. There was a line written on the card:The bomb has a note saying ""J(x) = A"", where A is some positive integer. You suspect that the cancel code is some integer x that meets the equation J(x) = A. Now in order to decide whether you should neutralize the bomb or run for your life, you've got to count how many distinct positive integers x meet this equation.
The single line of the input contains a single integer A (1 ≀ A ≀ 1012).
Print the number of solutions of the equation J(x) = A.
Record x|n means that number n divides number x. is defined as the largest positive integer that divides both a and b.In the first sample test the only suitable value of x is 2. Then J(2) = 1 + 2.In the second sample test the following values of x match: x = 14, J(14) = 1 + 2 + 7 + 14 = 24 x = 15, J(15) = 1 + 3 + 5 + 15 = 24 x = 23, J(23) = 1 + 23 = 24
Input: 3 | Output: 1
Expert
5
835
72
55
5
776
G
776G
G. Sherlock and the Encrypted Data
2,900
bitmasks; combinatorics; dp
Sherlock found a piece of encrypted data which he thinks will be useful to catch Moriarty. The encrypted data consists of two integer l and r. He noticed that these integers were in hexadecimal form.He takes each of the integers from l to r, and performs the following operations: He lists the distinct digits present in the given number. For example: for 101416, he lists the digits as 1, 0, 4. Then he sums respective powers of two for each digit listed in the step above. Like in the above example sum = 21 + 20 + 24 = 1910. He changes the initial number by applying bitwise xor of the initial number and the sum. Example: . Note that xor is done in binary notation. One more example: for integer 1e the sum is sum = 21 + 214. Letters a, b, c, d, e, f denote hexadecimal digits 10, 11, 12, 13, 14, 15, respertively.Sherlock wants to count the numbers in the range from l to r (both inclusive) which decrease on application of the above four steps. He wants you to answer his q queries for different l and r.
First line contains the integer q (1 ≀ q ≀ 10000).Each of the next q lines contain two hexadecimal integers l and r (0 ≀ l ≀ r < 1615).The hexadecimal integers are written using digits from 0 to 9 and/or lowercase English letters a, b, c, d, e, f.The hexadecimal integers do not contain extra leading zeros.
Output q lines, i-th line contains answer to the i-th query (in decimal notation).
For the second input,1416 = 2010sum = 21 + 24 = 18 Thus, it reduces. And, we can verify that it is the only number in range 1 to 1e that reduces.
Input: 11014 1014 | Output: 1
Master
3
1,010
307
82
7
109
B
109B
B. Lucky Probability
1,900
brute force; probabilities
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 and his friend Vasya play an interesting game. Petya randomly chooses an integer p from the interval [pl, pr] and Vasya chooses an integer v from the interval [vl, vr] (also randomly). Both players choose their integers equiprobably. Find the probability that the interval [min(v, p), max(v, p)] contains exactly k lucky numbers.
The single line contains five integers pl, pr, vl, vr and k (1 ≀ pl ≀ pr ≀ 109, 1 ≀ vl ≀ vr ≀ 109, 1 ≀ k ≀ 1000).
On the single line print the result with an absolute error of no more than 10 - 9.
Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, In first case there are 32 suitable pairs: (1, 7), (1, 8), (1, 9), (1, 10), (2, 7), (2, 8), (2, 9), (2, 10), (3, 7), (3, 8), (3, 9), (3, 10), (4, 7), (4, 8), (4, 9), (4, 10), (7, 1), (7, 2), (7, 3), (7, 4), (8, 1), (8, 2), (8, 3), (8, 4), (9, 1), (9, 2), (9, 3), (9, 4), (10, 1), (10, 2), (10, 3), (10, 4). Total number of possible pairs is 10Β·10 = 100, so answer is 32 / 100.In second case Petya always get number less than Vasya and the only lucky 7 is between this numbers, so there will be always 1 lucky number.
Input: 1 10 1 10 2 | Output: 0.320000000000
Hard
2
553
113
82
1
32
D
32D
D. Constellation
1,600
implementation
A star map in Berland is a checked field n Γ— m squares. In each square there is or there is not a star. The favourite constellation of all Berland's astronomers is the constellation of the Cross. This constellation can be formed by any 5 stars so, that for some integer x (radius of the constellation) the following is true: the 2nd is on the same vertical line as the 1st, but x squares up the 3rd is on the same vertical line as the 1st, but x squares down the 4th is on the same horizontal line as the 1st, but x squares left the 5th is on the same horizontal line as the 1st, but x squares right Such constellations can be very numerous, that's why they are numbered with integers from 1 on the following principle: when two constellations are compared, the one with a smaller radius gets a smaller index; if their radii are equal β€” the one, whose central star if higher than the central star of the other one; if their central stars are at the same level β€” the one, whose central star is to the left of the central star of the other one.Your task is to find the constellation with index k by the given Berland's star map.
The first line contains three integers n, m and k (1 ≀ n, m ≀ 300, 1 ≀ k ≀ 3Β·107) β€” height and width of the map and index of the required constellation respectively. The upper-left corner has coordinates (1, 1), and the lower-right β€” (n, m). Then there follow n lines, m characters each β€” description of the map. j-th character in i-th line is Β«*Β», if there is a star in the corresponding square, and Β«.Β» if this square is empty.
If the number of the constellations is less than k, output -1. Otherwise output 5 lines, two integers each β€” coordinates of the required constellation. Output the stars in the following order: central, upper, lower, left, right.
Input: 5 6 1....*....***....*...*....***.. | Output: 2 51 53 52 42 6
Medium
1
1,126
429
228
0
954
I
954I
I. Yet Another String Matching Problem
2,200
fft; math
Suppose you have two strings s and t, and their length is equal. You may perform the following operation any number of times: choose two different characters c1 and c2, and replace every occurence of c1 in both strings with c2. Let's denote the distance between strings s and t as the minimum number of operations required to make these strings equal. For example, if s is abcd and t is ddcb, the distance between them is 2 β€” we may replace every occurence of a with b, so s becomes bbcd, and then we may replace every occurence of b with d, so both strings become ddcd.You are given two strings S and T. For every substring of S consisting of |T| characters you have to determine the distance between this substring and T.
The first line contains the string S, and the second β€” the string T (1 ≀ |T| ≀ |S| ≀ 125000). Both strings consist of lowercase Latin letters from a to f.
Print |S| - |T| + 1 integers. The i-th of these integers must be equal to the distance between the substring of S beginning at i-th index with length |T| and the string T.
Input: abcdefaddcb | Output: 2 3 3 3
Hard
2
723
154
171
9
1,534
A
1534A
A. Colour the Flag
800
brute force; implementation
Today we will be playing a red and white colouring game (no, this is not the Russian Civil War; these are just the colours of the Canadian flag).You are given an \(n \times m\) grid of ""R"", ""W"", and ""."" characters. ""R"" is red, ""W"" is white and ""."" is blank. The neighbours of a cell are those that share an edge with it (those that only share a corner do not count).Your job is to colour the blank cells red or white so that every red cell only has white neighbours (and no red ones) and every white cell only has red neighbours (and no white ones). You are not allowed to recolour already coloured cells.
The first line contains \(t\) (\(1 \le t \le 100\)), the number of test cases.In each test case, the first line will contain \(n\) (\(1 \le n \le 50\)) and \(m\) (\(1 \le m \le 50\)), the height and width of the grid respectively.The next \(n\) lines will contain the grid. Each character of the grid is either 'R', 'W', or '.'.
For each test case, output ""YES"" if there is a valid grid or ""NO"" if there is not.If there is, output the grid on the next \(n\) lines. If there are multiple answers, print any.In the output, the ""YES""s and ""NO""s are case-insensitive, meaning that outputs such as ""yEs"" and ""nO"" are valid. However, the grid is case-sensitive.
The answer for the first example case is given in the example output, and it can be proven that no grid exists that satisfies the requirements of the second example case. In the third example all cells are initially coloured, and the colouring is valid.
Input: 3 4 6 .R.... ...... ...... .W.... 4 4 .R.W .... .... .... 5 1 R W R W R | Output: YES WRWRWR RWRWRW WRWRWR RWRWRW NO YES R W R W R
Beginner
2
617
328
338
15
1,389
A
1389A
A. LCM Problem
800
constructive algorithms; greedy; math; number theory
Let \(LCM(x, y)\) be the minimum positive integer that is divisible by both \(x\) and \(y\). For example, \(LCM(13, 37) = 481\), \(LCM(9, 6) = 18\).You are given two integers \(l\) and \(r\). Find two integers \(x\) and \(y\) such that \(l \le x < y \le r\) and \(l \le LCM(x, y) \le r\).
The first line contains one integer \(t\) (\(1 \le t \le 10000\)) β€” the number of test cases.Each test case is represented by one line containing two integers \(l\) and \(r\) (\(1 \le l < r \le 10^9\)).
For each test case, print two integers: if it is impossible to find integers \(x\) and \(y\) meeting the constraints in the statement, print two integers equal to \(-1\); otherwise, print the values of \(x\) and \(y\) (if there are multiple valid answers, you may print any of them).
Input: 4 1 1337 13 69 2 4 88 89 | Output: 6 7 14 21 2 4 -1 -1
Beginner
4
288
202
283
13
1,028
E
1028E
E. Restore Array
2,400
constructive algorithms
While discussing a proper problem A for a Codeforces Round, Kostya created a cyclic array of positive integers \(a_1, a_2, \ldots, a_n\). Since the talk was long and not promising, Kostya created a new cyclic array \(b_1, b_2, \ldots, b_{n}\) so that \(b_i = (a_i \mod a_{i + 1})\), where we take \(a_{n+1} = a_1\). Here \(mod\) is the modulo operation. When the talk became interesting, Kostya completely forgot how array \(a\) had looked like. Suddenly, he thought that restoring array \(a\) from array \(b\) would be an interesting problem (unfortunately, not A).
The first line contains a single integer \(n\) (\(2 \le n \le 140582\)) β€” the length of the array \(a\).The second line contains \(n\) integers \(b_1, b_2, \ldots, b_{n}\) (\(0 \le b_i \le 187126\)).
If it is possible to restore some array \(a\) of length \(n\) so that \(b_i = a_i \mod a_{(i \mod n) + 1}\) holds for all \(i = 1, 2, \ldots, n\), print Β«YESΒ» in the first line and the integers \(a_1, a_2, \ldots, a_n\) in the second line. All \(a_i\) should satisfy \(1 \le a_i \le 10^{18}\). We can show that if an answer exists, then an answer with such constraint exists as well.It it impossible to restore any valid \(a\), print Β«NOΒ» in one line.You can print each letter in any case (upper or lower).
In the first example: \(1 \mod 3 = 1\) \(3 \mod 5 = 3\) \(5 \mod 2 = 1\) \(2 \mod 1 = 0\)
Input: 41 3 1 0 | Output: YES1 3 5 2
Expert
1
566
199
506
10
482
B
482B
B. Interesting Array
1,800
constructive algorithms; data structures; trees
We'll call an array of n non-negative integers a[1], a[2], ..., a[n] interesting, if it meets m constraints. The i-th of the m constraints consists of three integers li, ri, qi (1 ≀ li ≀ ri ≀ n) meaning that value should be equal to qi. Your task is to find any interesting array of n elements or state that such array doesn't exist.Expression x&y means the bitwise AND of numbers x and y. In programming languages C++, Java and Python this operation is represented as ""&"", in Pascal β€” as ""and"".
The first line contains two integers n, m (1 ≀ n ≀ 105, 1 ≀ m ≀ 105) β€” the number of elements in the array and the number of limits.Each of the next m lines contains three integers li, ri, qi (1 ≀ li ≀ ri ≀ n, 0 ≀ qi < 230) describing the i-th limit.
If the interesting array exists, in the first line print ""YES"" (without the quotes) and in the second line print n integers a[1], a[2], ..., a[n] (0 ≀ a[i] < 230) decribing the interesting array. If there are multiple answers, print any of them.If the interesting array doesn't exist, print ""NO"" (without the quotes) in the single line.
Input: 3 11 3 3 | Output: YES3 3 3
Medium
3
499
250
340
4
1,043
F
1043F
F. Make It One
2,500
bitmasks; combinatorics; dp; math; number theory; shortest paths
Janusz is a businessman. He owns a company ""Januszex"", which produces games for teenagers. Last hit of Januszex was a cool one-person game ""Make it one"". The player is given a sequence of \(n\) integers \(a_i\).It is allowed to select any subset of them, and the score is equal to the greatest common divisor of selected elements. The goal is to take as little elements as it is possible, getting the score \(1\). Now Janusz wonders, for given sequence, how much elements should the player choose?
The first line contains an only integer \(n\) (\(1 \le n \le 300\,000\)) β€” the number of integers in the sequence.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 300\,000\)).
If there is no subset of the given sequence with gcd equal to \(1\), output -1.Otherwise, output exactly one integer β€” the size of the smallest subset with gcd equal to \(1\).
In the first example, selecting a subset of all numbers gives a gcd of \(1\) and for all smaller subsets the gcd is greater than \(1\).In the second example, for all subsets of numbers the gcd is at least \(2\).
Input: 310 6 15 | Output: 3
Expert
6
501
209
175
10
519
B
519B
B. A and B and Compilation Errors
1,100
data structures; implementation; sortings
A and B are preparing themselves for programming contests.B loves to debug his code. But before he runs the solution and starts debugging, he has to first compile the code.Initially, the compiler displayed n compilation errors, each of them is represented as a positive integer. After some effort, B managed to fix some mistake and then another one mistake.However, despite the fact that B is sure that he corrected the two errors, he can not understand exactly what compilation errors disappeared β€” the compiler of the language which B uses shows errors in the new order every time! B is sure that unlike many other programming languages, compilation errors for his programming language do not depend on each other, that is, if you correct one error, the set of other error does not change.Can you help B find out exactly what two errors he corrected?
The first line of the input contains integer n (3 ≀ n ≀ 105) β€” the initial number of compilation errors.The second line contains n space-separated integers a1, a2, ..., an (1 ≀ ai ≀ 109) β€” the errors the compiler displayed for the first time. The third line contains n - 1 space-separated integers b1, b2, ..., bn - 1 β€” the errors displayed at the second compilation. It is guaranteed that the sequence in the third line contains all numbers of the second string except for exactly one. The fourth line contains n - 2 space-separated integers с1, с2, ..., сn - 2 β€” the errors displayed at the third compilation. It is guaranteed that the sequence in the fourth line contains all numbers of the third line except for exactly one.
Print two numbers on a single line: the numbers of the compilation errors that disappeared after B made the first and the second correction, respectively.
In the first test sample B first corrects the error number 8, then the error number 123.In the second test sample B first corrects the error number 1, then the error number 3. Note that if there are multiple errors with the same number, B can correct only one of them in one step.
Input: 51 5 8 123 7123 7 5 15 1 7 | Output: 8123
Easy
3
852
728
154
5
873
C
873C
C. Strange Game On Matrix
1,600
greedy; two pointers
Ivan is playing a strange game.He has a matrix a with n rows and m columns. Each element of the matrix is equal to either 0 or 1. Rows and columns are 1-indexed. Ivan can replace any number of ones in this matrix with zeroes. After that, his score in the game will be calculated as follows: Initially Ivan's score is 0; In each column, Ivan will find the topmost 1 (that is, if the current column is j, then he will find minimum i such that ai, j = 1). If there are no 1's in the column, this column is skipped; Ivan will look at the next min(k, n - i + 1) elements in this column (starting from the element he found) and count the number of 1's among these elements. This number will be added to his score. Of course, Ivan wants to maximize his score in this strange game. Also he doesn't want to change many elements, so he will replace the minimum possible number of ones with zeroes. Help him to determine the maximum possible score he can get and the minimum possible number of replacements required to achieve that score.
The first line contains three integer numbers n, m and k (1 ≀ k ≀ n ≀ 100, 1 ≀ m ≀ 100).Then n lines follow, i-th of them contains m integer numbers β€” the elements of i-th row of matrix a. Each number is either 0 or 1.
Print two numbers: the maximum possible score Ivan can get and the minimum number of replacements required to get this score.
In the first example Ivan will replace the element a1, 2.
Input: 4 3 20 1 01 0 10 1 01 1 1 | Output: 4 1
Medium
2
1,027
218
125
8
773
B
773B
B. Dynamic Problem Scoring
2,000
brute force; greedy
Vasya and Petya take part in a Codeforces round. The round lasts for two hours and contains five problems.For this round the dynamic problem scoring is used. If you were lucky not to participate in any Codeforces round with dynamic problem scoring, here is what it means. The maximum point value of the problem depends on the ratio of the number of participants who solved the problem to the total number of round participants. Everyone who made at least one submission is considered to be participating in the round.Pay attention to the range bounds. For example, if 40 people are taking part in the round, and 10 of them solve a particular problem, then the solvers fraction is equal to 1 / 4, and the problem's maximum point value is equal to 1500.If the problem's maximum point value is equal to x, then for each whole minute passed from the beginning of the contest to the moment of the participant's correct submission, the participant loses x / 250 points. For example, if the problem's maximum point value is 2000, and the participant submits a correct solution to it 40 minutes into the round, this participant will be awarded with 2000Β·(1 - 40 / 250) = 1680 points for this problem.There are n participants in the round, including Vasya and Petya. For each participant and each problem, the number of minutes which passed between the beginning of the contest and the submission of this participant to this problem is known. It's also possible that this participant made no submissions to this problem.With two seconds until the end of the round, all participants' submissions have passed pretests, and not a single hack attempt has been made. Vasya believes that no more submissions or hack attempts will be made in the remaining two seconds, and every submission will pass the system testing.Unfortunately, Vasya is a cheater. He has registered 109 + 7 new accounts for the round. Now Vasya can submit any of his solutions from these new accounts in order to change the maximum point values of the problems. Vasya can also submit any wrong solutions to any problems. Note that Vasya can not submit correct solutions to the problems he hasn't solved.Vasya seeks to score strictly more points than Petya in the current round. Vasya has already prepared the scripts which allow to obfuscate his solutions and submit them into the system from any of the new accounts in just fractions of seconds. However, Vasya doesn't want to make his cheating too obvious, so he wants to achieve his goal while making submissions from the smallest possible number of new accounts.Find the smallest number of new accounts Vasya needs in order to beat Petya (provided that Vasya's assumptions are correct), or report that Vasya can't achieve his goal.
The first line contains a single integer n (2 ≀ n ≀ 120) β€” the number of round participants, including Vasya and Petya.Each of the next n lines contains five integers ai, 1, ai, 2..., ai, 5 ( - 1 ≀ ai, j ≀ 119) β€” the number of minutes passed between the beginning of the round and the submission of problem j by participant i, or -1 if participant i hasn't solved problem j.It is guaranteed that each participant has made at least one successful submission.Vasya is listed as participant number 1, Petya is listed as participant number 2, all the other participants are listed in no particular order.
Output a single integer β€” the number of new accounts Vasya needs to beat Petya, or -1 if Vasya can't achieve his goal.
In the first example, Vasya's optimal strategy is to submit the solutions to the last three problems from two new accounts. In this case the first two problems will have the maximum point value of 1000, while the last three problems will have the maximum point value of 500. Vasya's score will be equal to 980 + 940 + 420 + 360 + 270 = 2970 points, while Petya will score just 800 + 820 + 420 + 440 + 470 = 2950 points.In the second example, Vasya has to make a single unsuccessful submission to any problem from two new accounts, and a single successful submission to the first problem from the third new account. In this case, the maximum point values of the problems will be equal to 500, 1500, 1000, 1500, 3000. Vasya will score 2370 points, while Petya will score just 2294 points.In the third example, Vasya can achieve his goal by submitting the solutions to the first four problems from 27 new accounts. The maximum point values of the problems will be equal to 500, 500, 500, 500, 2000. Thanks to the high cost of the fifth problem, Vasya will manage to beat Petya who solved the first four problems very quickly, but couldn't solve the fifth one.
Input: 25 15 40 70 11550 45 40 30 15 | Output: 2
Hard
2
2,742
600
118
7
1,070
L
1070L
L. Odd Federalization
2,600
constructive algorithms
Berland has \(n\) cities, some of which are connected by roads. Each road is bidirectional, connects two distinct cities and for each two cities there's at most one road connecting them.The president of Berland decided to split country into \(r\) states in such a way that each city will belong to exactly one of these \(r\) states.After this split each road will connect either cities of the same state or cities of the different states. Let's call roads that connect two cities of the same state ""inner"" roads.The president doesn't like odd people, odd cities and odd numbers, so he wants this split to be done in such a way that each city would have even number of ""inner"" roads connected to it.Please help president to find smallest possible \(r\) for which such a split exists.
The input contains one or several test cases. The first input line contains a single integer number \(t\) β€” number of test cases. Then, \(t\) test cases follow. Solve test cases separately, test cases are completely independent and do not affect each other.Then \(t\) blocks of input data follow. Each block starts from empty line which separates it from the remaining input data. The second line of each block contains two space-separated integers \(n\), \(m\) (\(1 \le n \le 2000\), \(0 \le m \le 10000\)) β€” the number of cities and number of roads in the Berland. Each of the next \(m\) lines contains two space-separated integers β€” \(x_i\), \(y_i\) (\(1 \le x_i, y_i \le n\); \(x_i \ne y_i\)), which denotes that the \(i\)-th road connects cities \(x_i\) and \(y_i\). Each pair of cities are connected by at most one road. Sum of values \(n\) across all test cases doesn't exceed \(2000\). Sum of values \(m\) across all test cases doesn't exceed \(10000\).
For each test case first print a line containing a single integer \(r\) β€” smallest possible number of states for which required split is possible. In the next line print \(n\) space-separated integers in range from \(1\) to \(r\), inclusive, where the \(j\)-th number denotes number of state for the \(j\)-th city. If there are multiple solutions, print any.
Input: 2 5 31 22 51 5 6 51 22 33 44 24 1 | Output: 11 1 1 1 1 22 1 1 1 1 1
Expert
1
786
961
358
10
558
A
558A
A. Lala Land and Apple Trees
1,100
brute force; implementation; sortings
Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees growing everywhere.Lala Land has exactly n apple trees. Tree number i is located in a position xi and has ai apples growing on it. Amr wants to collect apples from the apple trees. Amr currently stands in x = 0 position. At the beginning, he can choose whether to go right or left. He'll continue in his direction until he meets an apple tree he didn't visit before. He'll take all of its apples and then reverse his direction, continue walking in this direction until he meets another apple tree he didn't visit before and so on. In the other words, Amr reverses his direction when visiting each new apple tree. Amr will stop collecting apples when there are no more trees he didn't visit in the direction he is facing.What is the maximum number of apples he can collect?
The first line contains one number n (1 ≀ n ≀ 100), the number of apple trees in Lala Land.The following n lines contains two integers each xi, ai ( - 105 ≀ xi ≀ 105, xi β‰  0, 1 ≀ ai ≀ 105), representing the position of the i-th tree and number of apples on it.It's guaranteed that there is at most one apple tree at each coordinate. It's guaranteed that no tree grows in point 0.
Output the maximum number of apples Amr can collect.
In the first sample test it doesn't matter if Amr chose at first to go left or right. In both cases he'll get all the apples.In the second sample test the optimal solution is to go left to x = - 1, collect apples from there, then the direction will be reversed, Amr has to go to x = 1, collect apples from there, then the direction will be reversed and Amr goes to the final tree x = - 2.In the third sample test the optimal solution is to go right to x = 1, collect apples from there, then the direction will be reversed and Amr will not be able to collect anymore apples because there are no apple trees to his left.
Input: 2-1 51 5 | Output: 10
Easy
3
913
379
52
5
1,943
D1
1943D1
D1. Counting Is Fun (Easy Version)
2,400
brute force; combinatorics; dp; math
This is the easy version of the problem. The only difference between the two versions is the constraint on \(n\). You can make hacks only if both versions of the problem are solved.An array \(b\) of \(m\) non-negative integers is said to be good if all the elements of \(b\) can be made equal to \(0\) using the following operation some (possibly, zero) times: Select two distinct indices \(l\) and \(r\) (\(1 \leq l \color{red}{<} r \leq m\)) and subtract \(1\) from all \(b_i\) such that \(l \leq i \leq r\). You are given two positive integers \(n\), \(k\) and a prime number \(p\).Over all \((k+1)^n\) arrays of length \(n\) such that \(0 \leq a_i \leq k\) for all \(1 \leq i \leq n\), count the number of good arrays.Since the number might be too large, you are only required to find it modulo \(p\).
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^3\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains three positive integers \(n\), \(k\) and \(p\) (\(3 \leq n \leq 400\), \(1 \leq k \leq n\), \(10^8 < p < 10^9\)) β€” the length of the array \(a\), the upper bound on the elements of \(a\) and modulus \(p\).It is guaranteed that the sum of \(n^2\) over all test cases does not exceed \(2 \cdot 10^5\), and \(p\) is prime.
For each test case, on a new line, output the number of good arrays modulo \(p\).
In the first test case, the \(4\) good arrays \(a\) are: \([0,0,0]\); \([0,1,1]\); \([1,1,0]\); \([1,1,1]\).
Input: 43 1 9982448534 1 9982443533 2 998244353343 343 998244353 | Output: 4 7 10 456615865
Expert
4
805
543
81
19
818
A
818A
A. Diplomas and Certificates
800
implementation; math
There are n students who have taken part in an olympiad. Now it's time to award the students.Some of them will receive diplomas, some wiil get certificates, and others won't receive anything. Students with diplomas and certificates are called winners. But there are some rules of counting the number of diplomas and certificates. The number of certificates must be exactly k times greater than the number of diplomas. The number of winners must not be greater than half of the number of all students (i.e. not be greater than half of n). It's possible that there are no winners.You have to identify the maximum possible number of winners, according to these rules. Also for this case you have to calculate the number of students with diplomas, the number of students with certificates and the number of students who are not winners.
The first (and the only) line of input contains two integers n and k (1 ≀ n, k ≀ 1012), where n is the number of students and k is the ratio between the number of certificates and the number of diplomas.
Output three numbers: the number of students with diplomas, the number of students with certificates and the number of students who are not winners in case when the number of winners is maximum possible.It's possible that there are no winners.
Input: 18 2 | Output: 3 6 9
Beginner
2
832
203
243
8
1,970
B1
1970B1
B1. Exact Neighbours (Easy)
1,900
constructive algorithms
The only difference between this and the hard version is that all \(a_{i}\) are even.After some recent attacks on Hogwarts Castle by the Death Eaters, the Order of the Phoenix has decided to station \(n\) members in Hogsmead Village. The houses will be situated on a picturesque \(n\times n\) square field. Each wizard will have their own house, and every house will belong to some wizard. Each house will take up the space of one square.However, as you might know wizards are very superstitious. During the weekends, each wizard \(i\) will want to visit the house that is exactly \(a_{i}\) \((0 \leq a_{i} \leq n)\) away from their own house. The roads in the village are built horizontally and vertically, so the distance between points \((x_{i}, y_{i})\) and \((x_{j}, y_{j})\) on the \(n\times n\) field is \( |x_{i} - x_{j}| + |y_{i} - y_{j}|\). The wizards know and trust each other, so one wizard can visit another wizard's house when the second wizard is away. The houses to be built will be big enough for all \(n\) wizards to simultaneously visit any house.Apart from that, each wizard is mandated to have a view of the Hogwarts Castle in the north and the Forbidden Forest in the south, so the house of no other wizard should block the view. In terms of the village, it means that in each column of the \(n\times n\) field, there can be at most one house, i.e. if the \(i\)-th house has coordinates \((x_{i}, y_{i})\), then \(x_{i} \neq x_{j}\) for all \(i \neq j\).The Order of the Phoenix doesn't yet know if it is possible to place \(n\) houses in such a way that will satisfy the visit and view requirements of all \(n\) wizards, so they are asking for your help in designing such a plan.If it is possible to have a correct placement, where for the \(i\)-th wizard there is a house that is \(a_{i}\) away from it and the house of the \(i\)-th wizard is the only house in their column, output YES, the position of houses for each wizard, and to the house of which wizard should each wizard go during the weekends.If it is impossible to have a correct placement, output NO.
The first line contains \(n\) (\(2 \leq n \leq 2\cdot 10^{5}\)), the number of houses to be built.The second line contains \(n\) integers \(a_{1}, \ldots, a_{n}\) \((0 \leq a_{i} \leq n)\). All \(a_{i}\) are even.
If there exists such a placement, output YES on the first line; otherwise, output NO.If the answer is YES, output \(n + 1\) more lines describing the placement.The next \(n\) lines should contain the positions of the houses \(1 \leq x_{i}, y_{i} \leq n\) for each wizard.The \(i\)-th element of the last line should contain the index of the wizard, the house of which is exactly \(a_{i}\) away from the house of the \(i\)-th wizard. If there are multiple such wizards, you can output any.If there are multiple house placement configurations, you can output any.
For the sample, the house of the 1st wizard is located at \((4, 4)\), of the 2nd at \((1, 3)\), of the 3rd at \((2, 4)\), of the 4th at \((3, 1)\).The distance from the house of the 1st wizard to the house of the 1st wizard is \(|4 - 4| + |4 - 4| = 0\).The distance from the house of the 2nd wizard to the house of the 1st wizard is \(|1 - 4| + |3 - 4| = 4\).The distance from the house of the 3rd wizard to the house of the 1st wizard is \(|2 - 4| + |4 - 4| = 2\).The distance from the house of the 4th wizard to the house of the 3rd wizard is \(|3 - 2| + |1 - 4| = 4\).The view and the distance conditions are satisfied for all houses, so the placement is correct.
Input: 4 0 4 2 4 | Output: YES 4 4 1 3 2 4 3 1 1 1 1 3
Hard
1
2,086
213
561
19
1,438
B
1438B
B. Valerii Against Everyone
1,000
constructive algorithms; data structures; greedy; sortings
You're given an array \(b\) of length \(n\). Let's define another array \(a\), also of length \(n\), for which \(a_i = 2^{b_i}\) (\(1 \leq i \leq n\)). Valerii says that every two non-intersecting subarrays of \(a\) have different sums of elements. You want to determine if he is wrong. More formally, you need to determine if there exist four integers \(l_1,r_1,l_2,r_2\) that satisfy the following conditions: \(1 \leq l_1 \leq r_1 \lt l_2 \leq r_2 \leq n\); \(a_{l_1}+a_{l_1+1}+\ldots+a_{r_1-1}+a_{r_1} = a_{l_2}+a_{l_2+1}+\ldots+a_{r_2-1}+a_{r_2}\). If such four integers exist, you will prove Valerii wrong. Do they exist?An array \(c\) is a subarray of an array \(d\) if \(c\) can be obtained from \(d\) by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of every test case contains a single integer \(n\) (\(2 \le n \le 1000\)).The second line of every test case contains \(n\) integers \(b_1,b_2,\ldots,b_n\) (\(0 \le b_i \le 10^9\)).
For every test case, if there exist two non-intersecting subarrays in \(a\) that have the same sum, output YES on a separate line. Otherwise, output NO on a separate line. Also, note that each letter can be in any case.
In the first case, \(a = [16,8,1,2,4,1]\). Choosing \(l_1 = 1\), \(r_1 = 1\), \(l_2 = 2\) and \(r_2 = 6\) works because \(16 = (8+1+2+4+1)\).In the second case, you can verify that there is no way to select to such subarrays.
Input: 2 6 4 3 0 1 2 0 2 2 5 | Output: YES NO
Beginner
4
843
352
219
14
630
D
630D
D. Hexagons!
1,100
math
After a probationary period in the game development company of IT City Petya was included in a group of the programmers that develops a new turn-based strategy game resembling the well known ""Heroes of Might & Magic"". A part of the game is turn-based fights of big squadrons of enemies on infinite fields where every cell is in form of a hexagon.Some of magic effects are able to affect several field cells at once, cells that are situated not farther than n cells away from the cell in which the effect was applied. The distance between cells is the minimum number of cell border crosses on a path from one cell to another.It is easy to see that the number of cells affected by a magic effect grows rapidly when n increases, so it can adversely affect the game performance. That's why Petya decided to write a program that can, given n, determine the number of cells that should be repainted after effect application, so that game designers can balance scale of the effects and the game performance. Help him to do it. Find the number of hexagons situated not farther than n cells away from a given cell.
The only line of the input contains one integer n (0 ≀ n ≀ 109).
Output one integer β€” the number of hexagons situated not farther than n cells away from a given cell.
Input: 2 | Output: 19
Easy
1
1,107
64
101
6
1,795
A
1795A
A. Two Towers
800
brute force; implementation; strings
There are two towers consisting of blocks of two colors: red and blue. Both towers are represented by strings of characters B and/or R denoting the order of blocks in them from the bottom to the top, where B corresponds to a blue block, and R corresponds to a red block. These two towers are represented by strings BRBB and RBR. You can perform the following operation any number of times: choose a tower with at least two blocks, and move its top block to the top of the other tower. The pair of towers is beautiful if no pair of touching blocks has the same color; i. e. no red block stands on top of another red block, and no blue block stands on top of another blue block.You have to check if it is possible to perform any number of operations (possibly zero) to make the pair of towers beautiful.
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases.Each test case consists of three lines: the first line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 20\)) β€” the number of blocks in the first tower and the number of blocks in the second tower, respectively; the second line contains \(s\) β€” a string of exactly \(n\) characters B and/or R, denoting the first tower; the third line contains \(t\) β€” a string of exactly \(m\) characters B and/or R, denoting the second tower.
For each test case, print YES if it is possible to perform several (possibly zero) operations in such a way that the pair of towers becomes beautiful; otherwise print NO.You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
In the first test case, you can move the top block from the first tower to the second tower (see the third picture).In the second test case, you can move the top block from the second tower to the first tower \(6\) times.In the third test case, the pair of towers is already beautiful.
Input: 44 3BRBBRBR4 7BRBRRRBRBRB3 4RBRBRBR5 4BRBRRBRBR | Output: YES YES YES NO
Beginner
3
801
526
323
17
459
A
459A
A. Pashmak and Garden
1,200
implementation
Pashmak has fallen in love with an attractive girl called Parmida since one year ago...Today, Pashmak set up a meeting with his partner in a romantic garden. Unfortunately, Pashmak has forgotten where the garden is. But he remembers that the garden looks like a square with sides parallel to the coordinate axes. He also remembers that there is exactly one tree on each vertex of the square. Now, Pashmak knows the position of only two of the trees. Help him to find the position of two remaining ones.
The first line contains four space-separated x1, y1, x2, y2 ( - 100 ≀ x1, y1, x2, y2 ≀ 100) integers, where x1 and y1 are coordinates of the first tree and x2 and y2 are coordinates of the second tree. It's guaranteed that the given points are distinct.
If there is no solution to the problem, print -1. Otherwise print four space-separated integers x3, y3, x4, y4 that correspond to the coordinates of the two other trees. If there are several solutions you can output any of them. Note that x3, y3, x4, y4 must be in the range ( - 1000 ≀ x3, y3, x4, y4 ≀ 1000).
Input: 0 0 0 1 | Output: 1 0 1 1
Easy
1
502
253
309
4
960
A
960A
A. Check the string
1,200
implementation
A has a string consisting of some number of lowercase English letters 'a'. He gives it to his friend B who appends some number of letters 'b' to the end of this string. Since both A and B like the characters 'a' and 'b', they have made sure that at this point, at least one 'a' and one 'b' exist in the string.B now gives this string to C and he appends some number of letters 'c' to the end of the string. However, since C is a good friend of A and B, the number of letters 'c' he appends is equal to the number of 'a' or to the number of 'b' in the string. It is also possible that the number of letters 'c' equals both to the number of letters 'a' and to the number of letters 'b' at the same time.You have a string in your hands, and you want to check if it is possible to obtain the string in this way or not. If it is possible to obtain the string, print ""YES"", otherwise print ""NO"" (without the quotes).
The first and only line consists of a string \(S\) (\( 1 \le |S| \le 5\,000 \)). It is guaranteed that the string will only consist of the lowercase English letters 'a', 'b', 'c'.
Print ""YES"" or ""NO"", according to the condition.
Consider first example: the number of 'c' is equal to the number of 'a'. Consider second example: although the number of 'c' is equal to the number of the 'b', the order is not correct.Consider third example: the number of 'c' is equal to the number of 'b'.
Input: aaabccc | Output: YES
Easy
1
914
179
52
9
1,735
F
1735F
F. Pebbles and Beads
2,900
data structures; geometry
There are two currencies: pebbles and beads. Initially you have \(a\) pebbles, \(b\) beads.There are \(n\) days, each day you can exchange one currency for another at some exchange rate.On day \(i\), you can exchange \(-p_i \leq x \leq p_i\) pebbles for \(-q_i \leq y \leq q_i\) beads or vice versa. It's allowed not to perform an exchange at all. Meanwhile, if you perform an exchange, the proportion \(x \cdot q_i = -y \cdot p_i\) must be fulfilled. Fractional exchanges are allowed.You can perform no more than one such exchange in one day. The numbers of pebbles and beads you have must always remain non-negative.Please solve the following \(n\) problems independently: for each day \(i\), output the maximum number of pebbles that you can have at the end of day \(i\) if you perform exchanges optimally.
The first line of the input contains a single integer \(t\) (\(1 \le t \le 10^3\)) β€” the number of test cases. The description of test cases follows.The first line of each test case contains three integers \(n\), \(a\) and \(b\) (\(1 \le n \le 300\,000\), \(0 \le a, b \le 10^9\)) β€” the number of days and the initial number of pebbles and beads respectively.The second line of each test case contains \(n\) integers: \(p_1, p_2, \ldots, p_n\) (\(1 \le p_i \le 10^9\)).The third line of each test case contains \(n\) integers: \(q_1, q_2, \ldots, q_n\) (\(1 \le q_i \le 10^9\)).It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(300\,000\).
Output \(n\) numbers β€” the maximum number of pebbles at the end of each day.Your answer is considered correct if its absolute or relative error does not exceed \(10^{-6}\).Formally, let your answer be \(a\), and the jury's answer be \(b\). Your answer is accepted if and only if \(\frac{\left|a - b\right|}{\max(1, \left|b\right|)} \le 10^{-6}\).
In the image below you can see the solutions for the first two test cases. In each line there is an optimal sequence of actions for each day.In the first test case, the optimal strategy for the first day is to do no action at all, as we can only decrease the number of pebbles. The optimal strategy for the second day is at first to exchange \(1\) pebble for \(2\) beads, which is a correct exchange, since \(1 \cdot 4 = 2 \cdot 2\), and then to exchange \(2\) beads for \(3\) pebbles.
Input: 32 6 02 34 23 0 64 2 102 3 101 10 10331000 | Output: 6 8 4 6 9.000000 10.33
Master
2
809
665
346
17
1,201
E1
1201E1
E1. Knightmare (easy)
2,900
graphs; interactive; shortest paths
This problem only differs from the next problem in constraints.This is an interactive problem.Alice and Bob are playing a game on the chessboard of size \(n \times m\) where \(n\) and \(m\) are even. The rows are numbered from \(1\) to \(n\) and the columns are numbered from \(1\) to \(m\). There are two knights on the chessboard. A white one initially is on the position \((x_1, y_1)\), while the black one is on the position \((x_2, y_2)\). Alice will choose one of the knights to play with, and Bob will use the other one.The Alice and Bob will play in turns and whoever controls the white knight starts the game. During a turn, the player must move their knight adhering the chess rules. That is, if the knight is currently on the position \((x, y)\), it can be moved to any of those positions (as long as they are inside the chessboard): \((x+1, y+2)\), \((x+1, y-2)\), \((x-1, y+2)\), \((x-1, y-2)\),\((x+2, y+1)\), \((x+2, y-1)\), \((x-2, y+1)\), \((x-2, y-1)\). We all know that knights are strongest in the middle of the board. Both knight have a single position they want to reach: the owner of the white knight wins if it captures the black knight or if the white knight is at \((n/2, m/2)\) and this position is not under attack of the black knight at this moment; The owner of the black knight wins if it captures the white knight or if the black knight is at \((n/2+1, m/2)\) and this position is not under attack of the white knight at this moment. Formally, the player who captures the other knight wins. The player who is at its target square (\((n/2, m/2)\) for white, \((n/2+1, m/2)\) for black) and this position is not under opponent's attack, also wins.A position is under attack of a knight if it can move into this position. Capturing a knight means that a player moves their knight to the cell where the opponent's knight is.If Alice made \(350\) moves and nobody won, the game is a draw.Alice is unsure in her chess skills, so she asks you for a help. Choose a knight and win the game for her. It can be shown, that Alice always has a winning strategy.
In the first example, the white knight can reach it's target square in one move.In the second example black knight wins, no matter what white knight moves.
Input: 8 8 2 3 1 8 | Output: WHITE 4 4
Master
3
2,080
0
0
12
1,876
B
1876B
B. Effects of Anti Pimples
1,500
combinatorics; number theory; sortings
Chaneka has an array \([a_1,a_2,\ldots,a_n]\). Initially, all elements are white. Chaneka will choose one or more different indices and colour the elements at those chosen indices black. Then, she will choose all white elements whose indices are multiples of the index of at least one black element and colour those elements green. After that, her score is the maximum value of \(a_i\) out of all black and green elements.There are \(2^n-1\) ways for Chaneka to choose the black indices. Find the sum of scores for all possible ways Chaneka can choose the black indices. Since the answer can be very big, print the answer modulo \(998\,244\,353\).
The first line contains a single integer \(n\) (\(1 \leq n \leq 10^5\)) β€” the size of array \(a\).The second line contains \(n\) integers \(a_1,a_2,a_3,\ldots,a_n\) (\(0\leq a_i\leq10^5\)).
An integer representing the sum of scores for all possible ways Chaneka can choose the black indices, modulo \(998\,244\,353\).
In the first example, below are the \(15\) possible ways to choose the black indices: Index \(1\) is black. Indices \(2\), \(3\), and \(4\) are green. Maximum value among them is \(19\). Index \(2\) is black. Index \(4\) is green. Maximum value among them is \(14\). Index \(3\) is black. Maximum value among them is \(19\). Index \(4\) is black. Maximum value among them is \(9\). Indices \(1\) and \(2\) are black. Indices \(3\) and \(4\) are green. Maximum value among them is \(19\). Indices \(1\) and \(3\) are black. Indices \(2\) and \(4\) are green. Maximum value among them is \(19\). Indices \(1\) and \(4\) are black. Indices \(2\) and \(3\) are green. Maximum value among them is \(19\). Indices \(2\) and \(3\) are black. Index \(4\) is green. Maximum value among them is \(19\). Indices \(2\) and \(4\) are black. Maximum value among them is \(14\). Indices \(3\) and \(4\) are black. Maximum value among them is \(19\). Indices \(1\), \(2\), and \(3\) are black. Index \(4\) is green. Maximum value among them is \(19\). Indices \(1\), \(2\), and \(4\) are black. Index \(3\) is green. Maximum value among them is \(19\). Indices \(1\), \(3\), and \(4\) are black. Index \(2\) is green. Maximum value among them is \(19\). Indices \(2\), \(3\), and \(4\) are black. Maximum value among them is \(19\). Indices \(1\), \(2\), \(3\), and \(4\) are black. Maximum value among them is \(19\). The total sum is \(19+14+19+9+19+19+19+19+14+19+19+19+19+19+19 = 265\).
Input: 4 19 14 19 9 | Output: 265
Medium
3
647
189
127
18
1,290
D
1290D
D. Coffee Varieties (hard version)
3,000
constructive algorithms; graphs; interactive
This is the hard version of the problem. You can find the easy version in the Div. 2 contest. Both versions only differ in the number of times you can ask your friend to taste coffee.This is an interactive problem.You're considering moving to another city, where one of your friends already lives. There are \(n\) cafΓ©s in this city, where \(n\) is a power of two. The \(i\)-th cafΓ© produces a single variety of coffee \(a_i\). As you're a coffee-lover, before deciding to move or not, you want to know the number \(d\) of distinct varieties of coffees produced in this city.You don't know the values \(a_1, \ldots, a_n\). Fortunately, your friend has a memory of size \(k\), where \(k\) is a power of two.Once per day, you can ask him to taste a cup of coffee produced by the cafΓ© \(c\), and he will tell you if he tasted a similar coffee during the last \(k\) days.You can also ask him to take a medication that will reset his memory. He will forget all previous cups of coffee tasted. You can reset his memory at most \(30\ 000\) times.More formally, the memory of your friend is a queue \(S\). Doing a query on cafΓ© \(c\) will: Tell you if \(a_c\) is in \(S\); Add \(a_c\) at the back of \(S\); If \(|S| > k\), pop the front element of \(S\). Doing a reset request will pop all elements out of \(S\).Your friend can taste at most \(\dfrac{3n^2}{2k}\) cups of coffee in total. Find the diversity \(d\) (number of distinct values in the array \(a\)).Note that asking your friend to reset his memory does not count towards the number of times you ask your friend to taste a cup of coffee.In some test cases the behavior of the interactor is adaptive. It means that the array \(a\) may be not fixed before the start of the interaction and may depend on your queries. It is guaranteed that at any moment of the interaction, there is at least one array \(a\) consistent with all the answers given so far.
The first line contains two integers \(n\) and \(k\) (\(1 \le k \le n \le 1024\), \(k\) and \(n\) are powers of two).It is guaranteed that \(\dfrac{3n^2}{2k} \le 15\ 000\).
In the first example, the array is \(a = [1, 4, 1, 3]\). The city produces \(3\) different varieties of coffee (\(1\), \(3\) and \(4\)).The successive varieties of coffee tasted by your friend are \(1, 4, \textbf{1}, 3, 3, 1, 4\) (bold answers correspond to Y answers). Note that between the two ? 4 asks, there is a reset memory request R, so the answer to the second ? 4 ask is N. Had there been no reset memory request, the answer to the second ? 4 ask is Y.In the second example, the array is \(a = [1, 2, 3, 4, 5, 6, 6, 6]\). The city produces \(6\) different varieties of coffee.The successive varieties of coffee tasted by your friend are \(2, 6, 4, 5, \textbf{2}, \textbf{5}\).
Input: 4 2 N N Y N N N N | Output: ? 1 ? 2 ? 3 ? 4 R ? 4 ? 1 ? 2 ! 3
Master
3
1,902
172
0
12
240
F
240F
F. TorCoder
2,600
data structures
A boy named Leo doesn't miss a single TorCoder contest round. On the last TorCoder round number 100666 Leo stumbled over the following problem. He was given a string s, consisting of n lowercase English letters, and m queries. Each query is characterised by a pair of integers li, ri (1 ≀ li ≀ ri ≀ n). We'll consider the letters in the string numbered from 1 to n from left to right, that is, s = s1s2... sn. After each query he must swap letters with indexes from li to ri inclusive in string s so as to make substring (li, ri) a palindrome. If there are multiple such letter permutations, you should choose the one where string (li, ri) will be lexicographically minimum. If no such permutation exists, you should ignore the query (that is, not change string s).Everybody knows that on TorCoder rounds input line and array size limits never exceed 60, so Leo solved this problem easily. Your task is to solve the problem on a little bit larger limits. Given string s and m queries, print the string that results after applying all m queries to string s.
The first input line contains two integers n and m (1 ≀ n, m ≀ 105) β€” the string length and the number of the queries.The second line contains string s, consisting of n lowercase Latin letters.Each of the next m lines contains a pair of integers li, ri (1 ≀ li ≀ ri ≀ n) β€” a query to apply to the string.
In a single line print the result of applying m queries to string s. Print the queries in the order in which they are given in the input.
A substring (li, ri) 1 ≀ li ≀ ri ≀ n) of string s = s1s2... sn of length n is a sequence of characters slisli + 1...sri.A string is a palindrome, if it reads the same from left to right and from right to left.String x1x2... xp is lexicographically smaller than string y1y2... yq, if either p < q and x1 = y1, x2 = y2, ... , xp = yp, or exists such number r (r < p, r < q), that x1 = y1, x2 = y2, ... , xr = yr and xr + 1 < yr + 1.
Input: 7 2aabcbaa1 35 7 | Output: abacaba
Expert
1
1,056
304
137
2
1,612
E
1612E
E. Messages
2,000
brute force; dp; greedy; probabilities; sortings
Monocarp is a tutor of a group of \(n\) students. He communicates with them using a conference in a popular messenger.Today was a busy day for Monocarp β€” he was asked to forward a lot of posts and announcements to his group, that's why he had to write a very large number of messages in the conference. Monocarp knows the students in the group he is tutoring quite well, so he understands which message should each student read: Monocarp wants the student \(i\) to read the message \(m_i\).Of course, no one's going to read all the messages in the conference. That's why Monocarp decided to pin some of them. Monocarp can pin any number of messages, and if he wants anyone to read some message, he should pin it β€” otherwise it will definitely be skipped by everyone.Unfortunately, even if a message is pinned, some students may skip it anyway. For each student \(i\), Monocarp knows that they will read at most \(k_i\) messages. Suppose Monocarp pins \(t\) messages; if \(t \le k_i\), then the \(i\)-th student will read all the pinned messages; but if \(t > k_i\), the \(i\)-th student will choose exactly \(k_i\) random pinned messages (all possible subsets of pinned messages of size \(k_i\) are equiprobable) and read only the chosen messages.Monocarp wants to maximize the expected number of students that read their respective messages (i.e. the number of such indices \(i\) that student \(i\) reads the message \(m_i\)). Help him to choose how many (and which) messages should he pin!
The first line contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of students in the conference.Then \(n\) lines follow. The \(i\)-th line contains two integers \(m_i\) and \(k_i\) (\(1 \le m_i \le 2 \cdot 10^5\); \(1 \le k_i \le 20\)) β€” the index of the message which Monocarp wants the \(i\)-th student to read and the maximum number of messages the \(i\)-th student will read, respectively.
In the first line, print one integer \(t\) (\(1 \le t \le 2 \cdot 10^5\)) β€” the number of messages Monocarp should pin. In the second line, print \(t\) distinct integers \(c_1\), \(c_2\), ..., \(c_t\) (\(1 \le c_i \le 2 \cdot 10^5\)) β€” the indices of the messages Monocarp should pin. The messages can be listed in any order.If there are multiple answers, print any of them.
Let's consider the examples from the statement. In the first example, Monocarp pins the messages \(5\) and \(10\). if the first student reads the message \(5\), the second student reads the messages \(5\) and \(10\), and the third student reads the messages \(5\) and \(10\), the number of students which have read their respective messages will be \(2\); if the first student reads the message \(10\), the second student reads the messages \(5\) and \(10\), and the third student reads the messages \(5\) and \(10\), the number of students which have read their respective messages will be \(3\). So, the expected number of students which will read their respective messages is \(\frac{5}{2}\). In the second example, Monocarp pins the message \(10\). if the first student reads the message \(10\), the second student reads the message \(10\), and the third student reads the message \(10\), the number of students which have read their respective messages will be \(2\). So, the expected number of students which will read their respective messages is \(2\). If Monocarp had pinned both messages \(5\) and \(10\), the expected number of students which read their respective messages would have been \(2\) as well. In the third example, the expected number of students which will read their respective messages is \(\frac{8}{3}\). In the fourth example, the expected number of students which will read their respective messages is \(2\).
Input: 3 10 1 10 2 5 2 | Output: 2 5 10
Hard
5
1,491
413
374
16
566
C
566C
C. Logistical Questions
3,000
dfs and similar; divide and conquer; trees
Some country consists of n cities, connected by a railroad network. The transport communication of the country is so advanced that the network consists of a minimum required number of (n - 1) bidirectional roads (in the other words, the graph of roads is a tree). The i-th road that directly connects cities ai and bi, has the length of li kilometers.The transport network is served by a state transporting company FRR (Fabulous Rail Roads). In order to simplify the price policy, it offers a single ride fare on the train. In order to follow the route of length t kilometers, you need to pay burles. Note that it is forbidden to split a long route into short segments and pay them separately (a special railroad police, or RRP, controls that the law doesn't get violated).A Large Software Company decided to organize a programming tournament. Having conducted several online rounds, the company employees determined a list of finalists and sent it to the logistical department to find a place where to conduct finals. The Large Software Company can easily organize the tournament finals in any of the n cities of the country, so the the main factor in choosing the city for the last stage of the tournament is the total cost of buying tickets for all the finalists. We know that the i-th city of the country has wi cup finalists living there.Help the company employees find the city such that the total cost of travel of all the participants to it is minimum.
The first line of the input contains number n (1 ≀ n ≀ 200 000) β€” the number of cities in the country.The next line contains n integers w1, w2, ..., wn (0 ≀ wi ≀ 108) β€” the number of finalists living in each city of the country.Next (n - 1) lines contain the descriptions of the railroad, the i-th line contains three integers, ai, bi, li (1 ≀ ai, bi ≀ n, ai β‰  bi, 1 ≀ li ≀ 1000).
Print two numbers β€” an integer f that is the number of the optimal city to conduct the competition, and the real number c, equal to the minimum total cost of transporting all the finalists to the competition. Your answer will be considered correct if two conditions are fulfilled at the same time: The absolute or relative error of the printed number c in comparison with the cost of setting up a final in city f doesn't exceed 10 - 6; Absolute or relative error of the printed number c in comparison to the answer of the jury doesn't exceed 10 - 6. If there are multiple answers, you are allowed to print any of them.
In the sample test an optimal variant of choosing a city to conduct the finals of the competition is 3. At such choice the cost of conducting is burles.In the second sample test, whatever city you would choose, you will need to pay for the transport for five participants, so you will need to pay burles for each one of them.
Input: 53 1 2 6 51 2 32 3 14 3 95 3 1 | Output: 3 192.0
Master
3
1,460
380
618
5
2,089
A
2089A
A. Simple Permutation
1,700
constructive algorithms; number theory
Given an integer \(n\). Construct a permutation \(p_1, p_2, \ldots, p_n\) of length \(n\) that satisfies the following property:For \(1 \le i \le n\), define \(c_i = \lceil \frac{p_1+p_2+\ldots +p_i}{i} \rceil\), then among \(c_1,c_2,\ldots,c_n\) there must be at least \(\lfloor \frac{n}{3} \rfloor - 1\) prime numbers.
The first line contains an integer \(t\) (\(1 \le t \le 10\)) β€” the number of test cases. The description of the test cases follows.In a single line of each test case, there is a single integer \(n\) (\(2 \le n \le 10^5)\) β€” the size of the permutation.
For each test case, output the permutation \(p_1,p_2,\ldots,p_n\) of length \(n\) that satisfies the condition. It is guaranteed that such a permutation always exists.
In the first test case, \(c_1 = \lceil \frac{2}{1} \rceil = 2\), \(c_2 = \lceil \frac{2+1}{2} \rceil = 2\). Both are prime numbers.In the third test case, \(c_1 = \lceil \frac{2}{1} \rceil = 2\), \(c_2 = \lceil \frac{3}{2} \rceil = 2\), \(c_3 = \lceil \frac{6}{3} \rceil = 2\), \(c_4 = \lceil \frac{10}{4} \rceil = 3\), \(c_5 = \lceil \frac{15}{5} \rceil = 3\). All these numbers are prime.
Input: 3235 | Output: 2 1 2 1 3 2 1 3 4 5
Medium
2
320
253
167
20
1,157
E
1157E
E. Minimum Array
1,700
binary search; data structures; greedy
You are given two arrays \(a\) and \(b\), both of length \(n\). All elements of both arrays are from \(0\) to \(n-1\).You can reorder elements of the array \(b\) (if you want, you may leave the order of elements as it is). After that, let array \(c\) be the array of length \(n\), the \(i\)-th element of this array is \(c_i = (a_i + b_i) \% n\), where \(x \% y\) is \(x\) modulo \(y\).Your task is to reorder elements of the array \(b\) to obtain the lexicographically minimum possible array \(c\).Array \(x\) of length \(n\) is lexicographically less than array \(y\) of length \(n\), if there exists such \(i\) (\(1 \le i \le n\)), that \(x_i < y_i\), and for any \(j\) (\(1 \le j < i\)) \(x_j = y_j\).
The first line of the input contains one integer \(n\) (\(1 \le n \le 2 \cdot 10^5\)) β€” the number of elements in \(a\), \(b\) and \(c\).The second line of the input contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(0 \le a_i < n\)), where \(a_i\) is the \(i\)-th element of \(a\).The third line of the input contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(0 \le b_i < n\)), where \(b_i\) is the \(i\)-th element of \(b\).
Print the lexicographically minimum possible array \(c\). Recall that your task is to reorder elements of the array \(b\) and obtain the lexicographically minimum possible array \(c\), where the \(i\)-th element of \(c\) is \(c_i = (a_i + b_i) \% n\).
Input: 4 0 1 2 1 3 2 1 1 | Output: 1 0 0 2
Medium
3
705
428
251
11
162
C
162C
C. Prime factorization
1,800
*special
You are given a positive integer n. Output its prime factorization.If n = a1b1 a2b2 ... akbk (bi > 0), where ak are prime numbers, the output of your program should look as follows: a1*...*a1*a2*...*a2*...*ak*...*ak, where the factors are ordered in non-decreasing order, and each factor ai is printed bi times.
The only line of input contains an integer n (2 ≀ n ≀ 10000).
Output the prime factorization of n, as described above.
Input: 245 | Output: 5*7*7
Medium
1
311
61
56
1
2,010
B
2010B
B. Three Brothers
800
brute force; implementation; math
Three brothers agreed to meet. Let's number the brothers as follows: the oldest brother is number 1, the middle brother is number 2, and the youngest brother is number 3.When it was time for the meeting, one of the brothers was late. Given the numbers of the two brothers who arrived on time, you need to determine the number of the brother who was late.
The first line of input contains two different integers a and b (1 ≀ a, b ≀ 3, a β‰  b) β€” the numbers of the brothers who arrived on time. The numbers are given in arbitrary order.
Output a single integer β€” the number of the brother who was late to the meeting.
Input: 3 1 | Output: 2
Beginner
3
354
178
80
20
1,765
G
1765G
G. Guess the String
2,600
constructive algorithms; interactive; probabilities
This is an interactive problem. You have to use flush operation right after printing each line. For example, in C++ you should use the function fflush(stdout), in Java or Kotlin β€” System.out.flush(), and in Python β€” sys.stdout.flush().The jury has a string \(s\) consisting of characters 0 and/or 1. The first character of this string is 0. The length of this string is \(n\). You have to guess this string. Let's denote \(s[l..r]\) as the substring of \(s\) from \(l\) to \(r\) (i. e. \(s[l..r]\) is the string \(s_ls_{l+1} \dots s_r\)).Let the prefix function of the string \(s\) be an array \([p_1, p_2, \dots, p_n]\), where \(p_i\) is the greatest integer \(j \in [0, i-1]\) such that \(s[1..j] = s[i-j+1..i]\). Also, let the antiprefix function of the string \(s\) be an array \([q_1, q_2, \dots, q_n]\), where \(q_i\) is the greatest integer \(j \in [0, i-1]\) such that \(s[1..j]\) differs from \(s[i-j+1..i]\) in every position.For example, for the string 011001, its prefix function is \([0, 0, 0, 1, 1, 2]\), and its antiprefix function is \([0, 1, 1, 2, 3, 4]\).You can ask queries of two types to guess the string \(s\): \(1\) \(i\) β€” ""what is the value of \(p_i\)?""; \(2\) \(i\) β€” ""what is the value of \(q_i\)?"". You have to guess the string by asking no more than \(789\) queries. Note that giving the answer does not count as a query.In every test and in every test case, the string \(s\) is fixed beforehand.
The example contains one possible way of interaction in a test where \(t = 2\), and the strings guessed by the jury are 011001 and 00111. Note that everything after the // sign is a comment that explains which line means what in the interaction. The jury program won't print these comments in the actual problem, and you shouldn't print them. The empty lines are also added for your convenience, the jury program won't print them, and your solution should not print any empty lines.
Input: 2 // 2 test cases 6 // n = 6 0 // p[3] = 0 1 // q[2] = 1 4 // q[6] = 4 1 // p[4] = 1 1 // answer is correct 5 // n = 5 1 // p[2] = 1 2 // q[4] = 2 2 // q[5] = 2 1 // answer is correct | Output: 1 3 // what is p[3]? 2 2 // what is q[2]? 2 6 // what is q[6]? 1 4 // what is p[4]? 0 011001 // the guess is 011001 1 2 // what is p[2]? 2 4 // what is q[4]? 2 5 // what is q[5]? 0 00111 // the guess is 00111
Expert
3
1,429
0
0
17
710
A
710A
A. King Moves
800
implementation
The only king stands on the standard chess board. You are given his position in format ""cd"", where c is the column from 'a' to 'h' and d is the row from '1' to '8'. Find the number of moves permitted for the king.Check the king's moves here https://en.wikipedia.org/wiki/King_(chess). King moves from the position e4
The only line contains the king's position in the format ""cd"", where 'c' is the column from 'a' to 'h' and 'd' is the row from '1' to '8'.
Print the only integer x β€” the number of moves permitted for the king.
Input: e4 | Output: 8
Beginner
1
318
140
70
7
976
C
976C
C. Nested Segments
1,500
greedy; implementation; sortings
You are given a sequence a1, a2, ..., an of one-dimensional segments numbered 1 through n. Your task is to find two distinct indices i and j such that segment ai lies within segment aj.Segment [l1, r1] lies within segment [l2, r2] iff l1 β‰₯ l2 and r1 ≀ r2.Print indices i and j. If there are multiple answers, print any of them. If no answer exists, print -1 -1.
The first line contains one integer n (1 ≀ n ≀ 3Β·105) β€” the number of segments.Each of the next n lines contains two integers li and ri (1 ≀ li ≀ ri ≀ 109) β€” the i-th segment.
Print two distinct indices i and j such that segment ai lies within segment aj. If there are multiple answers, print any of them. If no answer exists, print -1 -1.
In the first example the following pairs are considered correct: (2, 1), (3, 1), (4, 1), (5, 1) β€” not even touching borders; (3, 2), (4, 2), (3, 5), (4, 5) β€” touch one border; (5, 2), (2, 5) β€” match exactly.
Input: 51 102 93 92 32 9 | Output: 2 1
Medium
3
361
175
163
9
409
H
409H
H. A + B Strikes Back
1,500
*special; brute force; constructive algorithms; dsu; implementation
A + B is often used as an example of the easiest problem possible to show some contest platform. However, some scientists have observed that sometimes this problem is not so easy to get accepted. Want to try?
The input contains two integers a and b (0 ≀ a, b ≀ 103), separated by a single space.
Output the sum of the given integers.
Input: 5 14 | Output: 19
Medium
5
208
86
37
4
830
B
830B
B. Cards Sorting
1,600
data structures; implementation; sortings
Vasily has a deck of cards consisting of n cards. There is an integer on each of the cards, this integer is between 1 and 100 000, inclusive. It is possible that some cards have the same integers on them.Vasily decided to sort the cards. To do this, he repeatedly takes the top card from the deck, and if the number on it equals the minimum number written on the cards in the deck, then he places the card away. Otherwise, he puts it under the deck and takes the next card from the top, and so on. The process ends as soon as there are no cards in the deck. You can assume that Vasily always knows the minimum number written on some card in the remaining deck, but doesn't know where this card (or these cards) is.You are to determine the total number of times Vasily takes the top card from the deck.
The first line contains single integer n (1 ≀ n ≀ 100 000) β€” the number of cards in the deck.The second line contains a sequence of n integers a1, a2, ..., an (1 ≀ ai ≀ 100 000), where ai is the number written on the i-th from top card in the deck.
Print the total number of times Vasily takes the top card from the deck.
In the first example Vasily at first looks at the card with number 6 on it, puts it under the deck, then on the card with number 3, puts it under the deck, and then on the card with number 1. He places away the card with 1, because the number written on it is the minimum among the remaining cards. After that the cards from top to bottom are [2, 6, 3]. Then Vasily looks at the top card with number 2 and puts it away. After that the cards from top to bottom are [6, 3]. Then Vasily looks at card 6, puts it under the deck, then at card 3 and puts it away. Then there is only one card with number 6 on it, and Vasily looks at it and puts it away. Thus, in total Vasily looks at 7 cards.
Input: 46 3 1 2 | Output: 7
Medium
3
801
248
72
8
546
B
546B
B. Soldier and Badges
1,200
brute force; greedy; implementation; sortings
Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors. Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.
First line of input consists of one integer n (1 ≀ n ≀ 3000).Next line consists of n integers ai (1 ≀ ai ≀ n), which stand for coolness factor of each badge.
Output single integer β€” minimum amount of coins the colonel has to pay.
In first sample test we can increase factor of first badge by 1.In second sample test we can increase factors of the second and the third badge by 1.
Input: 41 3 1 4 | Output: 1
Easy
4
672
157
71
5
1,662
O
1662O
O. Circular Maze
0
brute force; dfs and similar; graphs; implementation
You are given a circular maze such as the ones shown in the figures. Determine if it can be solved, i.e., if there is a path which goes from the center to the outside of the maze which does not touch any wall. The maze is described by \(n\) walls. Each wall can be either circular or straight. Circular walls are described by a radius \(r\), the distance from the center, and two angles \(\theta_1, \theta_2\) describing the beginning and the end of the wall in the clockwise direction. Notice that swapping the two angles changes the wall. Straight walls are described by an angle \(\theta\), the direction of the wall, and two radii \(r_1 < r_2\) describing the beginning and the end of the wall. Angles are measured in degrees; the angle \(0\) corresponds to the upward pointing direction; and angles increase clockwise (hence the east direction corresponds to the angle \(90\)).
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1\le t\le 20\)) β€” the number of test cases. The descriptions of the \(t\) test cases follow.The first line of each test case contains an integer \(n\) (\(1 \leq n \leq 5000\)) β€” the number of walls. Each of the following \(n\) lines each contains a character (C for circular, and S for straight) and three integers: either \(r, \theta_1, \theta_2\) (\(1 \leq r \leq 20\) and \(0 \leq \theta_1,\theta_2 < 360\) with \(\theta_1 \neq \theta_2\)) if the wall is circular, or \(r_1\), \(r_2\) and \(\theta\) (\(1 \leq r_1 < r_2 \leq 20\) and \(0 \leq \theta < 360\)) if the wall is straight. It is guaranteed that circular walls do not overlap (but two circular walls may intersect at one or two points), and that straight walls do not overlap (but two straight walls may intersect at one point). However, circular and straight walls can intersect arbitrarily.
For each test case, print YES if the maze can be solved and NO otherwise.
The two sample test cases correspond to the two mazes in the picture.
Input: 2 5 C 1 180 90 C 5 250 230 C 10 150 140 C 20 185 180 S 1 20 180 6 C 1 180 90 C 5 250 230 C 10 150 140 C 20 185 180 S 1 20 180 S 5 10 0 | Output: YES NO
Beginner
4
882
939
73
16
733
B
733B
B. Parade
1,100
math
Very soon there will be a parade of victory over alien invaders in Berland. Unfortunately, all soldiers died in the war and now the army consists of entirely new recruits, many of whom do not even know from which leg they should begin to march. The civilian population also poorly understands from which leg recruits begin to march, so it is only important how many soldiers march in step.There will be n columns participating in the parade, the i-th column consists of li soldiers, who start to march from left leg, and ri soldiers, who start to march from right leg.The beauty of the parade is calculated by the following formula: if L is the total number of soldiers on the parade who start to march from the left leg, and R is the total number of soldiers on the parade who start to march from the right leg, so the beauty will equal |L - R|.No more than once you can choose one column and tell all the soldiers in this column to switch starting leg, i.e. everyone in this columns who starts the march from left leg will now start it from right leg, and vice versa. Formally, you can pick no more than one index i and swap values li and ri. Find the index of the column, such that switching the starting leg for soldiers in it will maximize the the beauty of the parade, or determine, that no such operation can increase the current beauty.
The first line contains single integer n (1 ≀ n ≀ 105) β€” the number of columns. The next n lines contain the pairs of integers li and ri (1 ≀ li, ri ≀ 500) β€” the number of soldiers in the i-th column which start to march from the left or the right leg respectively.
Print single integer k β€” the number of the column in which soldiers need to change the leg from which they start to march, or 0 if the maximum beauty is already reached.Consider that columns are numbered from 1 to n in the order they are given in the input data.If there are several answers, print any of them.
In the first example if you don't give the order to change the leg, the number of soldiers, who start to march from the left leg, would equal 5 + 8 + 10 = 23, and from the right leg β€” 6 + 9 + 3 = 18. In this case the beauty of the parade will equal |23 - 18| = 5.If you give the order to change the leg to the third column, so the number of soldiers, who march from the left leg, will equal 5 + 8 + 3 = 16, and who march from the right leg β€” 6 + 9 + 10 = 25. In this case the beauty equals |16 - 25| = 9.It is impossible to reach greater beauty by giving another orders. Thus, the maximum beauty that can be achieved is 9.
Input: 35 68 910 3 | Output: 3
Easy
1
1,344
265
310
7
1,787
B
1787B
B. Number Factorization
1,100
greedy; math; number theory
Given an integer \(n\).Consider all pairs of integer arrays \(a\) and \(p\) of the same length such that \(n = \prod a_i^{p_i}\) (i.e. \(a_1^{p_1}\cdot a_2^{p_2}\cdot\ldots\)) (\(a_i>1;p_i>0\)) and \(a_i\) is the product of some (possibly one) distinct prime numbers.For example, for \(n = 28 = 2^2\cdot 7^1 = 4^1 \cdot 7^1\) the array pair \(a = [2, 7]\), \(p = [2, 1]\) is correct, but the pair of arrays \(a = [4, 7]\), \(p = [1, 1]\) is not, because \(4=2^2\) is a product of non-distinct prime numbers.Your task is to find the maximum value of \(\sum a_i \cdot p_i\) (i.e. \(a_1\cdot p_1 + a_2\cdot p_2 + \ldots\)) over all possible pairs of arrays \(a\) and \(p\). Note that you do not need to minimize or maximize the length of the arrays.
Each test contains multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β€” the number of test cases. Each test case contains only one integer \(n\) (\(2 \le n \le 10^9\)).
For each test case, print the maximum value of \(\sum a_i \cdot p_i\).
In the first test case, \(100 = 10^2\) so that \(a = [10]\), \(p = [2]\) when \(\sum a_i \cdot p_i\) hits the maximum value \(10\cdot 2 = 20\). Also, \(a = [100]\), \(p = [1]\) does not work since \(100\) is not made of distinct prime factors.In the second test case, we can consider \(10\) as \(10^1\), so \(a = [10]\), \(p = [1]\). Notice that when \(10 = 2^1\cdot 5^1\), \(\sum a_i \cdot p_i = 7\).
Input: 71001086413005619210000000002999999018 | Output: 20 10 22 118 90 2 333333009
Easy
3
746
202
70
17
1,919
C
1919C
C. Grouping Increases
1,400
data structures; dp; greedy
You are given an array \(a\) of size \(n\). You will do the following process to calculate your penalty: Split array \(a\) into two (possibly empty) subsequences\(^\dagger\) \(s\) and \(t\) such that every element of \(a\) is either in \(s\) or \(t^\ddagger\). For an array \(b\) of size \(m\), define the penalty \(p(b)\) of an array \(b\) as the number of indices \(i\) between \(1\) and \(m - 1\) where \(b_i < b_{i + 1}\). The total penalty you will receive is \(p(s) + p(t)\). If you perform the above process optimally, find the minimum possible penalty you will receive.\(^\dagger\) A sequence \(x\) is a subsequence of a sequence \(y\) if \(x\) can be obtained from \(y\) by the deletion of several (possibly, zero or all) elements.\(^\ddagger\) Some valid ways to split array \(a=[3,1,4,1,5]\) into \((s,t)\) are \(([3,4,1,5],[1])\), \(([1,1],[3,4,5])\) and \(([\,],[3,1,4,1,5])\) while some invalid ways to split \(a\) are \(([3,4,5],[1])\), \(([3,1,4,1],[1,5])\) and \(([1,3,4],[5,1])\).
Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1\le n\le 2\cdot 10^5\)) β€” the size of the array \(a\).The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β€” 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 representing the minimum possible penalty you will receive.
In the first test case, a possible way to split \(a\) is \(s=[2,4,5]\) and \(t=[1,3]\). The penalty is \(p(s)+p(t)=2 + 1 =3\).In the second test case, a possible way to split \(a\) is \(s=[8,3,1]\) and \(t=[2,1,7,4,3]\). The penalty is \(p(s)+p(t)=0 + 1 =1\).In the third test case, a possible way to split \(a\) is \(s=[\,]\) and \(t=[3,3,3,3,3]\). The penalty is \(p(s)+p(t)=0 + 0 =0\).
Input: 551 2 3 4 588 2 3 1 1 7 4 353 3 3 3 31122 1 | Output: 3 1 0 0 0
Easy
3
998
518
103
19
1,237
D
1237D
D. Balanced Playlist
2,000
binary search; data structures; implementation
Your favorite music streaming platform has formed a perfectly balanced playlist exclusively for you. The playlist consists of \(n\) tracks numbered from \(1\) to \(n\). The playlist is automatic and cyclic: whenever track \(i\) finishes playing, track \(i+1\) starts playing automatically; after track \(n\) goes track \(1\).For each track \(i\), you have estimated its coolness \(a_i\). The higher \(a_i\) is, the cooler track \(i\) is.Every morning, you choose a track. The playlist then starts playing from this track in its usual cyclic fashion. At any moment, you remember the maximum coolness \(x\) of already played tracks. Once you hear that a track with coolness strictly less than \(\frac{x}{2}\) (no rounding) starts playing, you turn off the music immediately to keep yourself in a good mood.For each track \(i\), find out how many tracks you will listen to before turning off the music if you start your morning with track \(i\), or determine that you will never turn the music off. Note that if you listen to the same track several times, every time must be counted.
The first line contains a single integer \(n\) (\(2 \le n \le 10^5\)), denoting the number of tracks in the playlist.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)), denoting coolnesses of the tracks.
Output \(n\) integers \(c_1, c_2, \ldots, c_n\), where \(c_i\) is either the number of tracks you will listen to if you start listening from track \(i\) or \(-1\) if you will be listening to music indefinitely.
In the first example, here is what will happen if you start with... track \(1\): listen to track \(1\), stop as \(a_2 < \frac{a_1}{2}\). track \(2\): listen to track \(2\), stop as \(a_3 < \frac{a_2}{2}\). track \(3\): listen to track \(3\), listen to track \(4\), listen to track \(1\), stop as \(a_2 < \frac{\max(a_3, a_4, a_1)}{2}\). track \(4\): listen to track \(4\), listen to track \(1\), stop as \(a_2 < \frac{\max(a_4, a_1)}{2}\). In the second example, if you start with track \(4\), you will listen to track \(4\), listen to track \(1\), listen to track \(2\), listen to track \(3\), listen to track \(4\) again, listen to track \(1\) again, and stop as \(a_2 < \frac{max(a_4, a_1, a_2, a_3, a_4, a_1)}{2}\). Note that both track \(1\) and track \(4\) are counted twice towards the result.
Input: 4 11 5 2 7 | Output: 1 1 3 2
Hard
3
1,080
243
210
12
258
B
258B
B. Little Elephant and Elections
1,900
brute force; combinatorics; dp
There have recently been elections in the zoo. Overall there were 7 main political parties: one of them is the Little Elephant Political Party, 6 other parties have less catchy names.Political parties find their number in the ballot highly important. Overall there are m possible numbers: 1, 2, ..., m. Each of these 7 parties is going to be assigned in some way to exactly one number, at that, two distinct parties cannot receive the same number.The Little Elephant Political Party members believe in the lucky digits 4 and 7. They want to evaluate their chances in the elections. For that, they need to find out, how many correct assignments are there, such that the number of lucky digits in the Little Elephant Political Party ballot number is strictly larger than the total number of lucky digits in the ballot numbers of 6 other parties. Help the Little Elephant Political Party, calculate this number. As the answer can be rather large, print the remainder from dividing it by 1000000007 (109 + 7).
A single line contains a single positive integer m (7 ≀ m ≀ 109) β€” the number of possible numbers in the ballot.
In a single line print a single integer β€” the answer to the problem modulo 1000000007 (109 + 7).
Input: 7 | Output: 0
Hard
3
1,005
112
96
2
294
C
294C
C. Shaass and Lights
1,900
combinatorics; number theory
There are n lights aligned in a row. These lights are numbered 1 to n from left to right. Initially some of the lights are switched on. Shaass wants to switch all the lights on. At each step he can switch a light on (this light should be switched off at that moment) if there's at least one adjacent light which is already switched on. He knows the initial state of lights and he's wondering how many different ways there exist to switch all the lights on. Please find the required number of ways modulo 1000000007 (109 + 7).
The first line of the input contains two integers n and m where n is the number of lights in the sequence and m is the number of lights which are initially switched on, (1 ≀ n ≀ 1000, 1 ≀ m ≀ n). The second line contains m distinct integers, each between 1 to n inclusive, denoting the indices of lights which are initially switched on.
In the only line of the output print the number of different possible ways to switch on all the lights modulo 1000000007 (109 + 7).
Input: 3 11 | Output: 1
Hard
2
525
336
131
2
1,266
F
1266F
F. Almost Same Distance
2,900
dfs and similar; graphs
Let \(G\) be a simple graph. Let \(W\) be a non-empty subset of vertices. Then \(W\) is almost-\(k\)-uniform if for each pair of distinct vertices \(u,v \in W\) the distance between \(u\) and \(v\) is either \(k\) or \(k+1\).You are given a tree on \(n\) vertices. For each \(i\) between \(1\) and \(n\), find the maximum size of an almost-\(i\)-uniform set.
The first line contains a single integer \(n\) (\(2 \leq n \leq 5 \cdot 10^5\)) – the number of vertices of the tree.Then \(n-1\) lines follows, the \(i\)-th of which consisting of two space separated integers \(u_i\), \(v_i\) (\(1 \leq u_i, v_i \leq n\)) meaning that there is an edge between vertices \(u_i\) and \(v_i\). It is guaranteed that the given graph is tree.
Output a single line containing \(n\) space separated integers \(a_i\), where \(a_i\) is the maximum size of an almost-\(i\)-uniform set.
Consider the first example. The only maximum almost-\(1\)-uniform set is \(\{1, 2, 3, 4\}\). One of the maximum almost-\(2\)-uniform sets is or \(\{2, 3, 5\}\), another one is \(\{2, 3, 4\}\). A maximum almost-\(3\)-uniform set is any pair of vertices on distance \(3\). Any single vertex is an almost-\(k\)-uniform set for \(k \geq 1\). In the second sample there is an almost-\(2\)-uniform set of size \(4\), and that is \(\{2, 3, 5, 6\}\).
Input: 5 1 2 1 3 1 4 4 5 | Output: 4 3 2 1 1
Master
2
358
370
137
12
1,703
G
1703G
G. Good Key, Bad Key
1,600
bitmasks; brute force; dp; greedy; math
There are \(n\) chests. The \(i\)-th chest contains \(a_i\) coins. You need to open all \(n\) chests in order from chest \(1\) to chest \(n\).There are two types of keys you can use to open a chest: a good key, which costs \(k\) coins to use; a bad key, which does not cost any coins, but will halve all the coins in each unopened chest, including the chest it is about to open. The halving operation will round down to the nearest integer for each chest halved. In other words using a bad key to open chest \(i\) will do \(a_i = \lfloor{\frac{a_i}{2}\rfloor}\), \(a_{i+1} = \lfloor\frac{a_{i+1}}{2}\rfloor, \dots, a_n = \lfloor \frac{a_n}{2}\rfloor\); any key (both good and bad) breaks after a usage, that is, it is a one-time use. You need to use in total \(n\) keys, one for each chest. Initially, you have no coins and no keys. If you want to use a good key, then you need to buy it.During the process, you are allowed to go into debt; for example, if you have \(1\) coin, you are allowed to buy a good key worth \(k=3\) coins, and your balance will become \(-2\) coins.Find the maximum number of coins you can have after opening all \(n\) chests in order from chest \(1\) to chest \(n\).
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.The first line of each test case contains two integers \(n\) and \(k\) (\(1 \leq n \leq 10^5\); \(0 \leq k \leq 10^9\)) β€” the number of chests and the cost of a good key respectively.The second line of each test case contains \(n\) integers \(a_i\) (\(0 \leq a_i \leq 10^9\)) β€” the amount of coins in each chest.The sum of \(n\) over all test cases does not exceed \(10^5\).
For each test case output a single integer β€” the maximum number of coins you can obtain after opening the chests in order from chest \(1\) to chest \(n\).Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).
In the first test case, one possible strategy is as follows: Buy a good key for \(5\) coins, and open chest \(1\), receiving \(10\) coins. Your current balance is \(0 + 10 - 5 = 5\) coins. Buy a good key for \(5\) coins, and open chest \(2\), receiving \(10\) coins. Your current balance is \(5 + 10 - 5 = 10\) coins. Use a bad key and open chest \(3\). As a result of using a bad key, the number of coins in chest \(3\) becomes \(\left\lfloor \frac{3}{2} \right\rfloor = 1\), and the number of coins in chest \(4\) becomes \(\left\lfloor \frac{1}{2} \right\rfloor = 0\). Your current balance is \(10 + 1 = 11\). Use a bad key and open chest \(4\). As a result of using a bad key, the number of coins in chest \(4\) becomes \(\left\lfloor \frac{0}{2} \right\rfloor = 0\). Your current balance is \(11 + 0 = 11\). At the end of the process, you have \(11\) coins, which can be proven to be maximal.
Input: 54 510 10 3 11 213 1210 10 2912 515 74 89 45 18 69 67 67 11 96 23 592 5785 60 | Output: 11 0 13 60 58
Medium
5
1,193
473
340
17
1,583
A
1583A
A. Windblume Ode
800
math; number theory
A bow adorned with nameless flowers that bears the earnest hopes of an equally nameless person.You have obtained the elegant bow known as the Windblume Ode. Inscribed in the weapon is an array of \(n\) (\(n \ge 3\)) positive distinct integers (i.e. different, no duplicates are allowed).Find the largest subset (i.e. having the maximum number of elements) of this array such that its sum is a composite number. A positive integer \(x\) is called composite if there exists a positive integer \(y\) such that \(1 < y < x\) and \(x\) is divisible by \(y\).If there are multiple subsets with this largest size with the composite sum, you can output any of them. It can be proven that under the constraints of the problem such a non-empty subset always exists.
Each test consists of multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). Description of the test cases follows.The first line of each test case contains an integer \(n\) (\(3 \leq n \leq 100\)) β€” the length of the array.The second line of each test case contains \(n\) distinct integers \(a_{1},a_{2},\dots,a_{n}\) (\(1 \leq a_{i} \leq 200\)) β€” the elements of the array.
Each test case should have two lines of output.The first line should contain a single integer \(x\): the size of the largest subset with composite sum. The next line should contain \(x\) space separated integers representing the indices of the subset of the initial array.
In the first test case, the subset \(\{a_2, a_1\}\) has a sum of \(9\), which is a composite number. The only subset of size \(3\) has a prime sum equal to \(11\). Note that you could also have selected the subset \(\{a_1, a_3\}\) with sum \(8 + 2 = 10\), which is composite as it's divisible by \(2\).In the second test case, the sum of all elements equals to \(21\), which is a composite number. Here we simply take the whole array as our subset.
Input: 4 3 8 1 2 4 6 9 4 2 9 1 2 3 4 5 6 7 8 9 3 200 199 198 | Output: 2 2 1 4 2 1 4 3 9 6 9 1 2 3 4 5 7 8 3 1 2 3
Beginner
2
755
419
272
15
2,001
A
2001A
A. Make All Equal
800
greedy; implementation
You are given a cyclic array \(a_1, a_2, \ldots, a_n\).You can perform the following operation on \(a\) at most \(n - 1\) times: Let \(m\) be the current size of \(a\), you can choose any two adjacent elements where the previous one is no greater than the latter one (In particular, \(a_m\) and \(a_1\) are adjacent and \(a_m\) is the previous one), and delete exactly one of them. In other words, choose an integer \(i\) (\(1 \leq i \leq m\)) where \(a_i \leq a_{(i \bmod m) + 1}\) holds, and delete exactly one of \(a_i\) or \(a_{(i \bmod m) + 1}\) from \(a\). Your goal is to find the minimum number of operations needed to make all elements in \(a\) equal.
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β€” the length of the array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le n\)) β€” the elements of array \(a\).
For each test case, output a single line containing an integer: the minimum number of operations needed to make all elements in \(a\) equal.
In the first test case, there is only one element in \(a\), so we can't do any operation.In the second test case, we can perform the following operations to make all elements in \(a\) equal: choose \(i = 2\), delete \(a_3\), then \(a\) would become \([1, 2]\). choose \(i = 1\), delete \(a_1\), then \(a\) would become \([2]\). It can be proven that we can't make all elements in \(a\) equal using fewer than \(2\) operations, so the answer is \(2\).
Input: 71131 2 331 2 255 4 3 2 161 1 2 2 3 388 7 6 3 8 7 6 361 1 4 5 1 4 | Output: 0 2 1 4 4 6 3
Beginner
2
660
415
140
20
1,786
A2
1786A2
A2. Alternating Deck (hard version)
800
implementation
This is a hard version of the problem. In this version, there are two colors of the cards.Alice has \(n\) cards, each card is either black or white. The cards are stacked in a deck in such a way that the card colors alternate, starting from a white card. Alice deals the cards to herself and to Bob, dealing at once several cards from the top of the deck in the following order: one card to herself, two cards to Bob, three cards to Bob, four cards to herself, five cards to herself, six cards to Bob, seven cards to Bob, eight cards to herself, and so on. In other words, on the \(i\)-th step, Alice deals \(i\) top cards from the deck to one of the players; on the first step, she deals the cards to herself and then alternates the players every two steps. When there aren't enough cards at some step, Alice deals all the remaining cards to the current player, and the process stops. First Alice's steps in a deck of many cards. How many cards of each color will Alice and Bob have at the end?
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 200\)). The description of the test cases followsThe only line of each test case contains a single integer \(n\) (\(1 \le n \le 10^6\)) β€” the number of cards.
For each test case print four integers β€” the number of cards in the end for each player β€” in this order: white cards Alice has, black cards Alice has, white cards Bob has, black cards Bob has.
Input: 51061781000000 | Output: 3 2 2 3 1 0 2 3 6 4 3 4 2 1 2 3 250278 249924 249722 250076
Beginner
1
995
268
192
17
2,041
D
2041D
D. Drunken Maze
1,700
brute force; dfs and similar; graphs; shortest paths
Image generated by ChatGPT 4o. You are given a two-dimensional maze with a start and end position. Your task is to find the fastest way to get from the start to the end position. The fastest way is to make the minimum number of steps where one step is going left, right, up, or down. Of course, you cannot walk through walls.There is, however, a catch: If you make more than three steps in the same direction, you lose balance and fall down. Therefore, it is forbidden to make more than three consecutive steps in the same direction. It is okay to walk three times to the right, then one step to the left, and then again three steps to the right. This has the same effect as taking five steps to the right, but is slower.
The first line contains two numbers \(n\) and \(m\), which are the height and width of the maze. This is followed by an ASCII-representation of the maze where \(\tt{\#}\) is a wall, \(\tt{.}\) is an empty space, and \(\tt S\) and \(\tt T\) are the start and end positions. \(12 \leq n\times m \leq 200000\). \(3\leq n,m \leq 10000\). Characters are only \(\tt{.\#ST}\) and there is exactly one \(\tt{S}\) and one \(\tt{T}\). The outer borders are only \(\tt{\#}\) (walls).
The minimum number of steps to reach the end position from the start position or -1 if that is impossible.
Input: 7 12#############S........T##.########.##..........##..........##..#..#....############# | Output: 15
Medium
4
721
472
106
20
1,196
F
1196F
F. K-th Path
2,200
brute force; constructive algorithms; shortest paths; sortings
You are given a connected undirected weighted graph consisting of \(n\) vertices and \(m\) edges.You need to print the \(k\)-th smallest shortest path in this graph (paths from the vertex to itself are not counted, paths from \(i\) to \(j\) and from \(j\) to \(i\) are counted as one).More formally, if \(d\) is the matrix of shortest paths, where \(d_{i, j}\) is the length of the shortest path between vertices \(i\) and \(j\) (\(1 \le i < j \le n\)), then you need to print the \(k\)-th element in the sorted array consisting of all \(d_{i, j}\), where \(1 \le i < j \le n\).
The first line of the input contains three integers \(n, m\) and \(k\) (\(2 \le n \le 2 \cdot 10^5\), \(n - 1 \le m \le \min\Big(\frac{n(n-1)}{2}, 2 \cdot 10^5\Big)\), \(1 \le k \le \min\Big(\frac{n(n-1)}{2}, 400\Big)\) β€” the number of vertices in the graph, the number of edges in the graph and the value of \(k\), correspondingly.Then \(m\) lines follow, each containing three integers \(x\), \(y\) and \(w\) (\(1 \le x, y \le n\), \(1 \le w \le 10^9\), \(x \ne y\)) denoting an edge between vertices \(x\) and \(y\) of weight \(w\).It is guaranteed that the given graph is connected (there is a path between any pair of vertices), there are no self-loops (edges connecting the vertex with itself) and multiple edges (for each pair of vertices \(x\) and \(y\), there is at most one edge between this pair of vertices in the graph).
Print one integer β€” the length of the \(k\)-th smallest shortest path in the given graph (paths from the vertex to itself are not counted, paths from \(i\) to \(j\) and from \(j\) to \(i\) are counted as one).
Input: 6 10 5 2 5 1 5 3 9 6 2 2 1 3 1 5 1 8 6 5 10 1 6 5 6 4 6 3 6 2 3 4 5 | Output: 3
Hard
4
578
833
209
11
1,250
C
1250C
C. Trip to Saint Petersburg
2,100
data structures
You are planning your trip to Saint Petersburg. After doing some calculations, you estimated that you will have to spend \(k\) rubles each day you stay in Saint Petersburg β€” you have to rent a flat, to eat at some local cafe, et cetera. So, if the day of your arrival is \(L\), and the day of your departure is \(R\), you will have to spend \(k(R - L + 1)\) rubles in Saint Petersburg.You don't want to spend a lot of money on your trip, so you decided to work in Saint Petersburg during your trip. There are \(n\) available projects numbered from \(1\) to \(n\), the \(i\)-th of them lasts from the day \(l_i\) to the day \(r_i\) inclusive. If you choose to participate in the \(i\)-th project, then you have to stay and work in Saint Petersburg for the entire time this project lasts, but you get paid \(p_i\) rubles for completing it.Now you want to come up with an optimal trip plan: you have to choose the day of arrival \(L\), the day of departure \(R\) and the set of projects \(S\) to participate in so that all the following conditions are met: your trip lasts at least one day (formally, \(R \ge L\)); you stay in Saint Petersburg for the duration of every project you have chosen (formally, for each \(s \in S\) \(L \le l_s\) and \(R \ge r_s\)); your total profit is strictly positive and maximum possible (formally, you have to maximize the value of \(\sum \limits_{s \in S} p_s - k(R - L + 1)\), and this value should be positive). You may assume that no matter how many projects you choose, you will still have time and ability to participate in all of them, even if they overlap.
The first line contains two integers \(n\) and \(k\) (\(1 \le n \le 2\cdot10^5\), \(1 \le k \le 10^{12}\)) β€” the number of projects and the amount of money you have to spend during each day in Saint Petersburg, respectively.Then \(n\) lines follow, each containing three integers \(l_i\), \(r_i\), \(p_i\) (\(1 \le l_i \le r_i \le 2\cdot10^5\), \(1 \le p_i \le 10^{12}\)) β€” the starting day of the \(i\)-th project, the ending day of the \(i\)-th project, and the amount of money you get paid if you choose to participate in it, respectively.
If it is impossible to plan a trip with strictly positive profit, print the only integer \(0\).Otherwise, print two lines. The first line should contain four integers \(p\), \(L\), \(R\) and \(m\) β€” the maximum profit you can get, the starting day of your trip, the ending day of your trip and the number of projects you choose to complete, respectively. The second line should contain \(m\) distinct integers \(s_1\), \(s_2\), ..., \(s_{m}\) β€” the projects you choose to complete, listed in arbitrary order. If there are multiple answers with maximum profit, print any of them.
Input: 4 5 1 1 3 3 3 11 5 5 17 7 7 4 | Output: 13 3 5 2 3 2
Hard
1
1,594
542
578
12
1,934
D1
1934D1
D1. XOR Break β€” Solo Version
2,100
bitmasks; constructive algorithms; greedy
This is the solo version of the problem. Note that the solution of this problem may or may not share ideas with the solution of the game version. You can solve and get points for both versions independently.You can make hacks only if both versions of the problem are solved.Given an integer variable \(x\) with the initial value of \(n\). A single break operation consists of the following steps: Choose a value \(y\) such that \(0 \lt y \lt x\) and \(0 \lt (x \oplus y) \lt x\). Update \(x\) by either setting \(x = y\) or setting \(x = x \oplus y\). Determine whether it is possible to transform \(x\) into \(m\) using a maximum of \(63\) break operations. If it is, provide the sequence of operations required to achieve \(x = m\).You don't need to minimize the number of operations.Here \(\oplus\) denotes the bitwise XOR operation.
The first line contains one positive integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.Each test case consists of a single line containing two integers \(n\) and \(m\) (\(1 \leq m \lt n \leq 10^{18}\)) β€” the initial value of \(x\) and the target value of \(x\).
For each test case, output your answer in the following format.If it is not possible to achieve \(m\) in \(63\) operations, print \(-1\).Otherwise, The first line should contain \(k\) (\(1 \leq k \leq 63\)) β€” where \(k\) is the number of operations required.The next line should contain \(k+1\) integers β€” the sequence where variable \(x\) changes after each break operation. The \(1\)-st and \(k+1\)-th integers should be \(n\) and \(m\), respectively.
In the first test case \(n = 7\), for the first operation \(x = 7\) if we choose \(y = 3\) then \((7 \oplus 3) \lt 7\), hence we can update \(x\) with \(3\) which is equal to \(m\).In the second test case \(n = 4\), for the first operation \(x = 4\).If we choose: \(y = 1\) then \((4 \oplus 1) \gt 4\) \(y = 2\) then \((4 \oplus 2) \gt 4\) \(y = 3\) then \((4 \oplus 3) \gt 4\) Hence we can't do the first operation and it is impossible to make \(x = 2\).
Input: 37 34 2481885160128643072 45035996273704960 | Output: 1 7 3 -1 3 481885160128643072 337769972052787200 49539595901075456 45035996273704960
Hard
3
836
275
453
19
2,121
F
2121F
F. Yamakasi
1,800
binary search; brute force; data structures; greedy; two pointers
You are given an array of integers \(a_1, a_2, \ldots, a_n\) and two integers \(s\) and \(x\). Count the number of subsegments of the array whose sum of elements equals \(s\) and whose maximum value equals \(x\).More formally, count the number of pairs \(1 \leq l \leq r \leq n\) such that: \(a_l + a_{l + 1} + \ldots + a_r = s\). \(\max(a_l, a_{l + 1}, \ldots, a_r) = x\).
Each test consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. The description of the test cases follows.The first line of each test case contains three integers \(n\), \(s\), and \(x\) (\(1 \leq n \leq 2 \cdot 10^5\), \(-2 \cdot 10^{14} \leq s \leq 2 \cdot 10^{14}\), \(-10^9 \leq x \leq 10^9\)). The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^9 \leq a_i \leq 10^9\)).It is guaranteed that the sum of \(n\) across all test cases does not exceed \(2 \cdot 10^5\).
For each test case, output the number of subsegments of the array whose sum of elements equals \(s\) and whose maximum value equals \(x\).
In the first test case, the suitable subsegment is \(l = 1\), \(r = 1\).In the third test case, the suitable subsegments are \(l = 1\), \(r = 1\) and \(l = 3\), \(r = 3\). In the fifth test case, the suitable subsegments are \(l = 1\), \(r = 3\) and \(l = 6\), \(r = 8\).In the sixth test case, the suitable subsegments are those for which \(r = l + 2\). In the seventh test case, the following subsegments are suitable: \(l = 1\), \(r = 7\). \(l = 2\), \(r = 7\). \(l = 3\), \(r = 6\). \(l = 4\), \(r = 8\). \(l = 7\), \(r = 11\). \(l = 7\), \(r = 12\). \(l = 8\), \(r = 10\). \(l = 9\), \(r = 13\).
Input: 91 0 001 -2 -1-23 -1 -1-1 1 -16 -3 -2-1 -1 -1 -2 -1 -18 3 22 2 -1 -2 3 -1 2 29 6 31 2 3 1 2 3 1 2 313 7 30 -1 3 3 3 -2 1 2 2 3 -1 0 32 -2 -1-2 -12 -2 -1-1 -2 | Output: 1 0 2 0 2 7 8 0 0
Medium
5
373
587
138
21
1,204
A
1204A
A. BowWow and the Timetable
1,000
math
In the city of Saint Petersburg, a day lasts for \(2^{100}\) minutes. From the main station of Saint Petersburg, a train departs after \(1\) minute, \(4\) minutes, \(16\) minutes, and so on; in other words, the train departs at time \(4^k\) for each integer \(k \geq 0\). Team BowWow has arrived at the station at the time \(s\) and it is trying to count how many trains have they missed; in other words, the number of trains that have departed strictly before time \(s\). For example if \(s = 20\), then they missed trains which have departed at \(1\), \(4\) and \(16\). As you are the only one who knows the time, help them!Note that the number \(s\) will be given you in a binary representation without leading zeroes.
The first line contains a single binary number \(s\) (\(0 \leq s < 2^{100}\)) without leading zeroes.
Output a single number β€” the number of trains which have departed strictly before the time \(s\).
In the first example \(100000000_2 = 256_{10}\), missed trains have departed at \(1\), \(4\), \(16\) and \(64\).In the second example \(101_2 = 5_{10}\), trains have departed at \(1\) and \(4\).The third example is explained in the statements.
Input: 100000000 | Output: 4
Beginner
1
721
101
97
12
258
A
258A
A. Little Elephant and Bits
1,100
greedy; math
The Little Elephant has an integer a, written in the binary notation. He wants to write this number on a piece of paper.To make sure that the number a fits on the piece of paper, the Little Elephant ought to delete exactly one any digit from number a in the binary record. At that a new number appears. It consists of the remaining binary digits, written in the corresponding order (possible, with leading zeroes).The Little Elephant wants the number he is going to write on the paper to be as large as possible. Help him find the maximum number that he can obtain after deleting exactly one binary digit and print it in the binary notation.
The single line contains integer a, written in the binary notation without leading zeroes. This number contains more than 1 and at most 105 digits.
In the single line print the number that is written without leading zeroes in the binary notation β€” the answer to the problem.
In the first sample the best strategy is to delete the second digit. That results in number 112 = 310.In the second sample the best strategy is to delete the third or fourth digits β€” that results in number 110102 = 2610.
Input: 101 | Output: 11
Easy
2
641
147
126
2
1,374
A
1374A
A. Required Remainder
800
math
You are given three integers \(x, y\) and \(n\). Your task is to find the maximum integer \(k\) such that \(0 \le k \le n\) that \(k \bmod x = y\), where \(\bmod\) is modulo operation. Many programming languages use percent operator % to implement it.In other words, with given \(x, y\) and \(n\) you need to find the maximum possible integer from \(0\) to \(n\) that has the remainder \(y\) modulo \(x\).You have to answer \(t\) independent test cases. It is guaranteed that such \(k\) exists for each test case.
The first line of the input contains one integer \(t\) (\(1 \le t \le 5 \cdot 10^4\)) β€” the number of test cases. The next \(t\) lines contain test cases.The only line of the test case contains three integers \(x, y\) and \(n\) (\(2 \le x \le 10^9;~ 0 \le y < x;~ y \le n \le 10^9\)).It can be shown that such \(k\) always exists under the given constraints.
For each test case, print the answer β€” maximum non-negative integer \(k\) such that \(0 \le k \le n\) and \(k \bmod x = y\). It is guaranteed that the answer always exists.
In the first test case of the example, the answer is \(12339 = 7 \cdot 1762 + 5\) (thus, \(12339 \bmod 7 = 5\)). It is obvious that there is no greater integer not exceeding \(12345\) which has the remainder \(5\) modulo \(7\).
Input: 7 7 5 12345 5 0 4 10 5 15 17 8 54321 499999993 9 1000000000 10 5 187 2 0 999999999 | Output: 12339 0 15 54306 999999995 185 999999998
Beginner
1
513
358
172
13
1,197
D
1197D
D. Yet Another Subarray Problem
1,900
dp; greedy; math
You are given an array \(a_1, a_2, \dots , a_n\) and two integers \(m\) and \(k\).You can choose some subarray \(a_l, a_{l+1}, \dots, a_{r-1}, a_r\). The cost of subarray \(a_l, a_{l+1}, \dots, a_{r-1}, a_r\) is equal to \(\sum\limits_{i=l}^{r} a_i - k \lceil \frac{r - l + 1}{m} \rceil\), where \(\lceil x \rceil\) is the least integer greater than or equal to \(x\). The cost of empty subarray is equal to zero.For example, if \(m = 3\), \(k = 10\) and \(a = [2, -4, 15, -3, 4, 8, 3]\), then the cost of some subarrays are: \(a_3 \dots a_3: 15 - k \lceil \frac{1}{3} \rceil = 15 - 10 = 5\); \(a_3 \dots a_4: (15 - 3) - k \lceil \frac{2}{3} \rceil = 12 - 10 = 2\); \(a_3 \dots a_5: (15 - 3 + 4) - k \lceil \frac{3}{3} \rceil = 16 - 10 = 6\); \(a_3 \dots a_6: (15 - 3 + 4 + 8) - k \lceil \frac{4}{3} \rceil = 24 - 20 = 4\); \(a_3 \dots a_7: (15 - 3 + 4 + 8 + 3) - k \lceil \frac{5}{3} \rceil = 27 - 20 = 7\). Your task is to find the maximum cost of some subarray (possibly empty) of array \(a\).
The first line contains three integers \(n\), \(m\), and \(k\) (\(1 \le n \le 3 \cdot 10^5, 1 \le m \le 10, 1 \le k \le 10^9\)).The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(-10^9 \le a_i \le 10^9\)).
Print the maximum cost of some subarray of array \(a\).
Input: 7 3 10 2 -4 15 -3 4 8 3 | Output: 7
Hard
3
996
222
55
11
1,472
F
1472F
F. New Year's Puzzle
2,100
brute force; dp; graph matchings; greedy; sortings
Every year Santa Claus gives gifts to all children. However, each country has its own traditions, and this process takes place in different ways. For example, in Berland you need to solve the New Year's puzzle.Polycarp got the following problem: given a grid strip of size \(2 \times n\), some cells of it are blocked. You need to check if it is possible to tile all free cells using the \(2 \times 1\) and \(1 \times 2\) tiles (dominoes).For example, if \(n = 5\) and the strip looks like this (black cells are blocked): Then it can be tiled, for example, using two vertical and two horizontal tiles, as in the picture below (different tiles are marked by different colors). And if \(n = 3\) and the strip looks like this: It is impossible to tile free cells.Polycarp easily solved this task and received his New Year's gift. Can you solve it?
The first line contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases. Then \(t\) test cases follow.Each test case is preceded by an empty line.The first line of each test case contains two integers \(n\) and \(m\) (\(1 \le n \le 10^9\), \(1 \le m \le 2 \cdot 10^5\)) β€” the length of the strip and the number of blocked cells on it.Each of the next \(m\) lines contains two integers \(r_i, c_i\) (\(1 \le r_i \le 2, 1 \le c_i \le n\)) β€” numbers of rows and columns of blocked cells. It is guaranteed that all blocked cells are different, i.e. \((r_i, c_i) \ne (r_j, c_j), i \ne j\).It is guaranteed that the sum of \(m\) over all test cases does not exceed \(2 \cdot 10^5\).
For each test case, print on a separate line: ""YES"", if it is possible to tile all unblocked squares with the \(2 \times 1\) and \(1 \times 2\) tiles; ""NO"" otherwise. You can output ""YES"" and ""NO"" in any case (for example, the strings yEs, yes, Yes and YES will be recognized as positive).
The first two test cases are explained in the statement.In the third test case the strip looks like this: It is easy to check that the unblocked squares on it can not be tiled.
Input: 3 5 2 2 2 1 4 3 2 2 1 2 3 6 4 2 1 2 3 2 4 2 6 | Output: YES NO NO
Hard
5
844
697
297
14
2,009
E
2009E
E. Klee's SUPER DUPER LARGE Array!!!
1,400
binary search; math; ternary search
Klee has an array \(a\) of length \(n\) containing integers \([k, k+1, ..., k+n-1]\) in that order. Klee wants to choose an index \(i\) (\(1 \leq i \leq n\)) such that \(x = |a_1 + a_2 + \dots + a_i - a_{i+1} - \dots - a_n|\) is minimized. Note that for an arbitrary integer \(z\), \(|z|\) represents the absolute value of \(z\). Output the minimum possible value of \(x\).
The first line contains \(t\) (\(1 \leq t \leq 10^4\)) β€” the number of test cases.Each test case contains two integers \(n\) and \(k\) (\(2 \leq n, k \leq 10^9\)) β€” the length of the array and the starting element of the array.
For each test case, output the minimum value of \(x\) on a new line.
In the first sample, \(a = [2, 3]\). When \(i = 1\) is chosen, \(x = |2-3| = 1\). It can be shown this is the minimum possible value of \(x\).In the third sample, \(a = [3, 4, 5, 6, 7]\). When \(i = 3\) is chosen, \(x = |3 + 4 + 5 - 6 - 7| = 1\). It can be shown this is the minimum possible value of \(x\).
Input: 42 27 25 31000000000 1000000000 | Output: 1 5 1 347369930
Easy
3
373
227
68
20
840
A
840A
A. Leha and Function
1,300
combinatorics; greedy; math; number theory; sortings
Leha like all kinds of strange things. Recently he liked the function F(n, k). Consider all possible k-element subsets of the set [1, 2, ..., n]. For subset find minimal element in it. F(n, k) β€” mathematical expectation of the minimal element among all k-element subsets.But only function does not interest him. He wants to do interesting things with it. Mom brought him two arrays A and B, each consists of m integers. For all i, j such that 1 ≀ i, j ≀ m the condition Ai β‰₯ Bj holds. Help Leha rearrange the numbers in the array A so that the sum is maximally possible, where A' is already rearranged array.
First line of input data contains single integer m (1 ≀ m ≀ 2Β·105) β€” length of arrays A and B.Next line contains m integers a1, a2, ..., am (1 ≀ ai ≀ 109) β€” array A.Next line contains m integers b1, b2, ..., bm (1 ≀ bi ≀ 109) β€” array B.
Output m integers a'1, a'2, ..., a'm β€” array A' which is permutation of the array A.
Input: 57 3 5 3 42 1 3 2 3 | Output: 4 7 3 5 3
Easy
5
608
236
84
8
120
E
120E
E. Put Knight!
1,400
games; math
Petya and Gena play a very interesting game ""Put a Knight!"" on a chessboard n Γ— n in size. In this game they take turns to put chess pieces called ""knights"" on the board so that no two knights could threat each other. A knight located in square (r, c) can threat squares (r - 1, c + 2), (r - 1, c - 2), (r + 1, c + 2), (r + 1, c - 2), (r - 2, c + 1), (r - 2, c - 1), (r + 2, c + 1) and (r + 2, c - 1) (some of the squares may be located outside the chessboard). The player who can't put a new knight during his move loses. Determine which player wins considering that both players play optimally well and Petya starts.
The first line contains integer T (1 ≀ T ≀ 100) β€” the number of boards, for which you should determine the winning player. Next T lines contain T integers ni (1 ≀ ni ≀ 10000) β€” the sizes of the chessboards.
For each ni Γ— ni board print on a single line ""0"" if Petya wins considering both players play optimally well. Otherwise, print ""1"".
Input: 221 | Output: 10
Easy
2
622
206
135
1
1,567
B
1567B
B. MEXor Mixup
1,000
bitmasks; greedy
Alice gave Bob two integers \(a\) and \(b\) (\(a > 0\) and \(b \ge 0\)). Being a curious boy, Bob wrote down an array of non-negative integers with \(\operatorname{MEX}\) value of all elements equal to \(a\) and \(\operatorname{XOR}\) value of all elements equal to \(b\).What is the shortest possible length of the array Bob wrote?Recall that the \(\operatorname{MEX}\) (Minimum EXcluded) of an array is the minimum non-negative integer that does not belong to the array and the \(\operatorname{XOR}\) of an array is the bitwise XOR of all the elements of the array.
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 5 \cdot 10^4\)) β€” the number of test cases. The description of the test cases follows.The only line of each test case contains two integers \(a\) and \(b\) (\(1 \leq a \leq 3 \cdot 10^5\); \(0 \leq b \leq 3 \cdot 10^5\)) β€” the \(\operatorname{MEX}\) and \(\operatorname{XOR}\) of the array, respectively.
For each test case, output one (positive) integer β€” the length of the shortest array with \(\operatorname{MEX}\) \(a\) and \(\operatorname{XOR}\) \(b\). We can show that such an array always exists.
In the first test case, one of the shortest arrays with \(\operatorname{MEX}\) \(1\) and \(\operatorname{XOR}\) \(1\) is \([0, 2020, 2021]\).In the second test case, one of the shortest arrays with \(\operatorname{MEX}\) \(2\) and \(\operatorname{XOR}\) \(1\) is \([0, 1]\).It can be shown that these arrays are the shortest arrays possible.
Input: 5 1 1 2 1 2 0 1 10000 2 10000 | Output: 3 2 3 2 3
Beginner
2
567
405
198
15
130
D
130D
D. Exponentiation
1,500
*special
You are given integers a, b and c. Calculate ab modulo c.
Input data contains numbers a, b and c, one number per line. Each number is an integer between 1 and 100, inclusive.
Output ab mod c.
Input: 2540 | Output: 32
Medium
1
57
116
16
1
630
R
630R
R. Game
1,200
games; math
There is a legend in the IT City college. A student that failed to answer all questions on the game theory exam is given one more chance by his professor. The student has to play a game with the professor.The game is played on a square field consisting of n Γ— n cells. Initially all cells are empty. On each turn a player chooses and paint an empty cell that has no common sides with previously painted cells. Adjacent corner of painted cells is allowed. On the next turn another player does the same, then the first one and so on. The player with no cells to paint on his turn loses.The professor have chosen the field size n and allowed the student to choose to be the first or the second player in the game. What should the student choose to win the game? Both players play optimally.
The only line of the input contains one integer n (1 ≀ n ≀ 1018) β€” the size of the field.
Output number 1, if the player making the first turn wins when both players play optimally, otherwise print number 2.
Input: 1 | Output: 1
Easy
2
787
89
117
6
289
B
289B
B. Polo the Penguin and Matrix
1,400
brute force; dp; implementation; sortings; ternary search
Little penguin Polo has an n Γ— m matrix, consisting of integers. Let's index the matrix rows from 1 to n from top to bottom and let's index the columns from 1 to m from left to right. Let's represent the matrix element on the intersection of row i and column j as aij.In one move the penguin can add or subtract number d from some matrix element. Find the minimum number of moves needed to make all matrix elements equal. If the described plan is impossible to carry out, say so.
The first line contains three integers n, m and d (1 ≀ n, m ≀ 100, 1 ≀ d ≀ 104) β€” the matrix sizes and the d parameter. Next n lines contain the matrix: the j-th integer in the i-th row is the matrix element aij (1 ≀ aij ≀ 104).
In a single line print a single integer β€” the minimum number of moves the penguin needs to make all matrix elements equal. If that is impossible, print ""-1"" (without the quotes).
Input: 2 2 22 46 8 | Output: 4
Easy
5
479
228
180
2
1,422
E
1422E
E. Minlexes
2,700
dp; greedy; implementation; strings
Some time ago Lesha found an entertaining string \(s\) consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows.Lesha chooses an arbitrary (possibly zero) number of pairs on positions \((i, i + 1)\) in such a way that the following conditions are satisfied: for each pair \((i, i + 1)\) the inequality \(0 \le i < |s| - 1\) holds; for each pair \((i, i + 1)\) the equality \(s_i = s_{i + 1}\) holds; there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string.
The only line contains the string \(s\) (\(1 \le |s| \le 10^5\)) β€” the initial string consisting of lowercase English letters only.
In \(|s|\) lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than \(10\) characters, instead print the first \(5\) characters, then ""..."", then the last \(2\) characters of the answer.
Consider the first example. The longest suffix is the whole string ""abcdd"". Choosing one pair \((4, 5)\), Lesha obtains ""abc"". The next longest suffix is ""bcdd"". Choosing one pair \((3, 4)\), we obtain ""bc"". The next longest suffix is ""cdd"". Choosing one pair \((2, 3)\), we obtain ""c"". The next longest suffix is ""dd"". Choosing one pair \((1, 2)\), we obtain """" (an empty string). The last suffix is the string ""d"". No pair can be chosen, so the answer is ""d"". In the second example, for the longest suffix ""abbcdddeaaffdfouurtytwoo"" choose three pairs \((11, 12)\), \((16, 17)\), \((23, 24)\) and we obtain ""abbcdddeaadfortytw""
Input: abcdd | Output: 3 abc 2 bc 1 c 0 1 d
Master
4
800
131
308
14
1,282
C
1282C
C. Petya and Exam
1,800
greedy; sortings; two pointers
Petya has come to the math exam and wants to solve as many problems as possible. He prepared and carefully studied the rules by which the exam passes.The exam consists of \(n\) problems that can be solved in \(T\) minutes. Thus, the exam begins at time \(0\) and ends at time \(T\). Petya can leave the exam at any integer time from \(0\) to \(T\), inclusive.All problems are divided into two types: easy problems β€” Petya takes exactly \(a\) minutes to solve any easy problem; hard problems β€” Petya takes exactly \(b\) minutes (\(b > a\)) to solve any hard problem. Thus, if Petya starts solving an easy problem at time \(x\), then it will be solved at time \(x+a\). Similarly, if at a time \(x\) Petya starts to solve a hard problem, then it will be solved at time \(x+b\).For every problem, Petya knows if it is easy or hard. Also, for each problem is determined time \(t_i\) (\(0 \le t_i \le T\)) at which it will become mandatory (required). If Petya leaves the exam at time \(s\) and there is such a problem \(i\) that \(t_i \le s\) and he didn't solve it, then he will receive \(0\) points for the whole exam. Otherwise (i.e if he has solved all such problems for which \(t_i \le s\)) he will receive a number of points equal to the number of solved problems. Note that leaving at time \(s\) Petya can have both ""mandatory"" and ""non-mandatory"" problems solved.For example, if \(n=2\), \(T=5\), \(a=2\), \(b=3\), the first problem is hard and \(t_1=3\) and the second problem is easy and \(t_2=2\). Then: if he leaves at time \(s=0\), then he will receive \(0\) points since he will not have time to solve any problems; if he leaves at time \(s=1\), he will receive \(0\) points since he will not have time to solve any problems; if he leaves at time \(s=2\), then he can get a \(1\) point by solving the problem with the number \(2\) (it must be solved in the range from \(0\) to \(2\)); if he leaves at time \(s=3\), then he will receive \(0\) points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time \(s=4\), then he will receive \(0\) points since at this moment both problems will be mandatory, but he will not be able to solve both of them; if he leaves at time \(s=5\), then he can get \(2\) points by solving all problems. Thus, the answer to this test is \(2\).Help Petya to determine the maximal number of points that he can receive, before leaving the exam.
The first line contains the integer \(m\) (\(1 \le m \le 10^4\)) β€” the number of test cases in the test.The next lines contain a description of \(m\) test cases. The first line of each test case contains four integers \(n, T, a, b\) (\(2 \le n \le 2\cdot10^5\), \(1 \le T \le 10^9\), \(1 \le a < b \le 10^9\)) β€” the number of problems, minutes given for the exam and the time to solve an easy and hard problem, respectively.The second line of each test case contains \(n\) numbers \(0\) or \(1\), separated by single space: the \(i\)-th number means the type of the \(i\)-th problem. A value of \(0\) means that the problem is easy, and a value of \(1\) that the problem is hard.The third line of each test case contains \(n\) integers \(t_i\) (\(0 \le t_i \le T\)), where the \(i\)-th number means the time at which the \(i\)-th problem will become mandatory.It is guaranteed that the sum of \(n\) for all test cases does not exceed \(2\cdot10^5\).
Print the answers to \(m\) test cases. For each set, print a single integer β€” maximal number of points that he can receive, before leaving the exam.
Input: 10 3 5 1 3 0 0 1 2 1 4 2 5 2 3 1 0 3 2 1 20 2 4 0 16 6 20 2 5 1 1 0 1 0 0 0 8 2 9 11 6 4 16 3 6 1 0 1 1 8 3 5 6 6 20 3 6 0 1 0 0 1 0 20 11 3 20 16 17 7 17 1 6 1 1 0 1 0 0 0 1 7 0 11 10 15 10 6 17 2 6 0 0 1 0 0 1 7 6 3 7 10 12 5 17 2 5 1 1 1 1 0 17 11 10 6 4 1 1 1 2 0 1 | Output: 3 2 1 0 1 4 0 1 2 1
Medium
3
2,449
949
148
12
1,464
F
1464F
F. My Beautiful Madness
3,500
data structures; trees
You are given a tree. We will consider simple paths on it. Let's denote path between vertices \(a\) and \(b\) as \((a, b)\). Let \(d\)-neighborhood of a path be a set of vertices of the tree located at a distance \(\leq d\) from at least one vertex of the path (for example, \(0\)-neighborhood of a path is a path itself). Let \(P\) be a multiset of the tree paths. Initially, it is empty. You are asked to maintain the following queries: \(1\) \(u\) \(v\) β€” add path \((u, v)\) into \(P\) (\(1 \leq u, v \leq n\)). \(2\) \(u\) \(v\) β€” delete path \((u, v)\) from \(P\) (\(1 \leq u, v \leq n\)). Notice that \((u, v)\) equals to \((v, u)\). For example, if \(P = \{(1, 2), (1, 2)\}\), than after query \(2\) \(2\) \(1\), \(P = \{(1, 2)\}\). \(3\) \(d\) β€” if intersection of all \(d\)-neighborhoods of paths from \(P\) is not empty output ""Yes"", otherwise output ""No"" (\(0 \leq d \leq n - 1\)).
The first line contains two integers \(n\) and \(q\) β€” the number of vertices in the tree and the number of queries, accordingly (\(1 \leq n \leq 2 \cdot 10^5\), \(2 \leq q \leq 2 \cdot 10^5\)).Each of the following \(n - 1\) lines contains two integers \(x_i\) and \(y_i\) β€” indices of vertices connected by \(i\)-th edge (\(1 \le x_i, y_i \le n\)).The following \(q\) lines contain queries in the format described in the statement.It's guaranteed that: for a query \(2\) \(u\) \(v\), path \((u, v)\) (or \((v, u)\)) is present in \(P\), for a query \(3\) \(d\), \(P \neq \varnothing\), there is at least one query of the third type.
For each query of the third type output answer on a new line.
Input: 1 4 1 1 1 1 1 1 2 1 1 3 0 | Output: Yes
Master
2
897
634
61
14
369
D
369D
D. Valera and Fools
2,200
dfs and similar; dp; graphs; shortest paths
One fine morning, n fools lined up in a row. After that, they numbered each other with numbers from 1 to n, inclusive. Each fool got a unique number. The fools decided not to change their numbers before the end of the fun.Every fool has exactly k bullets and a pistol. In addition, the fool number i has probability of pi (in percent) that he kills the fool he shoots at.The fools decided to have several rounds of the fun. Each round of the fun looks like this: each currently living fool shoots at another living fool with the smallest number (a fool is not stupid enough to shoot at himself). All shots of the round are perfomed at one time (simultaneously). If there is exactly one living fool, he does not shoot.Let's define a situation as the set of numbers of all the living fools at the some time. We say that a situation is possible if for some integer number j (0 ≀ j ≀ k) there is a nonzero probability that after j rounds of the fun this situation will occur.Valera knows numbers p1, p2, ..., pn and k. Help Valera determine the number of distinct possible situations.
The first line contains two integers n, k (1 ≀ n, k ≀ 3000) β€” the initial number of fools and the number of bullets for each fool.The second line contains n integers p1, p2, ..., pn (0 ≀ pi ≀ 100) β€” the given probabilities (in percent).
Print a single number β€” the answer to the problem.
In the first sample, any situation is possible, except for situation {1, 2}.In the second sample there is exactly one fool, so he does not make shots.In the third sample the possible situations are {1, 2} (after zero rounds) and the ""empty"" situation {} (after one round).In the fourth sample, the only possible situation is {1, 2, 3}.
Input: 3 350 50 50 | Output: 7
Hard
4
1,080
236
50
3
1,793
B
1793B
B. Fedya and Array
1,100
constructive algorithms; math
For his birthday recently Fedya was given an array \(a\) of \(n\) integers arranged in a circle, For each pair of neighboring numbers (\(a_1\) and \(a_2\), \(a_2\) and \(a_3\), \(\ldots\), \(a_{n - 1}\) and \(a_n\), \(a_n\) and \(a_1\)) the absolute difference between them is equal to \(1\).Let's call a local maximum an element, which is greater than both of its neighboring elements. Also call a local minimum an element, which is less than both of its neighboring elements. Note, that elements \(a_1\) and \(a_n\) are neighboring elements.Unfortunately, Fedya lost an array, but he remembered in it the sum of local maximums \(x\) and the sum of local minimums \(y\).Given \(x\) and \(y\), help Fedya find any matching array of minimum length.
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 1000\)). Description of the test cases follows.Each line of each test case contain two integers \(x\) and \(y\) (\(-10^{9} \le y < x \le 10^{9}\)) β€” the sum of local maximums and the sum of local minimums, respectively.
For each test case, in the first line print one integer \(n\) β€” the minimum length of matching arrays.In the second line print \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(-10^{9} \leqslant a_i \leqslant 10^{9}\)) β€” the array elements such that the the absolute difference between each pair of neighboring is equal to \(1\).If there are multiple solutions, print any of them.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^{5}\).
In the first test case, the local maximums are the numbers at \(3, 7\) and \(10\) positions, and the local minimums are the numbers at \(1, 6\) and \(8\) positions. \(x = a_3 + a_7 + a_{10} = 2 + 0 + 1 = 3\), \(y = a_1 + a_6 + a_8 = 0 + (-1) + (-1) = -2\).In the second test case, the local maximums are the numbers at \(2\) and \(10\) positions, and the local minimums are the numbers at \(1\) and \(3\) positions. \(x = a_2 + a_{10} = -1 + 5 = 4\), \(y = a_1 + a_3 = -2 + (-2) = -4\).In the third test case, the local maximums are the numbers at \(1\) and \(5\) positions, and the local minimums are the numbers at \(3\) and \(6\) positions.
Input: 43 -24 -42 -15 -3 | Output: 10 0 1 2 1 0 -1 0 -1 0 1 16 -2 -1 -2 -1 0 1 2 3 4 5 4 3 2 1 0 -1 6 1 0 -1 0 1 0 16 2 3 2 1 0 -1 0 -1 0 -1 0 1 2 1 0 1
Easy
2
747
329
468
17
2,045
G
2045G
G. X Aura
2,200
graphs; math; shortest paths
Mount ICPC can be represented as a grid of \(R\) rows (numbered from \(1\) to \(R\)) and \(C\) columns (numbered from \(1\) to \(C\)). The cell located at row \(r\) and column \(c\) is denoted as \((r, c)\) and has a height of \(H_{r, c}\). Two cells are adjacent to each other if they share a side. Formally, \((r, c)\) is adjacent to \((r-1, c)\), \((r+1, c)\), \((r, c-1)\), and \((r, c+1)\), if any exists.You can move only between adjacent cells, and each move comes with a penalty. With an aura of an odd positive integer \(X\), moving from a cell with height \(h_1\) to a cell with height \(h_2\) gives you a penalty of \((h_1 - h_2)^X\). Note that the penalty can be negative.You want to answer \(Q\) independent scenarios. In each scenario, you start at the starting cell \((R_s, C_s)\) and you want to go to the destination cell \((R_f, C_f)\) with minimum total penalty. In some scenarios, the total penalty might become arbitrarily small; such a scenario is called invalid. Find the minimum total penalty to move from the starting cell to the destination cell, or determine if the scenario is invalid.
The first line consists of three integers \(R\) \(C\) \(X\) (\(1 \leq R, C \leq 1000; 1 \leq X \leq 9; X\) is an odd integer).Each of the next \(R\) lines consists of a string \(H_r\) of length \(C\). Each character in \(H_r\) is a number from 0 to 9. The \(c\)-th character of \(H_r\) represents the height of cell \((r, c)\), or \(H_{r, c}\).The next line consists of an integer \(Q\) (\(1 \leq Q \leq 100\,000)\).Each of the next \(Q\) lines consists of four integers \(R_s\) \(C_s\) \(R_f\) \(C_f\) (\(1 \leq R_s, R_f \leq R; 1 \leq C_s, C_f \leq C\)).
For each scenario, output the following in a single line. If the scenario is invalid, output INVALID. Otherwise, output a single integer representing the minimum total penalty to move from the starting cell to the destination cell.
Explanation for the sample input/output #1For the first scenario, one of the solutions is to move as follows: \((1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4)\). The total penalty of this solution is \((3 - 4)^1 + (4 - 3)^1 + (3 - 6)^1 + (6 - 8)^1 + (8 - 1)^1 = 2\).Explanation for the sample input/output #2For the first scenario, the cycle \((1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (1, 2) \rightarrow (1, 1)\) has a penalty of \((1 - 2)^5 + (2 - 0)^5 + (0 - 9)^5 + (9 - 1)^5 = -26250\). You can keep repeating this cycle to make your total penalty arbitrarily small. Similarly, for the second scenario, you can move to \((1, 1)\) first, then repeat the same cycle.
Input: 3 4 1 3359 4294 3681 5 1 1 3 4 3 3 2 1 2 2 1 4 1 3 3 2 1 1 1 1 | Output: 2 4 -7 -1 0
Hard
3
1,113
556
231
20
505
E
505E
E. Mr. Kitayuta vs. Bamboos
2,900
binary search; greedy
Mr. Kitayuta's garden is planted with n bamboos. (Bamboos are tall, fast-growing tropical plants with hollow stems.) At the moment, the height of the i-th bamboo is hi meters, and it grows ai meters at the end of each day. Actually, Mr. Kitayuta hates these bamboos. He once attempted to cut them down, but failed because their stems are too hard. Mr. Kitayuta have not given up, however. He has crafted Magical Hammer with his intelligence to drive them into the ground.He can use Magical Hammer at most k times during each day, due to his limited Magic Power. Each time he beat a bamboo with Magical Hammer, its height decreases by p meters. If the height would become negative by this change, it will become 0 meters instead (it does not disappear). In other words, if a bamboo whose height is h meters is beaten with Magical Hammer, its new height will be max(0, h - p) meters. It is possible to beat the same bamboo more than once in a day.Mr. Kitayuta will fight the bamboos for m days, starting today. His purpose is to minimize the height of the tallest bamboo after m days (that is, m iterations of ""Mr. Kitayuta beats the bamboos and then they grow""). Find the lowest possible height of the tallest bamboo after m days.
The first line of the input contains four space-separated integers n, m, k and p (1 ≀ n ≀ 105, 1 ≀ m ≀ 5000, 1 ≀ k ≀ 10, 1 ≀ p ≀ 109). They represent the number of the bamboos in Mr. Kitayuta's garden, the duration of Mr. Kitayuta's fight in days, the maximum number of times that Mr. Kitayuta beat the bamboos during each day, and the power of Magic Hammer, respectively.The following n lines describe the properties of the bamboos. The i-th of them (1 ≀ i ≀ n) contains two space-separated integers hi and ai (0 ≀ hi ≀ 109, 1 ≀ ai ≀ 109), denoting the initial height and the growth rate of the i-th bamboo, respectively.
Print the lowest possible height of the tallest bamboo after m days.
Input: 3 1 2 510 1010 1015 2 | Output: 17
Master
2
1,231
622
68
5
386
D
386D
D. Game with Points
2,100
dp; graphs; implementation; shortest paths
You are playing the following game. There are n points on a plane. They are the vertices of a regular n-polygon. Points are labeled with integer numbers from 1 to n. Each pair of distinct points is connected by a diagonal, which is colored in one of 26 colors. Points are denoted by lowercase English letters. There are three stones positioned on three distinct vertices. All stones are the same. With one move you can move the stone to another free vertex along some diagonal. The color of this diagonal must be the same as the color of the diagonal, connecting another two stones. Your goal is to move stones in such way that the only vertices occupied by stones are 1, 2 and 3. You must achieve such position using minimal number of moves. Write a program which plays this game in an optimal way.
In the first line there is one integer n (3 ≀ n ≀ 70) β€” the number of points. In the second line there are three space-separated integer from 1 to n β€” numbers of vertices, where stones are initially located.Each of the following n lines contains n symbols β€” the matrix denoting the colors of the diagonals. Colors are denoted by lowercase English letters. The symbol j of line i denotes the color of diagonal between points i and j. Matrix is symmetric, so j-th symbol of i-th line is equal to i-th symbol of j-th line. Main diagonal is filled with '*' symbols because there is no diagonal, connecting point to itself.
If there is no way to put stones on vertices 1, 2 and 3, print -1 on a single line. Otherwise, on the first line print minimal required number of moves and in the next lines print the description of each move, one move per line. To describe a move print two integers. The point from which to remove the stone, and the point to which move the stone. If there are several optimal solutions, print any of them.
In the first example we can move stone from point 4 to point 1 because this points are connected by the diagonal of color 'a' and the diagonal connection point 2 and 3, where the other stones are located, are connected by the diagonal of the same color. After that stones will be on the points 1, 2 and 3.
Input: 42 3 4*abaa*abba*babb* | Output: 14 1
Hard
4
799
618
407
3
746
C
746C
C. Tram
1,600
constructive algorithms; implementation; math
The tram in Berland goes along a straight line from the point 0 to the point s and back, passing 1 meter per t1 seconds in both directions. It means that the tram is always in the state of uniform rectilinear motion, instantly turning around at points x = 0 and x = s.Igor is at the point x1. He should reach the point x2. Igor passes 1 meter per t2 seconds. Your task is to determine the minimum time Igor needs to get from the point x1 to the point x2, if it is known where the tram is and in what direction it goes at the moment Igor comes to the point x1.Igor can enter the tram unlimited number of times at any moment when his and the tram's positions coincide. It is not obligatory that points in which Igor enter and exit the tram are integers. Assume that any boarding and unboarding happens instantly. Igor can move arbitrary along the line (but not faster than 1 meter per t2 seconds). He can also stand at some point for some time.
The first line contains three integers s, x1 and x2 (2 ≀ s ≀ 1000, 0 ≀ x1, x2 ≀ s, x1 β‰  x2) β€” the maximum coordinate of the point to which the tram goes, the point Igor is at, and the point he should come to.The second line contains two integers t1 and t2 (1 ≀ t1, t2 ≀ 1000) β€” the time in seconds in which the tram passes 1 meter and the time in seconds in which Igor passes 1 meter.The third line contains two integers p and d (1 ≀ p ≀ s - 1, d is either 1 or ) β€” the position of the tram in the moment Igor came to the point x1 and the direction of the tram at this moment. If , the tram goes in the direction from the point s to the point 0. If d = 1, the tram goes in the direction from the point 0 to the point s.
Print the minimum time in seconds which Igor needs to get from the point x1 to the point x2.
In the first example it is profitable for Igor to go by foot and not to wait the tram. Thus, he has to pass 2 meters and it takes 8 seconds in total, because he passes 1 meter per 4 seconds. In the second example Igor can, for example, go towards the point x2 and get to the point 1 in 6 seconds (because he has to pass 3 meters, but he passes 1 meters per 2 seconds). At that moment the tram will be at the point 1, so Igor can enter the tram and pass 1 meter in 1 second. Thus, Igor will reach the point x2 in 7 seconds in total.
Input: 4 2 43 41 1 | Output: 8
Medium
3
942
719
92
7
2,001
E1
2001E1
E1. Deterministic Heap (Easy Version)
2,400
combinatorics; dp; math; trees
This is the easy version of the problem. The difference between the two versions is the definition of deterministic max-heap, time limit, and constraints on \(n\) and \(t\). You can make hacks only if both versions of the problem are solved.Consider a perfect binary tree with size \(2^n - 1\), with nodes numbered from \(1\) to \(2^n-1\) and rooted at \(1\). For each vertex \(v\) (\(1 \le v \le 2^{n - 1} - 1\)), vertex \(2v\) is its left child and vertex \(2v + 1\) is its right child. Each node \(v\) also has a value \(a_v\) assigned to it.Define the operation \(\mathrm{pop}\) as follows: initialize variable \(v\) as \(1\); repeat the following process until vertex \(v\) is a leaf (i.e. until \(2^{n - 1} \le v \le 2^n - 1\)); among the children of \(v\), choose the one with the larger value on it and denote such vertex as \(x\); if the values on them are equal (i.e. \(a_{2v} = a_{2v + 1}\)), you can choose any of them; assign \(a_x\) to \(a_v\) (i.e. \(a_v := a_x\)); assign \(x\) to \(v\) (i.e. \(v := x\)); assign \(-1\) to \(a_v\) (i.e. \(a_v := -1\)). Then we say the \(\mathrm{pop}\) operation is deterministic if there is a unique way to do such operation. In other words, \(a_{2v} \neq a_{2v + 1}\) would hold whenever choosing between them.A binary tree is called a max-heap if for every vertex \(v\) (\(1 \le v \le 2^{n - 1} - 1\)), both \(a_v \ge a_{2v}\) and \(a_v \ge a_{2v + 1}\) hold.A max-heap is deterministic if the \(\mathrm{pop}\) operation is deterministic to the heap when we do it for the first time.Initially, \(a_v := 0\) for every vertex \(v\) (\(1 \le v \le 2^n - 1\)), and your goal is to count the number of different deterministic max-heaps produced by applying the following operation \(\mathrm{add}\) exactly \(k\) times: Choose an integer \(v\) (\(1 \le v \le 2^n - 1\)) and, for every vertex \(x\) on the path between \(1\) and \(v\), add \(1\) to \(a_x\). Two heaps are considered different if there is a node which has different values in the heaps. Since the answer might be large, print it modulo \(p\).
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 500\)). The description of the test cases follows.The first line of each test case contains three integers \(n, k, p\) (\(1 \le n, k \le 500\), \(10^8 \le p \le 10^9\), \(p\) is a prime).It is guaranteed that the sum of \(n\) and the sum of \(k\) over all test cases does not exceed \(500\).
For each test case, output a single line containing an integer: the number of different deterministic max-heaps produced by applying the aforementioned operation \(\mathrm{add}\) exactly \(k\) times, modulo \(p\).
For the first testcase, there is only one way to generate \(a\), and such sequence is a deterministic max-heap, so the answer is \(1\).For the second testcase, if we choose \(v = 1\) and do the operation, we would have \(a = [1, 0, 0]\), and since \(a_2 = a_3\), we can choose either of them when doing the first \(\mathrm{pop}\) operation, so such heap is not a deterministic max-heap. And if we choose \(v = 2\), we would have \(a = [1, 1, 0]\), during the first \(\mathrm{pop}\), the following would happen: initialize \(v\) as \(1\) since \(a_{2v} > a_{2v + 1}\), choose \(2v\) as \(x\), then \(x = 2\) assign \(a_x\) to \(a_v\), then \(a = [1, 1, 0]\) assign \(x\) to \(v\), then \(v = 2\) since \(v\) is a leaf, assign \(-1\) to \(a_v\), then \(a = [1, -1, 0]\) Since the first \(\mathrm{pop}\) operation is deterministic, this is a deterministic max-heap. Also, if we choose \(v = 3\), \(a\) would be a deterministic max-heap, so the answer is \(2\).
Input: 71 13 9982443532 1 9982443533 2 9982448533 3 9982443533 4 1000000374 2 1000000394 3 100000037 | Output: 1 2 12 52 124 32 304
Expert
4
2,053
401
213
20
1,857
D
1857D
D. Strong Vertices
1,300
math; sortings; trees
Given two arrays \(a\) and \(b\), both of length \(n\). Elements of both arrays indexed from \(1\) to \(n\). You are constructing a directed graph, where edge from \(u\) to \(v\) (\(u\neq v\)) exists if \(a_u-a_v \ge b_u-b_v\).A vertex \(V\) is called strong if there exists a path from \(V\) to all other vertices.A path in a directed graph is a chain of several vertices, connected by edges, such that moving from the vertex \(u\), along the directions of the edges, the vertex \(v\) can be reached.Your task is to find all strong vertices.For example, if \(a=[3,1,2,4]\) and \(b=[4,3,2,1]\), the graph will look like this: The graph has only one strong vertex with number \(4\)
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\) (\(2 \le n \le 2\cdot 10^5\)) β€” the length of \(a\) and \(b\).The second line of each test case contains \(n\) integers \(a_1,a_2 \dots a_n\) (\(-10^9 \le a_i \le 10^9\)) β€” the array \(a\).The third line of each test case contains \(n\) integers \(b_1,b_2 \dots b_n\) (\(-10^9 \le b_i \le 10^9\)) β€” the array \(b\).It is guaranteed that the sum of \(n\) for all test cases does not exceed \(2\cdot 10^5\).
For each test case, output two lines: in the first line, output the number of strong vertices, and in the second line, output all strong vertices in ascending order.
The first sample is covered in the problem statement.For the second sample, the graph looks like this: The graph has two strong vertices with numbers \(3\) and \(5\). Note that there is a bidirectional edge between vertices \(3\) and \(5\). In the third sample, the vertices are connected by a single directed edge from vertex \(2\) to vertex \(1\), so the only strong vertex is \(2\).In the fourth sample, all vertices are connected to each other by bidirectional edges, so there is a path from every vertex to any other vertex.
Input: 543 1 2 44 3 2 151 2 4 1 25 2 3 3 121 22 130 2 11 3 235 7 4-2 -3 -6 | Output: 1 4 2 3 5 1 2 3 1 2 3 2 2 3
Easy
3
680
553
165
18
44
E
44E
E. Anfisa the Monkey
1,400
dp
Anfisa the monkey learns to type. She is yet unfamiliar with the ""space"" key and can only type in lower-case Latin letters. Having typed for a fairly long line, Anfisa understood that it would be great to divide what she has written into k lines not shorter than a and not longer than b, for the text to resemble human speech more. Help Anfisa.
The first line contains three integers k, a and b (1 ≀ k ≀ 200, 1 ≀ a ≀ b ≀ 200). The second line contains a sequence of lowercase Latin letters β€” the text typed by Anfisa. It is guaranteed that the given line is not empty and its length does not exceed 200 symbols.
Print k lines, each of which contains no less than a and no more than b symbols β€” Anfisa's text divided into lines. It is not allowed to perform any changes in the text, such as: deleting or adding symbols, changing their order, etc. If the solution is not unique, print any of them. If there is no solution, print ""No solution"" (without quotes).
Input: 3 2 5abrakadabra | Output: abrakadabra
Easy
1
346
266
348
0
2,071
E
2071E
E. LeaFall
2,600
combinatorics; dp; probabilities; trees
You are given a tree\(^{\text{βˆ—}}\) with \(n\) vertices. Over time, each vertex \(i\) (\(1 \le i \le n\)) has a probability of \(\frac{p_i}{q_i}\) of falling. Determine the expected value of the number of unordered pairs\(^{\text{†}}\) of distinct vertices that become leaves\(^{\text{‑}}\) in the resulting forest\(^{\text{Β§}}\), modulo \(998\,244\,353\).Note that when vertex \(v\) falls, it is removed along with all edges connected to it. However, adjacent vertices remain unaffected by the fall of \(v\).\(^{\text{βˆ—}}\)A tree is a connected graph without cycles. \(^{\text{†}}\)An unordered pair is a collection of two elements where the order in which the elements appear does not matter. For example, the unordered pair \((1, 2)\) is considered the same as \((2, 1)\).\(^{\text{‑}}\)A leaf is a vertex that is connected to exactly one edge.\(^{\text{Β§}}\)A forest is a graph without cycles
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)).The \(i\)-th line of the following \(n\) lines contains two integers \(p_i\) and \(q_i\) (\(1 \le p_i < q_i < 998\,244\,353\)).Each of the following \(n - 1\) lines contains two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \neq v\)) β€” the indices of the vertices connected by an edge.It is guaranteed that the given edges form a tree.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
For each test case, output a single integer β€” the expected value of the number of unordered pairs of distinct vertices that become leaves in the resulting forest modulo \(998\,244\,353\).Formally, let \(M = 998\,244\,353\). It can be shown that the exact answer can be expressed as an irreducible fraction \(\frac{p}{q}\), where \(p\) and \(q\) are integers and \(q \not \equiv 0 \pmod{M}\). Output the integer equal to \(p \cdot q^{-1} \bmod M\). In other words, output such an integer \(x\) that \(0 \le x < M\) and \(x \cdot q \equiv p \pmod{M}\).
In the first test case, only one vertex is in the tree, which is not a leaf, so the answer is \(0\).In the second test case, the tree is shown below. Vertices that have not fallen are denoted in bold. Let us examine the following three cases: We arrive at this configuration with a probability of \(\left( \frac{1}{2} \right) ^3\), where the only unordered pair of distinct leaf vertices is \((2, 3)\). We arrive at this configuration with a probability of \(\left( \frac{1}{2} \right) ^3\), where the only unordered pair of distinct leaf vertices is \((2, 1)\). We arrive at this configuration with a probability of \(\left( \frac{1}{2} \right) ^3\), where the only unordered pair of distinct leaf vertices is \((1, 3)\). All remaining cases contain no unordered pairs of distinct leaf vertices. Hence, the answer is \(\frac{1 + 1 + 1}{8} = \frac{3}{8}\), which is equal to \(623\,902\,721\) modulo \(998\,244\,353\).
Input: 511 231 21 21 21 22 331 31 51 31 22 31998244351 998244352610 177 136 112 1010 195 134 33 61 43 53 2 | Output: 0 623902721 244015287 0 799215919
Expert
4
896
675
550
20
1,700
F
1700F
F. Puzzle
2,600
constructive algorithms; dp; greedy
Pupils Alice and Ibragim are best friends. It's Ibragim's birthday soon, so Alice decided to gift him a new puzzle. The puzzle can be represented as a matrix with \(2\) rows and \(n\) columns, every element of which is either \(0\) or \(1\). In one move you can swap two values in neighboring cells.More formally, let's number rows \(1\) to \(2\) from top to bottom, and columns \(1\) to \(n\) from left to right. Also, let's denote a cell in row \(x\) and column \(y\) as \((x, y)\). We consider cells \((x_1, y_1)\) and \((x_2, y_2)\) neighboring if \(|x_1 - x_2| + |y_1 - y_2| = 1\).Alice doesn't like the way in which the cells are currently arranged, so she came up with her own arrangement, with which she wants to gift the puzzle to Ibragim. Since you are her smartest friend, she asked you to help her find the minimal possible number of operations in which she can get the desired arrangement. Find this number, or determine that it's not possible to get the new arrangement.
The first line contains an integer \(n\) (\(1 \leq n \leq 200\,000\)) β€” the number of columns in the puzzle.Following two lines describe the current arrangement on the puzzle. Each line contains \(n\) integers, every one of which is either \(0\) or \(1\).The last two lines describe Alice's desired arrangement in the same format.
If it is possible to get the desired arrangement, print the minimal possible number of steps, otherwise print \(-1\).
In the first example the following sequence of swaps will suffice: \((2, 1), (1, 1)\), \((1, 2), (1, 3)\), \((2, 2), (2, 3)\), \((1, 4), (1, 5)\), \((2, 5), (2, 4)\). It can be shown that \(5\) is the minimal possible answer in this case.In the second example no matter what swaps you do, you won't get the desired arrangement, so the answer is \(-1\).
Input: 5 0 1 0 1 0 1 1 0 0 1 1 0 1 0 1 0 0 1 1 0 | Output: 5
Expert
3
984
330
117
17
2,120
C
2120C
C. Divine Tree
1,400
constructive algorithms; greedy; math; sortings; trees
Harshith attained enlightenment in Competitive Programming by training under a Divine Tree. A divine tree is a rooted tree\(^{\text{βˆ—}}\) with \(n\) nodes, labelled from \(1\) to \(n\). The divineness of a node \(v\), denoted \(d(v)\), is defined as the smallest node label on the unique simple path from the root to node \(v\).Aryan, being a hungry Competitive Programmer, asked Harshith to pass on the knowledge. Harshith agreed on the condition that Aryan would be given two positive integers \(n\) and \(m\), and he had to construct a divine tree with \(n\) nodes such that the total divineness of the tree is \(m\), i.e., \(\displaystyle\sum\limits_{i=1}^n d(i)=m\). If no such tree exists, Aryan must report that it is impossible.Desperate for knowledge, Aryan turned to you for help in completing this task. As a good friend of his, help him solve the task.\(^{\text{βˆ—}}\)A tree is a connected graph without cycles. A rooted tree is a tree where one vertex is special and called the root.
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 \(n\) and \(m\) (\(1 \le n \le 10^6\), \(1 \le m \le 10^{12}\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^6\).
For each test case, output a single integer \(k\) in one line β€” the root of the tree.Then \(n-1\) lines follow, each containing a description of an edge of the tree β€” a pair of positive integers \(u_i,v_i\) (\(1\le u_i,v_i\le n\), \(u_i\ne v_i\)), denoting the \(i\)-th edge connects vertices \(u_i\) and \(v_i\).The edges and vertices of the edges can be printed in any order. If there are multiple solutions, print any of them.If there is no solution, print ""-1"" instead.
In the first test case, there is a single node with a value of \(1\), so getting a sum of \(2\) is impossible.In the second test case, getting a sum of \(6\) is possible with the given tree rooted at \(3\).
Input: 21 24 6 | Output: -1 3 3 1 1 2 2 4
Easy
5
995
365
475
21
624
A
624A
A. Save Luke
800
math
Luke Skywalker got locked up in a rubbish shredder between two presses. R2D2 is already working on his rescue, but Luke needs to stay alive as long as possible. For simplicity we will assume that everything happens on a straight line, the presses are initially at coordinates 0 and L, and they move towards each other with speed v1 and v2, respectively. Luke has width d and is able to choose any position between the presses. Luke dies as soon as the distance between the presses is less than his width. Your task is to determine for how long Luke can stay alive.
The first line of the input contains four integers d, L, v1, v2 (1 ≀ d, L, v1, v2 ≀ 10 000, d < L) β€” Luke's width, the initial position of the second press and the speed of the first and second presses, respectively.
Print a single real value β€” the maximum period of time Luke can stay alive for. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6. Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .
In the first sample Luke should stay exactly in the middle of the segment, that is at coordinates [2;4], as the presses move with the same speed.In the second sample he needs to occupy the position . In this case both presses move to his edges at the same time.
Input: 2 6 2 2 | Output: 1.00000000000000000000
Beginner
1
564
216
313
6
1,805
C
1805C
C. Place for a Selfie
1,400
binary search; data structures; geometry; math
The universe is a coordinate plane. There are \(n\) space highways, each of which is a straight line \(y=kx\) passing through the origin \((0, 0)\). Also, there are \(m\) asteroid belts on the plane, which we represent as open upwards parabolas, i. e. graphs of functions \(y=ax^2+bx+c\), where \(a > 0\).You want to photograph each parabola. To do this, for each parabola you need to choose a line that does not intersect this parabola and does not touch it. You can select the same line for different parabolas. Please find such a line for each parabola, or determine that there is no such line.
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 \(2\) integers \(n\) and \(m\) (\(1 \le n, m \le 10^5\)) β€”the number of lines and parabolas, respectively.Each of the next \(n\) lines contains one integer \(k\) (\(|k| \le 10^8\)), denoting a line that is described with the equation \(y=kx\). The lines are not necessarily distinct, \(k\) can be equal to \(0\).Each of the next \(m\) lines contains \(3\) integers \(a\), \(b\), and \(c\) (\(a, |b|, |c| \le 10^8\), \(a > 0\)) β€” coefficients of equations of the parabolas \(ax^2+bx+c\). The parabolas are not necessarily distinct.It is guaranteed that the sum \(n\) over all test cases does not exceed \(10^5\), and the sum \(m\) over all test cases also does not exceed \(10^5\).
For each test case, output the answers for each parabola in the given order. If there is a line that does not intersect the given parabola and doesn't touch it, print on a separate line the word ""YES"", and then on a separate line the number \(k\) β€” the coefficient of this line. If there are several answers, print any of them. If the line does not exist, print one word ""NO"".You can output the answer in any case (upper or lower). For example, the strings ""yEs"", ""yes"", ""Yes"", and ""YES"" will be recognized as positive responses.The empty lines in the output in the example are given only for illustration, you do not need to output them (but you can).
In the first test case, both parabolas do not intersect the only given line \(y=1\cdot x\), so the answer is two numbers \(1\). In the second test case, the line \(y=x\) and the parabola \(2x^2+5x+1\) intersect, and also the line \(y=4x\) and the parabola \(x^2+2x+1\) touch, so these pairs do not satisfy the condition. So for the first parabola, the answer is \(1\) (\(y=1x\)), and for the second parabola β€” \(4\). In the third test set, the line and the parabola intersect, so the answer is ""NO"".
Input: 51 211 -1 21 -1 32 2141 2 12 5 11 101 0 01 1100000000100000000 100000000 1000000002 3022 2 11 -2 11 -2 -1 | Output: YES 1 YES 1 YES 1 YES 4 NO YES 100000000 YES 0 NO NO
Easy
4
597
883
664
18
130
C
130C
C. Decimal sum
1,500
*special
You are given an array of integer numbers. Calculate the sum of its elements.
The first line of the input contains an integer n (1 ≀ n ≀ 100) β€” the size of the array. Next n lines contain the elements of the array, one per line. Each element is an integer between 1 and 100, inclusive.
Output the sum of the elements of the array.
Input: 512345 | Output: 15
Medium
1
77
207
44
1
389
A
389A
A. Fox and Number Game
1,000
greedy; math
Fox Ciel is playing a game with numbers now. Ciel has n positive integers: x1, x2, ..., xn. She can do the following operation as many times as needed: select two different indexes i and j such that xi > xj hold, and then apply assignment xi = xi - xj. The goal is to make the sum of all numbers as small as possible.Please help Ciel to find this minimal sum.
The first line contains an integer n (2 ≀ n ≀ 100). Then the second line contains n integers: x1, x2, ..., xn (1 ≀ xi ≀ 100).
Output a single integer β€” the required minimal sum.
In the first example the optimal way is to do the assignment: x2 = x2 - x1.In the second example the optimal sequence of operations is: x3 = x3 - x2, x2 = x2 - x1.
Input: 21 2 | Output: 2
Beginner
2
359
125
51
3
1,512
F
1512F
F. Education
1,900
brute force; dp; greedy; implementation
Polycarp is wondering about buying a new computer, which costs \(c\) tugriks. To do this, he wants to get a job as a programmer in a big company.There are \(n\) positions in Polycarp's company, numbered starting from one. An employee in position \(i\) earns \(a[i]\) tugriks every day. The higher the position number, the more tugriks the employee receives. Initially, Polycarp gets a position with the number \(1\) and has \(0\) tugriks.Each day Polycarp can do one of two things: If Polycarp is in the position of \(x\), then he can earn \(a[x]\) tugriks. If Polycarp is in the position of \(x\) (\(x < n\)) and has at least \(b[x]\) tugriks, then he can spend \(b[x]\) tugriks on an online course and move to the position \(x+1\). For example, if \(n=4\), \(c=15\), \(a=[1, 3, 10, 11]\), \(b=[1, 2, 7]\), then Polycarp can act like this: On the first day, Polycarp is in the \(1\)-st position and earns \(1\) tugrik. Now he has \(1\) tugrik; On the second day, Polycarp is in the \(1\)-st position and move to the \(2\)-nd position. Now he has \(0\) tugriks; On the third day, Polycarp is in the \(2\)-nd position and earns \(3\) tugriks. Now he has \(3\) tugriks; On the fourth day, Polycarp is in the \(2\)-nd position and is transferred to the \(3\)-rd position. Now he has \(1\) tugriks; On the fifth day, Polycarp is in the \(3\)-rd position and earns \(10\) tugriks. Now he has \(11\) tugriks; On the sixth day, Polycarp is in the \(3\)-rd position and earns \(10\) tugriks. Now he has \(21\) tugriks; Six days later, Polycarp can buy himself a new computer. Find the minimum number of days after which Polycarp will be able to buy himself a new computer.
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)). Then \(t\) test cases follow.The first line of each test case contains two integers \(n\) and \(c\) (\(2 \le n \le 2 \cdot 10^5\), \(1 \le c \le 10^9\)) β€” the number of positions in the company and the cost of a new computer.The second line of each test case contains \(n\) integers \(a_1 \le a_2 \le \ldots \le a_n\) (\(1 \le a_i \le 10^9\)).The third line of each test case contains \(n - 1\) integer \(b_1, b_2, \ldots, b_{n-1}\) (\(1 \le b_i \le 10^9\)).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 minimum number of days after which Polycarp will be able to buy a new computer.
Input: 3 4 15 1 3 10 11 1 2 7 4 100 1 5 10 50 3 14 12 2 1000000000 1 1 1 | Output: 6 13 1000000000
Hard
4
1,664
621
110
15
1,914
E2
1914E2
E2. Game with Marbles (Hard Version)
1,400
games; greedy; sortings
The easy and hard versions of this problem differ only in the constraints on the number of test cases and \(n\). In the hard version, the number of test cases does not exceed \(10^4\), and the sum of values of \(n\) over all test cases does not exceed \(2 \cdot 10^5\). Furthermore, there are no additional constraints on \(n\) in a single test case.Recently, Alice and Bob were given marbles of \(n\) different colors by their parents. Alice has received \(a_1\) marbles of color \(1\), \(a_2\) marbles of color \(2\),..., \(a_n\) marbles of color \(n\). Bob has received \(b_1\) marbles of color \(1\), \(b_2\) marbles of color \(2\), ..., \(b_n\) marbles of color \(n\). All \(a_i\) and \(b_i\) are between \(1\) and \(10^9\).After some discussion, Alice and Bob came up with the following game: players take turns, starting with Alice. On their turn, a player chooses a color \(i\) such that both players have at least one marble of that color. The player then discards one marble of color \(i\), and their opponent discards all marbles of color \(i\). The game ends when there is no color \(i\) such that both players have at least one marble of that color.The score in the game is the difference between the number of remaining marbles that Alice has and the number of remaining marbles that Bob has at the end of the game. In other words, the score in the game is equal to \((A-B)\), where \(A\) is the number of marbles Alice has and \(B\) is the number of marbles Bob has at the end of the game. Alice wants to maximize the score, while Bob wants to minimize it.Calculate the score at the end of the game if both players play optimally.
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β€” the number of test cases.Each test case consists of three lines: the first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β€” the number of colors; the second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the number of marbles of the \(i\)-th color that Alice has; the third line contains \(n\) integers \(b_1, b_2, \dots, b_n\) (\(1 \le b_i \le 10^9\)), where \(b_i\) is the number of marbles of the \(i\)-th color that Bob has. Additional constraint on the input: the sum of \(n\) for all test cases does not exceed \(2 \cdot 10^5\).
For each test case, output a single integer β€” the score at the end of the game if both Alice and Bob act optimally.
In the first example, one way to achieve a score of \(1\) is as follows: Alice chooses color \(1\), discards \(1\) marble. Bob also discards \(1\) marble; Bob chooses color \(3\), discards \(1\) marble. Alice also discards \(1\) marble; Alice chooses color \(2\), discards \(1\) marble, and Bob discards \(2\) marble. As a result, Alice has \(a = [3, 1, 0]\) remaining, and Bob has \(b = [0, 0, 3]\) remaining. The score is \(3 + 1 - 3 = 1\).It can be shown that neither Alice nor Bob can achieve a better score if both play optimally.In the second example, Alice can first choose color \(1\), then Bob will choose color \(4\), after which Alice will choose color \(2\), and Bob will choose color \(3\). It can be shown that this is the optimal game.
Input: 534 2 11 2 441 20 1 20100 15 10 2051000000000 1000000000 1000000000 1000000000 10000000001 1 1 1 135 6 52 1 763 2 4 2 5 59 4 7 9 2 5 | Output: 1 -9 2999999997 8 -6
Easy
3
1,645
677
115
19
1,603
F
1603F
F. October 18, 2017
2,700
combinatorics; dp; implementation; math
It was October 18, 2017. Shohag, a melancholic soul, made a strong determination that he will pursue Competitive Programming seriously, by heart, because he found it fascinating. Fast forward to 4 years, he is happy that he took this road. He is now creating a contest on Codeforces. He found an astounding problem but has no idea how to solve this. Help him to solve the final problem of the round.You are given three integers \(n\), \(k\) and \(x\). Find the number, modulo \(998\,244\,353\), of integer sequences \(a_1, a_2, \ldots, a_n\) such that the following conditions are satisfied: \(0 \le a_i \lt 2^k\) for each integer \(i\) from \(1\) to \(n\). There is no non-empty subsequence in \(a\) such that the bitwise XOR of the elements of the subsequence is \(x\). A sequence \(b\) is a subsequence of a sequence \(c\) if \(b\) can be obtained from \(c\) by deletion of several (possibly, zero or all) elements.
The first line contains a single integer \(t\) (\(1 \le t \le 10^5\)) β€” the number of test cases.The first and only line of each test case contains three space-separated integers \(n\), \(k\), and \(x\) (\(1 \le n \le 10^9\), \(0 \le k \le 10^7\), \(0 \le x \lt 2^{\operatorname{min}(20, k)}\)).It is guaranteed that the sum of \(k\) over all test cases does not exceed \(5 \cdot 10^7\).
For each test case, print a single integer β€” the answer to the problem.
In the first test case, the valid sequences are \([1, 2]\), \([1, 3]\), \([2, 1]\), \([2, 3]\), \([3, 1]\) and \([3, 2]\).In the second test case, the only valid sequence is \([0, 0]\).
Input: 6 2 2 0 2 1 1 3 2 3 69 69 69 2017 10 18 5 7 0 | Output: 6 1 15 699496932 892852568 713939942
Master
4
918
387
71
16