Problem ID stringlengths 2 6 | Problem Description stringlengths 0 7.52k | Rating float64 800 3.5k | math bool 2 classes | greedy bool 2 classes | implementation bool 2 classes | dp bool 2 classes | data structures bool 2 classes | constructive algorithms bool 2 classes | brute force bool 2 classes | binary search bool 2 classes | sortings bool 2 classes | graphs bool 2 classes | __index_level_0__ int64 3 9.98k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1788F | You are given a tree of $$$n$$$ vertices. The vertices are numbered from $$$1$$$ to $$$n$$$. You will need to assign a weight to each edge. Let the weight of the $$$i$$$-th edge be $$$a_i$$$ ($$$1 leq i leq n-1$$$). The weight of each edge should be an integer between $$$0$$$ and $$$2^{30}-1$$$, inclusive. You are given $$$q$$$ conditions. Each condition consists of three integers $$$u$$$, $$$v$$$, and $$$x$$$. This means that the . The $$$i$$$-th of the following $$$n-1$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i, y_i le n$$$, $$$x_i eq y_i$$$), meaning that the $$$i$$$-th edge connects vertices $$$x_i$$$ and $$$y_i$$$ in the tree. It is guaranteed that the given edges form a tree. The following $$$q$$$ lines contain information about conditions. Each line contains three integers $$$u$$$, $$$v$$$, $$$x$$$ ($$$1 le u, v le n$$$, $$$u eq v$$$, $$$0 le x le 2^{30}-1$$$), meaning that the bitwise XOR of all edges on the shortest path from $$$u$$$ to $$$v$$$ should be $$$x$$$. Output If there do not exist $$$a_1$$$, $$$a_2$$$, ..., $$$a_{n-1}$$$ that satisfy the given conditions, print "No". Otherwise, print "Yes" in the first line. Then print $$$n-1$$$ integers on the next line, where the $$$i$$$-th integer is the weight of the $$$i$$$-th edge. If there are multiple solutions that satisfy the given conditions, print a solution such that $$$a_1 oplus a_2 oplus ldots oplus a_{n-1}$$$ is the smallest. If there are multiple solutions such that $$$a_1 oplus a_2 oplus ldots oplus a_{n-1}$$$ is the smallest, print any. When printing "Yes" or "No", you can print each letter in any case (either upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Examples Input 4 4 1 2 2 3 3 4 1 4 3 2 4 2 1 3 1 2 3 1 Input 6 2 1 2 2 3 3 4 2 5 5 6 1 4 2 2 6 7 Input 6 2 1 2 2 3 3 4 2 5 5 6 1 4 3 1 6 5 Note For the first example, there does not exist a set of edge weights that satisfies the given conditions. For the second example, the two given conditions are $$$a_1 oplus a_2 oplus a_3=2$$$ and $$$a_4 oplus a_5=7$$$. There can be multiple solutions, for example, $$$(a_1, a_2, a_3, a_4, a_5)=(1, 2, 1, 4, 3)$$$. For the third example, the two given conditions are $$$a_1 oplus a_2 oplus a_3=3$$$ and $$$a_1 oplus a_4 oplus a_5=5$$$. There are multiple solutions that satisfy the given conditions. $$$(a_1, a_2, a_3, a_4, a_5)=(1, 1, 3, 4, 0)$$$ satisfies the given conditions, but the bitwise XOR of all edge weights is $$$7$$$, which does not have the smallest $$$a_1 oplus a_2 oplus ldots oplus a_{n-1}$$$, so it cannot be the answer. | 2,500 | false | true | true | false | false | true | false | false | false | true | 1,549 |
776D | Moriarty has trapped _n_ people in _n_ distinct rooms in a hotel. Some rooms are locked, others are unlocked. But, there is a condition that the people in the hotel can only escape when all the doors are unlocked at the same time. There are _m_ switches. Each switch control doors of some rooms, but each door is controlled by exactly two switches. You are given the initial configuration of the doors. Toggling any switch, that is, turning it ON when it is OFF, or turning it OFF when it is ON, toggles the condition of the doors that this switch controls. Say, we toggled switch 1, which was connected to room 1, 2 and 3 which were respectively locked, unlocked and unlocked. Then, after toggling the switch, they become unlocked, locked and locked. You need to tell Sherlock, if there exists a way to unlock all doors at the same time. Input First line of input contains two integers _n_ and _m_ (2u2009β€u2009_n_u2009β€u2009105, 2u2009β€u2009_m_u2009β€u2009105)xa0β the number of rooms and the number of switches. Next line contains _n_ space-separated integers _r_1,u2009_r_2,u2009...,u2009_r__n_ (0u2009β€u2009_r__i_u2009β€u20091) which tell the status of room doors. The _i_-th room is locked if _r__i_u2009=u20090, otherwise it is unlocked. The _i_-th of next _m_ lines contains an integer _x__i_ (0u2009β€u2009_x__i_u2009β€u2009_n_) followed by _x__i_ distinct integers separated by space, denoting the number of rooms controlled by the _i_-th switch followed by the room numbers that this switch controls. It is guaranteed that the room numbers are in the range from 1 to _n_. It is guaranteed that each door is controlled by exactly two switches. Output Output "YES" without quotes, if it is possible to open all doors at the same time, otherwise output "NO" without quotes. Examples Input 3 3 1 0 1 2 1 3 2 1 2 2 2 3 Input 3 3 1 0 1 3 1 2 3 1 2 2 1 3 Input 3 3 1 0 1 3 1 2 3 2 1 2 1 3 Note In the second example input, the initial statuses of the doors are [1,u20090,u20091] (0 means locked, 1xa0β unlocked). After toggling switch 3, we get [0,u20090,u20090] that means all doors are locked. Then, after toggling switch 1, we get [1,u20091,u20091] that means all doors are unlocked. It can be seen that for the first and for the third example inputs it is not possible to make all doors unlocked. | 2,000 | false | false | false | false | false | false | false | false | false | true | 6,665 |
1859C | Andrey is just starting to come up with problems, and it's difficult for him. That's why he came up with a strange problem about permutations$$$^{dagger}$$$ and asks you to solve it. Can you do it? Let's call the cost of a permutation $$$p$$$ of length $$$n$$$ the value of the expression: $$$(sum_{i = 1}^{n} p_i cdot i) - (max_{j = 1}^{n} p_j cdot j)$$$. Find the maximum cost among all permutations of length $$$n$$$. $$$^{dagger}$$$A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 30$$$) β the number of test cases. The description of the test cases follows. The only line of each test case contains a single integer $$$n$$$ ($$$2 le n le 250$$$) β the length of the permutation. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$500$$$. Output For each test case, output a single integer β the maximum cost among all permutations of length $$$n$$$. Note In the first test case, the permutation with the maximum cost is $$$[2, 1]$$$. The cost is equal to $$$2 cdot 1 + 1 cdot 2 - max (2 cdot 1, 1 cdot 2)= 2 + 2 - 2 = 2$$$. In the second test case, the permutation with the maximum cost is $$$[1, 2, 4, 3]$$$. The cost is equal to $$$1 cdot 1 + 2 cdot 2 + 4 cdot 3 + 3 cdot 4 - 4 cdot 3 = 17$$$. | 1,200 | true | true | false | true | false | false | true | false | false | false | 1,129 |
920G | Problem - 920G - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags binary search bitmasks brute force combinatorics math number theory *2200 No tag edit access β Contest materials an infinite sequence of integers _y_ such that _gcd_(_p_,u2009_y_)u2009=u20091 and _y_u2009>u2009_x_ (where _gcd_ is the greatest common divisor of two integer numbers), sorted in ascending order. The elements of _L_(_x_,u2009_p_) are 1-indexed; for example, 9, 13 and 15 are the first, the second and the third elements of _L_(7,u200922), respectively. You have to process _t_ queries. Each query is denoted by three integers _x_, _p_ and _k_, and the answer to this query is _k_-th element of _L_(_x_,u2009_p_). Input The first line contains one integer _t_ (1u2009β€u2009_t_u2009β€u200930000) β the number of queries to process. Then _t_ lines follow. _i_-th line contains three integers _x_, _p_ and _k_ for _i_-th query (1u2009β€u2009_x_,u2009_p_,u2009_k_u2009β€u2009106). Output Print _t_ integers, where _i_-th integer is the answer to _i_-th query. Examples Input 3 7 22 1 7 22 2 7 22 3 Output 9 13 15 Input 5 42 42 42 43 43 43 44 44 44 45 45 45 46 46 46 Output 187 87 139 128 141 | 2,200 | true | false | false | false | false | false | true | true | false | false | 6,044 |
1437F | $$$n$$$ fishermen have just returned from a fishing vacation. The $$$i$$$-th fisherman has caught a fish of weight $$$a_i$$$. Fishermen are going to show off the fish they caught to each other. To do so, they firstly choose an order in which they show their fish (each fisherman shows his fish exactly once, so, formally, the order of showing fish is a permutation of integers from $$$1$$$ to $$$n$$$). Then they show the fish they caught according to the chosen order. When a fisherman shows his fish, he might either become happy, become sad, or stay content. Suppose a fisherman shows a fish of weight $$$x$$$, and the maximum weight of a previously shown fish is $$$y$$$ ($$$y = 0$$$ if that fisherman is the first to show his fish). Then: if $$$x ge 2y$$$, the fisherman becomes happy; if $$$2x le y$$$, the fisherman becomes sad; if none of these two conditions is met, the fisherman stays content. Let's call an order in which the fishermen show their fish emotional if, after all fishermen show their fish according to this order, each fisherman becomes either happy or sad. Calculate the number of emotional orders modulo $$$998244353$$$. Input The first line contains one integer $$$n$$$ ($$$2 le n le 5000$$$). The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$1 le a_i le 10^9$$$). Output Print one integer β the number of emotional orders, taken modulo $$$998244353$$$. Examples Input 8 42 1337 13 37 420 666 616 97 | 2,600 | true | false | false | true | false | false | false | false | false | false | 3,480 |
365A | Problem - 365A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation *1100 No tag edit access β Contest materials . You've got a number _k_ and an array _a_ containing _n_ numbers. Find out how many _k_-good numbers are in _a_ (count each number every time it occurs in array _a_). Input The first line contains integers _n_ and _k_ (1u2009β€u2009_n_u2009β€u2009100, 0u2009β€u2009_k_u2009β€u20099). The _i_-th of the following _n_ lines contains integer _a__i_ without leading zeroes (1u2009β€u2009_a__i_u2009β€u2009109). Output Print a single integer β the number of _k_-good numbers in _a_. Examples Input 10 6 1234560 1234560 1234560 1234560 1234560 1234560 1234560 1234560 1234560 1234560 Output 10 Input 2 1 1 10 Output 1 | 1,100 | false | false | true | false | false | false | false | false | false | false | 8,372 |
1352B | You are given two positive integers $$$n$$$ ($$$1 le n le 10^9$$$) and $$$k$$$ ($$$1 le k le 100$$$). Represent the number $$$n$$$ as the sum of $$$k$$$ positive integers of the same parity (have the same remainder when divided by $$$2$$$). In other words, find $$$a_1, a_2, ldots, a_k$$$ such that all $$$a_i>0$$$, $$$n = a_1 + a_2 + ldots + a_k$$$ and either all $$$a_i$$$ are even or all $$$a_i$$$ are odd at the same time. If such a representation does not exist, then report it. Input The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$) β the number of test cases in the input. Next, $$$t$$$ test cases are given, one per line. Each test case is two positive integers $$$n$$$ ($$$1 le n le 10^9$$$) and $$$k$$$ ($$$1 le k le 100$$$). Output For each test case print: YES and the required values $$$a_i$$$, if the answer exists (if there are several answers, print any of them); NO if the answer does not exist. The letters in the words YES and NO can be printed in any case. Example Input 8 10 3 100 4 8 7 97 2 8 8 3 10 5 3 1000000000 9 Output YES 4 2 4 YES 55 5 5 35 NO NO YES 1 1 1 1 1 1 1 1 NO YES 3 1 1 YES 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111120 | 1,200 | true | false | false | false | false | true | false | false | false | false | 3,951 |
1338C | Consider the infinite sequence $$$s$$$ of positive integers, created by repeating the following steps: 1. Find the lexicographically smallest triple of positive integers $$$(a, b, c)$$$ such that $$$a oplus b oplus c = 0$$$, where $$$oplus$$$ denotes the $$$ is considered to be lexicographically smaller than triple $$$(a_2, b_2, c_2)$$$ if sequence $$$[a_1, b_1, c_1]$$$ is lexicographically smaller than sequence $$$[a_2, b_2, c_2]$$$. 2. Append $$$a$$$, $$$b$$$, $$$c$$$ to $$$s$$$ in this order. 3. Go back to the first step. You have integer $$$n$$$. Find the $$$n$$$-th element of $$$s$$$. You have to answer $$$t$$$ independent test cases. A sequence $$$a$$$ is lexicographically smaller than a sequence $$$b$$$ if in the first position where $$$a$$$ and $$$b$$$ differ, the sequence $$$a$$$ has a smaller element than the corresponding element in $$$b$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0β the number of test cases. Each of the next $$$t$$$ lines contains a single integer $$$n$$$ ($$$1le n le 10^{16}$$$)xa0β the position of the element you want to know. Output In each of the $$$t$$$ lines, output the answer to the corresponding test case. Example Output 1 2 3 4 8 12 5 10 15 Note The first elements of $$$s$$$ are $$$1, 2, 3, 4, 8, 12, 5, 10, 15, dots $$$ | 2,200 | true | false | false | false | false | true | true | false | false | false | 4,012 |
1881C | Kristina has a matrix of size $$$n$$$ by $$$n$$$, filled with lowercase Latin letters. The value of $$$n$$$ is even. She wants to change some characters so that her matrix becomes a perfect square. A matrix is called a perfect square if it remains unchanged when rotated $$$90^circ$$$ clockwise once. Here is an example of rotating a matrix by $$$90^circ$$$: In one operation, Kristina can choose any cell and replace its value with the next character in the alphabet. If the character is equal to "z", its value does not change. Find the minimum number of operations required to make the matrix a perfect square. For example, if the $$$4$$$ by $$$4$$$ matrix looks like this: $$$$$$matrix{ a & b & b & a cr b & c & extbf{b} & b cr b & c & c & bcr a & b & b & a cr }$$$$$$ then it is enough to apply $$$1$$$ operation to the letter b, highlighted in bold. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 10^2$$$)xa0β the number of test cases. Then follows the description of each test case. The first line of each test case contains a single even integer $$$n$$$ ($$$2 le n le 10^3$$$)xa0β the number of rows and columns in the matrix. Then follows $$$n$$$ lines, each containing exactly $$$n$$$ lowercase Latin letters. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$. Output For each test case, output a single number on a separate line: the minimum number of operations required for Kristina to obtain a perfect square. Example Input 5 4 abba bcbb bccb abba 2 ab ba 6 codefo rcesco deforc escode forces codefo 4 baaa abba baba baab 4 bbaa abba aaba abba Note The first test case is explained in the problem statement. | 1,200 | false | false | true | false | false | false | true | false | false | false | 1,002 |
379A | Problem - 379A - Codeforces =============== xa0 . Output Print a single integer β the number of hours Vasily can light up the room for. Examples Input 4 2 Output 7 Input 6 3 Output 8 Note Consider the first sample. For the first four hours Vasily lights up new candles, then he uses four burned out candles to make two new ones and lights them up. When these candles go out (stop burning), Vasily can make another candle. Overall, Vasily can light up the room for 7 hours. | 1,000 | false | false | true | false | false | false | false | false | false | false | 8,317 |
461B | Appleman has a tree with _n_ vertices. Some of the vertices (at least one) are colored black and other vertices are colored white. Consider a set consisting of _k_ (0u2009β€u2009_k_u2009<u2009_n_) edges of Appleman's tree. If Appleman deletes these edges from the tree, then it will split into (_k_u2009+u20091) parts. Note, that each part will be a tree with colored vertices. Now Appleman wonders, what is the number of sets splitting the tree in such a way that each resulting part will have exactly one black vertex? Find this number modulo 1000000007 (109u2009+u20097). Input The first line contains an integer _n_ (2u2009u2009β€u2009_n_u2009β€u2009105) β the number of tree vertices. The second line contains the description of the tree: _n_u2009-u20091 integers _p_0,u2009_p_1,u2009...,u2009_p__n_u2009-u20092 (0u2009β€u2009_p__i_u2009β€u2009_i_). Where _p__i_ means that there is an edge connecting vertex (_i_u2009+u20091) of the tree and vertex _p__i_. Consider tree vertices are numbered from 0 to _n_u2009-u20091. The third line contains the description of the colors of the vertices: _n_ integers _x_0,u2009_x_1,u2009...,u2009_x__n_u2009-u20091 (_x__i_ is either 0 or 1). If _x__i_ is equal to 1, vertex _i_ is colored black. Otherwise, vertex _i_ is colored white. Output Output a single integer β the number of ways to split the tree modulo 1000000007 (109u2009+u20097). Examples Input 6 0 1 1 0 4 1 1 0 0 1 0 Input 10 0 1 2 1 4 4 4 0 8 0 0 0 1 0 1 1 0 0 1 | 2,000 | false | false | false | true | false | false | false | false | false | false | 7,995 |
1726A | Mainak has an array $$$a_1, a_2, ldots, a_n$$$ of $$$n$$$ positive integers. He will do the following operation to this array exactly once: Pick a subsegment of this array and cyclically rotate it by any amount. Formally, he can do the following exactly once: Pick two integers $$$l$$$ and $$$r$$$, such that $$$1 le l le r le n$$$, and any positive integer $$$k$$$. Repeat this $$$k$$$ times: set $$$a_l=a_{l+1}, a_{l+1}=a_{l+2}, ldots, a_{r-1}=a_r, a_r=a_l$$$ (all changes happen at the same time). Mainak wants to maximize the value of $$$(a_n - a_1)$$$ after exactly one such operation. Determine the maximum value of $$$(a_n - a_1)$$$ that he can obtain. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 50$$$)xa0β the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 2000$$$). The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 999$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2000$$$. Output For each test case, output a single integerxa0β the maximum value of $$$(a_n - a_1)$$$ that Mainak can obtain by doing the operation exactly once. Example Input 5 6 1 3 9 11 5 7 1 20 3 9 99 999 4 2 1 8 1 3 2 1 5 Note In the first test case, we can rotate the subarray from index $$$3$$$ to index $$$6$$$ by an amount of $$$2$$$ (i.e. choose $$$l = 3$$$, $$$r = 6$$$ and $$$k = 2$$$) to get the optimal array: $$$$$$[1, 3, underline{9, 11, 5, 7}] longrightarrow [1, 3, underline{5, 7, 9, 11}]$$$$$$ So the answer is $$$a_n - a_1 = 11 - 1 = 10$$$. In the second testcase, it is optimal to rotate the subarray starting and ending at index $$$1$$$ and rotating it by an amount of $$$2$$$. In the fourth testcase, it is optimal to rotate the subarray starting from index $$$1$$$ to index $$$4$$$ and rotating it by an amount of $$$3$$$. So the answer is $$$8 - 1 = 7$$$. | 900 | true | true | false | false | false | false | false | false | false | false | 1,933 |
1510H | Problem - 1510H - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags dp *3200 No tag edit access β Contest materials ") in such a way that no two subsegments intersect (they are allowed to have common endpoints though) and the sum of their lengths ($$$sum_{i=1}^n r_i - l_i$$$) is maximized. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^3$$$)xa0β the number of segments. The $$$i$$$-th of the next $$$n$$$ lines contains two integers $$$L_i$$$ and $$$R_i$$$ ($$$0 le L_i < R_i le 10^9$$$)xa0β the endpoints of the $$$i$$$-th segment. All the given $$$2n$$$ segment endpoints are distinct. The set of segments is laminar. Output On the first line, output the maximum possible sum of subsegment lengths. On the $$$i$$$-th of the next $$$n$$$ lines, output two integers $$$l_i$$$ and $$$r_i$$$ ($$$L_i le l_i < r_i le R_i$$$), denoting the chosen subsegment of the $$$i$$$-th segment. Example Input 4 1 10 2 3 5 9 6 7 Output 7 3 6 2 3 7 9 6 7 Note The example input and the example output are illustrated below. | 3,200 | false | false | false | true | false | false | false | false | false | false | 3,120 |
894A | Problem - 894A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force dp *800 No tag edit access β Contest materials ") Editorial") . illustration by η«ε± wants to know how many subsequences "QAQ" are in the string Diamond has given. Note that the letters "QAQ" don't have to be consecutive, but the order of letters should be exact. Input The only line contains a string of length _n_ (1u2009β€u2009_n_u2009β€u2009100). It's guaranteed that the string only contains uppercase English letters. Output Print a single integerxa0β the number of subsequences "QAQ" in the string. Examples Input QAQAQYSYIOIWIN Output 4 Input QAQQQZZYNOIWIN Output 3 Note In the first example there are 4 subsequences "QAQ": "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN", "QAQAQYSYIOIWIN". | 800 | false | false | false | true | false | false | true | false | false | false | 6,175 |
1227B | Permutation $$$p$$$ is a sequence of integers $$$p=[p_1, p_2, dots, p_n]$$$, consisting of $$$n$$$ distinct (unique) positive integers between $$$1$$$ and $$$n$$$, inclusive. For example, the following sequences are permutations: $$$[3, 4, 1, 2]$$$, $$$[1]$$$, $$$[1, 2]$$$. The following sequences are not permutations: $$$[0]$$$, $$$[1, 2, 1]$$$, $$$[2, 3]$$$, $$$[0, 1, 2]$$$. The important key is in the locked box that you need to open. To open the box you need to enter secret code. Secret code is a permutation $$$p$$$ of length $$$n$$$. You don't know this permutation, you only know the array $$$q$$$ of prefix maximums of this permutation. Formally: $$$q_1=p_1$$$, $$$q_2=max(p_1, p_2)$$$, $$$q_3=max(p_1, p_2,p_3)$$$, ... $$$q_n=max(p_1, p_2,dots,p_n)$$$. You want to construct any possible suitable permutation (i.e. any such permutation, that calculated $$$q$$$ for this permutation is equal to the given array). Input The first line contains integer number $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases in the input. Then $$$t$$$ test cases follow. The first line of a test case contains one integer $$$n$$$ $$$(1 le n le 10^{5})$$$xa0β the number of elements in the secret code permutation $$$p$$$. The second line of a test case contains $$$n$$$ integers $$$q_1, q_2, dots, q_n$$$ $$$(1 le q_i le n)$$$xa0β elements of the array $$$q$$$ for secret permutation. It is guaranteed that $$$q_i le q_{i+1}$$$ for all $$$i$$$ ($$$1 le i < n$$$). The sum of all values $$$n$$$ over all the test cases in the input doesn't exceed $$$10^5$$$. Note In the first test case of the example answer $$$[1,3,4,5,2]$$$ is the only possible answer: $$$q_{1} = p_{1} = 1$$$; $$$q_{2} = max(p_{1}, p_{2}) = 3$$$; $$$q_{3} = max(p_{1}, p_{2}, p_{3}) = 4$$$; $$$q_{4} = max(p_{1}, p_{2}, p_{3}, p_{4}) = 5$$$; $$$q_{5} = max(p_{1}, p_{2}, p_{3}, p_{4}, p_{5}) = 5$$$. It can be proved that there are no answers for the second test case of the example. | 1,200 | false | false | false | false | false | true | false | false | false | false | 4,543 |
1380E | You have a set of $$$n$$$ discs, the $$$i$$$-th disc has radius $$$i$$$. Initially, these discs are split among $$$m$$$ towers: each tower contains at least one disc, and the discs in each tower are sorted in descending order of their radii from bottom to top. You would like to assemble one tower containing all of those discs. To do so, you may choose two different towers $$$i$$$ and $$$j$$$ (each containing at least one disc), take several (possibly all) top discs from the tower $$$i$$$ and put them on top of the tower $$$j$$$ in the same order, as long as the top disc of tower $$$j$$$ is bigger than each of the discs you move. You may perform this operation any number of times. For example, if you have two towers containing discs $$$[6, 4, 2, 1]$$$ and $$$[8, 7, 5, 3]$$$ (in order from bottom to top), there are only two possible operations: move disc $$$1$$$ from the first tower to the second tower, so the towers are $$$[6, 4, 2]$$$ and $$$[8, 7, 5, 3, 1]$$$; move discs $$$[2, 1]$$$ from the first tower to the second tower, so the towers are $$$[6, 4]$$$ and $$$[8, 7, 5, 3, 2, 1]$$$. Let the difficulty of some set of towers be the minimum number of operations required to assemble one tower containing all of the discs. For example, the difficulty of the set of towers $$$[[3, 1], [2]]$$$ is $$$2$$$: you may move the disc $$$1$$$ to the second tower, and then move both discs from the second tower to the first tower. You are given $$$m - 1$$$ queries. Each query is denoted by two numbers $$$a_i$$$ and $$$b_i$$$, and means "merge the towers $$$a_i$$$ and $$$b_i$$$" (that is, take all discs from these two towers and assemble a new tower containing all of them in descending order of their radii from top to bottom). The resulting tower gets index $$$a_i$$$. For each $$$k in [0, m - 1]$$$, calculate the difficulty of the set of towers after the first $$$k$$$ queries are performed. Input The first line of the input contains two integers $$$n$$$ and $$$m$$$ ($$$2 le m le n le 2 cdot 10^5$$$) β the number of discs and the number of towers, respectively. The second line contains $$$n$$$ integers $$$t_1$$$, $$$t_2$$$, ..., $$$t_n$$$ ($$$1 le t_i le m$$$), where $$$t_i$$$ is the index of the tower disc $$$i$$$ belongs to. Each value from $$$1$$$ to $$$m$$$ appears in this sequence at least once. Then $$$m - 1$$$ lines follow, denoting the queries. Each query is represented by two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 le a_i, b_i le m$$$, $$$a_i e b_i$$$), meaning that, during the $$$i$$$-th query, the towers with indices $$$a_i$$$ and $$$b_i$$$ are merged ($$$a_i$$$ and $$$b_i$$$ are chosen in such a way that these towers exist before the $$$i$$$-th query). | 2,300 | false | false | true | false | true | false | false | false | false | false | 3,767 |
264B | Problem - 264B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags dp number theory *1500 No tag edit access β Contest materials . No two adjacent elements are coprime, i.e. _gcd_(_x__i_,u2009_x__i_u2009+u20091)u2009>u20091 for each _i_ (1u2009β€u2009_i_u2009β€u2009_k_u2009-u20091) (where _gcd_(_p_,u2009_q_) denotes the greatest common divisor of the integers _p_ and _q_). All elements of the sequence are good integers. Find the length of the longest good sequence. Input The input consists of two lines. The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the number of good integers. The second line contains a single-space separated list of good integers _a_1,u2009_a_2,u2009...,u2009_a__n_ in strictly increasing order (1u2009β€u2009_a__i_u2009β€u2009105;xa0_a__i_u2009<u2009_a__i_u2009+u20091). Output Print a single integer β the length of the longest good sequence. Examples Input 5 2 3 4 6 9 Output 4 Input 9 1 2 3 5 6 7 8 9 10 Output 4 Note In the first example, the following sequences are examples of good sequences: | 1,500 | false | false | false | true | false | false | false | false | false | false | 8,777 |
650B | Vasya's telephone contains _n_ photos. Photo number 1 is currently opened on the phone. It is allowed to move left and right to the adjacent photo by swiping finger over the screen. If you swipe left from the first photo, you reach photo _n_. Similarly, by swiping right from the last photo you reach photo 1. It takes _a_ seconds to swipe from photo to adjacent. For each photo it is known which orientation is intended for it β horizontal or vertical. Phone is in the vertical orientation and can't be rotated. It takes _b_ second to change orientation of the photo. Vasya has _T_ seconds to watch photos. He want to watch as many photos as possible. If Vasya opens the photo for the first time, he spends 1 second to notice all details in it. If photo is in the wrong orientation, he spends _b_ seconds on rotating it before watching it. If Vasya has already opened the photo, he just skips it (so he doesn't spend any time for watching it or for changing its orientation). It is not allowed to skip unseen photos. Help Vasya find the maximum number of photos he is able to watch during _T_ seconds. Input The first line of the input contains 4 integers _n_,u2009_a_,u2009_b_,u2009_T_ (1u2009β€u2009_n_u2009β€u20095Β·105, 1u2009β€u2009_a_,u2009_b_u2009β€u20091000, 1u2009β€u2009_T_u2009β€u2009109) β the number of photos, time to move from a photo to adjacent, time to change orientation of a photo and time Vasya can spend for watching photo. Second line of the input contains a string of length _n_ containing symbols 'w' and 'h'. If the _i_-th position of a string contains 'w', then the photo _i_ should be seen in the horizontal orientation. If the _i_-th position of a string contains 'h', then the photo _i_ should be seen in vertical orientation. Output Output the only integer, the maximum number of photos Vasya is able to watch during those _T_ seconds. Note In the first sample test you can rotate the first photo (3 seconds), watch the first photo (1 seconds), move left (2 second), rotate fourth photo (3 seconds), watch fourth photo (1 second). The whole process takes exactly 10 seconds. Note that in the last sample test the time is not enough even to watch the first photo, also you can't skip it. | 1,900 | false | false | false | true | false | false | true | true | false | false | 7,216 |
1693E | Yeri has an array of $$$n + 2$$$ non-negative integers : $$$a_0, a_1, ..., a_n, a_{n + 1}$$$. We know that $$$a_0 = a_{n + 1} = 0$$$. She wants to make all the elements of $$$a$$$ equal to zero in the minimum number of operations. In one operation she can do one of the following: Choose the leftmost maximum element and change it to the maximum of the elements on its left. Choose the rightmost maximum element and change it to the maximum of the elements on its right. Help her find the minimum number of operations needed to make all elements of $$$a$$$ equal to zero. Note In the first sample, you get $$$langle 1, underline{1}, 2, 4, 0, 2 angle$$$ by performing the first operation and $$$langle 1, 4, 2, underline{2}, 0, 2 angle$$$ by performing the second operation. One way to achieve our goal is shown below. (The underlines show the last change.) $$$langle 1, 4, 2, 4, 0, 2 angle o langle 1, 4, 2, underline{2}, 0, 2 angle o langle 1, underline{1}, 2, 2, 0, 2 angle o langle 1, 1, 2, 2, 0, underline{0} angle o langle 1, 1, 2, underline{0}, 0, 0 angle o langle 1, 1, underline{0}, 0, 0, 0 angle o langle underline{0}, 1, 0, 0, 0, 0 angle o langle 0, underline{0}, 0, 0, 0, 0 angle $$$ In the third sample each element is already equal to zero so no operations are needed. | 3,400 | false | true | false | false | true | false | false | false | false | false | 2,118 |
387C | George is a cat, so he really likes to play. Most of all he likes to play with his array of positive integers _b_. During the game, George modifies the array by using special changes. Let's mark George's current array as _b_1,u2009_b_2,u2009...,u2009_b__b_ (record _b_ denotes the current length of the array). Then one change is a sequence of actions: Choose two distinct indexes _i_ and _j_ (1u2009β€u2009_i_,u2009_j_u2009β€u2009_b_;xa0_i_u2009β u2009_j_), such that _b__i_u2009β₯u2009_b__j_. Get number _v_u2009=u2009_concat_(_b__i_,u2009_b__j_), where _concat_(_x_,u2009_y_) is a number obtained by adding number _y_ to the end of the decimal record of number _x_. For example, _concat_(500,u200910)u2009=u200950010, _concat_(2,u20092)u2009=u200922. Add number _v_ to the end of the array. The length of the array will increase by one. Remove from the array numbers with indexes _i_ and _j_. The length of the array will decrease by two, and elements of the array will become re-numbered from 1 to current length of the array. George played for a long time with his array _b_ and received from array _b_ an array consisting of exactly one number _p_. Now George wants to know: what is the maximum number of elements array _b_ could contain originally? Help him find this number. Note that originally the array could contain only positive integers. Input The first line of the input contains a single integer _p_ (1u2009β€u2009_p_u2009<u200910100000). It is guaranteed that number _p_ doesn't contain any leading zeroes. Output Print an integer β the maximum number of elements array _b_ could contain originally. Examples Input 1000000000000001223300003342220044555 Note Let's consider the test examples: Originally array _b_ can be equal to {5,u20099,u20095,u20095}. The sequence of George's changes could have been: {5,u20099,u20095,u20095}u2009βu2009{5,u20095,u200995}u2009βu2009{95,u200955}u2009βu2009{9555}. Originally array _b_ could be equal to {1000000000,u20095}. Please note that the array _b_ cannot contain zeros. Originally array _b_ could be equal to {800,u200910,u20091}. Originally array _b_ could be equal to {45}. It cannot be equal to {4,u20095}, because George can get only array {54} from this array in one operation. Note that the numbers can be very large. | 1,700 | false | true | true | false | false | false | false | false | false | false | 8,280 |
161B | One day Polycarpus stopped by a supermarket on his way home. It turns out that the supermarket is having a special offer for stools. The offer is as follows: if a customer's shopping cart contains at least one stool, the customer gets a 50% discount on the cheapest item in the cart (that is, it becomes two times cheaper). If there are several items with the same minimum price, the discount is available for only one of them! Polycarpus has _k_ carts, and he wants to buy up all stools and pencils from the supermarket. Help him distribute the stools and the pencils among the shopping carts, so that the items' total price (including the discounts) is the least possible. Polycarpus must use all _k_ carts to purchase the items, no shopping cart can remain empty. Each shopping cart can contain an arbitrary number of stools and/or pencils. Input The first input line contains two integers _n_ and _k_ (1u2009β€u2009_k_u2009β€u2009_n_u2009β€u2009103) β the number of items in the supermarket and the number of carts, correspondingly. Next _n_ lines describe the items as "_c__i_ _t__i_" (without the quotes), where _c__i_ (1u2009β€u2009_c__i_u2009β€u2009109) is an integer denoting the price of the _i_-th item, _t__i_ (1u2009β€u2009_t__i_u2009β€u20092) is an integer representing the type of item _i_ (1 for a stool and 2 for a pencil). The numbers in the lines are separated by single spaces. Output In the first line print a single real number with exactly one decimal place β the minimum total price of the items, including the discounts. In the following _k_ lines print the descriptions of the items in the carts. In the _i_-th line print the description of the _i_-th cart as "_t_ _b_1 _b_2 ... _b__t_" (without the quotes), where _t_ is the number of items in the _i_-th cart, and the sequence _b_1,u2009_b_2,u2009...,u2009_b__t_ (1u2009β€u2009_b__j_u2009β€u2009_n_) gives the indices of items to put in this cart in the optimal distribution. All indices of items in all carts should be pairwise different, each item must belong to exactly one cart. You can print the items in carts and the carts themselves in any order. The items are numbered from 1 to _n_ in the order in which they are specified in the input. If there are multiple optimal distributions, you are allowed to print any of them. Note In the first sample case the first cart should contain the 1st and 2nd items, and the second cart should contain the 3rd item. This way each cart has a stool and each cart has a 50% discount for the cheapest item. The total price of all items will be: 2Β·0.5u2009+u2009(3u2009+u20093Β·0.5)u2009=u20091u2009+u20094.5u2009=u20095.5. | 1,700 | false | true | false | false | false | true | false | false | true | false | 9,229 |
1451F | Jeel and Ashish play a game on an $$$n imes m$$$ matrix. The rows are numbered $$$1$$$ to $$$n$$$ from top to bottom and the columns are numbered $$$1$$$ to $$$m$$$ from left to right. They play turn by turn. Ashish goes first. Initially, each cell of the matrix contains a non-negative integer. Each turn, a player must perform all of the following actions in order. Choose a starting cell $$$(r_1, c_1)$$$ with non-zero value. Choose a finishing cell $$$(r_2, c_2)$$$ such that $$$r_1 leq r_2$$$ and $$$c_1 leq c_2$$$. Decrease the value of the starting cell by some positive non-zero integer. Pick any of the shortest paths between the two cells and either increase, decrease or leave the values of cells on this path unchanged. Note that: a shortest path is one that passes through the least number of cells; all cells on this path excluding the starting cell, but the finishing cell may be modified; the resulting value of each cell must be a non-negative integer; the cells are modified independently and not necessarily by the same value. If the starting and ending cells are the same, then as per the rules, the value of the cell is decreased. No other operations are performed. The game ends when all the values become zero. The player who is unable to make a move loses. It can be shown that the game will end in a finite number of moves if both players play optimally. Given the initial matrix, if both players play optimally, can you predict who will win? Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10$$$)xa0β the number of test cases. The description of each test case is as follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n, m leq 100$$$)xa0β the dimensions of the matrix. The next $$$n$$$ lines contain $$$m$$$ space separated integers $$$a_{i,j}$$$ ($$$0 leq a_{i,j} leq 10^6$$$)xa0β the values of each cell of the matrix. Output For each test case, if Ashish wins the game, print "Ashish", otherwise print "Jeel" (without the quotes). Example Input 4 1 1 0 1 3 0 0 5 2 2 0 1 1 0 3 3 1 2 3 4 5 6 7 8 9 Output Jeel Ashish Jeel Ashish Note In the first test case, the only cell of the matrix is 0. There are no moves Ashish can make. Jeel is the winner. In the second test case, Ashish can choose $$$(r_1, c_1) = (r_2, c_2) = (1,3)$$$ and reduce the cell to $$$0$$$, leaving $$$[0, 0, 0]$$$. Jeel cannot perform any moves. Ashish wins. | 2,700 | false | false | false | false | false | true | false | false | false | false | 3,422 |
1574F | A subarray of array $$$a$$$ from index $$$l$$$ to the index $$$r$$$ is the array $$$[a_l, a_{l+1}, dots, a_{r}]$$$. The number of occurrences of the array $$$b$$$ in the array $$$a$$$ is the number of subarrays of $$$a$$$ such that they are equal to $$$b$$$. You are given $$$n$$$ arrays $$$A_1, A_2, dots, A_n$$$; the elements of these arrays are integers from $$$1$$$ to $$$k$$$. You have to build an array $$$a$$$ consisting of $$$m$$$ integers from $$$1$$$ to $$$k$$$ in such a way that, for every given subarray $$$A_i$$$, the number of occurrences of $$$A_i$$$ in the array $$$a$$$ is not less than the number of occurrences of each non-empty subarray of $$$A_i$$$ in $$$a$$$. Note that if $$$A_i$$$ doesn't occur in $$$a$$$, and no subarray of $$$A_i$$$ occurs in $$$a$$$, this condition is still met for $$$A_i$$$. Your task is to calculate the number of different arrays $$$a$$$ you can build, and print it modulo $$$998244353$$$. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 le n, m, k le 3 cdot 10^5$$$) β the number of the given arrays, the desired length of the array $$$a$$$, and the upper bound on the values in the arrays. Then $$$n$$$ lines follow. The $$$i$$$-th line represents the array $$$A_i$$$. The first integer in the $$$i$$$-th line is $$$c_i$$$ ($$$1 le c_i le m$$$) β the number of elements in $$$A_i$$$; then, $$$c_i$$$ integers from $$$1$$$ to $$$k$$$ follow β the elements of the array $$$A_i$$$. Additional constraint on the input: $$$sumlimits_{i=1}^n c_i le 3 cdot 10^5$$$; i.u2009e., the number of elements in the given arrays in total does not exceed $$$3 cdot 10^5$$$. Output Print one integer β the number of different arrays $$$a$$$ you can build, taken modulo $$$998244353$$$. | 2,700 | false | false | false | true | false | false | false | false | false | true | 2,769 |
1439D | Today is the final contest of INOI (Iranian National Olympiad in Informatics). The contest room is a row with $$$n$$$ computers. All computers are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. There are $$$m$$$ participants, numbered with integers from $$$1$$$ to $$$m$$$. We have an array $$$a$$$ of length $$$m$$$ where $$$a_{i}$$$ ($$$1 leq a_i leq n$$$) is the computer behind which the $$$i$$$-th participant wants to sit. Also, we have another array $$$b$$$ of length $$$m$$$ consisting of characters 'L' and 'R'. $$$b_i$$$ is the side from which the $$$i$$$-th participant enters the room. 'L' means the participant enters from the left of computer $$$1$$$ and goes from left to right, and 'R' means the participant enters from the right of computer $$$n$$$ and goes from right to left. The participants in the order from $$$1$$$ to $$$m$$$ enter the room one by one. The $$$i$$$-th of them enters the contest room in the direction $$$b_i$$$ and goes to sit behind the $$$a_i$$$-th computer. If it is occupied he keeps walking in his direction until he reaches the first unoccupied computer. After that, he sits behind it. If he doesn't find any computer he gets upset and gives up on the contest. The madness of the $$$i$$$-th participant is the distance between his assigned computer ($$$a_i$$$) and the computer he ends up sitting behind. The distance between computers $$$i$$$ and $$$j$$$ is equal to $$$i - j$$$. The values in the array $$$a$$$ can be equal. There exist $$$n^m cdot 2^m$$$ possible pairs of arrays $$$(a, b)$$$. Consider all pairs of arrays $$$(a, b)$$$ such that no person becomes upset. For each of them let's calculate the sum of participants madnesses. Find the sum of all these values. You will be given some prime modulo $$$p$$$. Find this sum by modulo $$$p$$$. Input The only line contains three integers $$$n$$$, $$$m$$$, $$$p$$$ ($$$1 leq m leq n leq 500, 10^8 leq p leq 10 ^ 9 + 9$$$). It is guaranteed, that the number $$$p$$$ is prime. Note In the first test, there are three possible arrays $$$a$$$: $$${1}$$$, $$${2}$$$, and $$$ {3}$$$ and two possible arrays $$$b$$$: $$${mathtt{L}}$$$ and $$${mathtt{R}}$$$. For all six pairs of arrays $$$(a, b)$$$, the only participant will sit behind the computer $$$a_1$$$, so his madness will be $$$0$$$. So the total sum of madnesses will be $$$0$$$. In the second test, all possible pairs of arrays $$$(a, b)$$$, such that no person becomes upset are: $$$({1, 1}, {mathtt{L}, mathtt{L}})$$$, the sum of madnesses is $$$1$$$; $$$({1, 1}, {mathtt{R}, mathtt{L}})$$$, the sum of madnesses is $$$1$$$; $$$({2, 2}, {mathtt{R}, mathtt{R}})$$$, the sum of madnesses is $$$1$$$; $$$({2, 2}, {mathtt{L}, mathtt{R}})$$$, the sum of madnesses is $$$1$$$; all possible pairs of $$$a in {{1, 2}, {2, 1}}$$$ and $$$b in {{mathtt{L}, mathtt{L}}, {mathtt{R}, mathtt{L}}, {mathtt{L}, mathtt{R}}, {mathtt{R}, mathtt{R}}}$$$, the sum of madnesses is $$$0$$$. So, the answer is $$$1 + 1 + 1 + 1 + 0 ldots = 4$$$. | 3,100 | false | false | false | true | false | false | false | false | false | false | 3,468 |
1077D | You are given an array $$$s$$$ consisting of $$$n$$$ integers. You have to find any array $$$t$$$ of length $$$k$$$ such that you can cut out maximum number of copies of array $$$t$$$ from array $$$s$$$. Cutting out the copy of $$$t$$$ means that for each element $$$t_i$$$ of array $$$t$$$ you have to find $$$t_i$$$ in $$$s$$$ and remove it from $$$s$$$. If for some $$$t_i$$$ you cannot find such element in $$$s$$$, then you cannot cut out one more copy of $$$t$$$. The both arrays can contain duplicate elements. For example, if $$$s = [1, 2, 3, 2, 4, 3, 1]$$$ and $$$k = 3$$$ then one of the possible answers is $$$t = [1, 2, 3]$$$. This array $$$t$$$ can be cut out $$$2$$$ times. To cut out the first copy of $$$t$$$ you can use the elements $$$[1, underline{ extbf{2}}, 3, 2, 4, underline{ extbf{3}}, underline{ extbf{1}}]$$$ (use the highlighted elements). After cutting out the first copy of $$$t$$$ the array $$$s$$$ can look like $$$[1, 3, 2, 4]$$$. To cut out the second copy of $$$t$$$ you can use the elements $$$[underline{ extbf{1}}, underline{ extbf{3}}, underline{ extbf{2}}, 4]$$$. After cutting out the second copy of $$$t$$$ the array $$$s$$$ will be $$$[4]$$$. Your task is to find such array $$$t$$$ that you can cut out the copy of $$$t$$$ from $$$s$$$ maximum number of times. If there are multiple answers, you may choose any of them. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2 cdot 10^5$$$) β the number of elements in $$$s$$$ and the desired number of elements in $$$t$$$, respectively. The second line of the input contains exactly $$$n$$$ integers $$$s_1, s_2, dots, s_n$$$ ($$$1 le s_i le 2 cdot 10^5$$$). Output Print $$$k$$$ integers β the elements of array $$$t$$$ such that you can cut out maximum possible number of copies of this array from $$$s$$$. If there are multiple answers, print any of them. The required array $$$t$$$ can contain duplicate elements. All the elements of $$$t$$$ ($$$t_1, t_2, dots, t_k$$$) should satisfy the following condition: $$$1 le t_i le 2 cdot 10^5$$$. Examples Input 10 4 1 3 1 3 10 3 7 7 12 3 Input 15 2 1 2 1 1 1 2 1 1 2 1 2 1 1 1 1 Note The first example is described in the problem statement. In the second example the only answer is $$$[7, 3, 1, 3]$$$ and any its permutations. It can be shown that you cannot choose any other array such that the maximum number of copies you can cut out would be equal to $$$2$$$. In the third example the array $$$t$$$ can be cut out $$$5$$$ times. | 1,600 | false | false | false | false | false | false | false | true | true | false | 5,334 |
402B | The Queen of England has _n_ trees growing in a row in her garden. At that, the _i_-th (1u2009β€u2009_i_u2009β€u2009_n_) tree from the left has height _a__i_ meters. Today the Queen decided to update the scenery of her garden. She wants the trees' heights to meet the condition: for all _i_ (1u2009β€u2009_i_u2009<u2009_n_), _a__i_u2009+u20091u2009-u2009_a__i_u2009=u2009_k_, where _k_ is the number the Queen chose. Unfortunately, the royal gardener is not a machine and he cannot fulfill the desire of the Queen instantly! In one minute, the gardener can either decrease the height of a tree to any positive integer height or increase the height of a tree to any positive integer height. How should the royal gardener act to fulfill a whim of Her Majesty in the minimum number of minutes? Input The first line contains two space-separated integers: _n_, _k_ (1u2009β€u2009_n_,u2009_k_u2009β€u20091000). The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u20091000) β the heights of the trees in the row. Output In the first line print a single integer _p_ β the minimum number of minutes the gardener needs. In the next _p_ lines print the description of his actions. If the gardener needs to increase the height of the _j_-th (1u2009β€u2009_j_u2009β€u2009_n_) tree from the left by _x_ (_x_u2009β₯u20091) meters, then print in the corresponding line "+xa0jxa0x". If the gardener needs to decrease the height of the _j_-th (1u2009β€u2009_j_u2009β€u2009_n_) tree from the left by _x_ (_x_u2009β₯u20091) meters, print on the corresponding line "-xa0jxa0x". If there are multiple ways to make a row of trees beautiful in the minimum number of actions, you are allowed to print any of them. | 1,400 | false | false | true | false | false | false | true | false | false | false | 8,216 |
649D | Statement is not available on English language ΠΠ°ΠΌΡΡΡ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ° ΡΠΎΡΡΠΎΠΈΡ ΠΈΠ· _n_ ΡΡΠ΅Π΅ΠΊ, ΠΊΠΎΡΠΎΡΡΠ΅ Π²ΡΡΡΡΠΎΠ΅Π½Ρ Π² ΡΡΠ΄. ΠΡΠΎΠ½ΡΠΌΠ΅ΡΡΠ΅ΠΌ ΡΡΠ΅ΠΉΠΊΠΈ ΠΎΡ 1 Π΄ΠΎ _n_ ΡΠ»Π΅Π²Π° Π½Π°ΠΏΡΠ°Π²ΠΎ. ΠΡΠΎ ΠΊΠ°ΠΆΠ΄ΡΡ ΡΡΠ΅ΠΉΠΊΡ ΠΈΠ·Π²Π΅ΡΡΠ½ΠΎ, ΡΠ²ΠΎΠ±ΠΎΠ΄Π½Π° ΠΎΠ½Π° ΠΈΠ»ΠΈ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ ΠΊΠ°ΠΊΠΎΠΌΡ-Π»ΠΈΠ±ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΡ (Π² ΡΠ°ΠΊΠΎΠΌ ΡΠ»ΡΡΠ°Π΅ ΠΈΠ·Π²Π΅ΡΡΠ΅Π½ ΠΏΡΠΎΡΠ΅ΡΡ, ΠΊΠΎΡΠΎΡΠΎΠΌΡ ΠΎΠ½Π° ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ). ΠΠ»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΠ° ΠΈΠ·Π²Π΅ΡΡΠ½ΠΎ, ΡΡΠΎ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°ΡΠΈΠ΅ Π΅ΠΌΡ ΡΡΠ΅ΠΉΠΊΠΈ Π·Π°Π½ΠΈΠΌΠ°ΡΡ Π² ΠΏΠ°ΠΌΡΡΠΈ Π½Π΅ΠΏΡΠ΅ΡΡΠ²Π½ΡΠΉ ΡΡΠ°ΡΡΠΎΠΊ. Π‘ ΠΏΠΎΠΌΠΎΡΡΡ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ Π²ΠΈΠ΄Π° Β«ΠΏΠ΅ΡΠ΅ΠΏΠΈΡΠ°ΡΡ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· Π·Π°Π½ΡΡΠΎΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² ΡΠ²ΠΎΠ±ΠΎΠ΄Π½ΡΡ, Π° Π·Π°Π½ΡΡΡΡ ΡΠ΅ΠΏΠ΅ΡΡ ΡΡΠΈΡΠ°ΡΡ ΡΠ²ΠΎΠ±ΠΎΠ΄Π½ΠΎΠΉΒ» ΡΡΠ΅Π±ΡΠ΅ΡΡΡ ΡΠ°ΡΠΏΠΎΠ»ΠΎΠΆΠΈΡΡ Π²ΡΠ΅ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°ΡΠΈΠ΅ ΠΏΡΠΎΡΠ΅ΡΡΠ°ΠΌ ΡΡΠ΅ΠΉΠΊΠΈ Π² Π½Π°ΡΠ°Π»Π΅ ΠΏΠ°ΠΌΡΡΠΈ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ°. ΠΡΡΠ³ΠΈΠΌΠΈ ΡΠ»ΠΎΠ²Π°ΠΌΠΈ, Π»ΡΠ±Π°Ρ ΡΠ²ΠΎΠ±ΠΎΠ΄Π½Π°Ρ ΡΡΠ΅ΠΉΠΊΠ° Π΄ΠΎΠ»ΠΆΠ½Π° ΡΠ°ΡΠΏΠΎΠ»Π°Π³Π°ΡΡΡΡ ΠΏΡΠ°Π²Π΅Π΅ (ΠΈΠΌΠ΅ΡΡ Π±ΠΎΠ»ΡΡΠΈΠΉ Π½ΠΎΠΌΠ΅Ρ) Π»ΡΠ±ΠΎΠΉ Π·Π°Π½ΡΡΠΎΠΉ. ΠΠ°ΠΌ Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎ Π½Π°ΠΉΡΠΈ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ ΠΏΠ΅ΡΠ΅ΠΏΠΈΡΡΠ²Π°Π½ΠΈΡ Π΄Π°Π½Π½ΡΡ
ΠΈΠ· ΠΎΠ΄Π½ΠΎΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² Π΄ΡΡΠ³ΡΡ, Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΠΊΠΎΡΠΎΡΡΡ
ΠΌΠΎΠΆΠ½ΠΎ Π΄ΠΎΡΡΠΈΡΡ ΠΎΠΏΠΈΡΠ°Π½Π½ΡΡ
ΡΡΠ»ΠΎΠ²ΠΈΠΉ. ΠΠΎΠΏΡΡΡΠΈΠΌΠΎ, ΡΡΠΎ ΠΎΡΠ½ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠΎΡΡΠ΄ΠΎΠΊ ΡΡΠ΅Π΅ΠΊ Π² ΠΏΠ°ΠΌΡΡΠΈ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΈΠ· ΠΏΡΠΎΡΠ΅ΡΡΠΎΠ² ΠΈΠ·ΠΌΠ΅Π½ΠΈΡΡΡ ΠΏΠΎΡΠ»Π΅ Π΄Π΅ΡΡΠ°Π³ΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ, Π½ΠΎ ΠΎΡΠ½ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠΎΡΡΠ΄ΠΎΠΊ ΡΠ°ΠΌΠΈΡ
ΠΏΡΠΎΡΠ΅ΡΡΠΎΠ² Π΄ΠΎΠ»ΠΆΠ΅Π½ ΠΎΡΡΠ°ΡΡΡΡ Π±Π΅Π· ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΠΉ. ΠΡΠΎ Π·Π½Π°ΡΠΈΡ, ΡΡΠΎ Π΅ΡΠ»ΠΈ Π²ΡΠ΅ ΡΡΠ΅ΠΉΠΊΠΈ, ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ°ΡΠΈΠ΅ ΠΏΡΠΎΡΠ΅ΡΡΡ _i_, Π½Π°Ρ
ΠΎΠ΄ΠΈΠ»ΠΈΡΡ Π² ΠΏΠ°ΠΌΡΡΠΈ ΡΠ°Π½ΡΡΠ΅ Π²ΡΠ΅Ρ
ΡΡΠ΅Π΅ΠΊ ΠΏΡΠΎΡΠ΅ΡΡΠ° _j_, ΡΠΎ ΠΈ ΠΏΠΎΡΠ»Π΅ ΠΏΠ΅ΡΠ΅ΠΌΠ΅ΡΠ΅Π½ΠΈΠΉ ΡΡΠΎ ΡΡΠ»ΠΎΠ²ΠΈΠ΅ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π²ΡΠΏΠΎΠ»Π½ΡΡΡΡΡ. Π‘ΡΠΈΡΠ°ΠΉΡΠ΅, ΡΡΠΎ Π½ΠΎΠΌΠ΅ΡΠ° Π²ΡΠ΅Ρ
ΠΏΡΠΎΡΠ΅ΡΡΠΎΠ² ΡΠ½ΠΈΠΊΠ°Π»ΡΠ½Ρ, Ρ
ΠΎΡΡ Π±Ρ ΠΎΠ΄Π½Π° ΡΡΠ΅ΠΉΠΊΠ° ΠΏΠ°ΠΌΡΡΠΈ Π·Π°Π½ΡΡΠ° ΠΊΠ°ΠΊΠΈΠΌ-Π»ΠΈΠ±ΠΎ ΠΏΡΠΎΡΠ΅ΡΡΠΎΠΌ. ΠΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ Π ΠΏΠ΅ΡΠ²ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
Π·Π°ΠΏΠΈΡΠ°Π½ΠΎ ΡΠΈΡΠ»ΠΎ _n_ (1u2009β€u2009_n_u2009β€u2009200u2009000)xa0β ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΡΠ΅Π΅ΠΊ Π² ΠΏΠ°ΠΌΡΡΠΈ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ°. ΠΠΎ Π²ΡΠΎΡΠΎΠΉ ΡΡΡΠΎΠΊΠ΅ Π²Ρ
ΠΎΠ΄Π½ΡΡ
Π΄Π°Π½Π½ΡΡ
ΡΠ»Π΅Π΄ΡΡΡ _n_ ΡΠ΅Π»ΡΡ
ΡΠΈΡΠ΅Π» _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009_n_), Π³Π΄Π΅ _a__i_ ΡΠ°Π²Π½ΠΎ Π»ΠΈΠ±ΠΎ 0 (ΡΡΠΎ ΠΎΠ·Π½Π°ΡΠ°Π΅Ρ, ΡΡΠΎ _i_-Ρ ΡΡΠ΅ΠΉΠΊΠ° ΠΏΠ°ΠΌΡΡΠΈ ΡΠ²ΠΎΠ±ΠΎΠ΄Π½Π°), Π»ΠΈΠ±ΠΎ Π½ΠΎΠΌΠ΅ΡΡ ΠΏΡΠΎΡΠ΅ΡΡΠ°, ΠΊΠΎΡΠΎΡΠΎΠΌΡ ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠΈΡ _i_-Ρ ΡΡΠ΅ΠΉΠΊΠ° ΠΏΠ°ΠΌΡΡΠΈ. ΠΠ°ΡΠ°Π½ΡΠΈΡΡΠ΅ΡΡΡ, ΡΡΠΎ Ρ
ΠΎΡΡ Π±Ρ ΠΎΠ΄Π½ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ _a__i_ Π½Π΅ ΡΠ°Π²Π½ΠΎ 0. ΠΡΠΎΡΠ΅ΡΡΡ ΠΏΡΠΎΠ½ΡΠΌΠ΅ΡΠΎΠ²Π°Π½Ρ ΡΠ΅Π»ΡΠΌΠΈ ΡΠΈΡΠ»Π°ΠΌΠΈ ΠΎΡ 1 Π΄ΠΎ _n_ Π² ΠΏΡΠΎΠΈΠ·Π²ΠΎΠ»ΡΠ½ΠΎΠΌ ΠΏΠΎΡΡΠ΄ΠΊΠ΅. ΠΡΠΈ ΡΡΠΎΠΌ ΠΏΡΠΎΡΠ΅ΡΡΡ Π½Π΅ ΠΎΠ±ΡΠ·Π°ΡΠ΅Π»ΡΠ½ΠΎ ΠΏΡΠΎΠ½ΡΠΌΠ΅ΡΠΎΠ²Π°Π½Ρ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΡΠΌΠΈ ΡΠΈΡΠ»Π°ΠΌΠΈ. ΠΡΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ ΠΡΠ²Π΅Π΄ΠΈΡΠ΅ ΠΎΠ΄Π½ΠΎ ΡΠ΅Π»ΠΎΠ΅ ΡΠΈΡΠ»ΠΎxa0β ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π½ΡΠΆΠ½ΠΎ ΡΠ΄Π΅Π»Π°ΡΡ Π΄Π»Ρ Π΄Π΅ΡΡΠ°Π³ΠΌΠ΅Π½ΡΠ°ΡΠΈΠΈ ΠΏΠ°ΠΌΡΡΠΈ. ΠΡΠΈΠΌΠ΅ΡΡ ΠΡ
ΠΎΠ΄Π½ΡΠ΅ Π΄Π°Π½Π½ΡΠ΅ 8 0 8 8 8 0 4 4 2 ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΠ΅ Π ΠΏΠ΅ΡΠ²ΠΎΠΌ ΡΠ΅ΡΡΠΎΠ²ΠΎΠΌ ΠΏΡΠΈΠΌΠ΅ΡΠ΅ Π΄ΠΎΡΡΠ°ΡΠΎΡΠ½ΠΎ Π΄Π²ΡΡ
ΠΎΠΏΠ΅ΡΠ°ΡΠΈΠΉ: 1. ΠΠ΅ΡΠ΅ΠΏΠΈΡΠ°ΡΡ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· ΡΡΠ΅ΡΡΠ΅ΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² ΠΏΠ΅ΡΠ²ΡΡ. ΠΠΎΡΠ»Π΅ ΡΡΠΎΠ³ΠΎ ΠΏΠ°ΠΌΡΡΡ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ° ΠΏΡΠΈΠΌΠ΅Ρ Π²ΠΈΠ΄: 2xa02xa00xa01. 2. ΠΠ΅ΡΠ΅ΠΏΠΈΡΠ°ΡΡ Π΄Π°Π½Π½ΡΠ΅ ΠΈΠ· ΡΠ΅ΡΠ²Π΅ΡΡΠΎΠΉ ΡΡΠ΅ΠΉΠΊΠΈ Π² ΡΡΠ΅ΡΡΡ. ΠΠΎΡΠ»Π΅ ΡΡΠΎΠ³ΠΎ ΠΏΠ°ΠΌΡΡΡ ΠΊΠΎΠΌΠΏΡΡΡΠ΅ΡΠ° ΠΏΡΠΈΠΌΠ΅Ρ Π²ΠΈΠ΄: 2xa02xa01xa00. | 1,600 | false | true | true | false | false | true | false | false | false | false | 7,219 |
2041E | Problem - 2041E - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags constructive algorithms math *1200 No tag edit access β Contest materials ") ") div 2 = 4$$$. Note that the mean and median are not rounded to an integer. For example, the mean of array $$$ | 1,200 | true | false | false | false | false | true | false | false | false | false | 9 |
1498C | Gaurang has grown up in a mystical universe. He is faced by $$$n$$$ consecutive 2D planes. He shoots a particle of decay age $$$k$$$ at the planes. A particle can pass through a plane directly, however, every plane produces an identical copy of the particle going in the opposite direction with a decay age $$$k-1$$$. If a particle has decay age equal to $$$1$$$, it will NOT produce a copy. For example, if there are two planes and a particle is shot with decay age $$$3$$$ (towards the right), the process is as follows: (here, $$$D(x)$$$ refers to a single particle with decay age $$$x$$$) 1. the first plane produces a $$$D(2)$$$ to the left and lets $$$D(3)$$$ continue on to the right; 2. the second plane produces a $$$D(2)$$$ to the left and lets $$$D(3)$$$ continue on to the right; 3. the first plane lets $$$D(2)$$$ continue on to the left and produces a $$$D(1)$$$ to the right; 4. the second plane lets $$$D(1)$$$ continue on to the right ($$$D(1)$$$ cannot produce any copies). In total, the final multiset $$$S$$$ of particles is $$${D(3), D(2), D(2), D(1)}$$$. (See notes for visual explanation of this test case.) Gaurang is unable to cope up with the complexity of this situation when the number of planes is too large. Help Gaurang find the size of the multiset $$$S$$$, given $$$n$$$ and $$$k$$$. Since the size of the multiset can be very large, you have to output it modulo $$$10^9+7$$$. Note: Particles can go back and forth between the planes without colliding with each other. Input The first line of the input contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Then, $$$t$$$ lines follow, each containing two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 1000$$$). Additionally, the sum of $$$n$$$ over all test cases will not exceed $$$1000$$$, and the sum of $$$k$$$ over all test cases will not exceed $$$1000$$$. All test cases in one test are different. Output Output $$$t$$$ integers. The $$$i$$$-th of them should be equal to the answer to the $$$i$$$-th test case. Note Let us explain the first example with four test cases. Test case 1: ($$$n = 2$$$, $$$k = 3$$$) is already explained in the problem statement. See the below figure of this simulation. Each straight line with a different color represents the path of a different particle. As you can see, there are four distinct particles in the multiset. Note that the vertical spacing between reflected particles is for visual clarity only (as mentioned before, no two distinct particles collide with each other) Test case 2: ($$$n = 2$$$, $$$k = 2$$$) is explained as follows: 1. the first plane produces a $$$D(1)$$$ to the left and lets $$$D(2)$$$ continue on to the right; 2. the second plane produces a $$$D(1)$$$ to the left and lets $$$D(2)$$$ continue on to the right; 3. the first plane lets $$$D(1)$$$ continue on to the left ($$$D(1)$$$ cannot produce any copies). Total size of multiset obtained $$${D(1), D(1), D(2)}$$$ is equal to three. Test case 3: ($$$n = 3$$$, $$$k = 1$$$), there are three planes, but decay age is only one. So no new copies are produced while the one particle passes through the planes. Hence, the answer is one. Test case 4: ($$$n = 1$$$, $$$k = 3$$$) there is only one plane. The particle produces a new copy to the left. The multiset $$${D(2), D(3)}$$$ is of size two. | 1,600 | false | false | false | true | true | false | true | false | false | false | 3,179 |
1420B | You must lift the dam. With a lever. I will give it to you.You must block the canal. With a rock. I will not give the rock to you." Danik urgently needs rock and lever! Obviously, the easiest way to get these things is to ask Hermit Lizard for them. Hermit Lizard agreed to give Danik the lever. But to get a stone, Danik needs to solve the following task. You are given a positive integer $$$n$$$, and an array $$$a$$$ of positive integers. The task is to calculate the number of such pairs $$$(i,j)$$$ that $$$i<j$$$ and $$$a_i$$$ $$$&$$$ $$$a_j ge a_i oplus a_j$$$, where $$$&$$$ denotes the denoting the number of test cases. Description of the test cases follows. The first line of each test case contains one positive integer $$$n$$$ ($$$1 le n le 10^5$$$)xa0β length of the array. The second line contains $$$n$$$ positive integers $$$a_i$$$ ($$$1 le a_i le 10^9$$$)xa0β elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For every test case print one non-negative integerxa0β the answer to the problem. Example Input 5 5 1 4 3 7 10 3 1 1 1 4 6 2 5 3 2 2 4 1 1 Note In the first test case there is only one pair: $$$(4,7)$$$: for it $$$4$$$ $$$&$$$ $$$7 = 4$$$, and $$$4 oplus 7 = 3$$$. In the second test case all pairs are good. In the third test case there are two pairs: $$$(6,5)$$$ and $$$(2,3)$$$. In the fourth test case there are no good pairs. | 1,200 | true | false | false | false | false | false | false | false | false | false | 3,579 |
1108A | You are given two segments $$$[l_1; r_1]$$$ and $$$[l_2; r_2]$$$ on the $$$x$$$-axis. It is guaranteed that $$$l_1 < r_1$$$ and $$$l_2 < r_2$$$. Segments may intersect, overlap or even coincide with each other. The example of two segments on the $$$x$$$-axis. Your problem is to find two integers $$$a$$$ and $$$b$$$ such that $$$l_1 le a le r_1$$$, $$$l_2 le b le r_2$$$ and $$$a e b$$$. In other words, you have to choose two distinct integer points in such a way that the first point belongs to the segment $$$[l_1; r_1]$$$ and the second one belongs to the segment $$$[l_2; r_2]$$$. It is guaranteed that the answer exists. If there are multiple answers, you can print any of them. You have to answer $$$q$$$ independent queries. Input The first line of the input contains one integer $$$q$$$ ($$$1 le q le 500$$$) β the number of queries. Each of the next $$$q$$$ lines contains four integers $$$l_{1_i}, r_{1_i}, l_{2_i}$$$ and $$$r_{2_i}$$$ ($$$1 le l_{1_i}, r_{1_i}, l_{2_i}, r_{2_i} le 10^9, l_{1_i} < r_{1_i}, l_{2_i} < r_{2_i}$$$) β the ends of the segments in the $$$i$$$-th query. Output Print $$$2q$$$ integers. For the $$$i$$$-th query print two integers $$$a_i$$$ and $$$b_i$$$ β such numbers that $$$l_{1_i} le a_i le r_{1_i}$$$, $$$l_{2_i} le b_i le r_{2_i}$$$ and $$$a_i e b_i$$$. Queries are numbered in order of the input. It is guaranteed that the answer exists. If there are multiple answers, you can print any. Example Input 5 1 2 1 2 2 6 3 4 2 4 1 3 1 2 1 3 1 4 5 8 Output 2 1 3 4 3 2 1 2 3 7 | 800 | false | false | true | false | false | false | false | false | false | false | 5,162 |
1704H1 | This is the easy version of this problem. The difference between easy and hard versions is the constraint on $$$k$$$ and the time limit. Also, in this version of the problem, you only need to calculate the answer when $$$n=k$$$. You can make hacks only if both versions of the problem are solved. Cirno is playing a war simulator game with $$$n$$$ towers (numbered from $$$1$$$ to $$$n$$$) and $$$n$$$ bots (numbered from $$$1$$$ to $$$n$$$). The $$$i$$$-th tower is initially occupied by the $$$i$$$-th bot for $$$1 le i le n$$$. Before the game, Cirno first chooses a permutation $$$p = [p_1, p_2, ldots, p_n]$$$ of length $$$n$$$ (A permutation of length $$$n$$$ is an array of length $$$n$$$ where each integer between $$$1$$$ and $$$n$$$ appears exactly once). After that, she can choose a sequence $$$a = [a_1, a_2, ldots, a_n]$$$ ($$$1 le a_i le n$$$ and $$$a_i e i$$$ for all $$$1 le i le n$$$). The game has $$$n$$$ rounds of attacks. In the $$$i$$$-th round, if the $$$p_i$$$-th bot is still in the game, it will begin its attack, and as the result the $$$a_{p_i}$$$-th tower becomes occupied by the $$$p_i$$$-th bot; the bot that previously occupied the $$$a_{p_i}$$$-th tower will no longer occupy it. If the $$$p_i$$$-th bot is not in the game, nothing will happen in this round. After each round, if a bot doesn't occupy any towers, it will be eliminated and leave the game. Please note that no tower can be occupied by more than one bot, but one bot can occupy more than one tower during the game. At the end of the game, Cirno will record the result as a sequence $$$b = [b_1, b_2, ldots, b_n]$$$, where $$$b_i$$$ is the number of the bot that occupies the $$$i$$$-th tower at the end of the game. However, as a mathematics master, she wants you to solve the following counting problem instead of playing games: Count the number of different pairs of sequences $$$a$$$ and $$$b$$$ that we can get from all possible choices of sequence $$$a$$$ and permutation $$$p$$$. Since this number may be large, output it modulo $$$M$$$. Note For $$$n=1$$$, no valid sequence $$$a$$$ exists. We regard the answer as $$$0$$$. For $$$n=2$$$, there is only one possible array $$$a$$$: $$$[2, 1]$$$. For array $$$a$$$ is $$$[2, 1]$$$ and permutation $$$p$$$ is $$$[1, 2]$$$, the sequence $$$b$$$ will be $$$[1, 1]$$$ after all rounds have finished. The details for each rounds: In the first round, the first bot will begin its attack and successfully capture the tower $$$2$$$. After this round, the second bot will be eliminated and leave the game as all of its towers are occupied by other bots. In the second round, the second bot is not in the game. For array $$$a$$$ is $$$[2, 1]$$$ and permutation $$$p$$$ is $$$[2, 1]$$$, the sequence $$$b$$$ will be $$$[2, 2]$$$ after all rounds have finished. The details for each rounds: In the first round, the second bot will begin its attack and successfully capture the tower $$$1$$$. After this round, the first bot will be eliminated and leave the game as all of its towers are occupied by other bots. In the second round, the first bot is not in the game. So the number of different pairs of sequences $$$(a,b)$$$ is $$$2$$$ ($$$[2, 1]$$$, $$$[1, 1]$$$ and $$$[2, 1]$$$, $$$[2, 2]$$$) for $$$n=2$$$. | 3,200 | true | false | false | true | false | true | false | false | false | false | 2,048 |
794B | Igor the analyst has adopted _n_ little bunnies. As we all know, bunnies love carrots. Thus, Igor has bought a carrot to be shared between his bunnies. Igor wants to treat all the bunnies equally, and thus he wants to cut the carrot into _n_ pieces of equal area. Formally, the carrot can be viewed as an isosceles triangle with base length equal to 1 and height equal to _h_. Igor wants to make _n_u2009-u20091 cuts parallel to the base to cut the carrot into _n_ pieces. He wants to make sure that all _n_ pieces have the same area. Can you help Igor determine where to cut the carrot so that each piece have equal area? Illustration to the first example. Input The first and only line of input contains two space-separated integers, _n_ and _h_ (2u2009β€u2009_n_u2009β€u20091000, 1u2009β€u2009_h_u2009β€u2009105). Output The output should contain _n_u2009-u20091 real numbers _x_1,u2009_x_2,u2009...,u2009_x__n_u2009-u20091. The number _x__i_ denotes that the _i_-th cut must be made _x__i_ units away from the apex of the carrot. In addition, 0u2009<u2009_x_1u2009<u2009_x_2u2009<u2009...u2009<u2009_x__n_u2009-u20091u2009<u2009_h_ must hold. Your output will be considered correct if absolute or relative error of every number in your output doesn't exceed 10u2009-u20096. Formally, let your answer be _a_, and the jury's answer be _b_. Your answer is considered correct if . Examples Output 1.154700538379 1.632993161855 | 1,200 | true | false | false | false | false | false | false | false | false | false | 6,599 |
931C | Anya and Kirill are doing a physics laboratory work. In one of the tasks they have to measure some value _n_ times, and then compute the average value to lower the error. Kirill has already made his measurements, and has got the following integer values: _x_1, _x_2, ..., _x__n_. It is important that the values are close to each other, namely, the difference between the maximum value and the minimum value is at most 2. Anya does not want to make the measurements, however, she can't just copy the values from Kirill's work, because the error of each measurement is a random value, and this coincidence will be noted by the teacher. Anya wants to write such integer values _y_1, _y_2, ..., _y__n_ in her work, that the following conditions are met: the average value of _x_1,u2009_x_2,u2009...,u2009_x__n_ is equal to the average value of _y_1,u2009_y_2,u2009...,u2009_y__n_; all Anya's measurements are in the same bounds as all Kirill's measurements, that is, the maximum value among Anya's values is not greater than the maximum value among Kirill's values, and the minimum value among Anya's values is not less than the minimum value among Kirill's values; the number of equal measurements in Anya's work and Kirill's work is as small as possible among options with the previous conditions met. Formally, the teacher goes through all Anya's values one by one, if there is equal value in Kirill's work and it is not strike off yet, he strikes off this Anya's value and one of equal values in Kirill's work. The number of equal measurements is then the total number of strike off values in Anya's work. Help Anya to write such a set of measurements that the conditions above are met. Input The first line contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009100u2009000) β the numeber of measurements made by Kirill. The second line contains a sequence of integers _x_1,u2009_x_2,u2009...,u2009_x__n_ (u2009-u2009100u2009000u2009β€u2009_x__i_u2009β€u2009100u2009000) β the measurements made by Kirill. It is guaranteed that the difference between the maximum and minimum values among values _x_1,u2009_x_2,u2009...,u2009_x__n_ does not exceed 2. Output In the first line print the minimum possible number of equal measurements. In the second line print _n_ integers _y_1,u2009_y_2,u2009...,u2009_y__n_ β the values Anya should write. You can print the integers in arbitrary order. Keep in mind that the minimum value among Anya's values should be not less that the minimum among Kirill's values, and the maximum among Anya's values should be not greater than the maximum among Kirill's values. If there are multiple answers, print any of them. Examples Input 7 -10 -9 -10 -8 -10 -9 -9 Output 5 -10 -10 -9 -9 -9 -9 -9 Note In the first example Anya can write zeros as here measurements results. The average value is then equal to the average value of Kirill's values, and there are only two equal measurements. In the second example Anya should write two values 100 and one value 101 (in any order), because it is the only possibility to make the average be the equal to the average of Kirill's values. Thus, all three measurements are equal. In the third example the number of equal measurements is 5. | 1,700 | true | false | true | false | false | false | false | false | false | false | 5,979 |
1762E | Let us call an edge-weighted tree with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$ good if the weight of each edge is either $$$1$$$ or $$$-1$$$ and for each vertex $$$i$$$, the product of the edge weights of all edges having $$$i$$$ as one endpoint is $$$-1$$$. You are given a positive integer $$$n$$$. There are $$$n^{n-2} cdot 2^{n-1}$$$ distinct$$$^dagger$$$ edge-weighted trees with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$ such that each edge is either $$$1$$$ or $$$-1$$$. Your task is to find the sum of $$$d(1,n)^ddagger$$$ of all such trees that are good. Since the answer can be quite large, you only need to find it modulo $$$998,244,353$$$. $$$^dagger$$$ Two trees are considered to be distinct if either: there exists two vertices such that there is an edge between them in one of the trees, and not in the other. there exists two vertices such that there is an edge between them in both trees but the weight of the edge between them in one tree is different from the one in the other tree. Note that by . That is why total number of distinct edge-weighted tree is $$$n^{n-2} cdot 2^{n-1}$$$. $$$^ddagger$$$ $$$d(u,v)$$$ denotes the sum of the weight of all edges on the unique simple path from $$$u$$$ to $$$v$$$. Input The first and only line of input contains a single integer $$$n$$$ ($$$1 leq n leq 5 cdot 10^5$$$). Output The only line of output should contain a single integer, the required answer, modulo $$$998,244,353$$$. Note In the first test case, there is only $$$1$$$ distinct good tree. The value of $$$d(1,2)$$$ for that tree is $$$-1$$$, which is $$$998,244,352$$$ under modulo $$$998,244,353$$$. In the second test case, the value of $$$d(1,1)$$$ for any tree is $$$0$$$, so the answer is $$$0$$$. In the third test case, there are $$$16$$$ distinct good trees. The value of $$$d(1,4)$$$ is: $$$-2$$$ for $$$2$$$ trees; $$$-1$$$ for $$$8$$$ trees; $$$0$$$ for $$$4$$$ trees; $$$1$$$ for $$$2$$$ trees. The sum of $$$d(1,4)$$$ over all trees is $$$2 cdot (-2) + 8 cdot (-1) + 4 cdot (0) + 2 cdot (1) = -10$$$, which is $$$998,244,343$$$ under modulo $$$998,244,353$$$. | 2,600 | true | false | false | false | false | false | false | false | false | false | 1,736 |
1844F2 | The only difference between this problem and the easy version is the constraints on $$$t$$$ and $$$n$$$. You are given an array of $$$n$$$ positive integers $$$a_1,dots,a_n$$$, and a (possibly negative) integer $$$c$$$. Across all permutations $$$b_1,dots,b_n$$$ of the array $$$a_1,dots,a_n$$$, consider the minimum possible value of $$$$$$sum_{i=1}^{n-1} b_{i+1}-b_i-c.$$$$$$ Find the lexicographically smallest permutation $$$b$$$ of the array $$$a$$$ that achieves this minimum. A sequence $$$x$$$ is lexicographically smaller than a sequence $$$y$$$ if and only if one of the following holds: $$$x$$$ is a prefix of $$$y$$$, but $$$x e y$$$; in the first position where $$$x$$$ and $$$y$$$ differ, the sequence $$$x$$$ has a smaller element than the corresponding element in $$$y$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$c$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$-10^9 le c le 10^9$$$). The second line of each test case contains $$$n$$$ integers $$$a_1,dots,a_n$$$ ($$$1 le a_i le 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output $$$n$$$ integers $$$b_1,dots,b_n$$$, the lexicographically smallest permutation of $$$a$$$ that achieves the minimum $$$sumlimits_{i=1}^{n-1} b_{i+1}-b_i-c$$$. Example Input 3 6 -7 3 1 4 1 5 9 3 2 1 3 5 1 2718 2818 Output 9 3 1 4 5 1 1 3 5 2818 Note In the first test case, it can be proven that the minimum possible value of $$$sumlimits_{i=1}^{n-1} b_{i+1}-b_i-c$$$ is $$$27$$$, and the permutation $$$b = [9,3,1,4,5,1]$$$ is the lexicographically smallest permutation of $$$a$$$ that achieves this minimum: $$$3-9-(-7)+1-3-(-7)+4-1-(-7)+5-4-(-7)+1-5-(-7) = 1+5+10+8+3 = 27$$$. In the second test case, the minimum possible value of $$$sumlimits_{i=1}^{n-1} b_{i+1}-b_i-c$$$ is $$$0$$$, and $$$b = [1,3,5]$$$ is the lexicographically smallest permutation of $$$a$$$ that achieves this. In the third test case, there is only one permutation $$$b$$$. | 2,800 | true | true | false | false | true | true | false | true | true | false | 1,217 |
363B | There is a fence in front of Polycarpus's home. The fence consists of _n_ planks of the same width which go one after another from left to right. The height of the _i_-th plank is _h__i_ meters, distinct planks can have distinct heights. Fence for _n_u2009=u20097 and _h_u2009=u2009[1,u20092,u20096,u20091,u20091,u20097,u20091] Polycarpus has bought a posh piano and is thinking about how to get it into the house. In order to carry out his plan, he needs to take exactly _k_ consecutive planks from the fence. Higher planks are harder to tear off the fence, so Polycarpus wants to find such _k_ consecutive planks that the sum of their heights is minimal possible. Write the program that finds the indexes of _k_ consecutive planks with minimal total height. Pay attention, the fence is not around Polycarpus's home, it is in front of home (in other words, the fence isn't cyclic). Input The first line of the input contains integers _n_ and _k_ (1u2009β€u2009_n_u2009β€u20091.5Β·105,u20091u2009β€u2009_k_u2009β€u2009_n_) β the number of planks in the fence and the width of the hole for the piano. The second line contains the sequence of integers _h_1,u2009_h_2,u2009...,u2009_h__n_ (1u2009β€u2009_h__i_u2009β€u2009100), where _h__i_ is the height of the _i_-th plank of the fence. Output Print such integer _j_ that the sum of the heights of planks _j_, _j_u2009+u20091, ..., _j_u2009+u2009_k_u2009-u20091 is the minimum possible. If there are multiple such _j_'s, print any of them. Note In the sample, your task is to find three consecutive planks with the minimum sum of heights. In the given case three planks with indexes 3, 4 and 5 have the required attribute, their total height is 8. | 1,100 | false | false | false | true | false | false | true | false | false | false | 8,381 |
587B | Problem - 587B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags dp *2100 No tag edit access β Contest materials ") array, _a_0,u2009...,u2009_a__n_u2009-u20091 that _b_ can be build from _a_ with formula: _b__i_u2009=u2009_a__i_ _mod_ _n_ where _a_ _mod_ _b_ denoted the remainder of dividing _a_ by _b_. Duff is so curious, she wants to know the number of subsequences of _b_ like _b__i_1,u2009_b__i_2,u2009...,u2009_b__i__x_ (0u2009β€u2009_i_1u2009<u2009_i_2u2009<u2009...u2009<u2009_i__x_u2009<u2009_l_), such that: 1u2009β€u2009_x_u2009β€u2009_k_ For each 1u2009β€u2009_j_u2009β€u2009_x_u2009-u20091, For each 1u2009β€u2009_j_u2009β€u2009_x_u2009-u20091, _b__i__j_u2009β€u2009_b__i__j_u2009+u20091. i.e this subsequence is non-decreasing. Since this number can be very large, she want to know it modulo 109u2009+u20097. Duff is not a programmer, and Malek is unavailable at the moment. So she asked for your help. Please tell her this number. Input The first line of input contains three integers, _n_,u2009_l_ and _k_ (1u2009β€u2009_n_,u2009_k_, _n_u2009Γu2009_k_u2009β€u2009106 and 1u2009β€u2009_l_u2009β€u20091018). The second line contains _n_ space separated integers, _a_0,u2009_a_1,u2009...,u2009_a__n_u2009-u20091 (1u2009β€u2009_a__i_u2009β€u2009109 for each 0u2009β€u2009_i_u2009β€u2009_n_u2009-u20091). Output Print the answer modulo 1u2009000u2009000u2009007 in one line. Examples Input 3 5 3 5 9 1 Output 10 Input 5 10 3 1 2 3 4 5 Output 25 Note In the first sample case, . So all such sequences are: , , , , , , , , and . | 2,100 | false | false | false | true | false | false | false | false | false | false | 7,496 |
1682C | You are given an array $$$a$$$ of $$$n$$$ positive integers. Let $$$ ext{LIS}(a)$$$ denote the length of $$$ = $$$2$$$. $$$ ext{LIS}([underline{3}, underline{5}, underline{10}, underline{20}])$$$ = $$$4$$$. $$$ ext{LIS}([3, underline{1}, underline{2}, underline{4}])$$$ = $$$3$$$. We define array $$$a'$$$ as the array obtained after reversing the array $$$a$$$ i.e. $$$a' = [a_n, a_{n-1}, ldots , a_1]$$$. The beauty of array $$$a$$$ is defined as $$$min( ext{LIS}(a), ext{LIS}(a'))$$$. Your task is to determine the maximum possible beauty of the array $$$a$$$ if you can rearrange the array $$$a$$$ arbitrarily. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ $$$(1 leq t leq 10^4)$$$ xa0β the number of test cases. Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ $$$(1 leq n leq 2cdot 10^5)$$$ xa0β the length of array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,a_2, ldots ,a_n$$$ $$$(1 leq a_i leq 10^9)$$$ xa0β the elements of the array $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Note In the first test case, $$$a$$$ = $$$[6, 6, 6]$$$ and $$$a'$$$ = $$$[6, 6, 6]$$$. $$$ ext{LIS}(a) = ext{LIS}(a')$$$ = $$$1$$$. Hence the beauty is $$$min(1, 1) = 1$$$. In the second test case, $$$a$$$ can be rearranged to $$$[2, 5, 4, 5, 4, 2]$$$. Then $$$a'$$$ = $$$[2, 4, 5, 4, 5, 2]$$$. $$$ ext{LIS}(a) = ext{LIS}(a') = 3$$$. Hence the beauty is $$$3$$$ and it can be shown that this is the maximum possible beauty. In the third test case, $$$a$$$ can be rearranged to $$$[1, 2, 3, 2]$$$. Then $$$a'$$$ = $$$[2, 3, 2, 1]$$$. $$$ ext{LIS}(a) = 3$$$, $$$ ext{LIS}(a') = 2$$$. Hence the beauty is $$$min(3, 2) = 2$$$ and it can be shown that $$$2$$$ is the maximum possible beauty. | 1,400 | true | true | true | false | false | true | false | false | false | false | 2,177 |
1469C | You want to build a fence that will consist of $$$n$$$ equal sections. All sections have a width equal to $$$1$$$ and height equal to $$$k$$$. You will place all sections in one line side by side. Unfortunately, the ground beneath the fence is not flat. For simplicity, you can think that the ground level under the $$$i$$$-th section is equal to $$$h_i$$$. You should follow several rules to build the fence: 1. the consecutive sections should have a common side of length at least $$$1$$$; 2. the first and the last sections should stand on the corresponding ground levels; 3. the sections between may be either on the ground level or higher, but not higher than $$$k - 1$$$ from the ground level $$$h_i$$$ (the height should be an integer); One of possible fences (blue color) for the first test case Is it possible to build a fence that meets all rules? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 2 cdot 10^5$$$; $$$2 le k le 10^8$$$)xa0β the number of sections in the fence and the height of each section. The second line of each test case contains $$$n$$$ integers $$$h_1, h_2, dots, h_n$$$ ($$$0 le h_i le 10^8$$$), where $$$h_i$$$ is the ground level beneath the $$$i$$$-th section. It's guaranteed that the sum of $$$n$$$ over test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case print YES if it's possible to build the fence that meets all rules. Otherwise, print NO. You may print each letter in any case (for example, YES, Yes, yes, yEs will all be recognized as positive answer). Example Input 3 6 3 0 0 2 5 1 1 2 3 0 2 3 2 3 0 2 Note In the first test case, one of the possible fences is shown in the picture. In the second test case, according to the second rule, you should build both sections on the corresponding ground levels, and since $$$k = 3$$$, $$$h_1 = 0$$$, and $$$h_2 = 2$$$ the first rule is also fulfilled. In the third test case, according to the second rule, you should build the first section on height $$$3$$$ and the third section on height $$$2$$$. According to the first rule, the second section should be on the height of at least $$$2$$$ (to have a common side with the first section), but according to the third rule, the second section can be built on the height of at most $$$h_2 + k - 1 = 1$$$. | 1,600 | false | true | true | true | false | false | false | false | false | false | 3,336 |
1020B | In Summer Informatics School, if a student doesn't behave well, teachers make a hole in his badge. And today one of the teachers caught a group of $$$n$$$ students doing yet another trick. Let's assume that all these students are numbered from $$$1$$$ to $$$n$$$. The teacher came to student $$$a$$$ and put a hole in his badge. The student, however, claimed that the main culprit is some other student $$$p_a$$$. After that, the teacher came to student $$$p_a$$$ and made a hole in his badge as well. The student in reply said that the main culprit was student $$$p_{p_a}$$$. This process went on for a while, but, since the number of students was finite, eventually the teacher came to the student, who already had a hole in his badge. After that, the teacher put a second hole in the student's badge and decided that he is done with this process, and went to the sauna. You don't know the first student who was caught by the teacher. However, you know all the numbers $$$p_i$$$. Your task is to find out for every student $$$a$$$, who would be the student with two holes in the badge if the first caught student was $$$a$$$. Input The first line of the input contains the only integer $$$n$$$ ($$$1 le n le 1000$$$)xa0β the number of the naughty students. The second line contains $$$n$$$ integers $$$p_1$$$, ..., $$$p_n$$$ ($$$1 le p_i le n$$$), where $$$p_i$$$ indicates the student who was reported to the teacher by student $$$i$$$. Output For every student $$$a$$$ from $$$1$$$ to $$$n$$$ print which student would receive two holes in the badge, if $$$a$$$ was the first student caught by the teacher. Note The picture corresponds to the first example test case. When $$$a = 1$$$, the teacher comes to students $$$1$$$, $$$2$$$, $$$3$$$, $$$2$$$, in this order, and the student $$$2$$$ is the one who receives a second hole in his badge. When $$$a = 2$$$, the teacher comes to students $$$2$$$, $$$3$$$, $$$2$$$, and the student $$$2$$$ gets a second hole in his badge. When $$$a = 3$$$, the teacher will visit students $$$3$$$, $$$2$$$, $$$3$$$ with student $$$3$$$ getting a second hole in his badge. For the second example test case it's clear that no matter with whom the teacher starts, that student would be the one who gets the second hole in his badge. | 1,000 | false | false | false | false | false | false | true | false | false | true | 5,591 |
731B | The programming competition season has already started and it's time to train for ICPC. Sereja coaches his teams for a number of year and he knows that to get ready for the training session it's not enough to prepare only problems and editorial. As the training sessions lasts for several hours, teams become hungry. Thus, Sereja orders a number of pizzas so they can eat right after the end of the competition. Teams plan to train for _n_ times during _n_ consecutive days. During the training session Sereja orders exactly one pizza for each team that is present this day. He already knows that there will be _a__i_ teams on the _i_-th day. There are two types of discounts in Sereja's favourite pizzeria. The first discount works if one buys two pizzas at one day, while the second is a coupon that allows to buy one pizza during two consecutive days (two pizzas in total). As Sereja orders really a lot of pizza at this place, he is the golden client and can use the unlimited number of discounts and coupons of any type at any days. Sereja wants to order exactly _a__i_ pizzas on the _i_-th day while using only discounts and coupons. Note, that he will never buy more pizzas than he need for this particular day. Help him determine, whether he can buy the proper amount of pizzas each day if he is allowed to use only coupons and discounts. Note, that it's also prohibited to have any active coupons after the end of the day _n_. Input The first line of input contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009200u2009000)xa0β the number of training sessions. The second line contains _n_ integers _a_1, _a_2, ..., _a__n_ (0u2009β€u2009_a__i_u2009β€u200910u2009000)xa0β the number of teams that will be present on each of the days. Output If there is a way to order pizzas using only coupons and discounts and do not buy any extra pizzas on any of the days, then print "YES" (without quotes) in the only line of output. Otherwise, print "NO" (without quotes). Note In the first sample, Sereja can use one coupon to buy one pizza on the first and the second days, one coupon to buy pizza on the second and the third days and one discount to buy pizzas on the fourth days. This is the only way to order pizzas for this sample. In the second sample, Sereja can't use neither the coupon nor the discount without ordering an extra pizza. Note, that it's possible that there will be no teams attending the training sessions on some days. | 1,100 | false | true | false | false | false | true | false | false | false | false | 6,875 |
600B | Problem - 600B - Codeforces =============== xa0 ]( "21794") β the sizes of arrays _a_ and _b_. The second line contains _n_ integers β the elements of array _a_ (u2009-u2009109u2009β€u2009_a__i_u2009β€u2009109). The third line contains _m_ integers β the elements of array _b_ (u2009-u2009109u2009β€u2009_b__j_u2009β€u2009109). Output Print _m_ integers, separated by spaces: the _j_-th of which is equal to the number of such elements in array _a_ that are less than or equal to the value _b__j_. Examples Input 5 4 1 3 5 7 9 6 4 2 8 Output 3 2 1 4 Input 5 5 1 2 1 2 5 3 1 4 1 5 Output 4 2 4 2 5 | 1,300 | false | false | false | false | true | false | false | true | true | false | 7,445 |
1649A | You are playing a very popular computer game. The next level consists of $$$n$$$ consecutive locations, numbered from $$$1$$$ to $$$n$$$, each of them containing either land or water. It is known that the first and last locations contain land, and for completing the level you have to move from the first location to the last. Also, if you become inside a location with water, you will die, so you can only move between locations with land. You can jump between adjacent locations for free, as well as no more than once jump from any location with land $$$i$$$ to any location with land $$$i + x$$$, spending $$$x$$$ coins ($$$x geq 0$$$). Your task is to spend the minimum possible number of coins to move from the first location to the last one. Note that this is always possible since both the first and last locations are the land locations. Input There are several test cases in the input data. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0β the number of test cases. This is followed by the test cases description. The first line of each test case contains one integer $$$n$$$ ($$$2 leq n leq 100$$$)xa0β the number of locations. The second line of the test case contains a sequence of integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 leq a_i leq 1$$$), where $$$a_i = 1$$$ means that the $$$i$$$-th location is the location with land, and $$$a_i = 0$$$ means that the $$$i$$$-th location is the location with water. It is guaranteed that $$$a_1 = 1$$$ and $$$a_n = 1$$$. Output For each test case print a single integerxa0β the answer to the problem. Example Input 3 2 1 1 5 1 0 1 0 1 4 1 0 1 1 Note In the first test case, it is enough to make one free jump from the first location to the second one, which is also the last one, so the answer is $$$0$$$. In the second test case, the only way to move from the first location to the last one is to jump between them, which will cost $$$4$$$ coins. In the third test case, you can jump from the first location to the third for $$$2$$$ coins, and then jump to the fourth location for free, so the answer is $$$2$$$. It can be shown that this is the optimal way. | 800 | false | false | true | false | false | false | false | false | false | false | 2,377 |
1786A1 | This is an easy version of the problem. In this version, all cards have the same color. Alice has $$$n$$$ cards, each card is white, and the cards are stacked in a deck. Alice deals the cards to herself and to Bob, dealing at once several cards from the top of the deck in the following order: one card to herself, two cards to Bob, three cards to Bob, four cards to herself, five cards to herself, six cards to Bob, seven cards to Bob, eight cards to herself, and so on. In other words, on the $$$i$$$-th step, Alice deals $$$i$$$ top cards from the deck to one of the players; on the first step, she deals the cards to herself and then alternates the players every two steps. When there aren't enough cards at some step, Alice deals all the remaining cards to the current player, and the process stops. First Alice's steps in a deck of many cards. How many cards will Alice and Bob have at the end? Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 200$$$). The description of the test cases follows The only line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^6$$$)xa0β the number of cards. Output For each test case print two integersxa0β the number of cards in the end for each playerxa0β in this order: cards Alice has, cards Bob has. Example Output 5 5 1 5 10 7 3 5 500202 499798 | 800 | false | false | true | false | false | false | false | false | false | false | 1,566 |
385B | Problem - 385B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags brute force greedy implementation math strings *1200 No tag edit access β Contest materials ") β Editorial") , consisting of lowercase English letters. The bear wants to count the number of such pairs of indices _i_,u2009_j_ (1u2009β€u2009_i_u2009β€u2009_j_u2009β€u2009_s_), that string _x_(_i_,u2009_j_)u2009=u2009_s__i__s__i_u2009+u20091... _s__j_ contains at least one string "bear" as a substring. String _x_(_i_,u2009_j_) contains string "bear", if there is such index _k_ (_i_u2009β€u2009_k_u2009β€u2009_j_u2009-u20093), that _s__k_u2009=u2009_b_, _s__k_u2009+u20091u2009=u2009_e_, _s__k_u2009+u20092u2009=u2009_a_, _s__k_u2009+u20093u2009=u2009_r_. Help the bear cope with the given problem. Input The first line contains a non-empty string _s_ (1u2009β€u2009_s_u2009β€u20095000). It is guaranteed that the string only consists of lowercase English letters. Output Print a single number β the answer to the problem. Examples Input bearbtear Output 6 Input bearaabearc Output 20 Note In the first sample, the following pairs (_i_,u2009_j_) match: (1,u20094),u2009(1,u20095),u2009(1,u20096),u2009(1,u20097),u2009(1,u20098),u2009(1,u20099). In the second sample, the following pairs (_i_,u2009_j_) match: (1,u2009u20094),u2009(1,u2009u20095),u2009(1,u2009u20096),u2009(1,u2009u20097),u2009(1,u2009u20098),u2009(1,u2009u20099),u2009(1,u2009u200910),u2009(1,u2009u200911),u2009(2,u2009u200910),u2009(2,u2009u200911),u2009(3,u2009u200910),u2009(3,u2009u200911),u2009(4,u2009u200910),u2009(4,u2009u200911),u2009(5,u2009u200910),u2009(5,u2009u200911),u2009(6,u2009u200910),u2009(6,u2009u200911),u2009(7,u2009u200910),u2009(7,u2009u200911). | 1,200 | true | true | true | false | false | false | true | false | false | false | 8,290 |
1714G | You are given a rooted tree. It contains $$$n$$$ vertices, which are numbered from $$$1$$$ to $$$n$$$. The root is the vertex $$$1$$$. Each edge has two positive integer values. Thus, two positive integers $$$a_j$$$ and $$$b_j$$$ are given for each edge. Output $$$n-1$$$ numbers $$$r_2, r_3, dots, r_n$$$, where $$$r_i$$$ is defined as follows. Consider the path from the root (vertex $$$1$$$) to $$$i$$$ ($$$2 le i le n$$$). Let the sum of the costs of $$$a_j$$$ along this path be $$$A_i$$$. Then $$$r_i$$$ is equal to the length of the maximum prefix of this path such that the sum of $$$b_j$$$ along this prefix does not exceed $$$A_i$$$. Example for $$$n=9$$$. The blue color shows the costs of $$$a_j$$$, and the red color shows the costs of $$$b_j$$$. Consider an example. In this case: $$$r_2=0$$$, since the path to $$$2$$$ has an amount of $$$a_j$$$ equal to $$$5$$$, only the prefix of this path of length $$$0$$$ has a smaller or equal amount of $$$b_j$$$; $$$r_3=3$$$, since the path to $$$3$$$ has an amount of $$$a_j$$$ equal to $$$5+9+5=19$$$, the prefix of length $$$3$$$ of this path has a sum of $$$b_j$$$ equal to $$$6+10+1=17$$$ ( the number is $$$17 le 19$$$); $$$r_4=1$$$, since the path to $$$4$$$ has an amount of $$$a_j$$$ equal to $$$5+9=14$$$, the prefix of length $$$1$$$ of this path has an amount of $$$b_j$$$ equal to $$$6$$$ (this is the longest suitable prefix, since the prefix of length $$$2$$$ already has an amount of $$$b_j$$$ equal to $$$6+10=16$$$, which is more than $$$14$$$); $$$r_5=2$$$, since the path to $$$5$$$ has an amount of $$$a_j$$$ equal to $$$5+9+2=16$$$, the prefix of length $$$2$$$ of this path has a sum of $$$b_j$$$ equal to $$$6+10=16$$$ (this is the longest suitable prefix, since the prefix of length $$$3$$$ already has an amount of $$$b_j$$$ equal to $$$6+10+1=17$$$, what is more than $$$16$$$); $$$r_6=1$$$, since the path up to $$$6$$$ has an amount of $$$a_j$$$ equal to $$$2$$$, the prefix of length $$$1$$$ of this path has an amount of $$$b_j$$$ equal to $$$1$$$; $$$r_7=1$$$, since the path to $$$7$$$ has an amount of $$$a_j$$$ equal to $$$5+3=8$$$, the prefix of length $$$1$$$ of this path has an amount of $$$b_j$$$ equal to $$$6$$$ (this is the longest suitable prefix, since the prefix of length $$$2$$$ already has an amount of $$$b_j$$$ equal to $$$6+3=9$$$, which is more than $$$8$$$); $$$r_8=2$$$, since the path up to $$$8$$$ has an amount of $$$a_j$$$ equal to $$$2+4=6$$$, the prefix of length $$$2$$$ of this path has an amount of $$$b_j$$$ equal to $$$1+3=4$$$; $$$r_9=3$$$, since the path to $$$9$$$ has an amount of $$$a_j$$$ equal to $$$2+4+1=7$$$, the prefix of length $$$3$$$ of this path has a sum of $$$b_j$$$ equal to $$$1+3+3=7$$$. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases in the test. The descriptions of test cases follow. Each description begins with a line that contains an integer $$$n$$$ ($$$2 le n le 2cdot10^5$$$) β the number of vertices in the tree. This is followed by $$$n-1$$$ string, each of which contains three numbers $$$p_j, a_j, b_j$$$ ($$$1 le p_j le n$$$; $$$1 le a_j,b_j le 10^9$$$) β the ancestor of the vertex $$$j$$$, the first and second values an edge that leads from $$$p_j$$$ to $$$j$$$. The value of $$$j$$$ runs through all values from $$$2$$$ to $$$n$$$ inclusive. It is guaranteed that each set of input data has a correct hanged tree with a root at the vertex $$$1$$$. It is guaranteed that the sum of $$$n$$$ over all input test cases does not exceed $$$2cdot10^5$$$. Output For each test case, output $$$n-1$$$ integer in one line: $$$r_2, r_3, dots, r_n$$$. Example Input 4 9 1 5 6 4 5 1 2 9 10 4 2 1 1 2 1 2 3 3 6 4 3 8 1 3 4 1 1 100 2 1 1 3 101 1 4 1 100 1 2 1 1 3 1 101 10 1 1 4 2 3 5 2 5 1 3 4 3 3 1 5 5 3 5 5 2 1 1 3 2 6 2 1 Output 0 3 1 2 1 1 2 3 0 0 3 1 2 2 0 1 2 1 1 2 2 1 1 Note The first example is clarified in the statement. In the second example: $$$r_2=0$$$, since the path to $$$2$$$ has an amount of $$$a_j$$$ equal to $$$1$$$, only the prefix of this path of length $$$0$$$ has a smaller or equal amount of $$$b_j$$$; $$$r_3=0$$$, since the path to $$$3$$$ has an amount of $$$a_j$$$ equal to $$$1+1=2$$$, the prefix of length $$$1$$$ of this path has an amount of $$$b_j$$$ equal to $$$100$$$ ($$$100 > 2$$$); $$$r_4=3$$$, since the path to $$$4$$$ has an amount of $$$a_j$$$ equal to $$$1+1+101=103$$$, the prefix of length $$$3$$$ of this path has an amount of $$$b_j$$$ equal to $$$102$$$, . | 1,700 | false | false | false | false | true | false | false | true | false | false | 1,994 |
1403C | The mythic world of Chess Land is a rectangular grid of squares with $$$R$$$ rows and $$$C$$$ columns, $$$R$$$ being greater than or equal to $$$C$$$. Its rows and columns are numbered from $$$1$$$ to $$$R$$$ and $$$1$$$ to $$$C$$$, respectively. The inhabitants of Chess Land are usually mentioned as pieces in everyday language, and there are $$$5$$$ specific types of them roaming the land: pawns, rooks, bishops, queens and kings. Contrary to popular belief, chivalry is long dead in Chess Land, so there are no knights to be found. Each piece is unique in the way it moves around from square to square: in one step, a pawn can move one row forward (i.e. from row $$$r$$$ to $$$r+1$$$), without changing columns; a rook can move any number of columns left/right without changing rows OR move any number of rows forward/backward without changing columns; a bishop can move to any square of the two diagonals intersecting at its currently occupied square; a queen can move to any square where a rook or a bishop could move to from her position; and a king can move to any of the $$$8$$$ adjacent squares. In the following figure, we marked by X the squares each piece can move to in a single step (here, the rows are numbered from bottom to top, and the columns from left to right). Recently, Chess Land has become a dangerous place: pieces that are passing through the land can get captured unexpectedly by unknown forces and simply disappear. As a consequence, they would like to reach their destinations as fast (i.e. in as few moves) as possible, and they are also interested in the number of different ways it is possible for them to reach it, using the minimal number of steps β because more paths being available could mean lower chances of getting captured. Two paths are considered different if they differ in at least one visited square. For this problem, let us assume that pieces are entering Chess Land in a given column of row $$$1$$$, and exit the land in a given column of row $$$R$$$. Your task is to answer $$$Q$$$ questions: given the type of a piece, the column it enters row $$$1$$$ and the column it must reach in row $$$R$$$ in order to exit, compute the minimal number of moves it has to make in Chess Land, and the number of different ways it is able to do so. Input The first line contains three space-separated integers $$$R$$$, $$$C$$$, and $$$Q$$$ $$$(1 leq Q leq 1000$$$, $$$2 leq C leq 1000$$$ and $$$C leq R leq 10^9)$$$ β the number of rows and columns of Chess Land, and the number of questions, respectively. Then $$$Q$$$ lines follow. Each line consists of a character $$$T$$$, corresponding to the type of the piece in question ('P' for pawn, 'R' for rook, 'B' for bishop, 'Q' for queen and 'K' for king); two integers $$$c_1$$$ and $$$c_R$$$, $$$1leq c_1,c_Rleq C$$$, denoting that the piece starts from the $$$c_1$$$-th column of row $$$1$$$, and has to reach the $$$c_R$$$-th column of row $$$R$$$. Output You have to print $$$Q$$$ lines, the $$$i$$$-th one containing two space separated integers, the answer to the $$$i$$$-th question: the first one is the minimal number of steps needed, the second is the number of different paths available using this number of steps. Since the answer can be quite large, you have to compute it modulo $$$10^9+7$$$. If it is impossible to reach the target square, output the line "0 0". | 3,200 | true | false | true | true | false | false | false | false | false | false | 3,654 |
763C | Little Timofey likes integers a lot. Unfortunately, he is very young and can't work with very big integers, so he does all the operations modulo his favorite prime _m_. Also, Timofey likes to look for arithmetical progressions everywhere. One of his birthday presents was a sequence of distinct integers _a_1,u2009_a_2,u2009...,u2009_a__n_. Timofey wants to know whether he can rearrange the elements of the sequence so that is will be an arithmetical progression modulo _m_, or not. Arithmetical progression modulo _m_ of length _n_ with first element _x_ and difference _d_ is sequence of integers _x_,u2009_x_u2009+u2009_d_,u2009_x_u2009+u20092_d_,u2009...,u2009_x_u2009+u2009(_n_u2009-u20091)Β·_d_, each taken modulo _m_. Input The first line contains two integers _m_ and _n_ (2u2009β€u2009_m_u2009β€u2009109u2009+u20097, 1u2009β€u2009_n_u2009β€u2009105, _m_ is prime)xa0β Timofey's favorite prime module and the length of the sequence. The second line contains _n_ distinct integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009β€u2009_a__i_u2009<u2009_m_)xa0β the elements of the sequence. Output Print -1 if it is not possible to rearrange the elements of the sequence so that is will be an arithmetical progression modulo _m_. Otherwise, print two integersxa0β the first element of the obtained progression _x_ (0u2009β€u2009_x_u2009<u2009_m_) and its difference _d_ (0u2009β€u2009_d_u2009<u2009_m_). If there are multiple answers, print any of them. | 2,600 | true | false | true | false | false | false | true | false | false | false | 6,735 |
188D | . Output Output the described pattern. Examples Output * ** *** **** ***** | 1,100 | false | false | true | false | false | false | false | false | false | false | 9,089 |
1350A | Orac is studying number theory, and he is interested in the properties of divisors. For two positive integers $$$a$$$ and $$$b$$$, $$$a$$$ is a divisor of $$$b$$$ if and only if there exists an integer $$$c$$$, such that $$$acdot c=b$$$. For $$$n ge 2$$$, we will denote as $$$f(n)$$$ the smallest positive divisor of $$$n$$$, except $$$1$$$. For example, $$$f(7)=7,f(10)=2,f(35)=5$$$. For the fixed integer $$$n$$$, Orac decided to add $$$f(n)$$$ to $$$n$$$. For example, if he had an integer $$$n=5$$$, the new value of $$$n$$$ will be equal to $$$10$$$. And if he had an integer $$$n=6$$$, $$$n$$$ will be changed to $$$8$$$. Orac loved it so much, so he decided to repeat this operation several times. Now, for two positive integers $$$n$$$ and $$$k$$$, Orac asked you to add $$$f(n)$$$ to $$$n$$$ exactly $$$k$$$ times (note that $$$n$$$ will change after each operation, so $$$f(n)$$$ may change too) and tell him the final value of $$$n$$$. For example, if Orac gives you $$$n=5$$$ and $$$k=2$$$, at first you should add $$$f(5)=5$$$ to $$$n=5$$$, so your new value of $$$n$$$ will be equal to $$$n=10$$$, after that, you should add $$$f(10)=2$$$ to $$$10$$$, so your new (and the final!) value of $$$n$$$ will be equal to $$$12$$$. Orac may ask you these queries many times. Input The first line of the input is a single integer $$$t (1le tle 100)$$$: the number of times that Orac will ask you. Each of the next $$$t$$$ lines contains two positive integers $$$n,k (2le nle 10^6, 1le kle 10^9)$$$, corresponding to a query by Orac. It is guaranteed that the total sum of $$$n$$$ is at most $$$10^6$$$. Note In the first query, $$$n=5$$$ and $$$k=1$$$. The divisors of $$$5$$$ are $$$1$$$ and $$$5$$$, the smallest one except $$$1$$$ is $$$5$$$. Therefore, the only operation adds $$$f(5)=5$$$ to $$$5$$$, and the result is $$$10$$$. In the second query, $$$n=8$$$ and $$$k=2$$$. The divisors of $$$8$$$ are $$$1,2,4,8$$$, where the smallest one except $$$1$$$ is $$$2$$$, then after one operation $$$8$$$ turns into $$$8+(f(8)=2)=10$$$. The divisors of $$$10$$$ are $$$1,2,5,10$$$, where the smallest one except $$$1$$$ is $$$2$$$, therefore the answer is $$$10+(f(10)=2)=12$$$. In the third query, $$$n$$$ is changed as follows: $$$3 o 6 o 8 o 10 o 12$$$. | 900 | true | false | false | false | false | false | false | false | false | false | 3,957 |
1692C | Mihai has an $$$8 imes 8$$$ chessboard whose rows are numbered from $$$1$$$ to $$$8$$$ from top to bottom and whose columns are numbered from $$$1$$$ to $$$8$$$ from left to right. Mihai has placed exactly one bishop on the chessboard. The bishop is not placed on the edges of the board. (In other words, the row and column of the bishop are between $$$2$$$ and $$$7$$$, inclusive.) The bishop attacks in all directions diagonally, and there is no limit to the distance which the bishop can attack. Note that the cell on which the bishop is placed is also considered attacked. An example of a bishop on a chessboard. The squares it attacks are marked in red. Mihai has marked all squares the bishop attacks, but forgot where the bishop was! Help Mihai find the position of the bishop. Input The first line of the input contains a single integer $$$t$$$ ($$$1 leq t leq 36$$$)xa0β the number of test cases. The description of test cases follows. There is an empty line before each test case. Each test case consists of $$$8$$$ lines, each containing $$$8$$$ characters. Each of these characters is either '#' or '.', denoting a square under attack and a square not under attack, respectively. Output For each test case, output two integers $$$r$$$ and $$$c$$$ ($$$2 leq r, c leq 7$$$)xa0β the row and column of the bishop. The input is generated in such a way that there is always exactly one possible location of the bishop that is not on the edge of the board. Example Input 3 .....#.. #...#... .#.#.... ..#..... .#.#.... #...#... .....#.. ......#. #.#..... .#...... #.#..... ...#.... ....#... .....#.. ......#. .......# .#.....# ..#...#. ...#.#.. ....#... ...#.#.. ..#...#. .#.....# #....... Note The first test case is pictured in the statement. Since the bishop lies in the intersection row $$$4$$$ and column $$$3$$$, the correct output is 4 3. | 800 | false | false | true | false | false | false | false | false | false | false | 2,128 |
1278C | Karlsson has recently discovered a huge stock of berry jam jars in the basement of the house. More specifically, there were $$$2n$$$ jars of strawberry and blueberry jam. All the $$$2n$$$ jars are arranged in a row. The stairs to the basement are exactly in the middle of that row. So when Karlsson enters the basement, he sees exactly $$$n$$$ jars to his left and $$$n$$$ jars to his right. For example, the basement might look like this: Being the starightforward man he is, he immediately starts eating the jam. In one minute he chooses to empty either the first non-empty jar to his left or the first non-empty jar to his right. Finally, Karlsson decided that at the end the amount of full strawberry and blueberry jam jars should become the same. For example, this might be the result: He has eaten $$$1$$$ jar to his left and then $$$5$$$ jars to his right. There remained exactly $$$3$$$ full jars of both strawberry and blueberry jam. Jars are numbered from $$$1$$$ to $$$2n$$$ from left to right, so Karlsson initially stands between jars $$$n$$$ and $$$n+1$$$. What is the minimum number of jars Karlsson is required to empty so that an equal number of full strawberry and blueberry jam jars is left? Your program should answer $$$t$$$ independent test cases. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1000$$$) β the number of test cases. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$). The second line of each test case contains $$$2n$$$ integers $$$a_1, a_2, dots, a_{2n}$$$ ($$$1 le a_i le 2$$$) β $$$a_i=1$$$ means that the $$$i$$$-th jar from the left is a strawberry jam jar and $$$a_i=2$$$ means that it is a blueberry jam jar. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Note The picture from the statement describes the first test case. In the second test case the number of strawberry and blueberry jam jars is already equal. In the third test case Karlsson is required to eat all $$$6$$$ jars so that there remain $$$0$$$ jars of both jams. In the fourth test case Karlsson can empty either the second and the third jars or the third and the fourth one. The both scenarios will leave $$$1$$$ jar of both jams. | 1,700 | false | true | true | true | true | false | false | false | false | false | 4,304 |
316D1 | Smart Beaver decided to be not only smart, but also a healthy beaver! And so he began to attend physical education classes at school X. In this school, physical education has a very creative teacher. One of his favorite warm-up exercises is throwing balls. Students line up. Each one gets a single ball in the beginning. The balls are numbered from 1 to _n_ (by the demand of the inventory commission). Figure 1. The initial position for _n_u2009=u20095. After receiving the balls the students perform the warm-up exercise. The exercise takes place in a few throws. For each throw the teacher chooses any two arbitrary different students who will participate in it. The selected students throw their balls to each other. Thus, after each throw the students remain in their positions, and the two balls are swapped. Figure 2. The example of a throw. In this case there was a throw between the students, who were holding the 2-nd and the 4-th balls. Since the warm-up has many exercises, each of them can only continue for little time. Therefore, for each student we know the maximum number of throws he can participate in. For this lessons maximum number of throws will be 1 or 2. Note that after all phases of the considered exercise any ball can end up with any student. Smart Beaver decided to formalize it and introduced the concept of the "ball order". The ball order is a sequence of _n_ numbers that correspond to the order of balls in the line. The first number will match the number of the ball of the first from the left student in the line, the second number will match the ball of the second student, and so on. For example, in figure 2 the order of the balls was (1, 2, 3, 4, 5), and after the throw it was (1, 4, 3, 2, 5). Smart beaver knows the number of students and for each student he knows the maximum number of throws in which he can participate. And now he is wondering: what is the number of distinct ways of ball orders by the end of the exercise. Input The first line contains a single number _n_ β the number of students in the line and the number of balls. The next line contains exactly _n_ space-separated integers. Each number corresponds to a student in the line (the _i_-th number corresponds to the _i_-th from the left student in the line) and shows the number of throws he can participate in. The input limits for scoring 30 points are (subproblem D1): 1u2009β€u2009_n_u2009β€u200910. The input limits for scoring 70 points are (subproblems D1+D2): 1u2009β€u2009_n_u2009β€u2009500. The input limits for scoring 100 points are (subproblems D1+D2+D3): 1u2009β€u2009_n_u2009β€u20091000000. | 2,300 | false | false | false | true | false | false | true | false | false | false | 8,577 |
521E | You are organizing a cycling race on the streets of the city. The city contains _n_ junctions, some pairs of them are connected by roads; on each road you can move in any direction. No two roads connect the same pair of intersections, and no road connects the intersection with itself. You want the race to be open to both professional athletes and beginner cyclists, and that's why you will organize the race in three nominations: easy, moderate and difficult; each participant will choose the more suitable nomination. For each nomination you must choose the route β the chain of junctions, consecutively connected by roads. Routes must meet the following conditions: all three routes should start at the same intersection, and finish at the same intersection (place of start and finish can't be the same); to avoid collisions, no two routes can have common junctions (except for the common start and finish), and can not go along the same road (irrespective of the driving direction on the road for those two routes); no route must pass twice through the same intersection or visit the same road twice (irrespective of the driving direction on the road for the first and second time of visit). Preparing for the competition is about to begin, and you need to determine the routes of the race as quickly as possible. The length of the routes is not important, it is only important that all the given requirements were met. Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u20092Β·105) β the number of intersections and roads, respectively. The following _m_ lines contain two integers β the numbers of the intersections connected by a road (the intersections are numbered starting with 1). It is guaranteed that each pair of intersections is connected by no more than one road, and no road connects the intersection to itself. Please note that it is not guaranteed that you can get from any junction to any other one by using the roads. Output If it is possible to create the routes, in the first line print "YES". In the next three lines print the descriptions of each of the three routes in the format "_l_ _p_1 ... _p__l_", where _l_ is the number of intersections in the route, and _p_1,u2009...,u2009_p__l_ are their numbers in the order they follow. The routes must meet all the requirements specified in the statement. If it is impossible to make the routes in accordance with the requirements, print NO. Examples Input 5 6 1 2 1 3 1 4 2 5 3 5 4 5 Output YES 3 5 4 1 3 5 3 1 3 5 2 1 | 3,100 | false | false | false | false | false | false | false | false | false | true | 7,751 |
358D | Dima liked the present he got from Inna very much. He liked the present he got from Seryozha even more. Dima felt so grateful to Inna about the present that he decided to buy her _n_ hares. Inna was very happy. She lined up the hares in a row, numbered them from 1 to _n_ from left to right and started feeding them with carrots. Inna was determined to feed each hare exactly once. But in what order should she feed them? Inna noticed that each hare radiates joy when she feeds it. And the joy of the specific hare depends on whether Inna fed its adjacent hares before feeding it. Inna knows how much joy a hare radiates if it eats when either both of his adjacent hares are hungry, or one of the adjacent hares is full (that is, has been fed), or both of the adjacent hares are full. Please note that hares number 1 and _n_ don't have a left and a right-adjacent hare correspondingly, so they can never have two full adjacent hares. Help Inna maximize the total joy the hares radiate. :) Input The first line of the input contains integer _n_ (1u2009β€u2009_n_u2009β€u20093000) β the number of hares. Then three lines follow, each line has _n_ integers. The first line contains integers _a_1 _a_2 ... _a__n_. The second line contains _b_1,u2009_b_2,u2009...,u2009_b__n_. The third line contains _c_1,u2009_c_2,u2009...,u2009_c__n_. The following limits are fulfilled: 0u2009β€u2009_a__i_,u2009_b__i_,u2009_c__i_u2009β€u2009105. Number _a__i_ in the first line shows the joy that hare number _i_ gets if his adjacent hares are both hungry. Number _b__i_ in the second line shows the joy that hare number _i_ radiates if he has exactly one full adjacent hare. Number _Ρ__i_ in the third line shows the joy that hare number _i_ radiates if both his adjacent hares are full. Output In a single line, print the maximum possible total joy of the hares Inna can get by feeding them. Examples Input 4 1 2 3 4 4 3 2 1 0 1 1 0 Input 7 8 5 7 6 1 8 9 2 7 9 5 4 3 1 2 3 3 4 1 1 3 | 1,800 | false | true | false | true | false | false | false | false | false | false | 8,401 |
1560F2 | Problem - 1560F2 - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags bitmasks brute force constructive algorithms dfs and similar dp greedy *2100 No tag edit access β Contest materials ") time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output It is a complicated version of problem F1. The difference between them is the constraints (F1: $$$k le 2$$$, F2: $$$k le 10$$$). You are given an integer $$$n$$$. Find the minimum integer $$$x$$$ such that $$$x ge n$$$ and the number $$$x$$$ is $$$k$$$-beautiful. A number is called $$$k$$$-beautiful if its decimal representation having no leading zeroes contains no more than $$$k$$$ different digits. E.g. if $$$k = 2$$$, the numbers $$$3434443$$$, $$$55550$$$, $$$777$$$ and $$$21$$$ are $$$k$$$-beautiful whereas the numbers $$$120$$$, $$$445435$$$ and $$$998244353$$$ are not. Input The first line contains one integer $$$t$$$ ($$$1 le t le 10^4$$$) β the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^9$$$, $$$1 le k le 10$$$). Output For each test case output on a separate line $$$x$$$ β the minimum $$$k$$$-beautiful integer such that $$$x ge n$$$. Example Input 6 2021 3 177890 2 34512 3 724533 4 998244353 1 12345678 10 Output 2021 181111 34533 724542 999999999 12345678 | 2,100 | false | true | false | true | false | true | true | false | false | false | 2,824 |
1846C | Rudolf has registered for a programming competition that will follow the rules of ICPC. The rules imply that for each solved problem, a participant gets $$$1$$$ point, and also incurs a penalty equal to the number of minutes passed from the beginning of the competition to the moment of solving the problem. In the final table, the participant with the most points is ranked higher, and in case of a tie in points, the participant with the lower penalty is ranked higher. In total, $$$n$$$ participants have registered for the competition. Rudolf is a participant with index $$$1$$$. It is known that $$$m$$$ problems will be proposed. And the competition will last $$$h$$$ minutes. A powerful artificial intelligence has predicted the values $$$t_{i, j}$$$, which represent the number of minutes it will take for the $$$i$$$-th participant to solve the $$$j$$$-th problem. Rudolf realized that the order of solving problems will affect the final result. For example, if $$$h = 120$$$, and the times to solve problems are [$$$20, 15, 110$$$], then if Rudolf solves the problems in the order: $$${3, 1, 2}$$$, then he will only solve the third problem and get $$$1$$$ point and $$$110$$$ penalty. $$${1, 2, 3}$$$, then he will solve the first problem after $$$20$$$ minutes from the start, the second one after $$$20+15=35$$$ minutes, and he will not have time to solve the third one. Thus, he will get $$$2$$$ points and $$$20+35=55$$$ penalty. $$${2, 1, 3}$$$, then he will solve the second problem after $$$15$$$ minutes from the start, the first one after $$$15+20=35$$$ minutes, and he will not have time to solve the third one. Thus, he will get $$$2$$$ points and $$$15+35=50$$$ penalty. Rudolf became interested in what place he will take in the competition if each participant solves problems in the optimal order based on the predictions of the artificial intelligence. It will be assumed that in case of a tie in points and penalty, Rudolf will take the best place. Input The first line contains an integer $$$t$$$ ($$$1 le t le 10^3$$$) β the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains three integers $$$n, m, h$$$ ($$$1 le n cdot m le 2 cdot 10^5, 1 le h le 10^6$$$) β the number of participants, the number of problems, and the duration of the competition, respectively. Then there are $$$n$$$ lines, each containing $$$m$$$ integers $$$t_{i, j}$$$ ($$$1 le t_{i, j} le 10^6$$$) β the number of minutes it will take for the $$$i$$$-th participant to solve the $$$j$$$-th problem. The sum of $$$n cdot m$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output an integer β Rudolf's place in the final table if all participants solve problems in the optimal order. Example Input 5 3 3 120 20 15 110 90 90 100 40 40 40 2 1 120 30 30 1 3 120 10 20 30 3 2 27 8 9 10 7 10 8 3 3 15 7 2 6 7 5 4 1 9 8 Note In the first example, Rudolf will get $$$2$$$ points and $$$50$$$ penalty minutes. The second participant will solve only one problem and get $$$1$$$ point and $$$90$$$ penalty minutes. And the third participant will solve all $$$3$$$ problems and get $$$3$$$ points and $$$240$$$ penalty minutes. Thus, Rudolf will take the second place. In the second example, both participants will get $$$1$$$ point and $$$30$$$ penalty minutes. In case of a tie in points, Rudolf gets the better position, so he will take the first place. In the third example, Rudolf is the only participant, so he will take the first place. In the fourth example, all participants can solve two problems with penalty of $$$25 = 8 + (8 + 9)$$$, $$$24 = 7 + (7 + 10)$$$ and $$$26 = 8 + (8 + 10)$$$, respectively, thanks to the penalty, the second participant gets the first place, and Rudolf gets the second. | 1,200 | false | true | false | true | true | true | false | false | true | false | 1,206 |
917C | As we all know, Dart is some kind of creature from Upside Down world. For simplicity, we call their kind pollywogs. Dart and _x_u2009-u20091 other pollywogs are playing a game. There are _n_ stones in a row, numbered from 1 through _n_ from left to right. At most 1 pollywog may be sitting on each stone at a time. Initially, the pollywogs are sitting on the first _x_ stones (one pollywog on each stone). Dart and his friends want to end up on the last _x_ stones. At each second, the leftmost pollywog should jump to the right. A pollywog can jump at most _k_ stones; more specifically, a pollywog can jump from stone number _i_ to stones _i_u2009+u20091,u2009_i_u2009+u20092,u2009... _i_u2009+u2009_k_. A pollywog can't jump on an occupied stone. Jumping a distance _i_ takes _c__i_ amounts of energy from the pollywog. Also, _q_ stones are special Each time landing on a special stone _p_, takes _w__p_ amounts of energy (in addition to the energy for jump) from the pollywog. _w__p_ could be negative, in this case, it means the pollywog absorbs _w__p_ amounts of energy. Pollywogs want to spend as little energy as possible (this value could be negative). They're just pollywogs, so they asked for your help. Tell them the total change in their energy, in case they move optimally. Input The first line of input contains four integers, _x_,u2009_k_,u2009_n_ and _q_ (1u2009β€u2009_x_u2009β€u2009_k_u2009β€u20098, _k_u2009β€u2009_n_u2009β€u2009108, 0u2009β€u2009_q_u2009β€u2009_min_(25,u2009_n_u2009-u2009_x_))xa0β the number of pollywogs, the maximum length of jump, the number of stones and the number of special stones. The next line contains _k_ integers, _c_1,u2009_c_2,u2009... _c__k_, separated by spaces (1u2009β€u2009_c__i_u2009β€u2009109)xa0β the energetic costs of jumps. The next _q_ lines contain description of the special stones. Each line contains two integers _p_ and _w__p_ (_x_u2009+u20091u2009β€u2009_p_u2009β€u2009_n_, _w__p_u2009β€u2009109). All _p_ are distinct. | 2,900 | false | false | false | true | false | false | false | false | false | false | 6,061 |
809C | After a wonderful evening in the restaurant the time to go home came. Leha as a true gentlemen suggested Noora to give her a lift. Certainly the girl agreed with pleasure. Suddenly one problem appeared: Leha cannot find his car on a huge parking near the restaurant. So he decided to turn to the watchman for help. Formally the parking can be represented as a matrix 109u2009Γu2009109. There is exactly one car in every cell of the matrix. All cars have their own machine numbers represented as a positive integer. Let's index the columns of the matrix by integers from 1 to 109 from left to right and the rows by integers from 1 to 109 from top to bottom. By coincidence it turned out, that for every cell (_x_,u2009_y_) the number of the car, which stands in this cell, is equal to the minimum positive integer, which can't be found in the cells (_i_,u2009_y_) and (_x_,u2009_j_), 1u2009β€u2009_i_u2009<u2009_x_,u20091u2009β€u2009_j_u2009<u2009_y_. The upper left fragment 5u2009Γu20095 of the parking Leha wants to ask the watchman _q_ requests, which can help him to find his car. Every request is represented as five integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2,u2009_k_. The watchman have to consider all cells (_x_,u2009_y_) of the matrix, such that _x_1u2009β€u2009_x_u2009β€u2009_x_2 and _y_1u2009β€u2009_y_u2009β€u2009_y_2, and if the number of the car in cell (_x_,u2009_y_) does not exceed _k_, increase the answer to the request by the number of the car in cell (_x_,u2009_y_). For each request Leha asks the watchman to tell him the resulting sum. Due to the fact that the sum can turn out to be quite large, hacker asks to calculate it modulo 109u2009+u20097. However the requests seem to be impracticable for the watchman. Help the watchman to answer all Leha's requests. Input The first line contains one integer _q_ (1u2009β€u2009_q_u2009β€u2009104)xa0β the number of Leha's requests. The next _q_ lines contain five integers _x_1,u2009_y_1,u2009_x_2,u2009_y_2,u2009_k_ (1u2009β€u2009_x_1u2009β€u2009_x_2u2009β€u2009109,u20091u2009β€u2009_y_1u2009β€u2009_y_2u2009β€u2009109,u20091u2009β€u2009_k_u2009β€u20092Β·109)xa0β parameters of Leha's requests. Note Let's analyze all the requests. In each case the requested submatrix is highlighted in blue. In the first request (_k_u2009=u20091) Leha asks only about the upper left parking cell. In this cell the car's number is 1. Consequentally the answer is 1. In the second request (_k_u2009=u20095) suitable numbers are 4,u20091,u20092,u20093,u20092,u20091. Consequentally the answer is 4u2009+u20091u2009+u20092u2009+u20093u2009+u20092u2009+u20091u2009=u200913. In the third request (_k_u2009=u200910000) Leha asks about the upper left frament 5u2009Γu20095 of the parking. Since _k_ is big enough, the answer is equal to 93. In the last request (_k_u2009=u20092) none of the cur's numbers are suitable, so the answer is 0. | 2,600 | false | false | false | true | false | false | false | false | false | false | 6,526 |
1728C | Let's define $$$f(x)$$$ for a positive integer $$$x$$$ as the length of the base-10 representation of $$$x$$$ without leading zeros. I like to call it a digital logarithm. Similar to a digital root, if you are familiar with that. You are given two arrays $$$a$$$ and $$$b$$$, each containing $$$n$$$ positive integers. In one operation, you do the following: 1. pick some integer $$$i$$$ from $$$1$$$ to $$$n$$$; 2. assign either $$$f(a_i)$$$ to $$$a_i$$$ or $$$f(b_i)$$$ to $$$b_i$$$. Two arrays are considered similar to each other if you can rearrange the elements in both of them, so that they are equal (e.u2009g. $$$a_i = b_i$$$ for all $$$i$$$ from $$$1$$$ to $$$n$$$). What's the smallest number of operations required to make $$$a$$$ and $$$b$$$ similar to each other? Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0β the number of testcases. The first line of the testcase contains a single integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$)xa0β the number of elements in each of the arrays. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i < 10^9$$$). The third line contains $$$n$$$ integers $$$b_1, b_2, dots, b_n$$$ ($$$1 le b_j < 10^9$$$). The sum of $$$n$$$ over all testcases doesn't exceed $$$2 cdot 10^5$$$. Output For each testcase, print the smallest number of operations required to make $$$a$$$ and $$$b$$$ similar to each other. Example Input 4 1 1 1000 4 1 2 3 4 3 1 4 2 3 2 9 3 1 100 9 10 75019 709259 5 611271314 9024533 81871864 9 3 6 4865 9503 2 371245467 6 7 37376159 8 364036498 52295554 169 Note In the first testcase, you can apply the digital logarithm to $$$b_1$$$ twice. In the second testcase, the arrays are already similar to each other. In the third testcase, you can first apply the digital logarithm to $$$a_1$$$, then to $$$b_2$$$. | 1,400 | false | true | false | false | true | false | false | false | true | false | 1,923 |
261A | Maxim always goes to the supermarket on Sundays. Today the supermarket has a special offer of discount systems. There are _m_ types of discounts. We assume that the discounts are indexed from 1 to _m_. To use the discount number _i_, the customer takes a special basket, where he puts exactly _q__i_ items he buys. Under the terms of the discount system, in addition to the items in the cart the customer can receive at most two items from the supermarket for free. The number of the "free items" (0, 1 or 2) to give is selected by the customer. The only condition imposed on the selected "free items" is as follows: each of them mustn't be more expensive than the cheapest item out of the _q__i_ items in the cart. Maxim now needs to buy _n_ items in the shop. Count the minimum sum of money that Maxim needs to buy them, if he use the discount system optimally well. Please assume that the supermarket has enough carts for any actions. Maxim can use the same discount multiple times. Of course, Maxim can buy items without any discounts. Input The first line contains integer _m_ (1u2009β€u2009_m_u2009β€u2009105) β the number of discount types. The second line contains _m_ integers: _q_1,u2009_q_2,u2009...,u2009_q__m_ (1u2009β€u2009_q__i_u2009β€u2009105). The third line contains integer _n_ (1u2009β€u2009_n_u2009β€u2009105) β the number of items Maxim needs. The fourth line contains _n_ integers: _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009104) β the items' prices. The numbers in the lines are separated by single spaces. Output In a single line print a single integer β the answer to the problem. Note In the first sample Maxim needs to buy two items that cost 100 and get a discount for two free items that cost 50. In that case, Maxim is going to pay 200. In the second sample the best strategy for Maxim is to buy 3 items and get 2 items for free using the discount. In that case, Maxim is going to pay 150. | 1,400 | false | true | false | false | false | false | false | false | true | false | 8,790 |
1208H | You are given a tree of $$$n$$$ nodes. The tree is rooted at node $$$1$$$, which is not considered as a leaf regardless of its degree. Each leaf of the tree has one of the two colors: red or blue. Leaf node $$$v$$$ initially has color $$$s_{v}$$$. The color of each of the internal nodes (including the root) is determined as follows. Let $$$b$$$ be the number of blue immediate children, and $$$r$$$ be the number of red immediate children of a given vertex. Then the color of this vertex is blue if and only if $$$b - r ge k$$$, otherwise red. Integer $$$k$$$ is a parameter that is same for all the nodes. You need to handle the following types of queries: 1 v: print the color of node $$$v$$$; 2 v c: change the color of leaf $$$v$$$ to $$$c$$$ ($$$c = 0$$$ means red, $$$c = 1$$$ means blue); 3 h: update the current value of $$$k$$$ to $$$h$$$. Input The first line of the input consists of two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 10^{5}$$$, $$$-n le k le n$$$)xa0β the number of nodes and the initial parameter $$$k$$$. Each of the next $$$n - 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 le u,v le n$$$), denoting that there is an edge between vertices $$$u$$$ and $$$v$$$. The next line consists of $$$n$$$ space separated integersxa0β the initial array $$$s$$$ ($$$-1 le s_i le 1$$$). $$$s_{i} = 0$$$ means that the color of node $$$i$$$ is red. $$$s_{i} = 1$$$ means that the color of node $$$i$$$ is blue. $$$s_{i} = -1$$$ means that the node $$$i$$$ is not a leaf. The next line contains an integer $$$q$$$ ($$$1 le q le 10^5$$$), the number of queries. $$$q$$$ lines follow, each containing a query in one of the following queries: 1 v ($$$1 le v le n$$$): print the color of node $$$v$$$; 2 v c ($$$1 le v le n$$$, $$$c = 0$$$ or $$$c = 1$$$): change the color of leaf $$$v$$$ to $$$c$$$ ($$$c = 0$$$ means red, $$$c = 1$$$ means blue). It is guaranteed that $$$v$$$ is a leaf; 3 h ($$$-n le h le n$$$): update the current value of $$$k$$$ to $$$h$$$. Output For each query of the first type, print $$$0$$$ if the color of vertex $$$v$$$ is red, and $$$1$$$ otherwise. Example Input 5 2 1 2 1 3 2 4 2 5 -1 -1 0 1 0 9 1 1 1 2 3 -2 1 1 1 2 3 1 2 5 1 1 1 1 2 Note Figures:(i) The initial tree (ii) The tree after the 3rd query (iii) The tree after the 7th query | 3,500 | false | false | true | false | true | false | false | false | false | false | 4,647 |
76F | Tourist walks along the _X_ axis. He can choose either of two directions and any speed not exceeding _V_. He can also stand without moving anywhere. He knows from newspapers that at time _t_1 in the point with coordinate _x_1 an interesting event will occur, at time _t_2 in the point with coordinate _x_2 β another one, and so on up to (_x__n_,u2009_t__n_). Interesting events are short so we can assume they are immediate. Event _i_ counts visited if at time _t__i_ tourist was at point with coordinate _x__i_. Write program tourist that will find maximum number of events tourist if: at the beginning (when time is equal to 0) tourist appears at point 0, tourist can choose initial point for himself. Yes, you should answer on two similar but different questions. Input The first line of input contains single integer number _N_ (1u2009β€u2009_N_u2009β€u2009100000) β number of interesting events. The following _N_ lines contain two integers _x__i_ and _t__i_ β coordinate and time of the _i_-th event. The last line of the input contains integer _V_ β maximum speed of the tourist. All _x__i_ will be within range u2009-u20092Β·108u2009β€u2009_x__i_u2009β€u20092Β·108, all _t__i_ will be between 1 and 2Β·106 inclusive. _V_ will be positive and will not exceed 1000. The input may contain events that happen at the same time or in the same place but not in the same place at the same time. Output The only line of the output should contain two space-sepatated integers β maximum number of events tourist can visit in he starts moving from point 0 at time 0, and maximum number of events tourist can visit if he chooses the initial point for himself. | 2,300 | false | false | false | true | true | false | false | true | false | false | 9,581 |
1250D | A large-scale conference on unnatural sciences is going to be held soon in Berland! In total, $$$n$$$ scientists from all around the world have applied. All of them have indicated a time segment when they will attend the conference: two integers $$$l_i$$$, $$$r_i$$$ β day of arrival and day of departure. Also, some of the scientists have indicated their country, while some preferred not to. So, each scientist also has a value $$$c_i$$$, where: $$$c_i > 0$$$, if the scientist is coming from country $$$c_i$$$ (all countries are numbered from $$$1$$$ to $$$200$$$); $$$c_i = 0$$$, if the scientist preferred to not indicate the country. Everyone knows that it is interesting and useful to chat with representatives of other countries! A participant of the conference will be upset if she will not meet people from other countries during the stay. It is possible to meet people during all time of stay, including the day of arrival and the day of departure. Conference organizers need to be ready for the worst! They are interested in the largest number $$$x$$$, that it is possible that among all people attending the conference exactly $$$x$$$ will be upset. Help the organizers to find the maximum number of upset scientists. Input The first line of the input contains integer $$$t$$$ ($$$1 le t le 100$$$) β number of test cases. Then the test cases follow. The first line of each test case contains integer $$$n$$$ ($$$1 le n le 500$$$) β the number of registered conference participants. Next $$$n$$$ lines follow, each containing three integers $$$l_i$$$, $$$r_i$$$, $$$c_i$$$ ($$$1 le l_i le r_i le 10^6$$$, $$$0 le c_i le 200$$$) β the day of arrival, the day of departure and the country (or the fact that it was not indicated) for the $$$i$$$-th participant. The sum of $$$n$$$ among all test cases in the input does not exceed $$$500$$$. Output Output $$$t$$$ integers β maximum number of upset scientists for each test case. Example Input 2 4 1 10 30 5 6 30 6 12 0 1 1 0 4 1 2 1 2 3 0 3 4 0 4 5 2 | 3,000 | false | false | false | true | false | false | false | false | false | false | 4,452 |
1137F | Lena is playing with matches. The natural question arising in the head of any child playing with matches is whether it's possible to set a tree on fire with a matches, or not. Let's say, that the tree is a connected graph without cycles and the vertices are labeled with integers $$$1, 2, ldots, n$$$. Also every vertex $$$v$$$ has some integer priority $$$p_v$$$ associated with it. All priorities are distinct. It turns out, that if you set a tree on fire, it will burn to nothing. However, this process doesn't happen instantly. At the beginning, burns out the leaf (a vertex is called to be a leaf if it has only one adjacent vertex) of the tree of the minimum priority. Then burns out the leaf of the minimal priority of the remaining tree, and so on. This way, the vertices turn into the leaves and burn out until only one vertex remains. Then this vertex burns out as well. Lena has prepared a tree of $$$n$$$ vertices and every vertex in it has a priority $$$p_v = v$$$. Lena is very curious about burning out this tree. However, she understands, that if she will burn the tree now, then it will disappear completely. Lena is a kind girl and she will feel bad for the burned tree, so she wants to study the process of burning the tree only in her mind. Lena wants to process $$$q$$$ queries, each of them is one of three following types: "up $$$v$$$", assign the vertex $$$v$$$ priority $$$1 + max{p_1, p_2, ldots, p_n}$$$; "when $$$v$$$", find the step at which the vertex $$$v$$$ will burn out, if the tree would be set on fire now; "compare $$$v$$$ $$$u$$$", find out which of the vertices $$$v$$$ and $$$u$$$ will burn out first, if the tree would be set on fire now. Notice, that if all priorities would be distinct, then after the "up" query they will stay distinct as well. Initially all priorities are distinct, hence during any (purely hypothetical of course) burning of the tree, all leafs would have distinct priorities. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 200,000$$$, $$$1 le q le 200,000$$$)xa0β the number of vertices in the tree and the number of queries. The $$$i$$$-th of the following $$$n - 1$$$ lines contains two integers $$$v_i$$$, $$$u_i$$$ ($$$1 le v_i, u_i le n$$$), denoting the endpoints of the $$$i$$$-th edge. Each of the remaining $$$q$$$ lines contains a query of one of the following three types: "up $$$v$$$" ($$$1 le v le n$$$)xa0β change the priority of vertex $$$v$$$; "when $$$v$$$" ($$$1 le v le n$$$)xa0β determine the step at which the vertex $$$v$$$ will burn out; "compare $$$v$$$ $$$u$$$" ($$$1 le v, u le n$$$, $$$v e u$$$)xa0β determine which of vertices $$$v$$$ and $$$u$$$ will burn out earlier in the current tree. It's guaranteed, that there is at least one query of type "when" or "compare". Output For every query of type "when" print one integer in range from $$$1$$$ to $$$n$$$xa0β the step at which the vertex $$$v$$$ will burn out. For every query of type "compare" print either $$$v$$$ or $$$u$$$, depending on which one will burn out earlier. Examples Input 5 7 1 5 1 2 1 3 4 3 when 1 when 2 when 3 when 4 when 5 compare 2 3 compare 3 4 Input 5 5 1 5 1 2 1 3 4 3 up 1 compare 2 4 compare 4 3 compare 3 1 compare 1 5 Note In the first example, the process of burning of the tree is illustrated on the following picture: In particular, the vertices of the tree will burn out in the following order: $$$[2, 4, 3, 1, 5]$$$. In the second example, after applying the "up" operation, the order of vertices will change to: $$$[2, 4, 3, 5, 1]$$$. | 3,400 | false | false | false | false | true | false | false | false | false | false | 5,037 |
1828B | You are given an unsorted permutation $$$p_1, p_2, ldots, p_n$$$. To sort the permutation, you choose a constant $$$k$$$ ($$$k ge 1$$$) and do some operations on the permutation. In one operation, you can choose two integers $$$i$$$, $$$j$$$ ($$$1 le j < i le n$$$) such that $$$i - j = k$$$, then swap $$$p_i$$$ and $$$p_j$$$. What is the maximum value of $$$k$$$ that you can choose to sort the given permutation? 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). An unsorted permutation $$$p$$$ is a permutation such that there is at least one position $$$i$$$ that satisfies $$$p_i e i$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 le n le 10^{5}$$$)xa0β the length of the permutation $$$p$$$. The second line of each test case contains $$$n$$$ distinct integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$)xa0β the permutation $$$p$$$. It is guaranteed that the given numbers form a permutation of length $$$n$$$ and the given permutation is unsorted. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^{5}$$$. Output For each test case, output the maximum value of $$$k$$$ that you can choose to sort the given permutation. We can show that an answer always exists. Note In the first test case, the maximum value of $$$k$$$ you can choose is $$$1$$$. The operations used to sort the permutation are: Swap $$$p_2$$$ and $$$p_1$$$ ($$$2 - 1 = 1$$$) $$$ ightarrow$$$ $$$p = [1, 3, 2]$$$ Swap $$$p_2$$$ and $$$p_3$$$ ($$$3 - 2 = 1$$$) $$$ ightarrow$$$ $$$p = [1, 2, 3]$$$ In the second test case, the maximum value of $$$k$$$ you can choose is $$$2$$$. The operations used to sort the permutation are: Swap $$$p_3$$$ and $$$p_1$$$ ($$$3 - 1 = 2$$$) $$$ ightarrow$$$ $$$p = [1, 4, 3, 2]$$$ Swap $$$p_4$$$ and $$$p_2$$$ ($$$4 - 2 = 2$$$) $$$ ightarrow$$$ $$$p = [1, 2, 3, 4]$$$ | 900 | true | false | false | false | false | false | false | false | false | false | 1,315 |
936D | Vitya loves programming and problem solving, but sometimes, to distract himself a little, he plays computer games. Once he found a new interesting game about tanks, and he liked it so much that he went through almost all levels in one day. Remained only the last level, which was too tricky. Then Vitya remembered that he is a programmer, and wrote a program that helped him to pass this difficult level. Try do the same. The game is organized as follows. There is a long road, two cells wide and _n_ cells long. Some cells have obstacles. You control a tank that occupies one cell. Initially, the tank is located before the start of the road, in a cell with coordinates (0,u20091). Your task is to move the tank to the end of the road, to the cell (_n_u2009+u20091,u20091) or (_n_u2009+u20091,u20092). Every second the tank moves one cell to the right: the coordinate _x_ is increased by one. When you press the up or down arrow keys, the tank instantly changes the lane, that is, the _y_ coordinate. When you press the spacebar, the tank shoots, and the nearest obstacle along the lane in which the tank rides is instantly destroyed. In order to load a gun, the tank needs _t_ seconds. Initially, the gun is not loaded, that means, the first shot can be made only after _t_ seconds after the tank starts to move. If at some point the tank is in the same cell with an obstacle not yet destroyed, it burns out. If you press the arrow exactly at the moment when the tank moves forward, the tank will first move forward, and then change the lane, so it will not be possible to move diagonally. Your task is to find out whether it is possible to pass the level, and if possible, to find the order of actions the player need to make. Input The first line contains four integers _n_, _m_1, _m_2 and _t_, the length of the field, the number of obstacles in the first lane, the number of obstacles in the second lane and the number of tank steps before reloading, respectively (1u2009β€u2009_n_u2009β€u2009109; 0u2009β€u2009_m_1,u2009_m_2u2009β€u2009_n_; 0u2009β€u2009_m_1u2009+u2009_m_2u2009β€u2009106; 1u2009β€u2009_t_u2009β€u2009_n_). The next two lines contain a description of the obstacles. The first of these lines contains _m_1 numbers _x__i_ β the obstacle coordinates in the first lane (1u2009β€u2009_x__i_u2009β€u2009_n_; _x__i_u2009<u2009_x__i_u2009+u20091). The _y_ coordinate for all these obstacles will be 1. The second line contains _m_2 numbers describing the obstacles of the second lane in the same format. The _y_ coordinate of all these obstacles will be 2. Output In the first line print Β«YesΒ», if it is possible to pass the level, or Β«NoΒ», otherwise. If it is possible, then in the second line print the number of times the tank moves from one lane to another, and in the next line print the coordinates of the transitions, one number per transition: the coordinate _x_ (0u2009β€u2009_x_u2009β€u2009_n_u2009+u20091). All transition coordinates coordinates must be distinct and should be output in strictly increasing order.The number of transitions should not exceed 2Β·106. If the tank can pass the level, then it can do it using no more than 2Β·106 transitions. In the fourth line print the number of shots that the tank makes during the movement, in the following lines print two numbers, _x_ and _y_ coordinates of the point (1u2009β€u2009_x_u2009β€u2009_n_, 1u2009β€u2009_y_u2009β€u20092), from which the tank fired a shot, the number of shots must not exceed _m_1u2009+u2009_m_2. Shots must be output in the order in which they are fired. If there are several solutions, output any one. | 3,000 | false | true | false | true | false | false | false | false | false | false | 5,955 |
35B | Once upon a time, when the world was more beautiful, the sun shone brighter, the grass was greener and the sausages tasted better Arlandia was the most powerful country. And its capital was the place where our hero DravDe worked. He couldnβt program or make up problems (in fact, few people saw a computer those days) but he was nevertheless happy. He worked in a warehouse where a magical but non-alcoholic drink Ogudar-Olok was kept. We wonβt describe his work in detail and take a better look at a simplified version of the warehouse. The warehouse has one set of shelving. It has _n_ shelves, each of which is divided into _m_ sections. The shelves are numbered from top to bottom starting from 1 and the sections of each shelf are numbered from left to right also starting from 1. Each section can contain exactly one box of the drink, and try as he might, DravDe can never put a box in a section that already has one. In the course of his work DravDe frequently notices that he has to put a box in a filled section. In that case his solution is simple. DravDe ignores that section and looks at the next one to the right. If it is empty, he puts the box there. Otherwise he keeps looking for the first empty section to the right. If no empty section is found by the end of the shelf, he looks at the shelf which is under it, then the next one, etc. Also each time he looks at a new shelf he starts from the shelfβs beginning. If DravDe still canβt find an empty section for the box, he immediately drinks it all up and throws the empty bottles away not to be caught. After one great party with a lot of Ogudar-Olok drunk DravDe asked you to help him. Unlike him, you can program and therefore modeling the process of counting the boxes in the warehouse will be easy work for you. The process of counting contains two types of query messages: Β«+1 x y idΒ» (where _x_, _y_ are integers, 1u2009β€u2009_x_u2009β€u2009_n_, 1u2009β€u2009_y_u2009β€u2009_m_, and _id_ is a string of lower case Latin letters β from 1 to 10 characters long). That query means that the warehouse got a box identified as _id_, which should be put in the section _y_ on the shelf _x_. If the section is full, use the rules described above. It is guaranteed that every moment of the process the identifiers of all the boxes in the warehouse are different. You donβt have to answer this query. Β«-1 idΒ» (where _id_ is a string of lower case Latin letters β from 1 to 10 characters long). That query means that a box identified as _id_ is removed from the warehouse. You have to answer this query (see output format). Input The first input line contains integers _n_, _m_ and _k_ (1u2009β€u2009_n_,u2009_m_u2009β€u200930, 1u2009β€u2009_k_u2009β€u20092000) β the height, the width of shelving and the amount of the operations in the warehouse that you need to analyze. In the following _k_ lines the queries are given in the order of appearance in the format described above. Output For each query of the Β«-1 idΒ» type output two numbers in a separate line β index of the shelf and index of the section where the box with this identifier lay. If there was no such box in the warehouse when the query was made, output Β«-1 -1Β» without quotes. | 1,700 | false | false | true | false | false | false | false | false | false | false | 9,822 |
1720D2 | It is the hard version of the problem. The only difference is that in this version $$$a_i le 10^9$$$. You are given an array of $$$n$$$ integers $$$a_0, a_1, a_2, ldots a_{n - 1}$$$. Bryap wants to find the longest beautiful subsequence in the array. An array $$$b = xa0β the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 leq n leq 3 cdot 10^5$$$) β the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_0,a_1,...,a_{n-1}$$$ ($$$0 leq a_i leq 10^9$$$) β the elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Note In the first test case, we can pick the whole array as a beautiful subsequence because $$$1 oplus 1 < 2 oplus 0$$$. In the second test case, we can pick elements with indexes $$$1$$$, $$$2$$$ and $$$4$$$ (in $$$0$$$ indexation). For this elements holds: $$$2 oplus 2 < 4 oplus 1$$$ and $$$4 oplus 4 < 1 oplus 2$$$. | 2,400 | false | false | false | true | true | false | false | false | false | false | 1,961 |
1637E | You are given an array $$$a$$$ of length $$$n$$$. Let $$$cnt_x$$$ be the number of elements from the array which are equal to $$$x$$$. Let's also define $$$f(x, y)$$$ as $$$(cnt_x + cnt_y) cdot (x + y)$$$. Also you are given $$$m$$$ bad pairs $$$(x_i, y_i)$$$. Note that if $$$(x, y)$$$ is a bad pair, then $$$(y, x)$$$ is also bad. Your task is to find the maximum value of $$$f(u, v)$$$ over all pairs $$$(u, v)$$$, such that $$$u eq v$$$, that this pair is not bad, and also that $$$u$$$ and $$$v$$$ each occur in the array $$$a$$$. It is guaranteed that such a pair exists. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10,000$$$)xa0β the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 3 cdot 10^5$$$, $$$0 le m le 3 cdot 10^5$$$)xa0β the length of the array and the number of bad pairs. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0β elements of the array. The $$$i$$$-th of the next $$$m$$$ lines contains two integers $$$x_i$$$ and $$$y_i$$$ ($$$1 le x_i < y_i le 10^9$$$), which represent a bad pair. It is guaranteed that no bad pair occurs twice in the input. It is also guaranteed that $$$cnt_{x_i} > 0$$$ and $$$cnt_{y_i} > 0$$$. It is guaranteed that for each test case there is a pair of integers $$$(u, v)$$$, $$$u e v$$$, that is not bad, and such that both of these numbers occur in $$$a$$$. It is guaranteed that the total sum of $$$n$$$ and the total sum of $$$m$$$ don't exceed $$$3 cdot 10^5$$$. Output For each test case print a single integerxa0β the answer to the problem. Example Input 3 6 1 6 3 6 7 3 3 3 6 2 0 3 4 7 4 1 2 2 3 1 5 1 1 5 3 5 1 3 2 5 Note In the first test case $$$3$$$, $$$6$$$, $$$7$$$ occur in the array. $$$f(3, 6) = (cnt_3 + cnt_6) cdot (3 + 6) = (3 + 2) cdot (3 + 6) = 45$$$. But $$$(3, 6)$$$ is bad so we ignore it. $$$f(3, 7) = (cnt_3 + cnt_7) cdot (3 + 7) = (3 + 1) cdot (3 + 7) = 40$$$. $$$f(6, 7) = (cnt_6 + cnt_7) cdot (6 + 7) = (2 + 1) cdot (6 + 7) = 39$$$. The answer to the problem is $$$max(40, 39) = 40$$$. | 2,100 | false | false | true | false | false | false | true | true | false | false | 2,430 |
618D | A group of _n_ cities is connected by a network of roads. There is an undirected road between every pair of cities, so there are roads in total. It takes exactly _y_ seconds to traverse any single road. A spanning tree is a set of roads containing exactly _n_u2009-u20091 roads such that it's possible to travel between any two cities using only these roads. Some spanning tree of the initial network was chosen. For every road in this tree the time one needs to traverse this road was changed from _y_ to _x_ seconds. Note that it's not guaranteed that _x_ is smaller than _y_. You would like to travel through all the cities using the shortest path possible. Given _n_, _x_, _y_ and a description of the spanning tree that was chosen, find the cost of the shortest path that starts in any city, ends in any city and visits all cities exactly once. Input The first line of the input contains three integers _n_, _x_ and _y_ (2u2009β€u2009_n_u2009β€u2009200u2009000,u20091u2009β€u2009_x_,u2009_y_u2009β€u2009109). Each of the next _n_u2009-u20091 lines contains a description of a road in the spanning tree. The _i_-th of these lines contains two integers _u__i_ and _v__i_ (1u2009β€u2009_u__i_,u2009_v__i_u2009β€u2009_n_)xa0β indices of the cities connected by the _i_-th road. It is guaranteed that these roads form a spanning tree. Output Print a single integerxa0β the minimum number of seconds one needs to spend in order to visit all the cities exactly once. Note In the first sample, roads of the spanning tree have cost 2, while other roads have cost 3. One example of an optimal path is . In the second sample, we have the same spanning tree, but roads in the spanning tree cost 3, while other roads cost 2. One example of an optimal path is . | 2,200 | false | true | false | true | false | false | false | false | false | false | 7,361 |
145B | Problem - 145B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags constructive algorithms *1800 No tag edit access β Contest materials be the number of occurrences of number _x_ in number _d_ as a substring. For example, if _d_u2009=u2009747747, then _cnt_(4)u2009=u20092, _cnt_(7)u2009=u20094, _cnt_(47)u2009=u20092, _cnt_(74)u2009=u20092. Petya wants the following condition to fulfil simultaneously: _cnt_(4)u2009=u2009_a_1, _cnt_(7)u2009=u2009_a_2, _cnt_(47)u2009=u2009_a_3, _cnt_(74)u2009=u2009_a_4. Petya is not interested in the occurrences of other numbers. Help him cope with this task. Input The single line contains four integers _a_1, _a_2, _a_3 and _a_4 (1u2009β€u2009_a_1,u2009_a_2,u2009_a_3,u2009_a_4u2009β€u2009106). Output On the single line print without leading zeroes the answer to the problem β the minimum lucky number _d_ such, that _cnt_(4)u2009=u2009_a_1, _cnt_(7)u2009=u2009_a_2, _cnt_(47)u2009=u2009_a_3, _cnt_(74)u2009=u2009_a_4. If such number does not exist, print the single number "-1" (without the quotes). Examples Input 2 2 1 1 Output 4774 Input 4 7 3 1 Output -1 | 1,800 | false | false | false | false | false | true | false | false | false | false | 9,294 |
1260E | You are organizing a boxing tournament, where $$$n$$$ boxers will participate ($$$n$$$ is a power of $$$2$$$), and your friend is one of them. All boxers have different strength from $$$1$$$ to $$$n$$$, and boxer $$$i$$$ wins in the match against boxer $$$j$$$ if and only if $$$i$$$ is stronger than $$$j$$$. The tournament will be organized as follows: $$$n$$$ boxers will be divided into pairs; the loser in each pair leaves the tournament, and $$$frac{n}{2}$$$ winners advance to the next stage, where they are divided into pairs again, and the winners in all pairs advance to the next stage, and so on, until only one boxer remains (who is declared the winner). Your friend really wants to win the tournament, but he may be not the strongest boxer. To help your friend win the tournament, you may bribe his opponents: if your friend is fighting with a boxer you have bribed, your friend wins even if his strength is lower. Furthermore, during each stage you distribute the boxers into pairs as you wish. The boxer with strength $$$i$$$ can be bribed if you pay him $$$a_i$$$ dollars. What is the minimum number of dollars you have to spend to make your friend win the tournament, provided that you arrange the boxers into pairs during each stage as you wish? Input The first line contains one integer $$$n$$$ ($$$2 le n le 2^{18}$$$) β the number of boxers. $$$n$$$ is a power of $$$2$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$, where $$$a_i$$$ is the number of dollars you have to pay if you want to bribe the boxer with strength $$$i$$$. Exactly one of $$$a_i$$$ is equal to $$$-1$$$ β it means that the boxer with strength $$$i$$$ is your friend. All other values are in the range $$$[1, 10^9]$$$. Output Print one integer β the minimum number of dollars you have to pay so your friend wins. Examples Input 8 11 -1 13 19 24 7 17 5 Note In the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament. In the second test case you can distribute boxers as follows (your friend is number $$$2$$$): $$$1 : 2, 8 : 5, 7 : 3, 6 : 4$$$ (boxers $$$2, 8, 7$$$ and $$$6$$$ advance to the next stage); $$$2 : 6, 8 : 7$$$ (boxers $$$2$$$ and $$$8$$$ advance to the next stage, you have to bribe the boxer with strength $$$6$$$); $$$2 : 8$$$ (you have to bribe the boxer with strength $$$8$$$); | 2,400 | false | true | false | true | false | false | true | false | false | false | 4,390 |
666D | Group of Berland scientists, with whom you have a close business relationship, makes a research in the area of peaceful nuclear energy. In particular, they found that a group of four nanobots, placed on a surface of a plate, can run a powerful chain reaction under certain conditions. To be precise, researchers introduced a rectangular Cartesian coordinate system on a flat plate and selected four distinct points with integer coordinates where bots will be placed initially. Next each bot will be assigned with one of the four directions (up, down, left or right) parallel to the coordinate axes. After that, each bot is shifted by an integer distance (which may be different for different bots) along its direction. The chain reaction starts, if the bots are in the corners of a square with positive area with sides parallel to the coordinate axes. Each corner of the square must contain one nanobot. This reaction will be stronger, if bots spend less time to move. We can assume that bots move with unit speed. In other words, the lesser is the maximum length traveled by bot, the stronger is reaction. Scientists have prepared a set of plates and selected starting position for the bots for each plate. Now they ask you to assign the direction for each bot to move after landing such that the maximum length traveled by bot is as small as possible. Input The first line contains an integer number _t_ (1u2009β€u2009_t_u2009β€u200950) β the number of plates. _t_ descriptions of plates follow. A description of each plate consists of four lines. Each line consists of a pair of integers numbers _x__i_,u2009_y__i_ (u2009-u2009108u2009β€u2009_x__i_,u2009_y__i_u2009β€u2009108) β coordinates of the next bot. All bots are in different locations. Note, though, the problem can include several records in one test, you can hack other people's submissions only with the test of one plate, i.e. parameter _t_ in a hack test should be equal to 1. Output Print answers for all plates separately. First goes a single integer number in a separate line. If scientists have made an unfortunate mistake and nanobots are not able to form the desired square, print -1. Otherwise, print the minimum possible length of the longest bot's path. If a solution exists, in the next four lines print two integer numbers β positions of each bot after moving. Print bots' positions in the order they are specified in the input data. If there are multiple solution, you can print any of them. Examples Input 2 1 1 1 -1 -1 1 -1 -1 1 1 2 2 4 4 6 6 Output 0 1 1 1 -1 -1 1 -1 -1 -1 | 3,000 | false | false | false | false | false | false | true | false | false | false | 7,152 |
1010D | Natasha travels around Mars in the Mars rover. But suddenly it broke down, namelyxa0β the logical scheme inside it. The scheme is an undirected tree (connected acyclic graph) with a root in the vertex $$$1$$$, in which every leaf (excluding root) is an input, and all other vertices are logical elements, including the root, which is output. One bit is fed to each input. One bit is returned at the output. There are four types of logical elements: , , , . Logical elements take values from their direct descendants (inputs) and return the result of the function they perform. Natasha knows the logical scheme of the Mars rover, as well as the fact that only one input is broken. In order to fix the Mars rover, she needs to change the value on this input. For each input, determine what the output will be if Natasha changes this input. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 10^6$$$)xa0β the number of vertices in the graph (both inputs and elements). The $$$i$$$-th of the next $$$n$$$ lines contains a description of $$$i$$$-th vertex: the first word "AND", "OR", "XOR", "NOT" or "IN" (means the input of the scheme) is the vertex type. If this vertex is "IN", then the value of this input follows ($$$0$$$ or $$$1$$$), otherwise follow the indices of input vertices of this element: "AND", "OR", "XOR" have $$$2$$$ inputs, whereas "NOT" has $$$1$$$ input. The vertices are numbered from one. It is guaranteed that input data contains a correct logical scheme with an output produced by the vertex $$$1$$$. Output Print a string of characters '0' and '1' (without quotes)xa0β answers to the problem for each input in the ascending order of their vertex indices. Example Input 10 AND 9 4 IN 1 IN 1 XOR 6 5 AND 3 7 IN 0 NOT 10 IN 1 IN 1 AND 2 8 Note The original scheme from the example (before the input is changed): Green indicates bits '1', yellow indicates bits '0'. If Natasha changes the input bit $$$2$$$ to $$$0$$$, then the output will be $$$1$$$. If Natasha changes the input bit $$$3$$$ to $$$0$$$, then the output will be $$$0$$$. If Natasha changes the input bit $$$6$$$ to $$$1$$$, then the output will be $$$1$$$. If Natasha changes the input bit $$$8$$$ to $$$0$$$, then the output will be $$$1$$$. If Natasha changes the input bit $$$9$$$ to $$$0$$$, then the output will be $$$0$$$. | 2,000 | false | false | true | false | false | false | false | false | false | true | 5,632 |
1407B | Alexander is a well-known programmer. Today he decided to finally go out and play football, but with the first hit he left a dent on the new Rolls-Royce of the wealthy businessman Big Vova. Vladimir has recently opened a store on the popular online marketplace "Zmey-Gorynych", and offers Alex a job: if he shows his programming skills by solving a task, he'll work as a cybersecurity specialist. Otherwise, he'll be delivering some doubtful products for the next two years. You're given $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$. Using each of them exactly at once, you're to make such sequence $$$b_1, b_2, dots, b_n$$$ that sequence $$$c_1, c_2, dots, c_n$$$ is lexicographically maximal, where $$$c_i=GCD(b_1,dots,b_i)$$$ - the greatest common divisor of the first $$$i$$$ elements of $$$b$$$. Alexander is really afraid of the conditions of this simple task, so he asks you to solve it. A sequence $$$a$$$ is lexicographically smaller than a sequence $$$b$$$ if and only if one of the following holds: $$$a$$$ is a prefix of $$$b$$$, but $$$a e b$$$; in the first position where $$$a$$$ and $$$b$$$ differ, the sequence $$$a$$$ has a smaller element than the corresponding element in $$$b$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^3$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 10^3$$$) xa0β the length of the sequence $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1,dots,a_n$$$ ($$$1 le a_i le 10^3$$$) xa0β the sequence $$$a$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$. Output For each test case output the answer in a single line xa0β the desired sequence $$$b$$$. If there are multiple answers, print any. Example Input 7 2 2 5 4 1 8 2 3 3 3 8 9 5 64 25 75 100 50 1 42 6 96 128 88 80 52 7 5 2 4 8 16 17 Output 5 2 8 2 1 3 9 3 8 100 50 25 75 64 42 128 96 80 88 52 7 17 2 4 8 16 Note In the first test case of the example, there are only two possible permutations $$$b$$$ xa0β $$$[2, 5]$$$ and $$$[5, 2]$$$: for the first one $$$c=[2, 1]$$$, for the second one $$$c=[5, 1]$$$. In the third test case of the example, number $$$9$$$ should be the first in $$$b$$$, and $$$GCD(9, 3)=3$$$, $$$GCD(9, 8)=1$$$, so the second number of $$$b$$$ should be $$$3$$$. In the seventh test case of the example, first four numbers pairwise have a common divisor (a power of two), but none of them can be the first in the optimal permutation $$$b$$$. | 1,300 | true | true | false | false | false | false | true | false | false | false | 3,640 |
1981A | Turtle and Piggy are playing a number game. First, Turtle will choose an integer $$$x$$$, such that $$$l le x le r$$$, where $$$l, r$$$ are given. It's also guaranteed that $$$2l le r$$$. Then, Piggy will keep doing the following operation until $$$x$$$ becomes $$$1$$$: Choose an integer $$$p$$$ such that $$$p ge 2$$$ and $$$p mid x$$$ (i.e. $$$x$$$ is a multiple of $$$p$$$). Set $$$x$$$ to $$$frac{x}{p}$$$, and the score will increase by $$$1$$$. The score is initially $$$0$$$. Both Turtle and Piggy want to maximize the score. Please help them to calculate the maximum score. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$l, r$$$ ($$$1 le l le r le 10^9, 2l le r$$$) β The range where Turtle can choose the integer from. Output For each test case, output a single integer β the maximum score. Example Input 5 2 4 3 6 2 15 6 22 114514 1919810 Note In the first test case, Turtle can choose an integer $$$x$$$, such that $$$2 le x le 4$$$. He can choose $$$x = 4$$$. Then Piggy can choose $$$p = 2$$$ for $$$2$$$ times. After that, $$$x$$$ will become $$$1$$$, and the score will be $$$2$$$, which is maximized. In the second test case, Turtle can choose an integer $$$3 le x le 6$$$. He can choose $$$x = 6$$$. Then Piggy can choose $$$p = 2$$$, then choose $$$p = 3$$$. After that, $$$x$$$ will become $$$1$$$, and the score will be $$$2$$$, which is maximum. In the third test case, Turtle can choose $$$x = 12$$$. In the fourth test case, Turtle can choose $$$x = 16$$$. | 800 | true | true | false | false | false | false | true | false | false | false | 402 |
1763E | Let's call an ordered pair of nodes $$$(u, v)$$$ in a directed graph unidirectional if $$$u eq v$$$, there exists a path from $$$u$$$ to $$$v$$$, and there are no paths from $$$v$$$ to $$$u$$$. A directed graph is called $$$p$$$-reachable if it contains exactly $$$p$$$ ordered pairs of nodes $$$(u, v)$$$ such that $$$u < v$$$ and $$$u$$$ and $$$v$$$ are reachable from each other. Find the minimum number of nodes required to create a $$$p$$$-reachable directed graph. Also, among all such $$$p$$$-reachable directed graphs with the minimum number of nodes, let $$$G$$$ denote a graph which maximizes the number of unidirectional pairs of nodes. Find this number. Input The first and only line contains a single integer $$$p$$$ ($$$0 le p le 2 cdot 10^5$$$)xa0β the number of ordered pairs of nodes. Output Print a single line containing two integersxa0β the minimum number of nodes required to create a $$$p$$$-reachable directed graph, and the maximum number of unidirectional pairs of nodes among all such $$$p$$$-reachable directed graphs with the minimum number of nodes. Note In the first test case, the minimum number of nodes required to create a $$$3$$$-reachable directed graph is $$$3$$$. Among all $$$3$$$-reachable directed graphs with $$$3$$$ nodes, the following graph $$$G$$$ is one of the graphs with the maximum number of unidirectional pairs of nodes, which is $$$0$$$. | 2,200 | true | false | false | true | false | false | false | false | false | true | 1,729 |
1372A | # Omkar and Completion Input file: standard input Output file: standard output Time limit: 1 second Memory limit: 256 megabytes You have been blessed as a child of Omkar. To express your gratitude, please solve this problem for Omkar! An array a of length n is called complete if all elements are positive and donβt exceed 1000 , and for all indices x,y,z (1 β€ x, y, z β€ n), ax + ay 6 = az (not necessarily distinct). You are given one integer n. Please find any complete array of length n. It is guaranteed that under given constraints such array exists. # Input Each test contains multiple test cases. The first line contains t (1 β€ t β€ 1000 ) x16 the number of test cases. Description of the test cases follows. The only line of each test case contains one integer n (1 β€ n β€ 1000 ). It is guaranteed that the sum of n over all test cases does not exceed 1000 . # Output For each test case, print a complete array on a single line. All elements have to be integers between 1 and 1000 and for all indices x,y,z (1 β€ x, y, z β€ n) (not necessarily distinct), ax + ay 6 = az must hold. If multiple solutions exist, you may print any. # Example standard input standard output 2541 5 3 77 12 384 384 44 44 # Note It can be shown that the outputs above are valid for each test case. For example, 44 + 44 6 = 384 .Below are some examples of arrays that are NOT complete for the 1st test case: [1 , 2, 3, 4, 5] Notice that a1 + a2 = a3. [1 , 3000 , 1, 300 , 1] Notice that a2 = 3000 > 1000 . Page 1 of 1 | 800 | false | false | true | false | false | true | false | false | false | false | 3,807 |
799F | Butler Ostin wants to show Arkady that rows of odd number of fountains are beautiful, while rows of even number of fountains are not. The butler wants to show Arkady _n_ gardens. Each garden is a row of _m_ cells, the _i_-th garden has one fountain in each of the cells between _l__i_ and _r__i_ inclusive, and there are no more fountains in that garden. The issue is that some of the gardens contain even number of fountains, it is wrong to show them to Arkady. Ostin wants to choose two integers _a_u2009β€u2009_b_ and show only part of each of the gardens that starts at cell _a_ and ends at cell _b_. Of course, only such segments suit Ostin that each garden has either zero or odd number of fountains on this segment. Also, it is necessary that at least one garden has at least one fountain on the segment from _a_ to _b_. Help Ostin to find the total length of all such segments, i.e. sum up the value (_b_u2009-u2009_a_u2009+u20091) for each suitable pair (_a_,u2009_b_). Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_,u2009_m_u2009β€u20092Β·105)xa0β the number of gardens and the length of each garden. _n_ lines follow. The _i_-th of these lines contains two integers _l__i_ and _r__i_ (1u2009β€u2009_l__i_u2009β€u2009_r__i_u2009β€u2009_m_)xa0β the bounds of the segment that contains fountains in the _i_-th garden. Note In the first example the following pairs suit Ostin: (_a_,u2009_b_): (1,u20092), (1,u20094), (1,u20095), (2,u20092), (2,u20094), (2,u20095), (3,u20093), (4,u20094), (4,u20095). In the second example the following pairs suit Ostin: (_a_,u2009_b_): (1,u20092), (1,u20095), (2,u20092), (2,u20095), (3,u20093), (4,u20094), (4,u20096), (5,u20095), (6,u20096). | 3,500 | false | false | false | false | true | false | false | false | false | false | 6,571 |
1552D | You are given a sequence of $$$n$$$ integers $$$a_1, , a_2, , dots, , a_n$$$. Does there exist a sequence of $$$n$$$ integers $$$b_1, , b_2, , dots, , b_n$$$ such that the following property holds? For each $$$1 le i le n$$$, there exist two (not necessarily distinct) indices $$$j$$$ and $$$k$$$ ($$$1 le j, , k le n$$$) such that $$$a_i = b_j - b_k$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 20$$$) β the number of test cases. Then $$$t$$$ test cases follow. The first line of each test case contains one integer $$$n$$$ ($$$1 le n le 10$$$). The second line of each test case contains the $$$n$$$ integers $$$a_1, , dots, , a_n$$$ ($$$-10^5 le a_i le 10^5$$$). Output For each test case, output a line containing YES if a sequence $$$b_1, , dots, , b_n$$$ satisfying the required property exists, and NO otherwise. Example Input 5 5 4 -7 -1 5 10 1 0 3 1 10 100 4 -3 2 10 2 9 25 -171 250 174 152 242 100 -205 -258 Note In the first test case, the sequence $$$b = [-9, , 2, , 1, , 3, , -2]$$$ satisfies the property. Indeed, the following holds: $$$a_1 = 4 = 2 - (-2) = b_2 - b_5$$$; $$$a_2 = -7 = -9 - (-2) = b_1 - b_5$$$; $$$a_3 = -1 = 1 - 2 = b_3 - b_2$$$; $$$a_4 = 5 = 3 - (-2) = b_4 - b_5$$$; $$$a_5 = 10 = 1 - (-9) = b_3 - b_1$$$. In the second test case, it is sufficient to choose $$$b = [0]$$$, since $$$a_1 = 0 = 0 - 0 = b_1 - b_1$$$. In the third test case, it is possible to show that no sequence $$$b$$$ of length $$$3$$$ satisfies the property. | 1,800 | true | false | false | true | false | true | true | false | false | true | 2,881 |
846E | Igor is a post-graduate student of chemistry faculty in Berland State University (BerSU). He needs to conduct a complicated experiment to write his thesis, but laboratory of BerSU doesn't contain all the materials required for this experiment. Fortunately, chemical laws allow material transformations (yes, chemistry in Berland differs from ours). But the rules of transformation are a bit strange. Berland chemists are aware of _n_ materials, numbered in the order they were discovered. Each material can be transformed into some other material (or vice versa). Formally, for each _i_ (2u2009β€u2009_i_u2009β€u2009_n_) there exist two numbers _x__i_ and _k__i_ that denote a possible transformation: _k__i_ kilograms of material _x__i_ can be transformed into 1 kilogram of material _i_, and 1 kilogram of material _i_ can be transformed into 1 kilogram of material _x__i_. Chemical processing equipment in BerSU allows only such transformation that the amount of resulting material is always an integer number of kilograms. For each _i_ (1u2009β€u2009_i_u2009β€u2009_n_) Igor knows that the experiment requires _a__i_ kilograms of material _i_, and the laboratory contains _b__i_ kilograms of this material. Is it possible to conduct an experiment after transforming some materials (or none)? Input The first line contains one integer number _n_ (1u2009β€u2009_n_u2009β€u2009105) β the number of materials discovered by Berland chemists. The second line contains _n_ integer numbers _b_1,u2009_b_2... _b__n_ (1u2009β€u2009_b__i_u2009β€u20091012) β supplies of BerSU laboratory. The third line contains _n_ integer numbers _a_1,u2009_a_2... _a__n_ (1u2009β€u2009_a__i_u2009β€u20091012) β the amounts required for the experiment. Then _n_u2009-u20091 lines follow. _j_-th of them contains two numbers _x__j_u2009+u20091 and _k__j_u2009+u20091 that denote transformation of (_j_u2009+u20091)-th material (1u2009β€u2009_x__j_u2009+u20091u2009β€u2009_j_,u20091u2009β€u2009_k__j_u2009+u20091u2009β€u2009109). Output Print YES if it is possible to conduct an experiment. Otherwise print NO. | 2,300 | false | true | false | false | false | false | false | false | false | false | 6,373 |
43B | Problem - 43B - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation strings *1100 No tag edit access β Contest materials ") ") | 1,100 | false | false | true | false | false | false | false | false | false | false | 9,773 |
145A | Problem - 145A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags greedy implementation *1200 No tag edit access β Contest materials ; swap any pair of digits in string _a_. Petya is interested in the minimum number of operations that are needed to make string _a_ equal to string _b_. Help him with the task. Input The first and the second line contains strings _a_ and _b_, correspondingly. Strings _a_ and _b_ have equal lengths and contain only lucky digits. The strings are not empty, their length does not exceed 105. Output Print on the single line the single number β the minimum number of operations needed to convert string _a_ into string _b_. Examples Input 47 74 Output 1 Input 774 744 Output 1 Input 777 444 Output 3 Note In the first sample it is enough simply to swap the first and the second digit. In the second sample we should replace the second digit with its opposite. In the third number we should replace all three digits with their opposites. | 1,200 | false | true | true | false | false | false | false | false | false | false | 9,295 |
1458C | You are given a square matrix of size $$$n$$$. Every row and every column of this matrix is a permutation of $$$1$$$, $$$2$$$, $$$ldots$$$, $$$n$$$. Let $$$a_{i, j}$$$ be the element at the intersection of $$$i$$$-th row and $$$j$$$-th column for every $$$1 leq i, j leq n$$$. Rows are numbered $$$1, ldots, n$$$ top to bottom, and columns are numbered $$$1, ldots, n$$$ left to right. There are six types of operations: R: cyclically shift all columns to the right, formally, set the value of each $$$a_{i, j}$$$ to $$$a_{i, ((j - 2)bmod n) + 1}$$$; L: cyclically shift all columns to the left, formally, set the value of each $$$a_{i, j}$$$ to $$$a_{i, (jbmod n) + 1}$$$; D: cyclically shift all rows down, formally, set the value of each $$$a_{i, j}$$$ to $$$a_{((i - 2)bmod n) + 1, j}$$$; U: cyclically shift all rows up, formally, set the value of each $$$a_{i, j}$$$ to $$$a_{(ibmod n) + 1, j}$$$; I: replace the permutation read left to right in each row with its inverse. C: replace the permutation read top to bottom in each column with its inverse. Inverse of a permutation $$$p_1$$$, $$$p_2$$$, $$$ldots$$$, $$$p_n$$$ is a permutation $$$q_1$$$, $$$q_2$$$, $$$ldots$$$, $$$q_n$$$, such that $$$p_{q_i} = i$$$ for every $$$1 leq i leq n$$$. One can see that after any sequence of operations every row and every column of the matrix will still be a permutation of $$$1, 2, ldots, n$$$. Given the initial matrix description, you should process $$$m$$$ operations and output the final matrix. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$) β number of test cases. $$$t$$$ test case descriptions follow. The first line of each test case description contains two integers $$$n$$$ and $$$m$$$ ($$$1 leq n leq 1000, 1 leq m leq 10^5$$$) β size of the matrix and number of operations. Each of the next $$$n$$$ lines contains $$$n$$$ integers separated by single spaces β description of the matrix $$$a$$$ ($$$1 leq a_{i, j} leq n$$$). The last line of the description contains a string of $$$m$$$ characters describing the operations in order, according to the format above. The sum of $$$n$$$ does not exceed $$$1000$$$, and the sum of $$$m$$$ does not exceed $$$10^5$$$. | 2,700 | true | false | false | false | false | false | false | false | false | false | 3,392 |
8E | One Martian boy called Zorg wants to present a string of beads to his friend from the Earth β Masha. He knows that Masha likes two colours: blue and red, β and right in the shop where he has come, there is a variety of adornments with beads of these two colours. All the strings of beads have a small fastener, and if one unfastens it, one might notice that all the strings of beads in the shop are of the same length. Because of the peculiarities of the Martian eyesight, if Zorg sees one blue-and-red string of beads first, and then the other with red beads instead of blue ones, and blue β instead of red, he regards these two strings of beads as identical. In other words, Zorg regards as identical not only those strings of beads that can be derived from each other by the string turnover, but as well those that can be derived from each other by a mutual replacement of colours and/or by the string turnover. It is known that all Martians are very orderly, and if a Martian sees some amount of objects, he tries to put them in good order. Zorg thinks that a red bead is smaller than a blue one. Let's put 0 for a red bead, and 1 β for a blue one. From two strings the Martian puts earlier the string with a red bead in the _i_-th position, providing that the second string has a blue bead in the _i_-th position, and the first two beads _i_u2009-u20091 are identical. At first Zorg unfastens all the strings of beads, and puts them into small heaps so, that in each heap strings are identical, in his opinion. Then he sorts out the heaps and chooses the minimum string in each heap, in his opinion. He gives the unnecassary strings back to the shop assistant and says he doesn't need them any more. Then Zorg sorts out the remaining strings of beads and buys the string with index _k_. All these manupulations will take Zorg a lot of time, that's why he asks you to help and find the string of beads for Masha. Input The input file contains two integers _n_ and _k_ (2u2009β€u2009_n_u2009β€u200950;1u2009β€u2009_k_u2009β€u20091016) βthe length of a string of beads, and the index of the string, chosen by Zorg. Output Output the _k_-th string of beads, putting 0 for a red bead, and 1 β for a blue one. If it s impossible to find the required string, output the only number -1. Note Let's consider the example of strings of length 4 β 0001, 0010, 0011, 0100, 0101, 0110, 0111, 1000, 1001, 1010, 1011, 1100, 1101, 1110. Zorg will divide them into heaps: {0001, 0111, 1000, 1110}, {0010, 0100, 1011, 1101}, {0011, 1100}, {0101, 1010}, {0110, 1001}. Then he will choose the minimum strings of beads in each heap: 0001, 0010, 0011, 0101, 0110. The forth string β 0101. | 2,600 | false | false | false | true | false | false | false | false | false | true | 9,951 |
1210C | Kamil likes streaming the competitive programming videos. His MeTube channel has recently reached $$$100$$$ million subscribers. In order to celebrate this, he posted a video with an interesting problem he couldn't solve yet. Can you help him? You're given a tree β a connected undirected graph consisting of $$$n$$$ vertices connected by $$$n - 1$$$ edges. The tree is rooted at vertex $$$1$$$. A vertex $$$u$$$ is called an ancestor of $$$v$$$ if it lies on the shortest path between the root and $$$v$$$. In particular, a vertex is an ancestor of itself. Each vertex $$$v$$$ is assigned its beauty $$$x_v$$$ β a non-negative integer not larger than $$$10^{12}$$$. This allows us to define the beauty of a path. Let $$$u$$$ be an ancestor of $$$v$$$. Then we define the beauty $$$f(u, v)$$$ as the greatest common divisor of the beauties of all vertices on the shortest path between $$$u$$$ and $$$v$$$. Formally, if $$$u=t_1, t_2, t_3, dots, t_k=v$$$ are the vertices on the shortest path between $$$u$$$ and $$$v$$$, then $$$f(u, v) = gcd(x_{t_1}, x_{t_2}, dots, x_{t_k})$$$. Here, $$$gcd$$$ denotes the greatest common divisor of a set of numbers. In particular, $$$f(u, u) = gcd(x_u) = x_u$$$. Your task is to find the sum $$$$$$ sum_{u ext{ is an ancestor of }v} f(u, v). $$$$$$ As the result might be too large, please output it modulo $$$10^9 + 7$$$. Note that for each $$$y$$$, $$$gcd(0, y) = gcd(y, 0) = y$$$. In particular, $$$gcd(0, 0) = 0$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 100,000$$$) β the number of vertices in the tree. The following line contains $$$n$$$ integers $$$x_1, x_2, dots, x_n$$$ ($$$0 le x_i le 10^{12}$$$). The value $$$x_v$$$ denotes the beauty of vertex $$$v$$$. The following $$$n - 1$$$ lines describe the edges of the tree. Each of them contains two integers $$$a, b$$$ ($$$1 le a, b le n$$$, $$$a eq b$$$) β the vertices connected by a single edge. | 2,000 | true | false | false | false | false | false | false | false | false | false | 4,634 |
1884C | The array $$$a_1, a_2, ldots, a_m$$$ is initially filled with zeroes. You are given $$$n$$$ pairwise distinct segments $$$1 le l_i le r_i le m$$$. You have to select an arbitrary subset of these segments (in particular, you may select an empty set). Next, you do the following: For each $$$i = 1, 2, ldots, n$$$, if the segment $$$(l_i, r_i)$$$ has been selected to the subset, then for each index $$$l_i le j le r_i$$$ you increase $$$a_j$$$ by $$$1$$$ (i.xa0e. $$$a_j$$$ is replaced by $$$a_j + 1$$$). If the segment $$$(l_i, r_i)$$$ has not been selected, the array does not change. Next (after processing all values of $$$i = 1, 2, ldots, n$$$), you compute $$$max(a)$$$ as the maximum value among all elements of $$$a$$$. Analogously, compute $$$min(a)$$$ as the minimum value. Finally, the cost of the selected subset of segments is declared as $$$max(a) - min(a)$$$. Please, find the maximum cost among all subsets of segments. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 10^5$$$, $$$1 le m le 10^9$$$)xa0β the number of segments and the length of the array. The following $$$n$$$ lines of each test case describe the segments. The $$$i$$$-th of these lines contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le m$$$). It is guaranteed that the segments are pairwise distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output the maximum cost among all subsets of the given set of segments. Example Input 6 1 3 2 2 3 8 2 4 3 5 4 6 6 3 1 1 1 2 1 3 2 2 2 3 3 3 7 6 2 2 1 6 1 2 5 6 1 5 4 4 3 6 6 27 6 26 5 17 2 3 20 21 1 22 12 24 4 1000000000 2 999999999 3 1000000000 123456789 987654321 9274 123456789 Note In the first test case, there is only one segment available. If we do not select it, then the array will be $$$a = [0, 0, 0]$$$, and the cost of such (empty) subset of segments will be $$$0$$$. If, however, we select the only segment, the array will be $$$a = [0, 1, 0]$$$, and the cost will be $$$1 - 0 = 1$$$. In the second test case, we can select all the segments: the array will be $$$a = [0, 1, 2, 3, 2, 1, 0, 0]$$$ in this case. The cost will be $$$3 - 0 = 3$$$. | 1,700 | false | true | false | true | true | false | true | false | true | false | 981 |
962E | The cities of Byteland and Berland are located on the axis $$$Ox$$$. In addition, on this axis there are also disputed cities, which belong to each of the countries in their opinion. Thus, on the line $$$Ox$$$ there are three types of cities: the cities of Byteland, the cities of Berland, disputed cities. Recently, the project BNET has been launched β a computer network of a new generation. Now the task of the both countries is to connect the cities so that the network of this country is connected. The countries agreed to connect the pairs of cities with BNET cables in such a way that: If you look at the only cities of Byteland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables, If you look at the only cities of Berland and the disputed cities, then in the resulting set of cities, any city should be reachable from any other one by one or more cables. Thus, it is necessary to choose a set of pairs of cities to connect by cables in such a way that both conditions are satisfied simultaneously. Cables allow bi-directional data transfer. Each cable connects exactly two distinct cities. The cost of laying a cable from one city to another is equal to the distance between them. Find the minimum total cost of laying a set of cables so that two subsets of cities (Byteland and disputed cities, Berland and disputed cities) are connected. Each city is a point on the line $$$Ox$$$. It is technically possible to connect the cities $$$a$$$ and $$$b$$$ with a cable so that the city $$$c$$$ ($$$a < c < b$$$) is not connected to this cable, where $$$a$$$, $$$b$$$ and $$$c$$$ are simultaneously coordinates of the cities $$$a$$$, $$$b$$$ and $$$c$$$. Input The first line contains a single integer $$$n$$$ ($$$2 le n le 2 cdot 10^{5}$$$) β the number of cities. The following $$$n$$$ lines contains an integer $$$x_i$$$ and the letter $$$c_i$$$ ($$$-10^{9} le x_i le 10^{9}$$$) β the coordinate of the city and its type. If the city belongs to Byteland, $$$c_i$$$ equals to 'B'. If the city belongs to Berland, $$$c_i$$$ equals to Β«RΒ». If the city is disputed, $$$c_i$$$ equals to 'P'. All cities have distinct coordinates. Guaranteed, that the cities are given in the increasing order of their coordinates. Output Print the minimal total length of such set of cables, that if we delete all Berland cities ($$$c_i$$$='R'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Similarly, if we delete all Byteland cities ($$$c_i$$$='B'), it will be possible to find a way from any remaining city to any other remaining city, moving only by cables. Examples Input 5 10 R 14 B 16 B 21 R 32 R Note In the first example, you should connect the first city with the second, the second with the third, and the third with the fourth. The total length of the cables will be $$$5 + 3 + 4 = 12$$$. In the second example there are no disputed cities, so you need to connect all the neighboring cities of Byteland and all the neighboring cities of Berland. The cities of Berland have coordinates $$$10, 21, 32$$$, so to connect them you need two cables of length $$$11$$$ and $$$11$$$. The cities of Byteland have coordinates $$$14$$$ and $$$16$$$, so to connect them you need one cable of length $$$2$$$. Thus, the total length of all cables is $$$11 + 11 + 2 = 24$$$. | 2,200 | false | true | false | false | false | true | false | false | false | false | 5,852 |
403E | You have two rooted undirected trees, each contains _n_ vertices. Let's number the vertices of each tree with integers from 1 to _n_. The root of each tree is at vertex 1. The edges of the first tree are painted blue, the edges of the second one are painted red. For simplicity, let's say that the first tree is blue and the second tree is red. Edge {_x_,u2009_y_} is called bad for edge {_p_,u2009_q_} if two conditions are fulfilled: 1. The color of edge {_x_,u2009_y_} is different from the color of edge {_p_,u2009_q_}. 2. Let's consider the tree of the same color that edge {_p_,u2009_q_} is. Exactly one of vertices _x_, _y_ lies both in the subtree of vertex _p_ and in the subtree of vertex _q_. In this problem, your task is to simulate the process described below. The process consists of several stages: 1. On each stage edges of exactly one color are deleted. 2. On the first stage, exactly one blue edge is deleted. 3. Let's assume that at the stage _i_ we've deleted edges {_u_1,u2009_v_1}, {_u_2,u2009_v_2}, ..., {_u__k_,u2009_v__k_}. At the stage _i_u2009+u20091 we will delete all undeleted bad edges for edge {_u_1,u2009_v_1}, then we will delete all undeleted bad edges for edge {_u_2,u2009_v_2} and so on until we reach edge {_u__k_,u2009_v__k_}. For each stage of deleting edges determine what edges will be removed on the stage. Note that the definition of a bad edge always considers the initial tree before it had any edges removed. Input The first line contains integer _n_ (2u2009β€u2009_n_u2009β€u20092Β·105) β the number of vertices in each tree. The next line contains _n_u2009-u20091 positive integers _a_2,u2009_a_3,u2009...,u2009_a__n_ (1u2009β€u2009_a__i_u2009β€u2009_n_;xa0_a__i_u2009β u2009_i_) β the description of edges of the first tree. Number _a__i_ means that the first tree has an edge connecting vertex _a__i_ and vertex _i_. The next line contains _n_u2009-u20091 positive integers _b_2,u2009_b_3,u2009...,u2009_b__n_ (1u2009β€u2009_b__i_u2009β€u2009_n_;xa0_b__i_u2009β u2009_i_) β the description of the edges of the second tree. Number _b__i_ means that the second tree has an edge connecting vertex _b__i_ and vertex _i_. The next line contains integer _idx_ (1u2009β€u2009_idx_u2009<u2009_n_) β the index of the blue edge that was removed on the first stage. Assume that the edges of each tree are numbered with numbers from 1 to _n_u2009-u20091 in the order in which they are given in the input. Output For each stage of removing edges print its description. Each description must consist of exactly two lines. If this is the stage when blue edges are deleted, then the first line of the description must contain word _Blue_, otherwise β word _Red_. In the second line print the indexes of the edges that will be deleted on this stage in the increasing order. Examples Output Blue 3 Red 1 3 Blue 1 2 Red 2 Note For simplicity let's assume that all edges of the root tree received some direction, so that all vertices are reachable from vertex 1. Then a subtree of vertex _v_ is a set of vertices reachable from vertex _v_ in the resulting directed graph (vertex _v_ is also included in the set). | 2,900 | false | false | true | false | true | false | false | false | false | false | 8,211 |
1718D | We call an array $$$a$$$ pure if all elements in it are pairwise distinct. For example, an array $$$[1, 7, 9]$$$ is pure, $$$[1, 3, 3, 7]$$$ isn't, because $$$3$$$ occurs twice in it. A pure array $$$b$$$ is similar to a pure array $$$c$$$ if their lengths $$$n$$$ are the same and for all pairs of indices $$$l$$$, $$$r$$$, such that $$$1 le l le r le n$$$, it's true that $$$$$$operatorname{argmax}([b_l, b_{l + 1}, ldots, b_r]) = operatorname{argmax}([c_l, c_{l + 1}, ldots, c_r]),$$$$$$ where $$$operatorname{argmax}(x)$$$ is defined as the index of the largest element in $$$x$$$ (which is unique for pure arrays). For example, $$$operatorname{argmax}([3, 4, 2]) = 2$$$, $$$operatorname{argmax}([1337, 179, 57]) = 1$$$. Recently, Tonya found out that Burenka really likes a permutation $$$p$$$ of length $$$n$$$. Tonya decided to please her and give her an array $$$a$$$ similar to $$$p$$$. He already fixed some elements of $$$a$$$, but exactly $$$k$$$ elements are missing (in these positions temporarily $$$a_i = 0$$$). It is guaranteed that $$$k ge 2$$$. Also, he has a set $$$S$$$ of $$$k - 1$$$ numbers. Tonya realized that he was missing one number to fill the empty places of $$$a$$$, so he decided to buy it. He has $$$q$$$ options to buy. Tonya thinks that the number $$$d$$$ suits him, if it is possible to replace all zeros in $$$a$$$ with numbers from $$$S$$$ and the number $$$d$$$, so that $$$a$$$ becomes a pure array similar to $$$p$$$. For each option of $$$d$$$, output whether this number is suitable for him or not. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) is the number of test cases. The description of the test cases follows. The first line of each test case contains a couple of integers $$$n$$$ and $$$q$$$ ($$$1 le n, q le 3 cdot 10^5$$$). The second line of each input test case contains $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 le p_i le n$$$)xa0β the permutation Burenka likes. The third line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 10^6$$$)xa0β elements of Tonya's array, where $$$0$$$ denotes a missing element. It is guaranteed that there are two indexes $$$i, j$$$ $$$(1 le i, j le n, i e j)$$$ such that $$$a_i = 0, a_j = 0$$$, which implies that $$$k geq 2$$$. The fourth line of each test case contains $$$k - 1$$$ distinct integers $$$s_1, s_2, ldots, s_{k-1}$$$ ($$$1 le s_i le 10^6$$$)xa0β elements of Tonya's set $$$S$$$. Each of the next $$$q$$$ lines contains a single integer $$$d$$$ ($$$1 le d le 10^6$$$)xa0β the number that Tonya plans to buy. It is guaranteed that for each given $$$d$$$ it's possible to fill in the gaps in $$$a$$$ with numbers from $$$S$$$ and the number $$$d$$$ to get a pure array. It is guaranteed that the sum of $$$n$$$ and the sum of $$$q$$$ in all tests does not exceed $$$3 cdot 10^5$$$. Output Output $$$q$$$ lines. For each value $$$d$$$, print "YES" if there is a way to fill the array $$$a$$$ to make it similar to $$$p$$$, and "NO" otherwise. Example Input 4 4 3 1 4 3 2 5 0 7 0 6 9 1 4 5 3 1 2 5 4 3 0 5 10 0 0 3 9 1 8 11 5 2 1 4 3 2 5 0 0 0 0 0 7 9 1 5 6 100 4 2 4 1 3 2 0 5 3 0 2 4 6 Output YES NO NO YES YES NO YES YES NO NO Note In the first test case for $$$d = 9$$$, you can get $$$a = [5, 9, 7, 6]$$$, it can be proved that $$$a$$$ is similar to $$$p$$$, for $$$d=1$$$ and $$$d=4$$$ it can be proved that there is no answer. In the second test case for $$$d = 1$$$, you can get $$$a = [1, 5, 10, 9, 3]$$$, for $$$d = 8$$$, you can get $$$a = [3, 5, 10, 9, 8]$$$, it can be proved that for $$$d = 11$$$ there is no answer. | 3,300 | true | true | false | false | true | false | false | false | false | false | 1,971 |
1722G | Problem - 1722G - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags bitmasks constructive algorithms greedy *1500 No tag edit access β Contest materials ") xa0β the number of test cases. Then $$$t$$$ lines follow, each containing a single integer $$$n$$$ $$$(3 leq n leq 2cdot10^5)$$$xa0β the length of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output one line containing $$$n$$$ distinct integers that satisfy the conditions. If there are multiple answers, you can output any of them. Example Input 7 8 3 4 5 6 7 9 Output 4 2 1 5 0 6 7 3 2 1 3 2 1 3 0 2 0 4 5 3 4 1 2 12 3 8 1 2 3 4 5 6 7 8 2 3 7 4 0 5 6 9 Note In the first test case the XOR on odd indices is $$$4 oplus 1 oplus 0 oplus 7 = 2$$$ and the XOR on even indices is $$$2 oplus 5 oplus 6 oplus 3= 2$$$. | 1,500 | false | true | false | false | false | true | false | false | false | false | 1,947 |
1601B | 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 le n le 300,000$$$)xa0β the depth of the well. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 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, ldots, b_n$$$ ($$$0 le b_i le 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$$$xa0β the minimum possible number of jumps. Then print the sequence $$$d_1,,d_2,,ldots,,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 10 0 1 2 3 5 5 6 7 8 5 9 8 7 1 5 4 3 2 0 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 Rightarrow 9 dashrightarrow 9 Rightarrow 4 dashrightarrow 5$$$ where $$$Rightarrow$$$ is the jump and $$$dashrightarrow$$$ is slipping during breaks. | 1,900 | false | false | false | true | true | false | false | false | false | true | 2,650 |
231C | A piece of paper contains an array of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_. Your task is to find a number that occurs the maximum number of times in this array. However, before looking for such number, you are allowed to perform not more than _k_ following operations β choose an arbitrary element from the array and add 1 to it. In other words, you are allowed to increase some array element by 1 no more than _k_ times (you are allowed to increase the same element of the array multiple times). Your task is to find the maximum number of occurrences of some number in the array after performing no more than _k_ allowed operations. If there are several such numbers, your task is to find the minimum one. Input The first line contains two integers _n_ and _k_ (1u2009β€u2009_n_u2009β€u2009105; 0u2009β€u2009_k_u2009β€u2009109) β the number of elements in the array and the number of operations you are allowed to perform, correspondingly. The third line contains a sequence of _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (_a__i_u2009β€u2009109) β the initial array. The numbers in the lines are separated by single spaces. Output In a single line print two numbers β the maximum number of occurrences of some number in the array after at most _k_ allowed operations are performed, and the minimum number that reaches the given maximum. Separate the printed numbers by whitespaces. Note In the first sample your task is to increase the second element of the array once and increase the fifth element of the array twice. Thus, we get sequence 6,u20094,u20094,u20090,u20094, where number 4 occurs 3 times. In the second sample you don't need to perform a single operation or increase each element by one. If we do nothing, we get array 5,u20095,u20095, if we increase each by one, we get 6,u20096,u20096. In both cases the maximum number of occurrences equals 3. So we should do nothing, as number 5 is less than number 6. In the third sample we should increase the second array element once and the fifth element once. Thus, we get sequence 3,u20092,u20092,u20092,u20092, where number 2 occurs 4 times. | 1,600 | false | false | false | false | false | false | false | true | true | false | 8,914 |
705A | Problem - 705A - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags implementation *800 No tag edit access β Contest materials . As we all know, he can barely talk when he turns into the incredible Hulk. That's why he asked you to help him to express his feelings. Hulk likes the Inception so much, and like that his feelings are complicated. They have _n_ layers. The first layer is hate, second one is love, third one is hate and so on... For example if _n_u2009=u20091, then his feeling is "I hate it" or if _n_u2009=u20092 it's "I hate that I love it", and if _n_u2009=u20093 it's "I hate that I love that I hate it" and so on. Please help Dr. Banner. Input The only line of the input contains a single integer _n_ (1u2009β€u2009_n_u2009β€u2009100)xa0β the number of layers of love and hate. Output Print Dr.Banner's feeling in one line. Examples Input 1 Output I hate it Input 2 Output I hate that I love it Input 3 Output I hate that I love that I hate it | 800 | false | false | true | false | false | false | false | false | false | false | 7,001 |
736D | Problem - 736D - Codeforces =============== xa0 ]( --- Finished β Virtual participation Virtual contest is a way to take part in past contest, as close as possible to participation on time. It is supported only ICPC mode for virtual contests. If you've seen these problems, a virtual contest is not for you - solve these problems in the archive. If you just want to solve some problem from a contest, a virtual contest is not for you - solve this problem in the archive. Never use someone else's code, read the tutorials or communicate with other person during a virtual contest. β Problem tags math matrices *2800 No tag edit access β Contest materials from the list. Input The first line contains two integers _n_ and _m_ (1u2009β€u2009_n_u2009β€u20092000, _n_u2009β€u2009_m_u2009β€u2009_min_(_n_2,u2009500u2009000))xa0β the number of elements in the permutation. Then follow _m_ lines, each containing some valid pair (_a__i_,u2009_b__i_) (1u2009β€u2009_a__i_,u2009_b__i_u2009β€u2009_n_). It's guaranteed that no pair occurs in the input twice and that the total number of valid permutations (i.e. using only allowed pairs position-elements) is odd. Output Print _m_ lines, one line for each valid pair. The _i_-th line should contain "YES" if after Ostap removes the _i_-th pair (and only it) the remaining number of valid permutations is odd. Otherwise, print Β«NOΒ». Examples Input 2 3 1 1 1 2 2 2 Output NO YES NO Input 3 3 1 1 2 2 3 3 Output NO NO NO Input 3 7 3 3 3 1 1 3 1 1 2 2 1 2 2 1 Output YES NO NO NO YES NO NO | 2,800 | true | false | false | false | false | false | false | false | false | false | 6,847 |
1044E | You are given an $$$n imes m$$$ grid. Each grid cell is filled with a unique integer from $$$1$$$ to $$$nm$$$ so that each integer appears exactly once. In one operation, you can choose an arbitrary cycle of the grid and move all integers along that cycle one space over. Here, a cycle is any sequence that satisfies the following conditions: There are at least four squares. Each square appears at most once. Every pair of adjacent squares, and also the first and last squares, share an edge. For example, if we had the following grid: We can choose an arbitrary cycle like this one: To get the following grid: In this particular case, the chosen cycle can be represented as the sequence $$$[1, 2, 3, 6, 5, 8, 7, 4]$$$, the numbers are in the direction that we want to rotate them in. Find any sequence of operations to sort the grid so that the array created by concatenating the rows from the highest to the lowest is sorted (look at the first picture above). Note you do not need to minimize number of operations or sum of cycle lengths. The only constraint is that the sum of all cycles lengths must not be greater than $$$10^5$$$. We can show that an answer always exists under the given constraints. Output any valid sequence of moves that will sort the grid. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$3 leq n,m leq 20$$$)xa0β the dimensions of the grid. Each of the next $$$n$$$ lines contains $$$m$$$ integers $$$x_{i,1}, x_{i,2}, ldots, x_{i, m}$$$ ($$$1 leq x_{i,j} leq nm$$$), denoting the values of the block in row $$$i$$$ and column $$$j$$$. It is guaranteed that all $$$x_{i,j}$$$ are distinct. Output First, print a single integer $$$k$$$, the number of operations ($$$k geq 0$$$). On each of the next $$$k$$$ lines, print a cycle as follows: $$$$$$s y_1 y_2 ldots y_s$$$$$$ Here, $$$s$$$ is the number of blocks to move ($$$s geq 4$$$). Here we have block $$$y_1$$$ moving to where block $$$y_2$$$ is, block $$$y_2$$$ moving to where block $$$y_3$$$ is, and so on with block $$$y_s$$$ moving to where block $$$y_1$$$ is. The sum of $$$s$$$ over all operations must be at most $$$10^5$$$. Examples Input 3 5 1 2 3 5 10 11 6 4 14 9 12 7 8 13 15 Output 3 4 4 14 13 8 4 5 10 9 4 4 12 7 6 11 Note The first sample is the case in the statement. Here, we can use the cycle in reverse order to sort the grid. | 3,100 | false | false | true | false | false | false | false | false | false | false | 5,472 |
1280E | Your friend Kirchhoff is shocked with the current state of electronics design. "Ohmygosh! Watt is wrong with the field? All these circuits are inefficient! There's so much capacity for improvement. The electrical engineers must not conduct their classes very well. It's absolutely revolting" he said. The negativity just keeps flowing out of him, but even after complaining so many times he still hasn't lepton the chance to directly change anything. "These circuits have too much total resistance. Wire they designed this way? It's just causing a massive loss of resistors! Their entire field could conserve so much money if they just maximized the potential of their designs. Why can't they just try alternative ideas?" The frequency of his protests about the electrical engineering department hertz your soul, so you have decided to take charge and help them yourself. You plan to create a program that will optimize the circuits while keeping the same circuit layout and maintaining the same effective resistance. A circuit has two endpoints, and is associated with a certain constant, $$$R$$$, called its effective resistance. The circuits we'll consider will be formed from individual resistors joined together in series or in parallel, forming more complex circuits. The following image illustrates combining circuits in series or parallel. According to your friend Kirchhoff, the effective resistance can be calculated quite easily when joining circuits this way: When joining $$$k$$$ circuits in series with effective resistances $$$R_1, R_2, ldots, R_k$$$, the effective resistance $$$R$$$ of the resulting circuit is the sum $$$$$$R = R_1 + R_2 + ldots + R_k.$$$$$$ When joining $$$k$$$ circuits in parallel with effective resistances $$$R_1, R_2, ldots, R_k$$$, the effective resistance $$$R$$$ of the resulting circuit is found by solving for $$$R$$$ in $$$$$$frac{1}{R} = frac{1}{R_1} + frac{1}{R_2} + ldots + frac{1}{R_k},$$$$$$ assuming all $$$R_i > 0$$$; if at least one $$$R_i = 0$$$, then the effective resistance of the whole circuit is simply $$$R = 0$$$. Circuits will be represented by strings. Individual resistors are represented by an asterisk, "*". For more complex circuits, suppose $$$s_1, s_2, ldots, s_k$$$ represent $$$k ge 2$$$ circuits. Then: "($$$s_1$$$ S $$$s_2$$$ S $$$ldots$$$ S $$$s_k$$$)" represents their series circuit; "($$$s_1$$$ P $$$s_2$$$ P $$$ldots$$$ P $$$s_k$$$)" represents their parallel circuit. For example, "(P (S *) P *)" represents the following circuit: Given a circuit, your task is to assign the resistances of the individual resistors such that they satisfy the following requirements: Each individual resistor has a nonnegative integer resistance value; The effective resistance of the whole circuit is $$$r$$$; The sum of the resistances of the individual resistors is minimized. If there are $$$n$$$ individual resistors, then you need to output the list $$$r_1, r_2, ldots, r_n$$$ ($$$0 le r_i$$$, and $$$r_i$$$ is an integer), where $$$r_i$$$ is the resistance assigned to the $$$i$$$-th individual resistor that appears in the input (from left to right). If it is impossible to accomplish the task, you must say so as well. If it is possible, then it is guaranteed that the minimum sum of resistances is at most $$$10^{18}$$$. Input The first line of input contains a single integer $$$t$$$ ($$$1 le t le 32000$$$), denoting the number of test cases. The next lines contain descriptions of the test cases. Each test case consists of a single line containing the integer $$$r$$$ ($$$1 le r le 10^6$$$), space, and then the string representing the circuit. It is guaranteed that the string is valid and follows the description above. The number of individual resistors (symbols "*") is at least $$$1$$$ and at most $$$80000$$$. It is guaranteed that the total number of individual resistors across all test cases is at most $$$320000$$$. | 2,900 | true | false | false | false | false | false | false | false | false | false | 4,290 |
1553G | Consider a sequence of distinct integers $$$a_1, ldots, a_n$$$, each representing one node of a graph. There is an edge between two nodes if the two values are not coprime, i.xa0e. they have a common divisor greater than $$$1$$$. There are $$$q$$$ queries, in each query, you want to get from one given node $$$a_s$$$ to another $$$a_t$$$. In order to achieve that, you can choose an existing value $$$a_i$$$ and create new value $$$a_{n+1} = a_i cdot (1 + a_i)$$$, with edges to all values that are not coprime with $$$a_{n+1}$$$. Also, $$$n$$$ gets increased by $$$1$$$. You can repeat that operation multiple times, possibly making the sequence much longer and getting huge or repeated values. What's the minimum possible number of newly created nodes so that $$$a_t$$$ is reachable from $$$a_s$$$? Queries are independent. In each query, you start with the initial sequence $$$a$$$ given in the input. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 leq n leq 150,000$$$, $$$1 leq q leq 300,000$$$)xa0β the size of the sequence and the number of queries. The second line contains $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$ ($$$2 leq a_i leq 10^6$$$, $$$a_i eq a_j$$$ if $$$i e j$$$). The $$$j$$$-th of the following $$$q$$$ lines contains two distinct integers $$$s_j$$$ and $$$t_j$$$ ($$$1 leq s_j, t_j leq n$$$, $$$s_j eq t_j$$$)xa0β indices of nodes for $$$j$$$-th query. Output Print $$$q$$$ lines. The $$$j$$$-th line should contain one integer: the minimum number of new nodes you create in order to move from $$$a_{s_j}$$$ to $$$a_{t_j}$$$. Examples Input 3 3 2 10 3 1 2 1 3 2 3 Input 5 12 3 8 7 6 25 1 2 1 3 1 4 1 5 2 1 2 3 2 4 2 5 3 1 3 2 3 4 3 5 Output 0 1 0 1 0 1 0 1 1 1 1 2 Note In the first example, you can first create new value $$$2 cdot 3 = 6$$$ or $$$10 cdot 11 = 110$$$ or $$$3 cdot 4 = 12$$$. None of that is needed in the first query because you can already get from $$$a_1 = 2$$$ to $$$a_2 = 10$$$. In the second query, it's optimal to first create $$$6$$$ or $$$12$$$. For example, creating $$$6$$$ makes it possible to get from $$$a_1 = 2$$$ to $$$a_3 = 3$$$ with a path $$$(2, 6, 3)$$$. In the last query of the second example, we want to get from $$$a_3 = 7$$$ to $$$a_5 = 25$$$. One way to achieve that is to first create $$$6 cdot 7 = 42$$$ and then create $$$25 cdot 26 = 650$$$. The final graph has seven nodes and it contains a path from $$$a_3 = 7$$$ to $$$a_5 = 25$$$. | 2,700 | true | false | false | false | false | true | true | false | false | true | 2,869 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.