contest_id
int32 1
2.13k
| index
stringclasses 62
values | problem_id
stringlengths 2
6
| title
stringlengths 0
67
| rating
int32 0
3.5k
| tags
stringlengths 0
139
| statement
stringlengths 0
6.96k
| input_spec
stringlengths 0
2.32k
| output_spec
stringlengths 0
1.52k
| note
stringlengths 0
5.06k
| sample_tests
stringlengths 0
1.02k
| difficulty_category
stringclasses 6
values | tag_count
int8 0
11
| statement_length
int32 0
6.96k
| input_spec_length
int16 0
2.32k
| output_spec_length
int16 0
1.52k
| contest_year
int16 0
21
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1,952 |
H
|
1952H
|
H. Palindrome
| 0 |
*special; implementation; strings
|
A palindrome is a string that reads the same in both directions, for example z, uwu, or moom.
|
The first line contains an integer \(t\) (\(1 \leq t \leq 100\)) β the number of testcases.The following \(t\) lines each contain a string of length at most \(100\) consisting of lowercase English letters.
|
For each test case, output ""YES"" or ""NO"", denoting the answer.
|
Input: 8actleradarracecarphpatcodercodeforcessteam | Output: NO NO YES YES NO NO YES YES
|
Beginner
| 3 | 93 | 205 | 66 | 19 |
|
662 |
D
|
662D
|
D. International Olympiad
| 2,000 |
constructive algorithms; greedy; implementation; math
|
International Abbreviation Olympiad takes place annually starting from 1989. Each year the competition receives an abbreviation of form IAO'y, where y stands for some number of consequent last digits of the current year. Organizers always pick an abbreviation with non-empty string y that has never been used before. Among all such valid abbreviations they choose the shortest one and announce it to be the abbreviation of this year's competition.For example, the first three Olympiads (years 1989, 1990 and 1991, respectively) received the abbreviations IAO'9, IAO'0 and IAO'1, while the competition in 2015 received an abbreviation IAO'15, as IAO'5 has been already used in 1995.You are given a list of abbreviations. For each of them determine the year it stands for.
|
The first line of the input contains a single integer n (1 β€ n β€ 1000) β the number of abbreviations to process. Then n lines follow, each containing a single abbreviation. It's guaranteed that each abbreviation contains at most nine digits.
|
For each abbreviation given in the input, find the year of the corresponding Olympiad.
|
Input: 5IAO'15IAO'2015IAO'1IAO'9IAO'0 | Output: 201512015199119891990
|
Hard
| 4 | 770 | 241 | 86 | 6 |
|
1,407 |
B
|
1407B
|
B. Big Vova
| 1,300 |
brute force; greedy; math; number theory
|
Alexander is a well-known programmer. Today he decided to finally go out and play football, but with the first hit he left a dent on the new Rolls-Royce of the wealthy businessman Big Vova. Vladimir has recently opened a store on the popular online marketplace ""Zmey-Gorynych"", and offers Alex a job: if he shows his programming skills by solving a task, he'll work as a cybersecurity specialist. Otherwise, he'll be delivering some doubtful products for the next two years.You're given \(n\) positive integers \(a_1, a_2, \dots, a_n\). Using each of them exactly at once, you're to make such sequence \(b_1, b_2, \dots, b_n\) that sequence \(c_1, c_2, \dots, c_n\) is lexicographically maximal, where \(c_i=GCD(b_1,\dots,b_i)\) - the greatest common divisor of the first \(i\) elements of \(b\). Alexander is really afraid of the conditions of this simple task, so he asks you to solve it.A sequence \(a\) is lexicographically smaller than a sequence \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a \ne b\); in the first position where \(a\) and \(b\) differ, the sequence \(a\) has a smaller element than the corresponding element in \(b\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^3\)). Description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^3\)) β the length of the sequence \(a\).The second line of each test case contains \(n\) integers \(a_1,\dots,a_n\) (\(1 \le a_i \le 10^3\)) β the sequence \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^3\).
|
For each test case output the answer in a single line β the desired sequence \(b\). If there are multiple answers, print any.
|
In the first test case of the example, there are only two possible permutations \(b\) β \([2, 5]\) and \([5, 2]\): for the first one \(c=[2, 1]\), for the second one \(c=[5, 1]\).In the third test case of the example, number \(9\) should be the first in \(b\), and \(GCD(9, 3)=3\), \(GCD(9, 8)=1\), so the second number of \(b\) should be \(3\).In the seventh test case of the example, first four numbers pairwise have a common divisor (a power of two), but none of them can be the first in the optimal permutation \(b\).
|
Input: 7 2 2 5 4 1 8 2 3 3 3 8 9 5 64 25 75 100 50 1 42 6 96 128 88 80 52 7 5 2 4 8 16 17 | Output: 5 2 8 2 1 3 9 3 8 100 50 25 75 64 42 128 96 80 88 52 7 17 2 4 8 16
|
Easy
| 4 | 1,181 | 486 | 125 | 14 |
81 |
C
|
81C
|
C. Average Score
| 1,700 |
greedy; math; sortings
|
After the educational reform Polycarp studies only two subjects at school, Safety Studies and PE (Physical Education). During the long months of the fourth term, he received n marks in them. When teachers wrote a mark in the journal, they didn't write in what subject the mark was for, they just wrote the mark.Now it's time to show the journal to his strict parents. Polycarp knows that recently at the Parent Meeting the parents were told that he received a Safety Studies marks and b PE marks (a + b = n). Now Polycarp wants to write a subject's name in front of each mark so that: there are exactly a Safety Studies marks, there are exactly b PE marks, the total average score in both subjects is maximum. An average subject grade is the sum of all marks in it, divided by the number of them. Of course, the division is performed in real numbers without rounding up or down. Polycarp aims to maximize the x1 + x2, where x1 is the average score in the first subject (Safety Studies), and x2 is the average score in the second one (Physical Education).
|
The first line contains an integer n (2 β€ n β€ 105), n is the number of marks in Polycarp's Journal. The second line contains two positive integers a, b (1 β€ a, b β€ n - 1, a + b = n). The third line contains a sequence of integers t1, t2, ..., tn (1 β€ ti β€ 5), they are Polycarp's marks.
|
Print the sequence of integers f1, f2, ..., fn, where fi (1 β€ fi β€ 2) is the number of a subject to which the i-th mark should be attributed. If there are several possible solutions, then print such that the sequence f1, f2, ..., fn is the smallest lexicographically.The sequence p1, p2, ..., pn is lexicographically less than q1, q2, ..., qn if there exists such j (1 β€ j β€ n) that pi = qi for all 1 β€ i < j, Π°nd pj < qj.
|
In the first sample the average score in the first subject is equal to 4, and in the second one β to 4.5. The total average score is 8.5.
|
Input: 53 24 4 5 4 4 | Output: 1 1 2 1 2
|
Medium
| 3 | 1,054 | 286 | 422 | 0 |
860 |
E
|
860E
|
E. Arkady and a Nobody-men
| 2,700 |
data structures; dfs and similar; trees
|
Arkady words in a large company. There are n employees working in a system of a strict hierarchy. Namely, each employee, with an exception of the CEO, has exactly one immediate manager. The CEO is a manager (through a chain of immediate managers) of all employees.Each employee has an integer rank. The CEO has rank equal to 1, each other employee has rank equal to the rank of his immediate manager plus 1.Arkady has a good post in the company, however, he feels that he is nobody in the company's structure, and there are a lot of people who can replace him. He introduced the value of replaceability. Consider an employee a and an employee b, the latter being manager of a (not necessarily immediate). Then the replaceability r(a, b) of a with respect to b is the number of subordinates (not necessarily immediate) of the manager b, whose rank is not greater than the rank of a. Apart from replaceability, Arkady introduced the value of negligibility. The negligibility za of employee a equals the sum of his replaceabilities with respect to all his managers, i.e. , where the sum is taken over all his managers b.Arkady is interested not only in negligibility of himself, but also in negligibility of all employees in the company. Find the negligibility of each employee for Arkady.
|
The first line contains single integer n (1 β€ n β€ 5Β·105) β the number of employees in the company.The second line contains n integers p1, p2, ..., pn (0 β€ pi β€ n), where pi = 0 if the i-th employee is the CEO, otherwise pi equals the id of the immediate manager of the employee with id i. The employees are numbered from 1 to n. It is guaranteed that there is exactly one 0 among these values, and also that the CEO is a manager (not necessarily immediate) for all the other employees.
|
Print n integers β the negligibilities of all employees in the order of their ids: z1, z2, ..., zn.
|
Consider the first example: The CEO has no managers, thus z1 = 0. r(2, 1) = 2 (employees 2 and 4 suit the conditions, employee 3 has too large rank). Thus z2 = r(2, 1) = 2. Similarly, z4 = r(4, 1) = 2. r(3, 2) = 1 (employee 3 is a subordinate of 2 and has suitable rank). r(3, 1) = 3 (employees 2, 3, 4 suit the conditions). Thus z3 = r(3, 2) + r(3, 1) = 4.
|
Input: 40 1 2 1 | Output: 0 2 4 2
|
Master
| 3 | 1,286 | 485 | 99 | 8 |
1,758 |
B
|
1758B
|
B. XOR = Average
| 900 |
constructive algorithms
|
You are given an integer \(n\). Find a sequence of \(n\) integers \(a_1, a_2, \dots, a_n\) such that \(1 \leq a_i \leq 10^9\) for all \(i\) and $$$\(a_1 \oplus a_2 \oplus \dots \oplus a_n = \frac{a_1 + a_2 + \dots + a_n}{n},\)\( where \)\oplus$$$ represents the bitwise XOR.It can be proven that there exists a sequence of integers that satisfies all the conditions above.
|
The first line of input contains \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases.The first and only line of each test case contains one integer \(n\) (\(1 \leq n \leq 10^5\)) β the length of the sequence you have to find.The sum of \(n\) over all test cases does not exceed \(10^5\).
|
For each test case, output \(n\) space-separated integers \(a_1, a_2, \dots, a_n\) satisfying the conditions in the statement. If there are several possible answers, you can output any of them.
|
In the first test case, \(69 = \frac{69}{1} = 69\).In the second test case, \(13 \oplus 2 \oplus 8 \oplus 1 = \frac{13 + 2 + 8 + 1}{4} = 6\).
|
Input: 3143 | Output: 69 13 2 8 1 7 7 7
|
Beginner
| 1 | 372 | 293 | 193 | 17 |
1,323 |
B
|
1323B
|
B. Count Subrectangles
| 1,500 |
binary search; greedy; implementation
|
You are given an array \(a\) of length \(n\) and array \(b\) of length \(m\) both consisting of only integers \(0\) and \(1\). Consider a matrix \(c\) of size \(n \times m\) formed by following rule: \(c_{i, j} = a_i \cdot b_j\) (i.e. \(a_i\) multiplied by \(b_j\)). It's easy to see that \(c\) consists of only zeroes and ones too.How many subrectangles of size (area) \(k\) consisting only of ones are there in \(c\)?A subrectangle is an intersection of a consecutive (subsequent) segment of rows and a consecutive (subsequent) segment of columns. I.e. consider four integers \(x_1, x_2, y_1, y_2\) (\(1 \le x_1 \le x_2 \le n\), \(1 \le y_1 \le y_2 \le m\)) a subrectangle \(c[x_1 \dots x_2][y_1 \dots y_2]\) is an intersection of the rows \(x_1, x_1+1, x_1+2, \dots, x_2\) and the columns \(y_1, y_1+1, y_1+2, \dots, y_2\).The size (area) of a subrectangle is the total number of cells in it.
|
The first line contains three integers \(n\), \(m\) and \(k\) (\(1 \leq n, m \leq 40\,000, 1 \leq k \leq n \cdot m\)), length of array \(a\), length of array \(b\) and required size of subrectangles.The second line contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \leq a_i \leq 1\)), elements of \(a\).The third line contains \(m\) integers \(b_1, b_2, \ldots, b_m\) (\(0 \leq b_i \leq 1\)), elements of \(b\).
|
Output single integer β the number of subrectangles of \(c\) with size (area) \(k\) consisting only of ones.
|
In first example matrix \(c\) is: There are \(4\) subrectangles of size \(2\) consisting of only ones in it: In second example matrix \(c\) is:
|
Input: 3 3 2 1 0 1 1 1 1 | Output: 4
|
Medium
| 3 | 895 | 416 | 108 | 13 |
1,283 |
A
|
1283A
|
A. Minutes Before the New Year
| 800 |
math
|
New Year is coming and you are excited to know how many minutes remain before the New Year. You know that currently the clock shows \(h\) hours and \(m\) minutes, where \(0 \le hh < 24\) and \(0 \le mm < 60\). We use 24-hour time format!Your task is to find the number of minutes before the New Year. You know that New Year comes when the clock shows \(0\) hours and \(0\) minutes.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 1439\)) β the number of test cases.The following \(t\) lines describe test cases. The \(i\)-th line contains the time as two integers \(h\) and \(m\) (\(0 \le h < 24\), \(0 \le m < 60\)). It is guaranteed that this time is not a midnight, i.e. the following two conditions can't be met at the same time: \(h=0\) and \(m=0\). It is guaranteed that both \(h\) and \(m\) are given without leading zeros.
|
For each test case, print the answer on it β the number of minutes before the New Year.
|
Input: 5 23 55 23 0 0 1 4 20 23 59 | Output: 5 60 1439 1180 1
|
Beginner
| 1 | 429 | 470 | 87 | 12 |
|
2,125 |
A
|
2125A
|
A. Difficult Contest
| 800 |
constructive algorithms; implementation; sortings; strings
|
It is known that a contest can be represented by a string \(s\), consisting of uppercase Latin letters that denote problems. It is also known that a contest is difficult if it contains ""FFT"" or ""NTT"" as a contiguous substring.Your task is to rearrange the problem in contest \(s\) in such a way that this contest is not difficult. If the initial contest is not difficult, you may leave it as it is.
|
Each test consists of several 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 only line of each test case contains \(s\) (\(1 \le |s| \le 2 \cdot 10^{5}\)).Additional constraints on the input data: the total length of strings across all test cases does not exceed \(2 \cdot 10^{5}\).
|
For each test case, output a string β a non-difficult contest that was obtained from \(s\) by rearranging the letters.If there are multiple correct answers, you may output any. It can be shown that at least one correct answer always exists.
|
Input: 5FFTABFBANTTAFFTNTTFFTFFTFFTNNTNNTAFFTBFFNTTFTTZ | Output: FTF ABFBANATT NTFTFT TFFFFFFNTNTNTNT AFTFBTTFFNFTTZ
|
Beginner
| 4 | 402 | 393 | 240 | 21 |
|
1,267 |
L
|
1267L
|
L. Lexicography
| 1,800 |
constructive algorithms; strings
|
Lucy likes letters. She studied the definition of the lexicographical order at school and plays with it.At first, she tried to construct the lexicographically smallest word out of given letters. It was so easy! Then she tried to build multiple words and minimize one of them. This was much harder!Formally, Lucy wants to make \(n\) words of length \(l\) each out of the given \(n \cdot l\) letters, so that the \(k\)-th of them in the lexicographic order is lexicographically as small as possible.
|
The first line contains three integers \(n\), \(l\), and \(k\) (\(1\le k \le n \le 1\,000\); \(1 \le l \le 1\,000\)) β the total number of words, the length of each word, and the index of the word Lucy wants to minimize.The next line contains a string of \(n \cdot l\) lowercase letters of the English alphabet.
|
Output \(n\) words of \(l\) letters each, one per line, using the letters from the input. Words must be sorted in the lexicographic order, and the \(k\)-th of them must be lexicographically as small as possible. If there are multiple answers with the smallest \(k\)-th word, output any of them.
|
Input: 3 2 2 abcdef | Output: af bc ed
|
Medium
| 2 | 497 | 311 | 294 | 12 |
|
865 |
F
|
865F
|
F. Egg Roulette
| 3,300 |
bitmasks; brute force; divide and conquer; math; meet-in-the-middle
|
The game of Egg Roulette is played between two players. Initially 2R raw eggs and 2C cooked eggs are placed randomly into a carton. The shells are left on so there is no way to distinguish a raw egg from a cooked egg. One at a time, a player will select an egg, and then smash the egg on his/her forehead. If the egg was cooked, not much happens, but if the egg was raw, it will make quite the mess. This continues until one player has broken R raw eggs, at which point that player is declared the loser and the other player wins.The order in which players take turns can be described as a string of 'A' and 'B' characters, where the i-th character tells which player should choose the i-th egg. Traditionally, players take turns going one after the other. That is, they follow the ordering ""ABABAB..."". This isn't very fair though, because the second player will win more often than the first. We'd like you to find a better ordering for the players to take their turns. Let's define the unfairness of an ordering as the absolute difference between the first player's win probability and the second player's win probability. We're interested in orderings that minimize the unfairness. We only consider an ordering valid if it contains the same number of 'A's as 'B's.You will also be given a string S of length 2(R + C) containing only 'A', 'B', and '?' characters. An ordering is said to match S if it only differs from S in positions where S contains a '?'. Of the valid orderings that minimize unfairness, how many match S?
|
The first line of input will contain integers R and C (1 β€ R, C β€ 20, R + C β€ 30).The second line of input will contain the string S of length 2(R + C) consisting only of characters 'A', 'B', '?'.
|
Print the number of valid orderings that minimize unfairness and match S.
|
In the first test case, the minimum unfairness is 0, and the orderings that achieve it are ""ABBA"" and ""BAAB"", neither of which match S. Note that an ordering such as ""ABBB"" would also have an unfairness of 0, but is invalid because it does not contain the same number of 'A's as 'B's.In the second example, the only matching ordering is ""BBAAABABABBA"".
|
Input: 1 1??BB | Output: 0
|
Master
| 5 | 1,529 | 196 | 73 | 8 |
92 |
A
|
92A
|
A. Chips
| 800 |
implementation; math
|
There are n walruses sitting in a circle. All of them are numbered in the clockwise order: the walrus number 2 sits to the left of the walrus number 1, the walrus number 3 sits to the left of the walrus number 2, ..., the walrus number 1 sits to the left of the walrus number n.The presenter has m chips. The presenter stands in the middle of the circle and starts giving the chips to the walruses starting from walrus number 1 and moving clockwise. The walrus number i gets i chips. If the presenter can't give the current walrus the required number of chips, then the presenter takes the remaining chips and the process ends. Determine by the given n and m how many chips the presenter will get in the end.
|
The first line contains two integers n and m (1 β€ n β€ 50, 1 β€ m β€ 104) β the number of walruses and the number of chips correspondingly.
|
Print the number of chips the presenter ended up with.
|
In the first sample the presenter gives one chip to the walrus number 1, two chips to the walrus number 2, three chips to the walrus number 3, four chips to the walrus number 4, then again one chip to the walrus number 1. After that the presenter runs out of chips. He can't give anything to the walrus number 2 and the process finishes.In the third sample the presenter gives one chip to the walrus number 1, two chips to the walrus number 2, three chips to the walrus number 3, then again one chip to the walrus number 1. The presenter has one chip left and he can't give two chips to the walrus number 2, that's why the presenter takes the last chip.
|
Input: 4 11 | Output: 0
|
Beginner
| 2 | 708 | 136 | 54 | 0 |
1,650 |
B
|
1650B
|
B. DIV + MOD
| 900 |
math
|
Not so long ago, Vlad came up with an interesting function: \(f_a(x)=\left\lfloor\frac{x}{a}\right\rfloor + x \bmod a\), where \(\left\lfloor\frac{x}{a}\right\rfloor\) is \(\frac{x}{a}\), rounded down, \(x \bmod a\) β the remainder of the integer division of \(x\) by \(a\).For example, with \(a=3\) and \(x=11\), the value \(f_3(11) = \left\lfloor\frac{11}{3}\right\rfloor + 11 \bmod 3 = 3 + 2 = 5\).The number \(a\) is fixed and known to Vlad. Help Vlad find the maximum value of \(f_a(x)\) if \(x\) can take any integer value from \(l\) to \(r\) inclusive (\(l \le x \le r\)).
|
The first line of input data contains an integer \(t\) (\(1 \le t \le 10^4\)) β the number of input test cases.This is followed by \(t\) lines, each of which contains three integers \(l_i\), \(r_i\) and \(a_i\) (\(1 \le l_i \le r_i \le 10^9, 1 \le a_i \le 10^9\)) β the left and right boundaries of the segment and the fixed value of \(a\).
|
For each test case, output one number on a separate line β the maximum value of the function on a given segment for a given \(a\).
|
In the first sample: \(f_3(1) = \left\lfloor\frac{1}{3}\right\rfloor + 1 \bmod 3 = 0 + 1 = 1\), \(f_3(2) = \left\lfloor\frac{2}{3}\right\rfloor + 2 \bmod 3 = 0 + 2 = 2\), \(f_3(3) = \left\lfloor\frac{3}{3}\right\rfloor + 3 \bmod 3 = 1 + 0 = 1\), \(f_3(4) = \left\lfloor\frac{4}{3}\right\rfloor + 4 \bmod 3 = 1 + 1 = 2\) As an answer, obviously, \(f_3(2)\) and \(f_3(4)\) are suitable.
|
Input: 51 4 35 8 46 10 61 1000000000 100000000010 12 8 | Output: 2 4 5 999999999 5
|
Beginner
| 1 | 579 | 340 | 130 | 16 |
1,110 |
B
|
1110B
|
B. Tape
| 1,400 |
greedy; sortings
|
You have a long stick, consisting of \(m\) segments enumerated from \(1\) to \(m\). Each segment is \(1\) centimeter long. Sadly, some segments are broken and need to be repaired.You have an infinitely long repair tape. You want to cut some pieces from the tape and use them to cover all of the broken segments. To be precise, a piece of tape of integer length \(t\) placed at some position \(s\) will cover segments \(s, s+1, \ldots, s+t-1\).You are allowed to cover non-broken segments; it is also possible that some pieces of tape will overlap.Time is money, so you want to cut at most \(k\) continuous pieces of tape to cover all the broken segments. What is the minimum total length of these pieces?
|
The first line contains three integers \(n\), \(m\) and \(k\) (\(1 \le n \le 10^5\), \(n \le m \le 10^9\), \(1 \le k \le n\)) β the number of broken segments, the length of the stick and the maximum number of pieces you can use.The second line contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le m\)) β the positions of the broken segments. These integers are given in increasing order, that is, \(b_1 < b_2 < \ldots < b_n\).
|
Print the minimum total length of the pieces.
|
In the first example, you can use a piece of length \(11\) to cover the broken segments \(20\) and \(30\), and another piece of length \(6\) to cover \(75\) and \(80\), for a total length of \(17\).In the second example, you can use a piece of length \(4\) to cover broken segments \(1\), \(2\) and \(4\), and two pieces of length \(1\) to cover broken segments \(60\) and \(87\).
|
Input: 4 100 220 30 75 80 | Output: 17
|
Easy
| 2 | 704 | 440 | 45 | 11 |
385 |
A
|
385A
|
A. Bear and Raspberry
| 1,000 |
brute force; greedy; implementation
|
The bear decided to store some raspberry for the winter. He cunningly found out the price for a barrel of honey in kilos of raspberry for each of the following n days. According to the bear's data, on the i-th (1 β€ i β€ n) day, the price for one barrel of honey is going to is xi kilos of raspberry.Unfortunately, the bear has neither a honey barrel, nor the raspberry. At the same time, the bear's got a friend who is ready to lend him a barrel of honey for exactly one day for c kilograms of raspberry. That's why the bear came up with a smart plan. He wants to choose some day d (1 β€ d < n), lent a barrel of honey and immediately (on day d) sell it according to a daily exchange rate. The next day (d + 1) the bear wants to buy a new barrel of honey according to a daily exchange rate (as he's got some raspberry left from selling the previous barrel) and immediately (on day d + 1) give his friend the borrowed barrel of honey as well as c kilograms of raspberry for renting the barrel.The bear wants to execute his plan at most once and then hibernate. What maximum number of kilograms of raspberry can he earn? Note that if at some point of the plan the bear runs out of the raspberry, then he won't execute such a plan.
|
The first line contains two space-separated integers, n and c (2 β€ n β€ 100, 0 β€ c β€ 100), β the number of days and the number of kilos of raspberry that the bear should give for borrowing the barrel.The second line contains n space-separated integers x1, x2, ..., xn (0 β€ xi β€ 100), the price of a honey barrel on day i.
|
Print a single integer β the answer to the problem.
|
In the first sample the bear will lend a honey barrel at day 3 and then sell it for 7. Then the bear will buy a barrel for 3 and return it to the friend. So, the profit is (7 - 3 - 1) = 3.In the second sample bear will lend a honey barrel at day 1 and then sell it for 100. Then the bear buy the barrel for 1 at the day 2. So, the profit is (100 - 1 - 2) = 97.
|
Input: 5 15 10 7 3 20 | Output: 3
|
Beginner
| 3 | 1,226 | 320 | 51 | 3 |
1,266 |
C
|
1266C
|
C. Diverse Matrix
| 1,400 |
constructive algorithms; greedy; math; number theory
|
Let \(a\) be a matrix of size \(r \times c\) containing positive integers, not necessarily distinct. Rows of the matrix are numbered from \(1\) to \(r\), columns are numbered from \(1\) to \(c\). We can construct an array \(b\) consisting of \(r + c\) integers as follows: for each \(i \in [1, r]\), let \(b_i\) be the greatest common divisor of integers in the \(i\)-th row, and for each \(j \in [1, c]\) let \(b_{r+j}\) be the greatest common divisor of integers in the \(j\)-th column. We call the matrix diverse if all \(r + c\) numbers \(b_k\) (\(k \in [1, r + c]\)) are pairwise distinct. The magnitude of a matrix equals to the maximum of \(b_k\).For example, suppose we have the following matrix: \(\begin{pmatrix} 2 & 9 & 7\\ 4 & 144 & 84 \end{pmatrix}\) We construct the array \(b\): \(b_1\) is the greatest common divisor of \(2\), \(9\), and \(7\), that is \(1\); \(b_2\) is the greatest common divisor of \(4\), \(144\), and \(84\), that is \(4\); \(b_3\) is the greatest common divisor of \(2\) and \(4\), that is \(2\); \(b_4\) is the greatest common divisor of \(9\) and \(144\), that is \(9\); \(b_5\) is the greatest common divisor of \(7\) and \(84\), that is \(7\). So \(b = [1, 4, 2, 9, 7]\). All values in this array are distinct, so the matrix is diverse. The magnitude is equal to \(9\).For a given \(r\) and \(c\), find a diverse matrix that minimises the magnitude. If there are multiple solutions, you may output any of them. If there are no solutions, output a single integer \(0\).
|
The only line in the input contains two space separated integers \(r\) and \(c\) (\(1 \leq r,c \leq 500\)) β the number of rows and the number of columns of the matrix to be found.
|
If there is no solution, output a single integer \(0\).Otherwise, output \(r\) rows. The \(i\)-th of them should contain \(c\) space-separated integers, the \(j\)-th of which is \(a_{i,j}\) β the positive integer in the \(i\)-th row and \(j\)-th column of a diverse matrix minimizing the magnitude.Furthermore, it must hold that \(1 \leq a_{i,j} \leq 10^9\). It can be shown that if a solution exists, there is also a solution with this additional constraint (still having minimum possible magnitude).
|
In the first example, the GCDs of rows are \(b_1 = 4\) and \(b_2 = 1\), and the GCDs of columns are \(b_3 = 2\) and \(b_4 = 3\). All GCDs are pairwise distinct and the maximum of them is \(4\). Since the GCDs have to be distinct and at least \(1\), it is clear that there are no diverse matrices of size \(2 \times 2\) with magnitude smaller than \(4\).In the second example, no matter what \(a_{1,1}\) is, \(b_1 = b_2\) will always hold, so there are no diverse matrices.
|
Input: 2 2 | Output: 4 12 2 9
|
Easy
| 4 | 1,510 | 180 | 501 | 12 |
1,831 |
A
|
1831A
|
A. Twin Permutations
| 800 |
constructive algorithms
|
You are given a permutation\(^\dagger\) \(a\) of length \(n\).Find any permutation \(b\) of length \(n\) such that \(a_1+b_1 \le a_2+b_2 \le a_3+b_3 \le \ldots \le a_n+b_n\).It can be proven that a permutation \(b\) that satisfies the condition above always exists.\(^\dagger\) A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array).
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 2000\)) β the number of test cases. The description of test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\)) β the length of permutations \(a\) and \(b\).The second line of each test case contains \(n\) distinct integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le n\)) β the elements of permutation \(a\). All elements of \(a\) are distinct.Note that there is no bound on the sum of \(n\) over all test cases.
|
For each test case, output any permutation \(b\) which satisfies the constraints mentioned in the statement. It can be proven that a permutation \(b\) that satisfies the condition above always exists.
|
In the first test case \(a=[1, 2, 4, 5, 3]\). Then the permutation \(b=[1, 2, 4, 3, 5]\) satisfies the condition because \(1 + 1 \le 2 + 2 \le 4 + 4 \le 5 + 3 \le 3 + 5\).
|
Input: 551 2 4 5 321 21133 2 141 4 3 2 | Output: 1 2 4 3 5 2 1 1 1 2 3 1 2 3 4
|
Beginner
| 1 | 602 | 569 | 200 | 18 |
2,048 |
I2
|
2048I2
|
I2. Kevin and Puzzle (Hard Version)
| 3,500 |
bitmasks; fft; math
|
This is the hard version of the problem. The difference between the versions is that in this version, you need to count the number of good arrays. You can hack only if you solved all versions of this problem. Kevin is visiting the Red Church, and he found a puzzle on the wall.For an array \( a \), let \( c(l,r) \) indicate how many distinct numbers are among \( a_l, a_{l+1}, \ldots, a_r \). In particular, if \( l > r \), define \( c(l,r) = 0 \).You are given a string \( s \) of length \( n \) consisting of letters \( \texttt{L} \) and \( \texttt{R} \) only. Let a non-negative array \( a \) be called good, if the following conditions hold for \( 1 \leq i \leq n \): if \(s_i=\verb!L!\), then \(c(1,i-1)=a_i\); if \(s_i=\verb!R!\), then \(c(i+1,n)=a_i\). You need to count the number of good arrays \(a\). Since the answer may be large, you only need to output the answer modulo \(998\,244\,353\).
|
Each test contains 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\) (\(2\leq n\leq 2\cdot 10^5\)) β the length of string \(s\).The second line of each test case contains a string \(s\) with a length \(n\), containing only English uppercase letters \(\verb!L!\) and \(\verb!R!\).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 number of good arrays modulo \(998\,244\,353\).
|
All arrays satisfying the conditions can be found in the easy version of this problem.
|
Input: 43LLR3RRL4RRLR5LLRLR | Output: 1 2 0 1
|
Master
| 3 | 903 | 545 | 78 | 20 |
2,084 |
G2
|
2084G2
|
G2. Wish Upon a Satellite (Hard Version)
| 3,500 |
data structures; dp
|
This is the hard version of the problem. The difference between the versions is that in this version, \(t \le 10^4\), \(n \le 5 \cdot 10^5\) and the sum of \(n\) does not exceed \(5 \cdot 10^5\). You can hack only if you solved all versions of this problem. For a non-empty sequence \(c\) of length \(k\), define \(f(c)\) as follows: Turtle and Piggy are playing a game on a sequence. They are given the sequence \(c_1, c_2, \ldots, c_k\), and Turtle goes first. Turtle and Piggy alternate in turns (so, Turtle does the first turn, Piggy does the second, Turtle does the third, etc.). The game goes as follows: Let the current length of the sequence be \(m\). If \(m = 1\), the game ends. If the game does not end and it's Turtle's turn, then Turtle must choose an integer \(i\) such that \(1 \le i \le m - 1\), set \(c_i\) to \(\min(c_i, c_{i + 1})\), and remove \(c_{i + 1}\). If the game does not end and it's Piggy's turn, then Piggy must choose an integer \(i\) such that \(1 \le i \le m - 1\), set \(c_i\) to \(\max(c_i, c_{i + 1})\), and remove \(c_{i + 1}\). Turtle wants to maximize the value of \(c_1\) in the end, while Piggy wants to minimize the value of \(c_1\) in the end. \(f(c)\) is the value of \(c_1\) in the end if both players play optimally. For a permutation \(p\) of length \(n\)\(^{\text{β}}\), Turtle defines the beauty of the permutation as \(\sum\limits_{i = 1}^n \sum\limits_{j = i}^n f([p_i, p_{i + 1}, \ldots, p_j])\) (i.e., the sum of \(f(c)\) where \(c\) is a non-empty subsegment\(^{\text{β }}\) of \(p\)).Piggy gives Turtle a permutation \(a\) of length \(n\) where some elements are missing and represented by \(0\).Turtle asks you to determine a permutation \(b\) of length \(n\) such that: \(b\) can be formed by filling in the missing elements of \(a\) (i.e., for all \(1 \le i \le n\), if \(a_i \ne 0\), then \(b_i = a_i\)). The beauty of the permutation \(b\) is maximized. For convenience, you only need to find the maximum beauty of such permutation \(b\).\(^{\text{β}}\)A permutation of length \(n\) is an array consisting of \(n\) distinct integers from \(1\) to \(n\) in arbitrary order. For example, \([2,3,1,5,4]\) is a permutation, but \([1,2,2]\) is not a permutation (\(2\) appears twice in the array), and \([1,3,4]\) is also not a permutation (\(n=3\) but there is \(4\) in the array). \(^{\text{β }}\)A sequence \(a\) is a subsegment of a sequence \(b\) if \(a\) can be obtained from \(b\) by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
|
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 5 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le n\)). It is guaranteed that the elements of \(a\) that are not \(0\) are distinct.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(5 \cdot 10^5\).
|
For each test case, output a single integer β the maximum beauty of the permutation \(b\).
|
In the first test case, the permutation \(b\) with the maximum beauty is \([1, 2]\). The beauty of \([1, 2]\) is \(4\) since \(f([1]) + f([2]) + f([1, 2]) = 1 + 2 + 1 = 4\). If \(c = [1, 2]\), then \(f(c) = 1\) since Turtle can only choose \(i = 1\) and he will set \(c_1\) to \(\min(c_1, c_2) = 1\).In the second test case, one of the permutations \(b\) with the maximum beauty is \([3, 2, 1]\). The beauty of \([3, 2, 1]\) is \(12\) since \(f([3]) + f([2]) + f([1]) + f([3, 2]) + f([2, 1]) + f([3, 2, 1]) = 3 + 2 + 1 + 2 + 1 + 3 = 12\).In the third test case, one of the permutations \(b\) with the maximum beauty is \([2, 1, 3]\).In the fourth test case, if \(c = [3, 2, 4, 5, 1]\), then \(f(c) = 3\). One of the possible game processes is as follows: Turtle can choose \(i = 3\). Then he will set \(c_3\) to \(\min(c_3, c_4) = 4\) and remove \(c_4\). The sequence \(c\) will become \([3, 2, 4, 1]\). Piggy can choose \(i = 1\). Then he will set \(c_1\) to \(\max(c_1, c_2) = 3\) and remove \(c_2\). The sequence \(c\) will become \([3, 4, 1]\). Turtle can choose \(i = 2\). Then he will set \(c_2\) to \(\min(c_2, c_3) = 1\) and remove \(c_3\). The sequence \(c\) will become \([3, 1]\). Piggy can choose \(i = 1\). Then he will set \(c_1\) to \(\max(c_1, c_2) = 3\) and remove \(c_2\). The sequence \(c\) will become \([3]\). The length of the sequence becomes \(1\), so the game will end. The value of \(c_1\) will be \(3\) in the end. In the fifth test case, one of the permutations \(b\) with the maximum beauty is \([1, 3, 2, 5, 6, 4, 7]\).
|
Input: 821 030 0 030 1 053 2 4 5 170 3 2 5 0 0 0101 2 6 5 8 9 0 0 0 050 4 1 0 050 1 5 2 3 | Output: 4 12 11 44 110 300 45 40
|
Master
| 2 | 2,579 | 533 | 90 | 20 |
922 |
E
|
922E
|
E. Birds
| 2,200 |
dp
|
Apart from plush toys, Imp is a huge fan of little yellow birds! To summon birds, Imp needs strong magic. There are n trees in a row on an alley in a park, there is a nest on each of the trees. In the i-th nest there are ci birds; to summon one bird from this nest Imp needs to stay under this tree and it costs him costi points of mana. However, for each bird summoned, Imp increases his mana capacity by B points. Imp summons birds one by one, he can summon any number from 0 to ci birds from the i-th nest. Initially Imp stands under the first tree and has W points of mana, and his mana capacity equals W as well. He can only go forward, and each time he moves from a tree to the next one, he restores X points of mana (but it can't exceed his current mana capacity). Moving only forward, what is the maximum number of birds Imp can summon?
|
The first line contains four integers n, W, B, X (1 β€ n β€ 103, 0 β€ W, B, X β€ 109) β the number of trees, the initial points of mana, the number of points the mana capacity increases after a bird is summoned, and the number of points restored when Imp moves from a tree to the next one.The second line contains n integers c1, c2, ..., cn (0 β€ ci β€ 104) β where ci is the number of birds living in the i-th nest. It is guaranteed that .The third line contains n integers cost1, cost2, ..., costn (0 β€ costi β€ 109), where costi is the mana cost to summon a bird from the i-th nest.
|
Print a single integer β the maximum number of birds Imp can summon.
|
In the first sample base amount of Imp's mana is equal to 12 (with maximum capacity also equal to 12). After he summons two birds from the first nest, he loses 8 mana points, although his maximum capacity will not increase (since B = 0). After this step his mana will be 4 of 12; during the move you will replenish 4 mana points, and hence own 8 mana out of 12 possible. Now it's optimal to take 4 birds from the second nest and spend 8 mana. The final answer will be β 6.In the second sample the base amount of mana is equal to 1000. The right choice will be to simply pick all birds from the last nest. Note that Imp's mana doesn't restore while moving because it's initially full.
|
Input: 2 12 0 43 44 2 | Output: 6
|
Hard
| 1 | 844 | 578 | 68 | 9 |
253 |
B
|
253B
|
B. Physics Practical
| 1,400 |
binary search; dp; sortings; two pointers
|
One day Vasya was on a physics practical, performing the task on measuring the capacitance. He followed the teacher's advice and did as much as n measurements, and recorded the results in the notebook. After that he was about to show the results to the teacher, but he remembered that at the last lesson, the teacher had made his friend Petya redo the experiment because the largest and the smallest results differed by more than two times. Vasya is lazy, and he does not want to redo the experiment. He wants to do the task and go home play computer games. So he decided to cheat: before Vasya shows the measurements to the teacher, he will erase some of them, so as to make the largest and the smallest results of the remaining measurements differ in no more than two times. In other words, if the remaining measurements have the smallest result x, and the largest result y, then the inequality y β€ 2Β·x must fulfill. Of course, to avoid the teacher's suspicion, Vasya wants to remove as few measurement results as possible from his notes.Help Vasya, find what minimum number of measurement results he will have to erase from his notes so that the largest and the smallest of the remaining results of the measurements differed in no more than two times.
|
The first line contains integer n (2 β€ n β€ 105) β the number of measurements Vasya made. The second line contains n integers c1, c2, ..., cn (1 β€ ci β€ 5000) β the results of the measurements. The numbers on the second line are separated by single spaces.
|
Print a single integer β the minimum number of results Vasya will have to remove.
|
In the first sample you can remove the fourth and the sixth measurement results (values 8 and 7). Then the maximum of the remaining values will be 5, and the minimum one will be 3. Or else, you can remove the third and fifth results (both equal 3). After that the largest remaining result will be 8, and the smallest one will be 4.
|
Input: 64 5 3 8 3 7 | Output: 2
|
Easy
| 4 | 1,254 | 254 | 81 | 2 |
962 |
F
|
962F
|
F. Simple Cycles Edges
| 2,400 |
dfs and similar; graphs; trees
|
You are given an undirected graph, consisting of \(n\) vertices and \(m\) edges. The graph does not necessarily connected. Guaranteed, that the graph does not contain multiple edges (more than one edges between a pair of vertices) or loops (edges from a vertex to itself).A cycle in a graph is called a simple, if it contains each own vertex exactly once. So simple cycle doesn't allow to visit a vertex more than once in a cycle.Determine the edges, which belong to exactly on one simple cycle.
|
The first line contain two integers \(n\) and \(m\) \((1 \le n \le 100\,000\), \(0 \le m \le \min(n \cdot (n - 1) / 2, 100\,000))\) β the number of vertices and the number of edges.Each of the following \(m\) lines contain two integers \(u\) and \(v\) (\(1 \le u, v \le n\), \(u \neq v\)) β the description of the edges.
|
In the first line print the number of edges, which belong to exactly one simple cycle.In the second line print the indices of edges, which belong to exactly one simple cycle, in increasing order. The edges are numbered from one in the same order as they are given in the input.
|
Input: 3 31 22 33 1 | Output: 31 2 3
|
Expert
| 3 | 495 | 320 | 277 | 9 |
|
1,621 |
C
|
1621C
|
C. Hidden Permutations
| 1,700 |
dfs and similar; interactive; math
|
This is an interactive problem.The jury has a permutation \(p\) of length \(n\) and wants you to guess it. For this, the jury created another permutation \(q\) of length \(n\). Initially, \(q\) is an identity permutation (\(q_i = i\) for all \(i\)).You can ask queries to get \(q_i\) for any \(i\) you want. After each query, the jury will change \(q\) in the following way: At first, the jury will create a new permutation \(q'\) of length \(n\) such that \(q'_i = q_{p_i}\) for all \(i\). Then the jury will replace permutation \(q\) with pemutation \(q'\). You can make no more than \(2n\) queries in order to quess \(p\).
|
The first line of input contains a single integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases.
|
In the first test case the hidden permutation \(p = [4, 2, 1, 3]\).Before the first query \(q = [1, 2, 3, 4]\) so answer for the query will be \(q_3 = 3\).Before the second query \(q = [4, 2, 1, 3]\) so answer for the query will be \(q_2 = 2\).Before the third query \(q = [3, 2, 4, 1]\) so answer for the query will be \(q_4 = 1\).In the second test case the hidden permutation \(p = [1, 3, 4, 2]\).Empty strings are given only for better readability. There will be no empty lines in the testing system.
|
Input: 2 4 3 2 1 4 2 4 4 | Output: ? 3 ? 2 ? 4 ! 4 2 1 3 ? 2 ? 3 ? 2 ! 1 3 4 2
|
Medium
| 3 | 625 | 108 | 0 | 16 |
|
1,987 |
G1
|
1987G1
|
G1. Spinning Round (Easy Version)
| 2,900 |
divide and conquer; dp; trees
|
This is the easy version of the problem. The only difference between the two versions are the allowed characters in \(s\). In the easy version, \(s\) only contains the character ?. You can make hacks only if both versions of the problem are solved.You are given a permutation \(p\) of length \(n\). You are also given a string \(s\) of length \(n\), consisting only of the character ?.For each \(i\) from \(1\) to \(n\): Define \(l_i\) as the largest index \(j < i\) such that \(p_j > p_i\). If there is no such index, \(l_i := i\). Define \(r_i\) as the smallest index \(j > i\) such that \(p_j > p_i\). If there is no such index, \(r_i := i\). Initially, you have an undirected graph with \(n\) vertices (numbered from \(1\) to \(n\)) and no edges. Then, for each \(i\) from \(1\) to \(n\), add one edge to the graph: If \(s_i =\) L, add the edge \((i, l_i)\) to the graph. If \(s_i =\) R, add the edge \((i, r_i)\) to the graph. If \(s_i =\) ?, either add the edge \((i, l_i)\) or the edge \((i, r_i)\) to the graph at your choice. Find the maximum possible diameter\(^{\text{β}}\) over all connected graphs that you can form. Output \(-1\) if it is not possible to form any connected graphs.\(^{\text{β}}\) Let \(d(s, t)\) denote the smallest number of edges on any path between \(s\) and \(t\).The diameter of the graph is defined as the maximum value of \(d(s, t)\) over all pairs of vertices \(s\) and \(t\).
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 2 \cdot 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\) (\(2 \le n \le 4 \cdot 10^5\)) β the length of the permutation \(p\). The second line of each test case contains \(n\) integers \(p_1,p_2,\ldots, p_n\) (\(1 \le p_i \le n\)) β the elements of \(p\), which are guaranteed to form a permutation.The third line of each test case contains a string \(s\) of length \(n\). It is guaranteed that it consists only of the character ?.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(4 \cdot 10^5\).
|
For each test case, output the maximum possible diameter over all connected graphs that you form, or \(-1\) if it is not possible to form any connected graphs.
|
In the first test case, here are some possible connected graphs that you can form (the labels are indices): In the second test case, the only connected graph has a diameter of \(1\).
|
Input: 852 1 4 3 5?????21 2??33 1 2???75 3 1 6 4 2 7???????55 2 1 3 4?????66 2 3 4 5 1??????81 7 5 6 2 8 4 3????????126 10 7 1 8 5 12 2 11 3 4 9???????????? | Output: 4 1 2 6 4 5 5 8
|
Master
| 3 | 1,415 | 728 | 159 | 19 |
914 |
E
|
914E
|
E. Palindromes in a Tree
| 2,400 |
bitmasks; data structures; divide and conquer; trees
|
You are given a tree (a connected acyclic undirected graph) of n vertices. Vertices are numbered from 1 to n and each vertex is assigned a character from a to t.A path in the tree is said to be palindromic if at least one permutation of the labels in the path is a palindrome.For each vertex, output the number of palindromic paths passing through it. Note: The path from vertex u to vertex v is considered to be the same as the path from vertex v to vertex u, and this path will be counted only once for each of the vertices it passes through.
|
The first line contains an integer n (2 β€ n β€ 2Β·105) β the number of vertices in the tree.The next n - 1 lines each contain two integers u and v (1 β€ u, v β€ n, u β v) denoting an edge connecting vertex u and vertex v. It is guaranteed that the given graph is a tree.The next line contains a string consisting of n lowercase characters from a to t where the i-th (1 β€ i β€ n) character is the label of vertex i in the tree.
|
Print n integers in a single line, the i-th of which is the number of palindromic paths passing through vertex i in the tree.
|
In the first sample case, the following paths are palindromic:2 - 3 - 42 - 3 - 54 - 3 - 5Additionally, all paths containing only one vertex are palindromic. Listed below are a few paths in the first sample that are not palindromic:1 - 2 - 31 - 2 - 3 - 41 - 2 - 3 - 5
|
Input: 51 22 33 43 5abcbb | Output: 1 3 4 3 3
|
Expert
| 4 | 544 | 421 | 125 | 9 |
1,001 |
I
|
1001I
|
I. Deutsch-Jozsa algorithm
| 1,700 |
*special
|
You are given a quantum oracle - an operation on N + 1 qubits which implements a function . You are guaranteed that the function f implemented by the oracle is either constant (returns 0 on all inputs or 1 on all inputs) or balanced (returns 0 on exactly one half of the input domain and 1 on the other half).There are only two possible constant functions: f(x) = 0 and f(x) = 1. The functions implemented by oracles in the two previous problems (f(x) = xk and ) are examples of balanced functions.Your task is to figure out whether the function given by the oracle is constant. Your code is allowed to call the given oracle only once.
|
You have to implement an operation which takes the following inputs: an integer N - the number of qubits in the oracle input, an oracle Uf, implemented as an operation with signature ((Qubit[], Qubit) => ()), i.e., an operation which takes as input an array of qubits and an output qubit and has no output.The return of your operation is a Boolean value: true if the oracle implements a constant function and false otherwise.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (N : Int, Uf : ((Qubit[], Qubit) => ())) : Bool { body { // your code here } }}
|
Medium
| 1 | 635 | 651 | 0 | 10 |
|||
1,704 |
B
|
1704B
|
B. Luke is a Foodie
| 1,000 |
brute force; greedy; implementation
|
Luke likes to eat. There are \(n\) piles of food aligned in a straight line in front of him. The \(i\)-th pile contains \(a_i\) units of food. Luke will walk from the \(1\)-st pile towards the \(n\)-th pile, and he wants to eat every pile of food without walking back. When Luke reaches the \(i\)-th pile, he can eat that pile if and only if \(|v - a_i| \leq x\), where \(x\) is a fixed integer, and \(v\) is Luke's food affinity.Before Luke starts to walk, he can set \(v\) to any integer. Also, for each \(i\) (\(1 \leq i \leq n\)), Luke can change his food affinity to any integer before he eats the \(i\)-th pile.Find the minimum number of changes needed to eat every pile of food.Note that the initial choice for \(v\) is not considered as a change.
|
The input consists of multiple test cases. The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of test cases follows.For each test case, the first line contains two integers, \(n, x\) (\(1 \leq n \leq 2 \cdot 10^5\), \(1 \leq x \leq 10^9\)) β the number of piles, and the maximum difference between the size of a pile and Luke's food affinity, such that Luke can eat the pile.The second line contains \(n\) integers \(a_1, a_2, \ldots , a_n\) (\(1 \leq a_i \leq 10^9\)).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output an integer on a separate line, which is the minimum number of changes needed.
|
In the first test case, Luke can set \(v\) to \(5\) before he starts to walk. And he can walk straight to eat every piles of food without changing \(v\).In the second test case, Luke can set \(v\) to \(3\) before he starts to walk. And he could change \(v\) to \(10\) before he eats the second pile. After that, he can walk straight to eat remaining food without changing \(v\).In the fourth test case, Luke can set \(v\) to \(3\) before he starts to walk. And he could change \(v\) to \(8\) before he eats the sixth pile. After that, he can walk straight to eat remaining food without changing \(v\).In the fifth test case, Luke can set \(v\) to \(4\) before he starts to walk. And he could change \(v\) to \(6\) before he eats the fourth pile. Then he could change \(v\) to \(12\) before he eats the seventh pile. After that, he can walk straight to eat remaining food without changing \(v\).
|
Input: 75 33 8 5 6 75 33 10 9 8 712 825 3 3 17 8 6 1 16 15 25 17 2310 21 2 3 4 5 6 7 8 9 108 22 4 6 8 6 4 12 148 22 7 8 9 6 13 21 2815 511 4 13 23 7 10 5 21 20 11 17 5 29 16 11 | Output: 0 1 2 1 2 4 6
|
Beginner
| 3 | 754 | 626 | 104 | 17 |
1,140 |
A
|
1140A
|
A. Detective Book
| 1,000 |
implementation
|
Ivan recently bought a detective book. The book is so interesting that each page of this book introduces some sort of a mystery, which will be explained later. The \(i\)-th page contains some mystery that will be explained on page \(a_i\) (\(a_i \ge i\)).Ivan wants to read the whole book. Each day, he reads the first page he didn't read earlier, and continues to read the following pages one by one, until all the mysteries he read about are explained and clear to him (Ivan stops if there does not exist any page \(i\) such that Ivan already has read it, but hasn't read page \(a_i\)). After that, he closes the book and continues to read it on the following day from the next page.How many days will it take to read the whole book?
|
The first line contains single integer \(n\) (\(1 \le n \le 10^4\)) β the number of pages in the book.The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\) (\(i \le a_i \le n\)), where \(a_i\) is the number of page which contains the explanation of the mystery on page \(i\).
|
Print one integer β the number of days it will take to read the whole book.
|
Explanation of the example test:During the first day Ivan will read only the first page. During the second day Ivan will read pages number \(2\) and \(3\). During the third day β pages \(4\)-\(8\). During the fourth (and the last) day Ivan will read remaining page number \(9\).
|
Input: 9 1 3 3 6 7 6 8 8 9 | Output: 4
|
Beginner
| 1 | 735 | 286 | 75 | 11 |
444 |
B
|
444B
|
B. DZY Loves FFT
| 2,300 |
probabilities
|
DZY loves Fast Fourier Transformation, and he enjoys using it.Fast Fourier Transformation is an algorithm used to calculate convolution. Specifically, if a, b and c are sequences with length n, which are indexed from 0 to n - 1, andWe can calculate c fast using Fast Fourier Transformation.DZY made a little change on this formula. NowTo make things easier, a is a permutation of integers from 1 to n, and b is a sequence only containing 0 and 1. Given a and b, DZY needs your help to calculate c.Because he is naughty, DZY provides a special way to get a and b. What you need is only three integers n, d, x. After getting them, use the code below to generate a and b.//x is 64-bit variable;function getNextX() { x = (x * 37 + 10007) % 1000000007; return x;}function initAB() { for(i = 0; i < n; i = i + 1){ a[i] = i + 1; } for(i = 0; i < n; i = i + 1){ swap(a[i], a[getNextX() % (i + 1)]); } for(i = 0; i < n; i = i + 1){ if (i < d) b[i] = 1; else b[i] = 0; } for(i = 0; i < n; i = i + 1){ swap(b[i], b[getNextX() % (i + 1)]); }}Operation x % y denotes remainder after division x by y. Function swap(x, y) swaps two values x and y.
|
The only line of input contains three space-separated integers n, d, x (1 β€ d β€ n β€ 100000; 0 β€ x β€ 1000000006). Because DZY is naughty, x can't be equal to 27777500.
|
Output n lines, the i-th line should contain an integer ci - 1.
|
In the first sample, a is [1 3 2], b is [1 0 0], so c0 = max(1Β·1) = 1, c1 = max(1Β·0, 3Β·1) = 3, c2 = max(1Β·0, 3Β·0, 2Β·1) = 2.In the second sample, a is [2 1 4 5 3], b is [1 1 1 0 1].In the third sample, a is [5 2 1 4 3], b is [1 1 1 1 0].
|
Input: 3 1 1 | Output: 132
|
Expert
| 1 | 1,132 | 166 | 63 | 4 |
1,907 |
A
|
1907A
|
A. Rook
| 800 |
implementation
|
As you probably know, chess is a game that is played on a board with 64 squares arranged in an \(8\times 8\) grid. Columns of this board are labeled with letters from a to h, and rows are labeled with digits from 1 to 8. Each square is described by the row and column it belongs to. The rook is a piece in the game of chess. During its turn, it may move any non-zero number of squares horizontally or vertically. Your task is to find all possible moves for a rook on an empty chessboard.
|
The first line of input contains single integer \(t\) (\(1 \le t \le 64\)) β the number of test cases. The descriptions of test cases follow.Each test case contains one string of two characters, description of the square where rook is positioned. The first character is a letter from a to h, the label of column, and the second character is a digit from 1 to 8, the label of row. The same position may occur in more than one test case.
|
For each test case, output descriptions of all squares where the rook can move, in the same format as in the input. You can output squares in any order per test case.
|
Input: 1d5 | Output: d1 d2 b5 g5 h5 d3 e5 f5 d8 a5 d6 d7 c5 d4
|
Beginner
| 1 | 487 | 435 | 166 | 19 |
|
1,515 |
A
|
1515A
|
A. Phoenix and Gold
| 800 |
constructive algorithms; greedy; math
|
Phoenix has collected \(n\) pieces of gold, and he wants to weigh them together so he can feel rich. The \(i\)-th piece of gold has weight \(w_i\). All weights are distinct. He will put his \(n\) pieces of gold on a weight scale, one piece at a time. The scale has an unusual defect: if the total weight on it is exactly \(x\), it will explode. Can he put all \(n\) gold pieces onto the scale in some order, without the scale exploding during the process? If so, help him find some possible order. Formally, rearrange the array \(w\) so that for each \(i\) \((1 \le i \le n)\), \(\sum\limits_{j = 1}^{i}w_j \ne x\).
|
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.The first line of each test case contains two integers \(n\) and \(x\) (\(1 \le n \le 100\); \(1 \le x \le 10^4\)) β the number of gold pieces that Phoenix has and the weight to avoid, respectively. The second line of each test case contains \(n\) space-separated integers \((1 \le w_i \le 100)\) β the weights of the gold pieces. It is guaranteed that the weights are pairwise distinct.
|
For each test case, if Phoenix cannot place all \(n\) pieces without the scale exploding, print NO. Otherwise, print YES followed by the rearranged array \(w\). If there are multiple solutions, print any.
|
In the first test case, Phoenix puts the gold piece with weight \(3\) on the scale first, then the piece with weight \(2\), and finally the piece with weight \(1\). The total weight on the scale is \(3\), then \(5\), then \(6\). The scale does not explode because the total weight on the scale is never \(2\).In the second test case, the total weight on the scale is \(8\), \(9\), \(11\), \(14\), then \(18\). It is never \(3\).In the third test case, Phoenix must put the gold piece with weight \(5\) on the scale, and the scale will always explode.
|
Input: 3 3 2 3 2 1 5 3 1 2 3 4 8 1 5 5 | Output: YES 3 2 1 YES 8 1 2 3 4 NO
|
Beginner
| 3 | 615 | 521 | 204 | 15 |
2,081 |
G2
|
2081G2
|
G2. Hard Formula (Hard Version)
| 3,400 |
math
|
This is the hard version of the problem. The difference between the versions is that in this version, the limit on \(n\) and the time limit are higher. You can hack only if you solved all versions of this problem. You are given an integer \(n\), and you need to compute \((\sum_{k=1}^n k\bmod\varphi(k))\bmod 2^{32}\), where \(\varphi(k)\) equals the number of positive integers no greater than \(k\) that are coprime with \(k\).
|
The only line contains a single integer \(n\) (\(1 \le n \le 10^{12}\)).
|
Print a single integer, representing \((\sum_{k=1}^n k\bmod\varphi(k))\bmod 2^{32}\).
|
Input: 5 | Output: 2
|
Master
| 1 | 429 | 72 | 85 | 20 |
|
71 |
E
|
71E
|
E. Nuclear Fusion
| 2,200 |
bitmasks; dp
|
There is the following puzzle popular among nuclear physicists.A reactor contains a set of n atoms of some chemical elements. We shall understand the phrase ""atomic number"" as the number of this atom's element in the periodic table of the chemical elements.You are allowed to take any two different atoms and fuse a new one from them. That results in a new atom, whose number is equal to the sum of the numbers of original atoms. The fusion operation can be performed several times.The aim is getting a new pregiven set of k atoms.The puzzle's difficulty is that it is only allowed to fuse two atoms into one, it is not allowed to split an atom into several atoms. You are suggested to try to solve the puzzle.
|
The first line contains two integers n and k (1 β€ k β€ n β€ 17). The second line contains space-separated symbols of elements of n atoms, which are available from the start. The third line contains space-separated symbols of elements of k atoms which need to be the result of the fusion. The symbols of the elements coincide with the symbols from the periodic table of the chemical elements. The atomic numbers do not exceed 100 (elements possessing larger numbers are highly unstable). Some atoms can have identical numbers (that is, there can be several atoms of the same element). The sum of numbers of initial atoms is equal to the sum of numbers of the atoms that need to be synthesized.
|
If it is impossible to synthesize the required atoms, print ""NO"" without the quotes. Otherwise, print on the first line Β«YESΒ», and on the next k lines print the way of synthesizing each of k atoms as equations. Each equation has the following form: ""x1+x2+...+xt->yi"", where xj is the symbol of the element of some atom from the original set, and yi is the symbol of the element of some atom from the resulting set. Each atom from the input data should occur in the output data exactly one time. The order of summands in the equations, as well as the output order does not matter. If there are several solutions, print any of them. For a better understanding of the output format, see the samples.
|
The reactions from the first example possess the following form (the atomic number is written below and to the left of the element):To find a periodic table of the chemical elements, you may use your favorite search engine.The pretest set contains each of the first 100 elements of the periodic table at least once. You can use that information to check for misprints.
|
Input: 10 3Mn Co Li Mg C P F Zn Sc KSn Pt Y | Output: YESMn+C+K->SnCo+Zn+Sc->PtLi+Mg+P+F->Y
|
Hard
| 2 | 712 | 690 | 701 | 0 |
1,409 |
B
|
1409B
|
B. Minimum Product
| 1,100 |
brute force; greedy; math
|
You are given four integers \(a\), \(b\), \(x\) and \(y\). Initially, \(a \ge x\) and \(b \ge y\). You can do the following operation no more than \(n\) times: Choose either \(a\) or \(b\) and decrease it by one. However, as a result of this operation, value of \(a\) cannot become less than \(x\), and value of \(b\) cannot become less than \(y\). Your task is to find the minimum possible product of \(a\) and \(b\) (\(a \cdot b\)) you can achieve by applying the given operation no more than \(n\) times.You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The only line of the test case contains five integers \(a\), \(b\), \(x\), \(y\) and \(n\) (\(1 \le a, b, x, y, n \le 10^9\)). Additional constraint on the input: \(a \ge x\) and \(b \ge y\) always holds.
|
For each test case, print one integer: the minimum possible product of \(a\) and \(b\) (\(a \cdot b\)) you can achieve by applying the given operation no more than \(n\) times.
|
In the first test case of the example, you need to decrease \(b\) three times and obtain \(10 \cdot 7 = 70\).In the second test case of the example, you need to decrease \(a\) one time, \(b\) one time and obtain \(11 \cdot 7 = 77\).In the sixth test case of the example, you need to decrease \(a\) five times and obtain \(5 \cdot 11 = 55\).In the seventh test case of the example, you need to decrease \(b\) ten times and obtain \(10 \cdot 1 = 10\).
|
Input: 7 10 10 8 5 3 12 8 8 7 2 12343 43 4543 39 123212 1000000000 1000000000 1 1 1 1000000000 1000000000 1 1 1000000000 10 11 2 1 5 10 11 9 1 10 | Output: 70 77 177177 999999999000000000 999999999 55 10
|
Easy
| 3 | 555 | 347 | 176 | 14 |
1,527 |
B1
|
1527B1
|
B1. Palindrome Game (easy version)
| 1,200 |
constructive algorithms; games
|
The only difference between the easy and hard versions is that the given string \(s\) in the easy version is initially a palindrome, this condition is not always true for the hard version.A palindrome is a string that reads the same left to right and right to left. For example, ""101101"" is a palindrome, while ""0101"" is not.Alice and Bob are playing a game on a string \(s\) (which is initially a palindrome in this version) of length \(n\) consisting of the characters '0' and '1'. Both players take alternate turns with Alice going first.In each turn, the player can perform one of the following operations: Choose any \(i\) (\(1 \le i \le n\)), where \(s[i] =\) '0' and change \(s[i]\) to '1'. Pay 1 dollar. Reverse the whole string, pay 0 dollars. This operation is only allowed if the string is currently not a palindrome, and the last operation was not reverse. That is, if Alice reverses the string, then Bob can't reverse in the next move, and vice versa. Reversing a string means reordering its letters from the last to the first. For example, ""01001"" becomes ""10010"" after reversing.The game ends when every character of string becomes '1'. The player who spends minimum dollars till this point wins the game and it is a draw if both spend equal dollars. If both players play optimally, output whether Alice wins, Bob wins, or if it is a draw.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^3\)). Then \(t\) test cases follow.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^3\)).The second line of each test case contains the string \(s\) of length \(n\), consisting of the characters '0' and '1'. It is guaranteed that the string \(s\) is a palindrome and contains at least one '0'. Note that there is no limit on the sum of \(n\) over test cases.
|
For each test case print a single word in a new line: ""ALICE"", if Alice will win the game, ""BOB"", if Bob will win the game, ""DRAW"", if the game ends in a draw.
|
In the first test case of the example, in the \(1\)-st move Alice has to perform the \(1\)-st operation, since the string is currently a palindrome. in the \(2\)-nd move Bob reverses the string. in the \(3\)-rd move Alice again has to perform the \(1\)-st operation. All characters of the string are '1', game over. Alice spends \(2\) dollars while Bob spends \(0\) dollars. Hence, Bob always wins.
|
Input: 2 4 1001 1 0 | Output: BOB BOB
|
Easy
| 2 | 1,362 | 457 | 165 | 15 |
690 |
B3
|
690B3
|
B3. Recover Polygon (hard)
| 2,600 |
data structures
|
Zombies have found out about the Zombie Contamination level checker and managed to damage it! Now detecting the shape of their main compound will be a real challenge for Heidi. As before, a lair can be represented as a strictly convex polygon on a lattice. Each vertex of the polygon occupies a point on the lattice. However, the damaged Zombie Contamination level checker can only tell, for each cell, whether the level of Zombie Contamination for that cell is in the set {1, 2, 3}. In other words, Heidi knows all the cells of the lattice for which the Contamination level is not 0 and not 4.Given this information, Heidi still wants to know the exact shape of the lair to rain destruction on the zombies. Help her!
|
The input contains multiple test cases.The first line of each test case contains two space-separated integers N and M, where N is the size of the lattice grid (5 β€ N β€ 100000) and M is the number of lattice points for which the Zombie Contamination level is 1, 2, or 3 (8 β€ M β€ 200000).The second line of each test case contains M pairs of integers x1, y1, ..., xM, yM β coordinates of the cells with Zombie Contamination level not equal to 0 nor 4. It is guaranteed that 1 β€ xi, yi β€ N. All pairs xi, yi are different.Cells are enumerated based on the coordinates of their upper right corner. This means that the bottommost leftmost cell that touches the origin has coordinates (1, 1), and the uppermost leftmost cell is identified as (1, N).The last line of the file contains two zeroes. This line should not be treated as a test case. The sum of the M values for all tests in one file will not exceed 200000.
|
For each test case, the following output is expected:The first line of the output should contain one integer V, the number of vertices of the polygon that is the secret lair. The next V lines each should contain two integers, denoting the vertices of the polygon in the clockwise order, starting from the lexicographically smallest vertex.
|
It is guaranteed that the solution always exists and is unique. It is guaranteed that in the correct solution the coordinates of the polygon vertices are between 1 and N - 1. A vertex (x1, y1) is lexicographically smaller than vertex (x2, y2) if x1 < x2 or .
|
Input: 8 192 3 2 4 2 5 3 3 3 5 4 3 4 5 4 6 5 2 5 3 5 6 6 2 6 3 6 4 6 5 6 6 6 7 7 6 7 75 82 2 2 3 2 4 3 2 3 4 4 2 4 3 4 40 0 | Output: 42 32 46 65 242 22 33 33 2
|
Expert
| 1 | 717 | 911 | 339 | 6 |
1,443 |
A
|
1443A
|
A. Kids Seating
| 800 |
constructive algorithms; math
|
Today the kindergarten has a new group of \(n\) kids who need to be seated at the dinner table. The chairs at the table are numbered from \(1\) to \(4n\). Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers \(a\) and \(b\) (\(a \neq b\)) will indulge if: \(gcd(a, b) = 1\) or, \(a\) divides \(b\) or \(b\) divides \(a\). \(gcd(a, b)\) β the maximum number \(x\) such that \(a\) is divisible by \(x\) and \(b\) is divisible by \(x\).For example, if \(n=3\) and the kids sit on chairs with numbers \(2\), \(3\), \(4\), then they will indulge since \(4\) is divided by \(2\) and \(gcd(2, 3) = 1\). If kids sit on chairs with numbers \(4\), \(6\), \(10\), then they will not indulge.The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no \(2\) of the kid that can indulge. More formally, she wants no pair of chairs \(a\) and \(b\) that the kids occupy to fulfill the condition above.Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem.
|
The first line contains one integer \(t\) (\(1 \leq t \leq 100\)) β the number of test cases. Then \(t\) test cases follow.Each test case consists of one line containing an integer \(n\) (\(1 \leq n \leq 100\)) β the number of kids.
|
Output \(t\) lines, which contain \(n\) distinct integers from \(1\) to \(4n\) β the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print \(n\) numbers in any order.
|
Input: 3 2 3 4 | Output: 6 4 4 6 10 14 10 12 8
|
Beginner
| 2 | 1,072 | 232 | 254 | 14 |
|
272 |
B
|
272B
|
B. Dima and Sequence
| 1,400 |
implementation; math
|
Dima got into number sequences. Now he's got sequence a1, a2, ..., an, consisting of n positive integers. Also, Dima has got a function f(x), which can be defined with the following recurrence: f(0) = 0; f(2Β·x) = f(x); f(2Β·x + 1) = f(x) + 1. Dima wonders, how many pairs of indexes (i, j) (1 β€ i < j β€ n) are there, such that f(ai) = f(aj). Help him, count the number of such pairs.
|
The first line contains integer n (1 β€ n β€ 105). The second line contains n positive integers a1, a2, ..., an (1 β€ ai β€ 109).The numbers in the lines are separated by single spaces.
|
In a single line print the answer to the problem.Please, don't use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.
|
In the first sample any pair (i, j) will do, so the answer is 3.In the second sample only pair (1, 2) will do.
|
Input: 31 2 4 | Output: 3
|
Easy
| 2 | 382 | 181 | 195 | 2 |
1,891 |
A
|
1891A
|
A. Sorting with Twos
| 800 |
constructive algorithms; sortings
|
You are given an array of integers \(a_1, a_2, \ldots, a_n\). In one operation, you do the following: Choose a non-negative integer \(m\), such that \(2^m \leq n\). Subtract \(1\) from \(a_i\) for all integers \(i\), such that \(1 \leq i \leq 2^m\). Can you sort the array in non-decreasing order by performing some number (possibly zero) of operations?An array is considered non-decreasing if \(a_i \leq a_{i + 1}\) for all integers \(i\) such that \(1 \leq i \leq n - 1\).
|
The first line contains a single integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 20\)) β the length of array \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) β the integers in array \(a\) (\(0 \leq a_i \leq 1000\)).
|
For each test case, output ""YES"" if the array can be sorted, and ""NO"" otherwise.
|
In the first test case, the array is already sorted in non-decreasing order, so we don't have to perform any operations.In the second test case, we can choose \(m = 1\) twice to get the array \([4, 3, 3, 4, 4]\). Then, we can choose \(m = 0\) once and get the sorted in non-decreasing order array \([3, 3, 3, 4, 4]\).In the third test case, we can choose \(m = 0\) once and get the array \([5, 5, 5, 7, 5, 6, 6, 8, 7]\). Then, we can choose \(m = 2\) twice and get the array \([3, 3, 3, 5, 5, 6, 6, 8, 7]\). After that, we can choose \(m = 3\) once and get the sorted in non-decreasing order array \([2, 2, 2, 4, 4, 5, 5, 7, 7]\).For the fourth and fifth test case, it can be shown that the array could not be sorted using these operations.
|
Input: 851 2 3 4 556 5 3 4 496 5 5 7 5 6 6 8 744 3 2 162 2 4 5 3 281 3 17 19 27 57 179 1353 17 57 179 92101 2 3 4 0 6 7 8 9 10 | Output: YES YES YES NO NO NO YES YES
|
Beginner
| 2 | 474 | 399 | 84 | 18 |
1,654 |
F
|
1654F
|
F. Minimal String Xoration
| 2,800 |
bitmasks; data structures; divide and conquer; greedy; hashing; sortings; strings
|
You are given an integer \(n\) and a string \(s\) consisting of \(2^n\) lowercase letters of the English alphabet. The characters of the string \(s\) are \(s_0s_1s_2\cdots s_{2^n-1}\).A string \(t\) of length \(2^n\) (whose characters are denoted by \(t_0t_1t_2\cdots t_{2^n-1}\)) is a xoration of \(s\) if there exists an integer \(j\) (\(0\le j \leq 2^n-1\)) such that, for each \(0 \leq i \leq 2^n-1\), \(t_i = s_{i \oplus j}\) (where \(\oplus\) denotes the operation bitwise XOR).Find the lexicographically minimal xoration of \(s\).A string \(a\) is lexicographically smaller than a string \(b\) if and only if one of the following holds: \(a\) is a prefix of \(b\), but \(a \ne b\); in the first position where \(a\) and \(b\) differ, the string \(a\) has a letter that appears earlier in the alphabet than the corresponding letter in \(b\).
|
The first line contains a single integer \(n\) (\(1 \le n \le 18\)).The second line contains a string \(s\) consisting of \(2^n\) lowercase letters of the English alphabet.
|
Print a single line containing the lexicographically minimal xoration of \(s\).
|
In the first test, the lexicographically minimal xoration \(t\) of \(s =\)""acba"" is ""abca"". It's a xoration because, for \(j = 3\), \(t_0 = s_{0 \oplus j} = s_3 =\) ""a""; \(t_1 = s_{1 \oplus j} = s_2 =\) ""b""; \(t_2 = s_{2 \oplus j} = s_1 =\) ""c""; \(t_3 = s_{3 \oplus j} = s_0 =\) ""a"". There isn't any xoration of \(s\) lexicographically smaller than ""abca"".In the second test, the minimal string xoration corresponds to choosing \(j = 4\) in the definition of xoration.In the third test, the minimal string xoration corresponds to choosing \(j = 11\) in the definition of xoration.In the fourth test, the minimal string xoration corresponds to choosing \(j = 10\) in the definition of xoration.In the fifth test, the minimal string xoration corresponds to choosing either \(j = 0\) or \(j = 1\) in the definition of xoration.
|
Input: 2 acba | Output: abca
|
Master
| 7 | 847 | 172 | 79 | 16 |
117 |
A
|
117A
|
A. Elevator
| 1,300 |
implementation; math
|
And now the numerous qualifying tournaments for one of the most prestigious Russian contests Russian Codec Cup are over. All n participants who have made it to the finals found themselves in a huge m-floored 108-star hotel. Of course the first thought to come in a place like this is ""How about checking out the elevator?"".The hotel's elevator moves between floors according to one never changing scheme. Initially (at the moment of time 0) the elevator is located on the 1-st floor, then it moves to the 2-nd floor, then β to the 3-rd floor and so on until it reaches the m-th floor. After that the elevator moves to floor m - 1, then to floor m - 2, and so on until it reaches the first floor. This process is repeated infinitely. We know that the elevator has infinite capacity; we also know that on every floor people get on the elevator immediately. Moving between the floors takes a unit of time.For each of the n participant you are given si, which represents the floor where the i-th participant starts, fi, which represents the floor the i-th participant wants to reach, and ti, which represents the time when the i-th participant starts on the floor si.For each participant print the minimum time of his/her arrival to the floor fi. If the elevator stops on the floor si at the time ti, then the i-th participant can enter the elevator immediately. If the participant starts on the floor si and that's the floor he wanted to reach initially (si = fi), then the time of arrival to the floor fi for this participant is considered equal to ti.
|
The first line contains two space-separated integers n and m (1 β€ n β€ 105, 2 β€ m β€ 108). Next n lines contain information about the participants in the form of three space-separated integers si fi ti (1 β€ si, fi β€ m, 0 β€ ti β€ 108), described in the problem statement.
|
Print n lines each containing one integer β the time of the arrival for each participant to the required floor.
|
Let's consider the first sample. The first participant starts at floor s = 2 by the time equal to t = 3. To get to the floor f = 4, he has to wait until the time equals 7, that's the time when the elevator will go upwards for the second time. Then the first participant should get on the elevator and go two floors up. In this case the first participant gets to the floor f at time equal to 9. The second participant starts at the time t = 0 on the floor s = 1, enters the elevator immediately, and arrives to the floor f = 2. The third participant doesn't wait for the elevator, because he needs to arrive to the same floor where he starts.
|
Input: 7 42 4 31 2 02 2 01 2 14 3 51 2 24 2 0 | Output: 91071075
|
Easy
| 2 | 1,552 | 267 | 111 | 1 |
1,866 |
K
|
1866K
|
K. Keen Tree Calculation
| 2,500 |
binary search; data structures; dp; geometry; graphs; implementation; trees
|
There is a tree of \(N\) vertices and \(N-1\) edges. The \(i\)-th edge connects vertices \(U_i\) and \(V_i\) and has a length of \(W_i\).Chaneka, the owner of the tree, asks you \(Q\) times. For the \(j\)-th question, the following is the question format: \(X_j\) \(K_j\) β If each edge that contains vertex \(X_j\) has its length multiplied by \(K_j\), what is the diameter of the tree? Notes: Each of Chaneka's question is independent, which means the changes in edge length do not influence the next questions. The diameter of a tree is the maximum possible distance between two different vertices in the tree.
|
The first line contains a single integer \(N\) (\(2\leq N\leq10^5\)) β the number of vertices in the tree.The \(i\)-th of the next \(N-1\) lines contains three integers \(U_i\), \(V_i\), and \(W_i\) (\(1 \leq U_i,V_i \leq N\); \(1\leq W_i\leq10^9\)) β an edge that connects vertices \(U_i\) and \(V_i\) with a length of \(W_i\). The edges form a tree.The \((N+1)\)-th line contains a single integer \(Q\) (\(1\leq Q\leq10^5\)) β the number of questions.The \(j\)-th of the next \(Q\) lines contains two integers \(X_j\) and \(K_j\) as described (\(1 \leq X_j \leq N\); \(1 \leq K_j \leq 10^9\)).
|
Output \(Q\) lines with an integer in each line. The integer in the \(j\)-th line represents the diameter of the tree on the \(j\)-th question.
|
In the first example, the following is the tree without any changes.The following is the tree on the \(1\)-st question.The maximum distance is between vertices \(6\) and \(7\), which is \(6+6+6=18\), so the diameter is \(18\).The following is the tree on the \(2\)-nd question.The maximum distance is between vertices \(2\) and \(6\), which is \(3+2+6=11\), so the diameter is \(11\).
|
Input: 7 5 1 2 1 4 2 3 4 1 2 5 3 6 1 6 4 7 2 2 4 3 3 2 | Output: 18 11
|
Expert
| 7 | 613 | 595 | 143 | 18 |
500 |
G
|
500G
|
G. New Year Running
| 3,200 |
number theory; trees
|
New Year is coming in Tree Island! In this island, as the name implies, there are n cities connected by n - 1 roads, and for any two distinct cities there always exists exactly one path between them. For every person in Tree Island, it takes exactly one minute to pass by exactly one road.There is a weird New Year tradition for runnners in Tree Island, which is called ""extreme run"". This tradition can be done as follows.A runner chooses two distinct cities a and b. For simplicity, let's denote the shortest path from city a to city b as p1, p2, ..., pl (here, p1 = a and pl = b holds). Then following happens: The runner starts at city a. The runner runs from city a to b, following the shortest path from city a to city b. When the runner arrives at city b, he turns his direction immediately (it takes no time), and runs towards city a, following the shortest path from city b to city a. When the runner arrives at city a, he turns his direction immediately (it takes no time), and runs towards city b, following the shortest path from city a to city b. Repeat step 3 and step 4 forever. In short, the course of the runner can be denoted as: Two runners JH and JY decided to run ""extremely"" in order to celebrate the New Year. JH has chosen two cities u and v, and JY has chosen two cities x and y. They decided to start running at the same moment, and run until they meet at the same city for the first time. Meeting on a road doesn't matter for them. Before running, they want to know the amount of time they will run.It is too hard for JH and JY to calculate this, so they ask you for help.
|
The first line contains a single positive integer n (5 β€ n β€ 2 Γ 105) β the number of cities in Tree Island.Next n - 1 lines describe the roads of Tree Island. The i-th line (1 β€ i β€ n - 1) of them contains two space-separated integers ai and bi (1 β€ ai, bi β€ n, ai β bi) β the vertices connected by a single road of the tree.The next line contains an integer t (1 β€ t β€ 2 Γ 105) β the number of test cases.Next t lines describes the test cases. The j-th line (1 β€ j β€ t) of them contains four space-separated integers uj, vj, xj, yj (1 β€ uj, vj, xj, yj β€ n, uj β vj, xj β yj). It means that in this test case, JH has chosen two cities uj and vj, JY has chosen two cities xj and yj. JH starts running at city uj, and JY starts running at city xj.
|
For each test case, print an integer describing the amount of time they should run in minutes. If they have to run for an infinitely long time (in other words, if they never meet at the same city), print -1 instead. If they meet at the beginning of their run, print 0.
|
The example looks like:
|
Input: 71 33 67 43 75 47 246 5 5 33 5 4 61 5 1 31 5 3 1 | Output: 210-1
|
Master
| 2 | 1,603 | 746 | 268 | 5 |
914 |
F
|
914F
|
F. Substrings in a String
| 3,000 |
bitmasks; brute force; data structures; string suffix structures; strings
|
Given a string s, process q queries, each having one of the following forms: 1 i c β Change the i-th character in the string to c. 2 l r y β Consider the substring of s starting at position l and ending at position r. Output the number of times y occurs as a substring in it.
|
The first line of the input contains the string s (1 β€ |s| β€ 105) of lowercase English letters.The second line contains an integer q (1 β€ q β€ 105) β the number of queries to process.The next q lines describe the queries and may have one of the following forms: 1 i c (1 β€ i β€ |s|) 2 l r y (1 β€ l β€ r β€ |s|) c is a lowercase English letter and y is a non-empty string consisting of only lowercase English letters.The sum of |y| over all queries of second type is at most 105.It is guaranteed that there is at least one query of second type.All strings are 1-indexed.|s| is the length of the string s.
|
For each query of type 2, output the required answer in a separate line.
|
Consider the first sample case. Initially, the string aba occurs 3 times in the range [1, 7]. Note that two occurrences may overlap. After the update, the string becomes ababcbaba and now aba occurs only once in the range [1, 7].
|
Input: ababababa32 1 7 aba1 5 c2 1 7 aba | Output: 31
|
Master
| 5 | 275 | 599 | 72 | 9 |
2,108 |
B
|
2108B
|
B. SUMdamental Decomposition
| 1,300 |
bitmasks; constructive algorithms; greedy; implementation; math
|
On a recent birthday, your best friend Maurice gave you a pair of numbers \(n\) and \(x\), and asked you to construct an array of positive numbers \(a\) of length \(n\) such that \(a_1 \oplus a_2 \oplus \cdots \oplus a_n = x\) \(^{\text{β}}\). This task seemed too simple to you, and therefore you decided to give Maurice a return gift by constructing an array among all such arrays that has the smallest sum of its elements. You immediately thought of a suitable array; however, since writing it down turned out to be too time-consuming, Maurice will have to settle for just the sum of its elements.\(^{\text{β}}\)\(\oplus\) denotes the bitwise XOR operation.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)). The description of the test cases follows. Each test case consists of a single line containing a pair of numbers \(n\) and \(x\) (\(1 \le n \le 10^9, \; 0 \le x \le 10^9\)) β the numbers given to you by Maurice.
|
For each test case, output your gift to Maurice β the sum of the elements of the array that satisfies all the described properties. If a suitable array does not exist, output \(-1\).
|
In the first test case, one of the suitable arrays is \([2, 3]\). It can be shown that it is impossible to achieve a smaller sum of array elements.In the second case, one of the suitable arrays is \([1, 3, 4]\). It can also be shown that this is the optimal amount.
|
Input: 82 13 61 02 05 02 2715 4312345678 9101112 | Output: 5 8 -1 2 8 27 55 21446778
|
Easy
| 5 | 660 | 330 | 182 | 21 |
2,052 |
B
|
2052B
| 2,400 |
Expert
| 0 | 0 | 0 | 0 | 20 |
|||||||
1,514 |
D
|
1514D
|
D. Cut and Stick
| 2,000 |
binary search; data structures; greedy; implementation; sortings
|
Baby Ehab has a piece of Cut and Stick with an array \(a\) of length \(n\) written on it. He plans to grab a pair of scissors and do the following to it: pick a range \((l, r)\) and cut out every element \(a_l\), \(a_{l + 1}\), ..., \(a_r\) in this range; stick some of the elements together in the same order they were in the array; end up with multiple pieces, where every piece contains some of the elements and every element belongs to some piece. More formally, he partitions the sequence \(a_l\), \(a_{l + 1}\), ..., \(a_r\) into subsequences. He thinks a partitioning is beautiful if for every piece (subsequence) it holds that, if it has length \(x\), then no value occurs strictly more than \(\lceil \frac{x}{2} \rceil\) times in it.He didn't pick a range yet, so he's wondering: for \(q\) ranges \((l, r)\), what is the minimum number of pieces he needs to partition the elements \(a_l\), \(a_{l + 1}\), ..., \(a_r\) into so that the partitioning is beautiful.A sequence \(b\) is a subsequence of an array \(a\) if \(b\) can be obtained from \(a\) by deleting some (possibly zero) elements. Note that it does not have to be contiguous.
|
The first line contains two integers \(n\) and \(q\) (\(1 \le n,q \le 3 \cdot 10^5\)) β the length of the array \(a\) and the number of queries.The second line contains \(n\) integers \(a_1\), \(a_2\), ..., \(a_{n}\) (\(1 \le a_i \le n\)) β the elements of the array \(a\).Each of the next \(q\) lines contains two integers \(l\) and \(r\) (\(1 \le l \le r \le n\)) β the range of this query.
|
For each query, print the minimum number of subsequences you need to partition this range into so that the partitioning is beautiful. We can prove such partitioning always exists.
|
In the first query, you can just put the whole array in one subsequence, since its length is \(6\), and no value occurs more than \(3\) times in it.In the second query, the elements of the query range are \([3,2,3,3]\). You can't put them all in one subsequence, since its length is \(4\), and \(3\) occurs more than \(2\) times. However, you can partition it into two subsequences: \([3]\) and \([2,3,3]\).
|
Input: 6 2 1 3 2 3 3 2 1 6 2 5 | Output: 1 2
|
Hard
| 5 | 1,145 | 392 | 179 | 15 |
1,237 |
F
|
1237F
|
F. Balanced Domino Placements
| 2,600 |
combinatorics; dp
|
Consider a square grid with \(h\) rows and \(w\) columns with some dominoes on it. Each domino covers exactly two cells of the grid that share a common side. Every cell is covered by at most one domino.Let's call a placement of dominoes on the grid perfectly balanced if no row and no column contains a pair of cells covered by two different dominoes. In other words, every row and column may contain no covered cells, one covered cell, or two covered cells that belong to the same domino.You are given a perfectly balanced placement of dominoes on a grid. Find the number of ways to place zero or more extra dominoes on this grid to keep the placement perfectly balanced. Output this number modulo \(998\,244\,353\).
|
The first line contains three integers \(h\), \(w\), and \(n\) (\(1 \le h, w \le 3600\); \(0 \le n \le 2400\)), denoting the dimensions of the grid and the number of already placed dominoes. The rows are numbered from \(1\) to \(h\), and the columns are numbered from \(1\) to \(w\).Each of the next \(n\) lines contains four integers \(r_{i, 1}, c_{i, 1}, r_{i, 2}, c_{i, 2}\) (\(1 \le r_{i, 1} \le r_{i, 2} \le h\); \(1 \le c_{i, 1} \le c_{i, 2} \le w\)), denoting the row id and the column id of the cells covered by the \(i\)-th domino. Cells \((r_{i, 1}, c_{i, 1})\) and \((r_{i, 2}, c_{i, 2})\) are distinct and share a common side.The given domino placement is perfectly balanced.
|
Output the number of ways to place zero or more extra dominoes on the grid to keep the placement perfectly balanced, modulo \(998\,244\,353\).
|
In the first example, the initial grid looks like this:Here are \(8\) ways to place zero or more extra dominoes to keep the placement perfectly balanced:In the second example, the initial grid looks like this:No extra dominoes can be placed here.
|
Input: 5 7 2 3 1 3 2 4 4 4 5 | Output: 8
|
Expert
| 2 | 717 | 687 | 142 | 12 |
1,572 |
B
|
1572B
|
B. Xor of 3
| 2,500 |
brute force; constructive algorithms; greedy; two pointers
|
You are given a sequence \(a\) of length \(n\) consisting of \(0\)s and \(1\)s.You can perform the following operation on this sequence: Pick an index \(i\) from \(1\) to \(n-2\) (inclusive). Change all of \(a_{i}\), \(a_{i+1}\), \(a_{i+2}\) to \(a_{i} \oplus a_{i+1} \oplus a_{i+2}\) simultaneously, where \(\oplus\) denotes the bitwise XOR operation Find a sequence of at most \(n\) operations that changes all elements of \(a\) to \(0\)s or report that it's impossible.We can prove that if there exists a sequence of operations of any length that changes all elements of \(a\) to \(0\)s, then there is also such a sequence of length not greater than \(n\).
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 10^4\)).The first line of each test case contains a single integer \(n\) (\(3 \le n \le 2\cdot10^5\)) β the length of \(a\).The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(a_i = 0\) or \(a_i = 1\)) β elements of \(a\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case, do the following: if there is no way of making all the elements of \(a\) equal to \(0\) after performing the above operation some number of times, print ""NO"". otherwise, in the first line print ""YES"", in the second line print \(k\) (\(0 \le k \le n\)) β the number of operations that you want to perform on \(a\), and in the third line print a sequence \(b_1, b_2, \dots, b_k\) (\(1 \le b_i \le n - 2\)) β the indices on which the operation should be applied. If there are multiple solutions, you may print any.
|
In the first example, the sequence contains only \(0\)s so we don't need to change anything.In the second example, we can transform \([1, 1, 1, 1, 0]\) to \([1, 1, 0, 0, 0]\) and then to \([0, 0, 0, 0, 0]\) by performing the operation on the third element of \(a\) and then on the first element of \(a\).In the third example, no matter whether we first perform the operation on the first or on the second element of \(a\) we will get \([1, 1, 1, 1]\), which cannot be transformed to \([0, 0, 0, 0]\).
|
Input: 3 3 0 0 0 5 1 1 1 1 0 4 1 0 0 1 | Output: YES 0 YES 2 3 1 NO
|
Expert
| 4 | 659 | 457 | 535 | 15 |
1,607 |
G
|
1607G
|
G. Banquet Preparations 1
| 2,200 |
greedy
|
A known chef has prepared \(n\) dishes: the \(i\)-th dish consists of \(a_i\) grams of fish and \(b_i\) grams of meat. The banquet organizers estimate the balance of \(n\) dishes as follows. The balance is equal to the absolute value of the difference between the total mass of fish and the total mass of meat.Technically, the balance equals to \(\left|\sum\limits_{i=1}^n a_i - \sum\limits_{i=1}^n b_i\right|\). The smaller the balance, the better.In order to improve the balance, a taster was invited. He will eat exactly \(m\) grams of food from each dish. For each dish, the taster determines separately how much fish and how much meat he will eat. The only condition is that he should eat exactly \(m\) grams of each dish in total.Determine how much of what type of food the taster should eat from each dish so that the value of the balance is as minimal as possible. If there are several correct answers, you may choose any of them.
|
The first line of input data contains an integer \(t\) (\(1 \leq t \leq 10^4\)) β the number of the test cases.Each test case's description is preceded by a blank line. Next comes a line that contains integers \(n\) and \(m\) (\(1 \leq n \leq 2 \cdot 10^5\); \(0 \leq m \leq 10^6\)). The next \(n\) lines describe dishes, the \(i\)-th of them contains a pair of integers \(a_i\) and \(b_i\) (\(0 \leq a_i, b_i \le 10^6\)) β the masses of fish and meat in the \(i\)-th dish.It is guaranteed that it is possible to eat \(m\) grams of food from each dish. In other words, \(m \leq a_i+b_i\) for all \(i\) from \(1\) to \(n\) inclusive.The sum of all \(n\) values over all test cases in the test does not exceed \(2 \cdot 10^5\).
|
For each test case, print on the first line the minimal balance value that can be achieved by eating exactly \(m\) grams of food from each dish.Then print \(n\) lines that describe a way to do this: the \(i\)-th line should contain two integers \(x_i\) and \(y_i\) (\(0 \leq x_i \leq a_i\); \(0 \leq y_i \leq b_i\); \(x_i+y_i=m\)), where \(x_i\) is how many grams of fish taster should eat from the \(i\)-th meal and \(y_i\) is how many grams of meat.If there are several ways to achieve a minimal balance, find any of them.
|
Input: 8 1 5 3 4 1 6 3 4 2 2 1 3 4 2 2 4 1 3 1 7 3 6 1 7 1 8 1 9 3 6 1 8 1 9 30 10 3 4 3 1 3 2 4 1 5 4 0 7 6 4 0 8 4 1 5 3 | Output: 0 2 3 1 3 3 0 1 1 1 1 2 1 3 0 4 3 0 6 0 6 0 6 7 1 5 1 5 6 0 0 3 1 3 1 3 1 0 0 4 2 2 0 4 3 1 1 3
|
Hard
| 1 | 938 | 725 | 524 | 16 |
|
344 |
A
|
344A
|
A. Magnets
| 800 |
implementation
|
Mad scientist Mike entertains himself by arranging rows of dominoes. He doesn't need dominoes, though: he uses rectangular magnets instead. Each magnet has two poles, positive (a ""plus"") and negative (a ""minus""). If two magnets are put together at a close distance, then the like poles will repel each other and the opposite poles will attract each other.Mike starts by laying one magnet horizontally on the table. During each following step Mike adds one more magnet horizontally to the right end of the row. Depending on how Mike puts the magnet on the table, it is either attracted to the previous one (forming a group of multiple magnets linked together) or repelled by it (then Mike lays this magnet at some distance to the right from the previous one). We assume that a sole magnet not linked to others forms a group of its own. Mike arranged multiple magnets in a row. Determine the number of groups that the magnets formed.
|
The first line of the input contains an integer n (1 β€ n β€ 100000) β the number of magnets. Then n lines follow. The i-th line (1 β€ i β€ n) contains either characters ""01"", if Mike put the i-th magnet in the ""plus-minus"" position, or characters ""10"", if Mike put the magnet in the ""minus-plus"" position.
|
On the single line of the output print the number of groups of magnets.
|
The first testcase corresponds to the figure. The testcase has three groups consisting of three, one and two magnets.The second testcase has two groups, each consisting of two magnets.
|
Input: 6101010011010 | Output: 3
|
Beginner
| 1 | 935 | 310 | 71 | 3 |
962 |
E
|
962E
|
E. Byteland, Berland and Disputed Cities
| 2,200 |
constructive algorithms; greedy
|
The cities of Byteland and Berland are located on the axis \(Ox\). In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line \(Ox\) there are three types of cities: the cities of Byteland, the cities of Berland, disputed cities. Recently, the project BNET has been launched β a computer network of a new generation. Now the task of the both countries is to connect the cities so that the network of this country is connected.The countries agreed to connect the pairs of cities with BNET cables in such a way that: If you look at the only cities of Byteland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables, If you look at the only cities of Berland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables. Thus, it is necessary to choose a set of pairs of cities to connect by cables in such a way that both conditions are satisfied simultaneously. Cables allow bi-directional data transfer. Each cable connects exactly two distinct cities.The cost of laying a cable from one city to another is equal to the distance between them. Find the minimum total cost of laying a set of cables so that two subsets of cities (Byteland and disputed cities, Berland and disputed cities) are connected.Each city is a point on the line \(Ox\). It is technically possible to connect the cities \(a\) and \(b\) with a cable so that the city \(c\) (\(a < c < b\)) is not connected to this cable, where \(a\), \(b\) and \(c\) are simultaneously coordinates of the cities \(a\), \(b\) and \(c\).
|
The first line contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^{5}\)) β the number of cities.The following \(n\) lines contains an integer \(x_i\) and the letter \(c_i\) (\(-10^{9} \le x_i \le 10^{9}\)) β the coordinate of the city and its type. If the city belongs to Byteland, \(c_i\) equals to 'B'. If the city belongs to Berland, \(c_i\) equals to Β«RΒ». If the city is disputed, \(c_i\) equals to 'P'. All cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates.
|
Print the minimal total length of such set of cables, that if we delete all Berland cities (\(c_i\)='R'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities (\(c_i\)='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables.
|
In the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be \(5 + 3 + 4 = 12\).In the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neighboring cities of Berland. The cities of Berland have coordinates \(10, 21, 32\), so to connect them you need two cables of length \(11\) and \(11\). The cities of Byteland have coordinates \(14\) and \(16\), so to connect them you need one cable of length \(2\). Thus, the total length of all cables is \(11 + 11 + 2 = 24\).
|
Input: 4-5 R0 P3 P7 B | Output: 12
|
Hard
| 2 | 1,709 | 536 | 384 | 9 |
1,279 |
E
|
1279E
|
E. New Year Permutations
| 2,700 |
combinatorics; dp
|
Yeah, we failed to make up a New Year legend for this problem.A permutation of length \(n\) is an array of \(n\) integers such that every integer from \(1\) to \(n\) appears in it exactly once. An element \(y\) of permutation \(p\) is reachable from element \(x\) if \(x = y\), or \(p_x = y\), or \(p_{p_x} = y\), and so on. The decomposition of a permutation \(p\) is defined as follows: firstly, we have a permutation \(p\), all elements of which are not marked, and an empty list \(l\). Then we do the following: while there is at least one not marked element in \(p\), we find the leftmost such element, list all elements that are reachable from it in the order they appear in \(p\), mark all of these elements, then cyclically shift the list of those elements so that the maximum appears at the first position, and add this list as an element of \(l\). After all elements are marked, \(l\) is the result of this decomposition.For example, if we want to build a decomposition of \(p = [5, 4, 2, 3, 1, 7, 8, 6]\), we do the following: initially \(p = [5, 4, 2, 3, 1, 7, 8, 6]\) (bold elements are marked), \(l = []\); the leftmost unmarked element is \(5\); \(5\) and \(1\) are reachable from it, so the list we want to shift is \([5, 1]\); there is no need to shift it, since maximum is already the first element; \(p = [\textbf{5}, 4, 2, 3, \textbf{1}, 7, 8, 6]\), \(l = [[5, 1]]\); the leftmost unmarked element is \(4\), the list of reachable elements is \([4, 2, 3]\); the maximum is already the first element, so there's no need to shift it; \(p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, 7, 8, 6]\), \(l = [[5, 1], [4, 2, 3]]\); the leftmost unmarked element is \(7\), the list of reachable elements is \([7, 8, 6]\); we have to shift it, so it becomes \([8, 6, 7]\); \(p = [\textbf{5}, \textbf{4}, \textbf{2}, \textbf{3}, \textbf{1}, \textbf{7}, \textbf{8}, \textbf{6}]\), \(l = [[5, 1], [4, 2, 3], [8, 6, 7]]\); all elements are marked, so \([[5, 1], [4, 2, 3], [8, 6, 7]]\) is the result. The New Year transformation of a permutation is defined as follows: we build the decomposition of this permutation; then we sort all lists in decomposition in ascending order of the first elements (we don't swap the elements in these lists, only the lists themselves); then we concatenate the lists into one list which becomes a new permutation. For example, the New Year transformation of \(p = [5, 4, 2, 3, 1, 7, 8, 6]\) is built as follows: the decomposition is \([[5, 1], [4, 2, 3], [8, 6, 7]]\); after sorting the decomposition, it becomes \([[4, 2, 3], [5, 1], [8, 6, 7]]\); \([4, 2, 3, 5, 1, 8, 6, 7]\) is the result of the transformation. We call a permutation good if the result of its transformation is the same as the permutation itself. For example, \([4, 3, 1, 2, 8, 5, 6, 7]\) is a good permutation; and \([5, 4, 2, 3, 1, 7, 8, 6]\) is bad, since the result of transformation is \([4, 2, 3, 5, 1, 8, 6, 7]\).Your task is the following: given \(n\) and \(k\), find the \(k\)-th (lexicographically) good permutation of length \(n\).
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Then the test cases follow. Each test case is represented by one line containing two integers \(n\) and \(k\) (\(1 \le n \le 50\), \(1 \le k \le 10^{18}\)).
|
For each test case, print the answer to it as follows: if the number of good permutations of length \(n\) is less than \(k\), print one integer \(-1\); otherwise, print the \(k\)-th good permutation on \(n\) elements (in lexicographical order).
|
Input: 5 3 3 5 15 4 13 6 8 4 2 | Output: 2 1 3 3 1 2 5 4 -1 1 2 6 3 4 5 1 2 4 3
|
Master
| 2 | 3,066 | 248 | 244 | 12 |
|
75 |
C
|
75C
|
C. Modified GCD
| 1,600 |
binary search; number theory
|
Well, here is another math class task. In mathematics, GCD is the greatest common divisor, and it's an easy task to calculate the GCD between two positive integers.A common divisor for two positive numbers is a number which both numbers are divisible by.But your teacher wants to give you a harder task, in this task you have to find the greatest common divisor d between two integers a and b that is in a given range from low to high (inclusive), i.e. low β€ d β€ high. It is possible that there is no common divisor in the given range.You will be given the two integers a and b, then n queries. Each query is a range from low to high and you have to answer each query.
|
The first line contains two integers a and b, the two integers as described above (1 β€ a, b β€ 109). The second line contains one integer n, the number of queries (1 β€ n β€ 104). Then n lines follow, each line contains one query consisting of two integers, low and high (1 β€ low β€ high β€ 109).
|
Print n lines. The i-th of them should contain the result of the i-th query in the input. If there is no common divisor in the given range for any query, you should print -1 as a result for this query.
|
Input: 9 2731 510 119 11 | Output: 3-19
|
Medium
| 2 | 668 | 291 | 201 | 0 |
|
1,357 |
B2
|
1357B2
|
B2. ""Is the number divisible by 3?"" oracle
| 0 |
*special
|
Implement a quantum oracle on \(N\) qubits which checks whether the input bit string is a little-endian notation of a number that is divisible by 3. Your operation should take the following inputs: an array of \(N \le 8\) qubits ""inputs"" in an arbitrary state. a qubit ""output"" in an arbitrary state. Your operation should perform a unitary transformation on those qubits that can be described by its effect on the basis states: if ""inputs"" is in the basis state \(|x\rangle\) and ""output"" is in the basis state \(|y\rangle\), the result of applying the operation should be \(|x\rangle|y \oplus f(x)\rangle\), where \(f(x) = 1\) if the integer represented by the bit string \(x\) is divisible by \(3\), and \(0\) otherwise.For example, if the qubits passed to your operation are in the state \(\frac{1}{\sqrt{2}}(|110\rangle + |001\rangle)_x \otimes |0\rangle_y = \frac{1}{\sqrt{2}}(|3\rangle + |4\rangle)_x \otimes |0\rangle_y\), the state of the system after applying the operation should be \(\frac{1}{\sqrt{2}}(|3\rangle_x\otimes |1\rangle_y + |4\rangle_x |0\rangle_y) = \frac{1}{\sqrt{2}}(|110\rangle_x\otimes |1\rangle_y + |001\rangle_x |0\rangle_y)\).Your code should have the following signature (note that your operation should have Adjoint and Controlled variants defined for it; is Adj+Ctl in the operation signature will generate them automatically based on your code):namespace Solution { open Microsoft.Quantum.Intrinsic; operation Solve (inputs : Qubit[], output : Qubit) : Unit is Adj+Ctl { // your code here }}Your code is not allowed to use measurements or arbitrary rotation gates. This operation can be implemented using just the X gate and its controlled variants (possibly with multiple qubits as controls).
|
Beginner
| 1 | 1,737 | 0 | 0 | 13 |
||||
1,747 |
B
|
1747B
|
B. BAN BAN
| 900 |
constructive algorithms
|
You are given an integer \(n\).Let's define \(s(n)\) as the string ""BAN"" concatenated \(n\) times. For example, \(s(1)\) = ""BAN"", \(s(3)\) = ""BANBANBAN"". Note that the length of the string \(s(n)\) is equal to \(3n\).Consider \(s(n)\). You can perform the following operation on \(s(n)\) any number of times (possibly zero): Select any two distinct indices \(i\) and \(j\) \((1 \leq i, j \leq 3n, i \ne j)\). Then, swap \(s(n)_i\) and \(s(n)_j\). You want the string ""BAN"" to not appear in \(s(n)\) as a subsequence. What's the smallest number of operations you have to do to achieve this? Also, find one such shortest sequence of operations.A string \(a\) is a subsequence of a string \(b\) if \(a\) can be obtained from \(b\) by deletion of several (possibly, zero or all) characters.
|
The input consists of multiple test cases. The first line contains a single integer \(t\) \((1 \leq t \leq 100)\) β the number of test cases. The description of the test cases follows.The only line of each test case contains a single integer \(n\) \((1 \leq n \leq 100)\).
|
For each test case, in the first line output \(m\) (\(0 \le m \le 10^5\)) β the minimum number of operations required. It's guaranteed that the objective is always achievable in at most \(10^5\) operations under the constraints of the problem. Then, output \(m\) lines. The \(k\)-th of these lines should contain two integers \(i_k\), \(j_k\) \((1\leq i_k, j_k \leq 3n, i_k \ne j_k)\) denoting that you want to swap characters at indices \(i_k\) and \(j_k\) at the \(k\)-th operation. After all \(m\) operations, ""BAN"" must not appear in \(s(n)\) as a subsequence. If there are multiple possible answers, output any.
|
In the first testcase, \(s(1) = \) ""BAN"", we can swap \(s(1)_1\) and \(s(1)_2\), converting \(s(1)\) to ""ABN"", which does not contain ""BAN"" as a subsequence.In the second testcase, \(s(2) = \) ""BANBAN"", we can swap \(s(2)_2\) and \(s(2)_6\), converting \(s(2)\) to ""BNNBAA"", which does not contain ""BAN"" as a subsequence.
|
Input: 2 1 2 | Output: 1 1 2 1 2 6
|
Beginner
| 1 | 794 | 272 | 618 | 17 |
2,022 |
E2
|
2022E2
|
E2. Billetes MX (Hard Version)
| 2,600 |
binary search; combinatorics; data structures; dsu; graphs
|
This is the hard version of the problem. In this version, it is guaranteed that \(q \leq 10^5\). You can make hacks only if both versions of the problem are solved.An integer grid \(A\) with \(p\) rows and \(q\) columns is called beautiful if: All elements of the grid are integers between \(0\) and \(2^{30}-1\), and For any subgrid, the XOR of the values at the corners is equal to \(0\). Formally, for any four integers \(i_1\), \(i_2\), \(j_1\), \(j_2\) (\(1 \le i_1 < i_2 \le p\); \(1 \le j_1 < j_2 \le q\)), \(A_{i_1, j_1} \oplus A_{i_1, j_2} \oplus A_{i_2, j_1} \oplus A_{i_2, j_2} = 0\), where \(\oplus\) denotes the bitwise XOR operation. There is a partially filled integer grid \(G\) with \(n\) rows and \(m\) columns where only \(k\) cells are filled. Polycarp wants to know how many ways he can assign integers to the unfilled cells so that the grid is beautiful.However, Monocarp thinks that this problem is too easy. Therefore, he will perform \(q\) updates on the grid. In each update, he will choose an unfilled cell and assign an integer to it. Note that these updates are persistent. That is, changes made to the grid will apply when processing future updates.For each of the \(q + 1\) states of the grid, the initial state and after each of the \(q\) queries, determine the number of ways Polycarp can assign integers to the unfilled cells so that the grid is beautiful. Since this number can be very large, you are only required to output their values modulo \(10^9+7\).
|
The first line contains \(t\) (\(1 \le t \le 10^4\)) β the number of test cases.The first line of each test case contains four integers \(n\), \(m\), \(k\) and \(q\) (\(2 \le n, m \le 10^5\); \(0 \le k, q \leq 10^5\)) β the number of rows, the number of columns, the number of fixed cells, and the number of updates.The following \(k\) lines contain three integers \(r\), \(c\) and \(v\) (\(1 \le r \le n, 1 \le c \le m\); \(0 \le v < 2^{30}\)) indicating that \(G_{r, c}\) is assigned the integer \(v\).The following \(q\) lines contain three integers \(r\), \(c\) and \(v\) (\(1 \le r \le n, 1 \le c \le m\); \(0 \le v < 2^{30}\)) indicating that \(G_{r, c}\) is assigned the integer \(v\).It is guaranteed that the pairs \((r,c)\) over all assignments are distinct.It is guaranteed that the sum of \(n\), \(m\), \(k\) and \(q\) over all test cases does not exceed \(10^5\) respectively.
|
For each test case, output \(q + 1\) lines. The \(i\)-th line of output should contain the answer of the \(i\)-th state of the grid modulo \(10^9 + 7\).
|
In the first test case of the example, we initially have the following grid: \(0\)\(6\)\(10\)\(6\)\(0\)\(12\)\(10\)\(12\)\(?\) It can be proven that the only valid value for tile \((3, 3)\) is \(0\), so the first answer is \(1\). For the second query, the grid does not satisfy the condition, and thus the answer is \(0\).
|
Input: 33 3 8 12 1 63 2 121 2 62 2 01 3 101 1 02 3 123 1 103 3 12 5 2 01 1 101 2 302 5 0 21 1 101 2 30 | Output: 1 0 489373567 651321892 769740174 489373567
|
Expert
| 5 | 1,491 | 889 | 152 | 20 |
1,562 |
C
|
1562C
|
C. Rings
| 1,500 |
constructive algorithms; math
|
Frodo was caught by Saruman. He tore a pouch from Frodo's neck, shook out its contents βthere was a pile of different rings: gold and silver...""How am I to tell which is the One?!"" the mage howled.""Throw them one by one into the Cracks of Doom and watch when Mordor falls!"" Somewhere in a parallel Middle-earth, when Saruman caught Frodo, he only found \(n\) rings. And the \(i\)-th ring was either gold or silver. For convenience Saruman wrote down a binary string \(s\) of \(n\) characters, where the \(i\)-th character was 0 if the \(i\)-th ring was gold, and 1 if it was silver.Saruman has a magic function \(f\), which takes a binary string and returns a number obtained by converting the string into a binary number and then converting the binary number into a decimal number. For example, \(f(001010) = 10, f(111) = 7, f(11011101) = 221\).Saruman, however, thinks that the order of the rings plays some important role. He wants to find \(2\) pairs of integers \((l_1, r_1), (l_2, r_2)\), such that: \(1 \le l_1 \le n\), \(1 \le r_1 \le n\), \(r_1-l_1+1\ge \lfloor \frac{n}{2} \rfloor\) \(1 \le l_2 \le n\), \(1 \le r_2 \le n\), \(r_2-l_2+1\ge \lfloor \frac{n}{2} \rfloor\) Pairs \((l_1, r_1)\) and \((l_2, r_2)\) are distinct. That is, at least one of \(l_1 \neq l_2\) and \(r_1 \neq r_2\) must hold. Let \(t\) be the substring \(s[l_1:r_1]\) of \(s\), and \(w\) be the substring \(s[l_2:r_2]\) of \(s\). Then there exists non-negative integer \(k\), such that \(f(t) = f(w) \cdot k\).Here substring \(s[l:r]\) denotes \(s_ls_{l+1}\ldots s_{r-1}s_r\), and \(\lfloor x \rfloor\) denotes rounding the number down to the nearest integer.Help Saruman solve this problem! It is guaranteed that under the constraints of the problem at least one solution exists.
|
Each test contains multiple test cases.The first line contains one positive integer \(t\) (\(1 \le t \le 10^3\)), denoting the number of test cases. Description of the test cases follows.The first line of each test case contains one positive integer \(n\) (\(2 \le n \le 2 \cdot 10^4\)) β length of the string.The second line of each test case contains a non-empty binary string of length \(n\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).
|
For every test case print four integers \(l_1\), \(r_1\), \(l_2\), \(r_2\), which denote the beginning of the first substring, the end of the first substring, the beginning of the second substring, and the end of the second substring, respectively.If there are multiple solutions, print any.
|
In the first testcase \(f(t) = f(1111) = 15\), \(f(w) = f(101) = 5\).In the second testcase \(f(t) = f(111000111) = 455\), \(f(w) = f(000111) = 7\).In the third testcase \(f(t) = f(0000) = 0\), \(f(w) = f(1000) = 8\).In the fourth testcase \(f(t) = f(11011) = 27\), \(f(w) = f(011) = 3\).In the fifth testcase \(f(t) = f(001111) = 15\), \(f(w) = f(011) = 3\).
|
Input: 7 6 101111 9 111000111 8 10000000 5 11011 6 001111 3 101 30 100000000000000100000000000000 | Output: 3 6 1 3 1 9 4 9 5 8 1 4 1 5 3 5 1 6 2 4 1 2 2 3 1 15 16 30
|
Medium
| 2 | 1,766 | 479 | 291 | 15 |
220 |
B
|
220B
|
B. Little Elephant and Array
| 1,800 |
constructive algorithms; data structures
|
The Little Elephant loves playing with arrays. He has array a, consisting of n positive integers, indexed from 1 to n. Let's denote the number with index i as ai. Additionally the Little Elephant has m queries to the array, each query is characterised by a pair of integers lj and rj (1 β€ lj β€ rj β€ n). For each query lj, rj the Little Elephant has to count, how many numbers x exist, such that number x occurs exactly x times among numbers alj, alj + 1, ..., arj.Help the Little Elephant to count the answers to all queries.
|
The first line contains two space-separated integers n and m (1 β€ n, m β€ 105) β the size of array a and the number of queries to it. The next line contains n space-separated positive integers a1, a2, ..., an (1 β€ ai β€ 109). Next m lines contain descriptions of queries, one per line. The j-th of these lines contains the description of the j-th query as two space-separated integers lj and rj (1 β€ lj β€ rj β€ n).
|
In m lines print m integers β the answers to the queries. The j-th line should contain the answer to the j-th query.
|
Input: 7 23 1 2 2 3 3 71 73 4 | Output: 31
|
Medium
| 2 | 525 | 411 | 116 | 2 |
|
1,687 |
E
|
1687E
|
E. Become Big For Me
| 3,500 |
combinatorics; constructive algorithms; greedy; math; number theory
|
Come, let's build a world where even the weak are not forgotten!βKijin Seija, Double Dealing CharactersShinmyoumaru has a mallet that can turn objects bigger or smaller. She is testing it out on a sequence \(a\) and a number \(v\) whose initial value is \(1\). She wants to make \(v = \gcd\limits_{i\ne j}\{a_i\cdot a_j\}\) by no more than \(10^5\) operations (\(\gcd\limits_{i\ne j}\{a_i\cdot a_j\}\) denotes the \(\gcd\) of all products of two distinct elements of the sequence \(a\)). In each operation, she picks a subsequence \(b\) of \(a\), and does one of the followings: Enlarge: \(v = v \cdot \mathrm{lcm}(b)\) Reduce: \(v = \frac{v}{\mathrm{lcm}(b)}\) Note that she does not need to guarantee that \(v\) is an integer, that is, \(v\) does not need to be a multiple of \(\mathrm{lcm}(b)\) when performing Reduce.Moreover, she wants to guarantee that the total length of \(b\) chosen over the operations does not exceed \(10^6\). Fine a possible operation sequence for her. You don't need to minimize anything.
|
The first line contains a single integer \(n\) (\(2\leq n\leq 10^5\)) β the size of sequence \(a\).The second line contains \(n\) integers \(a_1,a_2,\cdots,a_n\) (\(1\leq a_i\leq 10^6\)) β the sequence \(a\).It can be shown that the answer exists.
|
The first line contains a non-negative integer \(k\) (\(0\leq k\leq 10^5\)) β the number of operations.The following \(k\) lines contains several integers. For each line, the first two integers \(f\) (\(f\in\{0,1\}\)) and \(p\) (\(1\le p\le n\)) stand for the option you choose (\(0\) for Enlarge and \(1\) for Reduce) and the length of \(b\). The other \(p\) integers of the line \(i_1,i_2,\ldots,i_p\) (\(1\le i_1<i_2<\ldots<i_p\le n\)) represents the indexes of the subsequence. Formally, \(b_j=a_{i_j}\).
|
Test case 1:\(\gcd\limits_{i\ne j}\{a_i\cdot a_j\}=\gcd\{60,90,150\}=30\).Perform \(v = v\cdot \operatorname{lcm}\{a_1,a_2,a_3\}=30\).Test case 2:\(\gcd\limits_{i\ne j}\{a_i\cdot a_j\}=8\).Perform \(v = v\cdot \operatorname{lcm}\{a_4\}=16\).Perform \(v = \frac{v}{\operatorname{lcm}\{a_1\}}=8\).
|
Input: 3 6 10 15 | Output: 1 0 3 1 2 3
|
Master
| 5 | 1,018 | 247 | 508 | 16 |
1,975 |
E
|
1975E
|
E. Chain Queries
| 2,100 |
binary search; data structures; dfs and similar; implementation; trees
|
You are given a tree of \(n\) vertices numbered from \(1\) to \(n\). Initially, all vertices are colored white or black.You are asked to perform \(q\) queries: ""u"" β toggle the color of vertex \(u\) (if it was white, change it to black and vice versa). After each query, you should answer whether all the black vertices form a chain. That is, there exist two black vertices such that the simple path between them passes through all the black vertices and only the black vertices. Specifically, if there is only one black vertex, they form a chain. If there are no black vertices, they do not form a chain.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1\leq t\leq 10^4\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(q\) (\(1\leq n,q\leq 2\cdot 10^5\)).The second line of each test case contains \(n\) integers \(c_1,c_2,\ldots,c_n\) (\(c_i \in \{ \mathtt{0}, \mathtt{1} \}\)) β the initial color of the vertices. \(c_i\) denotes the color of vertex \(i\) where \(\mathtt{0}\) denotes the color white, and \(\mathtt{1}\) denotes the color black.Then \(n - 1\) lines follow, each line contains two integers \(x_i\) and \(y_i\) (\(1 \le x_i,y_i \le n\)), indicating an edge between vertices \(x_i\) and \(y_i\). It is guaranteed that these edges form a tree.The following \(q\) lines each contain an integer \(u_i\) (\(1 \le u_i \le n\)), indicating the color of vertex \(u_i\) needs to be toggled.It is guaranteed that the sum of \(n\) and \(q\) over all test cases respectively does not exceed \(2\cdot 10^5\).
|
For each query, output ""Yes"" if the black vertices form a chain, and output ""No"" otherwise.You can output ""Yes"" and ""No"" in any case (for example, strings ""yEs"", ""yes"", ""Yes"" and ""YES"" will be recognized as a positive response).
|
In the second test case, the color of the vertices are as follows:The initial tree: The first query toggles the color of vertex \(4\): The second query toggles the color of vertex \(3\): The third query toggles the color of vertex \(2\): The fourth query toggles the color of vertex \(5\):
|
Input: 22 11 01 215 41 0 0 0 01 21 31 53 44325 | Output: No No Yes Yes No
|
Hard
| 5 | 607 | 1,021 | 244 | 19 |
896 |
D
|
896D
|
D. Nephren Runs a Cinema
| 2,900 |
chinese remainder theorem; combinatorics; math; number theory
|
Lakhesh loves to make movies, so Nephren helps her run a cinema. We may call it No. 68 Cinema.However, one day, the No. 68 Cinema runs out of changes (they don't have 50-yuan notes currently), but Nephren still wants to start their business. (Assume that yuan is a kind of currency in Regulu Ere.)There are three types of customers: some of them bring exactly a 50-yuan note; some of them bring a 100-yuan note and Nephren needs to give a 50-yuan note back to him/her; some of them bring VIP cards so that they don't need to pay for the ticket.Now n customers are waiting outside in queue. Nephren wants to know how many possible queues are there that they are able to run smoothly (i.e. every customer can receive his/her change), and that the number of 50-yuan notes they have after selling tickets to all these customers is between l and r, inclusive. Two queues are considered different if there exists a customer whose type is different in two queues. As the number can be large, please output the answer modulo p.
|
One line containing four integers n (1 β€ n β€ 105), p (1 β€ p β€ 2Β·109), l and r (0 β€ l β€ r β€ n).
|
One line indicating the answer modulo p.
|
We use A, B and C to indicate customers with 50-yuan notes, customers with 100-yuan notes and customers with VIP cards respectively.For the first sample, the different possible queues that there are 2 50-yuan notes left are AAAB, AABA, ABAA, AACC, ACAC, ACCA, CAAC, CACA and CCAA, and the different possible queues that there are 3 50-yuan notes left are AAAC, AACA, ACAA and CAAA. So there are 13 different queues satisfying the first sample. Similarly, there are 35 different queues satisfying the second sample.
|
Input: 4 97 2 3 | Output: 13
|
Master
| 4 | 1,019 | 94 | 40 | 8 |
2,021 |
B
|
2021B
|
B. Maximize Mex
| 1,200 |
brute force; greedy; math; number theory
|
You are given an array \(a\) of \(n\) positive integers and an integer \(x\). You can do the following two-step operation any (possibly zero) number of times: Choose an index \(i\) (\(1 \leq i \leq n\)). Increase \(a_i\) by \(x\), in other words \(a_i := a_i + x\). Find the maximum value of the \(\operatorname{MEX}\) of \(a\) if you perform the operations optimally.The \(\operatorname{MEX}\) (minimum excluded value) of an array is the smallest non-negative integer that is not in the array. For example: The \(\operatorname{MEX}\) of \([2,2,1]\) is \(0\) because \(0\) is not in the array. The \(\operatorname{MEX}\) of \([3,1,0,1]\) is \(2\) because \(0\) and \(1\) are in the array but \(2\) is not. The \(\operatorname{MEX}\) of \([0,3,1,2]\) is \(4\) because \(0\), \(1\), \(2\) and \(3\) are in the array but \(4\) is not.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5000\)). The description of the test cases follows.The first line of each test case contains two integers \(n\) and \(x\) (\(1 \le n \le 2 \cdot 10^5\); \(1 \le x \le 10^9\)) β the length of the array and the integer to be used in the operation.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \le 10^9\)) β the given array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output a single integer: the maximum \(\operatorname{MEX}\) of \(a\) if you perform the operations optimally.
|
In the first test case, the \(\operatorname{MEX}\) of \(a\) is \(4\) without performing any operations, which is the maximum.In the second test case, the \(\operatorname{MEX}\) of \(a\) is \(5\) without performing any operations. If we perform two operations both with \(i=1\), we will have the array \(a=[5,3,4,1,0,2]\). Then, the \(\operatorname{MEX}\) of \(a\) will become \(6\), which is the maximum.In the third test case, the \(\operatorname{MEX}\) of \(a\) is \(0\) without performing any operations, which is the maximum.
|
Input: 36 30 3 2 1 5 26 21 3 4 1 0 24 52 5 10 3 | Output: 4 6 0
|
Easy
| 4 | 831 | 574 | 129 | 20 |
908 |
A
|
908A
|
A. New Year and Counting Cards
| 800 |
brute force; implementation
|
Your friend has n cards.You know that each card has a lowercase English letter on one side and a digit on the other.Currently, your friend has laid out the cards on a table so only one side of each card is visible.You would like to know if the following statement is true for cards that your friend owns: ""If a card has a vowel on one side, then it has an even digit on the other side."" More specifically, a vowel is one of 'a', 'e', 'i', 'o' or 'u', and even digit is one of '0', '2', '4', '6' or '8'.For example, if a card has 'a' on one side, and '6' on the other side, then this statement is true for it. Also, the statement is true, for example, for a card with 'b' and '4', and for a card with 'b' and '3' (since the letter is not a vowel). The statement is false, for example, for card with 'e' and '5'. You are interested if the statement is true for all cards. In particular, if no card has a vowel, the statement is true.To determine this, you can flip over some cards to reveal the other side. You would like to know what is the minimum number of cards you need to flip in the worst case in order to verify that the statement is true.
|
The first and only line of input will contain a string s (1 β€ |s| β€ 50), denoting the sides of the cards that you can see on the table currently. Each character of s is either a lowercase English letter or a digit.
|
Print a single integer, the minimum number of cards you must turn over to verify your claim.
|
In the first sample, we must turn over both cards. Note that even though both cards have the same letter, they could possibly have different numbers on the other side.In the second sample, we don't need to turn over any cards. The statement is vacuously true, since you know your friend has no cards with a vowel on them.In the third sample, we need to flip the second and fourth cards.
|
Input: ee | Output: 2
|
Beginner
| 2 | 1,147 | 214 | 92 | 9 |
1,671 |
A
|
1671A
|
A. String Building
| 800 |
implementation
|
You are given a string \(s\). You have to determine whether it is possible to build the string \(s\) out of strings aa, aaa, bb and/or bbb by concatenating them. You can use the strings aa, aaa, bb and/or bbb any number of times and in any order.For example: aaaabbb can be built as aa \(+\) aa \(+\) bbb; bbaaaaabbb can be built as bb \(+\) aaa \(+\) aa \(+\) bbb; aaaaaa can be built as aa \(+\) aa \(+\) aa; abab cannot be built from aa, aaa, bb and/or bbb.
|
The first line contains one integer \(t\) (\(1 \le t \le 1000\)) β the number of test cases.Each test case consists of one line containing the string \(s\) (\(1 \le |s| \le 50\)), consisting of characters a and/or b.
|
For each test case, print YES if it is possible to build the string \(s\). Otherwise, print NO.You may print each letter in any case (for example, YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).
|
The first four test cases of the example are described in the statement.
|
Input: 8aaaabbbbbaaaaabbbaaaaaaabababaaaabbbaaa | Output: YES YES YES NO NO NO NO YES
|
Beginner
| 1 | 460 | 216 | 261 | 16 |
1,722 |
G
|
1722G
|
G. Even-Odd XOR
| 1,500 |
bitmasks; constructive algorithms; greedy
|
Given an integer \(n\), find any array \(a\) of \(n\) distinct nonnegative integers less than \(2^{31}\) such that the bitwise XOR of the elements on odd indices equals the bitwise XOR of the elements on even indices.
|
The first line of the input contains an integer \(t\) (\(1 \leq t \leq 629\)) β the number of test cases.Then \(t\) lines follow, each containing a single integer \(n\) \((3 \leq n \leq 2\cdot10^5)\) β the length of the array.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot 10^5\).
|
For each test case, output one line containing \(n\) distinct integers that satisfy the conditions.If there are multiple answers, you can output any of them.
|
In the first test case the XOR on odd indices is \(4 \oplus 1 \oplus 0 \oplus 7 = 2\) and the XOR on even indices is \(2 \oplus 5 \oplus 6 \oplus 3= 2\).
|
Input: 7 8 3 4 5 6 7 9 | Output: 4 2 1 5 0 6 7 3 2 1 3 2 1 3 0 2 0 4 5 3 4 1 2 12 3 8 1 2 3 4 5 6 7 8 2 3 7 4 0 5 6 9
|
Medium
| 3 | 217 | 317 | 157 | 17 |
1,017 |
E
|
1017E
|
E. The Supersonic Rocket
| 2,400 |
geometry; hashing; strings
|
After the war, the supersonic rocket became the most common public transportation.Each supersonic rocket consists of two ""engines"". Each engine is a set of ""power sources"". The first engine has \(n\) power sources, and the second one has \(m\) power sources. A power source can be described as a point \((x_i, y_i)\) on a 2-D plane. All points in each engine are different.You can manipulate each engine separately. There are two operations that you can do with each engine. You can do each operation as many times as you want. For every power source as a whole in that engine: \((x_i, y_i)\) becomes \((x_i+a, y_i+b)\), \(a\) and \(b\) can be any real numbers. In other words, all power sources will be shifted. For every power source as a whole in that engine: \((x_i, y_i)\) becomes \((x_i \cos \theta - y_i \sin \theta, x_i \sin \theta + y_i \cos \theta)\), \(\theta\) can be any real number. In other words, all power sources will be rotated.The engines work as follows: after the two engines are powered, their power sources are being combined (here power sources of different engines may coincide). If two power sources \(A(x_a, y_a)\) and \(B(x_b, y_b)\) exist, then for all real number \(k\) that \(0 \lt k \lt 1\), a new power source will be created \(C_k(kx_a+(1-k)x_b,ky_a+(1-k)y_b)\). Then, this procedure will be repeated again with all new and old power sources. After that, the ""power field"" from all power sources will be generated (can be considered as an infinite set of all power sources occurred).A supersonic rocket is ""safe"" if and only if after you manipulate the engines, destroying any power source and then power the engine, the power field generated won't be changed (comparing to the situation where no power source erased). Two power fields are considered the same if and only if any power source in one field belongs to the other one as well.Given a supersonic rocket, check whether it is safe or not.
|
The first line contains two integers \(n\), \(m\) (\(3 \le n, m \le 10^5\)) β the number of power sources in each engine.Each of the next \(n\) lines contains two integers \(x_i\) and \(y_i\) (\(0\leq x_i, y_i\leq 10^8\)) β the coordinates of the \(i\)-th power source in the first engine.Each of the next \(m\) lines contains two integers \(x_i\) and \(y_i\) (\(0\leq x_i, y_i\leq 10^8\)) β the coordinates of the \(i\)-th power source in the second engine.It is guaranteed that there are no two or more power sources that are located in the same point in each engine.
|
Print ""YES"" if the supersonic rocket is safe, otherwise ""NO"".You can print each letter in an arbitrary case (upper or lower).
|
The first sample: Those near pairs of blue and orange points actually coincide. First, manipulate the first engine: use the second operation with \(\theta = \pi\) (to rotate all power sources \(180\) degrees).The power sources in the first engine become \((0, 0)\), \((0, -2)\), and \((-2, 0)\). Second, manipulate the second engine: use the first operation with \(a = b = -2\).The power sources in the second engine become \((-2, 0)\), \((0, 0)\), \((0, -2)\), and \((-1, -1)\). You can examine that destroying any point, the power field formed by the two engines are always the solid triangle \((0, 0)\), \((-2, 0)\), \((0, -2)\).In the second sample, no matter how you manipulate the engines, there always exists a power source in the second engine that power field will shrink if you destroy it.
|
Input: 3 40 00 22 00 22 22 01 1 | Output: YES
|
Expert
| 3 | 1,940 | 569 | 129 | 10 |
1,433 |
C
|
1433C
|
C. Dominant Piranha
| 900 |
constructive algorithms; greedy
|
There are \(n\) piranhas with sizes \(a_1, a_2, \ldots, a_n\) in the aquarium. Piranhas are numbered from left to right in order they live in the aquarium.Scientists of the Berland State University want to find if there is dominant piranha in the aquarium. The piranha is called dominant if it can eat all the other piranhas in the aquarium (except itself, of course). Other piranhas will do nothing while the dominant piranha will eat them.Because the aquarium is pretty narrow and long, the piranha can eat only one of the adjacent piranhas during one move. Piranha can do as many moves as it needs (or as it can). More precisely: The piranha \(i\) can eat the piranha \(i-1\) if the piranha \(i-1\) exists and \(a_{i - 1} < a_i\). The piranha \(i\) can eat the piranha \(i+1\) if the piranha \(i+1\) exists and \(a_{i + 1} < a_i\). When the piranha \(i\) eats some piranha, its size increases by one (\(a_i\) becomes \(a_i + 1\)).Your task is to find any dominant piranha in the aquarium or determine if there are no such piranhas.Note that you have to find any (exactly one) dominant piranha, you don't have to find all of them.For example, if \(a = [5, 3, 4, 4, 5]\), then the third piranha can be dominant. Consider the sequence of its moves: The piranha eats the second piranha and \(a\) becomes \([5, \underline{5}, 4, 5]\) (the underlined piranha is our candidate). The piranha eats the third piranha and \(a\) becomes \([5, \underline{6}, 5]\). The piranha eats the first piranha and \(a\) becomes \([\underline{7}, 5]\). The piranha eats the second piranha and \(a\) becomes \([\underline{8}]\). You have to answer \(t\) independent test cases.
|
The first line of the input contains one integer \(t\) (\(1 \le t \le 2 \cdot 10^4\)) β the number of test cases. Then \(t\) test cases follow.The first line of the test case contains one integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) β the number of piranhas in the aquarium. The second line of the test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 10^9\)), where \(a_i\) is the size of the \(i\)-th piranha.It is guaranteed that the sum of \(n\) does not exceed \(3 \cdot 10^5\) (\(\sum n \le 3 \cdot 10^5\)).
|
For each test case, print the answer: -1 if there are no dominant piranhas in the aquarium or index of any dominant piranha otherwise. If there are several answers, you can print any.
|
The first test case of the example is described in the problem statement.In the second test case of the example, there are no dominant piranhas in the aquarium.In the third test case of the example, the fourth piranha can firstly eat the piranha to the left and the aquarium becomes \([4, 4, 5, 4]\), then it can eat any other piranha in the aquarium.
|
Input: 6 5 5 3 4 4 5 3 1 1 1 5 4 4 3 4 4 5 5 5 4 3 2 3 1 1 2 5 5 4 3 5 5 | Output: 3 -1 4 3 3 1
|
Beginner
| 2 | 1,655 | 536 | 183 | 14 |
754 |
D
|
754D
|
D. Fedor and coupons
| 2,100 |
binary search; data structures; greedy; sortings
|
All our characters have hobbies. The same is true for Fedor. He enjoys shopping in the neighboring supermarket. The goods in the supermarket have unique integer ids. Also, for every integer there is a product with id equal to this integer. Fedor has n discount coupons, the i-th of them can be used with products with ids ranging from li to ri, inclusive. Today Fedor wants to take exactly k coupons with him.Fedor wants to choose the k coupons in such a way that the number of such products x that all coupons can be used with this product x is as large as possible (for better understanding, see examples). Fedor wants to save his time as well, so he asks you to choose coupons for him. Help Fedor!
|
The first line contains two integers n and k (1 β€ k β€ n β€ 3Β·105) β the number of coupons Fedor has, and the number of coupons he wants to choose.Each of the next n lines contains two integers li and ri ( - 109 β€ li β€ ri β€ 109) β the description of the i-th coupon. The coupons can be equal.
|
In the first line print single integer β the maximum number of products with which all the chosen coupons can be used. The products with which at least one coupon cannot be used shouldn't be counted.In the second line print k distinct integers p1, p2, ..., pk (1 β€ pi β€ n) β the ids of the coupons which Fedor should choose.If there are multiple answers, print any of them.
|
In the first example if we take the first two coupons then all the products with ids in range [40, 70] can be bought with both coupons. There are 31 products in total.In the second example, no product can be bought with two coupons, that is why the answer is 0. Fedor can choose any two coupons in this example.
|
Input: 4 21 10040 70120 130125 180 | Output: 311 2
|
Hard
| 4 | 700 | 290 | 373 | 7 |
1,149 |
C
|
1149C
|
C. Tree Generatorβ’
| 2,700 |
data structures; implementation; trees
|
Owl Pacino has always been into trees β unweighted rooted trees in particular. He loves determining the diameter of every tree he sees β that is, the maximum length of any simple path in the tree.Owl Pacino's owl friends decided to present him the Tree Generatorβ’ β a powerful machine creating rooted trees from their descriptions. An \(n\)-vertex rooted tree can be described by a bracket sequence of length \(2(n - 1)\) in the following way: find any walk starting and finishing in the root that traverses each edge exactly twice β once down the tree, and later up the tree. Then follow the path and write down ""("" (an opening parenthesis) if an edge is followed down the tree, and "")"" (a closing parenthesis) otherwise.The following figure shows sample rooted trees and their descriptions: Owl wrote down the description of an \(n\)-vertex rooted tree. Then, he rewrote the description \(q\) times. However, each time he wrote a new description, he picked two different characters in the description he wrote the last time, swapped them and wrote down the resulting string. He always made sure that each written string was the description of a rooted tree.Pacino then used Tree Generatorβ’ for each description he wrote down. What is the diameter of each constructed tree?
|
The first line of the input contains two integers \(n, q\) (\(3 \le n \le 100\,000\), \(1 \le q \le 100\,000\)) β the number of vertices in the tree and the number of changes to the tree description. The following line contains a description of the initial tree β a string of length \(2(n-1)\) consisting of opening and closing parentheses.Each of the following \(q\) lines describes a single change to the description and contains two space-separated integers \(a_i, b_i\) (\(2 \leq a_i, b_i \leq 2n-3\)) which identify the indices of two brackets to be swapped. You can assume that the description will change after each query, and that after each change a tree can be constructed from the description.
|
Output \(q + 1\) integers β the diameter of each constructed tree, in the order their descriptions have been written down.
|
The following figure shows each constructed tree and its description in the first example test:
|
Input: 5 5 (((()))) 4 5 3 4 5 6 3 6 2 5 | Output: 4 3 3 2 4 4
|
Master
| 3 | 1,278 | 704 | 122 | 11 |
1,116 |
C1
|
1116C1
|
C1. Alternating bits oracle
| 0 |
*special
|
Implement a quantum oracle on \(N\) qubits which checks whether the bits in the input vector \(\vec{x}\) alternate (i.e., implements the function \(f(\vec{x}) = 1\) if \(\vec{x}\) does not have a pair of adjacent bits in state 00 or 11).You have to implement an operation which takes the following inputs: an array of \(N\) (\(1 \le N \le 7\)) qubits \(x\) in an arbitrary state (input register), a qubit \(y\) in an arbitrary state (output qubit),and performs a transformation \(|x\rangle|y\rangle \rightarrow |x\rangle|y \oplus f(x)\rangle\). The operation doesn't have an output; its ""output"" is the state in which it leaves the qubits. Note that the input register \(x\) has to remain unchanged after applying the operation.Your code should have the following signature:namespace Solution { open Microsoft.Quantum.Primitive; open Microsoft.Quantum.Canon; operation Solve (x : Qubit[], y : Qubit) : Unit { body (...) { // your code here } adjoint auto; }}Note: the operation has to have an adjoint specified for it; adjoint auto means that the adjoint will be generated automatically. For details on adjoint, see Operation Definitions.You are not allowed to use measurements in your operation.
|
Beginner
| 1 | 1,198 | 0 | 0 | 11 |
||||
1,987 |
C
|
1987C
|
C. Basil's Garden
| 1,200 |
dp; greedy
|
There are \(n\) flowers in a row, the \(i\)-th of them initially has a positive height of \(h_i\) meters.Every second, the wind will blow from the left, causing the height of some flowers to decrease.Specifically, every second, for each \(i\) from \(1\) to \(n\), in this order, the following happens: If \(i = n\) or \(h_i > h_{i + 1}\), the value of \(h_i\) changes to \(\max(0, h_i - 1)\). How many seconds will pass before \(h_i=0\) for all \(1 \le i \le n\) for the first time?
|
Each test contains multiple test cases. The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 10^5\)) β the number of flowers.The second line of each test case contains \(n\) integers \(h_1, h_2, \ldots, h_n\) (\(1 \le h_i \le 10 ^ 9\)) β the heights of the flowers.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 number of seconds that will pass before \(h_i=0\) for all \(1 \le i \le n\).
|
In the first test case, the flower heights change as follows: \([1, 1, 2] \rightarrow [1, 1, 1] \rightarrow [1, 1, 0] \rightarrow [1, 0, 0] \rightarrow [0, 0, 0]\).In the second test case, the flower heights change as follows: \([3, 1] \rightarrow [2, 0] \rightarrow [1, 0] \rightarrow [0, 0]\).
|
Input: 431 1 223 11957 4 4 3 2 | Output: 4 3 9 7
|
Easy
| 2 | 482 | 525 | 126 | 19 |
848 |
A
|
848A
|
A. From Y to Y
| 1,600 |
constructive algorithms
|
From beginning till end, this message has been waiting to be conveyed.For a given unordered multiset of n lowercase English letters (""multi"" means that a letter may appear more than once), we treat all letters as strings of length 1, and repeat the following operation n - 1 times: Remove any two elements s and t from the set, and add their concatenation s + t to the set. The cost of such operation is defined to be , where f(s, c) denotes the number of times character c appears in string s.Given a non-negative integer k, construct any valid non-empty set of no more than 100 000 letters, such that the minimum accumulative cost of the whole process is exactly k. It can be shown that a solution always exists.
|
The first and only line of input contains a non-negative integer k (0 β€ k β€ 100 000) β the required minimum cost.
|
Output a non-empty string of no more than 100 000 lowercase English letters β any multiset satisfying the requirements, concatenated to be a string.Note that the printed string doesn't need to be the final concatenated string. It only needs to represent an unordered multiset of letters.
|
For the multiset {'a', 'b', 'a', 'b', 'a', 'b', 'a', 'b'}, one of the ways to complete the process is as follows: {""ab"", ""a"", ""b"", ""a"", ""b"", ""a"", ""b""}, with a cost of 0; {""aba"", ""b"", ""a"", ""b"", ""a"", ""b""}, with a cost of 1; {""abab"", ""a"", ""b"", ""a"", ""b""}, with a cost of 1; {""abab"", ""ab"", ""a"", ""b""}, with a cost of 0; {""abab"", ""aba"", ""b""}, with a cost of 1; {""abab"", ""abab""}, with a cost of 1; {""abababab""}, with a cost of 8. The total cost is 12, and it can be proved to be the minimum cost of the process.
|
Input: 12 | Output: abababab
|
Medium
| 1 | 716 | 113 | 287 | 8 |
1,792 |
B
|
1792B
|
B. Stand-up Comedian
| 1,200 |
greedy; math
|
Eve is a beginner stand-up comedian. Her first show gathered a grand total of two spectators: Alice and Bob.Eve prepared \(a_1 + a_2 + a_3 + a_4\) jokes to tell, grouped by their type: type 1: both Alice and Bob like them; type 2: Alice likes them, but Bob doesn't; type 3: Bob likes them, but Alice doesn't; type 4: neither Alice nor Bob likes them. Initially, both spectators have their mood equal to \(0\). When a spectator hears a joke he/she likes, his/her mood increases by \(1\). When a spectator hears a joke he/she doesn't like, his/her mood decreases by \(1\). If the mood of a spectator becomes negative (strictly below zero), he/she leaves.When someone leaves, Eve gets sad and ends the show. If no one leaves, and Eve is out of jokes, she also ends the show.Thus, Eve wants to arrange her jokes in such a way that the show lasts as long as possible. Help her to calculate the maximum number of jokes she can tell before the show ends.
|
The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of testcases.The only line of each testcase contains four integers \(a_1, a_2, a_3, a_4\) (\(0 \le a_1, a_2, a_3, a_4 \le 10^8\); \(a_1 + a_2 + a_3 + a_4 \ge 1\)) β the number of jokes of each type Eve prepared.
|
For each testcase, print a single integer β the maximum number of jokes Eve can tell before at least one of the spectators leaves or before she runs out of jokes.
|
In the first testcase, Eve only has jokes of the first type. Thus, there's no order to choose. She tells all her jokes, both Alice and Bob like them. Their mood becomes \(5\). The show ends after Eve runs out of jokes.In the second testcase, Eve only has jokes of the fourth type. Thus, once again no order to choose. She tells a joke, and neither Alice, nor Bob likes it. Their mood decrease by one, becoming \(-1\). They both have negative mood, thus, both leave, and the show ends.In the third testcase, first, Eve tells both jokes of the first type. Both Alice and Bob has mood \(2\). Then she can tell \(2\) jokes of the third type. Alice's mood becomes \(0\). Bob's mood becomes \(4\). Then \(4\) jokes of the second type. Alice's mood becomes \(4\). Bob's mood becomes \(0\). Then another \(4\) jokes of the third type. Alice's mood becomes \(0\). Bob's mood becomes \(4\). Then the remaining joke of the second type. Alice's mood becomes \(1\). Bob's mood becomes \(3\). Then one more joke of the third type, and a joke of the fourth type, for example. Alice's mood becomes \(-1\), she leaves, and the show ends. In the fourth testcase, Eve should first tell the jokes both spectators like, then the jokes they don't. She can tell \(4\) jokes of the fourth type until the spectators leave.
|
Input: 45 0 0 00 0 0 52 5 10 63 0 0 7 | Output: 5 1 15 7
|
Easy
| 2 | 947 | 294 | 162 | 17 |
1,459 |
A
|
1459A
|
A. Red-Blue Shuffle
| 800 |
math; probabilities
|
There are \(n\) cards numbered \(1, \ldots, n\). The card \(i\) has a red digit \(r_i\) and a blue digit \(b_i\) written on it.We arrange all \(n\) cards in random order from left to right, with all permutations of \(1, \ldots, n\) having the same probability. We then read all red digits on the cards from left to right, and obtain an integer \(R\). In the same way, we read all blue digits and obtain an integer \(B\). When reading a number, leading zeros can be ignored. If all digits in a number are zeros, then the number is equal to \(0\). Below is an illustration of a possible rearrangement of three cards, and how \(R\) and \(B\) can be found. Two players, Red and Blue, are involved in a bet. Red bets that after the shuffle \(R > B\), and Blue bets that \(R < B\). If in the end \(R = B\), the bet results in a draw, and neither player wins.Determine, which of the two players is more likely (has higher probability) to win the bet, or that their chances are equal. Refer to the Note section for a formal discussion of comparing probabilities.
|
The first line contains a single integer \(T\) (\(1 \leq T \leq 100\)) β the number of test cases.Descriptions of \(T\) test cases follow. Each test case description starts with a line containing a single integer \(n\) (\(1 \leq n \leq 1000\)) β the number of cards.The following line contains a string of \(n\) digits \(r_1, \ldots, r_n\) β red digits on cards \(1, \ldots, n\) respectively.The following line contains a string of \(n\) digits \(b_1, \ldots, b_n\) β blue digits on cards \(1, \ldots, n\) respectively.Note that digits in the same line are not separated with any delimiters.
|
Print \(T\) answers for the test cases in order, one per line.If Red has a strictly higher change to win, print ""RED"".If Blue has a strictly higher change to win, print ""BLUE"".If both players are equally likely to win, print ""EQUAL"".Note that all answers are case-sensitive.
|
Formally, let \(n_R\) be the number of permutations of cards \(1, \ldots, n\) such that the resulting numbers \(R\) and \(B\) satisfy \(R > B\). Similarly, let \(n_B\) be the number of permutations such that \(R < B\). If \(n_R > n_B\), you should print ""RED"". If \(n_R < n_B\), you should print ""BLUE"". If \(n_R = n_B\), print ""EQUAL"".In the first sample case, \(R = 777\) and \(B = 111\) regardless of the card order, thus Red always wins.In the second sample case, there are two card orders when Red wins, and four card orders when Blue wins: order \(1, 2, 3\): \(314 > 159\); order \(1, 3, 2\): \(341 > 195\); order \(2, 1, 3\): \(134 < 519\); order \(2, 3, 1\): \(143 < 591\); order \(3, 1, 2\): \(431 < 915\); order \(3, 2, 1\): \(413 < 951\).Since \(R < B\) is more frequent, the answer is ""BLUE"".In the third sample case, \(R = B\) regardless of the card order, thus the bet is always a draw, and both Red and Blue have zero chance to win.
|
Input: 3 3 777 111 3 314 159 5 09281 09281 | Output: RED BLUE EQUAL
|
Beginner
| 2 | 1,054 | 591 | 280 | 14 |
2,101 |
C
|
2101C
|
C. 23 Kingdom
| 2,200 |
binary search; brute force; data structures; greedy; ternary search; two pointers
|
The distance of a value \(x\) in an array \(c\), denoted as \(d_x(c)\), is defined as the largest gap between any two occurrences of \(x\) in \(c\). Formally, \(d_x(c) = \max(j - i)\) over all pairs \(i < j\) where \(c_i = c_j = x\). If \(x\) appears only once or not at all in \(c\), then \(d_x(c) = 0\). The beauty of an array is the sum of the distances of each distinct value in the array. Formally, the beauty of an array \(c\) is equal to \(\sum\limits_{1\le x\le n} d_x(c)\).Given an array \(a\) of length \(n\), an array \(b\) is nice if it also has length \(n\) and its elements satisfy \(1\le b_i\le a_i\) for all \(1\le i\le n\). Your task is to find the maximum possible beauty of any nice array.
|
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 2\cdot10^5\)) β the length of 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\).It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case, output a single integer representing the maximum possible beauty among all nice arrays.
|
In the first test case, if \(b = [1, 2, 1, 2]\), then \(d_1(b) = 3 - 1 = 2\) and \(d_2(b) = 4 - 2 = 2\), resulting in a beauty of \(2 + 2 = 4\). It can be proven that there are no nice arrays with a beauty greater than \(4\).In the second test case, both \(b = [1, 1]\) and \(b = [2, 2]\) are valid solutions with a beauty of \(1\).In the third test case, if \(b = [1, 2, 1, 4, 1, 2, 1, 1, 1, 2]\) with \(d_1(b) = 9 - 1 = 8\), \(d_2(b) = 10 - 2 = 8\), and \(d_4(b) = 0\), resulting in a beauty of \(16\).
|
Input: 441 2 1 222 2101 2 1 5 1 2 2 1 1 281 5 2 8 4 1 4 2 | Output: 4 1 16 16
|
Hard
| 6 | 708 | 503 | 107 | 21 |
77 |
A
|
77A
|
A. Heroes
| 1,400 |
brute force; implementation
|
The year of 2012 is coming...According to an ancient choradrican legend in this very year, in 2012, Diablo and his brothers Mephisto and Baal will escape from hell, and innumerable hordes of demons will enslave the human world. But seven brave heroes have already gathered on the top of a mountain Arreat to protect us mere mortals from the effect of this terrible evil.The seven great heroes are: amazon Anka, barbarian Chapay, sorceress Cleo, druid Troll, necromancer Dracul, paladin Snowy and a professional hit girl Hexadecimal. Heroes already know how much experience will be given for each of the three megabosses: a for Mephisto, b for Diablo and c for Baal.Here's the problem: heroes are as much as seven and megabosses are only three! Then our heroes decided to split into three teams, where each team will go to destroy their own megaboss. Each team member will receive a of experience, rounded down, where x will be the amount of experience for the killed megaboss and y β the number of people in the team.Heroes do not want to hurt each other's feelings, so they want to split into teams so that the difference between the hero who received the maximum number of experience and the hero who received the minimum number of experience were minimal. Since there can be several divisions into teams, then you need to find the one in which the total amount of liking in teams were maximum.It is known that some heroes like others. But if hero p likes hero q, this does not mean that the hero q likes hero p. No hero likes himself.The total amount of liking in teams is the amount of ordered pairs (p, q), such that heroes p and q are in the same group, and hero p likes hero q (but it is not important if hero q likes hero p). In case of heroes p and q likes each other and they are in the same group, this pair should be counted twice, as (p, q) and (q, p).A team can consist even of a single hero, but it is important that every megaboss was destroyed. All heroes must be involved in the campaign against evil. None of the heroes can be in more than one team.It is guaranteed that every hero is able to destroy any megaboss alone.
|
The first line contains a single non-negative integer n (0 β€ n β€ 42) β amount of liking between the heroes. Next n lines describe liking in the form ""p likes q"", meaning that the hero p likes the hero q (p β q). Every liking is described in the input exactly once, no hero likes himself.In the last line are given three integers a, b and c (1 β€ a, b, c β€ 2Β·109), separated by spaces: the experience for Mephisto, the experience for Diablo and experience for Baal.In all the pretests, except for examples from the statement, the following condition is satisfied: a = b = c.
|
Print two integers β the minimal difference in the experience between two heroes who will receive the maximum and minimum number of experience points, and the maximal total amount of liking in teams (the number of friendships between heroes that end up in one team).When calculating the second answer, the team division should satisfy the difference-minimizing contraint. I.e. primary you should minimize the difference in the experience and secondary you should maximize the total amount of liking.
|
A note to first example: it the first team should be Dracul, Troll and Anka, in the second one Hexadecimal and Snowy, and in the third Cleo ΠΈ Chapay.
|
Input: 3Troll likes DraculDracul likes AnkaSnowy likes Hexadecimal210 200 180 | Output: 30 3
|
Easy
| 2 | 2,139 | 574 | 499 | 0 |
910 |
C
|
910C
|
C. Minimum Sum
| 1,700 |
constructive algorithms; greedy; math
|
Petya has n positive integers a1, a2, ..., an. His friend Vasya decided to joke and replaced all digits in Petya's numbers with a letters. He used the lowercase letters of the Latin alphabet from 'a' to 'j' and replaced all digits 0 with one letter, all digits 1 with another letter and so on. For any two different digits Vasya used distinct letters from 'a' to 'j'.Your task is to restore Petya's numbers. The restored numbers should be positive integers without leading zeros. Since there can be multiple ways to do it, determine the minimum possible sum of all Petya's numbers after the restoration. It is guaranteed that before Vasya's joke all Petya's numbers did not have leading zeros.
|
The first line contains a single integer n (1 β€ n β€ 1 000) β the number of Petya's numbers.Each of the following lines contains non-empty string si consisting of lowercase Latin letters from 'a' to 'j' β the Petya's numbers after Vasya's joke. The length of each string does not exceed six characters.
|
Determine the minimum sum of all Petya's numbers after the restoration. The restored numbers should be positive integers without leading zeros. It is guaranteed that the correct restore (without leading zeros) exists for all given tests.
|
In the first example, you need to replace the letter 'a' with the digit 1, the letter 'b' with the digit 0, the letter 'd' with the digit 2, the letter 'e' with the digit 3, and the letter 'j' with the digit 4. So after the restoration numbers will look like [10, 23, 14]. The sum of them is equal to 47, which is the minimum possible sum of the numbers after the correct restoration.In the second example the numbers after the restoration can look like: [120468, 3579, 2468, 10024, 3]. In the second example the numbers after the restoration can look like: [11, 22, 11].
|
Input: 3abdeaj | Output: 47
|
Medium
| 3 | 693 | 301 | 237 | 9 |
1,375 |
E
|
1375E
|
E. Inversion SwapSort
| 2,500 |
constructive algorithms; greedy; sortings
|
Madeline has an array \(a\) of \(n\) integers. A pair \((u, v)\) of integers forms an inversion in \(a\) if: \(1 \le u < v \le n\). \(a_u > a_v\). Madeline recently found a magical paper, which allows her to write two indices \(u\) and \(v\) and swap the values \(a_u\) and \(a_v\). Being bored, she decided to write a list of pairs \((u_i, v_i)\) with the following conditions: all the pairs in the list are distinct and form an inversion in \(a\). all the pairs that form an inversion in \(a\) are in the list. Starting from the given array, if you swap the values at indices \(u_1\) and \(v_1\), then the values at indices \(u_2\) and \(v_2\) and so on, then after all pairs are processed, the array \(a\) will be sorted in non-decreasing order. Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.
|
The first line of the input contains a single integer \(n\) (\(1 \le n \le 1000\)) β the length of the array.Next line contains \(n\) integers \(a_1,a_2,...,a_n\) \((1 \le a_i \le 10^9)\) β elements of the array.
|
Print -1 if no such list exists. Otherwise in the first line you should print a single integer \(m\) (\(0 \le m \le \dfrac{n(n-1)}{2}\)) β number of pairs in the list.The \(i\)-th of the following \(m\) lines should contain two integers \(u_i, v_i\) (\(1 \le u_i < v_i\le n\)).If there are multiple possible answers, you may find any of them.
|
In the first sample test case the array will change in this order \([3,1,2] \rightarrow [2,1,3] \rightarrow [1,2,3]\).In the second sample test case it will be \([1,8,1,6] \rightarrow [1,6,1,8] \rightarrow [1,1,6,8]\).In the third sample test case the array is already sorted.
|
Input: 3 3 1 2 | Output: 2 1 3 1 2
|
Expert
| 3 | 875 | 212 | 342 | 13 |
178 |
D1
|
178D1
|
D1. Magic Squares
| 1,500 |
The Smart Beaver from ABBYY loves puzzles. One of his favorite puzzles is the magic square. He has recently had an idea to automate the solution of this puzzle. The Beaver decided to offer this challenge to the ABBYY Cup contestants.The magic square is a matrix of size n Γ n. The elements of this matrix are integers. The sum of numbers in each row of the matrix is equal to some number s. The sum of numbers in each column of the matrix is also equal to s. In addition, the sum of the elements on the main diagonal is equal to s and the sum of elements on the secondary diagonal is equal to s. Examples of magic squares are given in the following figure: Magic squares You are given a set of n2 integers ai. It is required to place these numbers into a square matrix of size n Γ n so that they form a magic square. Note that each number must occur in the matrix exactly the same number of times as it occurs in the original set.It is guaranteed that a solution exists!
|
The first input line contains a single integer n. The next line contains n2 integers ai ( - 108 β€ ai β€ 108), separated by single spaces.The input limitations for getting 20 points are: 1 β€ n β€ 3 The input limitations for getting 50 points are: 1 β€ n β€ 4 It is guaranteed that there are no more than 9 distinct numbers among ai. The input limitations for getting 100 points are: 1 β€ n β€ 4
|
The first line of the output should contain a single integer s. In each of the following n lines print n integers, separated by spaces and describing the resulting magic square. In the resulting magic square the sums in the rows, columns and diagonals must be equal to s. If there are multiple solutions, you are allowed to print any of them.
|
Input: 31 2 3 4 5 6 7 8 9 | Output: 152 7 69 5 14 3 8
|
Medium
| 0 | 970 | 387 | 342 | 1 |
||
1,253 |
A
|
1253A
|
A. Single Push
| 1,000 |
implementation
|
You're given two arrays \(a[1 \dots n]\) and \(b[1 \dots n]\), both of the same length \(n\).In order to perform a push operation, you have to choose three integers \(l, r, k\) satisfying \(1 \le l \le r \le n\) and \(k > 0\). Then, you will add \(k\) to elements \(a_l, a_{l+1}, \ldots, a_r\).For example, if \(a = [3, 7, 1, 4, 1, 2]\) and you choose \((l = 3, r = 5, k = 2)\), the array \(a\) will become \([3, 7, \underline{3, 6, 3}, 2]\).You can do this operation at most once. Can you make array \(a\) equal to array \(b\)?(We consider that \(a = b\) if and only if, for every \(1 \le i \le n\), \(a_i = b_i\))
|
The first line contains a single integer \(t\) (\(1 \le t \le 20\)) β the number of test cases in the input.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 100\ 000\)) β the number of elements in each array.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 1000\)).The third line of each test case contains \(n\) integers \(b_1, b_2, \ldots, b_n\) (\(1 \le b_i \le 1000\)).It is guaranteed that the sum of \(n\) over all test cases doesn't exceed \(10^5\).
|
For each test case, output one line containing ""YES"" if it's possible to make arrays \(a\) and \(b\) equal by performing at most once the described operation or ""NO"" if it's impossible.You can print each letter in any case (upper or lower).
|
The first test case is described in the statement: we can perform a push operation with parameters \((l=3, r=5, k=2)\) to make \(a\) equal to \(b\).In the second test case, we would need at least two operations to make \(a\) equal to \(b\).In the third test case, arrays \(a\) and \(b\) are already equal.In the fourth test case, it's impossible to make \(a\) equal to \(b\), because the integer \(k\) has to be positive.
|
Input: 4 6 3 7 1 4 1 2 3 7 3 6 3 2 5 1 1 1 1 1 1 2 1 3 1 2 42 42 42 42 1 7 6 | Output: YES NO YES NO
|
Beginner
| 1 | 615 | 539 | 244 | 12 |
631 |
C
|
631C
|
C. Report
| 1,700 |
data structures; sortings
|
Each month Blake gets the report containing main economic indicators of the company ""Blake Technologies"". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first ri numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).Employees of the ""Blake Technologies"" are preparing the report right now. You know the initial sequence ai of length n and the description of each manager, that is value ri and his favourite order. You are asked to speed up the process and determine how the final report will look like.
|
The first line of the input contains two integers n and m (1 β€ n, m β€ 200 000) β the number of commodities in the report and the number of managers, respectively.The second line contains n integers ai (|ai| β€ 109) β the initial report before it gets to the first manager.Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers ti and ri (, 1 β€ ri β€ n), meaning that the i-th manager sorts the first ri numbers either in the non-descending (if ti = 1) or non-ascending (if ti = 2) order.
|
Print n integers β the final report, which will be passed to Blake by manager number m.
|
In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake.
|
Input: 3 11 2 32 2 | Output: 2 1 3
|
Medium
| 2 | 878 | 567 | 87 | 6 |
1,982 |
F
|
1982F
|
F. Sorting Problem Again
| 2,600 |
binary search; data structures; sortings
|
You have an array \(a\) of \(n\) elements. There are also \(q\) modifications of the array. Before the first modification and after each modification, you would like to know the following:What is the minimum length subarray that needs to be sorted in non-decreasing order in order for the array \(a\) to be completely sorted in non-decreasing order?More formally, you want to select a subarray of the array \((l, r)\) with the minimum value of \(r - l + 1\). After that, you will sort the elements \(a_{l}, a_{l + 1}, \ldots, a_{r}\) and want the condition \(a_{i} \le a_{i + 1}\) to hold for all \(1 \le i < n\). If the array is already sorted in non-decreasing order, then \(l\) and \(r\) should be considered as equal to \(-1\).Note that finding such \((l, r)\) does not change the array in any way. The modifications themselves take the form: assign \(a_{pos} = x\) for given \(pos\) and \(x\).
|
Each test consists of several test cases. The first line contains an integer \(t\) (\(1 \le t \le 10\)) β the number of test cases. Then follows the description of test cases.The first line of each test case contains a single integer \(n\) (\(1 \le n \le 5 \cdot 10^{5}\)).The second line of each test case contains \(n\) integers \(a_{i}\) (\(0 \le |a_{i}| \le 10^{9}\)) β the initial elements of the array \(a\).The third line of each test case contains a number \(q\) (\(0 \le q \le 5 \cdot 10^{5}\)) β the number of modifications to the array.The following \(q\) lines of each test case contain two integers \(pos_{i}\) (\(1 \le pos_{i} \le n\)) and \(val_{i}\) (\(0 \le |val_{i}| \le 10^{9}\)) β this means that for the \(i\)-th modification, \(a_{pos_{i}}\) is assigned the value \(val_{i}\).It is guaranteed that the sum of \(n\) and the sum of \(q\) for all test cases does not exceed \(5 \cdot 10^{5}\).
|
For each test case, output \(q + 1\) lines. Each line should contain \(2\) integers \(l, r\) β the boundaries of the minimum subarray, such that sorting it will make the array \(a\) completely sorted. If \(a\) is already sorted, then output \(l = -1\), \(r = -1\).
|
Let's consider the first test case: Initially, the array is sorted in non-decreasing order: \([2, 2, 3, 4, 5]\) After the first query, the array looks like this: \([\color{red}{2}, \color{red}{1}, 3, 4, 5]\). After the second query, the array looks like this: \([\color{red}{2}, \color{red}{1}, \color{red}{3}, \color{red}{1}, 5]\). After the third query, the array looks like this: \([1, 1, \color{red}{3}, \color{red}{1}, 5]\). The red segments indicate the subarrays that need to be sorted in order for the entire array to be sorted in non-decreasing order.
|
Input: 252 2 3 4 532 14 11 151 2 3 4 591 42 35 23 11 15 14 13 12 1 | Output: -1 -1 1 2 1 4 3 4 -1 -1 1 3 1 3 1 5 1 5 2 5 2 5 2 5 2 5 -1 -1
|
Expert
| 3 | 898 | 912 | 264 | 19 |
627 |
A
|
627A
|
A. XOR Equation
| 1,700 |
dp; math
|
Two positive integers a and b have a sum of s and a bitwise XOR of x. How many possible values are there for the ordered pair (a, b)?
|
The first line of the input contains two integers s and x (2 β€ s β€ 1012, 0 β€ x β€ 1012), the sum and bitwise xor of the pair of positive integers, respectively.
|
Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0.
|
In the first sample, we have the following solutions: (2, 7), (3, 6), (6, 3), (7, 2).In the second sample, the only solutions are (1, 2) and (2, 1).
|
Input: 9 5 | Output: 4
|
Medium
| 2 | 133 | 159 | 104 | 6 |
601 |
E
|
601E
|
E. A Museum Robbery
| 2,800 |
data structures; dp
|
There's a famous museum in the city where KleofΓ‘Ε‘ lives. In the museum, n exhibits (numbered 1 through n) had been displayed for a long time; the i-th of those exhibits has value vi and mass wi. Then, the museum was bought by a large financial group and started to vary the exhibits. At about the same time, KleofΓ‘Ε‘... gained interest in the museum, so to say.You should process q events of three types: type 1 β the museum displays an exhibit with value v and mass w; the exhibit displayed in the i-th event of this type is numbered n + i (see sample explanation for more details) type 2 β the museum removes the exhibit with number x and stores it safely in its vault type 3 β KleofΓ‘Ε‘ visits the museum and wonders (for no important reason at all, of course): if there was a robbery and exhibits with total mass at most m were stolen, what would their maximum possible total value be?For each event of type 3, let s(m) be the maximum possible total value of stolen exhibits with total mass β€ m.Formally, let D be the set of numbers of all exhibits that are currently displayed (so initially D = {1, ..., n}). Let P(D) be the set of all subsets of D and let Then, s(m) is defined as Compute s(m) for each . Note that the output follows a special format.
|
The first line of the input contains two space-separated integers n and k (1 β€ n β€ 5000, 1 β€ k β€ 1000) β the initial number of exhibits in the museum and the maximum interesting mass of stolen exhibits. Then, n lines follow. The i-th of them contains two space-separated positive integers vi and wi (1 β€ vi β€ 1 000 000, 1 β€ wi β€ 1000) β the value and mass of the i-th exhibit.The next line contains a single integer q (1 β€ q β€ 30 000) β the number of events.Each of the next q lines contains the description of one event in the following format: 1 v w β an event of type 1, a new exhibit with value v and mass w has been added (1 β€ v β€ 1 000 000, 1 β€ w β€ 1000) 2 x β an event of type 2, the exhibit with number x has been removed; it's guaranteed that the removed exhibit had been displayed at that time 3 β an event of type 3, KleofΓ‘Ε‘ visits the museum and asks his questionThere will be at most 10 000 events of type 1 and at least one event of type 3.
|
As the number of values s(m) can get large, output the answers to events of type 3 in a special format.For each event of type 3, consider the values s(m) computed for the question that KleofΓ‘Ε‘ asked in this event; print one line containing a single number where p = 107 + 19 and q = 109 + 7.Print the answers to events of type 3 in the order in which they appear in the input.
|
In the first sample, the numbers of displayed exhibits and values s(1), ..., s(10) for individual events of type 3 are, in order: The values of individual exhibits are v1 = 30, v2 = 60, v3 = 5, v4 = 42, v5 = 20, v6 = 40 and their masses are w1 = 4, w2 = 6, w3 = 1, w4 = 5, w5 = 3, w6 = 6.In the second sample, the only question is asked after removing all exhibits, so s(m) = 0 for any m.
|
Input: 3 1030 460 65 1931 42 51 20 332 22 431 40 63 | Output: 556674384168191145947033915181541912
|
Master
| 2 | 1,254 | 954 | 376 | 6 |
573 |
A
|
573A
|
A. Bear and Poker
| 1,300 |
implementation; math; number theory
|
Limak is an old brown bear. He often plays poker with his friends. Today they went to a casino. There are n players (including Limak himself) and right now all of them have bids on the table. i-th of them has bid with size ai dollars.Each player can double his bid any number of times and triple his bid any number of times. The casino has a great jackpot for making all bids equal. Is it possible that Limak and his friends will win a jackpot?
|
First line of input contains an integer n (2 β€ n β€ 105), the number of players.The second line contains n integer numbers a1, a2, ..., an (1 β€ ai β€ 109) β the bids of players.
|
Print ""Yes"" (without the quotes) if players can make their bids become equal, or ""No"" otherwise.
|
In the first sample test first and third players should double their bids twice, second player should double his bid once and fourth player should both double and triple his bid.It can be shown that in the second sample test there is no way to make all bids equal.
|
Input: 475 150 75 50 | Output: Yes
|
Easy
| 3 | 444 | 175 | 100 | 5 |
1,991 |
H
|
1991H
|
H. Prime Split Game
| 3,300 |
bitmasks; dp; fft; games; math; number theory
|
Alice and Bob are playing a game with \(n\) piles of stones, where the \(i\)-th pile has \(a_i\) stones. Players take turns making moves, with Alice going first.On each move, the player does the following three-step process: Choose an integer \(k\) (\(1 \leq k \leq \frac n 2\)). Note that the value of \(k\) can be different for different moves. Remove \(k\) piles of stones. Choose another \(k\) piles of stones and split each pile into two piles. The number of stones in each new pile must be a prime number. The player who is unable to make a move loses.Determine who will win if both players play optimally.
|
Each test contains 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 test cases follows.The first line of each test case contains a single integer \(n\) (\(2 \le n \le 2 \cdot 10^5\)) β the number of piles of stones.The second line of each test case contains \(n\) integers \(a_1, a_2, \ldots, a_n\) (\(1 \le a_i \le 2 \cdot 10^5\)) β the number of stones in the piles.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).
|
For each test case, output ""Alice"" (without quotes) if Alice wins and ""Bob"" (without quotes) otherwise.You can output each letter in any case (upper or lower). For example, the strings ""alIcE"", ""Alice"", and ""alice"" will all be considered identical.
|
In the first test case, there are \(2\) piles of stones with \(2\) and \(1\) stones respectively. Since neither \(1\) nor \(2\) can be split into two prime numbers, Alice cannot make a move, so Bob wins.In the second test case, there are \(3\) piles of stones with \(3\), \(5\), and \(7\) stones respectively. Alice can choose \(k = 1\), remove the pile of \(7\) stones, and then split the pile of \(5\) stones into two piles of prime numbers of stones, \(2\) and \(3\). Then, the piles consist of \(3\) piles of stones with \(3\), \(2\), and \(3\) stones respectively, leaving Bob with no valid moves, so Alice wins.In the third test case, there are \(4\) piles of stones with \(4\), \(6\), \(8\), and \(10\) stones respectively. Alice can choose \(k = 2\), removing two piles of \(8\) and \(10\) stones. She splits the pile of \(4\) stones into two piles of prime numbers of stones, \(2\) and \(2\), and the pile of \(6\) stones into two piles of \(3\) and \(3\) stones. Then, Bob has no valid moves, so Alice wins.In the fourth test case, there are \(5\) piles of stones, each containing \(8\) stones. It can be shown that if both players play optimally, Bob will win.
|
Input: 422 133 5 744 6 8 1058 8 8 8 8 | Output: Bob Alice Alice Bob
|
Master
| 6 | 612 | 549 | 258 | 19 |
835 |
A
|
835A
|
A. Key races
| 800 |
math
|
Two boys decided to compete in text typing on the site ""Key races"". During the competition, they have to type a text consisting of s characters. The first participant types one character in v1 milliseconds and has ping t1 milliseconds. The second participant types one character in v2 milliseconds and has ping t2 milliseconds.If connection ping (delay) is t milliseconds, the competition passes for a participant as follows: Exactly after t milliseconds after the start of the competition the participant receives the text to be entered. Right after that he starts to type it. Exactly t milliseconds after he ends typing all the text, the site receives information about it. The winner is the participant whose information on the success comes earlier. If the information comes from both participants at the same time, it is considered that there is a draw.Given the length of the text and the information about participants, determine the result of the game.
|
The first line contains five integers s, v1, v2, t1, t2 (1 β€ s, v1, v2, t1, t2 β€ 1000) β the number of characters in the text, the time of typing one character for the first participant, the time of typing one character for the the second participant, the ping of the first participant and the ping of the second participant.
|
If the first participant wins, print ""First"". If the second participant wins, print ""Second"". In case of a draw print ""Friendship"".
|
In the first example, information on the success of the first participant comes in 7 milliseconds, of the second participant β in 14 milliseconds. So, the first wins.In the second example, information on the success of the first participant comes in 11 milliseconds, of the second participant β in 5 milliseconds. So, the second wins.In the third example, information on the success of the first participant comes in 22 milliseconds, of the second participant β in 22 milliseconds. So, it is be a draw.
|
Input: 5 1 2 1 2 | Output: First
|
Beginner
| 1 | 962 | 325 | 137 | 8 |
1,238 |
F
|
1238F
|
F. The Maximum Subtree
| 2,200 |
dfs and similar; dp; graphs; trees
|
Assume that you have \(k\) one-dimensional segments \(s_1, s_2, \dots s_k\) (each segment is denoted by two integers β its endpoints). Then you can build the following graph on these segments. The graph consists of \(k\) vertexes, and there is an edge between the \(i\)-th and the \(j\)-th vertexes (\(i \neq j\)) if and only if the segments \(s_i\) and \(s_j\) intersect (there exists at least one point that belongs to both of them).For example, if \(s_1 = [1, 6], s_2 = [8, 20], s_3 = [4, 10], s_4 = [2, 13], s_5 = [17, 18]\), then the resulting graph is the following: A tree of size \(m\) is good if it is possible to choose \(m\) one-dimensional segments so that the graph built on these segments coincides with this tree.You are given a tree, you have to find its good subtree with maximum possible size. Recall that a subtree is a connected subgraph of a tree.Note that you have to answer \(q\) independent queries.
|
The first line contains one integer \(q\) (\(1 \le q \le 15 \cdot 10^4\)) β the number of the queries. The first line of each query contains one integer \(n\) (\(2 \le n \le 3 \cdot 10^5\)) β the number of vertices in the tree.Each of the next \(n - 1\) lines contains two integers \(x\) and \(y\) (\(1 \le x, y \le n\)) denoting an edge between vertices \(x\) and \(y\). It is guaranteed that the given graph is a tree.It is guaranteed that the sum of all \(n\) does not exceed \(3 \cdot 10^5\).
|
For each query print one integer β the maximum size of a good subtree of the given tree.
|
In the first query there is a good subtree of size \(8\). The vertices belonging to this subtree are \({9, 4, 10, 2, 5, 1, 6, 3}\).
|
Input: 1 10 1 2 1 3 1 4 2 5 2 6 3 7 3 8 4 9 4 10 | Output: 8
|
Hard
| 4 | 923 | 496 | 88 | 12 |
1,866 |
D
|
1866D
|
D. Digital Wallet
| 2,300 |
dp; greedy
|
There are \(N\) arrays, each array has \(M\) positive integer elements The \(j\)-th element of the \(i\)-th array is \(A_{i,j}\).Initially, Chaneka's digital wallet contains \(0\) money. Given an integer \(K\). Chaneka will do \(M-K+1\) operations. In the \(p\)-th operation, Chaneka does the following procedure: Choose any array. Let's say Chaneka chooses the \(x\)-th array. Choose an index \(y\) in that array such that \(p \leq y \leq p+K-1\). Add the value of \(A_{x, y}\) to the total money in the wallet. Change the value of \(A_{x, y}\) into \(0\). Determine the maximum total money that can be earned!
|
The first line contains three integers \(N\), \(M\), and \(K\) (\(1 \leq N \leq 10\); \(1 \leq M \leq 10^5\); \(1 \leq K \leq \min(10, M)\)) β the number of arrays, the size of each array, and the constant that describes the operation constraints.The \(i\)-th of the next \(N\) lines contains \(M\) integers \(A_{i,1}, A_{i,2}, \ldots, A_{i,M}\) (\(1 \leq A_{i,j} \leq 10^6\)) β the elements of the \(i\)-th array.
|
Output an integer representing the maximum total money that can be earned.
|
In the first example, the following is a sequence of operations of one optimal strategy: Choosing element \(A_{1, 1}\) with a value of \(10\). Choosing element \(A_{3, 2}\) with a value of \(8\). Choosing element \(A_{2, 3}\) with a value of \(9\). So the total money earned is \(10+8+9=27\).In the second example, the following is a sequence of operations of one optimal strategy: Choosing element \(A_{3, 2}\) with a value of \(8\). Choosing element \(A_{1, 2}\) with a value of \(9\). So the total money earned is \(8+9=17\).In the third example, the following is a sequence of operations of one optimal strategy: Choosing element \(A_{1, 3}\) with a value of \(10\). Choosing element \(A_{1, 2}\) with a value of \(9\). So the total money earned is \(10+9=19\).
|
Input: 3 3 1 10 4 2 8 1 9 4 8 2 | Output: 27
|
Expert
| 2 | 611 | 414 | 74 | 18 |
712 |
A
|
712A
|
A. Memory and Crow
| 800 |
implementation; math
|
There are n integers b1, b2, ..., bn written in a row. For all i from 1 to n, values ai are defined by the crows performing the following procedure: The crow sets ai initially 0. The crow then adds bi to ai, subtracts bi + 1, adds the bi + 2 number, and so on until the n'th number. Thus, ai = bi - bi + 1 + bi + 2 - bi + 3.... Memory gives you the values a1, a2, ..., an, and he now wants you to find the initial numbers b1, b2, ..., bn written in the row? Can you do it?
|
The first line of the input contains a single integer n (2 β€ n β€ 100 000) β the number of integers written in the row.The next line contains n, the i'th of which is ai ( - 109 β€ ai β€ 109) β the value of the i'th number.
|
Print n integers corresponding to the sequence b1, b2, ..., bn. It's guaranteed that the answer is unique and fits in 32-bit integer type.
|
In the first sample test, the crows report the numbers 6, - 4, 8, - 2, and 3 when he starts at indices 1, 2, 3, 4 and 5 respectively. It is easy to check that the sequence 2 4 6 1 3 satisfies the reports. For example, 6 = 2 - 4 + 6 - 1 + 3, and - 4 = 4 - 6 + 1 - 3.In the second sample test, the sequence 1, - 3, 4, 11, 6 satisfies the reports. For example, 5 = 11 - 6 and 6 = 6.
|
Input: 56 -4 8 -2 3 | Output: 2 4 6 1 3
|
Beginner
| 2 | 472 | 219 | 138 | 7 |
1,791 |
G2
|
1791G2
|
G2. Teleporters (Hard Version)
| 1,900 |
binary search; greedy; sortings
|
The only difference between the easy and hard versions are the locations you can teleport to.Consider the points \(0,1,\dots,n+1\) on the number line. There is a teleporter located on each of the points \(1,2,\dots,n\). At point \(i\), you can do the following: Move left one unit: it costs \(1\) coin. Move right one unit: it costs \(1\) coin. Use a teleporter at point \(i\), if it exists: it costs \(a_i\) coins. As a result, you can choose whether to teleport to point \(0\) or point \(n+1\). Once you use a teleporter, you can't use it again. You have \(c\) coins, and you start at point \(0\). What's the most number of teleporters you can use?
|
The input consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 1000\)) β the number of test cases. The descriptions of the test cases follow.The first line of each test case contains two integers \(n\) and \(c\) (\(1 \leq n \leq 2\cdot10^5\); \(1 \leq c \leq 10^9\)) β the length of the array and the number of coins you have respectively.The following line contains \(n\) space-separated positive integers \(a_1,a_2,\dots,a_n\) (\(1 \leq a_i \leq 10^9\)) β the costs to use the teleporters.It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2\cdot10^5\).
|
For each test case, output the maximum number of teleporters you can use.
|
In the first test case, you can move one unit to the right, use the teleporter at index \(1\) and teleport to point \(n+1\), move one unit to the left and use the teleporter at index \(5\). You are left with \(6-1-1-1-1 = 2\) coins, and wherever you teleport, you won't have enough coins to use another teleporter. You have used two teleporters, so the answer is two.In the second test case, you go four units to the right and use the teleporter to go to \(n+1\), then go three units left and use the teleporter at index \(6\) to go to \(n+1\), and finally, you go left four times and use the teleporter. The total cost will be \(4+6+3+4+4+9 = 30\), and you used three teleporters.In the third test case, you don't have enough coins to use any teleporter, so the answer is zero.
|
Input: 105 61 1 1 1 18 32100 52 13 6 9 4 100 351 154 54 3 2 15 92 3 1 4 15 82 3 1 4 14 32 3 4 14 95 4 3 32 147 55 600000000500000000 400000000 300000000 200000000 100000000 | Output: 2 3 0 1 3 2 1 1 2 2
|
Hard
| 3 | 650 | 619 | 73 | 17 |
2,074 |
B
|
2074B
|
B. The Third Side
| 800 |
geometry; greedy; math
|
The pink soldiers have given you a sequence \(a\) consisting of \(n\) positive integers.You must repeatedly perform the following operation until there is only \(1\) element left. Choose two distinct indices \(i\) and \(j\). Then, choose a positive integer value \(x\) such that there exists a non-degenerate triangle\(^{\text{β}}\) with side lengths \(a_i\), \(a_j\), and \(x\). Finally, remove two elements \(a_i\) and \(a_j\), and append \(x\) to the end of \(a\). Please find the maximum possible value of the only last element in the sequence \(a\).\(^{\text{β}}\)A triangle with side lengths \(a\), \(b\), \(c\) is non-degenerate when \(a+b > c\), \(a+c > b\), \(b+c > a\).
|
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 2 \cdot 10^5\)).The second line of each test case contains \(n\) integers \(a_1,a_2,\ldots,a_n\) (\(1 \le a_i \le 1000\)) β the elements of the sequence \(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 the maximum possible value of the only last element on a separate line.
|
On the first test case, there is already only one element. The value of the only last element is \(10\).On the second test case, \(a\) is initially \([998,244,353]\). The following series of operations is valid: Erase \(a_2=244\) and \(a_3=353\), and append \(596\) to the end of \(a\). \(a\) is now \([998,596]\). Erase \(a_1=998\) and \(a_2=596\), and append \(1593\) to the end of \(a\). \(a\) is now \([1593]\). It can be shown that the only last element cannot be greater than \(1593\). Therefore, the answer is \(1593\).
|
Input: 41103998 244 35351 2 3 4 599 9 8 2 4 4 3 5 3 | Output: 10 1593 11 39
|
Beginner
| 3 | 679 | 493 | 98 | 20 |
1,990 |
E1
|
1990E1
|
E1. Catch the Mole(Easy Version)
| 2,500 |
binary search; data structures; dfs and similar; interactive; trees
|
This is the easy version of the problem. The only difference is the limit on the number of queries.This is an interactive problem.You are given a tree of \(n\) nodes with node \(1\) as its root node.There is a hidden mole in one of the nodes. To find its position, you can pick an integer \(x\) (\(1 \le x \le n\)) to make an inquiry to the jury. Next, the jury will return \(1\) when the mole is in subtree \(x\). Otherwise, the judge will return \(0\). If the judge returns \(0\) and the mole is not in root node \(1\), the mole will move to the parent node of the node it is currently on.Use at most \(300\) operations to find the current node where the mole is located.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 100\)). The description of the test cases follows.
|
In the first test case, the mole is in node \(2\) initially.For the query ""? 2"", the jury returns \(1\) because the mole is in subtree \(2\). After this query, the mole does not move.The answer \(2\) is the current node where the mole is located, so the answer is considered correct.In the second test case, the mole is in node \(6\) initially.For the query ""? 2"", the jury returns \(0\) because the mole is not in subtree \(2\). After this query, the mole moves from node \(6\) to node \(5\).For the query ""? 6"", the jury returns \(0\) because the mole is not in subtree \(6\). After this query, the mole moves from node \(5\) to node \(4\).For the query ""? 4"", the jury returns \(1\) because the mole is in subtree \(4\). After this query, the mole does not move.The answer \(4\) is the current node where the mole is located, so the answer is considered correct.Please note that the example is only for understanding the statement, and the queries in the example do not guarantee to determine the unique position of the mole.
|
Input: 2 2 1 2 1 6 1 2 1 3 1 4 4 5 5 6 0 0 1 | Output: ? 2 ! 2 ? 2 ? 6 ? 4 ! 4
|
Expert
| 5 | 673 | 160 | 0 | 19 |
|
1,239 |
A
|
1239A
|
A. Ivan the Fool and the Probability Theory
| 1,700 |
combinatorics; dp; math
|
Recently Ivan the Fool decided to become smarter and study the probability theory. He thinks that he understands the subject fairly well, and so he began to behave like he already got PhD in that area.To prove his skills, Ivan decided to demonstrate his friends a concept of random picture. A picture is a field of \(n\) rows and \(m\) columns, where each cell is either black or white. Ivan calls the picture random if for every cell it has at most one adjacent cell of the same color. Two cells are considered adjacent if they share a side.Ivan's brothers spent some time trying to explain that it's not how the randomness usually works. Trying to convince Ivan, they want to count the number of different random (according to Ivan) pictures. Two pictures are considered different if at least one cell on those two picture is colored differently. Since the number of such pictures may be quite large, print it modulo \(10^9 + 7\).
|
The only line contains two integers \(n\) and \(m\) (\(1 \le n, m \le 100\,000\)), the number of rows and the number of columns of the field.
|
Print one integer, the number of random pictures modulo \(10^9 + 7\).
|
The picture below shows all possible random pictures of size \(2\) by \(3\).
|
Input: 2 3 | Output: 8
|
Medium
| 3 | 932 | 141 | 69 | 12 |
1,330 |
A
|
1330A
|
A. Dreamoon and Ranking Collection
| 900 |
implementation
|
Dreamoon is a big fan of the Codeforces contests.One day, he claimed that he will collect all the places from \(1\) to \(54\) after two more rated contests. It's amazing!Based on this, you come up with the following problem:There is a person who participated in \(n\) Codeforces rounds. His place in the first round is \(a_1\), his place in the second round is \(a_2\), ..., his place in the \(n\)-th round is \(a_n\).You are given a positive non-zero integer \(x\).Please, find the largest \(v\) such that this person can collect all the places from \(1\) to \(v\) after \(x\) more rated contests.In other words, you need to find the largest \(v\), such that it is possible, that after \(x\) more rated contests, for each \(1 \leq i \leq v\), there will exist a contest where this person took the \(i\)-th place.For example, if \(n=6\), \(x=2\) and \(a=[3,1,1,5,7,10]\) then answer is \(v=5\), because if on the next two contest he will take places \(2\) and \(4\), then he will collect all places from \(1\) to \(5\), so it is possible to get \(v=5\).
|
The first line contains an integer \(t\) (\(1 \leq t \leq 5\)) denoting the number of test cases in the input.Each test case contains two lines. The first line contains two integers \(n, x\) (\(1 \leq n, x \leq 100\)). The second line contains \(n\) positive non-zero integers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 100\)).
|
For each test case print one line containing the largest \(v\), such that it is possible that after \(x\) other contests, for each \(1 \leq i \leq v\), there will exist a contest where this person took the \(i\)-th place.
|
The first test case is described in the statement.In the second test case, the person has one hundred future contests, so he can take place \(1,2,\ldots,99\) and place \(101\) on them in some order, to collect places \(1,2,\ldots,101\).
|
Input: 5 6 2 3 1 1 5 7 10 1 100 100 11 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 57 80 60 40 20 | Output: 5 101 2 2 60
|
Beginner
| 1 | 1,053 | 329 | 221 | 13 |
383 |
D
|
383D
|
D. Antimatter
| 2,300 |
dp
|
Iahub accidentally discovered a secret lab. He found there n devices ordered in a line, numbered from 1 to n from left to right. Each device i (1 β€ i β€ n) can create either ai units of matter or ai units of antimatter. Iahub wants to choose some contiguous subarray of devices in the lab, specify the production mode for each of them (produce matter or antimatter) and finally take a photo of it. However he will be successful only if the amounts of matter and antimatter produced in the selected subarray will be the same (otherwise there would be overflowing matter or antimatter in the photo). You are requested to compute the number of different ways Iahub can successful take a photo. A photo is different than another if it represents another subarray, or if at least one device of the subarray is set to produce matter in one of the photos and antimatter in the other one.
|
The first line contains an integer n (1 β€ n β€ 1000). The second line contains n integers a1, a2, ..., an (1 β€ ai β€ 1000).The sum a1 + a2 + ... + an will be less than or equal to 10000.
|
Output a single integer, the number of ways Iahub can take a photo, modulo 1000000007 (109 + 7).
|
The possible photos are [1+, 2-], [1-, 2+], [2+, 3-], [2-, 3+], [3+, 4-], [3-, 4+], [1+, 2+, 3-, 4-], [1+, 2-, 3+, 4-], [1+, 2-, 3-, 4+], [1-, 2+, 3+, 4-], [1-, 2+, 3-, 4+] and [1-, 2-, 3+, 4+], where ""i+"" means that the i-th element produces matter, and ""i-"" means that the i-th element produces antimatter.
|
Input: 41 1 1 1 | Output: 12
|
Expert
| 1 | 879 | 184 | 96 | 3 |
1,932 |
G
|
1932G
|
G. Moving Platforms
| 2,300 |
graphs; math; number theory; shortest paths
|
There is a game where you need to move through a labyrinth. The labyrinth consists of \(n\) platforms, connected by \(m\) passages. Each platform is at some level \(l_i\), an integer number from \(0\) to \(H - 1\). In a single step, if you are currently on platform \(i\), you can stay on it, or move to another platform \(j\). To move to platform \(j\) they have to be connected by the passage, and their levels have to be the same, namely \(l_i = l_j\).After each step, the levels of all platforms change. The new level of platform \(i\) is calculated as \(l'_i = (l_i + s_i) \bmod H\), for all \(i\). You start on platform \(1\). Find the minimum number of steps you need to get to platform \(n\).
|
The first line of input contains a single integer \(t\) (\(1 \le t \le 10^4\)) β the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains three integers \(n\), \(m\), and \(H\) (\(2 \le n \le 10^5\), \(1 \le m \le 10^5\), \(1 \le H \le 10^9\)).The second line contains \(n\) integers \(l_i\), the initial level of each platform (\(0 \le l_i \le H-1\)). The third line contains \(n\) integers \(s_i\), the change of level for each platform (\(0 \le s_i \le H-1\)).Next \(m\) lines contain a description of the passages. Each passage is described as a pair of integers β the platforms, connected by the passage. There is at most one passage connecting each pair of platforms, and there is no passage connecting a platform to itself.The sum of \(n\) for all tests does not exceed \(10^5\), the sum of \(m\) for all tests does not exceed \(10^5\).
|
For each test case, print a single integer, the minimum number of steps needed to get from platform \(1\) to platform \(n\).If it is impossible to get to platform \(n\), print \(-1\).
|
This is how levels of the platforms change, and what actions we need to perform in the first example.Platform 1Platform 2Platform 3ActionStep 1194Stay on the platform 1Step 2324Stay on the platform 1Step 3554Move to the platform 2Step 4784Stay on the platform 2Step 5914Stay on the platform 2Step 6144Move to the platform 3
|
Input: 33 3 101 9 42 3 01 23 21 32 1 101 24 61 28 7 2522 14 5 3 10 14 11 19 5 4 10 7 16 18 182 86 33 57 52 61 44 7 | Output: 6 -1 52
|
Expert
| 4 | 700 | 901 | 183 | 19 |
1,798 |
B
|
1798B
|
B. Three Sevens
| 1,000 |
brute force; data structures; greedy; implementation
|
Lottery ""Three Sevens"" was held for \(m\) days. On day \(i\), \(n_i\) people with the numbers \(a_{i, 1}, \ldots, a_{i, n_i}\) participated in the lottery.It is known that in each of the \(m\) days, only one winner was selected from the lottery participants. The lottery winner on day \(i\) was not allowed to participate in the lottery in the days from \(i+1\) to \(m\).Unfortunately, the information about the lottery winners has been lost. You need to find any possible list of lottery winners on days from \(1\) to \(m\) or determine that no solution exists.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 50\,000\)). The description of the test cases follows.The first line of each test case contains a single integer \(m\) (\(1 \le m \le 50\,000\)) β the number of days in which the lottery was held.Next, for each \(i\) from \(1\) to \(m\), follows a two-line block of data.The first line of each block contains a single integer \(n_i\) (\(1 \le n_i \le 50\,000\)) β the number of lottery participants on day \(i\).The second line of the block contains integers \(a_{i, 1}, \ldots, a_{i, n_i}\) (\(1 \le a_{i, j} \le 50\,000\)) β lottery participants on day \(i\). It is guaranteed that all the numbers \(a_{i, 1}, \ldots, a_{i, n_i}\) are pairwise distinct.It is guaranteed that the sum of \(n_i\) over all blocks of all test cases does not exceed \(50\,000\).
|
For each test case, if there is no solution, print a single integer \(-1\).Otherwise, print \(m\) integers \(p_1, p_2, \ldots, p_m\) (\(1 \le p_i \le 50\,000\)) β lottery winners on days from \(1\) to \(m\). If there are multiple solutions, print any of them.
|
In the first test case, one of the answers is \([8, 2, 1]\) since the participant with the number \(8\) participated on day \(1\), but did not participate on days \(2\) and \(3\); the participant with the number \(2\) participated on day \(2\), but did not participate on day \(3\); and the participant with the number \(1\) participated on day \(3\). Note that this is not the only possible answer, for example, \([8, 9, 4]\) is also a correct answer.In the second test case, both lottery participants participated on both days, so any possible lottery winner on the day \(1\) must have participated on the day \(2\), which is not allowed. Thus, there is no correct answer.In the third test case, only one participant participated on days \(2\), \(3\), \(4\), and on day \(1\) there is only one participant who did not participate in the lottery on days \(2, 3, 4\) β participant \(2\), which means \([2, 1, 4, 3]\) is the only correct answer to this test case.
|
Input: 3341 2 4 832 9 121 4221 222 1441 2 3 4111413 | Output: 8 2 1 -1 2 1 4 3
|
Beginner
| 4 | 564 | 868 | 259 | 17 |
2,046 |
F2
|
2046F2
|
F2. Yandex Cuneiform (Hard Version)
| 3,500 |
constructive algorithms; data structures; greedy; implementation
|
This is the hard version of the problem. The difference between the versions is that in this version, there is no restriction on the number of question marks. You can hack only if you solved all versions of this problem. For a long time, no one could decipher Sumerian cuneiform. However, it has finally succumbed to pressure! Today, you have the chance to decipher Yandex cuneiform.Yandex cuneiform is defined by the following rules: An empty string is a Yandex cuneiform. If you insert exactly one copy of each of the three letters 'Y', 'D', and 'X' into a Yandex cuneiform in such a way that no two adjacent letters become equal after the operation, you obtain a Yandex cuneiform. If a string can't be obtained using the above rules, it is not a Yandex cuneiform. You are given a template. A template is a string consisting of the characters 'Y', 'D', 'X', and '?'.You need to check whether there exists a way to replace each question mark with 'Y', 'D', or 'X' to obtain a Yandex cuneiform, and if it exists, output any of the matching options, as well as a sequence of insertion operations to obtain the resulting cuneiform.In this version of the problem, the number of question marks in the template can be arbitrary.
|
Each test contains multiple test cases. The first line contains the number of test cases \(t\) (\(1 \le t \le 5 \cdot 10^4\)). The description of the test cases follows. Each test case consists of a single line containing a template of length \(n\) (\(3 \leq n < 2 \cdot 10^5\), \(n \bmod 3 = 0\)), consisting only of characters 'Y', 'D', 'X', and '?'.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 line containing 'NO' if it is not possible to obtain a cuneiform from the given template.Otherwise, output 'YES' on the first line, and on the second line, any obtainable cuneiform. After that, you need to output the sequence of operations that leads to the cuneiform you printed.A sequence of operations is described by \(\frac{n}{3}\) triples of pairs. A pair has the form c p, where \(c\) is one of the letters 'Y', 'D', or 'X', and \(p\) is the position at which the letter \(c\) should be inserted. The insertion position is the number of letters to skip from the beginning of the string for the insertion. For example, after inserting the character 'D' into the string ""YDX"" with \(p=3\), the result is ""YDXD"", and with \(p=0\), it is ""DYDX"". Note that the index cannot exceed the current length of the string.The operations are applied from top to bottom, left to right. After inserting each triple to the string, there should be no two adjacent identical characters.
|
In the second example, the string is transformed like this: \("""" \to \mathtt{YDX} \to \mathtt{YDXDYX}\).
|
Input: 4???Y??D?X???D??DXYXYX | Output: YES YDX X 0 D 0 Y 0 YES YDXDYX X 0 Y 0 D 1 X 2 D 3 Y 4 YES YDX Y 0 D 1 X 2 NO
|
Master
| 4 | 1,223 | 444 | 1,016 | 20 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.