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 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1878D | You are given a string $$$s$$$ of length $$$n$$$, containing lowercase Latin letters. Next you will be given a positive integer $$$k$$$ and two arrays, $$$l$$$ and $$$r$$$ of length $$$k$$$. It is guaranteed that the following conditions hold for these 2 arrays: $$$l_1 = 1$$$; $$$r_k = n$$$; $$$l_i le r_i$$$, for each positive integer $$$i$$$ such that $$$1 le i le k$$$; $$$l_i = r_{i-1}+1$$$, for each positive integer $$$i$$$ such that $$$2 le i le k$$$; Now you will be given a positive integer $$$q$$$ which represents the number of modifications you need to do on $$$s$$$. Each modification is defined with one positive integer $$$x$$$: Find an index $$$i$$$ such that $$$l_i le x le r_i$$$ (notice that such $$$i$$$ is unique). Let $$$a=min(x, r_i+l_i-x)$$$ and let $$$b=max(x, r_i+l_i-x)$$$. Reverse the substring of $$$s$$$ from index $$$a$$$ to index $$$b$$$. Reversing the substring $$$[a, b]$$$ of a string $$$s$$$ means to make $$$s$$$ equal to $$$s_1, s_2, dots, s_{a-1}, s_b, s_{b-1}, dots, s_{a+1}, s_a, s_{b+1}, s_{b+2}, dots, s_{n-1}, s_n$$$. Print $$$s$$$ after the last modification is finished. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 2cdot 10^5$$$)xa0— the length of the string $$$s$$$, and the length of arrays $$$l$$$ and $$$r$$$. The second line of each test case contains the string $$$s$$$ ($$$ s = n$$$) containing lowercase Latin lettersxa0— the initial string. The third line of each test case contains $$$k$$$ positive integers $$$l_1, l_2, dots, l_k$$$ ($$$1 le l_i le n$$$)xa0— the array $$$l$$$. The fourth line of each test case contains $$$k$$$ positive integers $$$r_1, r_2, dots, r_k$$$ ($$$1 le r_i le n$$$)xa0— the array $$$r$$$. The fifth line of each test case contains a positive integer $$$q$$$ ($$$1 le q le 2 cdot 10^5 $$$)xa0— the number of modifications you need to do to $$$s$$$. The sixth line of each test case contains $$$q$$$ positive integers $$$x_1, x_2, dots, x_q$$$ ($$$1le x_i le n$$$)xa0— the description of the modifications. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2cdot10^5$$$. It is guaranteed that the sum of $$$q$$$ over all test cases does not exceed $$$2cdot10^5$$$. It is guaranteed that the conditions in the statement hold for the arrays $$$l$$$ and $$$r$$$. Output For each test case, in a new line, output the string $$$s$$$ after the last modification is done. Example Input 5 4 2 abcd 1 3 2 4 2 1 3 5 3 abcde 1 2 3 1 2 5 3 1 2 3 3 1 gaf 1 3 2 2 2 10 1 aghcdegdij 1 10 5 1 2 3 4 2 1 1 a 1 1 1 1 Output badc abedc gaf jihgedcdga a Note In the first test case: The initial string is "abcd". In the first modification, we have $$$x=1$$$. Since $$$l_1=1leq x leq r_1=2$$$, we find the index $$$i = 1$$$. We reverse the substring from index $$$x=1$$$ to $$$l_1+r_1-x=1+2-1=2$$$. After this modification, our string is "bacd". In the second modification (and the last modification), we have $$$x=3$$$. Since $$$l_2=3leq x leq r_2=4$$$, we find the index $$$i = 2$$$. We reverse the substring from index $$$x=3$$$ to $$$l_2+r_2-x=3+4-3=4$$$. After this modification, our string is "badc". In the second test case: The initial string is "abcde". In the first modification, we have $$$x=1$$$. Since $$$l_1=1leq x leq r_1=1$$$, we find the index $$$i = 1$$$. We reverse the substring from index $$$x=1$$$ to $$$l_1+r_1-x=1+1-1=1$$$. After this modification, our string hasn't changed ("abcde"). In the second modification, we have $$$x=2$$$. Since $$$l_2=2leq x leq r_2=2$$$, we find the index $$$i = 2$$$. We reverse the substring from index $$$x=2$$$ to $$$l_2+r_2-x=2+2-2=2$$$. After this modification, our string hasn't changed ("abcde"). In the third modification (and the last modification), we have $$$x=3$$$. Since $$$l_3=3leq x leq r_3=5$$$, we find the index $$$i = 3$$$. We reverse the substring from index $$$x=3$$$ to $$$l_3+r_3-x=3+5-3=5$$$. After this modification, our string is "abedc". | 1,600 | false | true | false | false | true | false | false | false | false | false | 1,014 |
1619A | Problem - 1619A - 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 *800 No tag edit access → Contest materials ") xa0—the number of test cases. This is followed by $$$t$$$ lines, each containing a description of one test case. The given strings consist only of lowercase Latin letters and have lengths between $$$1$$$ and $$$100$$$ inclusive. Output For each test case, output on a separate line: YES if the string in the corresponding test case is square, NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response). Example Input 10 a aa aaa aaaa abab abcabc abacaba xxyy xyyx xyxy Output NO YES NO YES YES YES NO NO NO YES | 800 | false | false | true | false | false | false | false | false | false | false | 2,535 |
1451E1 | The only difference between the easy and hard versions is the constraints on the number of queries. This is an interactive problem. Ridbit has a hidden array $$$a$$$ of $$$n$$$ integers which he wants Ashish to guess. Note that $$$n$$$ is a power of two. Ashish is allowed to ask three different types of queries. They are of the form AND $$$i$$$ $$$j$$$: ask for the $$$ OR $$$i$$$ $$$j$$$: ask for the $$$ XOR $$$i$$$ $$$j$$$: ask for the $$$ Can you help Ashish guess the elements of the array? In this version, each element takes a value in the range $$$[0, n-1]$$$ (inclusive) and Ashish can ask no more than $$$n+2$$$ queries. Interaction To ask a query print a single line containing one of the following (without quotes) "AND i j" "OR i j" "XOR i j" where $$$i$$$ and $$$j$$$ $$$(1 leq i, j le n$$$, $$$i eq j)$$$ denote the indices being queried. For each query, you will receive an integer $$$x$$$ whose value depends on the type of query. If the indices queried are invalid or you exceed the number of queries however, you will get $$$x = -1$$$. In this case, you should terminate the program immediately. When you have guessed the elements of the array, print a single line "! " (without quotes), followed by $$$n$$$ space-separated integers xa0— the elements of the array. Guessing the array does not count towards the number of queries asked. The interactor is not adaptive. The array $$$a$$$ does not change with queries. After printing a query do not forget to output the end of the line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see the documentation for other languages. Hacks To hack the solution, use the following test format: On the first line print a single integer $$$n$$$ $$$(4 le n le 2^{16})$$$xa0— the length of the array. It must be a power of 2. The next line should contain $$$n$$$ space-separated integers in the range $$$[0, n-1]$$$xa0— the array $$$a$$$. Note The array $$$a$$$ in the example is $$$[0, 0, 2, 3]$$$. | 2,000 | true | false | false | false | false | true | false | false | false | false | 3,424 |
1310E | Problem - 1310E - 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 *2900 No tag edit access → Contest materials and Codeforces Round #623 ") = {1, 1, 2, 2, 4}$$$. Let's define $$$f^k(a)$$$, as applying $$$f$$$ to array $$$a$$$ $$$k$$$ times: $$$f^k(a) = f(f^{k-1}(a)), f^0(a) = a$$$. E.g., $$$f^2({5, 5, 1, 2, 5, 2, 3, 3, 9, 5}) = {1, 2, 2}$$$. You are given integers $$$n, k$$$ and you are asked how many different values the function $$$f^k(a)$$$ can have, where $$$a$$$ is arbitrary non-empty array with numbers of size no more than $$$n$$$. Print the answer modulo $$$998,244,353$$$. Input The first and only line of input consists of two integers $$$n, k$$$ ($$$1 le n, k le 2020$$$). Output Print one numberxa0— the number of different values of function $$$f^k(a)$$$ on all possible non-empty arrays with no more than $$$n$$$ elements modulo $$$998,244,353$$$. Examples Input 3 1 Output 6 Input 5 6 Output 1 Input 10 1 Output 138 Input 10 2 Output 33 | 2,900 | false | false | false | true | false | false | false | false | false | false | 4,145 |
117C | Problem - 117C - Codeforces =============== xa0 ") exists either an edge going from _u_ to _v_, or an edge from _v_ to _u_. You are given a tournament consisting of _n_ vertexes. Your task is to find there a cycle of length three. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u20095000). Next _n_ lines contain the adjacency matrix _A_ of the graph (without spaces). _A__i_,u2009_j_u2009=u20091 if the graph has an edge going from vertex _i_ to vertex _j_, otherwise _A__i_,u2009_j_u2009=u20090. _A__i_,u2009_j_ stands for the _j_-th character in the _i_-th line. It is guaranteed that the given graph is a tournament, that is, _A__i_,u2009_i_u2009=u20090,u2009_A__i_,u2009_j_u2009≠u2009_A__j_,u2009_i_ (1u2009≤u2009_i_,u2009_j_u2009≤u2009_n_,u2009_i_u2009≠u2009_j_). Output Print three distinct vertexes of the graph _a_1, _a_2, _a_3 (1u2009≤u2009_a__i_u2009≤u2009_n_), such that _A__a_1,u2009_a_2u2009=u2009_A__a_2,u2009_a_3u2009=u2009_A__a_3,u2009_a_1u2009=u20091, or "-1", if a cycle whose length equals three does not exist. If there are several solutions, print any of them. Examples Input 5 00100 10000 01001 11101 11000 Output 1 3 2 Input 5 01111 00000 01000 01100 01110 Output -1 | 2,000 | false | false | false | false | false | false | false | false | false | true | 9,419 |
958D1 | The Rebel fleet is on the run. It consists of _m_ ships currently gathered around a single planet. Just a few seconds ago, the vastly more powerful Empire fleet has appeared in the same solar system, and the Rebels will need to escape into hyperspace. In order to spread the fleet, the captain of each ship has independently come up with the coordinate to which that ship will jump. In the obsolete navigation system used by the Rebels, this coordinate is given as the value of an arithmetic expression of the form . To plan the future of the resistance movement, Princess Heidi needs to know, for each ship, how many ships are going to end up at the same coordinate after the jump. You are her only hope! Input The first line of the input contains a single integer _m_ (1u2009≤u2009_m_u2009≤u2009200u2009000) – the number of ships. The next _m_ lines describe one jump coordinate each, given as an arithmetic expression. An expression has the form (a+b)/c. Namely, it consists of: an opening parenthesis (, a positive integer _a_ of up to two decimal digits, a plus sign +, a positive integer _b_ of up to two decimal digits, a closing parenthesis ), a slash /, and a positive integer _c_ of up to two decimal digits. Output Print a single line consisting of _m_ space-separated integers. The _i_-th integer should be equal to the number of ships whose coordinate is equal to that of the _i_-th ship (including the _i_-th ship itself). Example Input 4 (99+98)/97 (26+4)/10 (12+33)/15 (5+1)/7 Note In the sample testcase, the second and the third ship will both end up at the coordinate 3. Note that this problem has only two versions – easy and hard. | 1,400 | true | false | false | false | false | false | false | false | false | false | 5,885 |
156E | Mrs. Hudson hasn't made her famous pancakes for quite a while and finally she decided to make them again. She has learned _m_ new recipes recently and she can't wait to try them. Those recipes are based on _n_ special spices. Mrs. Hudson has these spices in the kitchen lying in jars numbered with integers from 0 to _n_u2009-u20091 (each spice lies in an individual jar). Each jar also has the price of the corresponding spice inscribed — some integer _a__i_. We know three values for the _i_-th pancake recipe: _d__i_, _s__i_, _c__i_. Here _d__i_ and _c__i_ are integers, and _s__i_ is the pattern of some integer written in the numeral system with radix _d__i_. The pattern contains digits, Latin letters (to denote digits larger than nine) and question marks. Number _x_ in the _d__i_-base numeral system matches the pattern _s__i_, if we can replace question marks in the pattern with digits and letters so that we obtain number _x_ (leading zeroes aren't taken into consideration when performing the comparison). More formally: each question mark should be replaced by exactly one digit or exactly one letter. If after we replace all question marks we get a number with leading zeroes, we can delete these zeroes. For example, number 40A9875 in the 11-base numeral system matches the pattern "??4??987?", and number 4A9875 does not. To make the pancakes by the _i_-th recipe, Mrs. Hudson should take all jars with numbers whose representation in the _d__i_-base numeral system matches the pattern _s__i_. The control number of the recipe (_z__i_) is defined as the sum of number _c__i_ and the product of prices of all taken jars. More formally: (where _j_ is all such numbers whose representation in the _d__i_-base numeral system matches the pattern _s__i_). Mrs. Hudson isn't as interested in the control numbers as she is in their minimum prime divisors. Your task is: for each recipe _i_ find the minimum prime divisor of number _z__i_. If this divisor exceeds 100, then you do not have to find it, print -1. Input The first line contains the single integer _n_ (1u2009≤u2009_n_u2009≤u2009104). The second line contains space-separated prices of the spices _a_0,u2009_a_1,u2009...,u2009_a__n_u2009-u20091, where _a__i_ is an integer (1u2009≤u2009_a__i_u2009≤u20091018). The third line contains the single integer _m_ (1u2009≤u2009_m_u2009≤u20093·104) — the number of recipes Mrs. Hudson has learned. Next _m_ lines describe the recipes, one per line. First you are given an integer _d__i_, written in the decimal numeral system (2u2009≤u2009_d__i_u2009≤u200916). Then after a space follows the _s__i_ pattern — a string from 1 to 30 in length, inclusive, consisting of digits from "0" to "9", letters from "A" to "F" and signs "?". Letters from "A" to "F" should be considered as digits from 10 to 15 correspondingly. It is guaranteed that all digits of the pattern (including the digits that are represented by letters) are strictly less than _d__i_. Then after a space follows an integer _c__i_, written in the decimal numeral system (1u2009≤u2009_c__i_u2009≤u20091018). Please do not use the %lld specificator to read or write 64-bit integers in С++, in is preferred to use cin, cout, strings or the %I64d specificator instead. Output For each recipe count by what minimum prime number the control number is divided and print this prime number on the single line. If this number turns out larger than 100, print -1. Examples Input 4 2 3 5 7 4 2 ?0 11 2 ?1 13 2 0? 17 2 1? 19 Input 1 1000000000000000000 1 16 ?????????????? 1 Note In the first test any one-digit number in the binary system matches. The jar is only one and its price is equal to 1, the number _c_ is also equal to 1, the control number equals 2. The minimal prime divisor of 2 is 2. In the second test there are 4 jars with numbers from 0 to 3, and the prices are equal 2, 3, 5 and 7 correspondingly — the first four prime numbers. In all recipes numbers should be two-digit. In the first recipe the second digit always is 0, in the second recipe the second digit always is 1, in the third recipe the first digit must be 0, in the fourth recipe the first digit always is 1. Consequently, the control numbers u200bu200bare as follows: in the first recipe 2u2009×u20095u2009+u200911u2009=u200921 (the minimum prime divisor is 3), in the second recipe 3u2009×u20097u2009+u200913u2009=u200944 (the minimum prime divisor is 2), in the third recipe 2u2009×u20093u2009+u200917u2009=u200923 (the minimum prime divisor is 23) and, finally, in the fourth recipe 5u2009×u20097u2009+u200919u2009=u200954 (the minimum prime divisor is 2). In the third test, the number should consist of fourteen digits and be recorded in a sixteen-base numeral system. Number 0 (the number of the single bottles) matches, the control number will be equal to 1018u2009+u20091. The minimum prime divisor of this number is equal to 101 and you should print -1. | 3,000 | false | false | false | true | false | false | true | false | false | false | 9,248 |
1537F | You have a connected undirected graph made of $$$n$$$ nodes and $$$m$$$ edges. The $$$i$$$-th node has a value $$$v_i$$$ and a target value $$$t_i$$$. In an operation, you can choose an edge $$$(i, j)$$$ and add $$$k$$$ to both $$$v_i$$$ and $$$v_j$$$, where $$$k$$$ can be any integer. In particular, $$$k$$$ can be negative. Your task to determine if it is possible that by doing some finite number of operations (possibly zero), you can achieve for every node $$$i$$$, $$$v_i = t_i$$$. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 1000$$$), the number of test cases. Then the test cases follow. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$2 leq n leq 2cdot 10^5$$$, $$$n-1leq mleq min(2cdot 10^5, frac{n(n-1)}{2})$$$) — the number of nodes and edges respectively. The second line contains $$$n$$$ integers $$$v_1ldots, v_n$$$ ($$$-10^9 leq v_i leq 10^9$$$) — initial values of nodes. The third line contains $$$n$$$ integers $$$t_1ldots, t_n$$$ ($$$-10^9 leq t_i leq 10^9$$$) — target values of nodes. Each of the next $$$m$$$ lines contains two integers $$$i$$$ and $$$j$$$ representing an edge between node $$$i$$$ and node $$$j$$$ ($$$1 leq i, j leq n$$$, $$$i e j$$$). It is guaranteed that the graph is connected and there is at most one edge between the same pair of nodes. It is guaranteed that the sum of $$$n$$$ over all testcases does not exceed $$$2 cdot 10^5$$$ and the sum of $$$m$$$ over all testcases does not exceed $$$2 cdot 10^5$$$. Output For each test case, if it is possible for every node to reach its target after some number of operations, print "YES". Otherwise, print "NO". Example Input 2 4 4 5 1 2 -3 3 3 10 1 1 2 1 4 3 2 3 4 4 4 5 8 6 6 -3 1 15 4 1 2 1 4 3 2 3 4 Note Here is a visualization of the first test case (the orange values denote the initial values and the blue ones the desired values): One possible order of operations to obtain the desired values for each node is the following: Operation $$$1$$$: Add $$$2$$$ to nodes $$$2$$$ and $$$3$$$. Operation $$$2$$$: Add $$$-2$$$ to nodes $$$1$$$ and $$$4$$$. Operation $$$3$$$: Add $$$6$$$ to nodes $$$3$$$ and $$$4$$$. Now we can see that in total we added $$$-2$$$ to node $$$1$$$, $$$2$$$ to node $$$2$$$, $$$8$$$ to node $$$3$$$ and $$$4$$$ to node $$$4$$$ which brings each node exactly to it's desired value. For the graph from the second test case it's impossible to get the target values. | 2,200 | true | true | false | false | false | true | false | false | false | true | 2,956 |
1878A | We define an integer to be the most common on a subsegment, if its number of occurrences on that subsegment is larger than the number of occurrences of any other integer in that subsegment. A subsegment of an array is a consecutive segment of elements in the array $$$a$$$. Given an array $$$a$$$ of size $$$n$$$, and an integer $$$k$$$, determine if there exists a non-empty subsegment of $$$a$$$ where $$$k$$$ is the most common element. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 100$$$, $$$1 le k le 100$$$)xa0— the number of elements in array and the element which must be the most common. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$a_3$$$, $$$dots$$$, $$$a_n$$$ ($$$1 le a_i le 100$$$)xa0— elements of the array. Output For each test case output "YES" if there exists a subsegment in which $$$k$$$ is the most common element, and "NO" otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer). Example Input 7 5 4 1 4 3 4 1 4 1 2 3 4 4 5 6 43 5 60 4 2 2 5 1 5 4 1 5 3 3 1 1 3 3 5 3 3 4 1 5 5 Output YES NO NO YES YES YES YES Note In the first test case we need to check if there is a subsegment where the most common element is $$$4$$$. On the subsegment $$$[2,5]$$$ the elements are $$$4, 3, 4, 1$$$. $$$4$$$ appears $$$2$$$ times; $$$1$$$ appears $$$1$$$ time; $$$3$$$ appears $$$1$$$ time. This means that $$$4$$$ is the most common element on the subsegment $$$[2, 5]$$$, so there exists a subsegment where $$$4$$$ is the most common element. | 800 | false | true | false | false | false | false | false | false | false | false | 1,017 |
290B | Problem - 290B - Codeforces =============== xa0 , separated by a single space. Output Output a single integer. Examples Input 1 1 Output 0 Input 3 7 Output 0 Input 13 10 Output 1 | 1,500 | false | false | true | false | false | false | false | false | false | false | 8,676 |
1720D1 | It is the easy version of the problem. The only difference is that in this version $$$a_i le 200$$$. 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 200$$$) — 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$$$. | 1,800 | false | false | false | true | false | false | true | false | false | false | 1,962 |
388D | Problem - 388D - 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 *2700 No tag edit access → Contest materials , . Where operation _xor_ means exclusive or operation ( calculate the number of perfect sets consisting of integers not greater than _k_. The answer can be very large, so print it modulo 1000000007 (109u2009+u20097). Input The first line contains an integer _k_ (0u2009≤u2009_k_u2009≤u2009109). Output Print a single integer — the number of required sets modulo 1000000007 (109u2009+u20097). Examples Input 1 Output 2 Input 2 Output 3 Input 3 Output 5 Input 4 Output 6 Note In example 1, there are 2 such sets: {0} and {0, 1}. Note that {1} is not a perfect set since 1 xor 1 = 0 and {1} doesn't contain zero. In example 4, there are 6 such sets: {0}, {0, 1}, {0, 2}, {0, 3}, {0, 4} and {0, 1, 2, 3}. | 2,700 | true | false | false | false | false | false | false | false | false | false | 8,274 |
1256C | There is a river of width $$$n$$$. The left bank of the river is cell $$$0$$$ and the right bank is cell $$$n + 1$$$ (more formally, the river can be represented as a sequence of $$$n + 2$$$ cells numbered from $$$0$$$ to $$$n + 1$$$). There are also $$$m$$$ wooden platforms on a river, the $$$i$$$-th platform has length $$$c_i$$$ (so the $$$i$$$-th platform takes $$$c_i$$$ consecutive cells of the river). It is guaranteed that the sum of lengths of platforms does not exceed $$$n$$$. You are standing at $$$0$$$ and want to reach $$$n+1$$$ somehow. If you are standing at the position $$$x$$$, you can jump to any position in the range $$$[x + 1; x + d]$$$. However you don't really like the water so you can jump only to such cells that belong to some wooden platform. For example, if $$$d=1$$$, you can jump only to the next position (if it belongs to the wooden platform). You can assume that cells $$$0$$$ and $$$n+1$$$ belong to wooden platforms. You want to know if it is possible to reach $$$n+1$$$ from $$$0$$$ if you can move any platform to the left or to the right arbitrary number of times (possibly, zero) as long as they do not intersect each other (but two platforms can touch each other). It also means that you cannot change the relative order of platforms. Note that you should move platforms until you start jumping (in other words, you first move the platforms and then start jumping). For example, if $$$n=7$$$, $$$m=3$$$, $$$d=2$$$ and $$$c = [1, 2, 1]$$$, then one of the ways to reach $$$8$$$ from $$$0$$$ is follow: The first example: $$$n=7$$$. Input The first line of the input contains three integers $$$n$$$, $$$m$$$ and $$$d$$$ ($$$1 le n, m, d le 1000, m le n$$$) — the width of the river, the number of platforms and the maximum distance of your jump, correspondingly. The second line of the input contains $$$m$$$ integers $$$c_1, c_2, dots, c_m$$$ ($$$1 le c_i le n, sumlimits_{i=1}^{m} c_i le n$$$), where $$$c_i$$$ is the length of the $$$i$$$-th platform. Output If it is impossible to reach $$$n+1$$$ from $$$0$$$, print NO in the first line. Otherwise, print YES in the first line and the array $$$a$$$ of length $$$n$$$ in the second line — the sequence of river cells (excluding cell $$$0$$$ and cell $$$n + 1$$$). If the cell $$$i$$$ does not belong to any platform, $$$a_i$$$ should be $$$0$$$. Otherwise, it should be equal to the index of the platform ($$$1$$$-indexed, platforms are numbered from $$$1$$$ to $$$m$$$ in order of input) to which the cell $$$i$$$ belongs. Note that all $$$a_i$$$ equal to $$$1$$$ should form a contiguous subsegment of the array $$$a$$$ of length $$$c_1$$$, all $$$a_i$$$ equal to $$$2$$$ should form a contiguous subsegment of the array $$$a$$$ of length $$$c_2$$$, ..., all $$$a_i$$$ equal to $$$m$$$ should form a contiguous subsegment of the array $$$a$$$ of length $$$c_m$$$. The leftmost position of $$$2$$$ in $$$a$$$ should be greater than the rightmost position of $$$1$$$, the leftmost position of $$$3$$$ in $$$a$$$ should be greater than the rightmost position of $$$2$$$, ..., the leftmost position of $$$m$$$ in $$$a$$$ should be greater than the rightmost position of $$$m-1$$$. See example outputs for better understanding. Examples Output YES 0 0 0 0 0 0 0 0 0 1 Output YES 0 0 0 0 1 1 0 0 0 0 Note Consider the first example: the answer is $$$[0, 1, 0, 2, 2, 0, 3]$$$. The sequence of jumps you perform is $$$0 ightarrow 2 ightarrow 4 ightarrow 5 ightarrow 7 ightarrow 8$$$. Consider the second example: it does not matter how to place the platform because you always can jump from $$$0$$$ to $$$11$$$. Consider the third example: the answer is $$$[0, 0, 0, 0, 1, 1, 0, 0, 0, 0]$$$. The sequence of jumps you perform is $$$0 ightarrow 5 ightarrow 6 ightarrow 11$$$. | 1,700 | false | true | false | false | false | false | false | false | false | false | 4,405 |
482E | You have a root tree containing _n_ vertexes. Let's number the tree vertexes with integers from 1 to _n_. The tree root is in the vertex 1. Each vertex (except fot the tree root) _v_ has a direct ancestor _p__v_. Also each vertex _v_ has its integer value _s__v_. Your task is to perform following queries: P _v_ _u_ (_u_u2009≠u2009_v_). If _u_ isn't in subtree of _v_, you must perform the assignment _p__v_u2009=u2009_u_. Otherwise you must perform assignment _p__u_u2009=u2009_v_. Note that after this query the graph continues to be a tree consisting of _n_ vertexes. V _v_ _t_. Perform assignment _s__v_u2009=u2009_t_. Your task is following. Before starting performing queries and after each query you have to calculate expected value written on the lowest common ancestor of two equiprobably selected vertices _i_ and _j_. Here lowest common ancestor of _i_ and _j_ is the deepest vertex that lies on the both of the path from the root to vertex _i_ and the path from the root to vertex _j_. Please note that the vertices _i_ and _j_ can be the same (in this case their lowest common ancestor coincides with them). Input The first line of the input contains integer _n_ (2u2009≤u2009_n_u2009≤u20095·104) — the number of the tree vertexes. The second line contains _n_u2009-u20091 integer _p_2,u2009_p_3,u2009...,u2009_p__n_ (1u2009≤u2009_p__i_u2009≤u2009_n_) — the description of the tree edges. It is guaranteed that those numbers form a tree. The third line contains _n_ integers — _s_1,u2009_s_2,u2009... _s__n_ (0u2009≤u2009_s__i_u2009≤u2009106) — the values written on each vertex of the tree. The next line contains integer _q_ (1u2009≤u2009_q_u2009≤u20095·104) — the number of queries. Each of the following _q_ lines contains the description of the query in the format described in the statement. It is guaranteed that query arguments _u_ and _v_ lie between 1 and _n_. It is guaranteed that argument _t_ in the queries of type V meets limits 0u2009≤u2009_t_u2009≤u2009106. Output Print _q_u2009+u20091 number — the corresponding expected values. Your answer will be considered correct if its absolute or relative error doesn't exceed 10u2009-u20099. Examples Input 5 1 2 2 1 1 2 3 4 5 5 P 3 4 P 4 5 V 2 3 P 5 2 P 1 4 Output 1.640000000 1.800000000 2.280000000 2.320000000 2.800000000 1.840000000 Note Note that in the query P _v_ _u_ if _u_ lies in subtree of _v_ you must perform assignment _p__u_u2009=u2009_v_. An example of such case is the last query in the sample. | 3,200 | false | false | false | false | true | false | false | false | false | false | 7,905 |
1443A | Today the kindergarten has a new group of $$$n$$$ kids who need to be seated at the dinner table. The chairs at the table are numbered from $$$1$$$ to $$$4n$$$. Two kids can't sit on the same chair. It is known that two kids who sit on chairs with numbers $$$a$$$ and $$$b$$$ ($$$a eq b$$$) will indulge if: 1. $$$gcd(a, b) = 1$$$ or, 2. $$$a$$$ divides $$$b$$$ or $$$b$$$ divides $$$a$$$. $$$gcd(a, b)$$$xa0— the maximum number $$$x$$$ such that $$$a$$$ is divisible by $$$x$$$ and $$$b$$$ is divisible by $$$x$$$. For example, if $$$n=3$$$ and the kids sit on chairs with numbers $$$2$$$, $$$3$$$, $$$4$$$, then they will indulge since $$$4$$$ is divided by $$$2$$$ and $$$gcd(2, 3) = 1$$$. If kids sit on chairs with numbers $$$4$$$, $$$6$$$, $$$10$$$, then they will not indulge. The teacher really doesn't want the mess at the table, so she wants to seat the kids so there are no $$$2$$$ of the kid that can indulge. More formally, she wants no pair of chairs $$$a$$$ and $$$b$$$ that the kids occupy to fulfill the condition above. Since the teacher is very busy with the entertainment of the kids, she asked you to solve this problem. Input The first line contains one integer $$$t$$$ ($$$1 leq t leq 100$$$)xa0— the number of test cases. Then $$$t$$$ test cases follow. Each test case consists of one line containing an integer $$$n$$$ ($$$1 leq n leq 100$$$)xa0— the number of kids. Output Output $$$t$$$ lines, which contain $$$n$$$ distinct integers from $$$1$$$ to $$$4n$$$xa0— the numbers of chairs that the kids should occupy in the corresponding test case. If there are multiple answers, print any of them. You can print $$$n$$$ numbers in any order. | 800 | true | false | false | false | false | true | false | false | false | false | 3,458 |
1555F | You have an undirected graph consisting of $$$n$$$ vertices with weighted edges. A simple cycle is a cycle of the graph without repeated vertices. Let the weight of the cycle be the xa0— the number of vertices and queries. Next $$$q$$$ lines contain queriesxa0— one per line. Each query contains three integers $$$u$$$, $$$v$$$ and $$$x$$$ ($$$1 le u, v le n$$$; $$$u eq v$$$; $$$0 le x le 1$$$)xa0— the vertices of the edge and its weight. It's guaranteed that there are no multiple edges in the input. Output For each query, print YES if the edge was added to the graph, or NO otherwise (both case-insensitive). Example Input 9 12 6 1 0 1 3 1 3 6 0 6 2 0 6 4 1 3 4 1 2 4 0 2 5 0 4 5 0 7 8 1 8 9 1 9 7 0 Output YES YES YES YES YES NO YES YES NO YES YES NO | 2,700 | false | false | false | false | true | false | false | false | false | true | 2,856 |
919B | Problem - 919B - 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 brute force dp implementation number theory *1100 No tag edit access → Contest materials ") Editorial") . Output A single number, denoting the $$$k$$$-th smallest perfect integer. Examples Input 1 Output 19 Input 2 Output 28 Note The first perfect integer is $$$19$$$ and the second one is $$$28$$$. | 1,100 | false | false | true | true | false | false | true | true | false | false | 6,055 |
1810E | There is an undirected graph with $$$n$$$ vertices and $$$m$$$ edges. Initially, for each vertex $$$i$$$, there is a monster with danger $$$a_{i}$$$ on that vertex. For a monster with danger $$$a_{i}$$$, you can defeat it if and only if you have defeated at least $$$a_{i}$$$ other monsters before. Now you want to defeat all the monsters. First, you choose some vertex $$$s$$$ and defeat the monster on that vertex (since you haven't defeated any monsters before, $$$a_{s}$$$ has to be $$$0$$$). Then, you can move through the edges. If you want to move from vertex $$$u$$$ to vertex $$$v$$$, then the following must hold: either the monster on vertex $$$v$$$ has been defeated before, or you can defeat it now. For the second case, you defeat the monster on vertex $$$v$$$ and reach vertex $$$v$$$. You can pass the vertices and the edges any number of times. Determine whether you can defeat all the monsters or not. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. Their description follows. The first line of each test case contains two integers $$$n$$$, $$$m$$$ ($$$1 le n, m le 2 cdot 10^5$$$) — the number of vertices and edges in the graph respectively. The second line of each test case contains $$$n$$$ integers $$$a_{1}, a_{2}, ldots, a_{n}$$$ ($$$0 le a_{i} le n$$$) — the dangers of monsters on corresponding vertices. For the following $$$m$$$ lines, each line contains two integers $$$u$$$, $$$v$$$ ($$$1 le u, v le n$$$), describing an edge connecting vertex $$$u$$$ and vertex $$$v$$$. It is guaranteed that there are no multi-edges or self-loops in the graph. It is guaranteed that both the sum of $$$n$$$ and the sum of $$$m$$$ over all test cases do not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" if you can defeat all the monsters, or "NO" otherwise. You can output the answer in any case (upper or lower). For example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as positive responses. Example Input 5 4 3 2 1 0 3 1 2 2 3 3 4 6 6 0 1 2 3 0 1 1 2 2 3 3 4 4 5 4 6 5 6 4 3 0 1 2 0 1 2 2 3 1 3 4 6 1 1 1 0 1 2 3 2 4 3 2 4 4 1 1 3 5 5 0 1 3 2 0 1 2 2 3 3 4 4 5 3 5 Note In the first test case, you can start at vertex $$$3$$$ and defeat the monster on it, before you go to vertices $$$2$$$, $$$1$$$ in this order, defeating the monsters on them as well. Then you return to vertex $$$3$$$, and go to vertex $$$4$$$, defeating the monster on it. In the third test case, there is no path to vertex $$$4$$$ if you start at vertex $$$1$$$. Also, there is no path to vertices $$$1$$$, $$$2$$$, and $$$3$$$ if you start at vertex $$$4$$$. | 2,100 | false | true | false | false | true | false | true | false | false | true | 1,409 |
26D | Problem - 26D - 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 combinatorics math probabilities *2400 No tag edit access → Contest materials ") ") . Output Output on a single line the desired probability with at least 4 digits after the decimal point. Examples Input 5 3 1 Output 0.857143 Input 0 5 5 Output 1 Input 0 1 0 Output 0 | 2,400 | true | false | false | false | false | false | false | false | false | false | 9,865 |
1110G | The tic-tac-toe game is starting on a tree of $$$n$$$ vertices. Some vertices are already colored in white while the remaining are uncolored. There are two playersxa0— white and black. The players make moves alternatively. The white player starts the game. In his turn, a player must select one uncolored vertex and paint it in his color. The player wins if he paints some path of three vertices in his color. In case all vertices are colored and neither player won, the game ends in a draw. Could you please find who will win the game or whether it ends as a draw, assuming both players play optimally? Input The first line contains a single integer $$$T$$$ ($$$1 le T le 50,000$$$)xa0— the number of test cases. Then descriptions of $$$T$$$ test cases follow. The first line of each test contains a single integer $$$n$$$ ($$$1 le n le 5 cdot 10^5$$$)xa0— the number of vertices in the tree. Each of the following $$$n - 1$$$ lines contains integers $$$v$$$, $$$u$$$ ($$$1 le v, u le n$$$) denoting an edge of the tree connecting vertices $$$v$$$ and $$$u$$$. The last line of a test case contains a string of letters 'W' (for white) and 'N' (for not colored) of length $$$n$$$ denoting already colored vertices. Vertexes already colored in white are denoted as 'W'. It's guaranteed that the given edges form a tree, that there is at least one uncolored vertex and that there is no path of three white vertices. It's guaranteed that sum of all $$$n$$$ among all test cases is at most $$$5 cdot 10^5$$$. Output For every test case, print either "White", "Draw" or "Black", depending on the result of the game. Example Input 2 4 1 2 1 3 1 4 NNNW 5 1 2 2 3 3 4 4 5 NNNNN Note In the first example, vertex $$$4$$$ is already colored in white. The white player can win by coloring the vertex $$$1$$$ in white first and the remaining vertex on his second turn. The process is illustrated with the pictures below. In the second example, we can show that no player can enforce their victory. | 3,100 | false | false | false | false | false | true | false | false | false | false | 5,143 |
653A | Limak is a little polar bear. He has _n_ balls, the _i_-th ball has size _t__i_. Limak wants to give one ball to each of his three friends. Giving gifts isn't easyxa0— there are two rules Limak must obey to make friends happy: No two friends can get balls of the same size. No two friends can get balls of sizes that differ by more than 2. For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2). Your task is to check whether Limak can choose three balls that satisfy conditions above. Input The first line of the input contains one integer _n_ (3u2009≤u2009_n_u2009≤u200950)xa0— the number of balls Limak has. The second line contains _n_ integers _t_1,u2009_t_2,u2009...,u2009_t__n_ (1u2009≤u2009_t__i_u2009≤u20091000) where _t__i_ denotes the size of the _i_-th ball. Output Print "YES" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print "NO" (without quotes). Examples Input 8 5 972 3 4 1 4 970 971 Note In the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17. In the second sample, there is no way to give gifts to three friends without breaking the rules. In the third sample, there is even more than one way to choose balls: 1. Choose balls with sizes 3, 4 and 5. 2. Choose balls with sizes 972, 970, 971. | 900 | false | false | true | false | false | false | true | false | true | false | 7,204 |
737E | Tanya is now five so all her friends gathered together to celebrate her birthday. There are _n_ children on the celebration, including Tanya. The celebration is close to its end, and the last planned attraction is gaming machines. There are _m_ machines in the hall, they are numbered 1 through _m_. Each of the children has a list of machines he wants to play on. Moreover, for each of the machines he knows the exact time he wants to play on it. For every machine, no more than one child can play on this machine at the same time. It is evening already, so every adult wants to go home. To speed up the process, you can additionally rent second copies of each of the machines. To rent the second copy of the _j_-th machine, you have to pay _p__j_ burles. After you rent a machine, you can use it for as long as you want. How long it will take to make every child play according to his plan, if you have a budget of _b_ burles for renting additional machines? There is only one copy of each machine, so it's impossible to rent a third machine of the same type. The children can interrupt the game in any moment and continue it later. If the _i_-th child wants to play on the _j_-th machine, it is allowed after you rent the copy of the _j_-th machine that this child would play some part of the time on the _j_-th machine and some part of the time on its copy (each of these parts could be empty). The interruptions and changes take no time and can be performed in any integer moment of time. Of course, a child can't play on more than one machine at the same time. Remember, that it is not needed to save money (no one saves money at the expense of children happiness!), it is needed to minimize the latest moment of time some child ends his game. Input The first line contains three integers _n_, _m_ and _b_ (1u2009≤u2009_n_u2009≤u200940, 1u2009≤u2009_m_u2009≤u200910, 0u2009≤u2009_b_u2009≤u2009106)xa0— the number of children, the number of gaming machines and the budget for renting additional machines. The second line contains _m_ integers _p_1,u2009_p_2,u2009...,u2009_p__m_ (1u2009≤u2009_p__j_u2009≤u2009106), where _p__j_ is the rent price for the second copy of the _j_-th machine. _n_ lines follow, _i_-th of them describes the wishes of the _i_-th child. The line starts with an integer _k__i_ (0u2009≤u2009_k__i_u2009≤u2009_m_)xa0— the number of machines, the _i_-th child wants to play on. Then there are _k__i_ pairs in the line, the _y_-th of them is _x__iy_, _t__iy_. It means that, the _i_-th child wants to play _t__iy_ (1u2009≤u2009_t__iy_u2009≤u20092500) minutes on the _x__iy_-th (1u2009≤u2009_x__iy_u2009≤u2009_m_) machine. In each of these _n_ lines the values _x__iy_ are distinct. Output In the first line print the minimum time in which all the children can finish their games. In the second line print a string of length _m_ consisting of zeros and ones. The _j_-th character is '1', if the copy of _j_-th machine should be rated, and '0' otherwise. In the third line print integer _g_ (0u2009≤u2009_g_u2009≤u2009106)xa0— the total number of time segments of continuous playing for all of the children. Then in _g_ lines print the segments as four integers _i_, _j_, _s_, _d_, meaning that the _i_-th child was playing on the _j_-th machine or its copy from the time moment _s_ (_s_u2009≥u20090) for _d_ minutes (_d_u2009≥u20091). You can print these lines in arbitrary order. If there are multiple answers, print any of them. Examples Input 2 2 100 3 7 2 1 3 2 1 2 1 3 2 1 Output 4 10 8 1 1 0 1 2 2 0 1 1 1 1 1 2 1 1 1 2 1 2 1 1 1 2 1 1 2 3 1 2 1 3 1 Input 3 2 15 11 7 2 2 10 1 5 1 2 20 2 1 4 2 3 Output 20 01 17 2 2 0 4 2 2 4 1 1 1 5 2 2 2 5 2 1 2 7 5 2 2 7 5 2 2 12 1 1 2 12 1 3 1 13 4 2 2 13 4 1 2 13 4 1 1 17 2 3 2 17 2 2 2 17 2 1 1 19 1 2 2 19 1 3 2 19 1 | 3,300 | false | true | false | false | false | false | false | false | false | true | 6,845 |
1395C | Boboniu likes bit operations. He wants to play a game with you. Boboniu gives you two sequences of non-negative integers $$$a_1,a_2,ldots,a_n$$$ and $$$b_1,b_2,ldots,b_m$$$. For each $$$i$$$ ($$$1le ile n$$$), you're asked to choose a $$$j$$$ ($$$1le jle m$$$) and let $$$c_i=a_i& b_j$$$, where $$$&$$$ denotes the . The next line contains $$$n$$$ integers $$$a_1,a_2,ldots,a_n$$$ ($$$0le a_i < 2^9$$$). The next line contains $$$m$$$ integers $$$b_1,b_2,ldots,b_m$$$ ($$$0le b_i < 2^9$$$). Output Print one integer: the minimum possible $$$c_1 c_2 ldots c_n$$$. Examples Input 7 6 1 9 1 9 8 1 0 1 1 4 5 1 4 Input 8 5 179 261 432 162 82 43 10 38 379 357 202 184 197 Note For the first example, we have $$$c_1=a_1& b_2=0$$$, $$$c_2=a_2& b_1=2$$$, $$$c_3=a_3& b_1=0$$$, $$$c_4 = a_4& b_1=0$$$.Thus $$$c_1 c_2 c_3 c_4 =2$$$, and this is the minimal answer we can get. | 1,600 | false | true | false | true | false | false | true | false | false | false | 3,694 |
1389D | You are given two lists of segments $$$[al_1, ar_1], [al_2, ar_2], dots, [al_n, ar_n]$$$ and $$$[bl_1, br_1], [bl_2, br_2], dots, [bl_n, br_n]$$$. Initially, all segments $$$[al_i, ar_i]$$$ are equal to $$$[l_1, r_1]$$$ and all segments $$$[bl_i, br_i]$$$ are equal to $$$[l_2, r_2]$$$. In one step, you can choose one segment (either from the first or from the second list) and extend it by $$$1$$$. In other words, suppose you've chosen segment $$$[x, y]$$$ then you can transform it either into $$$[x - 1, y]$$$ or into $$$[x, y + 1]$$$. Let's define a total intersection $$$I$$$ as the sum of lengths of intersections of the corresponding pairs of segments, i.e. $$$sumlimits_{i=1}^{n}{ ext{intersection_length}([al_i, ar_i], [bl_i, br_i])}$$$. Empty intersection has length $$$0$$$ and length of a segment $$$[x, y]$$$ is equal to $$$y - x$$$. What is the minimum number of steps you need to make $$$I$$$ greater or equal to $$$k$$$? Input The first line contains the single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$1 le k le 10^9$$$)xa0— the length of lists and the minimum required total intersection. The second line of each test case contains two integers $$$l_1$$$ and $$$r_1$$$ ($$$1 le l_1 le r_1 le 10^9$$$)xa0— the segment all $$$[al_i, ar_i]$$$ are equal to initially. The third line of each test case contains two integers $$$l_2$$$ and $$$r_2$$$ ($$$1 le l_2 le r_2 le 10^9$$$)xa0— the segment all $$$[bl_i, br_i]$$$ are equal to initially. It's guaranteed that the sum of $$$n$$$ doesn't exceed $$$2 cdot 10^5$$$. Output Print $$$t$$$ integersxa0— one per test case. For each test case, print the minimum number of step you need to make $$$I$$$ greater or equal to $$$k$$$. Example Input 3 3 5 1 2 3 4 2 1000000000 1 1 999999999 999999999 10 3 5 10 7 8 Note In the first test case, we can achieve total intersection $$$5$$$, for example, using next strategy: make $$$[al_1, ar_1]$$$ from $$$[1, 2]$$$ to $$$[1, 4]$$$ in $$$2$$$ steps; make $$$[al_2, ar_2]$$$ from $$$[1, 2]$$$ to $$$[1, 3]$$$ in $$$1$$$ step; make $$$[bl_1, br_1]$$$ from $$$[3, 4]$$$ to $$$[1, 4]$$$ in $$$2$$$ steps; make $$$[bl_2, br_2]$$$ from $$$[3, 4]$$$ to $$$[1, 4]$$$ in $$$2$$$ steps. In result, $$$I = ext{intersection_length}([al_1, ar_1], [bl_1, br_1]) + ext{intersection_length}([al_2, ar_2], [bl_2, br_2]) + + ext{intersection_length}([al_3, ar_3], [bl_3, br_3]) = 3 + 2 + 0 = 5$$$ In the second test case, we can make $$$[al_1, ar_1] = [0, 1000000000]$$$ in $$$1000000000$$$ steps and $$$[bl_1, br_1] = [0, 1000000000]$$$ in $$$1000000000$$$ steps. In the third test case, the total intersection $$$I$$$ is already equal to $$$10 > 3$$$, so we don't need to do any steps. | 2,100 | true | true | true | false | false | false | true | false | false | false | 3,725 |
1606B | Berland State University has received a new update for the operating system. Initially it is installed only on the $$$1$$$-st computer. Update files should be copied to all $$$n$$$ computers. The computers are not connected to the internet, so the only way to transfer update files from one computer to another is to copy them using a patch cable (a cable connecting two computers directly). Only one patch cable can be connected to a computer at a time. Thus, from any computer where the update files are installed, they can be copied to some other computer in exactly one hour. Your task is to find the minimum number of hours required to copy the update files to all $$$n$$$ computers if there are only $$$k$$$ patch cables in Berland State University. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^5$$$)xa0— the number of test cases. Each test case consists of a single line that contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 10^{18}$$$) — the number of computers and the number of patch cables. Output For each test case print one integerxa0— the minimum number of hours required to copy the update files to all $$$n$$$ computers. Note Let's consider the test cases of the example: $$$n=8$$$, $$$k=3$$$: 1. during the first hour, we copy the update files from the computer $$$1$$$ to the computer $$$2$$$; 2. during the second hour, we copy the update files from the computer $$$1$$$ to the computer $$$3$$$, and from the computer $$$2$$$ to the computer $$$4$$$; 3. during the third hour, we copy the update files from the computer $$$1$$$ to the computer $$$5$$$, from the computer $$$2$$$ to the computer $$$6$$$, and from the computer $$$3$$$ to the computer $$$7$$$; 4. during the fourth hour, we copy the update files from the computer $$$2$$$ to the computer $$$8$$$. $$$n=6$$$, $$$k=6$$$: 1. during the first hour, we copy the update files from the computer $$$1$$$ to the computer $$$2$$$; 2. during the second hour, we copy the update files from the computer $$$1$$$ to the computer $$$3$$$, and from the computer $$$2$$$ to the computer $$$4$$$; 3. during the third hour, we copy the update files from the computer $$$1$$$ to the computer $$$5$$$, and from the computer $$$2$$$ to the computer $$$6$$$. $$$n=7$$$, $$$k=1$$$: 1. during the first hour, we copy the update files from the computer $$$1$$$ to the computer $$$2$$$; 2. during the second hour, we copy the update files from the computer $$$1$$$ to the computer $$$3$$$; 3. during the third hour, we copy the update files from the computer $$$1$$$ to the computer $$$4$$$; 4. during the fourth hour, we copy the update files from the computer $$$4$$$ to the computer $$$5$$$; 5. during the fifth hour, we copy the update files from the computer $$$4$$$ to the computer $$$6$$$; 6. during the sixth hour, we copy the update files from the computer $$$3$$$ to the computer $$$7$$$. | 1,100 | true | true | true | false | false | false | false | false | false | false | 2,628 |
314C | Problem - 314C - 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 data structures dp *2000 No tag edit access → Contest materials . Input The first line contains integer _n_ (1u2009≤u2009_n_u2009≤u2009105). The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009106). Output In the single line print the answer to the problem modulo 1000000007 (109u2009+u20097). Examples Input 1 42 Output 42 Input 3 1 2 2 Output 13 Input 5 1 2 3 4 5 Output 719 | 2,000 | false | false | false | true | true | false | false | false | false | false | 8,588 |
959E | Problem - 959E - 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 dp graphs implementation math *1900 No tag edit access → Contest materials ") , the number of vertices in the graph. Output The only line contains an integer _x_, the weight of the graph's minimum spanning tree. Example Input 4 Output 4 Note In the first sample: The weight of the minimum spanning tree is 1+2+1=4. | 1,900 | true | false | true | true | false | false | false | false | false | true | 5,873 |
747B | The process of mammoth's genome decoding in Berland comes to its end! One of the few remaining tasks is to restore unrecognized nucleotides in a found chain _s_. Each nucleotide is coded with a capital letter of English alphabet: 'A', 'C', 'G' or 'T'. Unrecognized nucleotides are coded by a question mark '?'. Thus, _s_ is a string consisting of letters 'A', 'C', 'G', 'T' and characters '?'. It is known that the number of nucleotides of each of the four types in the decoded genome of mammoth in Berland should be equal. Your task is to decode the genome and replace each unrecognized nucleotide with one of the four types so that the number of nucleotides of each of the four types becomes equal. Input The first line contains the integer _n_ (4u2009≤u2009_n_u2009≤u2009255)xa0— the length of the genome. The second line contains the string _s_ of length _n_xa0— the coded genome. It consists of characters 'A', 'C', 'G', 'T' and '?'. Output If it is possible to decode the genome, print it. If there are multiple answer, print any of them. If it is not possible, print three equals signs in a row: "===" (without quotes). Note In the first example you can replace the first question mark with the letter 'A', the second question mark with the letter 'G', the third question mark with the letter 'T', then each nucleotide in the genome would be presented twice. In the second example the genome is already decoded correctly and each nucleotide is exactly once in it. In the third and the fourth examples it is impossible to decode the genom. | 900 | false | false | true | false | false | false | false | false | false | false | 6,809 |
757D | The gym leaders were fascinated by the evolutions which took place at Felicity camp. So, they were curious to know about the secret behind evolving Pokemon. The organizers of the camp gave the gym leaders a PokeBlock, a sequence of _n_ ingredients. Each ingredient can be of type 0 or 1. Now the organizers told the gym leaders that to evolve a Pokemon of type _k_ (_k_u2009≥u20092), they need to make a valid set of _k_ cuts on the PokeBlock to get smaller blocks. Suppose the given PokeBlock sequence is _b_0_b_1_b_2... _b__n_u2009-u20091. You have a choice of making cuts at _n_u2009+u20091 places, i.e., Before _b_0, between _b_0 and _b_1, between _b_1 and _b_2, ..., between _b__n_u2009-u20092 and _b__n_u2009-u20091, and after _b__n_u2009-u20091. The _n_u2009+u20091 choices of making cuts are as follows (where a denotes a possible cut): _b_0 _b_1 _b_2 ... _b__n_u2009-u20092 _b__n_u2009-u20091 Consider a sequence of _k_ cuts. Now each pair of consecutive cuts will contain a binary string between them, formed from the ingredient types. The ingredients before the first cut and after the last cut are wasted, which is to say they are not considered. So there will be exactly _k_u2009-u20091 such binary substrings. Every substring can be read as a binary number. Let _m_ be the maximum number out of the obtained numbers. If all the obtained numbers are positive and the set of the obtained numbers contains all integers from 1 to _m_, then this set of cuts is said to be a valid set of cuts. For example, suppose the given PokeBlock sequence is 101101001110 and we made 5 cuts in the following way: 10 11 010 01 1 10 So the 4 binary substrings obtained are: 11, 010, 01 and 1, which correspond to the numbers 3, 2, 1 and 1 respectively. Here _m_u2009=u20093, as it is the maximum value among the obtained numbers. And all the obtained numbers are positive and we have obtained all integers from 1 to _m_. Hence this set of cuts is a valid set of 5 cuts. A Pokemon of type _k_ will evolve only if the PokeBlock is cut using a valid set of _k_ cuts. There can be many valid sets of the same size. Two valid sets of _k_ cuts are considered different if there is a cut in one set which is not there in the other set. Let _f_(_k_) denote the number of valid sets of _k_ cuts. Find the value of . Since the value of _s_ can be very large, output _s_ modulo 109u2009+u20097. Note In the first sample, the sets of valid cuts are: Size 2: 1011, 1011, 1011, 1011. Size 3: 1011, 1011, 1011, 1011. Size 4: 1011, 1011. Hence, _f_(2)u2009=u20094, _f_(3)u2009=u20094 and _f_(4)u2009=u20092. So, the value of _s_u2009=u200910. In the second sample, the set of valid cuts is: Size 2: 10. Hence, _f_(2)u2009=u20091 and _f_(3)u2009=u20090. So, the value of _s_u2009=u20091. | 2,200 | false | false | false | true | false | false | false | false | false | false | 6,761 |
30A | Problem - 30A - 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 math *1400 No tag edit access → Contest materials ") . King wants to show financial stability. To do this, he needs to find common coefficient _X_ — the coefficient of income growth during one year. This coefficient should satisfy the equation: _A_·_X__n_u2009=u2009_B_. Surely, the king is not going to do this job by himself, and demands you to find such number _X_. It is necessary to point out that the fractional numbers are not used in kingdom's economy. That's why all input numbers as well as coefficient _X_ must be integers. The number _X_ may be zero or negative. Input The input contains three integers _A_, _B_, _n_ (_A_,u2009_B_u2009≤u20091000, 1u2009≤u2009_n_u2009≤u200910). Output Output the required integer coefficient _X_, or «No solution», if such a coefficient does not exist or it is fractional. If there are several possible solutions, output any of them. Examples Input 2 18 2 Output 3 Input -1 8 3 Output -2 Input 0 0 10 Output 5 Input 1 16 5 Output No solution | 1,400 | true | false | false | false | false | false | true | false | false | false | 9,848 |
1717C | Given an array of integer $$$a_1, a_2, ldots, a_n$$$. In one operation you can make $$$a_i := a_i + 1$$$ if $$$i < n$$$ and $$$a_i leq a_{i + 1}$$$, or $$$i = n$$$ and $$$a_i leq a_1$$$. You need to check whether the array $$$a_1, a_2, ldots, a_n$$$ can become equal to the array $$$b_1, b_2, ldots, b_n$$$ in some number of operations (possibly, zero). Two arrays $$$a$$$ and $$$b$$$ of length $$$n$$$ are called equal if $$$a_i = b_i$$$ for all integers $$$i$$$ from $$$1$$$ to $$$n$$$. Input The input consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 4 cdot 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$$$ ($$$2 le n le 2 cdot 10^5$$$)xa0– the length of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, ldots, a_n$$$ ($$$1 le a_i le 10^9$$$)xa0– the elements of the array $$$a$$$. The third line of each test case contains $$$n$$$ integers $$$b_1, ldots, b_n$$$ ($$$1 le b_i le 10^9$$$)xa0– the elements of the array $$$b$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case, output "YES" if you can get the array $$$b$$$, otherwise output "NO". You may print each letter in any case (for example, "YES", "Yes", "yes", "yEs" will all be recognized as positive answer). Example Input 5 3 1 2 5 1 2 5 2 2 2 1 3 4 3 4 1 2 6 4 2 5 3 2 4 1 4 5 3 5 1 2 3 4 5 6 5 6 7 6 Note In the first test case, the array $$$a$$$ is already equal to the array $$$b$$$. In the second test case, we can't get the array $$$b$$$, because to do this we need to decrease $$$a_1$$$. In the fifth test case, we can apply operations in order to the elements with indices $$$4, 3, 3,2,2,2,1,1,1,1$$$, and then get the array $$$[5,5,5,5,5]$$$. After that, you can apply operations in order to elements with indices $$$5,4,4,3,1$$$ and already get an array $$$[6,5,6,7,6]$$$. | 1,300 | false | true | false | false | false | false | false | false | false | false | 1,979 |
482A | Problem - 482A - 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 greedy *1200 No tag edit access → Contest materials . Output Print _n_ integers forming the permutation. If there are multiple answers, print any of them. Examples Input 3 2 Output 1 3 2 Input 3 1 Output 1 2 3 Input 5 2 Output 1 3 2 4 5 Note By _x_ we denote the absolute value of number _x_. | 1,200 | false | true | false | false | false | true | false | false | false | false | 7,909 |
157B | One day, as Sherlock Holmes was tracking down one very important criminal, he found a wonderful painting on the wall. This wall could be represented as a plane. The painting had several concentric circles that divided the wall into several parts. Some parts were painted red and all the other were painted blue. Besides, any two neighboring parts were painted different colors, that is, the red and the blue color were alternating, i. e. followed one after the other. The outer area of the wall (the area that lied outside all circles) was painted blue. Help Sherlock Holmes determine the total area of red parts of the wall. Let us remind you that two circles are called concentric if their centers coincide. Several circles are called concentric if any two of them are concentric. Input The first line contains the single integer _n_ (1u2009≤u2009_n_u2009≤u2009100). The second line contains _n_ space-separated integers _r__i_ (1u2009≤u2009_r__i_u2009≤u20091000) — the circles' radii. It is guaranteed that all circles are different. Output Print the single real number — total area of the part of the wall that is painted red. The answer is accepted if absolute or relative error doesn't exceed 10u2009-u20094. Note In the first sample the picture is just one circle of radius 1. Inner part of the circle is painted red. The area of the red part equals πu2009×u200912u2009=u2009π. In the second sample there are three circles of radii 1, 4 and 2. Outside part of the second circle is painted blue. Part between the second and the third circles is painted red. Part between the first and the third is painted blue. And, finally, the inner part of the first circle is painted red. Overall there are two red parts: the ring between the second and the third circles and the inner part of the first circle. Total area of the red parts is equal (πu2009×u200942u2009-u2009πu2009×u200922)u2009+u2009πu2009×u200912u2009=u2009πu2009×u200912u2009+u2009πu2009=u200913π | 1,000 | false | false | false | false | false | false | false | false | true | false | 9,246 |
1514A | Problem - 1514A - 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 number theory *800 No tag edit access → Contest materials ") elements. Input The first line contains an integer $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, $$$ldots$$$, $$$a_{n}$$$ ($$$1 le a_i le 10^4$$$)xa0— the elements of the array $$$a$$$. Output If there's a subsequence of $$$a$$$ whose product isn't a perfect square, print "YES". Otherwise, print "NO". Example Input 2 3 1 5 4 2 100 10000 Output YES NO Note In the first example, the product of the whole array ($$$20$$$) isn't a perfect square. In the second example, all subsequences have a perfect square product. | 800 | true | false | false | false | false | false | false | false | false | false | 3,096 |
1945H | In the evenings during the hike, Kirill and Anton decided to take out an array of integers $$$a$$$ of length $$$n$$$ from their backpack and play a game with it. The rules are as follows: 1. Kirill chooses from $$$2$$$ to $$$(n-2)$$$ numbers and encircles them in red. 2. Anton encircles all the remaining numbers in blue. 3. Kirill calculates the greatest common divisor (xa0— the number of test cases. Then follows the description of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$4le n le 4cdot 10^5$$$, $$$0 le x le 4cdot 10^5$$$)xa0— the number of integers and the number $$$x$$$ respectively. The second line contains an array $$$a$$$ of length $$$n$$$ ($$$1 le a_i le 4cdot 10^5$$$). It is guaranteed that the sum of $$$n$$$ for all test cases does not exceed $$$4cdot 10^5$$$. It is also guaranteed that the sum of the maximum values of $$$a_i$$$ for each test case does not exceed $$$4cdot 10^5$$$. Output For each test case, output "YES" on the first line if the condition can be met, on the second line, output the number of chosen numbers by Kirill and the numbers themselves in any order separated by a space, and on the third line, output the size of the second set and the numbers in it. Otherwise, output "NO". You can output each letter in any case (lowercase or uppercase). For example, the strings "yEs", "yes", "Yes", and "YES" will be accepted as a positive answer. Example Input 8 4 1 4 3 1 8 4 1 4 5 8 4 5 0 1 1 1 1 1 5 2 31 63 127 63 31 4 1 1 3 3 3 8 3 4 3 4 1 2 2 5 3 4 2 1 4 3 6 8 48 31 61 37 15 53 26 61 12 Output YES 2 4 8 2 3 1 YES 2 4 4 2 5 8 NO YES 2 63 63 3 31 127 31 YES 2 3 3 2 1 3 YES 2 4 4 6 3 1 2 2 5 3 YES 2 3 6 2 1 4 YES 2 61 61 6 31 37 15 53 26 12 | 2,600 | true | false | false | false | true | false | true | false | false | false | 606 |
1790C | A sequence of $$$n$$$ numbers is called permutation if it contains all integers from $$$1$$$ to $$$n$$$ exactly once. For example, the sequences [$$$3, 1, 4, 2$$$], [$$$1$$$] and [$$$2,1$$$] are permutations, but [$$$1,2,1$$$], [$$$0,1$$$] and [$$$1,3,4$$$]xa0— are not. Kristina had a permutation $$$p$$$ of $$$n$$$ elements. She wrote it on the whiteboard $$$n$$$ times in such a way that: while writing the permutation at the $$$i$$$-th ($$$1 le i le n)$$$ time she skipped the element $$$p_i$$$ So, she wrote in total $$$n$$$ sequences of length $$$n-1$$$ each. For example, suppose Kristina had a permutation $$$p$$$ = $$$[4,2,1,3]$$$ of length $$$4$$$. Then she did the following: 1. Wrote the sequence $$$[2, 1, 3]$$$, skipping the element $$$p_1=4$$$ from the original permutation. 2. Wrote the sequence $$$[4, 1, 3]$$$, skipping the element $$$p_2=2$$$ from the original permutation. 3. Wrote the sequence $$$[4, 2, 3]$$$, skipping the element $$$p_3=1$$$ from the original permutation. 4. Wrote the sequence $$$[4, 2, 1]$$$, skipping the element $$$p_4=3$$$ from the original permutation. You know all $$$n$$$ of sequences that have been written on the whiteboard, but you do not know the order in which they were written. They are given in arbitrary order. Reconstruct the original permutation from them. For example, if you know the sequences $$$[4, 2, 1]$$$, $$$[4, 2, 3]$$$, $$$[2, 1, 3]$$$, $$$[4, 1, 3]$$$, then the original permutation will be $$$p$$$ = $$$[4, 2, 1, 3]$$$. Input The first line of input data contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. The description of the test cases follows. The first line of each test case contains one integer $$$n$$$ ($$$3 le n le 100$$$). This is followed by $$$n$$$ lines, each containing exactly $$$n-1$$$ integers and describing one of the sequences written out on the whiteboard. It is guaranteed that all sequences could be obtained from some permutation $$$p$$$, and that the sum $$$n^2$$$ over all input sets does not exceed $$$2 cdot 10^5$$$. Output For each test case, output on a separate line a permutation $$$p$$$ such that the given $$$n$$$ sequences could be obtained from it. It is guaranteed that the answer exists and it is the only one. In other words, for each test case the required permutation is sure to exist. | 1,000 | true | false | true | false | false | false | true | false | false | false | 1,540 |
1090G | , # Problem A. Combostone Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 512 megabytes One big company is developing a computer game named “Combostone”, that should blast the market. The rules of the game are quite complicated, so implementation of the server engine, which should adhere to the rules, was outsourced. You are asked to implement such server engine. The players of the game are able to summon creatures to the arena, and then they can improve these creatures. Every creature has two parameters — an integer value of its attack a and an integer value of its remaining health points h. Let us denote parameters of a creature as (a, h ). There are no creatures in the arena at the beginning of the game. Players are able to use the following spells: • Summon Creature : Summon new creature with parameters (1 , 1) . If k creatures have already been summoned, the new creature gets an id k + 1 . • Blessed Champion : Double the attack of the chosen creature. If the creature have had parameters (a, h ) before this spell, after the spell the creature has parameters (2 a, h ). • Divine Spirit : Double the health points of the chosen creature. If the creature have had parameters (a, h ) before this spell, after the spell the creature has parameters (a, 2h). • Molten Reflection : Summon the new creature, which has the same parameters as the chosen creature. If k creatures have already been summoned in the game, the new creature gets an id k + 1 . • Attack! : Make a fight between two different creatures. In the fight both creatures at the same time make one hit to the enemy. This hit decreases opponent’s health points by the attack value of the creature. In other words, after the fight between creatures with parameters (a1, h 1) and (a2, h 2) their parameters become (a1, h 1 − a2) and (a2, h 2 − a1), respectively. If after the fight the creature is left with 0 or less health points, it dies and can’t participate in following actions of the game. The server engine that you should implement must be able to process all the events, and for every summoned creature it must determine the number of the turn when this creature dies, or find out that it is still alive at the end of the game. Also, the engine should correctly process the cases when the player tries to interact with the dead creatures: if the spell Blessed Champion , Divine Spirit or Attack! is applied to the dead creature, nothing should happen. If the spell Molten Reflection is applied to the dead creature, a new creature with the same parameters is summoned, but it is assumed to be killed at the turn of its creation. # Input The first line of input contains an integer n — the number of turns in the game ( 1 ≤ n ≤ 250 000 ). The next n lines contain turn descriptions in the following format: • 1 — apply a spell Summon Creature ; • 2 i — apply a spell Blessed Champion to the creature with id i; • 3 i — apply a spell Divine Spirit to the creature with id i; • 4 i — apply a spell Molten Reflection to the creature with id i; • 5 i j — apply a spell Attack! to the creatures with ids i and j.It is guaranteed that any creatures mentioned in the queries have already been summoned at the time of the query, but they can be already dead. Page 1 of 2 , # Output The first line of output must contain one integer k — the number of creatures summoned in the game. The next line must contain k integers t1, t 2, . . . , t k. If the creature with id i is alive at the end of the game, ti should be equal to −1, otherwise ti should be equal to the number of the turn, when this creature was killed. # Example standard input standard output 16 12 1 3 1 15 1 2 3 1 13 3 3 3 4 1 5 1 3 3 3 5 1 3 5 4 3 5 4 3 4 1 513 5 14 -1 16 # Note The table below shows how the parameters of the creatures changed in the sample test. turn 1 2 3 4 50 - - - - -1 (1, 1) - - - -2 (2, 1) - - - -3 (2, 2) - - - -4 (2, 2) (1, 1) - - -5 (2, 1) dead - - -6 (2, 2) dead - - -7 (2, 2) dead (1, 1) - -8 (2, 2) dead (1, 2) - -9 (2, 2) dead (1, 4) - -10 (2, 2) dead (1, 4) (2, 2) -11 (2, 1) dead (1, 2) (2, 2) -12 (2, 1) dead (1, 4) (2, 2) -13 dead dead (1, 2) (2, 2) -14 dead dead dead (2, 1) -15 dead dead dead (2, 1) -16 dead dead dead (2, 1) dead Page 2 of 2 | 2,500 | false | false | true | false | false | false | false | false | false | false | 5,266 |
1036C | Problem - 1036C - 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 combinatorics dp *1900 No tag edit access → Contest materials — the number of segments in a testcase. Each of the next $$$T$$$ lines contains two integers $$$L_i$$$ and $$$R_i$$$ ($$$1 le L_i le R_i le 10^{18}$$$). Output Print $$$T$$$ lines — the $$$i$$$-th line should contain the number of classy integers on a segment $$$ | 1,900 | false | false | false | true | false | false | false | false | false | false | 5,521 |
2A | The winner of the card game popular in Berland "Berlogging" is determined according to the following rules. If at the end of the game there is only one player with the maximum number of points, he is the winner. The situation becomes more difficult if the number of such players is more than one. During each round a player gains or loses a particular number of points. In the course of the game the number of points is registered in the line "name score", where name is a player's name, and score is the number of points gained in this round, which is an integer number. If score is negative, this means that the player has lost in the round. So, if two or more players have the maximum number of points (say, it equals to _m_) at the end of the game, than wins the one of them who scored at least _m_ points first. Initially each player has 0 points. It's guaranteed that at the end of the game at least one player has a positive number of points. Input The first line contains an integer number _n_ (1u2009u2009≤u2009u2009_n_u2009u2009≤u2009u20091000), _n_ is the number of rounds played. Then follow _n_ lines, containing the information about the rounds in "name score" format in chronological order, where name is a string of lower-case Latin letters with the length from 1 to 32, and score is an integer number between -1000 and 1000, inclusive. Output Print the name of the winner. Examples Input 3 mike 3 andrew 5 mike 2 Input 3 andrew 3 andrew 2 mike 5 | 1,500 | false | false | true | false | false | false | false | false | false | false | 9,981 |
34A | Problem - 34A - 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 ") Разбор") — amount of soldiers. Then follow the heights of the soldiers in their order in the circle — _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20091000). The soldier heights are given in clockwise or counterclockwise direction. Output Output two integers — indexes of neighbouring soldiers, who should form a reconnaissance unit. If there are many optimum solutions, output any of them. Remember, that the soldiers stand in a circle. Examples Input 5 10 12 13 15 10 Output 5 1 Input 4 10 20 30 40 Output 1 2 | 800 | false | false | true | false | false | false | false | false | false | false | 9,828 |
221A | Problem - 221A - 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 math *1000 No tag edit access → Contest materials , that sorts the first _x_ permutation's elements, works as follows: If _x_u2009=u20091, exit the function. Otherwise, call _f_(_x_u2009-u20091), and then make _swap_(_a__x_u2009-u20091,u2009_a__x_) (swap the _x_-th and (_x_u2009-u20091)-th elements of _a_). The Little Elephant's teacher believes that this function does not work correctly. But that-be do not get an F, the Little Elephant wants to show the performance of its function. Help him, find a permutation of numbers from 1 to _n_, such that after performing the Little Elephant's function (that is call _f_(_n_)), the permutation will be sorted in ascending order. Input A single line contains integer _n_ (1u2009≤u2009_n_u2009≤u20091000) — the size of permutation. Output In a single line print _n_ distinct integers from 1 to _n_ — the required permutation. Numbers in a line should be separated by spaces. It is guaranteed that the answer exists. Examples Input 1 Output 1 Input 2 Output 2 1 | 1,000 | true | false | true | false | false | false | false | false | false | false | 8,954 |
1450H2 | The only difference between the two versions of the problem is that there are no updates in the easy version. There are $$$n$$$ spools of thread placed on the rim of a circular table. The spools come in two types of thread: the first thread is black and the second thread is white. For any two spools of the same color, you can attach them with a thread of that color in a straight line segment. Define a matching as a way to attach spools together so that each spool is attached to exactly one other spool. Coloring is an assignment of colors (white and black) to the spools. A coloring is called valid if it has at least one matching. That is if the number of black spools and the number of white spools are both even. Given a matching, we can find the number of times some white thread intersects some black thread. We compute the number of pairs of differently colored threads that intersect instead of the number of intersection points, so one intersection point may be counted multiple times if different pairs of threads intersect at the same point. If $$$c$$$ is a valid coloring, let $$$f(c)$$$ denote the minimum number of such intersections out of all possible matchings. The circle above is described by the coloring bwbbbwww. After matching the spools as shown, there is one intersection between differently colored threads. It can be proven that it is the minimum possible, so $$$f( ext{bwbbbwww}) = 1$$$. You are given a string $$$s$$$ representing an unfinished coloring, with black, white, and uncolored spools. A coloring $$$c$$$ is called $$$s$$$-reachable if you can achieve it by assigning colors to the uncolored spools of $$$s$$$ without changing the others. A coloring $$$c$$$ is chosen uniformly at random among all valid, $$$s$$$-reachable colorings. Compute the $$$. You should find it by modulo $$$998244353$$$. There will be $$$m$$$ updates to change one character of $$$s$$$. After each update, you should again compute the expected value of $$$f(c)$$$. We can show that each answer can be written in the form $$$frac{p}{q}$$$ where $$$p$$$ and $$$q$$$ are relatively prime integers and $$$q otequiv 0pmod{998244353}$$$. The answer by modulo $$$998244353$$$ is equal to $$$(pcdot q^{-1})$$$ modulo $$$998244353$$$. Input The first line contains two integers $$$n$$$, $$$m$$$ ($$$2le nle 2cdot 10^5$$$, $$$n$$$ is even, $$$0le mle 2cdot 10^5$$$) — the number of spools and the number of updates, respectively. The second line contains a string $$$s$$$ of length $$$n$$$ — the unfinished coloring of the spools. The $$$i$$$-th character will be 'w', 'b', or '?', describing if the $$$i$$$-th spool is white, black, or uncolored, respectively. Each of the next $$$m$$$ lines contains an integer $$$i$$$ ($$$1 leq i leq n$$$) — the position of the character in $$$s$$$ to be updated, and a character $$$c$$$ ($$$c in { ext{w}, ext{b}, ext{?}}$$$) — the new color of the spool $$$i$$$ after the update. It is guaranteed there exists at least one uncolored spool initially and after each update. Note The first test corresponds closely to the image. Coloring '?' as 'w' does not create a valid coloring because the number of black spools is odd. Then the only reachable valid coloring is 'bwbbbwww' and $$$f( ext{bwbbbwww}) = 1$$$, so the expected value is $$$1$$$. In the second test, the string after each update is: 1. ????w?wb?? 2. ??????wb?? 3. ?w????wb?? In the third test, the string after each update is: 1. ww?b 2. wb?b 3. wb?b | 3,300 | true | false | true | false | false | false | false | false | false | false | 3,429 |
49C | Problem - 49C - 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 *1700 No tag edit access → Contest materials ") is true both: _j_ _mod_ _i_u2009=u20090 and at the same time _p_(_j_) _mod_ _i_u2009=u20090, where _p_(_j_) is the number of the tome that stands on the _j_-th place and _mod_ is the operation of taking the division remainder. Naturally, one volume can occupy exactly one place and in one place can stand exactly one volume. Help Vasya — find the volume disposition with the minimum number of divisors. Input The first line contains number _n_ (1u2009≤u2009_n_u2009≤u2009100000) which represents the number of volumes and free places. Output Print _n_ numbers — the sought disposition with the minimum divisor number. The _j_-th number (1u2009≤u2009_j_u2009≤u2009_n_) should be equal to _p_(_j_) — the number of tome that stands on the _j_-th place. If there are several solutions, print any of them. Examples Input 2 Output 2 1 Input 3 Output 1 3 2 | 1,700 | true | false | false | false | false | true | false | false | false | false | 9,727 |
1285F | Problem - 1285F - 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 combinatorics number theory *2900 No tag edit access → Contest materials ") Editorial") ,$$$$$$ where $$$LCM(x, y)$$$ is the smallest positive integer that is divisible by both $$$x$$$ and $$$y$$$. For example, $$$LCM(6, 8) = 24$$$, $$$LCM(4, 12) = 12$$$, $$$LCM(2, 3) = 6$$$. Input The first line contains an integer $$$n$$$ ($$$2 le n le 10^5$$$)xa0— the number of elements in the array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^5$$$)xa0— the elements of the array $$$a$$$. Output Print one integer, the maximum value of the least common multiple of two elements in the array $$$a$$$. Examples Input 3 13 35 77 Output 1001 Input 6 1 2 4 8 16 32 Output 32 | 2,900 | false | false | false | false | false | false | false | true | false | false | 4,262 |
582B | Problem - 582B - 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 dp matrices *1900 No tag edit access → Contest materials . The second line contains _n_ space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u2009300). Output Print a single number — the length of a sought sequence. Examples Input 4 3 3 1 4 2 Output 5 Note The array given in the sample looks like that: 3, 1, 4, 2, 3, 1, 4, 2, 3, 1, 4, 2. The elements in bold form the largest non-decreasing subsequence. | 1,900 | false | false | false | true | false | true | false | false | false | false | 7,516 |
1381D | There is an undirected tree of $$$n$$$ vertices, connected by $$$n-1$$$ bidirectional edges. There is also a snake stuck inside of this tree. Its head is at vertex $$$a$$$ and its tail is at vertex $$$b$$$. The snake's body occupies all vertices on the unique simple path between $$$a$$$ and $$$b$$$. The snake wants to know if it can reverse itself xa0— that is, to move its head to where its tail started, and its tail to where its head started. Unfortunately, the snake's movements are restricted to the tree's structure. In an operation, the snake can move its head to an adjacent vertex not currently occupied by the snake. When it does this, the tail moves one vertex closer to the head, so that the length of the snake remains unchanged. Similarly, the snake can also move its tail to an adjacent vertex not currently occupied by the snake. When it does this, the head moves one unit closer to the tail. Let's denote a snake position by $$$(h,t)$$$, where $$$h$$$ is the index of the vertex with the snake's head, $$$t$$$ is the index of the vertex with the snake's tail. This snake can reverse itself with the movements $$$(4,7) o (5,1) o (4,2) o (1, 3) o (7,2) o (8,1) o (7,4)$$$. Determine if it is possible to reverse the snake with some sequence of operations. Input The first line contains a single integer $$$t$$$ ($$$1le tle 100$$$) xa0— the number of test cases. The next lines contain descriptions of test cases. The first line of each test case contains three integers $$$n,a,b$$$ ($$$2le nle 10^5,1le a,ble n,a e b$$$). Each of the next $$$n-1$$$ lines contains two integers $$$u_i,v_i$$$ ($$$1le u_i,v_ile n,u_i e v_i$$$), indicating an edge between vertices $$$u_i$$$ and $$$v_i$$$. It is guaranteed that the given edges form a tree. It is guaranteed that the sum of $$$n$$$ across all test cases does not exceed $$$10^5$$$. Output For each test case, output "YES" if it is possible for the snake to reverse itself, or "NO" otherwise. Example Input 4 8 4 7 1 2 2 3 1 4 4 5 4 6 1 7 7 8 4 3 2 4 3 1 2 2 3 9 3 5 1 2 2 3 3 4 1 5 5 6 6 7 1 8 8 9 16 15 12 1 2 2 3 1 4 4 5 5 6 6 7 4 8 8 9 8 10 10 11 11 12 11 13 13 14 10 15 15 16 Note The first test case is pictured above. In the second test case, the tree is a path. We can show that the snake cannot reverse itself. In the third test case, we can show that the snake cannot reverse itself. In the fourth test case, an example solution is: $$$(15,12) o (16,11) o (15,13) o (10,14) o (8,13) o (4,11) o (1,10)$$$ $$$ o (2,8) o (3,4) o (2,5) o (1,6) o (4,7) o (8,6) o (10,5)$$$ $$$ o (11,4) o (13,8) o (14,10) o (13,15) o (11,16) o (12,15)$$$. | 3,000 | false | true | false | true | false | false | false | false | false | false | 3,760 |
1143A | Three years have passes and nothing changed. It is still raining in London, and Mr. Black has to close all the doors in his home in order to not be flooded. Once, however, Mr. Black became so nervous that he opened one door, then another, then one more and so on until he opened all the doors in his house. There are exactly two exits from Mr. Black's house, let's name them left and right exits. There are several doors in each of the exits, so each door in Mr. Black's house is located either in the left or in the right exit. You know where each door is located. Initially all the doors are closed. Mr. Black can exit the house if and only if all doors in at least one of the exits is open. You are given a sequence in which Mr. Black opened the doors, please find the smallest index $$$k$$$ such that Mr. Black can exit the house after opening the first $$$k$$$ doors. We have to note that Mr. Black opened each door at most once, and in the end all doors became open. Input The first line contains integer $$$n$$$ ($$$2 le n le 200,000$$$)xa0— the number of doors. The next line contains $$$n$$$ integers: the sequence in which Mr. Black opened the doors. The $$$i$$$-th of these integers is equal to $$$0$$$ in case the $$$i$$$-th opened door is located in the left exit, and it is equal to $$$1$$$ in case it is in the right exit. It is guaranteed that there is at least one door located in the left exit and there is at least one door located in the right exit. Output Print the smallest integer $$$k$$$ such that after Mr. Black opened the first $$$k$$$ doors, he was able to exit the house. Note In the first example the first two doors are from the left exit, so when Mr. Black opened both of them only, there were two more closed door in the left exit and one closed door in the right exit. So Mr. Black wasn't able to exit at that moment. When he opened the third door, all doors from the right exit became open, so Mr. Black was able to exit the house. In the second example when the first two doors were opened, there was open closed door in each of the exit. With three doors opened Mr. Black was able to use the left exit. | 800 | false | false | true | false | false | false | false | false | false | false | 5,008 |
1528F | To AmShZ, all arrays are equal, but some arrays are more-equal than others. Specifically, the arrays consisting of $$$n$$$ elements from $$$1$$$ to $$$n$$$ that can be turned into permutations of numbers from $$$1$$$ to $$$n$$$ by adding a non-negative integer to each element. Mashtali who wants to appear in every problem statement thinks that an array $$$b$$$ consisting of $$$k$$$ elements is compatible with a more-equal array $$$a$$$ consisting of $$$n$$$ elements if for each $$$1 le i le k$$$ we have $$$1 le b_i le n$$$ and also $$$a_{b_1} = a_{b_2} = ldots = a_{b_k}$$$. Find the number of pairs of arrays $$$a$$$ and $$$b$$$ such that $$$a$$$ is a more-equal array consisting of $$$n$$$ elements and $$$b$$$ is an array compatible with $$$a$$$ consisting of $$$k$$$ elements modulo $$$998244353$$$. Note that the elements of $$$b$$$ are not necessarily distinct, same holds for $$$a$$$. Input The first line of input contains two integers $$$n$$$ and $$$k$$$ $$$(1 le n le 10^9 , 1 le k le 10^5)$$$. Output Print a single integer — the answer to the problem modulo $$$998244353$$$. Note There are eight possible pairs for the second example: 1. $$$a = {1, 1}, b = {1, 1}$$$ 2. $$$a = {1, 1}, b = {1, 2}$$$ 3. $$$a = {1, 1}, b = {2, 1}$$$ 4. $$$a = {1, 1}, b = {2, 2}$$$ 5. $$$a = {1, 2}, b = {1, 1}$$$ 6. $$$a = {1, 2}, b = {2, 2}$$$ 7. $$$a = {2, 1}, b = {1, 1}$$$ 8. $$$a = {2, 1}, b = {2, 2}$$$ | 3,300 | true | false | false | false | false | false | false | false | false | false | 3,018 |
929C | Statement is not available on English language Завтра у хоккейной команды, которой руководит Евгений, важный матч. Евгению нужно выбрать шесть игроков, которые выйдут на лед в стартовом составе: один вратарь, два защитника и три нападающих. Так как это стартовый состав, Евгения больше волнует, насколько красива будет команда на льду, чем способности игроков. А именно, Евгений хочет выбрать такой стартовый состав, чтобы номера любых двух игроков из стартового состава отличались не более, чем в два раза. Например, игроки с номерами 13, 14, 10, 18, 15 и 20 устроят Евгения, а если, например, на лед выйдут игроки с номерами 8 и 17, то это не устроит Евгения. Про каждого из игроков вам известно, на какой позиции он играет (вратарь, защитник или нападающий), а также его номер. В хоккее номера игроков не обязательно идут подряд. Посчитайте число различных стартовых составов из одного вратаря, двух защитников и трех нападающих, которые может выбрать Евгений, чтобы выполнялось его условие красоты. Входные данные Первая строка содержит три целых числа _g_, _d_ и _f_ (1u2009≤u2009_g_u2009≤u20091u2009000, 1u2009≤u2009_d_u2009≤u20091u2009000, 1u2009≤u2009_f_u2009≤u20091u2009000)xa0— число вратарей, защитников и нападающих в команде Евгения. Вторая строка содержит _g_ целых чисел, каждое в пределах от 1 до 100u2009000xa0— номера вратарей. Третья строка содержит _d_ целых чисел, каждое в пределах от 1 до 100u2009000xa0— номера защитников. Четвертая строка содержит _f_ целых чисел, каждое в пределах от 1 до 100u2009000xa0— номера нападающих. Гарантируется, что общее количество игроков не превосходит 1u2009000, т.xa0е. _g_u2009+u2009_d_u2009+u2009_f_u2009≤u20091u2009000. Все _g_u2009+u2009_d_u2009+u2009_f_ номеров игроков различны. Выходные данные Выведите одно целое числоxa0— количество возможных стартовых составов. Примеры Входные данные 1 2 3 15 10 19 20 11 13 Входные данные 2 3 4 16 40 20 12 19 13 21 11 10 Примечание В первом примере всего один вариант для выбора состава, который удовлетворяет описанным условиям, поэтому ответ 1. Во втором примере подходят следующие игровые сочетания (в порядке вратарь-защитник-защитник-нападающий-нападающий-нападающий): 16 20 12 13 21 11 16 20 12 13 11 10 16 20 19 13 21 11 16 20 19 13 11 10 16 12 19 13 21 11 16 12 19 13 11 10 Таким образом, ответ на этот пример — 6. | 1,700 | true | false | false | false | false | false | false | false | false | false | 5,988 |
1807G2 | The only difference between the two versions is that in this version, the constraints are higher. Initially, array $$$a$$$ contains just the number $$$1$$$. You can perform several operations in order to change the array. In an operation, you can select some subsequence$$$^{dagger}$$$ of $$$a$$$ and add into $$$a$$$ an element equal to the sum of all elements of the subsequence. You are given a final array $$$c$$$. Check if $$$c$$$ can be obtained from the initial array $$$a$$$ by performing some number (possibly 0) of operations on the initial array. $$$^{dagger}$$$ A sequence $$$b$$$ is a subsequence of a sequence $$$a$$$ if $$$b$$$ can be obtained from $$$a$$$ by the deletion of several (possibly zero, but not all) elements. In other words, select $$$k$$$ ($$$1 leq k leq a$$$) distinct indices $$$i_1, i_2, dots, i_k$$$ and insert anywhere into $$$a$$$ a new element with the value equal to $$$a_{i_1} + a_{i_2} + dots + a_{i_k}$$$. Input The first line of the input contains an integer $$$t$$$ ($$$1 leq t leq 1000$$$)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$$$ ($$$1 leq n leq 2 cdot 10^5$$$) xa0— the number of elements the final array $$$c$$$ should have. The second line of each test case contains $$$n$$$ space-separated integers $$$c_i$$$ ($$$1 leq c_i leq 2 cdot 10^5$$$) xa0— the elements of the final array $$$c$$$ that should be obtained from the initial array $$$a$$$. 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 "YES" (without quotes) if such a sequence of operations exists, and "NO" (without quotes) otherwise. You can output the answer in any case (for example, the strings "yEs", "yes", "Yes" and "YES" will be recognized as a positive answer). Example Input 6 1 1 1 2 5 5 1 3 2 1 5 7 1 5 2 1 3 1 1 1 5 1 1 4 2 1 Output YES NO YES NO YES YES Note For the first test case, the initial array $$$a$$$ is already equal to $$$[1]$$$, so the answer is "YES". For the second test case, performing any amount of operations will change $$$a$$$ to an array of size at least two which doesn't only have the element $$$2$$$, thus obtaining the array $$$[2]$$$ is impossible and the answer is "NO". For the third test case, we can perform the following operations in order to obtain the final given array $$$c$$$: Initially, $$$a = [1]$$$. By choosing the subsequence $$$[1]$$$, and inserting $$$1$$$ in the array, $$$a$$$ changes to $$$[1, 1]$$$. By choosing the subsequence $$$[1, 1]$$$, and inserting $$$1+1=2$$$ in the middle of the array, $$$a$$$ changes to $$$[1, 2, 1]$$$. By choosing the subsequence $$$[1, 2]$$$, and inserting $$$1+2=3$$$ after the first $$$1$$$ of the array, $$$a$$$ changes to $$$[1, 3, 2, 1]$$$. By choosing the subsequence $$$[1, 3, 1]$$$ and inserting $$$1+3+1=5$$$ at the beginning of the array, $$$a$$$ changes to $$$[5, 1, 3, 2, 1]$$$ (which is the array we needed to obtain). | 1,100 | false | true | true | true | false | false | false | false | true | false | 1,428 |
1440A | You are given four integers $$$n$$$, $$$c_0$$$, $$$c_1$$$ and $$$h$$$ and a binary string $$$s$$$ of length $$$n$$$. A binary string is a string consisting of characters $$$0$$$ and $$$1$$$. You can change any character of the string $$$s$$$ (the string should be still binary after the change). You should pay $$$h$$$ coins for each change. After some changes (possibly zero) you want to buy the string. To buy the string you should buy all its characters. To buy the character $$$0$$$ you should pay $$$c_0$$$ coins, to buy the character $$$1$$$ you should pay $$$c_1$$$ coins. Find the minimum number of coins needed to buy the string. Input The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10$$$)xa0— the number of test cases. Next $$$2t$$$ lines contain descriptions of test cases. The first line of the description of each test case contains four integers $$$n$$$, $$$c_{0}$$$, $$$c_{1}$$$, $$$h$$$ ($$$1 leq n, c_{0}, c_{1}, h leq 1000$$$). The second line of the description of each test case contains the binary string $$$s$$$ of length $$$n$$$. Output For each test case print a single integerxa0— the minimum number of coins needed to buy the string. Example Input 6 3 1 1 1 100 5 10 100 1 01010 5 10 1 1 11111 5 1 10 1 11111 12 2 1 10 101110110101 2 100 1 10 00 Note In the first test case, you can buy all characters and pay $$$3$$$ coins, because both characters $$$0$$$ and $$$1$$$ costs $$$1$$$ coin. In the second test case, you can firstly change $$$2$$$-nd and $$$4$$$-th symbols of the string from $$$1$$$ to $$$0$$$ and pay $$$2$$$ coins for that. Your string will be $$$00000$$$. After that, you can buy the string and pay $$$5 cdot 10 = 50$$$ coins for that. The total number of coins paid will be $$$2 + 50 = 52$$$. | 800 | true | false | true | false | false | false | false | false | false | false | 3,466 |
73A | Vasya plays The Elder Trolls IV: Oblivon. Oh, those creators of computer games! What they do not come up with! Absolutely unique monsters have been added to the The Elder Trolls IV: Oblivon. One of these monsters is Unkillable Slug. Why it is "Unkillable"? Firstly, because it can be killed with cutting weapon only, so lovers of two-handed amber hammers should find suitable knife themselves. Secondly, it is necessary to make so many cutting strokes to Unkillable Slug. Extremely many. Too many! Vasya has already promoted his character to 80-th level and in order to gain level 81 he was asked to kill Unkillable Slug. The monster has a very interesting shape. It looks like a rectangular parallelepiped with size _x_u2009×u2009_y_u2009×u2009_z_, consisting of undestructable cells 1u2009×u20091u2009×u20091. At one stroke Vasya can cut the Slug along an imaginary grid, i.e. cut with a plane parallel to one of the parallelepiped side. Monster dies when amount of parts it is divided reaches some critical value. All parts of monster do not fall after each cut, they remains exactly on its places. I. e. Vasya can cut several parts with one cut. Vasya wants to know what the maximum number of pieces he can cut the Unkillable Slug into striking him at most _k_ times. Vasya's character uses absolutely thin sword with infinite length. Input The first line of input contains four integer numbers _x_,u2009_y_,u2009_z_,u2009_k_ (1u2009≤u2009_x_,u2009_y_,u2009_z_u2009≤u2009106,u20090u2009≤u2009_k_u2009≤u2009109). Output Output the only number — the answer for the problem. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d). Note In the first sample Vasya make 3 pairwise perpendicular cuts. He cuts monster on two parts with the first cut, then he divides each part on two with the second cut, and finally he divides each of the 4 parts on two. | 1,600 | true | true | false | false | false | false | false | false | false | false | 9,602 |
1930B | You are given a positive integer $$$n$$$. Find a permutation$$$^dagger$$$ $$$p$$$ of length $$$n$$$ such that there do not exist two distinct indices $$$i$$$ and $$$j$$$ ($$$1 leq i, j < n$$$; $$$i eq j$$$) such that $$$p_i$$$ divides $$$p_j$$$ and $$$p_{i+1}$$$ divides $$$p_{j+1}$$$. Refer to the Notes section for some examples. Under the constraints of this problem, it can be proven that at least one $$$p$$$ exists. $$$^dagger$$$ A permutation of length $$$n$$$ is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$n=3$$$ but there is $$$4$$$ in the array). Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 10^3$$$)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$$$ ($$$3 leq n leq 10^5$$$)xa0— the length of the permutation $$$p$$$. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, output $$$p_1, p_2, ldots, p_n$$$. If there are multiple solutions, you may output any one of them. Note In the first test case, $$$p=[4,1,2,3]$$$ is a valid permutation. However, the permutation $$$p=[1,2,3,4]$$$ is not a valid permutation as we can choose $$$i=1$$$ and $$$j=3$$$. Then $$$p_1=1$$$ divides $$$p_3=3$$$ and $$$p_2=2$$$ divides $$$p_4=4$$$. Note that the permutation $$$p=[3, 4, 2, 1]$$$ is also not a valid permutation as we can choose $$$i=3$$$ and $$$j=2$$$. Then $$$p_3=2$$$ divides $$$p_2=4$$$ and $$$p_4=1$$$ divides $$$p_3=2$$$. In the second test case, $$$p=[1,2,3]$$$ is a valid permutation. In fact, all $$$6$$$ permutations of length $$$3$$$ are valid. | 1,000 | true | false | false | false | false | true | true | false | false | false | 710 |
1514C | Problem - 1514C - 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 number theory *1600 No tag edit access → Contest materials ") elements. The product of an empty subsequence is equal to $$$1$$$. Input The only line contains the integer $$$n$$$ ($$$2 le n le 10^5$$$). Output The first line should contain a single integer, the length of the longest subsequence. The second line should contain the elements of the subsequence, in increasing order. If there are multiple solutions, you can print any. Examples Input 5 Output 3 1 2 3 Input 8 Output 4 1 3 5 7 Note In the first example, the product of the elements is $$$6$$$ which is congruent to $$$1$$$ modulo $$$5$$$. The only longer subsequence is $$$ | 1,600 | false | true | false | false | false | false | false | false | false | false | 3,094 |
1646C | A number is called powerful if it is a power of two or a factorial. In other words, the number $$$m$$$ is powerful if there exists a non-negative integer $$$d$$$ such that $$$m=2^d$$$ or $$$m=d!$$$, where $$$d!=1cdot 2cdot ldots cdot d$$$ (in particular, $$$0! = 1$$$). For example $$$1$$$, $$$4$$$, and $$$6$$$ are powerful numbers, because $$$1=1!$$$, $$$4=2^2$$$, and $$$6=3!$$$ but $$$7$$$, $$$10$$$, or $$$18$$$ are not. You are given a positive integer $$$n$$$. Find the minimum number $$$k$$$ such that $$$n$$$ can be represented as the sum of $$$k$$$ distinct powerful numbers, or say that there is no such $$$k$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 100$$$). Description of the test cases follows. A test case consists of only one line, containing one integer $$$n$$$ ($$$1le nle 10^{12}$$$). Output For each test case print the answer on a separate line. If $$$n$$$ can not be represented as the sum of distinct powerful numbers, print $$$-1$$$. Otherwise, print a single positive integer xa0— the minimum possible value of $$$k$$$. Note In the first test case, $$$7$$$ can be represented as $$$7=1+6$$$, where $$$1$$$ and $$$6$$$ are powerful numbers. Because $$$7$$$ is not a powerful number, we know that the minimum possible value of $$$k$$$ in this case is $$$k=2$$$. In the second test case, a possible way to represent $$$11$$$ as the sum of three powerful numbers is $$$11=1+4+6$$$. We can show that there is no way to represent $$$11$$$ as the sum of two or less powerful numbers. In the third test case, $$$240$$$ can be represented as $$$240=24+32+64+120$$$. Observe that $$$240=120+120$$$ is not a valid representation, because the powerful numbers have to be distinct. In the fourth test case, $$$17179869184=2^{34}$$$, so $$$17179869184$$$ is a powerful number and the minimum $$$k$$$ in this case is $$$k=1$$$. | 1,500 | true | false | false | true | false | true | true | false | false | false | 2,393 |
191C | They say that Berland has exactly two problems, fools and roads. Besides, Berland has _n_ cities, populated by the fools and connected by the roads. All Berland roads are bidirectional. As there are many fools in Berland, between each pair of cities there is a path (or else the fools would get upset). Also, between each pair of cities there is no more than one simple path (or else the fools would get lost). But that is not the end of Berland's special features. In this country fools sometimes visit each other and thus spoil the roads. The fools aren't very smart, so they always use only the simple paths. A simple path is the path which goes through every Berland city not more than once. The Berland government knows the paths which the fools use. Help the government count for each road, how many distinct fools can go on it. Note how the fools' paths are given in the input. Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u2009105) — the number of cities. Each of the next _n_u2009-u20091 lines contains two space-separated integers _u__i_,u2009_v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_), that means that there is a road connecting cities _u__i_ and _v__i_. The next line contains integer _k_ (0u2009≤u2009_k_u2009≤u2009105) — the number of pairs of fools who visit each other. Next _k_ lines contain two space-separated numbers. The _i_-th line (_i_u2009>u20090) contains numbers _a__i_,u2009_b__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_). That means that the fool number 2_i_u2009-u20091 lives in city _a__i_ and visits the fool number 2_i_, who lives in city _b__i_. The given pairs describe simple paths, because between every pair of cities there is only one simple path. Output Print _n_u2009-u20091 integer. The integers should be separated by spaces. The _i_-th number should equal the number of fools who can go on the _i_-th road. The roads are numbered starting from one in the order, in which they occur in the input. Examples Input 5 1 2 1 3 2 4 2 5 2 1 4 3 5 Input 5 3 4 4 5 1 4 2 4 3 2 3 1 3 3 5 Note In the first sample the fool number one goes on the first and third road and the fool number 3 goes on the second, first and fourth ones. In the second sample, the fools number 1, 3 and 5 go on the first road, the fool number 5 will go on the second road, on the third road goes the fool number 3, and on the fourth one goes fool number 1. | 1,900 | false | false | false | false | true | false | false | false | false | false | 9,075 |
348C | You are given an array _a_1,u2009_a_2,u2009...,u2009_a__n_ and _m_ sets _S_1,u2009_S_2,u2009...,u2009_S__m_ of indices of elements of this array. Let's denote _S__k_u2009=u2009{_S__k_,u2009_i_}xa0(1u2009≤u2009_i_u2009≤u2009_S__k_). In other words, _S__k_,u2009_i_ is some element from set _S__k_. In this problem you have to answer _q_ queries of the two types: 1. Find the sum of elements with indices from set _S__k_: . The query format is "? k". 2. Add number _x_ to all elements at indices from set _S__k_: _a__S__k_,u2009_i_ is replaced by _a__S__k_,u2009_i_u2009+u2009_x_ for all _i_ (1u2009≤u2009_i_u2009≤u2009_S__k_). The query format is "+ k x". After each first type query print the required sum. Input The first line contains integers _n_,u2009_m_,u2009_q_ (1u2009≤u2009_n_,u2009_m_,u2009_q_u2009≤u2009105). The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (_a__i_u2009≤u2009108) — elements of array _a_. Each of the following _m_ lines describes one set of indices. The _k_-th line first contains a positive integer, representing the number of elements in set (_S__k_), then follow _S__k_ distinct integers _S__k_,u20091,u2009_S__k_,u20092,u2009...,u2009_S__k_,u2009_S__k_ (1u2009≤u2009_S__k_,u2009_i_u2009≤u2009_n_) — elements of set _S__k_. The next _q_ lines contain queries. Each query looks like either "? k" or "+ k x" and sits on a single line. For all queries the following limits are held: 1u2009≤u2009_k_u2009≤u2009_m_, _x_u2009≤u2009108. The queries are given in order they need to be answered. It is guaranteed that the sum of sizes of all sets _S__k_ doesn't exceed 105. Output After each first type query print the required sum on a single line. Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. | 2,500 | false | false | false | false | true | false | true | false | false | false | 8,440 |
1550B | You are given a string $$$s$$$ of length $$$n$$$ consisting only of the characters 0 and 1. You perform the following operation until the string becomes empty: choose some consecutive substring of equal characters, erase it from the string and glue the remaining two parts together (any of them can be empty) in the same order. For example, if you erase the substring 111 from the string 111110, you will get the string 110. When you delete a substring of length $$$l$$$, you get $$$a cdot l + b$$$ points. Your task is to calculate the maximum number of points that you can score in total, if you have to make the given string empty. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 2000$$$)xa0— the number of testcases. The first line of each testcase contains three integers $$$n$$$, $$$a$$$ and $$$b$$$ ($$$1 le n le 100; -100 le a, b le 100$$$)xa0— the length of the string $$$s$$$ and the parameters $$$a$$$ and $$$b$$$. The second line contains the string $$$s$$$. The string $$$s$$$ consists only of the characters 0 and 1. Output For each testcase, print a single integerxa0— the maximum number of points that you can score. Example Input 3 3 2 0 000 5 -2 5 11001 6 1 -4 100111 Note In the first example, it is enough to delete the entire string, then we will get $$$2 cdot 3 + 0 = 6$$$ points. In the second example, if we delete characters one by one, then for each deleted character we will get $$$(-2) cdot 1 + 5 = 3$$$ points, i.u2009e. $$$15$$$ points in total. In the third example, we can delete the substring 00 from the string 100111, we get $$$1 cdot 2 + (-4) = -2$$$ points, and the string will be equal to 1111, removing it entirely we get $$$1 cdot 4 + (-4) = 0$$$ points. In total, we got $$$-2$$$ points for $$$2$$$ operations. | 1,000 | true | true | false | false | false | false | false | false | false | false | 2,897 |
771E | Limak has a grid that consists of 2 rows and _n_ columns. The _j_-th cell in the _i_-th row contains an integer _t__i_,u2009_j_ which can be positive, negative or zero. A non-empty rectangle of cells is called nice if and only if the sum of numbers in its cells is equal to 0. Limak wants to choose some nice rectangles and give them to his friends, as gifts. No two chosen rectangles should share a cell. What is the maximum possible number of nice rectangles Limak can choose? Input The first line of the input contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009300u2009000)xa0— the number of columns in the grid. The next two lines contain numbers in the grid. The _i_-th of those two lines contains _n_ integers _t__i_,u20091,u2009_t__i_,u20092,u2009...,u2009_t__i_,u2009_n_ (u2009-u2009109u2009≤u2009_t__i_,u2009_j_u2009≤u2009109). Output Print one integer, denoting the maximum possible number of cell-disjoint nice rectangles. Examples Input 6 70 70 70 70 70 -15 90 -60 -30 30 -30 15 Input 3 1000000000 999999999 -1000000000 999999999 -1000000000 -999999998 Note In the first sample, there are four nice rectangles: Limak can't choose all of them because they are not disjoint. He should take three nice rectangles: those denoted as blue frames on the drawings. In the second sample, it's optimal to choose six nice rectangles, each consisting of one cell with a number 0. In the third sample, the only nice rectangle is the whole gridxa0— the sum of all numbers is 0. Clearly, Limak can choose at most one nice rectangle, so the answer is 1. | 3,000 | false | true | false | true | false | false | false | false | false | false | 6,694 |
375A | Problem - 375A - 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 number theory *1600 No tag edit access → Contest materials . The resulting number also mustn't contain any leading zeroes. Input The first line contains positive integer _a_ in the decimal record. It is guaranteed that the record of number _a_ contains digits: 1, 6, 8, 9. Number _a_ doesn't contain any leading zeroes. The decimal representation of number _a_ contains at least 4 and at most 106 characters. Output Print a number in the decimal notation without leading zeroes — the result of the permutation. If it is impossible to rearrange the digits of the number _a_ in the required manner, print 0. Examples Input 1689 Output 1869 Input 18906 Output 18690 | 1,600 | true | false | false | false | false | false | false | false | false | false | 8,331 |
864A | Petya and Vasya decided to play a game. They have _n_ cards (_n_ is an even number). A single integer is written on each card. Before the game Petya will choose an integer and after that Vasya will choose another integer (different from the number that Petya chose). During the game each player takes all the cards with number he chose. For example, if Petya chose number 5 before the game he will take all cards on which 5 is written and if Vasya chose number 10 before the game he will take all cards on which 10 is written. The game is considered fair if Petya and Vasya can take all _n_ cards, and the number of cards each player gets is the same. Determine whether Petya and Vasya can choose integer numbers before the game so that the game is fair. Input The first line contains a single integer _n_ (2u2009≤u2009_n_u2009≤u2009100) — number of cards. It is guaranteed that _n_ is an even number. The following _n_ lines contain a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (one integer per line, 1u2009≤u2009_a__i_u2009≤u2009100) — numbers written on the _n_ cards. Output If it is impossible for Petya and Vasya to choose numbers in such a way that the game will be fair, print "NO" (without quotes) in the first line. In this case you should not print anything more. In the other case print "YES" (without quotes) in the first line. In the second line print two distinct integers — number that Petya should choose and the number that Vasya should choose to make the game fair. If there are several solutions, print any of them. Note In the first example the game will be fair if, for example, Petya chooses number 11, and Vasya chooses number 27. Then the will take all cards — Petya will take cards 1 and 4, and Vasya will take cards 2 and 3. Thus, each of them will take exactly two cards. In the second example fair game is impossible because the numbers written on the cards are equal, but the numbers that Petya and Vasya should choose should be distinct. In the third example it is impossible to take all cards. Petya and Vasya can take at most five cards — for example, Petya can choose number 10 and Vasya can choose number 20. But for the game to be fair it is necessary to take 6 cards. | 1,000 | false | false | true | false | false | false | false | false | true | false | 6,287 |
1578M | This is an interactive problem. In this problem, you need to come up with a strategy for a cooperative game. This game is played by two players. Each player receives 5 cards. Each card has a random integer between 1 and 100 on it. It is guaranteed that all numbers on cards are distinct. The goal of the game is to play a card with a minimal number on it out of all 10 cards dealt to the players before any other card. The problem is that each player can only see their own cards and cannot communicate with another player in any way. The game consists of 5 turns. During each turn, players simultaneously make a move. Each player can either play their smallest card or do nothing. If on some turn the smallest card is played, and no other card is played on or before that turn, players win. If two cards are played at the same turn or if after all 5 turns, no card is still played, players lose. Players cannot communicate, so a strategy for the game should only be based on 5 cards that the player has. You can describe a strategy as five numbers $$$0.0 le p_i le 1.0, sum_{i=1}^{5}p_i le 1$$$, where $$$p_i$$$xa0— the probability of playing the player's smallest card in their hand on $$$i$$$-th turn. If you know the cards dealt to the players, and the strategies that players choose, you can compute the probability of winning by a simple formula. You will be given $$$n=1000$$$ randomly generated hands of 5 cards. You need to generate a strategy for each of the hands to maximize the probability of winning. After the judge program receives all $$$n$$$ strategies, it generates all possible valid pairs of those hands (pairs which have the same numbers are discarded), and computes a probability of winning based on two strategies provided by your program. To ensure that answers for different hands are independent, you must output a strategy for a hand and flush the standard output before reading information about the next hand. If the average probability of winning a game is more than 85% over all valid pairs of hands, the test is considered passed. This problem contains the sample test and $$$20$$$ randomly generated tests with $$$n = 1000$$$. Input The first line contains one integer $$$n$$$xa0— the number of hands. It is guaranteed that $$$n = 1000$$$ for all cases except the first sample case. Each of the next $$$n$$$ lines contains 5 numbers $$$a_i$$$ ($$$1 le a_i le 100, a_i < a_{i+1}$$$)xa0— the cards in the hand. It is guaranteed that each possible set of 5 cards has an equal probability of being chosen. | 2,700 | false | false | false | false | false | true | false | false | false | false | 2,742 |
1455D | You are given a sequence $$$a$$$ consisting of $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$, and an integer $$$x$$$. Your task is to make the sequence $$$a$$$ sorted (it is considered sorted if the condition $$$a_1 le a_2 le a_3 le dots le a_n$$$ holds). To make the sequence sorted, you may perform the following operation any number of times you want (possibly zero): choose an integer $$$i$$$ such that $$$1 le i le n$$$ and $$$a_i > x$$$, and swap the values of $$$a_i$$$ and $$$x$$$. For example, if $$$a = [0, 2, 3, 5, 4]$$$, $$$x = 1$$$, the following sequence of operations is possible: 1. choose $$$i = 2$$$ (it is possible since $$$a_2 > x$$$), then $$$a = [0, 1, 3, 5, 4]$$$, $$$x = 2$$$; 2. choose $$$i = 3$$$ (it is possible since $$$a_3 > x$$$), then $$$a = [0, 1, 2, 5, 4]$$$, $$$x = 3$$$; 3. choose $$$i = 4$$$ (it is possible since $$$a_4 > x$$$), then $$$a = [0, 1, 2, 3, 4]$$$, $$$x = 5$$$. Calculate the minimum number of operations you have to perform so that $$$a$$$ becomes sorted, or report that it is impossible. Input The first line contains one integer $$$t$$$ ($$$1 le t le 500$$$) — the number of test cases. Each test case consists of two lines. The first line contains two integers $$$n$$$ and $$$x$$$ ($$$1 le n le 500$$$, $$$0 le x le 500$$$) — the number of elements in the sequence and the initial value of $$$x$$$. The second line contains $$$n$$$ integers $$$a_1$$$, $$$a_2$$$, ..., $$$a_n$$$ ($$$0 le a_i le 500$$$). The sum of values of $$$n$$$ over all test cases in the input does not exceed $$$500$$$. Output For each test case, print one integer — the minimum number of operations you have to perform to make $$$a$$$ sorted, or $$$-1$$$, if it is impossible. Example Input 6 4 1 2 3 5 4 5 6 1 1 3 4 4 1 10 2 2 10 11 9 2 10 12 11 5 18 81 324 218 413 324 | 1,600 | false | true | false | true | false | false | false | false | true | false | 3,399 |
747A | Problem - 747A - 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 math *800 No tag edit access → Contest materials of pixels _a_ and the number of columns of pixels _b_, so that: there are exactly _n_ pixels on the display; the number of rows does not exceed the number of columns, it means _a_u2009≤u2009_b_; the difference _b_u2009-u2009_a_ is as small as possible. Input The first line contains the positive integer _n_ (1u2009≤u2009_n_u2009≤u2009106)xa0— the number of pixels display should have. Output Print two integersxa0— the number of rows and columns on the display. Examples Input 8 Output 2 4 Input 64 Output 8 8 Input 5 Output 1 5 Input 999999 Output 999 1001 Note In the first example the minimum possible difference equals 2, so on the display should be 2 rows of 4 pixels. In the second example the minimum possible difference equals 0, so on the display should be 8 rows of 8 pixels. In the third example the minimum possible difference equals 4, so on the display should be 1 row of 5 pixels. | 800 | true | false | false | false | false | false | true | false | false | false | 6,810 |
1195F | Denis holds a Geometers Anonymous Club meeting in SIS. He has prepared $$$n$$$ convex polygons numbered from $$$1$$$ to $$$n$$$ for the club. He plans to offer members of the club to calculate Minkowski sums of these polygons. More precisely, he plans to give $$$q$$$ tasks, the $$$i$$$-th of them asks to calculate the sum of Minkowski of polygons with indices from $$$l_i$$$ to $$$r_i$$$ inclusive. The sum of Minkowski of two sets $$$A$$$ and $$$B$$$ is the set $$$C = {a + b : a in A, b in B}$$$. It can be proven that if $$$A$$$ and $$$B$$$ are convex polygons then $$$C$$$ will also be a convex polygon. Sum of two convex polygons To calculate the sum of Minkowski of $$$p$$$ polygons ($$$p > 2$$$), you need to calculate the sum of Minkowski of the first $$$p - 1$$$ polygons, and then calculate the sum of Minkowski of the resulting polygon and the $$$p$$$-th polygon. For the convenience of checking answers, Denis has decided to prepare and calculate the number of vertices in the sum of Minkowski for each task he prepared. Help him to do it. Input The first line of the input contains one integer $$$n$$$ — the number of convex polygons Denis prepared ($$$1 le n le 100,000$$$). Then $$$n$$$ convex polygons follow. The description of the $$$i$$$-th polygon starts with one integer $$$k_i$$$ — the number of vertices in the $$$i$$$-th polygon ($$$3 le k_i$$$). The next $$$k_i$$$ lines contain two integers $$$x_{ij}$$$, $$$y_{ij}$$$ each — coordinates of vertices of the $$$i$$$-th polygon in counterclockwise order ($$$x_{ij}, y_{ij} le 10 ^ 9$$$). It is guaranteed, that there are no three consecutive vertices lying on the same line. The total number of vertices over all polygons does not exceed $$$300,000$$$. The following line contains one integer $$$q$$$ — the number of tasks ($$$1 le q le 100,000$$$). The next $$$q$$$ lines contain descriptions of tasks. Description of the $$$i$$$-th task contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le n$$$). Output For each task print a single integer — the number of vertices in the sum of Minkowski of polygons with indices from $$$l_i$$$ to $$$r_i$$$. Example Input 3 3 0 0 1 0 0 1 4 1 1 1 2 0 2 0 1 3 2 2 1 2 2 1 3 1 2 2 3 1 3 Note Description of the example: First, second and third polygons from the example Minkowski sums of the first and second, the second and third and all polygons correspondingly | 2,500 | true | false | false | false | true | false | false | false | true | false | 4,723 |
1824A | Enter Register HOME TOP CATALOG CONTESTS GYM PROBLEMSET GROUPS RATING EDU API CALENDAR HELP RAYAN Codeforces Round 872 (Div. 1) 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 *1400 No tag edit access → Contest materials Announcement (en) Tutorial (en) PROBLEMS SUBMIT STATUS STANDINGS CUSTOM TEST A. LuoTianyi and the Show time limit per test2 seconds memory limit per test256 megabytes There are $$$n$$$ people taking part in a show about VOCALOID. They will sit in the row of seats, numbered $$$1$$$ to $$$m$$$ from left to right. The $$$n$$$ people come and sit in order. Each person occupies a seat in one of three ways: Sit in the seat next to the left of the leftmost person who is already sitting, or if seat $$$1$$$ is taken, then leave the show. If there is no one currently sitting, sit in seat $$$m$$$. Sit in the seat next to the right of the rightmost person who is already sitting, or if seat $$$m$$$ is taken, then leave the show. If there is no one currently sitting, sit in seat $$$1$$$. Sit in the seat numbered $$$x_i$$$. If this seat is taken, then leave the show. Now you want to know what is the maximum number of people that can take a seat, if you can let people into the show in any order? Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 10^5$$$) — the number of people and the number of seats. The second line of each test case contains $$$n$$$ integers $$$x_1, x_2, ldots, x_n$$$ ($$$-2 le x_i le m$$$, $$$x_i e 0$$$), the $$$i$$$-th of which describes the way in which the $$$i$$$-th person occupies a seat: If $$$x_i=-1$$$, then $$$i$$$-th person takes the seat in the first way. If $$$x_i=-2$$$, then $$$i$$$-th person takes the seat in the second way. If $$$x_i > 0$$$, then the $$$i$$$-th person takes a seat in the third way, i.e. he wants to sit in the seat with the number $$$x_i$$$ or leave the show if it is occupied.. It is guaranteed that sum of $$$n$$$ and the sum of $$$m$$$ over all test cases don't exceed $$$10^5$$$. Output For each test case output a single integer — the maximum number of people who can occupy a seat. Example input 10 3 10 5 5 5 4 6 1 -2 -2 1 5 7 -1 -1 4 -2 -2 6 7 5 -2 -2 -2 -2 -2 6 6 -1 1 4 5 -1 4 6 8 -1 -1 -1 3 -1 -2 6 7 5 -1 -2 -2 -2 -2 3 1 -2 -2 1 2 5 5 -2 1 2 -1 output 1 3 5 6 5 5 5 1 2 1 Note In the first test case, all the people want to occupy the $$$5$$$ seat, so only $$$1$$$ people can occupy the seat. In the second test case, we can let people in order $$$1, 2, 3, 4$$$, then all but the last person can take a seat. In the third test case, we can let people into the show in that order: Let the third person in: – – – 3 – – – Let the fourth person in: – – – 3 4 – – Let the fifth person in: – – – 3 4 5 – Let the first person in: – – 1 3 4 5 – Let the second person in: – 2 1 3 4 5 – Thus, all $$$5$$$ people took seats. In the fifth test case, we can let people into the show in this order: Let the fourth person in: – – – – 4 – Let the third person in: – – – 3 4 – Let the sixth person in, he'll leave the show because he takes the third seat the third way and has to sit in the $$$4$$$ seat, but it's already taken: – – – 3 4 – Let the fifth person in: – – 5 3 4 – Let the first person in: – 1 5 3 4 – Let the second person in: 2 1 5 3 4 – Thus, $$$5$$$ of people took seats. In the seventh test case, we can let people into the show in this order: Let the third person in: 3 – – – – – – Let the fourth person in: 3 4 – – – – – Let the fifth person in: 3 4 5 – – – – Let the sixth person in: 3 4 5 6 – – – Let the first person in: 3 4 5 6 1 – – Let the second person in, he will leave the show because he occupies the first way, but the $$$1$$$ seat is taken: 3 4 5 6 1 – – Thus, $$$5$$$ people took seats. Codeforces (c) | 1,400 | false | true | true | false | false | false | false | false | false | false | 1,337 |
1089D | , # Problem A. Distance Sum Time limit: 3 seconds You are given a connected undirected unweighted graph. The distance d(u, v ) between two vertices u and v is defined as the number of edges in the shortest path between them. Find the sum of d(u, v ) over all unordered pairs (u, v ). ## Input The first line of the input contains two integers n and m (2 x14 n x14 10 5; n x00 1 x14 m x14 n + 42 ) — the number of vertices and the number of edges in the graph respectively. The vertices are numbered from 1 to n.Each of the following m lines contains two integers xi and yi (1 x14 xi, y i x14 n; xi̸ = yi) — the endpoints of the i-th edge. There is at most one edge between every pair of vertices. ## Output Output a single integer — the sum of the distances between all unordered pairs of vertices in the graph. ## Examples standard input standard output Illustration 4 4 1 2 2 3 3 1 3 4 81 2 3 4 7 10 1 2 2 6 5 3 5 4 5 7 3 6 1 7 5 1 7 4 4 1 34 4 1 7 5 3 2 6 ## Note In the first example the distance between four pairs of vertices connected by an edge is equal to 1 and d(1 , 4) = d(2 , 4) = 2 . Page 1 of 1 | 3,100 | false | false | false | false | false | false | false | false | false | true | 5,282 |
1868C | During the summer vacation after Zhongkao examination, Tom and Daniel are planning to go traveling. There are $$$n$$$ cities in their country, numbered from $$$1$$$ to $$$n$$$. And the traffic system in the country is very special. For each city $$$i$$$ ($$$1 le i le n$$$), there is a road between city $$$i$$$ and $$$2i$$$, if $$$2ile n$$$; a road between city $$$i$$$ and $$$2i+1$$$, if $$$2i+1le n$$$. Making a travel plan, Daniel chooses some integer value between $$$1$$$ and $$$m$$$ for each city, for the $$$i$$$-th city we denote it by $$$a_i$$$. Let $$$s_{i,j}$$$ be the maximum value of cities in the simple$$$^dagger$$$ path between cities $$$i$$$ and $$$j$$$. The score of the travel plan is $$$sum_{i=1}^nsum_{j=i}^n s_{i,j}$$$. Tom wants to know the sum of scores of all possible travel plans. Daniel asks you to help him find it. You just need to tell him the answer modulo $$$998,244,353$$$. $$$^dagger$$$ A simple path between cities $$$x$$$ and $$$y$$$ is a path between them that passes through each city at most once. Input The first line of input contains a single integer $$$t$$$ ($$$1le tle 200$$$) — the number of test cases. The description of test cases follows. The only line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1leq nleq 10^{18}$$$, $$$1leq mleq 10^5$$$) — the number of the cities and the maximum value of a city. It is guaranteed that the sum of $$$m$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case output one integer — the sum of scores of all possible travel plans, modulo $$$998,244,353$$$. Example Input 5 3 1 2 2 10 9 43 20 154 147 Output 6 19 583217643 68816635 714002110 Note In the first test case, there is only one possible travel plan: Path $$$1 ightarrow 1$$$: $$$s_{1,1}=a_1=1$$$. Path $$$1 ightarrow 2$$$: $$$s_{1,2}=max(1,1)=1$$$. Path $$$1 ightarrow 3$$$: $$$s_{1,3}=max(1,1)=1$$$. Path $$$2 ightarrow 2$$$: $$$s_{2,2}=a_2=1$$$. Path $$$2 ightarrow 1 ightarrow 3$$$: $$$s_{2,3}=max(1,1,1)=1$$$. Path $$$3 ightarrow 3$$$: $$$s_{3,3}=a_3=1$$$. The score is $$$1+1+1+1+1+1=6$$$. In the second test case, there are four possible travel plans: Score of plan $$$1$$$: $$$1+1+1=3$$$. Score of plan $$$2$$$: $$$1+2+2=5$$$. Score of plan $$$3$$$: $$$2+2+1=5$$$. Score of plan $$$4$$$: $$$2+2+2=6$$$. Therefore, the sum of score is $$$3+5+5+6=19$$$. | 2,400 | true | false | true | true | false | false | false | false | false | false | 1,065 |
1389E | Problem - 1389E - 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 number theory *2200 No tag edit access → Contest materials $$$ such that $$$x < y$$$ is ambiguous if day $$$x$$$ of month $$$y$$$ is the same day of the week as day $$$y$$$ of month $$$x$$$. Count the number of ambiguous pairs. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of testcases. Each of the next $$$t$$$ lines contains three integers $$$m$$$, $$$d$$$ and $$$w$$$ ($$$1 le m, d, w le 10^9$$$)xa0— the number of months in a year, the number of days in a month and the number of days in a week. Output Print $$$t$$$ integersxa0— for each testcase output the number of pairs $$$(x, y)$$$ such that $$$x < y$$$ and day $$$x$$$ of month $$$y$$$ is the same day of the week as day $$$y$$$ of month $$$x$$$. Example Input 5 6 7 4 10 7 12 12 30 7 1 1 1 3247834 10298779 625324 Output 6 9 5 0 116461800 Note Here are the pairs for the first test case: | 2,200 | true | false | false | false | false | false | false | false | false | false | 3,724 |
2025G | Monocarp is gathering an army to fight a dragon in a videogame. The army consists of two parts: the heroes and the defensive artifacts. Each hero has one parameterxa0— his health. Each defensive artifact also has one parameterxa0— its durability. Before the battle begins, Monocarp distributes artifacts to the heroes so that each hero receives at most one artifact. The battle consists of rounds that proceed as follows: first, the dragon deals damage equal to $$$frac{1}{a + b}$$$ (a real number without rounding) to each hero, where $$$a$$$ is the number of heroes alive and $$$b$$$ is the number of active artifacts; after that, all heroes with health $$$0$$$ or less die; finally, some artifacts are deactivated. An artifact with durability $$$x$$$ is deactivated when one of the following occurs: the hero holding the artifact either dies or receives $$$x$$$ total damage (from the start of the battle). If an artifact is not held by any hero, it is inactive from the beginning of the battle. The battle ends when there are no heroes left alive. Initially, the army is empty. There are $$$q$$$ queries: add a hero with health $$$x$$$ or an artifact with durability $$$y$$$ to the army. After each query, determine the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally. Input The first line contains one integer $$$q$$$ ($$$1 le q le 3 cdot 10^5$$$)xa0— the number of queries. In the $$$i$$$-th of the following $$$q$$$ lines, there are two integers $$$t_i$$$ and $$$v_i$$$ ($$$t_i in {1, 2}$$$; $$$1 le v_i le 10^9$$$)xa0— the type of the query and the value of the query parameter. If the type is $$$1$$$, a hero with health $$$v_i$$$ is added. If the type is $$$2$$$, an artifact with durability $$$v_i$$$ is added. Output Print $$$q$$$ integers. After each query, output the maximum number of rounds that Monocarp can survive if he distributes the artifacts optimally. Examples Input 10 1 9 1 6 2 4 1 8 1 3 2 10 1 3 1 6 1 10 2 6 Output 9 15 19 27 30 39 42 48 59 65 Note Let's consider the first example. An artifact with durability $$$5$$$ is added. Since there are no heroes yet, the battle ends immediately. A hero with health $$$4$$$ is added. Monocarp can give him an artifact with durability $$$5$$$. First, there are rounds in which the hero takes $$$frac{1}{1 + 1} = frac{1}{2}$$$ damage. After $$$8$$$ such rounds, a total of $$$4$$$ damage will have been dealt, and the hero will die, while the artifact will deactivate. There are no more heroes alive, so the battle ends after $$$8$$$ rounds. A hero with health $$$10$$$ is added. Now let the artifact with durability $$$5$$$ be with this hero. Then, in the first $$$12$$$ rounds, the heroes will take $$$12 cdot frac{1}{2 + 1} = 4$$$ damage, and the first hero will die. The second hero has $$$6$$$ health left, and the artifact has $$$1$$$ durability. Now the damage is $$$frac{1}{2}$$$, so after another $$$2$$$ rounds, the artifact will deactivate. The second hero has $$$5$$$ health left. After another $$$5$$$ rounds, the second hero will die. Therefore, the answer is $$$12 + 2 + 5 = 19$$$. | 3,000 | false | false | false | false | true | false | false | false | false | false | 117 |
1882E2 | This is the hard version of the problem. The difference between the two versions is that you have to minimize the number of operations in this version. You can make hacks only if both versions of the problem are solved. You have two permutations$$$^{dagger}$$$ $$$p_{1}, p_{2}, ldots, p_{n}$$$ (of integers $$$1$$$ to $$$n$$$) and $$$q_{1}, q_{2}, ldots, q_{m}$$$ (of integers $$$1$$$ to $$$m$$$). Initially $$$p_{i}=a_{i}$$$ for $$$i=1, 2, ldots, n$$$, and $$$q_{j} = b_{j}$$$ for $$$j = 1, 2, ldots, m$$$. You can apply the following operation on the permutations several (possibly, zero) times. In one operation, $$$p$$$ and $$$q$$$ will change according to the following three steps: You choose integers $$$i$$$, $$$j$$$ which satisfy $$$1 le i le n$$$ and $$$1 le j le m$$$. Permutation $$$p$$$ is partitioned into three parts using $$$p_i$$$ as a pivot: the left part is formed by elements $$$p_1, p_2, ldots, p_{i-1}$$$ (this part may be empty), the middle part is the single element $$$p_i$$$, and the right part is $$$p_{i+1}, p_{i+2}, ldots, p_n$$$ (this part may be empty). To proceed, swap the left and the right parts of this partition. Formally, after this step, $$$p$$$ will become $$$p_{i+1}, p_{i+2}, ldots, p_{n}, p_{i}, p_{1}, p_{2}, ldots, p_{i-1}$$$. The elements of the newly formed $$$p$$$ will be reindexed starting from $$$1$$$. Perform the same transformation on $$$q$$$ with index $$$j$$$. Formally, after this step, $$$q$$$ will become $$$q_{j+1}, q_{j+2}, ldots, q_{m}, q_{j}, q_{1}, q_{2}, ldots, q_{j-1}$$$. The elements of the newly formed $$$q$$$ will be reindexed starting from $$$1$$$. Your goal is to simultaneously make $$$p_{i}=i$$$ for $$$i=1, 2, ldots, n$$$, and $$$q_{j} = j$$$ for $$$j = 1, 2, ldots, m$$$. Find any way to achieve the goal using the minimum number of operations possible, or say that none exists. Please note that you have to minimize the number of operations. $$$^{dagger}$$$ A permutation of length $$$k$$$ is an array consisting of $$$k$$$ distinct integers from $$$1$$$ to $$$k$$$ 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 ($$$k=3$$$ but there is $$$4$$$ in the array). Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 2500$$$). The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$). The third line contains $$$m$$$ integers $$$b_1, b_2, ldots, b_m$$$ ($$$1 le b_i le m$$$). It is guaranteed that $$$a$$$ and $$$b$$$ are permutations. Output If there is no solution, print a single integer $$$-1$$$. Otherwise, print an integer $$$k$$$xa0— the number of operations to perform, followed by $$$k$$$ lines, each containing two integers $$$i$$$ and $$$j$$$ ($$$1 le i le n$$$, $$$1 le j le m$$$)xa0— the integers chosen for the operation. If there are multiple solutions, print any of them. Please note that you have to minimize the number of operations. | 3,100 | false | false | false | false | false | true | false | false | false | false | 992 |
1598F | A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence (or, shortly, an RBS) is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example: bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"); bracket sequences ")(", "(" and ")" are not. Let's denote the concatenation of two strings $$$x$$$ and $$$y$$$ as $$$x+y$$$. For example, "()()"xa0$$$+$$$xa0")("xa0$$$=$$$xa0"()())(". You are given $$$n$$$ bracket sequences $$$s_1, s_2, dots, s_n$$$. You can rearrange them in any order (you can rearrange only the strings themselves, but not the characters in them). Your task is to rearrange the strings in such a way that the string $$$s_1 + s_2 + dots + s_n$$$ has as many non-empty prefixes that are RBS as possible. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 20$$$). Then $$$n$$$ lines follow, the $$$i$$$-th of them contains $$$s_i$$$xa0— a bracket sequence (a string consisting of characters "(" and/or ")". All sequences $$$s_i$$$ are non-empty, their total length does not exceed $$$4 cdot 10^5$$$. Output Print one integerxa0— the maximum number of non-empty prefixes that are RBS for the string $$$s_1 + s_2 + dots + s_n$$$, if the strings $$$s_1, s_2, dots, s_n$$$ can be rearranged arbitrarily. Note In the first example, you can concatenate the strings as follows: "("xa0$$$+$$$xa0")"xa0$$$=$$$xa0"()", the resulting string will have one prefix, that is an RBS: "()". In the second example, you can concatenate the strings as follows: "("xa0$$$+$$$xa0")"xa0$$$+$$$xa0"()()())"xa0$$$+$$$xa0"("xa0$$$=$$$xa0"()()()())(", the resulting string will have four prefixes that are RBS: "()", "()()", "()()()", "()()()()". The third and the fourth examples contain only one string each, so the order is fixed. | 2,400 | false | false | false | true | true | false | true | true | false | false | 2,666 |
124A | Problem - 124A - 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 *1000 No tag edit access → Contest materials . Output Print the single number — the number of the sought positions. Examples Input 3 1 1 Output 2 Input 5 2 3 Output 3 Note The possible positions in the first sample are: 2 and 3 (if we number the positions starting with 1). In the second sample they are 3, 4 and 5. | 1,000 | true | false | false | false | false | false | false | false | false | false | 9,384 |
471A | Two polar bears Menshykov and Uslada from the St.Petersburg zoo and elephant Horace from the Kiev zoo got six sticks to play with and assess the animals' creativity. Menshykov, Uslada and Horace decided to make either an elephant or a bear from those sticks. They can make an animal from sticks in the following way: Four sticks represent the animal's legs, these sticks should have the same length. Two remaining sticks represent the animal's head and body. The bear's head stick must be shorter than the body stick. The elephant, however, has a long trunk, so his head stick must be as long as the body stick. Note that there are no limits on the relations between the leg sticks and the head and body sticks. Your task is to find out which animal can be made from the given stick set. The zoo keeper wants the sticks back after the game, so they must never be broken, even bears understand it. Input The single line contains six space-separated integers _l__i_ (1u2009≤u2009_l__i_u2009≤u20099) — the lengths of the six sticks. It is guaranteed that the input is such that you cannot make both animals from the sticks. Output If you can make a bear from the given set, print string "Bear" (without the quotes). If you can make an elephant, print string "Elephant" (wıthout the quotes). If you can make neither a bear nor an elephant, print string "Alien" (without the quotes). Note If you're out of creative ideas, see instructions below which show how to make a bear and an elephant in the first two samples. The stick of length 2 is in red, the sticks of length 4 are in green, the sticks of length 5 are in blue. | 1,100 | false | false | true | false | false | false | false | false | false | false | 7,952 |
1211E | Polycarp recently became an employee of the company "Double Permutation Inc." Now he is a fan of permutations and is looking for them everywhere! A permutation in this problem is a sequence of integers $$$p_1, p_2, dots, p_k$$$ such that every integer from $$$1$$$ to $$$k$$$ occurs exactly once in it. For example, the following sequences are permutations of $$$[3, 1, 4, 2]$$$, $$$[1]$$$ and $$$[6, 1, 2, 3, 5, 4]$$$. The following sequences are not permutations: $$$[0, 1]$$$, $$$[1, 2, 2]$$$, $$$[1, 2, 4]$$$ and $$$[2, 3]$$$. In the lobby of the company's headquarter statistics on visits to the company's website for the last $$$n$$$ days are published — the sequence $$$a_1, a_2, dots, a_n$$$. Polycarp wants to color all the elements of this sequence in one of three colors (red, green or blue) so that: all red numbers, being written out of $$$a_1, a_2, dots, a_n$$$ from left to right (that is, without changing their relative order), must form some permutation (let's call it $$$P$$$); all green numbers, being written out of $$$a_1, a_2, dots, a_n$$$ from left to right (that is, without changing their relative order), must form the same permutation $$$P$$$; among blue numbers there should not be elements that are equal to some element of the permutation $$$P$$$. Help Polycarp to color all $$$n$$$ numbers so that the total number of red and green elements is maximum. Input The first line contains an integer $$$n$$$ ($$$1 le n le 2cdot10^5$$$) — the length of the sequence $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 2cdot10^5$$$). Output Print a string $$$s$$$ of length $$$n$$$ such that: $$$s_i$$$='R', if the element $$$a_i$$$ must be colored in red; $$$s_i$$$='G', if the element $$$a_i$$$ must be colored in green; $$$s_i$$$='B', if the element $$$a_i$$$ must be colored in blue. The string $$$s$$$ should maximize the total number of red and green elements when fulfilling the requirements from the main part of the problem statement. If there are several optimal answers, print any of them. | 2,000 | false | true | false | false | false | false | false | true | false | false | 4,624 |
1876E | With a problem title like that, there is no way this is going to be a graph problem. Chaneka has a graph with $$$n$$$ vertices and $$$n-1$$$ edges. Some of the edges are directed and some of the edges are undirected. Edge $$$i$$$ connects vertex $$$u_i$$$ to vertex $$$v_i$$$. If $$$t_i=0$$$, edge $$$i$$$ is undirected. If $$$t_i=1$$$, edge $$$i$$$ is directed in the direction from $$$u_i$$$ to $$$v_i$$$. It is known that if you make all edges undirected, the graph becomes a tree$$$^dagger$$$. Chaneka wants to direct all undirected edges and colour each edge (different edges can have the same colour). After doing that, suppose Chaneka starts a walk from an arbitrary vertex $$$x$$$ to an arbitrary vertex $$$y$$$ (it is possible that $$$x=y$$$) going through one or more edges. She is allowed to go through each edge either following the direction or opposite to the direction of the edge. She is also allowed to visit a vertex or an edge more than once. During the walk, Chaneka maintains a stack of balls that is initially empty before the walk. Each time Chaneka goes through an edge, she does the following: If Chaneka goes through it in the right direction, she puts a new ball with a colour that is the same as the edge's colour to the top of the stack. If Chaneka goes through it in the opposite direction, she removes the ball that is on the top of the stack. A walk is stackable if and only if the stack is not empty before each time Chaneka goes through an edge in the opposite direction. A walk is ball-stackable if and only if it is stackable and each time Chaneka goes through an edge in the opposite direction, the colour of the ball removed from the stack is the same as the colour of the edge traversed. Is it possible to direct all undirected edges and colour each edge such that all stackable walks are also ball-stackable? If it is possible, find a construction example that uses the maximum number of different colours among all valid ways of directing and colouring. If there are multiple such solutions, output any of them. $$$^dagger$$$ A tree is a connected graph with no cycles. Input The first line contains a single integer $$$n$$$ ($$$2leq nleq10^5$$$) — the number of vertices in the graph. The $$$i$$$-th of the next $$$n-1$$$ lines contains three integers $$$u_i$$$, $$$v_i$$$, and $$$t_i$$$ ($$$1 leq u_i,v_i leq n$$$; $$$0leq t_ileq1$$$) — an undirected edge connecting vectices $$$u_i$$$ and $$$v_i$$$ if $$$t_i=0$$$, or a directed edge from vertex $$$u_i$$$ to vertex $$$v_i$$$ if $$$t_i=1$$$. If you make all edges undirected, the graph becomes a tree. Output A single line containing $$$-1$$$ if it is impossible. Otherwise, the output consists of $$$n$$$ lines describing your construction. The first line contains an integer $$$z$$$ representing the number of different colours used. The $$$i$$$-th of the next $$$n-1$$$ lines contains three integers $$$p$$$, $$$q$$$, and $$$c$$$ ($$$1leq p,qleq n$$$; $$$1leq cleq z$$$) — the edge connecting vertices $$$p$$$ and $$$q$$$ in the graph is directed from vertex $$$p$$$ to vertex $$$q$$$ and is coloured with colour $$$c$$$. If there are multiple such solutions, output any of them. Note that since there should be $$$z$$$ different colours in your construction, that means each colour from $$$1$$$ to $$$z$$$ must appear at least once in the graph. Example Input 5 2 5 1 1 3 0 5 4 0 1 5 1 Output 3 1 5 2 5 4 1 2 5 2 3 1 3 Note The following is the given graph. Chaneka can direct all undirected edges and colour each edge as follows. As an example, consider a stackable walk $$$3→1→5→2→5→4→5$$$. Let's show that that walk is ball-stackable. 1. Chaneka starts in vertex $$$3$$$. The stack is $$$[]$$$. 2. Chaneka moves to vertex $$$1$$$. She puts a ball of colour $$$3$$$. The stack is $$$[3]$$$. 3. Chaneka moves to vertex $$$5$$$. She puts a ball of colour $$$2$$$. The stack is $$$[3,2]$$$. 4. Chaneka moves to vertex $$$2$$$. She removes a ball of colour $$$2$$$ (same colour as the edge). The stack is $$$[3]$$$. 5. Chaneka moves to vertex $$$5$$$. She puts a ball of colour $$$2$$$. The stack is $$$[3,2]$$$. 6. Chaneka moves to vertex $$$4$$$. She puts a ball of colour $$$1$$$. The stack is $$$[3,2,1]$$$. 7. Chaneka moves to vertex $$$5$$$. She removes a ball of colour $$$1$$$ (same colour as the edge). The stack is $$$[3,2]$$$. Since every time Chaneka removes a ball from the stack, it has the same colour as the edge traversed, then the walk above is ball-stackable. It can be shown that if we direct and colour the edges as shown above, any possible stackable walk is also ball-stackable. | 3,300 | false | false | false | true | true | true | false | false | false | false | 1,022 |
1100E | Andrew prefers taxi to other means of transport, but recently most taxi drivers have been acting inappropriately. In order to earn more money, taxi drivers started to drive in circles. Roads in Andrew's city are one-way, and people are not necessary able to travel from one part to another, but it pales in comparison to insidious taxi drivers. The mayor of the city decided to change the direction of certain roads so that the taxi drivers wouldn't be able to increase the cost of the trip endlessly. More formally, if the taxi driver is on a certain crossroads, they wouldn't be able to reach it again if he performs a nonzero trip. Traffic controllers are needed in order to change the direction the road goes. For every road it is known how many traffic controllers are needed to change the direction of the road to the opposite one. It is allowed to change the directions of roads one by one, meaning that each traffic controller can participate in reversing two or more roads. You need to calculate the minimum number of traffic controllers that you need to hire to perform the task and the list of the roads that need to be reversed. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 leq n leq 100,000$$$, $$$1 leq m leq 100,000$$$)xa0— the number of crossroads and the number of roads in the city, respectively. Each of the following $$$m$$$ lines contain three integers $$$u_{i}$$$, $$$v_{i}$$$ and $$$c_{i}$$$ ($$$1 leq u_{i}, v_{i} leq n$$$, $$$1 leq c_{i} leq 10^9$$$, $$$u_{i} e v_{i}$$$)xa0— the crossroads the road starts at, the crossroads the road ends at and the number of traffic controllers required to reverse this road. Output In the first line output two integers the minimal amount of traffic controllers required to complete the task and amount of roads $$$k$$$ which should be reversed. $$$k$$$ should not be minimized. In the next line output $$$k$$$ integers separated by spaces — numbers of roads, the directions of which should be reversed. The roads are numerated from $$$1$$$ in the order they are written in the input. If there are many solutions, print any of them. Examples Input 5 6 2 1 1 5 2 6 2 3 2 3 4 3 4 5 5 1 5 4 Input 5 7 2 1 5 3 2 3 1 3 3 2 4 1 4 3 5 5 4 1 1 5 3 Note There are two simple cycles in the first example: $$$1 ightarrow 5 ightarrow 2 ightarrow 1$$$ and $$$2 ightarrow 3 ightarrow 4 ightarrow 5 ightarrow 2$$$. One traffic controller can only reverse the road $$$2 ightarrow 1$$$ and he can't destroy the second cycle by himself. Two traffic controllers can reverse roads $$$2 ightarrow 1$$$ and $$$2 ightarrow 3$$$ which would satisfy the condition. In the second example one traffic controller can't destroy the cycle $$$ 1 ightarrow 3 ightarrow 2 ightarrow 1 $$$. With the help of three controllers we can, for example, reverse roads $$$1 ightarrow 3$$$ ,$$$ 2 ightarrow 4$$$, $$$1 ightarrow 5$$$. | 2,200 | false | false | false | false | false | false | false | true | false | true | 5,202 |
1336A | Writing light novels is the most important thing in Linova's life. Last night, Linova dreamed about a fantastic kingdom. She began to write a light novel for the kingdom as soon as she woke up, and of course, she is the queen of it. xa0There are $$$n$$$ cities and $$$n-1$$$ two-way roads connecting pairs of cities in the kingdom. From any city, you can reach any other city by walking through some roads. The cities are numbered from $$$1$$$ to $$$n$$$, and the city $$$1$$$ is the capital of the kingdom. So, the kingdom has a tree structure. As the queen, Linova plans to choose exactly $$$k$$$ cities developing industry, while the other cities will develop tourism. The capital also can be either industrial or tourism city. A meeting is held in the capital once a year. To attend the meeting, each industry city sends an envoy. All envoys will follow the shortest path from the departure city to the capital (which is unique). Traveling in tourism cities is pleasant. For each envoy, his happiness is equal to the number of tourism cities on his path. In order to be a queen loved by people, Linova wants to choose $$$k$$$ cities which can maximize the sum of happinesses of all envoys. Can you calculate the maximum sum for her? Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2le nle 2 cdot 10^5$$$, $$$1le k< n$$$) xa0— the number of cities and industry cities respectively. Each of the next $$$n-1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1le u,vle n$$$), denoting there is a road connecting city $$$u$$$ and city $$$v$$$. It is guaranteed that from any city, you can reach any other city by the roads. Output Print the only line containing a single integer xa0— the maximum possible sum of happinesses of all envoys. Examples Input 7 4 1 2 1 3 1 4 3 5 3 6 4 7 Input 8 5 7 5 1 7 6 1 3 7 8 3 2 1 4 5 Note In the first example, Linova can choose cities $$$2$$$, $$$5$$$, $$$6$$$, $$$7$$$ to develop industry, then the happiness of the envoy from city $$$2$$$ is $$$1$$$, the happiness of envoys from cities $$$5$$$, $$$6$$$, $$$7$$$ is $$$2$$$. The sum of happinesses is $$$7$$$, and it can be proved to be the maximum one. In the second example, choosing cities $$$3$$$, $$$4$$$ developing industry can reach a sum of $$$3$$$, but remember that Linova plans to choose exactly $$$k$$$ cities developing industry, then the maximum sum is $$$2$$$. | 1,600 | false | true | false | true | false | false | false | false | true | false | 4,023 |
439D | Devu and his brother love each other a lot. As they are super geeks, they only like to play with arrays. They are given two arrays _a_ and _b_ by their father. The array _a_ is given to Devu and _b_ to his brother. As Devu is really a naughty kid, he wants the minimum value of his array _a_ should be at least as much as the maximum value of his brother's array _b_. Now you have to help Devu in achieving this condition. You can perform multiple operations on the arrays. In a single operation, you are allowed to decrease or increase any element of any of the arrays by 1. Note that you are allowed to apply the operation on any index of the array multiple times. You need to find minimum number of operations required to satisfy Devu's condition so that the brothers can play peacefully without fighting. Input The first line contains two space-separated integers _n_, _m_ (1u2009≤u2009_n_,u2009_m_u2009≤u2009105). The second line will contain _n_ space-separated integers representing content of the array _a_ (1u2009≤u2009_a__i_u2009≤u2009109). The third line will contain _m_ space-separated integers representing content of the array _b_ (1u2009≤u2009_b__i_u2009≤u2009109). Output You need to output a single integer representing the minimum number of operations needed to satisfy Devu's condition. Note In example 1, you can increase _a_1 by 1 and decrease _b_2 by 1 and then again decrease _b_2 by 1. Now array _a_ will be [3; 3] and array _b_ will also be [3; 3]. Here minimum element of _a_ is at least as large as maximum element of _b_. So minimum number of operations needed to satisfy Devu's condition are 3. In example 3, you don't need to do any operation, Devu's condition is already satisfied. | 1,700 | false | false | false | false | false | false | false | true | true | false | 8,081 |
1525A | You have an initially empty cauldron, and you want to brew a potion in it. The potion consists of two ingredients: magic essence and water. The potion you want to brew should contain exactly $$$k %$$$ magic essence and $$$(100 - k) %$$$ water. In one step, you can pour either one liter of magic essence or one liter of water into the cauldron. What is the minimum number of steps to brew a potion? You don't care about the total volume of the potion, only about the ratio between magic essence and water in it. A small reminder: if you pour $$$e$$$ liters of essence and $$$w$$$ liters of water ($$$e + w > 0$$$) into the cauldron, then it contains $$$frac{e}{e + w} cdot 100 %$$$ (without rounding) magic essence and $$$frac{w}{e + w} cdot 100 %$$$ water. Input The first line contains the single $$$t$$$ ($$$1 le t le 100$$$)xa0— the number of test cases. The first and only line of each test case contains a single integer $$$k$$$ ($$$1 le k le 100$$$)xa0— the percentage of essence in a good potion. Output For each test case, print the minimum number of steps to brew a good potion. It can be proved that it's always possible to achieve it in a finite number of steps. Note In the first test case, you should pour $$$3$$$ liters of magic essence and $$$97$$$ liters of water into the cauldron to get a potion with $$$3 %$$$ of magic essence. In the second test case, you can pour only $$$1$$$ liter of essence to get a potion with $$$100 %$$$ of magic essence. In the third test case, you can pour $$$1$$$ liter of magic essence and $$$3$$$ liters of water. | 800 | true | false | false | false | false | false | false | false | false | false | 3,042 |
387A | Problem - 387A - 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 *900 No tag edit access → Contest materials ") ") . Input The first line contains current time _s_ as a string in the format "hh:mm". The second line contains time _t_ in the format "hh:mm" — the duration of George's sleep. It is guaranteed that the input contains the correct time in the 24-hour format, that is, 00u2009≤u2009_hh_u2009≤u200923, 00u2009≤u2009_mm_u2009≤u200959. Output In the single line print time _p_ — the time George went to bed in the format similar to the format of the time in the input. Examples Input 05:50 05:44 Output 00:06 Input 00:00 01:00 Output 23:00 Input 00:01 00:00 Output 00:01 Note In the first sample George went to bed at "00:06". Note that you should print the time only in the format "00:06". That's why answers "0:06", "00:6" and others will be considered incorrect. In the second sample, George went to bed yesterday. In the third sample, George didn't do to bed at all. | 900 | false | false | true | false | false | false | false | false | false | false | 8,282 |
24D | Problem - 24D - Codeforces =============== xa0 . The board consists of _N_ rows and _M_ columns of cells. The robot is initially at some cell on the _i_-th row and the _j_-th column. Then at every step the robot could go to some another cell. The aim is to go to the bottommost (_N_-th) row. The robot can stay at it's current cell, move to the left, move to the right, or move to the cell below the current. If the robot is in the leftmost column it cannot move to the left, and if it is in the rightmost column it cannot move to the right. At every step all possible moves are equally probable. Return the expected number of step to reach the bottommost row. Input On the first line you will be given two space separated integers _N_ and _M_ (1u2009≤u2009_N_,u2009_M_u2009≤u20091000). On the second line you will be given another two space separated integers _i_ and _j_ (1u2009≤u2009_i_u2009≤u2009_N_,u20091u2009≤u2009_j_u2009≤u2009_M_) — the number of the initial row and the number of the initial column. Note that, (1,u20091) is the upper left corner of the board and (_N_,u2009_M_) is the bottom right corner. Output Output the expected number of steps on a line of itself with at least 4 digits after the decimal point. Examples Input 10 10 10 4 Output 0.0000000000 Input 10 14 5 14 Output 18.0038068653 | 2,400 | true | false | false | true | false | false | false | false | false | false | 9,875 |
796A | Zane the wizard had never loved anyone before, until he fell in love with a girl, whose name remains unknown to us. The girl lives in house _m_ of a village. There are _n_ houses in that village, lining in a straight line from left to right: house 1, house 2, ..., house _n_. The village is also well-structured: house _i_ and house _i_u2009+u20091 (1u2009≤u2009_i_u2009<u2009_n_) are exactly 10 meters away. In this village, some houses are occupied, and some are not. Indeed, unoccupied houses can be purchased. You will be given _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ that denote the availability and the prices of the houses. If house _i_ is occupied, and therefore cannot be bought, then _a__i_ equals 0. Otherwise, house _i_ can be bought, and _a__i_ represents the money required to buy it, in dollars. As Zane has only _k_ dollars to spare, it becomes a challenge for him to choose the house to purchase, so that he could live as near as possible to his crush. Help Zane determine the minimum distance from his crush's house to some house he can afford, to help him succeed in his love. Input The first line contains three integers _n_, _m_, and _k_ (2u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_m_u2009≤u2009_n_, 1u2009≤u2009_k_u2009≤u2009100)xa0— the number of houses in the village, the house where the girl lives, and the amount of money Zane has (in dollars), respectively. The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a__i_u2009≤u2009100)xa0— denoting the availability and the prices of the houses. It is guaranteed that _a__m_u2009=u20090 and that it is possible to purchase some house with no more than _k_ dollars. Output Print one integerxa0— the minimum distance, in meters, from the house where the girl Zane likes lives to the house Zane can buy. Examples Input 7 3 50 62 0 0 0 99 33 22 Input 10 5 100 1 0 1 0 0 0 0 0 1 1 Note In the first sample, with _k_u2009=u200920 dollars, Zane can buy only house 5. The distance from house _m_u2009=u20091 to house 5 is 10u2009+u200910u2009+u200910u2009+u200910u2009=u200940 meters. In the second sample, Zane can buy houses 6 and 7. It is better to buy house 6 than house 7, since house _m_u2009=u20093 and house 6 are only 30 meters away, while house _m_u2009=u20093 and house 7 are 40 meters away. | 800 | false | false | true | false | false | false | true | false | false | false | 6,593 |
670B | In late autumn evening _n_ robots gathered in the cheerful company of friends. Each robot has a unique identifierxa0— an integer from 1 to 109. At some moment, robots decided to play the game "Snowball". Below there are the rules of this game. First, all robots stand in a row. Then the first robot says his identifier. After that the second robot says the identifier of the first robot and then says his own identifier. Then the third robot says the identifier of the first robot, then says the identifier of the second robot and after that says his own. This process continues from left to right until the _n_-th robot says his identifier. Your task is to determine the _k_-th identifier to be pronounced. Input The first line contains two positive integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_k_u2009≤u2009_min_(2·109,u2009_n_·(_n_u2009+u20091)u2009/u20092). The second line contains the sequence _id_1,u2009_id_2,u2009...,u2009_id__n_ (1u2009≤u2009_id__i_u2009≤u2009109)xa0— identifiers of roborts. It is guaranteed that all identifiers are different. Output Print the _k_-th pronounced identifier (assume that the numeration starts from 1). Note In the first sample identifiers of robots will be pronounced in the following order: 1, 1, 2. As _k_u2009=u20092, the answer equals to 1. In the second test case identifiers of robots will be pronounced in the following order: 10, 10, 4, 10, 4, 18, 10, 4, 18, 3. As _k_u2009=u20095, the answer equals to 4. | 1,000 | false | false | true | false | false | false | false | false | false | false | 7,146 |
1599E | You are given two integer arrays of length $$$N$$$, $$$A1$$$ and $$$A2$$$. You are also given $$$Q$$$ queries of 4 types: 1 k l r x: set $$$Ak_i:=min(Ak_i, x)$$$ for each $$$l leq i leq r$$$. 2 k l r x: set $$$Ak_i:=max(Ak_i, x)$$$ for each $$$l leq i leq r$$$. 3 k l r x: set $$$Ak_i:=Ak_i+x$$$ for each $$$l leq i leq r$$$. 4 l r: find the $$$(sum_{i=l}^r F(A1_i+A2_i)) % (10^9+7)$$$ where $$$F(k)$$$ is the $$$k$$$-th Fibonacci number ($$$F(0)=0, F(1)=1, F(k)=F(k-1)+F(k-2)$$$), and $$$x % y$$$ denotes the remainder of the division of $$$x$$$ by $$$y$$$. You should process these queries and answer each query of the fourth type. Input The first line contains two integers $$$N$$$ and $$$Q$$$. ($$$1 leq N, Q leq 5 imes 10^4$$$) The second line contains $$$N$$$ integers, array $$$A1_1, A1_2, dots A1_N$$$. ($$$0 leq A1_i leq 10^6$$$) The third line contains $$$N$$$ integers, array $$$A2_1, A2_2, dots A2_N$$$. ($$$0 leq A2_i leq 10^6$$$) The next $$$Q$$$ lines describe the queries. Each line contains 5 or 3 integers, where the first integer denotes the type of the query. ($$$k in {1, 2}$$$, $$$1 leq l leq r leq N$$$) For queries of type 1 and 2, $$$0 leq x leq 10^9$$$ holds. For queries of type 3, $$$−10^6 leq x leq 10^6$$$ holds. It is guaranteed that after every query each number in arrays $$$A1$$$ and $$$A2$$$ will be nonnegative. Output Print the answer to each query of the fourth type, in separate lines. Examples Input 3 4 1 0 2 2 1 0 4 1 3 3 2 2 2 3 1 1 1 3 0 4 1 3 Input 5 4 1 3 5 3 2 4 2 1 3 3 4 1 3 4 2 5 2 1 2 4 6 4 2 4 Note In the first example: The answer for the first query is $$$F(1 + 2) + F(0 + 1) + F(2 + 0) = F(3) + F(1) + F(2) = 2 + 1 + 1 = 4$$$. After the second query, the array $$$A2$$$ changes to $$$[2, 4, 0]$$$. After the third query, the array $$$A1$$$ changes to $$$[0, 0, 0]$$$. The answer for the fourth query is $$$F(0 + 2) + F(0 + 4) + F(0 + 0) = F(2) + F(4) + F(0) = 1 + 3 + 0 = 4$$$. In the second example: The answer for the first query is $$$F(1 + 4) + F(3 + 2) + F(5 + 1) = F(5) + F(5) + F(6) = 5 + 5 + 8 = 18$$$. The answer for the second query is $$$F(3 + 2) + F(5 + 1) + F(3 + 3) + F(2 + 3) = F(5) + F(6) + F(6) + F(5) = 5 + 8 + 8 + 5 = 26$$$. After the third query, the array $$$A1$$$ changes to $$$[1, 6, 6, 6, 2]$$$. The answer for the fourth query is $$$F(6 + 2) + F(6 + 1) + F(6 + 3) = F(8) + F(7) + F(9) = 21 + 13 + 34 = 68$$$. | 3,200 | false | false | false | false | true | false | false | false | false | false | 2,660 |
1848E | In Vika's hometown, Vladivostok, there is a beautiful sea. Often you can see kids skimming stones. This is the process of throwing a stone into the sea at a small angle, causing it to fly far and bounce several times off the water surface. Vika has skimmed stones many times and knows that if you throw a stone from the shore perpendicular to the coastline with a force of $$$f$$$, it will first touch the water at a distance of $$$f$$$ from the shore, then bounce off and touch the water again at a distance of $$$f - 1$$$ from the previous point of contact. The stone will continue to fly in a straight line, reducing the distances between the points where it touches the water, until it falls into the sea. Formally, the points at which the stone touches the water surface will have the following coordinates: $$$f$$$, $$$f + (f - 1)$$$, $$$f + (f - 1) + (f - 2)$$$, ... , $$$f + (f - 1) + (f - 2) + ldots + 1$$$ (assuming that $$$0$$$ is the coordinate of the shoreline). Once, while walking along the embankment of Vladivostok in the evening, Vika saw a group of guys skipping stones across the sea, launching them from the same point with different forces. She became interested in what is the maximum number of guys who can launch a stone with their force $$$f_i$$$, so that all $$$f_i$$$ are different positive integers, and all $$$n$$$ stones touched the water at the point with the coordinate $$$x$$$ (assuming that $$$0$$$ is the coordinate of the shoreline). After thinking a little, Vika answered her question. After that, she began to analyze how the answer to her question would change if she multiplied the coordinate $$$x$$$ by some positive integers $$$x_1$$$, $$$x_2$$$, ... , $$$x_q$$$, which she picked for analysis. Vika finds it difficult to cope with such analysis on her own, so she turned to you for help. Formally, Vika is interested in the answer to her question for the coordinates $$$X_1 = x cdot x_1$$$, $$$X_2 = X_1 cdot x_2$$$, ... , $$$X_q = X_{q-1} cdot x_q$$$. Since the answer for such coordinates can be quite large, find it modulo $$$M$$$. It is guaranteed that $$$M$$$ is prime. Input The first line of the input contains three integers $$$x$$$ ($$$1 le x le 10^9$$$), $$$q$$$ ($$$1 le q le 10^5$$$) and $$$M$$$ ($$$100 le M le 2 cdot 10^9$$$)xa0— the initial coordinate for which Vika answered the question on her own, the number of integers $$$x_i$$$ by which Vika will multiply the initial coordinate and prime module $$$M$$$. The second line of the input contains $$$q$$$ integers $$$x_1, x_2, x_3, ldots, x_q$$$ ($$$1 le x_i le 10^6$$$)xa0— the integers described in the statement. Note In the first sample, to make the stone touch the water at a point with coordinate $$$2$$$, it needs to be thrown with a force of $$$2$$$. To make the stone touch the water at a point with coordinate $$$2 cdot 3 = 6$$$, it needs to be thrown with a force of $$$3$$$ or $$$6$$$. In the second sample, you can skim a stone with a force of $$$5$$$ or $$$14$$$ to make it touch the water at a point with coordinate $$$7 cdot 2 = 14$$$. For the coordinate $$$14 cdot 13 = 182$$$, there are $$$4$$$ possible forces: $$$20$$$, $$$29$$$, $$$47$$$, $$$182$$$. | 2,600 | true | false | true | false | false | false | true | false | false | false | 1,190 |
544A | Problem - 544A - 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 and the first characters of these strings are distinct. Find any beautiful sequence of strings or determine that the beautiful sequence doesn't exist. Input The first line contains a positive integer _k_ (1u2009≤u2009_k_u2009≤u200926) — the number of strings that should be in a beautiful sequence. The second line contains string _q_, consisting of lowercase Latin letters. The length of the string is within range from 1 to 100, inclusive. Output If such sequence doesn't exist, then print in a single line "NO" (without the quotes). Otherwise, print in the first line "YES" (without the quotes) and in the next _k_ lines print the beautiful sequence of strings _s_1,u2009_s_2,u2009...,u2009_s__k_. If there are multiple possible answers, print any of them. Examples Input 1 abca Output YES abca Input 2 aaacas Output YES aaa cas Input 4 abc Output NO Note In the second sample there are two possible answers: {"_aaaca_",u2009"_s_"} and {"_aaa_",u2009"_cas_"}. | 1,100 | false | false | true | false | false | false | false | false | false | false | 7,664 |
1992G | K1o0n gave you an array $$$a$$$ of length $$$n$$$, consisting of numbers $$$1, 2, ldots, n$$$. Accept it? Of course! But what to do with it? Of course, calculate $$$ ext{MEOW}(a)$$$. Let $$$ ext{MEX}(S, k)$$$ be the $$$k$$$-th positive (strictly greater than zero) integer in ascending order that is not present in the set $$$S$$$. Denote $$$ ext{MEOW}(a)$$$ as the sum of $$$ ext{MEX}(b, b + 1)$$$, over all distinct subsets $$$b$$$ of the array $$$a$$$. Examples of $$$ ext{MEX}(S, k)$$$ values for sets: $$$ ext{MEX}({3,2}, 1) = 1$$$, because $$$1$$$ is the first positive integer not present in the set; $$$ ext{MEX}({4,2,1}, 2) = 5$$$, because the first two positive integers not present in the set are $$$3$$$ and $$$5$$$; $$$ ext{MEX}({}, 4) = 4$$$, because there are no numbers in the empty set, so the first $$$4$$$ positive integers not present in it are $$$1, 2, 3, 4$$$. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. In a single line of each test case, an integer $$$n$$$ ($$$1 le n le 5000$$$) is entered, the size of the array of gifted numbers. It is guaranteed that the sum of $$$n^2$$$ over all test cases does not exceed $$$25 cdot 10^6$$$. Output For each test case, output a single numberxa0— $$$ ext{MEOW}(a)$$$. Since it may be very large, output it modulo $$$10^9 + 7$$$. Example Output 12 31 354226409 184 4 | 2,000 | true | false | false | true | false | false | false | false | false | false | 313 |
1753A1 | This is the easy version of the problem. The difference is that in this version the array can not contain zeros. You can make hacks only if both versions of the problem are solved. You are given an array $$$[a_1, a_2, ldots a_n]$$$ consisting of integers $$$-1$$$ and $$$1$$$. You have to build a partition of this array into the set of segments $$$[l_1, r_1], [l_2, r_2], ldots, [l_k, r_k]$$$ with the following property: Denote the alternating sum of all elements of the $$$i$$$-th segment as $$$s_i$$$: $$$s_i$$$ = $$$a_{l_i} - a_{l_i+1} + a_{l_i+2} - a_{l_i+3} + ldots pm a_{r_i}$$$. For example, the alternating sum of elements of segment $$$[2, 4]$$$ in array $$$[1, 0, -1, 1, 1]$$$ equals to $$$0 - (-1) + 1 = 2$$$. The sum of $$$s_i$$$ over all segments of partition should be equal to zero. Note that each $$$s_i$$$ does not have to be equal to zero, this property is about sum of $$$s_i$$$ over all segments of partition. The set of segments $$$[l_1, r_1], [l_2, r_2], ldots, [l_k, r_k]$$$ is called a partition of the array $$$a$$$ of length $$$n$$$ if $$$1 = l_1 le r_1, l_2 le r_2, ldots, l_k le r_k = n$$$ and $$$r_i + 1 = l_{i+1}$$$ for all $$$i = 1, 2, ldots k-1$$$. In other words, each element of the array must belong to exactly one segment. You have to build a partition of the given array with properties described above or determine that such partition does not exist. Note that it is not required to minimize the number of segments in the partition. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10,000$$$). Description of the test cases follows. The first line of each test case contains an integer $$$n$$$ ($$$1 le n le 200,000$$$)xa0— the length of the array $$$a$$$. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$a_i$$$ is $$$-1$$$ or $$$1$$$)xa0— the elements of the given array. It's guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$200,000$$$. Output For each test case, if required partition does not exist, print $$$-1$$$. Otherwise, print an integer $$$k$$$xa0— the number of segments in the partition. Then in the $$$i$$$-th of the following $$$k$$$ lines print two integers $$$l_i$$$ and $$$r_i$$$xa0— description of the $$$i$$$-th segment. The following conditions should be satisfied: $$$l_i le r_i$$$ for each $$$i$$$ from $$$1$$$ to $$$k$$$. $$$l_{i + 1} = r_i + 1$$$ for each $$$i$$$ from $$$1$$$ to $$$(k - 1)$$$. $$$l_1 = 1, r_k = n$$$. If there are multiple correct partitions of the array, print any of them. Note In the first test case we can build a partition of one segment of length $$$4$$$. The sum of this segment will be equal to $$$1 - 1 + 1 - 1 = 0$$$. In the second test case we can build a partition of two segments of length $$$3$$$. The sum of the first segment will be equal to $$$-1 -1 + 1 = -1$$$, and the sum of the second segment: $$$1 - 1 + 1 = 1$$$. So, the total sum will be equal to $$$-1 + 1 = 0$$$. In the third and in the fourth test cases it can be proved that there are no required partition. | 1,300 | false | true | false | true | false | true | false | false | false | false | 1,777 |
1422E | Some time ago Lesha found an entertaining string $$$s$$$ consisting of lowercase English letters. Lesha immediately developed an unique algorithm for this string and shared it with you. The algorithm is as follows. Lesha chooses an arbitrary (possibly zero) number of pairs on positions $$$(i, i + 1)$$$ in such a way that the following conditions are satisfied: for each pair $$$(i, i + 1)$$$ the inequality $$$0 le i < s - 1$$$ holds; for each pair $$$(i, i + 1)$$$ the equality $$$s_i = s_{i + 1}$$$ holds; there is no index that is contained in more than one pair. After that Lesha removes all characters on indexes contained in these pairs and the algorithm is over. Lesha is interested in the lexicographically smallest strings he can obtain by applying the algorithm to the suffixes of the given string. Input The only line contains the string $$$s$$$ ($$$1 le s le 10^5$$$) — the initial string consisting of lowercase English letters only. Output In $$$s$$$ lines print the lengths of the answers and the answers themselves, starting with the answer for the longest suffix. The output can be large, so, when some answer is longer than $$$10$$$ characters, instead print the first $$$5$$$ characters, then "...", then the last $$$2$$$ characters of the answer. Examples Output 3 abc 2 bc 1 c 0 1 d Input abbcdddeaaffdfouurtytwoo Output 18 abbcd...tw 17 bbcdd...tw 16 bcddd...tw 15 cddde...tw 14 dddea...tw 13 ddeaa...tw 12 deaad...tw 11 eaadf...tw 10 aadfortytw 9 adfortytw 8 dfortytw 9 fdfortytw 8 dfortytw 7 fortytw 6 ortytw 5 rtytw 6 urtytw 5 rtytw 4 tytw 3 ytw 2 tw 1 w 0 1 o Note Consider the first example. The longest suffix is the whole string "abcdd". Choosing one pair $$$(4, 5)$$$, Lesha obtains "abc". The next longest suffix is "bcdd". Choosing one pair $$$(3, 4)$$$, we obtain "bc". The next longest suffix is "cdd". Choosing one pair $$$(2, 3)$$$, we obtain "c". The next longest suffix is "dd". Choosing one pair $$$(1, 2)$$$, we obtain "" (an empty string). The last suffix is the string "d". No pair can be chosen, so the answer is "d". In the second example, for the longest suffix "abbcdddeaaffdfouurtytwoo" choose three pairs $$$(11, 12)$$$, $$$(16, 17)$$$, $$$(23, 24)$$$ and we obtain "abbcdddeaadfortytw | 2,700 | false | true | true | true | false | false | false | false | false | false | 3,565 |
1548D2 | This is the hard version of the problem. The only difference from the easy version is that in this version the coordinates can be both odd and even. There are $$$n$$$ fence-posts at distinct coordinates on a plane. It is guaranteed that no three fence posts lie on the same line. There are an infinite number of cows on the plane, one at every point with integer coordinates. Gregor is a member of the Illuminati, and wants to build a triangular fence, connecting $$$3$$$ distinct existing fence posts. A cow strictly inside the fence is said to be enclosed. If there are an odd number of enclosed cows and the area of the fence is an integer, the fence is said to be interesting. Find the number of interesting fences. Input The first line contains the integer $$$n$$$ ($$$3 le n le 6000$$$), the number of fence posts which Gregor can choose to form the vertices of a fence. Each of the next $$$n$$$ line contains two integers $$$x$$$ and $$$y$$$ ($$$0 le x,y le 10^7$$$, where $$$(x,y)$$$ is the coordinate of a fence post. All fence posts lie at distinct coordinates. No three fence posts are on the same line. Output Print a single integer, the number of interesting fences. Two fences are considered different if they are constructed with a different set of three fence posts. Examples Input 10 170 59 129 54 5 98 129 37 58 193 154 58 24 3 13 138 136 144 174 150 Note In the first example, there is only $$$1$$$ fence. That fence is interesting since its area is $$$4$$$ and there is $$$1$$$ enclosed cow, marked in red. In the second example, there are $$$4$$$ possible fences. Only one of them is interesting however. That fence has an area of $$$8$$$ and $$$5$$$ enclosed cows. | 3,300 | true | false | false | false | false | false | true | false | false | false | 2,902 |
721E | Once Danil the student was returning home from tram stop lately by straight road of length _L_. The stop is located at the point _x_u2009=u20090, but the Danil's homexa0— at the point _x_u2009=u2009_L_. Danil goes from _x_u2009=u20090 to _x_u2009=u2009_L_ with a constant speed and does not change direction of movement. There are _n_ street lights at the road, each of which lights some continuous segment of the road. All of the _n_ lightened segments do not share common points. Danil loves to sing, thus he wants to sing his favourite song over and over again during his walk. As soon as non-lightened segments of the road scare him, he sings only when he goes through the lightened segments. Danil passes distance _p_ while performing his favourite song once. Danil can't start another performance if the segment passed while performing is not fully lightened. Moreover, if Danil has taken a pause between two performances, he is not performing while not having passed a segment of length at least _t_. Formally, 1. Danil can start single performance at a point _x_ only if every point of segment [_x_,u2009_x_u2009+u2009_p_] is lightened; 2. If Danil has finished performing at a point _x_u2009+u2009_p_, then the next performance can be started only at a point _y_ such that _y_u2009=u2009_x_u2009+u2009_p_ or _y_u2009≥u2009_x_u2009+u2009_p_u2009+u2009_t_ satisfying the statement under the point 1. Blue half-circles denote performances. Please note that just after Danil has taken a pause in performing, he has not sang for a path of length of at least _t_. Determine how many times Danil can perform his favourite song during his walk from _x_u2009=u20090 to _x_u2009=u2009_L_. Please note that Danil does not break a single performance, thus, started singing another time, he finishes singing when having a segment of length of _p_ passed from the performance start point. Input The first line of the input contains four integers _L_, _n_, _p_ and _t_ (1u2009≤u2009_L_u2009≤u2009109, 0u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_p_u2009≤u2009109, 1u2009≤u2009_t_u2009≤u2009109)xa0— the length of the Danil's path, the number of street lights at the road, the distance Danil passes while doing single performance and the minimum distance of pause respectively. The next _n_ lines describe segments lightened by street lights. _i_-th of them contains two integers _l__i_,u2009_r__i_ (0u2009≤u2009_l__i_u2009<u2009_r__i_u2009≤u2009_L_)xa0— the endpoints of the segment lightened by _i_-th street light. It is guaranteed that no two segments are intersecting, nesting, or touching each other. The segments are given in the order from left to right. | 2,700 | false | false | false | true | false | false | false | true | false | false | 6,927 |
85E | In a far away kingdom lives a very greedy king. To defend his land, he built _n_ guard towers. Apart from the towers the kingdom has two armies, each headed by a tyrannical and narcissistic general. The generals can't stand each other, specifically, they will never let soldiers of two armies be present in one tower. During defence operations to manage a guard tower a general has to send part of his army to that tower. Each general asks some fee from the king for managing towers. As they live in a really far away kingdom, each general evaluates his fee in the following weird manner: he finds two remotest (the most distant) towers, where the soldiers of his army are situated and asks for the fee equal to the distance. Each tower is represented by a point on the plane with coordinates (_x_,u2009_y_), and the distance between two points with coordinates (_x_1,u2009_y_1) and (_x_2,u2009_y_2) is determined in this kingdom as _x_1u2009-u2009_x_2u2009+u2009_y_1u2009-u2009_y_2. The greedy king was not exactly satisfied with such a requirement from the generals, that's why he only agreed to pay one fee for two generals, equal to the maximum of two demanded fees. However, the king is still green with greed, and among all the ways to arrange towers between armies, he wants to find the cheapest one. Each tower should be occupied by soldiers of exactly one army. He hired you for that. You should find the minimum amount of money that will be enough to pay the fees. And as the king is also very scrupulous, you should also count the number of arrangements that will cost the same amount of money. As their number can be quite large, it is enough for the king to know it as a remainder from dividing by 109u2009+u20097. Two arrangements are distinct if the sets of towers occupied by soldiers of the first general are distinct. Input The first line contains an integer _n_ (2u2009≤u2009_n_u2009≤u20095000), _n_ is the number of guard towers. Then follow _n_ lines, each of which contains two integers _x_,u2009_y_ — the coordinates of the _i_-th tower (0u2009≤u2009_x_,u2009_y_u2009≤u20095000). No two towers are present at one point. Pretest 6 is one of the maximal tests for this problem. Output Print on the first line the smallest possible amount of money that will be enough to pay fees to the generals. Print on the second line the number of arrangements that can be carried out using the smallest possible fee. This number should be calculated modulo 1000000007 (109u2009+u20097). Note In the first example there are only two towers, the distance between which is equal to 2. If we give both towers to one general, then we well have to pay 2 units of money. If each general receives a tower to manage, to fee will be equal to 0. That is the smallest possible fee. As you can easily see, we can obtain it in two ways. | 2,600 | false | false | false | false | false | false | false | true | true | true | 9,542 |
1114B | An array $$$b$$$ is called to be a subarray of $$$a$$$ if it forms a continuous subsequence of $$$a$$$, that is, if it is equal to $$$a_l$$$, $$$a_{l + 1}$$$, $$$ldots$$$, $$$a_r$$$ for some $$$l, r$$$. Suppose $$$m$$$ is some known constant. For any array, having $$$m$$$ or more elements, let's define it's beauty as the sum of $$$m$$$ largest elements of that array. For example: For array $$$x = [4, 3, 1, 5, 2]$$$ and $$$m = 3$$$, the $$$3$$$ largest elements of $$$x$$$ are $$$5$$$, $$$4$$$ and $$$3$$$, so the beauty of $$$x$$$ is $$$5 + 4 + 3 = 12$$$. For array $$$x = [10, 10, 10]$$$ and $$$m = 2$$$, the beauty of $$$x$$$ is $$$10 + 10 = 20$$$. You are given an array $$$a_1, a_2, ldots, a_n$$$, the value of the said constant $$$m$$$ and an integer $$$k$$$. Your need to split the array $$$a$$$ into exactly $$$k$$$ subarrays such that: Each element from $$$a$$$ belongs to exactly one subarray. Each subarray has at least $$$m$$$ elements. The sum of all beauties of $$$k$$$ subarrays is maximum possible. Input The first line contains three integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$2 le n le 2 cdot 10^5$$$, $$$1 le m$$$, $$$2 le k$$$, $$$m cdot k le n$$$)xa0— the number of elements in $$$a$$$, the constant $$$m$$$ in the definition of beauty and the number of subarrays to split to. The second line contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$-10^9 le a_i le 10^9$$$). Output In the first line, print the maximum possible sum of the beauties of the subarrays in the optimal partition. In the second line, print $$$k-1$$$ integers $$$p_1, p_2, ldots, p_{k-1}$$$ ($$$1 le p_1 < p_2 < ldots < p_{k-1} < n$$$) representing the partition of the array, in which: All elements with indices from $$$1$$$ to $$$p_1$$$ belong to the first subarray. All elements with indices from $$$p_1 + 1$$$ to $$$p_2$$$ belong to the second subarray. $$$ldots$$$. All elements with indices from $$$p_{k-1} + 1$$$ to $$$n$$$ belong to the last, $$$k$$$-th subarray. If there are several optimal partitions, print any of them. Examples Input 9 2 3 5 2 5 2 4 1 1 3 2 Input 2 1 2 -1000000000 1000000000 Note In the first example, one of the optimal partitions is $$$[5, 2, 5]$$$, $$$[2, 4]$$$, $$$[1, 1, 3, 2]$$$. The beauty of the subarray $$$[5, 2, 5]$$$ is $$$5 + 5 = 10$$$. The beauty of the subarray $$$[2, 4]$$$ is $$$2 + 4 = 6$$$. The beauty of the subarray $$$[1, 1, 3, 2]$$$ is $$$3 + 2 = 5$$$. The sum of their beauties is $$$10 + 6 + 5 = 21$$$. In the second example, one optimal partition is $$$[4]$$$, $$$[1, 3]$$$, $$$[2, 2]$$$, $$$[3]$$$. | 1,500 | false | true | false | false | false | true | false | false | true | false | 5,133 |
932F | You are given a tree with _n_ nodes (numbered from 1 to _n_) rooted at node 1. Also, each node has two values associated with it. The values for _i_-th node are _a__i_ and _b__i_. You can jump from a node to any node in its subtree. The cost of one jump from node _x_ to node _y_ is the product of _a__x_ and _b__y_. The total cost of a path formed by one or more jumps is sum of costs of individual jumps. For every node, calculate the minimum total cost to reach any leaf from that node. Pay attention, that root can never be leaf, even if it has degree 1. Note that you cannot jump from a node to itself. Input The first line of input contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009105)xa0— the number of nodes in the tree. The second line contains _n_ space-separated integers _a_1,u2009u2009_a_2,u2009u2009...,u2009u2009_a__n_(u2009-u2009105u2009u2009≤u2009u2009_a__i_u2009u2009≤u2009u2009105). The third line contains _n_ space-separated integers _b_1,u2009u2009_b_2,u2009u2009...,u2009u2009_b__n_(u2009-u2009105u2009u2009≤u2009u2009_b__i_u2009u2009≤u2009u2009105). Next _n_u2009u2009-u2009u20091 lines contains two space-separated integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009u2009_v__i_u2009≤u2009u2009_n_) describing edge between nodes _u__i_ and _v__i_ in the tree. Output Output _n_ space-separated integers, _i_-th of which denotes the minimum cost of a path from node _i_ to reach any leaf. Examples Input 3 2 10 -1 7 -7 5 2 3 2 1 Input 4 5 -10 5 7 -8 -80 -3 -10 2 1 2 4 1 3 Note In the first example, node 3 is already a leaf, so the cost is 0. For node 2, jump to node 3 with cost _a_2u2009×u2009_b_3u2009=u200950. For node 1, jump directly to node 3 with cost _a_1u2009×u2009_b_3u2009=u200910. In the second example, node 3 and node 4 are leaves, so the cost is 0. For node 2, jump to node 4 with cost _a_2u2009×u2009_b_4u2009=u2009100. For node 1, jump to node 2 with cost _a_1u2009×u2009_b_2u2009=u2009u2009-u2009400 followed by a jump from 2 to 4 with cost _a_2u2009×u2009_b_4u2009=u2009100. | 2,700 | false | false | false | true | true | false | false | false | false | false | 5,973 |
1097G | A great legend used to be here, but some troll hacked Codeforces and erased it. Too bad for us, but in the troll society he earned a title of an ultimate-greatest-over troll. At least for them, it's something good. And maybe a formal statement will be even better for us? You are given a tree $$$T$$$ with $$$n$$$ vertices numbered from $$$1$$$ to $$$n$$$. For every non-empty subset $$$X$$$ of vertices of $$$T$$$, let $$$f(X)$$$ be the minimum number of edges in the smallest connected subtree of $$$T$$$ which contains every vertex from $$$X$$$. You're also given an integer $$$k$$$. You need to compute the sum of $$$(f(X))^k$$$ among all non-empty subsets of vertices, that is: $$$$$$ sumlimits_{X subseteq {1, 2,: dots :, n},, X eq varnothing} (f(X))^k. $$$$$$ As the result might be very large, output it modulo $$$10^9 + 7$$$. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$2 leq n leq 10^5$$$, $$$1 leq k leq 200$$$) — the size of the tree and the exponent in the sum above. Each of the following $$$n - 1$$$ lines contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 leq a_i,b_i leq n$$$) — the indices of the vertices connected by the corresponding edge. It is guaranteed, that the edges form a tree. Output Print a single integer — the requested sum modulo $$$10^9 + 7$$$. Note In the first two examples, the values of $$$f$$$ are as follows: $$$f({1}) = 0$$$ $$$f({2}) = 0$$$ $$$f({1, 2}) = 1$$$ $$$f({3}) = 0$$$ $$$f({1, 3}) = 2$$$ $$$f({2, 3}) = 1$$$ $$$f({1, 2, 3}) = 2$$$ $$$f({4}) = 0$$$ $$$f({1, 4}) = 2$$$ $$$f({2, 4}) = 1$$$ $$$f({1, 2, 4}) = 2$$$ $$$f({3, 4}) = 2$$$ $$$f({1, 3, 4}) = 3$$$ $$$f({2, 3, 4}) = 2$$$ $$$f({1, 2, 3, 4}) = 3$$$ | 3,000 | false | false | false | true | false | false | false | false | false | false | 5,218 |
553C | There are many anime that are about "love triangles": Alice loves Bob, and Charlie loves Bob as well, but Alice hates Charlie. You are thinking about an anime which has _n_ characters. The characters are labeled from 1 to _n_. Every pair of two characters can either mutually love each other or mutually hate each other (there is no neutral state). You hate love triangles (A-B are in love and B-C are in love, but A-C hate each other), and you also hate it when nobody is in love. So, considering any three characters, you will be happy if exactly one pair is in love (A and B love each other, and C hates both A and B), or if all three pairs are in love (A loves B, B loves C, C loves A). You are given a list of _m_ known relationships in the anime. You know for sure that certain pairs love each other, and certain pairs hate each other. You're wondering how many ways you can fill in the remaining relationships so you are happy with every triangle. Two ways are considered different if two characters are in love in one way but hate each other in the other. Print this count modulo 1u2009000u2009000u2009007. Input The first line of input will contain two integers _n_,u2009_m_ (3u2009≤u2009_n_u2009≤u2009100u2009000, 0u2009≤u2009_m_u2009≤u2009100u2009000). The next _m_ lines will contain the description of the known relationships. The _i_-th line will contain three integers _a__i_,u2009_b__i_,u2009_c__i_. If _c__i_ is 1, then _a__i_ and _b__i_ are in love, otherwise, they hate each other (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_, _a__i_u2009≠u2009_b__i_, ). Each pair of people will be described no more than once. Output Print a single integer equal to the number of ways to fill in the remaining pairs so that you are happy with every triangle modulo 1u2009000u2009000u2009007. Examples Input 4 4 1 2 1 2 3 1 3 4 0 4 1 0 Input 4 4 1 2 1 2 3 1 3 4 0 4 1 1 Note In the first sample, the four ways are to: Make everyone love each other Make 1 and 2 love each other, and 3 hate 1 and 2 (symmetrically, we get 3 ways from this). In the second sample, the only possible solution is to make 1 and 3 love each other and 2 and 4 hate each other. | 2,200 | false | false | false | false | false | false | false | false | false | true | 7,620 |
715E | ZS the Coder is given two permutations _p_ and _q_ of {1,u20092,u2009...,u2009_n_}, but some of their elements are replaced with 0. The distance between two permutations _p_ and _q_ is defined as the minimum number of moves required to turn _p_ into _q_. A move consists of swapping exactly 2 elements of _p_. ZS the Coder wants to determine the number of ways to replace the zeros with positive integers from the set {1,u20092,u2009...,u2009_n_} such that _p_ and _q_ are permutations of {1,u20092,u2009...,u2009_n_} and the distance between _p_ and _q_ is exactly _k_. ZS the Coder wants to find the answer for all 0u2009≤u2009_k_u2009≤u2009_n_u2009-u20091. Can you help him? Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009250)xa0— the number of elements in the permutations. The second line contains _n_ integers, _p_1,u2009_p_2,u2009...,u2009_p__n_ (0u2009≤u2009_p__i_u2009≤u2009_n_)xa0— the permutation _p_. It is guaranteed that there is at least one way to replace zeros such that _p_ is a permutation of {1,u20092,u2009...,u2009_n_}. The third line contains _n_ integers, _q_1,u2009_q_2,u2009...,u2009_q__n_ (0u2009≤u2009_q__i_u2009≤u2009_n_)xa0— the permutation _q_. It is guaranteed that there is at least one way to replace zeros such that _q_ is a permutation of {1,u20092,u2009...,u2009_n_}. Output Print _n_ integers, _i_-th of them should denote the answer for _k_u2009=u2009_i_u2009-u20091. Since the answer may be quite large, and ZS the Coder loves weird primes, print them modulo 998244353u2009=u2009223·7·17u2009+u20091, which is a prime. Examples Input 6 1 3 2 5 4 6 6 4 5 1 0 0 Note In the first sample case, there is the only way to replace zeros so that it takes 0 swaps to convert _p_ into _q_, namely _p_u2009=u2009(1,u20092,u20093),u2009_q_u2009=u2009(1,u20092,u20093). There are two ways to replace zeros so that it takes 1 swap to turn _p_ into _q_. One of these ways is _p_u2009=u2009(1,u20092,u20093),u2009_q_u2009=u2009(3,u20092,u20091), then swapping 1 and 3 from _p_ transform it into _q_. The other way is _p_u2009=u2009(1,u20093,u20092),u2009_q_u2009=u2009(1,u20092,u20093). Swapping 2 and 3 works in this case. Finally, there is one way to replace zeros so that it takes 2 swaps to turn _p_ into _q_, namely _p_u2009=u2009(1,u20093,u20092),u2009_q_u2009=u2009(3,u20092,u20091). Then, we can transform _p_ into _q_ like following: . | 3,400 | true | false | false | false | false | false | false | false | false | true | 6,955 |
1676F | Given an array $$$a$$$ of length $$$n$$$ and an integer $$$k$$$, you are tasked to find any two numbers $$$l$$$ and $$$r$$$ ($$$l leq r$$$) such that: For each $$$x$$$ $$$(l leq x leq r)$$$, $$$x$$$ appears in $$$a$$$ at least $$$k$$$ times (i.e. $$$k$$$ or more array elements are equal to $$$x$$$). The value $$$r-l$$$ is maximized. If no numbers satisfy the conditions, output -1. For example, if $$$a=[11, 11, 12, 13, 13, 14, 14]$$$ and $$$k=2$$$, then: for $$$l=12$$$, $$$r=14$$$ the first condition fails because $$$12$$$ does not appear at least $$$k=2$$$ times. for $$$l=13$$$, $$$r=14$$$ the first condition holds, because $$$13$$$ occurs at least $$$k=2$$$ times in $$$a$$$ and $$$14$$$ occurs at least $$$k=2$$$ times in $$$a$$$. for $$$l=11$$$, $$$r=11$$$ the first condition holds, because $$$11$$$ occurs at least $$$k=2$$$ times in $$$a$$$. A pair of $$$l$$$ and $$$r$$$ for which the first condition holds and $$$r-l$$$ is maximal is $$$l = 13$$$, $$$r = 14$$$. Input The first line of the input contains a single integer $$$t$$$ ($$$1 le t le 1000$$$) — the number of test cases. The description of test cases follows. The first line of each test case contains the integers $$$n$$$ and $$$k$$$ ($$$1 le n le 2 cdot 10^5$$$, $$$1 leq k leq n$$$) — the length of the array $$$a$$$ and the minimum amount of times each number in the range $$$[l, r]$$$ should appear respectively. Then a single line follows, containing $$$n$$$ integers describing the array $$$a$$$ ($$$1 leq a_i leq 10^9$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Output For each test case output $$$2$$$ numbers, $$$l$$$ and $$$r$$$ that satisfy the conditions, or "-1" if no numbers satisfy the conditions. If multiple answers exist, you can output any. Example Input 4 7 2 11 11 12 13 13 14 14 5 1 6 3 5 2 1 6 4 4 3 4 3 3 4 14 2 1 1 2 2 2 3 3 3 3 4 4 4 4 4 | 1,300 | false | true | true | false | true | false | false | false | true | false | 2,210 |
505A | Mr. Kitayuta has kindly given you a string _s_ consisting of lowercase English letters. You are asked to insert exactly one lowercase English letter into _s_ to make it a palindrome. A palindrome is a string that reads the same forward and backward. For example, "noon", "testset" and "a" are all palindromes, while "test" and "kitayuta" are not. You can choose any lowercase English letter, and insert it to any position of _s_, possibly to the beginning or the end of _s_. You have to insert a letter even if the given string is already a palindrome. If it is possible to insert one lowercase English letter into _s_ so that the resulting string will be a palindrome, print the string after the insertion. Otherwise, print "NA" (without quotes, case-sensitive). In case there is more than one palindrome that can be obtained, you are allowed to print any of them. Input The only line of the input contains a string _s_ (1u2009≤u2009_s_u2009≤u200910). Each character in _s_ is a lowercase English letter. Output If it is possible to turn _s_ into a palindrome by inserting one lowercase English letter, print the resulting string in a single line. Otherwise, print "NA" (without quotes, case-sensitive). In case there is more than one solution, any of them will be accepted. Note For the first sample, insert 'r' to the end of "revive" to obtain a palindrome "reviver". For the second sample, there is more than one solution. For example, "eve" will also be accepted. For the third sample, it is not possible to turn "kitayuta" into a palindrome by just inserting one letter. | 1,100 | false | false | true | false | false | false | true | false | false | false | 7,823 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.