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
|
---|---|---|---|---|---|
D. Rudolf and the Ball Gametime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRudolf and Bernard decided to play a game with their friends. n people stand in a circle and start throwing a ball to each other. They are numbered from 1 to n in the clockwise order.Let's call a transition a movement of the ball from one player to his neighbor. The transition can be made clockwise or counterclockwise.Let's call the clockwise (counterclockwise) distance from player y_1 to player y_2 the number of transitions clockwise (counterclockwise) that need to be made to move from player y_1 to player y_2. For example, if n=7 then the clockwise distance from 2 to 5 is 3, and the counterclockwise distance from 2 to 5 is 4.Initially, the ball is with the player number x (players are numbered clockwise). On the i-th move the person with the ball throws it at a distance of r_i (1 \le r_i \le n - 1) clockwise or counterclockwise. For example, if there are 7 players, and the 2nd player, after receiving the ball, throws it a distance of 5, then the ball will be caught by either the 7th player (throwing clockwise) or the 4th player (throwing counterclockwise). An illustration of this example is shown below. The game was interrupted after m throws due to unexpected rain. When the rain stopped, the guys gathered again to continue. However, no one could remember who had the ball. As it turned out, Bernard remembered the distances for each of the throws and the direction for some of the throws (clockwise or counterclockwise).Rudolf asks you to help him and based on the information from Bernard, calculate the numbers of the players who could have the ball after m throws.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then follow the descriptions of the test cases.The first line of each test case contains three integers n, m, x (2 \le n \le 1000, 1 \le m \le 1000, 1 \le x \le n) — the number of players, the number of throws made, and the number of the player who threw the ball first, respectively.The next m lines contain information about each throw in order. Each of them contains an integer r_i (1 \le r_i \le n - 1) — the distance at which the i-th throw was made, and a symbol c_i, equal to '0', '1', or '?': if c_i='0', then the i-th throw was made clockwise, if c_i='1', then the i-th throw was made counterclockwise, if c_i='?', then Bernard does not remember the direction and the i-th throw could have been made either clockwise or counterclockwise. It is guaranteed that the sum n \cdot m (n multiplied by m) over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output two lines.In the first line, output the number of players k (1 \le k \le n) who could have the ball at the end of the game.In the next line, output k numbers b_i (1 \le b_i \le n) — the numbers of the players in increasing order. All numbers must be different.ExampleInput
56 3 22 ?2 ?2 ?12 1 23 110 7 42 ?9 14 ?7 02 08 15 ?5 3 14 04 ?1 ?4 1 12 ?Output
3
2 4 6
1
11
4
3 5 7 9
3
2 3 5
1
3
NoteBelow is an illustration of three throws for the first test case. The arrows denote possible throw directions. Players who could have the ball after the throw are highlighted in gray. | 56 3 22 ?2 ?2 ?12 1 23 110 7 42 ?9 14 ?7 02 08 15 ?5 3 14 04 ?1 ?4 1 12 ? | 3 2 4 6 1 11 4 3 5 7 9 3 2 3 5 1 3 | 2 seconds | 256 megabytes | ['dfs and similar', 'dp', 'implementation', '*1200'] |
C. Rudolf and the Ugly Stringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRudolf has a string s of length n. Rudolf considers the string s to be ugly if it contains the substring^\dagger "pie" or the substring "map", otherwise the string s will be considered beautiful.For example, "ppiee", "mmap", "dfpiefghmap" are ugly strings, while "mathp", "ppiiee" are beautiful strings.Rudolf wants to shorten the string s by removing some characters to make it beautiful.The main character doesn't like to strain, so he asks you to make the string beautiful by removing the minimum number of characters. He can remove characters from any positions in the string (not just from the beginning or end of the string).^\dagger String a is a substring of b if there exists a consecutive segment of characters in string b equal to a.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. The descriptions of the test cases follow.The first line of each test case contains a single integer n (1 \le n \le 10^6) — the length of the string s.The next line of each test case contains the string s of length n. The string s consists of lowercase Latin letters.The sum of n over all test cases does not exceed 10^6.OutputFor each test case, output a single integer — the minimum number of characters that need to be deleted to make the string s beautiful. If the string is initially beautiful, then output 0.ExampleInput
69mmapnapie9azabazapi8mappppie18mapmapmapmapmapmap1p11pppiepieeeeOutput
2
0
2
6
0
2
NoteIn the first test case, for example, you can delete the 4th and 9th characters to make the string beautiful.In the second test case, the string is already beautiful. | 69mmapnapie9azabazapi8mappppie18mapmapmapmapmapmap1p11pppiepieeee | 2 0 2 6 0 2 | 2 seconds | 256 megabytes | ['dp', 'greedy', 'strings', '*900'] |
B. Rudolf and 121time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRudolf has an array a of n integers, the elements are numbered from 1 to n.In one operation, he can choose an index i (2 \le i \le n - 1) and assign: a_{i - 1} = a_{i - 1} - 1 a_i = a_i - 2 a_{i + 1} = a_{i + 1} - 1 Rudolf can apply this operation any number of times. Any index i can be used zero or more times.Can he make all the elements of the array equal to zero using this operation?InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases in the test.The first line of each case contains a single integer n (3 \le n \le 2 \cdot 10^5) — the number of elements in the array.The second line of each case contains n integers a_1, a_2, \dots, a_n (0 \le a_j \le 10^9) — the elements of the array.It is guaranteed that the sum of the values of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output "YES" if it is possible to make all the elements of the array zero using the described operations. Otherwise, output "NO".You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.ExampleInput
751 3 5 5 252 4 4 5 150 1 3 3 165 6 0 2 3 041 2 7 237 1 041 1 1 1Output
YES
NO
YES
NO
NO
NO
NO
NoteIn the first example, the original array is [1, 3, 5, 5, 2], to make all its elements zero, Rudolf can act as follows: apply the operation at i=4 and get the array [1, 3, 4, 3, 1]; apply the operation at i=3 and get the array [1, 2, 2, 2, 1]; apply the operation at i=2 and get the array [0, 0, 1, 2, 1]; apply the operation at i=4 and get the array [0, 0, 0, 0, 0]. | 751 3 5 5 252 4 4 5 150 1 3 3 165 6 0 2 3 041 2 7 237 1 041 1 1 1 | YES NO YES NO NO NO NO | 2 seconds | 256 megabytes | ['brute force', 'dp', 'greedy', 'math', '*1000'] |
A. Rudolf and the Tickettime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputRudolf is going to visit Bernard, and he decided to take the metro to get to him. The ticket can be purchased at a machine that accepts exactly two coins, the sum of which does not exceed k.Rudolf has two pockets with coins. In the left pocket, there are n coins with denominations b_1, b_2, \dots, b_n. In the right pocket, there are m coins with denominations c_1, c_2, \dots, c_m. He wants to choose exactly one coin from the left pocket and exactly one coin from the right pocket (two coins in total).Help Rudolf determine how many ways there are to select indices f and s such that b_f + c_s \le k.InputThe first line contains an integer t (1 \le t \le 100) — the number of test cases. Then follows the description of each test case.The first line of each test case contains three natural numbers n, m, and k (1 \le n, m \le 100, 1 \le k \le 2000) — the number of coins in the left and right pockets, and the maximum sum of two coins for the ticket payment at the counter, respectively.The second line of each test case contains n integers b_i (1 \le b_i \le 1000) — the denominations of coins in the left pocket.The third line of each test case contains m integers c_i (1 \le c_i \le 1000) — the denominations of coins in the right pocket.OutputFor each testcase, output a single integer — the number of ways Rudolf can select two coins, taking one from each pocket, so that the sum of the coins does not exceed k.ExampleInput
44 4 81 5 10 142 1 8 12 3 44 81 2 34 2 71 1 1 12 73 4 20001 1 11 1 1 1Output
6
0
4
12
NoteNote that the pairs indicate the indices of the coins in the array, not their denominations.In the first test case, Rudolf can choose the following pairs of coins: [1, 1], [1, 2], [1, 4], [2, 1], [2, 2], [2, 4].In the second test case, Rudolf cannot choose one coin from each pocket in any way, as the sum of any two elements from the first and second arrays will exceed the value of k=4.In the third test case, Rudolf can choose: [1, 1], [2, 1], [3, 1], [4, 1].In the fourth test case, Rudolf can choose any coin from the left pocket and any coin from the right pocket. | 44 4 81 5 10 142 1 8 12 3 44 81 2 34 2 71 1 1 12 73 4 20001 1 11 1 1 1 | 6 0 4 12 | 1 second | 256 megabytes | ['brute force', 'math', '*800'] |
B. Binary Pathtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a 2 \times n grid filled with zeros and ones. Let the number at the intersection of the i-th row and the j-th column be a_{ij}.There is a grasshopper at the top-left cell (1, 1) that can only jump one cell right or downwards. It wants to reach the bottom-right cell (2, n). Consider the binary string of length n+1 consisting of numbers written in cells of the path without changing their order.Your goal is to: Find the lexicographically smallest^\dagger string you can attain by choosing any available path; Find the number of paths that yield this lexicographically smallest string. ^\dagger If two strings s and t have the same length, then s is lexicographically smaller than t if and only if in the first position where s and t differ, the string s has a smaller element than the corresponding element in t.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 a single integer n (2 \le n \le 2 \cdot 10^5).The second line of each test case contains a binary string a_{11} a_{12} \ldots a_{1n} (a_{1i} is either 0 or 1).The third line of each test case contains a binary string a_{21} a_{22} \ldots a_{2n} (a_{2i} is either 0 or 1).It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output two lines: The lexicographically smallest string you can attain by choosing any available path; The number of paths that yield this string. ExampleInput
32000041101110080010011111101101Output
000
2
11000
1
001001101
4
NoteIn the first test case, the lexicographically smallest string is \mathtt{000}. There are two paths that yield this string: In the second test case, the lexicographically smallest string is \mathtt{11000}. There is only one path that yields this string: | 32000041101110080010011111101101 | 000 2 11000 1 001001101 4 | 1 second | 256 megabytes | ['dp', 'greedy', 'implementation', '*1300'] |
A. Shuffle Partytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a_1, a_2, \ldots, a_n. Initially, a_i=i for each 1 \le i \le n.The operation \texttt{swap}(k) for an integer k \ge 2 is defined as follows: Let d be the largest divisor^\dagger of k which is not equal to k itself. Then swap the elements a_d and a_k. Suppose you perform \texttt{swap}(i) for each i=2,3,\ldots, n in this exact order. Find the position of 1 in the resulting array. In other words, find such j that a_j = 1 after performing these operations.^\dagger An integer x is a divisor of y if there exists an integer z such that y = x \cdot z.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 only line of each test case contains one integer n (1 \le n \le 10^9) — the length of the array a.OutputFor each test case, output the position of 1 in the resulting array.ExampleInput
4145120240229Output
1
4
4
67108864
NoteIn the first test case, the array is [1] and there are no operations performed.In the second test case, a changes as follows: Initially, a is [1,2,3,4]. After performing \texttt{swap}(2), a changes to [\underline{2},\underline{1},3,4] (the elements being swapped are underlined). After performing \texttt{swap}(3), a changes to [\underline{3},1,\underline{2},4]. After performing \texttt{swap}(4), a changes to [3,\underline{4},2,\underline{1}]. Finally, the element 1 lies on index 4 (that is, a_4 = 1). Thus, the answer is 4. | 4145120240229 | 1 4 4 67108864 | 1 second | 256 megabytes | ['implementation', 'math', '*800'] |
F. Grand Finale: Circlestime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given n circles on the plane. The i-th of these circles is given by a tuple of integers (x_i, y_i, r_i), where (x_i, y_i) are the coordinates of its center, and r_i is the radius of the circle.Please find a circle C which meets the following conditions: C is contained inside all n circles given in the input. Among all circles C that meet the first condition, the radius of the circle is maximum. Let the largest suitable circle have the radius of a.Your output C, described as (x,y,r), will be accepted if it meets the following conditions: For each i, \sqrt{(x_i-x)^2+(y_i-y)^2}+ r \le r_i+\max(a,1)\cdot 10^{-7}. The absolute or relative error of r does not exceed 10^{-7}. Formally, your answer is accepted if and only if \frac{\left|r - a\right|}{\max(1, a)} \le 10^{-7}. InputThe first line contains a single integer n (1 \le n \le 10^5) — the number of circles.The i-th of the following n lines contains three integers x_i, y_i, r_i (-10^6 \le x_i,y_i \le 10^6, 1 \le r_i \le 2 \cdot 10^6).It is guaranteed that there is a circle with a radius of at least 10^{-6} which is contained inside all n circles.OutputOutput three real values, x, y, and r — the coordinates of the center and the radius of the circle.ExamplesInput
4
1 1 3
-1 1 3
1 -1 2
-1 -1 2
Output
0.0000000000000000 -0.7637626158259733 0.9724747683480533
Input
4
41580 -23621 95642
-41580 -23621 95642
0 47821 95642
0 0 109750
Output
0.0000000000000000 0.0000000000000000 47821.0000000000000000
NoteA two-dimensional plot depicting the first test case is given below. The output circle C is dashed with blue lines. A two-dimensional plot depicting the second test case is given below. The output circle C is dashed with blue lines. | 4
1 1 3
-1 1 3
1 -1 2
-1 -1 2
| 0.0000000000000000 -0.7637626158259733 0.9724747683480533 | 2 seconds | 512 megabytes | ['binary search', 'geometry', '*3300'] |
E. Yet Yet Another Permutation Problemtime limit per test5 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputYou are given a permutation p of length n.Please count the number of permutations q of length n which satisfy the following: for each 1 \le i < n, \max(q_1,\ldots,q_i) \neq \max(p_1,\ldots,p_i).Since the answer may be large, output the answer modulo 998\,244\,353.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 a single integer n (2 \le n \le 2 \cdot 10^5).The second line of each test case contains n integers p_1, p_2, \ldots, p_n (1 \le p_i \le n). It is guaranteed that p is a permutation.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, print a single integer — the answer modulo 998\,244\,353.ExampleInput
622 131 2 333 1 242 4 1 353 5 1 4 2156 13 2 8 7 11 1 3 9 15 4 5 12 10 14Output
1
3
2
4
18
424488915
NoteIn the first test case, p = [2, 1]. The only suitable q is [1, 2]. Indeed, we need to satisfy the inequality q_1 \neq p_1. It only holds for q = [1, 2].In the second test case, p = [1, 2, 3]. So q has to satisfy two inequalities: q_1 \neq p_1 and \max(q_1, q_2) \neq \max(1, 2) = 2. One can prove that this only holds for the following 3 permutations: q = [2, 3, 1]: in this case q_1 = 2 \neq 1 and \max(q_1, q_2) = 3 \neq 2; q = [3, 1, 2]: in this case q_1 = 3 \neq 1 and \max(q_1, q_2) = 3 \neq 2; q = [3, 2, 1]: in this case q_1 = 3 \neq 1 and \max(q_1, q_2) = 3 \neq 2. | 622 131 2 333 1 242 4 1 353 5 1 4 2156 13 2 8 7 11 1 3 9 15 4 5 12 10 14 | 1 3 2 4 18 424488915 | 5 seconds | 1024 megabytes | ['divide and conquer', 'fft', 'math', '*3400'] |
D. Bitwise Paradoxtime limit per test5 secondsmemory limit per test1024 megabytesinputstandard inputoutputstandard outputYou are given two arrays a and b of size n along with a fixed integer v.An interval [l, r] is called a good interval if (b_l \mid b_{l+1} \mid \ldots \mid b_r) \ge v, where | denotes the bitwise OR operation. The beauty of a good interval is defined as \max(a_l, a_{l+1}, \ldots, a_r).You are given q queries of two types: "1 i x": assign b_i := x; "2 l r": find the minimum beauty among all good intervals [l_0,r_0] satisfying l \le l_0 \le r_0 \le r. If there is no suitable good interval, output -1 instead. Please process all queries.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^5). The description of the test cases follows.The first line of each test case contains two integers n and v (1 \le n \le 2 \cdot 10^5, 1 \le v \le 10^9).The second line of each testcase contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^9).The third line of each testcase contains n integers b_1, b_2, \ldots, b_n (1 \le b_i \le 10^9).The fourth line of each testcase contains one integer q (1 \le q \le 2 \cdot 10^5).The i-th of the following q lines contains the description of queries. Each line is of one of two types: "1 i x" (1 \le i \le n, 1 \le x \le 10^9); "2 l r" (1 \le l \le r \le n). It is guaranteed that both the sum of n and the sum of q over all test cases do not exceed 2 \cdot 10^5.OutputFor each test case, output the answers for all queries of the second type.ExampleInput
33 72 1 32 2 342 1 31 2 52 2 32 1 34 55 1 2 44 2 3 362 1 41 3 152 3 42 2 41 2 132 1 41 56412 1 1Output
-1 3 2
5 2 2 1
-1
NoteIn the first test case, a = [2, 1, 3], b = [2, 2, 3], and v = 7.The first query is of the second type and has l = 1 and r = 3. The largest interval available is [1, 3], and its bitwise OR is b_1 \mid b_2 \mid b_3 = 3 which is less than v. Thus, no good interval exists.The second query asks to change b_2 to 5, so b becomes [2, 5, 3].The third query is of the second type and has l = 2 and r = 3. There are three possible intervals: [2, 2], [3, 3], and [2, 3]. However, b_2 = 5 < v, b_3 = 3 < v. So only the last interval is good: it has b_2 \mid b_3 = 7. The answer is thus \max(a_2, a_3) = 3.The fourth query is of the second type and has l = 1 and r = 3. There are three good intervals: [1, 2], [2, 3], and [1, 3]. Their beauty is 2, 3, 3 correspondingly. The answer is thus 2.In the second test case, a = [5, 1, 2, 4], b = [4, 2, 3, 3], and v = 5.The first query has l = 1 and r = 4. The only good intervals are: [1, 2], [1, 3], [1, 4]. Their beauty is 5, 5, 5 correspondingly. The answer is thus 5. | 33 72 1 32 2 342 1 31 2 52 2 32 1 34 55 1 2 44 2 3 362 1 41 3 152 3 42 2 41 2 132 1 41 56412 1 1 | -1 3 2 5 2 2 1 -1 | 5 seconds | 1024 megabytes | ['binary search', 'bitmasks', 'data structures', 'greedy', 'two pointers', '*3100'] |
C. Pokémon Arenatime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are at a dueling arena. You also possess n Pokémons. Initially, only the 1-st Pokémon is standing in the arena.Each Pokémon has m attributes. The j-th attribute of the i-th Pokémon is a_{i,j}. Each Pokémon also has a cost to be hired: the i-th Pokémon's cost is c_i.You want to have the n-th Pokémon stand in the arena. To do that, you can perform the following two types of operations any number of times in any order: Choose three integers i, j, k (1 \le i \le n, 1 \le j \le m, k > 0), increase a_{i,j} by k permanently. The cost of this operation is k. Choose two integers i, j (1 \le i \le n, 1 \le j \le m) and hire the i-th Pokémon to duel with the current Pokémon in the arena based on the j-th attribute. The i-th Pokémon will win if a_{i,j} is greater than or equal to the j-th attribute of the current Pokémon in the arena (otherwise, it will lose). After the duel, only the winner will stand in the arena. The cost of this operation is c_i. Find the minimum cost you need to pay to have the n-th Pokémon stand in the arena.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^5). The description of the test cases follows.The first line of each test case contains two integers n and m (2 \le n \le 4 \cdot 10^5, 1 \le m \le 2 \cdot 10^5, 2 \leq n \cdot m \leq 4 \cdot 10^5).The second line of each test case contains n integers c_1, c_2, \ldots, c_n (1 \le c_i \le 10^9).The i-th of the following n lines contains m integers a_{i,1}, a_{i,2}, \ldots, a_{i,m} (1 \le a_{i,j} \le 10^9).It is guaranteed that the sum of n \cdot m over all test cases does not exceed 4 \cdot 10^5.OutputFor each test case, output the minimum cost to make the n-th Pokémon stand in the arena.ExampleInput
43 32 3 12 9 96 1 71 2 13 32 3 19 9 96 1 71 2 14 22 8 3 518 2417 101 101 16 321412674 3212925 172015806 250849370 306960171 333018900950000001 950000001 950000001821757276 783362401 760000001570000001 700246226 600757652380000001 423513575 474035234315201473 300580025 2870234451 1 1Output
2
6
17
1224474550
NoteIn the first test case, the attribute array of the 1-st Pokémon (which is standing in the arena initially) is [2,9,9].In the first operation, you can choose i=3, j=1, k=1, and increase a_{3,1} by 1 permanently. Now the attribute array of the 3-rd Pokémon is [2,2,1]. The cost of this operation is k = 1.In the second operation, you can choose i=3, j=1, and hire the 3-rd Pokémon to duel with the current Pokémon in the arena based on the 1-st attribute. Since a_{i,j}=a_{3,1}=2 \ge 2=a_{1,1}, the 3-rd Pokémon will win. The cost of this operation is c_3 = 1.Thus, we have made the 3-rd Pokémon stand in the arena within the cost of 2. It can be proven that 2 is minimum possible.In the second test case, the attribute array of the 1-st Pokémon in the arena is [9,9,9].In the first operation, you can choose i=2, j=3, k=2, and increase a_{2,3} by 2 permanently. Now the attribute array of the 2-nd Pokémon is [6,1,9]. The cost of this operation is k = 2.In the second operation, you can choose i=2, j=3, and hire the 2-nd Pokémon to duel with the current Pokémon in the arena based on the 3-rd attribute. Since a_{i,j}=a_{2,3}=9 \ge 9=a_{1,3}, the 2-nd Pokémon will win. The cost of this operation is c_2 = 3.In the third operation, you can choose i=3, j=2, and hire the 3-rd Pokémon to duel with the current Pokémon in the arena based on the 2-nd attribute. Since a_{i,j}=a_{1,2}=2 \ge 1=a_{2,2}, the 3-rd Pokémon can win. The cost of this operation is c_3 = 1.Thus, we have made the 3-rd Pokémon stand in the arena within the cost of 6. It can be proven that 6 is minimum possible. | 43 32 3 12 9 96 1 71 2 13 32 3 19 9 96 1 71 2 14 22 8 3 518 2417 101 101 16 321412674 3212925 172015806 250849370 306960171 333018900950000001 950000001 950000001821757276 783362401 760000001570000001 700246226 600757652380000001 423513575 474035234315201473 300580025 2870234451 1 1 | 2 6 17 1224474550 | 3 seconds | 512 megabytes | ['data structures', 'graphs', 'greedy', 'implementation', 'shortest paths', 'sortings', '*2400'] |
B. Pinballtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a one-dimensional grid of length n. The i-th cell of the grid contains a character s_i, which is either '<' or '>'.When a pinball is placed on one of the cells, it moves according to the following rules: If the pinball is on the i-th cell and s_i is '<', the pinball moves one cell to the left in the next second. If s_i is '>', it moves one cell to the right. After the pinball has moved, the character s_i is inverted (i. e. if s_i used to be '<', it becomes '>', and vice versa). The pinball stops moving when it leaves the grid: either from the left border or from the right one. You need to answer n independent queries. In the i-th query, a pinball will be placed on the i-th cell. Note that we always place a pinball on the initial grid.For each query, calculate how many seconds it takes the pinball to leave the grid. It can be shown that the pinball will always leave the grid within a finite number of steps.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^5). The description of the test cases follows.The first line of each test case contains an integer n (1 \le n \le 5 \cdot 10^5).The second line of each test case contains a string s_1s_2 \ldots s_{n} of length n consisting of characters '<' and '>'.It is guaranteed that the sum of n over all test cases does not exceed 5 \cdot 10^5.OutputFor each test case, for each i (1 \le i \le n) output the answer if a pinball is initially placed on the i-th cell.ExampleInput
33><<4<<<<6<><<<>Output
3 6 5
1 2 3 4
1 4 7 10 8 1
NoteIn the first test case, the movement of the pinball for i=1 is shown in the following pictures. It takes the pinball 3 seconds to leave the grid. The movement of the pinball for i=2 is shown in the following pictures. It takes the pinball 6 seconds to leave the grid. | 33><<4<<<<6<><<<> | 3 6 5 1 2 3 4 1 4 7 10 8 1 | 2 seconds | 256 megabytes | ['binary search', 'data structures', 'implementation', 'math', 'two pointers', '*2000'] |
A. Bitwise Operation Wizardtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.There is a secret sequence p_0, p_1, \ldots, p_{n-1}, which is a permutation of \{0,1,\ldots,n-1\}.You need to find any two indices i and j such that p_i \oplus p_j is maximized, where \oplus denotes the bitwise XOR operation.To do this, you can ask queries. Each query has the following form: you pick arbitrary indices a, b, c, and d (0 \le a,b,c,d < n). Next, the jury calculates x = (p_a \mid p_b) and y = (p_c \mid p_d), where | denotes the bitwise OR operation. Finally, you receive the result of comparison between x and y. In other words, you are told if x < y, x > y, or x = y.Please find any two indices i and j (0 \le i,j < n) such that p_i \oplus p_j is maximum among all such pairs, using at most 3n queries. If there are multiple pairs of indices satisfying the condition, you may output any one of them.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^3). The description of the test cases follows.InteractionThe first line of each test case contains one integer n (2 \le n \le 10^4). At this moment, the permutation p_0, p_1, \ldots, p_{n-1} is chosen. The interactor in this task is not adaptive. In other words, the sequence p is fixed in every test case and does not change during the interaction.To ask a query, you need to pick four indices a, b, c, and d (0 \le a,b,c,d < n) and print the line of the following form: "? a b c d" After that, you receive: "<" if (p_a \mid p_b) < (p_c \mid p_d); "=" if (p_a \mid p_b) = (p_c \mid p_d); ">" if (p_a \mid p_b) > (p_c \mid p_d). You can make at most 3n queries of this form.Next, if your program has found a pair of indices i and j (0 \le i, j < n) such that p_i \oplus p_j is maximized, print the line of the following form: "! i j" Note that this line is not considered a query and is not taken into account when counting the number of queries asked.After this, proceed to the next test case.If you make more than 3n queries during an interaction, your program must terminate immediately, and you will receive the Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.After printing a query or the answer for a test case, do not forget to output the end of line and flush the output. Otherwise, you will get the verdict 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. It is guaranteed that the sum of n over all test cases does not exceed 10^4.HacksTo hack, follow the test format below.The first line contains the number of test cases t (1 \le t \le 10^3). The description of the test cases follows.The first line of each test case contains one integer n (2 \le n \le 10^4).The second line of each test case contains n integers p_0,p_1,\ldots,p_{n-1}, which represent a permutation of integers from 0 to n - 1.The sum of n over all test cases should not exceed 10^4.ExampleInput
2
4
<
=
>
2
Output
? 0 2 3 1
? 1 1 2 3
? 1 2 0 3
! 3 2
! 0 1
NoteIn the first test case, the hidden permutation is p=[0,3,1,2].For the query "? 0 2 3 1", the jury return "<" because (p_0 \mid p_2) = (0 \mid 1) =1 < (p_3 \mid p_1) = (2 \mid 3) = 3.For the query "? 1 1 2 3", the jury return "=" because (p_1 \mid p_1) = (3\mid 3)= 3 = (p_2 \mid p_3) = (1 \mid 2)=3.For the query "? 1 2 0 3", the jury return ">" because (p_1 \mid p_2) = (3 \mid 1) = 3 > (p_0 \mid p_3) = (0\mid 2)=2.The answer i = 3 and j = 2 is valid: (p_3 \oplus p_2) = (2 \oplus 1) = 3 is indeed equal to the maximum possible value of p_i \oplus p_j. Another valid answer would be i=0 and j=1. As the number of queries does not exceed 3n=12, the answer is considered correct.In the second test case, n = 2, so p is either [0, 1] or [1, 0]. In any case, p_0 \oplus p_1 = 1 is maximum possible. | 2
4
<
=
>
2
| ? 0 2 3 1 ? 1 1 2 3 ? 1 2 0 3 ! 3 2 ! 0 1 | 2 seconds | 256 megabytes | ['bitmasks', 'constructive algorithms', 'greedy', 'interactive', 'math', '*1700'] |
F. Andrey's Treetime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMaster Andrey loves trees^{\dagger} very much, so he has a tree consisting of n vertices.But it's not that simple. Master Timofey decided to steal one vertex from the tree. If Timofey stole vertex v from the tree, then vertex v and all edges with one end at vertex v are removed from the tree, while the numbers of other vertices remain unchanged. To prevent Andrey from getting upset, Timofey decided to make the resulting graph a tree again. To do this, he can add edges between any vertices a and b, but when adding such an edge, he must pay |a - b| coins to the Master's Assistance Center.Note that the resulting tree does not contain vertex v.Timofey has not yet decided which vertex v he will remove from the tree, so he wants to know for each vertex 1 \leq v \leq n, the minimum number of coins needed to be spent to make the graph a tree again after removing vertex v, as well as which edges need to be added.^{\dagger}A tree is an undirected connected graph without cycles.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (5 \le n \le 2\cdot10^5) — the number of vertices in Andrey's tree.The next n - 1 lines contain a description of the tree's edges. The i-th of these lines contains two integers u_i and v_i (1 \le u_i, v_i \le n) — the numbers of vertices connected by the i-th edge.It is guaranteed that the given edges form a tree.It is guaranteed that the sum of n over all test cases does not exceed 2\cdot10^5.OutputFor each test case, output the answer in the following format:For each vertex v (in the order from 1 to n), in the first line output two integers w and m — the minimum number of coins that need to be spent to make the graph a tree again after removing vertex v, and the number of added edges.Then output m lines, each containing two integers a and b (1 \le a, b \le n, a \ne v, b \ne v, a \ne b) — the ends of the added edge.If there are multiple ways to add edges, you can output any solution with the minimum cost.ExampleInput
351 31 44 53 254 24 33 55 152 11 51 41 3Output
1 1
3 4
0 0
1 1
1 2
2 1
3 5
0 0
0 0
0 0
1 1
1 2
1 1
1 2
1 1
1 2
3 3
2 3
4 5
3 4
0 0
0 0
0 0
0 0
NoteIn the first test case:Consider the removal of vertex 4: The optimal solution would be to add an edge from vertex 5 to vertex 3. Then we will spend |5 - 3| = 2 coins.In the third test case:Consider the removal of vertex 1: The optimal solution would be: Add an edge from vertex 2 to vertex 3, spending |2 - 3| = 1 coin. Add an edge from vertex 3 to vertex 4, spending |3 - 4| = 1 coin. Add an edge from vertex 4 to vertex 5, spending |4 - 5| = 1 coin. Then we will spend a total of 1 + 1 + 1 = 3 coins.Consider the removal of vertex 2: No edges need to be added, as the graph will remain a tree after removing the vertex. | 351 31 44 53 254 24 33 55 152 11 51 41 3 | 1 1 3 4 0 0 1 1 1 2 2 1 3 5 0 0 0 0 0 0 1 1 1 2 1 1 1 2 1 1 1 2 3 3 2 3 4 5 3 4 0 0 0 0 0 0 0 0 | 4 seconds | 256 megabytes | ['binary search', 'constructive algorithms', 'data structures', 'dfs and similar', 'dsu', 'greedy', 'implementation', 'trees', '*2800'] |
E. Distance Learning Courses in MACtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe New Year has arrived in the Master's Assistance Center, which means it's time to introduce a new feature!Now students are given distance learning courses, with a total of n courses available. For the i-th distance learning course, a student can receive a grade ranging from x_i to y_i.However, not all courses may be available to each student. Specifically, the j-th student is only given courses with numbers from l_j to r_j, meaning the distance learning courses with numbers l_j, l_j + 1, \ldots, r_j.The creators of the distance learning courses have decided to determine the final grade in a special way. Let the j-th student receive grades c_{l_j}, c_{l_j + 1}, \ldots, c_{r_j} for their distance learning courses. Then their final grade will be equal to c_{l_j} | c_{l_j + 1} | \ldots | c_{r_j}, where | denotes the bitwise OR operation.Since the chatbot for solving distance learning courses is broken, the students have asked for your help. For each of the q students, tell them the maximum final grade they can achieve.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 2 \cdot 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 2 \cdot 10^5) — the number of distance learning courses.Each of the following n lines contains two integers x_i and y_i (0 \le x_i \le y_i < 2^{30}) — the minimum and maximum grade that can be received for the i-th course.The next line contains a single integer q (1 \le q \le 2\cdot10^5) — the number of students.Each of the following q lines contains two integers l_j and r_j (1 \le l_j \le r_j \le n) — the minimum and maximum course numbers accessible to the j-th student.It is guaranteed that the sum of n over all test cases and the sum of q over all test cases do not exceed 2\cdot10^5.OutputFor each test case, output q integers, where the j-th integer is the maximum final grade that the j-th student can achieve.ExampleInput
320 13 431 11 22 241 71 73 102 251 33 42 31 41 261 22 20 11 13 30 043 45 52 51 2Output
1 5 4
15 11 15 15 7
1 3 3 3
NoteIn the first test case: The maximum grade for the first student is 1: On the first distance learning course, he will receive a grade of 1. Therefore, the final grade is 1. The maximum grade for the second student is 5: On the first distance learning course, he will receive a grade of 1. On the second distance learning course, he will receive a grade of 4. Therefore, the final grade is 1 | 4 = 5. The maximum grade for the third student is 4: On the second distance learning course, he will receive a grade of 4. Therefore, the final grade is 4. In the second test case: The maximum grade for the first student is 15: On the first distance learning course, he will receive a grade of 7. On the second distance learning course, he will receive a grade of 4. On the third distance learning course, he will receive a grade of 8. Therefore, the final grade is 7 | 4 | 8 = 15. The maximum grade for the second student is 11: On the third distance learning course, he will receive a grade of 9. On the fourth distance learning course, he will receive a grade of 2. Therefore, the final grade is 9 | 2 = 11. | 320 13 431 11 22 241 71 73 102 251 33 42 31 41 261 22 20 11 13 30 043 45 52 51 2 | 1 5 4 15 11 15 15 7 1 3 3 3 | 2 seconds | 256 megabytes | ['bitmasks', 'brute force', 'data structures', 'greedy', 'math', '*2400'] |
D. Exam in MACtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe Master's Assistance Center has announced an entrance exam, which consists of the following.The candidate is given a set s of size n and some strange integer c. For this set, it is needed to calculate the number of pairs of integers (x, y) such that 0 \leq x \leq y \leq c, x + y is not contained in the set s, and also y - x is not contained in the set s.Your friend wants to enter the Center. Help him pass the exam!InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 2 \cdot 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers n and c (1 \leq n \leq 3 \cdot 10^5, 1 \leq c \leq 10^9) — the size of the set and the strange integer.The second line of each test case contains n integers s_1, s_2, \ldots, s_{n} (0 \leq s_1 < s_2 < \ldots < s_{n} \leq c) — the elements of the set s.It is guaranteed that the sum of n over all test cases does not exceed 3 \cdot 10^5.OutputFor each test case, output a single integer — the number of suitable pairs of integers.ExampleInput
83 31 2 31 179574 60 3 5 61 115 100 2 4 8 105 101 3 5 7 94 102 4 6 73 1000000000228 1337 998244353Output
3
16139
10
2
33
36
35
499999998999122959
NoteIn the first test case, the following pairs are suitable: (0, 0), (2, 2), (3, 3).In the third test case, the following pairs are suitable: (0, 1), (0, 2), (0, 4), (1, 3), (2, 6), (3, 4), (3, 5), (4, 5), (4, 6), (5, 6). | 83 31 2 31 179574 60 3 5 61 115 100 2 4 8 105 101 3 5 7 94 102 4 6 73 1000000000228 1337 998244353 | 3 16139 10 2 33 36 35 499999998999122959 | 2 seconds | 256 megabytes | ['binary search', 'combinatorics', 'implementation', 'math', '*1800'] |
C. Messenger in MACtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn the new messenger for the students of the Master's Assistance Center, Keftemerum, an update is planned, in which developers want to optimize the set of messages shown to the user. There are a total of n messages. Each message is characterized by two integers a_i and b_i. The time spent reading the set of messages with numbers p_1, p_2, \ldots, p_k (1 \le p_i \le n, all p_i are distinct) is calculated by the formula:\Large \sum_{i=1}^{k} a_{p_i} + \sum_{i=1}^{k - 1} |b_{p_i} - b_{p_{i+1}}|Note that the time to read a set of messages consisting of one message with number p_1 is equal to a_{p_1}. Also, the time to read an empty set of messages is considered to be 0.The user can determine the time l that he is willing to spend in the messenger. The messenger must inform the user of the maximum possible size of the set of messages, the reading time of which does not exceed l. Note that the maximum size of the set of messages can be equal to 0.The developers of the popular messenger failed to implement this function, so they asked you to solve this problem.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 5 \cdot 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers n and l (1 \leq n \leq 2000, 1 \leq l \leq 10^9) — the number of messages and the time the user is willing to spend in the messenger.The i-th of the next n lines contains two integers a_i and b_i (1 \le a_i, b_i \le 10^9) — characteristics of the i-th message.It is guaranteed that the sum of n^2 over all test cases does not exceed 4 \cdot 10^6.OutputFor each test case, output a single integer — the maximum possible size of a set of messages, the reading time of which does not exceed l.ExampleInput
55 84 31 52 44 32 31 64 103 124 82 12 125 2624 78 2830 223 817 175 1415 31000000000 998244353179 239228 1337993 1007Output
3
1
2
1
0
NoteIn the first test case, you can take a set of three messages with numbers p_1 = 3, p_2 = 2, and p_3 = 5. The time spent reading this set is equal to a_3 + a_2 + a_5 + |b_3 - b_2| + |b_2 - b_5| = 2 + 1 + 2 + |4 - 5| + |5 - 3| = 8.In the second test case, you can take a set of one message with number p_1 = 1. The time spent reading this set is equal to a_1 = 4.In the fifth test case, it can be shown that there is no such non-empty set of messages, the reading time of which does not exceed l. | 55 84 31 52 44 32 31 64 103 124 82 12 125 2624 78 2830 223 817 175 1415 31000000000 998244353179 239228 1337993 1007 | 3 1 2 1 0 | 3 seconds | 256 megabytes | ['binary search', 'brute force', 'constructive algorithms', 'data structures', 'dp', 'greedy', 'sortings', '*1800'] |
B. Informatics in MACtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn the Master's Assistance Center, Nyam-Nyam was given a homework assignment in informatics.There is an array a of length n, and you want to divide it into k > 1 subsegments^{\dagger} in such a way that the \operatorname{MEX} ^{\ddagger} on each subsegment is equal to the same integer.Help Nyam-Nyam find any suitable division, or determine that it does not exist.^{\dagger}A division of an array into k subsegments is defined as k pairs of integers (l_1, r_1), (l_2, r_2), \ldots, (l_k, r_k) such that l_i \le r_i and for each 1 \le j \le k - 1, l_{j + 1} = r_j + 1, and also l_1 = 1 and r_k = n. These pairs represent the subsegments themselves.^{\ddagger}\operatorname{MEX} of an array is the smallest non-negative integer that does not belong to the array.For example: \operatorname{MEX} of the array [2, 2, 1] is 0, because 0 does not belong to the array. \operatorname{MEX} of the array [3, 1, 0, 1] is 2, because 0 and 1 belong to the array, but 2 does not. \operatorname{MEX} of the array [0, 3, 1, 2] is 4, because 0, 1, 2, and 3 belong to the array, but 4 does not. 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. The description of the test cases follows.The first line of each test case contains a single integer n (2 \le n \le 10^5) — the length of the array a.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (0 \le a_i < n) — the elements of the array a.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, output a single integer -1 if a suitable division does not exist.Otherwise, on the first line, output an integer k (2 \le k \le n) — the number of subsegments in the division.Then output k lines — the division into subsegments. The i-th line should contain two integers l_i and r_i (1 \le l_i \le r_i \le n) — the boundaries of the i-th subsegment.The following conditions must be satisfied: For all 1 \le j \le k - 1, l_{j + 1} = r_j + 1; l_1 = 1, r_k = n.If there are multiple possible solutions, output any of them.ExampleInput
520 050 1 2 3 480 1 7 1 0 1 0 332 2 240 1 2 0Output
2
1 1
2 2
-1
3
1 3
4 5
6 8
3
1 1
2 2
3 3
-1NoteIn the first test case, the array a can be divided into 2 subsegments with boundaries [1, 1] and [2, 2]: \operatorname{MEX} of the first subsegment [0] is 1, as 0 belongs to the subsegment, but 1 does not. \operatorname{MEX} of the second subsegment [0] is 1, as 0 belongs to the subsegment, but 1 does not. In the second test case, it can be proven that the required division does not exist.In the third test case, the array a can be divided into 3 subsegments with boundaries [1, 3], [4, 5], [6, 8]: \operatorname{MEX} of the first subsegment [0, 1, 7] is 2, as 0 and 1 belong to the subsegment, but 2 does not. \operatorname{MEX} of the second subsegment [1, 0] is 2, as 0 and 1 belong to the subsegment, but 2 does not. \operatorname{MEX} of the third subsegment [1, 0, 3] is 2, as 0 and 1 belong to the subsegment, but 2 does not. | 520 050 1 2 3 480 1 7 1 0 1 0 332 2 240 1 2 0 | 2 1 1 2 2 -1 3 1 3 4 5 6 8 3 1 1 2 2 3 3 -1 | 1 second | 256 megabytes | ['constructive algorithms', '*1200'] |
A. Entertainment in MACtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputCongratulations, you have been accepted to the Master's Assistance Center! However, you were extremely bored in class and got tired of doing nothing, so you came up with a game for yourself.You are given a string s and an even integer n. There are two types of operations that you can apply to it: Add the reversed string s to the end of the string s (for example, if s = cpm, then after applying the operation s = cpmmpc). Reverse the current string s (for example, if s = cpm, then after applying the operation s = mpc). It is required to determine the lexicographically smallest^{\dagger} string that can be obtained after applying exactly n operations. Note that you can apply operations of different types in any order, but you must apply exactly n operations in total.^{\dagger}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. InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 500) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single even integer n (2 \leq n \leq 10^9) — the number of operations applied to the string s.The second line of each test case contains a single string s (1 \leq |s| \leq 100), consisting of lowercase English letters, — the string to which the operations are applied.OutputFor each test case, output a single line — the lexicographically smallest string that can be obtained after applying exactly n operations.ExampleInput
54cpm2grib10kupitimilablodarbuz1000000000capybara6abacabaOutput
cpm
birggrib
kupitimilablodarbuz
arabypaccapybara
abacaba
NoteIn the first test case, you can apply the operation of the second type (i.e., reverse the string s) 4 times. Then the string s will remain equal to cpm.In the second test case, you can do the following: Apply the operation of the second type, after which s will become equal to birg. Apply operation of the first type (i.e., add the reversed string s to the end of the string s), after which s will become equal to birggrib. | 54cpm2grib10kupitimilablodarbuz1000000000capybara6abacaba | cpm birggrib kupitimilablodarbuz arabypaccapybara abacaba | 1 second | 256 megabytes | ['constructive algorithms', 'strings', '*800'] |
E. Weird LCM Operationstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an integer n, you construct an array a of n integers, where a_i = i for all integers i in the range [1, n]. An operation on this array is defined as follows: Select three distinct indices i, j, and k from the array, and let x = a_i, y = a_j, and z = a_k. Update the array as follows: a_i = \operatorname{lcm}(y, z), a_j = \operatorname{lcm}(x, z), and a_k = \operatorname{lcm}(x, y), where \operatorname{lcm} represents the least common multiple. Your task is to provide a possible sequence of operations, containing at most \lfloor \frac{n}{6} \rfloor + 5 operations such that after executing these operations, if you create a set containing the greatest common divisors (GCDs) of all subsequences with a size greater than 1, then all numbers from 1 to n should be present in this set.After all the operations a_i \le 10^{18} should hold for all 1 \le i \le n.We can show that an answer always exists.InputThe first line contains one integer t (1 \le t \le 10^2) — the number of test cases. The description of the test cases follows.The first and only line of each test case contains an integer n (3 \leq n \leq 3 \cdot 10^{4}) — the length of the array.It is guaranteed that the sum of n over all test cases does not exceed 3 \cdot 10^{4}.OutputThe first line should contain an integer k (0 \leq k \leq \lfloor \frac{n}{6} \rfloor + 5) — where k is the number of operations.The next k lines should contain the description of each operation i.e. 3 integers i, j and k, where 1 \leq i, j, k \leq n and all must be distinct.ExampleInput
3347Output
1
1 2 3
1
1 3 4
3
3 5 7
5 6 7
2 3 4NoteIn the third test case, a = [1, 2, 3, 4, 5, 6, 7].First operation:i = 3, j = 5, k = 7x = 3, y = 5, z = 7.a = [1, 2, \operatorname{lcm}(y,z), 4, \operatorname{lcm}(x,z), 6, \operatorname{lcm}(x,y)] = [1, 2, \color{red}{35}, 4, \color{red}{21}, 6, \color{red}{15}].Second operation:i = 5, j = 6, k = 7x = 21, y = 6, z = 15.a = [1, 2, 35, 4, \operatorname{lcm}(y,z), \operatorname{lcm}(x,z), \operatorname{lcm}(x,y)] = [1, 2, 35, 4, \color{red}{30}, \color{red}{105}, \color{red}{42}].Third operation:i = 2, j = 3, k = 4x = 2, y = 35, z = 4.a = [1, \operatorname{lcm}(y,z), \operatorname{lcm}(x,z), \operatorname{lcm}(x,y), 30, 105, 42] = [1, \color{red}{140}, \color{red}{4}, \color{red}{70}, 30, 105, 42].Subsequences whose GCD equal to i is as follows:\gcd(a_1, a_2) = \gcd(1, 140) = 1\gcd(a_3, a_4) = \gcd(4, 70) = 2\gcd(a_5, a_6, a_7) = \gcd(30, 105, 42) = 3\gcd(a_2, a_3) = \gcd(140, 4) = 4\gcd(a_2, a_4, a_5, a_6) = \gcd(140, 70, 30, 105) = 5\gcd(a_5, a_7) = \gcd(30, 42) = 6\gcd(a_2, a_4, a_6, a_7) = \gcd(140, 70, 105, 42) = 7 | 3347 | 1 1 2 3 1 1 3 4 3 3 5 7 5 6 7 2 3 4 | 2 seconds | 256 megabytes | ['brute force', 'constructive algorithms', 'number theory', '*3000'] |
D2. XOR Break — Game Versiontime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.This is the game version of the problem. Note that the solution of this problem may or may not share ideas with the solution of the solo version. You can solve and get points for both versions independently.Alice and Bob are playing a game. The game starts with a positive integer n, with players taking turns. On each turn of the game, the following sequence of events takes place: The player having the integer p breaks it into two integers p_{1} and p_{2}, where 0 \lt p_{1} \lt p, 0 \lt p_{2} \lt p and p_{1} \oplus p_{2} = p. If no such p_{1}, p_{2} exist, the player loses. Otherwise, the opponent does either select the integer p_{1} or p_{2}. The game continues with the selected integer. The opponent will try to break it. As Alice, your goal is to win. You can execute a maximum of 63 break operations. You have the choice to play first or second. The system will act for Bob.Here \oplus denotes the bitwise XOR operation.InputEach test contains multiple test cases. The first line of input contains a single integer t (1 \leq t \leq 1000) — the number of test cases.The only line of each test case contains a single integer n (1 \leq n \leq 10^{18}) — the number the game starts with.InteractionFor each test case, the interaction begins by reading the integer n.After reading n, print a single line containing either "first" or "second", denoting what you want to play as (as first or second correspondingly).On Alice's turn, you are required to print two positive integers, p_{1} and p_{2} such that 0 \lt p_{1} \lt p, 0 \lt p_{2} \lt p and p_{1} \oplus p_{2} = p. Here, p equals one of the two integers printed by Bob in the previous turn. If no turn has occurred previously, p is equal to n. If Alice cannot perform a break operation, print "0 0" to receive a Wrong answer verdict.On Bob's turn, you should read two integers, p_{1} and p_{2} such that 0 \lt p_{1} \lt p, 0 \lt p_{2} \lt p and p_{1} \oplus p_{2} = p. Here, p equals one of the two integers printed by Alice in the previous turn. If no turn has occurred previously, p is equal to n. If Bob cannot perform a break operation p_{1} = 0 and p_2 = 0 in which case you should proceed to the next test case.If any break operation performed by Alice is invalid, the interactor prints "-1 -1" and your code should promptly exit to receive a wrong answer verdict.If Alice performs 63 turns and Bob can still execute a break operation on the current integers, the interactor prints "-1 -1", and your code should promptly exit to receive a wrong answer verdict.After printing a query, 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. In this problem, hacks are disabled.ExampleInput
4
1
0 0
3
0 0
13
3 4
0 0
777777770001
0 0Output
second
first
2 1
first
10 7
1 2
first
777777770000 1NoteExplanation for the interaction.Interactor / BobAliceExplanation4t1n for the first test casesecondAlice chooses to go second0 0Bob says he cannot break p = 13n for the second test casefirstAlice chooses to go first1 2Alice breaks p = 3 into p_1 = 1 and p_2 = 20 0Bob says he cannot break p = 1 or p = 213n for the third test casefirstAlice chooses to go first10 7Alice breaks p = 13 into p_1 = 10 and p_2 = 73 4Bob breaks p = 7 into p_1 = 3 and p_2 = 41 2Alice breaks p = 3 into p_1 = 1 and p_2 = 20 0Bob says he cannot break p = 1 or p = 2777777770001n for the fourth test casefirstAlice chooses to go first777777770000 1Alice breaks p = 777\,777\,770\,001 into p_1 = 777\,777\,770\,000 and p_2 = 10 0Bob says he cannot perform break operation.This table is for explanation only and does not reflect the actual behavior of the interactor. Note that in the last test case Bob could choose p_1 and perform a break operation but he gave up. | 4
1
0 0
3
0 0
13
3 4
0 0
777777770001
0 0 | second first 2 1 first 10 7 1 2 first 777777770000 1 | 3 seconds | 256 megabytes | ['bitmasks', 'games', 'greedy', 'interactive', '*2400'] |
D1. XOR Break — Solo Versiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the solo version of the problem. Note that the solution of this problem may or may not share ideas with the solution of the game version. You can solve and get points for both versions independently.You can make hacks only if both versions of the problem are solved.Given an integer variable x with the initial value of n. A single break operation consists of the following steps: Choose a value y such that 0 \lt y \lt x and 0 \lt (x \oplus y) \lt x. Update x by either setting x = y or setting x = x \oplus y. Determine whether it is possible to transform x into m using a maximum of 63 break operations. If it is, provide the sequence of operations required to achieve x = m.You don't need to minimize the number of operations.Here \oplus denotes the bitwise XOR operation.InputThe first line contains one positive integer t (1 \le t \le 10^4) — the number of test cases.Each test case consists of a single line containing two integers n and m (1 \leq m \lt n \leq 10^{18}) — the initial value of x and the target value of x.OutputFor each test case, output your answer in the following format.If it is not possible to achieve m in 63 operations, print -1.Otherwise, The first line should contain k (1 \leq k \leq 63) — where k is the number of operations required.The next line should contain k+1 integers — the sequence where variable x changes after each break operation. The 1-st and k+1-th integers should be n and m, respectively.ExampleInput
37 34 2481885160128643072 45035996273704960Output
1
7 3
-1
3
481885160128643072 337769972052787200 49539595901075456 45035996273704960NoteIn the first test case n = 7, for the first operation x = 7 if we choose y = 3 then (7 \oplus 3) \lt 7, hence we can update x with 3 which is equal to m.In the second test case n = 4, for the first operation x = 4.If we choose: y = 1 then (4 \oplus 1) \gt 4 y = 2 then (4 \oplus 2) \gt 4 y = 3 then (4 \oplus 3) \gt 4 Hence we can't do the first operation and it is impossible to make x = 2. | 37 34 2481885160128643072 45035996273704960 | 1 7 3 -1 3 481885160128643072 337769972052787200 49539595901075456 45035996273704960 | 2 seconds | 256 megabytes | ['bitmasks', 'constructive algorithms', 'greedy', '*2100'] |
C. Find a Minetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.You are given a grid with n rows and m columns. The coordinates (x, y) represent the cell on the grid, where x (1 \leq x \leq n) is the row number counting from the top and y (1 \leq y \leq m) is the column number counting from the left. It is guaranteed that there are exactly 2 mines in the grid at distinct cells, denoted as (x_1, y_1) and (x_2, y_2). You are allowed to make no more than 4 queries to the interactor, and after these queries, you need to provide the location of one of the mines.In each query, you can choose any grid cell (x, y), and in return, you will receive the minimum Manhattan distance from both the mines to the chosen cell, i.e., you will receive the value \min(|x-x_1|+|y-y_1|, |x-x_2|+|y-y_2|).Your task is to determine the location of one of the mines after making the queries.InputEach test contains multiple test cases. The first line of input contains a single integer t (1 \leq t \leq 3 \cdot 10^{3}) — the number of test cases.The only line of each test case contains two integers n and m (2 \leq n \leq 10^{8}, 2 \leq m \leq 10^{8}) — the number of rows and columns.InteractionFor each test case, the interaction starts with reading n and m.Then you are allowed to make at most 4 queries in the following way:"? x y" (1 \leq x \leq n and 1 \leq y \leq m)After each one, you should read an integer d which is equal to \min(|x-x_1|+|y-y_1|, |x-x_2|+|y-y_2|).When you have found the location of any one of the mines, print a single line "! x y" (without quotes), representing the row and the column of one of the mines. Outputting the answer does not count as a query.After printing the answer, your program must then continue to solve the remaining test cases, or exit if all test cases have been solved.The interactor for this problem is not adaptive: cells of mines are fixed before any queries are made.After printing a query, 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. Hacks:To make a hack, use the following format:The first line contains a single integer t (1 \leq t \leq 3 \cdot 10^{3}) — the number of test cases.The description of each test case should consist of three lines. The first line contains two integers n and m (2 \leq n \leq 10^{8}, 2 \leq m \leq 10^{8}) — the number of rows and columns. The second line contains the coordinates of the first mine x_1 and y_1(1 \leq x_1 \leq n, 1 \leq y_1 \leq m).The third line contains the coordinates of the second mine x_2 and y_2(1 \leq x_2 \leq n, 1 \leq y_2 \leq m).The mines should be located at different positions.ExampleInput
2
4 4
3
2
2
0
5 5
1
2
3
Output
? 1 1
? 1 4
? 4 1
? 2 3
! 2 3
? 5 5
? 2 2
? 3 3
! 1 1
NoteIn the first test case, we start by querying the upper-left corner (1, 1) and get the result 3, which means that there is a mine on the counter diagonal, and there is no mine above it. In the image below, each cell contains a number indicating the distance to the blue cell. The green cells are candidates to contain the nearest mine. Then we ask three cells on that diagonal, and at the last query, we get the result 0, which means that a mine is found at the position (2, 3).The second mine was located at the position (3, 2).In the second test case, we start by asking the lower-right corner (5, 5), and get the result 1, which means that one of the two neighbours contains a mine, let's call it mine 1. Then we ask cell (2, 2). We can see that these green cells don't intersect with the green cells from the first query, so they contain the other mine, let's call it mine 2. Query 3 is cell (3, 3). These cells contain mine 1, but we still don't know where exactly. Nevertheless, we can determine that the only possible cell for mine 2 is (1, 1), because all other candidates are at a distance closer than 3 for this query. | 2
4 4
3
2
2
0
5 5
1
2
3
| ? 1 1 ? 1 4 ? 4 1 ? 2 3 ! 2 3 ? 5 5 ? 2 2 ? 3 3 ! 1 1 | 2 seconds | 256 megabytes | ['binary search', 'constructive algorithms', 'geometry', 'greedy', 'interactive', 'math', '*1700'] |
B. Yet Another Coin Problemtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have 5 different types of coins, each with a value equal to one of the first 5 triangular numbers: 1, 3, 6, 10, and 15. These coin types are available in abundance. Your goal is to find the minimum number of these coins required such that their total value sums up to exactly n.We can show that the answer always exists.InputThe first line contains one 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 \leq n \leq 10^9) — the target value.OutputFor each test case, output a single number — the minimum number of coins required.ExampleInput
14123571112141617182098402931328Output
1
2
1
3
2
2
2
3
2
3
2
2
8
26862090
NoteIn the first test case, for n = 1, the answer is 1 since only one 1 value coin is sufficient. 1 = 1 \cdot 1.In the fourth test case, for n = 5, the answer is 3, which can be achieved using two 1 value coins and one 3 value coin. 5 = 2 \cdot 1 + 1 \cdot 3.In the seventh test case, for n = 12, the answer is 2, which can be achieved using two 6 value coins.In the ninth test case, for n = 16, the answer is 2, which can be achieved using one 1 value coin and one 15 value coin or using one 10 value coin and one 6 value coin. 16 = 1 \cdot 1 + 1 \cdot 15 = 1 \cdot 6 + 1 \cdot 10. | 14123571112141617182098402931328 | 1 2 1 3 2 2 2 3 2 3 2 2 8 26862090 | 1 second | 256 megabytes | ['brute force', 'dp', 'greedy', 'math', '*1200'] |
A. Too Min Too Maxtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array a of n elements, find the maximum value of the expression:|a_i - a_j| + |a_j - a_k| + |a_k - a_l| + |a_l - a_i|where i, j, k, and l are four distinct indices of the array a, with 1 \le i, j, k, l \le n.Here |x| denotes the absolute value of x.InputThe first line contains one integer t (1 \le t \le 500) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (4 \le n \le 100) — the length of the given array.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (-10^6 \le a_i \le 10^6).OutputFor each test case, print a single integer — the maximum value.ExampleInput
541 1 1 151 1 2 2 385 1 3 2 -3 -1 10 343 3 1 141 2 2 -1Output
0
6
38
8
8
NoteIn the first test case, for any selection of i, j, k, l, the answer will be 0. For example, |a_1 - a_2| + |a_2 - a_3| + |a_3 - a_4| + |a_4 - a_1| = |1 - 1| + |1 - 1| + |1 - 1| + |1 - 1| = 0 + 0 + 0 + 0 = 0.In the second test case, for i = 1, j = 3, k = 2, and l = 5, the answer will be 6. |a_1 - a_3| + |a_3 - a_2| + |a_2 - a_5| + |a_5 - a_1| = |1 - 2| + |2 - 1| + |1 - 3| + |3 - 1| = 1 + 1 + 2 + 2 = 6. | 541 1 1 151 1 2 2 385 1 3 2 -3 -1 10 343 3 1 141 2 2 -1 | 0 6 38 8 8 | 1 second | 256 megabytes | ['greedy', 'math', '*800'] |
G. Turtle Magic: Royal Turtle Shell Patterntime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTurtle Alice is currently designing a fortune cookie box, and she would like to incorporate the theory of LuoShu into it.The box can be seen as an n \times m grid (n, m \ge 5), where the rows are numbered 1, 2, \dots, n and columns are numbered 1, 2, \dots, m. Each cell can either be empty or have a single fortune cookie of one of the following shapes: circle or square. The cell at the intersection of the a-th row and the b-th column is denoted as (a, b).Initially, the entire grid is empty. Then, Alice performs q operations on the fortune cookie box. The i-th operation (1 \le i \le q) is as follows: specify a currently empty cell (r_i,c_i) and a shape (circle or square), then put a fortune cookie of the specified shape on cell (r_i,c_i). Note that after the i-th operation, the cell (r_i,c_i) is no longer empty.Before all operations and after each of the q operations, Alice wonders what the number of ways to place fortune cookies in all remaining empty cells is, such that the following condition is satisfied:No three consecutive cells (in horizontal, vertical, and both diagonal directions) contain cookies of the same shape. Formally: There does not exist any (i,j) satisfying 1 \le i \le n, 1 \le j \le m-2, such that there are cookies of the same shape in cells (i,j), (i,j+1), (i,j+2). There does not exist any (i,j) satisfying 1 \le i \le n-2, 1 \le j \le m, such that there are cookies of the same shape in cells (i,j), (i+1,j), (i+2,j). There does not exist any (i,j) satisfying 1 \le i \le n-2, 1 \le j \le m-2, such that there are cookies of the same shape in cells (i,j), (i+1,j+1), (i+2,j+2). There does not exist any (i,j) satisfying 1 \le i \le n-2, 1 \le j \le m-2, such that there are cookies of the same shape in cells (i,j+2), (i+1,j+1), (i+2,j). You should output all answers modulo 998\,244\,353. Also note that it is possible that after some operations, the condition is already not satisfied with the already placed candies, in this case you should output 0.InputThe first line of the input contains a single integer t (1 \le t \le 10^3) — the number of test cases.The first line of each test case contains three integers n, m, q (5 \le n, m \le 10^9, 0 \le q \le \min(n \times m, 10^5)).The i-th of the next q lines contains two integers r_i, c_i and a single string \text{shape}_i (1 \le r_i \le n, 1 \le c_i \le m, \text{shape}_i= "circle" or "square"), representing the operations. It is guaranteed that the cell on the r_i-th row and the c_i-th column is initially empty. That means, each (r_i,c_i) will appear at most once in the updates.The sum of q over all test cases does not exceed 10^5.OutputFor each test case, output q+1 lines. The first line of each test case should contain the answer before any operations. The i-th line (2 \le i \le q+1) should contain the answer after the first i-1 operations. All answers should be taken modulo 998\,244\,353.ExampleInput
26 7 43 3 circle3 6 square5 3 circle5 4 square5 5 31 1 circle1 2 circle1 3 circleOutput
8
4
3
1
0
8
4
1
0
NoteIn the second sample, after placing a circle-shaped fortune cookie to cells (1,1), (1,2) and (1,3), the condition is already not satisfied. Therefore, you should output 0. | 26 7 43 3 circle3 6 square5 3 circle5 4 square5 5 31 1 circle1 2 circle1 3 circle | 8 4 3 1 0 8 4 1 0 | 3 seconds | 256 megabytes | ['bitmasks', 'brute force', 'combinatorics', 'constructive algorithms', 'dfs and similar', 'math', '*2300'] |
F. Turtle Mission: Robot and the Earthquaketime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe world is a grid with n rows and m columns. The rows are numbered 0, 1, \ldots, n-1, while the columns are numbered 0, 1, \ldots, m-1. In this world, the columns are cyclic (i.e. the top and the bottom cells in each column are adjacent). The cell on the i-th row and the j-th column (0 \le i < n, 0 \le j < m) is denoted as (i,j). At time 0, the cell (i,j) (where 0 \le i < n, 0 \le j < m) contains either a rock or nothing. The state of cell (i,j) can be described using the integer a_{i,j}: If a_{i,j} = 1, there is a rock at (i,j). If a_{i,j} = 0, there is nothing at (i,j). As a result of aftershocks from the earthquake, the columns follow tectonic plate movements: each column moves cyclically upwards at a velocity of 1 cell per unit of time. Formally, for some 0 \le i < n, 0 \le j < m, if (i,j) contains a rock at the moment, it will move from (i, j) to (i - 1, j) (or to (n - 1, j) if i=0). The robot called RT is initially positioned at (0,0). It has to go to (n-1,m-1) to carry out an earthquake rescue operation (to the bottom rightmost cell). The earthquake doesn't change the position of the robot, they only change the position of rocks in the world.Let RT's current position be (x,y) (0 \le x < n, 0 \le y < m), it can perform the following operations: Go one cell cyclically upwards, i.e. from (x,y) to ((x+n-1) \bmod n, y) using 1 unit of time. Go one cell cyclically downwards, i.e. (x,y) to ((x+1) \bmod n, y) using 1 unit of time. Go one cell to the right, i.e. (x,y) to (x, y+1) using 1 unit of time. (RT may perform this operation only if y < m-1.) Note that RT cannot go left using the operations nor can he stay at a position.Unfortunately, RT will explode upon colliding with a rock. As such, when RT is at (x,y) and there is a rock at ((x+1) \bmod n, y) or ((x+2) \bmod n, y), RT cannot move down or it will be hit by the rock. Similarly, if y+1 < m and there is a rock at ((x+1) \bmod n, y+1), RT cannot move right or it will be hit by the rock. However, it is worth noting that if there is a rock at (x \bmod n, y+1) and ((x+1) \bmod n, y), RT can still move right safely. Find the minimum amount of time RT needs to reach (n-1,m-1) without colliding with any rocks. If it is impossible to do so, output -1.InputThe first line of the input contains one integer t (1 \le t \le 10^4) — the number of test cases.In each test case, the first line contains two integers n, m (3 \le n, m \le 10^3) — the size of the planet's boundaries. Each of the next n lines contains m integers. The (j+1)-th integer on the (i+1)-th line (0 \le i < n, 0 \le j < m) is a_{i,j} (0 \le a_{i,j} \le 1), which denotes whether or not there is a rock at (i,j) at time 0.Additionally, it is guaranteed that a_{0,0} = 0, and a_{i, m-1} = 0 for 0 \le i < n. In other words, there is no rock at RT's initial position as well as column m-1.The sum of n \cdot m over all test cases does not exceed 10^6.OutputFor each test case: If the destination can be reached without colliding with any rocks, output a single integer — the minimum amount of time RT needs to reach (n-1,m-1). Otherwise, output -1. ExamplesInput
64 50 1 0 0 00 0 1 0 01 0 1 1 00 0 0 0 03 30 0 01 0 00 0 05 30 0 00 0 01 0 00 0 01 0 03 70 0 1 0 0 1 01 0 1 0 1 0 00 1 0 0 0 0 03 40 1 0 01 0 0 00 1 1 05 50 0 0 0 00 1 0 1 00 1 0 1 00 1 0 1 00 0 0 1 0Output
7
3
3
8
-1
12
Input
63 30 0 00 0 00 0 04 30 1 01 0 00 1 01 0 04 30 1 00 1 00 1 00 1 03 30 0 01 1 00 0 03 30 1 00 0 00 1 05 50 0 0 0 00 1 1 0 00 1 1 0 00 0 0 0 00 0 1 0 0Output
3
3
-1
-1
3
8
NoteVisual explanation of the first test case in the example: | 64 50 1 0 0 00 0 1 0 01 0 1 1 00 0 0 0 03 30 0 01 0 00 0 05 30 0 00 0 01 0 00 0 01 0 03 70 0 1 0 0 1 01 0 1 0 1 0 00 1 0 0 0 0 03 40 1 0 01 0 0 00 1 1 05 50 0 0 0 00 1 0 1 00 1 0 1 00 1 0 1 00 0 0 1 0 | 7 3 3 8 -1 12 | 3 seconds | 256 megabytes | ['dfs and similar', 'dp', 'graphs', 'shortest paths', '*2100'] |
E. Turtle vs. Rabbit Race: Optimal Trainingstime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputIsaac begins his training. There are n running tracks available, and the i-th track (1 \le i \le n) consists of a_i equal-length sections. Given an integer u (1 \le u \le 10^9), finishing each section can increase Isaac's ability by a certain value, described as follows: Finishing the 1-st section increases Isaac's performance by u. Finishing the 2-nd section increases Isaac's performance by u-1. Finishing the 3-rd section increases Isaac's performance by u-2. \ldots Finishing the k-th section (k \ge 1) increases Isaac's performance by u+1-k. (The value u+1-k can be negative, which means finishing an extra section decreases Isaac's performance.) You are also given an integer l. You must choose an integer r such that l \le r \le n and Isaac will finish each section of each track l, l + 1, \dots, r (that is, a total of \sum_{i=l}^r a_i = a_l + a_{l+1} + \ldots + a_r sections).Answer the following question: what is the optimal r you can choose that the increase in Isaac's performance is maximum possible? If there are multiple r that maximize the increase in Isaac's performance, output the smallest r.To increase the difficulty, you need to answer the question for q different values of l and u.InputThe first line of input contains a single integer t (1 \le t \le 10^4) — the number of test cases.The descriptions of the test cases follow.The first line contains a single integer n (1 \le n \le 10^5). The second line contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^4).The third line contains a single integer q (1 \le q \le 10^5).The next q lines each contain two integers l and u (1 \le l \le n, 1 \le u \le 10^9) — the descriptions to each query.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 q integers: the i-th integer contains the optimal r for the i-th query. If there are multiple solutions, output the smallest one.ExampleInput
563 1 4 1 5 931 82 75 911011 195 10 9 6 8 3 10 7 358 561 129 31 275 4557 9 2 5 2101 372 93 334 324 152 24 22 193 72 7109 1 6 7 6 3 10 7 3 10510 433 239 36 85 14Output
3 4 5
1
9 2 9 4 9
5 2 5 5 5 2 4 5 4 2
10 6 9 7 7
NoteFor the 1-st query in the first test case: By choosing r = 3, Isaac finishes a_1 + a_2 + a_3 = 3 + 1 + 4 = 8 sections in total, hence his increase in performance is u+(u-1)+\ldots+(u-7)=8+7+6+5+4+3+2+1 = 36. By choosing r = 4, Isaac finishes a_1 + a_2 + a_3 + a_4 = 3 + 1 + 4 + 1 = 9 sections in total, hence his increase in performance is u+(u-1)+\ldots+(u-8)=8+7+6+5+4+3+2+1+0 = 36. Both choices yield the optimal increase in performance, however we want to choose the smallest r. So we choose r = 3.For the 2-nd query in the first test case, by choosing r = 4, Isaac finishes a_2 + a_3 + a_4 = 1 + 4 + 1 = 6 sections in total, hence his increase in performance is u+(u-1)+\ldots+(u-5)=7+6+5+4+3+2 = 27. This is the optimal increase in performance.For the 3-rd query in the first test case: By choosing r = 5, Isaac finishes a_5 = 5 sections in total, hence his increase in performance is u+(u-1)+\ldots+(u-4)=9+8+7+6+5 = 35. By choosing r = 6, Isaac finishes a_5 + a_6 = 5 + 9 = 14 sections in total, hence his increase in performance is u+(u-1)+\ldots+(u-13)=9+8+7+6+5+4+3+2+1+0+(-1)+(-2)+(-3)+(-4) = 35. Both choices yield the optimal increase in performance, however we want to choose the smallest r. So we choose r = 5.Hence the output for the first test case is [3, 4, 5]. | 563 1 4 1 5 931 82 75 911011 195 10 9 6 8 3 10 7 358 561 129 31 275 4557 9 2 5 2101 372 93 334 324 152 24 22 193 72 7109 1 6 7 6 3 10 7 3 10510 433 239 36 85 14 | 3 4 5 1 9 2 9 4 9 5 2 5 5 5 2 4 5 4 2 10 6 9 7 7 | 5 seconds | 256 megabytes | ['binary search', 'implementation', 'math', 'ternary search', '*1500'] |
D. Turtle Tenacity: Continual Modstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array a_1, a_2, \ldots, a_n, determine whether it is possible to rearrange its elements into b_1, b_2, \ldots, b_n, such that b_1 \bmod b_2 \bmod \ldots \bmod b_n \neq 0.Here x \bmod y denotes the remainder from dividing x by y. Also, the modulo operations are calculated from left to right. That is, x \bmod y \bmod z = (x \bmod y) \bmod z. For example, 2024 \bmod 1000 \bmod 8 = (2024 \bmod 1000) \bmod 8 = 24 \bmod 8 = 0.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains a single integer n (2 \le n \le 10^5).The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^9).The sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output "YES" if it is possible, "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
861 2 3 4 5 653 3 3 3 332 2 351 1 2 3 731 2 231 1 265 2 10 10 10 243 6 9 3Output
YES
NO
YES
NO
YES
NO
YES
NO
NoteIn the first test case, rearranging the array into b = [1, 2, 3, 4, 5, 6] (doing nothing) would result in 1 \bmod 2 \bmod 3 \bmod 4 \bmod 5 \bmod 6 = 1. Hence it is possible to achieve the goal.In the second test case, the array b must be equal to [3, 3, 3, 3, 3], which would result in 3 \bmod 3 \bmod 3 \bmod 3 \bmod 3 = 0. Hence it is impossible to achieve the goal.In the third test case, rearranging the array into b = [3, 2, 2] would result in 3 \bmod 2 \bmod 2 = 1. Hence it is possible to achieve the goal. | 861 2 3 4 5 653 3 3 3 332 2 351 1 2 3 731 2 231 1 265 2 10 10 10 243 6 9 3 | YES NO YES NO YES NO YES NO | 2 seconds | 256 megabytes | ['constructive algorithms', 'greedy', 'math', 'number theory', 'sortings', '*1200'] |
C. Turtle Fingers: Count the Values of ktime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given three positive integers a, b and l (a,b,l>0). It can be shown that there always exists a way to choose non-negative (i.e. \ge 0) integers k, x, and y such that l = k \cdot a^x \cdot b^y. Your task is to find the number of distinct possible values of k across all such ways.InputThe first line contains the integer t (1 \le t \le 10^4) — the number of test cases.The following t lines contain three integers, a, b and l (2 \le a, b \le 100, 1 \le l \le 10^6) — description of a test case.OutputOutput t lines, with the i-th (1 \le i \le t) line containing an integer, the answer to the i-th test case.ExampleInput
112 5 202 5 214 6 482 3 723 5 752 2 10243 7 83349100 100 10000007 3 22 6 617 3 632043Output
6
1
5
12
6
11
24
4
1
3
24
NoteIn the first test case, a=2, b=5, l=20. The possible values of k (and corresponding x,y) are as follows: Choose k = 1, x = 2, y = 1. Then k \cdot a^x \cdot b^y = 1 \cdot 2^2 \cdot 5^1 = 20 = l. Choose k = 2, x = 1, y = 1. Then k \cdot a^x \cdot b^y = 2 \cdot 2^1 \cdot 5^1 = 20 = l. Choose k = 4, x = 0, y = 1. Then k \cdot a^x \cdot b^y = 4 \cdot 2^0 \cdot 5^1 = 20 = l. Choose k = 5, x = 2, y = 0. Then k \cdot a^x \cdot b^y = 5 \cdot 2^2 \cdot 5^0 = 20 = l. Choose k = 10, x = 1, y = 0. Then k \cdot a^x \cdot b^y = 10 \cdot 2^1 \cdot 5^0 = 20 = l. Choose k = 20, x = 0, y = 0. Then k \cdot a^x \cdot b^y = 20 \cdot 2^0 \cdot 5^0 = 20 = l. In the second test case, a=2, b=5, l=21. Note that l = 21 is not divisible by either a = 2 or b = 5. Therefore, we can only set x = 0, y = 0, which corresponds to k = 21.In the third test case, a=4, b=6, l=48. The possible values of k (and corresponding x,y) are as follows: Choose k = 2, x = 1, y = 1. Then k \cdot a^x \cdot b^y = 2 \cdot 4^1 \cdot 6^1 = 48 = l. Choose k = 3, x = 2, y = 0. Then k \cdot a^x \cdot b^y = 3 \cdot 4^2 \cdot 6^0 = 48 = l. Choose k = 8, x = 0, y = 1. Then k \cdot a^x \cdot b^y = 8 \cdot 4^0 \cdot 6^1 = 48 = l. Choose k = 12, x = 1, y = 0. Then k \cdot a^x \cdot b^y = 12 \cdot 4^1 \cdot 6^0 = 48 = l. Choose k = 48, x = 0, y = 0. Then k \cdot a^x \cdot b^y = 48 \cdot 4^0 \cdot 6^0 = 48 = l. | 112 5 202 5 214 6 482 3 723 5 752 2 10243 7 83349100 100 10000007 3 22 6 617 3 632043 | 6 1 5 12 6 11 24 4 1 3 24 | 5 seconds | 256 megabytes | ['brute force', 'implementation', 'math', 'number theory', '*1100'] |
B. Turtle Math: Fast Three Tasktime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a_1, a_2, \ldots, a_n.In one move, you can perform either of the following two operations: Choose an element from the array and remove it from the array. As a result, the length of the array decreases by 1; Choose an element from the array and increase its value by 1. You can perform any number of moves. If the current array becomes empty, then no more moves can be made.Your task is to find the minimum number of moves required to make the sum of the elements of the array a divisible by 3. It is possible that you may need 0 moves.Note that the sum of the elements of an empty array (an array of length 0) is equal to 0.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains a single integer n (1 \le n \le 10^5).The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^4).The sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer: the minimum number of moves.ExampleInput
842 2 5 431 3 243 7 6 81142 2 4 225 572 4 8 1 9 3 424 10Output
1
0
0
1
1
2
1
1
NoteIn the first test case, initially the array a = [2, 2, 5, 4]. One of the optimal ways to make moves is: remove the current 4th element and get a = [2, 2, 5]; As a result, the sum of the elements of the array a will be divisible by 3 (indeed, a_1 + a_2 + a_3 = 2 + 2 + 5 = 9).In the second test case, initially, the sum of the array is 1+3+2 = 6, which is divisible by 3. Therefore, no moves are required. Hence, the answer is 0.In the fourth test case, initially, the sum of the array is 1, which is not divisible by 3. By removing its only element, you will get an empty array, so its sum is 0. Hence, the answer is 1. | 842 2 5 431 3 243 7 6 81142 2 4 225 572 4 8 1 9 3 424 10 | 1 0 0 1 1 2 1 1 | 2 seconds | 256 megabytes | ['implementation', 'math', 'number theory', '*800'] |
A. Turtle Puzzle: Rearrange and Negatetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a of n integers. You must perform the following two operations on the array (the first, then the second): Arbitrarily rearrange the elements of the array or leave the order of its elements unchanged. Choose at most one contiguous segment of elements and replace the signs of all elements in this segment with their opposites. Formally, you can choose a pair of indices l, r such that 1 \le l \le r \le n and assign a_i = -a_i for all l \le i \le r (negate elements). Note that you may choose not to select a pair of indices and leave all the signs of the elements unchanged. What is the maximum sum of the array elements after performing these two operations (the first, then the second)?InputThe first line of the input contains a single integer t (1 \le t \le 1000) — the number of test cases. The descriptions of the test cases follow.The first line of each test case contains a single integer n (1 \le n \le 50) — the number of elements in array a. The second line of each test case contains n integers a_1, a_2, \ldots, a_n (-100 \le a_i \le 100) — elements of the array.OutputFor each test case, output the maximum sum of the array elements after sequentially performing the two given operations.ExampleInput
83-2 3 -31020 11-99410 -2 -3 75-1 -2 -3 -4 -56-41 22 -69 73 -15 -50121 2 3 4 5 6 7 8 9 10 11 12Output
8
0
1
99
22
15
270
78
NoteIn the first test case, you can first rearrange the array to get [3,-2,-3] (operation 1), then choose l = 2, r = 3 and get the sum 3 + -((-2) + (-3)) = 8 (operation 2).In the second test case, you can do nothing in both operations and get the sum 0.In the third test case, you can do nothing in both operations and get the sum 0 + 1 = 1.In the fourth test case, you can first leave the order unchanged (operation 1), then choose l = 1, r = 1 and get the sum -(-99) = 99 (operation 2).In the fifth test case, you can first leave the order unchanged (operation 1), then choose l = 2, r = 3 and get the sum 10 + -((-2) + (-3)) + 7 = 22 (operation 2).In the sixth test case, you can first leave the order unchanged (operation 1), then choose l = 1, r = 5 and get the sum -((-1)+(-2)+(-3)+(-4)+(-5))=15 (operation 2). | 83-2 3 -31020 11-99410 -2 -3 75-1 -2 -3 -4 -56-41 22 -69 73 -15 -50121 2 3 4 5 6 7 8 9 10 11 12 | 8 0 1 99 22 15 270 78 | 2 seconds | 256 megabytes | ['greedy', 'math', 'sortings', '*800'] |
G. Moving Platformstime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere is a game where you need to move through a labyrinth. The labyrinth consists of n platforms, connected by m passages. Each platform is at some level l_i, an integer number from 0 to H - 1. In a single step, if you are currently on platform i, you can stay on it, or move to another platform j. To move to platform j they have to be connected by the passage, and their levels have to be the same, namely l_i = l_j.After each step, the levels of all platforms change. The new level of platform i is calculated as l'_i = (l_i + s_i) \bmod H, for all i. You start on platform 1. Find the minimum number of steps you need to get to platform n.InputThe first line of input contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains three integers n, m, and H (2 \le n \le 10^5, 1 \le m \le 10^5, 1 \le H \le 10^9).The second line contains n integers l_i, the initial level of each platform (0 \le l_i \le H-1). The third line contains n integers s_i, the change of level for each platform (0 \le s_i \le H-1).Next m lines contain a description of the passages. Each passage is described as a pair of integers — the platforms, connected by the passage. There is at most one passage connecting each pair of platforms, and there is no passage connecting a platform to itself.The sum of n for all tests does not exceed 10^5, the sum of m for all tests does not exceed 10^5.OutputFor each test case, print a single integer, the minimum number of steps needed to get from platform 1 to platform n.If it is impossible to get to platform n, print -1.ExampleInput
33 3 101 9 42 3 01 23 21 32 1 101 24 61 28 7 2522 14 5 3 10 14 11 19 5 4 10 7 16 18 182 86 33 57 52 61 44 7Output
6
-1
52
NoteThis is how levels of the platforms change, and what actions we need to perform in the first example.Platform 1Platform 2Platform 3ActionStep 1194Stay on the platform 1Step 2324Stay on the platform 1Step 3554Move to the platform 2Step 4784Stay on the platform 2Step 5914Stay on the platform 2Step 6144Move to the platform 3 | 33 3 101 9 42 3 01 23 21 32 1 101 24 61 28 7 2522 14 5 3 10 14 11 19 5 4 10 7 16 18 182 86 33 57 52 61 44 7 | 6 -1 52 | 3 seconds | 512 megabytes | ['graphs', 'math', 'number theory', 'shortest paths', '*2300'] |
F. Feed Catstime limit per test3 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere is a fun game where you need to feed cats that come and go. The level of the game consists of n steps. There are m cats; the cat i is present in steps from l_i to r_i, inclusive. In each step, you can feed all the cats that are currently present or do nothing. If you feed the same cat more than once, it will overeat, and you will immediately lose the game. Your goal is to feed as many cats as possible without causing any cat to overeat. Find the maximum number of cats you can feed.Formally, you need to select several integer points from the segment from 1 to n in such a way that among given segments, none covers two or more of the selected points, and as many segments as possible cover one of the selected points.InputThe first line of input contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains two integers n and m (1 \le n \le 10^6, 1 \le m\le 2\cdot 10^5). The i-th of the next m lines contains a pair of integers l_i and r_i (1 \le l_i \le r_i \le n).The sum of n for all tests does not exceed 10^6, the sum of m for all tests does not exceed 2\cdot 10^5.OutputFor each test case, print a single integer, the maximum number of cats you can feed.ExampleInput
315 62 103 52 47 78 1211 111000 11 10005 101 23 43 43 43 41 11 23 33 43 4Output
5
1
10
NoteIn the first example, one of the ways to feed five cats is to feed at steps 4 and 11. At step 4, cats 1, 2, and 3 will be fed. At step 11, cats 5 and 6 will be fed. | 315 62 103 52 47 78 1211 111000 11 10005 101 23 43 43 43 41 11 23 33 43 4 | 5 1 10 | 3 seconds | 512 megabytes | ['data structures', 'dp', 'sortings', '*1900'] |
E. Final Countdowntime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are in a nuclear laboratory that is about to explode and destroy the Earth. You must save the Earth before the final countdown reaches zero. The countdown consists of n (1 \le n \le 4 \cdot 10^5) mechanical indicators, each showing one decimal digit. You noticed that when the countdown changes its state from x to x-1, it doesn't happen in one move. Instead, each change of a single digit takes one second. So, for example, if the countdown shows 42, then it will change to 41 in one second, because only one digit is changed, but if the countdown shows 2300, then it will change to 2299 in three seconds, because the three last digits are changed.Find out how much time is left before the countdown reaches zero.InputThe first line of input contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains a single integer n (1\le n\le 4\cdot 10^5). The second line contains a string of n digits, the current state of the countdown. It is guaranteed that at least one digit is not zero.The sum of n for all tests does not exceed 4\cdot 10^5.OutputFor each test case, print a single integer without leading zeroes, the number of seconds left before the countdown reaches zero. Note that this number may be huge.ExampleInput
52425123452994000527456480697259671309012631002Output
46
13715
108
5
507200774732968121125145546
NoteIn the first example, there are four changes that take 2 seconds: 40 to 39, 30 to 29, 20 to 19, and 10 to 09, other changes take 1 second each. So the total time is 2\cdot 4 + 1\cdot(42-4) = 46. | 52425123452994000527456480697259671309012631002 | 46 13715 108 5 507200774732968121125145546 | 2 seconds | 512 megabytes | ['implementation', 'math', 'number theory', '*1600'] |
D. Card Gametime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputTwo players are playing an online card game. The game is played using a 32-card deck. Each card has a suit and a rank. There are four suits: clubs, diamonds, hearts, and spades. We will encode them with characters 'C', 'D', 'H', and 'S', respectively. And there are 8 ranks, in increasing order: '2', '3', '4', '5', '6', '7', '8', '9'.Each card is denoted by two letters: its rank and its suit. For example, the 8 of Hearts is denoted as 8H.At the beginning of the game, one suit is chosen as the trump suit.In each round, players make moves like this: the first player places one of his cards on the table, and the second player must beat this card with one of their cards. After that, both cards are moved to the discard pile.A card can beat another card if both cards have the same suit and the first card has a higher rank than the second. For example, 8S can beat 4S. Additionally, a trump card can beat any non-trump card, regardless of the rank of the cards, for example, if the trump suit is clubs ('C'), then 3C can beat 9D. Note that trump cards can be beaten only by the trump cards of higher rank.There were n rounds played in the game, so the discard pile now contains 2n cards. You want to reconstruct the rounds played in the game, but the cards in the discard pile are shuffled. Find any possible sequence of n rounds that might have been played in the game.InputThe first line contains integer t (1 \le t \le 100) — the number of test cases. Then t test cases follow.The first line of a test case contains the integer number n (1\le n\le 16).The second line of a test case contains one character, the trump suit. It is one of "CDHS".The third line of a test case contains the description of 2n cards. Each card is described by a two-character string, the first character is the rank of the card, which is one of "23456789", and the second one is the suit of the card, which is one of "CDHS". All cards are different.OutputFor each test case print the answer to it: Print n lines. In each line, print the description of two cards, in the same format as in the input: the first card that was played by the first player, and then the card that was used by the second player to beat it. If there is no solution, print a single line "IMPOSSIBLE".If there are multiple solutions, print any of them.ExampleInput
83S3C 9S 4C 6D 3S 7S2C3S 5D 9S 6H1H6C 5D1S7S 3S1H9S 9H1S9S 9H1C9D 8H2C9C 9S 6H 8COutput
3C 4C
6D 9S
3S 7S
IMPOSSIBLE
IMPOSSIBLE
3S 7S
9S 9H
9H 9S
IMPOSSIBLE
6H 9C
9S 8C
| 83S3C 9S 4C 6D 3S 7S2C3S 5D 9S 6H1H6C 5D1S7S 3S1H9S 9H1S9S 9H1C9D 8H2C9C 9S 6H 8C | 3C 4C 6D 9S 3S 7S IMPOSSIBLE IMPOSSIBLE 3S 7S 9S 9H 9H 9S IMPOSSIBLE 6H 9C 9S 8C | 2 seconds | 256 megabytes | ['greedy', 'implementation', '*1400'] |
C. LR-remainderstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a of length n, a positive integer m, and a string of commands of length n. Each command is either the character 'L' or the character 'R'.Process all n commands in the order they are written in the string s. Processing a command is done as follows: First, output the remainder of the product of all elements of the array a when divided by m. Then, if the command is 'L', remove the leftmost element from the array a, if the command is 'R', remove the rightmost element from the array a. Note that after each move, the length of the array a decreases by 1, and after processing all commands, it will be empty.Write a program that will process all commands in the order they are written in the string s (from left to right).InputThe first line contains an integer t (1 \le t \le 10^4) — the number of test cases in the input. Then descriptions of t test cases follow.Each test case of the input is given by three lines.The first line contains two integers n and m (1 \le n \le 2\cdot10^5, 1 \le m \le 10^4) — the initial length of the array a and the value to take the remainder by.The second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^4) — the elements of the array a.The third line contains a string s consisting of n characters 'L' and 'R'.It is guaranteed that the sum of the values of n for all test cases in a test does not exceed 2\cdot10^5.OutputFor each test case, output n integers b_1, b_2, \dots, b_n, where b_i is the remainder when dividing the product of all elements of the current state of the array a by m at the beginning of the execution of the i-th command.ExampleInput
44 63 1 4 2LRRL5 11 1 1 1 1LLLLL6 81 2 3 4 5 6RLLLRR1 1000010000ROutput
0 2 4 1
0 0 0 0 0
0 0 0 4 4 4
0
NoteIn the first test case of the example: 3 \cdot 1 \cdot 4 \cdot 2 \bmod 6 = 24 \bmod 6 = 0; s_1 = \text{L}, so we remove the first element and get the array [1, 4, 2]; 1 \cdot 4 \cdot 2 \bmod 6 = 8 \bmod 6 = 2; s_2 = \text{R}, so we remove the last element and get the array [1, 4]; 1 \cdot 4 \bmod 6 = 4 \bmod 6 = 4; s_3 = \text{R}, so we remove the last element and get the array [1]; 1 \bmod 6 = 1; s_4 = \text{L}, so we remove the first element and get an empty array. | 44 63 1 4 2LRRL5 11 1 1 1 1LLLLL6 81 2 3 4 5 6RLLLRR1 1000010000R | 0 2 4 1 0 0 0 0 0 0 0 0 4 4 4 0 | 2 seconds | 256 megabytes | ['brute force', 'data structures', 'implementation', 'math', 'two pointers', '*1400'] |
B. Chaya Calendartime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThe Chaya tribe believes that there are n signs of the apocalypse. Over time, it has been found out that the i-th sign occurs every a_i years (in years a_i, 2 \cdot a_i, 3 \cdot a_i, \dots).According to the legends, for the apocalypse to happen, the signs must occur sequentially. That is, first they wait for the first sign to occur, then strictly after it, the second sign will occur, and so on. That is, if the i-th sign occurred in the year x, the tribe starts waiting for the occurrence of the (i+1)-th sign, starting from the year x+1.In which year will the n-th sign occur and the apocalypse will happen?InputThe first line of the input contains a single integer t (1 \le t \le 1000) — the number of test cases. Then follow the descriptions of the test cases.The first line of each test case contains a single integer n (1 \le n \le 100) — the number of signs.The second line of each test case contains n integers a_1, a_2, a_3, \dots, a_n (1 \le a_i \le 10^6) — the periodicities of the signs.OutputFor each test case, output a single integer — the year in which all n signs will occur.ExampleInput
463 2 4 5 9 1851 2 3 4 551 1 1 1 1650 30 711 200 503 1006Output
36
5
5
2012
NoteIn the first set of input data of the example: The tribe will wait for the first sign in the 3-rd year; the tribe will wait for the second sign in the 4-th year (since year 2 have already passed); the tribe will wait for the third sign in the 8-th year (since the second sign has already occurred in the 4-th year); the tribe will wait for the fourth sign in the 10-th year (since year 5 have already passed); the tribe will wait for the fifth sign in the 18-th year (since year 9 have already passed); the tribe will wait for the sixth sign in the 36-th year (since the fifth sign has already occurred in the 18-th year). | 463 2 4 5 9 1851 2 3 4 551 1 1 1 1650 30 711 200 503 1006 | 36 5 5 2012 | 2 seconds | 256 megabytes | ['number theory', '*1100'] |
A. Thorns and Coinstime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard outputDuring your journey through computer universes, you stumbled upon a very interesting world. It is a path with n consecutive cells, each of which can either be empty, contain thorns, or a coin. In one move, you can move one or two cells along the path, provided that the destination cell does not contain thorns (and belongs to the path). If you move to the cell with a coin, you pick it up. Here, green arrows correspond to legal moves, and the red arrow corresponds to an illegal move. You want to collect as many coins as possible. Find the maximum number of coins you can collect in the discovered world if you start in the leftmost cell of the path.InputThe first line of input contains a single integer t (1 \le t \le 1000) — the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains a single integer n (1 \le n \le 50) — the length of the path.The second line of each test case contains a string of n characters, the description of the path. The character '.' denotes an empty cell, '@' denotes a cell with a coin, and '*' denotes a cell with thorns. It is guaranteed that the first cell is empty.OutputFor each test case, output a single integer, the maximum number of coins you can collect.ExampleInput
310.@@*@.**@@5.@@@@15.@@..@***..@@@*Output
3
4
3
NoteThe picture for the first example is in the problem statement.Here is the picture for the second example: And here is the picture for the third example: | 310.@@*@.**@@5.@@@@15.@@..@***..@@@* | 3 4 3 | 1 second | 512 megabytes | ['dp', 'greedy', 'implementation', '*800'] |
G. One-Dimensional Puzzletime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a one-dimensional puzzle, all the elements of which need to be put in one row, connecting with each other. All the puzzle elements are completely white and distinguishable from each other only if they have different shapes. Each element has straight borders at the top and bottom, and on the left and right it has connections, each of which can be a protrusion or a recess. You cannot rotate the elements.You can see that there are exactly 4 types of elements. Two elements can be connected if the right connection of the left element is opposite to the left connection of the right element. All possible types of elements. The puzzle contains c_1, c_2, c_3, c_4 elements of each type. The puzzle is considered complete if you have managed to combine all elements into one long chain. You want to know how many ways this can be done.InputThe first line contains a single integer t (1 \le t \le 2 \cdot 10^5) — the number of input test cases. The descriptions of the test cases follow.The description of each test case contains 4 integers c_i (0 \le c_i \le 10^6) — the number of elements of each type, respectively.It is guaranteed that the sum of c_i for all test cases does not exceed 4 \cdot 10^6.OutputFor each test case, print one integer — the number of possible ways to solve the puzzle. Two methods are considered different if there is i, such that the types of elements at the i position in these methods differ.Since the answer can be very large, output it modulo 998244353.If it is impossible to solve the puzzle, print 0.ExampleInput
111 1 1 11 2 5 104 6 100 200900000 900000 900000 9000000 0 0 00 0 566 2391 0 0 0100 0 100 00 0 0 45 5 0 25 4 0 5Output
4
66
0
794100779
1
0
1
0
1
36
126
| 111 1 1 11 2 5 104 6 100 200900000 900000 900000 9000000 0 0 00 0 566 2391 0 0 0100 0 100 00 0 0 45 5 0 25 4 0 5 | 4 66 0 794100779 1 0 1 0 1 36 126 | 4 seconds | 256 megabytes | ['combinatorics', 'math', 'number theory', '*2000'] |
F. Chat Screenshotstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n people in the programming contest chat. Chat participants are ordered by activity, but each person sees himself at the top of the list.For example, there are 4 participants in the chat, and their order is [2, 3, 1, 4]. Then 1-st user sees the order [1, 2, 3, 4]. 2-nd user sees the order [2, 3, 1, 4]. 3-rd user sees the order [3, 2, 1, 4]. 4-th user sees the order [4, 2, 3, 1]. k people posted screenshots in the chat, which show the order of participants shown to this user. The screenshots were taken within a short period of time, and the order of participants has not changed. Your task is to determine whether there is a certain order that all screenshots correspond to.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of input test cases. The descriptions of test cases follow.The first line of the description of each test case contains two integers n and k (1 \le k \le n \le 2 \cdot 10^5, n \cdot k \le 2 \cdot 10^5) — the number of chat participants and the number of participants who posted screenshots.The following k lines contain descriptions of screenshots posted by the participants.The i-th row contains n integers a_{ij} each (1 \le a_{ij} \le n, all a_{ij} are different) — the order of participants shown to the participant a_{i0}, where a_{i0} — the author of the screenshot. You can show that in the screenshot description it will always be at the top of the list.It is guaranteed that the sum of n \cdot k for all test cases does not exceed 2 \cdot 10^5. It is also guaranteed that all the authors of the screenshots are different.OutputOutput t lines, each of which is the answer to the corresponding test case. As an answer, output "YES" if there exists at least one order of participants, under which all k screenshots could have been obtained. Otherwise, output "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
105 11 2 3 4 54 41 2 3 42 3 1 43 2 1 44 2 3 16 21 3 5 2 4 66 3 5 2 1 43 31 2 32 3 13 2 110 21 2 3 4 5 6 7 8 9 1010 9 8 7 6 5 4 3 2 11 115 21 2 3 5 42 1 3 5 43 33 1 22 3 11 3 25 43 5 1 4 22 5 1 4 31 5 4 3 25 1 4 3 23 31 3 22 1 33 2 1Output
YES
YES
YES
YES
NO
YES
YES
YES
YES
NO
| 105 11 2 3 4 54 41 2 3 42 3 1 43 2 1 44 2 3 16 21 3 5 2 4 66 3 5 2 1 43 31 2 32 3 13 2 110 21 2 3 4 5 6 7 8 9 1010 9 8 7 6 5 4 3 2 11 115 21 2 3 5 42 1 3 5 43 33 1 22 3 11 3 25 43 5 1 4 22 5 1 4 31 5 4 3 25 1 4 3 23 31 3 22 1 33 2 1 | YES YES YES YES NO YES YES YES YES NO | 2 seconds | 256 megabytes | ['combinatorics', 'dfs and similar', 'graphs', '*1700'] |
E. Anna and the Valentine's Day Gifttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSasha gave Anna a list a of n integers for Valentine's Day. Anna doesn't need this list, so she suggests destroying it by playing a game.Players take turns. Sasha is a gentleman, so he gives Anna the right to make the first move. On her turn, Anna must choose an element a_i from the list and reverse the sequence of its digits. For example, if Anna chose the element with a value of 42, it would become 24; if Anna chose the element with a value of 1580, it would become 851. Note that leading zeros are removed. After such a turn, the number of elements in the list does not change. On his turn, Sasha must extract two elements a_i and a_j (i \ne j) from the list, concatenate them in any order and insert the result back into the list. For example, if Sasha chose the elements equal to 2007 and 19, he would remove these two elements from the list and add the integer 200719 or 192007. After such a turn, the number of elements in the list decreases by 1.Players can't skip turns. The game ends when Sasha can't make a move, i.e. after Anna's move there is exactly one number left in the list. If this integer is not less than 10^m (i.e., \ge 10^m), Sasha wins. Otherwise, Anna wins.It can be shown that the game will always end. Determine who will win if both players play optimally.InputThe first line contains an integer t (1 \le t \le 10^4) — the number of test cases.Then follows the description of the test cases.The first line of each test case contains integers n, m (1 \le n \le 2 \cdot 10^5, 0 \le m \le 2 \cdot 10^6) — the number of integers in the list and the parameter determining when Sasha wins.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \le a_i \le 10^9) — the list that Sasha gave to Anna.It is guaranteed that the sum of n for all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output: "Sasha", if Sasha wins with optimal play; "Anna", if Anna wins with optimal play. ExampleInput
92 214 23 59 56 14 101 2007 800 15804 55000 123 30 410 106 4 6 2 3 1 10 9 10 71 161 1108 91 2 9 10 10 2 10 24 510 10 10 10Output
Sasha
Anna
Anna
Sasha
Sasha
Anna
Anna
Anna
Sasha
NoteConsider the first test case.Anna can reverse the integer 2, then Sasha can concatenate the integers 2 and 14, obtaining the integer 214, which is greater than 10^2 = 100. If Anna had reversed the integer 14, Sasha would have concatenated the integers 41 and 2, obtaining the integer 412, which is greater than 10^2 = 100. Anna has no other possible moves, so she loses. | 92 214 23 59 56 14 101 2007 800 15804 55000 123 30 410 106 4 6 2 3 1 10 9 10 71 161 1108 91 2 9 10 10 2 10 24 510 10 10 10 | Sasha Anna Anna Sasha Sasha Anna Anna Anna Sasha | 2 seconds | 256 megabytes | ['games', 'greedy', 'math', 'sortings', '*1400'] |
D. Divisible Pairstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp has two favorite integers x and y (they can be equal), and he has found an array a of length n.Polycarp considers a pair of indices \langle i, j \rangle (1 \le i < j \le n) beautiful if: a_i + a_j is divisible by x; a_i - a_j is divisible by y. For example, if x=5, y=2, n=6, a=[1, 2, 7, 4, 9, 6], then the only beautiful pairs are: \langle 1, 5 \rangle: a_1 + a_5 = 1 + 9 = 10 (10 is divisible by 5) and a_1 - a_5 = 1 - 9 = -8 (-8 is divisible by 2); \langle 4, 6 \rangle: a_4 + a_6 = 4 + 6 = 10 (10 is divisible by 5) and a_4 - a_6 = 4 - 6 = -2 (-2 is divisible by 2). Find the number of beautiful pairs in the array a.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains three integers n, x, and y (2 \le n \le 2 \cdot 10^5, 1 \le x, y \le 10^9) — the size of the array and Polycarp's favorite integers.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 elements of the array.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the number of beautiful pairs in the array a.ExampleInput
76 5 21 2 7 4 9 67 9 51 10 15 3 8 12 159 4 1014 10 2 2 11 11 13 5 69 5 610 7 6 7 9 7 7 10 109 6 24 9 7 1 2 2 13 3 159 2 314 6 1 15 12 15 8 2 1510 5 713 3 3 2 12 11 3 7 13 14Output
2
0
1
3
5
7
0
| 76 5 21 2 7 4 9 67 9 51 10 15 3 8 12 159 4 1014 10 2 2 11 11 13 5 69 5 610 7 6 7 9 7 7 10 109 6 24 9 7 1 2 2 13 3 159 2 314 6 1 15 12 15 8 2 1510 5 713 3 3 2 12 11 3 7 13 14 | 2 0 1 3 5 7 0 | 2 seconds | 256 megabytes | ['combinatorics', 'math', 'number theory', '*1300'] |
C. Make Equal Againtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have an array a of n integers. You can no more than once apply the following operation: select three integers i, j, x (1 \le i \le j \le n) and assign all elements of the array with indexes from i to j the value x. The price of this operation depends on the selected indices and is equal to (j - i + 1) burles. For example, the array is equal to [1, 2, 3, 4, 5, 1]. If we choose i = 2, j = 4, x = 8, then after applying this operation, the array will be equal to [1, 8, 8, 8, 5, 1].What is the least amount of burles you need to spend to make all the elements of the array equal?InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of input test cases. The descriptions of the test cases follow.The first line of the description of each test case contains a single integer n (1 \le n \le 2 \cdot 10 ^ 5) — the size of the array.The second line of the description of each test case contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le n) — array elements.It is guaranteed that the sum of n for all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output one integer — the minimum number of burles that will have to be spent to make all the elements of the array equal. It can be shown that this can always be done.ExampleInput
861 2 3 4 5 171 1 1 1 1 1 188 8 8 1 2 8 8 81121 231 2 374 3 2 7 1 1 399 9 2 9 2 5 5 5 3Output
4
0
2
0
1
2
6
7
| 861 2 3 4 5 171 1 1 1 1 1 188 8 8 1 2 8 8 81121 231 2 374 3 2 7 1 1 399 9 2 9 2 5 5 5 3 | 4 0 2 0 1 2 6 7 | 2 seconds | 256 megabytes | ['brute force', 'greedy', 'math', '*1000'] |
B. Make Equaltime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n containers of water lined up, numbered from left to right from 1 to n. Each container can hold any amount of water; initially, the i-th container contains a_i units of water. The sum of a_i is divisible by n.You can apply the following operation any (possibly zero) number of times: pour any amount of water from the i-th container to the j-th container, where i must be less than j (i.e. i<j). Any index can be chosen as i or j any number of times.Determine whether it is possible to make the amount of water in all containers the same using this operation.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains a single integer n (1 \le n \le 2 \cdot 10^5) — the number of containers with water.The second line of each test case contains n integers a_1, a_2, \dots, a_n (0 \le a_i \le 10^9) — the amounts of water in the containers. It is guaranteed that the sum of a_i in each test case does not exceed 2 \cdot 10^9. Also, the sum of a_i is divisible by n.It is guaranteed that the sum of n over all test cases in the input does not exceed 2 \cdot 10^5.OutputOutput t lines, each of which is the answer to the corresponding test case. As the answer, output "YES" if it is possible to make the amount of water in all containers the same using the described operation. Otherwise, output "NO".You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.ExampleInput
614321 354 5 2 1 331 2 374 5 5 0 6 4 476 5 5 1 3 4 4Output
YES
NO
YES
NO
NO
YES
NoteIn the third test case of the example (a=[4, 5, 2, 1, 3]), you can proceed as follows: pour 1 unit of water from the first vessel to the fourth, then a=[3, 5, 2, 2, 3]; pour 1 unit of water from the second vessel to the third, then a=[3, 4, 3, 2, 3]; pour 1 unit of water from the second vessel to the fourth, then a=[3, 3, 3, 3, 3]. | 614321 354 5 2 1 331 2 374 5 5 0 6 4 476 5 5 1 3 4 4 | YES NO YES NO NO YES | 2 seconds | 256 megabytes | ['greedy', '*800'] |
A. Recovering a Small Stringtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputNikita had a word consisting of exactly 3 lowercase Latin letters. The letters in the Latin alphabet are numbered from 1 to 26, where the letter "a" has the index 1, and the letter "z" has the index 26.He encoded this word as the sum of the positions of all the characters in the alphabet. For example, the word "cat" he would encode as the integer 3 + 1 + 20 = 24, because the letter "c" has the index 3 in the alphabet, the letter "a" has the index 1, and the letter "t" has the index 20.However, this encoding turned out to be ambiguous! For example, when encoding the word "ava", the integer 1 + 22 + 1 = 24 is also obtained.Determine the lexicographically smallest word of 3 letters that could have been encoded.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 of the input contains a single integer t (1 \le t \le 100) — the number of test cases in the test.This is followed by the descriptions of the test cases.The first and only line of each test case contains an integer n (3 \le n \le 78) — the encoded word.OutputFor each test case, output the lexicographically smallest three-letter word that could have been encoded on a separate line.ExampleInput
5247035548Output
aav
rzz
aaa
czz
auz
| 5247035548 | aav rzz aaa czz auz | 1 second | 256 megabytes | ['brute force', 'strings', '*800'] |
I. Counting Is Funtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a binary^\dagger pattern p of length n.A binary string q of the same length n is called good if for every i (1 \leq i \leq n), there exist indices l and r such that: 1 \leq l \leq i \leq r \leq n, and p_i is a mode^\ddagger of the string q_lq_{l+1}\ldots q_r. Count the number of good binary strings modulo 998\,244\,353.^\dagger A binary string is a string that only consists of characters \mathtt{0} and \mathtt{1}.^\ddagger Character c is a mode of string t of length m if the number of occurrences of c in t is at least \lceil \frac{m}{2} \rceil. For example, \mathtt{0} is a mode of \mathtt{010}, \mathtt{1} is not a mode of \mathtt{010}, and both \mathtt{0} and \mathtt{1} are modes of \mathtt{011010}.InputThe first line of input contains a single integer n (1 \le n \le 10^5) — the length of the binary string p.The second line of input contains a binary string p of length n consisting of characters 0 and 1.OutputOutput the number of good strings modulo 998\,244\,353.ExamplesInput
10Output
1
Input
3111Output
5
Input
41011Output
9
Input
6110001Output
36
Input
12111010001111Output
2441
NoteIn the second example, the good strings are \mathtt{010}; \mathtt{011}; \mathtt{101}; \mathtt{110}; \mathtt{111}. | 10 | 1 | 3 seconds | 256 megabytes | ['combinatorics', '*3500'] |
H. Interactive Mex Treetime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem.Alice has a tree T consisting of n nodes, numbered from 1 to n. Alice will show T to Bob. After observing T, Bob needs to tell Alice two permutations p_1 and p_2 of [1, 2, \ldots, n].Then, Alice will play q rounds of the following game. Alice will create an array a that is a permutation of [0,1,\ldots,n-1]. The value of node v will be a_v. Alice will choose two nodes u and v (1 \leq u, v \leq n, u \neq v) of T and tell them to Bob. Bob will need to find the \operatorname{MEX}^\dagger of the values on the unique simple path between nodes u and v. To find this value, Bob can ask Alice at most 5 queries. In each query, Bob should give three integers t, l and r to Alice such that t is either 1 or 2, and 1 \leq l \leq r \leq n. Alice will then tell Bob the value equal to \min_{i=l}^{r} a[p_{t,i}]. Note that all rounds are independent of each other. In particular, the values of a, u and v can be different in different rounds.Bob is puzzled as he only knows the HLD solution, which requires O(\log(n)) queries per round. So he needs your help to win the game. ^\dagger The \operatorname{MEX} (minimum excludant) of a collection of integers c_1, c_2, \ldots, c_k is defined as the smallest non-negative integer x which does not occur in the collection c.InteractionEach test contains multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. Read it. The description of the test cases follows.The first line of each test case contains two positive integers n and q (2 \leq n \leq 10^5, 1 \leq q \leq 10^4) — the number of nodes in T and the number of rounds respectively.The following next n-1 lines contains two integers u and v (1 \leq u, v \leq n, u \neq v) — denoting an edge between nodes u and v. It is guaranteed that the given edges form a tree.It is guaranteed that the sum of n and q over all test cases does not exceed 10^5 and 10^4 respectively.It is also guaranteed that the sum of n \cdot q does not exceed 3 \cdot 10^6.The interaction for each test case begins by outputting two permutations p_1 and p_2 of [1, 2, \ldots, n].On a new line, output n space-separated distinct integers denoting p_1. In the next line, output n space-separated distinct integers denoting p_2. Alice will start playing the game.For each round, you must read two integers, u and v (1 \leq u, v \leq n, u \neq v). You need to find the \operatorname{MEX} of the values on the unique simple path between nodes u and v.To make a query, output "? t l r" without quotes, such that t is either 1 or 2, and 1 \leq l \leq r \leq n. Afterwards, you should read a single integer — the answer to your query \min_{i=l}^{r} a_{p_{t,i}}. You can make at most 5 such queries in each round.If you want to print the answer, output "! x" (1 \leq x, y \leq n) without quotes. After doing that, read a single integer, which is normally equal to 1.If you receive the integer -1 instead of a valid reply, it means your program has made an invalid query, exceeded the query limit, or gave an incorrect answer on the previous test case. Your program must terminate immediately to receive a Wrong Answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream.After printing a query or the answer, do not forget to output the end of the line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. HacksTo hack, follow the test format below.The first line should contain a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case should contain two positive integers n and q (2 \leq n \leq 10^5; 1 \leq q \leq 10^4) — the number of nodes in T and the number of rounds respectively.The following next n-1 lines should contain two integers u and v (1 \leq u, v \leq n, u \neq v) — denoting an edge between nodes u and v. The given edges must form a tree.For each of the q rounds, first print a permutation of [0, 1, 2, \ldots, n-1] on a new line, denoting the array a chosen by Alice during the start of the round.In the following line, print two distinct nodes u and v (1 \leq u, v \leq v, u \neq v), representing the endpoints of the path asked by Alice.The sum of n and q over all test cases should not exceed 10^5 and 10^4 respectively.The sum of n \cdot q should not exceed 3 \cdot 10^6.ExampleInput
1
3 1
1 2
2 3
2 3
1
0
1Output
1 2 3
2 1 3
? 1 2 3
? 2 1 3
! 0
NoteIn the first test, the interaction proceeds as follows. SolutionJuryExplanation1There are 1 test cases.3 1The tree T consists of 3 nodes, and Alice will play for only one round.1 2First edge of T2 3Second edge of T1 2 3The permutation p_12 1 3The permutation p_2Alice shuffles a to a=[0,2,1] before giving the nodes for the only round.2 3Nodes for the round? 1 2 31\min(a_{p_{1,2}},a_{p_{1,3}})=\min(a_2,a_3)=1? 2 1 30\min(a_{p_{2,1}},a_{p_{2,2}},a_{p_{2,3}})=\min(a_2,a_1,a_3)=0! 01Considering the output of queries, it is clear that \operatorname{MEX} is 0. Since the output is correct, the jury responds with 1. After each test case, make sure to read 1 or -1. | 1
3 1
1 2
2 3
2 3
1
0
1 | 1 2 3 2 1 3 ? 1 2 3 ? 2 1 3 ! 0 | 5 seconds | 256 megabytes | ['constructive algorithms', 'dfs and similar', 'interactive', 'trees', '*3300'] |
G. Prefix Max Set Countingtime limit per test5 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputDefine a function f such that for an array b, f(b) returns the array of prefix maxima of b. In other words, f(b) is an array containing only those elements b_i, for which b_i=\max(b_1,b_2,\ldots,b_i), without changing their order. For example, f([3,10,4,10,15,1])=[3,10,10,15].You are given a tree consisting of n nodes rooted at 1.A permutation^\dagger p of is considered a pre-order of the tree if for all i the following condition holds: Let k be the number of proper descendants^\ddagger of node p_i. For all x such that i < x \leq i+k, p_x is a proper descendant of node p_i. Find the number of distinct values of f(a) over all possible pre-orders a. Since this value might be large, you only need to find it modulo 998\,244\,353.^\dagger A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).^\ddagger Node t is a proper descendant of node s if s \neq t and s is on the unique simple path from t to 1.InputEach test contains multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^5) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (1 \leq n \leq 10^6) — the number of vertices.The following next n-1 lines contain two integers u and v (1 \leq u, v \leq n, u \neq v) — denoting an edge between nodes u and v. It is guaranteed that the given edges form a tree.It is guaranteed that the sum of n over all test cases does not exceed 10^6.OutputFor each test case, output the number of distinct values of f(a) modulo 998\,244\,353 that you can get.ExampleInput
6121 231 21 331 22 351 21 31 41 5101 22 31 42 52 64 75 84 99 10Output
1
1
2
1
8
6
NoteIn the first test case, the only valid pre-order is a=[1]. So the only possible value of f(a) is [1].In the second test case, the only valid pre-order is a=[1,2]. So the only possible value f(a) is [1,2].In the third test case, the two valid pre-orders are a=[1,2,3] and a=[1,3,2]. So the possible values of f(a) are [1,2,3] and [1,3].In the fifth test case, the possible values of f(a) are: [1,5]; [1,2,5]; [1,3,5]; [1,4,5]; [1,2,3,5]; [1,2,4,5]; [1,3,4,5]; [1,2,3,4,5]. | 6121 231 21 331 22 351 21 31 41 5101 22 31 42 52 64 75 84 99 10 | 1 1 2 1 8 6 | 5 seconds | 512 megabytes | ['data structures', 'dp', 'trees', '*3100'] |
F. Maximize the Differencetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputFor an array b of m non-negative integers, define f(b) as the maximum value of \max\limits_{i = 1}^{m} (b_i | x) - \min\limits_{i = 1}^{m} (b_i | x) over all possible non-negative integers x, where | is bitwise OR operation.You are given integers n and q. You start with an empty array a. Process the following q queries: v: append v to the back of a and then output f(a). It is guaranteed that 0 \leq v < n. The queries are given in a modified way.InputEach test contains multiple test cases. The first line contains a single integer t (1 \leq t \leq 2 \cdot 10^5) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers n and q (1 \leq n \leq 2^{22}, 1 \leq q \leq 10^6) — the number of queries.The second line of each test case contains q space-separated integers e_1,e_2,\ldots,e_q (0 \leq e_i < n) — the encrypted values of v.Let \mathrm{last}_i equal the output of the (i-1)-th query for i\geq 2 and \mathrm{last}_i=0 for i=1. Then the value of v for the i-th query is (e_i + \mathrm{last}_i) modulo n. It is guaranteed that the sum of n over all test cases does not exceed 2^{22} and the sum of q over all test cases does not exceed 10^6.OutputFor each test case, print q integers. The i-th integer is the output of the i-th query.ExampleInput
25 21 27 43 1 5 2Output
0 2
0 2 3 5
NoteIn the first test case, the final a=[1,2]. For i=1, the answer is always 0, irrespective of x. For i=2, we can select x=5.In the second test case, the final a=[3,1,0,5]. | 25 21 27 43 1 5 2 | 0 2 0 2 3 5 | 3 seconds | 256 megabytes | ['bitmasks', 'brute force', 'dfs and similar', '*2700'] |
E. 2..3...4.... Wonderful! Wonderful!time limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputStack has an array a of length n such that a_i = i for all i (1 \leq i \leq n). He will select a positive integer k (1 \leq k \leq \lfloor \frac{n-1}{2} \rfloor) and do the following operation on a any number (possibly 0) of times. Select a subsequence^\dagger s of length 2 \cdot k + 1 from a. Now, he will delete the first k elements of s from a. To keep things perfectly balanced (as all things should be), he will also delete the last k elements of s from a. Stack wonders how many arrays a can he end up with for each k (1 \leq k \leq \lfloor \frac{n-1}{2} \rfloor). As Stack is weak at counting problems, he needs your help. Since the number of arrays might be too large, please print it modulo 998\,244\,353.^\dagger A sequence x is a subsequence of a sequence y if x can be obtained from y by deleting several (possibly, zero or all) elements. For example, [1, 3], [1, 2, 3] and [2, 3] are subsequences of [1, 2, 3]. On the other hand, [3, 1] and [2, 1, 3] are not subsequences of [1, 2, 3].InputEach test contains multiple test cases. The first line contains a single integer t (1 \leq t \leq 2 \cdot 10^3) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (3 \leq n \leq 10^6) — the length of the array a.It is guaranteed that the sum of n over all test cases does not exceed 10^6.OutputFor each test, on a new line, print \lfloor \frac{n-1}{2} \rfloor space-separated integers — the i-th integer representing the number of arrays modulo 998\,244\,353 that Stack can get if he selects k=i.ExampleInput
434510Output
2
4
10 2
487 162 85 10
NoteIn the first test case, two a are possible for k=1: [1,2,3]; [2]. In the second test case, four a are possible for k=1: [1,2,3,4]; [1,3]; [2,3]; [2,4]. In the third test case, two a are possible for k=2: [1,2,3,4,5]; [3]. | 434510 | 2 4 10 2 487 162 85 10 | 3 seconds | 256 megabytes | ['combinatorics', 'dp', 'math', '*2400'] |
D2. Sum over all Substrings (Hard Version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the hard version of the problem. The only difference between the two versions is the constraint on t and n. You can make hacks only if both versions of the problem are solved.For a binary^\dagger pattern p and a binary string q, both of length m, q is called p-good if for every i (1 \leq i \leq m), there exist indices l and r such that: 1 \leq l \leq i \leq r \leq m, and p_i is a mode^\ddagger of the string q_l q_{l+1} \ldots q_{r}. For a pattern p, let f(p) be the minimum possible number of \mathtt{1}s in a p-good binary string (of the same length as the pattern).You are given a binary string s of size n. Find \sum_{i=1}^{n} \sum_{j=i}^{n} f(s_i s_{i+1} \ldots s_j). In other words, you need to sum the values of f over all \frac{n(n+1)}{2} substrings of s.^\dagger A binary pattern is a string that only consists of characters \mathtt{0} and \mathtt{1}.^\ddagger Character c is a mode of string t of length m if the number of occurrences of c in t is at least \lceil \frac{m}{2} \rceil. For example, \mathtt{0} is a mode of \mathtt{010}, \mathtt{1} is not a mode of \mathtt{010}, and both \mathtt{0} and \mathtt{1} are modes of \mathtt{011010}.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 10^5) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 10^6) — the length of the binary string s.The second line of each test case contains a binary string s of length n consisting of only characters \mathtt{0} and \mathtt{1}.It is guaranteed that the sum of n over all test cases does not exceed 10^6.OutputFor each test case, output the sum of values of f over all substrings of s.ExampleInput
4112105000002011110110000000111111Output
1
2
0
346
NoteIn the first test case, the only \mathtt{1}-good string is \mathtt{1}. Thus, f(\mathtt{1})=1.In the second test case, f(\mathtt{10})=1 because \mathtt{01} is \mathtt{10}-good, and \mathtt{00} is not \mathtt{10}-good. Thus, the answer is f(\mathtt{1})+f(\mathtt{10})+f(\mathtt{0}) = 1 + 1 + 0 = 2.In the third test case, f equals to 0 for all 1 \leq i \leq j \leq 5. Thus, the answer is 0. | 4112105000002011110110000000111111 | 1 2 0 346 | 2 seconds | 256 megabytes | ['bitmasks', 'divide and conquer', 'dp', 'dsu', 'greedy', 'implementation', 'strings', '*2100'] |
D1. Sum over all Substrings (Easy Version)time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is the easy version of the problem. The only difference between the two versions is the constraint on t and n. You can make hacks only if both versions of the problem are solved.For a binary^\dagger pattern p and a binary string q, both of length m, q is called p-good if for every i (1 \leq i \leq m), there exist indices l and r such that: 1 \leq l \leq i \leq r \leq m, and p_i is a mode^\ddagger of the string q_l q_{l+1} \ldots q_{r}. For a pattern p, let f(p) be the minimum possible number of \mathtt{1}s in a p-good binary string (of the same length as the pattern).You are given a binary string s of size n. Find \sum_{i=1}^{n} \sum_{j=i}^{n} f(s_i s_{i+1} \ldots s_j). In other words, you need to sum the values of f over all \frac{n(n+1)}{2} substrings of s.^\dagger A binary pattern is a string that only consists of characters \mathtt{0} and \mathtt{1}.^\ddagger Character c is a mode of string t of length m if the number of occurrences of c in t is at least \lceil \frac{m}{2} \rceil. For example, \mathtt{0} is a mode of \mathtt{010}, \mathtt{1} is not a mode of \mathtt{010}, and both \mathtt{0} and \mathtt{1} are modes of \mathtt{011010}.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 500) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (1 \le n \le 100) — the length of the binary string s.The second line of each test case contains a binary string s of length n consisting of only characters \mathtt{0} and \mathtt{1}.It is guaranteed that the sum of n^2 over all test cases does not exceed 10^4.OutputFor each test case, output the sum of values of f over all substrings of s.ExampleInput
4112105000002011110110000000111111Output
1
2
0
346
NoteIn the first test case, the only \mathtt{1}-good string is \mathtt{1}. Thus, f(\mathtt{1})=1.In the second test case, f(\mathtt{10})=1 because \mathtt{01} is \mathtt{10}-good, and \mathtt{00} is not \mathtt{10}-good. Thus, the answer is f(\mathtt{1})+f(\mathtt{10})+f(\mathtt{0}) = 1 + 1 + 0 = 2.In the third test case, f equals to 0 for all 1 \leq i \leq j \leq 5. Thus, the answer is 0. | 4112105000002011110110000000111111 | 1 2 0 346 | 2 seconds | 256 megabytes | ['brute force', 'dp', 'greedy', 'strings', '*1800'] |
C. Lexicographically Largesttime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputStack has an array a of length n. He also has an empty set S. Note that S is not a multiset.He will do the following three-step operation exactly n times: Select an index i such that 1 \leq i \leq |a|. Insert^\dagger a_i + i into S. Delete a_i from a. Note that the indices of all elements to the right of a_i will decrease by 1. Note that after n operations, a will be empty.Stack will now construct a new array b which is S sorted in decreasing order. Formally, b is an array of size |S| where b_i is the i-th largest element of S for all 1 \leq i \leq |S|.Find the lexicographically largest^\ddagger b that Stack can make.^\dagger A set can only contain unique elements. Inserting an element that is already present in a set will not change the elements of the set.^\ddagger An array p is lexicographically larger than a sequence q if and only if one of the following holds: q is a prefix of p, but p \ne q; or in the first position where p and q differ, the array p has a larger element than the corresponding element in q. Note that [3,1,4,1,5] is lexicographically larger than [3,1,3], [\,], and [3,1,4,1] but not [3,1,4,1,5,9], [3,1,4,1,5], and [4].InputEach test contains multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (1 \leq n \leq 3 \cdot 10^5) — 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_i \leq 10^9) — the elements of array a.The sum of n over all test cases does not exceed 3 \cdot 10^5.OutputFor each test case, output the lexicographically largest b.ExampleInput
322 151 100 1000 1000000 100000000036 4 8Output
3 2
1000000005 1000004 1003 102 2
11 7 6
NoteIn the first test case, select i=1 in the first operation, insert a_1 + 1 = 3 in S, and delete a_1 from a. After the first operation, a becomes a=[1]. In the second operation, we select i=1 again and insert a_1 + 1 = 2 in S. Thus S=\{2, 3\}, and b = [3, 2].Note that if you select i=2 in the first operation, and i=1 in the second operation, S=\{3\} as 3 will be inserted twice, resulting in b=[3].As [3,2] is lexicographically larger than [3], we should select i=1 in the first operation.In the second test case, in each operation, select the last element. | 322 151 100 1000 1000000 100000000036 4 8 | 3 2 1000000005 1000004 1003 102 2 11 7 6 | 2 seconds | 256 megabytes | ['binary search', 'constructive algorithms', 'data structures', 'greedy', 'sortings', '*1700'] |
B. Permutation Printingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a positive integer n.Find a permutation^\dagger p of length n such that there do not exist two distinct indices i and j (1 \leq i, j < n; i \neq j) such that p_i divides p_j and p_{i+1} divides p_{j+1}.Refer to the Notes section for some examples.Under the constraints of this problem, it can be proven that at least one p exists.^\dagger A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).InputEach test contains multiple test cases. The first line contains a single integer t (1 \leq t \leq 10^3) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (3 \leq n \leq 10^5) — the length of the permutation p.It is guaranteed that the sum of n over all test cases does not exceed 10^5.OutputFor each test case, output p_1, p_2, \ldots, p_n.If there are multiple solutions, you may output any one of them.ExampleInput
243Output
4 1 2 3
1 2 3
NoteIn the first test case, p=[4,1,2,3] is a valid permutation. However, the permutation p=[1,2,3,4] is not a valid permutation as we can choose i=1 and j=3. Then p_1=1 divides p_3=3 and p_2=2 divides p_4=4. Note that the permutation p=[3, 4, 2, 1] is also not a valid permutation as we can choose i=3 and j=2. Then p_3=2 divides p_2=4 and p_4=1 divides p_3=2.In the second test case, p=[1,2,3] is a valid permutation. In fact, all 6 permutations of length 3 are valid. | 243 | 4 1 2 3 1 2 3 | 1 second | 256 megabytes | ['brute force', 'constructive algorithms', 'math', '*1000'] |
A. Maximise The Scoretime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are 2n positive integers written on a whiteboard. Being bored, you decided to play a one-player game with the numbers on the whiteboard.You start with a score of 0. You will increase your score by performing the following move exactly n times: Choose two integers x and y that are written on the whiteboard. Add \min(x,y) to your score. Erase x and y from the whiteboard. Note that after performing the move n times, there will be no more integers written on the whiteboard.Find the maximum final score you can achieve if you optimally perform the n moves.InputEach test contains multiple test cases. The first line contains a single integer t (1 \leq t \leq 5000) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (1 \leq n \leq 50) — the number of integers written on the whiteboard is 2n.The second line of each test case contains 2n integers a_1,a_2,\ldots,a_{2n} (1 \leq a_i \leq 10^7) — the numbers written on the whiteboard.OutputFor each test case, output the maximum final score that you can achieve.ExampleInput
312 321 1 2 131 1 1 1 1 1Output
2
2
3
NoteIn the first test case, you can only make one move. You select x=2 and y=3, and your score will be \min(x,y)=2.In the second test case, the following is a sequence of moves that achieves a final score of 2: In the first move, select x=1 and y=1. Then, add \min(x,y)=1 to the score. After erasing x and y, the integers left on the whiteboard are 1 and 2. In the second move, select x=1 and y=2. Then, add \min(x,y)=1 to the score. After removing x and y, no more integers will be left on the whiteboard. It can be proved that it is not possible to get a score greater than 2.In the third test case, you will perform the move thrice, adding 1 to the score each time. | 312 321 1 2 131 1 1 1 1 1 | 2 2 3 | 1 second | 256 megabytes | ['greedy', 'sortings', '*800'] |
F. Sasha and the Wedding Binary Search Treetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputHaving overcome all the difficulties and hardships, Sasha finally decided to marry his girlfriend. To do this, he needs to give her an engagement ring. However, his girlfriend does not like such romantic gestures, but she does like binary search trees^{\dagger}. So Sasha decided to give her such a tree.After spending a lot of time on wedding websites for programmers, he found the perfect binary search tree with the root at vertex 1. In this tree, the value at vertex v is equal to val_v.But after some time, he forgot the values in some vertices. Trying to remember the found tree, Sasha wondered — how many binary search trees could he have found on the website, if it is known that the values in all vertices are integers in the segment [1, C]. Since this number can be very large, output it modulo 998\,244\,353.^{\dagger}A binary search tree is a rooted binary tree in which for any vertex x, the following property holds: the values of all vertices in the left subtree of vertex x (if it exists) are less than or equal to the value at vertex x, and the values of all vertices in the right subtree of vertex x (if it exists) are greater than or equal to the value at vertex x.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 10^5) — the number of test cases. The description of the test cases follows.The first line of each test case contains two integers n and C (2 \leq n \leq 5 \cdot 10^5, 1 \leq C \leq 10^9) — the number of vertices in the tree and the maximum allowed value at the vertex.The next n lines describe the vertices of the tree. The i-th line contains three integers L_i, R_i and val_i (-1 \le L_i, R_i \le n, -1 \le val_i \le C, L_i, R_i, val_i \ne 0) — the number of the left child, the number of the right child, and the value at the i-th vertex, respectively. If L_i = -1, then the i-th vertex has no left son. If R_i = -1, then the i-th vertex has no right son. If val_i = -1, then the value at the i-th vertex is unknown.It is guaranteed that at least one suitable binary search tree exists.It is guaranteed that the sum of n over all test cases does not exceed 5 \cdot 10^5.OutputFor each test case, output a single integer — the number of suitable binary search trees modulo 998\,244\,353.ExampleInput
35 52 3 -1-1 -1 24 -1 3-1 5 -1-1 -1 -13 692 3 47-1 -1 13-1 -1 693 32 3 -1-1 -1 -1-1 -1 -1Output
4
1
10
NoteIn the first test case, the binary search tree has the following form: Then the possible values at the vertices are: [2, 2, 3, 2, 2], [2, 2, 3, 2, 3], [2, 2, 3, 3, 3], and [3, 2, 3, 3, 3].In the second test case, the values at all vertices are known, so there is only one suitable binary search tree. | 35 52 3 -1-1 -1 24 -1 3-1 5 -1-1 -1 -13 692 3 47-1 -1 13-1 -1 693 32 3 -1-1 -1 -1-1 -1 -1 | 4 1 10 | 2 seconds | 256 megabytes | ['brute force', 'combinatorics', 'data structures', 'dfs and similar', 'math', 'trees', '*2300'] |
E. Sasha and the Happy Tree Cuttingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSasha was given a tree^{\dagger} with n vertices as a prize for winning yet another competition. However, upon returning home after celebrating his victory, he noticed that some parts of the tree were missing. Sasha remembers that he colored some of the edges of this tree. He is certain that for any of the k pairs of vertices (a_1, b_1), \ldots, (a_k, b_k), he colored at least one edge on the simple path^{\ddagger} between vertices a_i and b_i.Sasha does not remember how many edges he exactly colored, so he asks you to tell him the minimum number of edges he could have colored to satisfy the above condition.^{\dagger}A tree is an undirected connected graph without cycles.^{\ddagger}A simple path is a path that passes through each vertex at most once.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (2 \leq n \leq 10^5) — the number of vertices in the tree.The next (n - 1) lines describe the edges of the tree. The i-th line contains two integers u_i and v_i (1 \leq u_i, v_i \leq n, u_i \ne v_i) — the numbers of the vertices connected by the i-th edge.The next line contains a single integer k (1 \leq k \leq 20) — the number of pairs of vertices between which Sasha colored at least one edge on a simple path.The next k lines describe pairs. The j-th line contains two integers a_j and b_j (1 \leq a_j, b_j \leq n, a_j \neq b_j) — the vertices in the j-th pair.It is guaranteed that the sum of n over all test cases does not exceed 10^5. It is guaranteed that the sum of 2^k over all test cases does not exceed 2^{20}.OutputFor each test case, output a single integer — the minimum number of edges Sasha could have colored.ExampleInput
341 22 32 421 34 161 23 16 15 24 233 13 62 651 22 33 44 541 22 33 44 5Output
1
2
4
NoteIn the first test case, Sasha could have colored only one edge (1, 2). Then, there would be at least one colored edge on the simple path between vertices 1 and 3, and vertices 4 and 1. In the second test case, Sasha could have colored the edges (1, 6) and (1, 3). | 341 22 32 421 34 161 23 16 15 24 233 13 62 651 22 33 44 541 22 33 44 5 | 1 2 4 | 2 seconds | 256 megabytes | ['bitmasks', 'brute force', 'dfs and similar', 'dp', 'graphs', 'greedy', 'math', 'trees', '*2300'] |
D. Sasha and a Walk in the Citytime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSasha wants to take a walk with his girlfriend in the city. The city consists of n intersections, numbered from 1 to n. Some of them are connected by roads, and from any intersection, there is exactly one simple path^{\dagger} to any other intersection. In other words, the intersections and the roads between them form a tree.Some of the intersections are considered dangerous. Since it is unsafe to walk alone in the city, Sasha does not want to visit three or more dangerous intersections during the walk.Sasha calls a set of intersections good if the following condition is satisfied: If in the city only the intersections contained in this set are dangerous, then any simple path in the city contains no more than two dangerous intersections.However, Sasha does not know which intersections are dangerous, so he is interested in the number of different good sets of intersections in the city. Since this number can be very large, output it modulo 998\,244\,353.^{\dagger}A simple path is a path that passes through each intersection at most once.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (2 \le n \leq 3 \cdot 10^5) — the number of intersections in the city.The next (n - 1) lines describe the roads. The i-th line contains two integers u_i and v_i (1 \leq u_i, v_i \leq n, u_i \ne v_i) — the numbers of the intersections connected by the i-th road.It is guaranteed that these roads form a tree.It is guaranteed that the sum of n over all test cases does not exceed 3 \cdot 10^5.OutputFor each test case, output a single integer — the number of good sets of intersections modulo 998\,244\,353.ExampleInput
431 33 243 42 33 151 23 45 12 341 22 33 4Output
7
12
16
11
NoteIn the first test case, there are 2^3 = 8 sets of intersections. All of them are good, except for the set \{1, 2, 3\}, because if intersections 1, 2, and 3 are dangerous, then the simple path 1 - 2 - 3 contains 3 dangerous intersections. Thus, there are 7 good sets.In the second test case, there are 2^4 = 16 sets of intersections. Among them, the sets \{1, 2, 3, 4\}, \{1, 2, 3\}, \{1, 3, 4\}, \{2, 3, 4\} are not good. Thus, there are a total of 12 good sets. The city layout is shown below: | 431 33 243 42 33 151 23 45 12 341 22 33 4 | 7 12 16 11 | 2 seconds | 256 megabytes | ['combinatorics', 'dp', 'math', 'trees', '*1900'] |
C. Sasha and the Casinotime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputSasha decided to give his girlfriend the best handbag, but unfortunately for Sasha, it is very expensive. Therefore, Sasha wants to earn it. After looking at earning tips on the internet, he decided to go to the casino.Sasha knows that the casino operates under the following rules. If Sasha places a bet of y coins (where y is a positive integer), then in case of winning, he will receive y \cdot k coins (i.e., his number of coins will increase by y \cdot (k - 1)). And in case of losing, he will lose the entire bet amount (i.e., his number of coins will decrease by y).Note that the bet amount must always be a positive (> 0) integer and cannot exceed Sasha's current number of coins.Sasha also knows that there is a promotion at the casino: he cannot lose more than x times in a row.Initially, Sasha has a coins. He wonders whether he can place bets such that he is guaranteed to win any number of coins. In other words, is it true that for any integer n, Sasha can make bets so that for any outcome that does not contradict the rules described above, at some moment of time he will have at least n coins.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 1000) — the number of test cases. The description of the test cases follows.The single line of each test case contains three integers k, x and a (2 \leq k \leq 30, 1 \leq x \leq 100, 1 \leq a \leq 10^9) — the number of times the bet is increased in case of a win, the maximum number of consecutive losses, and the initial number of coins Sasha has.OutputFor each test case, output "YES" (without quotes) if Sasha can achieve it and "NO" (without quotes) otherwise.You can output "YES" and "NO" in any case (for example, the strings "yEs", "yes" and "Yes" will be recognized as a positive answer).ExampleInput
92 1 72 1 12 3 153 3 64 4 55 4 74 88 100000000025 69 23113 97 18806Output
YES
NO
YES
NO
NO
YES
NO
NO
NO
NoteIn the first test case, Sasha can proceed as follows: If Sasha places a bet for the first time or if he won the previous bet, then he places 1 coin. If Sasha lost the previous bet, then he places 2 coins.Note that Sasha cannot lose more than once in a row.It can be proven that with this strategy, Sasha can obtain as many coins as he wants.In the second test case, Sasha can only place 1 coin for the first time. But in case of a loss, he will not be able to place any more bets, so he will not be able to guarantee having as many coins as he wants. | 92 1 72 1 12 3 153 3 64 4 55 4 74 88 100000000025 69 23113 97 18806 | YES NO YES NO NO YES NO NO NO | 2 seconds | 256 megabytes | ['binary search', 'brute force', 'constructive algorithms', 'games', 'greedy', 'math', '*1400'] |
B. Sasha and the Drawingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputEven in kindergarten, Sasha liked a girl. Therefore, he wanted to give her a drawing and attract her attention.As a drawing, he decided to draw a square grid of size n \times n, in which some cells are colored. But coloring the cells is difficult, so he wants to color as few cells as possible. But at the same time, he wants at least k diagonals to have at least one colored cell. Note that the square grid of size n \times n has a total of 4n - 2 diagonals.Help little Sasha to make the girl fall in love with him and tell him the minimum number of cells he needs to color.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 1000) — the number of test cases. The description of the test cases follows.The only line of each test case contains two integers n and k (2 \leq n \leq 10^8, 1 \leq k \leq 4n - 2) — the size of the square grid and the minimum number of diagonals in which there should be at least one colored cell.OutputFor each test case, output a single integer — the minimum number of cells that need to be colored.ExampleInput
73 43 33 103 94 77 112 3Output
2
2
6
5
4
6
2
NoteIn the pictures below, the colored cells are marked in black, and all diagonals are marked in purple.In the first test case, you can color 2 cells so that 4 diagonals contain at least one colored cell: In the third test case, you can color 6 cells so that all 10 diagonals contain at least one colored cell: | 73 43 33 103 94 77 112 3 | 2 2 6 5 4 6 2 | 1 second | 256 megabytes | ['constructive algorithms', 'greedy', 'math', '*800'] |
A. Sasha and the Beautiful Arraytime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputSasha decided to give his girlfriend an array a_1, a_2, \ldots, a_n. He found out that his girlfriend evaluates the beauty of the array as the sum of the values (a_i - a_{i - 1}) for all integers i from 2 to n.Help Sasha and tell him the maximum beauty of the array a that he can obtain, if he can rearrange its elements in any way.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 500) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single integer n (2 \leq n \leq 100) — the length of the array a.The second line of each test case contains n integers a_1, a_2, \ldots, a_n (1 \leq a_i \leq 10^9) — the elements of the array a.OutputFor each test case, output a single integer — the maximum beauty of the array a that can be obtained.ExampleInput
532 1 3369 69 695100 54 80 43 9043 4 3 322 1Output
2
0
57
1
1
NoteIn the first test case, the elements of the array a can be rearranged to make a = [1, 2, 3]. Then its beauty will be equal to (a_2 - a_1) + (a_3 - a_2) = (2 - 1) + (3 - 2) = 2.In the second test case, there is no need to rearrange the elements of the array a. Then its beauty will be equal to 0. | 532 1 3369 69 695100 54 80 43 9043 4 3 322 1 | 2 0 57 1 1 | 1 second | 256 megabytes | ['constructive algorithms', 'greedy', 'math', 'sortings', '*800'] |
F. Digital Patternstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAnya is engaged in needlework. Today she decided to knit a scarf from semi-transparent threads. Each thread is characterized by a single integer — the transparency coefficient.The scarf is made according to the following scheme: horizontal threads with transparency coefficients a_1, a_2, \ldots, a_n and vertical threads with transparency coefficients b_1, b_2, \ldots, b_m are selected. Then they are interwoven as shown in the picture below, forming a piece of fabric of size n \times m, consisting of exactly nm nodes: Example of a piece of fabric for n = m = 4. After the interweaving tightens and there are no gaps between the threads, each node formed by a horizontal thread with number i and a vertical thread with number j will turn into a cell, which we will denote as (i, j). Cell (i, j) will have a transparency coefficient of a_i + b_j.The interestingness of the resulting scarf will be the number of its sub-squares^{\dagger} in which there are no pairs of neighboring^{\dagger \dagger} cells with the same transparency coefficients.Anya has not yet decided which threads to use for the scarf, so you will also be given q queries to increase/decrease the coefficients for the threads on some ranges. After each query of which you need to output the interestingness of the resulting scarf.^{\dagger}A sub-square of a piece of fabric is defined as the set of all its cells (i, j), such that x_0 \le i \le x_0 + d and y_0 \le j \le y_0 + d for some integers x_0, y_0, and d (1 \le x_0 \le n - d, 1 \le y_0 \le m - d, d \ge 0).^{\dagger \dagger}. Cells (i_1, j_1) and (i_2, j_2) are neighboring if and only if |i_1 - i_2| + |j_1 - j_2| = 1.InputThe first line contains three integers n, m, and q (1 \le n, m \le 3 \cdot 10^5, 0 \le q \le 3 \cdot 10^5) — the number of horizontal threads, the number of vertical threads, and the number of change requests.The second line contains n integers a_1, a_2, \ldots, a_n (-10^9 \le a_i \le 10^9) — the transparency coefficients for the horizontal threads, with the threads numbered from top to bottom.The third line contains m integers b_1, b_2, \ldots, b_m (-10^9 \le b_i \le 10^9) — the transparency coefficients for the vertical threads, with the threads numbered from left to right.The next q lines specify the change requests. Each request is described by a quadruple of integers t, l, r, and x (1 \le t \le 2, l \le r, -10^9 \le x \le 10^9). Depending on the parameter t in the request, the following actions are required: t=1. The transparency coefficients for the horizontal threads in the range [l, r] are increased by x (in other words, for all integers l \le i \le r, the value of a_i is increased by x); t=2. The transparency coefficients for the vertical threads in the range [l, r] are increased by x (in other words, for all integers l \le i \le r, the value of b_i is increased by x). OutputOutput (q+1) lines. In the (i + 1)-th line (0 \le i \le q), output a single integer — the interestingness of the scarf after applying the first i requests.ExamplesInput
4 4 01 1 2 31 2 2 3Output
20
Input
3 3 21 1 12 2 81 2 3 12 2 3 -6Output
9
10
11
Input
3 2 2-1000000000 0 1000000000-1000000000 10000000001 1 1 10000000002 2 2 -1000000000Output
8
7
7
NoteIn the first example, the transparency coefficients of the cells in the resulting plate are as follows: 2334233434454556 Then there are the following sub-squares that do not contain two neighboring cells with the same transparency coefficient: Each of the 16 cells separately; A sub-square with the upper left corner at cell (3, 1) and the lower right corner at cell (4, 2); A sub-square with the upper left corner at cell (2, 3) and the lower right corner at cell (3, 4); A sub-square with the upper left corner at cell (2, 1) and the lower right corner at cell (3, 2); A sub-square with the upper left corner at cell (3, 3) and the lower right corner at cell (4, 4).In the second example, after the first query, the transparency coefficients of the horizontal threads are [1, 2, 2]. After the second query, the transparency coefficients of the vertical threads are [2, -4, 2]. | 4 4 01 1 2 31 2 2 3 | 20 | 2 seconds | 256 megabytes | ['combinatorics', 'data structures', 'implementation', 'math', '*2900'] |
E. Modular Sequencetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers x and y. A sequence a of length n is called modular if a_1=x, and for all 1 < i \le n the value of a_{i} is either a_{i-1} + y or a_{i-1} \bmod y. Here x \bmod y denotes the remainder from dividing x by y.Determine if there exists a modular sequence of length n with the sum of its elements equal to S, and if it exists, find any such sequence.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 2 \cdot 10^4). The description of the test cases follows.The first and only line of each test case contains four integers n, x, y, and s (1 \le n \le 2 \cdot 10^5, 0 \le x \le 2 \cdot 10^5, 1 \le y \le 2 \cdot 10^5, 0 \le s \le 2 \cdot 10^5) — the length of the sequence, the parameters x and y, and the required sum of the sequence elements.The sum of n over all test cases does not exceed 2 \cdot 10^5, and also the sum of s over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, if the desired sequence exists, output "Yes" on the first line (without quotes). Then, on the second line, output n integers a_1, a_2, \ldots, a_n separated by a space — the elements of the sequence a. If there are multiple suitable sequences, output any of them.If the sequence does not exist, output "No" on a single line.You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.ExampleInput
35 8 3 283 5 3 69 1 5 79Output
YES
8 11 2 2 5
NO
NO
NoteIn the first example, the sequence [8, 11, 2, 5, 2] satisfies the conditions. Thus, a_1 = 8 = x, a_2 = 11 = a_1 + 3, a_3 = 2 = a_2 \bmod 3, a_4 = 5 = a_3 + 3, a_5 = 2 = a_4 \bmod 3.In the second example, the first element of the sequence should be equal to 5, so the sequence [2, 2, 2] is not suitable. | 35 8 3 283 5 3 69 1 5 79 | YES 8 11 2 2 5 NO NO | 2 seconds | 256 megabytes | ['brute force', 'constructive algorithms', 'dp', 'graphs', 'greedy', 'math', 'number theory', '*2300'] |
D. Lonely Mountain Dungeonstime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputOnce, the people, elves, dwarves, and other inhabitants of Middle-earth gathered to reclaim the treasures stolen from them by Smaug. In the name of this great goal, they rallied around the powerful elf Timothy and began to plan the overthrow of the ruler of the Lonely Mountain.The army of Middle-earth inhabitants will consist of several squads. It is known that each pair of creatures of the same race, which are in different squads, adds b units to the total strength of the army. But since it will be difficult for Timothy to lead an army consisting of a large number of squads, the total strength of an army consisting of k squads is reduced by (k - 1) \cdot x units. Note that the army always consists of at least one squad.It is known that there are n races in Middle-earth, and the number of creatures of the i-th race is equal to c_i. Help the inhabitants of Middle-earth determine the maximum strength of the army they can assemble.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \le t \le 2 \cdot 10^4) — the number of test cases. The description of the test cases follows.The first line of each test case contains three integers n, b, and x (1 \le n \le 2 \cdot 10^5, 1 \le b \le 10^6, 0 \le x \le 10^9) — the number of races and the constants b and x described above.The second line of each test case contains n integers c_1, c_2, \ldots, c_n (1 \le c_i \le 2 \cdot 10^5) — the number of creatures of each of the n races.It is guaranteed that the sum of the values c_1 + c_2 + \ldots + c_n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the maximum strength of the army that the inhabitants of Middle-earth can assemble.ExampleInput
53 1 01 2 33 5 102 5 34 3 33 2 1 24 1 04 1 4 24 1 104 1 4 2Output
4
40
9
13
0
NoteIn the first test case, the inhabitants of Middle-earth can form 3 squads. Since x = 0, the army's strength will not decrease due to the number of squads. The inhabitants can be distributed among the squads as follows: The single representative of the first species can be sent to the first squad. The first representative of the second species can be sent to the first squad, the second representative of the second species can be sent to the second squad. Then the total strength of the army will increase by b = 1. The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by 3 \cdot b = 3, as they form three pairs in different squads.Thus, the total strength of the army is 4.In the second test case, the inhabitants of Middle-earth can form 3 squads. Since x = 10, the army's strength will decrease by 20. The inhabitants can be distributed among the squads as follows: The first representative of the first species can be sent to the first squad, the second representative of the first species can be sent to the second squad. Then the total strength of the army will increase by b = 5. The first and second representatives of the second species can be sent to the first squad, the third and fourth representatives of the second species can be sent to the second squad, the fifth representative of the second species can be sent to the third squad. Then the total strength of the army will increase by 8 \cdot b = 40. The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by 3 \cdot b = 15, as they form three pairs in different squads.Thus, the total strength of the army is 5 + 40 + 15 - 20 = 40. | 53 1 01 2 33 5 102 5 34 3 33 2 1 24 1 04 1 4 24 1 104 1 4 2 | 4 40 9 13 0 | 1 second | 256 megabytes | ['brute force', 'data structures', 'greedy', 'math', 'ternary search', '*1900'] |
C. Physical Education Lessontime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputIn a well-known school, a physical education lesson took place. As usual, everyone was lined up and asked to settle in "the first–k-th" position.As is known, settling in "the first–k-th" position occurs as follows: the first k people have numbers 1, 2, 3, \ldots, k, the next k - 2 people have numbers k - 1, k - 2, \ldots, 2, the next k people have numbers 1, 2, 3, \ldots, k, and so on. Thus, the settling repeats every 2k - 2 positions. Examples of settling are given in the "Note" section.The boy Vasya constantly forgets everything. For example, he forgot the number k described above. But he remembers the position he occupied in the line, as well as the number he received during the settling. Help Vasya understand how many natural numbers k fit under the given constraints.Note that the settling exists if and only if k > 1. In particular, this means that the settling does not exist for k = 1.InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 100) — the number of test cases. This is followed by the description of the test cases.The only line of each test case contains two integers n and x (1 \le x < n \le 10^9) — Vasya's position in the line and the number Vasya received during the settling.OutputFor each test case, output a single integer — the number of different k that fit under the given constraints.It can be proven that under the given constraints, the answer is finite.ExampleInput
510 23 176 4100 991000000000 500000000Output
4
1
9
0
1
NoteIn the first test case, k equals 2, 3, 5, 6 are suitable.An example of settling for these k: k / №1234567891021212121212312321232125123454321261234565432 In the second test case, k = 2 is suitable. | 510 23 176 4100 991000000000 500000000 | 4 1 9 0 1 | 1 second | 256 megabytes | ['brute force', 'math', 'number theory', '*1600'] |
B. Equalizetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVasya has two hobbies — adding permutations^{\dagger} to arrays and finding the most frequently occurring element. Recently, he found an array a and decided to find out the maximum number of elements equal to the same number in the array a that he can obtain after adding some permutation to the array a.More formally, Vasya must choose exactly one permutation p_1, p_2, p_3, \ldots, p_n of length n, and then change the elements of the array a according to the rule a_i := a_i + p_i. After that, Vasya counts how many times each number occurs in the array a and takes the maximum of these values. You need to determine the maximum value he can obtain.^{\dagger}A permutation of length n is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array), and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).InputEach test consists of multiple test cases. The first line contains a single integer t (1 \leq t \leq 2 \cdot 10^4) — the number of test cases. Then follows the description of the test cases.The first line of each test case contains a single integer n (1 \le n \le 2 \cdot 10^5) — 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 10^9) — the elements of the array a.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a single number — the maximum number of elements equal to the same number after the operation of adding a permutation.ExampleInput
721 247 1 4 13103 102 10451 101 1 100 151 10 100 1000 123 131000000000 999999997 999999999Output
2
2
3
2
1
1
2
NoteIn the first test case, it is optimal to choose p = [2, 1]. Then after applying the operation, the array a will be [3, 3], in which the number 3 occurs twice, so the answer is 2.In the second test case, one of the optimal options is p = [2, 3, 1, 4]. After applying the operation, the array a will be [9, 4, 5, 5]. Since the number 5 occurs twice, the answer is 2. | 721 247 1 4 13103 102 10451 101 1 100 151 10 100 1000 123 131000000000 999999997 999999999 | 2 2 3 2 1 1 2 | 1 second | 256 megabytes | ['binary search', 'greedy', 'sortings', 'two pointers', '*1200'] |
A. Rectangle Cuttingtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputBob has a rectangle of size a \times b. He tries to cut this rectangle into two rectangles with integer sides by making a cut parallel to one of the sides of the original rectangle. Then Bob tries to form some other rectangle from the two resulting rectangles, and he can rotate and move these two rectangles as he wishes.Note that if two rectangles differ only by a 90^{\circ} rotation, they are considered the same. For example, the rectangles 6 \times 4 and 4 \times 6 are considered the same.Thus, from the 2 \times 6 rectangle, another rectangle can be formed, because it can be cut into two 2 \times 3 rectangles, and then these two rectangles can be used to form the 4 \times 3 rectangle, which is different from the 2 \times 6 rectangle. However, from the 2 \times 1 rectangle, another rectangle cannot be formed, because it can only be cut into two rectangles of 1 \times 1, and from these, only the 1 \times 2 and 2 \times 1 rectangles can be formed, which are considered the same. Help Bob determine if he can obtain some other rectangle, or if he is just wasting his time.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. This is followed by the description of the test cases.The single line of each test case contains two integers a and b (1 \le a, b \le 10^9) — the size of Bob's rectangle.OutputFor each test case, output "Yes" if Bob can obtain another rectangle from the a \times b rectangle. Otherwise, output "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 answers.ExampleInput
71 12 12 63 22 22 46 3Output
No
No
Yes
Yes
Yes
Yes
No
NoteIn the first test case, the 1 \times 1 rectangle cannot be cut into two rectangles, so another rectangle cannot be obtained from it.In the fourth test case, the 3 \times 2 rectangle can be cut into two 3 \times 1 rectangles, and from these, the 1 \times 6 rectangle can be formed.In the fifth test case, the 2 \times 2 rectangle can be cut into two 1 \times 2 rectangles, and from these, the 1 \times 4 rectangle can be formed. | 71 12 12 63 22 22 46 3 | No No Yes Yes Yes Yes No | 1 second | 256 megabytes | ['geometry', 'math', '*800'] |
G. Paint Chargestime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputA horizontal grid strip of n cells is given. In the i-th cell, there is a paint charge of size a_i. This charge can be: either used to the left — then all cells to the left at a distance less than a_i (from \max(i - a_i + 1, 1) to i inclusive) will be painted, or used to the right — then all cells to the right at a distance less than a_i (from i to \min(i + a_i - 1, n) inclusive) will be painted, or not used at all. Note that a charge can be used no more than once (that is, it cannot be used simultaneously to the left and to the right). It is allowed for a cell to be painted more than once.What is the minimum number of times a charge needs to be used to paint all the cells of the strip?InputThe first line of the input contains an integer t (1 \le t \le 100) — the number of test cases in the test. This is followed by descriptions of t test cases.Each test case is specified by two lines. The first one contains an integer n (1 \le n \le 100) — the number of cells in the strip. The second line contains n positive integers a_1, a_2, \dots, a_n (1 \le a_i \le n), where a_i is the size of the paint charge in the i-th cell from the left of the strip.It is guaranteed that the sum of the values of n in the test does not exceed 1000.OutputFor each test case, output the minimum number of times the charges need to be used to paint all the cells of the strip.ExampleInput
131121 122 121 222 231 1 133 1 231 3 171 2 3 1 2 4 272 1 1 1 2 3 1102 2 5 1 6 1 8 2 8 262 1 2 1 1 261 1 4 1 3 2Output
1
2
1
1
1
3
1
2
3
4
2
3
3
NoteIn the third test case of the example, it is sufficient to use the charge from the 1-st cell to the right, then it will cover both cells 1 and 2.In the ninth test case of the example, you need to: use the charge from the 3-rd cell to the left, covering cells from the 1-st to the 3-rd; use the charge from the 5-th cell to the left, covering cells from the 4-th to the 5-th; use the charge from the 7-th cell to the left, covering cells from the 6-th to the 7-th. In the eleventh test case of the example, you need to: use the charge from the 5-th cell to the right, covering cells from the 5-th to the 10-th; use the charge from the 7-th cell to the left, covering cells from the 1-st to the 7-th. | 131121 122 121 222 231 1 133 1 231 3 171 2 3 1 2 4 272 1 1 1 2 3 1102 2 5 1 6 1 8 2 8 262 1 2 1 1 261 1 4 1 3 2 | 1 2 1 1 1 3 1 2 3 4 2 3 3 | 4 seconds | 256 megabytes | ['data structures', 'dp', 'greedy', 'math', '*2300'] |
F. Microcycletime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an undirected weighted graph with n vertices and m edges. There is at most one edge between each pair of vertices in the graph, and the graph does not contain loops (edges from a vertex to itself). The graph is not necessarily connected.A cycle in the graph is called simple if it doesn't pass through the same vertex twice and doesn't contain the same edge twice.Find any simple cycle in this graph in which the weight of the lightest edge is minimal.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then follow the descriptions of the test cases.The first line of each test case contains two integers n and m (3 \le n \le m \le \min(\frac{n\cdot(n - 1)}{2}, 2 \cdot 10^5)) — the size of the graph and the number of edges.The next m lines of the test case contain three integers u, v, and w (1 \le u, v \le n, u \ne v, 1 \le w \le 10^6) — vertices u and v are connected by an edge of weight w.It is guaranteed that there is at most one edge between each pair of vertices. Note that under the given constraints, there is always at least one simple cycle in the graph.It is guaranteed that the sum of the values of m for all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a pair of numbers b and k, where: b — the minimum weight of the edge in the found cycle, k — the number of vertices in the found cycle. On the next line, output k numbers from 1 to n — the vertices of the cycle in traversal order.Note that the answer always exists, as under the given constraints, there is always at least one simple cycle in the graph.ExampleInput
56 61 2 12 3 13 1 14 5 15 6 16 4 16 61 2 102 3 83 1 54 5 1005 6 406 4 36 151 2 45 2 86 1 76 3 106 5 13 2 84 3 45 3 62 6 65 4 54 1 36 4 54 2 13 1 71 5 54 62 3 21 3 101 4 13 4 72 4 51 2 24 52 1 103 1 34 2 61 4 72 3 3Output
1 3
1 2 3
3 3
6 4 5
1 5
4 2 1 6 3
1 4
1 4 3 2
3 3
2 3 1
| 56 61 2 12 3 13 1 14 5 15 6 16 4 16 61 2 102 3 83 1 54 5 1005 6 406 4 36 151 2 45 2 86 1 76 3 106 5 13 2 84 3 45 3 62 6 65 4 54 1 36 4 54 2 13 1 71 5 54 62 3 21 3 101 4 13 4 72 4 51 2 24 52 1 103 1 34 2 61 4 72 3 3 | 1 3 1 2 3 3 3 6 4 5 1 5 4 2 1 6 3 1 4 1 4 3 2 3 3 2 3 1 | 4 seconds | 256 megabytes | ['data structures', 'dfs and similar', 'dsu', 'graphs', 'greedy', 'implementation', 'sortings', 'trees', '*1900'] |
E. Klever Permutationtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers n and k (k \le n), where k is even.A permutation of length n is an array consisting of n distinct integers from 1 to n in any order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (as 2 appears twice in the array) and [0,1,2] is also not a permutation (as n=3, but 3 is not present in the array).Your task is to construct a k-level permutation of length n.A permutation is called k-level if, among all the sums of continuous segments of length k (of which there are exactly n - k + 1), any two sums differ by no more than 1.More formally, to determine if the permutation p is k-level, first construct an array s of length n - k + 1, where s_i=\sum_{j=i}^{i+k-1} p_j, i.e., the i-th element is equal to the sum of p_i, p_{i+1}, \dots, p_{i+k-1}.A permutation is called k-level if \max(s) - \min(s) \le 1.Find any k-level permutation of length n.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases. This is followed by the description of the test cases.The first and only line of each test case contains two integers n and k (2 \le k \le n \le 2 \cdot 10^5, k is even), where n is the length of the desired permutation.It is guaranteed that the sum of n for all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output any k-level permutation of length n.It is guaranteed that such a permutation always exists given the constraints.ExampleInput
52 23 210 413 47 4Output
2 1
1 3 2
1 8 4 10 2 7 5 9 3 6
4 10 1 13 5 9 2 12 6 8 3 11 7
1 6 3 7 2 5 4
NoteIn the second test case of the example: p_1 + p_2 = 3 + 1 = 4; p_2 + p_3 = 1 + 2 = 3. The maximum among the sums is 4, and the minimum is 3. | 52 23 210 413 47 4 | 2 1 1 3 2 1 8 4 10 2 7 5 9 3 6 4 10 1 13 5 9 2 12 6 8 3 11 7 1 6 3 7 2 5 4 | 2 seconds | 256 megabytes | ['constructive algorithms', 'math', 'two pointers', '*1400'] |
D. Find the Different Ones!time limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a of n integers, and q queries.Each query is represented by two integers l and r (1 \le l \le r \le n). Your task is to find, for each query, two indices i and j (or determine that they do not exist) such that: l \le i \le r; l \le j \le r; a_i \ne a_j. In other words, for each query, you need to find a pair of different elements among a_l, a_{l+1}, \dots, a_r, or report that such a pair does not exist.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases. The descriptions of the test cases follow.The first line of each test case contains a single integer n (2 \le n \le 2 \cdot 10^5) — the length of the array a.The second line of each test case contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^6) — the elements of the array a.The third line of each test case contains a single integer q (1 \le q \le 2 \cdot 10^5) — the number of queries.The next q lines contain two integers each, l and r (1 \le l < r \le n) — the boundaries of the query.It is guaranteed that the sum of the values of n across all test cases does not exceed 2 \cdot 10^5. Similarly, it is guaranteed that the sum of the values of q across all test cases does not exceed 2 \cdot 10^5.OutputFor each query, output two integers separated by space: i and j (l \le i, j \le r), for which a_i \ne a_j. If such a pair does not exist, output i=-1 and j=-1.You may separate the outputs for the test cases with empty lines. This is not a mandatory requirement.ExampleInput
551 1 2 1 131 51 21 3630 20 20 10 10 2051 22 32 42 63 545 2 3 441 21 42 32 451 4 3 2 451 52 43 43 54 552 3 1 4 271 21 41 52 42 53 54 5Output
2 3
-1 -1
1 3
2 1
-1 -1
4 2
4 6
5 3
1 2
1 2
2 3
3 2
1 3
2 4
3 4
5 3
5 4
1 2
4 2
1 3
2 3
3 2
5 4
5 4
| 551 1 2 1 131 51 21 3630 20 20 10 10 2051 22 32 42 63 545 2 3 441 21 42 32 451 4 3 2 451 52 43 43 54 552 3 1 4 271 21 41 52 42 53 54 5 | 2 3 -1 -1 1 3 2 1 -1 -1 4 2 4 6 5 3 1 2 1 2 2 3 3 2 1 3 2 4 3 4 5 3 5 4 1 2 4 2 1 3 2 3 3 2 5 4 5 4 | 5 seconds | 256 megabytes | ['binary search', 'brute force', 'data structures', 'dp', 'dsu', 'greedy', 'two pointers', '*1300'] |
C. Choose the Different Ones!time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputGiven an array a of n integers, an array b of m integers, and an even number k.Your task is to determine whether it is possible to choose exactly \frac{k}{2} elements from both arrays in such a way that among the chosen elements, every integer from 1 to k is included.For example: If a=[2, 3, 8, 5, 6, 5], b=[1, 3, 4, 10, 5], k=6, then it is possible to choose elements with values 2, 3, 6 from array a and elements with values 1, 4, 5 from array b. In this case, all numbers from 1 to k=6 will be included among the chosen elements. If a=[2, 3, 4, 5, 6, 5], b=[1, 3, 8, 10, 3], k=6, then it is not possible to choose elements in the required way. Note that you are not required to find a way to choose the elements — your program should only check whether it is possible to choose the elements in the required way.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases. The descriptions of the test cases follow.The first line of each test case contains three integers n, m, and k (1 \le n, m \le 2\cdot10^5, 2 \le k \le 2 \cdot \min(n, m), k is even) — the length of array a, the length of array b, and the number of elements to be chosen, respectively.The second line of each test case contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^6) — the elements of array a.The third line of each test case contains m integers b_1, b_2, \dots, b_m (1 \le b_j \le 10^6) — the elements of array b.It is guaranteed that the sum of values n and m over all test cases in a test does not exceed 4 \cdot 10^5.OutputOutput t lines, each of which is the answer to the corresponding test case. As the answer, output "YES" if it is possible to choose \frac{k}{2} numbers from each array in such a way that among the chosen elements, every integer from 1 to k is included. Otherwise, output "NO".You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer.ExampleInput
66 5 62 3 8 5 6 51 3 4 10 56 5 62 3 4 5 6 51 3 8 10 33 3 41 3 52 4 62 5 41 47 3 4 4 21 4 226 4 4 21 5 232 2 1 4 3Output
YES
NO
YES
YES
NO
NO
NoteIn the first test case of the example, it is possible to choose elements equal to 2, 3, and 6 from array a and elements equal to 1, 4, and 5 from array b. Thus, all numbers from 1 to k=6 are included among the chosen elements.In the second test case of the example, it can be shown that it is not possible to choose exactly three elements from each array in the required way.In the third test case of the example, it is possible to choose elements equal to 1 and 3 from array a and elements equal to 2 and 4 from array b. Thus, all numbers from 1 to k=4 are included among the chosen elements. | 66 5 62 3 8 5 6 51 3 4 10 56 5 62 3 4 5 6 51 3 8 10 33 3 41 3 52 4 62 5 41 47 3 4 4 21 4 226 4 4 21 5 232 2 1 4 3 | YES NO YES YES NO NO | 2 seconds | 256 megabytes | ['brute force', 'greedy', 'math', '*1000'] |
B. Following the Stringtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPolycarp lost the string s of length n consisting of lowercase Latin letters, but he still has its trace.The trace of the string s is an array a of n integers, where a_i is the number of such indices j (j < i) that s_i=s_j. For example, the trace of the string abracadabra is the array [0, 0, 0, 1, 0, 2, 0, 3, 1, 1, 4].Given a trace of a string, find any string s from which it could have been obtained. The string s should consist only of lowercase Latin letters a-z.InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases. Then the descriptions of the test cases follow.The first line of each test case contains a single integer n (1 \le n \le 2 \cdot 10^5) — the length of the lost string.The second line of each test case contains n integers a_1, a_2, \dots, a_n (0 \le a_i < n) — the trace of the string. It is guaranteed that for the given trace, there exists a suitable string s.It is guaranteed that the sum of n over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, output a string s that corresponds to the given trace. If there are multiple such strings s, then output any of them.The string s should consist of lowercase Latin letters a-z.It is guaranteed that for each test case, a valid answer exists.ExampleInput
5110 0 0 1 0 2 0 3 1 1 4100 0 0 0 0 1 0 1 1 01080 1 2 3 4 5 6 780 0 0 0 0 0 0 0Output
abracadabra
codeforces
a
aaaaaaaa
dijkstra
| 5110 0 0 1 0 2 0 3 1 1 4100 0 0 0 0 1 0 1 1 01080 1 2 3 4 5 6 780 0 0 0 0 0 0 0 | abracadabra codeforces a aaaaaaaa dijkstra | 2 seconds | 256 megabytes | ['constructive algorithms', 'greedy', 'strings', '*900'] |
A. Make it Whitetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a horizontal strip of n cells. Each cell is either white or black.You can choose a continuous segment of cells once and paint them all white. After this action, all the black cells in this segment will become white, and the white ones will remain white.What is the minimum length of the segment that needs to be painted white in order for all n cells to become white?InputThe first line of the input contains a single integer t (1 \le t \le 10^4) — the number of test cases. The descriptions of the test cases follow.The first line of each test case contains a single integer n (1 \le n \le 10) — the length of the strip.The second line of each test case contains a string s, consisting of n characters, each of which is either 'W' or 'B'. The symbol 'W' denotes a white cell, and 'B' — a black one. It is guaranteed that at least one cell of the given strip is black.OutputFor each test case, output a single number — the minimum length of a continuous segment of cells that needs to be painted white in order for the entire strip to become white.ExampleInput
86WBBWBW1B2WB3BBW4BWWB6BWBWWB6WWBBWB9WBWBWWWBWOutput
4
1
1
2
4
6
4
7
NoteIn the first test case of the example for the strip "WBBWBW", the minimum length of the segment to be repainted white is 4. It is necessary to repaint to white the segment from the 2-nd to the 5-th cell (the cells are numbered from 1 from left to right). | 86WBBWBW1B2WB3BBW4BWWB6BWBWWB6WWBBWB9WBWBWWWBW | 4 1 1 2 4 6 4 7 | 2 seconds | 256 megabytes | ['greedy', 'strings', '*800'] |
G. Vlad and Trouble at MITtime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVladislav has a son who really wanted to go to MIT. The college dormitory at MIT (Moldova Institute of Technology) can be represented as a tree with n vertices, each vertex being a room with exactly one student. A tree is a connected undirected graph with n vertices and n-1 edges.Tonight, there are three types of students: students who want to party and play music (marked with \texttt{P}), students who wish to sleep and enjoy silence (marked with \texttt{S}), and students who don't care (marked with \texttt{C}). Initially, all the edges are thin walls which allow music to pass through, so when a partying student puts music on, it will be heard in every room. However, we can place some thick walls on any edges — thick walls don't allow music to pass through them.The university wants to install some thick walls so that every partying student can play music, and no sleepy student can hear it.Because the university lost a lot of money in a naming rights lawsuit, they ask you to find the minimum number of thick walls they will need to use.InputThe first line contains a single integer t (1 \leq t \leq 1000) — the number of test cases.The first line of each test case contains an integer n (2 \leq n \leq 10^5) — the number of vertices in the tree.The second line of each test case contains n-1 integers a_2, \dots , a_n (1 \leq a_i < i) — it means there is an edge between i and a_i in the tree.The third line of each test case contains a string s of length n consisting of characters \texttt{P}, \texttt{S}, and \texttt{C}, denoting that student i is of type s_i.The sum of n over all test cases does not exceed 10^5.OutputFor each test case, output a single integer — the minimum number of thick walls needed.ExampleInput
331 1CSP41 2 2PCSS41 2 2PPSSOutput
1
1
2
NoteIn the first case, we can install one thick wall between rooms 1 and 2, as shown below. We cannot install 0 walls, since then the music from room 3 will reach room 2 where a student wants to sleep, so the answer is 1. There are other valid solutions. | 331 1CSP41 2 2PCSS41 2 2PPSS | 1 1 2 | 1 second | 256 megabytes | ['dfs and similar', 'dp', 'flows', 'graphs', 'greedy', 'implementation', 'trees', '*1900'] |
F. Vlad and Avoiding Xtime limit per test4 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVladislav has a grid of size 7 \times 7, where each cell is colored black or white. In one operation, he can choose any cell and change its color (black \leftrightarrow white).Find the minimum number of operations required to ensure that there are no black cells with four diagonal neighbors also being black. The left image shows that initially there are two black cells violating the condition. By flipping one cell, the grid will work. InputThe first line of input contains a single integer t (1 \leq t \leq 200) — the number of test cases. Then follows the description of the test cases.Each test case consists of 7 lines, each containing 7 characters. Each of these characters is either \texttt{W} or \texttt{B}, denoting a white or black cell, respectively.OutputFor each test case, output a single integer — the minimum number of operations required to ensure that there are no black cells with all four diagonal neighbors also being black.ExampleInput
4WWWWWWWWWWWBBBWWWWWBWWWBBBBBWWWBWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBBBBBWWBBBBBWWBBBBBWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWBBBBBWBBBBBBBBBBBBBBWWWWWWWBBBBBBBBBBBBBBBBBBBBBOutput
1
2
0
5
NoteThe first test case is illustrated in the statement.The second test case is illustrated below: In the third test case, the grid already satisfies the condition. | 4WWWWWWWWWWWBBBWWWWWBWWWBBBBBWWWBWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWBBBBBWWBBBBBWWBBBBBWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWBBBBBWBBBBBBBBBBBBBBWWWWWWWBBBBBBBBBBBBBBBBBBBBB | 1 2 0 5 | 4 seconds | 256 megabytes | ['bitmasks', 'brute force', 'dfs and similar', 'dp', 'implementation', '*2200'] |
E. Vlad and an Odd Orderingtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVladislav has n cards numbered 1, 2, \dots, n. He wants to lay them down in a row as follows: First, he lays down all the odd-numbered cards from smallest to largest. Next, he lays down all cards that are twice an odd number from smallest to largest (i.e. 2 multiplied by an odd number). Next, he lays down all cards that are 3 times an odd number from smallest to largest (i.e. 3 multiplied by an odd number). Next, he lays down all cards that are 4 times an odd number from smallest to largest (i.e. 4 multiplied by an odd number). And so on, until all cards are laid down. What is the k-th card he lays down in this process? Once Vladislav puts a card down, he cannot use that card again.InputThe first line contains an integer t (1 \leq t \leq 5 \cdot 10^4) — the number of test cases.The only line of each test case contains two integers n and k (1 \leq k \leq n \leq 10^9) — the number of cards Vlad has, and the position of the card you need to output.OutputFor each test case, output a single integer — the k-th card Vladislav lays down.ExampleInput
117 17 27 37 47 57 67 71 134 1484 191000000000 1000000000Output
1
3
5
7
2
6
4
1
27
37
536870912
NoteIn the first seven test cases, n=7. Vladislav lays down the cards as follows: First — all the odd-numbered cards in the order 1, 3, 5, 7. Next — all cards that are twice an odd number in the order 2, 6. Next, there are no remaining cards that are 3 times an odd number. (Vladislav has only one of each card.) Next — all cards that are 4 times an odd number, and there is only one such card: 4. There are no more cards left, so Vladislav stops. Thus the order of cards is 1, 3, 5, 7, 2, 6, 4. | 117 17 27 37 47 57 67 71 134 1484 191000000000 1000000000 | 1 3 5 7 2 6 4 1 27 37 536870912 | 2 seconds | 256 megabytes | ['binary search', 'bitmasks', 'data structures', 'dp', 'implementation', 'math', 'number theory', '*1500'] |
D. Vlad and Divisiontime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputVladislav has n non-negative integers, and he wants to divide all of them into several groups so that in any group, any pair of numbers does not have matching bit values among bits from 1-st to 31-st bit (i.e., considering the 31 least significant bits of the binary representation).For an integer k, let k_2(i) denote the i-th bit in its binary representation (from right to left, indexing from 1). For example, if k=43, since 43=101011_2, then 43_2(1)=1, 43_2(2)=1, 43_2(3)=0, 43_2(4)=1, 43_2(5)=0, 43_2(6)=1, 43_2(7)=0, 43_2(8)=0, \dots, 43_2(31)=0.Formally, for any two numbers x and y in the same group, the condition x_2(i) \neq y_2(i) must hold for all 1 \leq i < 32.What is the minimum number of groups Vlad needs to achieve his goal? Each number must fall into exactly one group.InputThe first line contains a single integer t (1 \leq t \leq 10^4) — the number of test cases.The first line of each test case contains a single integer n (1 \leq n \leq 2 \cdot 10^5) — the total number of integers.The second line of each test case contains n given integers a_1, \ldots, a_n (0 \leq a_j < 2^{31}).The sum of n over all test cases in a test does not exceed 2 \cdot 10^5.OutputFor each test case, output a single integer — the minimum number of groups required to satisfy the condition.ExampleInput
941 4 3 420 21474836475476319172 261956880 2136179468 1671164475 188552676731335890506 811593141 11282233624688873446 627404104 1520079543 1458610201461545621 2085938026 1269342732 143025857540 0 2147483647 214748364730 0 214748364781858058912 289424735 1858058912 2024818580 1858058912 289424735 122665067 289424735Output
4
1
3
2
2
3
2
2
4
NoteIn the first test case, any two numbers have the same last 31 bits, so we need to place each number in its own group.In the second test case, a_1=0000000000000000000000000000000_2, a_2=1111111111111111111111111111111_2 so they can be placed in the same group because a_1(i) \ne a_2(i) for each i between 1 and 31, inclusive. | 941 4 3 420 21474836475476319172 261956880 2136179468 1671164475 188552676731335890506 811593141 11282233624688873446 627404104 1520079543 1458610201461545621 2085938026 1269342732 143025857540 0 2147483647 214748364730 0 214748364781858058912 289424735 1858058912 2024818580 1858058912 289424735 122665067 289424735 | 4 1 3 2 2 3 2 2 4 | 2 seconds | 256 megabytes | ['bitmasks', 'greedy', '*1300'] |
C. Vlad and a Sum of Sum of Digitstime limit per test0.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputPlease note that the time limit for this problem is only 0.5 seconds per test.Vladislav wrote the integers from 1 to n, inclusive, on the board. Then he replaced each integer with the sum of its digits.What is the sum of the numbers on the board now?For example, if n=12 then initially the numbers on the board are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12. Then after the replacement, the numbers become: 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3. The sum of these numbers is 1+2+3+4+5+6+7+8+9+1+2+3=51. Thus, for n=12 the answer is 51.InputThe first line contains an integer t (1 \leq t \leq 10^4) — the number of test cases.The only line of each test case contains a single integer n (1 \leq n \leq 2 \cdot 10^5) — the largest number Vladislav writes.OutputFor each test case, output a single integer — the sum of the numbers at the end of the process.ExampleInput
71212314342024200000Output
51
1
3
6
18465
28170
4600002
| 71212314342024200000 | 51 1 3 6 18465 28170 4600002 | 0.5 seconds | 256 megabytes | ['dp', 'implementation', '*1200'] |
B. Vlad and Shapestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVladislav has a binary square grid of n \times n cells. A triangle or a square is drawn on the grid with symbols \texttt{1}. As he is too busy being cool, he asks you to tell him which shape is drawn on the grid. A triangle is a shape consisting of k (k>1) consecutive rows, where the i-th row has 2 \cdot i-1 consecutive characters \texttt{1}, and the central 1s are located in one column. An upside down triangle is also considered a valid triangle (but not rotated by 90 degrees). Two left pictures contain examples of triangles: k=4, k=3. The two right pictures don't contain triangles. A square is a shape consisting of k (k>1) consecutive rows, where the i-th row has k consecutive characters \texttt{1}, which are positioned at an equal distance from the left edge of the grid. Examples of two squares: k=2, k=4. For the given grid, determine the type of shape that is drawn on it.InputThe first line contains a single integer t (1 \leq t \leq 100) — the number of test cases.The first line of each test case contains a single integer n (2 \leq n \leq 10) — the size of the grid.The next n lines each contain n characters \texttt{0} or \texttt{1}.The grid contains exactly one triangle or exactly one square that contains all the \texttt{1}s in the grid. It is guaranteed that the size of the triangle or square is greater than 1 (i.e., the shape cannot consist of exactly one 1).OutputFor each test case, output "SQUARE" if all the \texttt{1}s in the grid form a square, and "TRIANGLE" otherwise (without quotes).ExampleInput
630000110114000000000100111021111500111000100000000000000001000000000000000000000000000000000000000000000000000111111111001111111000011111000000111000000001000003111111111Output
SQUARE
TRIANGLE
SQUARE
TRIANGLE
TRIANGLE
SQUARE
| 630000110114000000000100111021111500111000100000000000000001000000000000000000000000000000000000000000000000000111111111001111111000011111000000111000000001000003111111111 | SQUARE TRIANGLE SQUARE TRIANGLE TRIANGLE SQUARE | 1 second | 256 megabytes | ['geometry', 'implementation', '*800'] |
A. Vlad and the Best of Fivetime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputVladislav has a string of length 5, whose characters are each either \texttt{A} or \texttt{B}.Which letter appears most frequently: \texttt{A} or \texttt{B}?InputThe first line of the input contains an integer t (1 \leq t \leq 32) — the number of test cases.The only line of each test case contains a string of length 5 consisting of letters \texttt{A} and \texttt{B}.All t strings in a test are different (distinct).OutputFor each test case, output one letter (\texttt{A} or \texttt{B}) denoting the character that appears most frequently in the string.ExampleInput
8ABABBABABABBBABAAAAABBBBBBABAAAAAABBAAAAOutput
B
A
B
A
B
A
A
A
| 8ABABBABABABBBABAAAAABBBBBBABAAAAAABBAAAA | B A B A B A A A | 1 second | 256 megabytes | ['implementation', '*800'] |
D. Good Triptime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n children in a class, m pairs among them are friends. The i-th pair who are friends have a friendship value of f_i. The teacher has to go for k excursions, and for each of the excursions she chooses a pair of children randomly, equiprobably and independently. If a pair of children who are friends is chosen, their friendship value increases by 1 for all subsequent excursions (the teacher can choose a pair of children more than once). The friendship value of a pair who are not friends is considered 0, and it does not change for subsequent excursions. Find the expected value of the sum of friendship values of all k pairs chosen for the excursions (at the time of being chosen). It can be shown that this answer can always be expressed as a fraction \dfrac{p}{q} where p and q are coprime integers. Calculate p\cdot q^{-1} \bmod (10^9+7).InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 5 \cdot 10^4). Description of the test cases follows.The first line of each test case contains 3 integers n, m and k (2 \le n \le 10^5, 0 \le m \le \min \Big(10^5, \frac{n(n-1)}{2} \Big), 1 \le k \le 2 \cdot 10^5) — the number of children, pairs of friends and excursions respectively.The next m lines contain three integers each — a_i, b_i, f_i — the indices of the pair of children who are friends and their friendship value. (a_i \neq b_i, 1 \le a_i,b_i \le n, 1 \le f_i \le 10^9). It is guaranteed that all pairs of friends are distinct.It is guaranteed that the sum of n and sum m over all test cases does not exceed 10^5 and the sum of k over all test cases does not exceed 2 \cdot 10^5.OutputFor each test case, print one integer — the answer to the problem.ExampleInput
4100 0 242 1 101 2 13 1 22 1 15 2 41 2 253 2 24Output
0
55
777777784
40000020
NoteFor the first test case, there are no pairs of friends, so the friendship value of all pairs is 0 and stays 0 for subsequent rounds, hence the friendship value for all excursions is 0.For the second test case, there is only one pair possible (1, 2) and its friendship value is initially 1, so each turn they are picked and their friendship value increases by 1. Therefore, the total sum is 1+2+3+\ldots+10 = 55.For the third test case, the final answer is \frac{7}{9} = 777\,777\,784\bmod (10^9+7). | 4100 0 242 1 101 2 13 1 22 1 15 2 41 2 253 2 24 | 0 55 777777784 40000020 | 1 second | 256 megabytes | ['combinatorics', 'dp', 'math', 'probabilities', '*1900'] |
B. A Balanced Problemset?time limit per test1.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputJay managed to create a problem of difficulty x and decided to make it the second problem for Codeforces Round #921. But Yash fears that this problem will make the contest highly unbalanced, and the coordinator will reject it. So, he decided to break it up into a problemset of n sub-problems such that the difficulties of all the sub-problems are a positive integer and their sum is equal to x. The coordinator, Aleksey, defines the balance of a problemset as the GCD of the difficulties of all sub-problems in the problemset. Find the maximum balance that Yash can achieve if he chooses the difficulties of the sub-problems optimally.InputThe first line of input contains a single integer t (1\leq t\leq 10^3) denoting the number of test cases.Each test case contains a single line of input containing two integers x (1\leq x\leq 10^8) and n (1\leq n\leq x).OutputFor each test case, print a single line containing a single integer denoting the maximum balance of the problemset Yash can achieve.ExampleInput
310 35 5420 69Output
2
1
6
NoteFor the first test case, one possible way is to break up the problem of difficulty 10 into a problemset having three problems of difficulties 4, 2 and 4 respectively, giving a balance equal to 2.For the second test case, there is only one way to break up the problem of difficulty 5 into a problemset of 5 problems with each problem having a difficulty 1 giving a balance equal to 1. | 310 35 5420 69 | 2 1 6 | 1.5 seconds | 256 megabytes | ['brute force', 'greedy', 'math', 'number theory', '*1200'] |
A. We Got Everything Covered!time limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two positive integers n and k. Your task is to find a string s such that all possible strings of length n that can be formed using the first k lowercase English alphabets occur as a subsequence of s. If there are multiple answers, print the one with the smallest length. If there are still multiple answers, you may print any of them.Note: A string a is called a subsequence of another string b if a can be obtained by deleting some (possibly zero) characters from b without changing the order of the remaining characters.InputThe first line of input contains a single integer t (1\leq t\leq 676) denoting the number of test cases.Each test case consists of a single line of input containing two integers n (1\leq n\leq 26) and k (1\leq k\leq 26).OutputFor each test case, print a single line containing a single string s which satisfies the above property. If there are multiple answers, print the one with the smallest length. If there are still multiple answers, you may print any of them.ExampleInput
41 22 12 22 3Output
ab
aa
baab
abcbac
NoteFor the first test case, there are two strings of length 1 which can be formed using the first 2 lowercase English alphabets, and they are present in s as a subsequence as follows: \texttt{a}: {\color{red}{\texttt{a}}}\texttt{b} \texttt{b}: \texttt{a}{\color{red}{\texttt{b}}} For the second test case, there is only one string of length 2 which can be formed using the first lowercase English alphabet, and it is present in s as a subsequence as follows: \texttt{aa}: {\color{red}{\texttt{aa}}} For the third test case, there are 4 strings of length 2 which can be formed using the first 2 lowercase English alphabets, and they are present in s as a subsequence as follows: \texttt{aa}: \texttt{b}{\color{red}{\texttt{aa}}}\texttt{b} \texttt{ab}: \texttt{ba}{\color{red}{\texttt{ab}}} \texttt{ba}: {\color{red}{\texttt{ba}}}\texttt{ab} \texttt{bb}: {\color{red}{\texttt{b}}}\texttt{aa}{\color{red}{\texttt{b}}} For the fourth test case, there are 9 strings of length 2 which can be formed using the first 3 lowercase English alphabets, and they are present in s as a subsequence as follows: \texttt{aa}: {\color{red}{\texttt{a}}}\texttt{bcb}{\color{red}{\texttt{a}}}\texttt{c} \texttt{ab}: {\color{red}{\texttt{ab}}}\texttt{cbac} \texttt{ac}: \texttt{abcb}{\color{red}{\texttt{ac}}} \texttt{ba}: \texttt{abc}{\color{red}{\texttt{ba}}}\texttt{c} \texttt{bb}: \texttt{a}{\color{red}{\texttt{b}}}\texttt{c}{\color{red}{\texttt{b}}}\texttt{ac} \texttt{bc}: \texttt{a}{\color{red}{\texttt{bc}}}\texttt{bac} \texttt{ca}: \texttt{ab}{\color{red}{\texttt{c}}}\texttt{b}{\color{red}{\texttt{a}}}\texttt{c} \texttt{cb}: \texttt{ab}{\color{red}{\texttt{cb}}}\texttt{ac} \texttt{cc}: \texttt{ab}{\color{red}{\texttt{c}}}\texttt{ba}{\color{red}{\texttt{c}}} | 41 22 12 22 3 | ab aa baab abcbac | 1 second | 256 megabytes | ['constructive algorithms', 'greedy', 'strings', '*800'] |
F. Anti-Proxy Attendancetime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThis is an interactive problem!Mr. 1048576 is one of those faculty who hates wasting his time in taking class attendance. Instead of taking attendance the old-fashioned way, he decided to try out something new today.There are n students in his class, having roll numbers 1 to n. He knows that exactly 1 student is absent today. In order to determine who is absent, he can ask some queries to the class. In each query, he can provide two integers l and r (1\leq l\leq r\leq n) and all students whose roll numbers are between l and r (inclusive) will raise their hands. He then counts them to determine if the roll number of the absent student lies between these values.Things seemed fine until his teaching assistant noticed something — the students are dishonest! Some students whose roll numbers lie in the given range may not raise their hands, while some other students whose roll number does not lie in the given range may raise their hands. But the students don't want to raise much suspicion. So, only the following 4 cases are possible for a particular query (l,r) — True Positive: r-l+1 students are present and r-l+1 students raised their hands. True Negative: r-l students are present and r-l students raised their hands. False Positive: r-l students are present but r-l+1 students raised their hands. False Negative: r-l+1 students are present but r-l students raised their hands. In the first two cases, the students are said to be answering honestly, while in the last two cases, the students are said to be answering dishonestly. The students can mutually decide upon their strategy, not known to Mr. 1048576. Also, the students do not want to raise any suspicion and at the same time, want to create a lot of confusion. So, their strategy always meets the following two conditions — The students will never answer honestly 3 times in a row. The students will never answer dishonestly 3 times in a row. Mr. 1048576 is frustrated by this act of students. So, he is willing to mark at most 2 students as absent (though he knows that only one is). The attendance is said to be successful if the student who is actually absent is among those two. Also, due to limited class time, he can only ask up to \lceil\log_{1.116}{n}\rceil-1 queries (weird numbers but okay). Help him complete a successful attendance.InteractionFirst read a line containing a single integer t (1\leq t\leq 2048) denoting the number of independent test cases that you must solve.For each test case, first read a line containing a single integer n (3\leq n\leq 10^5). Then you may ask up to \lceil\log_{1.116}{n}\rceil-1 queries.To ask a query, print a single line in the format "? l r" (without quotes) (1\leq l\leq r\leq n). Then read a single line containing a single integer x (r-l\leq x\leq r-l+1) denoting the number of students who raised their hands corresponding to the query.To mark a student as absent, print a single line in the format "! a" (without quotes) (1\leq a\leq n). Then read a single integer y (y\in\{0,1\}). If the student with roll number a was absent, y=1, else, y=0. Note that this operation does not count as a query but you can do this operation at most 2 times.To end a test case, print a single line in the format "#" (without quotes). Then you must continue solving the remaining test cases.If you ask more queries than allowed or ask an invalid query, you will get the Wrong answer verdict.It is guaranteed that the sum of n over all test cases does not exceed 10^5.After printing the answers, do not forget to output end of line and flush the output buffer. Otherwise, you will get the verdict Idleness limit exceeded. To flush the buffer, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; Read documentation for other languages. Note that the grader for this problem is adaptive meaning that the answer may change depending on your queries but will always remain consistent with the constraints and the answer to the previous queries.Input format for HacksThe test cases for this problem use both non-adaptive and adaptive graders. You can use the non-adaptive grader for making hacks.The first line of input contains a single integer t (1\leq t\leq 2048).The first line of each test case contains three integers g, n and x where g=1 (to identify that this test case must use the non-adaptive grader), n (3\leq n\leq 10^5) represents the number of students in the class and x (1\leq x\leq n) represents the roll number of the student who is absent. You must ensure that the sum of n over all test cases does not exceed 10^5.The second line of each test case contains a single string S (1\leq\vert S\vert\leq 120, S_i\in \{\texttt{T},\texttt{F}\}). This string represents the pattern of the truth sequence. If S_{(i-1)\bmod \vert S\vert+1}= \texttt{T}, the students will act honestly during the i-th query, otherwise they will act dishonestly. You must also ensure that there is no index i such that S_{(i-1)\bmod \vert S\vert+1} = S_{i\bmod \vert S\vert+1} = S_{(i+1)\bmod \vert S\vert+1}.ExampleInput
2
5
3
2
1
2
0
1
0
2
0
1
6
6
2
2
0
1
1
0
0
0
1
Output
? 1 4
? 3 5
? 2 2
? 1 3
? 3 3
? 3 3
! 3
? 2 4
? 4 4
! 2
#
? 1 6
? 1 3
? 4 6
? 1 1
? 3 3
? 5 5
! 3
? 2 2
? 4 4
! 4
#
NoteFor the first test case, the student with roll number 2 is absent and the truth sequence (see section for hacks) is TFFTFTTF. During execution of your solution, this test case will use a non-adaptive grader.For the second test case, the student with roll number 4 is absent, and the truth sequence is FFTFTTFT. During the execution of your solution, in this test case your program will interact with an adaptive grader. So, the actual answer might be different depending on your queries but will always remain consistent with the responses to the previous queries. | 2
5
3
2
1
2
0
1
0
2
0
1
6
6
2
2
0
1
1
0
0
0
1
| ? 1 4 ? 3 5 ? 2 2 ? 1 3 ? 3 3 ? 3 3 ! 3 ? 2 4 ? 4 4 ! 2 # ? 1 6 ? 1 3 ? 4 6 ? 1 1 ? 3 3 ? 5 5 ! 3 ? 2 2 ? 4 4 ! 4 # | 2 seconds | 256 megabytes | ['constructive algorithms', 'dp', 'interactive', 'ternary search', '*3500'] |
E. Paper Cutting Againtime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere is a rectangular sheet of paper with initial height n and width m. Let the current height and width be h and w respectively. We introduce a xy-coordinate system so that the four corners of the sheet are (0, 0), (w, 0), (0, h), and (w, h). The sheet can then be cut along the lines x = 1,2,\ldots,w-1 and the lines y = 1,2,\ldots,h-1. In each step, the paper is cut randomly along any one of these h+w-2 lines. After each vertical and horizontal cut, the right and bottom piece of paper respectively are discarded.Find the expected number of steps required to make the area of the sheet of paper strictly less than k. It can be shown that this answer can always be expressed as a fraction \dfrac{p}{q} where p and q are coprime integers. Calculate p\cdot q^{-1} \bmod (10^9+7).InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 57000). Description of the test cases follows.The first line of each test case contains 3 integers n, m, and k (1 \le n, m \le 10^6, 2 \le k \le 10^{12}).It is guaranteed that the sum of n and the sum of m over all test cases do not exceed 10^6.OutputFor each test case, print one integer — the answer to the problem.ExampleInput
42 4 102 4 82 4 22 4 6Output
0
1
833333342
250000003
NoteFor the first test case, the area is already less than 10 so no cuts are required.For the second test case, the area is exactly 8 so any one of the 4 possible cuts would make the area strictly less than 8.For the third test case, the final answer is \frac{17}{6} = 833\,333\,342\bmod (10^9+7).For the fourth test case, the final answer is \frac{5}{4} = 250\,000\,003\bmod (10^9+7). | 42 4 102 4 82 4 22 4 6 | 0 1 833333342 250000003 | 3 seconds | 256 megabytes | ['combinatorics', 'probabilities', '*3100'] |
D. Balanced Subsequencestime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputA sequence of brackets is called balanced if one can turn it into a valid math expression by adding characters '+' and '1'. For example, sequences '(())()', '()', and '(()(()))' are balanced, while ')(', '(()', and '(()))(' are not.A subsequence is a sequence that can be derived from the given sequence by deleting zero or more elements without changing the order of the remaining elements.You are given three integers n, m and k. Find the number of sequences consisting of n '(' and m ')', such that the longest balanced subsequence is of length 2 \cdot k. Since the answer can be large calculate it modulo 1\,000\,000\,007 (10^9 + 7).InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \le t \le 3 \cdot 10^3). Description of the test cases follows.The first line of each test case contains three integers n, m and k (1 \le n, m, k \le 2 \cdot 10^3)OutputFor each test case, print one integer — the answer to the problem.ExampleInput
32 2 23 2 33 2 1Output
2
0
4
NoteFor the first test case "()()", "(())" are the 2 sequencesFor the second test case no sequence is possible.For the third test case ")((()", ")(()(", ")()((", "())((" are the 4 sequences. | 32 2 23 2 33 2 1 | 2 0 4 | 1 second | 256 megabytes | ['combinatorics', 'dp', 'math', '*2700'] |
C. Fractal Origamitime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou have a square piece of paper with a side length equal to 1 unit. In one operation, you fold each corner of the square to the center of the paper, thus forming another square with a side length equal to \dfrac{1}{\sqrt{2}} units. By taking this square as a new square, you do the operation again and repeat this process a total of N times. Performing operations for N = 2. After performing the set of operations, you open the paper with the same side up you started with and see some crease lines on it. Every crease line is one of two types: a mountain or a valley. A mountain is when the paper folds outward, and a valley is when the paper folds inward.You calculate the sum of the length of all mountain crease lines on the paper and call it M. Similarly, you calculate for valley crease lines and call it V. You want to find the value of \dfrac{M}{V}.It can be proved that this value can be represented in the form of A + B\sqrt{2}, where A and B are rational numbers. Let this B be represented as an irreducible fraction \dfrac{p}{q}, your task is to print p*inv(q) modulo 999\,999\,893 (note the unusual modulo), where inv(q) is the modular inverse of q.InputEach test contains multiple test cases. The first line contains the number of test cases t (1 \leq t \leq 10^4). Description of the test cases follows.The only line of each test case contains an integer N (1 \leq N \leq 10^9), the number of operations you perform on the square paper.OutputFor each test case, print on a new line the required answer.ExampleInput
3123Output
0
1
714285638
NoteThe blue lines in the given figures represent mountain crease lines, and the green lines represent valley crease lines. Crease lines after 1 operation (\dfrac{M}{V} = 0).Crease lines after 2 operations (\dfrac{M}{V} = \sqrt{2} - 1). | 3123 | 0 1 714285638 | 1 second | 256 megabytes | ['geometry', 'math', 'matrices', '*2400'] |
B. Space Harbourtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n points numbered 1 to n on a straight line. Initially, there are m harbours. The i-th harbour is at point X_i and has a value V_i. It is guaranteed that there are harbours at the points 1 and n. There is exactly one ship on each of the n points. The cost of moving a ship from its current location to the next harbour is the product of the value of the nearest harbour to its left and the distance from the nearest harbour to its right. Specifically, if a ship is already at a harbour, the cost of moving it to the next harbour is 0. Additionally, there are q queries, each of which is either of the following 2 types: 1 x v — Add a harbour at point x with value v. It is guaranteed that before adding the harbour, there is no harbour at point x. 2 l r — Print the sum of the cost of moving all ships at points from l to r to their next harbours. Note that you just need to calculate the cost of moving the ships but not actually move them. InputThe first line contains three integers n, m, and q (2 \le m \le n \le 3 \cdot 10^5, 1 \le q \le 3 \cdot 10^5) — the number of points, harbours, and queries, respectively.The second line contains m distinct integers X_1, X_2, \ldots, X_m(1 \le X_i \le n) — the position at which the i-th harbour is located.The third line contains m integers V_1, V_2, \ldots, V_m(1 \le V_i \le 10^7) — the value of the i-th harbour.Each of the next q lines contains three integers. The first integer is t (1\le t \le 2) — type of query. If t=1, then the next two integers are x and v (2 \le x \le n - 1, 1 \le v \le 10^7) — first-type query. If t=2, then the next two integers are l and r (1 \le l \le r \le n) — second-type query.It is guaranteed that there is at least one second-type query.OutputFor every second-type query, print one integer in a new line — answer to this query.ExampleInput
8 3 41 3 83 24 102 2 51 5 152 5 52 7 8Output
171
0
15
NoteFor the first type 2 query, the cost for ships at positions 2, 3, 4 and 5 are 3(3 \times 1), 0, 96(24 \times 4) and 72(24 \times 3) respectively.For the second type 2 query, since the ship at position 5 is already at a harbour, so the cost is 0.For the third type 2 query, the cost for ships at position 7 and 8 are 15(15 \times 1) and 0 respectively. | 8 3 41 3 83 24 102 2 51 5 152 5 52 7 8 | 171 0 15 | 2 seconds | 256 megabytes | ['data structures', 'implementation', 'math', 'sortings', '*2100'] |
A. Did We Get Everything Covered?time limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given two integers n and k along with a string s.Your task is to check whether all possible strings of length n that can be formed using the first k lowercase English alphabets occur as a subsequence of s. If the answer is NO, you also need to print a string of length n that can be formed using the first k lowercase English alphabets which does not occur as a subsequence of s.If there are multiple answers, you may print any of them.Note: A string a is called a subsequence of another string b if a can be obtained by deleting some (possibly zero) characters from b without changing the order of the remaining characters.InputThe first line of input contains a single integer t \, (1 \le t \le 10^5), the number of test cases.The first line of each test case contains 3 integers n \, (1 \le n \le 26), \: k \, (1 \le k \le 26), \: m \, (1 \le m \le 1000), where n and k are the same as described in the input and m is the length of the string s.The second line of each test case contains a single string s of length m, comprising only of the first k lowercase English alphabets.It is guaranteed that the sum of m and the sum of n over all test cases does not exceed 10^6.OutputFor each test case, print YES if all possible strings of length n that can be formed using the first k lowercase English alphabets occur as a subsequence of s, else print NO.If your answer is NO, print a string of length n that can be formed using the first k lowercase English alphabets which does not occur as a subsequence of s in the next line.You may print each letter of YES or NO in any case (for example, YES, yES, YeS will all be recognized as a positive answer).ExampleInput
32 2 4abba2 2 3abb3 3 10aabbccababOutput
YES
NO
aa
NO
ccc
NoteFor the first test case, all possible strings (aa, ab, ba, bb) of length 2 that can be formed using the first 2 English alphabets occur as a subsequence of abba.For the second test case, the string aa is not a subsequence of abb. | 32 2 4abba2 2 3abb3 3 10aabbccabab | YES NO aa NO ccc | 2 seconds | 256 megabytes | ['constructive algorithms', 'dp', 'greedy', 'shortest paths', 'strings', '*1500'] |
F. Shrink-Reversetime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given a binary string s of length n (a string consisting of n characters, and each character is either 0 or 1).Let's look at s as at a binary representation of some integer, and name that integer as the value of string s. For example, the value of 000 is 0, the value of 01101 is 13, "100000" is 32 and so on.You can perform at most k operations on s. Each operation should have one of the two following types: SWAP: choose two indices i < j in s and swap s_i with s_j; SHRINK-REVERSE: delete all leading zeroes from s and reverse s. For example, after you perform SHRINK-REVERSE on 000101100, you'll get 001101.What is the minimum value of s you can achieve by performing at most k operations on s?InputThe first line contains two integers n and k (2 \le n \le 5 \cdot 10^5; 1 \le k \le n) — the length of the string s and the maximum number of operations.The second line contains the string s of length n consisting of characters 0 and/or 1. Additional constraint on the input: s contains at least one 1.OutputPrint a single integer — the minimum value of s you can achieve using no more than k operations. Since the answer may be too large, print it modulo 10^{9} + 7.Note that you need to minimize the original value, not the remainder.ExamplesInput
8 210010010Output
7
Input
8 201101000Output
7
Input
30 30111111111111111111111111111111Output
73741816
Input
14 110110001111100Output
3197
NoteIn the first example, one of the optimal strategies is the following: 10010010 \xrightarrow{\texttt{SWAP}} 00010110; 00010110 \xrightarrow{\texttt{SWAP}} 00000111. The value of 00000111 is 7.In the second example, one of the optimal strategies is the following: 01101000 \xrightarrow{\texttt{SHRINK}} 1101000 \xrightarrow{\texttt{REVERSE}} 0001011; 0001011 \xrightarrow{\texttt{SWAP}} 0000111. The value of 0000111 is 7. | 8 210010010 | 7 | 3 seconds | 256 megabytes | ['binary search', 'brute force', 'greedy', 'hashing', 'implementation', 'string suffix structures', 'strings', '*2800'] |
E. Count Pathstime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputYou are given a tree, consisting of n vertices, numbered from 1 to n. Every vertex is colored in some color, denoted by an integer from 1 to n.A simple path of the tree is called beautiful if: it consists of at least 2 vertices; the first and the last vertices of the path have the same color; no other vertex on the path has the same color as the first vertex. Count the number of the beautiful simple paths of the tree. Note that paths are considered undirected (i. e. the path from x to y is the same as the path from y to x).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 (2 \le n \le 2 \cdot 10^5) — the number of vertices in the tree.The second line contains n integers c_1, c_2, \dots, c_n (1 \le c_i \le n) — the color of each vertex.The i-th of the next n - 1 lines contains two integers v_i and u_i (1 \le v_i, u_i \le n; v_i \neq u_i) — the i-th edge of the tree.The given edges form a valid tree. The sum of n over all testcases doesn't exceed 2 \cdot 10^5.OutputFor each testcase, print a single integer — the number of the beautiful simple paths of the tree.ExampleInput
431 2 11 22 352 1 2 1 21 21 33 44 551 2 3 4 51 21 33 44 542 2 2 23 13 23 4Output
1
3
0
3
| 431 2 11 22 352 1 2 1 21 21 33 44 551 2 3 4 51 21 33 44 542 2 2 23 13 23 4 | 1 3 0 3 | 2 seconds | 512 megabytes | ['data structures', 'dfs and similar', 'dp', 'dsu', 'graphs', 'trees', '*2000'] |
D. Slimestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n slimes placed in a line. The slimes are numbered from 1 to n in order from left to right. The size of the i-th slime is a_i.Every second, the following happens: exactly one slime eats one of its neighbors and increases its size by the eaten neighbor's size. A slime can eat its neighbor only if it is strictly bigger than this neighbor. If there is no slime which is strictly bigger than one of its neighbors, the process ends.For example, suppose n = 5, a = [2, 2, 3, 1, 4]. The process can go as follows: first, the 3-rd slime eats the 2-nd slime. The size of the 3-rd slime becomes 5, the 2-nd slime is eaten. then, the 3-rd slime eats the 1-st slime (they are neighbors since the 2-nd slime is already eaten). The size of the 3-rd slime becomes 7, the 1-st slime is eaten. then, the 5-th slime eats the 4-th slime. The size of the 5-th slime becomes 5, the 4-th slime is eaten. then, the 3-rd slime eats the 5-th slime (they are neighbors since the 4-th slime is already eaten). The size of the 3-rd slime becomes 12, the 5-th slime is eaten. For each slime, calculate the minimum number of seconds it takes for this slime to be eaten by another slime (among all possible ways the process can go), or report that it is impossible.InputThe first line contains a single integer t (1 \le t \le 10^4) — the number of test cases.The first line of each test case contains a single integer n (1 \le n \le 3 \cdot 10^5) — the number of slimes.The second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9).The sum of n over all test cases doesn't exceed 3 \cdot 10^5.OutputFor each test case, print n integers. The i-th integer should be equal to the minimum number of seconds it takes for the i-th slime to be eaten by another slime or -1 if it is impossible.ExampleInput
443 2 4 231 2 352 2 3 1 174 2 3 6 1 1 8Output
2 1 2 1
1 1 -1
2 1 -1 1 2
2 1 1 3 1 1 4
| 443 2 4 231 2 352 2 3 1 174 2 3 6 1 1 8 | 2 1 2 1 1 1 -1 2 1 -1 1 2 2 1 1 3 1 1 4 | 2 seconds | 256 megabytes | ['binary search', 'constructive algorithms', 'data structures', 'greedy', 'two pointers', '*1800'] |
C. Find Btime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputAn array a of length m is considered good if there exists an integer array b of length m such that the following conditions hold: \sum\limits_{i=1}^{m} a_i = \sum\limits_{i=1}^{m} b_i; a_i \neq b_i for every index i from 1 to m; b_i > 0 for every index i from 1 to m. You are given an array c of length n. Each element of this array is greater than 0.You have to answer q queries. During the i-th query, you have to determine whether the subarray c_{l_{i}}, c_{l_{i}+1}, \dots, c_{r_{i}} is good.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 two integers n and q (1 \le n, q \le 3 \cdot 10^5) — the length of the array c and the number of queries.The second line of each test case contains n integers c_1, c_2, \dots, c_n (1 \le c_i \le 10^9).Then q lines follow. The i-th of them contains two integers l_i and r_i (1 \le l_i \le r_i \le n) — the borders of the i-th subarray.Additional constraints on the input: the sum of n over all test cases does not exceed 3 \cdot 10^5; the sum of q over all test cases does not exceed 3 \cdot 10^5.OutputFor each query, print YES if the subarray is good. Otherwise, print NO.You can output each letter of the answer in any case (upper or lower). For example, the strings yEs, yes, Yes, and YES will all be recognized as positive responses.ExampleInput
15 41 2 1 4 51 54 43 41 3Output
YES
NO
YES
NO
| 15 41 2 1 4 51 54 43 41 3 | YES NO YES NO | 3 seconds | 256 megabytes | ['constructive algorithms', 'greedy', '*1400'] |
B. Monsters Attack!time limit per test2.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are playing a computer game. The current level of this game can be modeled as a straight line. Your character is in point 0 of this line. There are n monsters trying to kill your character; the i-th monster has health equal to a_i and is initially in the point x_i.Every second, the following happens: first, you fire up to k bullets at monsters. Each bullet targets exactly one monster and decreases its health by 1. For each bullet, you choose its target arbitrary (for example, you can fire all bullets at one monster, fire all bullets at different monsters, or choose any other combination). Any monster can be targeted by a bullet, regardless of its position and any other factors; then, all alive monsters with health 0 or less die; then, all alive monsters move 1 point closer to you (monsters to the left of you increase their coordinates by 1, monsters to the right of you decrease their coordinates by 1). If any monster reaches your character (moves to the point 0), you lose. Can you survive and kill all n monsters without letting any of them reach your character?InputThe first line of the input contains one integer t (1 \le t \le 3 \cdot 10^4) — the number of test cases.Each test case consists of three lines: the first line contains two integers n and k (1 \le n \le 3 \cdot 10^5; 1 \le k \le 2 \cdot 10^9); the second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9); the third line contains n integers x_1, x_2, \dots, x_n (-n \le x_1 < x_2 < x_3 < \dots < x_n \le n; x_i \ne 0). Additional constraint on the input: the sum of n over all test cases does not exceed 3 \cdot 10^5.OutputFor each test case, print YES if you can kill all n monsters before they reach your character, or NO otherwise.You can output each letter of the answer in any case (upper or lower). For example, the strings yEs, yes, Yes, and YES will all be recognized as positive responses.ExampleInput
53 21 2 3-1 2 32 11 1-1 14 103 4 2 5-3 -2 1 35 32 1 3 2 5-3 -2 3 4 52 11 21 2Output
YES
NO
YES
YES
NO
NoteIn the first example, you can act as follows: during the 1-st second, fire 1 bullet at the 1-st monster and 1 bullet at the 3-rd monster. Then the 1-st monster dies, the 2-nd and the 3-rd monster move closer; during the 2-nd second, fire 2 bullets at the 2-nd monster. Then the 2-nd monster dies, the 3-rd monster moves closer; during the 3-rd second, fire 2 bullets at the 3-rd monster. Then the 3-rd monster dies. In the second example, you can fire only 1 bullet, so you can kill only one of the two monsters during the 1-st second. Then, the remaining monster moves closer and kills your character. | 53 21 2 3-1 2 32 11 1-1 14 103 4 2 5-3 -2 1 35 32 1 3 2 5-3 -2 3 4 52 11 21 2 | YES NO YES YES NO | 2.5 seconds | 256 megabytes | ['dp', 'greedy', 'implementation', '*1100'] |
A. Moving Chipstime limit per test2 secondsmemory limit per test512 megabytesinputstandard inputoutputstandard outputThere is a ribbon divided into n cells, numbered from 1 to n from left to right. Each cell either contains a chip or is free.You can perform the following operation any number of times (possibly zero): choose a chip and move it to the closest free cell to the left. You can choose any chip that you want, provided that there is at least one free cell to the left of it. When you move the chip, the cell where it was before the operation becomes free.Your goal is to move the chips in such a way that they form a single block, without any free cells between them. What is the minimum number of operations you have to perform?InputThe first line contains one integer t (1 \le t \le 1000) — the number of test cases.Each test case consists of two lines: the first line contains one integer n (2 \le n \le 50) — the number of cells; the second line contains n integers a_1, a_2, \dots, a_n (0 \le a_i \le 1); a_i = 0 means that the i-th cell is free; a_i = 1 means that the i-th cell contains a chip. Additional constraint on the input: in each test case, at least one cell contains a chip.OutputFor each test case, print one integer — the minimum number of operations you have to perform so that all chips form a single block without any free cells between them.ExampleInput
580 1 1 1 0 1 1 060 1 0 0 0 061 1 1 1 1 151 0 1 0 190 1 1 0 0 0 1 1 0Output
1
0
0
2
3
NoteIn the first example, you can perform the operation on the chip in the 7-th cell. The closest free cell to the left is the 5-th cell, so it moves there. After that, all chips form a single block.In the second example, all chips are already in a single block. Same for the third example. | 580 1 1 1 0 1 1 060 1 0 0 0 061 1 1 1 1 151 0 1 0 190 1 1 0 0 0 1 1 0 | 1 0 0 2 3 | 2 seconds | 512 megabytes | ['greedy', 'implementation', '*800'] |
F. Replace on Segmenttime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputYou are given an array a_1, a_2, \dots, a_n, where each element is an integer from 1 to x.You can perform the following operation with it any number of times: choose three integers l, r and k such that 1 \le l \le r \le n, 1 \le k \le x and each element a_i such that l \le i \le r is different from k. Then, for each i \in [l, r], replace a_i with k. In other words, you choose a subsegment of the array and an integer from 1 to x which does not appear in that subsegment, and replace every element in the subsegment with that chosen integer.Your goal is to make all elements in the array equal. What is the minimum number of operations that you have to perform?InputThe first line contains one integer t (1 \le t \le 100) — the number of test cases.Each test case consists of two lines: the first line contains two integers n and x (1 \le x \le n \le 100); the second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le x). Additional constraint on the input: the sum of n over all test cases does not exceed 500.OutputFor each test case, print one integer — the minimum number of operations you have to perform.ExampleInput
33 21 2 16 31 2 3 1 2 312 33 1 3 1 2 1 1 2 3 1 1 3Output
1
2
2
| 33 21 2 16 31 2 3 1 2 312 33 1 3 1 2 1 1 2 3 1 1 3 | 1 2 2 | 3 seconds | 256 megabytes | ['dp', 'graph matchings', '*2500'] |
E. Increasing Subsequencestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's recall that an increasing subsequence of the array a is a sequence that can be obtained from it by removing some elements without changing the order of the remaining elements, and the remaining elements are strictly increasing (i. e a_{b_1} < a_{b_2} < \dots < a_{b_k} and b_1 < b_2 < \dots < b_k). Note that an empty subsequence is also increasing.You are given a positive integer X. Your task is to find an array of integers of length at most 200, such that it has exactly X increasing subsequences, or report that there is no such array. If there are several answers, you can print any of them.If two subsequences consist of the same elements, but correspond to different positions in the array, they are considered different (for example, the array [2, 2] has two different subsequences equal to [2]).InputThe first line contains a single integer t (1 \le t \le 1000) — the number of test cases.The only line of each test case contains a single integer X (2 \le X \le 10^{18}).OutputFor each query, print the answer to it. If it is impossible to find the required array, print -1 on the first line. Otherwise, print a positive integer n on the first line — the length of the array. On the second line, print n integers — the required array itself. If there are several answers, you can print any of them. All elements of the array should be in the range [-10^9; 10^9].ExampleInput
4251337Output
1
0
3
0 1 0
5
2 2 3 4 2
7
-1 -1 0 0 2 3 -1 | 4251337 | 1 0 3 0 1 0 5 2 2 3 4 2 7 -1 -1 0 0 2 3 -1 | 2 seconds | 256 megabytes | ['bitmasks', 'constructive algorithms', 'divide and conquer', 'greedy', 'math', '*1800'] |
D. Berserk Monsterstime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputMonocarp is playing a computer game (yet again). Guess what is he doing? That's right, killing monsters.There are n monsters in a row, numbered from 1 to n. The i-th monster has two parameters: attack value equal to a_i and defense value equal to d_i. In order to kill these monsters, Monocarp put a berserk spell on them, so they're attacking each other instead of Monocarp's character.The fight consists of n rounds. Every round, the following happens: first, every alive monster i deals a_i damage to the closest alive monster to the left (if it exists) and the closest alive monster to the right (if it exists); then, every alive monster j which received more than d_j damage during this round dies. I. e. the j-th monster dies if and only if its defense value d_j is strictly less than the total damage it received this round. For each round, calculate the number of monsters that will die during that round.InputThe first line contains one integer t (1 \le t \le 10^4) — the number of test cases.Each test case consists of three lines: the first line contains one integer n (1 \le n \le 3 \cdot 10^5); the second line contains n integers a_1, a_2, \dots, a_n (1 \le a_i \le 10^9); the third line contains n integers d_1, d_2, \dots, d_n (1 \le d_i \le 10^9). Additional constraint on the input: the sum of n over all test cases does not exceed 3 \cdot 10^5.OutputFor each test case, print n integers. The i-th integer should be equal to the number of monsters that die during the i-th round.ExampleInput
353 4 7 5 104 9 1 18 122 11 341 1 2 43 3 4 2Output
3 1 0 0 0
0 0
1 1 1 0
NoteExplanation for the first test case of the example:During the first round, the following happens: the monster 1 deals 3 damage to the monster 2; the monster 2 deals 4 damage to the monster 1 and the monster 3; the monster 3 deals 7 damage to the monster 2 and the monster 4; the monster 4 deals 5 damage to the monster 3 and the monster 5; the monster 5 deals 10 damage to the monster 4; the monster 1 does not die, since it received 4 damage and its defense is 4; the monster 2 dies, since it received 10 damage and its defense is 9; the monster 3 dies, since it received 9 damage and its defense is 1; the monster 4 does not die, since it received 17 damage and its defense is 18; the monster 5 dies, since it received 5 damage and its defense is 1. After the first round, the monsters 1 and 4 stay alive.During the second round, the following happens: the monster 1 deals 3 damage to the monster 4; the monster 4 deals 5 damage to the monster 1; the monster 1 dies, since it received 5 damage and its defense is 4; the monster 4 does not die, since it received 3 damage and its defense is 18. During the next three rounds, only the 4-th monster is alive, so nothing happens. | 353 4 7 5 104 9 1 18 122 11 341 1 2 43 3 4 2 | 3 1 0 0 0 0 0 1 1 1 0 | 2 seconds | 256 megabytes | ['brute force', 'data structures', 'dsu', 'implementation', 'math', '*1900'] |
C. Closest Citiestime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputThere are n cities located on the number line, the i-th city is in the point a_i. The coordinates of the cities are given in ascending order, so a_1 < a_2 < \dots < a_n.The distance between two cities x and y is equal to |a_x - a_y|.For each city i, let's define the closest city j as the city such that the distance between i and j is not greater than the distance between i and each other city k. For example, if the cities are located in points [0, 8, 12, 15, 20], then: the closest city to the city 1 is the city 2; the closest city to the city 2 is the city 3; the closest city to the city 3 is the city 4; the closest city to the city 4 is the city 3; the closest city to the city 5 is the city 4. The cities are located in such a way that for every city, the closest city is unique. For example, it is impossible for the cities to be situated in points [1, 2, 3], since this would mean that the city 2 has two closest cities (1 and 3, both having distance 1).You can travel between cities. Suppose you are currently in the city x. Then you can perform one of the following actions: travel to any other city y, paying |a_x - a_y| coins; travel to the city which is the closest to x, paying 1 coin. You are given m queries. In each query, you will be given two cities, and you have to calculate the minimum number of coins you have to spend to travel from one city to the other city.InputThe first line contains one integer t (1 \le t \le 10^4) — the number of test cases.Each test case is given in the following format: the first line contains one integer n (2 \le n \le 10^5); the second line contains n integers a_1, a_2, \dots, a_n (0 \le a_1 < a_2 < \dots < a_n \le 10^9); the third line contains one integer m (1 \le m \le 10^5); then m lines follow; the i-th of them contains two integers x_i and y_i (1 \le x_i, y_i \le n; x_i \ne y_i), denoting that in the i-th query, you have to calculate the minimum number of coins you have to spend to travel from the city x_i to the city y_i. Additional constraints on the input: in every test case, for each city, the closest city is determined uniquely; the sum of n over all test cases does not exceed 10^5; the sum of m over all test cases does not exceed 10^5. OutputFor each query, print one integer — the minimum number of coins you have to spend.ExampleInput
150 8 12 15 2051 41 53 43 25 1Output
3
8
1
4
14
NoteLet's consider the first two queries in the example from the statement: in the first query, you are initially in the city 1. You can travel to the closest city (which is the city 2), paying 1 coin. Then you travel to the closest city (which is the city 3) again, paying 1 coin. Then you travel to the closest city (which is the city 4) again, paying 1 coin. In total, you spend 3 coins to get from the city 1 to the city 4; in the second query, you can use the same way to get from the city 1 to the city 4, and then spend 5 coins to travel from the city 4 to the city 5. | 150 8 12 15 2051 41 53 43 25 1 | 3 8 1 4 14 | 2 seconds | 256 megabytes | ['greedy', 'implementation', 'math', '*1300'] |