problems
stringlengths
691
4.89k
Andi and Budi were given an assignment to tidy up their bookshelf of n books. Each book is represented by the book title β€” a string s_i numbered from 1 to n, each with length m. Andi really wants to sort the book lexicographically ascending, while Budi wants to sort it lexicographically descending. Settling their fight, they decided to combine their idea and sort it asc-desc-endingly, where the odd-indexed characters will be compared ascendingly, and the even-indexed characters will be compared descendingly. A string a occurs before a string b in asc-desc-ending order if and only if in the first position where a and b differ, the following holds: * if it is an odd position, the string a has a letter that appears earlier in the alphabet than the corresponding letter in b; * if it is an even position, the string a has a letter that appears later in the alphabet than the corresponding letter in b. Input The first line contains two integers n and m (1 ≀ n β‹… m ≀ 10^6). The i-th of the next n lines contains a string s_i consisting of m uppercase Latin letters β€” the book title. The strings are pairwise distinct. Output Output n integers β€” the indices of the strings after they are sorted asc-desc-endingly. Example Input 5 2 AA AB BB BA AZ Output 5 2 1 3 4 Note The following illustrates the first example. <image>
Mr. Chanek lives in a city represented as a plane. He wants to build an amusement park in the shape of a circle of radius r. The circle must touch the origin (point (0, 0)). There are n bird habitats that can be a photo spot for the tourists in the park. The i-th bird habitat is at point p_i = (x_i, y_i). Find the minimum radius r of a park with at least k bird habitats inside. A point is considered to be inside the park if and only if the distance between p_i and the center of the park is less than or equal to the radius of the park. Note that the center and the radius of the park do not need to be integers. In this problem, it is guaranteed that the given input always has a solution with r ≀ 2 β‹… 10^5. Input The first line contains two integers n and k (1 ≀ n ≀ 10^5, 1 ≀ k ≀ n) β€” the number of bird habitats in the city and the number of bird habitats required to be inside the park. The i-th of the next n lines contains two integers x_i and y_i (0 ≀ |x_i|, |y_i| ≀ 10^5) β€” the position of the i-th bird habitat. Output Output a single real number r denoting the minimum radius of a park with at least k bird habitats inside. It is guaranteed that the given input always has a solution with r ≀ 2 β‹… 10^5. Your answer is considered correct if its absolute or relative error does not exceed 10^{-4}. Formally, let your answer be a, and the jury's answer be b. Your answer is accepted if and only if \frac{|a - b|}{max{(1, |b|)}} ≀ 10^{-4}. Examples Input 8 4 -3 1 -4 4 1 5 2 2 2 -2 -2 -4 -1 -1 -6 0 Output 3.1622776589 Input 1 1 0 0 Output 0.0000000000 Note In the first example, Mr. Chanek can put the center of the park at (-3, -1) with radius √{10} β‰ˆ 3.162. It can be proven this is the minimum r. The following illustrates the first example. The blue points represent bird habitats and the red circle represents the amusement park. <image>
Denote a cyclic sequence of size n as an array s such that s_n is adjacent to s_1. The segment s[r, l] where l < r is the concatenation of s[r, n] and s[1, l]. You are given an array a consisting of n integers. Define b as the cyclic sequence obtained from concatenating m copies of a. Note that b has size n β‹… m. You are given an integer k where k = 1 or k is a prime number. Find the number of different segments in b where the sum of elements in the segment is divisible by k. Two segments are considered different if the set of indices of the segments are different. For example, when n = 3 and m = 2, the set of indices for segment s[2, 5] is \{2, 3, 4, 5\}, and for segment s[5, 2] is \{5, 6, 1, 2\}. In particular, the segments s[1, 6], s[2,1], …, s[6, 5] are considered as the same segment. Output the answer modulo 10^9 + 7. Input The first line contains three integers n, m, and k (1 ≀ n, m, k ≀ 2 β‹… 10^5, k = 1 or k is a prime number). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 2 β‹… 10^5). Output Output an integer denoting the number of different segments in b where the sum of elements in the segment is divisible by k, modulo 10^9 + 7. Examples Input 5 1 5 1 2 3 4 3 Output 4 Input 5 1 5 1 2 3 4 5 Output 5 Input 5 4 5 1 2 3 4 5 Output 125 Note In the first example, all valid segments are [1,4], [2, 3], [3, 5], and [4, 2]. In the second example, one of the valid segments is [1, 5].
Mr. Chanek has an integer represented by a string s. Zero or more digits have been erased and are denoted by the character _. There are also zero or more digits marked by the character X, meaning they're the same digit. Mr. Chanek wants to count the number of possible integer s, where s is divisible by 25. Of course, s must not contain any leading zero. He can replace the character _ with any digit. He can also replace the character X with any digit, but it must be the same for every character X. As a note, a leading zero is any 0 digit that comes before the first nonzero digit in a number string in positional notation. For example, 0025 has two leading zeroes. An exception is the integer zero, (0 has no leading zero, but 0000 has three leading zeroes). Input One line containing the string s (1 ≀ |s| ≀ 8). The string s consists of the characters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, _, and X. Output Output an integer denoting the number of possible integer s. Examples Input 25 Output 1 Input _00 Output 9 Input _XX Output 9 Input 0 Output 1 Input 0_25 Output 0 Note In the first example, the only possible s is 25. In the second and third example, s ∈ \{100, 200,300,400,500,600,700,800,900\}. In the fifth example, all possible s will have at least one leading zero.
There is a city park represented as a tree with n attractions as its vertices and n - 1 rails as its edges. The i-th attraction has happiness value a_i. Each rail has a color. It is either black if t_i = 0, or white if t_i = 1. Black trains only operate on a black rail track, and white trains only operate on a white rail track. If you are previously on a black train and want to ride a white train, or you are previously on a white train and want to ride a black train, you need to use 1 ticket. The path of a tour must be a simple path β€” it must not visit an attraction more than once. You do not need a ticket the first time you board a train. You only have k tickets, meaning you can only switch train types at most k times. In particular, you do not need a ticket to go through a path consisting of one rail color. Define f(u, v) as the sum of happiness values of the attractions in the tour (u, v), which is a simple path that starts at the u-th attraction and ends at the v-th attraction. Find the sum of f(u,v) for all valid tours (u, v) (1 ≀ u ≀ v ≀ n) that does not need more than k tickets, modulo 10^9 + 7. Input The first line contains two integers n and k (2 ≀ n ≀ 2 β‹… 10^5, 0 ≀ k ≀ n-1) β€” the number of attractions in the city park and the number of tickets you have. The second line contains n integers a_1, a_2,…, a_n (0 ≀ a_i ≀ 10^9) β€” the happiness value of each attraction. The i-th of the next n - 1 lines contains three integers u_i, v_i, and t_i (1 ≀ u_i, v_i ≀ n, 0 ≀ t_i ≀ 1) β€” an edge between vertices u_i and v_i with color t_i. The given edges form a tree. Output Output an integer denoting the total happiness value for all valid tours (u, v) (1 ≀ u ≀ v ≀ n), modulo 10^9 + 7. Examples Input 5 0 1 3 2 6 4 1 2 1 1 4 0 3 2 1 2 5 0 Output 45 Input 3 1 1 1 1 1 2 1 3 2 0 Output 10
Mr. Chanek opened a letter from his fellow, who is currently studying at Singanesia. Here is what it says. Define an array b (0 ≀ b_i < k) with n integers. While there exists a pair (i, j) such that b_i β‰  b_j, do the following operation: * Randomly pick a number i satisfying 0 ≀ i < n. Note that each number i has a probability of 1/n to be picked. * Randomly Pick a number j satisfying 0 ≀ j < k. * Change the value of b_i to j. It is possible for b_i to be changed to the same value. Denote f(b) as the expected number of operations done to b until all elements of b are equal. You are given two integers n and k, and an array a (-1 ≀ a_i < k) of n integers. For every index i with a_i = -1, replace a_i with a random number j satisfying 0 ≀ j < k. Let c be the number of occurrences of -1 in a. There are k^c possibilites of a after the replacement, each with equal probability of being the final array. Find the expected value of f(a) modulo 10^9 + 7. Formally, let M = 10^9 + 7. It can be shown that the answer can be expressed as an irreducible fraction p/q, where p and q are integers and q not ≑ 0 \pmod{M}. Output the integer equal to p β‹… q^{-1} mod M. In other words, output such an integer x that 0 ≀ x < M and x β‹… q ≑ p \pmod{M}. After reading the letter, Mr. Chanek gave the task to you. Solve it for the sake of their friendship! Input The first line contains two integers n and k (2 ≀ n ≀ 10^5, 2 ≀ k ≀ 10^9). The second line contains n integers a_1, a_2, …, a_n (-1 ≀ a_i < k). Output Output an integer denoting the expected value of f(a) modulo 10^9 + 7. Examples Input 2 2 0 1 Output 2 Input 2 2 0 -1 Output 1 Input 3 3 0 1 1 Output 12 Input 3 3 -1 -1 -1 Output 11 Input 10 9 -1 0 -1 1 1 2 2 3 3 3 Output 652419213
Mr. Chanek has an array a of n integers. The prettiness value of a is denoted as: $$$βˆ‘_{i=1}^{n} {βˆ‘_{j=1}^{n} {\gcd(a_i, a_j) β‹… \gcd(i, j)}}$$$ where \gcd(x, y) denotes the greatest common divisor (GCD) of integers x and y. In other words, the prettiness value of an array a is the total sum of \gcd(a_i, a_j) β‹… \gcd(i, j) for all pairs (i, j). Help Mr. Chanek find the prettiness value of a, and output the result modulo 10^9 + 7! Input The first line contains an integer n (2 ≀ n ≀ 10^5). The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^5). Output Output an integer denoting the prettiness value of a modulo 10^9 + 7. Example Input 5 3 6 2 1 4 Output 77
The Winter holiday will be here soon. Mr. Chanek wants to decorate his house's wall with ornaments. The wall can be represented as a binary string a of length n. His favorite nephew has another binary string b of length m (m ≀ n). Mr. Chanek's nephew loves the non-negative integer k. His nephew wants exactly k occurrences of b as substrings in a. However, Mr. Chanek does not know the value of k. So, for each k (0 ≀ k ≀ n - m + 1), find the minimum number of elements in a that have to be changed such that there are exactly k occurrences of b in a. A string s occurs exactly k times in t if there are exactly k different pairs (p,q) such that we can obtain s by deleting p characters from the beginning and q characters from the end of t. Input The first line contains two integers n and m (1 ≀ m ≀ n ≀ 500) β€” size of the binary string a and b respectively. The second line contains a binary string a of length n. The third line contains a binary string b of length m. Output Output n - m + 2 integers β€” the (k+1)-th integer denotes the minimal number of elements in a that have to be changed so there are exactly k occurrences of b as a substring in a. Example Input 9 3 100101011 101 Output 1 1 0 1 6 -1 -1 -1 Note For k = 0, to make the string a have no occurrence of 101, you can do one character change as follows. 100101011 β†’ 100100011 For k = 1, you can also change a single character. 100101011 β†’ 100001011 For k = 2, no changes are needed.
Chanek Jones is back, helping his long-lost relative Indiana Jones, to find a secret treasure in a maze buried below a desert full of illusions. The map of the labyrinth forms a tree with n rooms numbered from 1 to n and n - 1 tunnels connecting them such that it is possible to travel between each pair of rooms through several tunnels. The i-th room (1 ≀ i ≀ n) has a_i illusion rate. To go from the x-th room to the y-th room, there must exist a tunnel between x and y, and it takes max(|a_x + a_y|, |a_x - a_y|) energy. |z| denotes the absolute value of z. To prevent grave robbers, the maze can change the illusion rate of any room in it. Chanek and Indiana would ask q queries. There are two types of queries to be done: * 1\ u\ c β€” The illusion rate of the x-th room is changed to c (1 ≀ u ≀ n, 0 ≀ |c| ≀ 10^9). * 2\ u\ v β€” Chanek and Indiana ask you the minimum sum of energy needed to take the secret treasure at room v if they are initially at room u (1 ≀ u, v ≀ n). Help them, so you can get a portion of the treasure! Input The first line contains two integers n and q (2 ≀ n ≀ 10^5, 1 ≀ q ≀ 10^5) β€” the number of rooms in the maze and the number of queries. The second line contains n integers a_1, a_2, …, a_n (0 ≀ |a_i| ≀ 10^9) β€” inital illusion rate of each room. The i-th of the next n-1 lines contains two integers s_i and t_i (1 ≀ s_i, t_i ≀ n), meaning there is a tunnel connecting s_i-th room and t_i-th room. The given edges form a tree. The next q lines contain the query as described. The given queries are valid. Output For each type 2 query, output a line containing an integer β€” the minimum sum of energy needed for Chanek and Indiana to take the secret treasure. Example Input 6 4 10 -9 2 -1 4 -6 1 5 5 4 5 6 6 2 6 3 2 1 2 1 1 -3 2 1 2 2 3 3 Output 39 32 0 Note <image> In the first query, their movement from the 1-st to the 2-nd room is as follows. * 1 β†’ 5 β€” takes max(|10 + 4|, |10 - 4|) = 14 energy. * 5 β†’ 6 β€” takes max(|4 + (-6)|, |4 - (-6)|) = 10 energy. * 6 β†’ 2 β€” takes max(|-6 + (-9)|, |-6 - (-9)|) = 15 energy. In total, it takes 39 energy. In the second query, the illusion rate of the 1-st room changes from 10 to -3. In the third query, their movement from the 1-st to the 2-nd room is as follows. * 1 β†’ 5 β€” takes max(|-3 + 4|, |-3 - 4|) = 7 energy. * 5 β†’ 6 β€” takes max(|4 + (-6)|, |4 - (-6)|) = 10 energy. * 6 β†’ 2 β€” takes max(|-6 + (-9)|, |-6 - (-9)|) = 15 energy. Now, it takes 32 energy.
Mr. Chanek has a new game called Dropping Balls. Initially, Mr. Chanek has a grid a of size n Γ— m Each cell (x,y) contains an integer a_{x,y} denoting the direction of how the ball will move. * a_{x,y}=1 β€” the ball will move to the right (the next cell is (x, y + 1)); * a_{x,y}=2 β€” the ball will move to the bottom (the next cell is (x + 1, y)); * a_{x,y}=3 β€” the ball will move to the left (the next cell is (x, y - 1)). Every time a ball leaves a cell (x,y), the integer a_{x,y} will change to 2. Mr. Chanek will drop k balls sequentially, each starting from the first row, and on the c_1, c_2, ..., c_k-th (1 ≀ c_i ≀ m) columns. Determine in which column each ball will end up in (position of the ball after leaving the grid). Input The first line contains three integers n, m, and k (1 ≀ n, m ≀ 1000, 1 ≀ k ≀ 10^5) β€” the size of the grid and the number of balls dropped by Mr. Chanek. The i-th of the next n lines contains m integers a_{i,1},a_{i,2},…,a_{i,m} (1 ≀ a_{i,j} ≀ 3). It will satisfy a_{i, 1} β‰  3 and a_{i, m} β‰  1. The next line contains k integers c_1, c_2, …, c_k (1 ≀ c_i ≀ m) β€” the balls' column positions dropped by Mr. Chanek sequentially. Output Output k integers β€” the i-th integer denoting the column where the i-th ball will end. Examples Input 5 5 3 1 2 3 3 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 2 1 Output 2 2 1 Input 1 2 2 1 3 1 2 Output 1 2 Note In the first example, the first ball will drop as follows. Note that the cell (1, 1) will change direction to the bottom direction. <image> The second and third balls will drop as follows. <image> All balls will be dropped from the first row and on the c_1, c_2, ..., c_k-th columns respectively. A ball will stop dropping once it leaves the grid.
Mr. Chanek wants to knit a batik, a traditional cloth from Indonesia. The cloth forms a grid a with size n Γ— m. There are k colors, and each cell in the grid can be one of the k colors. Define a sub-rectangle as an ordered pair of two cells ((x_1, y_1), (x_2, y_2)), denoting the top-left cell and bottom-right cell (inclusively) of a sub-rectangle in a. Two sub-rectangles ((x_1, y_1), (x_2, y_2)) and ((x_3, y_3), (x_4, y_4)) have the same pattern if and only if the following holds: * they have the same width (x_2 - x_1 = x_4 - x_3); * they have the same height (y_2 - y_1 = y_4 - y_3); * for every pair (i, j) where 0 ≀ i ≀ x_2 - x_1 and 0 ≀ j ≀ y_2 - y_1, the color of cells (x_1 + i, y_1 + j) and (x_3 + i, y_3 + j) are equal. Count the number of possible batik color combinations, such that the subrectangles ((a_x, a_y),(a_x + r - 1, a_y + c - 1)) and ((b_x, b_y),(b_x + r - 1, b_y + c - 1)) have the same pattern. Output the answer modulo 10^9 + 7. Input The first line contains five integers n, m, k, r, and c (1 ≀ n, m ≀ 10^9, 1 ≀ k ≀ 10^9, 1 ≀ r ≀ min(10^6, n), 1 ≀ c ≀ min(10^6, m)) β€” the size of the batik, the number of colors, and size of the sub-rectangle. The second line contains four integers a_x, a_y, b_x, and b_y (1 ≀ a_x, b_x ≀ n, 1 ≀ a_y, b_y ≀ m) β€” the top-left corners of the first and second sub-rectangle. Both of the sub-rectangles given are inside the grid (1 ≀ a_x + r - 1, b_x + r - 1 ≀ n, 1 ≀ a_y + c - 1, b_y + c - 1 ≀ m). Output Output an integer denoting the number of possible batik color combinations modulo 10^9 + 7. Examples Input 3 3 2 2 2 1 1 2 2 Output 32 Input 4 5 170845 2 2 1 4 3 1 Output 756680455 Note The following are all 32 possible color combinations in the first example. <image>
Mr. Chanek gives you a sequence a indexed from 1 to n. Define f(a) as the number of indices where a_i = i. You can pick an element from the current sequence and remove it, then concatenate the remaining elements together. For example, if you remove the 3-rd element from the sequence [4, 2, 3, 1], the resulting sequence will be [4, 2, 1]. You want to remove some elements from a in order to maximize f(a), using zero or more operations. Find the largest possible f(a). Input The first line contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the initial length of the sequence. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 2 β‹… 10^5) β€” the initial sequence a. Output Output an integer denoting the largest f(a) that can be obtained by doing zero or more operations. Examples Input 7 2 1 4 2 5 3 7 Output 3 Input 4 4 2 3 1 Output 2 Note In the first example, f(A) = 3 by doing the following operations. [2,1,4,2,5,3,7] β†’ [2,1,2,5,3,7] β†’ [1,2,5,3,7] β†’ [1,2,5,3] β†’ [1,2,3] In the second example, f(A) = 2 and no additional operation is needed.
Mr. Chanek's city can be represented as a plane. He wants to build a housing complex in the city. There are some telephone poles on the plane, which is represented by a grid a of size (n + 1) Γ— (m + 1). There is a telephone pole at (x, y) if a_{x, y} = 1. For each point (x, y), define S(x, y) as the square of the Euclidean distance between the nearest pole and (x, y). Formally, the square of the Euclidean distance between two points (x_1, y_1) and (x_2, y_2) is (x_2 - x_1)^2 + (y_2 - y_1)^2. To optimize the building plan, the project supervisor asks you the sum of all S(x, y) for each 0 ≀ x ≀ n and 0 ≀ y ≀ m. Help him by finding the value of βˆ‘_{x=0}^{n} {βˆ‘_{y=0}^{m} {S(x, y)}}. Input The first line contains two integers n and m (0 ≀ n, m < 2000) β€” the size of the grid. Then (n + 1) lines follow, each containing (m + 1) integers a_{i, j} (0 ≀ a_{i, j} ≀ 1) β€” the grid denoting the positions of telephone poles in the plane. There is at least one telephone pole in the given grid. Output Output an integer denoting the value of βˆ‘_{x=0}^{n} {βˆ‘_{y=0}^{m} {S(x, y)}}. Examples Input 2 2 101 000 000 Output 18 Input 5 4 10010 00000 01000 00001 00100 00010 Output 36 Note <image> In the first example, the nearest telephone pole for the points (0,0), (1,0), (2,0), (0,1), (1,1), and (2,1) is at (0, 0). While the nearest telephone pole for the points (0, 2), (1,2), and (2,2) is at (0, 2). Thus, βˆ‘_{x=0}^{n} {βˆ‘_{y=0}^{m} {S(x, y)}} = (0 + 1 + 4) + (1 + 2 + 5) + (0 + 1 + 4) = 18.
Casimir has a string s which consists of capital Latin letters 'A', 'B', and 'C' only. Each turn he can choose to do one of the two following actions: * he can either erase exactly one letter 'A' and exactly one letter 'B' from arbitrary places of the string (these letters don't have to be adjacent); * or he can erase exactly one letter 'B' and exactly one letter 'C' from arbitrary places in the string (these letters don't have to be adjacent). Therefore, each turn the length of the string is decreased exactly by 2. All turns are independent so for each turn, Casimir can choose any of two possible actions. For example, with s = "ABCABC" he can obtain a string s = "ACBC" in one turn (by erasing the first occurrence of 'B' and the second occurrence of 'A'). There are also many other options for a turn aside from this particular example. For a given string s determine whether there is a sequence of actions leading to an empty string. In other words, Casimir's goal is to erase all letters from the string. Is there a way to do this? Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case is described by one string s, for which you need to determine if it can be fully erased by some sequence of turns. The string s consists of capital letters 'A', 'B', 'C' and has a length from 1 to 50 letters, inclusive. Output Print t lines, each line containing the answer to the corresponding test case. The answer to a test case should be YES if there is a way to fully erase the corresponding string and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answers). Example Input 6 ABACAB ABBA AC ABC CABCBB BCBCBCBCBCBCBCBC Output NO YES NO NO YES YES
The new generation external memory contains an array of integers a[1 … n] = [a_1, a_2, …, a_n]. This type of memory does not support changing the value of an arbitrary element. Instead, it allows you to cut out any segment of the given array, cyclically shift (rotate) it by any offset and insert it back into the same place. Technically, each cyclic shift consists of two consecutive actions: 1. You may select arbitrary indices l and r (1 ≀ l < r ≀ n) as the boundaries of the segment. 2. Then you replace the segment a[l … r] with it's cyclic shift to the left by an arbitrary offset d. The concept of a cyclic shift can be also explained by following relations: the sequence [1, 4, 1, 3] is a cyclic shift of the sequence [3, 1, 4, 1] to the left by the offset 1 and the sequence [4, 1, 3, 1] is a cyclic shift of the sequence [3, 1, 4, 1] to the left by the offset 2. For example, if a = [1, \color{blue}{3, 2, 8}, 5], then choosing l = 2, r = 4 and d = 2 yields a segment a[2 … 4] = [3, 2, 8]. This segment is then shifted by the offset d = 2 to the left, and you get a segment [8, 3, 2] which then takes the place of of the original elements of the segment. In the end you get a = [1, \color{blue}{8, 3, 2}, 5]. Sort the given array a using no more than n cyclic shifts of any of its segments. Note that you don't need to minimize the number of cyclic shifts. Any method that requires n or less cyclic shifts will be accepted. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The next 2t lines contain the descriptions of the test cases. The first line of each test case description contains an integer n (2 ≀ n ≀ 50) β€” the length of the array. The second line consists of space-separated elements of the array a_i (-10^9 ≀ a_i ≀ 10^9). Elements of array a may repeat and don't have to be unique. Output Print t answers to all input test cases. The first line of the answer of each test case should contain an integer k (0 ≀ k ≀ n) β€” the number of actions to sort the array. The next k lines should contain descriptions of the actions formatted as "l r d" (without quotes) where l and r (1 ≀ l < r ≀ n) are the boundaries of the segment being shifted, while d (1 ≀ d ≀ r - l) is the offset value. Please remember that only the cyclic shifts to the left are considered so the chosen segment will be shifted by the offset d to the to the left. Note that you are not required to find the minimum number of cyclic shifts needed for sorting. Any sorting method where the number of shifts does not exceed n will be accepted. If the given array a is already sorted, one of the possible answers is k = 0 and an empty sequence of cyclic shifts. If there are several possible answers, you may print any of them. Example Input 4 2 2 1 3 1 2 1 4 2 4 1 3 5 2 5 1 4 3 Output 1 1 2 1 1 1 3 2 3 2 4 1 2 3 1 1 3 2 4 2 4 2 1 5 3 1 2 1 1 3 1 Note Explanation of the fourth data set in the example: 1. The segment a[2 … 4] is selected and is shifted to the left by 2: [2, \color{blue}{5, 1, 4}, 3] \longrightarrow [2, \color{blue}{4, 5, 1}, 3] 2. The segment a[1 … 5] is then selected and is shifted to the left by 3: [\color{blue}{2, 4, 5, 1, 3}] \longrightarrow [\color{blue}{1, 3, 2, 4, 5}] 3. After that the segment a[1 … 2] is selected and is shifted to the left by 1: [\color{blue}{1, 3}, 2, 4, 5] \longrightarrow [\color{blue}{3, 1}, 2, 4, 5] 4. And in the end the segment a[1 … 3] is selected and is shifted to the left by 1: [\color{blue}{3, 1, 2}, 4, 5] \longrightarrow [\color{blue}{1, 2, 3}, 4, 5]
Casimir has a rectangular piece of paper with a checkered field of size n Γ— m. Initially, all cells of the field are white. Let us denote the cell with coordinates i vertically and j horizontally by (i, j). The upper left cell will be referred to as (1, 1) and the lower right cell as (n, m). Casimir draws ticks of different sizes on the field. A tick of size d (d > 0) with its center in cell (i, j) is drawn as follows: 1. First, the center cell (i, j) is painted black. 2. Then exactly d cells on the top-left diagonally to the center and exactly d cells on the top-right diagonally to the center are also painted black. 3. That is all the cells with coordinates (i - h, j Β± h) for all h between 0 and d are painted. In particular, a tick consists of 2d + 1 black cells. An already painted cell will remain black if painted again. Below you can find an example of the 4 Γ— 9 box, with two ticks of sizes 2 and 3. <image> You are given a description of a checkered field of size n Γ— m. Casimir claims that this field came about after he drew some (possibly 0) ticks on it. The ticks could be of different sizes, but the size of each tick is at least k (that is, d β‰₯ k for all the ticks). Determine whether this field can indeed be obtained by drawing some (possibly none) ticks of sizes d β‰₯ k or not. Input The first line contains an integer t (1 ≀ t ≀ 100) β€” the number test cases. The following lines contain the descriptions of the test cases. The first line of the test case description contains the integers n, m, and k (1 ≀ k ≀ n ≀ 10; 1 ≀ m ≀ 19) β€” the field size and the minimum size of the ticks that Casimir drew. The following n lines describe the field: each line consists of m characters either being '.' if the corresponding cell is not yet painted or '*' otherwise. Output Print t lines, each line containing the answer to the corresponding test case. The answer to a test case should be YES if the given field can be obtained by drawing ticks of at least the given size and NO otherwise. You may print every letter in any case you want (so, for example, the strings yEs, yes, Yes, and YES will all be recognized as positive answers). Example Input 8 2 3 1 *.* ... 4 9 2 *.*.*...* .*.*...*. ..*.*.*.. .....*... 4 4 1 *.*. **** .**. .... 5 5 1 ..... *...* .*.*. ..*.* ...*. 5 5 2 ..... *...* .*.*. ..*.* ...*. 4 7 1 *.....* .....*. ..*.*.. ...*... 3 3 1 *** *** *** 3 5 1 *...* .***. .**.. Output NO YES YES YES NO NO NO NO Note The first sample test case consists of two asterisks neither of which can be independent ticks since ticks of size 0 don't exist. The second sample test case is already described in the statement (check the picture in the statement). This field can be obtained by drawing ticks of sizes 2 and 3, as shown in the figure. The field in the third sample test case corresponds to three ticks of size 1. Their center cells are marked with \color{blue}{blue}, \color{red}{red} and \color{green}{green} colors: *.*. --- *\color{blue}{*}** .\color{green}{*}\color{red}{*}. .... The field in the fourth sample test case could have been obtained by drawing two ticks of sizes 1 and 2. Their vertices are marked below with \color{blue}{blue} and \color{red}{red} colors respectively: ..... --- *...* .*.*. ..\color{red}{*}.* ...\color{blue}{*}. The field in the fifth sample test case can not be obtained because k = 2, and the last asterisk in the fourth row from the top with coordinates (4, 5) can only be a part of a tick of size 1. The field in the sixth sample test case can not be obtained because the top left asterisk (1, 1) can't be an independent tick, since the sizes of the ticks must be positive, and cannot be part of a tick with the center cell in the last row, since it is separated from it by a gap (a point, '.') in (2, 2). In the seventh sample test case, similarly, the field can not be obtained by the described process because the asterisks with coordinates (1, 2) (second cell in the first row), (3, 1) and (3, 3) (leftmost and rightmost cells in the bottom) can not be parts of any ticks.
An important meeting is to be held and there are exactly n people invited. At any moment, any two people can step back and talk in private. The same two people can talk several (as many as they want) times per meeting. Each person has limited sociability. The sociability of the i-th person is a non-negative integer a_i. This means that after exactly a_i talks this person leaves the meeting (and does not talk to anyone else anymore). If a_i = 0, the i-th person leaves the meeting immediately after it starts. A meeting is considered most productive if the maximum possible number of talks took place during it. You are given an array of sociability a, determine which people should talk to each other so that the total number of talks is as large as possible. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The next 2t lines contain descriptions of the test cases. The first line of each test case description contains an integer n (2 ≀ n ≀ 2 β‹… 10^5) β€”the number of people in the meeting. The second line consists of n space-separated integers a_1, a_2, ..., a_n (0 ≀ a_i ≀ 2 β‹… 10^5) β€” the sociability parameters of all people. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. It is also guaranteed that the sum of all a_i (over all test cases and all i) does not exceed 2 β‹… 10^5. Output Print t answers to all test cases. On the first line of each answer print the number k β€” the maximum number of talks possible in a meeting. On each of the next k lines print two integers i and j (1 ≀ i, j ≀ n and i β‰  j) β€” the numbers of people who will have another talk. If there are several possible answers, you may print any of them. Example Input 8 2 2 3 3 1 2 3 4 1 2 3 4 3 0 0 2 2 6 2 3 0 0 2 5 8 2 0 1 1 5 0 1 0 0 6 Output 2 1 2 1 2 3 1 3 2 3 2 3 5 1 3 2 4 2 4 3 4 3 4 0 2 1 2 1 2 0 4 1 2 1 5 1 4 1 2 1 5 2
In fact, the problems E1 and E2 do not have much in common. You should probably think of them as two separate problems. You are given an integer array a[1 … n] = [a_1, a_2, …, a_n]. Let us consider an empty [deque](https://tinyurl.com/pfeucbux) (double-ended queue). A deque is a data structure that supports adding elements to both the beginning and the end. So, if there are elements [3, 4, 4] currently in the deque, adding an element 1 to the beginning will produce the sequence [\color{red}{1}, 3, 4, 4], and adding the same element to the end will produce [3, 4, 4, \color{red}{1}]. The elements of the array are sequentially added to the initially empty deque, starting with a_1 and finishing with a_n. Before adding each element to the deque, you may choose whether to add it to the beginning or to the end. For example, if we consider an array a = [3, 7, 5, 5], one of the possible sequences of actions looks like this: 1. | add 3 to the beginning of the deque: | deque has a sequence [\color{red}{3}] in it; ---|---|--- 2. | add 7 to the end of the deque: | deque has a sequence [3, \color{red}{7}] in it; 3. | add 5 to the end of the deque: | deque has a sequence [3, 7, \color{red}{5}] in it; 4. | add 5 to the beginning of the deque: | deque has a sequence [\color{red}{5}, 3, 7, 5] in it; Find the minimal possible number of inversions in the deque after the whole array is processed. An inversion in sequence d is a pair of indices (i, j) such that i < j and d_i > d_j. For example, the array d = [5, 3, 7, 5] has exactly two inversions β€” (1, 2) and (3, 4), since d_1 = 5 > 3 = d_2 and d_3 = 7 > 5 = d_4. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The next 2t lines contain descriptions of the test cases. The first line of each test case description contains an integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” array size. The second line of the description contains n space-separated integers a_i (-10^9 ≀ a_i ≀ 10^9) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output Print t lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer β€” the minimal possible number of inversions in the deque after executing the described algorithm. Example Input 6 4 3 7 5 5 3 3 2 1 3 3 1 2 4 -1 2 2 -1 4 4 5 1 3 5 1 3 1 3 2 Output 2 0 1 0 1 2 Note One of the ways to get the sequence [5, 3, 7, 5] in the deque, containing only two inversions, from the initial array [3, 7, 5, 5] (the first sample test case) is described in the problem statement. Also, in this example, you could get the answer of two inversions by simply putting each element of the original array at the end of the deque. In this case, the original sequence [3, 7, 5, 5], also containing exactly two inversions, will be in the deque as-is.
You are given an array a[0 … n - 1] = [a_0, a_1, …, a_{n - 1}] of zeroes and ones only. Note that in this problem, unlike the others, the array indexes are numbered from zero, not from one. In one step, the array a is replaced by another array of length n according to the following rules: 1. First, a new array a^{β†’ d} is defined as a cyclic shift of the array a to the right by d cells. The elements of this array can be defined as a^{β†’ d}_i = a_{(i + n - d) mod n}, where (i + n - d) mod n is the remainder of integer division of i + n - d by n. It means that the whole array a^{β†’ d} can be represented as a sequence $$$a^{β†’ d} = [a_{n - d}, a_{n - d + 1}, …, a_{n - 1}, a_0, a_1, …, a_{n - d - 1}]$$$ 2. Then each element of the array a_i is replaced by a_i \& a^{β†’ d}_i, where \& is a logical "AND" operator. For example, if a = [0, 0, 1, 1] and d = 1, then a^{β†’ d} = [1, 0, 0, 1] and the value of a after the first step will be [0 \& 1, 0 \& 0, 1 \& 0, 1 \& 1], that is [0, 0, 0, 1]. The process ends when the array stops changing. For a given array a, determine whether it will consist of only zeros at the end of the process. If yes, also find the number of steps the process will take before it finishes. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The next 2t lines contain descriptions of the test cases. The first line of each test case description contains two integers: n (1 ≀ n ≀ 10^6) β€” array size and d (1 ≀ d ≀ n) β€” cyclic shift offset. The second line of the description contains n space-separated integers a_i (0 ≀ a_i ≀ 1) β€” elements of the array. It is guaranteed that the sum of n over all test cases does not exceed 10^6. Output Print t lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer β€” the number of steps after which the array will contain only zeros for the first time. If there are still elements equal to 1 in the array after the end of the process, print -1. Example Input 5 2 1 0 1 3 2 0 1 0 5 2 1 1 0 1 0 4 2 0 1 0 1 1 1 0 Output 1 1 3 -1 0 Note In the third sample test case the array will change as follows: 1. At the beginning a = [1, 1, 0, 1, 0], and a^{β†’ 2} = [1, 0, 1, 1, 0]. Their element-by-element "AND" is equal to $$$[1 \& 1, 1 \& 0, 0 \& 1, 1 \& 1, 0 \& 0] = [1, 0, 0, 1, 0]$$$ 2. Now a = [1, 0, 0, 1, 0], then a^{β†’ 2} = [1, 0, 1, 0, 0]. Their element-by-element "AND" equals to $$$[1 \& 1, 0 \& 0, 0 \& 1, 1 \& 0, 0 \& 0] = [1, 0, 0, 0, 0]$$$ 3. And finally, when a = [1, 0, 0, 0, 0] we get a^{β†’ 2} = [0, 0, 1, 0, 0]. Their element-by-element "AND" equals to $$$[1 \& 0, 0 \& 0, 0 \& 1, 0 \& 0, 0 \& 0] = [0, 0, 0, 0, 0]$$$ Thus, the answer is 3 steps. In the fourth sample test case, the array will not change as it shifts by 2 to the right, so each element will be calculated as 0 \& 0 or 1 \& 1 thus not changing its value. So the answer is -1, the array will never contain only zeros.
You are given n lengths of segments that need to be placed on an infinite axis with coordinates. The first segment is placed on the axis so that one of its endpoints lies at the point with coordinate 0. Let's call this endpoint the "start" of the first segment and let's call its "end" as that endpoint that is not the start. The "start" of each following segment must coincide with the "end" of the previous one. Thus, if the length of the next segment is d and the "end" of the previous one has the coordinate x, the segment can be placed either on the coordinates [x-d, x], and then the coordinate of its "end" is x - d, or on the coordinates [x, x+d], in which case its "end" coordinate is x + d. The total coverage of the axis by these segments is defined as their overall union which is basically the set of points covered by at least one of the segments. It's easy to show that the coverage will also be a segment on the axis. Determine the minimal possible length of the coverage that can be obtained by placing all the segments on the axis without changing their order. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The next 2t lines contain descriptions of the test cases. The first line of each test case description contains an integer n (1 ≀ n ≀ 10^4) β€” the number of segments. The second line of the description contains n space-separated integers a_i (1 ≀ a_i ≀ 1000) β€” lengths of the segments in the same order they should be placed on the axis. It is guaranteed that the sum of n over all test cases does not exceed 10^4. Output Print t lines, each line containing the answer to the corresponding test case. The answer to a test case should be a single integer β€” the minimal possible length of the axis coverage. Example Input 6 2 1 3 3 1 2 3 4 6 2 3 9 4 6 8 4 5 7 1 2 4 6 7 7 3 8 8 6 5 1 2 2 3 6 Output 3 3 9 9 7 8 Note In the third sample test case the segments should be arranged as follows: [0, 6] β†’ [4, 6] β†’ [4, 7] β†’ [-2, 7]. As you can see, the last segment [-2, 7] covers all the previous ones, and the total length of coverage is 9. In the fourth sample test case the segments should be arranged as [0, 6] β†’ [-2, 6] β†’ [-2, 2] β†’ [2, 7]. The union of these segments also occupies the area [-2, 7] and has the length of 9.
CQXYM found a rectangle A of size n Γ— m. There are n rows and m columns of blocks. Each block of the rectangle is an obsidian block or empty. CQXYM can change an obsidian block to an empty block or an empty block to an obsidian block in one operation. A rectangle M size of a Γ— b is called a portal if and only if it satisfies the following conditions: * a β‰₯ 5,b β‰₯ 4. * For all 1 < x < a, blocks M_{x,1} and M_{x,b} are obsidian blocks. * For all 1 < x < b, blocks M_{1,x} and M_{a,x} are obsidian blocks. * For all 1<x<a,1<y<b, block M_{x,y} is an empty block. * M_{1, 1}, M_{1, b}, M_{a, 1}, M_{a, b} can be any type. Note that the there must be a rows and b columns, not b rows and a columns. Note that corners can be any type CQXYM wants to know the minimum number of operations he needs to make at least one sub-rectangle a portal. Input The first line contains an integer t (t β‰₯ 1), which is the number of test cases. For each test case, the first line contains two integers n and m (5 ≀ n ≀ 400, 4 ≀ m ≀ 400). Then n lines follow, each line contains m characters 0 or 1. If the j-th character of i-th line is 0, block A_{i,j} is an empty block. Otherwise, block A_{i,j} is an obsidian block. It is guaranteed that the sum of n over all test cases does not exceed 400. It is guaranteed that the sum of m over all test cases does not exceed 400. Output Output t answers, and each answer in a line. Examples Input 1 5 4 1000 0000 0110 0000 0001 Output 12 Input 1 9 9 001010001 101110100 000010011 100000001 101010101 110001111 000001111 111100000 000110000 Output 5 Note In the first test case, the final portal is like this: 1110 1001 1001 1001 0111
Let c_1, c_2, …, c_n be a permutation of integers 1, 2, …, n. Consider all subsegments of this permutation containing an integer x. Given an integer m, we call the integer x good if there are exactly m different values of maximum on these subsegments. Cirno is studying mathematics, and the teacher asks her to count the number of permutations of length n with exactly k good numbers. Unfortunately, Cirno isn't good at mathematics, and she can't answer this question. Therefore, she asks you for help. Since the answer may be very big, you only need to tell her the number of permutations modulo p. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array). A sequence a is a subsegment of a sequence b if a can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains four integers n, m, k, p (1 ≀ n ≀ 100, 1 ≀ m ≀ n, 1 ≀ k ≀ n, 1 ≀ p ≀ 10^9). Output Output the number of permutations modulo p. Examples Input 4 3 2 10007 Output 4 Input 6 4 1 769626776 Output 472 Input 66 11 9 786747482 Output 206331312 Input 99 30 18 650457567 Output 77365367 Note In the first test case, there are four permutations: [1, 3, 2, 4], [2, 3, 1, 4], [4, 1, 3, 2] and [4, 2, 3, 1]. Take permutation [1, 3, 2, 4] as an example: For number 1, all subsegments containing it are: [1], [1, 3], [1, 3, 2] and [1, 3, 2, 4], and there're three different maxima 1, 3 and 4. Similarly, for number 3, there're two different maxima 3 and 4. For number 2, there're three different maxima 2, 3 and 4. And for number 4, there're only one, that is 4 itself.
Kawasiro Nitori is excellent in engineering. Thus she has been appointed to help maintain trains. There are n models of trains, and Nitori's department will only have at most one train of each model at any moment. In the beginning, there are no trains, at each of the following m days, one train will be added, or one train will be removed. When a train of model i is added at day t, it works for x_i days (day t inclusive), then it is in maintenance for y_i days, then in work for x_i days again, and so on until it is removed. In order to make management easier, Nitori wants you to help her calculate how many trains are in maintenance in each day. On a day a train is removed, it is not counted as in maintenance. Input The first line contains two integers n, m (1 ≀ n,m ≀ 2 β‹… 10^5). The i-th of the next n lines contains two integers x_i,y_i (1 ≀ x_i,y_i ≀ 10^9). Each of the next m lines contains two integers op, k (1 ≀ k ≀ n, op = 1 or op = 2). If op=1, it means this day's a train of model k is added, otherwise the train of model k is removed. It is guaranteed that when a train of model x is added, there is no train of the same model in the department, and when a train of model x is removed, there is such a train in the department. Output Print m lines, The i-th of these lines contains one integers, denoting the number of trains in maintenance in the i-th day. Examples Input 3 4 10 15 12 10 1 1 1 3 1 1 2 1 2 3 Output 0 1 0 0 Input 5 4 1 1 10000000 100000000 998244353 1 2 1 1 2 1 5 2 5 1 5 1 1 Output 0 0 0 1 Note Consider the first example: The first day: Nitori adds a train of model 3. Only a train of model 3 is running and no train is in maintenance. The second day: Nitori adds a train of model 1. A train of model 1 is running and a train of model 3 is in maintenance. The third day: Nitori removes a train of model 1. The situation is the same as the first day. The fourth day: Nitori removes a train of model 3. There are no trains at all.
Alice has an integer sequence a of length n and all elements are different. She will choose a subsequence of a of length m, and defines the value of a subsequence a_{b_1},a_{b_2},…,a_{b_m} as $$$βˆ‘_{i = 1}^m (m β‹… a_{b_i}) - βˆ‘_{i = 1}^m βˆ‘_{j = 1}^m f(min(b_i, b_j), max(b_i, b_j)), where f(i, j) denotes \min(a_i, a_{i + 1}, \ldots, a_j)$$$. Alice wants you to help her to maximize the value of the subsequence she choose. A sequence s is a subsequence of a sequence t if s can be obtained from t by deletion of several (possibly, zero or all) elements. Input The first line contains two integers n and m (1 ≀ m ≀ n ≀ 4000). The second line contains n distinct integers a_1, a_2, …, a_n (1 ≀ a_i < 2^{31}). Output Print the maximal value Alice can get. Examples Input 6 4 15 2 18 12 13 4 Output 100 Input 11 5 9 3 7 1 8 12 10 20 15 18 5 Output 176 Input 1 1 114514 Output 0 Input 2 1 666 888 Output 0 Note In the first example, Alice can choose the subsequence [15, 2, 18, 13], which has the value 4 β‹… (15 + 2 + 18 + 13) - (15 + 2 + 2 + 2) - (2 + 2 + 2 + 2) - (2 + 2 + 18 + 12) - (2 + 2 + 12 + 13) = 100. In the second example, there are a variety of subsequences with value 176, and one of them is [9, 7, 12, 20, 18].
Because the railway system in Gensokyo is often congested, as an enthusiastic engineer, Kawasiro Nitori plans to construct more railway to ease the congestion. There are n stations numbered from 1 to n and m two-way railways in Gensokyo. Every two-way railway connects two different stations and has a positive integer length d. No two two-way railways connect the same two stations. Besides, it is possible to travel from any station to any other using those railways. Among these n stations, station 1 is the main station. You can get to any station from any other station using only two-way railways. Because of the technological limitation, Nitori can only construct one-way railways, whose length can be arbitrary positive integer. Constructing a one-way railway from station u will costs w_u units of resources, no matter where the railway ends. To ease the congestion, Nitori plans that after construction there are at least two shortest paths from station 1 to any other station, and these two shortest paths do not pass the same station except station 1 and the terminal. Besides, Nitori also does not want to change the distance of the shortest path from station 1 to any other station. Due to various reasons, sometimes the cost of building a new railway will increase uncontrollably. There will be a total of q occurrences of this kind of incident, and the i-th event will add additional amount of x_i to the cost of building a new railway from the station k_i. To save resources, before all incidents and after each incident, Nitori wants you to help her calculate the minimal cost of railway construction. Input The first line contains three integers n, m, and q (1 ≀ n ≀ 2 β‹… 10^5, 1 ≀ m ≀ 3 β‹… 10^5, 0 ≀ q ≀ 2β‹…10^5). The second line contains n integers w_1,w_2,…,w_n (1 ≀ w_i ≀ 10^9). Each of the next m lines contains three integers u, v, d (1 ≀ u,v ≀ n, u β‰  v, 1 ≀ d ≀ 10^9), denoting a two-way railway connecting station u and station v, with length d. The i-th of the next q lines contains two integers k_i,x_i (1 ≀ k_i ≀ n, 1 ≀ x_i ≀ 4 Γ— 10^8). Output Print q+1 lines, and the i-th of these lines contains one integer, denoting the minimal cost of railway construction after the i-1-th incident (especially, the 0-th incident means no incident occurred). Examples Input 5 5 1 1 1 1 1 1 1 2 1 2 3 1 2 4 1 3 5 1 4 5 1 1 2 Output 3 9 Input 8 11 0 14 4 16 15 1 3 1 14 4 2 1 1 2 3 7 5 4 2 3 1 8 6 2 8 5 5 5 4 5 7 6 7 3 5 5 1 6 6 8 1 4 Output 46 Input 10 16 8 29 1 75 73 51 69 24 17 1 97 1 2 18 2 3 254 2 4 546 2 5 789 5 6 998 6 7 233 7 8 433 1 9 248 5 10 488 2 6 1787 10 8 1176 3 8 2199 4 8 1907 2 10 1277 4 10 731 9 10 1047 1 11 1 9 8 8 1 3 2 19 9 5 9 4 7 6 Output 34 45 54 54 57 76 96 112 112 Note In the second example, Nitori can build railways as follows: 1 β†’ 2, 1 β†’ 3, 1 β†’ 4, 2 β†’ 8, and the cost is 14 + 14 + 14 + 4 = 46.
XYMXYM and CQXYM will prepare n problems for Codeforces. The difficulty of the problem i will be an integer a_i, where a_i β‰₯ 0. The difficulty of the problems must satisfy a_i+a_{i+1}<m (1 ≀ i < n), and a_1+a_n<m, where m is a fixed integer. XYMXYM wants to know how many plans of the difficulty of the problems there are modulo 998 244 353. Two plans of difficulty a and b are different only if there is an integer i (1 ≀ i ≀ n) satisfying a_i β‰  b_i. Input A single line contains two integers n and m (2 ≀ n ≀ 50 000, 1 ≀ m ≀ 10^9). Output Print a single integer β€” the number of different plans. Examples Input 3 2 Output 4 Input 5 9 Output 8105 Input 21038 3942834 Output 338529212 Note In the first test case, the valid a are: [0,0,0], [0,0,1], [0,1,0], [1,0,0]. [1,0,1] is invalid since a_1+a_n β‰₯ m.
CQXYM is counting permutations length of 2n. A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array). A permutation p(length of 2n) will be counted only if the number of i satisfying p_i<p_{i+1} is no less than n. For example: * Permutation [1, 2, 3, 4] will count, because the number of such i that p_i<p_{i+1} equals 3 (i = 1, i = 2, i = 3). * Permutation [3, 2, 1, 4] won't count, because the number of such i that p_i<p_{i+1} equals 1 (i = 3). CQXYM wants you to help him to count the number of such permutations modulo 1000000007 (10^9+7). In addition, [modulo operation](https://en.wikipedia.org/wiki/Modulo_operation) is to get the remainder. For example: * 7 mod 3=1, because 7 = 3 β‹… 2 + 1, * 15 mod 4=3, because 15 = 4 β‹… 3 + 3. Input The input consists of multiple test cases. The first line contains an integer t (t β‰₯ 1) β€” the number of test cases. The description of the test cases follows. Only one line of each test case contains an integer n(1 ≀ n ≀ 10^5). It is guaranteed that the sum of n over all test cases does not exceed 10^5 Output For each test case, print the answer in a single line. Example Input 4 1 2 9 91234 Output 1 12 830455698 890287984 Note n=1, there is only one permutation that satisfies the condition: [1,2]. In permutation [1,2], p_1<p_2, and there is one i=1 satisfy the condition. Since 1 β‰₯ n, this permutation should be counted. In permutation [2,1], p_1>p_2. Because 0<n, this permutation should not be counted. n=2, there are 12 permutations: [1,2,3,4],[1,2,4,3],[1,3,2,4],[1,3,4,2],[1,4,2,3],[2,1,3,4],[2,3,1,4],[2,3,4,1],[2,4,1,3],[3,1,2,4],[3,4,1,2],[4,1,2,3].
CQXYM wants to create a connected undirected graph with n nodes and m edges, and the diameter of the graph must be strictly less than k-1. Also, CQXYM doesn't want a graph that contains self-loops or multiple edges (i.e. each edge connects two different vertices and between each pair of vertices there is at most one edge). The diameter of a graph is the maximum distance between any two nodes. The distance between two nodes is the minimum number of the edges on the path which endpoints are the two nodes. CQXYM wonders whether it is possible to create such a graph. Input The input consists of multiple test cases. The first line contains an integer t (1 ≀ t ≀ 10^5) β€” the number of test cases. The description of the test cases follows. Only one line of each test case contains three integers n(1 ≀ n ≀ 10^9), m, k (0 ≀ m,k ≀ 10^9). Output For each test case, print YES if it is possible to create the graph, or print NO if it is impossible. You can print each letter in any case (upper or lower). Example Input 5 1 0 3 4 5 3 4 6 3 5 4 1 2 1 1 Output YES NO YES NO NO Note In the first test case, the graph's diameter equal to 0. In the second test case, the graph's diameter can only be 2. In the third test case, the graph's diameter can only be 1.
Luntik has decided to try singing. He has a one-minute songs, b two-minute songs and c three-minute songs. He wants to distribute all songs into two concerts such that every song should be included to exactly one concert. He wants to make the absolute difference of durations of the concerts as small as possible. The duration of the concert is the sum of durations of all songs in that concert. Please help Luntik and find the minimal possible difference in minutes between the concerts durations. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. Each test case consists of one line containing three integers a, b, c (1 ≀ a, b, c ≀ 10^9) β€” the number of one-minute, two-minute and three-minute songs. Output For each test case print the minimal possible difference in minutes between the concerts durations. Example Input 4 1 1 1 2 1 3 5 5 5 1 1 2 Output 0 1 0 1 Note In the first test case, Luntik can include a one-minute song and a two-minute song into the first concert, and a three-minute song into the second concert. Then the difference will be equal to 0. In the second test case, Luntik can include two one-minute songs and a two-minute song and a three-minute song into the first concert, and two three-minute songs into the second concert. The duration of the first concert will be 1 + 1 + 2 + 3 = 7, the duration of the second concert will be 6. The difference of them is |7-6| = 1.
Luntik came out for a morning stroll and found an array a of length n. He calculated the sum s of the elements of the array (s= βˆ‘_{i=1}^{n} a_i). Luntik calls a subsequence of the array a nearly full if the sum of the numbers in that subsequence is equal to s-1. Luntik really wants to know the number of nearly full subsequences of the array a. But he needs to come home so he asks you to solve that problem! A sequence x is a subsequence of a sequence y if x can be obtained from y by deletion of several (possibly, zero or all) elements. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The next 2 β‹… t lines contain descriptions of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer n (1 ≀ n ≀ 60) β€” the length of the array. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 10^9) β€” the elements of the array a. Output For each test case print the number of nearly full subsequences of the array. Example Input 5 5 1 2 3 4 5 2 1000 1000 2 1 0 5 3 0 2 1 1 5 2 1 0 3 0 Output 1 0 2 4 4 Note In the first test case, s=1+2+3+4+5=15, only (2,3,4,5) is a nearly full subsequence among all subsequences, the sum in it is equal to 2+3+4+5=14=15-1. In the second test case, there are no nearly full subsequences. In the third test case, s=1+0=1, the nearly full subsequences are (0) and () (the sum of an empty subsequence is 0).
Grandma Capa has decided to knit a scarf and asked Grandpa Sher to make a pattern for it, a pattern is a string consisting of lowercase English letters. Grandpa Sher wrote a string s of length n. Grandma Capa wants to knit a beautiful scarf, and in her opinion, a beautiful scarf can only be knit from a string that is a palindrome. She wants to change the pattern written by Grandpa Sher, but to avoid offending him, she will choose one lowercase English letter and erase some (at her choice, possibly none or all) occurrences of that letter in string s. She also wants to minimize the number of erased symbols from the pattern. Please help her and find the minimum number of symbols she can erase to make string s a palindrome, or tell her that it's impossible. Notice that she can only erase symbols equal to the one letter she chose. A string is a palindrome if it is the same from the left to the right and from the right to the left. For example, the strings 'kek', 'abacaba', 'r' and 'papicipap' are palindromes, while the strings 'abb' and 'iq' are not. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next 2 β‹… t lines contain the description of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer n (1 ≀ n ≀ 10^5) β€” the length of the string. The second line of each test case contains the string s consisting of n lowercase English letters. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case print the minimum number of erased symbols required to make the string a palindrome, if it is possible, and -1, if it is impossible. Example Input 5 8 abcaacab 6 xyzxyz 4 abba 8 rprarlap 10 khyyhhyhky Output 2 -1 0 3 2 Note In the first test case, you can choose a letter 'a' and erase its first and last occurrences, you will get a string 'bcaacb', which is a palindrome. You can also choose a letter 'b' and erase all its occurrences, you will get a string 'acaaca', which is a palindrome as well. In the second test case, it can be shown that it is impossible to choose a letter and erase some of its occurrences to get a palindrome. In the third test case, you don't have to erase any symbols because the string is already a palindrome.
Vupsen and Pupsen were gifted an integer array. Since Vupsen doesn't like the number 0, he threw away all numbers equal to 0 from the array. As a result, he got an array a of length n. Pupsen, on the contrary, likes the number 0 and he got upset when he saw the array without zeroes. To cheer Pupsen up, Vupsen decided to come up with another array b of length n such that βˆ‘_{i=1}^{n}a_i β‹… b_i=0. Since Vupsen doesn't like number 0, the array b must not contain numbers equal to 0. Also, the numbers in that array must not be huge, so the sum of their absolute values cannot exceed 10^9. Please help Vupsen to find any such array b! Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next 2 β‹… t lines contain the description of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer n (2 ≀ n ≀ 10^5) β€” the length of the array. The second line contains n integers a_1, a_2, …, a_n (-10^4 ≀ a_i ≀ 10^4, a_i β‰  0) β€” the elements of the array a. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case print n integers b_1, b_2, …, b_n β€” elements of the array b (|b_1|+|b_2|+… +|b_n| ≀ 10^9, b_i β‰  0, βˆ‘_{i=1}^{n}a_i β‹… b_i=0). It can be shown that the answer always exists. Example Input 3 2 5 5 5 5 -2 10 -9 4 7 1 2 3 4 5 6 7 Output 1 -1 -1 5 1 -1 -1 -10 2 2 -3 5 -1 -1 Note In the first test case, 5 β‹… 1 + 5 β‹… (-1)=5-5=0. You could also print 3 -3, for example, since 5 β‹… 3 + 5 β‹… (-3)=15-15=0 In the second test case, 5 β‹… (-1) + (-2) β‹… 5 + 10 β‹… 1 + (-9) β‹… (-1) + 4 β‹… (-1)=-5-10+10+9-4=0.
Pchelyonok decided to give Mila a gift. Pchelenok has already bought an array a of length n, but gifting an array is too common. Instead of that, he decided to gift Mila the segments of that array! Pchelyonok wants his gift to be beautiful, so he decided to choose k non-overlapping segments of the array [l_1,r_1], [l_2,r_2], … [l_k,r_k] such that: * the length of the first segment [l_1,r_1] is k, the length of the second segment [l_2,r_2] is k-1, …, the length of the k-th segment [l_k,r_k] is 1 * for each i<j, the i-th segment occurs in the array earlier than the j-th (i.e. r_i<l_j) * the sums in these segments are strictly increasing (i.e. let sum(l … r) = βˆ‘_{i=l}^{r} a_i β€” the sum of numbers in the segment [l,r] of the array, then sum(l_1 … r_1) < sum(l_2 … r_2) < … < sum(l_k … r_k)). Pchelenok also wants his gift to be as beautiful as possible, so he asks you to find the maximal value of k such that he can give Mila a gift! Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. The next 2 β‹… t lines contain the descriptions of test cases. The description of each test case consists of two lines. The first line of each test case contains a single integer n (1 ≀ n ≀ 10^5) β€” the length of the array. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^9) β€” the elements of the array a. It is guaranteed that the sum of n over all test cases does not exceed 10^5. Output For each test case, print the maximum possible value of k. Example Input 5 1 1 3 1 2 3 5 1 1 2 2 3 7 1 2 1 1 3 2 6 5 9 6 7 9 7 Output 1 1 2 3 1
This is an easier version of the problem with smaller constraints. Korney Korneevich dag up an array a of length n. Korney Korneevich has recently read about the operation [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), so he wished to experiment with it. For this purpose, he decided to find all integers x β‰₯ 0 such that there exists an increasing subsequence of the array a, in which the bitwise XOR of numbers is equal to x. It didn't take a long time for Korney Korneevich to find all such x, and he wants to check his result. That's why he asked you to solve this problem! A sequence s is a subsequence of a sequence b if s can be obtained from b by deletion of several (possibly, zero or all) elements. A sequence s_1, s_2, … , s_m is called increasing if s_1 < s_2 < … < s_m. Input The first line contains a single integer n (1 ≀ n ≀ 10^5). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 500) β€” the elements of the array a. Output In the first line print a single integer k β€” the number of found x values. In the second line print k integers in increasing order x_1, x_2, … x_k (0 ≀ x_1 < … < x_k) β€” found x values. Examples Input 4 4 2 2 4 Output 4 0 2 4 6 Input 8 1 0 1 7 12 5 3 2 Output 12 0 1 2 3 4 5 6 7 10 11 12 13 Note In the first test case: * To get value x = 0 it is possible to choose and empty subsequence * To get value x = 2 it is possible to choose a subsequence [2] * To get value x = 4 it is possible to choose a subsequence [4] * To get value x = 6 it is possible to choose a subsequence [2, 4]
This is a harder version of the problem with bigger constraints. Korney Korneevich dag up an array a of length n. Korney Korneevich has recently read about the operation [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR), so he wished to experiment with it. For this purpose, he decided to find all integers x β‰₯ 0 such that there exists an increasing subsequence of the array a, in which the bitwise XOR of numbers is equal to x. It didn't take a long time for Korney Korneevich to find all such x, and he wants to check his result. That's why he asked you to solve this problem! A sequence s is a subsequence of a sequence b if s can be obtained from b by deletion of several (possibly, zero or all) elements. A sequence s_1, s_2, … , s_m is called increasing if s_1 < s_2 < … < s_m. Input The first line contains a single integer n (1 ≀ n ≀ 10^6). The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ 5000) β€” the elements of the array a. Output In the first line print a single integer k β€” the number of found x values. In the second line print k integers in increasing order x_1, x_2, … x_k (0 ≀ x_1 < … < x_k) β€” found x values. Examples Input 4 4 2 2 4 Output 4 0 2 4 6 Input 8 1 0 1 7 12 5 3 2 Output 12 0 1 2 3 4 5 6 7 10 11 12 13 Note In the first test case: * To get value x = 0 it is possible to choose and empty subsequence * To get value x = 2 it is possible to choose a subsequence [2] * To get value x = 4 it is possible to choose a subsequence [4] * To get value x = 6 it is possible to choose a subsequence [2, 4]
Kuzya started going to school. He was given math homework in which he was given an array a of length n and an array of symbols b of length n, consisting of symbols '*' and '/'. Let's denote a path of calculations for a segment [l; r] (1 ≀ l ≀ r ≀ n) in the following way: * Let x=1 initially. For every i from l to r we will consequently do the following: if b_i= '*', x=x*a_i, and if b_i= '/', then x=(x)/(a_i). Let's call a path of calculations for the segment [l; r] a list of all x that we got during the calculations (the number of them is exactly r - l + 1). For example, let a=[7, 12, 3, 5, 4, 10, 9], b=[/, *, /, /, /, *, *], l=2, r=6, then the path of calculations for that segment is [12, 4, 0.8, 0.2, 2]. Let's call a segment [l;r] simple if the path of calculations for it contains only integer numbers. Kuzya needs to find the number of simple segments [l;r] (1 ≀ l ≀ r ≀ n). Since he obviously has no time and no interest to do the calculations for each option, he asked you to write a program to get to find that number! Input The first line contains a single integer n (2 ≀ n ≀ 10^6). The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^6). The third line contains n symbols without spaces between them β€” the array b_1, b_2 … b_n (b_i= '/' or b_i= '*' for every 1 ≀ i ≀ n). Output Print a single integer β€” the number of simple segments [l;r]. Examples Input 3 1 2 3 */* Output 2 Input 7 6 4 10 1 2 15 1 */*/*// Output 8
A bow adorned with nameless flowers that bears the earnest hopes of an equally nameless person. You have obtained the elegant bow known as the Windblume Ode. Inscribed in the weapon is an array of n (n β‰₯ 3) positive distinct integers (i.e. different, no duplicates are allowed). Find the largest subset (i.e. having the maximum number of elements) of this array such that its sum is a composite number. A positive integer x is called composite if there exists a positive integer y such that 1 < y < x and x is divisible by y. If there are multiple subsets with this largest size with the composite sum, you can output any of them. It can be proven that under the constraints of the problem such a non-empty subset always exists. Input Each test consists of multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains an integer n (3 ≀ n ≀ 100) β€” the length of the array. The second line of each test case contains n distinct integers a_{1},a_{2},...,a_{n} (1 ≀ a_{i} ≀ 200) β€” the elements of the array. Output Each test case should have two lines of output. The first line should contain a single integer x: the size of the largest subset with composite sum. The next line should contain x space separated integers representing the indices of the subset of the initial array. Example Input 4 3 8 1 2 4 6 9 4 2 9 1 2 3 4 5 6 7 8 9 3 200 199 198 Output 2 2 1 4 2 1 4 3 9 6 9 1 2 3 4 5 7 8 3 1 2 3 Note In the first test case, the subset \\{a_2, a_1\} has a sum of 9, which is a composite number. The only subset of size 3 has a prime sum equal to 11. Note that you could also have selected the subset \\{a_1, a_3\} with sum 8 + 2 = 10, which is composite as it's divisible by 2. In the second test case, the sum of all elements equals to 21, which is a composite number. Here we simply take the whole array as our subset.
Lord Omkar would like to have a tree with n nodes (3 ≀ n ≀ 10^5) and has asked his disciples to construct the tree. However, Lord Omkar has created m (1 ≀ m < n) restrictions to ensure that the tree will be as heavenly as possible. A tree with n nodes is an connected undirected graph with n nodes and n-1 edges. Note that for any two nodes, there is exactly one simple path between them, where a simple path is a path between two nodes that does not contain any node more than once. Here is an example of a tree: <image> A restriction consists of 3 pairwise distinct integers, a, b, and c (1 ≀ a,b,c ≀ n). It signifies that node b cannot lie on the simple path between node a and node c. Can you help Lord Omkar and become his most trusted disciple? You will need to find heavenly trees for multiple sets of restrictions. It can be shown that a heavenly tree will always exist for any set of restrictions under the given constraints. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains two integers, n and m (3 ≀ n ≀ 10^5, 1 ≀ m < n), representing the size of the tree and the number of restrictions. The i-th of the next m lines contains three integers a_i, b_i, c_i (1 ≀ a_i, b_i, c_i ≀ n, a, b, c are distinct), signifying that node b_i cannot lie on the simple path between nodes a_i and c_i. It is guaranteed that the sum of n across all test cases will not exceed 10^5. Output For each test case, output n-1 lines representing the n-1 edges in the tree. On each line, output two integers u and v (1 ≀ u, v ≀ n, u β‰  v) signifying that there is an edge between nodes u and v. Given edges have to form a tree that satisfies Omkar's restrictions. Example Input 2 7 4 1 2 3 3 4 5 5 6 7 6 5 4 5 3 1 2 3 2 3 4 3 4 5 Output 1 2 1 3 3 5 3 4 2 7 7 6 5 1 1 3 3 2 2 4 Note The output of the first sample case corresponds to the following tree: <image> For the first restriction, the simple path between 1 and 3 is 1, 3, which doesn't contain 2. The simple path between 3 and 5 is 3, 5, which doesn't contain 4. The simple path between 5 and 7 is 5, 3, 1, 2, 7, which doesn't contain 6. The simple path between 6 and 4 is 6, 7, 2, 1, 3, 4, which doesn't contain 5. Thus, this tree meets all of the restrictions. The output of the second sample case corresponds to the following tree: <image>
The problem statement looms below, filling you with determination. Consider a grid in which some cells are empty and some cells are filled. Call a cell in this grid exitable if, starting at that cell, you can exit the grid by moving up and left through only empty cells. This includes the cell itself, so all filled in cells are not exitable. Note that you can exit the grid from any leftmost empty cell (cell in the first column) by going left, and from any topmost empty cell (cell in the first row) by going up. Let's call a grid determinable if, given only which cells are exitable, we can exactly determine which cells are filled in and which aren't. You are given a grid a of dimensions n Γ— m , i. e. a grid with n rows and m columns. You need to answer q queries (1 ≀ q ≀ 2 β‹… 10^5). Each query gives two integers x_1, x_2 (1 ≀ x_1 ≀ x_2 ≀ m) and asks whether the subgrid of a consisting of the columns x_1, x_1 + 1, …, x_2 - 1, x_2 is determinable. Input The first line contains two integers n, m (1 ≀ n, m ≀ 10^6, nm ≀ 10^6) β€” the dimensions of the grid a. n lines follow. The y-th line contains m characters, the x-th of which is 'X' if the cell on the intersection of the the y-th row and x-th column is filled and "." if it is empty. The next line contains a single integer q (1 ≀ q ≀ 2 β‹… 10^5) β€” the number of queries. q lines follow. Each line contains two integers x_1 and x_2 (1 ≀ x_1 ≀ x_2 ≀ m), representing a query asking whether the subgrid of a containing the columns x_1, x_1 + 1, …, x_2 - 1, x_2 is determinable. Output For each query, output one line containing "YES" if the subgrid specified by the query is determinable and "NO" otherwise. The output is case insensitive (so "yEs" and "No" will also be accepted). Example Input 4 5 ..XXX ...X. ...X. ...X. 5 1 3 3 3 4 5 5 5 1 5 Output YES YES NO YES NO Note For each query of the example, the corresponding subgrid is displayed twice below: first in its input format, then with each cell marked as "E" if it is exitable and "N" otherwise. For the first query: ..X EEN ... EEE ... EEE ... EEE For the second query: X N . E . E . E Note that you can exit the grid by going left from any leftmost cell (or up from any topmost cell); you do not need to reach the top left corner cell to exit the grid. For the third query: XX NN X. NN X. NN X. NN This subgrid cannot be determined only from whether each cell is exitable, because the below grid produces the above "exitability grid" as well: XX XX XX XX For the fourth query: X N . E . E . E For the fifth query: ..XXX EENNN ...X. EEENN ...X. EEENN ...X. EEENN This query is simply the entire grid. It cannot be determined only from whether each cell is exitable because the below grid produces the above "exitability grid" as well: ..XXX ...XX ...XX ...XX
It turns out that the meaning of life is a permutation p_1, p_2, …, p_n of the integers 1, 2, …, n (2 ≀ n ≀ 100). Omkar, having created all life, knows this permutation, and will allow you to figure it out using some queries. A query consists of an array a_1, a_2, …, a_n of integers between 1 and n. a is not required to be a permutation. Omkar will first compute the pairwise sum of a and p, meaning that he will compute an array s where s_j = p_j + a_j for all j = 1, 2, …, n. Then, he will find the smallest index k such that s_k occurs more than once in s, and answer with k. If there is no such index k, then he will answer with 0. You can perform at most 2n queries. Figure out the meaning of life p. Interaction Start the interaction by reading single integer n (2 ≀ n ≀ 100) β€” the length of the permutation p. You can then make queries. A query consists of a single line "? \enspace a_1 \enspace a_2 \enspace … \enspace a_n" (1 ≀ a_j ≀ n). The answer to each query will be a single integer k as described above (0 ≀ k ≀ n). After making a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * see documentation for other languages. To output your answer, print a single line "! \enspace p_1 \enspace p_2 \enspace … \enspace p_n" then terminate. You can make at most 2n queries. Outputting the answer does not count as a query. Hack Format To hack, first output a line containing n (2 ≀ n ≀ 100), then output another line containing the hidden permutation p_1, p_2, …, p_n of numbers from 1 to n. Example Input 5 2 0 1 Output ? 4 4 2 3 2 ? 3 5 1 5 5 ? 5 2 4 3 1 ! 3 2 1 5 4 Note In the sample, the hidden permutation p is [3, 2, 1, 5, 4]. Three queries were made. The first query is a = [4, 4, 2, 3, 2]. This yields s = [3 + 4, 2 + 4, 1 + 2, 5 + 3, 4 + 2] = [7, 6, 3, 8, 6]. 6 is the only number that appears more than once, and it appears first at index 2, making the answer to the query 2. The second query is a = [3, 5, 1, 5, 5]. This yields s = [3 + 3, 2 + 5, 1 + 1, 5 + 5, 4 + 5] = [6, 7, 2, 10, 9]. There are no numbers that appear more than once here, so the answer to the query is 0. The third query is a = [5, 2, 4, 3, 1]. This yields s = [3 + 5, 2 + 2, 1 + 4, 5 + 3, 4 + 1] = [8, 4, 5, 8, 5]. 5 and 8 both occur more than once here. 5 first appears at index 3, while 8 first appears at index 1, and 1 < 3, making the answer to the query 1. Note that the sample is only meant to provide an example of how the interaction works; it is not guaranteed that the above queries represent a correct strategy with which to determine the answer.
She does her utmost to flawlessly carry out a person's last rites and preserve the world's balance of yin and yang. Hu Tao, being the little prankster she is, has tried to scare you with this graph problem! You are given a connected undirected graph of n nodes with m edges. You also have q queries. Each query consists of two nodes a and b. Initially, all edges in the graph have a weight of 0. For each query, you must choose a simple path starting from a and ending at b. Then you add 1 to every edge along this path. Determine if it's possible, after processing all q queries, for all edges in this graph to have an even weight. If so, output the choice of paths for each query. If it is not possible, determine the smallest number of extra queries you could add to make it possible. It can be shown that this number will not exceed 10^{18} under the given constraints. A simple path is defined as any path that does not visit a node more than once. An edge is said to have an even weight if its value is divisible by 2. Input The first line contains two integers n and m (2 ≀ n ≀ 3 β‹… 10^5, n-1 ≀ m ≀ min{\left((n(n-1))/(2), 3 β‹… 10^5\right)}). Each of the next m lines contains two integers x and y (1 ≀ x, y ≀ n, xβ‰  y) indicating an undirected edge between node x and y. The input will not contain self-loops or duplicate edges, and the provided graph will be connected. The next line contains a single integer q (1 ≀ q ≀ 3 β‹… 10^5). Each of the next q lines contains two integers a and b (1 ≀ a, b ≀ n, a β‰  b), the description of each query. It is guaranteed that nq ≀ 3 β‹… 10^5. Output If it is possible to force all edge weights to be even, print "YES" on the first line, followed by 2q lines indicating the choice of path for each query in the same order the queries are given. For each query, the first line should contain a single integer x: the number of nodes in the chosen path. The next line should then contain x spaced separated integers p_i indicating the path you take (p_1 = a, p_x = b and all numbers should fall between 1 and n). This path cannot contain duplicate nodes and must be a valid simple path in the graph. If it is impossible to force all edge weights to be even, print "NO" on the first line and the minimum number of added queries on the second line. Examples Input 6 7 2 1 2 3 3 5 1 4 6 1 5 6 4 5 3 1 4 5 1 4 5 Output YES 2 1 4 4 5 3 2 1 5 4 1 2 3 5 Input 5 7 4 3 4 5 2 1 1 4 1 3 3 5 3 2 4 4 2 3 5 5 1 4 5 Output NO 2 Note Here is what the queries look like for the first test case (red corresponds to the 1st query, blue 2nd query, and green 3rd query): <image> Notice that every edge in the graph is part of either 0 or 2 colored query edges. The graph in the second test case looks like this: <image> There does not exist an assignment of paths that will force all edges to have even weights with the given queries. One must add at least 2 new queries to obtain a set of queries that can satisfy the condition.
Even if you just leave them be, they will fall to pieces all by themselves. So, someone has to protect them, right? You find yourself playing with Teucer again in the city of Liyue. As you take the eccentric little kid around, you notice something interesting about the structure of the city. Liyue can be represented as a directed graph containing n nodes. Nodes are labeled from 1 to n. There is a directed edge from node a to node b if and only if a < b. A path between nodes a and b is defined as a sequence of edges such that you can start at a, travel along all of these edges in the corresponding direction, and end at b. The length of a path is defined by the number of edges. A rainbow path of length x is defined as a path in the graph such that there exists at least 2 distinct colors among the set of x edges. Teucer's favorite number is k. You are curious about the following scenario: If you were to label each edge with a color, what is the minimum number of colors needed to ensure that all paths of length k or longer are rainbow paths? Teucer wants to surprise his older brother with a map of Liyue. He also wants to know a valid coloring of edges that uses the minimum number of colors. Please help him with this task! Input The only line of input contains two integers n and k (2 ≀ k < n ≀ 1000). Output On the first line, output c, the minimum colors you need to satisfy the above requirements. On the second line, print a valid edge coloring as an array of (n(n-1))/(2) integers ranging from 1 to c. Exactly c distinct colors should exist in the construction. Print the edges in increasing order by the start node first, then by the second node. For example, if n=4, the edge colors will correspond to this order of edges: (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4) Examples Input 5 3 Output 2 1 2 2 2 2 2 2 1 1 1 Input 5 2 Output 3 3 2 2 1 2 2 1 3 1 1 Input 8 7 Output 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Input 3 2 Output 2 1 2 2 Note The corresponding construction for the first test case looks like this: <image> It is impossible to satisfy the constraints with less than 2 colors. The corresponding construction for the second test case looks like this: <image> One can show there exists no construction using less than 3 colors.
El Psy Kongroo. Omkar is watching Steins;Gate. In Steins;Gate, Okabe Rintarou needs to complete n tasks (1 ≀ n ≀ 2 β‹… 10^5). Unfortunately, he doesn't know when he needs to complete the tasks. Initially, the time is 0. Time travel will now happen according to the following rules: * For each k = 1, 2, …, n, Okabe will realize at time b_k that he was supposed to complete the k-th task at time a_k (a_k < b_k). * When he realizes this, if k-th task was already completed at time a_k, Okabe keeps the usual flow of time. Otherwise, he time travels to time a_k then immediately completes the task. * If Okabe time travels to time a_k, all tasks completed after this time will become incomplete again. That is, for every j, if a_j>a_k, the j-th task will become incomplete, if it was complete (if it was incomplete, nothing will change). * Okabe has bad memory, so he can time travel to time a_k only immediately after getting to time b_k and learning that he was supposed to complete the k-th task at time a_k. That is, even if Okabe already had to perform k-th task before, he wouldn't remember it before stumbling on the info about this task at time b_k again. Please refer to the notes for an example of time travelling. There is a certain set s of tasks such that the first moment that all of the tasks in s are simultaneously completed (regardless of whether any other tasks are currently completed), a funny scene will take place. Omkar loves this scene and wants to know how many times Okabe will time travel before this scene takes place. Find this number modulo 10^9 + 7. It can be proven that eventually all n tasks will be completed and so the answer always exists. Input The first line contains an integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of tasks that Okabe needs to complete. n lines follow. The k-th of these lines contain two integers a_k and b_k (1 ≀ a_k < b_k ≀ 2n) β€” the time at which Okabe needs to complete the k-th task and the time that he realizes this respectively. All 2n of these times are distinct (so every time from 1 to 2n inclusive appears exactly once in the input). The next line contains a single integer t (1 ≀ t ≀ n) β€” the size of the set s of tasks that lead to the funny scene. The last line contains t integers s_1, s_2, …, s_t β€” (1 ≀ s_k ≀ n, the numbers s_1, s_2, …, s_t are distinct) β€” the set s of tasks. Output Output a single integer β€” the number of times that Okabe time travels until all tasks in the set s are simultaneously completed, modulo 10^9 + 7. Examples Input 2 1 4 2 3 2 1 2 Output 3 Input 2 1 4 2 3 1 1 Output 2 Input 1 1 2 1 1 Output 1 Input 6 10 12 3 7 4 6 2 9 5 8 1 11 3 2 4 6 Output 17 Input 16 31 32 3 26 17 19 4 24 1 28 15 21 12 16 18 29 20 23 7 8 11 14 9 22 6 30 5 10 25 27 2 13 6 3 8 2 5 12 11 Output 138 Note For the first sample, all tasks need to be completed in order for the funny scene to occur. Initially, the time is 0. Nothing happens until time 3, when Okabe realizes that he should have done the 2-nd task at time 2. He then time travels to time 2 and completes the task. As the task is done now, he does not time travel again when the time is again 3. However, at time 4, he travels to time 1 to complete the 1-st task. This undoes the 2-nd task. This means that the 2-nd task is not currently completed, meaning that the funny scene will not occur at this point even though the 1-st task is currently completed and Okabe had previously completed the 2-nd task. Once it is again time 3 he travels back to time 2 once more and does the 2-nd task again. Now all tasks are complete, with Okabe having time travelled 3 times. The second sample has the same tasks for Okabe to complete. However, this time the funny scene only needs the first task to be completed in order to occur. From reading the above sample you can see that this occurs once Okabe has time travelled 2 times.
Omkar is hosting tours of his country, Omkarland! There are n cities in Omkarland, and, rather curiously, there are exactly n-1 bidirectional roads connecting the cities to each other. It is guaranteed that you can reach any city from any other city through the road network. Every city has an enjoyment value e. Each road has a capacity c, denoting the maximum number of vehicles that can be on it, and an associated toll t. However, the toll system in Omkarland has an interesting quirk: if a vehicle travels on multiple roads on a single journey, they pay only the highest toll of any single road on which they traveled. (In other words, they pay max t over all the roads on which they traveled.) If a vehicle traverses no roads, they pay 0 toll. Omkar has decided to host q tour groups. Each tour group consists of v vehicles starting at city x. (Keep in mind that a tour group with v vehicles can travel only on roads with capacity β‰₯ v.) Being the tour organizer, Omkar wants his groups to have as much fun as they possibly can, but also must reimburse his groups for the tolls that they have to pay. Thus, for each tour group, Omkar wants to know two things: first, what is the enjoyment value of the city y with maximum enjoyment value that the tour group can reach from their starting city, and second, how much per vehicle will Omkar have to pay to reimburse the entire group for their trip from x to y? (This trip from x to y will always be on the shortest path from x to y.) In the case that there are multiple reachable cities with the maximum enjoyment value, Omkar will let his tour group choose which one they want to go to. Therefore, to prepare for all possible scenarios, he wants to know the amount of money per vehicle that he needs to guarantee that he can reimburse the group regardless of which city they choose. Input The first line contains two integers n and q (2 ≀ n ≀ 2 β‹… 10^5, 1 ≀ q ≀ 2 β‹… 10^5), representing the number of cities and the number of groups, respectively. The next line contains n integers e_1, e_2, …, e_n (1 ≀ e_i ≀ 10^9), where e_i represents the enjoyment value for city i. The next n-1 lines each contain four integers a, b, c, and t (1 ≀ a,b ≀ n, 1 ≀ c ≀ 10^9, 1 ≀ t ≀ 10^9), representing an road between city a and city b with capacity c and toll t. The next q lines each contain two integers v and x (1 ≀ v ≀ 10^9, 1 ≀ x ≀ n), representing the number of vehicles in the tour group and the starting city, respectively. Output Output q lines. The i-th line should contain two integers: the highest possible enjoyment value of a city reachable by the i-th tour group, and the amount of money per vehicle Omkar needs to guarantee that he can reimburse the i-th tour group. Examples Input 5 3 2 2 3 3 3 1 2 4 7 1 3 2 8 2 4 8 2 2 5 1 1 1 3 9 5 6 2 Output 3 8 3 0 3 2 Input 5 5 1 2 3 4 5 1 2 4 1 1 3 3 1 1 4 2 1 2 5 1 1 5 1 4 1 3 1 2 1 1 1 Output 1 0 2 1 3 1 4 1 5 1 Input 5 5 1 2 2 2 2 1 2 5 8 1 3 6 3 1 4 4 5 1 5 7 1 4 1 5 1 6 1 7 1 8 1 Output 2 8 2 8 2 3 2 1 1 0 Note A map of the first sample is shown below. For the nodes, unbolded numbers represent indices and bolded numbers represent enjoyment values. For the edges, unbolded numbers represent capacities and bolded numbers represent tolls. <image> For the first query, a tour group of size 1 starting at city 3 can reach cities 1, 2, 3, 4, and 5. Thus, the largest enjoyment value that they can reach is 3. If the tour group chooses to go to city 4, Omkar will have to pay 8 per vehicle, which is the maximum. For the second query, a tour group of size 9 starting at city 5 can reach only city 5. Thus, the largest reachable enjoyment value is still 3, and Omkar will pay 0 per vehicle. For the third query, a tour group of size 6 starting at city 2 can reach cities 2 and 4. The largest reachable enjoyment value is again 3. If the tour group chooses to go to city 4, Omkar will have to pay 2 per vehicle, which is the maximum. A map of the second sample is shown below: <image> For the first query, a tour group of size 5 starting at city 1 can only reach city 1. Thus, their maximum enjoyment value is 1 and the cost Omkar will have to pay is 0 per vehicle. For the second query, a tour group of size 4 starting at city 1 can reach cities 1 and 2. Thus, their maximum enjoyment value is 2 and Omkar will pay 1 per vehicle. For the third query, a tour group of size 3 starting at city 1 can reach cities 1, 2, and 3. Thus, their maximum enjoyment value is 3 and Omkar will pay 1 per vehicle. For the fourth query, a tour group of size 2 starting at city 1 can reach cities 1, 2, 3 and 4. Thus, their maximum enjoyment value is 4 and Omkar will pay 1 per vehicle. For the fifth query, a tour group of size 1 starting at city 1 can reach cities 1, 2, 3, 4, and 5. Thus, their maximum enjoyment value is 5 and Omkar will pay 1 per vehicle.
Omkar is creating a mosaic using colored square tiles, which he places in an n Γ— n grid. When the mosaic is complete, each cell in the grid will have either a glaucous or sinoper tile. However, currently he has only placed tiles in some cells. A completed mosaic will be a mastapeece if and only if each tile is adjacent to exactly 2 tiles of the same color (2 tiles are adjacent if they share a side.) Omkar wants to fill the rest of the tiles so that the mosaic becomes a mastapeece. Now he is wondering, is the way to do this unique, and if it is, what is it? Input The first line contains a single integer n (1 ≀ n ≀ 2000). Then follow n lines with n characters in each line. The i-th character in the j-th line corresponds to the cell in row i and column j of the grid, and will be S if Omkar has placed a sinoper tile in this cell, G if Omkar has placed a glaucous tile, . if it's empty. Output On the first line, print UNIQUE if there is a unique way to get a mastapeece, NONE if Omkar cannot create any, and MULTIPLE if there is more than one way to do so. All letters must be uppercase. If you print UNIQUE, then print n additional lines with n characters in each line, such that the i-th character in the j^{th} line is S if the tile in row i and column j of the mastapeece is sinoper, and G if it is glaucous. Examples Input 4 S... ..G. .... ...S Output MULTIPLE Input 6 S..... ....G. ..S... .....S ....G. G..... Output NONE Input 10 .S....S... .......... ...SSS.... .......... .......... ...GS..... ....G...G. .......... ......G... .......... Output UNIQUE SSSSSSSSSS SGGGGGGGGS SGSSSSSSGS SGSGGGGSGS SGSGSSGSGS SGSGSSGSGS SGSGGGGSGS SGSSSSSSGS SGGGGGGGGS SSSSSSSSSS Input 1 . Output NONE Note For the first test case, Omkar can make the mastapeeces SSSS SGGS SGGS SSSS and SSGG SSGG GGSS GGSS. For the second test case, it can be proven that it is impossible for Omkar to add tiles to create a mastapeece. For the third case, it can be proven that the given mastapeece is the only mastapeece Omkar can create by adding tiles. For the fourth test case, it's clearly impossible for the only tile in any mosaic Omkar creates to be adjacent to two tiles of the same color, as it will be adjacent to 0 tiles total.
Petya has got an interesting flower. Petya is a busy person, so he sometimes forgets to water it. You are given n days from Petya's live and you have to determine what happened with his flower in the end. The flower grows as follows: * If the flower isn't watered for two days in a row, it dies. * If the flower is watered in the i-th day, it grows by 1 centimeter. * If the flower is watered in the i-th and in the (i-1)-th day (i > 1), then it grows by 5 centimeters instead of 1. * If the flower is not watered in the i-th day, it does not grow. At the beginning of the 1-st day the flower is 1 centimeter tall. What is its height after n days? Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line of each test case contains the only integer n (1 ≀ n ≀ 100). The second line of each test case contains n integers a_1, a_2, ..., a_n (a_i = 0 or a_i = 1). If a_i = 1, the flower is watered in the i-th day, otherwise it is not watered. Output For each test case print a single integer k β€” the flower's height after n days, or -1, if the flower dies. Example Input 4 3 1 0 1 3 0 1 1 4 1 0 0 1 1 0 Output 3 7 -1 1
You are given an array a of length n. Let's define the eversion operation. Let x = a_n. Then array a is partitioned into two parts: left and right. The left part contains the elements of a that are not greater than x (≀ x). The right part contains the elements of a that are strictly greater than x (> x). The order of elements in each part is kept the same as before the operation, i. e. the partition is stable. Then the array is replaced with the concatenation of the left and the right parts. For example, if the array a is [2, 4, 1, 5, 3], the eversion goes like this: [2, 4, 1, 5, 3] β†’ [2, 1, 3], [4, 5] β†’ [2, 1, 3, 4, 5]. We start with the array a and perform eversions on this array. We can prove that after several eversions the array a stops changing. Output the minimum number k such that the array stops changing after k eversions. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 100). Description of the test cases follows. The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5). The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9). It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case print a single integer k β€” the number of eversions after which the array stops changing. Example Input 3 5 2 4 1 5 3 5 5 3 2 4 1 4 1 1 1 1 Output 1 2 0 Note Consider the fist example. * The first eversion: a = [1, 4, 2, 5, 3], x = 3. [2, 4, 1, 5, 3] β†’ [2, 1, 3], [4, 5] β†’ [2, 1, 3, 4, 5]. * The second and following eversions: a = [2, 1, 3, 4, 5], x = 5. [2, 1, 3, 4, 5] β†’ [2, 1, 3, 4, 5], [] β†’ [2, 1, 3, 4, 5]. This eversion does not change the array, so the answer is 1. Consider the second example. * The first eversion: a = [5, 3, 2, 4, 1], x = 1. [5, 3, 2, 4, 1] β†’ [1], [5, 3, 2, 4] β†’ [1, 5, 3, 2, 4]. * The second eversion: a = [1, 5, 3, 2, 4], x = 4. [1, 5, 3, 2, 4] β†’ [1, 3, 2, 4], [5] β†’ [1, 3, 2, 4, 5]. * The third and following eversions: a = [1, 3, 2, 4, 5], x = 5. [1, 3, 2, 4, 5] β†’ [1, 3, 2, 4, 5], [] β†’ [1, 3, 2, 4, 5]. This eversion does not change the array, so the answer is 2.
A total of n depots are located on a number line. Depot i lies at the point x_i for 1 ≀ i ≀ n. You are a salesman with n bags of goods, attempting to deliver one bag to each of the n depots. You and the n bags are initially at the origin 0. You can carry up to k bags at a time. You must collect the required number of goods from the origin, deliver them to the respective depots, and then return to the origin to collect your next batch of goods. Calculate the minimum distance you need to cover to deliver all the bags of goods to the depots. You do not have to return to the origin after you have delivered all the bags. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10 500). Description of the test cases follows. The first line of each test case contains two integers n and k (1 ≀ k ≀ n ≀ 2 β‹… 10^5). The second line of each test case contains n integers x_1, x_2, …, x_n (-10^9 ≀ x_i ≀ 10^9). It is possible that some depots share the same position. It is guaranteed that the sum of n over all test cases does not exceed 2 β‹… 10^5. Output For each test case, output a single integer denoting the minimum distance you need to cover to deliver all the bags of goods to the depots. Example Input 4 5 1 1 2 3 4 5 9 3 -5 -10 -15 6 5 8 3 7 4 5 3 2 2 3 3 3 4 2 1000000000 1000000000 1000000000 1000000000 Output 25 41 7 3000000000 Note In the first test case, you can carry only one bag at a time. Thus, the following is a solution sequence that gives a minimum travel distance: 0 β†’ 2 β†’ 0 β†’ 4 β†’ 0 β†’ 3 β†’ 0 β†’ 1 β†’ 0 β†’ 5, where each 0 means you go the origin and grab one bag, and each positive integer means you deliver the bag to a depot at this coordinate, giving a total distance of 25 units. It must be noted that there are other sequences that give the same distance. In the second test case, you can follow the following sequence, among multiple such sequences, to travel minimum distance: 0 β†’ 6 β†’ 8 β†’ 7 β†’ 0 β†’ 5 β†’ 4 β†’ 3 β†’ 0 β†’ (-5) β†’ (-10) β†’ (-15), with distance 41. It can be shown that 41 is the optimal distance for this test case.
Petya has an array of integers a_1, a_2, …, a_n. He only likes sorted arrays. Unfortunately, the given array could be arbitrary, so Petya wants to sort it. Petya likes to challenge himself, so he wants to sort array using only 3-cycles. More formally, in one operation he can pick 3 pairwise distinct indices i, j, and k (1 ≀ i, j, k ≀ n) and apply i β†’ j β†’ k β†’ i cycle to the array a. It simultaneously places a_i on position j, a_j on position k, and a_k on position i, without changing any other element. For example, if a is [10, 50, 20, 30, 40, 60] and he chooses i = 2, j = 1, k = 5, then the array becomes [\underline{50}, \underline{40}, 20, 30, \underline{10}, 60]. Petya can apply arbitrary number of 3-cycles (possibly, zero). You are to determine if Petya can sort his array a, i. e. make it non-decreasing. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 5 β‹… 10^5). Description of the test cases follows. The first line of each test case contains a single integer n (1 ≀ n ≀ 5 β‹… 10^5) β€” the length of the array a. The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n). It is guaranteed that the sum of n over all test cases does not exceed 5 β‹… 10^5. Output For each test case, print "YES" (without quotes) if Petya can sort the array a using 3-cycles, and "NO" (without quotes) otherwise. You can print each letter in any case (upper or lower). Example Input 7 1 1 2 2 2 2 2 1 3 1 2 3 3 2 1 3 3 3 1 2 4 2 1 4 3 Output YES YES NO YES NO YES YES Note In the 6-th test case Petya can use the 3-cycle 1 β†’ 3 β†’ 2 β†’ 1 to sort the array. In the 7-th test case Petya can apply 1 β†’ 3 β†’ 2 β†’ 1 and make a = [1, 4, 2, 3]. Then he can apply 2 β†’ 4 β†’ 3 β†’ 2 and finally sort the array.
Petya has a rooted tree with an integer written on each vertex. The vertex 1 is the root. You are to answer some questions about the tree. A tree is a connected graph without cycles. A rooted tree has a special vertex called the root. The parent of a node v is the next vertex on the shortest path from v to the root. Each question is defined by three integers v, l, and k. To get the answer to the question, you need to perform the following steps: * First, write down the sequence of all integers written on the shortest path from the vertex v to the root (including those written in the v and the root). * Count the number of times each integer occurs. Remove all integers with less than l occurrences. * Replace the sequence, removing all duplicates and ordering the elements by the number of occurrences in the original list in increasing order. In case of a tie, you can choose the order of these elements arbitrary. * The answer to the question is the k-th number in the remaining sequence. Note that the answer is not always uniquely determined, because there could be several orderings. Also, it is possible that the length of the sequence on this step is less than k, in this case the answer is -1. For example, if the sequence of integers on the path from v to the root is [2, 2, 1, 7, 1, 1, 4, 4, 4, 4], l = 2 and k = 2, then the answer is 1. Please answer all questions about the tree. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^6). Description of the test cases follows. The first line of each test case contains two integers n, q (1 ≀ n, q ≀ 10^6) β€” the number of vertices in the tree and the number of questions. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ n), where a_i is the number written on the i-th vertex. The third line contains n-1 integers p_2, p_3, …, p_n (1 ≀ p_i ≀ n), where p_i is the parent of node i. It's guaranteed that the values p define a correct tree. Each of the next q lines contains three integers v, l, k (1 ≀ v, l, k ≀ n) β€” descriptions of questions. It is guaranteed that the sum of n and the sum of q over all test cases do not exceed 10^6. Output For each question of each test case print the answer to the question. In case of multiple answers, print any. Example Input 2 3 3 1 1 1 1 2 3 1 1 3 1 2 3 2 1 5 5 1 2 1 1 2 1 1 2 2 3 1 1 2 1 2 4 1 1 4 2 1 4 2 2 Output 1 -1 1 1 1 2 1 -1
You are given an array of n positive integers a_1, a_2, …, a_n. Your task is to calculate the number of arrays of n positive integers b_1, b_2, …, b_n such that: * 1 ≀ b_i ≀ a_i for every i (1 ≀ i ≀ n), and * b_i β‰  b_{i+1} for every i (1 ≀ i ≀ n - 1). The number of such arrays can be very large, so print it modulo 998 244 353. Input The first line contains a single integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the length of the array a. The second line contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^9). Output Print the answer modulo 998 244 353 in a single line. Examples Input 3 2 2 2 Output 2 Input 2 2 3 Output 4 Input 3 1 1 1 Output 0 Note In the first test case possible arrays are [1, 2, 1] and [2, 1, 2]. In the second test case possible arrays are [1, 2], [1, 3], [2, 1] and [2, 3].
Theofanis has a riddle for you and if you manage to solve it, he will give you a Cypriot snack halloumi for free (Cypriot cheese). You are given an integer n. You need to find two integers l and r such that -10^{18} ≀ l < r ≀ 10^{18} and l + (l + 1) + … + (r - 1) + r = n. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. The first and only line of each test case contains a single integer n (1 ≀ n ≀ 10^{18}). Output For each test case, print the two integers l and r such that -10^{18} ≀ l < r ≀ 10^{18} and l + (l + 1) + … + (r - 1) + r = n. It can be proven that an answer always exists. If there are multiple answers, print any. Example Input 7 1 2 3 6 100 25 3000000000000 Output 0 1 -1 2 1 2 1 3 18 22 -2 7 999999999999 1000000000001 Note In the first test case, 0 + 1 = 1. In the second test case, (-1) + 0 + 1 + 2 = 2. In the fourth test case, 1 + 2 + 3 = 6. In the fifth test case, 18 + 19 + 20 + 21 + 22 = 100. In the sixth test case, (-2) + (-1) + 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 = 25.
Theofanis really likes sequences of positive integers, thus his teacher (Yeltsa Kcir) gave him a problem about a sequence that consists of only special numbers. Let's call a positive number special if it can be written as a sum of different non-negative powers of n. For example, for n = 4 number 17 is special, because it can be written as 4^0 + 4^2 = 1 + 16 = 17, but 9 is not. Theofanis asks you to help him find the k-th special number if they are sorted in increasing order. Since this number may be too large, output it modulo 10^9+7. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. The first and only line of each test case contains two integers n and k (2 ≀ n ≀ 10^9; 1 ≀ k ≀ 10^9). Output For each test case, print one integer β€” the k-th special number in increasing order modulo 10^9+7. Example Input 3 3 4 2 12 105 564 Output 9 12 3595374 Note For n = 3 the sequence is [1,3,4,9...]
Theofanis has a string s_1 s_2 ... s_n and a character c. He wants to make all characters of the string equal to c using the minimum number of operations. In one operation he can choose a number x (1 ≀ x ≀ n) and for every position i, where i is not divisible by x, replace s_i with c. Find the minimum number of operations required to make all the characters equal to c and the x-s that he should use in his operations. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. The first line of each test case contains the integer n (3 ≀ n ≀ 3 β‹… 10^5) and a lowercase Latin letter c β€” the length of the string s and the character the resulting string should consist of. The second line of each test case contains a string s of lowercase Latin letters β€” the initial string. It is guaranteed that the sum of n over all test cases does not exceed 3 β‹… 10^5. Output For each test case, firstly print one integer m β€” the minimum number of operations required to make all the characters equal to c. Next, print m integers x_1, x_2, ..., x_m (1 ≀ x_j ≀ n) β€” the x-s that should be used in the order they are given. It can be proved that under given constraints, an answer always exists. If there are multiple answers, print any. Example Input 3 4 a aaaa 4 a baaa 4 b bzyx Output 0 1 2 2 2 3 Note Let's describe what happens in the third test case: 1. x_1 = 2: we choose all positions that are not divisible by 2 and replace them, i. e. bzyx β†’ bzbx; 2. x_2 = 3: we choose all positions that are not divisible by 3 and replace them, i. e. bzbx β†’ bbbb.
Theofanis started playing the new online game called "Among them". However, he always plays with Cypriot players, and they all have the same name: "Andreas" (the most common name in Cyprus). In each game, Theofanis plays with n other players. Since they all have the same name, they are numbered from 1 to n. The players write m comments in the chat. A comment has the structure of "i j c" where i and j are two distinct integers and c is a string (1 ≀ i, j ≀ n; i β‰  j; c is either imposter or crewmate). The comment means that player i said that player j has the role c. An imposter always lies, and a crewmate always tells the truth. Help Theofanis find the maximum possible number of imposters among all the other Cypriot players, or determine that the comments contradict each other (see the notes for further explanation). Note that each player has exactly one role: either imposter or crewmate. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Description of each test case follows. The first line of each test case contains two integers n and m (1 ≀ n ≀ 2 β‹… 10^5; 0 ≀ m ≀ 5 β‹… 10^5) β€” the number of players except Theofanis and the number of comments. Each of the next m lines contains a comment made by the players of the structure "i j c" where i and j are two distinct integers and c is a string (1 ≀ i, j ≀ n; i β‰  j; c is either imposter or crewmate). There can be multiple comments for the same pair of (i, j). It is guaranteed that the sum of all n does not exceed 2 β‹… 10^5 and the sum of all m does not exceed 5 β‹… 10^5. Output For each test case, print one integer β€” the maximum possible number of imposters. If the comments contradict each other, print -1. Example Input 5 3 2 1 2 imposter 2 3 crewmate 5 4 1 3 crewmate 2 5 crewmate 2 4 imposter 3 4 imposter 2 2 1 2 imposter 2 1 crewmate 3 5 1 2 imposter 1 2 imposter 3 2 crewmate 3 2 crewmate 1 3 imposter 5 0 Output 2 4 -1 2 5 Note In the first test case, imposters can be Andreas 2 and 3. In the second test case, imposters can be Andreas 1, 2, 3 and 5. In the third test case, comments contradict each other. This is because player 1 says that player 2 is an imposter, and player 2 says that player 1 is a crewmate. If player 1 is a crewmate, then he must be telling the truth, so player 2 must be an imposter. But if player 2 is an imposter then he must be lying, so player 1 can't be a crewmate. Contradiction.
It is the easy version of the problem. The difference is that in this version, there are no nodes with already chosen colors. Theofanis is starving, and he wants to eat his favorite food, sheftalia. However, he should first finish his homework. Can you help him with this problem? You have a perfect binary tree of 2^k - 1 nodes β€” a binary tree where all vertices i from 1 to 2^{k - 1} - 1 have exactly two children: vertices 2i and 2i + 1. Vertices from 2^{k - 1} to 2^k - 1 don't have any children. You want to color its vertices with the 6 Rubik's cube colors (White, Green, Red, Blue, Orange and Yellow). Let's call a coloring good when all edges connect nodes with colors that are neighboring sides in the Rubik's cube. <image>| <image> ---|--- A picture of Rubik's cube and its 2D map. More formally: * a white node can not be neighboring with white and yellow nodes; * a yellow node can not be neighboring with white and yellow nodes; * a green node can not be neighboring with green and blue nodes; * a blue node can not be neighboring with green and blue nodes; * a red node can not be neighboring with red and orange nodes; * an orange node can not be neighboring with red and orange nodes; You want to calculate the number of the good colorings of the binary tree. Two colorings are considered different if at least one node is colored with a different color. The answer may be too large, so output the answer modulo 10^9+7. Input The first and only line contains the integers k (1 ≀ k ≀ 60) β€” the number of levels in the perfect binary tree you need to color. Output Print one integer β€” the number of the different colorings modulo 10^9+7. Examples Input 3 Output 24576 Input 14 Output 934234 Note In the picture below, you can see one of the correct colorings of the first example. <image>
It is the hard version of the problem. The difference is that in this version, there are nodes with already chosen colors. Theofanis is starving, and he wants to eat his favorite food, sheftalia. However, he should first finish his homework. Can you help him with this problem? You have a perfect binary tree of 2^k - 1 nodes β€” a binary tree where all vertices i from 1 to 2^{k - 1} - 1 have exactly two children: vertices 2i and 2i + 1. Vertices from 2^{k - 1} to 2^k - 1 don't have any children. You want to color its vertices with the 6 Rubik's cube colors (White, Green, Red, Blue, Orange and Yellow). Let's call a coloring good when all edges connect nodes with colors that are neighboring sides in the Rubik's cube. <image>| <image> ---|--- A picture of Rubik's cube and its 2D map. More formally: * a white node can not be neighboring with white and yellow nodes; * a yellow node can not be neighboring with white and yellow nodes; * a green node can not be neighboring with green and blue nodes; * a blue node can not be neighboring with green and blue nodes; * a red node can not be neighboring with red and orange nodes; * an orange node can not be neighboring with red and orange nodes; However, there are n special nodes in the tree, colors of which are already chosen. You want to calculate the number of the good colorings of the binary tree. Two colorings are considered different if at least one node is colored with a different color. The answer may be too large, so output the answer modulo 10^9+7. Input The first line contains the integers k (1 ≀ k ≀ 60) β€” the number of levels in the perfect binary tree you need to color. The second line contains the integer n (1 ≀ n ≀ min(2^k - 1, 2000)) β€” the number of nodes, colors of which are already chosen. The next n lines contains integer v (1 ≀ v ≀ 2^k - 1) and string s β€” the index of the node and the color of the node (s is one of the white, yellow, green, blue, red and orange). It is guaranteed that each node v appears in the input at most once. Output Print one integer β€” the number of the different colorings modulo 10^9+7. Examples Input 3 2 5 orange 2 white Output 1024 Input 2 2 1 white 2 white Output 0 Input 10 3 1 blue 4 red 5 orange Output 328925088 Note In the picture below, you can see one of the correct colorings of the first test example. <image>
Theofanis decided to visit his uncle's farm. There are s animals and n animal pens on the farm. For utility purpose, animal pens are constructed in one row. Uncle told Theofanis that a farm is lucky if you can distribute all animals in all pens in such a way that there are no empty pens and there is at least one continuous segment of pens that has exactly k animals in total. Moreover, a farm is ideal if it's lucky for any distribution without empty pens. Neither Theofanis nor his uncle knows if their farm is ideal or not. Can you help them to figure it out? Input The first line contains a single integer t (1 ≀ t ≀ 10^5) β€” the number of test cases. The first and only line of each test case contains three integers s, n, and k (1 ≀ s, n, k ≀ 10^{18}; n ≀ s). Output For each test case, print YES (case-insensitive), if the farm is ideal, or NO (case-insensitive) otherwise. Example Input 4 1 1 1 1 1 2 100 50 200 56220 47258 14497 Output YES NO NO YES Note For the first and the second test case, the only possible combination is [1] so there always will be a subsegment with 1 animal but not with 2 animals.
Monocarp is playing a computer game. Now he wants to complete the first level of this game. A level is a rectangular grid of 2 rows and n columns. Monocarp controls a character, which starts in cell (1, 1) β€” at the intersection of the 1-st row and the 1-st column. Monocarp's character can move from one cell to another in one step if the cells are adjacent by side and/or corner. Formally, it is possible to move from cell (x_1, y_1) to cell (x_2, y_2) in one step if |x_1 - x_2| ≀ 1 and |y_1 - y_2| ≀ 1. Obviously, it is prohibited to go outside the grid. There are traps in some cells. If Monocarp's character finds himself in such a cell, he dies, and the game ends. To complete a level, Monocarp's character should reach cell (2, n) β€” at the intersection of row 2 and column n. Help Monocarp determine if it is possible to complete the level. Input The first line contains a single integer t (1 ≀ t ≀ 100) β€” the number of test cases. Then the test cases follow. Each test case consists of three lines. The first line contains a single integer n (3 ≀ n ≀ 100) β€” the number of columns. The next two lines describe the level. The i-th of these lines describes the i-th line of the level β€” the line consists of the characters '0' and '1'. The character '0' corresponds to a safe cell, the character '1' corresponds to a trap cell. Additional constraint on the input: cells (1, 1) and (2, n) are safe. Output For each test case, output YES if it is possible to complete the level, and NO otherwise. Example Input 4 3 000 000 4 0011 1100 4 0111 1110 6 010101 101010 Output YES YES NO YES Note Consider the example from the statement. In the first test case, one of the possible paths is (1, 1) β†’ (2, 2) β†’ (2, 3). In the second test case, one of the possible paths is (1, 1) β†’ (1, 2) β†’ (2, 3) β†’ (2, 4). In the fourth test case, one of the possible paths is (1, 1) β†’ (2, 2) β†’ (1, 3) β†’ (2, 4) β†’ (1, 5) β†’ (2, 6).
n students attended the first meeting of the Berland SU programming course (n is even). All students will be divided into two groups. Each group will be attending exactly one lesson each week during one of the five working days (Monday, Tuesday, Wednesday, Thursday and Friday), and the days chosen for the groups must be different. Furthermore, both groups should contain the same number of students. Each student has filled a survey in which they told which days of the week are convenient for them to attend a lesson, and which are not. Your task is to determine if it is possible to choose two different week days to schedule the lessons for the group (the first group will attend the lesson on the first chosen day, the second group will attend the lesson on the second chosen day), and divide the students into two groups, so the groups have equal sizes, and for each student, the chosen lesson day for their group is convenient. Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of testcases. Then the descriptions of t testcases follow. The first line of each testcase contains one integer n (2 ≀ n ≀ 1 000) β€” the number of students. The i-th of the next n lines contains 5 integers, each of them is 0 or 1. If the j-th integer is 1, then the i-th student can attend the lessons on the j-th day of the week. If the j-th integer is 0, then the i-th student cannot attend the lessons on the j-th day of the week. Additional constraints on the input: for each student, at least one of the days of the week is convenient, the total number of students over all testcases doesn't exceed 10^5. Output For each testcase print an answer. If it's possible to divide the students into two groups of equal sizes and choose different days for the groups so each student can attend the lesson in the chosen day of their group, print "YES" (without quotes). Otherwise, print "NO" (without quotes). Example Input 2 4 1 0 0 1 0 0 1 0 0 1 0 0 0 1 0 0 1 0 1 0 2 0 0 0 1 0 0 0 0 1 0 Output YES NO Note In the first testcase, there is a way to meet all the constraints. For example, the first group can consist of the first and the third students, they will attend the lessons on Thursday (the fourth day); the second group can consist of the second and the fourth students, and they will attend the lessons on Tuesday (the second day). In the second testcase, it is impossible to divide the students into groups so they attend the lessons on different days.
Monocarp has got an array a consisting of n integers. Let's denote k as the mathematic mean of these elements (note that it's possible that k is not an integer). The mathematic mean of an array of n elements is the sum of elements divided by the number of these elements (i. e. sum divided by n). Monocarp wants to delete exactly two elements from a so that the mathematic mean of the remaining (n - 2) elements is still equal to k. Your task is to calculate the number of pairs of positions [i, j] (i < j) such that if the elements on these positions are deleted, the mathematic mean of (n - 2) remaining elements is equal to k (that is, it is equal to the mathematic mean of n elements of the original array a). Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” the number of testcases. The first line of each testcase contains one integer n (3 ≀ n ≀ 2 β‹… 10^5) β€” the number of elements in the array. The second line contains a sequence of integers a_1, a_2, ..., a_n (0 ≀ a_i ≀ 10^{9}), where a_i is the i-th element of the array. The sum of n over all testcases doesn't exceed 2 β‹… 10^5. Output Print one integer β€” the number of pairs of positions [i, j] (i < j) such that if the elements on these positions are deleted, the mathematic mean of (n - 2) remaining elements is equal to k (that is, it is equal to the mathematic mean of n elements of the original array a). Example Input 4 4 8 8 8 8 3 50 20 10 5 1 4 7 3 5 7 1 2 3 4 5 6 7 Output 6 0 2 3 Note In the first example, any pair of elements can be removed since all of them are equal. In the second example, there is no way to delete two elements so the mathematic mean doesn't change. In the third example, it is possible to delete the elements on positions 1 and 3, or the elements on positions 4 and 5.
Monocarp is the coach of the Berland State University programming teams. He decided to compose a problemset for a training session for his teams. Monocarp has n problems that none of his students have seen yet. The i-th problem has a topic a_i (an integer from 1 to n) and a difficulty b_i (an integer from 1 to n). All problems are different, that is, there are no two tasks that have the same topic and difficulty at the same time. Monocarp decided to select exactly 3 problems from n problems for the problemset. The problems should satisfy at least one of two conditions (possibly, both): * the topics of all three selected problems are different; * the difficulties of all three selected problems are different. Your task is to determine the number of ways to select three problems for the problemset. Input The first line contains a single integer t (1 ≀ t ≀ 50000) β€” the number of testcases. The first line of each testcase contains an integer n (3 ≀ n ≀ 2 β‹… 10^5) β€” the number of problems that Monocarp have. In the i-th of the following n lines, there are two integers a_i and b_i (1 ≀ a_i, b_i ≀ n) β€” the topic and the difficulty of the i-th problem. It is guaranteed that there are no two problems that have the same topic and difficulty at the same time. The sum of n over all testcases doesn't exceed 2 β‹… 10^5. Output Print the number of ways to select three training problems that meet either of the requirements described in the statement. Example Input 2 4 2 4 3 4 2 1 1 3 5 1 5 2 4 3 3 4 2 5 1 Output 3 10 Note In the first example, you can take the following sets of three problems: * problems 1, 2, 4; * problems 1, 3, 4; * problems 2, 3, 4. Thus, the number of ways is equal to three.
You are given a matrix, consisting of n rows and m columns. The rows are numbered top to bottom, the columns are numbered left to right. Each cell of the matrix can be either free or locked. Let's call a path in the matrix a staircase if it: * starts and ends in the free cell; * visits only free cells; * has one of the two following structures: 1. the second cell is 1 to the right from the first one, the third cell is 1 to the bottom from the second one, the fourth cell is 1 to the right from the third one, and so on; 2. the second cell is 1 to the bottom from the first one, the third cell is 1 to the right from the second one, the fourth cell is 1 to the bottom from the third one, and so on. In particular, a path, consisting of a single cell, is considered to be a staircase. Here are some examples of staircases: <image> Initially all the cells of the matrix are free. You have to process q queries, each of them flips the state of a single cell. So, if a cell is currently free, it makes it locked, and if a cell is currently locked, it makes it free. Print the number of different staircases after each query. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path. Input The first line contains three integers n, m and q (1 ≀ n, m ≀ 1000; 1 ≀ q ≀ 10^4) β€” the sizes of the matrix and the number of queries. Each of the next q lines contains two integers x and y (1 ≀ x ≀ n; 1 ≀ y ≀ m) β€” the description of each query. Output Print q integers β€” the i-th value should be equal to the number of different staircases after i queries. Two staircases are considered different if there exists such a cell that appears in one path and doesn't appear in the other path. Examples Input 2 2 8 1 1 1 1 1 1 2 2 1 1 1 2 2 1 1 1 Output 5 10 5 2 5 3 1 0 Input 3 4 10 1 4 1 2 2 3 1 2 2 3 3 2 1 3 3 4 1 3 3 1 Output 49 35 24 29 49 39 31 23 29 27 Input 1000 1000 2 239 634 239 634 Output 1332632508 1333333000
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: * bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); * bracket sequences ")(", "(" and ")" are not. Let's denote the concatenation of two strings x and y as x+y. For example, "()()" + ")(" = "()())(". You are given n bracket sequences s_1, s_2, ..., s_n. You can rearrange them in any order (you can rearrange only the strings themselves, but not the characters in them). Your task is to rearrange the strings in such a way that the string s_1 + s_2 + ... + s_n has as many non-empty prefixes that are RBS as possible. Input The first line contains a single integer n (1 ≀ n ≀ 20). Then n lines follow, the i-th of them contains s_i β€” a bracket sequence (a string consisting of characters "(" and/or ")". All sequences s_i are non-empty, their total length does not exceed 4 β‹… 10^5. Output Print one integer β€” the maximum number of non-empty prefixes that are RBS for the string s_1 + s_2 + ... + s_n, if the strings s_1, s_2, ..., s_n can be rearranged arbitrarily. Examples Input 2 ( ) Output 1 Input 4 ()()()) ( ( ) Output 4 Input 1 (()) Output 1 Input 1 )(() Output 0 Note In the first example, you can concatenate the strings as follows: "(" + ")" = "()", the resulting string will have one prefix, that is an RBS: "()". In the second example, you can concatenate the strings as follows: "(" + ")" + "()()())" + "(" = "()()()())(", the resulting string will have four prefixes that are RBS: "()", "()()", "()()()", "()()()()". The third and the fourth examples contain only one string each, so the order is fixed.
Let's call a positive integer good if there is no digit 0 in its decimal representation. For an array of a good numbers a, one found out that the sum of some two neighboring elements is equal to x (i.e. x = a_i + a_{i + 1} for some i). x had turned out to be a good number as well. Then the elements of the array a were written out one after another without separators into one string s. For example, if a = [12, 5, 6, 133], then s = 1256133. You are given a string s and a number x. Your task is to determine the positions in the string that correspond to the adjacent elements of the array that have sum x. If there are several possible answers, you can print any of them. Input The first line contains the string s (2 ≀ |s| ≀ 5 β‹… 10^5). The second line contains an integer x (2 ≀ x < 10^{200000}). An additional constraint on the input: the answer always exists, i.e you can always select two adjacent substrings of the string s so that if you convert these substrings to integers, their sum is equal to x. Output In the first line, print two integers l_1, r_1, meaning that the first term of the sum (a_i) is in the string s from position l_1 to position r_1. In the second line, print two integers l_2, r_2, meaning that the second term of the sum (a_{i + 1}) is in the string s from position l_2 to position r_2. Examples Input 1256133 17 Output 1 2 3 3 Input 9544715561 525 Output 2 3 4 6 Input 239923 5 Output 1 1 2 2 Input 1218633757639 976272 Output 2 7 8 13 Note In the first example s[1;2] = 12 and s[3;3] = 5, 12+5=17. In the second example s[2;3] = 54 and s[4;6] = 471, 54+471=525. In the third example s[1;1] = 2 and s[2;2] = 3, 2+3=5. In the fourth example s[2;7] = 218633 and s[8;13] = 757639, 218633+757639=976272.
You are given an array A of length N weights of masses A_1, A_2...A_N. No two weights have the same mass. You can put every weight on one side of the balance (left or right). You don't have to put weights in order A_1,...,A_N. There is also a string S consisting of characters "L" and "R", meaning that after putting the i-th weight (not A_i, but i-th weight of your choice) left or right side of the balance should be heavier. Find the order of putting the weights on the balance such that rules of string S are satisfied. Input The first line contains one integer N (1 ≀ N ≀ 2*10^5) - the length of the array A The second line contains N distinct integers: A_1, A_2,...,A_N (1 ≀ A_i ≀ 10^9) - the weights given The third line contains string S of length N consisting only of letters "L" and "R" - string determining which side of the balance should be heavier after putting the i-th weight of your choice Output The output contains N lines. In every line, you should print one integer and one letter - integer representing the weight you are putting on the balance in that move and the letter representing the side of the balance where you are putting the weight. If there is no solution, print -1. Example Input 5 3 8 2 13 7 LLRLL Output 3 L 2 R 8 R 13 L 7 L Note Explanation for the test case: after the 1st weight: 3 L (left side is heavier) after the 2nd weight: 2 R (left side is heavier) after the 3rd weight: 8 R (right side is heavier) after the 4th weight: 13 L (left side is heavier) after the 5th weight: 7 L (left side is heavier) So, the rules given by string S are fulfilled and our order of putting the weights is correct.
Alice and Bob always had hard time choosing restaurant for the dinner. Previously they performed Eenie Meenie Miney Mo game, but eventually as their restaurant list grew, they had to create a new game. This new game starts as they write restaurant names on N cards and align the cards in one line. Before the game begins, they both choose starting card and starting direction they are going to. They take turns in order one after another. After each turn, they move one card in their current direction. If they reach the end or beginning of the line of cards they change direction. Once they meet in a card, the card is marked for removal and is removed the first moment they both leave the card. <image> Example of how card is removed They repeat this process until there is only one restaurant card left. Since there are a lot of restaurant cards, they are bored to simulate this process over and over and need your help to determine the last card that remains. Can you help them? Input The first line of the input is one integer T (1 ≀ T ≀ 10^{4}) representing number of test cases. Each test case contains 3 lines: The first line contains an integer N representing initial number of cards. Next line contains two integer values A,B (0 ≀ A, B < N, 2 ≀ N ≀ 10^{18}) representing starting 0-based index of the card in the array. Last line contains two strings D_A, D_B ∈ {"left", "right"} representing starting direction of their movement. Output The output contains T integer number – the 0-based index of the last card that remains for every test case in order. Example Input 1 4 0 1 left right Output 0 Note Note that since Alice is starting at the beginning of the line even though her initial direction is left, on her next move she will go right.
Little Johnny Bubbles enjoys spending hours in front of his computer playing video games. His favorite game is Bubble Strike, fast-paced bubble shooting online game for two players. Each game is set in one of the N maps, each having different terrain configuration. First phase of each game decides on which map the game will be played. The game system randomly selects three maps and shows them to the players. Each player must pick one of those three maps to be discarded. The game system then randomly selects one of the maps that were not picked by any of the players and starts the game. Johnny is deeply enthusiastic about the game and wants to spend some time studying maps, thus increasing chances to win games played on those maps. However, he also needs to do his homework, so he does not have time to study all the maps. That is why he asked himself the following question: "What is the minimum number of maps I have to study, so that the probability to play one of those maps is at least P"? Can you help Johnny find the answer for this question? You can assume Johnny's opponents do not know him, and they will randomly pick maps. Input The first line contains two integers N (3 ≀ N ≀ 10^{3}) and P (0 ≀ P ≀ 1) – total number of maps in the game and probability to play map Johnny has studied. P will have at most four digits after the decimal point. Output Output contains one integer number – minimum number of maps Johnny has to study. Example Input 7 1.0000 Output 6
There are N bubbles in a coordinate plane. Bubbles are so tiny that it can be assumed that each bubble is a point (X_i, Y_i). Q Bubble Cup finalists plan to play with the bubbles. Each finalist would link to use infinitely long Bubble Cup stick to pop some bubbles. The i-th finalist would like to place the stick in the direction of vector (dxi, dyi), and plays the following game until K_i bubbles are popped. The game starts with finalist placing the stick in the direction of vector (dx_i, dy_i), and sweeping it from the infinity to the left until it hits some bubble, which is immediately popped. It is guaranteed that only one bubble will be hit in this step. After that the finalist starts rotating the stick in the counter clockwise direction with the center of rotation in point where the previous bubble was popped. When the next bubble is hit, it is immediately popped and becomes the new center of rotation. The process continues until K_i bubbles have been popped. It is guaranteed that the stick won't hit two bubbles simultaneously in this process. For each finalist find which bubble would be popped the last. Note that each game starts with the configuration of all N bubbles, so the games don't depend on the previous games. Input The first line contains one integer N β€” the number of bubbles. (1 ≀ N ≀ 10^5) Each of the next N lines contains two integers. The i-th line contains integers X_i and Y_i β€” the coordinates of the i-th bubble. (-10^9 ≀ X_i, Y_i ≀ 10^9, (X_i, Y_i) β‰  (X_j, Y_j) for i β‰  j) The next line contains one integer Q β€” the number of finalists willing to play with the bubbles. (1 ≀ Q ≀ 10^5) Each of the next Q lines contains 3 integers. The i-th line contains integers dx_i, dy_i and K_i. (-10^9 ≀ dx_i, dy_i ≀ 10^9, 1 ≀ K_i ≀ N) Output For each of the Q finalists, print the index of the bubble which would be popped last, in the separate line. Examples Input 4 0 0 1 0 0 1 1 1 2 1 -1 3 -1 1 4 Output 4 2 Input 4 1 1 2 2 7 1 1 7 3 2 2 1 1 -5 4 -6 5 3 Output 3 2 3 Note There are two finalists willing to play with the bubbles. If the first finalist plays with the bubbles, then the bubbles at coordinates (0, 0), (1, 0) and (1, 1) would be popped in that order. Their indexes are 1, 2 and 4, so the answer is 4. If the second finalist plays with the bubbles, then the bubbles at coordinates (1, 1), (0, 1), (0, 0) and (1, 0) would be popped in that order, so the answer is 2. Visualization: [link](https://petljamediastorage.blob.core.windows.net/uploads/example1.gif).
You are given two integer arrays of length N, A1 and A2. You are also given Q queries of 4 types: 1 k l r x: set Ak_i:=min(Ak_i, x) for each l ≀ i ≀ r. 2 k l r x: set Ak_i:=max(Ak_i, x) for each l ≀ i ≀ r. 3 k l r x: set Ak_i:=Ak_i+x for each l ≀ i ≀ r. 4 l r: find the (βˆ‘_{i=l}^r F(A1_i+A2_i)) \% (10^9+7) where F(k) is the k-th Fibonacci number (F(0)=0, F(1)=1, F(k)=F(k-1)+F(k-2)), and x \% y denotes the remainder of the division of x by y. You should process these queries and answer each query of the fourth type. Input The first line contains two integers N and Q. (1 ≀ N, Q ≀ 5 Γ— 10^4) The second line contains N integers, array A1_1, A1_2, ... A1_N. (0 ≀ A1_i ≀ 10^6) The third line contains N integers, array A2_1, A2_2, ... A2_N. (0 ≀ A2_i ≀ 10^6) The next Q lines describe the queries. Each line contains 5 or 3 integers, where the first integer denotes the type of the query. (k ∈ \{1, 2\}, 1 ≀ l ≀ r ≀ N) For queries of type 1 and 2, 0 ≀ x ≀ 10^9 holds. For queries of type 3, βˆ’10^6 ≀ x ≀ 10^6 holds. It is guaranteed that after every query each number in arrays A1 and A2 will be nonnegative. Output Print the answer to each query of the fourth type, in separate lines. Examples Input 3 4 1 0 2 2 1 0 4 1 3 3 2 2 2 3 1 1 1 3 0 4 1 3 Output 4 4 Input 5 4 1 3 5 3 2 4 2 1 3 3 4 1 3 4 2 5 2 1 2 4 6 4 2 4 Output 18 26 68 Note In the first example: The answer for the first query is F(1 + 2) + F(0 + 1) + F(2 + 0) = F(3) + F(1) + F(2) = 2 + 1 + 1 = 4. After the second query, the array A2 changes to [2, 4, 0]. After the third query, the array A1 changes to [0, 0, 0]. The answer for the fourth query is F(0 + 2) + F(0 + 4) + F(0 + 0) = F(2) + F(4) + F(0) = 1 + 3 + 0 = 4. In the second example: The answer for the first query is F(1 + 4) + F(3 + 2) + F(5 + 1) = F(5) + F(5) + F(6) = 5 + 5 + 8 = 18. The answer for the second query is F(3 + 2) + F(5 + 1) + F(3 + 3) + F(2 + 3) = F(5) + F(6) + F(6) + F(5) = 5 + 8 + 8 + 5 = 26. After the third query, the array A1 changes to [1, 6, 6, 6, 2]. The answer for the fourth query is F(6 + 2) + F(6 + 1) + F(6 + 3) = F(8) + F(7) + F(9) = 21 + 13 + 34 = 68.
In the year 2420 humans have finally built a colony on Mars thanks to the work of Elon Tusk. There are 10^9+7 cities arranged in a circle in this colony and none of them are connected yet. Elon Tusk wants to connect some of those cities using only roads of the same size in order to lower the production cost of those roads. Because of that he gave a list on N cites where some cites can appear more than once and Q queries that you need to answer. For the query you need to determine if it is possible to connect all the cities from L_{i} to R_{i} on that list using only roads of length D_{i}. Input The first line contains two integers N and Q (1 ≀ N, Q ≀ 2β‹…10^5 ) β€” the length of the array of cities and the number of queries you need to answer. The second lines contains N integers representing the array of cites. Next Q lines contain three integers L, R and D (1 ≀ L_{i}, R_{i} ≀ N, 0 ≀ D_{i} ≀ 10^9+6) β€” the range of cities that needs to be connected and the length of the road that you can use. Output The output contains Q lines. If it is possible to connect all the cities from the i-th query can be connected with roads of length D_{i} the i-th line should contain the word "Yes", otherwise it should contain the word "No". Examples Input 9 8 17 0 12 6 10 8 2 4 5 2 3 12 2 3 6 2 4 6 4 6 2 2 8 2 1 2 17 1 8 2 9 9 14 Output Yes No Yes Yes Yes Yes No Yes Input 4 1 7 21 14 0 1 4 1000000000 Output Yes Note In the 5^{th} query of the first test case we can connect cities in this order 0-2-4-6-8-10-12 this way distance between any two connected cities is 2. In the second test case we can connect cities in this order 21-14-7-0 this way distance between any two connected cities is 10^9 module 10^9+7.
You are given N points on an infinite plane with the Cartesian coordinate system on it. N-1 points lay on one line, and one point isn't on that line. You are on point K at the start, and the goal is to visit every point. You can move between any two points in a straight line, and you can revisit points. What is the minimum length of the path? Input The first line contains two integers: N (3 ≀ N ≀ 2*10^5) - the number of points, and K (1 ≀ K ≀ N) - the index of the starting point. Each of the next N lines contain two integers, A_i, B_i (-10^6 ≀ A_i, B_i ≀ 10^6) - coordinates of the i-th point. Output The output contains one number - the shortest path to visit all given points starting from point K. The absolute difference between your solution and the main solution shouldn't exceed 10^-6; Example Input 5 2 0 0 -1 1 2 -2 0 1 -2 2 Output 7.478709 Note The shortest path consists of these moves: 2 -> 5 5 -> 4 4 -> 1 1 -> 3 There isn't any shorter path possible.
This is an interactive problem! As part of your contribution in the Great Bubble War, you have been tasked with finding the newly built enemy fortress. The world you live in is a giant 10^9 Γ— 10^9 grid, with squares having both coordinates between 1 and 10^9. You know that the enemy base has the shape of a rectangle, with the sides parallel to the sides of the grid. The people of your world are extremely scared of being at the edge of the world, so you know that the base doesn't contain any of the squares on the edges of the grid (the x or y coordinate being 1 or 10^9). To help you locate the base, you have been given a device that you can place in any square of the grid, and it will tell you the manhattan distance to the closest square of the base. The manhattan distance from square (a, b) to square (p, q) is calculated as |aβˆ’p|+|bβˆ’q|. If you try to place the device inside the enemy base, you will be captured by the enemy. Because of this, you need to make sure to never place the device inside the enemy base. Unfortunately, the device is powered by a battery and you can't recharge it. This means that you can use the device at most 40 times. Input The input contains the answers to your queries. Interaction Your code is allowed to place the device on any square in the grid by writing "? i j" (1 ≀ i,j ≀ 10^9). In return, it will recieve the manhattan distance to the closest square of the enemy base from square (i,j) or -1 if the square you placed the device on is inside the enemy base or outside the grid. If you recieve -1 instead of a positive number, exit immidiately and you will see the wrong answer verdict. Otherwise, you can get an arbitrary verdict because your solution will continue to read from a closed stream. Your solution should use no more than 40 queries. Once you are sure where the enemy base is located, you should print "! x y p q" (1 ≀ x ≀ p≀ 10^9, 1 ≀ y ≀ q≀ 10^9), where (x, y) is the square inside the enemy base with the smallest x and y coordinates, and (p, q) is the square inside the enemy base with the largest x and y coordinates. Note that answering doesn't count as one of the 40 queries. After printing a query or printing the answer, do not forget to output end of line and flush the output. Otherwise, you will get idleness limit exceeded. To do this, use: * fflush(stdout) or cout.flush() in C++; * System.out.flush() in Java; * flush(output) in Pascal; * stdout.flush() in Python; * See the documentation for other languages. Example Input 1 1 2 1 Output ? 2 2 ? 5 5 ? 4 7 ? 1 5 ! 2 3 4 5
You are given an undirected graph of N nodes and M edges, E_1, E_2, ... E_M. A connected graph is a cactus if each of it's edges belogs to at most one simple cycle. A graph is a desert if each of it's connected components is a cactus. Find the number of pairs (L, R), (1 ≀ L ≀ R ≀ M) such that, if we delete all the edges except for E_L, E_{L+1}, ... E_R, the graph is a desert. Input The first line contains two integers N and M (2 ≀ N ≀ 2.5 Γ— 10^5, 1 ≀ M ≀ 5 Γ— 10^5). Each of the next M lines contains two integers. The i-th line describes the i-th edge. It contains integers U_i and V_i, the nodes connected by the i-th edge (E_i=(U_i, V_i)). It is guaranteed that 1 ≀ U_i, V_i ≀ N and U_i β‰  V_i. Output The output contains one integer number – the answer. Examples Input 5 6 1 2 2 3 3 4 4 5 5 1 2 4 Output 20 Input 2 3 1 2 1 2 1 2 Output 5 Note In the second example: Graphs for pairs (1, 1), (2, 2) and (3, 3) are deserts because they don't have any cycles. Graphs for pairs (1, 2) and (2, 3) have one cycle of length 2 so they are deserts.
Bob really likes playing with arrays of numbers. That's why for his birthday, his friends bought him a really interesting machine – an array beautifier. The array beautifier takes an array A consisting of N integers, and it outputs a new array B of length N that it constructed based on the array given to it. The array beautifier constructs the new array in the following way: it takes two numbers at different indices from the original array and writes their sum to the end of the new array. It does this step N times - resulting in an output array of length N. During this process, the machine can take the same index multiple times in different steps. Bob was very excited about the gift that his friends gave him, so he put his favorite array in the machine. However, when the machine finished, Bob was not happy with the resulting array. He misses his favorite array very much, and hopes to get it back. Given the array that the machine outputted, help Bob find an array that could be the original array that he put in the machine. Sometimes the machine makes mistakes, so it is possible that no appropriate input array exists for the array it has outputted. In such case, let Bob know that his array is forever lost. Input The first line contains one positive integer N (2 ≀ N ≀ 10^3) – the length of Bob's array. The second line contains N integers B_1, B_2, ..., B_N (1 ≀ B_i ≀ 10^6) – the elements of the array the machine outputted. Output If an appropriate input array exists, print "YES", followed by the input array A_1, A_2, ..., A_N (-10^9 ≀ A_i ≀ 10^9) in the next line. Otherwise, print "NO". Examples Input 2 5 5 Output YES 2 3 Input 3 1 2 3 Output YES 0 1 2 Input 3 2 4 5 Output NO Input 4 1 3 5 7 Output YES 6 -3 4 1
Alice and Bob are playing a game. They are given an array A of length N. The array consists of integers. They are building a sequence together. In the beginning, the sequence is empty. In one turn a player can remove a number from the left or right side of the array and append it to the sequence. The rule is that the sequence they are building must be strictly increasing. The winner is the player that makes the last move. Alice is playing first. Given the starting array, under the assumption that they both play optimally, who wins the game? Input The first line contains one integer N (1 ≀ N ≀ 2*10^5) - the length of the array A. The second line contains N integers A_1, A_2,...,A_N (0 ≀ A_i ≀ 10^9) Output The first and only line of output consists of one string, the name of the winner. If Alice won, print "Alice", otherwise, print "Bob". Examples Input 1 5 Output Alice Input 3 5 4 5 Output Alice Input 6 5 8 2 1 10 9 Output Bob
On the great island of Baltia, there live N people, numbered from 1 to N. There are exactly M pairs of people that are friends with each other. The people of Baltia want to organize a successful party, but they have very strict rules on what a party is and when the party is successful. On the island of Baltia, a party is a gathering of exactly 5 people. The party is considered to be successful if either all the people at the party are friends with each other (so that they can all talk to each other without having to worry about talking to someone they are not friends with) or no two people at the party are friends with each other (so that everyone can just be on their phones without anyone else bothering them). Please help the people of Baltia organize a successful party or tell them that it's impossible to do so. Input The first line contains two integer numbers, N (5 ≀ N ≀ 2*10^5) and M (0 ≀ M ≀ 2*10^5) – the number of people that live in Baltia, and the number of friendships. The next M lines each contains two integers U_i and V_i (1 ≀ U_i,V_i ≀ N) – meaning that person U_i is friends with person V_i. Two friends can not be in the list of friends twice (no pairs are repeated) and a person can be friends with themselves (U_i β‰  V_i). Output If it's possible to organize a successful party, print 5 numbers indicating which 5 people should be invited to the party. If it's not possible to organize a successful party, print -1 instead. If there are multiple successful parties possible, print any. Examples Input 6 3 1 4 4 2 5 4 Output 1 2 3 5 6 Input 5 4 1 2 2 3 3 4 4 5 Output -1
You have received data from a Bubble bot. You know your task is to make factory facilities, but before you even start, you need to know how big the factory is and how many rooms it has. When you look at the data you see that you have the dimensions of the construction, which is in rectangle shape: N x M. Then in the next N lines you have M numbers. These numbers represent factory tiles and they can go from 0 to 15. Each of these numbers should be looked in its binary form. Because from each number you know on which side the tile has walls. For example number 10 in it's binary form is 1010, which means that it has a wall from the North side, it doesn't have a wall from the East, it has a wall on the South side and it doesn't have a wall on the West side. So it goes North, East, South, West. It is guaranteed that the construction always has walls on it's edges. The input will be correct. Your task is to print the size of the rooms from biggest to smallest. Input The first line has two numbers which are N and M, the size of the construction. Both are integers: n (1 ≀ n ≀ 10^3) m (1 ≀ m ≀ 10^3) Next N x M numbers represent each tile of construction. Output Once you finish processing the data your output consists of one line sorted from biggest to smallest room sizes. Example Input 4 5 9 14 11 12 13 5 15 11 6 7 5 9 14 9 14 3 2 14 3 14 Output 9 4 4 2 1
You are given array a_1, a_2, …, a_n, consisting of non-negative integers. Let's define operation of "elimination" with integer parameter k (1 ≀ k ≀ n) as follows: * Choose k distinct array indices 1 ≀ i_1 < i_2 < … < i_k ≀ n. * Calculate x = a_{i_1} ~ \& ~ a_{i_2} ~ \& ~ … ~ \& ~ a_{i_k}, where \& denotes the [bitwise AND operation](https://en.wikipedia.org/wiki/Bitwise_operation#AND) (notes section contains formal definition). * Subtract x from each of a_{i_1}, a_{i_2}, …, a_{i_k}; all other elements remain untouched. Find all possible values of k, such that it's possible to make all elements of array a equal to 0 using a finite number of elimination operations with parameter k. It can be proven that exists at least one possible k for any array a. Note that you firstly choose k and only after that perform elimination operations with value k you've chosen initially. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains one integer n (1 ≀ n ≀ 200 000) β€” the length of array a. The second line of each test case contains n integers a_1, a_2, …, a_n (0 ≀ a_i < 2^{30}) β€” array a itself. It's guaranteed that the sum of n over all test cases doesn't exceed 200 000. Output For each test case, print all values k, such that it's possible to make all elements of a equal to 0 in a finite number of elimination operations with the given parameter k. Print them in increasing order. Example Input 5 4 4 4 4 4 4 13 7 25 19 6 3 5 3 1 7 1 1 1 5 0 0 0 0 0 Output 1 2 4 1 2 1 1 1 2 3 4 5 Note In the first test case: * If k = 1, we can make four elimination operations with sets of indices \{1\}, \{2\}, \{3\}, \{4\}. Since \& of one element is equal to the element itself, then for each operation x = a_i, so a_i - x = a_i - a_i = 0. * If k = 2, we can make two elimination operations with, for example, sets of indices \{1, 3\} and \{2, 4\}: x = a_1 ~ \& ~ a_3 = a_2 ~ \& ~ a_4 = 4 ~ \& ~ 4 = 4. For both operations x = 4, so after the first operation a_1 - x = 0 and a_3 - x = 0, and after the second operation β€” a_2 - x = 0 and a_4 - x = 0. * If k = 3, it's impossible to make all a_i equal to 0. After performing the first operation, we'll get three elements equal to 0 and one equal to 4. After that, all elimination operations won't change anything, since at least one chosen element will always be equal to 0. * If k = 4, we can make one operation with set \{1, 2, 3, 4\}, because x = a_1 ~ \& ~ a_2 ~ \& ~ a_3 ~ \& ~ a_4 = 4. In the second test case, if k = 2 then we can make the following elimination operations: * Operation with indices \{1, 3\}: x = a_1 ~ \& ~ a_3 = 13 ~ \& ~ 25 = 9. a_1 - x = 13 - 9 = 4 and a_3 - x = 25 - 9 = 16. Array a will become equal to [4, 7, 16, 19]. * Operation with indices \{3, 4\}: x = a_3 ~ \& ~ a_4 = 16 ~ \& ~ 19 = 16. a_3 - x = 16 - 16 = 0 and a_4 - x = 19 - 16 = 3. Array a will become equal to [4, 7, 0, 3]. * Operation with indices \{2, 4\}: x = a_2 ~ \& ~ a_4 = 7 ~ \& ~ 3 = 3. a_2 - x = 7 - 3 = 4 and a_4 - x = 3 - 3 = 0. Array a will become equal to [4, 4, 0, 0]. * Operation with indices \{1, 2\}: x = a_1 ~ \& ~ a_2 = 4 ~ \& ~ 4 = 4. a_1 - x = 4 - 4 = 0 and a_2 - x = 4 - 4 = 0. Array a will become equal to [0, 0, 0, 0]. Formal definition of bitwise AND: Let's define bitwise AND (\&) as follows. Suppose we have two non-negative integers x and y, let's look at their binary representations (possibly, with leading zeroes): x_k ... x_2 x_1 x_0 and y_k ... y_2 y_1 y_0. Here, x_i is the i-th bit of number x, and y_i is the i-th bit of number y. Let r = x ~ \& ~ y is a result of operation \& on number x and y. Then binary representation of r will be r_k ... r_2 r_1 r_0, where: $$$ r_i = \begin{cases} 1, ~ if ~ x_i = 1 ~ and ~ y_i = 1 \\\ 0, ~ if ~ x_i = 0 ~ or ~ y_i = 0 \end{cases} $$$
Frog Gorf is traveling through Swamp kingdom. Unfortunately, after a poor jump, he fell into a well of n meters depth. Now Gorf is on the bottom of the well and has a long way up. The surface of the well's walls vary in quality: somewhere they are slippery, but somewhere have convenient ledges. In other words, if Gorf is on x meters below ground level, then in one jump he can go up on any integer distance from 0 to a_x meters inclusive. (Note that Gorf can't jump down, only up). Unfortunately, Gorf has to take a break after each jump (including jump on 0 meters). And after jumping up to position x meters below ground level, he'll slip exactly b_x meters down while resting. Calculate the minimum number of jumps Gorf needs to reach ground level. Input The first line contains a single integer n (1 ≀ n ≀ 300 000) β€” the depth of the well. The second line contains n integers a_1, a_2, …, a_n (0 ≀ a_i ≀ i), where a_i is the maximum height Gorf can jump from i meters below ground level. The third line contains n integers b_1, b_2, …, b_n (0 ≀ b_i ≀ n - i), where b_i is the distance Gorf will slip down if he takes a break on i meters below ground level. Output If Gorf can't reach ground level, print -1. Otherwise, firstly print integer k β€” the minimum possible number of jumps. Then print the sequence d_1,\,d_2, …,\,d_k where d_j is the depth Gorf'll reach after the j-th jump, but before he'll slip down during the break. Ground level is equal to 0. If there are multiple answers, print any of them. Examples Input 3 0 2 2 1 1 0 Output 2 1 0 Input 2 1 1 1 0 Output -1 Input 10 0 1 2 3 5 5 6 7 8 5 9 8 7 1 5 4 3 2 0 0 Output 3 9 4 0 Note In the first example, Gorf is on the bottom of the well and jump to the height 1 meter below ground level. After that he slip down by meter and stays on height 2 meters below ground level. Now, from here, he can reach ground level in one jump. In the second example, Gorf can jump to one meter below ground level, but will slip down back to the bottom of the well. That's why he can't reach ground level. In the third example, Gorf can reach ground level only from the height 5 meters below the ground level. And Gorf can reach this height using a series of jumps 10 β‡’ 9 \dashrightarrow 9 β‡’ 4 \dashrightarrow 5 where β‡’ is the jump and \dashrightarrow is slipping during breaks.
You are given two arrays of integers a_1, a_2, …, a_n and b_1, b_2, …, b_m. You need to insert all elements of b into a in an arbitrary way. As a result you will get an array c_1, c_2, …, c_{n+m} of size n + m. Note that you are not allowed to change the order of elements in a, while you can insert elements of b at arbitrary positions. They can be inserted at the beginning, between any elements of a, or at the end. Moreover, elements of b can appear in the resulting array in any order. What is the minimum possible number of inversions in the resulting array c? Recall that an inversion is a pair of indices (i, j) such that i < j and c_i > c_j. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 10^4). Description of the test cases follows. The first line of each test case contains two integers n and m (1 ≀ n, m ≀ 10^6). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^9). The third line of each test case contains m integers b_1, b_2, …, b_m (1 ≀ b_i ≀ 10^9). It is guaranteed that the sum of n for all tests cases in one input doesn't exceed 10^6. The sum of m for all tests cases doesn't exceed 10^6 as well. Output For each test case, print one integer β€” the minimum possible number of inversions in the resulting array c. Example Input 3 3 4 1 2 3 4 3 2 1 3 3 3 2 1 1 2 3 5 4 1 3 5 3 1 4 3 6 1 Output 0 4 6 Note Below is given the solution to get the optimal answer for each of the example test cases (elements of a are underscored). * In the first test case, c = [\underline{1}, 1, \underline{2}, 2, \underline{3}, 3, 4]. * In the second test case, c = [1, 2, \underline{3}, \underline{2}, \underline{1}, 3]. * In the third test case, c = [\underline{1}, 1, 3, \underline{3}, \underline{5}, \underline{3}, \underline{1}, 4, 6].
A group of n alpinists has just reached the foot of the mountain. The initial difficulty of climbing this mountain can be described as an integer d. Each alpinist can be described by two integers s and a, where s is his skill of climbing mountains and a is his neatness. An alpinist of skill level s is able to climb a mountain of difficulty p only if p ≀ s. As an alpinist climbs a mountain, they affect the path and thus may change mountain difficulty. Specifically, if an alpinist of neatness a climbs a mountain of difficulty p the difficulty of this mountain becomes max(p, a). Alpinists will climb the mountain one by one. And before the start, they wonder, what is the maximum number of alpinists who will be able to climb the mountain if they choose the right order. As you are the only person in the group who does programming, you are to answer the question. Note that after the order is chosen, each alpinist who can climb the mountain, must climb the mountain at that time. Input The first line contains two integers n and d (1 ≀ n ≀ 500 000; 0 ≀ d ≀ 10^9) β€” the number of alpinists and the initial difficulty of the mountain. Each of the next n lines contains two integers s_i and a_i (0 ≀ s_i, a_i ≀ 10^9) that define the skill of climbing and the neatness of the i-th alpinist. Output Print one integer equal to the maximum number of alpinists who can climb the mountain if they choose the right order to do so. Examples Input 3 2 2 6 3 5 5 7 Output 2 Input 3 3 2 4 6 4 4 6 Output 2 Input 5 0 1 5 4 8 2 7 7 6 3 2 Output 3 Note In the first example, alpinists 2 and 3 can climb the mountain if they go in this order. There is no other way to achieve the answer of 2. In the second example, alpinist 1 is not able to climb because of the initial difficulty of the mountain, while alpinists 2 and 3 can go up in any order. In the third example, the mountain can be climbed by alpinists 5, 3 and 4 in this particular order. There is no other way to achieve optimal answer.
Students of one unknown college don't have PE courses. That's why q of them decided to visit a gym nearby by themselves. The gym is open for n days and has a ticket system. At the i-th day, the cost of one ticket is equal to a_i. You are free to buy more than one ticket per day. You can activate a ticket purchased at day i either at day i or any day later. Each activated ticket is valid only for k days. In other words, if you activate ticket at day t, it will be valid only at days t, t + 1, ..., t + k - 1. You know that the j-th student wants to visit the gym at each day from l_j to r_j inclusive. Each student will use the following strategy of visiting the gym at any day i (l_j ≀ i ≀ r_j): 1. person comes to a desk selling tickets placed near the entrance and buy several tickets with cost a_i apiece (possibly, zero tickets); 2. if the person has at least one activated and still valid ticket, they just go in. Otherwise, they activate one of tickets purchased today or earlier and go in. Note that each student will visit gym only starting l_j, so each student has to buy at least one ticket at day l_j. Help students to calculate the minimum amount of money they have to spend in order to go to the gym. Input The first line contains three integers n, q and k (1 ≀ n, q ≀ 300 000; 1 ≀ k ≀ n) β€” the number of days, the number of students and the number of days each ticket is still valid. The second line contains n integers a_1, a_2, ..., a_n (1 ≀ a_i ≀ 10^9) β€” the cost of one ticket at the corresponding day. Each of the next q lines contains two integers l_i and r_i (1 ≀ l_i ≀ r_i ≀ n) β€” the segment of days the corresponding student want to visit the gym. Output For each student, print the minimum possible amount of money they have to spend in order to go to the gym at desired days. Example Input 7 5 2 2 15 6 3 7 5 6 1 2 3 7 5 5 7 7 3 5 Output 2 12 7 6 9 Note Let's see how each student have to spend their money: * The first student should buy one ticket at day 1. * The second student should buy one ticket at day 3 and two tickets at day 4. Note that student can keep purchased tickets for the next days. * The third student should buy one ticket at day 5. * The fourth student should buy one ticket at day 7. * The fifth student should buy one ticket at day 3 and one at day 4.
Integers from 1 to n (inclusive) were sorted lexicographically (considering integers as strings). As a result, array a_1, a_2, ..., a_n was obtained. Calculate value of (βˆ‘_{i = 1}^n ((i - a_i) mod 998244353)) mod 10^9 + 7. x mod y here means the remainder after division x by y. This remainder is always non-negative and doesn't exceed y - 1. For example, 5 mod 3 = 2, (-1) mod 6 = 5. Input The first line contains the single integer n (1 ≀ n ≀ 10^{12}). Output Print one integer β€” the required sum. Examples Input 3 Output 0 Input 12 Output 994733045 Input 21 Output 978932159 Input 1000000000000 Output 289817887 Note 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 β‰  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. For example, 42 is lexicographically smaller than 6, because they differ in the first digit, and 4 < 6; 42 < 420, because 42 is a prefix of 420. Let's denote 998244353 as M. In the first example, array a is equal to [1, 2, 3]. * (1 - 1) mod M = 0 mod M = 0 * (2 - 2) mod M = 0 mod M = 0 * (3 - 3) mod M = 0 mod M = 0 As a result, (0 + 0 + 0) mod 10^9 + 7 = 0 In the second example, array a is equal to [1, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9]. * (1 - 1) mod M = 0 mod M = 0 * (2 - 10) mod M = (-8) mod M = 998244345 * (3 - 11) mod M = (-8) mod M = 998244345 * (4 - 12) mod M = (-8) mod M = 998244345 * (5 - 2) mod M = 3 mod M = 3 * (6 - 3) mod M = 3 mod M = 3 * (7 - 4) mod M = 3 mod M = 3 * (8 - 5) mod M = 3 mod M = 3 * (9 - 6) mod M = 3 mod M = 3 * (10 - 7) mod M = 3 mod M = 3 * (11 - 8) mod M = 3 mod M = 3 * (12 - 9) mod M = 3 mod M = 3 As a result, (0 + 998244345 + 998244345 + 998244345 + 3 + 3 + 3 + 3 + 3 + 3 + 3 + 3) mod 10^9 + 7 = 2994733059 mod 10^9 + 7 = 994733045
For two positive integers l and r (l ≀ r) let c(l, r) denote the number of integer pairs (i, j) such that l ≀ i ≀ j ≀ r and \operatorname{gcd}(i, j) β‰₯ l. Here, \operatorname{gcd}(i, j) is the [greatest common divisor (GCD)](https://en.wikipedia.org/wiki/Greatest_common_divisor) of integers i and j. YouKn0wWho has two integers n and k where 1 ≀ k ≀ n. Let f(n, k) denote the minimum of βˆ‘_{i=1}^{k}{c(x_i+1,x_{i+1})} over all integer sequences 0=x_1 < x_2 < … < x_{k} < x_{k+1}=n. Help YouKn0wWho find f(n, k). Input The first line contains a single integer t (1 ≀ t ≀ 3 β‹… 10^5) β€” the number of test cases. The first and only line of each test case contains two integers n and k (1 ≀ k ≀ n ≀ 10^5). Output For each test case, print a single integer β€” f(n, k). Example Input 4 6 2 4 4 3 1 10 3 Output 8 4 6 11 Note In the first test case, YouKn0wWho can select the sequence [0, 2, 6]. So f(6, 2) = c(1, 2) + c(3, 6) = 3 + 5 = 8 which is the minimum possible.
A sequence of integers b_1, b_2, …, b_m is called good if max(b_1, b_2, …, b_m) β‹… min(b_1, b_2, …, b_m) β‰₯ b_1 + b_2 + … + b_m. A sequence of integers a_1, a_2, …, a_n is called perfect if every non-empty subsequence of a is good. YouKn0wWho has two integers n and M, M is prime. Help him find the number, modulo M, of perfect sequences a_1, a_2, …, a_n such that 1 ≀ a_i ≀ n + 1 for each integer i from 1 to n. A sequence d is a subsequence of a sequence c if d can be obtained from c by deletion of several (possibly, zero or all) elements. Input The first and only line of the input contains two space-separated integers n and M (1 ≀ n ≀ 200; 10^8 ≀ M ≀ 10^9). It is guaranteed that M is prime. Output Print a single integer β€” the number of perfect sequences modulo M. Examples Input 2 998244353 Output 4 Input 4 100000007 Output 32 Input 69 999999937 Output 456886663 Note In the first test case, the perfect sequences are [2, 2], [2, 3], [3, 2] and [3, 3]. In the second test case, some of the perfect sequences are [3, 4, 3, 5], [4, 5, 4, 4], [4, 5, 5, 5] etc. One example of a sequence which is not perfect is [2, 3, 3, 4], because, for example, the subsequence [2, 3, 4] is not an good as 2 β‹… 4 < 2 + 3 + 4.
It was October 18, 2017. Shohag, a melancholic soul, made a strong determination that he will pursue Competitive Programming seriously, by heart, because he found it fascinating. Fast forward to 4 years, he is happy that he took this road. He is now creating a contest on Codeforces. He found an astounding problem but has no idea how to solve this. Help him to solve the final problem of the round. You are given three integers n, k and x. Find the number, modulo 998 244 353, of integer sequences a_1, a_2, …, a_n such that the following conditions are satisfied: * 0 ≀ a_i < 2^k for each integer i from 1 to n. * There is no non-empty subsequence in a such that the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of the elements of the subsequence is x. A sequence b is a subsequence of a sequence c if b can be obtained from c by deletion of several (possibly, zero or all) elements. Input The first line contains a single integer t (1 ≀ t ≀ 10^5) β€” the number of test cases. The first and only line of each test case contains three space-separated integers n, k, and x (1 ≀ n ≀ 10^9, 0 ≀ k ≀ 10^7, 0 ≀ x < 2^{\operatorname{min}(20, k)}). It is guaranteed that the sum of k over all test cases does not exceed 5 β‹… 10^7. Output For each test case, print a single integer β€” the answer to the problem. Example Input 6 2 2 0 2 1 1 3 2 3 69 69 69 2017 10 18 5 7 0 Output 6 1 15 699496932 892852568 713939942 Note In the first test case, the valid sequences are [1, 2], [1, 3], [2, 1], [2, 3], [3, 1] and [3, 2]. In the second test case, the only valid sequence is [0, 0].
Shohag has an integer sequence a_1, a_2, …, a_n. He can perform the following operation any number of times (possibly, zero): * Select any positive integer k (it can be different in different operations). * Choose any position in the sequence (possibly the beginning or end of the sequence, or in between any two elements) and insert k into the sequence at this position. * This way, the sequence a changes, and the next operation is performed on this changed sequence. For example, if a=[3,3,4] and he selects k = 2, then after the operation he can obtain one of the sequences [\underline{2},3,3,4], [3,\underline{2},3,4], [3,3,\underline{2},4], or [3,3,4,\underline{2}]. Shohag wants this sequence to satisfy the following condition: for each 1 ≀ i ≀ |a|, a_i ≀ i. Here, |a| denotes the size of a. Help him to find the minimum number of operations that he has to perform to achieve this goal. We can show that under the constraints of the problem it's always possible to achieve this goal in a finite number of operations. Input The first line contains a single integer t (1 ≀ t ≀ 200) β€” the number of test cases. The first line of each test case contains a single integer n (1 ≀ n ≀ 100) β€” the initial length of the sequence. The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^9) β€” the elements of the sequence. Output For each test case, print a single integer β€” the minimum number of operations needed to perform to achieve the goal mentioned in the statement. Example Input 4 3 1 3 4 5 1 2 5 7 4 1 1 3 69 6969 696969 Output 1 3 0 696966 Note In the first test case, we have to perform at least one operation, as a_2=3>2. We can perform the operation [1, 3, 4] β†’ [1, \underline{2}, 3, 4] (the newly inserted element is underlined), now the condition is satisfied. In the second test case, Shohag can perform the following operations: [1, 2, 5, 7, 4] β†’ [1, 2, \underline{3}, 5, 7, 4] β†’ [1, 2, 3, \underline{4}, 5, 7, 4] β†’ [1, 2, 3, 4, 5, \underline{3}, 7, 4]. In the third test case, the sequence already satisfies the condition.
YouKn0wWho has an integer sequence a_1, a_2, … a_n. Now he will split the sequence a into one or more consecutive subarrays so that each element of a belongs to exactly one subarray. Let k be the number of resulting subarrays, and h_1, h_2, …, h_k be the lengths of the longest increasing subsequences of corresponding subarrays. For example, if we split [2, 5, 3, 1, 4, 3, 2, 2, 5, 1] into [2, 5, 3, 1, 4], [3, 2, 2, 5], [1], then h = [3, 2, 1]. YouKn0wWho wonders if it is possible to split the sequence a in such a way that the [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR) of h_1, h_2, …, h_k is equal to 0. You have to tell whether it is possible. The longest increasing subsequence (LIS) of a sequence b_1, b_2, …, b_m is the longest sequence of valid indices i_1, i_2, …, i_k such that i_1 < i_2 < … < i_k and b_{i_1} < b_{i_2} < … < b_{i_k}. For example, the LIS of [2, 5, 3, 3, 5] is [2, 3, 5], which has length 3. An array c is a subarray of an array b if c can be obtained from b by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains a single integer n (2 ≀ n ≀ 10^5). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^9). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print "YES" (without quotes) if it is possible to split into subarrays in the desired way, print "NO" (without quotes) otherwise. You can print each letter in any register (upper or lower). Example Input 4 7 1 3 4 2 2 1 5 3 1 3 4 5 1 3 2 4 2 4 4 3 2 1 Output YES NO YES YES Note In the first test case, YouKn0wWho can split the sequence in the following way: [1, 3, 4], [2, 2], [1, 5]. This way, the LIS lengths are h = [3, 1, 2], and the bitwise XOR of the LIS lengths is 3 βŠ• 1 βŠ• 2 = 0. In the second test case, it can be shown that it is impossible to split the sequence into subarrays that will satisfy the condition.
YouKn0wWho has an integer sequence a_1, a_2, …, a_n. He will perform the following operation until the sequence becomes empty: select an index i such that 1 ≀ i ≀ |a| and a_i is not divisible by (i + 1), and erase this element from the sequence. Here |a| is the length of sequence a at the moment of operation. Note that the sequence a changes and the next operation is performed on this changed sequence. For example, if a=[3,5,4,5], then he can select i = 2, because a_2 = 5 is not divisible by i+1 = 3. After this operation the sequence is [3,4,5]. Help YouKn0wWho determine if it is possible to erase the whole sequence using the aforementioned operation. Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains a single integer n (1 ≀ n ≀ 10^5). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^9). It is guaranteed that the sum of n over all test cases doesn't exceed 3 β‹… 10^5. Output For each test case, print "YES" (without quotes) if it is possible to erase the whole sequence using the aforementioned operation, print "NO" (without quotes) otherwise. You can print each letter in any register (upper or lower). Example Input 5 3 1 2 3 1 2 2 7 7 10 384836991 191890310 576823355 782177068 404011431 818008580 954291757 160449218 155374934 840594328 8 6 69 696 69696 696969 6969696 69696969 696969696 Output YES NO YES YES NO Note In the first test case, YouKn0wWho can perform the following operations (the erased elements are underlined): [1, \underline{2}, 3] β†’ [\underline{1}, 3] β†’ [\underline{3}] β†’ [ ]. In the second test case, it is impossible to erase the sequence as i can only be 1, and when i=1, a_1 = 2 is divisible by i + 1 = 2.
YouKn0wWho has two even integers x and y. Help him to find an integer n such that 1 ≀ n ≀ 2 β‹… 10^{18} and n mod x = y mod n. Here, a mod b denotes the remainder of a after division by b. If there are multiple such integers, output any. It can be shown that such an integer always exists under the given constraints. Input The first line contains a single integer t (1 ≀ t ≀ 10^5) β€” the number of test cases. The first and only line of each test case contains two integers x and y (2 ≀ x, y ≀ 10^9, both are even). Output For each test case, print a single integer n (1 ≀ n ≀ 2 β‹… 10^{18}) that satisfies the condition mentioned in the statement. If there are multiple such integers, output any. It can be shown that such an integer always exists under the given constraints. Example Input 4 4 8 4 2 420 420 69420 42068 Output 4 10 420 9969128 Note In the first test case, 4 mod 4 = 8 mod 4 = 0. In the second test case, 10 mod 4 = 2 mod 10 = 2. In the third test case, 420 mod 420 = 420 mod 420 = 0.
For an array b of n integers, the extreme value of this array is the minimum number of times (possibly, zero) the following operation has to be performed to make b non-decreasing: * Select an index i such that 1 ≀ i ≀ |b|, where |b| is the current length of b. * Replace b_i with two elements x and y such that x and y both are positive integers and x + y = b_i. * This way, the array b changes and the next operation is performed on this modified array. For example, if b = [2, 4, 3] and index 2 gets selected, then the possible arrays after this operation are [2, \underline{1}, \underline{3}, 3], [2, \underline{2}, \underline{2}, 3], or [2, \underline{3}, \underline{1}, 3]. And consequently, for this array, this single operation is enough to make it non-decreasing: [2, 4, 3] β†’ [2, \underline{2}, \underline{2}, 3]. It's easy to see that every array of positive integers can be made non-decreasing this way. YouKn0wWho has an array a of n integers. Help him find the sum of extreme values of all nonempty subarrays of a modulo 998 244 353. If a subarray appears in a multiple times, its extreme value should be counted the number of times it appears. An array d is a subarray of an array c if d can be obtained from c by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Input The first line contains a single integer t (1 ≀ t ≀ 10 000) β€” the number of test cases. The first line of each test case contains a single integer n (1 ≀ n ≀ 10^5). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≀ a_i ≀ 10^5). It is guaranteed that the sum of n over all test cases doesn't exceed 10^5. Output For each test case, print a single integer β€” the sum of extreme values of all subarrays of a modulo 998 244 353. Example Input 4 3 5 4 3 4 3 2 1 4 1 69 8 7264 40515 28226 92776 35285 21709 75124 48163 Output 5 9 0 117 Note Let f(l, r) denote the extreme value of [a_l, a_{l+1}, …, a_r]. In the first test case, * f(1, 3) = 3, because YouKn0wWho can perform the following operations on the subarray [5, 4, 3] (the newly inserted elements are underlined): [5, 4, 3] β†’ [\underline{3}, \underline{2}, 4, 3] β†’ [3, 2, \underline{2}, \underline{2}, 3] β†’ [\underline{1}, \underline{2}, 2, 2, 2, 3]; * f(1, 2) = 1, because [5, 4] β†’ [\underline{2}, \underline{3}, 4]; * f(2, 3) = 1, because [4, 3] β†’ [\underline{1}, \underline{3}, 3]; * f(1, 1) = f(2, 2) = f(3, 3) = 0, because they are already non-decreasing. So the total sum of extreme values of all subarrays of a = 3 + 1 + 1 + 0 + 0 + 0 = 5.
You are given a string s of length n consisting of characters a and/or b. Let \operatorname{AB}(s) be the number of occurrences of string ab in s as a substring. Analogically, \operatorname{BA}(s) is the number of occurrences of ba in s as a substring. In one step, you can choose any index i and replace s_i with character a or b. What is the minimum number of steps you need to make to achieve \operatorname{AB}(s) = \operatorname{BA}(s)? Reminder: The number of occurrences of string d in s as substring is the number of indices i (1 ≀ i ≀ |s| - |d| + 1) such that substring s_i s_{i + 1} ... s_{i + |d| - 1} is equal to d. For example, \operatorname{AB}(aabbbabaa) = 2 since there are two indices i: i = 2 where aabbbabaa and i = 6 where aabbbabaa. Input Each test contains multiple test cases. The first line contains the number of test cases t (1 ≀ t ≀ 1000). Description of the test cases follows. The first and only line of each test case contains a single string s (1 ≀ |s| ≀ 100, where |s| is the length of the string s), consisting only of characters a and/or b. Output For each test case, print the resulting string s with \operatorname{AB}(s) = \operatorname{BA}(s) you'll get making the minimum number of steps. If there are multiple answers, print any of them. Example Input 4 b aabbbabaa abbb abbaab Output b aabbbabaa bbbb abbaaa Note In the first test case, both \operatorname{AB}(s) = 0 and \operatorname{BA}(s) = 0 (there are no occurrences of ab (ba) in b), so can leave s untouched. In the second test case, \operatorname{AB}(s) = 2 and \operatorname{BA}(s) = 2, so you can leave s untouched. In the third test case, \operatorname{AB}(s) = 1 and \operatorname{BA}(s) = 0. For example, we can change s_1 to b and make both values zero. In the fourth test case, \operatorname{AB}(s) = 2 and \operatorname{BA}(s) = 1. For example, we can change s_6 to a and make both values equal to 1.
Berland State University has received a new update for the operating system. Initially it is installed only on the 1-st computer. Update files should be copied to all n computers. The computers are not connected to the internet, so the only way to transfer update files from one computer to another is to copy them using a patch cable (a cable connecting two computers directly). Only one patch cable can be connected to a computer at a time. Thus, from any computer where the update files are installed, they can be copied to some other computer in exactly one hour. Your task is to find the minimum number of hours required to copy the update files to all n computers if there are only k patch cables in Berland State University. Input The first line contains a single integer t (1 ≀ t ≀ 10^5) β€” the number of test cases. Each test case consists of a single line that contains two integers n and k (1 ≀ k ≀ n ≀ 10^{18}) β€” the number of computers and the number of patch cables. Output For each test case print one integer β€” the minimum number of hours required to copy the update files to all n computers. Example Input 4 8 3 6 6 7 1 1 1 Output 4 3 6 0 Note Let's consider the test cases of the example: * n=8, k=3: 1. during the first hour, we copy the update files from the computer 1 to the computer 2; 2. during the second hour, we copy the update files from the computer 1 to the computer 3, and from the computer 2 to the computer 4; 3. during the third hour, we copy the update files from the computer 1 to the computer 5, from the computer 2 to the computer 6, and from the computer 3 to the computer 7; 4. during the fourth hour, we copy the update files from the computer 2 to the computer 8. * n=6, k=6: 1. during the first hour, we copy the update files from the computer 1 to the computer 2; 2. during the second hour, we copy the update files from the computer 1 to the computer 3, and from the computer 2 to the computer 4; 3. during the third hour, we copy the update files from the computer 1 to the computer 5, and from the computer 2 to the computer 6. * n=7, k=1: 1. during the first hour, we copy the update files from the computer 1 to the computer 2; 2. during the second hour, we copy the update files from the computer 1 to the computer 3; 3. during the third hour, we copy the update files from the computer 1 to the computer 4; 4. during the fourth hour, we copy the update files from the computer 4 to the computer 5; 5. during the fifth hour, we copy the update files from the computer 4 to the computer 6; 6. during the sixth hour, we copy the update files from the computer 3 to the computer 7.
In Berland, n different types of banknotes are used. Banknotes of the i-th type have denomination 10^{a_i} burles (burles are the currency used in Berland); the denomination of banknotes of the first type is exactly 1. Let's denote f(s) as the minimum number of banknotes required to represent exactly s burles. For example, if the denominations of banknotes used in Berland are 1, 10 and 100, then f(59) = 14: 9 banknotes with denomination of 1 burle and 5 banknotes with denomination of 10 burles can be used to represent exactly 9 β‹… 1 + 5 β‹… 10 = 59 burles, and there's no way to do it with fewer banknotes. For a given integer k, find the minimum positive number of burles s that cannot be represented with k or fewer banknotes (that is, f(s) > k). Input The first line contains a single integer t (1 ≀ t ≀ 10^4) β€” number of test cases. The first line of each test case contains two integers n and k (1 ≀ n ≀ 10; 1 ≀ k ≀ 10^9). The next line contains n integers a_1, a_2, ..., a_n (0 = a_1 < a_2 < ... < a_n ≀ 9). Output For each test case, print one integer β€” the minimum positive number of burles s that cannot be represented with k or fewer banknotes. Example Input 4 3 13 0 1 2 2 777 0 4 3 255 0 1 3 10 1000000000 0 1 2 3 4 5 6 7 8 9 Output 59 778 148999 999999920999999999
You are given a matrix, consisting of n rows and m columns. The j-th cell of the i-th row contains an integer a_{ij}. First, you have to color each row of the matrix either red or blue in such a way that at least one row is colored red and at least one row is colored blue. Then, you have to choose an integer k (1 ≀ k < m) and cut the colored matrix in such a way that the first k columns become a separate matrix (the left matrix) and the last m-k columns become a separate matrix (the right matrix). The coloring and the cut are called perfect if two properties hold: * every red cell in the left matrix contains an integer greater than every blue cell in the left matrix; * every blue cell in the right matrix contains an integer greater than every red cell in the right matrix. Find any perfect coloring and cut, or report that there are none. Input The first line contains a single integer t (1 ≀ t ≀ 1000) β€” the number of testcases. Then the descriptions of t testcases follow. The first line of each testcase contains two integers n and m (2 ≀ n, m ≀ 5 β‹… 10^5; n β‹… m ≀ 10^6) β€” the number of rows and the number of columns in the matrix, respectively. The i-th of the next n lines contains m integers a_{i1}, a_{i2}, ..., a_{im} (1 ≀ a_{ij} ≀ 10^6). The sum of n β‹… m over all testcases doesn't exceed 10^6. Output For each testcase print an answer. If there are no perfect colorings and cuts in the matrix, then print "NO". Otherwise, first, print "YES". Then a string, consisting of n characters: the i-th character should be 'R' if the i-th row is colored red and 'B' if it's colored blue. The string should contain at least one 'R' and at least one 'B'. Finally, print an integer k (1 ≀ k < m) β€” the number of columns from the left that are cut. Example Input 3 5 5 1 5 8 8 7 5 2 1 4 3 1 6 9 7 5 9 3 3 3 2 1 7 9 9 8 3 3 8 9 8 1 5 3 7 5 7 2 6 3 3 3 2 2 2 1 1 1 4 4 4 Output YES BRBRB 1 NO YES RB 3 Note The coloring and the cut for the first testcase: <image>
There are n heroes fighting in the arena. Initially, the i-th hero has a_i health points. The fight in the arena takes place in several rounds. At the beginning of each round, each alive hero deals 1 damage to all other heroes. Hits of all heroes occur simultaneously. Heroes whose health is less than 1 at the end of the round are considered killed. If exactly 1 hero remains alive after a certain round, then he is declared the winner. Otherwise, there is no winner. Your task is to calculate the number of ways to choose the initial health points for each hero a_i, where 1 ≀ a_i ≀ x, so that there is no winner of the fight. The number of ways can be very large, so print it modulo 998244353. Two ways are considered different if at least one hero has a different amount of health. For example, [1, 2, 1] and [2, 1, 1] are different. Input The only line contains two integers n and x (2 ≀ n ≀ 500; 1 ≀ x ≀ 500). Output Print one integer β€” the number of ways to choose the initial health points for each hero a_i, where 1 ≀ a_i ≀ x, so that there is no winner of the fight, taken modulo 998244353. Examples Input 2 5 Output 5 Input 3 3 Output 15 Input 5 4 Output 1024 Input 13 37 Output 976890680
You are given a tree consisting of n vertices. Recall that a tree is an undirected connected acyclic graph. The given tree is rooted at the vertex 1. You have to process q queries. In each query, you are given a vertex of the tree v and an integer k. To process a query, you may delete any vertices from the tree in any order, except for the root and the vertex v. When a vertex is deleted, its children become the children of its parent. You have to process a query in such a way that maximizes the value of c(v) - m β‹… k (where c(v) is the resulting number of children of the vertex v, and m is the number of vertices you have deleted). Print the maximum possible value you can obtain. The queries are independent: the changes you make to the tree while processing a query don't affect the tree in other queries. Input The first line contains one integer n (1 ≀ n ≀ 2 β‹… 10^5) β€” the number of vertices in the tree. Then n-1 lines follow, the i-th of them contains two integers x_i and y_i (1 ≀ x_i, y_i ≀ n; x_i β‰  y_i) β€” the endpoints of the i-th edge. These edges form a tree. The next line contains one integer q (1 ≀ q ≀ 2 β‹… 10^5) β€” the number of queries. Then q lines follow, the j-th of them contains two integers v_j and k_j (1 ≀ v_j ≀ n; 0 ≀ k_j ≀ 2 β‹… 10^5) β€” the parameters of the j-th query. Output For each query, print one integer β€” the maximum value of c(v) - m β‹… k you can achieve. Example Input 8 6 7 3 2 8 3 5 7 7 4 7 1 7 3 6 1 0 1 2 1 3 7 1 5 0 7 200000 Output 5 2 1 4 0 4 Note The tree in the first example is shown in the following picture: <image> Answers to the queries are obtained as follows: 1. v=1,k=0: you can delete vertices 7 and 3, so the vertex 1 has 5 children (vertices 2, 4, 5, 6, and 8), and the score is 5 - 2 β‹… 0 = 5; 2. v=1,k=2: you can delete the vertex 7, so the vertex 1 has 4 children (vertices 3, 4, 5, and 6), and the score is 4 - 1 β‹… 2 = 2. 3. v=1,k=3: you shouldn't delete any vertices, so the vertex 1 has only one child (vertex 7), and the score is 1 - 0 β‹… 3 = 1; 4. v=7,k=1: you can delete the vertex 3, so the vertex 7 has 5 children (vertices 2, 4, 5, 6, and 8), and the score is 5 - 1 β‹… 1 = 4; 5. v=5,k=0: no matter what you do, the vertex 5 will have no children, so the score is 0; 6. v=7,k=200000: you shouldn't delete any vertices, so the vertex 7 has 4 children (vertices 3, 4, 5, and 6), and the score is 4 - 0 β‹… 200000 = 4.
You are given a keyboard that consists of 26 keys. The keys are arranged sequentially in one row in a certain order. Each key corresponds to a unique lowercase Latin letter. You have to type the word s on this keyboard. It also consists only of lowercase Latin letters. To type a word, you need to type all its letters consecutively one by one. To type each letter you must position your hand exactly over the corresponding key and press it. Moving the hand between the keys takes time which is equal to the absolute value of the difference between positions of these keys (the keys are numbered from left to right). No time is spent on pressing the keys and on placing your hand over the first letter of the word. For example, consider a keyboard where the letters from 'a' to 'z' are arranged in consecutive alphabetical order. The letters 'h', 'e', 'l' and 'o' then are on the positions 8, 5, 12 and 15, respectively. Therefore, it will take |5 - 8| + |12 - 5| + |12 - 12| + |15 - 12| = 13 units of time to type the word "hello". Determine how long it will take to print the word s. Input The first line contains an integer t (1 ≀ t ≀ 1000) β€” the number of test cases. The next 2t lines contain descriptions of the test cases. The first line of a description contains a keyboard β€” a string of length 26, which consists only of lowercase Latin letters. Each of the letters from 'a' to 'z' appears exactly once on the keyboard. The second line of the description contains the word s. The word has a length from 1 to 50 letters inclusive and consists of lowercase Latin letters. Output Print t lines, each line containing the answer to the corresponding test case. The answer to the test case is the minimal time it takes to type the word s on the given keyboard. Example Input 5 abcdefghijklmnopqrstuvwxyz hello abcdefghijklmnopqrstuvwxyz i abcdefghijklmnopqrstuvwxyz codeforces qwertyuiopasdfghjklzxcvbnm qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq qwertyuiopasdfghjklzxcvbnm abacaba Output 13 0 68 0 74
The grasshopper is located on the numeric axis at the point with coordinate x_0. Having nothing else to do he starts jumping between integer points on the axis. Making a jump from a point with coordinate x with a distance d to the left moves the grasshopper to a point with a coordinate x - d, while jumping to the right moves him to a point with a coordinate x + d. The grasshopper is very fond of positive integers, so for each integer i starting with 1 the following holds: exactly i minutes after the start he makes a jump with a distance of exactly i. So, in the first minutes he jumps by 1, then by 2, and so on. The direction of a jump is determined as follows: if the point where the grasshopper was before the jump has an even coordinate, the grasshopper jumps to the left, otherwise he jumps to the right. For example, if after 18 consecutive jumps he arrives at the point with a coordinate 7, he will jump by a distance of 19 to the right, since 7 is an odd number, and will end up at a point 7 + 19 = 26. Since 26 is an even number, the next jump the grasshopper will make to the left by a distance of 20, and it will move him to the point 26 - 20 = 6. Find exactly which point the grasshopper will be at after exactly n jumps. Input The first line of input contains an integer t (1 ≀ t ≀ 10^4) β€” the number of test cases. Each of the following t lines contains two integers x_0 (-10^{14} ≀ x_0 ≀ 10^{14}) and n (0 ≀ n ≀ 10^{14}) β€” the coordinate of the grasshopper's initial position and the number of jumps. Output Print exactly t lines. On the i-th line print one integer β€” the answer to the i-th test case β€” the coordinate of the point the grasshopper will be at after making n jumps from the point x_0. Example Input 9 0 1 0 2 10 10 10 99 177 13 10000000000 987654321 -433494437 87178291199 1 0 -1 1 Output -1 1 11 110 190 9012345679 -87611785637 1 0 Note The first two test cases in the example correspond to the first two jumps from the point x_0 = 0. Since 0 is an even number, the first jump of length 1 is made to the left, and the grasshopper ends up at the point 0 - 1 = -1. Then, since -1 is an odd number, a jump of length 2 is made to the right, bringing the grasshopper to the point with coordinate -1 + 2 = 1.