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. Music Festivaltime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard output The boy Vitya loves to listen to music very much. He knows that n albums are due to be released this Friday, i-th of which contains k_i tracks. Of course, Vitya has already listened to all the tracks, and knows that in the i-th album, the coolness of the j-th track is equal to a_{i,j}.Vitya has a friend Masha, whom he really wants to invite to the festival, where his favorite bands perform. However, in order for a friend to agree, she must first evaluate the released novelties. Vitya knows that if Masha listens to a track that was cooler than all the previous ones, she will get 1 unit of impression. Unfortunately, albums can only be listened to in their entirety, without changing the songs in them in places.Help Vitya find such an order of albums so that Masha's impression turns out to be as much as possible, and she definitely went to the festival with him. InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 200\,000) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer n (1 \le n \le 200\,000) — number of albums.The album descriptions follow. Each album description consists of two lines:The first line contains a single integer k_i (1 \le k_i \le 200\,000) — the number of tracks in the ith album.The following line contains k_i integers a_{i, 1},\ a_{i, 2},\ a_{i, 3},\ \ldots,\ a_{i, k_i} (1 \le a_{i,j} \le 200\,000) — the coolness of the tracks in the i album. Denote for \sum k_i the sum of all k_i. It is guaranteed that \sum k_i \le 200\,000. OutputFor each test case print the singular number  — the maximum impression that Masha can get. Example Input 2454 9 4 6 81728 611423 421 822 827 9Output 4 4 NoteIn the first test example, the optimal order is listening to the 4th, 2nd, 3rd and 1st albums. In this case, Masha will listen to the tracks in the following order: 1; 7; 8, 6; 4, 9, 4, 6, 8 and will receive 4 units of impression.In the second test example, you must first listen to the 1st, then the 4th, and in any order the 2nd and 3rd. In this case, Masha will get the maximum impression, and for every song in the 1st and 4th albums and nothing for the 2nd and 3rd.
2454 9 4 6 81728 611423 421 822 827 9
4 4
1 second
512 megabytes
['binary search', 'data structures', 'dp', 'greedy', 'sortings', '*1900']
B. Buying giftstime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard output Little Sasha has two friends, whom he wants to please with gifts on the Eighth of March. To do this, he went to the largest shopping center in the city.There are n departments in the mall, each of which has exactly two stores. For convenience, we number the departments with integers from 1 to n. It is known that gifts in the first store of the i department cost a_i rubles, and in the second store of the i department — b_i rubles.Entering the mall, Sasha will visit each of the n departments of the mall, and in each department, he will enter exactly one store. When Sasha gets into the i-th department, he will perform exactly one of two actions: Buy a gift for the first friend, spending a_i rubles on it. Buy a gift for the second friend, spending b_i rubles on it. Sasha is going to buy at least one gift for each friend. Moreover, he wants to pick up gifts in such a way that the price difference of the most expensive gifts bought for friends is as small as possible so that no one is offended.More formally: let m_1  be the maximum price of a gift bought to the first friend, and m_2  be the maximum price of a gift bought to the second friend. Sasha wants to choose gifts in such a way as to minimize the value of \lvert m_1 - m_2 \rvert. InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 1\,000). The description of the test cases follows.The first line of each test case contains a single integer n (2 \le n \le 500\,000) — the number of departments in the mall.Each of the following n lines of each test case contains two integers a_i and b_i (0 \le a_i, b_i \le 10^9) — the prices of gifts in the first and second store of the i department, respectively.It is guaranteed that the sum of n over all test cases does not exceed 500\,000. OutputPrint one integer — the minimum price difference of the most expensive gifts bought to friends. Example Input 221 22 151 52 73 34 102 5Output 0 1 NoteIn the first test case, Sasha has two possible options: buy a gift for the first friend in the first department, and the second friend  — in the second department, or vice versa. In the first case, m_1 = m_2 = 1, and in the second case — m_1 = m_2 = 2. In both cases, the answer is 0. In the second test case, you can buy gifts for the first friend in the 2, 4 and 5 departments, and for the second friend  — in the 1 and 3 departments.So m_1 = \max(2, 4, 2) = 4, m_2 = \max(5, 3) = 5. The answer is \lvert 4 - 5 \rvert = 1.
221 22 151 52 73 34 102 5
0 1
3 seconds
512 megabytes
['data structures', 'greedy', 'sortings', '*1800']
A. The Very Beautiful Blankettime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputKirill wants to weave the very beautiful blanket consisting of n \times m of the same size square patches of some colors. He matched some non-negative integer to each color. Thus, in our problem, the blanket can be considered a B matrix of size n \times m consisting of non-negative integers.Kirill considers that the blanket is very beautiful, if for each submatrix A of size 4 \times 4 of the matrix B is true: A_{11} \oplus A_{12} \oplus A_{21} \oplus A_{22} = A_{33} \oplus A_{34} \oplus A_{43} \oplus A_{44}, A_{13} \oplus A_{14} \oplus A_{23} \oplus A_{24} = A_{31} \oplus A_{32} \oplus A_{41} \oplus A_{42},where \oplus means bitwise exclusive ORKirill asks you to help her weave a very beautiful blanket, and as colorful as possible!He gives you two integers n and m. Your task is to generate a matrix B of size n \times m, which corresponds to a very beautiful blanket and in which the number of different numbers maximized.InputThe first line of input data contains one integer number t (1 \le t \le 1000) — the number of test cases.The single line of each test case contains two integers n and m (4 \le n, \, m \le 200) — the size of matrix B.It is guaranteed that the sum of n \cdot m does not exceed 2 \cdot 10^5.OutputFor each test case, in first line output one integer cnt (1 \le cnt \le n \cdot m) — the maximum number of different numbers in the matrix.Then output the matrix B (0 \le B_{ij} < 2^{63}) of size n \times m. If there are several correct matrices, it is allowed to output any one.It can be shown that if there exists a matrix with an optimal number of distinct numbers, then there exists among suitable matrices such a B that (0 \le B_{ij} < 2^{63}).ExampleInput 45 54 44 66 6Output 25 9740 1549 9744 1553 9748 1550 1551 1554 1555 1558 10252 2061 10256 2065 10260 2062 2063 2066 2067 2070 10764 2573 10768 2577 10772 16 3108 3109 3112 3113 3110 3111 3114 3115 3620 3621 3624 3625 3622 3623 3626 3627 24 548 549 552 553 556 557 550 551 554 555 558 559 1060 1061 1064 1065 1068 1069 1062 1063 1066 1067 1070 1071 36 25800 25801 25804 25805 25808 25809 25802 4294993099 25806 4294993103 25810 4294993107 26312 26313 26316 26317 26320 26321 26314 4294993611 26318 4294993615 26322 4294993619 26824 26825 26828 26829 26832 26833 26826 4294994123 26830 4294994127 26834 4294994131 NoteIn the first test case, there is only 4 submatrix of size 4 \times 4. Consider a submatrix whose upper-left corner coincides with the upper-left corner of the matrix B: \left[ {\begin{array}{cccc} 9740 & 1549 & 9744 & 1553 \\ 1550 & 1551 & 1554 & 1555 \\ 10252 & 2061 & 10256 & 2065 \\ 2062 & 2063 & 2066 & 2067 \\ \end{array} } \right] 9740 \oplus 1549 \oplus 1550 \oplus 1551 = 10256 \oplus 2065 \oplus 2066 \oplus 2067 = 8192;10252 \oplus 2061 \oplus 2062 \oplus 2063 = 9744 \oplus 1553 \oplus 1554 \oplus 1555 = 8192.So, chosen submatrix fits the condition. Similarly, you can make sure that the other three submatrices also fit the condition.
45 54 44 66 6
25 9740 1549 9744 1553 9748 1550 1551 1554 1555 1558 10252 2061 10256 2065 10260 2062 2063 2066 2067 2070 10764 2573 10768 2577 10772 16 3108 3109 3112 3113 3110 3111 3114 3115 3620 3621 3624 3625 3622 3623 3626 3627 24 548 549 552 553 556 557 550 551 554 555 558 559 1060 1061 1064 1065 1068 1069 1062 1063 1066 1067 1070 1071 36 25800 25801 25804 25805 25808 25809 25802 4294993099 25806 4294993103 25810 4294993107 26312 26313 26316 26317 26320 26321 26314 4294993611 26318 4294993615 26322 4294993619 26824 26825 26828 26829 26832 26833 26826 4294994123 26830 4294994127 26834 4294994131
1 second
256 megabytes
['bitmasks', 'constructive algorithms', '*1600']
G. Symmetreetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputKid was gifted a tree of n vertices with the root in the vertex 1. Since he really like symmetrical objects, Kid wants to find out if this tree is symmetrical. For example, the trees in the picture above are symmetrical. And the trees in this picture are not symmetrical. Formally, a tree is symmetrical if there exists an order of children such that: The subtree of the leftmost child of the root is a mirror image of the subtree of the rightmost child; the subtree of the second-left child of the root is a mirror image of the subtree of the second-right child of the root; ... if the number of children of the root is odd, then the subtree of the middle child should be symmetrical. InputThe first line of input data contains single integer t (1 \le t \le 10^4) — the number of test cases in the test.The first line of each case contains an integer n (1 \le n \le 2 \cdot 10^5) — the number of vertices in the tree.The following n-1 lines contain two integers each u and v (1 \le u, v \le n, u \neq v) — indices of vertices connected by an edge.It is guaranteed that the sum of n over all cases does not exceed 2 \cdot 10^5.OutputOutput t strings, each of which is the answer to the corresponding test case. As an answer, output "YES" if this tree is symmetrical, 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).ExampleInput 661 51 61 22 32 471 51 33 61 44 74 291 22 42 33 51 77 67 88 9102 99 102 36 74 31 23 82 56 5103 28 109 74 28 22 14 56 55 71Output YES NO YES NO NO YES
661 51 61 22 32 471 51 33 61 44 74 291 22 42 33 51 77 67 88 9102 99 102 36 74 31 23 82 56 5103 28 109 74 28 22 14 56 55 71
YES NO YES NO NO YES
2 seconds
256 megabytes
['dfs and similar', 'hashing', 'implementation', 'trees', '*2200']
F. Dasha and Nightmarestime limit per test4 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputDasha, an excellent student, is studying at the best mathematical lyceum in the country. Recently, a mysterious stranger brought n words consisting of small latin letters s_1, s_2, \ldots, s_n to the lyceum. Since that day, Dasha has been tormented by nightmares.Consider some pair of integers \langle i, j \rangle (1 \le i \le j \le n). A nightmare is a string for which it is true: It is obtained by concatenation s_{i}s_{j}; Its length is odd; The number of different letters in it is exactly 25; The number of occurrences of each letter that is in the word is odd. For example, if s_i= "abcdefg" and s_j= "ijklmnopqrstuvwxyz", the pair \langle i, j \rangle creates a nightmare.Dasha will stop having nightmares if she counts their number. There are too many nightmares, so Dasha needs your help. Count the number of different nightmares.Nightmares are called different if the corresponding pairs \langle i, j \rangle are different. The pairs \langle i_1, j_1 \rangle and \langle i_2, j_2 \rangle are called different if i_1 \neq i_2 or j_1 \neq j_2.InputThe first line contains a single integer n (1 \le n \le 2 \cdot 10^5) — the number of words.The following n lines contain the words s_1, s_2, \ldots, s_n, consisting of small latin letters.It is guaranteed that the total length of words does not exceed 5 \cdot 10^6.OutputPrint a single integer — the number of different nightmares.ExampleInput 10ftlabcdefghijklmnopqrstuvwxyabcdeffghijkllmnopqrsttuvwxyffftlaabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyythedevidbcdefghhiiiijklmnopqrsuwxyzgorillasilverbackabcdefgijklmnopqrstuvwxyzOutput 5 NoteIn the first test, nightmares are created by pairs \langle 1, 3 \rangle, \langle 2, 5 \rangle, \langle 3, 4 \rangle, \langle 6, 7 \rangle, \langle 9, 10 \rangle.
10ftlabcdefghijklmnopqrstuvwxyabcdeffghijkllmnopqrsttuvwxyffftlaabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyythedevidbcdefghhiiiijklmnopqrsuwxyzgorillasilverbackabcdefgijklmnopqrstuvwxyz
5
4 seconds
512 megabytes
['bitmasks', 'hashing', 'meet-in-the-middle', 'strings', '*1900']
E2. Unforgivable Curse (hard version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is a complex version of the problem. This version has no additional restrictions on the number k.The chief wizard of the Wizengamot once caught the evil wizard Drahyrt, but the evil wizard has returned and wants revenge on the chief wizard. So he stole spell s from his student Harry.The spell — is a n-length string of lowercase Latin letters.Drahyrt wants to replace spell with an unforgivable curse — string t.Dragirt, using ancient magic, can swap letters at a distance k or k+1 in spell as many times as he wants. In other words, Drahyrt can change letters in positions i and j in spell s if |i-j|=k or |i-j|=k+1.For example, if k = 3, s = "talant" and t = "atltna", Drahyrt can act as follows: swap the letters at positions 1 and 4 to get spell "aaltnt". swap the letters at positions 2 and 6 to get spell "atltna". You are given spells s and t. Can Drahyrt change spell s to t?InputThe first line of input gives a single integer T (1 \le T \le 10^4) — the number of test cases in the test.Descriptions of the test cases are follow.The first line contains two integers n, k (1 \le n \le 2 \cdot 10^5, 1 \le k \le 2 \cdot 10^5) — the length spells and the number k such that Drahyrt can change letters in a spell at a distance k or k+1.The second line gives spell s — a string of length n consisting of lowercase Latin letters.The third line gives spell t — a string of length n consisting of lowercase Latin letters.It is guaranteed that the sum of n values over all test cases does not exceed 2 \cdot 10^5. Note that there is no limit on the sum of k values over all test cases.OutputFor each test case, output on a separate line "YES" if Drahyrt can change spell s to t and "NO" otherwise.You can output the answer in any case (for example, lines "yEs", "yes", "Yes" and "YES" will be recognized as positive answer).ExampleInput 76 3talantatltna7 1abacabaaaaabbc12 6abracadabraaavadakedavra5 3acciocicao5 4lumosmolus4 3uwjttwju4 3kvpxvxpkOutput YES YES NO YES NO YES NO NoteThe first case is explained in the condition.In the second case, we can swap adjacent letters, so we can sort the string using bubble sorting, for example.In the third case, we can show that from the string s we cannot get the string t by swapping letters at a distance of 6 or 7.In the fourth case, for example, the following sequence of transformations is appropriate: "accio" \rightarrow "aocic" \rightarrow "cocia" \rightarrow "iocca" \rightarrow "aocci" \rightarrow "aicco" \rightarrow "cicao" In the fifth case, we can show that it is impossible to get the string s from the string t.In the sixth example, it is enough to swap the two outermost letters.
76 3talantatltna7 1abacabaaaaabbc12 6abracadabraaavadakedavra5 3acciocicao5 4lumosmolus4 3uwjttwju4 3kvpxvxpk
YES YES NO YES NO YES NO
1 second
256 megabytes
['brute force', 'constructive algorithms', 'dfs and similar', 'dsu', 'graphs', 'greedy', 'strings', '*1500']
E1. Unforgivable Curse (easy version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an easy version of the problem. In this version, k is always 3.The chief wizard of the Wizengamot once caught the evil wizard Drahyrt, but the evil wizard has returned and wants revenge on the chief wizard. So he stole spell s from his student Harry.The spell — is a n-length string of lowercase Latin letters.Drahyrt wants to replace spell with an unforgivable curse — string t.Drahyrt, using ancient magic, can swap letters at a distance k or k+1 in spell as many times as he wants. In this version of the problem, you can swap letters at a distance of 3 or 4. In other words, Drahyrt can change letters in positions i and j in spell s if |i-j|=3 or |i-j|=4.For example, if s = "talant" and t = "atltna", Drahyrt can act as follows: swap the letters at positions 1 and 4 to get spell "aaltnt". swap the letters at positions 2 and 6 to get spell "atltna". You are given spells s and t. Can Drahyrt change spell s to t?InputThe first line of input gives a single integer T (1 \le T \le 10^4) — the number of test cases in the test.Descriptions of the test cases are follow.The first line contains two integers n, k (1 \le n \le 2 \cdot 10^5, k = 3) — the length spells and the number k such that Drahyrt can change letters in a spell at a distance k or k+1.The second line gives spell s — a string of length n consisting of lowercase Latin letters.The third line gives spell t — a string of length n consisting of lowercase Latin letters.It is guaranteed that the sum of n values over all test cases does not exceed 2 \cdot 10^5. Note that there is no limit on the sum of k values over all test cases.OutputFor each test case, output on a separate line "YES" if Drahyrt can change spell s to t and "NO" otherwise.You can output the answer in any case (for example, lines "yEs", "yes", "Yes" and "YES" will be recognized as positive answer).ExampleInput 76 3talantatltna7 3abacabaaaaabbc12 3abracadabraaavadakedavra5 3acciocicao5 3lumosmolus4 3uwjttwju4 3kvpxvxpkOutput YES YES NO YES NO YES NO NoteThe first example is explained in the condition.In the second example we can proceed as follows: Swap the letters at positions 2 and 5 (distance 3), then we get the spell "aaacbba". Swap the letters at positions 4 and 7 (distance 3), then you get the spell "aaaabbc". In the third example, we can show that it is impossible to get the string t from the string s by swapping the letters at a distance of 3 or 4.In the fourth example, for example, the following sequence of transformations is appropriate: "accio" \rightarrow "aocic" \rightarrow "cocia" \rightarrow "iocca" \rightarrow "aocci" \rightarrow "aicco" \rightarrow "cicao" In the fifth example, you can show that it is impossible to get the string s from the string t.In the sixth example, it is enough to swap the two outermost letters.
76 3talantatltna7 3abacabaaaaabbc12 3abracadabraaavadakedavra5 3acciocicao5 3lumosmolus4 3uwjttwju4 3kvpxvxpk
YES YES NO YES NO YES NO
1 second
256 megabytes
['brute force', 'constructive algorithms', 'dsu', 'graphs', 'greedy', 'strings', '*1400']
D. Remove Two Letterstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputDmitry has a string s, consisting of lowercase Latin letters.Dmitry decided to remove two consecutive characters from the string s and you are wondering how many different strings can be obtained after such an operation.For example, Dmitry has a string "aaabcc". You can get the following different strings: "abcc"(by deleting the first two or second and third characters), "aacc"(by deleting the third and fourth characters),"aaac"(by deleting the fourth and the fifth character) and "aaab" (by deleting the last two).InputThe first line of input data contains a single integer t (1 \le t \le 10^4) — number of test cases. The descriptions of the test cases follow.The first line of the description of each test case contains an integer n (3 \le n \le 2 \cdot 10^5).The second line of the description of each test case contains a string s of length n consisting of lowercase Latin letters.It is guaranteed that the sum of n for all test cases does not exceed 2 \cdot 10^5.OutputFor each test case print one integer — the number of distinct strings that can be obtained by removing two consecutive letters.ExampleInput 76aaabcc10aaaaaaaaaa6abcdef7abacaba6cccfff4abba5ababaOutput 4 1 5 3 3 3 1 NoteThe first example is explained in the statement.In the third example, the following strings are obtained: "cdef", "adef", "abef", "abcf", "abcd".In the seventh example, any deletion will result in the string "aba".
76aaabcc10aaaaaaaaaa6abcdef7abacaba6cccfff4abba5ababa
4 1 5 3 3 3 1
2 seconds
256 megabytes
['data structures', 'greedy', 'hashing', 'strings', '*1200']
C2. Powering the Hero (hard version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is a hard version of the problem. It differs from the easy one only by constraints on n and t.There is a deck of n cards, each of which is characterized by its power. There are two types of cards: a hero card, the power of such a card is always equal to 0; a bonus card, the power of such a card is always positive. You can do the following with the deck: take a card from the top of the deck; if this card is a bonus card, you can put it on top of your bonus deck or discard; if this card is a hero card, then the power of the top card from your bonus deck is added to his power (if it is not empty), after that the hero is added to your army, and the used bonus discards. Your task is to use such actions to gather an army with the maximum possible total power.InputThe first line of input data contains single integer t (1 \le t \le 10^4) — the number of test cases in the test.The first line of each test case contains one integer n (1 \le n \le 2 \cdot 10^5) — the number of cards in the deck.The second line of each test case contains n integers s_1, s_2, \dots, s_n (0 \le s_i \le 10^9) — card powers in top-down order.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputOutput t numbers, each of which is the answer to the corresponding test case — the maximum possible total power of the army that can be achieved.ExampleInput 553 3 3 0 060 3 3 0 0 371 2 3 0 4 5 071 2 5 0 4 3 053 1 0 0 4Output 6 6 8 9 4 NoteIn the first sample, you can take bonuses 1 and 2. Both hero cards will receive 3 power. If you take all the bonuses, one of them will remain unused.In the second sample, the hero's card on top of the deck cannot be powered up, and the rest can be powered up with 2 and 3 bonuses and get 6 total power.In the fourth sample, you can take bonuses 1, 2, 3, 5 and skip the bonus 6, then the hero 4 will be enhanced with a bonus 3 by 5, and the hero 7 with a bonus 5 by 4. 4+5=9.
553 3 3 0 060 3 3 0 0 371 2 3 0 4 5 071 2 5 0 4 3 053 1 0 0 4
6 6 8 9 4
2 seconds
256 megabytes
['data structures', 'greedy', '*1100']
C1. Powering the Hero (easy version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an easy version of the problem. It differs from the hard one only by constraints on n and t.There is a deck of n cards, each of which is characterized by its power. There are two types of cards: a hero card, the power of such a card is always equal to 0; a bonus card, the power of such a card is always positive. You can do the following with the deck: take a card from the top of the deck; if this card is a bonus card, you can put it on top of your bonus deck or discard; if this card is a hero card, then the power of the top card from your bonus deck is added to his power (if it is not empty), after that the hero is added to your army, and the used bonus discards. Your task is to use such actions to gather an army with the maximum possible total power.InputThe first line of input data contains single integer t (1 \le t \le 1000) — the number of test cases in the test.The first line of each test case contains one integer n (1 \le n \le 5000) — the number of cards in the deck.The second line of each test case contains n integers s_1, s_2, \dots, s_n (0 \le s_i \le 10^9) — card powers in top-down order.It is guaranteed that the sum of n over all test cases does not exceed 5000.OutputOutput t numbers, each of which is the answer to the corresponding test case — the maximum possible total power of the army that can be achieved.ExampleInput 553 3 3 0 060 3 3 0 0 371 2 3 0 4 5 071 2 5 0 4 3 053 1 0 0 4Output 6 6 8 9 4 NoteIn the first sample, you can take bonuses 1 and 2. Both hero cards will receive 3 power. If you take all the bonuses, one of them will remain unused.In the second sample, the hero's card on top of the deck cannot be powered up, and the rest can be powered up with 2 and 3 bonuses and get 6 total power.In the fourth sample, you can take bonuses 1, 2, 3, 5 and skip the bonus 6, then the hero 4 will be enhanced with a bonus 3 by 5, and the hero 7 with a bonus 5 by 4. 4+5=9.
553 3 3 0 060 3 3 0 0 371 2 3 0 4 5 071 2 5 0 4 3 053 1 0 0 4
6 6 8 9 4
2 seconds
256 megabytes
['data structures', 'greedy', '*1000']
B. Count the Number of Pairstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputKristina has a string s of length n, consisting only of lowercase and uppercase Latin letters. For each pair of lowercase letter and its matching uppercase letter, Kristina can get 1 burl. However, pairs of characters cannot overlap, so each character can only be in one pair.For example, if she has the string s = "aAaaBACacbE", she can get a burl for the following character pairs: s_1 = "a" and s_2 = "A" s_4 = "a" and s_6 = "A" s_5 = "B" and s_{10} = "b" s_7= "C" and s_9 = "c" Kristina wants to get more burles for her string, so she is going to perform no more than k operations on it. In one operation, she can: either select the lowercase character s_i (1 \le i \le n) and make it uppercase. or select uppercase character s_i (1 \le i \le n) and make it lowercase. For example, when k = 2 and s = "aAaaBACacbE" it can perform one operation: choose s_3 = "a" and make it uppercase. Then she will get another pair of s_3 = "A" and s_8 = "a"Find maximum number of burles Kristina can get for her string.InputThe first line of input data contains a single integer t (1 \le t \le 10^4) — the number of test cases.The description of the test cases follows.The first line of each test case contains two integers n (1 \le n \le 2 \cdot 10^5) and k (0 \le k \le n) — the number of characters in the string and the maximum number of operations that can be performed on it.The second line of each test case contains a string s of length n, consisting only of lowercase and uppercase Latin letters.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, print exactly one integer on a separate line: the maximum number of burles that Kristina can get for her string s.ExampleInput 511 2aAaaBACacbE2 2ab4 1aaBB6 0abBAcC5 3cbccbOutput 5 0 1 3 2 NoteThe first test case is explained in the problem statement.In the second test case, it is not possible to get any pair by performing any number of operations.
511 2aAaaBACacbE2 2ab4 1aaBB6 0abBAcC5 3cbccb
5 0 1 3 2
2 seconds
256 megabytes
['greedy', 'strings', '*1000']
A. Is It a Cat?time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou were walking down the street and heard a sound. The sound was described by the string s consisting of lowercase and uppercase Latin characters. Now you want to find out if the sound was a cat meowing.For the sound to be a meowing, the string can only contain the letters 'm', 'e', 'o' and 'w', in either uppercase or lowercase. Also: string must start with non-empty sequence consisting only of characters 'm' or 'M' it must be immediately followed by non-empty sequence consisting only of characters 'e' or 'E' it must be immediately followed by non-empty sequence consisting only of characters 'o' or 'O' it must be immediately followed by non-empty sequence consisting only of characters 'w' or 'W', this sequence ends the string, after it immediately comes the string end For example, strings "meow", "mmmEeOWww", "MeOooOw" describe a meowing, but strings "Mweo", "MeO", "moew", "MmEW", "meowmeow" do not.Determine whether the sound you heard was a cat meowing or something else.InputThe first line of input data 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 an integer n (1 \le n \le 50) — the length of the string describing the sound.The second line of each test case contains a string s of n characters. The string describes the sound you heard and consists only of lowercase and uppercase Latin letters. OutputFor each test case, output on a separate line: YES if the sound was a cat meowing; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive response).ExampleInput 74meOw14mMmeoOoWWWwwwW3mew7MmeEeUw4MEOW6MmyaVW5meowAOutput YES YES NO NO YES NO NO NoteIn the first test case, the string consists of a sequence of characters 'm', 'e', 'O', 'w', which satisfies the definition of meowing.In the second test case, the string consists of a sequence of 3 characters 'm' and 'M', one 'e', a sequence of 3 characters 'o' and 'O' and a sequence of 7 characters 'w' and 'W', which satisfies the definition of meowing.In the third test case, the string does not describe a meowing because it lacks a sequence of 'o' or 'O' characters between 'e' and 'w'.In the fourth test case, the string contains the character 'U', so it does not describe a meowing.
74meOw14mMmeoOoWWWwwwW3mew7MmeEeUw4MEOW6MmyaVW5meowA
YES YES NO NO YES NO NO
2 seconds
256 megabytes
['implementation', 'strings', '*800']
H. Tree Cuttingtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a tree with n vertices.A hero k times do the following operation: Choose some edge. Remove it. Take one of the two remaining parts and delete it. Write the number of vertices in the remaining part. You are given an initial tree and the a sequence of written numbers. Find the number of ways to make operations such that the written numbers are equal to the given numbers. Due to the answer can be big, find it by modulo 998\,244\,353. Two ways are considered different, if on some operation edge or remaining part are selected differently.InputThe first line contains a single integer n (2 \leq n \leq 5000) — the number of vertices.Each of the next n-1 lines contains two integers s, f (1 \leq s, f \leq n, s \neq f) — description of edge (s, f).Next line contains a single integer k (1 \leq k \leq \min{(6, n - 1)}) — the number of operations.Next line contains k integers s_1, s_2, \ldots, s_k (n > s_1 > s_2 > \ldots > s_k \geq 1) — written numbers.OutputPrint a single integer — the answer to the problem by modulo 998\,244\,353.ExamplesInput 3 1 2 2 3 2 2 1 Output 4 Input 7 2 1 3 2 4 1 5 3 6 4 7 4 2 4 2 Output 2 Input 7 1 2 1 3 1 4 2 5 3 6 4 7 1 2 Output 3 Input 7 1 2 1 3 1 4 2 5 3 6 4 7 4 6 5 2 1 Output 24 Input 8 1 2 2 3 3 4 3 5 3 6 3 7 3 8 2 7 4 Output 0 NoteIn the first test there are four possible ways to make operations: Remove the edge (1, 2) and delete vertex 1. Remove the edge (2, 3) and delete vertex 2. Remove the edge (1, 2) and delete vertex 1. Remove the edge (3, 2) and delete vertex 3. Remove the edge (3, 2) and delete vertex 3. Remove the edge (1, 2) and delete vertex 1. Remove the edge (3, 2) and delete vertex 3. Remove the edge (2, 1) and delete vertex 2. In the second test there are two possible ways to make operations: Remove the edge (4, 1) and delete the part with vertex 4. Remove the edge (2, 3) and delete the part with vertex 2. Remove the edge (4, 1) and delete the part with vertex 4. Remove the edge (3, 2) and delete the part with vertex 3.
3 1 2 2 3 2 2 1
4
2 seconds
512 megabytes
['bitmasks', 'dfs and similar', 'dp', '*3200']
G. Count Votingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n people that will participate in voting. Each person has exactly one vote.i-th person has a team t_i (1 \leq t_i \leq n) where t_i = t_j means i, j are in the same team. By the rules each person should vote for the person from the different team. Note that it automatically means that each person can't vote for himself.Each person knows the number of votes c_i he wants to get. How many possible votings exists, such that each person will get the desired number of votes? Due to this number can be big, find it by modulo 998\,244\,353.InputThe first line contains a single integer n (1 \leq n \leq 200) — the number of people.The second line contains n integers c_1, c_2, \ldots, c_n (0 \leq c_i \leq n) — desired number of votes. It is guaranteed, that \sum\limits_{i=1}^{n} c_i = n.The third line contains n integers t_1, t_2, \ldots, t_n (1 \leq t_i \leq n) — team numbers.OutputPrint a single integer — the number of possible votings by modulo 998\,244\,353.ExamplesInput 3 1 1 1 1 2 3 Output 2 Input 5 2 0 1 0 2 1 2 3 4 5 Output 10 Input 5 1 2 2 0 0 3 5 4 3 4 Output 5 NoteIn the first test there are two possible votings: (2, 3, 1), (3, 1, 2).In the third test there are five possible votings: (3, 3, 2, 2, 1), (2, 3, 2, 3, 1), (3, 3, 1, 2, 2), (3, 1, 2, 3, 2), (2, 3, 1, 3, 2).
3 1 1 1 1 2 3
2
1 second
256 megabytes
['combinatorics', 'dp', 'math', '*2600']
F. Halve or Subtracttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have an array of positive integers a_1, a_2, \ldots, a_n, of length n. You are also given a positive integer b.You are allowed to perform the following operations (possibly several) times in any order: Choose some 1 \le i \le n, and replace a_i with \lceil \frac{a_i}{2} \rceil. Here, \lceil x \rceil denotes the smallest integer not less than x. Choose some 1 \le i \le n, and replace a_i with \max(a_i - b, 0). However, you must also follow these rules: You can perform at most k_1 operations of type 1 in total. You can perform at most k_2 operations of type 2 in total. For all 1 \le i \le n, you can perform at most 1 operation of type 1 on element a_i. For all 1 \le i \le n, you can perform at most 1 operation of type 2 on element a_i. The cost of an array is the sum of its elements. Find the minimum cost of a you can achieve by performing these operations.InputInput consists of multiple test cases. The first line contains a single integer t, the number of test cases (1 \le t \le 5000).The first line of each test case contains n, b, k_1, and k_2 (1 \le n \le 5000, 1 \le b \le 10^9, 0 \le k_1, k_2 \le n).The second line of each test case contains n integers a_1, a_2, \ldots, a_n describing the array a (1 \le a_i \le 10^9).It is guaranteed the sum of n over all test cases does not exceed 5000.OutputFor each test case, print the minimum cost of a you can achieve by performing the operations.ExampleInput 73 2 1 19 3 52 1 2 01000000000 15 3 1 12 8 3 19 36 9 4 21 2 3 4 5 63 10 3 31 2 35 1 0 0999999999 999999999 999999999 999999999 9999999995 5 4 35 9 10 7 4Output 11 500000001 23 6 0 4999999995 6 NoteIn the first test case, you can do the following: Perform operation 2 on element a_3. It changes from 5 to 3. Perform operation 1 on element a_1. It changes from 9 to 5. After these operations, the array is a = [5, 3, 3] has a cost 5 + 3 + 3 = 11. We can show that this is the minimum achievable cost.In the second test case, note that we are not allowed to perform operation 1 more than once on a_1. So it is optimal to apply operation 1 once to each a_1 and a_2. Alternatively we could apply operation 1 only once to a_1, since it has no effect on a_2.In the third test case, here is one way to achieve a cost of 23: Apply operation 1 to a_4. It changes from 19 to 10. Apply operation 2 to a_4. It changes from 10 to 7. After these operations, a = [2, 8, 3, 7, 3]. The cost of a is 2 + 8 + 3 + 7 + 3 = 23. We can show that this is the minimum achievable cost.
73 2 1 19 3 52 1 2 01000000000 15 3 1 12 8 3 19 36 9 4 21 2 3 4 5 63 10 3 31 2 35 1 0 0999999999 999999999 999999999 999999999 9999999995 5 4 35 9 10 7 4
11 500000001 23 6 0 4999999995 6
1 second
256 megabytes
['binary search', 'brute force', 'dp', 'greedy', 'sortings', '*2700']
E. City Uniontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n \times m grid. Some cells are filled and some are empty.A city is a maximal (by inclusion) set of filled cells such that it is possible to get from any cell in the set to any other cell in the set by moving to adjacent (by side) cells, without moving into any cells not in the set. In other words, a city is a connected component of filled cells with edges between adjacent (by side) cells.Initially, there are two cities on the grid. You want to change some empty cells into filled cells so that both of the following are satisfied: There is one city on the resulting grid. The shortest path between any two filled cells, achievable only by moving onto filled cells, is equal to the Manhattan distance between them. The Manhattan distance between two cells (a, b) and (c, d) is equal to |a - c| + |b - d|.Find a way to add filled cells that satisfies these conditions and minimizes the total number of filled cells.InputInput consists of multiple test cases. The first line contains a single integer t, the number of test cases (1 \le t \le 5000).The first line of each test case contains two integers n and m (1 \le n, m \le 50, nm \geq 3).The next n lines describe the grid. The i-th line contains a string s_i of length m. s_{i,j} is '#' if the cell in position (i, j) is filled, and '.' if it is empty.It is guaranteed that there are exactly two cities in the initial grid.It is guaranteed that the sum of n\cdot m over all test cases does not exceed 25\,000.OutputFor each test case, output n lines, each containing a string of length m, describing the grid you create in the same format as the input.If there are multiple possible answers with the minimum number of filled cells print any.ExampleInput 111 3#.#2 2.##.4 4..##...##...##..6 6.##...##..............##.....#...###6 5.#..#.#..#.#..#.#.##.#...##...5 5######...##.#.##...######4 4.##.##.##.##.##.5 5..###....#.....#....#....5 6.##...##....#....#....##...##.6 5..##....##....##....##....##..5 4..##..#...#.#...#...Output ### .# ## ..## ..## ###. ##.. .##... ###... ..#... ..#### ...### ...### .#### .#### .#### .#### .#... ##... ##### ##### ##### ##### ##### .##. #### #### .##. ..### ..### ..#.. ###.. #.... .##... ###... ###### ...### ...##. ..##. ..### ..### ###.. ###.. .##.. ..## ..#. ..#. ###. #...NoteIn the first test case, we can add a single filled cell between the two cities to connect them. We can verify that the second condition is satisfied.In the second test case, we can also connect the cities with a single filled cell, while satisfying the second condition. In the third test case, note that if we filled the 3 cells in the top left, the cities would be connected, but the second condition would not be satisfied for cells (4, 2) and (2, 4).
111 3#.#2 2.##.4 4..##...##...##..6 6.##...##..............##.....#...###6 5.#..#.#..#.#..#.#.##.#...##...5 5######...##.#.##...######4 4.##.##.##.##.##.5 5..###....#.....#....#....5 6.##...##....#....#....##...##.6 5..##....##....##....##....##..5 4..##..#...#.#...#...
### .# ## ..## ..## ###. ##.. .##... ###... ..#... ..#### ...### ...### .#### .#### .#### .#### .#... ##... ##### ##### ##### ##### ##### .##. #### #### .##. ..### ..### ..#.. ###.. #.... .##... ###... ###### ...### ...##. ..##. ..### ..### ###.. ###.. .##.. ..## ..#. ..#. ###. #...
1 second
256 megabytes
['constructive algorithms', 'dfs and similar', 'dsu', 'geometry', 'greedy', 'implementation', 'math', '*2300']
D2. Hot Start Up (hard version)time limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputThis is a hard version of the problem. The constraints of t, n, k are the only difference between versions.You have a device with two CPUs. You also have k programs, numbered 1 through k, that you can run on the CPUs. The i-th program (1 \le i \le k) takes cold_i seconds to run on some CPU. However, if the last program we ran on this CPU was also program i, it only takes hot_i seconds (hot_i \le cold_i). Note that this only applies if we run program i multiple times consecutively  — if we run program i, then some different program, then program i again, it will take cold_i seconds the second time.You are given a sequence a_1, a_2, \ldots, a_n of length n, consisting of integers from 1 to k. You need to use your device to run programs a_1, a_2, \ldots, a_n in sequence. For all 2 \le i \le n, you cannot start running program a_i until program a_{i - 1} has completed.Find the minimum amount of time needed to run all programs a_1, a_2, \ldots, a_n in sequence.InputInput consists of multiple test cases. The first line contains a single integer t, the number of test cases (1 \le t \le 10^5).The first line of each test case contains n and k (1 \le n, k \le 3 \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 k).The third line of each test case contains k integers cold_1, cold_2, \ldots, cold_k (1 \le cold_i \le 10^9).The fourth line of each test case contains k integers hot_1, hot_2, \ldots, hot_k (1 \le hot_i \le cold_i).It is guaranteed the sum of n and the sum of k over all test cases do not exceed 3 \cdot 10^5.OutputFor each test case, print the minimum time needed to run all programs in the given order.ExampleInput 93 21 2 23 22 14 21 2 1 25 32 14 31 2 3 1100 100 1001 1 15 22 1 2 1 165 4554 75 31 3 2 1 22 2 21 1 15 11 1 1 1 110000000009999999995 61 6 1 4 13 6 4 1 4 51 1 1 1 4 11 334 5 61 2 38 33 3 3 1 2 3 2 110 10 810 10 5Output 6 11 301 225 8 4999999996 11 6 63 NoteIn the first test case, we can do the following: Run program a_1 = 1 on CPU 1. It takes cold_1 = 3 seconds to run. Run program a_2 = 2 on CPU 2. It takes cold_2 = 2 seconds to run. Run program a_3 = 2 on CPU 2. The last program run on this CPU was also program 2, so it takes hot_2 = 1 second to run. In total, we need 3 + 2 + 1 = 6 seconds to run them all. We can show this is optimal.In the second test case, we can use do the following: Run program a_1 = 1 on CPU 1. It takes cold_1 = 5 seconds to run. Run program a_2 = 2 on CPU 2. It takes cold_2 = 3 seconds to run. Run program a_3 = 1 on CPU 1. The last program run on this CPU was also program 1, so it takes hot_1 = 2 seconds to run. Run program a_4 = 2 on CPU 2. The last program run on this CPU was also program 2, so it takes hot_2 = 1 second to run. In total, we need 5 + 3 + 2 + 1 = 11 seconds. We can show this is optimal.
93 21 2 23 22 14 21 2 1 25 32 14 31 2 3 1100 100 1001 1 15 22 1 2 1 165 4554 75 31 3 2 1 22 2 21 1 15 11 1 1 1 110000000009999999995 61 6 1 4 13 6 4 1 4 51 1 1 1 4 11 334 5 61 2 38 33 3 3 1 2 3 2 110 10 810 10 5
6 11 301 225 8 4999999996 11 6 63
1 second
512 megabytes
['data structures', 'dp', '*2100']
D1. Hot Start Up (easy version)time limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputThis is an easy version of the problem. The constraints of t, n, k are the only difference between versions.You have a device with two CPUs. You also have k programs, numbered 1 through k, that you can run on the CPUs. The i-th program (1 \le i \le k) takes cold_i seconds to run on some CPU. However, if the last program we ran on this CPU was also program i, it only takes hot_i seconds (hot_i \le cold_i). Note that this only applies if we run program i multiple times consecutively  — if we run program i, then some different program, then program i again, it will take cold_i seconds the second time.You are given a sequence a_1, a_2, \ldots, a_n of length n, consisting of integers from 1 to k. You need to use your device to run programs a_1, a_2, \ldots, a_n in sequence. For all 2 \le i \le n, you cannot start running program a_i until program a_{i - 1} has completed.Find the minimum amount of time needed to run all programs a_1, a_2, \ldots, a_n in sequence.InputInput consists of multiple test cases. The first line contains a single integer t, the number of test cases (1 \le t \le 5000).The first line of each test case contains n and k (1 \le n, k \le 5000).The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le k).The third line of each test case contains k integers cold_1, cold_2, \ldots, cold_k (1 \le cold_i \le 10^9).The fourth line of each test case contains k integers hot_1, hot_2, \ldots, hot_k (1 \le hot_i \le cold_i).It is guaranteed the sum of n and the sum of k over all test cases do not exceed 5000.OutputFor each test case, print the minimum time needed to run all programs in the given order.ExampleInput 93 21 2 23 22 14 21 2 1 25 32 14 31 2 3 1100 100 1001 1 15 22 1 2 1 165 4554 75 31 3 2 1 22 2 21 1 15 11 1 1 1 110000000009999999995 61 6 1 4 13 6 4 1 4 51 1 1 1 4 11 334 5 61 2 38 33 3 3 1 2 3 2 110 10 810 10 5Output 6 11 301 225 8 4999999996 11 6 63 NoteIn the first test case, we can do the following: Run program a_1 = 1 on CPU 1. It takes cold_1 = 3 seconds to run. Run program a_2 = 2 on CPU 2. It takes cold_2 = 2 seconds to run. Run program a_3 = 2 on CPU 2. The last program run on this CPU was also program 2, so it takes hot_2 = 1 second to run. In total, we need 3 + 2 + 1 = 6 seconds to run them all. We can show this is optimal.In the second test case, we can use do the following: Run program a_1 = 1 on CPU 1. It takes cold_1 = 5 seconds to run. Run program a_2 = 2 on CPU 2. It takes cold_2 = 3 seconds to run. Run program a_3 = 1 on CPU 1. The last program run on this CPU was also program 1, so it takes hot_1 = 2 seconds to run. Run program a_4 = 2 on CPU 2. The last program run on this CPU was also program 2, so it takes hot_2 = 1 second to run. In total, we need 5 + 3 + 2 + 1 = 11 seconds. We can show this is optimal.
93 21 2 23 22 14 21 2 1 25 32 14 31 2 3 1100 100 1001 1 15 22 1 2 1 165 4554 75 31 3 2 1 22 2 21 1 15 11 1 1 1 110000000009999999995 61 6 1 4 13 6 4 1 4 51 1 1 1 4 11 334 5 61 2 38 33 3 3 1 2 3 2 110 10 810 10 5
6 11 301 225 8 4999999996 11 6 63
1 second
512 megabytes
['dp', '*1900']
C. Double Lexicographically Minimumtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a string s. You can reorder the characters to form a string t. Define t_{\mathrm{max}} to be the lexicographical maximum of t and t in reverse order.Given s determine the lexicographically minimum value of t_{\mathrm{max}} over all reorderings t of s.A string a is lexicographically smaller than a string b if and only if one of the following holds: a is a prefix of b, but a \ne b; in the first position where a and b differ, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b. InputThe first line contains a single integer t (1 \leq t \leq 10^5) — the number of test cases. Descriptions of test cases follow.The first and only line of each test case contains a string s (1 \leq |s| \leq 10^5). s consists of only lowercase English letters. It is guaranteed that the sum of |s| over all test cases does not exceed 10^5.OutputFor each test case print the lexicographically minimum value of t_{\mathrm{max}} over all reorderings t of s. ExampleInput 12aaababbabcaabbaabbbaaabbabbbabbbbabbcceagaffcabaOutput a aba bab bca abba abbba ababa bbab bbabb bbcca agea acffba NoteFor the first test case, there is only one reordering of s, namely "a".For the second test case, there are three reorderings of s. t = \mathtt{aab}: t_{\mathrm{max}} = \max(\mathtt{aab}, \mathtt{baa}) = \mathtt{baa} t = \mathtt{aba}: t_{\mathrm{max}} = \max(\mathtt{aba}, \mathtt{aba}) = \mathtt{aba} t = \mathtt{baa}: t_{\mathrm{max}} = \max(\mathtt{baa}, \mathtt{aab}) = \mathtt{baa} The lexicographical minimum of t_{\mathrm{max}} over all cases is "aba".
12aaababbabcaabbaabbbaaabbabbbabbbbabbcceagaffcaba
a aba bab bca abba abbba ababa bbab bbabb bbcca agea acffba
2 seconds
256 megabytes
['greedy', 'strings', '*1700']
B. Equalize by Dividetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a_1, a_2, \ldots, a_n of positive integers.You can make this operation multiple (possibly zero) times: Choose two indices i, j (1 \leq i, j \leq n, i \neq j). Assign a_i := \lceil \frac{a_i}{a_j} \rceil. Here \lceil x \rceil denotes x rounded up to the smallest integer \geq x. Is it possible to make all array elements equal by some sequence of operations (possibly empty)? If yes, print any way to do it in at most 30n operations.It can be proven, that under the problem constraints, if some way exists to make all elements equal, there exists a way with at most 30n operations.InputThe first line contains a single integer t (1 \leq t \leq 1000) — the number of test cases. Descriptions of test cases follow.The first line of each test case description contains a single integer n (1 \leq n \leq 100).The second line of each test case description 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 for all test cases does not exceed 1000.OutputFor each test case print a single integer q (-1 \leq q \leq 30n). If q=-1, there is no solution, otherwise q is equal to the number of operations.If q \geq 0, on the next q lines print two integers i, j (1 \leq i, j \leq n, i \neq j) — descriptions of operations.If there are multiple solutions, you can print any.ExampleInput 10110031 1 122 125 534 3 243 3 4 422 10055 3 6 7 863 3 80 3 8 3419 40 19 55Output 0 0 -1 0 2 1 3 2 1 4 3 1 4 2 1 3 2 4 6 2 1 2 1 2 1 2 1 2 1 2 1 8 5 2 4 2 3 2 1 3 1 3 2 1 4 1 5 1 4 5 1 3 1 3 1 3 1 9 4 2 2 1 1 2 1 2 3 2 3 2 1 4 2 4 3 4 NoteIn the first and second, fourth test cases all numbers are equal, so it is possible to do nothing.In the third test case, it is impossible to make all numbers equal.In the fifth test case: [\color{red}{4}, 3, \color{blue}{2}] \to [\color{blue}{2}, \color{red}{3}, 2] \to [2, 2, 2].In the sixth test case: [\color{blue}{3}, 3, \color{red}{4}, 4] \to [3, \color{blue}{3}, 2, \color{red}{4}] \to [\color{red}{3}, 3, \color{blue}{2}, 2] \to [2, \color{red}{3}, 2, \color{blue}{2}] \to [2, 2, 2, 2].Here the red numbers are i indices (that will be assigned), blue numbers are j indices.
10110031 1 122 125 534 3 243 3 4 422 10055 3 6 7 863 3 80 3 8 3419 40 19 55
0 0 -1 0 2 1 3 2 1 4 3 1 4 2 1 3 2 4 6 2 1 2 1 2 1 2 1 2 1 2 1 8 5 2 4 2 3 2 1 3 1 3 2 1 4 1 5 1 4 5 1 3 1 3 1 3 1 9 4 2 2 1 1 2 1 2 3 2 3 2 1 4 2 4 3 4
1 second
256 megabytes
['brute force', 'constructive algorithms', 'greedy', 'math', '*1200']
A. Recent Actionstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputOn Codeforces the "Recent Actions" field shows the last n posts with recent actions.Initially, there are posts 1, 2, \ldots, n in the field (this is in order from top to down). Also there are infinitely many posts not in the field, numbered with integers n + 1, n + 2, \ldots.When recent action happens in the post p: If it is in the "Recent Actions" field, it moves from its position to the top position. Otherwise, it is added to the top position, and the post on the down position is removed from the "Recent Actions" field. You know, that the next m recent actions will happen in the posts p_1, p_2, \ldots, p_m (n + 1 \leq p_i \leq n + m) in the moments of time 1, 2, \ldots, m. Note, that recent actions only happen with posts with numbers \geq n + 1.For each post i (1 \leq i \leq n), find the first time it will be removed from the "Recent Actions" field or say, that it won't be removed.InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. Descriptions of test cases follow.The first line of each test case contains two integers n, m (1 \leq n, m \leq 5 \cdot 10^4) — the size of the "Recent Actions" field and the number of actions.The next line contains m integers p_1, p_2, \ldots, p_m (n + 1 \leq p_i \leq n + m).It is guaranteed, that the sum of n and the sum of m for all test cases does not exceed 5 \cdot 10^4.OutputFor each test case print n integers t_1, t_2, \ldots, t_n, where t_i=-1 if the post i won't be removed or t_i equals to the first moment of time the post i will be removed (1 \leq t_i \leq m).ExampleInput 101 123 25 44 55 9 9 5 75 56 7 8 9 103 44 4 4 44 45 5 6 63 54 5 5 5 44 205 5 24 24 24 5 6 7 8 9 10 12 13 14 15 16 17 18 19 205 77 8 7 11 7 12 106 78 11 7 8 8 8 12Output 1 -1 2 1 -1 5 2 1 5 4 3 2 1 -1 -1 1 -1 -1 3 1 -1 2 1 8 7 3 1 7 6 4 2 1 -1 -1 7 3 2 1 NoteIn the first test case, the only post 1 will be removed at the moment 1 and replaced by the post 2.In the second test case the "Recent Actions" field will be (given an order from top to down): Before moment 1: [1, 2, 3], after moment 1: [5, 1, 2]. Post number 3 was removed. Before moment 2: [5, 1, 2], after moment 2: [4, 5, 1]. Post number 2 was removed. Post number 1 won't be removed.In the third test case the "Recent Actions" field will be (given an order from top to down): Before moment 1: [1, 2, 3, 4], after moment 1: [5, 1, 2, 3]. Post number 4 was removed. Before moment 2: [5, 1, 2, 3], after moment 2: [9, 5, 1, 2]. Post number 3 was removed. Before moment 3: [9, 5, 1, 2], after moment 3: [9, 5, 1, 2]. Nothing was changed. Before moment 4: [9, 5, 1, 2], after moment 4: [5, 9, 1, 2]. The order was changed. Before moment 5: [5, 9, 1, 2], after moment 5: [7, 5, 9, 1]. Post number 2 was removed. Post number 1 won't be removed.
101 123 25 44 55 9 9 5 75 56 7 8 9 103 44 4 4 44 45 5 6 63 54 5 5 5 44 205 5 24 24 24 5 6 7 8 9 10 12 13 14 15 16 17 18 19 205 77 8 7 11 7 12 106 78 11 7 8 8 8 12
1 -1 2 1 -1 5 2 1 5 4 3 2 1 -1 -1 1 -1 -1 3 1 -1 2 1 8 7 3 1 7 6 4 2 1 -1 -1 7 3 2 1
1 second
256 megabytes
['data structures', 'greedy', 'implementation', 'math', '*800']
F. Gifts from Grandfather Ahmedtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGrandfather Ahmed's School has n+1 students. The students are divided into k classes, and s_i students study in the i-th class. So, s_1 + s_2 + \ldots + s_k = n+1.Due to the upcoming April Fools' Day, all students will receive gifts!Grandfather Ahmed planned to order n+1 boxes of gifts. Each box can contain one or more gifts. He plans to distribute the boxes between classes so that the following conditions are satisfied: Class number i receives exactly s_i boxes (so that each student can open exactly one box). The total number of gifts in the boxes received by the i-th class should be a multiple of s_i (it should be possible to equally distribute the gifts among the s_i students of this class). Unfortunately, Grandfather Ahmed ordered only n boxes with gifts, the i-th of which contains a_i gifts.Ahmed has to buy the missing gift box, and the number of gifts in the box should be an integer between 1 and 10^6. Help Ahmed to determine, how many gifts should the missing box contain, and build a suitable distribution of boxes to classes, or report that this is impossible.InputThe first line of the input contains two integers n and k (1 \le n, k \le 200, k \le n + 1).The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^6) — the number of gifts in the available boxes.The third line contains k integers s_1, s_2, \ldots, s_k (1 \le s_i \le n+1) — the number of students in classes. It is guaranteed that \sum s_i = n+1.OutputIf there is no way to buy the remaining box, output the integer -1 in a single line.Otherwise, in the first line, output a single integer s — the number of gifts in the box that Grandfather Ahmed should buy (1 \le s \le 10^6).Next, in k lines, print the distribution of boxes to classes. In the i-th line print s_i integers — the sizes of the boxes that should be sent to the i-th class.If there are multiple solutions, print any of them.ExamplesInput 4 2 7 7 7 127 2 3 Output 1 7 7 7 127 1 Input 18 4 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 6 1 9 3 Output 9 7 1 7 6 5 4 9 1 2 3 8 3 2 9 8 9 6 5 4 NoteIn the first test, Grandfather Ahmed can buy a box with just 1 gift. After that, two boxes with 7 gifts are sent to the first class. 7 + 7 = 14 is divisible by 2. And the second class gets boxes with 1, 7, 127 gifts. 1 + 7 + 127 = 135 is evenly divisible by 3.In the second test, the classes have sizes 6, 1, 9, and 3. We show that the available boxes are enough to distribute into classes with sizes 6, 9, 3, and in the class with size 1, you can buy a box of any size. In class with size 6 we send boxes with sizes 7, 1, 7, 6, 5, 4. 7 + 1 + 7 + 6 + 5 + 4 = 30 is divisible by 6. In class with size 9 we send boxes with sizes 1, 2, 3, 8, 3, 2, 9, 8, 9. 1 + 2 + 3 + 8 + 3 + 2 + 9 + 8 + 9 = 45 is divisible by 9. The remaining boxes (6, 5, 4) are sent to the class with size 3. 6 + 5 + 4 = 15 is divisible by 3.
4 2 7 7 7 127 2 3
1 7 7 7 127 1
1 second
256 megabytes
['dp', 'math', 'number theory', '*2500']
E. Multitest Generatortime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's call an array b_1, b_2, \ldots, b_m a test if b_1 = m - 1.Let's call an array b_1, b_2, \ldots, b_m a multitest if the array b_2, b_3, \ldots, b_m can be split into b_1 non-empty subarrays so that each of these subarrays is a test. Note that each element of the array must be included in exactly one subarray, and the subarrays must consist of consecutive elements.Let's define the function f from the array b_1, b_2, \ldots, b_m as the minimum number of operations of the form "Replace any b_i with any non-negative integer x", which needs to be done so that the array b_1, b_2, \ldots, b_m becomes a multitest.You are given an array of positive integers a_1, a_2, \ldots, a_n. For each i from 1 to n - 1, find f([a_i, a_{i+1}, \ldots, a_n]).Below are some examples of tests and multitests. Tests: [\underline{1}, 5], [\underline{2}, 2, 2], [\underline{3}, 4, 1, 1], [\underline{5}, 0, 0, 0, 0, 0], [\underline{7}, 1, 2, 3, 4, 5, 6, 7], [\underline{0}]. These arrays are tests since their first element (underlined) is equal to the length of the array minus one. Multitests: [1, \underline{\underline{1}, 1}], [2, \underline{\underline{3}, 0, 0, 1}, \underline{\underline{1}, 12}], [3, \underline{\underline{2}, 2, 7}, \underline{\underline{1}, 1}, \underline{\underline{3}, 4, 4, 4}], [4, \underline{\underline{0}}, \underline{\underline{3}, 1, 7, 9}, \underline{\underline{4}, 2, 0, 0, 9}, \underline{\underline{1}, 777}]. Underlined are the subarrays after the split, and double underlined are the first elements of each subarray. InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 300\,000). The description of the test cases follows.The first line of each test case contains a single integer n (2 \le n \le 300\,000) — the length of the array a.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 300\,000) — elements of the array a.It is guaranteed that the sum of n over all test cases does not exceed 300\,000.OutputFor each test case print n - 1 numbers — f([a_i, a_{i+1}, \ldots, a_n]) for each i from 1 to n - 1.ExamplesInput 341 2 1 773 1 3 1 2 1 142 7 1 1Output 0 1 1 0 1 1 0 1 1 1 1 1 Input 1193 4 1 2 1 7 7 3 1 3 1 2 1 1 4 2 7 1 1Output 0 0 1 1 1 1 1 1 1 0 1 0 1 0 2 1 1 1 NoteIn the first test case of the first test the array [1, 2, 1, 7] is a multitest since the array [2, 1, 7] is a test. The array [2, 1, 7] is not a multitest, but after replacing the first number with 1, an array [1, 1, 7] is obtained, which is a multitest. The array [1, 7] is also not a multitest, but the array [1, 0] is, so f([1, 7]) = 1.In the second test case of first test, for i = 2, f([a_i, a_{i+1}, \ldots, a_n]) = f([1, 3, 1, 2, 1, 1]) = 1, since the array itself is not a multitest, but after replacing the second element with 4 you get multitest.In the third test case of first test, for i = 1, f([a_i, a_{i+1}, \ldots, a_n]) = f([2, 7, 1, 1]) = 1, since the array itself is not a multitest, but after replacing the second element with 0 you get multitest.The second test is an array composed of all the numbers of the first test. Therefore f([a_1, a_2, \ldots, a_n]) naturally equals to 0.
341 2 1 773 1 3 1 2 1 142 7 1 1
0 1 1 0 1 1 0 1 1 1 1 1
2 seconds
256 megabytes
['brute force', 'dp', '*2300']
D. Shocking Arrangementtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a_1, a_2, \ldots, a_n consisting of integers such that a_1 + a_2 + \ldots + a_n = 0.You have to rearrange the elements of the array a so that the following condition is satisfied:\max\limits_{1 \le l \le r \le n} \lvert a_l + a_{l+1} + \ldots + a_r \rvert < \max(a_1, a_2, \ldots, a_n) - \min(a_1, a_2, \ldots, a_n), where |x| denotes the absolute value of x.More formally, determine if there exists a permutation p_1, p_2, \ldots, p_n that for the array a_{p_1}, a_{p_2}, \ldots, a_{p_n}, the condition above is satisfied, and find the corresponding array.Recall that the array p_1, p_2, \ldots, p_n is called a permutation if for each integer x from 1 to n there is exactly one i from 1 to n such that p_i = x.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 50\,000). The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 300\,000) — the length of the array a.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (-10^9 \le a_i \le 10^9) — elements of the array a. It is guaranteed that the sum of the array a is zero, in other words: a_1 + a_2 + \ldots + a_n = 0.It is guaranteed that the sum of n over all test cases does not exceed 300\,000.OutputFor each test case, if it is impossible to rearrange the elements of the array a in the required way, print "No" in a single line.If possible, print "Yes" in the first line, and then in a separate line n numbers — elements a_1, a_2, \ldots, a_n rearranged in a valid order (a_{p_1}, a_{p_2}, \ldots, a_{p_n}).If there are several possible answers, you can output any of them.ExampleInput 7 4 3 4 -2 -5 5 2 2 2 -3 -3 8 -3 -3 1 1 1 1 1 1 3 0 1 -1 7 -3 4 3 4 -4 -4 0 1 0 7 -18 13 -18 -17 12 15 13 Output Yes -5 -2 3 4 Yes -3 2 -3 2 2 Yes 1 1 1 -3 1 1 1 -3 Yes -1 0 1 Yes 4 -4 4 -4 0 3 -3 No Yes 13 12 -18 15 -18 13 -17 NoteIn the first test case \max(a_1, \ldots, a_n) - \min(a_1, \ldots, a_n) = 9. Therefore, the elements can be rearranged as [-5, -2, 3, 4]. It is easy to see that for such an arrangement \lvert a_l + \ldots + a_r \rvert is always not greater than 7, and therefore less than 9.In the second test case you can rearrange the elements of the array as [-3, 2, -3, 2, 2]. Then the maximum modulus of the sum will be reached on the subarray [-3, 2, -3], and will be equal to \lvert -3 + 2 + -3 \rvert = \lvert -4 \rvert = 4, which is less than 5.In the fourth test example, any rearrangement of the array a will be suitable as an answer, including [-1, 0, 1].
7 4 3 4 -2 -5 5 2 2 2 -3 -3 8 -3 -3 1 1 1 1 1 1 3 0 1 -1 7 -3 4 3 4 -4 -4 0 1 0 7 -18 13 -18 -17 12 15 13
Yes -5 -2 3 4 Yes -3 2 -3 2 2 Yes 1 1 1 -3 1 1 1 -3 Yes -1 0 1 Yes 4 -4 4 -4 0 3 -3 No Yes 13 12 -18 15 -18 13 -17
2 seconds
256 megabytes
['constructive algorithms', 'greedy', 'math', '*1600']
C. Candy Storetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe store sells n types of candies with numbers from 1 to n. One candy of type i costs b_i coins. In total, there are a_i candies of type i in the store.You need to pack all available candies in packs, each pack should contain only one type of candies. Formally, for each type of candy i you need to choose the integer d_i, denoting the number of type i candies in one pack, so that a_i is divided without remainder by d_i.Then the cost of one pack of candies of type i will be equal to b_i \cdot d_i. Let's denote this cost by c_i, that is, c_i = b_i \cdot d_i.After packaging, packs will be placed on the shelf. Consider the cost of the packs placed on the shelf, in order c_1, c_2, \ldots, c_n. Price tags will be used to describe costs of the packs. One price tag can describe the cost of all packs from l to r inclusive if c_l = c_{l+1} = \ldots = c_r. Each of the packs from 1 to n must be described by at least one price tag. For example, if c_1, \ldots, c_n = [4, 4, 2, 4, 4], to describe all the packs, a 3 price tags will be enough, the first price tag describes the packs 1, 2, the second: 3, the third: 4, 5.You are given the integers a_1, b_1, a_2, b_2, \ldots, a_n, b_n. Your task is to choose integers d_i so that a_i is divisible by d_i for all i, and the required number of price tags to describe the values of c_1, c_2, \ldots, c_n is the minimum possible.For a better understanding of the statement, look at the illustration of the first test case of the first test: Let's repeat the meaning of the notation used in the problem:a_i — the number of candies of type i available in the store.b_i — the cost of one candy of type i.d_i — the number of candies of type i in one pack.c_i — the cost of one pack of candies of type i is expressed by the formula c_i = b_i \cdot d_i.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 100\,000). Description of the test cases follows.The first line of each test case contains a single integer n (2 \le n \le 200\,000) — the number of types of candies.Each of the next n lines of each test case contains two integers a_i and b_i (1 \le a_i \le 10^9, 1 \le b_i \le 10\,000) — the number of candies and the cost of one candy of type i, respectively.It is guaranteed that the sum of n over all test cases does not exceed 200\,000.OutputFor each test case, output the minimum number of price tags required to describe the costs of all packs of candies in the store.ExampleInput 5420 36 214 520 73444 52002 102020 257 76 515 210 37 7510 111 55 12 28 267 1212 35 39 129 31000000000 10000Output 2 1 3 2 5 NoteIn the first test case, you can choose d_1 = 4, d_2 = 6, d_3 = 7, d_4 = 5. Then the cost of packs will be equal to [12, 12, 35, 35]. 2 price tags are enough to describe them, the first price tag for c_1, c_2 and the second price tag for c_3, c_4. It can be shown that with any correct choice of d_i, at least 2 of the price tag will be needed to describe all the packs. Also note that this example is illustrated by a picture in the statement.In the second test case, with d_1 = 4, d_2 = 2, d_3 = 10, the costs of all packs will be equal to 20. Thus, 1 price tag is enough to describe all the packs. Note that a_i is divisible by d_i for all i, which is necessary condition.In the third test case, it is not difficult to understand that one price tag can be used to describe 2nd, 3rd and 4th packs. And additionally a price tag for pack 1 and pack 5. Total: 3 price tags.
5420 36 214 520 73444 52002 102020 257 76 515 210 37 7510 111 55 12 28 267 1212 35 39 129 31000000000 10000
2 1 3 2 5
3 seconds
256 megabytes
['greedy', 'math', 'number theory', '*1700']
B. Three Sevenstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLottery "Three Sevens" was held for m days. On day i, n_i people with the numbers a_{i, 1}, \ldots, a_{i, n_i} participated in the lottery.It is known that in each of the m days, only one winner was selected from the lottery participants. The lottery winner on day i was not allowed to participate in the lottery in the days from i+1 to m.Unfortunately, the information about the lottery winners has been lost. You need to find any possible list of lottery winners on days from 1 to m or determine that no solution exists.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 50\,000). The description of the test cases follows.The first line of each test case contains a single integer m (1 \le m \le 50\,000) — the number of days in which the lottery was held.Next, for each i from 1 to m, follows a two-line block of data.The first line of each block contains a single integer n_i (1 \le n_i \le 50\,000) — the number of lottery participants on day i.The second line of the block contains integers a_{i, 1}, \ldots, a_{i, n_i} (1 \le a_{i, j} \le 50\,000) — lottery participants on day i. It is guaranteed that all the numbers a_{i, 1}, \ldots, a_{i, n_i} are pairwise distinct.It is guaranteed that the sum of n_i over all blocks of all test cases does not exceed 50\,000.OutputFor each test case, if there is no solution, print a single integer -1.Otherwise, print m integers p_1, p_2, \ldots, p_m (1 \le p_i \le 50\,000) — lottery winners on days from 1 to m. If there are multiple solutions, print any of them.ExampleInput 3341 2 4 832 9 121 4221 222 1441 2 3 4111413Output 8 2 1 -1 2 1 4 3 NoteIn the first test case, one of the answers is [8, 2, 1] since the participant with the number 8 participated on day 1, but did not participate on days 2 and 3; the participant with the number 2 participated on day 2, but did not participate on day 3; and the participant with the number 1 participated on day 3. Note that this is not the only possible answer, for example, [8, 9, 4] is also a correct answer.In the second test case, both lottery participants participated on both days, so any possible lottery winner on the day 1 must have participated on the day 2, which is not allowed. Thus, there is no correct answer.In the third test case, only one participant participated on days 2, 3, 4, and on day 1 there is only one participant who did not participate in the lottery on days 2, 3, 4 — participant 2, which means [2, 1, 4, 3] is the only correct answer to this test case.
3341 2 4 832 9 121 4221 222 1441 2 3 4111413
8 2 1 -1 2 1 4 3
2 seconds
256 megabytes
['brute force', 'data structures', 'greedy', 'implementation', '*1000']
A. Showstoppertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two arrays a_1, a_2, \ldots, a_n and b_1, b_2, \ldots, b_n.In one operation, you can choose any integer i from 1 to n and swap the numbers a_i and b_i.Determine whether, after using any (possibly zero) number of operations, the following two conditions can be satisfied simultaneously: a_n = \max(a_1, a_2, \ldots, a_n), b_n = \max(b_1, b_2, \ldots, b_n). Here \max(c_1, c_2, \ldots, c_k) denotes the maximum number among c_1, c_2, \ldots, c_k. For example, \max(3, 5, 4) = 5, \max(1, 7, 7) = 7, \max(6, 2) = 6.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 200). The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 100) — the length of the arrays.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 100) — elements of the first array.The third line of each test case contains n integers b_1, b_2, \ldots, b_n (1 \le b_i \le 100) — elements of the second array.OutputFor each test case, print "Yes" if after using any (possibly zero) number of operations the conditions described above are satisfied. Otherwise, print "No".You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.ExampleInput 737 9 77 6 9410 10 15 1510 16 15 152100 9999 10011191 2 3 4 5 6 7 8 99 9 9 9 9 9 6 6 671 1 2 2 1 1 21 2 1 2 1 2 1230 45 30Output Yes No Yes Yes Yes No No NoteIn the first test case, you can swap the numbers a_3 and b_3, after which the array a becomes equal to [7, 9, 9], and the array b becomes equal to [7, 6, 7], and both conditions are met.In the second test case, it can be proved that it is impossible to satisfy both conditions.In the third test case, you can swap the numbers a_1 and b_1, after which the array a becomes equal to [99, 99], and the array b becomes equal to [100, 100], and both conditions are satisfied.In fifth test case, you can swap a_7 and b_7, a_8 and b_8, a_9 and b_9, after which the array a becomes equal to [1, 2, 3, 4, 5, 6, 6, 6, 6], and the array b becomes equal to [9, 9, 9, 9, 9, 9, 7, 8, 9], and both conditions are satisfied.
737 9 77 6 9410 10 15 1510 16 15 152100 9999 10011191 2 3 4 5 6 7 8 99 9 9 9 9 9 6 6 671 1 2 2 1 1 21 2 1 2 1 2 1230 45 30
Yes No Yes Yes Yes No No
2 seconds
256 megabytes
['greedy', 'implementation', 'sortings', '*800']
F. Li Hua and Pathtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLi Hua has a tree of n vertices and n-1 edges. The vertices are numbered from 1 to n.A pair of vertices (u,v) (u < v) is considered cute if exactly one of the following two statements is true: u is the vertex with the minimum index among all vertices on the path (u,v). v is the vertex with the maximum index among all vertices on the path (u,v).There will be m operations. In each operation, he decides an integer k_j, then inserts a vertex numbered n+j to the tree, connecting with the vertex numbered k_j.He wants to calculate the number of cute pairs before operations and after each operation.Suppose you were Li Hua, please solve this problem.InputThe first line contains the single integer n (2\le n\le 2\cdot 10^5) — the number of vertices in the tree.Next n-1 lines contain the edges of the tree. The i-th line contains two integers u_i and v_i (1\le u_i,v_i\le n; u_i\ne v_i) — the corresponding edge. The given edges form a tree.The next line contains the single integer m (1\le m\le 2\cdot 10^5) — the number of operations.Next m lines contain operations — one operation per line. The j-th operation contains one integer k_j (1\le k_j < n+j) — a vertex.OutputPrint m+1 integers — the number of cute pairs before operations and after each operation.ExampleInput 7 2 1 1 3 1 4 4 6 4 7 6 5 2 5 6 Output 11 15 19 NoteThe initial tree is shown in the following picture: There are 11 cute pairs — (1,5),(2,3),(2,4),(2,6),(2,7),(3,4),(3,6),(3,7),(4,5),(5,7),(6,7).Similarly, we can count the cute pairs after each operation and the result is 15 and 19.
7 2 1 1 3 1 4 4 6 4 7 6 5 2 5 6
11 15 19
2 seconds
256 megabytes
['data structures', 'dfs and similar', 'divide and conquer', 'dsu', 'trees', '*3000']
E. Li Hua and Arraytime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputLi Hua wants to solve a problem about \varphi — Euler's totient function. Please recall that \varphi(x)=\sum\limits_{i=1}^x[\gcd(i,x)=1].^{\dagger,\ddagger}He has a sequence a_1,a_2,\cdots,a_n and he wants to perform m operations: "1 l r" (1\le l\le r\le n) — for each x\in[l,r], change a_x into \varphi(a_x). "2 l r" (1\le l\le r\le n) — find out the minimum changes needed to make sure a_l=a_{l+1}=\cdots=a_r. In each change, he chooses one x\in[l,r], change a_x into \varphi(a_x). Each operation of this type is independent, which means the array doesn't actually change. Suppose you were Li Hua, please solve this problem.^\dagger \gcd(x,y) denotes the greatest common divisor (GCD) of integers x and y. ^\ddagger The notation [\textrm{cond}] equals 1 if the condition \textrm{cond} is true, and 0 otherwise.InputThe first line contains two integers n and m (1\le n,m\le 10^{5}) — the number of elements in the array and the number of operations to process, respectively.The second line contains n integers a_{1},a_{2},\cdots ,a_{n} (1\le a_{i}\le 5\cdot 10^{6}) — the elements of the array.Next m lines, each line contains three integers t_{i},l_{i},r_{i} (t_i\in\{1,2\},1\le l_i\le r_i\le n) — the i-th operation. OutputFor each "2 l r", output the answer in an separate line.ExampleInput 5 4 8 1 6 3 7 2 1 5 2 3 4 1 1 3 2 3 4 Output 10 2 1 NoteDenote \varphi^k(x)=\begin{cases}x,&k=0\\\varphi(\varphi^{k-1}(x)),&k > 0\end{cases}.At first, a=[8,1,6,3,7].To make sure a_1=a_2=a_3=a_4=a_5, we can change a to a'=[\varphi^3(8),\varphi^0(1),\varphi^2(6),\varphi^2(3),\varphi^3(7)]=[1,1,1,1,1], using 3+0+2+2+3=10 changes.To make sure a_3=a_4, we can change a to a'=[\varphi^0(8),\varphi^0(1),\varphi^1(6),\varphi^1(3),\varphi^0(7)]=[8,1,2,2,7], using 0+0+1+1+0=2 changes.After "1 1 3", a is changed to a=[\varphi^1(8),\varphi^1(1),\varphi^1(6),\varphi^0(3),\varphi^0(7)]=[4,1,2,3,7].To make sure a_3=a_4, we can change a to a'=[\varphi^0(4),\varphi^0(1),\varphi^0(2),\varphi^1(3),\varphi^0(7)]=[4,1,2,2,7], using 0+0+0+1+0=1 change.
5 4 8 1 6 3 7 2 1 5 2 3 4 1 1 3 2 3 4
10 2 1
3 seconds
512 megabytes
['brute force', 'data structures', 'dsu', 'math', 'number theory', 'two pointers', '*2300']
D. Li Hua and Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLi Hua has a tree of n vertices and n-1 edges. The root of the tree is vertex 1. Each vertex i has importance a_i. Denote the size of a subtree as the number of vertices in it, and the importance as the sum of the importance of vertices in it. Denote the heavy son of a non-leaf vertex as the son with the largest subtree size. If multiple of them exist, the heavy son is the one with the minimum index.Li Hua wants to perform m operations: "1 x" (1\leq x \leq n) — calculate the importance of the subtree whose root is x. "2 x" (2\leq x \leq n) — rotate the heavy son of x up. Formally, denote son_x as the heavy son of x, fa_x as the father of x. He wants to remove the edge between x and fa_x and connect an edge between son_x and fa_x. It is guaranteed that x is not root, but not guaranteed that x is not a leaf. If x is a leaf, please ignore the operation. Suppose you were Li Hua, please solve this problem.InputThe first line contains 2 integers n,m (2\le n\le 10^{5},1\le m\le 10^{5}) — the number of vertices in the tree and the number of operations.The second line contains n integers a_{1},a_{2},\ldots ,a_{n} (-10^{9}\le a_{i}\le 10^{9}) — the importance of each vertex.Next n-1 lines contain the edges of the tree. The i-th line contains two integers u_i and v_i (1\le u_i,v_i\le n, u_i\ne v_i) — the corresponding edge. The given edges form a tree.Next m lines contain operations — one operation per line. The j-th operation contains two integers t_{j},x_{j} (t_{j}\in \{1,2\}, 1 \leq x_{j} \leq n, x_{j}\neq 1 if t_j = 2) — the j-th operation. OutputFor each query "1 x", output the answer in an independent line.ExamplesInput 7 4 1 1 1 1 1 1 1 1 2 1 3 2 4 2 5 3 6 6 7 1 6 2 3 1 6 1 2 Output 2 3 3 Input 10 14 -160016413 -90133231 -671446275 -314847579 -910548234 121155052 -359359950 83112406 -704889624 145489303 1 6 1 10 10 8 1 4 3 4 2 7 2 5 3 2 9 8 1 4 2 2 2 4 1 4 1 10 2 10 1 9 1 6 2 8 2 10 1 5 1 8 1 1 2 5 Output -2346335269 -314847579 -476287915 -704889624 121155052 -1360041415 228601709 -2861484545 NoteIn the first example:The initial tree is shown in the following picture: The importance of the subtree of 6 is a_6+a_7=2.After rotating the heavy son of 3 (which is 6) up, the tree is shown in the following picture: The importance of the subtree of 6 is a_6+a_3+a_7=3.The importance of the subtree of 2 is a_2+a_4+a_5=3.
7 4 1 1 1 1 1 1 1 1 2 1 3 2 4 2 5 3 6 6 7 1 6 2 3 1 6 1 2
2 3 3
2 seconds
256 megabytes
['brute force', 'data structures', 'dfs and similar', 'dp', 'implementation', 'trees', '*1900']
C. Li Hua and Chesstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.Li Ming and Li Hua are playing a game. Li Hua has a chessboard of size n\times m. Denote (r, c) (1\le r\le n, 1\le c\le m) as the cell on the r-th row from the top and on the c-th column from the left. Li Ming put a king on the chessboard and Li Hua needs to guess its position.Li Hua can ask Li Ming no more than 3 questions. In each question, he can choose a cell and ask the minimum steps needed to move the king to the chosen cell. Each question is independent, which means the king doesn't actually move.A king can move from (x,y) to (x',y') if and only if \max\{|x-x'|,|y-y'|\}=1 (shown in the following picture). The position of the king is chosen before the interaction.Suppose you were Li Hua, please solve this problem.InteractionThe first line contains the number of test cases t (1 \le t \le 10^3). The first line of each test case contains two integers n,m (1\le n,m\le 10^9) — the size of the chessboard, and then the interaction begins.To ask a question, print "? r c" (without quotes, 1 \leq r \leq n, 1 \leq c \leq m). Then you should input the response from standard input — the minimum steps the king needs to move to the chosen cell.If your program has asked an invalid question or has run out of questions, the interactor will terminate immediately and your program will get a verdict Wrong answer.To give the final answer, print "! r c" (without the quotes, (r,c) is the king's initial coordinate). Note that giving this answer is not counted towards the limit of 3 questions.After asking a question do not forget to output the 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 the documentation for other languages. HacksTo hack, use the following format.The first line should contain a single integer t (1 \le t \le 10^3).The first and only line of each test case should contain four integers n,m,r,c (1\le r\le n\le 10^9,1\le c\le m\le 10^9).ExampleInput 2 3 4 1 2 5 3 3 1 2Output ? 2 3 ? 2 4 ! 2 2 ? 2 2 ? 5 2 ? 5 3 ! 5 1NoteIn test case 1, the king is at (2,2). It takes 1 step to move to (2,3) and 2 steps to move to (2,4). Note that the questions may not seem sensible. They are just a sample of questions you may ask.
2 3 4 1 2 5 3 3 1 2
? 2 3 ? 2 4 ! 2 2 ? 2 2 ? 5 2 ? 5 3 ! 5 1
1 second
256 megabytes
['constructive algorithms', 'greedy', 'interactive', '*1600']
B. Li Hua and Patterntime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputLi Hua has a pattern of size n\times n, each cell is either blue or red. He can perform exactly k operations. In each operation, he chooses a cell and changes its color from red to blue or from blue to red. Each cell can be chosen as many times as he wants. Is it possible to make the pattern, that matches its rotation by 180^{\circ}?Suppose you were Li Hua, please solve this problem.InputThe first line contains the single integer t (1 \le t \le 100) — the number of test cases.The first line of each test case contains two integers n,k (1\le n\le 10^3,0\le k \le 10^9) — the size of the pattern and the number of operations.Each of next n lines contains n integers a_{i,j} (a_{i,j}\in\{0,1\}) — the initial color of the cell, 0 for blue and 1 for red.It's guaranteed that sum of n over all test cases does not exceed 10^3.OutputFor each set of input, print "YES" if it's possible to make the pattern, that matches its rotation by 180^{\circ} after applying exactly k of operations, and "NO" otherwise.You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.ExampleInput 34 01 1 1 10 0 0 11 0 1 01 1 1 14 31 0 1 11 0 0 00 1 0 11 1 0 15 40 0 0 0 00 1 1 1 10 1 0 0 01 1 1 1 10 0 0 0 0Output NO YES YES NoteIn test case 1, you can't perform any operation. The pattern after rotation is on the right. In test case 2, you can perform operations on (2,1),(3,2),(3,4). The pattern after operations is in the middle and the pattern after rotation is on the right.
34 01 1 1 10 0 0 11 0 1 01 1 1 14 31 0 1 11 0 0 00 1 0 11 1 0 15 40 0 0 0 00 1 1 1 10 1 0 0 01 1 1 1 10 0 0 0 0
NO YES YES
1 second
256 megabytes
['constructive algorithms', 'greedy', '*1100']
A. Li Hua and Mazetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a rectangular maze of size n\times m. Denote (r,c) as the cell on the r-th row from the top and the c-th column from the left. Two cells are adjacent if they share an edge. A path is a sequence of adjacent empty cells.Each cell is initially empty. Li Hua can choose some cells (except (x_1, y_1) and (x_2, y_2)) and place an obstacle in each of them. He wants to know the minimum number of obstacles needed to be placed so that there isn't a path from (x_1, y_1) to (x_2, y_2).Suppose you were Li Hua, please solve this problem.InputThe first line contains the single integer t (1 \le t \le 500) — the number of test cases.The first line of each test case contains two integers n,m (4\le n,m\le 10^9) — the size of the maze.The second line of each test case contains four integers x_1,y_1,x_2,y_2 (1\le x_1,x_2\le n, 1\le y_1,y_2\le m) — the coordinates of the start and the end.It is guaranteed that |x_1-x_2|+|y_1-y_2|\ge 2.OutputFor each test case print the minimum number of obstacles you need to put on the field so that there is no path from (x_1, y_1) to (x_2, y_2).ExampleInput 34 42 2 3 36 71 1 2 39 95 1 3 6Output 4 2 3 NoteIn test case 1, you can put obstacles on (1,3), (2,3), (3,2), (4,2). Then the path from (2,2) to (3,3) will not exist.
34 42 2 3 36 71 1 2 39 95 1 3 6
4 2 3
1 second
256 megabytes
['constructive algorithms', 'flows', 'graphs', 'greedy', 'implementation', '*800']
F. Strange Triplestime limit per test10 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's call a triple of positive integers (a, b, n) strange if the equality \frac{an}{nb} = \frac{a}{b} holds, where an is the concatenation of a and n and nb is the concatenation of n and b. For the purpose of concatenation, the integers are considered without leading zeroes.For example, if a = 1, b = 5 and n = 9, then the triple is strange, because \frac{19}{95} = \frac{1}{5}. But a = 7, b = 3 and n = 11 is not strange, because \frac{711}{113} \ne \frac{7}{3}.You are given three integers A, B and N. Calculate the number of strange triples (a, b, n), such that 1 \le a < A, 1 \le b < B and 1 \le n < N.InputThe only line contains three integers A, B and N (1 \le A, B \le 10^5; 1 \le N \le 10^9).OutputPrint one integer — the number of strange triples (a, b, n) such that 1 \le a < A, 1 \le b < B and 1 \le n < N.ExamplesInput 5 6 10 Output 7 Input 10 10 100 Output 29 Input 1 10 25 Output 0 Input 4242 6969 133333337 Output 19536 Input 94841 47471 581818184 Output 98715 NoteIn the first example, there are 7 strange triples: (1, 1, 1), (1, 4, 6), (1, 5, 9), (2, 2, 2), (2, 5, 6), (3, 3, 3) and (4, 4, 4).
5 6 10
7
10 seconds
256 megabytes
['brute force', 'math', 'number theory', '*2900']
E. Colored Subgraphstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp has a tree, consisting of n vertices.He is going to select some vertex r and perform the following operations on each vertex v from 1 to n: set d_v equal to the distance from v to r (the number of edges on the shortest path); color v some color. A nice coloring satisfies two conditions: for each pair of vertices of the same color (v, u), there exists a path from v to u that only visits vertices of the same color; for each pair of vertices of the same color (v, u), d_v \neq d_u. Note that Monocarp can choose any amount of different colors he wants to use.For each used color, he then counts the number of vertices of this color. The cost of the tree is the minimum of these numbers.What can be the maximum cost of the tree?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 n (3 \le n \le 2 \cdot 10^5) — the number of vertices in the tree.Each of the next n-1 lines contains two integers v and u (1 \le v, u \le n) — the description of an edge.The given edges form a tree. The sum of n over all testcases doesn't exceed 2 \cdot 10^5.OutputFor each testcase print a single integer — the maximum possible cost of the tree.ExampleInput 441 22 33 451 21 31 41 531 33 273 22 57 53 11 61 4Output 4 1 3 3
441 22 33 451 21 31 41 531 33 273 22 57 53 11 61 4
4 1 3 3
2 seconds
256 megabytes
['dfs and similar', 'dp', 'games', 'greedy', 'trees', '*2500']
D. Maximum Subarraytime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an array a_1, a_2, \dots, a_n, consisting of n integers. You are also given two integers k and x.You have to perform the following operation exactly once: add x to the elements on exactly k distinct positions, and subtract x from all the others.For example, if a = [2, -1, 2, 3], k = 1, x = 2, and we have picked the first element, then after the operation the array a = [4, -3, 0, 1].Let f(a) be the maximum possible sum of a subarray of a. The subarray of a is a contiguous part of the array a, i. e. the array a_i, a_{i + 1}, \dots, a_j for some 1 \le i \le j \le n. An empty subarray should also be considered, it has sum 0.Let the array a' be the array a after applying the aforementioned operation. Apply the operation in such a way that f(a') is the maximum possible, and print the maximum possible value of f(a').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 three integers n, k and x (1 \le n \le 2 \cdot 10^5; 0 \le k \le \min(20, n); -10^9 \le x \le 10^9).The second line contains n integers a_1, a_2, \dots, a_n (-10^9 \le a_i \le 10^9). The sum of n over all test cases doesn't exceed 2 \cdot 10^5.OutputFor each test case, print one integer — the maximum possible value of f(a').ExampleInput 44 1 22 -1 2 32 2 3-1 23 0 53 2 46 2 -84 -1 9 -3 7 -8Output 5 7 0 44
44 1 22 -1 2 32 2 3-1 23 0 53 2 46 2 -84 -1 9 -3 7 -8
5 7 0 44
2 seconds
512 megabytes
['data structures', 'dp', 'greedy', 'two pointers', '*2000']
C. Maximum Settime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputA set of positive integers S is called beautiful if, for every two integers x and y from this set, either x divides y or y divides x (or both).You are given two integers l and r. Consider all beautiful sets consisting of integers not less than l and not greater than r. You have to print two numbers: the maximum possible size of a beautiful set where all elements are from l to r; the number of beautiful sets consisting of integers from l to r with the maximum possible size. Since the second number can be very large, print it modulo 998244353.InputThe first line contains one integer t (1 \le t \le 2 \cdot 10^4) — the number of test cases.Each test case consists of one line containing two integers l and r (1 \le l \le r \le 10^6).OutputFor each test case, print two integers — the maximum possible size of a beautiful set consisting of integers from l to r, and the number of such sets with maximum possible size. Since the second number can be very large, print it modulo 998244353.ExampleInput 43 1113 371 224 100Output 2 4 2 6 5 1 5 7 NoteIn the first test case, the maximum possible size of a beautiful set with integers from 3 to 11 is 2. There are 4 such sets which have the maximum possible size: \{ 3, 6 \}; \{ 3, 9 \}; \{ 4, 8 \}; \{ 5, 10 \}.
43 1113 371 224 100
2 4 2 6 5 1 5 7
2 seconds
512 megabytes
['binary search', 'math', '*1600']
B. Asterisk-Minor Templatetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two strings a and b, consisting of lowercase Latin letters.A template t is string, consisting of lowercase Latin letters and asterisks (character '*'). A template is called asterisk-minor if the number of asterisks in it is less than or equal to the number of letters in it.A string s is said to be matching a template t if you can replace each asterisk in t with a string of lowercase Latin letters (possibly, an empty string) so that it becomes equal to s.Find an asterisk-minor template such that both a and b match it, or report that such a template doesn't exist. If there are multiple answers, print any of them.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 string a (1 \le |a| \le 50, where |a| is the length of a), consisting of lowercase Latin letters.The second line contains a string b (1 \le |b| \le 50), consisting of lowercase Latin letters.OutputFor each testcase, output "NO", if there doesn't exist an asterisk-minor template that both a and b match. Otherwise, print "YES" in the first line and the template in the second line. If there are multiple answers, print any of them.A template should consist only of lowercase Latin letters and asterisks (character '*'). The number of asterisks should be less than or equal to the number of letters.ExampleInput 6aaabzzzbcodeforcesatcodercodeforcestokitlxaaaaaaaaaaabcdabcdcfOutput YES *b YES *co* NO YES a*a*a*a YES abcd NO NoteIn the first testcase, for a template "*b", you can replace the only asterisk with "aaa" to get "aaab" (which is equal to a) or with "zzz" to get "zzzb" (which is equal to b).In the third testcase, a template "*o*" is not asterisk-minor, as it contains more asterisks than letters. There are no asterisk-minor templates that both a and b match.In the fourth testcase, for a template "a*a*a*a", you can replace all asterisks with empty strings to get "aaaa" (which is equal to a) or two of them with "a" and two of them with an empty string to get "aaaaaa" (which is equal to b).In the fifth testcase, there are no asterisks in a template "abcd", so only "abcd" can match it (which is coincidentally both a and b).
6aaabzzzbcodeforcesatcodercodeforcestokitlxaaaaaaaaaaabcdabcdcf
YES *b YES *co* NO YES a*a*a*a YES abcd NO
2 seconds
256 megabytes
['implementation', 'strings', '*1000']
A. Typical Interview Problemtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe FB-string is formed as follows. Initially, it is empty. We go through all positive integers, starting from 1, in ascending order, and do the following for each integer: if the current integer is divisible by 3, append F to the end of the FB-string; if the current integer is divisible by 5, append B to the end of the FB-string. Note that if an integer is divisible by both 3 and 5, we append F, and then B, not in the opposite order.The first 10 characters of the FB-string are FBFFBFFBFB: the first F comes from the integer 3, the next character (B) comes from 5, the next F comes from the integer 6, and so on. It's easy to see that this string is infinitely long. Let f_i be the i-th character of FB-string; so, f_1 is F, f_2 is B, f_3 is F, f_4 is F, and so on.You are given a string s, consisting of characters F and/or B. You have to determine whether it is a substring (contiguous subsequence) of the FB-string. In other words, determine if it is possible to choose two integers l and r (1 \le l \le r) so that the string f_l f_{l+1} f_{l+2} \dots f_r is exactly s.For example: FFB is a substring of the FB-string: if we pick l = 3 and r = 5, the string f_3 f_4 f_5 is exactly FFB; BFFBFFBF is a substring of the FB-string: if we pick l = 2 and r = 9, the string f_2 f_3 f_4 \dots f_9 is exactly BFFBFFBF; BBB is not a substring of the FB-string. InputThe first line contains one integer t (1 \le t \le 2046) — the number of test cases.Each test case consists of two lines. The first line contains one integer k (1 \le k \le 10) — the number of characters in s. The second line contains s, which is a string of exactly k characters. Each character of s is either F or B.OutputFor each test case, print YES if s is a substring of the FB-string, or NO otherwise.You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).ExampleInput 33FFB8BFFBFFBF3BBBOutput YES YES NO
33FFB8BFFBFFBF3BBB
YES YES NO
2 seconds
512 megabytes
['brute force', 'implementation', 'strings', '*800']
G. Removal Sequencestime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a simple undirected graph, consisting of n vertices and m edges. The vertices are numbered from 1 to n. The i-th vertex has a value a_i written on it.You will be removing vertices from that graph. You are allowed to remove vertex i only if its degree is equal to a_i. When a vertex is removed, all edges incident to it are also removed, thus, decreasing the degree of adjacent non-removed vertices.A valid sequence of removals is a permutation p_1, p_2, \dots, p_n (1 \le p_i \le n) such that the i-th vertex to be removed is p_i, and every removal is allowed.A pair (x, y) of vertices is nice if there exist two valid sequences of removals such that x is removed before y in one of them and y is removed before x in the other one.Count the number of nice pairs (x, y) such that x < y.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 two integers n and m (1 \le n \le 10^5; 0 \le m \le \min(10^5, \frac{n \cdot (n - 1)}{2})) — the number of vertices and the number of edges of the graph.The second line contains n integers a_1, a_2, \dots, a_n (0 \le a_i \le n - 1) — the degree requirements for each removal.Each of the next m lines contains two integers v and u (1 \le v, u \le n; v \neq u) — the description of an edge.The graph doesn't contain any self-loops or multiple edges.The sum of n over all testcases doesn't exceed 10^5. The sum of m over all testcases doesn't exceed 10^5.Additional constraint on the input: there always exists at least one valid sequence of removals.OutputFor each testcase, print a single integer — the number of nice pairs of vertices.ExampleInput 43 21 0 12 31 23 31 2 01 22 31 35 63 0 2 1 01 24 14 23 42 35 11 00Output 1 0 4 0
43 21 0 12 31 23 31 2 01 22 31 35 63 0 2 1 01 24 14 23 42 35 11 00
1 0 4 0
4 seconds
256 megabytes
['bitmasks', 'dfs and similar', 'graphs', '*2700']
F. Blocking Chipstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree, consisting of n vertices. There are k chips, placed in vertices a_1, a_2, \dots, a_k. All a_i are distinct. Vertices a_1, a_2, \dots, a_k are colored black initially. The remaining vertices are white.You are going to play a game where you perform some moves (possibly, zero). On the i-th move (1-indexed) you are going to move the ((i - 1) \bmod k + 1)-st chip from its current vertex to an adjacent white vertex and color that vertex black. So, if k=3, you move chip 1 on move 1, chip 2 on move 2, chip 3 on move 3, chip 1 on move 4, chip 2 on move 5 and so on. If there is no adjacent white vertex, then the game ends.What's the maximum number of moves you can perform?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 n (1 \le n \le 2 \cdot 10^5) — the number of vertices of the tree.Each of the next n - 1 lines contains two integers v and u (1 \le v, u \le n) — the descriptions of the edges. The given edges form a tree.The next line contains a single integer k (1 \le k \le n) — the number of chips.The next line contains k integers a_1, a_2, \dots, a_k (1 \le a_i \le n) — the vertices with the chips. All a_i are distinct.The sum of n over all testcases doesn't exceed 2 \cdot 10^5.OutputFor each testcase, print a single integer — the maximum number of moves you can perform.ExampleInput 551 22 33 44 51351 22 33 44 521 251 22 33 44 522 161 21 32 42 53 631 4 6111Output 2 0 1 2 0
551 22 33 44 51351 22 33 44 521 251 22 33 44 522 161 21 32 42 53 631 4 6111
2 0 1 2 0
3 seconds
256 megabytes
['binary search', 'constructive algorithms', 'dfs and similar', 'greedy', 'trees', '*2400']
E. Explosions?time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are playing yet another game where you kill monsters using magic spells. There are n cells in the row, numbered from 1 to n. Initially, the i-th cell contains the i-th monster with h_i health.You have a basic spell that costs 1 MP and deals 1 damage to the monster you choose. You can cast it any number of times. Also, you have a special scroll with "Explosion" spell you can use only once. You want to finish killing monsters with explosion, that's why you, firstly, cast the basic spell several times (possibly, zero), and then after that, you cast one "Explosion".How does "Explosion" spell work? Firstly, you choose the power of the spell: if you pour x MP into it, "Explosion" will deal x damage. Secondly, you choose some monster i, which will be targeted by the spell. That's what happens next: if its current health h_i > x, then he stays alive with health decreased by x; if h_i \le x, the i-th monster dies with an explosion that deals h_i - 1 damage to monsters in the neighboring cells i - 1 and i + 1, if these cells exist and monsters inside are still alive; if the damage dealt by the explosion is enough to kill the monster i - 1 (or i + 1), i. e. the current h_{i - 1} \le h_i - 1 (or h_{i + 1} \le h_i - 1), then that monster also dies creating a secondary explosion of power h_{i-1} - 1 (or h_{i+1} - 1) that may deals damage to their neighbors, and so on, until the explosions end. Your goal is to kill all the remaining monsters with those "chaining" explosions, that's why you need a basic spell to decrease h_i of some monsters or even kill them beforehand (monsters die when their current health h_i becomes less or equal to zero). Note that monsters don't move between cells, so, for example, monsters i and i + 2 will never become neighbors.What is the minimum total MP you need to kill all monsters in the way you want? The total MP is counted as the sum of the number of basic spells you cast and the power x of explosion scroll you've chosen.InputThe first line contains one 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 (1 \le n \le 3 \cdot 10^5) — the number of cells in the row, i. e. the number of monsters.The second line of each test case contains n integers h_1, h_2, \dots, h_n (1 \le h_i \le 10^6) — the initial health of the monsters.It's guaranteed that the sum of n over all test cases doesn't exceed 3 \cdot 10^5.OutputFor each test case, print one integer — the minimum total MP you need to kill all monsters by finishing them with explosion.ExampleInput 531 1 144 1 2 145 10 15 1014291 2 3 2 2 2 3 2 1Output 3 6 15 42 12 NoteIn the first test case, you can, for example, use basic spell on monsters 1 and 2 (once per monster) to kill them. After that, you cast "Explosion" of power x = 1 on monster 3 to kill it. The total MP you need is 2 + 1 = 3.In the second test case, it's optimal to cast basic spell 4 times onto monster 1 to kill it. After that, you can cast "Explosion" of power x = 2 onto monster 3. It dies, creating an explosion of power 1 that kills monsters 2 and 4. The total MP you need is 4 + 2 = 6.In the third test case, you cast "Explosion" of power 15 onto monster 3. Explosion of the 3-rd monster (of power 14) kills monsters 2 and 4. Secondary explosion of monster 2 (of power 9) kills monster 1.
531 1 144 1 2 145 10 15 1014291 2 3 2 2 2 3 2 1
3 6 15 42 12
2 seconds
256 megabytes
['binary search', 'data structures', 'dp', 'greedy', 'math', '*2200']
D. Triangle Coloringtime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given an undirected graph consisting of n vertices and n edges, where n is divisible by 6. Each edge has a weight, which is a positive (greater than zero) integer.The graph has the following structure: it is split into \frac{n}{3} triples of vertices, the first triple consisting of vertices 1, 2, 3, the second triple consisting of vertices 4, 5, 6, and so on. Every pair of vertices from the same triple is connected by an edge. There are no edges between vertices from different triples.You have to paint the vertices of this graph into two colors, red and blue. Each vertex should have exactly one color, there should be exactly \frac{n}{2} red vertices and \frac{n}{2} blue vertices. The coloring is called valid if it meets these constraints.The weight of the coloring is the sum of weights of edges connecting two vertices with different colors.Let W be the maximum possible weight of a valid coloring. Calculate the number of valid colorings with weight W, and print it modulo 998244353.InputThe first line contains one integer n (6 \le n \le 3 \cdot 10^5, n is divisible by 6).The second line contains n integers w_1, w_2, \dots, w_n (1 \le w_i \le 1000) — the weights of the edges. Edge 1 connects vertices 1 and 2, edge 2 connects vertices 1 and 3, edge 3 connects vertices 2 and 3, edge 4 connects vertices 4 and 5, edge 5 connects vertices 4 and 6, edge 6 connects vertices 5 and 6, and so on.OutputPrint one integer — the number of valid colorings with maximum possible weight, taken modulo 998244353.ExamplesInput 12 1 3 3 7 8 5 2 2 2 2 4 2 Output 36 Input 6 4 2 6 6 6 4 Output 2 NoteThe following picture describes the graph from the first example test. The maximum possible weight of a valid coloring of this graph is 31.
12 1 3 3 7 8 5 2 2 2 2 4 2
36
3 seconds
512 megabytes
['combinatorics', 'math', '*1600']
C. Tea Tastingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA tea manufacturer decided to conduct a massive tea tasting. n sorts of tea will be tasted by n tasters. Both the sorts of tea and the tasters are numbered from 1 to n. The manufacturer prepared a_i milliliters of the i-th sort of tea. The j-th taster can drink b_j milliliters of tea at once.The tasting will be conducted in steps. During the first step, the i-th taster tastes the i-th sort of tea. The i-th taster drinks \min(a_i, b_i) tea (how much is available of the i-th sort and how much the i-th taster can drink). a_i also decreases by this amount.Then all tasters move to the previous sort of tea. Thus, during the second step, the i-th taster tastes the (i-1)-st sort of tea. The i-th taster drinks \min(a_{i-1}, b_i) tea. The 1-st person ends the tasting.During the third step, the i-th taster tastes the (i-2)-nd sort of tea. The 2-nd taster ends the tasting. This goes on until everyone ends the tasting.Take a look at the tasting process for n = 3, a = [10, 20, 15], b = [9, 8, 6]. In the left row, there are the current amounts of each sort of tea. In the right column, there are current amounts of tea each taster has drunk in total. The arrow tells which taster each tea goes to on the current step. The number on the arrow is the amount — minimum of how much is available of the sort of tea and how much the taster can drink. For each taster, print how many milliliters of tea he/she will drink in total.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 n (1 \le n \le 2 \cdot 10^5) — the number of sorts of tea and the number of tasters.The second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9) — the amount of each sort of tea.The third line contains n integers b_1, b_2, \dots, b_n (1 \le b_i \le 10^9) — the amount of tea each taster can drink at once.The sum of n over all testcases doesn't exceed 2 \cdot 10^5.OutputFor each testcase, print n integers — the i-th value should be equal to the total amount of tea the i-th taster will drink.ExampleInput 4310 20 159 8 6157413 8 5 43 4 2 131000000000 1000000000 10000000001 1 1000000000Output 9 9 12 5 3 8 6 4 1 2 2999999997 NoteThe first testcase is described in the statement. Here are the remaining amounts of each sort of tea after each step and the total amount of tea each taster has drunk: a = [1, 12, 9], \mathit{ans} = [9, 8, 6] a = [0, 6, 9], \mathit{ans} = [9, 9, 12] a = [0, 6, 9], \mathit{ans} = [9, 9, 12] In the second testcase, the only taster drinks \min(5, 7) milliliters of tea of the only sort.Here are the remaining amounts of each sort of tea after each step and the total amount of tea each taster has drunk for the third testcase: a = [10, 4, 3, 3], \mathit{ans} = [3, 4, 2, 1]; a = [6, 2, 2, 3], \mathit{ans} = [3, 8, 4, 2]; a = [4, 1, 2, 3], \mathit{ans} = [3, 8, 6, 3]; a = [3, 1, 2, 3], \mathit{ans} = [3, 8, 6, 4]. Here are the remaining amounts of each sort of tea after each step and the total amount of tea each taster has drunk for the fourth testcase: a = [999999999, 999999999, 0], \mathit{ans} = [1, 1, 1000000000]; a = [999999998, 0, 0], \mathit{ans} = [1, 2, 1999999999]; a = [0, 0, 0], \mathit{ans} = [1, 2, 2999999997].
4310 20 159 8 6157413 8 5 43 4 2 131000000000 1000000000 10000000001 1 1000000000
9 9 12 5 3 8 6 4 1 2 2999999997
2 seconds
256 megabytes
['binary search', 'data structures', 'implementation', '*1500']
B. Ideal Pointtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n one-dimensional segments (each segment is denoted by two integers — its endpoints).Let's define the function f(x) as the number of segments covering point x (a segment covers the point x if l \le x \le r, where l is the left endpoint and r is the right endpoint of the segment).An integer point x is called ideal if it belongs to more segments than any other integer point, i. e. f(y) < f(x) is true for any other integer point y.You are given an integer k. Your task is to determine whether it is possible to remove some (possibly zero) segments, so that the given point k becomes ideal.InputThe first line contains one integer t (1 \le t \le 1000) — the number of test cases.The first line of each test case contains two integers n and k (1 \le n, k \le 50).Then n lines follow, i-th line of them contains two integers l_i and r_i (1 \le l_i, r_i \le 50; l_i \le r_i) — the endpoints of the i-th segment.OutputFor each test case, print YES if it is possible to remove some (possibly zero) segments, so that the given point k becomes ideal, otherwise print NO.You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).ExampleInput 44 31 37 92 53 62 91 43 71 32 43 51 46 75 5Output YES NO NO YES NoteIn the first example, the point 3 is already ideal (it is covered by three segments), so you don't have to delete anything.In the fourth example, you can delete everything except the segment [5, 5].
44 31 37 92 53 62 91 43 71 32 43 51 46 75 5
YES NO NO YES
2 seconds
256 megabytes
['brute force', 'geometry', 'greedy', '*900']
A. Two Towerstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are two towers consisting of blocks of two colors: red and blue. Both towers are represented by strings of characters B and/or R denoting the order of blocks in them from the bottom to the top, where B corresponds to a blue block, and R corresponds to a red block. These two towers are represented by strings BRBB and RBR. You can perform the following operation any number of times: choose a tower with at least two blocks, and move its top block to the top of the other tower. The pair of towers is beautiful if no pair of touching blocks has the same color; i. e. no red block stands on top of another red block, and no blue block stands on top of another blue block.You have to check if it is possible to perform any number of operations (possibly zero) to make the pair of towers beautiful.InputThe first line contains one integer t (1 \le t \le 1000) — the number of test cases.Each test case consists of three lines: the first line contains two integers n and m (1 \le n, m \le 20) — the number of blocks in the first tower and the number of blocks in the second tower, respectively; the second line contains s — a string of exactly n characters B and/or R, denoting the first tower; the third line contains t — a string of exactly m characters B and/or R, denoting the second tower. OutputFor each test case, print YES if it is possible to perform several (possibly zero) operations in such a way that the pair of towers becomes beautiful; otherwise print NO.You may print each letter in any case (YES, yes, Yes will all be recognized as positive answer, NO, no and nO will all be recognized as negative answer).ExampleInput 44 3BRBBRBR4 7BRBRRRBRBRB3 4RBRBRBR5 4BRBRRBRBROutput YES YES YES NO NoteIn the first test case, you can move the top block from the first tower to the second tower (see the third picture).In the second test case, you can move the top block from the second tower to the first tower 6 times.In the third test case, the pair of towers is already beautiful.
44 3BRBBRBR4 7BRBRRRBRBRB3 4RBRBRBR5 4BRBRRBRBR
YES YES YES NO
2 seconds
256 megabytes
['brute force', 'implementation', 'strings', '*800']
E. Labeling the Tree with Distancestime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an unweighted tree of n vertices numbered from 1 to n and a list of n-1 integers a_1, a_2, \ldots, a_{n-1}. A tree is a connected undirected graph without cycles. You will use each element of the list to label one vertex. No vertex should be labeled twice. You can label the only remaining unlabeled vertex with any integer. A vertex x is called good if it is possible to do this labeling so that for each vertex i, its label is the distance between x and i. The distance between two vertices s and t on a tree is the minimum number of edges on a path that starts at vertex s and ends at vertex t. Find all good vertices.InputThe first line contains one integer n (2\le n\le 2\cdot 10^5) — the number of vertices in the tree.The second line contains n - 1 integers a_1,a_2,\ldots,a_{n-1} (0\le a_i < n) — the given list.Then, n−1 lines follow. Each of them contains two integers u and v (1\le u,v\le n) denoting an edge between vertices u and v. It is guaranteed that the edges form a tree.OutputIn the first line print the number of good vertices.In the second line, print the indices of all good vertices in ascending order.ExamplesInput 6 2 1 0 1 2 1 2 2 3 2 4 4 5 4 6 Output 2 2 4 Input 5 1 2 1 2 1 2 3 2 3 4 5 4 Output 1 3 Input 3 2 2 1 2 2 3 Output 0 NoteThis is the tree for the first example: And these are two possible labelings with the elements on the list so that 2 is a good vertex (left) and 4 is a good vertex (right). The square below each vertex represents its label. The black squares contain the numbers which were on the given list and the only white square contains the only number which was not on the given list.In the second example, the only good vertex is vertex 3.In the third example, there are no good vertices.
6 2 1 0 1 2 1 2 2 3 2 4 4 5 4 6
2 2 4
4 seconds
256 megabytes
['data structures', 'dp', 'greedy', 'hashing', 'implementation', 'trees', '*2400']
D. Counting Factorizationstime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe prime factorization of a positive integer m is the unique way to write it as \displaystyle m=p_1^{e_1}\cdot p_2^{e_2}\cdot \ldots \cdot p_k^{e_k}, where p_1, p_2, \ldots, p_k are prime numbers, p_1 < p_2 < \ldots < p_k and e_1, e_2, \ldots, e_k are positive integers. For each positive integer m, f(m) is defined as the multiset of all numbers in its prime factorization, that is f(m)=\{p_1,e_1,p_2,e_2,\ldots,p_k,e_k\}. For example, f(24)=\{2,3,3,1\}, f(5)=\{1,5\} and f(1)=\{\}.You are given a list consisting of 2n integers a_1, a_2, \ldots, a_{2n}. Count how many positive integers m satisfy that f(m)=\{a_1, a_2, \ldots, a_{2n}\}. Since this value may be large, print it modulo 998\,244\,353.InputThe first line contains one integer n (1\le n \le 2022).The second line contains 2n integers a_1, a_2, \ldots, a_{2n} (1\le a_i\le 10^6)  — the given list.OutputPrint one integer, the number of positive integers m satisfying f(m)=\{a_1, a_2, \ldots, a_{2n}\} modulo 998\,244\,353.ExamplesInput 2 1 3 2 3 Output 2 Input 2 2 2 3 5 Output 5 Input 1 1 4 Output 0 NoteIn the first sample, the two values of m such that f(m)=\{1,2,3,3\} are m=24 and m=54. Their prime factorizations are 24=2^3\cdot 3^1 and 54=2^1\cdot 3^3.In the second sample, the five values of m such that f(m)=\{2,2,3,5\} are 200, 225, 288, 500 and 972.In the third sample, there is no value of m such that f(m)=\{1,4\}. Neither 1^4 nor 4^1 are prime factorizations because 1 and 4 are not primes.
2 1 3 2 3
2
4 seconds
256 megabytes
['combinatorics', 'divide and conquer', 'dp', 'math', 'number theory', '*1900']
C. Scoring Subsequencestime limit per test2.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe score of a sequence [s_1, s_2, \ldots, s_d] is defined as \displaystyle \frac{s_1\cdot s_2\cdot \ldots \cdot s_d}{d!}, where d!=1\cdot 2\cdot \ldots \cdot d. In particular, the score of an empty sequence is 1.For a sequence [s_1, s_2, \ldots, s_d], let m be the maximum score among all its subsequences. Its cost is defined as the maximum length of a subsequence with a score of m.You are given a non-decreasing sequence [a_1, a_2, \ldots, a_n] of integers of length n. In other words, the condition a_1 \leq a_2 \leq \ldots \leq a_n is satisfied. For each k=1, 2, \ldots , n, find the cost of the sequence [a_1, a_2, \ldots , a_k]. A sequence x is a subsequence of a sequence y if x can be obtained from y by deletion of several (possibly, zero or all) elements.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of the test cases follows.The first line of each test case contains an integer n (1\le n\le 10^5) — the length of the given sequence. The second line of each test case contains n integers a_1,a_2,\ldots,a_n (1\le a_i\leq n) — the given sequence. It is guaranteed that its elements are in non-decreasing order.It is guaranteed that the sum of n over all test cases does not exceed 5\cdot 10^5.OutputFor each test case, output n integers — the costs of sequences [a_1, a_2, \ldots , a_k] in ascending order of k.ExampleInput 331 2 321 155 5 5 5 5Output 1 1 2 1 1 1 2 3 4 5 NoteIn the first test case: The maximum score among the subsequences of [1] is 1. The subsequences [1] and [] (the empty sequence) are the only ones with this score. Thus, the cost of [1] is 1. The maximum score among the subsequences of [1, 2] is 2. The only subsequence with this score is [2]. Thus, the cost of [1, 2] is 1. The maximum score among the subsequences of [1, 2, 3] is 3. The subsequences [2, 3] and [3] are the only ones with this score. Thus, the cost of [1, 2, 3] is 2. Therefore, the answer to this case is 1\:\:1\:\:2, which are the costs of [1], [1, 2] and [1, 2, 3] in this order.
331 2 321 155 5 5 5 5
1 1 2 1 1 1 2 3 4 5
2.5 seconds
256 megabytes
['binary search', 'greedy', 'math', 'two pointers', '*1300']
B. Not Dividingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array of n positive integers a_1, a_2, \ldots, a_n. In one operation, you can choose any number of the array and add 1 to it. Make at most 2n operations so that the array satisfies the following property: a_{i+1} is not divisible by a_i, for each i = 1, 2, \ldots, n-1. You do not need to minimize the number of operations.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^4). The description of the test cases follows.The first line of each test case contains an integer n (1\le n\le 10^4) — the length of the given array. The second line of each test case contains n integers a_1,a_2,\ldots,a_n (1\le a_i\leq 10^9) — the given array.It is guaranteed that the sum of n over all test cases does not exceed 5\cdot 10^4.OutputFor each test case, print the answer on a separate line. In the only line, print n integers — the resulting array a after applying at most 2n operations. We can show that an answer always exists under the given constraints. If there are multiple answers, print any of them.ExampleInput 342 4 3 631 2 324 2Output 4 5 6 7 3 2 3 4 2 NoteIn the first test case, the array [4, 5, 6, 7] can be achieved by applying 2 operations to the first element, 1 operation to the second element, 3 operations to the third element, and 1 operation to the last element. The total number of operations performed is 7, which is less than the allowed 8 operations in this case.In the second test case, the array [3, 2, 3] can be achieved by applying two operations to the first element. Another possible resulting array could be [2, 3, 5], because the total number of operations does not need to be minimum.In the third test case, not applying any operations results in an array that satisfies the statement's property. Observe that it is not mandatory to make operations.
342 4 3 631 2 324 2
4 5 6 7 3 2 3 4 2
2 seconds
256 megabytes
['constructive algorithms', 'greedy', 'math', '*900']
A. Prefix and Suffix Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMarcos loves strings a lot, so he has a favorite string s consisting of lowercase English letters. For this string, he wrote down all its non-empty prefixes and suffixes (except for s) on a piece of paper in arbitrary order. You see all these strings and wonder if Marcos' favorite string is a palindrome or not. So, your task is to decide whether s is a palindrome by just looking at the piece of paper.A string a is a prefix of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the end.A string a is a suffix of a string b if a can be obtained from b by deletion of several (possibly, zero or all) characters from the beginning.A palindrome is a string that reads the same backward as forward, for example, strings "gg", "ioi", "abba", "icpci" are palindromes, but strings "codeforces", "abcd", "alt" are not.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 120) — the number of test cases. The description of test cases follows.The first line of each test case contains a single integer n (2\le n \le 20) — the length of the string s.The second line of each test case contains 2n-2 strings a_1, a_2, \cdots, a_{2n-2} — all non-empty prefixes and suffixes of s, not including itself, in arbitrary order.It is guaranteed that these strings are all the non-empty prefixes and suffixes of some string consisting of lowercase English letters.OutputFor each test case, output "YES" if s is a palindrome, and "NO" otherwise.You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.ExampleInput 54bcd cd a d abc ab3i io i oi2g g3t al lt a4bba a ab a abb baOutput NO YES YES NO YES NoteIn the first test case, s is "abcd". Its prefixes are "a", "ab" and "abc", and its suffixes are "d", "cd" and "bcd". As the string "abcd" is not a palindrome, the answer is NO.In the second test case, s is "ioi". Its prefixes are "i" and "io", and its suffixes are "i" and "oi". As the string "ioi" is a palindrome, the answer is YES.In the third test case, s is "gg" which is a palindrome.In the fourth test case, s is "alt" which is not a palindrome.
54bcd cd a d abc ab3i io i oi2g g3t al lt a4bba a ab a abb ba
NO YES YES NO YES
1 second
256 megabytes
['strings', '*800']
F. Rebrendingtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputKostya and Zhenya — the creators of the band "Paper" — after the release of the legendary album decided to create a new band "Day movers", for this they need to find two new people.They invited n people to the casting. The casting will last q days. On the ith of the days, Kostya and Zhenya want to find two people on the segment from l_i to r_i who are most suitable for their band. Since "Day movers" are doing a modern art, musical skills are not important to them and they look only at other signs: they want the height difference between two people to be as small as possible.Help them, and for each day, find the minimum difference in the growth of people from the casting on this segment!InputIn the first line you are given two integers n and q (2 \leq n \leq 3 \cdot 10^5, 1 \leq q \leq 10^6) — the number of people who came to the casting and the number of casting days.In the second line, you are given n integers a_1, a_2, a_3, \ldots, a_n (1 \leq a_i \leq n) — the growth of each of the candidates.It is also guaranteed that all a_i are different.The following q lines each contains two integers l_i and r_i (1 \leq l_i < r_i \leq n) — a segment of people on the ith day of casting.OutputOutput q lines. In the i-th line there should be a minimum height difference between the two candidates on the segment on the i-th day of casting.ExamplesInput 3 31 3 21 22 31 3Output 2 1 1 Input 5 34 1 5 3 21 23 42 4Output 3 2 2 Input 7 42 6 1 7 3 5 44 61 23 61 3Output 2 4 2 1 NoteIn the first example, the minimum difference on the segment [1, 2] is 2, on the segment [2, 3] — 1, on the segment [1, 3] is also 1.In the third example, the numbers with the minimum difference on the segment [4, 6] are 3 and 5 (5 - 3 = 2). On the segment [1, 2], the numbers with the minimum difference are 2 and 6 (6 - 2 = 4). On the segment [3, 6], the numbers with the minimum difference are 1 and 3 (3 - 1 = 2). On the segment [1, 3], the minimum difference is formed by the numbers 1 and 2 (2 - 1 = 1).
3 31 3 21 22 31 3
2 1 1
4 seconds
256 megabytes
['brute force', 'data structures', 'divide and conquer', 'implementation', '*2600']
E. Velepin and Marketingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe famous writer Velepin is very productive. Recently, he signed a contract with a well-known publication and now he needs to write k_i books for i-th year. This is not a problem for him at all, he can write as much as he wants about samurai, space, emptiness, insects and werewolves.He has n regular readers, each of whom in the i-th year will read one of the k_i books published by Velepin. Readers are very fond of discussing books, so the j-th of them will be satisfied within a year if at least a_j persons read the same book as him (including himself).Velepin has obvious problems with marketing, so he turned to you! A well-known book reading service can control what each of Velepin's regular readers will read, but he does not want books to be wasted, so someone should read each book. And so they turned to you with a request to tell you what the maximum number of regular readers can be made satisfied during each of the years, if you can choose each person the book he will read.InputThe first line contains a single integer n (2 \le n \le 3 \cdot 10^5) — the number of regular readers of Velepin.The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le n) — the number of necessary readers of the same book for the happiness of the i-th person.The third line contains a single integer q (1 \le q \le 3 \cdot 10^5) — the number of years to analyze.Each of the following q lines contains a single integer k_j (2 \le k_j \le n) — the number of books that Velepin must write in j-th a year.OutputPrint q lines, each of them has exactly one number — the maximum number of people who can be satisfied in j-th a year if Velepin releases k_j books.ExamplesInput 5 1 2 2 2 2 3 2 3 4 Output 5 5 3 Input 6 1 2 3 4 5 6 2 2 3 Output 5 4 Input 6 4 4 1 4 4 4 3 2 3 4 Output 6 5 1 NoteIn the first example, in the first year, the optimal division is 1, 2, 2, 2, 2 (the first book is read by the first person, and everyone else reads the second). In the second year, the optimal solution is 1, 2, 2, 3, 3 (the first book is read by the first person, the second book is read by the second and third person, and all the others read the third book). In the third year, the optimal split will be 1, 2, 3, 4, 2. Accordingly, the number of satisfied people over the years will be 5, 5, 3.In the second example, in the first year, the optimal division is 1, 1, 1, 1, 1, 2, then everyone will be happy except the 6-th person. In the second year, the optimal division is 1, 1, 1, 1, 2, 3, then everyone will be happy except the 5-th and 6-th person.
5 1 2 2 2 2 3 2 3 4
5 5 3
2 seconds
256 megabytes
['binary search', 'data structures', 'dp', 'greedy', 'sortings', 'two pointers', '*2600']
D. Moscow Gorillastime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn winter, the inhabitants of the Moscow Zoo are very bored, in particular, it concerns gorillas. You decided to entertain them and brought a permutation p of length n to the zoo.A permutation of length n is an array consisting of n distinct integers from 1 to n in any order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 occurs twice in the array) and [1,3,4] is also not a permutation (n=3, but 4 is present in the array).The gorillas had their own permutation q of length n. They suggested that you count the number of pairs of integers l, r (1 \le l \le r \le n) such that \operatorname{MEX}([p_l, p_{l+1}, \ldots, p_r])=\operatorname{MEX}([q_l, q_{l+1}, \ldots, q_r]).The \operatorname{MEX} of the sequence is the minimum integer positive number missing from this sequence. For example, \operatorname{MEX}([1, 3]) = 2, \operatorname{MEX}([5]) = 1, \operatorname{MEX}([3, 1, 2, 6]) = 4.You do not want to risk your health, so you will not dare to refuse the gorillas.InputThe first line contains a single integer n (1 \le n \le 2 \cdot 10^5) — the permutations length.The second line contains n integers p_1, p_2, \ldots, p_n (1 \le p_i \le n) — the elements of the permutation p.The third line contains n integers q_1, q_2, \ldots, q_n (1 \le q_i \le n) — the elements of the permutation q.OutputPrint a single integer — the number of suitable pairs l and r.ExamplesInput 31 3 22 1 3Output 2 Input 77 3 6 2 1 5 46 7 2 5 3 1 4Output 16 Input 61 2 3 4 5 66 5 4 3 2 1Output 11 NoteIn the first example, two segments are correct – [1, 3] with \operatorname{MEX} equal to 4 in both arrays and [3, 3] with \operatorname{MEX} equal to 1 in both of arrays.In the second example, for example, the segment [1, 4] is correct, and the segment [6, 7] isn't correct, because \operatorname{MEX}(5, 4) \neq \operatorname{MEX}(1, 4).
31 3 22 1 3
2
2 seconds
256 megabytes
['binary search', 'dp', 'greedy', 'implementation', 'math', 'two pointers', '*1800']
C. Dora and Searchtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAs you know, the girl Dora is always looking for something. This time she was given a permutation, and she wants to find such a subsegment of it that none of the elements at its ends is either the minimum or the maximum of the entire subsegment. More formally, you are asked to find the numbers l and r (1 \leq l \leq r \leq n) such that a_l \neq \min(a_l, a_{l + 1}, \ldots, a_r), a_l \neq \max(a_l, a_{l + 1}, \ldots, a_r) and a_r \neq \min(a_l, a_{l + 1}, \ldots, a_r), a_r \neq \max(a_l, a_{l + 1}, \ldots, a_r).A permutation of length n is an array consisting of n distinct integers from 1 to n in any order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 occurs twice in the array) and [1,3,4] is also not a permutation (n=3, but 4 is present in the array).Help Dora find such a subsegment, or tell her that such a subsegment does not exist.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. Description of the test cases follows.For each test case, the first line contains one integer n (1 \leq n \leq 2 \cdot 10^5) — the length of permutation.The second line contains n distinct integers a_1, a_2, \ldots, a_n (1 \leq a_i \leq n) — the elements of permutation. It is guarented that the sum of n over all test cases doesn't exceed 2 \cdot 10^5.OutputFor each test case, output -1 if the desired subsegment does not exist.Otherwise, output two indexes l, r such that [a_{l}, a_{l + 1}, \ldots, a_{r}] satisfies all conditions.If there are several solutions, then output any of them.ExampleInput 431 2 342 1 4 371 3 2 4 6 5 762 3 6 5 4 1Output -1 1 4 2 6 -1 NoteIn the first and fourth test cases, it can be shown that there are no desired subsegments.In the second test case, the subsegment [1, 4] satisfies all the conditions, because \max(a_1, a_2, a_3, a_4) = 4, \min(a_1, a_2, a_3, a_4) = 1, as we see, all the conditions are met.In the third test case, the subsegment [2, 6] also satisfies all the conditions described.
431 2 342 1 4 371 3 2 4 6 5 762 3 6 5 4 1
-1 1 4 2 6 -1
1 second
256 megabytes
['constructive algorithms', 'data structures', 'two pointers', '*1200']
B. Fedya and Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputFor his birthday recently Fedya was given an array a of n integers arranged in a circle, For each pair of neighboring numbers (a_1 and a_2, a_2 and a_3, \ldots, a_{n - 1} and a_n, a_n and a_1) the absolute difference between them is equal to 1.Let's call a local maximum an element, which is greater than both of its neighboring elements. Also call a local minimum an element, which is less than both of its neighboring elements. Note, that elements a_1 and a_n are neighboring elements.Unfortunately, Fedya lost an array, but he remembered in it the sum of local maximums x and the sum of local minimums y.Given x and y, help Fedya find any matching array of minimum length.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.Each line of each test case contain two integers x and y (-10^{9} \le y < x \le 10^{9}) — the sum of local maximums and the sum of local minimums, respectively.OutputFor each test case, in the first line print one integer n — the minimum length of matching arrays.In the second line print n integers a_1, a_2, \ldots, a_n (-10^{9} \leqslant a_i \leqslant 10^{9}) — the array elements such that the the absolute difference between each pair of neighboring is equal to 1.If there are multiple solutions, print any of them.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^{5}.ExampleInput 43 -24 -42 -15 -3Output 10 0 1 2 1 0 -1 0 -1 0 1 16 -2 -1 -2 -1 0 1 2 3 4 5 4 3 2 1 0 -1 6 1 0 -1 0 1 0 16 2 3 2 1 0 -1 0 -1 0 -1 0 1 2 1 0 1NoteIn the first test case, the local maximums are the numbers at 3, 7 and 10 positions, and the local minimums are the numbers at 1, 6 and 8 positions. x = a_3 + a_7 + a_{10} = 2 + 0 + 1 = 3, y = a_1 + a_6 + a_8 = 0 + (-1) + (-1) = -2.In the second test case, the local maximums are the numbers at 2 and 10 positions, and the local minimums are the numbers at 1 and 3 positions. x = a_2 + a_{10} = -1 + 5 = 4, y = a_1 + a_3 = -2 + (-2) = -4.In the third test case, the local maximums are the numbers at 1 and 5 positions, and the local minimums are the numbers at 3 and 6 positions.
43 -24 -42 -15 -3
10 0 1 2 1 0 -1 0 -1 0 1 16 -2 -1 -2 -1 0 1 2 3 4 5 4 3 2 1 0 -1 6 1 0 -1 0 1 0 16 2 3 2 1 0 -1 0 -1 0 -1 0 1 2 1 0 1
1 second
256 megabytes
['constructive algorithms', 'math', '*1100']
A. Yet Another Promotiontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe famous store "Second Food" sells groceries only two days a month. And the prices in each of days differ. You wanted to buy n kilos of potatoes for a month. You know that on the first day of the month 1 kilo of potatoes costs a coins, and on the second day b coins. In "Second Food" you can buy any integer kilograms of potatoes.Fortunately, "Second Food" has announced a promotion for potatoes, which is valid only on the first day of the month — for each m kilos of potatoes you buy, you get 1 kilo as a gift! In other words, you can get m + 1 kilograms by paying for m kilograms.Find the minimum number of coins that you have to spend to buy at least n kilos of potatoes.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10\,000). Description of the test cases follows.The first line of each test case contains two integers a and b (1 \leq a, b \leq 10^9) — the prices of 1 kilo of potatoes on the first and second days, respectively.The second line contains two integers n and m (1 \leq n, m \leq 10^9) — the required amount of potatoes to buy and the amount of potatoes to use the promotion.OutputFor each test case print one integer — the minimum number of coins that you have to pay to buy at least n kilos of potatoes.ExampleInput 55 43 15 43 23 43 520 1510 21000000000 9000000001000000000 8Output 9 10 9 135 888888888900000000 NoteIn the first test case, on the first day you buy 1 kilo and get 1 more for a promotion. On the second day, you can buy 1 kilo of potatoes. Thus, you will spend 5+4=9 coins in total.In the second test case, on the first day you buy 2 kilo and get another 1 more for a promotion. This way you will spend 2 \cdot 5 = 10 coins.
55 43 15 43 23 43 520 1510 21000000000 9000000001000000000 8
9 10 9 135 888888888900000000
1 second
256 megabytes
['greedy', 'math', '*800']
F2. Graph Coloring (hard version)time limit per test5.5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe only difference between the easy and the hard version is the constraint on n.You are given an undirected complete graph on n vertices. A complete graph is a graph where each pair of vertices is connected by an edge. You have to paint the edges of the graph into two colors, red and blue (each edge will have one color).A set of vertices S is red-connected if, for every pair of vertices (v_1, v_2) such that v_1 \in S and v_2 \in S, there exists a path from v_1 to v_2 that goes only through red edges and vertices from S. Similarly, a set of vertices S is blue-connected if, for every pair of vertices (v_1, v_2) such that v_1 \in S and v_2 \in S, there exists a path from v_1 to v_2 that goes only through blue edges and vertices from S.You have to paint the graph in such a way that: there is at least one red edge; there is at least one blue edge; for each set of vertices S such that |S| \ge 2, S is either red-connected or blue-connected, but not both. Calculate the number of ways to paint the graph, and print it modulo 998244353.InputThe first (and only) line contains one integer n (3 \le n \le 5 \cdot 10^4).OutputPrint one integer — the number of ways to paint the graph, taken modulo 998244353.ExamplesInput 3 Output 6 Input 4 Output 50 Input 100 Output 878752271 Input 1337 Output 520628749 Input 42013 Output 906821221
3
6
5.5 seconds
512 megabytes
['brute force', 'combinatorics', 'divide and conquer', 'dp', 'fft', 'graphs', '*2900']
F1. Graph Coloring (easy version)time limit per test5.5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThe only difference between the easy and the hard version is the constraint on n.You are given an undirected complete graph on n vertices. A complete graph is a graph where each pair of vertices is connected by an edge. You have to paint the edges of the graph into two colors, red and blue (each edge will have one color).A set of vertices S is red-connected if, for every pair of vertices (v_1, v_2) such that v_1 \in S and v_2 \in S, there exists a path from v_1 to v_2 that goes only through red edges and vertices from S. Similarly, a set of vertices S is blue-connected if, for every pair of vertices (v_1, v_2) such that v_1 \in S and v_2 \in S, there exists a path from v_1 to v_2 that goes only through blue edges and vertices from S.You have to paint the graph in such a way that: there is at least one red edge; there is at least one blue edge; for each set of vertices S such that |S| \ge 2, S is either red-connected or blue-connected, but not both. Calculate the number of ways to paint the graph, and print it modulo 998244353.InputThe first (and only) line contains one integer n (3 \le n \le 5 \cdot 10^3).OutputPrint one integer — the number of ways to paint the graph, taken modulo 998244353.ExamplesInput 3 Output 6 Input 4 Output 50 Input 100 Output 878752271 Input 1337 Output 520628749
3
6
5.5 seconds
512 megabytes
['combinatorics', 'dp', 'graphs', '*2700']
E. Divisors and Tabletime limit per test2.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an n \times n multiplication table and a positive integer m = m_1 \cdot m_2. A n \times n multiplication table is a table with n rows and n columns numbered from 1 to n, where a_{i, j} = i \cdot j.For each divisor d of m, check: does d occur in the table at least once, and if it does, what is the minimum row that contains d.InputThe first line contains a single integer t (1 \le t \le 10) — the number of test cases.The first and only line of each test case contains three integers n, m_1 and m_2 (1 \le n \le 10^9; 1 \le m_1, m_2 \le 10^9) — the size of the multiplication table and the integer m represented as m_1 \cdot m_2.OutputFor each test case, let d_1, d_2, \dots, d_k be all divisors of m sorted in the increasing order. And let a_1, a_2, \dots, a_k be an array of answers, where a_i is equal to the minimum row index where divisor d_i occurs, or 0, if there is no such row.Since array a may be large, first, print an integer s — the number of divisors of m that are present in the n \times n table. Next, print a single value X = a_1 \oplus a_2 \oplus \dots \oplus a_k, where \oplus denotes the bitwise XOR operation.ExampleInput 33 72 110 10 156 1 210Output 6 2 10 0 8 5 NoteIn the first test case, m = 72 \cdot 1 = 72 and has 12 divisors [1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 36, 72]. The 3 \times 3 multiplication table looks like that: 123112322463369 For each divisor of m that is present in the table, the position with minimum row index is marked. So the array of answers a is equal to [1, 1, 1, 2, 2, 0, 3, 0, 0, 0, 0, 0]. There are only 6 non-zero values, and xor of a is equal to 2.In the second test case, m = 10 \cdot 15 = 150 and has 12 divisors [1, 2, 3, 5, 6, 10, 15, 25, 30, 50, 75, 150]. All divisors except 75 and 150 are present in the 10 \times 10 table. Array a = [1, 1, 1, 1, 1, 1, 3, 5, 3, 5, 0, 0]. There are 10 non-zero values, and xor of a is equal to 0.In the third test case, m = 1 \cdot 210 = 210 and has 16 divisors [1, 2, 3, 5, 6, 7, 10, 14, 15, 21, 30, 35, 42, 70, 105, 210]. The 6 \times 6 table with marked divisors is shown below: 1234561123456224681012336912151844812162024551015202530661218243036 Array a = [1, 1, 1, 1, 1, 0, 2, 0, 3, 0, 5, 0, 0, 0, 0, 0]. There are 8 non-zero values, and xor of a is equal to 5.
33 72 110 10 156 1 210
6 2 10 0 8 5
2.5 seconds
256 megabytes
['brute force', 'dfs and similar', 'dp', 'number theory', '*2400']
D. Fixed Prefix Permutationstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given n permutations a_1, a_2, \dots, a_n, each of length m. Recall that a permutation of length m is a sequence of m distinct integers from 1 to m.Let the beauty of a permutation p_1, p_2, \dots, p_m be the largest k such that p_1 = 1, p_2 = 2, \dots, p_k = k. If p_1 \neq 1, then the beauty is 0.The product of two permutations p \cdot q is a permutation r such that r_j = q_{p_j}.For each i from 1 to n, print the largest beauty of a permutation a_i \cdot a_j over all j from 1 to n (possibly, i = j).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 two integers n and m (1 \le n \le 5 \cdot 10^4; 1 \le m \le 10) — the number of permutations and the length of each permutation.The i-th of the next n lines contains a permutation a_i — m distinct integers from 1 to m.The sum of n doesn't exceed 5 \cdot 10^4 over all testcases.OutputFor each testcase, print n integers. The i-th value should be equal to the largest beauty of a permutation a_i \cdot a_j over all j (1 \le j \le n).ExampleInput 33 42 4 1 31 2 4 32 1 3 42 21 22 18 103 4 9 6 10 2 7 8 1 53 9 1 8 5 7 4 10 2 63 10 1 7 5 9 6 4 2 81 2 3 4 8 6 10 7 9 51 2 3 4 10 6 8 5 7 99 6 1 2 10 4 7 8 3 57 9 3 2 5 6 4 8 1 109 4 3 7 5 6 1 10 8 2Output 1 4 4 2 2 10 8 1 6 8 10 1 7
33 42 4 1 31 2 4 32 1 3 42 21 22 18 103 4 9 6 10 2 7 8 1 53 9 1 8 5 7 4 10 2 63 10 1 7 5 9 6 4 2 81 2 3 4 8 6 10 7 9 51 2 3 4 10 6 8 5 7 99 6 1 2 10 4 7 8 3 57 9 3 2 5 6 4 8 1 109 4 3 7 5 6 1 10 8 2
1 4 4 2 2 10 8 1 6 8 10 1 7
2 seconds
256 megabytes
['binary search', 'bitmasks', 'data structures', 'hashing', 'math', 'sortings', '*1700']
C. Min Max Sorttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a permutation p of length n (a permutation of length n is an array of length n in which each integer from 1 to n occurs exactly once).You can perform the following operation any number of times (possibly zero): choose two different elements x and y and erase them from the permutation; insert the minimum of x and y into the permutation in such a way that it becomes the first element; insert the maximum of x and y into the permutation in such a way that it becomes the last element. For example, if p = [1, 5, 4, 2, 3] and we want to apply the operation to the elements 3 and 5, then after the first step of the operation, the permutation becomes p = [1, 4, 2]; and after we insert the elements, it becomes p = [3, 1, 4, 2, 5].Your task is to calculate the minimum number of operations described above to sort the permutation p in ascending order (i. e. transform p so that p_1 < p_2 < \dots < p_n).InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of the test case contains a single integer n (1 \le n \le 2 \cdot 10^5) — the number of elements in the permutation.The second line of the test case contains n distinct integers from 1 to n — the given permutation p.The sum of n over all test cases doesn't exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the minimum number of operations described above to sort the array p in ascending order.ExampleInput 451 5 4 2 331 2 342 1 4 365 2 4 1 6 3Output 2 0 1 3 NoteIn the first example, you can proceed as follows: in the permutation p = [1, 5, 4, 2, 3], let's choose the elements 4 and 2, then, after applying the operation, the permutation becomes p = [2, 1, 5, 3, 4]; in the permutation p = [2, 1, 5, 3, 4], let's choose the elements 1 and 5, then, after applying operation, the permutation becomes p = [1, 2, 3, 4, 5].
451 5 4 2 331 2 342 1 4 365 2 4 1 6 3
2 0 1 3
2 seconds
256 megabytes
['binary search', 'brute force', 'greedy', 'math', 'two pointers', '*1500']
B. Stand-up Comediantime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputEve is a beginner stand-up comedian. Her first show gathered a grand total of two spectators: Alice and Bob.Eve prepared a_1 + a_2 + a_3 + a_4 jokes to tell, grouped by their type: type 1: both Alice and Bob like them; type 2: Alice likes them, but Bob doesn't; type 3: Bob likes them, but Alice doesn't; type 4: neither Alice nor Bob likes them. Initially, both spectators have their mood equal to 0. When a spectator hears a joke he/she likes, his/her mood increases by 1. When a spectator hears a joke he/she doesn't like, his/her mood decreases by 1. If the mood of a spectator becomes negative (strictly below zero), he/she leaves.When someone leaves, Eve gets sad and ends the show. If no one leaves, and Eve is out of jokes, she also ends the show.Thus, Eve wants to arrange her jokes in such a way that the show lasts as long as possible. Help her to calculate the maximum number of jokes she can tell before the show ends.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of testcases.The only line of each testcase contains four integers a_1, a_2, a_3, a_4 (0 \le a_1, a_2, a_3, a_4 \le 10^8; a_1 + a_2 + a_3 + a_4 \ge 1) — the number of jokes of each type Eve prepared.OutputFor each testcase, print a single integer — the maximum number of jokes Eve can tell before at least one of the spectators leaves or before she runs out of jokes.ExampleInput 45 0 0 00 0 0 52 5 10 63 0 0 7Output 5 1 15 7 NoteIn the first testcase, Eve only has jokes of the first type. Thus, there's no order to choose. She tells all her jokes, both Alice and Bob like them. Their mood becomes 5. The show ends after Eve runs out of jokes.In the second testcase, Eve only has jokes of the fourth type. Thus, once again no order to choose. She tells a joke, and neither Alice, nor Bob likes it. Their mood decrease by one, becoming -1. They both have negative mood, thus, both leave, and the show ends.In the third testcase, first, Eve tells both jokes of the first type. Both Alice and Bob has mood 2. Then she can tell 2 jokes of the third type. Alice's mood becomes 0. Bob's mood becomes 4. Then 4 jokes of the second type. Alice's mood becomes 4. Bob's mood becomes 0. Then another 4 jokes of the third type. Alice's mood becomes 0. Bob's mood becomes 4. Then the remaining joke of the second type. Alice's mood becomes 1. Bob's mood becomes 3. Then one more joke of the third type, and a joke of the fourth type, for example. Alice's mood becomes -1, she leaves, and the show ends. In the fourth testcase, Eve should first tell the jokes both spectators like, then the jokes they don't. She can tell 4 jokes of the fourth type until the spectators leave.
45 0 0 00 0 0 52 5 10 63 0 0 7
5 1 15 7
2 seconds
256 megabytes
['greedy', 'math', '*1200']
A. GamingForcestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp is playing a computer game. He's going to kill n monsters, the i-th of them has h_i health.Monocarp's character has two spells, either of which he can cast an arbitrary number of times (possibly, zero) and in an arbitrary order: choose exactly two alive monsters and decrease their health by 1; choose a single monster and kill it. When a monster's health becomes 0, it dies.What's the minimum number of spell casts Monocarp should perform in order to kill all monsters?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 n (1 \le n \le 100) — the number of monsters.The second line contains n integers h_1, h_2, \dots, h_n (1 \le h_i \le 100) — the health of each monster.The sum of n over all testcases doesn't exceed 2 \cdot 10^4.OutputFor each testcase, print a single integer — the minimum number of spell casts Monocarp should perform in order to kill all monsters.ExampleInput 341 2 1 232 4 251 2 3 4 5Output 3 3 5 NoteIn the first testcase, the initial health list is [1, 2, 1, 2]. Three spells are casted: the first spell on monsters 1 and 2 — monster 1 dies, monster 2 has now health 1, new health list is [0, 1, 1, 2]; the first spell on monsters 3 and 4 — monster 3 dies, monster 4 has now health 1, new health list is [0, 1, 0, 1]; the first spell on monsters 2 and 4 — both monsters 2 and 4 die. In the second testcase, the initial health list is [2, 4, 2]. Three spells are casted: the first spell on monsters 1 and 3 — both monsters have health 1 now, new health list is [1, 4, 1]; the second spell on monster 2 — monster 2 dies, new health list is [1, 0, 1]; the first spell on monsters 1 and 3 — both monsters 1 and 3 die. In the third testcase, the initial health list is [1, 2, 3, 4, 5]. Five spells are casted. The i-th of them kills the i-th monster with the second spell. Health list sequence: [1, 2, 3, 4, 5] \rightarrow [0, 2, 3, 4, 5] \rightarrow [0, 0, 3, 4, 5] \rightarrow [0, 0, 0, 4, 5] \rightarrow [0, 0, 0, 0, 5] \rightarrow [0, 0, 0, 0, 0].
341 2 1 232 4 251 2 3 4 5
3 3 5
2 seconds
256 megabytes
['greedy', 'sortings', '*800']
G2. Teleporters (Hard Version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe only difference between the easy and hard versions are the locations you can teleport to.Consider the points 0,1,\dots,n+1 on the number line. There is a teleporter located on each of the points 1,2,\dots,n. At point i, you can do the following: Move left one unit: it costs 1 coin. Move right one unit: it costs 1 coin. Use a teleporter at point i, if it exists: it costs a_i coins. As a result, you can choose whether to teleport to point 0 or point n+1. Once you use a teleporter, you can't use it again. You have c coins, and you start at point 0. What's the most number of teleporters you can use?InputThe input consists of multiple test cases. The first line contains an integer t (1 \leq t \leq 1000) — the number of test cases. The descriptions of the test cases follow.The first line of each test case contains two integers n and c (1 \leq n \leq 2\cdot10^5; 1 \leq c \leq 10^9)  — the length of the array and the number of coins you have respectively.The following line contains n space-separated positive integers a_1,a_2,\dots,a_n (1 \leq a_i \leq 10^9) — the costs to use the teleporters.It is guaranteed that the sum of n over all test cases does not exceed 2\cdot10^5.OutputFor each test case, output the maximum number of teleporters you can use.ExampleInput 105 61 1 1 1 18 32100 52 13 6 9 4 100 351 154 54 3 2 15 92 3 1 4 15 82 3 1 4 14 32 3 4 14 95 4 3 32 147 55 600000000500000000 400000000 300000000 200000000 100000000Output 2 3 0 1 3 2 1 1 2 2 NoteIn the first test case, you can move one unit to the right, use the teleporter at index 1 and teleport to point n+1, move one unit to the left and use the teleporter at index 5. You are left with 6-1-1-1-1 = 2 coins, and wherever you teleport, you won't have enough coins to use another teleporter. You have used two teleporters, so the answer is two.In the second test case, you go four units to the right and use the teleporter to go to n+1, then go three units left and use the teleporter at index 6 to go to n+1, and finally, you go left four times and use the teleporter. The total cost will be 4+6+3+4+4+9 = 30, and you used three teleporters.In the third test case, you don't have enough coins to use any teleporter, so the answer is zero.
105 61 1 1 1 18 32100 52 13 6 9 4 100 351 154 54 3 2 15 92 3 1 4 15 82 3 1 4 14 32 3 4 14 95 4 3 32 147 55 600000000500000000 400000000 300000000 200000000 100000000
2 3 0 1 3 2 1 1 2 2
1 second
256 megabytes
['binary search', 'greedy', 'sortings', '*1900']
G1. Teleporters (Easy Version)time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe only difference between the easy and hard versions are the locations you can teleport to.Consider the points 0, 1, \dots, n on the number line. There is a teleporter located on each of the points 1, 2, \dots, n. At point i, you can do the following: Move left one unit: it costs 1 coin. Move right one unit: it costs 1 coin. Use a teleporter at point i, if it exists: it costs a_i coins. As a result, you teleport to point 0. Once you use a teleporter, you can't use it again. You have c coins, and you start at point 0. What's the most number of teleporters you can use?InputThe input consists of multiple test cases. The first line contains an integer t (1 \leq t \leq 1000) — the number of test cases. The descriptions of the test cases follow.The first line of each test case contains two integers n and c (1 \leq n \leq 2\cdot10^5; 1 \leq c \leq 10^9)  — the length of the array and the number of coins you have respectively.The following line contains n space-separated integers a_1, a_2, \dots, a_n (1 \leq a_i \leq 10^9) — the costs to use the teleporters.It is guaranteed that the sum of n over all test cases does not exceed 2\cdot10^5.OutputFor each test case, output the maximum number of teleporters you can use.ExampleInput 105 61 1 1 1 18 32100 52 13 6 9 4 100 351 154 54 3 2 15 92 3 1 4 15 82 3 1 4 14 32 3 4 14 95 4 3 32 147 55 600000000500000000 400000000 300000000 200000000 100000000Output 2 2 0 1 2 2 1 1 1 2 NoteIn the first test case, you can move one unit to the right, use the teleporter at index 1 and teleport to point 0, move two units to the right and use the teleporter at index 2. You are left with 6-1-1-2-1 = 1 coins you don't have enough coins to use another teleporter. You have used two teleporters, so the answer is two.In the second test case, you go four units to the right and use the teleporter to go to 0, then go six units right and use the teleporter at index 6 to go to 0. The total cost will be 4+6+6+4 = 20. You are left with 12 coins, but it is not enough to reach any other teleporter and use it so the answer is 2.In the third test case, you don't have enough coins to use any teleporter, so the answer is zero.
105 61 1 1 1 18 32100 52 13 6 9 4 100 351 154 54 3 2 15 92 3 1 4 15 82 3 1 4 14 32 3 4 14 95 4 3 32 147 55 600000000500000000 400000000 300000000 200000000 100000000
2 2 0 1 2 2 1 1 1 2
1 second
256 megabytes
['greedy', 'sortings', '*1100']
F. Range Update Point Querytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array a_1, a_2, \dots, a_n, you need to handle a total of q updates and queries of two types: 1 l r — for each index i with l \leq i \leq r, update the value of a_i to the sum of the digits of a_i. 2 x — output a_x. InputThe first line of the input contains an integer t (1 \leq t \leq 1000) — the number of testcases.The first line of each test case contains two integers n and q (1 \le n, q \le 2 \cdot 10^5) — the size of the array and the number of queries, respectively.The second line of each test case contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9).The next q lines of each test case are of two forms: 1 l r (1 \leq l \leq r \leq n) — it means, for each index i with l \leq i \leq r, you should update the value of a_i to the sum of its digits. 2 x (1 \leq x \leq n) — it means you should output a_x. There is at least one query of the second type.The sum of n over all test cases does not exceed 2 \cdot 10^5.The sum of q over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output the answers of queries of the second type, in the order they are given.ExampleInput 35 81 420 69 1434 20231 2 32 22 32 41 2 52 12 32 52 39999 10001 1 22 12 21 112 1Output 6 15 1434 1 6 7 36 1 1 NoteIn the first test case, the following process occurs: Initially, a = [1, 420, 69, 1434, 2023]. The operation is performed for l=2, r=3, yielding [1, \color{red}{6}, \color{red}{15}, 1434, 2023]. We are queried for x=2, x=3, and x=4, and output 6, 15, and 1434. The operation is performed for l=2, r=5, yielding [1, \color{red}{6}, \color{red}{6}, \color{red}{12}, \color{red}{7}]. We are queried for x=1, x=3, and x=5, and output 1, 6, and 7.
35 81 420 69 1434 20231 2 32 22 32 41 2 52 12 32 52 39999 10001 1 22 12 21 112 1
6 15 1434 1 6 7 36 1 1
2 seconds
256 megabytes
['binary search', 'brute force', 'data structures', '*1500']
E. Negatives and Positivestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array a consisting of n elements, find the maximum possible sum the array can have after performing the following operation any number of times: Choose 2 adjacent elements and flip both of their signs. In other words choose an index i such that 1 \leq i \leq n - 1 and assign a_i = -a_i and a_{i+1} = -a_{i+1}. InputThe input consists of multiple test cases. The first line contains an integer t (1 \leq t \leq 1000) — the number of test cases. The descriptions of the test cases follow.The first line of each test case contains an integer n (2 \leq n \leq 2\cdot10^5) — the length of the array.The following line contains n space-separated integers a_1,a_2,\dots,a_n (-10^9 \leq a_i \leq 10^9).It is guaranteed that the sum of n over all test cases does not exceed 2\cdot10^5.OutputFor each test case, output the maximum possible sum the array can have after performing the described operation any number of times.ExampleInput 53-1 -1 -151 5 -5 0 231 2 36-1 10 9 8 7 62-1 -1Output 1 13 6 39 2 NoteFor the first test case, by performing the operation on the first two elements, we can change the array from [-1, -1, -1] to [1, 1, -1], and it can be proven this array obtains the maximum possible sum which is 1 + 1 + (-1) = 1.For the second test case, by performing the operation on -5 and 0, we change the array from [1, 5, -5, 0, 2] to [1, 5, -(-5), -0, 2] = [1, 5, 5, 0, 2], which has the maximum sum since all elements are non-negative. So, the answer is 1 + 5 + 5 + 0 + 2 = 13.For the third test case, the array already contains only positive numbers, so performing operations is unnecessary. The answer is just the sum of the whole array, which is 1 + 2 + 3 = 6.
53-1 -1 -151 5 -5 0 231 2 36-1 10 9 8 7 62-1 -1
1 13 6 39 2
2 seconds
256 megabytes
['dp', 'greedy', 'sortings', '*1100']
D. Distinct Splittime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's denote the f(x) function for a string x as the number of distinct characters that the string contains. For example f(\texttt{abc}) = 3, f(\texttt{bbbbb}) = 1, and f(\texttt{babacaba}) = 3.Given a string s, split it into two non-empty strings a and b such that f(a) + f(b) is the maximum possible. In other words, find the maximum possible value of f(a) + f(b) such that a + b = s (the concatenation of string a and string b is equal to string s).InputThe input consists of multiple test cases. The first line contains an integer t (1 \leq t \leq 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains an integer n (2 \leq n \leq 2\cdot10^5) — the length of the string s.The second line contains the string s, consisting of lowercase English letters.It is guaranteed that the sum of n over all test cases does not exceed 2\cdot10^5.OutputFor each test case, output a single integer  — the maximum possible value of f(a) + f(b) such that a + b = s.ExampleInput 52aa7abcabcd5aaaaa10paiumoment4aazzOutput 2 7 2 10 3 NoteFor the first test case, there is only one valid way to split \texttt{aa} into two non-empty strings \texttt{a} and \texttt{a}, and f(\texttt{a}) + f(\texttt{a}) = 1 + 1 = 2.For the second test case, by splitting \texttt{abcabcd} into \texttt{abc} and \texttt{abcd} we can get the answer of f(\texttt{abc}) + f(\texttt{abcd}) = 3 + 4 = 7 which is maximum possible.For the third test case, it doesn't matter how we split the string, the answer will always be 2.
52aa7abcabcd5aaaaa10paiumoment4aazz
2 7 2 10 3
2 seconds
256 megabytes
['brute force', 'greedy', 'strings', '*1000']
C. Prepend and Appendtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTimur initially had a binary string^{\dagger} s (possibly of length 0). He performed the following operation several (possibly zero) times: Add \texttt{0} to one end of the string and \texttt{1} to the other end of the string. For example, starting from the string \texttt{1011}, you can obtain either \color{red}{\texttt{0}}\texttt{1011}\color{red}{\texttt{1}} or \color{red}{\texttt{1}}\texttt{1011}\color{red}{\texttt{0}}. You are given Timur's final string. What is the length of the shortest possible string he could have started with?^{\dagger} A binary string is a string (possibly the empty string) whose characters are either \texttt{0} or \texttt{1}.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 2000) — the length of Timur's final string.The second line of each test case contains a string s of length n consisting of characters \texttt{0} or \texttt{1}, denoting the final string.OutputFor each test case, output a single nonnegative integer — the shortest possible length of Timur's original string. Note that Timur's original string could have been empty, in which case you should output 0.ExampleInput 931004011151010161010107101011011210211101011011010Output 1 2 5 0 3 1 0 2 4 NoteIn the first test case, the shortest possible string Timur started with is \texttt{0}, and he performed the following operation: \texttt{0} \to \color{red}{\texttt{1}}\texttt{0}\color{red}{\texttt{0}}.In the second test case, the shortest possible string Timur started with is \texttt{11}, and he performed the following operation: \texttt{11} \to \color{red}{\texttt{0}}\texttt{11}\color{red}{\texttt{1}}.In the third test case, the shortest possible string Timur started with is \texttt{10101}, and he didn't perform any operations.In the fourth test case, the shortest possible string Timur started with is the empty string (which we denote by \varepsilon), and he performed the following operations: \varepsilon \to \color{red}{\texttt{1}}\texttt{}\color{red}{\texttt{0}} \to \color{red}{\texttt{0}}\texttt{10}\color{red}{\texttt{1}} \to \color{red}{\texttt{1}}\texttt{0101}\color{red}{\texttt{0}}.In the fifth test case, the shortest possible string Timur started with is \texttt{101}, and he performed the following operations: \texttt{101} \to \color{red}{\texttt{0}}\texttt{101}\color{red}{\texttt{1}} \to \color{red}{\texttt{1}}\texttt{01011}\color{red}{\texttt{0}}.
931004011151010161010107101011011210211101011011010
1 2 5 0 3 1 0 2 4
1 second
256 megabytes
['implementation', 'two pointers', '*800']
B. Following Directionstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAlperen is standing at the point (0,0). He is given a string s of length n and performs n moves. The i-th move is as follows: if s_i = \texttt{L}, then move one unit left; if s_i = \texttt{R}, then move one unit right; if s_i = \texttt{U}, then move one unit up; if s_i = \texttt{D}, then move one unit down. If Alperen starts at the center point, he can make the four moves shown. There is a candy at (1,1) (that is, one unit above and one unit to the right of Alperen's starting point). You need to determine if Alperen ever passes the candy. Alperen's path in the first test case. InputThe first line of the input contains an integer t (1 \leq t \leq 1000) — 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 characters \texttt{L}, \texttt{R}, \texttt{D}, and \texttt{U}, denoting the moves Alperen makes.OutputFor each test case, output "YES" (without quotes) if Alperen passes the candy, and "NO" (without quotes) 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).ExampleInput 77UUURDDL2UR8RRRUUDDD3LLL4DUUR5RUDLL11LLLLDDRUDRDOutput YES YES NO NO YES YES NO NoteIn the first test case, Alperen follows the path (0,0) \overset{\texttt{U}}{\to} (0,1) \overset{\texttt{U}}{\to} (0,2) \overset{\texttt{U}}{\to} (0,3) \overset{\texttt{R}}{\to} (1,3) \overset{\texttt{D}}{\to} (1,2) \overset{\texttt{D}}{\to} \color{green}{\mathbf{(1,1)}} \overset{\texttt{L}}{\to} (0,1). Note that Alperen doesn't need to end at the candy's location of (1,1), he just needs to pass it at some point.In the second test case, Alperen follows the path (0,0) \overset{\texttt{U}}{\to} (0,1) \overset{\texttt{R}}{\to} \color{green}{\mathbf{(1,1)}}.In the third test case, Alperen follows the path (0,0) \overset{\texttt{R}}{\to} (1,0) \overset{\texttt{R}}{\to} (2,0) \overset{\texttt{R}}{\to} (3,0) \overset{\texttt{U}}{\to} (3,1) \overset{\texttt{U}}{\to} (3,2) \overset{\texttt{D}}{\to} (3,1) \overset{\texttt{D}}{\to} (3,0) \overset{\texttt{D}}{\to} (3,-1).In the fourth test case, Alperen follows the path (0,0) \overset{\texttt{L}}{\to} (-1,0) \overset{\texttt{L}}{\to} (-2,0) \overset{\texttt{L}}{\to} (-3,0).
77UUURDDL2UR8RRRUUDDD3LLL4DUUR5RUDLL11LLLLDDRUDRD
YES YES NO NO YES YES NO
1 second
256 megabytes
['geometry', 'implementation', '*800']
A. Codeforces Checkingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven a lowercase Latin character (letter), check if it appears in the string \texttt{codeforces}.InputThe first line of the input contains an integer t (1 \leq t \leq 26) — the number of test cases.The only line of each test case contains a character c — a single lowercase Latin character (letter).OutputFor each test case, output "YES" (without quotes) if c satisfies the condition, and "NO" (without quotes) 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).ExampleInput 10abcdefghijOutput NO NO YES YES YES YES NO NO NO NO
10abcdefghij
NO NO YES YES YES YES NO NO NO NO
1 second
256 megabytes
['implementation', 'strings', '*800']
G. Tokens on Graphtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an undirected connected graph, some vertices of which contain tokens and/or bonuses. Consider a game involving one player  — you.You can move tokens according to the following rules: At the beginning of the game, you can make exactly one turn: move any token to any adjacent vertex. If the movement of the token ended on the bonus, then you are allowed to make another turn with any other token. You can use different bonuses in any order. The same bonus can be used an unlimited number of times. Bonuses do not move during the game.There can be several tokens in one vertex at the same time, but initially there is no more than one token in each vertex.The vertex with number 1 is the finish vertex, and your task is to determine whether it is possible to hit it with any token by making turns with the tiles according to the rules described above. If a token is initially located at the vertex of 1, then the game is considered already won. The finish line is in black, the bonuses are in red, the chips are in grey. For example, for a given graph, you can reach the finish line with a chip from the 8th vertex by making the following sequence of turns: Move from the 8-th vertex to the 6-th. Move from the 7-th vertex to the 5-th. Move from the 6-th vertex to the 4-th. Move from the 5-th vertex to the 6-th. Move from the 4-th vertex to the 2-nd. Move from the 6-th vertex to the 4-th. Move from the 2-nd vertex to the 1-st vertex, which is the finish. InputThe first line of input data contains a single integer t (1 \le t \le 10^4) — number of test cases in the test. The descriptions of the test cases follow.The first line of the description of each test case contains two integers n and m (1 \le n \le 2 \cdot 10^5, 0 \le m \le 2 \cdot 10^5) — the number of vertices and edges in the graph, respectively.The second line of the description of each test case contains two integers p and b (1 \le p \le n, 0 \le b \le n) — the number of tokens and bonuses, respectively.The third line of the description of each test case contains p different integers from 1 to n — the indices of the vertices in which the tokens are located.The fourth line of the description of each input data set contains b different integers from 1 to n — the indices of the vertices in which the bonuses are located. Note that the value of b can be equal to 0. In this case, this line is empty.There can be both a token and a bonus in one vertex at the same time.The next m lines of the description of each test case contain two integers u_i and v_i (1 \le u_i, v_i \le n, u_i \ne v_i) — vertices connected by the i-th edge. There is at most one edge between each pair of vertices. The given graph is connected, that is, from any vertex you can get to any one by moving along the edges.The test cases are separated by an empty string.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5. Similarly, it is guaranteed that the sum of m over all input data sets does not exceed 2 \cdot 10^5.OutputFor each test case, print YES in a separate line if you can reach the finish with some token, and NO otherwise.You can output YES and NO in any case (for example, the strings yEs, yes, Yes and YES will be recognized as a positive response).ExampleInput 68 102 47 82 4 5 61 22 32 43 43 54 65 65 76 87 85 41 1531 22 33 44 52 11 021 24 31 223 41 22 32 45 43 25 3 42 41 22 33 44 51 01 111Output YES NO YES YES YES YES Note The first test case is explained in the statement. In the second test case, there is only one token which can make only one turn, and it cannot reach the finish. In the third test case, the token can reach the finish line in 1 turn. In the fourth test case, you need to make just one turn from 2 to 1. In the sixth test case, the token is initially at node number 1, so we win immediately.
68 102 47 82 4 5 61 22 32 43 43 54 65 65 76 87 85 41 1531 22 33 44 52 11 021 24 31 223 41 22 32 45 43 25 3 42 41 22 33 44 51 01 111
YES NO YES YES YES YES
2 seconds
256 megabytes
['constructive algorithms', 'dfs and similar', 'graphs', 'shortest paths', '*2300']
F. Timofey and Black-White Treetime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTimofey came to a famous summer school and found a tree on n vertices. A tree is a connected undirected graph without cycles.Every vertex of this tree, except c_0, is colored white. The vertex c_0 is colored black.Timofey wants to color all the vertices of this tree in black. To do this, he performs n - 1 operations. During the i-th operation, he selects the vertex c_i, which is currently white, and paints it black.Let's call the positivity of tree the minimum distance between all pairs of different black vertices in it. The distance between the vertices v and u is the number of edges on the path from v to u.After each operation, Timofey wants to know the positivity of the current tree.InputThe first line contains the integer t (1 \le t \le 10^4) — the number of testcases.The first line of each testcase contains the integers n, c_0 (2 \le n \le 2 \cdot 10^5, 1 \le c_0 \le n) — the number of vertices in the tree and index of the initial black vertex.The second line of each testcase contains n - 1 unique integers c_1, c_2, \dots, c_{n-1} (1 \le c_i \le n, c_i \ne c_0), where c_i is the vertex which is colored black during the i-th operation.Each of the next n - 1 row of each testcase contains the integers v_i, u_i (1 \le v_i, u_i \le n) — edges in the tree.It is guaranteed that the sum of n for all testcases does not exceed 2 \cdot 10^5.OutputFor each testcase, print n - 1 integer on a separate line.The integer with index i must be equal to positivity of the tree obtained by the first i operations.ExampleInput 66 64 1 3 5 22 46 55 33 41 34 24 1 33 12 31 410 310 7 6 5 2 9 8 1 41 21 34 54 36 48 79 810 81 87 37 5 1 2 4 61 23 24 53 46 57 69 79 3 1 4 2 6 8 54 18 94 82 67 32 43 55 410 21 8 5 10 6 9 4 3 710 77 83 69 77 64 21 67 59 2Output 3 2 1 1 1 3 1 1 3 2 2 2 2 2 1 1 1 4 2 2 1 1 1 5 1 1 1 1 1 1 1 4 3 2 2 1 1 1 1 1 NoteIn the first testcase, after the second operation, the tree looks like this: The distance between vertices 1 and 6 is 3, the distance between vertices 4 and 6 is 3, the distance between vertices 1 and 4 is 2. The positivity of this tree is equal to the minimum of these distances. It equals 2.In the third testcase, after the fourth operation, the tree looks like this: The positivity of this tree is 2.
66 64 1 3 5 22 46 55 33 41 34 24 1 33 12 31 410 310 7 6 5 2 9 8 1 41 21 34 54 36 48 79 810 81 87 37 5 1 2 4 61 23 24 53 46 57 69 79 3 1 4 2 6 8 54 18 94 82 67 32 43 55 410 21 8 5 10 6 9 4 3 710 77 83 69 77 64 21 67 59 2
3 2 1 1 1 3 1 1 3 2 2 2 2 2 1 1 1 4 2 2 1 1 1 5 1 1 1 1 1 1 1 4 3 2 2 1 1 1 1 1
4 seconds
256 megabytes
['brute force', 'dfs and similar', 'divide and conquer', 'graphs', 'greedy', 'math', 'shortest paths', 'trees', '*2100']
E. Vlad and a Pair of Numberstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVlad found two positive numbers a and b (a,b>0). He discovered that a \oplus b = \frac{a + b}{2}, where \oplus means the bitwise exclusive OR , and division is performed without rounding..Since it is easier to remember one number than two, Vlad remembered only a\oplus b, let's denote this number as x. Help him find any suitable a and b or tell him that they do not exist.InputThe first line of the input data contains the single integer t (1 \le t \le 10^4) — the number of test cases in the test. Each test case is described by a single integer x (1 \le x \le 2^{29}) — the number that Vlad remembered.OutputOutput t lines, each of which is the answer to the corresponding test case. As the answer, output a and b (0 < a,b \le 2^{32}), such that x = a \oplus b = \frac{a + b}{2}. If there are several answers, output any of them. If there are no matching pairs, output -1.ExampleInput 6251061836Output 3 1 -1 13 7 -1 25 11 50 22
6251061836
3 1 -1 13 7 -1 25 11 50 22
1 second
256 megabytes
['bitmasks', 'constructive algorithms', '*1400']
D. Matryoshkastime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMatryoshka is a wooden toy in the form of a painted doll, inside which you can put a similar doll of a smaller size.A set of nesting dolls contains one or more nesting dolls, their sizes are consecutive positive integers. Thus, a set of nesting dolls is described by two numbers: s — the size of a smallest nesting doll in a set and m — the number of dolls in a set. In other words, the set contains sizes of s, s + 1, \dots, s + m - 1 for some integer s and m (s,m > 0).You had one or more sets of nesting dolls. Recently, you found that someone mixed all your sets in one and recorded a sequence of doll sizes — integers a_1, a_2, \dots, a_n.You do not remember how many sets you had, so you want to find the minimum number of sets that you could initially have.For example, if a given sequence is a=[2, 2, 3, 4, 3, 1]. Initially, there could be 2 sets: the first set consisting of 4 nesting dolls with sizes [1, 2, 3, 4]; a second set consisting of 2 nesting dolls with sizes [2, 3]. According to a given sequence of sizes of nesting dolls a_1, a_2, \dots, a_n, determine the minimum number of nesting dolls that can make this sequence.Each set is completely used, so all its nesting dolls are used. Each element of a given sequence must correspond to exactly one doll from some set.InputThe first line of input data 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 (1 \le n \le 2 \cdot 10^5) — the total number of matryoshkas that were in all sets.The second line of each test case contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9)  — the sizes of the matryoshkas. It is guaranteed that the sum of values of n over all test cases does not exceed 2\cdot10^5.OutputFor each test case, print one integer k — the minimum possible number of matryoshkas sets.ExampleInput 1062 2 3 4 3 1511 8 7 10 961000000000 1000000000 1000000000 1000000000 1000000000 100000000081 1 4 4 2 3 2 361 2 3 2 3 4710 11 11 12 12 13 1378 8 9 9 10 10 1184 14 5 15 6 16 7 1785 15 6 14 8 12 9 1154 2 2 3 4Output 2 1 6 2 2 2 2 2 4 3 NoteThe first test case is described in the problem statement.In the second test case, all matryoshkas could be part of the same set with minimum size s=7.In the third test case, each matryoshka represents a separate set.
1062 2 3 4 3 1511 8 7 10 961000000000 1000000000 1000000000 1000000000 1000000000 100000000081 1 4 4 2 3 2 361 2 3 2 3 4710 11 11 12 12 13 1378 8 9 9 10 10 1184 14 5 15 6 16 7 1785 15 6 14 8 12 9 1154 2 2 3 4
2 1 6 2 2 2 2 2 4 3
2 seconds
256 megabytes
['data structures', 'greedy', 'sortings', '*1200']
C. Premutationtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA sequence of n numbers is called permutation if it contains all integers from 1 to n exactly once. For example, the sequences [3, 1, 4, 2], [1] and [2,1] are permutations, but [1,2,1], [0,1] and [1,3,4] — are not.Kristina had a permutation p of n elements. She wrote it on the whiteboard n times in such a way that: while writing the permutation at the i-th (1 \le i \le n) time she skipped the element p_i So, she wrote in total n sequences of length n-1 each.For example, suppose Kristina had a permutation p = [4,2,1,3] of length 4. Then she did the following: Wrote the sequence [2, 1, 3], skipping the element p_1=4 from the original permutation. Wrote the sequence [4, 1, 3], skipping the element p_2=2 from the original permutation. Wrote the sequence [4, 2, 3], skipping the element p_3=1 from the original permutation. Wrote the sequence [4, 2, 1], skipping the element p_4=3 from the original permutation. You know all n of sequences that have been written on the whiteboard, but you do not know the order in which they were written. They are given in arbitrary order. Reconstruct the original permutation from them.For example, if you know the sequences [4, 2, 1], [4, 2, 3], [2, 1, 3], [4, 1, 3], then the original permutation will be p = [4, 2, 1, 3].InputThe first line of input data 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 (3 \le n \le 100).This is followed by n lines, each containing exactly n-1 integers and describing one of the sequences written out on the whiteboard.It is guaranteed that all sequences could be obtained from some permutation p, and that the sum n^2 over all input sets does not exceed 2 \cdot 10^5.OutputFor each test case, output on a separate line a permutation p such that the given n sequences could be obtained from it.It is guaranteed that the answer exists and it is the only one. In other words, for each test case the required permutation is sure to exist.ExampleInput 544 2 14 2 32 1 34 1 332 31 31 254 2 1 32 1 3 54 2 3 54 1 3 54 2 1 542 3 41 3 41 2 31 2 432 11 32 3Output 4 2 1 3 1 2 3 4 2 1 3 5 1 2 3 4 2 1 3 NoteThe first test case is described in the problem statement.In the second test case, the sequences are written in the correct order.
544 2 14 2 32 1 34 1 332 31 31 254 2 1 32 1 3 54 2 3 54 1 3 54 2 1 542 3 41 3 41 2 31 2 432 11 32 3
4 2 1 3 1 2 3 4 2 1 3 5 1 2 3 4 2 1 3
3 seconds
256 megabytes
['brute force', 'implementation', 'math', '*1000']
B. Taisia and Dicetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputTaisia has n six-sided dice. Each face of the die is marked with a number from 1 to 6, each number from 1 to 6 is used once.Taisia rolls all n dice at the same time and gets a sequence of values a_1, a_2, \ldots, a_n (1 \le a_i \le 6), where a_i is the value on the upper face of the i-th dice. The sum of this sequence is equal to s.Suddenly, Taisia's pet cat steals exactly one dice with maximum value a_i and calculates the sum of the values on the remaining n-1 dice, which is equal to r.You only know the number of dice n and the values of s, r. Restore a possible sequence a that fulfills the constraints.InputThe first line contains the integer t (1 \le t \le 1000) — the number of testcases.Each testcase is given on a separate line and contains three integers n, s, r (2 \le n \le 50, 1 \le r < s \le 300).It is guaranteed that a solution exists.OutputFor each testcase, print: n integers a_1, a_2, \ldots, a_n in any order. It is guaranteed that such sequence exists.If there are multiple solutions, print any.ExampleInput 72 2 12 4 24 9 55 17 113 15 104 4 35 20 15Output 1 1 2 2 1 2 2 4 6 4 2 3 2 5 5 5 1 1 1 1 1 4 5 5 5
72 2 12 4 24 9 55 17 113 15 104 4 35 20 15
1 1 2 2 1 2 2 4 6 4 2 3 2 5 5 5 1 1 1 1 1 4 5 5 5
1 second
256 megabytes
['greedy', 'greedy', 'math', '*800']
A. Polycarp and the Day of Pitime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputOn March 14, the day of the number \pi is celebrated all over the world. This is a very important mathematical constant equal to the ratio of the circumference of a circle to its diameter.Polycarp was told at school that the number \pi is irrational, therefore it has an infinite number of digits in decimal notation. He wanted to prepare for the Day of the number \pi by memorizing this number as accurately as possible.Polycarp wrote out all the digits that he managed to remember. For example, if Polycarp remembered \pi as 3.1415, he wrote out 31415.Polycarp was in a hurry and could have made a mistake, so you decided to check how many first digits of the number \pi Polycarp actually remembers correctly.InputThe first line of the input data contains the single integer t (1 \le t \le 10^3) — the number of test cases in the test. Each test case is described by a single string of digits n, which was written out by Polycarp.The string n contains up to 30 digits.OutputOutput t integers, each of which is the answer to the corresponding test case, that is how many first digits of the number \pi Polycarp remembers correctly.ExampleInput 900034141592653141592653589793238462643383279314203141531415926535827182314159265358979323846264338327Output 0 1 0 0 3 5 12 0 30
900034141592653141592653589793238462643383279314203141531415926535827182314159265358979323846264338327
0 1 0 0 3 5 12 0 30
1 second
256 megabytes
['implementation', 'math', 'strings', '*800']
F. Serval and Brain Powertime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputServal loves Brain Power and his brain power problem.Serval defines that a string T is powerful iff T can be obtained by concatenating some string T' multiple times. Formally speaking, T is powerful iff there exist a string T' and an integer k\geq 2 such that T=\underbrace{T'+T'+\dots+T'}_{k\text{ times}}For example, gogogo is powerful because it can be obtained by concatenating go three times, but power is not powerful.Serval has a string S consists of lowercase English letters. He is curious about the longest powerful subsequence of S, and he only needs you to find out the length of it. If all the non-empty subsequences of S is not powerful, the answer is considered to be 0.A string a is a subsequence of a string b if a can be obtained from b by the deletion of several (possibly, zero or all) characters.InputThe first line contains a single string S (|S|\leq 80) consisting of lowercase English letters.OutputPrint a single integer — the length of the longest powerful subsequence of S. If all the non-empty subsequences of S is not powerful, print 0.ExamplesInput buaa Output 2 Input codeforcesround Output 6 Input oooooooooooaaaaeaaiaujooooooooooooo Output 24 Input zero Output 0 NoteFor the first sample, all of the non-empty subsequences of buaa are listed below: b u a (occurred twice, buaa and buaa) bu ba (occurred twice, buaa and buaa) ua (occurred twice, buaa and buaa) aa bua (occurred twice, buaa and buaa ) baa uaa buaa Since \texttt{aa} = \texttt{a} + \texttt{a}, aa is a powerful subsequence. It can be shown that aa is the only powerful subsequence among them. So the answer is 2.For the second sample, the longest powerful subsequence is codcod from codeforcesround. So the answer is 6.
buaa
2
2 seconds
256 megabytes
['bitmasks', 'brute force', 'dp', 'greedy', 'implementation', 'strings', '*2700']
E. Serval and Music Gametime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputServal loves playing music games. He meets a problem when playing music games, and he leaves it for you to solve.You are given n positive integers s_1 < s_2 < \ldots < s_n. f(x) is defined as the number of i (1\leq i\leq n) that exist non-negative integers p_i, q_i such that: s_i=p_i\left\lfloor{s_n\over x}\right\rfloor + q_i\left\lceil{s_n\over x}\right\rceilFind out \sum_{x=1}^{s_n} x\cdot f(x) modulo 998\,244\,353.As a reminder, \lfloor x\rfloor denotes the maximal integer that is no greater than x, and \lceil x\rceil denotes the minimal integer that is no less than x.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 cases contains a single integer n (1\leq n\leq 10^6).The second line of each test case contains n positive integers s_1,s_2,\ldots,s_n (1\leq s_1 < s_2 < \ldots < s_n \leq 10^7).It is guaranteed that the sum of n over all test cases does not exceed 10^6, and the sum of s_n does not exceed 10^7.OutputFor each test case, print a single integer in a single line — the sum of x\cdot f(x) over all possible x modulo 998\,244\,353.ExampleInput 431 2 441 2 7 94344208 591000 4779956 540342951633 1661 1741 2134 2221Output 26 158 758737625 12334970 NoteFor the first test case, s_n=4, f(x) are calculated as followings: f(1)=1 \left\lfloor s_n\over 1\right\rfloor=4, \left\lceil s_n\over 1\right\rceil=4. It can be shown that such p_1,p_2 and q_1,q_2 that satisfy the conditions don't exist. Let p_3=1 and q_3=0, then s_3 = p_3\cdot\left\lfloor s_n\over 1\right\rfloor + q_3\cdot\left\lceil s_n\over 1\right\rceil = 1\cdot 4 + 0\cdot 4 = 4. f(2)=2 \left\lfloor s_n\over 2\right\rfloor=2, \left\lceil s_n\over 2\right\rceil=2. It can be shown that such p_1 and q_1 that satisfy the conditions don't exist. Let p_2=1 and q_2=0, then s_2 = p_2\cdot\left\lfloor s_n\over 2\right\rfloor + q_2\cdot\left\lceil s_n\over 2\right\rceil = 1\cdot 2 + 0\cdot 2 = 2. Let p_3=0 and q_3=2, then s_3 = p_3\cdot\left\lfloor s_n\over 2\right\rfloor + q_3\cdot\left\lceil s_n\over 2\right\rceil = 0\cdot 2 + 2\cdot 2 = 4. f(3)=3 \left\lfloor s_n\over 3\right\rfloor=1, \left\lceil s_n\over 3\right\rceil=2. Let p_1=1 and q_1=0, then s_1 = p_1\cdot\left\lfloor s_n\over 3\right\rfloor + q_1\cdot\left\lceil s_n\over 3\right\rceil = 1\cdot 1 + 0\cdot 2 = 1. Let p_2=0 and q_2=1, then s_2 = p_2\cdot\left\lfloor s_n\over 3\right\rfloor + q_2\cdot\left\lceil s_n\over 3\right\rceil = 0\cdot 1 + 1\cdot 2 = 2. Let p_3=0 and q_3=2, then s_3 = p_3\cdot\left\lfloor s_n\over 3\right\rfloor + q_3\cdot\left\lceil s_n\over 3\right\rceil = 0\cdot 1 + 2\cdot 2 = 4. f(4)=3 \left\lfloor s_n\over 4\right\rfloor=1, \left\lceil s_n\over 4\right\rceil=1. Let p_1=1 and q_1=0, then s_1 = p_1\cdot\left\lfloor s_n\over 4\right\rfloor + q_1\cdot\left\lceil s_n\over 4\right\rceil = 1\cdot 1 + 0\cdot 1 = 1. Let p_2=1 and q_2=1, then s_2 = p_2\cdot\left\lfloor s_n\over 4\right\rfloor + q_2\cdot\left\lceil s_n\over 4\right\rceil = 1\cdot 1 + 1\cdot 1 = 2. Let p_3=2 and q_3=2, then s_3 = p_3\cdot\left\lfloor s_n\over 4\right\rfloor + q_3\cdot\left\lceil s_n\over 4\right\rceil = 2\cdot 1 + 2\cdot 1 = 4. Therefore, the answer is \sum_{x=1}^4 x\cdot f(x) = 1\cdot 1 + 2\cdot 2 + 3\cdot 3 + 4\cdot 3 = 26.For the second test case: f(1)=f(2)=f(3)=1 f(4)=3 f(5)=f(6)=f(7)=f(8)=f(9)=4 For example, when x=3 we have f(3)=1 because there exist p_4 and q_4:9 = 1 \cdot\left\lfloor{9\over 3}\right\rfloor + 2 \cdot\left\lceil{9\over 3}\right\rceilIt can be shown that it is impossible to find p_1,p_2,p_3 and q_1,q_2,q_3 that satisfy the conditions.When x=5 we have f(5)=4 because there exist p_i and q_i as followings:1 = 1 \cdot\left\lfloor{9\over 5}\right\rfloor + 0 \cdot\left\lceil{9\over 5}\right\rceil 2 = 0 \cdot\left\lfloor{9\over 5}\right\rfloor + 1 \cdot\left\lceil{9\over 5}\right\rceil 7 = 3 \cdot\left\lfloor{9\over 5}\right\rfloor + 2 \cdot\left\lceil{9\over 5}\right\rceil 9 = 3 \cdot\left\lfloor{9\over 5}\right\rfloor + 3 \cdot\left\lceil{9\over 5}\right\rceilTherefore, the answer is \sum_{x=1}^9 x\cdot f(x) = 158.
431 2 441 2 7 94344208 591000 4779956 540342951633 1661 1741 2134 2221
26 158 758737625 12334970
2 seconds
256 megabytes
['brute force', 'dp', 'implementation', 'math', 'number theory', '*2500']
D. Serval and Shift-Shift-Shifttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputServal has two n-bit binary integer numbers a and b. He wants to share those numbers with Toxel.Since Toxel likes the number b more, Serval decides to change a into b by some (possibly zero) operations. In an operation, Serval can choose any positive integer k between 1 and n, and change a into one of the following number: a\oplus(a\ll k) a\oplus(a\gg k) In other words, the operation moves every bit of a left or right by k positions, where the overflowed bits are removed, and the missing bits are padded with 0. The bitwise XOR of the shift result and the original a is assigned back to a.Serval does not have much time. He wants to perform no more than n operations to change a into b. Please help him to find out an operation sequence, or determine that it is impossible to change a into b in at most n operations. You do not need to minimize the number of operations. In this problem, x\oplus y denotes the bitwise XOR operation of x and y. a\ll k and a\gg k denote the logical left shift and logical right shift.InputEach test contains multiple test cases. The first line contains the number of test cases t (1\le t\le2\cdot10^{3}). The description of the test cases follows.The first line of each test case contains a single integer n (1\le n\le2\cdot10^{3}) — the number of bits in numbers a and b.The second and the third line of each test case contain a binary string of length n, representing a and b, respectively. The strings contain only characters 0 and 1.It is guaranteed that the sum of n over all test cases does not exceed 2\cdot10^{3}.OutputFor each test case, if it is impossible to change a into b in at most n operations, print a single integer -1.Otherwise, in the first line, print the number of operations m (0\le m\le n).If m>0, in the second line, print m integers k_{1},k_{2},\dots,k_{m} representing the operations. If 1\le k_{i}\le n, it means logical left shift a by k_{i} positions. If -n\le k_{i}\le-1, it means logical right shift a by -k_{i} positions.If there are multiple solutions, print any of them.ExampleInput 3500111110001113001000Output 2 3 -2 0 -1NoteIn the first test case:The first operation changes a into \require{cancel}00111\oplus\cancel{001}11\underline{000}=11111.The second operation changes a into \require{cancel}11111\oplus\underline{00}111\cancel{11}=11000.The bits with strikethroughs are overflowed bits that are removed. The bits with underline are padded bits.In the second test case, a is already equal to b, so no operations are needed.In the third test case, it can be shown that a cannot be changed into b.
3500111110001113001000
2 3 -2 0 -1
2 seconds
256 megabytes
['bitmasks', 'brute force', 'constructive algorithms', 'implementation', '*2200']
C. Serval and Toxel's Arraystime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputToxel likes arrays. Before traveling to the Paldea region, Serval gave him an array a as a gift. This array has n pairwise distinct elements.In order to get more arrays, Toxel performed m operations with the initial array. In the i-th operation, he modified the p_{i}-th element of the (i-1)-th array to v_{i}, resulting in the i-th array (the initial array a is numbered as 0). During modifications, Toxel guaranteed that the elements of each array are still pairwise distinct after each operation.Finally, Toxel got m+1 arrays and denoted them as A_{0}=a, A_{1},\ldots,A_{m}. For each pair (i,j) (0\le i<j\le m), Toxel defines its value as the number of distinct elements of the concatenation of A_{i} and A_{j}. Now Toxel wonders, what is the sum of the values of all pairs? Please help him to calculate the answer.InputEach test contains multiple test cases. The first line contains the number of test cases t (1\le t\le10^{4}). The description of the test cases follows.The first line of each test case contains two integers n and m (1\le n,m\le2\cdot10^{5}) — the length of the array and the number of operations.The second line of each test case contains n integers a_{1},a_{2},\dots,a_{n} (1\le a_{i}\le n+m). It is guaranteed that all a_i are pairwise distinct.Each of the next m lines of each test case contains two integers p_{i} and v_{i} (1\le p_{i}\le n, 1\le v_{i}\le n+m) — the position of the modified element and its new value. It is guaranteed that the elements of each array are still pairwise distinct after each modification.It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 2\cdot10^{5}.OutputFor each test case, print a single integer — the sum of the values of all pairs of arrays.ExampleInput 33 21 2 31 42 51 111 110 104 6 9 12 16 20 2 10 19 71 35 42 172 186 117 18 175 55 52 2Output 13 1 705 NoteIn the first test case, the arrays change as follows: [1,2,3]\to[\underline{4},2,3]\to[4,\underline{5},3].The concatenation of the 0-th array and the 1-st array is \require{cancel}[1,2,3,4,\cancel{2},\cancel{3}]. There are 4 distinct elements.The concatenation of the 0-th array and the 2-nd array is \require{cancel}[1,2,3,4,5,\cancel{3}]. There are 5 distinct elements.The concatenation of the 1-st array and the 2-nd array is \require{cancel}[4,2,3,\cancel{4},5,\cancel{3}]. There are 4 distinct elements.Strikethrough elements are duplicates in the array.Therefore, the answer is 4+5+4=13.In the second test case, note that the array may remain unchanged after operations.
33 21 2 31 42 51 111 110 104 6 9 12 16 20 2 10 19 71 35 42 172 186 117 18 175 55 52 2
13 1 705
2 seconds
256 megabytes
['combinatorics', 'dp', 'implementation', 'math', '*1500']
B. Serval and Inversion Magictime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputServal has a string s that only consists of 0 and 1 of length n. The i-th character of s is denoted as s_i, where 1\leq i\leq n.Serval can perform the following operation called Inversion Magic on the string s: Choose an segment [l, r] (1\leq l\leq r\leq n). For l\leq i\leq r, change s_i into 1 if s_i is 0, and change s_i into 0 if s_i is 1. For example, let s be 010100 and the segment [2,5] is chosen. The string s will be 001010 after performing the Inversion Magic.Serval wants to make s a palindrome after performing Inversion Magic exactly once. Help him to determine whether it is possible.A string is a palindrome iff it reads the same backwards as forwards. For example, 010010 is a palindrome but 10111 is not.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 a single integer n (2\leq n\leq 10^5) — the length of string s.The second line of each test case contains a binary string s of length n. Only characters 0 and 1 can appear in s.It's guaranteed that the sum of n over all test cases does not exceed 2\cdot 10^5.OutputFor each test case, print Yes if s can be a palindrome after performing Inversion Magic exactly once, and print No if not.You can output Yes and No in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).ExampleInput 34100151001070111011Output Yes Yes No NoteIn the first test case, Serval can perform Inversion Magic on the segment [1,4]. The string s will be 0110 after the magic.In the second test case, Serval can perform Inversion Magic on the segment [1,3]. The string s will be 01110 after the magic.In the third test case, Serval can't make s a palindrome by performing Inversion Magic exactly once.
34100151001070111011
Yes Yes No
1 second
256 megabytes
['brute force', 'implementation', 'strings', 'two pointers', '*800']
A. Serval and Mocha's Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputMocha likes arrays, and Serval gave her an array consisting of positive integers as a gift.Mocha thinks that for an array of positive integers a, it is good iff the greatest common divisor of all the elements in a is no more than its length. And for an array of at least 2 positive integers, it is beautiful iff all of its prefixes whose length is no less than 2 are good. For example: [3,6] is not good, because \gcd(3,6)=3 is greater than its length 2. [1,2,4] is both good and beautiful, because all of its prefixes whose length is no less than 2, which are [1,2] and [1,2,4], are both good. [3,6,1] is good but not beautiful, because [3,6] is not good. Now Mocha gives you the gift array a of n positive integers, and she wants to know whether array a could become beautiful by reordering the elements in a. It is allowed to keep the array a unchanged.InputEach test contains multiple test cases. The first line contains the number of test cases t (1\leq t\leq 500). The description of the test cases follows.The first line of each test case contains a single integer n (2\leq n\leq 100) — the length of array a.The second line of each test case contains n integers a_1,a_2,\ldots,a_n (1\leq a_1,a_2,\ldots,a_n\leq 10^6) — the elements of array a.OutputFor each test case, print Yes if it is possible to reorder the elements in a to make it beautiful, and print No if not.You can output Yes and No in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).ExampleInput 623 631 2 433 6 1315 35 21435 10 35 1451261 227821 143 4171 1941Output No Yes Yes No Yes Yes NoteIn the first test case, neither [3,6] nor [6,3] are beautiful, so it's impossible to obtain a beautiful array by reordering the elements in a.In the second test case, [1,2,4] is already beautiful. Keeping the array a unchanged can obtain a beautiful array.
623 631 2 433 6 1315 35 21435 10 35 1451261 227821 143 4171 1941
No Yes Yes No Yes Yes
1 second
256 megabytes
['brute force', 'math', 'number theory', '*800']
F. XOR, Tree, and Queriestime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a tree of n vertices. The vertices are numbered from 1 to n.You will need to assign a weight to each edge. Let the weight of the i-th edge be a_i (1 \leq i \leq n-1). The weight of each edge should be an integer between 0 and 2^{30}-1, inclusive.You are given q conditions. Each condition consists of three integers u, v, and x. This means that the bitwise XOR of all edges on the shortest path from u to v should be x.Find out if there exist a_1, a_2, \ldots, a_{n-1} that satisfy the given conditions. If yes, print a solution such that a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} is the smallest. Here, \oplus denotes the bitwise XOR operation.If there are multiple solutions such that a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} is the smallest, print any.InputThe first line contains two integers n and q (2 \le n \le 2.5 \cdot 10^5, 0 \le q \le 2.5 \cdot 10^5).The i-th of the following n-1 lines contains two integers x_i and y_i (1 \le x_i, y_i \le n, x_i \neq y_i), meaning that the i-th edge connects vertices x_i and y_i in the tree.It is guaranteed that the given edges form a tree.The following q lines contain information about conditions.Each line contains three integers u, v, x (1 \le u, v \le n, u \neq v, 0 \le x \le 2^{30}-1), meaning that the bitwise XOR of all edges on the shortest path from u to v should be x.OutputIf there do not exist a_1, a_2, ..., a_{n-1} that satisfy the given conditions, print "No".Otherwise, print "Yes" in the first line.Then print n-1 integers on the next line, where the i-th integer is the weight of the i-th edge. If there are multiple solutions that satisfy the given conditions, print a solution such that a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} is the smallest.If there are multiple solutions such that a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1} is the smallest, print any.When printing "Yes" or "No", you can print each letter in any case (either upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.ExamplesInput 4 4 1 2 2 3 3 4 1 4 3 2 4 2 1 3 1 2 3 1 Output NoInput 6 2 1 2 2 3 3 4 2 5 5 6 1 4 2 2 6 7 Output Yes 4 2 4 1 6Input 6 2 1 2 2 3 3 4 2 5 5 6 1 4 3 1 6 5 Output Yes 6 1 4 3 0NoteFor the first example, there does not exist a set of edge weights that satisfies the given conditions.For the second example, the two given conditions are a_1 \oplus a_2 \oplus a_3=2 and a_4 \oplus a_5=7. There can be multiple solutions, for example, (a_1, a_2, a_3, a_4, a_5)=(1, 2, 1, 4, 3).For the third example, the two given conditions are a_1 \oplus a_2 \oplus a_3=3 and a_1 \oplus a_4 \oplus a_5=5. There are multiple solutions that satisfy the given conditions. (a_1, a_2, a_3, a_4, a_5)=(1, 1, 3, 4, 0) satisfies the given conditions, but the bitwise XOR of all edge weights is 7, which does not have the smallest a_1 \oplus a_2 \oplus \ldots \oplus a_{n-1}, so it cannot be the answer.
4 4 1 2 2 3 3 4 1 4 3 2 4 2 1 3 1 2 3 1
No
3 seconds
256 megabytes
['bitmasks', 'constructive algorithms', 'dfs and similar', 'dsu', 'graphs', 'greedy', 'implementation', 'trees', '*2500']
E. Sum Over Zerotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a_1, a_2, \ldots, a_n of n integers. Consider S as a set of segments satisfying the following conditions. Each element of S should be in form [x, y], where x and y are integers between 1 and n, inclusive, and x \leq y. No two segments in S intersect with each other. Two segments [a, b] and [c, d] intersect if and only if there exists an integer x such that a \leq x \leq b and c \leq x \leq d. For each [x, y] in S, a_x+a_{x+1}+ \ldots +a_y \geq 0. The length of the segment [x, y] is defined as y-x+1. f(S) is defined as the sum of the lengths of every element in S. In a formal way, f(S) = \sum_{[x, y] \in S} (y - x + 1). Note that if S is empty, f(S) is 0.What is the maximum f(S) among all possible S?InputThe first line contains one integer n (1 \leq n \leq 2 \cdot 10^5).The next line is followed by n integers a_1, a_2, \ldots, a_n (-10^9 \leq a_i \leq 10^9).OutputPrint a single integer, the maximum f(S) among every possible S.ExamplesInput 5 3 -3 -2 5 -4 Output 4Input 10 5 -2 -4 -6 2 3 -6 5 3 -2 Output 9Input 4 -1 -2 -3 -4 Output 0NoteIn the first example, S=\{[1, 2], [4, 5]\} can be a possible S because a_1+a_2=0 and a_4+a_5=1. S=\{[1, 4]\} can also be a possible solution.Since there does not exist any S that satisfies f(S) > 4, the answer is 4.In the second example, S=\{[1, 9]\} is the only set that satisfies f(S)=9. Since every possible S satisfies f(S) \leq 9, the answer is 9.In the third example, S can only be an empty set, so the answer is 0.
5 3 -3 -2 5 -4
4
1 second
256 megabytes
['data structures', 'dfs and similar', 'dp', '*2200']
D. Moving Dotstime limit per test1.5 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputWe play a game with n dots on a number line.The initial coordinate of the i-th dot is x_i. These coordinates are distinct. Every dot starts moving simultaneously with the same constant speed.Each dot moves in the direction of the closest dot (different from itself) until it meets another dot. In the case of a tie, it goes to the left. Two dots meet if they are in the same coordinate, after that, they stop moving.After enough time, every dot stops moving. The result of a game is the number of distinct coordinates where the dots stop.Because this game is too easy, calculate the sum of results when we play the game for every subset of the given n dots that has at least two dots. As the result can be very large, print the sum modulo 10^9+7.InputThe first line contains one integer n (2 \leq n \leq 3000).The next line contains n integers x_1, x_2, \ldots, x_n (1 \leq x_1 < x_2 < \ldots < x_n \leq 10^9), where x_i represents the initial coordinate of i-th dot.OutputPrint the sum of results modulo 10^9+7.ExamplesInput 4 1 2 4 6 Output 11Input 5 1 3 5 11 15 Output 30NoteIn the first example, for a subset of size 2, two dots move toward each other, so there is 1 coordinate where the dots stop.For a subset of size 3, the first dot and third dot move toward the second dot, so there is 1 coordinate where the dots stop no matter the direction where the second dot moves.For [1, 2, 4, 6], the first and second dots move toward each other. For the third dot, initially, the second dot and the fourth dot are the closest dots. Since it is a tie, the third dot moves left. The fourth dot also moves left. So there is 1 coordinate where the dots stop, which is 1.5.Because there are 6 subsets of size 2, 4 subsets of size 3 and one subset of size 4, the answer is 6 \cdot 1 + 4 \cdot 1 + 1 = 11.In the second example, for a subset of size 5 (when there are dots at 1, 3, 5, 11, 15), dots at 1 and 11 will move right and dots at 3, 5, 15 will move left. Dots at 1, 3, 5 will eventually meet at 2, and dots at 11 and 15 will meet at 13, so there are 2 coordinates where the dots stop.
4 1 2 4 6
11
1.5 seconds
1024 megabytes
['binary search', 'brute force', 'combinatorics', 'math', 'two pointers', '*2000']
C. Matching Numberstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an integer n. Pair the integers 1 to 2n (i.e. each integer should be in exactly one pair) so that each sum of matched pairs is consecutive and distinct. Formally, let (a_i, b_i) be the pairs that you matched. \{a_1, b_1, a_2, b_2, \ldots, a_n, b_n\} should be a permutation of \{1, 2, \ldots, 2n\}. Let the sorted list of \{a_1+b_1, a_2+b_2, \ldots, a_n+b_n\} be s_1 < s_2 < \ldots < s_n. We must have s_{i+1}-s_i = 1 for 1 \le i \le n - 1.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 500). The description of the test cases follows.For each test case, a single integer n (1 \leq n \leq 10^5) is given.It is guaranteed that the sum of n over all test cases doesn't exceed 10^5.OutputFor each test case, if it is impossible to make such a pairing, print "No".Otherwise, print "Yes" followed by n lines.At each line, print two integers that are paired.You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses.If there are multiple solutions, print any.ExampleInput 4 1 2 3 4 Output Yes 1 2 No Yes 1 6 3 5 4 2 No NoteFor the third test case, each integer from 1 to 6 appears once. The sums of matched pairs are 4+2=6, 1+6=7, and 3+5=8, which are consecutive and distinct.
4 1 2 3 4
Yes 1 2 No Yes 1 6 3 5 4 2 No
1 second
256 megabytes
['constructive algorithms', 'greedy', 'math', '*1300']
B. Sum of Two Numberstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe sum of digits of a non-negative integer a is the result of summing up its digits together when written in the decimal system. For example, the sum of digits of 123 is 6 and the sum of digits of 10 is 1. In a formal way, the sum of digits of \displaystyle a=\sum_{i=0}^{\infty} a_i \cdot 10^i, where 0 \leq a_i \leq 9, is defined as \displaystyle\sum_{i=0}^{\infty}{a_i}.Given an integer n, find two non-negative integers x and y which satisfy the following conditions. x+y=n, and the sum of digits of x and the sum of digits of y differ by at most 1. It can be shown that such x and y always exist.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10\,000). Each test case consists of a single integer n (1 \leq n \leq 10^9)OutputFor each test case, print two integers x and y.If there are multiple answers, print any.ExampleInput 5 1 161 67 1206 19 Output 1 0 67 94 60 7 1138 68 14 5NoteIn the second test case, the sum of digits of 67 and the sum of digits of 94 are both 13.In the third test case, the sum of digits of 60 is 6, and the sum of digits of 7 is 7.
5 1 161 67 1206 19
1 0 67 94 60 7 1138 68 14 5
1 second
256 megabytes
['constructive algorithms', 'greedy', 'implementation', 'math', 'probabilities', '*1100']
A. One and Twotime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a sequence a_1, a_2, \ldots, a_n. Each element of a is 1 or 2.Find out if an integer k exists so that the following conditions are met. 1 \leq k \leq n-1, and a_1 \cdot a_2 \cdot \ldots \cdot a_k = a_{k+1} \cdot a_{k+2} \cdot \ldots \cdot a_n. If there exist multiple k that satisfy the given condition, print the smallest.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 one integer n (2 \leq n \leq 1000).The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \leq a_i \leq 2).OutputFor each test case, if there is no such k, print -1.Otherwise, print the smallest possible k.ExampleInput 362 2 1 2 1 231 2 141 1 1 1Output 2 -1 1 NoteFor the first test case, k=2 satisfies the condition since a_1 \cdot a_2 = a_3 \cdot a_4 \cdot a_5 \cdot a_6 = 4. k=3 also satisfies the given condition, but the smallest should be printed.For the second test case, there is no k that satisfies a_1 \cdot a_2 \cdot \ldots \cdot a_k = a_{k+1} \cdot a_{k+2} \cdot \ldots \cdot a_nFor the third test case, k=1, 2, and 3 satisfy the given condition, so the answer is 1.
362 2 1 2 1 231 2 141 1 1 1
2 -1 1
1 second
256 megabytes
['brute force', 'implementation', 'math', '*800']
I. Treasure Hunttime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputDefine the beauty value of a sequence b_1,b_2,\ldots,b_c as the maximum value of \sum\limits_{i=1}^{q}b_i + \sum\limits_{i=s}^{t}b_i, where q, s, t are all integers and s > q or t\leq q. Note that b_i = 0 when i<1 or i>c, \sum\limits_{i=s}^{t}b_i = 0 when s>t.For example, when b = [-1,-2,-3], we may have q = 0, s = 3, t = 2 so the beauty value is 0 + 0 = 0. And when b = [-1,2,-3], we have q = s = t = 2 so the beauty value is 1 + 2 = 3.You are given a sequence a of length n, determine the sum of the beauty value of all non-empty subsegments a_l,a_{l+1},\ldots,a_r (1\leq l\leq r\leq n) of the sequence a.Print the answer modulo 998\,244\,353.InputEach test contains multiple test cases. The first line contains an integer T (1 \le T \le 10^4) — the number of test cases.The first line contains an integer n (1\le n\le 10^6) — the length of a.The second line contains n integers a_1,a_2,\ldots,a_n (-10^6 \leq a_i \leq 10^6) — the given sequence.It's guaranteed that the sum of n does not exceed 10^6.OutputFor each test case, print a line containing a single integer — the answer modulo 998\,244\,353.ExampleInput 4780 59 100 -52 -86 -62 758-48 -14 -26 43 -41 34 13 551742056 -60 62 13 88 -48 64 36 -10 19 94 25 -69 88 87 79 -70 74 -26 59Output 5924 2548 148 98887 NoteIn the second test case, for the subsequence [-26,43,-41,34,13], when q=5, s=2, t=5, \sum\limits_{i=1}^{q}b_i + \sum\limits_{i=s}^{t}b_i = 23 + 49 = 72.In the third test case, there is only one non-empty consecutive subsequence [74]. When q=1, s=1, t=1, \sum\limits_{i=1}^{q}b_i + \sum\limits_{i=s}^{t}b_i = 148.
4780 59 100 -52 -86 -62 758-48 -14 -26 43 -41 34 13 551742056 -60 62 13 88 -48 64 36 -10 19 94 25 -69 88 87 79 -70 74 -26 59
5924 2548 148 98887
2 seconds
512 megabytes
['data structures', 'divide and conquer', 'two pointers', '*3400']
H. Codeforces Scoreboardtime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are participating in a Codeforces Round with n problems. You spend exactly one minute to solve each problem, the time it takes to submit a problem can be ignored. You can only solve at most one problem at any time. The contest starts at time 0, so you can make your first submission at any time t \ge 1 minutes. Whenever you submit a problem, it is always accepted.The scoring of the i-th problem can be represented by three integers k_i, b_i, and a_i. If you solve it at time t minutes, you get \max(b_i - k_i \cdot t,a_i) points.Your task is to choose an order to solve all these n problems to get the maximum possible score. You can assume the contest is long enough to solve all problems.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 10^4) — the number of test cases. The first line of each test case contains one integer n (1 \le n \le 2 \cdot 10^5) — the number of problems.The n lines follow, the i-th of them contains three integers k_i, b_i, a_i (1\le k_i,b_i,a_i\le 10^9; a_i < b_i), denoting that you get the score of \max(b_i - k_i \cdot t,a_i) if you solve the i-th task at time t minutes.It's guaranteed that the sum of n does not exceed 2 \cdot 10^5.OutputFor each test case, print a line containing a single integer — the maximum score you can get.ExampleInput 4410000 1000000000 200610000 1000000000 99992 999991010 10101000000000 1000000000 99999999961 8 19 29 42 14 34 13 12 19 510 12 584 10 14 19 81 14 34 15 62 9 61 11 102 19 124 19 14105 12 75 39 122 39 113 23 155 30 113 17 135 29 143 17 113 36 183 9 8Output 3999961003 53 78 180 NoteIn the second test case, the points for all problems at each minute are listed below. Time123456Problem 1765\color{red}{4}32Problem 2\color{red}{20}114444Problem 31210\color{red}{8}643Problem 49511\color{red}{1}1Problem 517\color{red}{15}131197Problem 655555\color{red}{5} The points displayed in red denote one of the optimal orders with the score 53.
4410000 1000000000 200610000 1000000000 99992 999991010 10101000000000 1000000000 99999999961 8 19 29 42 14 34 13 12 19 510 12 584 10 14 19 81 14 34 15 62 9 61 11 102 19 124 19 14105 12 75 39 122 39 113 23 155 30 113 17 135 29 143 17 113 36 183 9 8
3999961003 53 78 180
3 seconds
512 megabytes
['binary search', 'data structures', 'dp', 'geometry', '*3300']
G. Colorful Tree Againtime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputAn edge-weighted tree of n nodes is given with each edge colored in some color. Each node of this tree can be blocked or unblocked, all nodes are unblocked initially.A simple path is a path in a graph that does not have repeating nodes. The length of a path is defined as the sum of weights of all edges on the path.A path is good when it is a simple path consisting of edges of the same color c, all edges of color c are on this path, and every node on the path is unblocked.You need to operate 2 kinds of queries: block a node, unblock a node. After each query, print the maximum length among all good paths. If there are no good paths, print 0.InputThe first line contains two integers n, q (1 \leq n,q \leq 2\cdot 10^5) — the number of nodes and the number of queries.Then n-1 lines follow, each containing four integers u, v, w and c (1 \leq u,v,w,c \leq n; u \not = v), denoting a weighted edge connecting node u and node v with weight w and color c. It is guaranteed that these edges form a tree.Then q lines follow, each containing two integers p and x (p = 0 or p = 1, 1\leq x\leq n), denoting a query: if p = 0, block the node x. It's guaranteed that it's not blocked at this time; if p = 1, unblock the node x. It's guaranteed that it's blocked at this time. OutputFor each query, print the maximum length of a good path. If there are no good paths, print 0.ExamplesInput 5 4 4 1 3 4 5 2 4 4 3 1 3 2 1 2 5 1 0 4 0 3 0 2 1 3 Output 5 5 0 3 Input 5 5 4 1 4 4 4 5 2 2 3 1 2 4 3 2 3 1 0 3 0 4 1 3 1 4 0 1 Output 2 0 3 6 3 Input 6 9 3 2 2 3 2 4 4 2 3 1 5 5 6 4 3 2 5 3 1 3 0 2 0 4 0 5 0 6 1 2 1 4 1 5 0 3 1 6 Output 5 5 5 5 5 5 5 0 7 Input 1 2 0 1 1 1 Output 0 0
5 4 4 1 3 4 5 2 4 4 3 1 3 2 1 2 5 1 0 4 0 3 0 2 1 3
5 5 0 3
2 seconds
512 megabytes
['brute force', 'data structures', 'trees', '*3000']
F. Inverse Transformationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA permutation scientist is studying a self-transforming permutation a consisting of n elements a_1,a_2,\ldots,a_n.A permutation is a sequence of integers from 1 to n of length n containing each number exactly once. For example, [1], [4, 3, 5, 1, 2] are permutations, while [1, 1], [4, 3, 1] are not.The permutation transforms day by day. On each day, each element x becomes a_x, that is, a_x becomes a_{a_x}. Specifically: on the first day, the permutation becomes b, where b_x = a_{a_x}; on the second day, the permutation becomes c, where c_x = b_{b_x}; \ldots For example, consider permutation a = [2,3,1]. On the first day, it becomes [3,1,2]. On the second day, it becomes [2,3,1].You're given the permutation a' on the k-th day.Define \sigma(x) = a_x, and define f(x) as the minimal positive integer m such that \sigma^m(x) = x, where \sigma^m(x) denotes \underbrace{\sigma(\sigma(\ldots \sigma}_{m \text{ times}}(x) \ldots)).For example, if a = [2,3,1], then \sigma(1) = 2, \sigma^2(1) = \sigma(\sigma(1)) = \sigma(2) = 3, \sigma^3(1) = \sigma(\sigma(\sigma(1))) = \sigma(3) = 1, so f(1) = 3. And if a=[4,2,1,3], \sigma(2) = 2 so f(2) = 1; \sigma(3) = 1, \sigma^2(3) = 4, \sigma^3(3) = 3 so f(3) = 3.Find the initial permutation a such that \sum\limits^n_{i=1} \dfrac{1}{f(i)} is minimum possible.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains two integers n and k (1 \le n \le 2 \cdot 10^5; 1 \le k \le 10^9) — the length of a, and the last day.The second line contains n integers a'_1,a'_2,\ldots,a'_n (1 \le a'_i \le n) — the permutation on the k-th day.It's guaranteed that the sum of n does not exceed 2 \cdot 10^5.OutputFor each test case, if at least one initial a consistent with the given a' exists, print "YES", then print n integers a_1,a_2,\ldots,a_n — the initial permutation with the smallest sum \sum\limits^n_{i=1} \dfrac{1}{f(i)}. If there are multiple answers with the smallest sum, print any.If there are no valid permutations, print "NO".ExampleInput 105 31 2 3 4 57 21 2 3 4 5 6 78 9981 2 3 4 5 6 7 86 16 3 5 4 1 24 84 2 1 39 11 5 4 8 7 6 3 2 95 99999992 3 4 5 17 978432204 6 1 2 7 5 33 10000000002 1 312 38 9 10 1 5 3 11 4 7 6 12 2Output YES 2 3 4 1 5 YES 6 2 5 7 1 3 4 YES 2 3 4 5 6 7 8 1 YES 3 1 6 4 2 5 YES 4 2 1 3 NO YES 3 4 5 1 2 YES 2 5 4 6 3 7 1 NO YES 3 7 8 6 5 1 12 10 11 4 2 9 NoteIn the second test case, the initial permutation can be a = [6,2,5,7,1,3,4], which becomes [3,2,1,4,6,5,7] on the first day and a' = [1,2,3,4,5,6,7] on the second day (the k-th day). Also, among all the permutations satisfying that, it has the minimum \sum\limits^n_{i=1} \dfrac{1}{f(i)}, which is \dfrac{1}{4}+\dfrac{1}{1}+\dfrac{1}{4}+\dfrac{1}{2}+\dfrac{1}{4}+\dfrac{1}{4}+\dfrac{1}{2}=3.In the fifth test case, the initial permutation can be a = [4,2,1,3], which becomes [3,2,4,1] on the first day, [4,2,1,3] on the second day, and so on. So it finally becomes a' = [4,2,1,3] on the 8-th day (the k-th day). And it has the minimum \sum\limits^n_{i=1} \dfrac{1}{f(i)} = \dfrac{1}{3} + \dfrac{1}{1} + \dfrac{1}{3} + \dfrac{1}{3} = 2.
105 31 2 3 4 57 21 2 3 4 5 6 78 9981 2 3 4 5 6 7 86 16 3 5 4 1 24 84 2 1 39 11 5 4 8 7 6 3 2 95 99999992 3 4 5 17 978432204 6 1 2 7 5 33 10000000002 1 312 38 9 10 1 5 3 11 4 7 6 12 2
YES 2 3 4 1 5 YES 6 2 5 7 1 3 4 YES 2 3 4 5 6 7 8 1 YES 3 1 6 4 2 5 YES 4 2 1 3 NO YES 3 4 5 1 2 YES 2 5 4 6 3 7 1 NO YES 3 7 8 6 5 1 12 10 11 4 2 9
1 second
256 megabytes
['constructive algorithms', 'implementation', 'math', '*2500']
E. The Harmonization of XORtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array of exactly n numbers [1,2,3,\ldots,n] along with integers k and x.Partition the array in exactly k non-empty disjoint subsequences such that the bitwise XOR of all numbers in each subsequence is x, and each number is in exactly one subsequence. Notice that there are no constraints on the length of each subsequence.A sequence a is a subsequence of a sequence b if a can be obtained from b by the deletion of several (possibly, zero or all) elements.For example, for n = 15, k = 6, x = 7, the following scheme is valid: [6,10,11], 6 \oplus 10 \oplus 11 = 7, [5,12,14], 5 \oplus 12 \oplus 14 = 7, [3,9,13], 3 \oplus 9 \oplus 13 = 7, [1,2,4], 1 \oplus 2 \oplus 4 = 7, [8,15], 8 \oplus 15 = 7, [7], 7 = 7, where \oplus represents the bitwise XOR operation.The following scheme is invalid, since 8, 15 do not appear: [6,10,11], 6 \oplus 10 \oplus 11 = 7, [5,12,14], 5 \oplus 12 \oplus 14 = 7, [3,9,13], 3 \oplus 9 \oplus 13 = 7, [1,2,4], 1 \oplus 2 \oplus 4 = 7, [7], 7 = 7. The following scheme is invalid, since 3 appears twice, and 1, 2 do not appear: [6,10,11], 6 \oplus 10 \oplus 11 = 7, [5,12,14], 5 \oplus 12 \oplus 14 = 7, [3,9,13], 3 \oplus 9 \oplus 13 = 7, [3,4], 3 \oplus 4 = 7, [8,15], 8 \oplus 15 = 7, [7], 7 = 7. InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 10^4) — the number of test cases. The first and the only line of each test case contains three integers n, k, x (1 \le k \le n \le 2 \cdot 10^5; 1\le x \le 10^9) — the length of the array, the number of subsequences and the required XOR.It's guaranteed that the sum of n does not exceed 2 \cdot 10^5.OutputFor each test case, if it is possible to partition the sequence, print "YES" in the first line. In the i-th of the following k lines first print the length s_i of the i-th subsequence, then print s_i integers, representing the elements in the i-th subsequence. If there are multiple answers, print any. Note that you can print a subsequence in any order.If it is not possible to partition the sequence, print "NO".ExampleInput 715 6 711 4 55 3 24 1 46 1 711 5 511 6 5Output YES 3 6 10 11 3 5 12 14 3 3 9 13 3 1 2 4 2 8 15 1 7 YES 2 1 4 2 2 7 2 3 6 5 5 8 9 10 11 NO YES 4 1 2 3 4 YES 6 1 2 3 4 5 6 NO NONoteIn the first test case, we construct the following 6 subsequences: [6,10,11], 6 \oplus 10 \oplus 11 = 7, [5,12,14], 5 \oplus 12 \oplus 14 = 7, [3,9,13], 3 \oplus 9 \oplus 13 = 7, [1,2,4], 1 \oplus 2 \oplus 4 = 7, [8,15], 8 \oplus 15 = 7, [7], 7 = 7. In the second test case, we construct the following 4 subsequences: [1,4], 1 \oplus 4 = 5, [2,7], 2 \oplus 7 = 5, [3,6], 3 \oplus 6 = 5, [5,8,9,10,11], 5 \oplus 8 \oplus 9 \oplus 10 \oplus 11 = 5. The following solution is considered correct in this test case as well: [1,4], 1 \oplus 4 = 5, [2,7], 2 \oplus 7 = 5, [5], 5 = 5, [3,6,8,9,10,11], 3 \oplus 6 \oplus 8 \oplus 9 \oplus 10 \oplus 11 = 5.
715 6 711 4 55 3 24 1 46 1 711 5 511 6 5
YES 3 6 10 11 3 5 12 14 3 3 9 13 3 1 2 4 2 8 15 1 7 YES 2 1 4 2 2 7 2 3 6 5 5 8 9 10 11 NO YES 4 1 2 3 4 YES 6 1 2 3 4 5 6 NO NO
1 second
256 megabytes
['bitmasks', 'constructive algorithms', 'greedy', 'math', '*2100']
D. Game on Axistime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n points 1,2,\ldots,n, each point i has a number a_i on it. You're playing a game on them. Initially, you are at point 1. When you are at point i, take following steps: If 1\le i\le n, go to i+a_i, Otherwise, the game ends. Before the game begins, you can choose two integers x and y satisfying 1\le x\le n, -n \le y \le n and replace a_x with y (set a_x := y). Find the number of distinct pairs (x,y) such that the game that you start after making the change ends in a finite number of steps.Notice that you do not have to satisfy a_x\not=y.InputEach test contains multiple test cases. The first line contains an integer t (1\le t\le 10^4) — the number of test cases.The first line of each test case contains one integer n (1\le n\le 2\cdot 10^5) — the number of points.The second line contains n integers a_1,a_2,\ldots,a_n (-n \le a_i \le n) — the numbers on the axis.It's guaranteed that the sum of n does not exceed 2\cdot 10^5.OutputFor each test case, print a line containing a single integer — the number of distinct pairs (x,y) with which the game ends.ExampleInput 9102-1 021 -121 13-1 -2 -131 -2 -14-1 4 -2 151 1 1 1 -451 1 1 1 1Output 2 8 6 7 20 17 34 30 40 NoteIn the first test case, the pairs (x,y) with which the game ends are (1,-1) and (1,1), corresponding to the routes 1\rightarrow 0 and 1\rightarrow 2. Note that (1,2) is invalid since when n=1, y=2 violates -n\le y\le n. (1,0) is also invalid since you will go from 1 to 1 forever.In the second test case, the pairs are (1,-2),(1,-1),(1,2),(2,-2),(2,-1),(2,0),(2,1),(2,2).In the fourth test case, the pairs are (1,-2),(1,-1),(1,1),(1,2),(2,-2),(2,1),(2,2).
9102-1 021 -121 13-1 -2 -131 -2 -14-1 4 -2 151 1 1 1 -451 1 1 1 1
2 8 6 7 20 17 34 30 40
1 second
256 megabytes
['combinatorics', 'dfs and similar', 'dsu', 'graphs', 'implementation', '*1900']
C. Remove the Brackettime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputRSJ has a sequence a of n integers a_1,a_2, \ldots, a_n and an integer s. For each of a_2,a_3, \ldots, a_{n-1}, he chose a pair of non-negative integers x_i and y_i such that x_i+y_i=a_i and (x_i-s) \cdot (y_i-s) \geq 0.Now he is interested in the value F = a_1 \cdot x_2+y_2 \cdot x_3+y_3 \cdot x_4 + \ldots + y_{n - 2} \cdot x_{n-1}+y_{n-1} \cdot a_n.Please help him find the minimum possible value F he can get by choosing x_i and y_i optimally. It can be shown that there is always at least one valid way to choose them.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains two integers n, s (3 \le n \le 2 \cdot 10^5; 0 \le s \le 2 \cdot 10^5).The second line contains n integers a_1,a_2,\ldots,a_n (0 \le a_i \le 2 \cdot 10^5).It is guaranteed that the sum of n does not exceed 2 \cdot 10^5.OutputFor each test case, print the minimum possible value of F.ExampleInput 105 02 0 1 3 45 15 3 4 3 57 27 6 5 4 3 2 15 11 2 3 4 55 21 2 3 4 54 00 1 1 15 54 3 5 6 44 10 2 1 03 99999200000 200000 2000006 81397976 129785 12984 78561 173685 15480Output 0 18 32 11 14 0 16 0 40000000000 2700826806 NoteIn the first test case, 2\cdot 0+0\cdot 1+0\cdot 3+0\cdot 4 = 0.In the second test case, 5\cdot 1+2\cdot 2+2\cdot 2+1\cdot 5 = 18.
105 02 0 1 3 45 15 3 4 3 57 27 6 5 4 3 2 15 11 2 3 4 55 21 2 3 4 54 00 1 1 15 54 3 5 6 44 10 2 1 03 99999200000 200000 2000006 81397976 129785 12984 78561 173685 15480
0 18 32 11 14 0 16 0 40000000000 2700826806
1 second
256 megabytes
['dp', 'greedy', 'math', '*1600']
B. Number Factorizationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an integer n.Consider all pairs of integer arrays a and p of the same length such that n = \prod a_i^{p_i} (i.e. a_1^{p_1}\cdot a_2^{p_2}\cdot\ldots) (a_i>1;p_i>0) and a_i is the product of some (possibly one) distinct prime numbers.For example, for n = 28 = 2^2\cdot 7^1 = 4^1 \cdot 7^1 the array pair a = [2, 7], p = [2, 1] is correct, but the pair of arrays a = [4, 7], p = [1, 1] is not, because 4=2^2 is a product of non-distinct prime numbers.Your task is to find the maximum value of \sum a_i \cdot p_i (i.e. a_1\cdot p_1 + a_2\cdot p_2 + \ldots) over all possible pairs of arrays a and p. Note that you do not need to minimize or maximize the length of the arrays.InputEach test contains multiple test cases. The first line contains an integer t (1 \le t \le 1000) — the number of test cases. Each test case contains only one integer n (2 \le n \le 10^9).OutputFor each test case, print the maximum value of \sum a_i \cdot p_i.ExampleInput 71001086413005619210000000002999999018Output 20 10 22 118 90 2 333333009 NoteIn the first test case, 100 = 10^2 so that a = [10], p = [2] when \sum a_i \cdot p_i hits the maximum value 10\cdot 2 = 20. Also, a = [100], p = [1] does not work since 100 is not made of distinct prime factors.In the second test case, we can consider 10 as 10^1, so a = [10], p = [1]. Notice that when 10 = 2^1\cdot 5^1, \sum a_i \cdot p_i = 7.
71001086413005619210000000002999999018
20 10 22 118 90 2 333333009
1 second
256 megabytes
['greedy', 'math', 'number theory', '*1100']
A. Exponential Equationtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an integer n.Find any pair of integers (x,y) (1\leq x,y\leq n) such that x^y\cdot y+y^x\cdot x = n.InputThe first line contains a single integer t (1\leq t\leq 10^4) — the number of test cases.Each test case contains one line with a single integer n (1\leq n\leq 10^9).OutputFor each test case, if possible, print two integers x and y (1\leq x,y\leq n). If there are multiple answers, print any.Otherwise, print -1.ExampleInput 537423125020732790Output -1 -1 2 3 5 5 3 13 NoteIn the third test case, 2^3 \cdot 3+3^2 \cdot 2 = 42, so (2,3),(3,2) will be considered as legal solutions.In the fourth test case, 5^5 \cdot 5+5^5 \cdot 5 = 31250, so (5,5) is a legal solution.
537423125020732790
-1 -1 2 3 5 5 3 13
1 second
256 megabytes
['constructive algorithms', 'math', '*800']