problem_statement
stringlengths
147
8.53k
input
stringlengths
1
771
output
stringlengths
1
592
time_limit
stringclasses
32 values
memory_limit
stringclasses
21 values
tags
stringlengths
6
168
C. Fighting Tournamenttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBurenka is about to watch the most interesting sporting event of the year — a fighting tournament organized by her friend Tonya.n athletes participate in the tournament, numbered from 1 to n. Burenka determined the strength of the i-th athlete as an integer a_i, where 1 \leq a_i \leq n. All the strength values are different, that is, the array a is a permutation of length n. We know that in a fight, if a_i > a_j, then the i-th participant always wins the j-th.The tournament goes like this: initially, all n athletes line up in ascending order of their ids, and then there are infinitely many fighting rounds. In each round there is exactly one fight: the first two people in line come out and fight. The winner goes back to the front of the line, and the loser goes to the back.Burenka decided to ask Tonya q questions. In each question, Burenka asks how many victories the i-th participant gets in the first k rounds of the competition for some given numbers i and k. Tonya is not very good at analytics, so he asks you to help him answer all the questions.InputThe first line contains one integer t (1 \leq t \leq 10^4) — the number of test cases. Description of the test cases follows.The first line of each test case contains two integers n and q (2 \leq n \leq 10^5, 1 \leq q \leq 10^5) — the number of tournament participants and the number of questions.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \leq a_i \leq n) — the array a, which is a permutation.The next q lines of a test case contain questions. Each line contains two integers i and k (1 \leq i \leq n, 1 \leq k \leq 10^9) — the number of the participant and the number of rounds.It is guaranteed that the sum of n and the sum of q over all test cases do not exceed 10^5.OutputFor each Burenka's question, print a single line containing one integer — the answer to the question.ExampleInput 33 13 1 21 24 21 3 4 24 53 25 21 2 3 5 45 10000000004 6Output 2 0 1 0 4 NoteIn the first test case, the first numbered athlete has the strength of 3, in the first round he will defeat the athlete with the number 2 and the strength of 1, and in the second round, the athlete with the number 3 and the strength of 2.In the second test case, we list the strengths of the athletes fighting in the first 5 fights: 1 and 3, 3 and 4, 4 and 2, 4 and 1, 4 and 3. The participant with the number 4 in the first 5 rounds won 0 times (his strength is 2). The participant with the number 3 has a strength of 4 and won 1 time in the first two fights by fighting 1 time.
33 13 1 21 24 21 3 4 24 53 25 21 2 3 5 45 10000000004 6
2 0 1 0 4
2 seconds
256 megabytes
['binary search', 'data structures', 'implementation', 'two pointers', '*1400']
B. Mathematical Circustime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA new entertainment has appeared in Buryatia — a mathematical circus! The magician shows two numbers to the audience — n and k, where n is even. Next, he takes all the integers from 1 to n, and splits them all into pairs (a, b) (each integer must be in exactly one pair) so that for each pair the integer (a + k) \cdot b is divisible by 4 (note that the order of the numbers in the pair matters), or reports that, unfortunately for viewers, such a split is impossible.Burenka really likes such performances, so she asked her friend Tonya to be a magician, and also gave him the numbers n and k.Tonya is a wolf, and as you know, wolves do not perform in the circus, even in a mathematical one. Therefore, he asks you to help him. Let him know if a suitable splitting into pairs is possible, and if possible, then tell it.InputThe first line contains one integer t (1 \leq t \leq 10^4) — the number of test cases. The following is a description of the input data sets.The single line of each test case contains two integers n and k (2 \leq n \leq 2 \cdot 10^5, 0 \leq k \leq 10^9, n is even) — the number of integers and the number being added k.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, first output the string "YES" if there is a split into pairs, and "NO" if there is none.If there is a split, then in the following \frac{n}{2} lines output pairs of the split, in each line print 2 numbers — first the integer a, then the integer b.ExampleInput 44 12 012 1014 11Output YES 1 2 3 4 NO YES 3 4 7 8 11 12 2 1 6 5 10 9 YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14 NoteIn the first test case, splitting into pairs (1, 2) and (3, 4) is suitable, same as splitting into (1, 4) and (3, 2).In the second test case, (1 + 0) \cdot 2 = 1 \cdot (2 + 0) = 2 is not divisible by 4, so there is no partition.
44 12 012 1014 11
YES 1 2 3 4 NO YES 3 4 7 8 11 12 2 1 6 5 10 9 YES 1 2 3 4 5 6 7 8 9 10 11 12 13 14
1 second
256 megabytes
['constructive algorithms', 'math', '*800']
A. Chip Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputBurenka and Tonya are playing an old Buryat game with a chip on a board of n \times m cells.At the beginning of the game, the chip is located in the lower left corner of the board. In one move, the player can move the chip to the right or up by any odd number of cells (but you cannot move the chip both to the right and up in one move). The one who cannot make a move loses.Burenka makes the first move, the players take turns. Burenka really wants to win the game, but she is too lazy to come up with a strategy, so you are invited to solve the difficult task of finding it. Name the winner of the game (it is believed that Burenka and Tonya are masters of playing with chips, so they always move in the optimal way). Chip's starting cell is green, the only cell from which chip can't move is red. if the chip is in the yellow cell, then blue cells are all options to move the chip in one move. InputThe first line contains one integer t (1 \leq t \leq 10^4) — the number of test cases. The following is a description of the input data sets.The only line of each test case contains two integers n and m (1 \leq n, m \leq 10^9) — the dimensions of the game board.OutputFor each test case print a single line — the name of the winner of the game ("Burenka" or "Tonya").ExampleInput 61 11 45 62 26 3999999999 1000000000Output Tonya Burenka Burenka Tonya Burenka Burenka NoteIn the first case, Burenka has no move, so Tonya wins.In the second case, Burenka can move 3 cells to the right, after which Tony will not be able to make a move, which means that Burenka wins.In the third case, Burenka can move 5 squares to the right. Then we can say that we have a game on a board of 1 \times 5 cells, and Tonya is the first player. In such game the second player wins, so in the original one Burenka will win.
61 11 45 62 26 3999999999 1000000000
Tonya Burenka Burenka Tonya Burenka Burenka
1 second
256 megabytes
['games', 'math', '*800']
F. Burenka, an Array and Queriestime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputEugene got Burenka an array a of length n of integers from 1 to m for her birthday. Burenka knows that Eugene really likes coprime integers (integers x and y such that they have only one common factor (equal to 1)) so she wants to to ask Eugene q questions about the present. Each time Burenka will choose a subsegment a_l, a_{l + 1}, \ldots, a_r of array a, and compute the product of these numbers p = a_l \cdot a_{l + 1} \cdot \ldots \cdot a_r. Then she will ask Eugene to count the number of integers between 1 and C inclusive which are coprime with p. Help Eugene answer all the questions!InputIn the first line of input there are four integers n, m, C, q (1 \leq n, q \leq 10^5, 1 \leq m \leq 2\cdot 10^4, 1 \leq C \leq 10^5) — the length of the array a, the maximum possible value of a_{i}, the value C, and the number of queries.The second line contains n integers a_1, a_2, \ldots, a_n (1 \leq a_{i} \leq m) — the array a .In the next q lines the queries are given. Each query consists of two integers l and r (1 \leq l \leq r \leq n).OutputPrint q integers — the answers to Burenka's queries.ExampleInput 5 5 5 3 1 2 3 2 5 1 1 2 4 4 5 Output 5 2 2 NoteHere's an explanation for the example: in the first query, the product is equal to 1, which is coprime with 1,2,3,4,5. in the second query, the product is equal to 12, which is coprime with 1 and 5. in the third query, the product is equal to 10, which is coprime with 1 and 3.
5 5 5 3 1 2 3 2 5 1 1 2 4 4 5
5 2 2
3 seconds
256 megabytes
['data structures', 'math', 'number theory', '*3300']
E. Impressionismtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputBurenka has two pictures a and b, which are tables of the same size n \times m. Each cell of each painting has a color — a number from 0 to 2 \cdot 10^5, and there are no repeating colors in any row or column of each of the two paintings, except color 0.Burenka wants to get a picture b from the picture a. To achieve her goal, Burenka can perform one of 2 operations: swap any two rows of a or any two of its columns. Tell Burenka if she can fulfill what she wants, and if so, tell her the sequence of actions.The rows are numbered from 1 to n from top to bottom, the columns are numbered from 1 to m from left to right.InputThe first line contains two integers n and m (1 \leq n \cdot m \leq 2 \cdot 10^5) — the sizes of the table.The i-th of the next n lines contains m integers a_{i, 1}, a_{i, 2}, \ldots, a_{i, m} (0 \leq a_{i,j} \leq 2 \cdot 10^5) — the colors of the i-th row of picture a. It is guaranteed that there are no identical colors in the same row or column, except color 0.The i-th of the following n lines contains m integers b_{i, 1}, b_{i, 2}, \ldots, b_{i, m} (0 \leq b_{i,j} \leq 2 \cdot 10^5) — the colors of the i-th row of picture b. It is guaranteed that there are no identical colors in the same row or column, except color 0.OutputIn the first line print the number -1 if it is impossible to achieve what Burenka wants, otherwise print the number of actions in your solution k (0 \le k \le 2 \cdot 10^5). It can be proved that if a solution exists, then there exists a solution where k \le 2 \cdot 10^5.In the next k lines print the operations. First print the type of the operation (1 — swap rows, 2 — columns), and then print the two indices of rows or columns to which the operation is applied.Note that you don't have to minimize the number of operations.ExamplesInput 3 3 1 0 2 0 0 0 2 0 1 2 0 1 0 0 0 1 0 2 Output 1 1 1 3 Input 4 4 0 0 1 2 3 0 0 0 0 1 0 0 1 0 0 0 2 0 0 1 0 3 0 0 0 1 0 0 0 0 1 0 Output 4 1 3 4 2 3 4 2 2 3 2 1 2 Input 3 3 1 2 0 0 0 0 0 0 0 1 0 0 2 0 0 0 0 0 Output -1
3 3 1 0 2 0 0 0 2 0 1 2 0 1 0 0 0 1 0 2
1 1 1 3
2 seconds
256 megabytes
['constructive algorithms', 'graphs', 'implementation', 'math', '*3500']
D. Permutation for Burenkatime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputWe call an array a pure if all elements in it are pairwise distinct. For example, an array [1, 7, 9] is pure, [1, 3, 3, 7] isn't, because 3 occurs twice in it.A pure array b is similar to a pure array c if their lengths n are the same and for all pairs of indices l, r, such that 1 \le l \le r \le n, it's true that \operatorname{argmax}([b_l, b_{l + 1}, \ldots, b_r]) = \operatorname{argmax}([c_l, c_{l + 1}, \ldots, c_r]), where \operatorname{argmax}(x) is defined as the index of the largest element in x (which is unique for pure arrays). For example, \operatorname{argmax}([3, 4, 2]) = 2, \operatorname{argmax}([1337, 179, 57]) = 1.Recently, Tonya found out that Burenka really likes a permutation p of length n. Tonya decided to please her and give her an array a similar to p. He already fixed some elements of a, but exactly k elements are missing (in these positions temporarily a_i = 0). It is guaranteed that k \ge 2. Also, he has a set S of k - 1 numbers.Tonya realized that he was missing one number to fill the empty places of a, so he decided to buy it. He has q options to buy. Tonya thinks that the number d suits him, if it is possible to replace all zeros in a with numbers from S and the number d, so that a becomes a pure array similar to p. For each option of d, output whether this number is suitable for him or not.InputThe first line contains a single integer t (1 \le t \le 10^4) is the number of test cases. The description of the test cases follows.The first line of each test case contains a couple of integers n and q (1 \le n, q \le 3 \cdot 10^5).The second line of each input test case contains n integers p_1, p_2, \ldots, p_n (1 \le p_i \le n) — the permutation Burenka likes.The third line of each test case contains n integers a_1, a_2, \ldots, a_n (0 \le a_i \le 10^6) — elements of Tonya's array, where 0 denotes a missing element. It is guaranteed that there are two indexes i, j (1 \le i, j \le n, i \ne j) such that a_i = 0, a_j = 0, which implies that k \geq 2.The fourth line of each test case contains k - 1 distinct integers s_1, s_2, \ldots, s_{k-1} (1 \le s_i \le 10^6) — elements of Tonya's set S.Each of the next q lines contains a single integer d (1 \le d \le 10^6) — the number that Tonya plans to buy.It is guaranteed that for each given d it's possible to fill in the gaps in a with numbers from S and the number d to get a pure array.It is guaranteed that the sum of n and the sum of q in all tests does not exceed 3 \cdot 10^5.OutputOutput q lines. For each value d, print "YES" if there is a way to fill the array a to make it similar to p, and "NO" otherwise.ExampleInput 44 31 4 3 25 0 7 069145 31 2 5 4 30 5 10 0 03 918115 21 4 3 2 50 0 0 0 07 9 1 561004 24 1 3 20 5 3 0246Output YES NO NO YES YES NO YES YES NO NO NoteIn the first test case for d = 9, you can get a = [5, 9, 7, 6], it can be proved that a is similar to p, for d=1 and d=4 it can be proved that there is no answer.In the second test case for d = 1, you can get a = [1, 5, 10, 9, 3], for d = 8, you can get a = [3, 5, 10, 9, 8], it can be proved that for d = 11 there is no answer.
44 31 4 3 25 0 7 069145 31 2 5 4 30 5 10 0 03 918115 21 4 3 2 50 0 0 0 07 9 1 561004 24 1 3 20 5 3 0246
YES NO NO YES YES NO YES YES NO NO
2 seconds
256 megabytes
['data structures', 'graph matchings', 'greedy', 'math', 'trees', '*3300']
C. Tonya and Burenka-179time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTonya was given an array of a of length n written on a postcard for his birthday. For some reason, the postcard turned out to be a cyclic array, so the index of the element located strictly to the right of the n-th is 1. Tonya wanted to study it better, so he bought a robot "Burenka-179".A program for Burenka is a pair of numbers (s, k), where 1 \leq s \leq n, 1 \leq k \leq n-1. Note that k cannot be equal to n. Initially, Tonya puts the robot in the position of the array s. After that, Burenka makes exactly n steps through the array. If at the beginning of a step Burenka stands in the position i, then the following happens: The number a_{i} is added to the usefulness of the program. "Burenka" moves k positions to the right (i := i + k is executed, if i becomes greater than n, then i := i - n). Help Tonya find the maximum possible usefulness of a program for "Burenka" if the initial usefulness of any program is 0.Also, Tony's friend Ilyusha asks him to change the array q times. Each time he wants to assign a_p := x for a given index p and a value x. You need to find the maximum possible usefulness of the program after each of these changes.InputThe first line contains a single integer t (1 \le t \le 10^4) is the number of test cases. The description of the test cases follows.The first line of each test case contains two integers n and q (2 \le n \le 2 \cdot 10^5, 0 \le q \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 10^9) — elements of the array.The following q lines contain changes, each of them contains two integers p and x (1 \leq p \leq n, 1 \leq x \leq 10^9), meaning you should assign a_p := x.It is guaranteed that the sum of n and the sum of q over all test cases do not exceed 2 \cdot 10^5.OutputFor each test case, output q+1 numbers — the maximum usefulness of a program initially and after each of the changes.ExampleInput 42 11 21 34 44 1 3 22 64 61 13 119 31 7 9 4 5 2 3 6 83 12 19 16 31 1 1 1 1 11 54 43 8Output 3 5 14 16 24 24 24 57 54 36 36 6 18 27 28 NoteIn the first test case, initially and after each request, the answer is achieved at s = 1, k = 1 or s = 2, k = 1.In the second test case, initially, the answer is achieved when s = 1, k = 2 or s = 3, k = 2. After the first request, the answer is achieved at s = 2, k = 2 or s = 4, k = 2.
42 11 21 34 44 1 3 22 64 61 13 119 31 7 9 4 5 2 3 6 83 12 19 16 31 1 1 1 1 11 54 43 8
3 5 14 16 24 24 24 57 54 36 36 6 18 27 28
3 seconds
256 megabytes
['data structures', 'greedy', 'math', 'number theory', '*2400']
B. Fibonacci Stringstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn all schools in Buryatia, in the 1 class, everyone is told the theory of Fibonacci strings."A block is a subsegment of a string where all the letters are the same and are bounded on the left and right by the ends of the string or by letters other than the letters in the block. A string is called a Fibonacci string if, when it is divided into blocks, their lengths in the order they appear in the string form the Fibonacci sequence (f_0 = f_1 = 1, f_i = f_{i-2} + f_{i-1}), starting from the zeroth member of this sequence. A string is called semi-Fibonacci if it possible to reorder its letters to get a Fibonacci string."Burenka decided to enter the Buryat State University, but at the entrance exam she was given a difficult task. She was given a string consisting of the letters of the Buryat alphabet (which contains exactly k letters), and was asked if the given string is semi-Fibonacci. The string can be very long, so instead of the string, she was given the number of appearances of each letter (c_i for the i-th letter) in that string. Unfortunately, Burenka no longer remembers the theory of Fibonacci strings, so without your help she will not pass the exam.InputThe first line contains one integer t (1 \leq t \leq 10^4) — the number of test cases. The following is a description of the input data sets.The first line of each test case contains one integer k (1 \leq k \leq 100) — the number of letters in the alphabet.The second line of each test case contains k integers c_1, c_2, \ldots, c_k (1 \leq c_i \leq 10^9) — the number of occurrences of each letter in the string.OutputFor each test case print the string "YES" if the corresponding string is semi-Fibonacci, and "NO" if it is not.You can print "YES" and "NO" in any case (for example, the strings "yEs", "yes", "Yes" will be recognized as a positive answer).ExampleInput 61121 121 233 1 327 5626 8 3 4 13 34Output YES YES NO YES NO YES NoteIn the first test case, a one-character string is semi-Fibonacci, being itself a Fibonacci string.In the second test case, a string of two different characters is Fibonacci.In the third test case, the string "abb" (let the first of the alphabet letter be a, the second letter b) is not a semi-Fibonacci string, since no permutation of its letters ("abb", "bab", and "bba") is a Fibonacci string.In the fourth test case, two permutations of the letters of the string "abaccac" (the first letter is a, the second letter is b, the third letter is c) are Fibonacci strings — "abaaccc" and "cbccaaa".
61121 121 233 1 327 5626 8 3 4 13 34
YES YES NO YES NO YES
2 seconds
256 megabytes
['greedy', 'implementation', 'math', 'number theory', '*2000']
A2. Burenka and Traditions (hard version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the hard version of this problem. The difference between easy and hard versions is only the constraints on a_i and on n. You can make hacks only if both versions of the problem are solved.Burenka is the crown princess of Buryatia, and soon she will become the n-th queen of the country. There is an ancient tradition in Buryatia — before the coronation, the ruler must show their strength to the inhabitants. To determine the strength of the n-th ruler, the inhabitants of the country give them an array of a of exactly n numbers, after which the ruler must turn all the elements of the array into zeros in the shortest time. The ruler can do the following two-step operation any number of times: select two indices l and r, so that 1 \le l \le r \le n and a non-negative integer x, then for all l \leq i \leq r assign a_i := a_i \oplus x, where \oplus denotes the bitwise XOR operation. It takes \left\lceil \frac{r-l+1}{2} \right\rceil seconds to do this operation, where \lceil y \rceil denotes y rounded up to the nearest integer. Help Burenka calculate how much time she will need.InputThe first line contains a single integer t (1 \le t \le 500)  — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 10^5) - the size of the arrayThe second line of each test case contains n integers a_1, a_2, \cdots , a_n (0 \le a_i < 2^{30}) — elements of the array.It is guaranteed that the sum of n in all tests does not exceed 10^5.OutputFor each test case, output a single number  — the minimum time that Burenka will need.ExampleInput 745 5 5 531 3 220 032 5 761 2 3 3 2 11027 27 34 32 2 31 23 56 52 451822 1799 57 23 55Output 2 2 0 2 4 7 4 NoteIn the first test case, Burenka can choose segment l = 1, r = 4, and x=5. so it will fill the array with zeros in 2 seconds.In the second test case, Burenka first selects segment l = 1, r = 2, and x = 1, after which a = [0, 2, 2], and then the segment l = 2, r = 3, and x=2, which fills the array with zeros. In total, Burenka will spend 2 seconds.
745 5 5 531 3 220 032 5 761 2 3 3 2 11027 27 34 32 2 31 23 56 52 451822 1799 57 23 55
2 2 0 2 4 7 4
1 second
256 megabytes
['data structures', 'dp', 'greedy', '*1900']
A1. Burenka and Traditions (easy version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the easy version of this problem. The difference between easy and hard versions is only the constraints on a_i and on n. You can make hacks only if both versions of the problem are solved.Burenka is the crown princess of Buryatia, and soon she will become the n-th queen of the country. There is an ancient tradition in Buryatia — before the coronation, the ruler must show their strength to the inhabitants. To determine the strength of the n-th ruler, the inhabitants of the country give them an array of a of exactly n numbers, after which the ruler must turn all the elements of the array into zeros in the shortest time. The ruler can do the following two-step operation any number of times: select two indices l and r, so that 1 \le l \le r \le n and a non-negative integer x, then for all l \leq i \leq r assign a_i := a_i \oplus x, where \oplus denotes the bitwise XOR operation. It takes \left\lceil \frac{r-l+1}{2} \right\rceil seconds to do this operation, where \lceil y \rceil denotes y rounded up to the nearest integer. Help Burenka calculate how much time she will need.InputThe first line contains a single integer t (1 \le t \le 500) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 5000) — the size of the array.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (0 \le a_i \le 5000) — elements of the array. It is guaranteed that the sum of n in all tests does not exceed 5000.OutputFor each test case, output a single number  — the minimum time that Burenka will need.ExampleInput 745 5 5 531 3 220 032 5 761 2 3 3 2 11027 27 34 32 2 31 23 56 52 451822 1799 57 23 55Output 2 2 0 2 4 7 4 NoteIn the first test case, Burenka can choose segment l = 1, r = 4, and x=5. so it will fill the array with zeros in 2 seconds.In the second test case, Burenka first selects segment l = 1, r = 2, and x = 1, after which a = [0, 2, 2], and then the segment l = 2, r = 3, and x=2, which fills the array with zeros. In total, Burenka will spend 2 seconds.
745 5 5 531 3 220 032 5 761 2 3 3 2 11027 27 34 32 2 31 23 56 52 451822 1799 57 23 55
2 2 0 2 4 7 4
1 second
256 megabytes
['dp', 'greedy', '*1800']
F. Madoka and The First Sessiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputOh no, on the first exam Madoka got this hard problem:Given integer n and m pairs of integers (v_i, u_i). Also there is an array b_1, b_2, \ldots, b_n, initially filled with zeros.Then for each index i, where 1 \leq i \leq m, perform either b_{v_i} := b_{v_i} - 1 and b_{u_i} := b_{u_i} + 1, or b_{v_i} := b_{v_i} + 1 and b_{u_i} := b_{u_i} - 1. Note that exactly one of these operations should be performed for every i.Also there is an array s of length n consisting of 0 and 1. And there is an array a_1, a_2, \ldots, a_n, where it is guaranteed, that if s_i = 0 holds, then a_i = 0.Help Madoka and determine whenever it is possible to perform operations in such way that for every i, where s_i = 1 it holds that a_i = b_i. If it possible you should also provide Madoka with a way to perform operations.InputThe first line contains two integers n and m (2 \leq n \leq 10000, 1 \leq m \leq 10000) — the length of the array a and the number of pair of integers.The second line contains n integers s_1, s_2, \ldots s_n (0 \le s_i \le 1) — the elements of the array s.The third line contains n integers a_1, a_2, \ldots, a_n (|a_i| \leq m) — the elements of the array a. It is guaranteed that if s_i = 0 holds, then a_i = 0.i-th of the following m lines contains two integers v_i and u_i (1 \leq v_i, u_i \leq n, v_i \ne u_i) — the indexes of the elements of the array b to which the operation is performed. It is also guaranteed that there are no two indices i and j, where 1 \le i < j \le m, such that (v_i, u_i) = (v_j, u_j) or (v_i, u_i) = (u_j, v_j).OutputIn the first line print "YES" if it is possible to perform operations in the required way, and "NO" otherwise.You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).In case you printed "YES", print m pairs of integers. If for pair (v_i, u_i) we should perform b_{v_i} := b_{v_i} - 1 and b_{u_i} := b_{u_i} + 1, print (v_i, u_i). Otherwise print (u_i, v_i). If there are multiple ways to get the correct answer, you can print any of them.You can print pairs in any order.ExamplesInput 5 5 1 1 1 1 1 -2 0 2 1 -1 1 5 1 4 3 5 3 4 4 5 Output YES 1 5 1 4 5 3 4 3 5 4 Input 5 5 0 1 0 1 0 0 1 0 0 0 1 3 2 3 3 5 3 4 4 5 Output YES 3 1 3 2 5 3 3 4 4 5 Input 4 4 1 1 1 1 0 2 -2 2 1 3 1 4 2 3 2 4 Output NO NoteIn the first example, the array b will change as follows: [0,0,0,0,0] \rightarrow [-1,0,0,1,0] \rightarrow [-2,0,0,1,1] \rightarrow [-2,0,1,0,1] \rightarrow [-2,0,2,0,0] \rightarrow [-2,0,2,1,-1]. a_i = b_i for all indices i from 1 to 5.In the second example, it is enough for us that b_2 = 1 at the end, since only s_2 = 1.In the third example, the operations cannot be performed as required.
5 5 1 1 1 1 1 -2 0 2 1 -1 1 5 1 4 3 5 3 4 4 5
YES 1 5 1 4 5 3 4 3 5 4
2 seconds
256 megabytes
['constructive algorithms', 'flows', 'graph matchings', 'graphs', 'implementation', '*2500']
E. Madoka and The Best Universitytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMadoka wants to enter to "Novosibirsk State University", but in the entrance exam she came across a very difficult task:Given an integer n, it is required to calculate \sum{\operatorname{lcm}(c, \gcd(a, b))}, for all triples of positive integers (a, b, c), where a + b + c = n.In this problem \gcd(x, y) denotes the greatest common divisor of x and y, and \operatorname{lcm}(x, y) denotes the least common multiple of x and y.Solve this problem for Madoka and help her to enter to the best university!InputThe first and the only line contains a single integer n (3 \le n \le 10^5).OutputPrint exactly one interger — \sum{\operatorname{lcm}(c, \gcd(a, b))}. Since the answer can be very large, then output it modulo 10^9 + 7.ExamplesInput 3 Output 1 Input 5 Output 11 Input 69228 Output 778304278 NoteIn the first example, there is only one suitable triple (1, 1, 1). So the answer is \operatorname{lcm}(1, \gcd(1, 1)) = \operatorname{lcm}(1, 1) = 1.In the second example, \operatorname{lcm}(1, \gcd(3, 1)) + \operatorname{lcm}(1, \gcd(2, 2)) + \operatorname{lcm}(1, \gcd(1, 3)) + \operatorname{lcm}(2, \gcd(2, 1)) + \operatorname{lcm}(2, \gcd(1, 2)) + \operatorname{lcm}(3, \gcd(1, 1)) = \operatorname{lcm}(1, 1) + \operatorname{lcm}(1, 2) + \operatorname{lcm}(1, 1) + \operatorname{lcm}(2, 1) + \operatorname{lcm}(2, 1) + \operatorname{lcm}(3, 1) = 1 + 2 + 1 + 2 + 2 + 3 = 11
3
1
1 second
256 megabytes
['math', 'number theory', '*2200']
D. Madoka and The Corruption Schemetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMadoka decided to entrust the organization of a major computer game tournament "OSU"!In this tournament, matches are held according to the "Olympic system". In other words, there are 2^n participants in the tournament, numbered with integers from 1 to 2^n. There are n rounds in total in the tournament. In the i-th round there are 2^{n - i} matches between two players (one of whom is right, the other is left), after which the winners go further along the tournament grid, and the losing participants are eliminated from the tournament. Herewith, the relative order in the next round does not change. And the winner of the tournament — is the last remaining participant.But the smaller the participant's number, the more he will pay Madoka if he wins, so Madoka wants the participant with the lowest number to win. To do this, she can arrange the participants in the first round as she likes, and also determine for each match who will win — the participant on the left or right.But Madoka knows that tournament sponsors can change the winner in matches no more than k times. (That is, if the participant on the left won before the change, then the participant on the right will win after the change, and if the participant on the right won, then the participant on the left will win after the change). So, the first image shows the tournament grid that Madoka made, where the red lines denote who should win the match. And the second one shows the tournament grid, after one change in the outcome of the match by sponsors (a match between 1 and 3 players). Print the minimum possible number of the winner in the tournament, which Madoka can get regardless of changes in sponsors. But since the answer can be very large, output it modulo 10^9 + 7. Note that we need to minimize the answer, and only then take it modulo.InputThe first and the only line contains two integers n and k (1 \le n \le 10^5, 1 \le k \le \min(2^n - 1, 10^9)) — the number of rounds in the tournament and the number of outcomes that sponsors can change.OutputPrint exactly one integer — the minimum number of the winner modulo 10^9 + 7ExamplesInput 1 1 Output 2 Input 2 1 Output 3 Input 3 2 Output 7 NoteIn the first example, there is only one match between players 1 and 2, so the sponsors can always make player 2 wins.The tournament grid from the second example is shown in the picture in the statement.
1 1
2
1 second
256 megabytes
['combinatorics', 'constructive algorithms', 'greedy', 'math', '*1900']
C. Madoka and Formal Statementtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array of integer a_1, a_2, \ldots, a_n. In one operation you can make a_i := a_i + 1 if i < n and a_i \leq a_{i + 1}, or i = n and a_i \leq a_1.You need to check whether the array a_1, a_2, \ldots, a_n can become equal to the array b_1, b_2, \ldots, b_n in some number of operations (possibly, zero). Two arrays a and b of length n are called equal if a_i = b_i for all integers i from 1 to n.InputThe input consists of multiple test cases. The first line contains a single integer t (1 \le t \le 4 \cdot 10^4) — the number of test cases. Description of the test cases follows.The first line of each test case contains a single integer n (2 \le n \le 2 \cdot 10^5) – the length of the array.The second line of each test case contains n integers a_1, \ldots, a_n (1 \le a_i \le 10^9) – the elements of the array a.The third line of each test case contains n integers b_1, \ldots, b_n (1 \le b_i \le 10^9) – the elements of the array b.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output "YES" if you can get the array b, otherwise output "NO".You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer).ExampleInput 531 2 51 2 522 21 343 4 1 26 4 2 532 4 14 5 351 2 3 4 56 5 6 7 6Output YES NO NO NO YES NoteIn the first test case, the array a is already equal to the array b.In the second test case, we can't get the array b, because to do this we need to decrease a_1.In the fifth test case, we can apply operations in order to the elements with indices 4, 3, 3,2,2,2,1,1,1,1, and then get the array [5,5,5,5,5]. After that, you can apply operations in order to elements with indices 5,4,4,3,1 and already get an array [6,5,6,7,6].
531 2 51 2 522 21 343 4 1 26 4 2 532 4 14 5 351 2 3 4 56 5 6 7 6
YES NO NO NO YES
1 second
256 megabytes
['greedy', '*1300']
B. Madoka and Underground Competitionstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMadoka decided to participate in an underground sports programming competition. And there was exactly one task in it:A square table of size n \times n, where n is a multiple of k, is called good if only the characters '.' and 'X' are written in it, as well as in any subtable of size 1 \times k or k \times 1, there is at least one character 'X'. In other words, among any k consecutive vertical or horizontal cells, there must be at least one containing the character 'X'.Output any good table that has the minimum possible number of characters 'X', and also the symbol 'X' is written in the cell (r, c). Rows are numbered from 1 to n from top to bottom, columns are numbered from 1 to n from left to right.InputThe input consists of multiple test cases. The first line contains a single integer t (1 \le t \le 100) — the number of test cases. Description of the test cases follows.The first and the only line of each test case contains four integers n, k, r, c (1 \le n \le 500, 1 \le k \le n, 1 \le r, c \le n) — the size of the table, the integer k and the coordinates of the cell, which must contain the character 'X'. It is guaranteed that n is a multiple of k.It is guaranteed that the sum of n over all test cases does not exceed 500.OutputFor each test case, output n lines, each consisting of n characters '.' and 'X', — the desired table. If there are several answers, then you can output anyone.ExampleInput 33 3 3 22 1 1 26 3 4 2Output X.. ..X .X. XX XX .X..X. X..X.. ..X..X .X..X. X..X.. ..X..X NoteLet's analyze the first test case.The following tables can be printed as the correct answer: X....X.X. or ..XX...X. It can be proved that there cannot be less than 3 characters 'X' in the answer.Note that the following table is invalid because cell (3, 2) does not contain the character 'X': X...X...X In the second test case, the only correct table is: XXXX Each subtable of size 1 \times 1 must contain a 'X' character, so all characters in the table must be equal to 'X'.
33 3 3 22 1 1 26 3 4 2
X.. ..X .X. XX XX .X..X. X..X.. ..X..X .X..X. X..X.. ..X..X
1 second
256 megabytes
['constructive algorithms', 'implementation', '*1100']
A. Madoka and Strange Thoughtstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMadoka is a very strange girl, and therefore she suddenly wondered how many pairs of integers (a, b) exist, where 1 \leq a, b \leq n, for which \frac{\operatorname{lcm}(a, b)}{\operatorname{gcd}(a, b)} \leq 3.In this problem, \operatorname{gcd}(a, b) denotes the greatest common divisor of the numbers a and b, and \operatorname{lcm}(a, b) denotes the smallest common multiple of the numbers a and b.InputThe input consists of multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. Description of the test cases follows.The first and the only line of each test case contains the integer n (1 \le n \le 10^8).OutputFor each test case output a single integer — the number of pairs of integers satisfying the condition.ExampleInput 612345100000000Output 1 4 7 10 11 266666666 NoteFor n = 1 there is exactly one pair of numbers — (1, 1) and it fits.For n = 2, there are only 4 pairs — (1, 1), (1, 2), (2, 1), (2, 2) and they all fit.For n = 3, all 9 pair are suitable, except (2, 3) and (3, 2), since their \operatorname{lcm} is 6, and \operatorname{gcd} is 1, which doesn't fit the condition.
612345100000000
1 4 7 10 11 266666666
1 second
256 megabytes
['math', 'number theory', '*800']
F. Bags with Ballstime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere are n bags, each bag contains m balls with numbers from 1 to m. For every i \in [1, m], there is exactly one ball with number i in each bag.You have to take exactly one ball from each bag (all bags are different, so, for example, taking the ball 1 from the first bag and the ball 2 from the second bag is not the same as taking the ball 2 from the first bag and the ball 1 from the second bag). After that, you calculate the number of balls with odd numbers among the ones you have taken. Let the number of these balls be F.Your task is to calculate the sum of F^k over all possible ways to take n balls, one from each bag.InputThe first line contains one integer t (1 \le t \le 5000) — the number of test cases.Each test case consists of one line containing three integers n, m and k (1 \le n, m \le 998244352; 1 \le k \le 2000).OutputFor each test case, print one integer — the sum of F^k over all possible ways to take n balls, one from each bag. Since it can be huge, print it modulo 998244353.ExampleInput 52 3 81 1 11 5 103 7 20001337666 42424242 2000Output 1028 1 3 729229716 652219904
52 3 81 1 11 5 103 7 20001337666 42424242 2000
1028 1 3 729229716 652219904
3 seconds
512 megabytes
['combinatorics', 'dp', 'math', 'number theory', '*2500']
E. Swap and Maximum Blocktime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array of length 2^n. The elements of the array are numbered from 1 to 2^n.You have to process q queries to this array. In the i-th query, you will be given an integer k (0 \le k \le n-1). To process the query, you should do the following: for every i \in [1, 2^n-2^k] in ascending order, do the following: if the i-th element was already swapped with some other element during this query, skip it; otherwise, swap a_i and a_{i+2^k}; after that, print the maximum sum over all contiguous subsegments of the array (including the empty subsegment). For example, if the array a is [-3, 5, -3, 2, 8, -20, 6, -1], and k = 1, the query is processed as follows: the 1-st element wasn't swapped yet, so we swap it with the 3-rd element; the 2-nd element wasn't swapped yet, so we swap it with the 4-th element; the 3-rd element was swapped already; the 4-th element was swapped already; the 5-th element wasn't swapped yet, so we swap it with the 7-th element; the 6-th element wasn't swapped yet, so we swap it with the 8-th element. So, the array becomes [-3, 2, -3, 5, 6, -1, 8, -20]. The subsegment with the maximum sum is [5, 6, -1, 8], and the answer to the query is 18.Note that the queries actually change the array, i. e. after a query is performed, the array does not return to its original state, and the next query will be applied to the modified array.InputThe first line contains one integer n (1 \le n \le 18).The second line contains 2^n integers a_1, a_2, \dots, a_{2^n} (-10^9 \le a_i \le 10^9).The third line contains one integer q (1 \le q \le 2 \cdot 10^5).Then q lines follow, the i-th of them contains one integer k (0 \le k \le n-1) describing the i-th query.OutputFor each query, print one integer — the maximum sum over all contiguous subsegments of the array (including the empty subsegment) after processing the query.ExampleInput 3 -3 5 -3 2 8 -20 6 -1 3 1 0 1 Output 18 8 13 NoteTransformation of the array in the example: [-3, 5, -3, 2, 8, -20, 6, -1] \rightarrow [-3, 2, -3, 5, 6, -1, 8, -20] \rightarrow [2, -3, 5, -3, -1, 6, -20, 8] \rightarrow [5, -3, 2, -3, -20, 8, -1, 6].
3 -3 5 -3 2 8 -20 6 -1 3 1 0 1
18 8 13
4 seconds
512 megabytes
['bitmasks', 'data structures', 'dfs and similar', 'divide and conquer', 'dp', '*2500']
D. Chip Movetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a chip on the coordinate line. Initially, the chip is located at the point 0. You can perform any number of moves; each move increases the coordinate of the chip by some positive integer (which is called the length of the move). The length of the first move you make should be divisible by k, the length of the second move — by k+1, the third — by k+2, and so on.For example, if k=2, then the sequence of moves may look like this: 0 \rightarrow 4 \rightarrow 7 \rightarrow 19 \rightarrow 44, because 4 - 0 = 4 is divisible by 2 = k, 7 - 4 = 3 is divisible by 3 = k + 1, 19 - 7 = 12 is divisible by 4 = k + 2, 44 - 19 = 25 is divisible by 5 = k + 3.You are given two positive integers n and k. Your task is to count the number of ways to reach the point x, starting from 0, for every x \in [1, n]. The number of ways can be very large, so print it modulo 998244353. Two ways are considered different if they differ as sets of visited positions.InputThe first (and only) line of the input contains two integers n and k (1 \le k \le n \le 2 \cdot 10^5).OutputPrint n integers — the number of ways to reach the point x, starting from 0, for every x \in [1, n], taken modulo 998244353.ExamplesInput 8 1 Output 1 1 2 2 3 4 5 6 Input 10 2 Output 0 1 0 1 1 1 1 2 2 2 NoteLet's look at the first example:Ways to reach the point 1: [0, 1];Ways to reach the point 2: [0, 2];Ways to reach the point 3: [0, 1, 3], [0, 3];Ways to reach the point 4: [0, 2, 4], [0, 4];Ways to reach the point 5: [0, 1, 5], [0, 3, 5], [0, 5];Ways to reach the point 6: [0, 1, 3, 6], [0, 2, 6], [0, 4, 6], [0, 6];Ways to reach the point 7: [0, 2, 4, 7], [0, 1, 7], [0, 3, 7], [0, 5, 7], [0, 7];Ways to reach the point 8: [0, 3, 5, 8], [0, 1, 5, 8], [0, 2, 8], [0, 4, 8], [0, 6, 8], [0, 8].
8 1
1 1 2 2 3 4 5 6
2 seconds
256 megabytes
['brute force', 'dp', 'math', '*2000']
C. Robot in a Hallwaytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a grid, consisting of 2 rows and m columns. The rows are numbered from 1 to 2 from top to bottom. The columns are numbered from 1 to m from left to right.The robot starts in a cell (1, 1). In one second, it can perform either of two actions: move into a cell adjacent by a side: up, right, down or left; remain in the same cell. The robot is not allowed to move outside the grid.Initially, all cells, except for the cell (1, 1), are locked. Each cell (i, j) contains a value a_{i,j} — the moment that this cell gets unlocked. The robot can only move into a cell (i, j) if at least a_{i,j} seconds have passed before the move.The robot should visit all cells without entering any cell twice or more (cell (1, 1) is considered entered at the start). It can finish in any cell.What is the fastest the robot can achieve that?InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of testcases.The first line of each testcase contains a single integer m (2 \le m \le 2 \cdot 10^5) — the number of columns of the grid.The i-th of the next 2 lines contains m integers a_{i,1}, a_{i,2}, \dots, a_{i,m} (0 \le a_{i,j} \le 10^9) — the moment of time each cell gets unlocked. a_{1,1} = 0. If a_{i,j} = 0, then cell (i, j) is unlocked from the start.The sum of m over all testcases doesn't exceed 2 \cdot 10^5.OutputFor each testcase, print a single integer — the minimum amount of seconds that the robot can take to visit all cells without entering any cell twice or more.ExampleInput 430 0 14 3 250 4 8 12 162 6 10 14 1840 10 10 1010 10 10 1020 00 0Output 5 19 17 3
430 0 14 3 250 4 8 12 162 6 10 14 1840 10 10 1010 10 10 1020 00 0
5 19 17 3
2 seconds
256 megabytes
['data structures', 'dp', 'greedy', 'implementation', 'ternary search', '*2000']
B. Permutation Chaintime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA permutation of length n is a sequence of integers from 1 to n such that each integer appears in it exactly once.Let the fixedness of a permutation p be the number of fixed points in it — the number of positions j such that p_j = j, where p_j is the j-th element of the permutation p.You are asked to build a sequence of permutations a_1, a_2, \dots, starting from the identity permutation (permutation a_1 = [1, 2, \dots, n]). Let's call it a permutation chain. Thus, a_i is the i-th permutation of length n.For every i from 2 onwards, the permutation a_i should be obtained from the permutation a_{i-1} by swapping any two elements in it (not necessarily neighboring). The fixedness of the permutation a_i should be strictly lower than the fixedness of the permutation a_{i-1}.Consider some chains for n = 3: a_1 = [1, 2, 3], a_2 = [1, 3, 2] — that is a valid chain of length 2. From a_1 to a_2, the elements on positions 2 and 3 get swapped, the fixedness decrease from 3 to 1. a_1 = [2, 1, 3], a_2 = [3, 1, 2] — that is not a valid chain. The first permutation should always be [1, 2, 3] for n = 3. a_1 = [1, 2, 3], a_2 = [1, 3, 2], a_3 = [1, 2, 3] — that is not a valid chain. From a_2 to a_3, the elements on positions 2 and 3 get swapped but the fixedness increase from 1 to 3. a_1 = [1, 2, 3], a_2 = [3, 2, 1], a_3 = [3, 1, 2] — that is a valid chain of length 3. From a_1 to a_2, the elements on positions 1 and 3 get swapped, the fixedness decrease from 3 to 1. From a_2 to a_3, the elements on positions 2 and 3 get swapped, the fixedness decrease from 1 to 0. Find the longest permutation chain. If there are multiple longest answers, print any of them.InputThe first line contains a single integer t (1 \le t \le 99) — the number of testcases.The only line of each testcase contains a single integer n (2 \le n \le 100) — the required length of permutations in the chain.OutputFor each testcase, first, print the length of a permutation chain k.Then print k permutations a_1, a_2, \dots, a_k. a_1 should be an identity permutation of length n ([1, 2, \dots, n]). For each i from 2 to k, a_i should be obtained by swapping two elements in a_{i-1}. It should also have a strictly lower fixedness than a_{i-1}.ExampleInput 223Output 2 1 2 2 1 3 1 2 3 3 2 1 3 1 2
223
2 1 2 2 1 3 1 2 3 3 2 1 3 1 2
2 seconds
256 megabytes
['constructive algorithms', 'math', '*800']
A. 2-3 Movestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are standing at the point 0 on a coordinate line. Your goal is to reach the point n. In one minute, you can move by 2 or by 3 to the left or to the right (i. e., if your current coordinate is x, it can become x-3, x-2, x+2 or x+3). Note that the new coordinate can become negative.Your task is to find the minimum number of minutes required to get from the point 0 to the point n.You have to answer t independent test cases.InputThe first line of the input contains one integer t (1 \le t \le 10^4) — the number of test cases. Then t lines describing the test cases follow.The i-th of these lines contains one integer n (1 \le n \le 10^9) — the goal of the i-th test case.OutputFor each test case, print one integer — the minimum number of minutes required to get from the point 0 to the point n for the corresponding test case.ExampleInput 413412Output 2 1 2 4
413412
2 1 2 4
1 second
256 megabytes
['greedy', 'math', '*800']
F. Crop Squarestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.Farmer Stanley grows corn on a rectangular field of size n \times m meters with corners in points (0, 0), (0, m), (n, 0), (n, m). This year the harvest was plentiful and corn covered the whole field.The night before harvest aliens arrived and poisoned the corn in a single 1 \times 1 square with sides parallel to field borders. The corn inside the square must not be eaten, but you cannot distinguish it from ordinary corn by sight. Stanley can only collect a sample of corn from an arbitrary polygon and bring it to the laboratory, where it will be analyzed and Stanley will be told the amount of corn in the sample that was poisoned. Since the harvest will soon deteriorate, such a study can be carried out no more than 5 times.More formally, it is allowed to make no more than 5 queries, each of them calculates the area of intersection of a chosen polygon with a square of poisoned corn. It is necessary to find out the coordinates of the lower-left corner of the drawn square (the vertex of the square with the smallest x and y coordinates).InputFirst line contains two integers n and m (1 \le n, m \le 100) — field sizes.InteractionIn order to query the area of intersection of a polygon with k (3 \le k \le 1000) vertices at points with coordinates (x_1, y_1),\; \dots ,\;(x_k, y_k) with a square of poisoned corn print k+1 lines. In the first of these lines print "? k". In the i-th of the next k lines print two real numbers x_i and y_i (|x_i|, |y_i| \le 10^4) with at most 15 digits after decimal place.The polygon must have strictly positive area and contain no self-intersections.In response to this query you will receive a real number s (0 \le s \le 1) with 15 digits after decimal place — the area of intersection of the square with the given polygon. If the polygon is invalid, there is no guarantee on the valid response.When you have identified the drawn square, print on a separate line "! x y", where x and y are real numbers with at most 15 digits after decimal place representing the coordinates of its lower-left corner (0 \le x \le n - 1, 0 \le y \le m - 1), and then you have to terminate your program.Your answer will be considered correct if its absolute or relative error on both coordinates does not exceed 10^{-6}. Formally let your answer be a, jury answer be b. Your answer will be considered correct if \frac{|a-b|}{max(1,|b|)} \le 10^{-6}.After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. HacksTo make a hack, use the following test format.The first line of the input should contain two integers n and m (1 \le n,m \le 100) — field sizes.The second line should contain two real numbers x (0 \le x \le n - 1) and y (0 \le y \le m - 1) — coordinates of the lower-left corner of the square of poisoned corn.ExampleInput 3 3 0.5 0.5Output ? 4 0 0 2 0 2 3 0 3 ? 4 0 0 0 1 3 1 3 0 ! 1.5 0.5NoteIn the first test from the statement, the aliens poisoned a square of corn with vertices at points with coordinates (1.5, 0.5), (1.5, 1.5), (2.5, 1.5), (2.5, 0.5). In the picture, it is red, the polygon selected in the query is blue, and their intersection is green.Picture for the first query: Picture for the second query:
3 3 0.5 0.5
? 4 0 0 2 0 2 3 0 3 ? 4 0 0 0 1 3 1 3 0 ! 1.5 0.5
1 second
256 megabytes
['constructive algorithms', 'geometry', 'interactive', 'math', '*2700']
E. Long Way Hometime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputStanley lives in a country that consists of n cities (he lives in city 1). There are bidirectional roads between some of the cities, and you know how long it takes to ride through each of them. Additionally, there is a flight between each pair of cities, the flight between cities u and v takes (u - v)^2 time.Stanley is quite afraid of flying because of watching "Sully: Miracle on the Hudson" recently, so he can take at most k flights. Stanley wants to know the minimum time of a journey to each of the n cities from the city 1.InputIn the first line of input there are three integers n, m, and k (2 \leq n \leq 10^{5}, 1 \leq m \leq 10^{5}, 1 \leq k \leq 20) — the number of cities, the number of roads, and the maximal number of flights Stanley can take.The following m lines describe the roads. Each contains three integers u, v, w (1 \leq u, v \leq n, u \neq v, 1 \leq w \leq 10^{9}) — the cities the road connects and the time it takes to ride through. Note that some pairs of cities may be connected by more than one road.OutputPrint n integers, i-th of which is equal to the minimum time of traveling to city i.ExamplesInput 3 1 2 1 3 1 Output 0 1 1 Input 4 3 1 1 2 3 2 4 5 3 4 7 Output 0 1 4 6 Input 2 1 1 2 1 893746473 Output 0 1 Input 5 5 2 2 1 33 1 5 93 5 3 48 2 3 21 4 2 1 Output 0 1 2 2 3 NoteIn the first sample, it takes no time to get to city 1; to get to city 2 it is possible to use a flight between 1 and 2, which will take 1 unit of time; to city 3 you can get via a road from city 1, which will take 1 unit of time. In the second sample, it also takes no time to get to city 1. To get to city 2 Stanley should use a flight between 1 and 2, which will take 1 unit of time. To get to city 3 Stanley can ride between cities 1 and 2, which will take 3 units of time, and then use a flight between 2 and 3. To get to city 4 Stanley should use a flight between 1 and 2, then take a ride from 2 to 4, which will take 5 units of time.
3 1 2 1 3 1
0 1 1
3 seconds
256 megabytes
['data structures', 'divide and conquer', 'dp', 'geometry', 'graphs', 'greedy', 'shortest paths', '*2400']
D. 2+ doorstime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe Narrator has an integer array a of length n, but he will only tell you the size n and q statements, each of them being three integers i, j, x, which means that a_i \mid a_j = x, where | denotes the bitwise OR operation.Find the lexicographically smallest array a that satisfies all the statements.An array a is lexicographically smaller than an array b of the same length if and only if the following holds: in the first position where a and b differ, the array a has a smaller element than the corresponding element in b. InputIn the first line you are given with two integers n and q (1 \le n \le 10^5, 0 \le q \le 2 \cdot 10^5).In the next q lines you are given with three integers i, j, and x (1 \le i, j \le n, 0 \le x < 2^{30}) — the statements.It is guaranteed that all q statements hold for at least one array.OutputOn a single line print n integers a_1, a_2, \ldots, a_n (0 \le a_i < 2^{30}) — array a.ExamplesInput 4 3 1 2 3 1 3 2 4 1 2 Output 0 3 2 2 Input 1 0 Output 0 Input 2 1 1 1 1073741823 Output 1073741823 0 NoteIn the first sample, these are all the arrays satisfying the statements: [0, 3, 2, 2], [2, 1, 0, 0], [2, 1, 0, 2], [2, 1, 2, 0], [2, 1, 2, 2], [2, 3, 0, 0], [2, 3, 0, 2], [2, 3, 2, 0], [2, 3, 2, 2].
4 3 1 2 3 1 3 2 4 1 2
0 3 2 2
1.5 seconds
256 megabytes
['2-sat', 'bitmasks', 'graphs', 'greedy', '*1900']
C. Monoblocktime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputStanley has decided to buy a new desktop PC made by the company "Monoblock", and to solve captcha on their website, he needs to solve the following task.The awesomeness of an array is the minimum number of blocks of consecutive identical numbers in which the array could be split. For example, the awesomeness of an array [1, 1, 1] is 1; [5, 7] is 2, as it could be split into blocks [5] and [7]; [1, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 9, 9, 9] is 3, as it could be split into blocks [1], [7, 7, 7, 7, 7, 7, 7], and [9, 9, 9, 9, 9, 9, 9, 9, 9]. You are given an array a of length n. There are m queries of two integers i, x. A query i, x means that from now on the i-th element of the array a is equal to x.After each query print the sum of awesomeness values among all subsegments of array a. In other words, after each query you need to calculate \sum\limits_{l = 1}^n \sum\limits_{r = l}^n g(l, r), where g(l, r) is the awesomeness of the array b = [a_l, a_{l + 1}, \ldots, a_r].InputIn the first line you are given with two integers n and m (1 \leq n, m \leq 10^5).The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^9) — the array a.In the next m lines you are given the descriptions of queries. Each line contains two integers i and x (1 \leq i \leq n, 1 \leq x \leq 10^9).OutputPrint the answer to each query on a new line.ExampleInput 5 5 1 2 3 4 5 3 2 4 2 3 1 2 1 2 2 Output 29 23 35 25 35 NoteAfter the first query a is equal to [1, 2, 2, 4, 5], and the answer is 29 because we can split each of the subsegments the following way: [1; 1]: [1], 1 block; [1; 2]: [1] + [2], 2 blocks; [1; 3]: [1] + [2, 2], 2 blocks; [1; 4]: [1] + [2, 2] + [4], 3 blocks; [1; 5]: [1] + [2, 2] + [4] + [5], 4 blocks; [2; 2]: [2], 1 block; [2; 3]: [2, 2], 1 block; [2; 4]: [2, 2] + [4], 2 blocks; [2; 5]: [2, 2] + [4] + [5], 3 blocks; [3; 3]: [2], 1 block; [3; 4]: [2] + [4], 2 blocks; [3; 5]: [2] + [4] + [5], 3 blocks; [4; 4]: [4], 1 block; [4; 5]: [4] + [5], 2 blocks; [5; 5]: [5], 1 block; which is 1 + 2 + 2 + 3 + 4 + 1 + 1 + 2 + 3 + 1 + 2 + 3 + 1 + 2 + 1 = 29 in total.
5 5 1 2 3 4 5 3 2 4 2 3 1 2 1 2 2
29 23 35 25 35
1 second
256 megabytes
['combinatorics', 'data structures', 'implementation', 'math', '*1700']
B. Beautiful Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputStanley defines the beauty of an array a of length n, which contains non-negative integers, as follows: \sum\limits_{i = 1}^{n} \left \lfloor \frac{a_{i}}{k} \right \rfloor, which means that we divide each element by k, round it down, and sum up the resulting values.Stanley told Sam the integer k and asked him to find an array a of n non-negative integers, such that the beauty is equal to b and the sum of elements is equal to s. Help Sam — find any of the arrays satisfying the conditions above.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 1000). Description of the test cases follows.The first line of each test case contains integers n, k, b, s (1 \leq n \leq 10^{5}, 1 \leq k \leq 10^{9}, 0 \leq b \leq 10^{9}, 0 \leq s \leq 10^{18}).It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case print -1 if such array a does not exist. Otherwise print n non-negative integers a_1, a_2, \ldots, a_n (0 \leq a_{i} \leq 10^{18}) — the answer.ExampleInput 81 6 3 1003 6 3 123 6 3 195 4 7 385 4 7 8099978 1000000000 100000000 10000000000000000001 1 0 04 1000000000 1000000000 1000000000000000000Output -1 -1 0 0 19 0 3 3 3 29 -1 -1 0 0 0 0 1000000000000000000 NoteIn the first, the second, the fifth and the sixth test cases of the example it is possible to show that such array does not exist.In the third testcase of the example a = [0, 0, 19]. The sum of elements in it is equal to 19, the beauty of it is equal to \left ( \left \lfloor \frac{0}{6} \right \rfloor + \left \lfloor \frac{0}{6} \right \rfloor + \left \lfloor \frac{19}{6} \right \rfloor \right ) = (0 + 0 + 3) = 3.In the fourth testcase of the example a = [0, 3, 3, 3, 29]. The sum of elements in it is equal to 38, the beauty of it is equal to (0 + 0 + 0 + 0 + 7) = 7.
81 6 3 1003 6 3 123 6 3 195 4 7 385 4 7 8099978 1000000000 100000000 10000000000000000001 1 0 04 1000000000 1000000000 1000000000000000000
-1 -1 0 0 19 0 3 3 3 29 -1 -1 0 0 0 0 1000000000000000000
1 second
256 megabytes
['constructive algorithms', 'greedy', 'math', '*1000']
A. Crossmarkettime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputStanley and Megan decided to shop in the "Crossmarket" grocery store, which can be represented as a matrix with n rows and m columns. Stanley and Megan can move to an adjacent cell using 1 unit of power. Two cells are considered adjacent if they share an edge. To speed up the shopping process, Megan brought her portals with her, and she leaves one in each cell she visits (if there is no portal yet). If a person (Stanley or Megan) is in a cell with a portal, that person can use 1 unit of power to teleport to any other cell with a portal, including Megan's starting cell.They decided to split up: Stanley will go from the upper-left cell (cell with coordinates (1, 1)) to the lower-right cell (cell with coordinates (n, m)), whilst Megan needs to get from the lower-left cell (cell with coordinates (n, 1)) to the upper-right cell (cell with coordinates (1, m)).What is the minimum total energy needed for them both to do that?Note that they can choose the time they move. Time does not affect energy.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 1000). Description of the test cases follows.The only line in the test case contains two integers n and m (1 \le n, m \le 10^5).OutputFor each test case print a single integer on a new line – the answer.ExampleInput 77 55 71 1100000 10000057 2281 55 1Output 15 15 0 299998 340 5 5 Note In the first test case they can stick to the following plan: Megan (red circle) moves to the cell (7, 3). Then she goes to the cell (1, 3), and Stanley (blue circle) does the same. Stanley uses the portal in that cell (cells with portals are grey) to get to the cell (7, 3). Then he moves to his destination — cell (7, 5). Megan also finishes her route and goes to the cell (1, 5). The total energy spent is (2 + 6) + (2 + 1 + 2) + (2)= 15, which is our final answer.
77 55 71 1100000 10000057 2281 55 1
15 15 0 299998 340 5 5
1 second
256 megabytes
['constructive algorithms', 'greedy', 'math', '*800']
G. Path Prefixestime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a rooted tree. It contains n vertices, which are numbered from 1 to n. The root is the vertex 1.Each edge has two positive integer values. Thus, two positive integers a_j and b_j are given for each edge.Output n-1 numbers r_2, r_3, \dots, r_n, where r_i is defined as follows.Consider the path from the root (vertex 1) to i (2 \le i \le n). Let the sum of the costs of a_j along this path be A_i. Then r_i is equal to the length of the maximum prefix of this path such that the sum of b_j along this prefix does not exceed A_i. Example for n=9. The blue color shows the costs of a_j, and the red color shows the costs of b_j. Consider an example. In this case: r_2=0, since the path to 2 has an amount of a_j equal to 5, only the prefix of this path of length 0 has a smaller or equal amount of b_j; r_3=3, since the path to 3 has an amount of a_j equal to 5+9+5=19, the prefix of length 3 of this path has a sum of b_j equal to 6+10+1=17 ( the number is 17 \le 19); r_4=1, since the path to 4 has an amount of a_j equal to 5+9=14, the prefix of length 1 of this path has an amount of b_j equal to 6 (this is the longest suitable prefix, since the prefix of length 2 already has an amount of b_j equal to 6+10=16, which is more than 14); r_5=2, since the path to 5 has an amount of a_j equal to 5+9+2=16, the prefix of length 2 of this path has a sum of b_j equal to 6+10=16 (this is the longest suitable prefix, since the prefix of length 3 already has an amount of b_j equal to 6+10+1=17, what is more than 16); r_6=1, since the path up to 6 has an amount of a_j equal to 2, the prefix of length 1 of this path has an amount of b_j equal to 1; r_7=1, since the path to 7 has an amount of a_j equal to 5+3=8, the prefix of length 1 of this path has an amount of b_j equal to 6 (this is the longest suitable prefix, since the prefix of length 2 already has an amount of b_j equal to 6+3=9, which is more than 8); r_8=2, since the path up to 8 has an amount of a_j equal to 2+4=6, the prefix of length 2 of this path has an amount of b_j equal to 1+3=4; r_9=3, since the path to 9 has an amount of a_j equal to 2+4+1=7, the prefix of length 3 of this path has a sum of b_j equal to 1+3+3=7.InputThe first line contains an integer t (1 \le t \le 10^4) — the number of test cases in the test.The descriptions of test cases follow.Each description begins with a line that contains an integer n (2 \le n \le 2\cdot10^5) — the number of vertices in the tree.This is followed by n-1 string, each of which contains three numbers p_j, a_j, b_j (1 \le p_j \le n; 1 \le a_j,b_j \le 10^9) — the ancestor of the vertex j, the first and second values an edge that leads from p_j to j. The value of j runs through all values from 2 to n inclusive. It is guaranteed that each set of input data has a correct hanged tree with a root at the vertex 1.It is guaranteed that the sum of n over all input test cases does not exceed 2\cdot10^5.OutputFor each test case, output n-1 integer in one line: r_2, r_3, \dots, r_n.ExampleInput 491 5 64 5 12 9 104 2 11 2 12 3 36 4 38 1 341 1 1002 1 13 101 141 100 12 1 13 1 101101 1 42 3 52 5 13 4 33 1 55 3 55 2 11 3 26 2 1Output 0 3 1 2 1 1 2 3 0 0 3 1 2 2 0 1 2 1 1 2 2 1 1 NoteThe first example is clarified in the statement.In the second example: r_2=0, since the path to 2 has an amount of a_j equal to 1, only the prefix of this path of length 0 has a smaller or equal amount of b_j; r_3=0, since the path to 3 has an amount of a_j equal to 1+1=2, the prefix of length 1 of this path has an amount of b_j equal to 100 (100 > 2); r_4=3, since the path to 4 has an amount of a_j equal to 1+1+101=103, the prefix of length 3 of this path has an amount of b_j equal to 102, .
491 5 64 5 12 9 104 2 11 2 12 3 36 4 38 1 341 1 1002 1 13 101 141 100 12 1 13 1 101101 1 42 3 52 5 13 4 33 1 55 3 55 2 11 3 26 2 1
0 3 1 2 1 1 2 3 0 0 3 1 2 2 0 1 2 1 1 2 2 1 1
3 seconds
256 megabytes
['binary search', 'data structures', 'dfs and similar', 'trees', '*1700']
F. Build a Tree and That Is Ittime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA tree is a connected undirected graph without cycles. Note that in this problem, we are talking about not rooted trees.You are given four positive integers n, d_{12}, d_{23} and d_{31}. Construct a tree such that: it contains n vertices numbered from 1 to n, the distance (length of the shortest path) from vertex 1 to vertex 2 is d_{12}, distance from vertex 2 to vertex 3 is d_{23}, the distance from vertex 3 to vertex 1 is d_{31}. Output any tree that satisfies all the requirements above, or determine that no such tree exists.InputThe first line of the input contains an integer t (1 \le t \le 10^4) —the number of test cases in the test.This is followed by t test cases, each written on a separate line.Each test case consists of four positive integers n, d_{12}, d_{23} and d_{31} (3 \le n \le 2\cdot10^5; 1 \le d_{12}, d_{23}, d_{31} \le n-1).It is guaranteed that the sum of n values for all test cases does not exceed 2\cdot10^5.OutputFor each test case, print YES if the suitable tree exists, and NO otherwise. If the answer is positive, print another n-1 line each containing a description of an edge of the tree — a pair of positive integers x_i, y_i, which means that the ith edge connects vertices x_i and y_i. The edges and vertices of the edges can be printed in any order. If there are several suitable trees, output any of them.ExampleInput 95 1 2 15 2 2 25 2 2 35 2 2 45 3 2 34 2 1 14 3 1 14 1 2 37 1 4 1Output YES 1 2 4 1 3 1 2 5 YES 4 3 2 5 1 5 5 3 NO YES 2 4 4 1 2 5 5 3 YES 5 4 4 1 2 5 3 5 YES 2 3 3 4 1 3 NO YES 4 3 1 2 2 4 NO
95 1 2 15 2 2 25 2 2 35 2 2 45 3 2 34 2 1 14 3 1 14 1 2 37 1 4 1
YES 1 2 4 1 3 1 2 5 YES 4 3 2 5 1 5 5 3 NO YES 2 4 4 1 2 5 5 3 YES 5 4 4 1 2 5 3 5 YES 2 3 3 4 1 3 NO YES 4 3 1 2 2 4 NO
2 seconds
256 megabytes
['constructive algorithms', 'implementation', 'trees', '*1900']
E. Add Modulo 10time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array of n integers a_1, a_2, \dots, a_nYou can apply the following operation an arbitrary number of times: select an index i (1 \le i \le n) and replace the value of the element a_i with the value a_i + (a_i \bmod 10), where a_i \bmod 10 is the remainder of the integer dividing a_i by 10. For a single index (value i), this operation can be applied multiple times. If the operation is applied repeatedly to the same index, then the current value of a_i is taken into account each time. For example, if a_i=47 then after the first operation we get a_i=47+7=54, and after the second operation we get a_i=54+4=58.Check if it is possible to make all array elements equal by applying multiple (possibly zero) operations.For example, you have an array [6, 11]. Let's apply this operation to the first element of the array. Let's replace a_1 = 6 with a_1 + (a_1 \bmod 10) = 6 + (6 \bmod 10) = 6 + 6 = 12. We get the array [12, 11]. Then apply this operation to the second element of the array. Let's replace a_2 = 11 with a_2 + (a_2 \bmod 10) = 11 + (11 \bmod 10) = 11 + 1 = 12. We get the array [12, 12]. Thus, by applying 2 operations, you can make all elements of an array equal.InputThe first line contains one integer t (1 \le t \le 10^4) — the number of test cases. What follows is a description of each test case.The first line of each test case contains one integer n (1 \le n \le 2 \cdot 10^5) — the size of the array.The second line of each test case contains n integers a_i (0 \le a_i \le 10^9) — array elements.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case print: YES if it is possible to make all array elements equal; NO otherwise. You can print YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive answer) .ExampleInput 1026 1132 18 2255 10 5 10 541 2 4 824 5393 96 102240 6250 30222 4421 5Output Yes No Yes Yes No Yes No No Yes No NoteThe first test case is clarified above.In the second test case, it is impossible to make all array elements equal.In the third test case, you need to apply this operation once to all elements equal to 5.In the fourth test case, you need to apply this operation to all elements until they become equal to 8.In the fifth test case, it is impossible to make all array elements equal.In the sixth test case, you need to apply this operation to all elements until they become equal to 102.
1026 1132 18 2255 10 5 10 541 2 4 824 5393 96 102240 6250 30222 4421 5
Yes No Yes Yes No Yes No No Yes No
2 seconds
256 megabytes
['brute force', 'math', 'number theory', '*1400']
D. Color with Occurrencestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given some text t and a set of n strings s_1, s_2, \dots, s_n. In one step, you can choose any occurrence of any string s_i in the text t and color the corresponding characters of the text in red. For example, if t=\texttt{bababa} and s_1=\texttt{ba}, s_2=\texttt{aba}, you can get t=\color{red}{\texttt{ba}}\texttt{baba}, t=\texttt{b}\color{red}{\texttt{aba}}\texttt{ba} or t=\texttt{bab}\color{red}{\texttt{aba}} in one step.You want to color all the letters of the text t in red. When you color a letter in red again, it stays red.In the example above, three steps are enough: Let's color t[2 \dots 4]=s_2=\texttt{aba} in red, we get t=\texttt{b}\color{red}{\texttt{aba}}\texttt{ba}; Let's color t[1 \dots 2]=s_1=\texttt{ba} in red, we get t=\color{red}{\texttt{baba}}\texttt{ba}; Let's color t[4 \dots 6]=s_2=\texttt{aba} in red, we get t=\color{red}{\texttt{bababa}}. Each string s_i can be applied any number of times (or not at all). Occurrences for coloring can intersect arbitrarily.Determine the minimum number of steps needed to color all letters t in red and how to do it. If it is impossible to color all letters of the text t in red, output -1.InputThe first line of the input contains an integer q (1 \le q \le 100) —the number of test cases in the test.The descriptions of the test cases follow.The first line of each test case contains the text t (1 \le |t| \le 100), consisting only of lowercase Latin letters, where |t| is the length of the text t.The second line of each test case contains a single integer n (1 \le n \le 10) — the number of strings in the set.This is followed by n lines, each containing a string s_i (1 \le |s_i| \le 10) consisting only of lowercase Latin letters, where |s_i| — the length of string s_i.OutputFor each test case, print the answer on a separate line.If it is impossible to color all the letters of the text in red, print a single line containing the number -1.Otherwise, on the first line, print the number m — the minimum number of steps it will take to turn all the letters t red.Then in the next m lines print pairs of indices: w_j and p_j (1 \le j \le m), which denote that the string with index w_j was used as a substring to cover the occurrences starting in the text t from position p_j. The pairs can be output in any order.If there are several answers, output any of them.ExampleInput 6bababa2baabacaba2bacacababacabaca3ababacacabaca3acbcodeforces4defcodeefoforcesaaaabbbbcccceeee4eeeeccccaaaabbbbOutput 3 2 2 1 1 2 4 -1 4 1 1 2 6 3 3 3 7 4 3 1 1 2 2 3 1 4 2 4 5 2 1 4 3 1 4 5 2 9 1 13NoteThe first test case is explained in the problem statement.In the second test case, it is impossible to color all the letters of the text in red.
6bababa2baabacaba2bacacababacabaca3ababacacabaca3acbcodeforces4defcodeefoforcesaaaabbbbcccceeee4eeeeccccaaaabbbb
3 2 2 1 1 2 4 -1 4 1 1 2 6 3 3 3 7 4 3 1 1 2 2 3 1 4 2 4 5 2 1 4 3 1 4 5 2 9 1 13
2 seconds
256 megabytes
['brute force', 'data structures', 'dp', 'greedy', 'strings', '*1600']
C. Minimum Varied Numbertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputFind the minimum number with the given sum of digits s such that all digits in it are distinct (i.e. all digits are unique).For example, if s=20, then the answer is 389. This is the minimum number in which all digits are different and the sum of the digits is 20 (3+8+9=20).For the given s print the required number.InputThe first line contains an integer t (1 \le t \le 45) — the number of test cases.Each test case is specified by a line that contains the only integer s (1 \le s \le 45).OutputPrint t integers — the answers to the given test cases.ExampleInput 42084510Output 389 8 123456789 19
42084510
389 8 123456789 19
1 second
256 megabytes
['greedy', '*800']
B. Remove Prefixtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp was presented with some sequence of integers a of length n (1 \le a_i \le n). A sequence can make Polycarp happy only if it consists of different numbers (i.e. distinct numbers).In order to make his sequence like this, Polycarp is going to make some (possibly zero) number of moves.In one move, he can: remove the first (leftmost) element of the sequence. For example, in one move, the sequence [3, 1, 4, 3] will produce the sequence [1, 4, 3], which consists of different numbers.Determine the minimum number of moves he needs to make so that in the remaining sequence all elements are different. In other words, find the length of the smallest prefix of the given sequence a, after removing which all values in the sequence will be unique.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases.Each test case consists of two lines.The first line contains an integer n (1 \le n \le 2 \cdot 10^5) — the length of the given sequence a.The second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le n) — elements of the given sequence a.It is guaranteed that the sum of n values over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case print your answer on a separate line — the minimum number of elements that must be removed from the beginning of the sequence so that all remaining elements are different.ExampleInput 543 1 4 351 1 1 1 11166 5 4 3 2 171 2 1 7 1 2 1Output 1 4 0 0 5 NoteThe following are the sequences that will remain after the removal of prefixes: [1, 4, 3]; [1]; [1]; [6, 5, 4, 3, 2, 1]; [2, 1]. It is easy to see that all the remaining sequences contain only distinct elements. In each test case, the shortest matching prefix was removed.
543 1 4 351 1 1 1 11166 5 4 3 2 171 2 1 7 1 2 1
1 4 0 0 5
2 seconds
256 megabytes
['data structures', 'greedy', 'implementation', '*800']
A. Everyone Loves to Sleeptime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVlad, like everyone else, loves to sleep very much.Every day Vlad has to do n things, each at a certain time. For each of these things, he has an alarm clock set, the i-th of them is triggered on h_i hours m_i minutes every day (0 \le h_i < 24, 0 \le m_i < 60). Vlad uses the 24-hour time format, so after h=12, m=59 comes h=13, m=0 and after h=23, m=59 comes h=0, m=0.This time Vlad went to bed at H hours M minutes (0 \le H < 24, 0 \le M < 60) and asks you to answer: how much he will be able to sleep until the next alarm clock.If any alarm clock rings at the time when he went to bed, then he will sleep for a period of time of length 0.InputThe first line of input data contains an integer t (1 \le t \le 100) — the number of test cases in the test.The first line of the case contains three integers n, H and M (1 \le n \le 10, 0 \le H < 24, 0 \le M < 60) — the number of alarms and the time Vlad went to bed.The following n lines contain two numbers each h_i and m_i (0 \le h_i < 24, 0 \le m_i < 60) — the time of the i alarm. It is acceptable that two or more alarms will trigger at the same time.Numbers describing time do not contain leading zeros.OutputOutput t lines, each containing the answer to the corresponding test case. As an answer, output two numbers  — the number of hours and minutes that Vlad will sleep, respectively. If any alarm clock rings at the time when he went to bed, the answer will be 0 0.ExampleInput 31 6 138 03 6 012 3014 456 02 23 3520 1510 30Output 1 47 0 0 10 55
31 6 138 03 6 012 3014 456 02 23 3520 1510 30
1 47 0 0 10 55
2 seconds
256 megabytes
['implementation', 'math', '*900']
F. Lost Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMy orzlers, we can optimize this problem from O(S^3) to O\left(T^\frac{5}{9}\right)!— Spyofgame, founder of Orzlim religionA long time ago, Spyofgame invented the famous array a (1-indexed) of length n that contains information about the world and life. After that, he decided to convert it into the matrix b (0-indexed) of size (n + 1) \times (n + 1) which contains information about the world, life and beyond.Spyofgame converted a into b with the following rules. b_{i,0} = 0 if 0 \leq i \leq n; b_{0,i} = a_{i} if 1 \leq i \leq n; b_{i,j} = b_{i,j-1} \oplus b_{i-1,j} if 1 \leq i, j \leq n. Here \oplus denotes the bitwise XOR operation.Today, archaeologists have discovered the famous matrix b. However, many elements of the matrix has been lost. They only know the values of b_{i,n} for 1 \leq i \leq n (note that these are some elements of the last column, not the last row).The archaeologists want to know what a possible array of a is. Can you help them reconstruct any array that could be a?InputThe first line contains a single integer n (1 \leq n \leq 5 \cdot 10^5).The second line contains n integers b_{1,n}, b_{2,n}, \ldots, b_{n,n} (0 \leq b_{i,n} < 2^{30}).OutputIf some array a is consistent with the information, print a line containing n integers a_1, a_2, \ldots, a_n. If there are multiple solutions, output any.If such an array does not exist, output -1 instead.ExamplesInput 3 0 2 1 Output 1 2 3 Input 1 199633 Output 199633 Input 10 346484077 532933626 858787727 369947090 299437981 416813461 865836801 141384800 157794568 691345607 Output 725081944 922153789 481174947 427448285 516570428 509717938 855104873 280317429 281091129 1050390365 NoteIf we let a = [1,2,3], then b will be: \bf{0}\bf{1}\bf{2}\bf{3}\bf{0}130\bf{0}122\bf{0}131 The values of b_{1,n}, b_{2,n}, \ldots, b_{n,n} generated are [0,2,1] which is consistent with what the archaeologists have discovered.
3 0 2 1
1 2 3
1 second
256 megabytes
['bitmasks', 'combinatorics', 'constructive algorithms', 'dp', 'math', '*2900']
E. Cross Swappingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a square matrix A of size n \times n whose elements are integers. We will denote the element on the intersection of the i-th row and the j-th column as A_{i,j}.You can perform operations on the matrix. In each operation, you can choose an integer k, then for each index i (1 \leq i \leq n), swap A_{i, k} with A_{k, i}. Note that cell A_{k, k} remains unchanged.For example, for n = 4 and k = 3, this matrix will be transformed like this: The operation k = 3 swaps the blue row with the green column. You can perform this operation any number of times. Find the lexicographically smallest matrix^\dagger you can obtain after performing arbitrary number of operations.{}^\dagger For two matrices A and B of size n \times n, let a_{(i-1) \cdot n + j} = A_{i,j} and b_{(i-1) \cdot n + j} = B_{i,j}. Then, the matrix A is lexicographically smaller than the matrix B when there exists an index i (1 \leq i \leq n^2) such that a_i < b_i and for all indices j such that 1 \leq j < i, a_j = b_j.InputThe first line contains a single integer t (1 \leq t \leq 10^5) — the number of test cases.The first line of each test case contains a single integer n (1 \leq n \leq 1000) — the size of the matrix.The i-th line of the next n lines contains n integers A_{i, 1}, A_{i, 2}, \dots, A_{i, n} (1 \le A_{i, j} \le 10^9) — description of the matrix A.It is guaranteed that the sum of n^2 over all test cases does not exceed 10^6.OutputFor each test case, print n lines with n integers each — the lexicographically smallest matrix.ExampleInput 232 1 22 1 21 1 243 3 1 21 1 3 13 2 3 22 3 3 1Output 2 1 1 2 1 1 2 2 2 3 1 1 2 3 1 2 1 3 3 3 3 2 3 2 1 NoteNote that in every picture below the matrix is transformed in such a way that the blue rows are swapped with the green columns.In the first test case, we can perform 1 operation for k = 3. The matrix will be transformed as below: In the second test case, we can perform 2 operations for k = 1 and k = 3:
232 1 22 1 21 1 243 3 1 21 1 3 13 2 3 22 3 3 1
2 1 1 2 1 1 2 2 2 3 1 1 2 3 1 2 1 3 3 3 3 2 3 2 1
1 second
256 megabytes
['2-sat', 'data structures', 'dsu', 'greedy', 'matrices', '*2400']
D. Tournament Countdowntime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.There was a tournament consisting of 2^n contestants. The 1-st contestant competed with the 2-nd, the 3-rd competed with the 4-th, and so on. After that, the winner of the first match competed with the winner of second match, etc. The tournament ended when there was only one contestant left, who was declared the winner of the tournament. Such a tournament scheme is known as the single-elimination tournament.You don't know the results, but you want to find the winner of the tournament. In one query, you select two integers a and b, which are the indices of two contestants. The jury will return 1 if a won more matches than b, 2 if b won more matches than a, or 0 if their number of wins was equal.Find the winner in no more than \left \lceil \frac{1}{3} \cdot 2^{n + 1} \right \rceil queries. Here \lceil x \rceil denotes the value of x rounded up to the nearest integer.Note that the tournament is long over, meaning that the results are fixed and do not depend on your queries.InputThe first line contains a single integer t (1 \leq t \leq 2^{14}) — the number of test cases.The only line of input contains a single integer n (1 \leq n \leq 17).It is guaranteed that the sum of 2^n over all test cases does not exceed 2^{17}.InteractionThe interaction for each test case begins by reading the integer n.To make a query, output "? a b" (1 \leq a, b \leq 2^n) without quotes. Afterwards, you should read one single integer — the answer for your query. You can make at most \left \lceil \frac{1}{3} \cdot 2^{n + 1} \right \rceil such queries in each test case.If you receive the integer -1 instead of an answer or a valid value of n, it means your program has made an invalid query, has exceed the limit of queries, or has given incorrect answer on the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.When you are ready to give the final answer, output "! x" (1 \leq x \leq 2^n) without quotes — the winner of the tournament. Giving this answer does not count towards the limit of queries. After solving a test case, your program should move to the next one immediately. After solving all test cases, your program should be terminated immediately.After printing a query or the answer do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. HacksTo hack, use the following format.The first line contains an integer t (1 \leq t \leq 2^{14}) — the number of test cases.The first line of each test case contains a single integer n (1 \leq n \leq 17).The second line of each test case contains 2^n numbers on a line — the number of wins of each participant. There should be a sequence of matches that is consistent with the number of wins.The sum of 2^n should not exceed 2^{17}.ExampleInput 1 3 2 0 2 Output ? 1 4 ? 1 6 ? 5 7 ! 7NoteThe tournament in the first test case is shown below. The number of wins is [1,0,0,2,0,1,3,0]. In this example, the winner is the 7-th contestant.
1 3 2 0 2
? 1 4 ? 1 6 ? 5 7 ! 7
2 seconds
256 megabytes
['constructive algorithms', 'greedy', 'interactive', 'number theory', 'probabilities', '*1800']
C. Build Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA \mathbf{0}-indexed array a of size n is called good if for all valid indices i (0 \le i \le n-1), a_i + i is a perfect square^\dagger.Given an integer n. Find a permutation^\ddagger p of [0,1,2,\ldots,n-1] that is good or determine that no such permutation exists.^\dagger An integer x is said to be a perfect square if there exists an integer y such that x = y^2.^\ddagger An array b is a permutation of an array a if b consists of the elements of a in arbitrary order. For example, [4,2,3,4] is a permutation of [3,2,4,4] while [1,2,2] is not a permutation of [1,2,3].InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The only line of each test case contains a single integer n (1 \le n \le 10^5) — the length of the permutation p.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, output n distinct integers p_0, p_1, \dots, p_{n-1} (0 \le p_i \le n-1) — the permutation p — if the answer exists, and -1 otherwise.ExampleInput 3347Output 1 0 2 0 3 2 1 1 0 2 6 5 4 3 NoteIn the first test case, we have n=3. The array p = [1, 0, 2] is good since 1 + 0 = 1^2, 0 + 1 = 1^2, and 2 + 2 = 2^2In the second test case, we have n=4. The array p = [0, 3, 2, 1] is good since 0 + 0 = 0^2, 3 + 1 = 2^2, 2+2 = 2^2, and 1+3 = 2^2.
3347
1 0 2 0 3 2 1 1 0 2 6 5 4 3
1 second
256 megabytes
['constructive algorithms', 'dp', 'math', '*1200']
B. Optimal Reductiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputConsider an array a of n positive integers.You may perform the following operation: select two indices l and r (1 \leq l \leq r \leq n), then decrease all elements a_l, a_{l + 1}, \dots, a_r by 1. Let's call f(a) the minimum number of operations needed to change array a into an array of n zeros.Determine if for all permutations^\dagger b of a, f(a) \leq f(b) is true. ^\dagger An array b is a permutation of an array a if b consists of the elements of a in arbitrary order. For example, [4,2,3,4] is a permutation of [3,2,4,4] while [1,2,2] is not a permutation of [1,2,3].InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases.The first line of each test case contains a single integer n (1 \leq n \leq 10^5) — the length of the array a.The second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9) — description of the array a.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, print "YES" (without quotes) if for all permutations b of a, f(a) \leq f(b) is true, and "NO" (without quotes) otherwise.You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).ExampleInput 342 3 5 431 2 343 1 3 2Output YES YES NO NoteIn the first test case, we can change all elements to 0 in 5 operations. It can be shown that no permutation of [2, 3, 5, 4] requires less than 5 operations to change all elements to 0.In the third test case, we need 5 operations to change all elements to 0, while [2, 3, 3, 1] only needs 3 operations.
342 3 5 431 2 343 1 3 2
YES YES NO
1 second
256 megabytes
['constructive algorithms', 'sortings', '*1000']
A. Traveling Salesman Problemtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are living on an infinite plane with the Cartesian coordinate system on it. In one move you can go to any of the four adjacent points (left, right, up, down).More formally, if you are standing at the point (x, y), you can: go left, and move to (x - 1, y), or go right, and move to (x + 1, y), or go up, and move to (x, y + 1), or go down, and move to (x, y - 1). There are n boxes on this plane. The i-th box has coordinates (x_i,y_i). It is guaranteed that the boxes are either on the x-axis or the y-axis. That is, either x_i=0 or y_i=0.You can collect a box if you and the box are at the same point. Find the minimum number of moves you have to perform to collect all of these boxes if you have to start and finish at the point (0,0).InputThe first line contains a single integer t (1 \le t \le 100) — the number of test cases.The first line of each test case contains a single integer n (1 \le n \le 100) — the number of boxes.The i-th line of the following n lines contains two integers x_i and y_i (-100 \le x_i, y_i \le 100) — the coordinate of the i-th box. It is guaranteed that either x_i=0 or y_i=0.Do note that the sum of n over all test cases is not bounded.OutputFor each test case output a single integer — the minimum number of moves required.ExampleInput 340 -21 0-1 00 230 2-3 00 -110 0Output 12 12 0 NoteIn the first test case, a possible sequence of moves that uses the minimum number of moves required is shown below. (0,0) \to (1,0) \to (1,1) \to (1, 2) \to (0,2) \to (-1,2) \to (-1,1) \to (-1,0) \to (-1,-1) \to (-1,-2) \to (0,-2) \to (0,-1) \to (0,0) In the second test case, a possible sequence of moves that uses the minimum number of moves required is shown below. (0,0) \to (0,1) \to (0,2) \to (-1, 2) \to (-2,2) \to (-3,2) \to (-3,1) \to (-3,0) \to (-3,-1) \to (-2,-1) \to (-1,-1) \to (0,-1) \to (0,0) In the third test case, we can collect all boxes without making any moves.
340 -21 0-1 00 230 2-3 00 -110 0
12 12 0
1 second
256 megabytes
['geometry', 'greedy', 'implementation', '*800']
F. Triametertime limit per test4.5 secondsmemory limit per test768 megabytesinputstandard inputoutputstandard output — What is my mission?  — To count graph diameters.You and Your SubmissionA tree is a connected undirected graph without cycles. A weighted tree has a weight assigned to each edge. The degree of a vertex is the number of edges connected to this vertex.You are given a weighted tree with n vertices, each edge has a weight of 1. Let L be the set of vertices with degree equal to 1. You have to answer q independent queries. In the i-th query: You are given a positive integer x_i. For all u,v \in L such that u < v, add edge (u, v) with weight x_i to the graph (initially the given tree). Find the diameter of the resulting graph. The diameter of a graph is equal to \max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}, where \operatorname{d}(u, v) is the length of the shortest path between vertex u and vertex v.InputThe first line contains a single integer n (3 \le n \le 10^6).The second line contains n - 1 integers p_2,p_3,\ldots,p_n (1 \le p_i < i) indicating that there is an edge between vertices i and p_i. It is guaranteed that the given edges form a tree.The third line contains a single integer q (1 \le q \le 10).The fourth line contains q integers x_1,x_2,\ldots,x_q (1 \le x_i \le n). All x_i are distinct.OutputPrint q integers in a single line — the answers to the queries.ExamplesInput 4 1 2 2 4 1 2 3 4 Output 1 2 2 2 Input 7 1 2 3 4 2 1 7 2 1 3 7 5 6 4 Output 3 3 4 5 5 5 4 Input 3 1 2 1 1 Output 1 NoteThe graph in the first test after adding the edges:
4 1 2 2 4 1 2 3 4
1 2 2 2
4.5 seconds
768 megabytes
['binary search', 'data structures', 'dfs and similar', 'trees', '*3200']
E2. LCM Sum (hard version)time limit per test3.5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWe are sum for we are manySome NumberThis version of the problem differs from the previous one only in the constraint on t. You can make hacks only if both versions of the problem are solved.You are given two positive integers l and r.Count the number of distinct triplets of integers (i, j, k) such that l \le i < j < k \le r and \operatorname{lcm}(i,j,k) \ge i + j + k.Here \operatorname{lcm}(i, j, k) denotes the least common multiple (LCM) of integers i, j, and k.InputEach test contains multiple test cases. The first line contains the number of test cases t (\bf{1 \le t \le 10^5}). Description of the test cases follows.The only line for each test case contains two integers l and r (1 \le l \le r \le 2 \cdot 10^5, l + 2 \le r).OutputFor each test case print one integer — the number of suitable triplets.ExampleInput 51 43 58 8668 866 86868Output 3 1 78975 969 109229059713337 NoteIn the first test case, there are 3 suitable triplets: (1,2,3), (1,3,4), (2,3,4). In the second test case, there is 1 suitable triplet: (3,4,5).
51 43 58 8668 866 86868
3 1 78975 969 109229059713337
3.5 seconds
512 megabytes
['brute force', 'data structures', 'math', 'number theory', 'two pointers', '*2500']
E1. LCM Sum (easy version)time limit per test3.5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputWe are sum for we are manySome NumberThis version of the problem differs from the next one only in the constraint on t. You can make hacks only if both versions of the problem are solved.You are given two positive integers l and r.Count the number of distinct triplets of integers (i, j, k) such that l \le i < j < k \le r and \operatorname{lcm}(i,j,k) \ge i + j + k.Here \operatorname{lcm}(i, j, k) denotes the least common multiple (LCM) of integers i, j, and k.InputEach test contains multiple test cases. The first line contains the number of test cases t (\bf{1 \le t \le 5}). Description of the test cases follows.The only line for each test case contains two integers l and r (1 \le l \le r \le 2 \cdot 10^5, l + 2 \le r).OutputFor each test case print one integer — the number of suitable triplets.ExampleInput 51 43 58 8668 866 86868Output 3 1 78975 969 109229059713337 NoteIn the first test case, there are 3 suitable triplets: (1,2,3), (1,3,4), (2,3,4). In the second test case, there is 1 suitable triplet: (3,4,5).
51 43 58 8668 866 86868
3 1 78975 969 109229059713337
3.5 seconds
512 megabytes
['binary search', 'brute force', 'combinatorics', 'math', 'number theory', 'two pointers', '*2300']
D. Empty Graphtime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output — Do you have a wish?  — I want people to stop gifting each other arrays.O_o and Another Young BoyAn array of n positive integers a_1,a_2,\ldots,a_n fell down on you from the skies, along with a positive integer k \le n.You can apply the following operation at most k times: Choose an index 1 \le i \le n and an integer 1 \le x \le 10^9. Then do a_i := x (assign x to a_i). Then build a complete undirected weighted graph with n vertices numbered with integers from 1 to n, where edge (l, r) (1 \le l < r \le n) has weight \min(a_{l},a_{l+1},\ldots,a_{r}).You have to find the maximum possible diameter of the resulting graph after performing at most k operations.The diameter of a graph is equal to \max\limits_{1 \le u < v \le n}{\operatorname{d}(u, v)}, where \operatorname{d}(u, v) is the length of the shortest path between vertex u and vertex v.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). Description of the test cases follows.The first line of each test case contains two integers n and k (2 \le n \le 10^5, 1 \le k \le n).The second line of each test case contains n positive integers a_1,a_2,\ldots,a_n (1 \le a_i \le 10^9).It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case print one integer — the maximum possible diameter of the graph after performing at most k operations.ExampleInput 63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2Output 4 168 10 1000000000 9 1000000000 NoteIn the first test case, one of the optimal arrays is [2,4,5].The graph built on this array: \operatorname{d}(1, 2) = \operatorname{d}(1, 3) = 2 and \operatorname{d}(2, 3) = 4, so the diameter is equal to \max(2,2,4) = 4.
63 12 4 13 21 9 843 110 2 63 2179 17 10000000002 15 92 24 2
4 168 10 1000000000 9 1000000000
1.5 seconds
256 megabytes
['binary search', 'constructive algorithms', 'data structures', 'greedy', 'shortest paths', '*2000']
C. Sort Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array is sorted if it has no inversionsA Young BoyYou are given an array of n positive integers a_1,a_2,\ldots,a_n. In one operation you do the following: Choose any integer x. For all i such that a_i = x, do a_i := 0 (assign 0 to a_i). Find the minimum number of operations required to sort the array in non-decreasing order.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). Description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 10^5).The second line of each test case contains n positive integers a_1,a_2,\ldots,a_n (1 \le a_i \le n).It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case print one integer — the minimum number of operations required to sort the array in non-decreasing order.ExampleInput 533 3 241 3 1 354 1 5 3 242 4 1 211Output 1 2 4 3 0 NoteIn the first test case, you can choose x = 3 for the operation, the resulting array is [0, 0, 2].In the second test case, you can choose x = 1 for the first operation and x = 3 for the second operation, the resulting array is [0, 0, 0, 0].
533 3 241 3 1 354 1 5 3 242 4 1 211
1 2 4 3 0
1 second
256 megabytes
['greedy', 'sortings', '*1100']
B. Woeful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputI wonder, does the falling rain Forever yearn for it's disdain?Effluvium of the MindYou are given a positive integer n.Find any permutation p of length n such that the sum \operatorname{lcm}(1,p_1) + \operatorname{lcm}(2, p_2) + \ldots + \operatorname{lcm}(n, p_n) is as large as possible. Here \operatorname{lcm}(x, y) denotes the least common multiple (LCM) of integers x and y.A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 1\,000). Description of the test cases follows.The only line for each test case contains a single integer n (1 \le n \le 10^5).It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case print n integers p_1, p_2, \ldots, p_n — the permutation with the maximum possible value of \operatorname{lcm}(1,p_1) + \operatorname{lcm}(2, p_2) + \ldots + \operatorname{lcm}(n, p_n).If there are multiple answers, print any of them.ExampleInput 212Output 1 2 1 NoteFor n = 1, there is only one permutation, so the answer is [1].For n = 2, there are two permutations: [1, 2] — the sum is \operatorname{lcm}(1,1) + \operatorname{lcm}(2, 2) = 1 + 2 = 3. [2, 1] — the sum is \operatorname{lcm}(1,2) + \operatorname{lcm}(2, 1) = 2 + 2 = 4.
212
1 2 1
1 second
256 megabytes
['constructive algorithms', 'greedy', 'number theory', '*800']
A. Wonderful Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGod's Blessing on This PermutationForces!A Random PebbleYou are given a permutation p_1,p_2,\ldots,p_n of length n and a positive integer k \le n. In one operation you can choose two indices i and j (1 \le i < j \le n) and swap p_i with p_j.Find the minimum number of operations needed to make the sum p_1 + p_2 + \ldots + p_k as small as possible.A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 100). Description of the test cases follows.The first line of each test case contains two integers n and k (1 \le k \le n \le 100).The second line of each test case contains n integers p_1,p_2,\ldots,p_n (1 \le p_i \le n). It is guaranteed that the given numbers form a permutation of length n.OutputFor each test case print one integer — the minimum number of operations needed to make the sum p_1 + p_2 + \ldots + p_k as small as possible.ExampleInput 43 12 3 13 31 2 34 23 4 1 21 11Output 1 0 2 0 NoteIn the first test case, the value of p_1 + p_2 + \ldots + p_k is initially equal to 2, but the smallest possible value is 1. You can achieve it by swapping p_1 with p_3, resulting in the permutation [1, 3, 2].In the second test case, the sum is already as small as possible, so the answer is 0.
43 12 3 13 31 2 34 23 4 1 21 11
1 0 2 0
1 second
256 megabytes
['greedy', 'implementation', '*800']
B. Partytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA club plans to hold a party and will invite some of its n members. The n members are identified by the numbers 1, 2, \dots, n. If member i is not invited, the party will gain an unhappiness value of a_i.There are m pairs of friends among the n members. As per tradition, if both people from a friend pair are invited, they will share a cake at the party. The total number of cakes eaten will be equal to the number of pairs of friends such that both members have been invited.However, the club's oven can only cook two cakes at a time. So, the club demands that the total number of cakes eaten is an even number.What is the minimum possible total unhappiness value of the party, respecting the constraint that the total number of cakes eaten is even?InputEach 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 m (1 \leq n \leq 10^5, 0 \leq m \leq \min(10^5,\frac{n(n-1)}{2})) — the number of club members and pairs of friends.The second line of each test case contains n integers a_1,a_2, \dots,a_n (0 \leq a_i \leq 10^4) — the unhappiness value array.Each of the next m lines contains two integers x and y (1 \leq x,y \leq n, x \neq y) indicating that x and y are friends. Each unordered pair (x,y) appears at most once in each test case.It is guaranteed that both the sum of n and the sum of m over all test cases do not exceed 10^5.OutputFor each test case, print a line containing a single integer – the minimum possible unhappiness value of a valid party.ExampleInput 41 013 12 1 31 35 51 2 3 4 51 21 31 41 52 35 51 1 1 1 11 22 33 44 55 1Output 0 2 3 2 NoteIn the first test case, all members can be invited. So the unhappiness value is 0.In the second test case, the following options are possible: invite 1 and 2 (0 cakes eaten, unhappiness value equal to 3); invite 2 and 3 (0 cakes eaten, unhappiness value equal to 2); invite only 1 (0 cakes eaten, unhappiness value equal to 4); invite only 2 (0 cakes eaten, unhappiness value equal to 5); invite only 3 (0 cakes eaten, unhappiness value equal to 3); invite nobody (0 cakes eaten, unhappiness value equal to 6). The minimum unhappiness value is achieved by inviting 2 and 3.In the third test case, inviting members 3,4,5 generates a valid party with the minimum possible unhappiness value.
41 013 12 1 31 35 51 2 3 4 51 21 31 41 52 35 51 1 1 1 11 22 33 44 55 1
0 2 3 2
2 seconds
256 megabytes
['brute force', 'graphs', '*1300']
A. Perfect Permutationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a positive integer n.The weight of a permutation p_1, p_2, \ldots, p_n is the number of indices 1\le i\le n such that i divides p_i. Find a permutation p_1,p_2,\dots, p_n with the minimum possible weight (among all permutations of length n).A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).InputEach 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 only line of each test case contains a single integer n (1 \leq n \leq 10^5) — the length of permutation.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, print a line containing n integers p_1, p_2,\dots, p_n so that the permutation p has the minimum possible weight.If there are several possible answers, you can print any of them.ExampleInput 214Output 1 2 1 4 3 NoteIn the first test case, the only valid permutation is p=[1]. Its weight is 1.In the second test case, one possible answer is the permutation p=[2,1,4,3]. One can check that 1 divides p_1 and i does not divide p_i for i=2,3,4, so the weight of this permutation is 1. It is impossible to find a permutation of length 4 with a strictly smaller weight.
214
1 2 1 4 3
1 second
256 megabytes
['constructive algorithms', '*800']
E. Two Arraystime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two arrays of integers a_1,a_2,\dots,a_n and b_1,b_2,\dots,b_m. Alice and Bob are going to play a game. Alice moves first and they take turns making a move.They play on a grid of size n \times m (a grid with n rows and m columns). Initially, there is a rook positioned on the first row and first column of the grid.During her/his move, a player can do one of the following two operations: Move the rook to a different cell on the same row or the same column of the current cell. A player cannot move the rook to a cell that has been visited 1000 times before (i.e., the rook can stay in a certain cell at most 1000 times during the entire game). Note that the starting cell is considered to be visited once at the beginning of the game. End the game immediately with a score of a_r+b_c, where (r, c) is the current cell (i.e., the rook is on the r-th row and c-th column). Bob wants to maximize the score while Alice wants to minimize it. If they both play this game optimally, what is the final score of the game?InputThe first line contains two integers n and m (1 \leq n,m \leq 2 \cdot 10^5) — the length of the arrays a and b (which coincide with the number of rows and columns of the grid).The second line contains the n integers a_1, a_2, \dots, a_n (1 \leq a_i \leq 5 \cdot 10^8).The third line contains the m integers b_1, b_2,\dots, b_n (1 \leq b_i \leq 5 \cdot 10^8).OutputPrint a single line containing the final score of the game.ExamplesInput 2 1 3 2 2 Output 4 Input 4 5 235499701 451218171 355604420 132973458 365049318 264083156 491406845 62875547 175951751 Output 531556171 NoteIn the first test case, Alice moves the rook to (2, 1) and Bob moves the rook to (1, 1). This process will repeat for 999 times until finally, after Alice moves the rook, Bob cannot move it back to (1, 1) because it has been visited 1000 times before. So the final score of the game is a_2+b_1=4.In the second test case, the final score of the game is a_3+b_5.
2 1 3 2 2
4
2 seconds
256 megabytes
['binary search', 'games', 'graph matchings', '*2400']
D. Recover the Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRhodoks has a tree with n vertices, but he doesn't remember its structure. The vertices are indexed from 1 to n.A segment [l,r] (1 \leq l \leq r \leq n) is good if the vertices with indices l, l + 1, ..., r form a connected component in Rhodoks' tree. Otherwise, it is bad.For example, if the tree is the one in the picture, then only the segment [3,4] is bad while all the other segments are good. For each of the \frac{n(n+1)}{2} segments, Rhodoks remembers whether it is good or bad. Can you help him recover the tree? If there are multiple solutions, print any.It is guaranteed that the there is at least one tree satisfying Rhodoks' description. InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \leq t \leq 1000). The description of the test cases follows.The first line of each test case contains an integer n (1 \leq n \leq 2000) — the number of vertices in the tree.Then n lines follow. The i-th of these lines contains a string good_i of length n+1-i consisting of 0 and 1. If the segment [i,i+j-1] is good then the j-th character of good_i is 1, otherwise j-th character of good_i is 0.It is guaranteed that the there is at least one tree consistent with the given data. It is guaranteed that the sum of n over all test cases does not exceed 2000.OutputFor each test case, print n-1 lines describing the tree you recover. The i-th line should contain two integers u_i and v_i (1 \leq u_i,v_i \leq n), denoting an edge between vertices u_i and v_i.If there are multiple solutions, print any.ExampleInput 341111111101611111111111111111111112100100000001111000000011000000000100000000100100011110000100000100001001111101Output 1 2 2 3 2 4 1 2 2 3 3 4 4 5 5 6 2 3 6 7 10 11 2 4 6 8 10 12 1 4 5 8 9 12 5 12 2 12 NoteThe first test case is explained in the statement.In the second test case, one possible tree is as follows: In the third test case, one possible tree is as follows:
341111111101611111111111111111111112100100000001111000000011000000000100000000100100011110000100000100001001111101
1 2 2 3 2 4 1 2 2 3 3 4 4 5 5 6 2 3 6 7 10 11 2 4 6 8 10 12 1 4 5 8 9 12 5 12 2 12
3 seconds
256 megabytes
['constructive algorithms', 'trees', '*3400']
C. XOR Triangletime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a positive integer n. Since n may be very large, you are given its binary representation.You should compute the number of triples (a,b,c) with 0 \leq a,b,c \leq n such that a \oplus b, b \oplus c, and a \oplus c are the sides of a non-degenerate triangle. Here, \oplus denotes the bitwise XOR operation.You should output the answer modulo 998\,244\,353.Three positive values x, y, and z are the sides of a non-degenerate triangle if and only if x+y>z, x+z>y, and y+z>x.InputThe first and only line contains the binary representation of an integer n (0 < n < 2^{200\,000}) without leading zeros.For example, the string 10 is the binary representation of the number 2, while the string 1010 represents the number 10.OutputPrint one integer — the number of triples (a,b,c) satisfying the conditions described in the statement modulo 998\,244\,353.ExamplesInput 101Output 12 Input 1110Output 780 Input 11011111101010010Output 141427753 NoteIn the first test case, 101_2=5. The triple (a, b, c) = (0, 3, 5) is valid because (a\oplus b, b\oplus c, c\oplus a) = (3, 6, 5) are the sides of a non-degenerate triangle. The triple (a, b, c) = (1, 2, 4) is valid because (a\oplus b, b\oplus c, c\oplus a) = (3, 6, 5) are the sides of a non-degenerate triangle. The 6 permutations of each of these two triples are all the valid triples, thus the answer is 12.In the third test case, 11\,011\,111\,101\,010\,010_2=114\,514. The full answer (before taking the modulo) is 1\,466\,408\,118\,808\,164.
101
12
4 seconds
512 megabytes
['bitmasks', 'brute force', 'constructive algorithms', 'dp', 'greedy', 'math', '*2500']
B. Raintime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are the owner of a harvesting field which can be modeled as an infinite line, whose positions are identified by integers.It will rain for the next n days. On the i-th day, the rain will be centered at position x_i and it will have intensity p_i. Due to these rains, some rainfall will accumulate; let a_j be the amount of rainfall accumulated at integer position j. Initially a_j is 0, and it will increase by \max(0,p_i-|x_i-j|) after the i-th day's rain.A flood will hit your field if, at any moment, there is a position j with accumulated rainfall a_j>m.You can use a magical spell to erase exactly one day's rain, i.e., setting p_i=0. For each i from 1 to n, check whether in case of erasing the i-th day's rain there is no flood.InputEach 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 m (1 \leq n \leq 2 \cdot 10^5, 1 \leq m \leq 10^9) — the number of rainy days and the maximal accumulated rainfall with no flood occurring.Then n lines follow. The i-th of these lines contains two integers x_i and p_i (1 \leq x_i,p_i \leq 10^9) — the position and intensity of the i-th day's rain.The sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a binary string s length of n. The i-th character of s is 1 if after erasing the i-th day's rain there is no flood, while it is 0, if after erasing the i-th day's rain the flood still happens.ExampleInput 43 61 55 53 42 31 35 22 51 610 66 124 51 612 55 59 78 3Output 001 11 00 100110 NoteIn the first test case, if we do not use the spell, the accumulated rainfall distribution will be like this: If we erase the third day's rain, the flood is avoided and the accumulated rainfall distribution looks like this: In the second test case, since initially the flood will not happen, we can erase any day's rain.In the third test case, there is no way to avoid the flood.
43 61 55 53 42 31 35 22 51 610 66 124 51 612 55 59 78 3
001 11 00 100110
4 seconds
256 megabytes
['binary search', 'brute force', 'data structures', 'geometry', 'greedy', 'implementation', 'math', '*2100']
A. Color the Picturetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA picture can be represented as an n\times m grid (n rows and m columns) so that each of the n \cdot m cells is colored with one color. You have k pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most a_i cells with the i-th pigment.A picture is considered beautiful if each cell has at least 3 toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers 1 \leq x_1,x_2 \leq n and 1 \leq y_1,y_2 \leq m, the cell in the x_1-th row and y_1-th column is a toroidal neighbor of the cell in the x_2-th row and y_2-th column if one of following two conditions holds: x_1-x_2 \equiv \pm1 \pmod{n} and y_1=y_2, or y_1-y_2 \equiv \pm1 \pmod{m} and x_1=x_2. Notice that each cell has exactly 4 toroidal neighbors. For example, if n=3 and m=4, the toroidal neighbors of the cell (1, 2) (the cell on the first row and second column) are: (3, 2), (2, 2), (1, 3), (1, 1). They are shown in gray on the image below: The gray cells show toroidal neighbors of (1, 2). Is it possible to color all cells with the pigments provided and create a beautiful picture?InputEach 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 three integers n, m, and k (3 \leq n,m \leq 10^9, 1 \leq k \leq 10^5) — the number of rows and columns of the picture and the number of pigments.The next line contains k integers a_1,a_2,\dots, a_k (1 \leq a_i \leq 10^9) — a_i is the maximum number of cells that can be colored with the i-th pigment.It is guaranteed that the sum of k over all test cases does not exceed 10^5.OutputFor each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes).ExampleInput 64 6 312 9 83 3 28 83 3 29 54 5 210 115 4 29 1110 10 311 45 14Output Yes No Yes Yes No No NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment 1.
64 6 312 9 83 3 28 83 3 29 54 5 210 115 4 29 1110 10 311 45 14
Yes No Yes Yes No No
1 second
256 megabytes
['constructive algorithms', 'greedy', 'math', '*1500']
F. Multiset of Stringstime limit per test6 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given three integers n, k and f.Consider all binary strings (i. e. all strings consisting of characters 0 and/or 1) of length from 1 to n. For every such string s, you need to choose an integer c_s from 0 to k.A multiset of binary strings of length exactly n is considered beautiful if for every binary string s with length from 1 to n, the number of strings in the multiset such that s is their prefix is not exceeding c_s.For example, let n = 2, c_{0} = 3, c_{00} = 1, c_{01} = 2, c_{1} = 1, c_{10} = 2, and c_{11} = 3. The multiset of strings \{11, 01, 00, 01\} is beautiful, since: for the string 0, there are 3 strings in the multiset such that 0 is their prefix, and 3 \le c_0; for the string 00, there is one string in the multiset such that 00 is its prefix, and 1 \le c_{00}; for the string 01, there are 2 strings in the multiset such that 01 is their prefix, and 2 \le c_{01}; for the string 1, there is one string in the multiset such that 1 is its prefix, and 1 \le c_1; for the string 10, there are 0 strings in the multiset such that 10 is their prefix, and 0 \le c_{10}; for the string 11, there is one string in the multiset such that 11 is its prefix, and 1 \le c_{11}. Now, for the problem itself. You have to calculate the number of ways to choose the integer c_s for every binary string s of length from 1 to n in such a way that the maximum possible size of a beautiful multiset is exactly f.InputThe only line of input contains three integers n, k and f (1 \le n \le 15; 1 \le k, f \le 2 \cdot 10^5).OutputPrint one integer — the number of ways to choose the integer c_s for every binary string s of length from 1 to n in such a way that the maximum possible size of a beautiful multiset is exactly f. Since it can be huge, print it modulo 998244353.ExamplesInput 1 42 2 Output 3 Input 2 37 13 Output 36871576 Input 4 1252 325 Output 861735572 Input 6 153 23699 Output 0 Input 15 200000 198756 Output 612404746 NoteIn the first example, the three ways to choose the integers c_s are: c_0 = 0, c_1 = 2, then the maximum beautiful multiset is \{1, 1\}; c_0 = 1, c_1 = 1, then the maximum beautiful multiset is \{0, 1\}; c_0 = 2, c_1 = 0, then the maximum beautiful multiset is \{0, 0\}.
1 42 2
3
6 seconds
512 megabytes
['bitmasks', 'brute force', 'dp', 'fft', 'flows', 'graphs', 'math', 'meet-in-the-middle', 'trees', '*2500']
E. XOR Treetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree consisting of n vertices. A number is written on each vertex; the number on vertex i is equal to a_i.Recall that a simple path is a path that visits each vertex at most once. Let the weight of the path be the bitwise XOR of the values written on vertices it consists of. Let's say that a tree is good if no simple path has weight 0.You can apply the following operation any number of times (possibly, zero): select a vertex of the tree and replace the value written on it with an arbitrary positive integer. What is the minimum number of times you have to apply this operation in order to make the tree good?InputThe first line contains one integer n (1 \le n \le 2 \cdot 10^5) — the number of vertices.The second line contains n integers a_1, a_2, ..., a_n (1 \le a_i < 2^{30}) — the numbers written on vertices.Then n - 1 lines follow, each containing two integers x and y (1 \le x, y \le n; x \ne y) denoting an edge connecting vertex x with vertex y. It is guaranteed that these edges form a tree.OutputPrint a single integer — the minimum number of times you have to apply the operation in order to make the tree good.ExamplesInput 6 3 2 1 3 2 1 4 5 3 4 1 4 2 1 6 1 Output 2 Input 4 2 1 1 1 1 2 1 3 1 4 Output 0 Input 5 2 2 2 2 2 1 2 2 3 3 4 4 5 Output 2 NoteIn the first example, it is enough to replace the value on the vertex 1 with 13, and the value on the vertex 4 with 42.
6 3 2 1 3 2 1 4 5 3 4 1 4 2 1 6 1
2
3 seconds
256 megabytes
['bitmasks', 'data structures', 'dfs and similar', 'dsu', 'greedy', 'trees', '*2400']
D. Rorororobottime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a grid, consisting of n rows and m columns. The rows are numbered from 1 to n from bottom to top. The columns are numbered from 1 to m from left to right. The i-th column has the bottom a_i cells blocked (the cells in rows 1, 2, \dots, a_i), the remaining n - a_i cells are unblocked.A robot is travelling across this grid. You can send it commands — move up, right, down or left. If a robot attempts to move into a blocked cell or outside the grid, it explodes.However, the robot is broken — it executes each received command k times. So if you tell it to move up, for example, it will move up k times (k cells). You can't send it commands while the robot executes the current one.You are asked q queries about the robot. Each query has a start cell, a finish cell and a value k. Can you send the robot an arbitrary number of commands (possibly, zero) so that it reaches the finish cell from the start cell, given that it executes each command k times?The robot must stop in the finish cell. If it visits the finish cell while still executing commands, it doesn't count.InputThe first line contains two integers n and m (1 \le n \le 10^9; 1 \le m \le 2 \cdot 10^5) — the number of rows and columns of the grid.The second line contains m integers a_1, a_2, \dots, a_m (0 \le a_i \le n) — the number of blocked cells on the bottom of the i-th column.The third line contains a single integer q (1 \le q \le 2 \cdot 10^5) — the number of queries.Each of the next q lines contain five integers x_s, y_s, x_f, y_f and k (a[y_s] < x_s \le n; 1 \le y_s \le m; a[y_f] < x_f \le n; 1 \le y_f \le m; 1 \le k \le 10^9) — the row and the column of the start cell, the row and the column of the finish cell and the number of times each your command is executed. The start and the finish cell of each query are unblocked.OutputFor each query, print "YES" if you can send the robot an arbitrary number of commands (possibly, zero) so that it reaches the finish cell from the start cell, given that it executes each command k times. Otherwise, print "NO".ExampleInput 11 10 9 0 0 10 3 4 8 11 10 8 6 1 2 1 3 1 1 2 1 3 2 4 3 4 5 2 5 3 11 5 3 5 3 11 5 2 11 9 9 10 1 Output YES NO NO NO YES YES
11 10 9 0 0 10 3 4 8 11 10 8 6 1 2 1 3 1 1 2 1 3 2 4 3 4 5 2 5 3 11 5 3 5 3 11 5 2 11 9 9 10 1
YES NO NO NO YES YES
2 seconds
256 megabytes
['binary search', 'data structures', 'greedy', 'math', '*1700']
C. Recover an RBStime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); bracket sequences ")(", "(" and ")" are not. There was an RBS. Some brackets have been replaced with question marks. Is it true that there is a unique way to replace question marks with brackets, so that the resulting sequence is an RBS?InputThe first line contains a single integer t (1 \le t \le 5 \cdot 10^4) — the number of testcases.The only line of each testcase contains an RBS with some brackets replaced with question marks. Each character is either '(', ')' or '?'. At least one RBS can be recovered from the given sequence.The total length of the sequences over all testcases doesn't exceed 2 \cdot 10^5.OutputFor each testcase, print "YES" if the way to replace question marks with brackets, so that the resulting sequence is an RBS, is unique. If there is more than one way, then print "NO".ExampleInput 5(?))??????()???(?)()?)Output YES NO YES YES NO NoteIn the first testcase, the only possible original RBS is "(())".In the second testcase, there are multiple ways to recover an RBS.In the third and the fourth testcases, the only possible original RBS is "()".In the fifth testcase, the original RBS can be either "((()()))" or "(())()()".
5(?))??????()???(?)()?)
YES NO YES YES NO
2 seconds
256 megabytes
['constructive algorithms', 'greedy', 'implementation', 'strings', '*1800']
B. Also Try Minecrafttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are beta testing the new secret Terraria update. This update will add quests to the game!Simply, the world map can be represented as an array of length n, where the i-th column of the world has height a_i.There are m quests you have to test. The j-th of them is represented by two integers s_j and t_j. In this quest, you have to go from the column s_j to the column t_j. At the start of the quest, you are appearing at the column s_j.In one move, you can go from the column x to the column x-1 or to the column x+1. In this version, you have Spectre Boots, which allow you to fly. Since it is a beta version, they are bugged, so they only allow you to fly when you are going up and have infinite fly duration. When you are moving from the column with the height p to the column with the height q, then you get some amount of fall damage. If the height p is greater than the height q, you get p - q fall damage, otherwise you fly up and get 0 damage.For each of the given quests, determine the minimum amount of fall damage you can get during this quest.InputThe first line of the input contains two integers n and m (2 \le n \le 10^5; 1 \le m \le 10^5) — the number of columns in the world and the number of quests you have to test, respectively.The second line of the input contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^9), where a_i is the height of the i-th column of the world.The next m lines describe quests. The j-th of them contains two integers s_j and t_j (1 \le s_j, t_j \le n; s_j \ne t_j), which means you have to move from the column s_j to the column t_j during the j-th quest.Note that s_j can be greater than t_j.OutputPrint m integers. The j-th of them should be the minimum amount of fall damage you can get during the j-th quest completion.ExampleInput 7 6 10 8 9 6 8 12 7 1 2 1 7 4 6 7 1 3 5 4 2 Output 2 10 0 7 3 1
7 6 10 8 9 6 8 12 7 1 2 1 7 4 6 7 1 3 5 4 2
2 10 0 7 3 1
2 seconds
256 megabytes
['data structures', 'dp', 'implementation', '*900']
A. Three Doorstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are three doors in front of you, numbered from 1 to 3 from left to right. Each door has a lock on it, which can only be opened with a key with the same number on it as the number on the door.There are three keys — one for each door. Two of them are hidden behind the doors, so that there is no more than one key behind each door. So two doors have one key behind them, one door doesn't have a key behind it. To obtain a key hidden behind a door, you should first unlock that door. The remaining key is in your hands.Can you open all the doors?InputThe first line contains a single integer t (1 \le t \le 18) — the number of testcases.The first line of each testcase contains a single integer x (1 \le x \le 3) — the number on the key in your hands.The second line contains three integers a, b and c (0 \le a, b, c \le 3) — the number on the key behind each of the doors. If there is no key behind the door, the number is equal to 0.Values 1, 2 and 3 appear exactly once among x, a, b and c.OutputFor each testcase, print "YES" if you can open all the doors. Otherwise, print "NO".ExampleInput 430 1 210 3 223 1 021 3 0Output YES NO YES NO
430 1 210 3 223 1 021 3 0
YES NO YES NO
2 seconds
256 megabytes
['brute force', 'greedy', 'implementation', 'math', '*800']
B. Difference of GCDstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three integers n, l, and r. You need to construct an array a_1,a_2,\dots,a_n (l\le a_i\le r) such that \gcd(i,a_i) are all distinct or report there's no solution.Here \gcd(x, y) denotes the greatest common divisor (GCD) of integers x and y.InputThe input consists of multiple test cases. The first line contains a single integer t (1\le t\le 10^4) — the number of test cases. The description of the test cases follows.The first line contains three integers n, l, r (1 \le n \le 10^5, 1\le l\le r\le 10^9).It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, if there is no solution, print "NO" (without quotes). You can print letters in any case (upper or lower).Otherwise, print "YES" (without quotes). In the next line, print n integers a_1,a_2,\ldots,a_n — the array you construct.If there are multiple solutions, you may output any.ExampleInput 4 5 1 5 9 1000 2000 10 30 35 1 1000000000 1000000000 Output YES 1 2 3 4 5 YES 1145 1926 1440 1220 1230 1350 1001 1000 1233 NO YES 1000000000 NoteIn the first test case, \gcd(1,a_1),\gcd(2,a_2),\ldots,\gcd(5,a_5) are equal to 1, 2, 3, 4, 5, respectively.
4 5 1 5 9 1000 2000 10 30 35 1 1000000000 1000000000
YES 1 2 3 4 5 YES 1145 1926 1440 1220 1230 1350 1001 1000 1233 NO YES 1000000000
1 second
256 megabytes
['constructive algorithms', 'math', '*1100']
A. Difference Operationstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a consisting of n positive integers.You are allowed to perform this operation any number of times (possibly, zero): choose an index i (2 \le i \le n), and change a_i to a_i - a_{i-1}. Is it possible to make a_i=0 for all 2\le i\le n?InputThe input consists of multiple test cases. The first line contains a single integer t (1\le t\le 100)  — the number of test cases. The description of the test cases follows.The first line contains one integer n (2 \le n \le 100) — the length of array a.The second line contains n integers a_1,a_2,\ldots,a_n (1 \le a_i \le 10^9).OutputFor each test case, print "YES" (without quotes), if it is possible to change a_i to 0 for all 2 \le i \le n, and "NO" (without quotes) otherwise.You can print letters in any case (upper or lower).ExampleInput 425 1031 2 341 1 1 199 9 8 2 4 4 3 5 3Output YES YES YES NO NoteIn the first test case, the initial array is [5,10]. You can perform 2 operations to reach the goal: Choose i=2, and the array becomes [5,5]. Choose i=2, and the array becomes [5,0]. In the second test case, the initial array is [1,2,3]. You can perform 4 operations to reach the goal: Choose i=3, and the array becomes [1,2,1]. Choose i=2, and the array becomes [1,1,1]. Choose i=3, and the array becomes [1,1,0]. Choose i=2, and the array becomes [1,0,0]. In the third test case, you can choose indices in the order 4, 3, 2.
425 1031 2 341 1 1 199 9 8 2 4 4 3 5 3
YES YES YES NO
1 second
256 megabytes
['greedy', 'math', '*800']
F. Bugabootime limit per test2 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputA transformation of an array of positive integers a_1,a_2,\dots,a_n is defined by replacing a with the array b_1,b_2,\dots,b_n given by b_i=a_i\oplus a_{(i\bmod n)+1}, where \oplus denotes the bitwise XOR operation.You are given integers n, t, and w. We consider an array c_1,c_2,\dots,c_n (0 \le c_i \le 2^w-1) to be bugaboo if and only if there exists an array a_1,a_2,\dots,a_n such that after transforming a for t times, a becomes c.For example, when n=6, t=2, w=2, then the array [3,2,1,0,2,2] is bugaboo because it can be given by transforming the array [2,3,1,1,0,1] for 2 times: [2,3,1,1,0,1]\to [2\oplus 3,3\oplus 1,1\oplus 1,1\oplus 0,0\oplus 1,1\oplus 2]=[1,2,0,1,1,3]; \\ [1,2,0,1,1,3]\to [1\oplus 2,2\oplus 0,0\oplus 1,1\oplus 1,1\oplus 3,3\oplus 1]=[3,2,1,0,2,2]. And the array [4,4,4,4,0,0] is not bugaboo because 4 > 2^2 - 1. The array [2,3,3,3,3,3] is also not bugaboo because it can't be given by transforming one array for 2 times.You are given an array c with some positions lost (only m positions are known at first and the remaining positions are lost). And there are q modifications, where each modification is changing a position of c. A modification can possibly change whether the position is lost or known, and it can possibly redefine a position that is already given.You need to calculate how many possible arrays c (with arbitrary elements on the lost positions) are bugaboos after each modification. Output the i-th answer modulo p_i (p_i is a given array consisting of q elements).InputThe first line contains four integers n, m, t and w (2\le n\le 10^7, 0\le m\le \min(n, 10^5), 1\le t\le 10^9, 1\le w\le 30).The i-th line of the following m lines contains two integers d_i and e_i (1\le d_i\le n, 0\le e_i< 2^w). It means the position d_i of the array c is given and c_{d_i}=e_i. It is guaranteed that 1\le d_1<d_2<\ldots<d_m\le n.The next line contains only one number q (1\le q\le 10^5) — the number of modifications.The i-th line of the following q lines contains three integers f_i, g_i, p_i (1\le f_i\le n, -1\le g_i< 2^w, 11\le p_i\le 10^9+7). The value g_i=-1 means changing the position f_i of the array c to a lost position, otherwise it means changing the position f_i of the array c to a known position, and c_{f_i}=g_i. The value p_i means you need to output the i-th answer modulo p_i.OutputThe output contains q lines, denoting your answers.ExamplesInput 3 2 1 1 1 1 3 1 4 2 0 123456789 2 1 111111111 1 -1 987654321 3 -1 555555555 Output 1 0 1 2 Input 24 8 5 4 4 4 6 12 8 12 15 11 16 7 20 2 21 9 22 12 13 2 13 11 3 15 12 5 7 13 9 3 14 10 5 15 11 15 16 13 14 17 14 1 18 18 9 19 19 6 20 23 10 21 24 8 22 21 13 23 Output 1 4 9 2 1 0 1 10 11 16 16 0 16 NoteIn the first example, n=3, t=1, and w=1. Let ? denote a lost position of c.In the first query, c=[1,0,1]. The only possible array [1,0,1] is bugaboo because it can be given by transforming [0,1,1] once. So the answer is 1\bmod 123\,456\,789 = 1.In the second query, c=[1,1,1]. The only possible array [1,1,1] is not bugaboo. So the answer is 0\bmod 111\,111\,111 = 0.In the third query, c=[?,1,1]. There are two possible arrays [1,1,1] and [0,1,1]. Only [0,1,1] is bugaboo because it can be given by transforming [1,1,0] once. So the answer is 1\bmod 987\,654\,321=1.In the fourth query, c=[?,1,?]. There are four possible arrays. [0,1,1] and [1,1,0] are bugaboos. [1,1,0] can be given by performing [1,0,1] once. So the answer is 2\bmod 555\,555\,555=2.
3 2 1 1 1 1 3 1 4 2 0 123456789 2 1 111111111 1 -1 987654321 3 -1 555555555
1 0 1 2
2 seconds
1024 megabytes
['bitmasks', 'constructive algorithms', 'dp', 'number theory', '*3500']
E. Replacetime limit per test1.5 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputYou are given an integer array a_1,\ldots, a_n, where 1\le a_i \le n for all i.There's a "replace" function f which takes a pair of integers (l, r), where l \le r, as input and outputs the pair f\big( (l, r) \big)=\left(\min\{a_l,a_{l+1},\ldots,a_r\},\, \max\{a_l,a_{l+1},\ldots,a_r\}\right).Consider repeated calls of this function. That is, from a starting pair (l, r) we get f\big((l, r)\big), then f\big(f\big((l, r)\big)\big), then f\big(f\big(f\big((l, r)\big)\big)\big), and so on.Now you need to answer q queries. For the i-th query you have two integers l_i and r_i (1\le l_i\le r_i\le n). You must answer the minimum number of times you must apply the "replace" function to the pair (l_i,r_i) to get (1, n), or report that it is impossible.InputThe first line contains two positive integers n, q (1\le n,q\le 10^5) — the length of the sequence a and the number of the queries.The second line contains n positive integers a_1,a_2,\ldots,a_n (1\le a_i\le n) — the sequence a.Each line of the following q lines contains two integers l_i, r_i (1\le l_i\le r_i\le n) — the queries.OutputFor each query, output the required number of times, or -1 if it is impossible.ExamplesInput 5 6 2 5 4 1 3 4 4 1 5 1 4 3 5 4 5 2 3 Output -1 0 1 2 3 4 Input 6 3 2 3 4 6 1 2 5 6 2 5 2 3 Output 5 1 3 Input 5 3 3 2 2 4 1 2 5 1 3 1 5 Output -1 -1 0 NoteIn the first example, n=5 and a=[2,5,4,1,3].For the first query: (4,4)\to(1,1)\to(2,2)\to(5,5)\to(3,3)\to(4,4)\to\ldots, so it's impossible to get (1,5).For the second query, you already have (1,5).For the third query: (1,4)\to(1,5).For the fourth query: (3,5)\to(1,4)\to(1,5).For the fifth query: (4,5)\to(1,3)\to(2,5)\to(1,5).For the sixth query: (2,3)\to(4,5)\to(1,3)\to(2,5)\to(1,5).
5 6 2 5 4 1 3 4 4 1 5 1 4 3 5 4 5 2 3
-1 0 1 2 3 4
1.5 seconds
1024 megabytes
['binary search', 'data structures', '*3500']
D. Partial Virtual Treestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputKawashiro Nitori is a girl who loves competitive programming. One day she found a rooted tree consisting of n vertices. The root is vertex 1. As an advanced problem setter, she quickly thought of a problem.Kawashiro Nitori has a vertex set U=\{1,2,\ldots,n\}. She's going to play a game with the tree and the set. In each operation, she will choose a vertex set T, where T is a partial virtual tree of U, and change U into T.A vertex set S_1 is a partial virtual tree of a vertex set S_2, if S_1 is a subset of S_2, S_1 \neq S_2, and for all pairs of vertices i and j in S_1, \operatorname{LCA}(i,j) is in S_1, where \operatorname{LCA}(x,y) denotes the lowest common ancestor of vertices x and y on the tree. Note that a vertex set can have many different partial virtual trees.Kawashiro Nitori wants to know for each possible k, if she performs the operation exactly k times, in how many ways she can make U=\{1\} in the end? Two ways are considered different if there exists an integer z (1 \le z \le k) such that after z operations the sets U are different.Since the answer could be very large, you need to find it modulo p. It's guaranteed that p is a prime number.InputThe first line contains two integers n and p (2 \le n \le 2000, 10^8 + 7 \le p \le 10^9+9). It's guaranteed that p is a prime number.Each of the next n-1 lines contains two integers u_i, v_i (1 \leq u_i, v_i \leq n), representing an edge between u_i and v_i.It is guaranteed that the given edges form a tree.OutputThe only line contains n-1 integers — the answer modulo p for k=1,2,\ldots,n-1.ExamplesInput 4 998244353 1 2 2 3 1 4 Output 1 6 6 Input 7 100000007 1 2 1 3 2 4 2 5 3 6 3 7 Output 1 47 340 854 880 320 Input 8 1000000007 1 2 2 3 3 4 4 5 5 6 6 7 7 8 Output 1 126 1806 8400 16800 15120 5040 NoteIn the first test case, when k=1, the only possible way is: \{1,2,3,4\} \to \{1\}. When k=2, there are 6 possible ways: \{1,2,3,4\} \to \{1,2\} \to \{1\}; \{1,2,3,4\} \to \{1,2,3\} \to \{1\}; \{1,2,3,4\} \to \{1,2,4\} \to \{1\}; \{1,2,3,4\} \to \{1,3\} \to \{1\}; \{1,2,3,4\} \to \{1,3,4\} \to \{1\}; \{1,2,3,4\} \to \{1,4\} \to \{1\}. When k=3, there are 6 possible ways: \{1,2,3,4\} \to \{1,2,3\} \to \{1,2\} \to \{1\}; \{1,2,3,4\} \to \{1,2,3\} \to \{1,3\} \to \{1\}; \{1,2,3,4\} \to \{1,2,4\} \to \{1,2\} \to \{1\}; \{1,2,3,4\} \to \{1,2,4\} \to \{1,4\} \to \{1\}; \{1,2,3,4\} \to \{1,3,4\} \to \{1,3\} \to \{1\}; \{1,2,3,4\} \to \{1,3,4\} \to \{1,4\} \to \{1\}.
4 998244353 1 2 2 3 1 4
1 6 6
2 seconds
256 megabytes
['combinatorics', 'dfs and similar', 'dp', 'math', 'trees', '*3000']
C. DFS Treestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a connected undirected graph consisting of n vertices and m edges. The weight of the i-th edge is i.Here is a wrong algorithm of finding a minimum spanning tree (MST) of a graph:vis := an array of length ns := a set of edgesfunction dfs(u): vis[u] := true iterate through each edge (u, v) in the order from smallest to largest edge weight if vis[v] = false add edge (u, v) into the set (s) dfs(v)function findMST(u): reset all elements of (vis) to false reset the edge set (s) to empty dfs(u) return the edge set (s)Each of the calls findMST(1), findMST(2), ..., findMST(n) gives you a spanning tree of the graph. Determine which of these trees are minimum spanning trees.InputThe first line of the input contains two integers n, m (2\le n\le 10^5, n-1\le m\le 2\cdot 10^5) — the number of vertices and the number of edges in the graph.Each of the following m lines contains two integers u_i and v_i (1\le u_i, v_i\le n, u_i\ne v_i), describing an undirected edge (u_i,v_i) in the graph. The i-th edge in the input has weight i.It is guaranteed that the graph is connected and there is at most one edge between any pair of vertices.OutputYou need to output a binary string s, where s_i=1 if findMST(i) creates an MST, and s_i = 0 otherwise.ExamplesInput 5 5 1 2 3 5 1 3 3 2 4 2 Output 01111 Input 10 11 1 2 2 5 3 4 4 2 8 1 4 5 10 5 9 5 8 2 5 7 4 6 Output 0011111011 NoteHere is the graph given in the first example. There is only one minimum spanning tree in this graph. A minimum spanning tree is (1,2),(3,5),(1,3),(2,4) which has weight 1+2+3+5=11.Here is a part of the process of calling findMST(1): reset the array vis and the edge set s; calling dfs(1); vis[1] := true; iterate through each edge (1,2),(1,3); add edge (1,2) into the edge set s, calling dfs(2): vis[2] := true iterate through each edge (2,1),(2,3),(2,4); because vis[1] = true, ignore the edge (2,1); add edge (2,3) into the edge set s, calling dfs(3): ... In the end, it will select edges (1,2),(2,3),(3,5),(2,4) with total weight 1+4+2+5=12>11, so findMST(1) does not find a minimum spanning tree.It can be shown that the other trees are all MSTs, so the answer is 01111.
5 5 1 2 3 5 1 3 3 2 4 2
01111
1 second
256 megabytes
['dfs and similar', 'dsu', 'graphs', 'greedy', 'sortings', 'trees', '*2400']
B. Difference Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a consisting of n non-negative integers. It is guaranteed that a is sorted from small to large.For each operation, we generate a new array b_i=a_{i+1}-a_{i} for 1 \le i < n. Then we sort b from small to large, replace a with b, and decrease n by 1.After performing n-1 operations, n becomes 1. You need to output the only integer in array a (that is to say, you need to output a_1).InputThe input consists of multiple test cases. The first line contains a single integer t (1\le t\le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains one integer n (2\le n\le 10^5) — the length of the array a.The second line contains n integers a_1,a_2,\dots,a_n (0\le a_1\le \ldots\le a_n \le 5\cdot 10^5) — the array a.It is guaranteed that the sum of n over all test cases does not exceed 2.5\cdot 10^5, and the sum of a_n over all test cases does not exceed 5\cdot 10^5.OutputFor each test case, output the answer on a new line.ExampleInput 531 10 10044 8 9 1350 0 0 8 1362 4 8 16 32 6470 0 0 0 0 0 0Output 81 3 1 2 0 NoteTo simplify the notes, let \operatorname{sort}(a) denote the array you get by sorting a from small to large.In the first test case, a=[1,10,100] at first. After the first operation, a=\operatorname{sort}([10-1,100-10])=[9,90]. After the second operation, a=\operatorname{sort}([90-9])=[81].In the second test case, a=[4,8,9,13] at first. After the first operation, a=\operatorname{sort}([8-4,9-8,13-9])=[1,4,4]. After the second operation, a=\operatorname{sort}([4-1,4-4])=[0,3]. After the last operation, a=\operatorname{sort}([3-0])=[3].
531 10 10044 8 9 1350 0 0 8 1362 4 8 16 32 6470 0 0 0 0 0 0
81 3 1 2 0
1 second
256 megabytes
['brute force', 'data structures', 'implementation', 'sortings', '*1900']
A. Doremy's IQtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputDoremy is asked to test n contests. Contest i can only be tested on day i. The difficulty of contest i is a_i. Initially, Doremy's IQ is q. On day i Doremy will choose whether to test contest i or not. She can only test a contest if her current IQ is strictly greater than 0.If Doremy chooses to test contest i on day i, the following happens: if a_i>q, Doremy will feel she is not wise enough, so q decreases by 1; otherwise, nothing changes. If she chooses not to test a contest, nothing changes.Doremy wants to test as many contests as possible. Please give Doremy a solution.InputThe input consists of multiple test cases. The first line contains a single integer t (1\le t\le 10^4) — the number of test cases. The description of the test cases follows.The first line contains two integers n and q (1 \le n \le 10^5, 1 \le q \le 10^9) — the number of contests and Doremy's IQ in the beginning.The second line contains n integers a_1,a_2,\cdots,a_n (1 \le a_i \le 10^9) — the difficulty of each contest.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, you need to output a binary string s, where s_i=1 if Doremy should choose to test contest i, and s_i=0 otherwise. The number of ones in the string should be maximum possible, and she should never test a contest when her IQ is zero or less.If there are multiple solutions, you may output any.ExampleInput 51 112 11 23 11 2 14 21 4 3 15 25 1 2 4 3Output 1 11 110 1110 01111 NoteIn the first test case, Doremy tests the only contest. Her IQ doesn't decrease.In the second test case, Doremy tests both contests. Her IQ decreases by 1 after testing contest 2.In the third test case, Doremy tests contest 1 and 2. Her IQ decreases to 0 after testing contest 2, so she can't test contest 3.
51 112 11 23 11 2 14 21 4 3 15 25 1 2 4 3
1 11 110 1110 01111
1 second
256 megabytes
['binary search', 'constructive algorithms', 'greedy', 'implementation', '*1600']
E. Qpwoeirut and Verticestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a connected undirected graph with n vertices and m edges. Vertices of the graph are numbered by integers from 1 to n and edges of the graph are numbered by integers from 1 to m.Your task is to answer q queries, each consisting of two integers l and r. The answer to each query is the smallest non-negative integer k such that the following condition holds: For all pairs of integers (a, b) such that l\le a\le b\le r, vertices a and b are reachable from one another using only the first k edges (that is, edges 1, 2, \ldots, k). InputThe first line contains a single integer t (1\le t\le 1000) — the number of test cases.The first line of each test case contains three integers n, m, and q (2\le n\le 10^5, 1\le m, q\le 2\cdot 10^5) — the number of vertices, edges, and queries respectively.Each of the next m lines contains two integers u_i and v_i (1\le u_i, v_i\le n) — ends of the i-th edge.It is guaranteed that the graph is connected and there are no multiple edges or self-loops.Each of the next q lines contains two integers l and r (1\le l\le r\le n) — descriptions of the queries.It is guaranteed that that the sum of n over all test cases does not exceed 10^5, the sum of m over all test cases does not exceed 2\cdot 10^5, and the sum of q over all test cases does not exceed 2\cdot 10^5.OutputFor each test case, print q integers — the answers to the queries.ExampleInput 32 1 21 21 11 25 5 51 21 32 43 43 51 43 42 22 53 53 2 11 32 31 3Output 0 1 3 3 0 5 5 2 Note Graph from the first test case. The integer near the edge is its number. In the first test case, the graph contains 2 vertices and a single edge connecting vertices 1 and 2.In the first query, l=1 and r=1. It is possible to reach any vertex from itself, so the answer to this query is 0.In the second query, l=1 and r=2. Vertices 1 and 2 are reachable from one another using only the first edge, through the path 1 \longleftrightarrow 2. It is impossible to reach vertex 2 from vertex 1 using only the first 0 edges. So, the answer to this query is 1. Graph from the second test case. The integer near the edge is its number. In the second test case, the graph contains 5 vertices and 5 edges.In the first query, l=1 and r=4. It is enough to use the first 3 edges to satisfy the condition from the statement: Vertices 1 and 2 are reachable from one another through the path 1 \longleftrightarrow 2 (edge 1). Vertices 1 and 3 are reachable from one another through the path 1 \longleftrightarrow 3 (edge 2). Vertices 1 and 4 are reachable from one another through the path 1 \longleftrightarrow 2 \longleftrightarrow 4 (edges 1 and 3). Vertices 2 and 3 are reachable from one another through the path 2 \longleftrightarrow 1 \longleftrightarrow 3 (edges 1 and 2). Vertices 2 and 4 are reachable from one another through the path 2 \longleftrightarrow 4 (edge 3). Vertices 3 and 4 are reachable from one another through the path 3 \longleftrightarrow 1 \longleftrightarrow 2 \longleftrightarrow 4 (edges 2, 1, and 3). If we use less than 3 of the first edges, then the condition won't be satisfied. For example, it is impossible to reach vertex 4 from vertex 1 using only the first 2 edges. So, the answer to this query is 3.In the second query, l=3 and r=4. Vertices 3 and 4 are reachable from one another through the path 3 \longleftrightarrow 1 \longleftrightarrow 2 \longleftrightarrow 4 (edges 2, 1, and 3). If we use any fewer of the first edges, nodes 3 and 4 will not be reachable from one another.
32 1 21 21 11 25 5 51 21 32 43 43 51 43 42 22 53 53 2 11 32 31 3
0 1 3 3 0 5 5 2
2 seconds
256 megabytes
['binary search', 'data structures', 'dfs and similar', 'divide and conquer', 'dsu', 'greedy', 'trees', '*2300']
D2. Chopping Carrots (Hard Version)time limit per test4 secondsmemory limit per test64 megabytesinputstandard inputoutputstandard outputThis is the hard version of the problem. The only difference between the versions is the constraints on n, k, a_i, and the sum of n over all test cases. You can make hacks only if both versions of the problem are solved.Note the unusual memory limit.You are given an array of integers a_1, a_2, \ldots, a_n of length n, and an integer k.The cost of an array of integers p_1, p_2, \ldots, p_n of length n is \max\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right) - \min\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right).Here, \lfloor \frac{x}{y} \rfloor denotes the integer part of the division of x by y. Find the minimum cost of an array p such that 1 \le p_i \le k for all 1 \le i \le n.InputThe first line contains a single integer t (1 \le t \le 100) — the number of test cases.The first line of each test case contains two integers n and k (1 \le n, k \le 10^5).The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_1 \le a_2 \le \ldots \le a_n \le 10^5).It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, print a single integer — the minimum possible cost of an array p satisfying the condition above.ExampleInput 75 24 5 6 8 115 124 5 6 8 113 12 9 157 32 3 5 5 6 9 106 5654 286 527 1436 2450 26813 9516 340 22412 21 3Output 2 0 13 1 4 7 0 NoteIn the first test case, the optimal array is p = [1, 1, 1, 2, 2]. The resulting array of values of \lfloor \frac{a_i}{p_i} \rfloor is [4, 5, 6, 4, 5]. The cost of p is \max\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) - \min\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) = 6 - 4 = 2. We can show that there is no array (satisfying the condition from the statement) with a smaller cost.In the second test case, one of the optimal arrays is p = [12, 12, 12, 12, 12], which results in all \lfloor \frac{a_i}{p_i} \rfloor being 0.In the third test case, the only possible array is p = [1, 1, 1].
75 24 5 6 8 115 124 5 6 8 113 12 9 157 32 3 5 5 6 9 106 5654 286 527 1436 2450 26813 9516 340 22412 21 3
2 0 13 1 4 7 0
4 seconds
64 megabytes
['brute force', 'constructive algorithms', 'data structures', 'dp', 'greedy', 'math', 'number theory', 'two pointers', '*2400']
D1. Chopping Carrots (Easy Version)time limit per test4 secondsmemory limit per test64 megabytesinputstandard inputoutputstandard outputThis is the easy version of the problem. The only difference between the versions is the constraints on n, k, a_i, and the sum of n over all test cases. You can make hacks only if both versions of the problem are solved.Note the unusual memory limit.You are given an array of integers a_1, a_2, \ldots, a_n of length n, and an integer k.The cost of an array of integers p_1, p_2, \ldots, p_n of length n is \max\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right) - \min\limits_{1 \le i \le n}\left(\left \lfloor \frac{a_i}{p_i} \right \rfloor \right).Here, \lfloor \frac{x}{y} \rfloor denotes the integer part of the division of x by y. Find the minimum cost of an array p such that 1 \le p_i \le k for all 1 \le i \le n.InputThe first line contains a single integer t (1 \le t \le 100) — the number of test cases.The first line of each test case contains two integers n and k (1 \le n, k \le 3000).The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_1 \le a_2 \le \ldots \le a_n \le 3000).It is guaranteed that the sum of n over all test cases does not exceed 3000.OutputFor each test case, print a single integer — the minimum possible cost of an array p satisfying the condition above.ExampleInput 75 24 5 6 8 115 124 5 6 8 113 12 9 157 32 3 5 5 6 9 106 5654 286 527 1436 2450 26813 9516 340 22412 21 3Output 2 0 13 1 4 7 0 NoteIn the first test case, the optimal array is p = [1, 1, 1, 2, 2]. The resulting array of values of \lfloor \frac{a_i}{p_i} \rfloor is [4, 5, 6, 4, 5]. The cost of p is \max\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) - \min\limits_{1 \le i \le n}(\lfloor \frac{a_i}{p_i} \rfloor) = 6 - 4 = 2. We can show that there is no array (satisfying the condition from the statement) with a smaller cost.In the second test case, one of the optimal arrays is p = [12, 12, 12, 12, 12], which results in all \lfloor \frac{a_i}{p_i} \rfloor being 0.In the third test case, the only possible array is p = [1, 1, 1].
75 24 5 6 8 115 124 5 6 8 113 12 9 157 32 3 5 5 6 9 106 5654 286 527 1436 2450 26813 9516 340 22412 21 3
2 0 13 1 4 7 0
4 seconds
64 megabytes
['binary search', 'brute force', 'constructive algorithms', 'greedy', 'number theory', '*1700']
C. Qpwoeirut And The Citytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputQpwoeirut has taken up architecture and ambitiously decided to remodel his city.Qpwoeirut's city can be described as a row of n buildings, the i-th (1 \le i \le n) of which is h_i floors high. You can assume that the height of every floor in this problem is equal. Therefore, building i is taller than the building j if and only if the number of floors h_i in building i is larger than the number of floors h_j in building j.Building i is cool if it is taller than both building i-1 and building i+1 (and both of them exist). Note that neither the 1-st nor the n-th building can be cool.To remodel the city, Qpwoeirut needs to maximize the number of cool buildings. To do this, Qpwoeirut can build additional floors on top of any of the buildings to make them taller. Note that he cannot remove already existing floors.Since building new floors is expensive, Qpwoeirut wants to minimize the number of floors he builds. Find the minimum number of floors Qpwoeirut needs to build in order to maximize the number of cool buildings.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains the single integer n (3 \le n \le 10^5) — the number of buildings in Qpwoeirut's city.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 number of floors in each of the buildings of the city.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, print a single integer — the minimum number of additional floors Qpwoeirut needs to build in order to maximize the number of cool buildings.ExampleInput 632 1 251 2 1 4 363 1 4 5 5 284 2 1 3 5 3 6 161 10 1 1 10 181 10 11 1 10 11 10 1Output 2 0 3 3 0 4 NoteIn the first test case, it is optimal for Qpwoeirut to make the second building cool by building 2 additional floors on top of it, making it taller than both of its adjacent buildings. The final heights of buildings will be [2, \underline{3}, 2]. In the second test case, the number of cool buildings is already maximized, so Qpwoeirut does not need to do anything.In the third test case, it is optimal for Qpwoeirut to make the third and fifth buildings cool by building 2 additional floors onto the third building and 1 additional floor onto the fifth building. The final heights of buildings will be [3, 1, \underline{6}, 5, \underline{6}, 2]. It can be shown that it is impossible to make more than 2 of the buildings cool, or to make 2 buildings cool using fewer than 3 additional floors.In the fourth test case, Qpwoeirut can either make the second building cool, or he can make the third building cool. Either way, he will be building 3 additional floors and maximizing the number of cool buildings. The final heights of buildings will be [4, 2, \underline{4}, 3, 5, 3, 6, 1] or [4, \underline{5}, 1, 3, 5, 3, 6, 1].
632 1 251 2 1 4 363 1 4 5 5 284 2 1 3 5 3 6 161 10 1 1 10 181 10 11 1 10 11 10 1
2 0 3 3 0 4
1 second
256 megabytes
['dp', 'flows', 'greedy', 'implementation', '*1400']
B. Making Towerstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a sequence of n colored blocks. The color of the i-th block is c_i, an integer between 1 and n.You will place the blocks down in sequence on an infinite coordinate grid in the following way. Initially, you place block 1 at (0, 0). For 2 \le i \le n, if the (i - 1)-th block is placed at position (x, y), then the i-th block can be placed at one of positions (x + 1, y), (x - 1, y), (x, y + 1) (but not at position (x, y - 1)), as long no previous block was placed at that position. A tower is formed by s blocks such that they are placed at positions (x, y), (x, y + 1), \ldots, (x, y + s - 1) for some position (x, y) and integer s. The size of the tower is s, the number of blocks in it. A tower of color r is a tower such that all blocks in it have the color r.For each color r from 1 to n, solve the following problem independently: Find the maximum size of a tower of color r that you can form by placing down the blocks according to the rules. InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. The first line of each test case contains a single integer n (1 \le n \le 10^5).The second line of each test case contains n integers c_1, c_2, \ldots, c_n (1 \le c_i \le n).It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output n integers. The r-th of them should be the maximum size of an tower of color r you can form by following the given rules. If you cannot form any tower of color r, the r-th integer should be 0.ExampleInput 671 2 3 1 2 3 164 2 2 2 4 41155 4 5 3 563 3 3 1 3 381 2 3 4 4 3 2 1Output 3 2 2 0 0 0 0 0 3 0 2 0 0 1 0 0 1 1 1 1 0 4 0 0 0 2 2 2 2 0 0 0 0 NoteIn the first test case, one of the possible ways to form a tower of color 1 and size 3 is: place block 1 at position (0, 0); place block 2 to the right of block 1, at position (1, 0); place block 3 above block 2, at position (1, 1); place block 4 to the left of block 3, at position (0, 1); place block 5 to the left of block 4, at position (-1, 1); place block 6 above block 5, at position (-1, 2); place block 7 to the right of block 6, at position (0, 2). The blocks at positions (0, 0), (0, 1), and (0, 2) all have color 1, forming an tower of size 3.In the second test case, note that the following placement is not valid, since you are not allowed to place block 6 under block 5: It can be shown that it is impossible to form a tower of color 4 and size 3.
671 2 3 1 2 3 164 2 2 2 4 41155 4 5 3 563 3 3 1 3 381 2 3 4 4 3 2 1
3 2 2 0 0 0 0 0 3 0 2 0 0 1 0 0 1 1 1 1 0 4 0 0 0 2 2 2 2 0 0 0 0
1 second
256 megabytes
['dp', 'greedy', 'math', '*1100']
A. Another String Minimization Problemtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a sequence a_1, a_2, \ldots, a_n of length n, consisting of integers between 1 and m. You also have a string s, consisting of m characters B.You are going to perform the following n operations. At the i-th (1 \le i \le n) operation, you replace either the a_i-th or the (m + 1 - a_i)-th character of s with A. You can replace the character at any position multiple times through the operations. Find the lexicographically smallest string you can get after these operations.A string x is lexicographically smaller than a string y of the same length if and only if in the first position where x and y differ, the string x has a letter that appears earlier in the alphabet than the corresponding letter in y.InputThe first line contains the number of test cases t (1 \le t \le 2000).The first line of each test case contains two integers n and m (1 \le n, m \le 50) — the length of the sequence a and the length of the string s respectively.The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le m) — the sequence a.OutputFor each test case, print a string of length m — the lexicographically smallest string you can get. Each character of the string should be either capital English letter A or capital English letter B.ExampleInput 64 51 1 3 11 524 11 1 1 12 41 32 77 54 55 5 3 5Output ABABA BABBB A AABB ABABBBB ABABA NoteIn the first test case, the sequence a = [1, 1, 3, 1]. One of the possible solutions is the following. At the 1-st operation, you can replace the 1-st character of s with A. After it, s becomes ABBBB. At the 2-nd operation, you can replace the 5-th character of s with A (since m+1-a_2=5). After it, s becomes ABBBA. At the 3-rd operation, you can replace the 3-rd character of s with A. After it, s becomes ABABA. At the 4-th operation, you can replace the 1-st character of s with A. After it, s remains equal to ABABA. The resulting string is ABABA. It is impossible to produce a lexicographically smaller string.In the second test case, you are going to perform only one operation. You can replace either the 2-nd character or 4-th character of s with A. You can get strings BABBB and BBBAB after the operation. The string BABBB is the lexicographically smallest among these strings.In the third test case, the only string you can get is A.In the fourth test case, you can replace the 1-st and 2-nd characters of s with A to get AABB.In the fifth test case, you can replace the 1-st and 3-rd characters of s with A to get ABABBBB.
64 51 1 3 11 524 11 1 1 12 41 32 77 54 55 5 3 5
ABABA BABBB A AABB ABABBBB ABABA
1 second
256 megabytes
['2-sat', 'constructive algorithms', 'greedy', 'string suffix structures', 'strings', '*800']
F. Mark and the Online Examtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMark is administering an online exam consisting of n true-false questions. However, he has lost all answer keys. He needs a way to retrieve the answers before his client gets infuriated.Fortunately, he has access to the grading system. Thus, for each query, you can input the answers to all n questions, and the grading system will output how many of them are correct.He doesn't have much time, so he can use the grading system at most 675 times. Help Mark determine the answer keys.Note that answer keys are fixed in advance and will not change depending on your queries.InputThe first line of the input consists of an integer n (1\leq n\leq 1000) — the number of questions.InteractionAfter reading n, you can start making queries to the grading system. For each query, print a line containing a string s of length n consisting of only letters 'T' and 'F'. s_i = 'T' means that you answer the i-question true. s_i = 'F' means that you answer the i-question false. After a successful query, you should read an integer k (0\leq k\leq n) — the number of correct answers. If you read n, then you found the answers, and your program should not make any more queries.If your program reads k = -1 instead of the number of correct answers, it means that you either made an invalid query or exceeded the query limits. Exit immediately after receiving -1, and you will see Wrong answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. HacksTo hack, use the following format:The first line contains an integer n (1\leq n\leq 1000) — the number of questions.The second line contains a string s of length n consisting of only 'T' and 'F' — the answer key.ExamplesInput 3 1 3Output FTT TTF Input 4 0 3 4 Output FTFF TTTT TFTT NoteThe empty lines in the example are just for you to better understand the interaction process. You're not required to print them.In the first example, there are 3 questions, and the answer to each question is 'true', 'true', and 'false', respectively. The first query, guessing the answers to be 'false', 'true', and 'true', respectively, guesses only one question — the 2-nd question — correctly. Then, in the second query, the program correctly guesses the answer key. The interaction ends here. In the second example, there are 4 questions, and the answer to each question is 'true', 'false', 'true', and 'true', respectively. The first query guessed none of the questions correctly, resulting in the answer 0. The second query guessed the 1-st, 3-rd, and 4-th question correctly, resulting in the answer 3. In the third query, the program correctly guesses the answer key. Then, the interaction ends.
3 1 3
FTT TTF
2 seconds
256 megabytes
['bitmasks', 'constructive algorithms', 'interactive', 'probabilities', '*2900']
E. Mark and Professor Korotime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAfter watching a certain anime before going to sleep, Mark dreams of standing in an old classroom with a blackboard that has a sequence of n positive integers a_1, a_2,\dots,a_n on it.Then, professor Koro comes in. He can perform the following operation: select an integer x that appears at least 2 times on the board, erase those 2 appearances, and write x+1 on the board. Professor Koro then asks Mark the question, "what is the maximum possible number that could appear on the board after some operations?"Mark quickly solves this question, but he is still slower than professor Koro. Thus, professor Koro decides to give Mark additional challenges. He will update the initial sequence of integers q times. Each time, he will choose positive integers k and l, then change a_k to l. After each update, he will ask Mark the same question again.Help Mark answer these questions faster than Professor Koro!Note that the updates are persistent. Changes made to the sequence a will apply when processing future updates.InputThe first line of the input contains two integers n and q (2\leq n\leq 2\cdot 10^5, 1\leq q\leq 2\cdot 10^5) — the length of the sequence a and the number of updates, respectively.The second line contains n integers a_1,a_2,\dots,a_n (1\leq a_i\leq 2\cdot 10^5)Then, q lines follow, each consisting of two integers k and l (1\leq k\leq n, 1\leq l\leq 2\cdot 10^5), telling to update a_k to l.OutputPrint q lines. The i-th line should consist of a single integer — the answer after the i-th update.ExamplesInput 5 4 2 2 2 4 5 2 3 5 3 4 1 1 4 Output 6 5 4 5 Input 2 1 200000 1 2 200000 Output 200001 NoteIn the first example test, the program must proceed through 4 updates.The sequence after the first update is [2,3,2,4,5]. One sequence of operations that achieves the number 6 the following. Initially, the blackboard has numbers [2,3,2,4,5]. Erase two copies of 2 and write 3, yielding [3,4,5,\color{red}{3}]. Erase two copies of 3 and write 4, yielding [4,5,\color{red}{4}]. Erase two copies of 4 and write 5, yielding [5,\color{red}{5}]. Erase two copies of 5 and write 6, yielding [\color{red}{6}]. Then, in the second update, the array is changed to [2,3,2,4,3]. This time, Mark cannot achieve 6. However, one sequence that Mark can use to achieve 5 is shown below. Initially, the blackboard has [2,3,2,4,3]. Erase two copies of 2 and write 3, yielding [3,4,3,\color{red}{3}]. Erase two copies of 3 and write 4, yielding [3,4,\color{red}{4}]. Erase two copies of 4 and write 5, yielding [3,\color{red}{5}]. In the third update, the array is changed to [2,3,2,1,3]. One way to achieve 4 is shown below. Initially, the blackboard has [2,3,2,1,3]. Erase two copies of 3 and write 4, yielding [2,2,1,\color{red}{4}].
5 4 2 2 2 4 5 2 3 5 3 4 1 1 4
6 5 4 5
2 seconds
256 megabytes
['binary search', 'bitmasks', 'brute force', 'combinatorics', 'data structures', 'greedy', '*2300']
D. Mark and Lightbulbstime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMark has just purchased a rack of n lightbulbs. The state of the lightbulbs can be described with binary string s = s_1s_2\dots s_n, where s_i=\texttt{1} means that the i-th lightbulb is turned on, while s_i=\texttt{0} means that the i-th lightbulb is turned off.Unfortunately, the lightbulbs are broken, and the only operation he can perform to change the state of the lightbulbs is the following: Select an index i from 2,3,\dots,n-1 such that s_{i-1}\ne s_{i+1}. Toggle s_i. Namely, if s_i is \texttt{0}, set s_i to \texttt{1} or vice versa. Mark wants the state of the lightbulbs to be another binary string t. Help Mark determine the minimum number of operations to do so.InputThe first line of the input contains a single integer q (1\leq q\leq 10^4) — the number of test cases.The first line of each test case contains a single integer n (3\leq n\leq 2\cdot 10^5) — the number of lightbulbs.The second line of each test case contains a binary string s of length n — the initial state of the lightbulbs.The third line of each test case contains a binary string t of length n — the final state of the lightbulbs.It is guaranteed that the sum of n across all test cases does not exceed 2\cdot 10^5.OutputFor each test case, print a line containing the minimum number of operations Mark needs to perform to transform s to t. If there is no such sequence of operations, print -1.ExampleInput 4401000010410100100501001000116000101010011Output 2 -1 -1 5 NoteIn the first test case, one sequence of operations that achieves the minimum number of operations is the following. Select i=3, changing \texttt{01}\color{red}{\texttt{0}}\texttt{0} to \texttt{01}\color{red}{\texttt{1}}\texttt{0}. Select i=2, changing \texttt{0}\color{red}{\texttt{1}}\texttt{10} to \texttt{0}\color{red}{\texttt{0}}\texttt{10}. In the second test case, there is no sequence of operations because one cannot change the first digit or the last digit of s.In the third test case, even though the first digits of s and t are the same and the last digits of s and t are the same, it can be shown that there is no sequence of operations that satisfies the condition.In the fourth test case, one sequence that achieves the minimum number of operations is the following: Select i=3, changing \texttt{00}\color{red}{\texttt{0}}\texttt{101} to \texttt{00}\color{red}{\texttt{1}}\texttt{101}. Select i=2, changing \texttt{0}\color{red}{\texttt{0}}\texttt{1101} to \texttt{0}\color{red}{\texttt{1}}\texttt{1101}. Select i=4, changing \texttt{011}\color{red}{\texttt{1}}\texttt{01} to \texttt{011}\color{red}{\texttt{0}}\texttt{01}. Select i=5, changing \texttt{0110}\color{red}{\texttt{0}}\texttt{1} to \texttt{0110}\color{red}{\texttt{1}}\texttt{1}. Select i=3, changing \texttt{01}\color{red}{\texttt{1}}\texttt{011} to \texttt{01}\color{red}{\texttt{0}}\texttt{011}.
4401000010410100100501001000116000101010011
2 -1 -1 5
1.5 seconds
256 megabytes
['combinatorics', 'constructive algorithms', 'greedy', 'math', 'sortings', '*1800']
C. Mark and His Unfinished Essaytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputOne night, Mark realized that there is an essay due tomorrow. He hasn't written anything yet, so Mark decided to randomly copy-paste substrings from the prompt to make the essay.More formally, the prompt is a string s of initial length n. Mark will perform the copy-pasting operation c times. Each operation is described by two integers l and r, which means that Mark will append letters s_l s_{l+1} \ldots s_r to the end of string s. Note that the length of s increases after this operation.Of course, Mark needs to be able to see what has been written. After copying, Mark will ask q queries: given an integer k, determine the k-th letter of the final string s.InputThe first line contains a single integer t (1\leq t\leq 1000) — the number of test cases.The first line of each test case contains three integers n, c, and q (1\leq n\leq 2\cdot 10^5, 1\leq c\leq 40, and 1\leq q\leq 10^4) — the length of the initial string s, the number of copy-pasting operations, and the number of queries, respectively.The second line of each test case contains a single string s of length n. It is guaranteed that s only contains lowercase English letters.The following c lines describe the copy-pasting operation. Each line contains two integers l and r (1\leq l\leq r\leq 10^{18}). It is also guaranteed that r does not exceed the current length of s.The last q lines of each test case describe the queries. Each line contains a single integer k (1\leq k\leq 10^{18}). It is also guaranteed that k does not exceed the final length of s.It is guaranteed that the sum of n and q across all test cases does not exceed 2\cdot 10^5 and 10^4, respectively.OutputFor each query, print the k-th letter of the final string s.ExampleInput 24 3 3mark1 45 73 8110127 3 3creamii2 33 42 991112Output m a r e a r NoteIn the first test case, the copy-paste process is as follows. The first step is pasting string \texttt{mark} at the end, yielding the string \texttt{mark}\color{red}{\texttt{mark}}. The second step is pasting string \texttt{mar} at the end, yielding the string \texttt{markmark}\color{red}{\texttt{mar}}. The third step is pasting string \texttt{rkmark} at the end, yielding the string \texttt{markmarkmar}\color{red}{\texttt{rkmark}}. In the second test case, the copy-paste process is as follows. The first step is pasting string \texttt{re} at the end, yielding the string \texttt{creamii}\color{red}{\texttt{re}}. The second step is pasting string \texttt{ea} at the end, yielding the string \texttt{creamiire}\color{red}{\texttt{ea}}. The third step is pasting string \texttt{reamiire} at the end, yielding the string \texttt{creamiireea}\color{red}{\texttt{reamiire}}.
24 3 3mark1 45 73 8110127 3 3creamii2 33 42 991112
m a r e a r
2 seconds
256 megabytes
['brute force', 'implementation', '*1400']
B. Mark the Dust Sweepertime limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMark is cleaning a row of n rooms. The i-th room has a nonnegative dust level a_i. He has a magical cleaning machine that can do the following three-step operation. Select two indices i<j such that the dust levels a_i, a_{i+1}, \dots, a_{j-1} are all strictly greater than 0. Set a_i to a_i-1. Set a_j to a_j+1. Mark's goal is to make a_1 = a_2 = \ldots = a_{n-1} = 0 so that he can nicely sweep the n-th room. Determine the minimum number of operations needed to reach his goal.InputThe first line contains a single integer t (1\leq t\leq 10^4) — the number of test cases.The first line of each test case contains a single integer n (2\leq n\leq 2\cdot 10^5) — the number of rooms.The second line of each test case contains n integers a_1, a_2, ..., a_n (0\leq a_i\leq 10^9) — the dust level of each room.It is guaranteed that the sum of n across all test cases does not exceed 2\cdot 10^5.OutputFor each test case, print a line containing a single integer — the minimum number of operations. It can be proven that there is a sequence of operations that meets the goal.ExampleInput 432 0 050 2 0 2 062 0 3 0 4 640 0 0 10Output 3 5 11 0 NoteIn the first case, one possible sequence of operations is as follows. Choose i=1 and j=2, yielding the array [1,1,0]. Choose i=1 and j=3, yielding the array [0,1,1]. Choose i=2 and j=3, yielding the array [0,0,2]. At this point, a_1=a_2=0, completing the process.In the second case, one possible sequence of operations is as follows. Choose i=4 and j=5, yielding the array [0,2,0,1,1]. Choose i=2 and j=3, yielding the array [0,1,1,1,1]. Choose i=2 and j=5, yielding the array [0,0,1,1,2]. Choose i=3 and j=5, yielding the array [0,0,0,1,3]. Choose i=4 and j=5, yielding the array [0,0,0,0,4]. In the last case, the array already satisfies the condition.
432 0 050 2 0 2 062 0 3 0 4 640 0 0 10
3 5 11 0
1.5 seconds
256 megabytes
['constructive algorithms', 'greedy', 'implementation', '*900']
A. Mark the Photographertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMark is asked to take a group photo of 2n people. The i-th person has height h_i units.To do so, he ordered these people into two rows, the front row and the back row, each consisting of n people. However, to ensure that everyone is seen properly, the j-th person of the back row must be at least x units taller than the j-th person of the front row for each j between 1 and n, inclusive.Help Mark determine if this is possible.InputThe first line contains one integer t (1\leq t\leq 100) — the number of test cases. Each test case consists of two lines.The first line of each test case contains two positive integers n and x (1\leq n\leq 100, 1\leq x\leq 10^3) — the number of people in each row and the minimum difference Mark wants.The second line of each test case contains 2n positive integers h_1,h_2,\ldots,h_{2n} (1\leq h_i\leq 10^3) — the height of each person in units.Note that the sum of n over all test cases is not bounded.OutputFor each test case, print a single line containing "YES" if Mark could arrange people satisfying his condition and "NO" otherwise.You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answers).ExampleInput 33 61 3 9 10 12 163 12 5 2 2 2 51 28 6Output YES NO YES NoteIn the first test case, one possible order is to have the third, fifth, and sixth person on the back row and the second, first, and fourth on the front row. The heights of the people will look like this. Back91216Front3110 It works because h_3-h_2 = 9-3 \geq 6, h_5-h_1 = 12-1\geq 6, and h_6-h_4 = 16-10\geq 6. In the second test case, it can be shown there is no way to order people in a way that satisfies the condition.In the third test case, the only way to arrange people to satisfy the condition is to have the first person on the back row and the second person on the front row.
33 61 3 9 10 12 163 12 5 2 2 2 51 28 6
YES NO YES
1 second
256 megabytes
['greedy', 'sortings', '*800']
H2. Game of AI (hard version)time limit per test12 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputThis is the hard version of this problem. The difference between easy and hard versions is the constraint on k and the time limit. Notice that you need to calculate the answer for all positive integers n \in [1,k] in this version. You can make hacks only if both versions of the problem are solved.Cirno is playing a war simulator game with n towers (numbered from 1 to n) and n bots (numbered from 1 to n). The i-th tower is initially occupied by the i-th bot for 1 \le i \le n.Before the game, Cirno first chooses a permutation p = [p_1, p_2, \ldots, p_n] of length n (A permutation of length n is an array of length n where each integer between 1 and n appears exactly once). After that, she can choose a sequence a = [a_1, a_2, \ldots, a_n] (1 \le a_i \le n and a_i \ne i for all 1 \le i \le n).The game has n rounds of attacks. In the i-th round, if the p_i-th bot is still in the game, it will begin its attack, and as the result the a_{p_i}-th tower becomes occupied by the p_i-th bot; the bot that previously occupied the a_{p_i}-th tower will no longer occupy it. If the p_i-th bot is not in the game, nothing will happen in this round.After each round, if a bot doesn't occupy any towers, it will be eliminated and leave the game. Please note that no tower can be occupied by more than one bot, but one bot can occupy more than one tower during the game.At the end of the game, Cirno will record the result as a sequence b = [b_1, b_2, \ldots, b_n], where b_i is the number of the bot that occupies the i-th tower at the end of the game.However, as a mathematics master, she wants you to solve the following counting problem instead of playing games:Count the number of different pairs of sequences a, b from all possible choices of sequence a and permutation p.Calculate the answers for all n such that 1 \le n \le k. Since these numbers may be large, output them modulo M.InputThe only line contains two positive integers k and M (1\le k\le 10^5, 2\le M\le 10^9 ). It is guaranteed that 2^{18} is a divisor of M-1 and M is a prime number.OutputOutput k lines, where the i-th line contains a non-negative integer, which is the answer for n=i modulo M.ExampleInput 8 998244353 Output 0 2 24 360 6800 153150 4057452 123391016 NoteFor n=1, no valid sequence a exists. We regard the answer as 0.For n=2, there is only one possible array a: [2, 1]. For array a is [2, 1] and permutation p is [1, 2], the sequence b will be [1, 1] after all rounds have finished. The details for each rounds: In the first round, the first bot will begin its attack and successfully capture the tower 2. After this round, the second bot will be eliminated and leave the game as all of its towers are occupied by other bots. In the second round, the second bot is not in the game. For array a is [2, 1] and permutation p is [2, 1], the sequence b will be [2, 2] after all rounds have finished. The details for each rounds: In the first round, the second bot will begin its attack and successfully capture the tower 1. After this round, the first bot will be eliminated and leave the game as all of its towers are occupied by other bots. In the second round, the first bot is not in the game. So the number of different pairs of sequences (a,b) is 2 ([2, 1], [1, 1] and [2, 1], [2, 2]) for n=2.
8 998244353
0 2 24 360 6800 153150 4057452 123391016
12 seconds
1024 megabytes
['combinatorics', 'fft', 'math', '*3500']
H1. Game of AI (easy version)time limit per test2.5 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputThis is the easy version of this problem. The difference between easy and hard versions is the constraint on k and the time limit. Also, in this version of the problem, you only need to calculate the answer when n=k. You can make hacks only if both versions of the problem are solved.Cirno is playing a war simulator game with n towers (numbered from 1 to n) and n bots (numbered from 1 to n). The i-th tower is initially occupied by the i-th bot for 1 \le i \le n.Before the game, Cirno first chooses a permutation p = [p_1, p_2, \ldots, p_n] of length n (A permutation of length n is an array of length n where each integer between 1 and n appears exactly once). After that, she can choose a sequence a = [a_1, a_2, \ldots, a_n] (1 \le a_i \le n and a_i \ne i for all 1 \le i \le n).The game has n rounds of attacks. In the i-th round, if the p_i-th bot is still in the game, it will begin its attack, and as the result the a_{p_i}-th tower becomes occupied by the p_i-th bot; the bot that previously occupied the a_{p_i}-th tower will no longer occupy it. If the p_i-th bot is not in the game, nothing will happen in this round.After each round, if a bot doesn't occupy any towers, it will be eliminated and leave the game. Please note that no tower can be occupied by more than one bot, but one bot can occupy more than one tower during the game.At the end of the game, Cirno will record the result as a sequence b = [b_1, b_2, \ldots, b_n], where b_i is the number of the bot that occupies the i-th tower at the end of the game.However, as a mathematics master, she wants you to solve the following counting problem instead of playing games:Count the number of different pairs of sequences a and b that we can get from all possible choices of sequence a and permutation p.Since this number may be large, output it modulo M.InputThe only line contains two positive integers k and M (1\le k\le 5000, 2\le M\le 10^9 ). It is guaranteed that 2^{18} is a divisor of M-1 and M is a prime number.You need to calculate the answer for n=k.OutputOutput a single integer — the number of different pairs of sequences for n=k modulo M.ExamplesInput 1 998244353 Output 0 Input 2 998244353 Output 2 Input 3 998244353 Output 24 Input 8 998244353 Output 123391016 NoteFor n=1, no valid sequence a exists. We regard the answer as 0.For n=2, there is only one possible array a: [2, 1]. For array a is [2, 1] and permutation p is [1, 2], the sequence b will be [1, 1] after all rounds have finished. The details for each rounds: In the first round, the first bot will begin its attack and successfully capture the tower 2. After this round, the second bot will be eliminated and leave the game as all of its towers are occupied by other bots. In the second round, the second bot is not in the game. For array a is [2, 1] and permutation p is [2, 1], the sequence b will be [2, 2] after all rounds have finished. The details for each rounds: In the first round, the second bot will begin its attack and successfully capture the tower 1. After this round, the first bot will be eliminated and leave the game as all of its towers are occupied by other bots. In the second round, the first bot is not in the game. So the number of different pairs of sequences (a,b) is 2 ([2, 1], [1, 1] and [2, 1], [2, 2]) for n=2.
1 998244353
0
2.5 seconds
1024 megabytes
['combinatorics', 'constructive algorithms', 'dp', 'fft', 'math', '*3200']
G. Mio and Lucky Arraytime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMio has an array a consisting of n integers, and an array b consisting of m integers. Mio can do the following operation to a: Choose an integer i (1 \leq i \leq n) that has not been chosen before, then add 1 to a_i, subtract 2 from a_{i+1}, add 3 to a_{i+2} an so on. Formally, the operation is to add (-1)^{j-i} \cdot (j-i+1) to a_j for i \leq j \leq n.Mio wants to transform a so that it will contain b as a subarray. Could you answer her question, and provide a sequence of operations to do so, if it is possible?An array b is a subarray of an array a if b can be obtained from a by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.InputThe 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.The first line of each test case contains one integer n (2 \leq n \leq 2 \cdot 10^5) — the number of elements in a.The second line of the test case contains n integers a_1, a_2, \cdots, a_n (-10^5 \leq a_i \leq 10^5), where a_i is the i-th element of a.The third line of the test case contains one integer m (2 \leq m \leq n) — the number of elements in b.The fourth line of the test case contains m integers b_1, b_2, \cdots, b_m (-10^{12} \leq b_i \leq 10^{12}), where b_i is the i-th element of b.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputIf it is impossible to transform a so that it contains b as a subarray, output -1.Otherwise, the first line of output should contain an integer k (0 \leq k \leq n), the number of operations to be done.The second line should contain k distinct integers, representing the operations done in order.If there are multiple solutions, you can output any.Notice that you do not need to minimize the number of operations.ExampleInput 5 5 1 2 3 4 5 5 2 0 6 0 10 5 1 2 3 4 5 3 3 5 3 8 -3 2 -3 -4 4 0 1 -2 4 10 -6 7 -7 5 1 2 3 4 5 4 1 10 1 1 5 0 0 0 0 0 2 10 12 Output 1 1 1 4 5 1 3 4 6 8 -1 -1 NoteIn the first test case, the sequence a = [1,2,3,4,5]. One of the possible solutions is doing one operation at i = 1 (add 1 to a_1, subtract 2 from a_2, add 3 to a_3, subtract 4 from a_4, add 5 to a_5). Then array a is transformed to a = [2,0,6,0,10], which contains b = [2, 0, 6, 0, 10] as a subarray.In the second test case, the sequence a = [1,2,3,4,5]. One of the possible solutions is doing one operation at i = 4 (add 1 to a_4, subtract 2 from a_5). Then array a is transformed to a = [1,2,3,5,3], which contains b = [3,5,3] as a subarray.In the third test case, the sequence a = [-3, 2, -3, -4, 4, 0, 1, -2]. One of the possible solutions is the following. Choose an integer i=8 to do the operation. Then array a is transformed to a = [-3, 2, -3, -4, 4, 0, 1, -1]. Choose an integer i=6 to do the operation. Then array a is transformed to a = [-3, 2, -3, -4, 4, 1, -1, 2]. Choose an integer i=4 to do the operation. Then array a is transformed to a = [-3, 2, -3, -3, 2, 4, -5, 7]. Choose an integer i=3 to do the operation. Then array a is transformed to a = [-3, 2, -2, -5, 5, 0, 0, 1]. Choose an integer i=1 to do the operation. Then array a is transformed to a = [-2, 0, 1, -9, 10, -6, 7, -7]. The resulting a is [-2, 0, 1, -9, 10, -6, 7, -7], which contains b = [10, -6, 7, -7] as a subarray.In the fourth test case, it is impossible to transform a so that it contains b as a subarray.In the fifth test case, it is impossible to transform a so that it contains b as a subarray.
5 5 1 2 3 4 5 5 2 0 6 0 10 5 1 2 3 4 5 3 3 5 3 8 -3 2 -3 -4 4 0 1 -2 4 10 -6 7 -7 5 1 2 3 4 5 4 1 10 1 1 5 0 0 0 0 0 2 10 12
1 1 1 4 5 1 3 4 6 8 -1 -1
5 seconds
256 megabytes
['constructive algorithms', 'fft', 'math', 'strings', '*3500']
F. Colouring Gametime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlice and Bob are playing a game. There are n cells in a row. Initially each cell is either red or blue. Alice goes first.On each turn, Alice chooses two neighbouring cells which contain at least one red cell, and paints that two cells white. Then, Bob chooses two neighbouring cells which contain at least one blue cell, and paints that two cells white. The player who cannot make a move loses.Find the winner if both Alice and Bob play optimally.Note that a chosen cell can be white, as long as the other cell satisfies the constraints.InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. Description of test cases follows.For each test case, the first line contains an integer n (2 \leq n \leq 5 \cdot 10^5) — the number of cells.The second line contains a string s of length n — the initial state of the cells. The i-th cell is red if s_i = R, blue if s_i = B.It is guaranteed that the sum of n over all test cases does not exceed 5 \cdot 10^5.OutputFor each test case, output the name of the winner on a separate line.ExampleInput 83BRB5RRBBB6RBRBRB8BBRRBRRB6BRRBRB12RBRBRBRBRRBB12RBRBRBRBBBRR4RBBROutput Bob Bob Alice Alice Alice Alice Bob Bob NoteIn the notes, the cell numbers increase from left to right.In the first testcase for Alice, she has two choices: paint the first and the second cells, or paint the second and the third cells. No matter what choice Alice makes, there will be exactly one blue cell after Alice's move. Bob just needs to paint the blue cell and its neighbour, then every cell will be white and Alice can't make a move. So Bob is the winner.In the second testcase no matter what Alice chooses, Bob can choose to paint the fourth and fifth cells in 2 turns.In the third testcase at first, Alice paints the third and the fourth cells. It doesn't matter if Bob paints the first and the second cells or the fifth and sixth cells, as Alice can paint the other two cells.In the fourth testcase at first, Alice paints the second and the third cells. If Bob paints the fifth and the sixth cells or the fourth and the fifth cells, then Alice paints the seventh and the eighth cells. If Bob paints the seventh and the eighth cells, then Alice paints the fifth and the sixth cells.In the fifth Alice chooses the middle two cells at first, then Bob obviously has only two options, whichever variant he chooses, Alice can choose the other one and win.In the eighth no matter what Alice chooses, Bob can choose the other two symmetrical cells.
83BRB5RRBBB6RBRBRB8BBRRBRRB6BRRBRB12RBRBRBRBRRBB12RBRBRBRBBBRR4RBBR
Bob Bob Alice Alice Alice Alice Bob Bob
1 second
256 megabytes
['constructive algorithms', 'dp', 'games', '*2600']
E. Count Secondstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputCirno has a DAG (Directed Acyclic Graph) with n nodes and m edges. The graph has exactly one node that has no out edges. The i-th node has an integer a_i on it.Every second the following happens: Let S be the set of nodes x that have a_x > 0. For all x \in S, 1 is subtracted from a_x, and then for each node y, such that there is an edge from x to y, 1 is added to a_y.Find the first moment of time when all a_i become 0. Since the answer can be very large, output it modulo 998\,244\,353.InputThe first line contains a single integer t (1 \leq t \leq 1000) — the number of test cases. Description of test cases follows.The first line of each test case contains two integers n, m (1 \leq n, m \leq 1000) — the number of vertices and edges in the graph.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (0 \leq a_i \leq 10^9) — the integer on vertices.Each line of the following m lines contains two integers x, y (1 \leq x, y \leq n), represent a directed edge from x to y. It is guaranteed that the graph is a DAG with no multi-edges, and there is exactly one node that has no out edges.It is guaranteed that both sum of n and sum of m over all test cases are less than or equal to 10\,000.OutputFor each test case, print an integer in a separate line — the first moment of time when all a_i become 0, modulo 998\,244\,353.ExampleInput 53 21 1 11 22 35 51 0 0 0 01 22 33 44 51 510 11998244353 0 0 0 998244353 0 0 0 0 01 22 33 44 55 66 77 88 99 101 37 95 61293 1145 9961 9961 19191 22 33 45 41 42 46 910 10 10 10 10 101 21 32 34 36 33 56 56 16 2Output 3 5 4 28010 110 NoteIn the first test case: At time 0, the values of the nodes are [1, 1, 1]. At time 1, the values of the nodes are [0, 1, 1]. At time 2, the values of the nodes are [0, 0, 1]. At time 3, the values of the nodes are [0, 0, 0].So the answer is 3. In the second test case: At time 0, the values of the nodes are [1, 0, 0, 0, 0]. At time 1, the values of the nodes are [0, 1, 0, 0, 1]. At time 2, the values of the nodes are [0, 0, 1, 0, 0]. At time 3, the values of the nodes are [0, 0, 0, 1, 0]. At time 4, the values of the nodes are [0, 0, 0, 0, 1]. At time 5, the values of the nodes are [0, 0, 0, 0, 0]. So the answer is 5.In the third test case:The first moment of time when all a_i become 0 is 6\cdot 998244353 + 4.
53 21 1 11 22 35 51 0 0 0 01 22 33 44 51 510 11998244353 0 0 0 998244353 0 0 0 0 01 22 33 44 55 66 77 88 99 101 37 95 61293 1145 9961 9961 19191 22 33 45 41 42 46 910 10 10 10 10 101 21 32 34 36 33 56 56 16 2
3 5 4 28010 110
1 second
256 megabytes
['brute force', 'constructive algorithms', 'dp', 'graphs', 'implementation', 'math', '*2200']
D. Magical Arraytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputEric has an array b of length m, then he generates n additional arrays c_1, c_2, \dots, c_n, each of length m, from the array b, by the following way:Initially, c_i = b for every 1 \le i \le n. Eric secretly chooses an integer k (1 \le k \le n) and chooses c_k to be the special array.There are two operations that Eric can perform on an array c_t: Operation 1: Choose two integers i and j (2 \leq i < j \leq m-1), subtract 1 from both c_t[i] and c_t[j], and add 1 to both c_t[i-1] and c_t[j+1]. That operation can only be used on a non-special array, that is when t \neq k.; Operation 2: Choose two integers i and j (2 \leq i < j \leq m-2), subtract 1 from both c_t[i] and c_t[j], and add 1 to both c_t[i-1] and c_t[j+2]. That operation can only be used on a special array, that is when t = k.Note that Eric can't perform an operation if any element of the array will become less than 0 after that operation.Now, Eric does the following: For every non-special array c_i (i \neq k), Eric uses only operation 1 on it at least once. For the special array c_k, Eric uses only operation 2 on it at least once.Lastly, Eric discards the array b.For given arrays c_1, c_2, \dots, c_n, your task is to find out the special array, i.e. the value k. Also, you need to find the number of times of operation 2 was used on it.InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. Description of test cases follows.The first line of each test case contains two integers n and m (3 \leq n \leq 10^5, 7 \leq m \leq 3 \cdot 10^5) — the number of arrays given to you, and the length of each array.The next n lines contains m integers each, c_{i,1}, c_{i,2}, \dots , c_{i,m}.It is guaranteed that each element of the discarded array b is in the range [0,10^6], and therefore 0 \leq c_{i,j} \leq 3 \cdot 10^{11} for all possible pairs of (i,j).It is guaranteed that the sum of n \cdot m over all test cases does not exceed 10^6.It is guaranteed that the input is generated according to the procedure above.OutputFor each test case, output one line containing two integers — the index of the special array, and the number of times that Operation 2 was performed on it. It can be shown that under the constraints given in the problem, this value is unique and won't exceed 10^{18}, so you can represent it as a 64-bit integer. It can also be shown that the index of the special array is uniquely determined.In this problem, hacks are disabled.ExampleInput 7 3 9 0 1 2 0 0 2 1 1 0 0 1 1 1 2 0 0 2 0 0 1 2 0 0 1 2 1 0 3 7 25 15 20 15 25 20 20 26 14 20 14 26 20 20 25 15 20 15 20 20 25 3 9 25 15 20 15 25 20 20 20 20 26 14 20 14 26 20 20 20 20 25 15 20 15 25 15 20 20 25 3 11 25 15 20 15 25 20 20 20 20 20 20 26 14 20 14 26 20 20 20 20 20 20 25 15 20 15 25 20 15 20 20 20 25 3 13 25 15 20 15 25 20 20 20 20 20 20 20 20 26 14 20 14 26 20 20 20 20 20 20 20 20 25 15 20 15 25 20 20 15 20 20 20 20 25 3 15 25 15 20 15 25 20 20 20 20 20 20 20 20 20 20 26 14 20 14 26 20 20 20 20 20 20 20 20 20 20 25 15 20 15 25 20 20 20 15 20 20 20 20 20 25 3 9 909459 479492 676924 224197 162866 164495 193268 742456 728277 948845 455424 731850 327890 304150 237351 251763 225845 798316 975446 401170 792914 272263 300770 242037 236619 334316 725899 Output 3 1 3 10 3 15 3 20 3 25 3 30 1 1378716 NoteIn the first test case, the secret array b is [0, 1, 1, 1, 1, 1, 1, 1, 0]. Array c_1 and array c_2 are generated by using operation 1. Array c_3 is generated by using operation 2.For Array c_1,you can choose i=4 and j=5 perform Operation 1 one time to generate it. For Array c_2, you can choose i=6 and j=7 perform Operation 1 one time to generate it. For Array c_3,you can choose i=4 and j=5 perform Operation 2 one time to generate it.In the second test case, the secret array b is [20, 20, 20, 20, 20, 20, 20]. You can also find that array c_1 and array c_2 are generated by using Operation 1. Array c_3 is generated by using Operation 2.In the third test case, the secret array b is [20, 20, 20, 20, 20, 20, 20, 20, 20]. You can also find that array c_1 and array c_2 are generated by using Operation 1. Array c_3 is generated by using Operation 2.
7 3 9 0 1 2 0 0 2 1 1 0 0 1 1 1 2 0 0 2 0 0 1 2 0 0 1 2 1 0 3 7 25 15 20 15 25 20 20 26 14 20 14 26 20 20 25 15 20 15 20 20 25 3 9 25 15 20 15 25 20 20 20 20 26 14 20 14 26 20 20 20 20 25 15 20 15 25 15 20 20 25 3 11 25 15 20 15 25 20 20 20 20 20 20 26 14 20 14 26 20 20 20 20 20 20 25 15 20 15 25 20 15 20 20 20 25 3 13 25 15 20 15 25 20 20 20 20 20 20 20 20 26 14 20 14 26 20 20 20 20 20 20 20 20 25 15 20 15 25 20 20 15 20 20 20 20 25 3 15 25 15 20 15 25 20 20 20 20 20 20 20 20 20 20 26 14 20 14 26 20 20 20 20 20 20 20 20 20 20 25 15 20 15 25 20 20 20 15 20 20 20 20 20 25 3 9 909459 479492 676924 224197 162866 164495 193268 742456 728277 948845 455424 731850 327890 304150 237351 251763 225845 798316 975446 401170 792914 272263 300770 242037 236619 334316 725899
3 1 3 10 3 15 3 20 3 25 3 30 1 1378716
2 seconds
256 megabytes
['constructive algorithms', 'hashing', 'implementation', 'math', '*1900']
C. Virustime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n houses numbered from 1 to n on a circle. For each 1 \leq i \leq n - 1, house i and house i + 1 are neighbours; additionally, house n and house 1 are also neighbours.Initially, m of these n houses are infected by a deadly virus. Each morning, Cirno can choose a house which is uninfected and protect the house from being infected permanently.Every day, the following things happen in order: Cirno chooses an uninfected house, and protect it permanently. All uninfected, unprotected houses which have at least one infected neighbor become infected. Cirno wants to stop the virus from spreading. Find the minimum number of houses that will be infected in the end, if she optimally choose the houses to protect.Note that every day Cirno always chooses a house to protect before the virus spreads. Also, a protected house will not be infected forever.InputThe input consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. Description of test cases follows.The first line of each test case consists of two positive integers n, m (5 \leq n \leq 10^9, 1 \leq m \leq \min(n, 10^5)) — the number of houses on the circle, and the number of houses that are initially infected. The second line of each test case consists of m distinct positive integers a_1, a_2, \cdots , a_m (1 \leq a_i \leq n) — the indices of the houses infected initially.It is guaranteed that the sum of m over all test cases does not exceed 10^5.OutputFor each test case, output an integer on a separate line, which is the minimum number of infected houses in the end.ExampleInput 810 33 6 86 22 520 33 7 1241 51 11 21 31 4110 52 4 6 8 105 53 2 5 4 11000000000 111000000000 41 1000000000 10 16Output 7 5 11 28 9 5 2 15 NoteIn the first test case:At the start of the first day, house 3, 6, 8 are infected. Choose house 2 to protect.At the start of the second day, house 3, 4, 5, 6, 7, 8, 9 are infected. Choose house 10 to protect.At the start of the third day, no more houses are infected.In the second test case:At the start of the first day, house 2, 5 are infected. Choose house 1 to protect.At the start of the second day, house 2, 3, 4, 5, 6 are infected. No more available houses can be protected.
810 33 6 86 22 520 33 7 1241 51 11 21 31 4110 52 4 6 8 105 53 2 5 4 11000000000 111000000000 41 1000000000 10 16
7 5 11 28 9 5 2 15
1 second
256 megabytes
['greedy', 'implementation', 'sortings', '*1200']
B. Luke is a Foodietime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLuke 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.InputThe 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.OutputFor each test case, output an integer on a separate line, which is the minimum number of changes needed.ExampleInput 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 11Output 0 1 2 1 2 4 6 NoteIn 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.
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
0 1 2 1 2 4 6
1 second
256 megabytes
['brute force', 'greedy', 'implementation', '*1000']
A. Two 0-1 Sequencestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAquaMoon has two binary sequences a and b, which contain only 0 and 1. AquaMoon can perform the following two operations any number of times (a_1 is the first element of a, a_2 is the second element of a, and so on): Operation 1: if a contains at least two elements, change a_2 to \operatorname{min}(a_1,a_2), and remove the first element of a. Operation 2: if a contains at least two elements, change a_2 to \operatorname{max}(a_1,a_2), and remove the first element of a.Note that after a removal of the first element of a, the former a_2 becomes the first element of a, the former a_3 becomes the second element of a and so on, and the length of a reduces by one.Determine if AquaMoon can make a equal to b by using these operations.InputThe first line contains a single integer t (1 \leq t \leq 2\,000) — the number of test cases. Description of test cases follows.The first line of each test case contains two integers n, m (1 \leq n,m \leq 50, m \leq n) — the lengths of a and b respectively.The second line of each test case contains a string a of length n, consisting only 0 and 1.The third line of each test case contains a string b of length m, consisting only 0 and 1.OutputFor each test case, output "YES" if AquaMoon can change a to b by using these options; otherwise, output "NO".You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as a positive answer).ExampleInput 106 2001001116 2110111016 2000001116 2111111018 510000101110107 4101000110018 6010100100100108 40101010110018 41010101001107 5101110011100Output YES YES NO NO NO YES YES NO NO YES NoteIn the first test case, you can use Operation 2 four times to make a equals to b.In the second test case, you can use Operation 1 four times to make a equals to b.In the third test case, it can be proved that no matter how we use the operations, it is impossible to make a equal to b.In the fourth test case, it can be proved that no matter how we use the operations, it is impossible to make a equal to b.In the fifth test case, you can use Operation 2 three times to make a become 10101, so the first element of a equals to the first element of b, but it can be proved that no matter how to operate, the second to the fifth elements of a can't be the same as b.
106 2001001116 2110111016 2000001116 2111111018 510000101110107 4101000110018 6010100100100108 40101010110018 41010101001107 5101110011100
YES YES NO NO NO YES YES NO NO YES
1 second
256 megabytes
['constructive algorithms', 'greedy', '*800']
G. Good Key, Bad Keytime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n chests. The i-th chest contains a_i coins. You need to open all n chests in order from chest 1 to chest n.There are two types of keys you can use to open a chest: a good key, which costs k coins to use; a bad key, which does not cost any coins, but will halve all the coins in each unopened chest, including the chest it is about to open. The halving operation will round down to the nearest integer for each chest halved. In other words using a bad key to open chest i will do a_i = \lfloor{\frac{a_i}{2}\rfloor}, a_{i+1} = \lfloor\frac{a_{i+1}}{2}\rfloor, \dots, a_n = \lfloor \frac{a_n}{2}\rfloor; any key (both good and bad) breaks after a usage, that is, it is a one-time use. You need to use in total n keys, one for each chest. Initially, you have no coins and no keys. If you want to use a good key, then you need to buy it.During the process, you are allowed to go into debt; for example, if you have 1 coin, you are allowed to buy a good key worth k=3 coins, and your balance will become -2 coins.Find the maximum number of coins you can have after opening all n chests in order from chest 1 to chest n.InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases.The first line of each test case contains two integers n and k (1 \leq n \leq 10^5; 0 \leq k \leq 10^9) — the number of chests and the cost of a good key respectively.The second line of each test case contains n integers a_i (0 \leq a_i \leq 10^9)  — the amount of coins in each chest.The sum of n over all test cases does not exceed 10^5.OutputFor each test case output a single integer  — the maximum number of coins you can obtain after opening the chests in order from chest 1 to chest n.Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).ExampleInput 54 510 10 3 11 213 1210 10 2912 515 74 89 45 18 69 67 67 11 96 23 592 5785 60Output 11 0 13 60 58 NoteIn the first test case, one possible strategy is as follows: Buy a good key for 5 coins, and open chest 1, receiving 10 coins. Your current balance is 0 + 10 - 5 = 5 coins. Buy a good key for 5 coins, and open chest 2, receiving 10 coins. Your current balance is 5 + 10 - 5 = 10 coins. Use a bad key and open chest 3. As a result of using a bad key, the number of coins in chest 3 becomes \left\lfloor \frac{3}{2} \right\rfloor = 1, and the number of coins in chest 4 becomes \left\lfloor \frac{1}{2} \right\rfloor = 0. Your current balance is 10 + 1 = 11. Use a bad key and open chest 4. As a result of using a bad key, the number of coins in chest 4 becomes \left\lfloor \frac{0}{2} \right\rfloor = 0. Your current balance is 11 + 0 = 11. At the end of the process, you have 11 coins, which can be proven to be maximal.
54 510 10 3 11 213 1210 10 2912 515 74 89 45 18 69 67 67 11 96 23 592 5785 60
11 0 13 60 58
3 seconds
256 megabytes
['bitmasks', 'brute force', 'dp', 'greedy', 'math', '*1600']
F. Yet Another Problem About Pairs Satisfying an Inequalitytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a_1, a_2, \dots a_n. Count the number of pairs of indices 1 \leq i, j \leq n such that a_i < i < a_j < j.InputThe first line contains an integer t (1 \leq t \leq 1000) — the number of test cases.The first line of each test case contains an integer n (2 \leq n \leq 2 \cdot 10^5) — the length of the array.The second line of each test case contains n integers a_1, a_2, \dots, a_n (0 \leq a_i \leq 10^9) — the elements of the array.It is guaranteed that the sum of n across all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the number of pairs of indices satisfying the condition in the statement.Please note, that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).ExampleInput 581 1 2 3 8 2 1 421 2100 2 1 6 3 4 1 2 8 321 100000000030 1000000000 2Output 3 0 10 0 1 NoteFor the first test cases the pairs are (i, j) = \{(2, 4), (2, 8), (3, 8)\}. The pair (2, 4) is true because a_2 = 1, a_4 = 3 and 1 < 2 < 3 < 4. The pair (2, 8) is true because a_2 = 1, a_8 = 4 and 1 < 2 < 4 < 8. The pair (3, 8) is true because a_3 = 2, a_8 = 4 and 2 < 3 < 4 < 8.
581 1 2 3 8 2 1 421 2100 2 1 6 3 4 1 2 8 321 100000000030 1000000000 2
3 0 10 0 1
2 seconds
256 megabytes
['binary search', 'data structures', 'dp', 'greedy', 'sortings', '*1300']
E. Mirror Gridtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a square grid with n rows and n columns. Each cell contains either 0 or 1. In an operation, you can select a cell of the grid and flip it (from 0 \to 1 or 1 \to 0). Find the minimum number of operations you need to obtain a square that remains the same when rotated 0^{\circ}, 90^{\circ}, 180^{\circ} and 270^{\circ}.The picture below shows an example of all rotations of a grid. InputThe first line contains a single integer t (1 \leq t \leq 100) — the number of test cases.The first line of each test case contains a single integer n (1 \leq n \leq 100) — the size of the grid.Then n lines follow, each with n characters a_{i,j} (0 \leq a_{i,j} \leq 1) — the number written in each cell.OutputFor each test case output a single integer  — the minimum number of operations needed to make the square look the same rotated 0^{\circ}, 90^{\circ}, 180^{\circ} and 270^{\circ}.ExampleInput 5301011001010511100110110101110011110005010001010101010000100100151100100000111111011001111Output 1 0 9 7 6 NoteIn the first test case, we can perform one operations to make the grid \begin{matrix}0 & 1 & 0\\ 1 & 1 & \color{red}{1}\\ 0 & 1 & 0\end{matrix}. Now, all rotations of the square are the same.In the second test case, all rotations of the square are already the same, so we don't need any flips.
5301011001010511100110110101110011110005010001010101010000100100151100100000111111011001111
1 0 9 7 6
2 seconds
256 megabytes
['implementation', '*1200']
D. Double Stringstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n strings s_1, s_2, \dots, s_n of length at most \mathbf{8}. For each string s_i, determine if there exist two strings s_j and s_k such that s_i = s_j + s_k. That is, s_i is the concatenation of s_j and s_k. Note that j can be equal to k.Recall that the concatenation of strings s and t is s + t = s_1 s_2 \dots s_p t_1 t_2 \dots t_q, where p and q are the lengths of strings s and t respectively. For example, concatenation of "code" and "forces" is "codeforces".InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases.The first line of each test case contains a single integer n (1 \leq n \leq 10^5) — the number of strings.Then n lines follow, the i-th of which contains non-empty string s_i of length at most \mathbf{8}, consisting of lowercase English letters. Among the given n strings, there may be equal (duplicates).The sum of n over all test cases doesn't exceed 10^5.OutputFor each test case, output a binary string of length n. The i-th bit should be \texttt{1} if there exist two strings s_j and s_k where s_i = s_j + s_k, and \texttt{0} otherwise. Note that j can be equal to k.ExampleInput 35ababababcabacbc3xxxxxx8codeforcescodescodforcforcesecodeOutput 10100 011 10100101 NoteIn the first test case, we have the following: s_1 = s_2 + s_2, since \texttt{abab} = \texttt{ab} + \texttt{ab}. Remember that j can be equal to k. s_2 is not the concatenation of any two strings in the list. s_3 = s_2 + s_5, since \texttt{abc} = \texttt{ab} + \texttt{c}. s_4 is not the concatenation of any two strings in the list. s_5 is not the concatenation of any two strings in the list. Since only s_1 and s_3 satisfy the conditions, only the first and third bits in the answer should be \texttt{1}, so the answer is \texttt{10100}.
35ababababcabacbc3xxxxxx8codeforcescodescodforcforcesecode
10100 011 10100101
2 seconds
256 megabytes
['brute force', 'data structures', 'strings', '*1100']
C. Cyphertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLuca has a cypher made up of a sequence of n wheels, each with a digit a_i written on it. On the i-th wheel, he made b_i moves. Each move is one of two types: up move (denoted by \texttt{U}): it increases the i-th digit by 1. After applying the up move on 9, it becomes 0. down move (denoted by \texttt{D}): it decreases the i-th digit by 1. After applying the down move on 0, it becomes 9. Example for n=4. The current sequence is 0 0 0 0. Luca knows the final sequence of wheels and the moves for each wheel. Help him find the original sequence and crack the cypher.InputThe first line contains a single integer t (1 \leq t \leq 100) — the number of test cases.The first line of each test case contains a single integer n (1 \leq n \leq 100) — the number of wheels.The second line contains n integers a_i (0 \leq a_i \leq 9) — the digit shown on the i-th wheel after all moves have been performed.Then n lines follow, the i-th of which contains the integer b_i (1 \leq b_i \leq 10) and b_i characters that are either \texttt{U} or \texttt{D} — the number of moves performed on the i-th wheel, and the moves performed. \texttt{U} and \texttt{D} represent an up move and a down move respectively.OutputFor each test case, output n space-separated digits  — the initial sequence of the cypher.ExampleInput 339 3 13 DDD4 UDUU2 DU20 99 DDDDDDDDD9 UUUUUUUUU50 5 9 8 310 UUUUUUUUUU3 UUD8 UUDUUDDD10 UUDUUDUDDU4 UUUUOutput 2 1 1 9 0 0 4 9 6 9 NoteIn the first test case, we can prove that initial sequence was [2,1,1]. In that case, the following moves were performed: On the first wheel: 2 \xrightarrow[\texttt{D}]{} 1 \xrightarrow[\texttt{D}]{} 0 \xrightarrow[\texttt{D}]{} 9. On the second wheel: 1 \xrightarrow[\texttt{U}]{} 2 \xrightarrow[\texttt{D}]{} 1 \xrightarrow[\texttt{U}]{} 2 \xrightarrow[\texttt{U}]{} 3. On the third wheel: 1 \xrightarrow[\texttt{D}]{} 0 \xrightarrow[\texttt{U}]{} 1. The final sequence was [9,3,1], which matches the input.
339 3 13 DDD4 UDUU2 DU20 99 DDDDDDDDD9 UUUUUUUUU50 5 9 8 310 UUUUUUUUUU3 UUD8 UUDUUDDD10 UUDUUDUDDU4 UUUU
2 1 1 9 0 0 4 9 6 9
1 second
256 megabytes
['brute force', 'implementation', 'strings', '*800']
B. ICPC Balloonstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn an ICPC contest, balloons are distributed as follows: Whenever a team solves a problem, that team gets a balloon. The first team to solve a problem gets an additional balloon. A contest has 26 problems, labelled \textsf{A}, \textsf{B}, \textsf{C}, ..., \textsf{Z}. You are given the order of solved problems in the contest, denoted as a string s, where the i-th character indicates that the problem s_i has been solved by some team. No team will solve the same problem twice.Determine the total number of balloons that the teams received. Note that some problems may be solved by none of the teams.InputThe first line of the input contains an integer t (1 \leq t \leq 100) — the number of testcases.The first line of each test case contains an integer n (1 \leq n \leq 50) — the length of the string.The second line of each test case contains a string s of length n consisting of uppercase English letters, denoting the order of solved problems.OutputFor each test case, output a single integer — the total number of balloons that the teams received.ExampleInput 63ABA1A3ORZ5BAAAA4BKPT10CODEFORCESOutput 5 2 6 7 8 17 NoteIn the first test case, 5 balloons are given out: Problem \textsf{A} is solved. That team receives 2 balloons: one because they solved the problem, an an additional one because they are the first team to solve problem \textsf{A}. Problem \textsf{B} is solved. That team receives 2 balloons: one because they solved the problem, an an additional one because they are the first team to solve problem \textsf{B}. Problem \textsf{A} is solved. That team receives only 1 balloon, because they solved the problem. Note that they don't get an additional balloon because they are not the first team to solve problem \textsf{A}. The total number of balloons given out is 2+2+1=5.In the second test case, there is only one problem solved. The team who solved it receives 2 balloons: one because they solved the problem, an an additional one because they are the first team to solve problem \textsf{A}.
63ABA1A3ORZ5BAAAA4BKPT10CODEFORCES
5 2 6 7 8 17
1 second
256 megabytes
['data structures', 'implementation', '*800']
A. YES or YES?time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a string s of length 3, consisting of uppercase and lowercase English letters. Check if it is equal to "YES" (without quotes), where each letter can be in any case. For example, "yES", "Yes", "yes" are all allowable.InputThe first line of the input contains an integer t (1 \leq t \leq 10^3) — the number of testcases.The description of each test consists of one line containing one string s consisting of three characters. Each character of s is either an uppercase or lowercase English letter.OutputFor each test case, output "YES" (without quotes) if s satisfies the condition, and "NO" (without quotes) otherwise.You can output "YES" and "NO" in any case (for example, strings "yES", "yes" and "Yes" will be recognized as a positive response).ExampleInput 10YESyESyesYesYeSNooorZyEzYasXESOutput YES YES YES YES YES NO NO NO NO NO NoteThe first five test cases contain the strings "YES", "yES", "yes", "Yes", "YeS". All of these are equal to "YES", where each character is either uppercase or lowercase.
10YESyESyesYesYeSNooorZyEzYasXES
YES YES YES YES YES NO NO NO NO NO
1 second
256 megabytes
['brute force', 'implementation', 'strings', '*800']
G2. Passable Paths (hard version)time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is a hard version of the problem. The only difference between an easy and a hard version is in the number of queries.Polycarp grew a tree from n vertices. We remind you that a tree of n vertices is an undirected connected graph of n vertices and n-1 edges that does not contain cycles.He calls a set of vertices passable if there is such a path in the tree that passes through each vertex of this set without passing through any edge twice. The path can visit other vertices (not from this set).In other words, a set of vertices is called passable if there is a simple path that passes through all the vertices of this set (and possibly some other).For example, for a tree below sets \{3, 2, 5\}, \{1, 5, 4\}, \{1, 4\} are passable, and \{1, 3, 5\}, \{1, 2, 3, 4, 5\} are not. Polycarp asks you to answer q queries. Each query is a set of vertices. For each query, you need to determine whether the corresponding set of vertices is passable.InputThe first line of input contains a single integer n (1 \le n \le 2 \cdot 10^5) — number of vertices.Following n - 1 lines a description of the tree..Each line contains two integers u and v (1 \le u, v \le n, u \ne v) — indices of vertices connected by an edge.Following line contains single integer q (1 \le q \le 10^5) — number of queries.The following 2 \cdot q lines contain descriptions of sets.The first line of the description contains an integer k (1 \le k \le n) — the size of the set.The second line of the description contains k of distinct integers p_1, p_2, \dots, p_k (1 \le p_i \le n) — indices of the vertices of the set.It is guaranteed that the sum of k values for all queries does not exceed 2 \cdot 10^5.OutputOutput q lines, each of which contains the answer to the corresponding query. As an answer, output "YES" if the set is passable, and "NO" otherwise.You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).ExamplesInput 5 1 2 2 3 2 4 4 5 5 3 3 2 5 5 1 2 3 4 5 2 1 4 3 1 3 5 3 1 5 4 Output YES NO YES NO YES Input 5 1 2 3 2 2 4 5 2 4 2 3 1 3 3 4 5 3 2 3 5 1 1 Output YES NO YES YES
5 1 2 2 3 2 4 4 5 5 3 3 2 5 5 1 2 3 4 5 2 1 4 3 1 3 5 3 1 5 4
YES NO YES NO YES
3 seconds
256 megabytes
['data structures', 'dfs and similar', 'trees', '*2000']
G1. Passable Paths (easy version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an easy version of the problem. The only difference between an easy and a hard version is in the number of queries.Polycarp grew a tree from n vertices. We remind you that a tree of n vertices is an undirected connected graph of n vertices and n-1 edges that does not contain cycles.He calls a set of vertices passable if there is such a path in the tree that passes through each vertex of this set without passing through any edge twice. The path can visit other vertices (not from this set).In other words, a set of vertices is called passable if there is a simple path that passes through all the vertices of this set (and possibly some other).For example, for a tree below sets \{3, 2, 5\}, \{1, 5, 4\}, \{1, 4\} are passable, and \{1, 3, 5\}, \{1, 2, 3, 4, 5\} are not. Polycarp asks you to answer q queries. Each query is a set of vertices. For each query, you need to determine whether the corresponding set of vertices is passable.InputThe first line of input contains a single integer n (1 \le n \le 2 \cdot 10^5) — number of vertices.Following n - 1 lines a description of the tree..Each line contains two integers u and v (1 \le u, v \le n, u \ne v) — indices of vertices connected by an edge.Following line contains single integer q (1 \le q \le 5) — number of queries.The following 2 \cdot q lines contain descriptions of sets.The first line of the description contains an integer k (1 \le k \le n) — the size of the set.The second line of the description contains k of distinct integers p_1, p_2, \dots, p_k (1 \le p_i \le n) — indices of the vertices of the set.It is guaranteed that the sum of k values for all queries does not exceed 2 \cdot 10^5.OutputOutput q lines, each of which contains the answer to the corresponding query. As an answer, output "YES" if the set is passable, and "NO" otherwise.You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer).ExamplesInput 5 1 2 2 3 2 4 4 5 5 3 3 2 5 5 1 2 3 4 5 2 1 4 3 1 3 5 3 1 5 4 Output YES NO YES NO YES Input 5 1 2 3 2 2 4 5 2 4 2 3 1 3 3 4 5 3 2 3 5 1 1 Output YES NO YES YES
5 1 2 2 3 2 4 4 5 5 3 3 2 5 5 1 2 3 4 5 2 1 4 3 1 3 5 3 1 5 4
YES NO YES NO YES
2 seconds
256 megabytes
['dfs and similar', 'trees', '*1900']
F. Equate Multisetstime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMultiset —is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. Two multisets are equal when each value occurs the same number of times. For example, the multisets \{2,2,4\} and \{2,4,2\} are equal, but the multisets \{1,2,2\} and \{1,1,2\} — are not.You are given two multisets a and b, each consisting of n integers.In a single operation, any element of the b multiset can be doubled or halved (rounded down). In other words, you have one of the following operations available for an element x of the b multiset: replace x with x \cdot 2, or replace x with \lfloor \frac{x}{2} \rfloor (round down). Note that you cannot change the elements of the a multiset.See if you can make the multiset b become equal to the multiset a in an arbitrary number of operations (maybe 0).For example, if n = 4, a = \{4, 24, 5, 2\}, b = \{4, 1, 6, 11\}, then the answer is yes. We can proceed as follows: Replace 1 with 1 \cdot 2 = 2. We get b = \{4, 2, 6, 11\}. Replace 11 with \lfloor \frac{11}{2} \rfloor = 5. We get b = \{4, 2, 6, 5\}. Replace 6 with 6 \cdot 2 = 12. We get b = \{4, 2, 12, 5\}. Replace 12 with 12 \cdot 2 = 24. We get b = \{4, 2, 24, 5\}. Got equal multisets a = \{4, 24, 5, 2\} and b = \{4, 2, 24, 5\}. InputThe first line of input data contains a single integer t (1 \le t \le 10^4) —the number of test cases.Each test case consists of three lines.The first line of the test case contains an integer n (1 \le n \le 2 \cdot 10^5) —the number of elements in the multisets a and b.The second line gives n integers: a_1, a_2, \dots, a_n (1 \le a_1 \le a_2 \le \dots \le a_n \le 10^9) —the elements of the multiset a. Note that the elements may be equal.The third line contains n integers: b_1, b_2, \dots, b_n (1 \le b_1 \le b_2 \le \dots \le b_n \le 10^9) — elements of the multiset b. Note that the elements may be equal.It is guaranteed that the sum of n values over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, print on a separate line: YES if you can make the multiset b become equal to a, NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive answer).ExampleInput 542 4 5 241 4 6 1131 4 174 5 3154 7 10 13 142 14 14 26 4252 2 4 4 428 46 62 71 9861 2 10 16 64 8020 43 60 74 85 99Output YES NO YES YES YES NoteThe first example is explained in the statement.In the second example, it is impossible to get the value 31 from the numbers of the multiset b by available operations.In the third example, we can proceed as follows: Replace 2 with 2 \cdot 2 = 4. We get b = \{4, 14, 14, 26, 42\}. Replace 14 with \lfloor \frac{14}{2} \rfloor = 7. We get b = \{4, 7, 14, 26, 42\}. Replace 26 with \lfloor \frac{26}{2} \rfloor = 13. We get b = \{4, 7, 14, 13, 42\}. Replace 42 with \lfloor \frac{42}{2} \rfloor = 21. We get b = \{4, 7, 14, 13, 21\}. Replace 21 with \lfloor \frac{21}{2} \rfloor = 10. We get b = \{4, 7, 14, 13, 10\}. Got equal multisets a = \{4, 7, 10, 13, 14\} and b = \{4, 7, 14, 13, 10\}.
542 4 5 241 4 6 1131 4 174 5 3154 7 10 13 142 14 14 26 4252 2 4 4 428 46 62 71 9861 2 10 16 64 8020 43 60 74 85 99
YES NO YES YES YES
4 seconds
256 megabytes
['constructive algorithms', 'data structures', 'greedy', 'math', 'number theory', '*1700']