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
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
105A | In Disgaea as in most role-playing games, characters have skills that determine the character's ability to use certain weapons or spells. If the character does not have the necessary skill, he cannot use it. The skill level is represented as an integer that increases when you use this skill. Different character classes are characterized by different skills. Unfortunately, the skills that are uncommon for the given character's class are quite difficult to obtain. To avoid this limitation, there is the so-called transmigration. Transmigration is reincarnation of the character in a new creature. His soul shifts to a new body and retains part of his experience from the previous life. As a result of transmigration the new character gets all the skills of the old character and the skill levels are reduced according to the _k_ coefficient (if the skill level was equal to _x_, then after transmigration it becomes equal to [_kx_], where [_y_] is the integral part of _y_). If some skill's levels are strictly less than 100, these skills are forgotten (the character does not have them any more). After that the new character also gains the skills that are specific for his class, but are new to him. The levels of those additional skills are set to 0. Thus, one can create a character with skills specific for completely different character classes via transmigrations. For example, creating a mage archer or a thief warrior is possible. You are suggested to solve the following problem: what skills will the character have after transmigration and what will the levels of those skills be? Input The first line contains three numbers _n_, _m_ and _k_ — the number of skills the current character has, the number of skills specific for the class into which the character is going to transmigrate and the reducing coefficient respectively; _n_ and _m_ are integers, and _k_ is a real number with exactly two digits after decimal point (1u2009≤u2009_n_,u2009_m_u2009≤u200920, 0.01u2009≤u2009_k_u2009≤u20090.99). Then follow _n_ lines, each of which describes a character's skill in the form "_name_ _exp_" — the skill's name and the character's skill level: _name_ is a string and _exp_ is an integer in range from 0 to 9999, inclusive. Then follow _m_ lines each of which contains names of skills specific for the class, into which the character transmigrates. All names consist of lowercase Latin letters and their lengths can range from 1 to 20 characters, inclusive. All character's skills have distinct names. Besides the skills specific for the class into which the player transmigrates also have distinct names. Output Print on the first line number _z_ — the number of skills the character will have after the transmigration. Then print _z_ lines, on each of which print a skill's name and level, separated by a single space. The skills should be given in the lexicographical order. Examples Input 5 4 0.75 axe 350 impaler 300 ionize 80 megafire 120 magicboost 220 heal megafire shield magicboost Output 6 axe 262 heal 0 impaler 225 magicboost 165 megafire 0 shield 0 | 1,700 | false | false | true | false | false | false | false | false | false | false | 9,466 |
976F | You are given a bipartite graph _G_u2009=u2009(_U_,u2009_V_,u2009_E_), _U_ is the set of vertices of the first part, _V_ is the set of vertices of the second part and _E_ is the set of edges. There might be multiple edges. Let's call some subset of its edges _k_-covering iff the graph has each of its vertices incident to at least _k_ edges. Minimal _k_-covering is such a _k_-covering that the size of the subset is minimal possible. Your task is to find minimal _k_-covering for each , where _minDegree_ is the minimal degree of any vertex in graph _G_. Input The first line contains three integers _n_1, _n_2 and _m_ (1u2009≤u2009_n_1,u2009_n_2u2009≤u20092000, 0u2009≤u2009_m_u2009≤u20092000) — the number of vertices in the first part, the number of vertices in the second part and the number of edges, respectively. The _i_-th of the next _m_ lines contain two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_u2009≤u2009_n_1,u20091u2009≤u2009_v__i_u2009≤u2009_n_2) — the description of the _i_-th edge, _u__i_ is the index of the vertex in the first part and _v__i_ is the index of the vertex in the second part. Output For each print the subset of edges (minimal _k_-covering) in separate line. The first integer _cnt__k_ of the _k_-th line is the number of edges in minimal _k_-covering of the graph. Then _cnt__k_ integers follow — original indices of the edges which belong to the minimal _k_-covering, these indices should be pairwise distinct. Edges are numbered 1 through _m_ in order they are given in the input. Examples Input 3 3 7 1 2 2 3 1 3 3 2 3 3 2 1 2 1 Output 0 3 3 7 4 6 1 3 6 7 4 5 Input 1 1 5 1 1 1 1 1 1 1 1 1 1 Output 0 1 5 2 4 5 3 3 4 5 4 2 3 4 5 5 1 2 3 4 5 | 2,500 | false | false | false | false | false | false | false | false | false | true | 5,825 |
1479C | Some time ago Homer lived in a beautiful city. There were $$$n$$$ blocks numbered from $$$1$$$ to $$$n$$$ and $$$m$$$ directed roads between them. Each road had a positive length, and each road went from the block with the smaller index to the block with the larger index. For every two (different) blocks, there was at most one road between them. Homer discovered that for some two numbers $$$L$$$ and $$$R$$$ the city was $$$(L, R)$$$-continuous. The city is said to be $$$(L, R)$$$-continuous, if 1. all paths from block $$$1$$$ to block $$$n$$$ are of length between $$$L$$$ and $$$R$$$ (inclusive); and 2. for every $$$L leq d leq R$$$, there is exactly one path from block $$$1$$$ to block $$$n$$$ whose length is $$$d$$$. A path from block $$$u$$$ to block $$$v$$$ is a sequence $$$u = x_0 o x_1 o x_2 o dots o x_k = v$$$, where there is a road from block $$$x_{i-1}$$$ to block $$$x_{i}$$$ for every $$$1 leq i leq k$$$. The length of a path is the sum of lengths over all roads in the path. Two paths $$$x_0 o x_1 o dots o x_k$$$ and $$$y_0 o y_1 o dots o y_l$$$ are different, if $$$k eq l$$$ or $$$x_i eq y_i$$$ for some $$$0 leq i leq min{k, l}$$$. After moving to another city, Homer only remembers the two special numbers $$$L$$$ and $$$R$$$ but forgets the numbers $$$n$$$ and $$$m$$$ of blocks and roads, respectively, and how blocks are connected by roads. However, he believes the number of blocks should be no larger than $$$32$$$ (because the city was small). As the best friend of Homer, please tell him whether it is possible to find a $$$(L, R)$$$-continuous city or not. Input The single line contains two integers $$$L$$$ and $$$R$$$ ($$$1 leq L leq R leq 10^6$$$). Output If it is impossible to find a $$$(L, R)$$$-continuous city within $$$32$$$ blocks, print "NO" in a single line. Otherwise, print "YES" in the first line followed by a description of a $$$(L, R)$$$-continuous city. The second line should contain two integers $$$n$$$ ($$$2 leq n leq 32$$$) and $$$m$$$ ($$$1 leq m leq frac {n(n-1)} 2$$$), where $$$n$$$ denotes the number of blocks and $$$m$$$ denotes the number of roads. Then $$$m$$$ lines follow. The $$$i$$$-th of the $$$m$$$ lines should contain three integers $$$a_i$$$, $$$b_i$$$ ($$$1 leq a_i < b_i leq n$$$) and $$$c_i$$$ ($$$1 leq c_i leq 10^6$$$) indicating that there is a directed road from block $$$a_i$$$ to block $$$b_i$$$ of length $$$c_i$$$. It is required that for every two blocks, there should be no more than 1 road connecting them. That is, for every $$$1 leq i < j leq m$$$, either $$$a_i eq a_j$$$ or $$$b_i eq b_j$$$. Examples Output YES 5 6 1 2 3 1 3 4 1 4 5 2 5 1 3 5 1 4 5 1 Note In the first example there is only one path from block $$$1$$$ to block $$$n = 2$$$, and its length is $$$1$$$. In the second example there are three paths from block $$$1$$$ to block $$$n = 5$$$, which are $$$1 o 2 o 5$$$ of length $$$4$$$, $$$1 o 3 o 5$$$ of length $$$5$$$ and $$$1 o 4 o 5$$$ of length $$$6$$$. | 2,500 | false | false | false | false | false | true | false | false | false | false | 3,278 |
1305C | To become the king of Codeforces, Kuroni has to solve the following problem. He is given $$$n$$$ numbers $$$a_1, a_2, dots, a_n$$$. Help Kuroni to calculate $$$prod_{1le i<jle n} a_i - a_j$$$. As result can be very big, output it modulo $$$m$$$. If you are not familiar with short notation, $$$prod_{1le i<jle n} a_i - a_j$$$ is equal to $$$a_1 - a_2cdota_1 - a_3cdot$$$ $$$dots$$$ $$$cdota_1 - a_ncdota_2 - a_3cdota_2 - a_4cdot$$$ $$$dots$$$ $$$cdota_2 - a_n cdot$$$ $$$dots$$$ $$$cdot a_{n-1} - a_n$$$. In other words, this is the product of $$$a_i - a_j$$$ for all $$$1le i < j le n$$$. Input The first line contains two integers $$$n$$$, $$$m$$$ ($$$2le n le 2cdot 10^5$$$, $$$1le m le 1000$$$)xa0— number of numbers and modulo. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$0 le a_i le 10^9$$$). Output Output the single numberxa0— $$$prod_{1le i<jle n} a_i - a_j bmod m$$$. Note In the first sample, $$$8 - 5 = 3 equiv 3 bmod 10$$$. In the second sample, $$$1 - 4cdot1 - 5cdot4 - 5 = 3cdot 4 cdot 1 = 12 equiv 0 bmod 12$$$. In the third sample, $$$1 - 4cdot1 - 9cdot4 - 9 = 3 cdot 8 cdot 5 = 120 equiv 1 bmod 7$$$. | 1,600 | true | false | false | false | false | false | true | false | false | false | 4,162 |
167E | In some country live wizards. They like to make weird bets. Two wizards draw an acyclic directed graph with _n_ vertices and _m_ edges (the graph's vertices are numbered from 1 to _n_). A source is a vertex with no incoming edges, and a sink is the vertex with no outgoing edges. Note that a vertex could be the sink and the source simultaneously. In the wizards' graph the number of the sinks and the sources is the same. Wizards numbered the sources in the order of increasing numbers of the vertices from 1 to _k_. The sinks are numbered from 1 to _k_ in the similar way. To make a bet, they, as are real wizards, cast a spell, which selects a set of _k_ paths from all sources to the sinks in such a way that no two paths intersect at the vertices. In this case, each sink has exactly one path going to it from exactly one source. Let's suppose that the _i_-th sink has a path going to it from the _a__i_'s source. Then let's call pair (_i_,u2009_j_) an inversion if _i_u2009<u2009_j_ and _a__i_u2009>u2009_a__j_. If the number of inversions among all possible pairs (_i_,u2009_j_), such that (1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_k_), is even, then the first wizard wins (the second one gives him one magic coin). Otherwise, the second wizard wins (he gets one magic coin from the first one). Our wizards are captured with feverish excitement, so they kept choosing new paths again and again for so long that eventually they have chosen every possible set of paths for exactly once. The two sets of non-intersecting pathes are considered to be different, if and only if there is an edge, which lies at some path in one set and doesn't lie at any path of another set. To check their notes, they asked you to count the total winnings of the first player for all possible sets of paths modulo a prime number _p_. Input The first line contains three space-separated integers _n_, _m_, _p_ (1u2009≤u2009_n_u2009≤u2009600, 0u2009≤u2009_m_u2009≤u2009105, 2u2009≤u2009_p_u2009≤u2009109u2009+u20097). It is guaranteed that _p_ is prime number. Next _m_ lines contain edges of the graph. Each line contains a pair of space-separated integers, _a__i_xa0_b__i_ — an edge from vertex _a__i_ to vertex _b__i_. It is guaranteed that the graph is acyclic and that the graph contains the same number of sources and sinks. Please note that the graph can have multiple edges. Output Print the answer to the problem — the total winnings of the first player modulo a prime number _p_. Please note that the winnings may be negative, but the modulo residue must be non-negative (see the sample). Examples Input 4 4 1000003 2 1 2 4 3 1 3 4 Input 6 5 1000003 1 4 1 5 1 6 2 6 3 6 Note In the first sample, there is exactly one set of paths — . The number of inversions is 0, which is an even number. Therefore, the first wizard gets 1 coin. In the second sample there is exactly one set of paths — . There is exactly one inversion. Therefore, the first wizard gets -1 coin. . In the third sample, there are two sets of paths, which are counted with opposite signs. In the fourth sample there are no set of paths at all. In the fifth sample, there are three sources — the vertices with the numbers (2, 3, 5) and three sinks — the vertices with numbers (1, 2, 4). For a single set of paths are 2 inversions, that is, their number is even. | 2,900 | true | false | false | false | false | false | false | false | false | true | 9,191 |
467A | Problem - 467A - 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 . George has a friend Alex who has also entered the university. Now they are moving into a dormitory. George and Alex want to live in the same room. The dormitory has _n_ rooms in total. At the moment the _i_-th room has _p__i_ people living in it and the room can accommodate _q__i_ people in total (_p__i_u2009≤u2009_q__i_). Your task is to count how many rooms has free place for both George and Alex. Input The first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100) — the number of rooms. The _i_-th of the next _n_ lines contains two integers _p__i_ and _q__i_ (0u2009≤u2009_p__i_u2009≤u2009_q__i_u2009≤u2009100) — the number of people who already live in the _i_-th room and the room's capacity. Output Print a single integer — the number of rooms where George and Alex can move in. Examples Input 3 1 1 2 2 3 3 Output 0 Input 3 1 10 0 10 10 10 Output 2 | 800 | false | false | true | false | false | false | false | false | false | false | 7,972 |
909A | The preferred way to generate user login in Polygon is to concatenate a prefix of the user's first name and a prefix of their last name, in that order. Each prefix must be non-empty, and any of the prefixes can be the full name. Typically there are multiple possible logins for each person. You are given the first and the last name of a user. Return the alphabetically earliest login they can get (regardless of other potential Polygon users). As a reminder, a prefix of a string _s_ is its substring which occurs at the beginning of _s_: "a", "ab", "abc" etc. are prefixes of string "{abcdef}" but "b" and 'bc" are not. A string _a_ is alphabetically earlier than a string _b_, if _a_ is a prefix of _b_, or _a_ and _b_ coincide up to some position, and then _a_ has a letter that is alphabetically earlier than the corresponding letter in _b_: "a" and "ab" are alphabetically earlier than "ac" but "b" and "ba" are alphabetically later than "ac". Input The input consists of a single line containing two space-separated strings: the first and the last names. Each character of each string is a lowercase English letter. The length of each string is between 1 and 10, inclusive. Output Output a single stringxa0— alphabetically earliest possible login formed from these names. The output should be given in lowercase as well. | 1,000 | false | true | false | false | false | false | true | false | true | false | 6,112 |
1068A | Ivan is collecting coins. There are only $$$N$$$ different collectible coins, Ivan has $$$K$$$ of them. He will be celebrating his birthday soon, so all his $$$M$$$ freinds decided to gift him coins. They all agreed to three terms: Everyone must gift as many coins as others. All coins given to Ivan must be different. Not less than $$$L$$$ coins from gifts altogether, must be new in Ivan's collection. But his friends don't know which coins have Ivan already got in his collection. They don't want to spend money so they want to buy minimum quantity of coins, that satisfy all terms, irrespective of the Ivan's collection. Help them to find this minimum number of coins or define it's not possible to meet all the terms. Input The only line of input contains 4 integers $$$N$$$, $$$M$$$, $$$K$$$, $$$L$$$ ($$$1 le K le N le 10^{18}$$$; $$$1 le M, ,, L le 10^{18}$$$)xa0— quantity of different coins, number of Ivan's friends, size of Ivan's collection and quantity of coins, that must be new in Ivan's collection. Output Print one numberxa0— minimal number of coins one friend can gift to satisfy all the conditions. If it is impossible to satisfy all three conditions print "-1" (without quotes). Note In the first test, one coin from each friend is enough, as he will be presented with 15 different coins and 13 of them will definitely be new. In the second test, Ivan has 11 friends, but there are only 10 different coins. So all friends can't present him different coins. | 1,400 | true | false | false | false | false | false | false | false | false | false | 5,370 |
120B | A team quiz game called "What? Where? When?" is very popular in Berland. The game is centered on two teams competing. They are the team of six Experts versus the team of the Audience. A person from the audience asks a question and the experts are allowed a minute on brainstorming and finding the right answer to the question. All it takes to answer a typical question is general knowledge and common logic. The question sent be the audience are in envelops lain out in a circle on a round table. Each envelop is marked by the name of the asker's town. Each question is positioned in a separate sector. In the centre of the table is a spinning arrow. Thus, the table rather resembles a roulette table with no ball but with a spinning arrow instead. The host sets off the spinning arrow to choose a question for the experts: when the arrow stops spinning, the question it is pointing at is chosen. If the arrow points at the question that has already been asked, the host chooses the next unanswered question in the clockwise direction. Your task is to determine which will be the number of the next asked question if the arrow points at sector number _k_. Input The first line contains two positive integers _n_ and _k_ (1u2009≤u2009_n_u2009≤u20091000 and 1u2009≤u2009_k_u2009≤u2009_n_) — the numbers of sectors on the table and the number of the sector where the arrow is pointing. The second line contains _n_ numbers: _a__i_u2009=u20090 if the question from sector _i_ has already been asked and _a__i_u2009=u20091 if the question from sector _i_ hasn't been asked yet (1u2009≤u2009_i_u2009≤u2009_n_). The sectors are given in the clockwise order, the first sector follows after the _n_-th one. Output Print the single number — the number of the sector containing the question the experts will be asked. It is guaranteed that the answer exists, that is that not all the questions have already been asked. | 1,100 | false | false | true | false | false | false | false | false | false | false | 9,405 |
1674B | The Berland language consists of words having exactly two letters. Moreover, the first letter of a word is different from the second letter. Any combination of two different Berland letters (which, by the way, are the same as the lowercase letters of Latin alphabet) is a correct word in Berland language. The Berland dictionary contains all words of this language. The words are listed in a way they are usually ordered in dictionaries. Formally, word $$$a$$$ comes earlier than word $$$b$$$ in the dictionary if one of the following conditions hold: the first letter of $$$a$$$ is less than the first letter of $$$b$$$; the first letters of $$$a$$$ and $$$b$$$ are the same, and the second letter of $$$a$$$ is less than the second letter of $$$b$$$. So, the dictionary looks like that: Word $$$1$$$: ab Word $$$2$$$: ac ... Word $$$25$$$: az Word $$$26$$$: ba Word $$$27$$$: bc ... Word $$$649$$$: zx Word $$$650$$$: zy You are given a word $$$s$$$ from the Berland language. Your task is to find its index in the dictionary. Input The first line contains one integer $$$t$$$ ($$$1 le t le 650$$$)xa0— the number of test cases. Each test case consists of one line containing $$$s$$$xa0— a string consisting of exactly two different lowercase Latin letters (i.u2009e. a correct word of the Berland language). Output For each test case, print one integerxa0— the index of the word $$$s$$$ in the dictionary. Example Output 1 2 25 26 27 649 650 | 800 | true | false | false | false | false | false | false | false | false | false | 2,228 |
716B | ZS the Coder loves to read the dictionary. He thinks that a word is nice if there exists a substring (contiguous segment of letters) of it of length 26 where each letter of English alphabet appears exactly once. In particular, if the string has length strictly less than 26, no such substring exists and thus it is not nice. Now, ZS the Coder tells you a word, where some of its letters are missing as he forgot them. He wants to determine if it is possible to fill in the missing letters so that the resulting word is nice. If it is possible, he needs you to find an example of such a word as well. Can you help him? Input The first and only line of the input contains a single string _s_ (1u2009≤u2009_s_u2009≤u200950u2009000), the word that ZS the Coder remembers. Each character of the string is the uppercase letter of English alphabet ('A'-'Z') or is a question mark ('?'), where the question marks denotes the letters that ZS the Coder can't remember. Output If there is no way to replace all the question marks with uppercase letters such that the resulting word is nice, then print u2009-u20091 in the only line. Otherwise, print a string which denotes a possible nice word that ZS the Coder learned. This string should match the string from the input, except for the question marks replaced with uppercase English letters. If there are multiple solutions, you may print any of them. Examples Input ABC??FGHIJK???OPQR?TUVWXY? Output ABCDEFGHIJKLMNOPQRZTUVWXYS Input WELCOMETOCODEFORCESROUNDTHREEHUNDREDANDSEVENTYTWO Input ?????????????????????????? Output MNBVCXZLKJHGFDSAQPWOEIRUYT Input AABCDEFGHIJKLMNOPQRSTUVW??M Note In the first sample case, ABCDEFGHIJKLMNOPQRZTUVWXYS is a valid answer beacuse it contains a substring of length 26 (the whole string in this case) which contains all the letters of the English alphabet exactly once. Note that there are many possible solutions, such as ABCDEFGHIJKLMNOPQRSTUVWXYZ or ABCEDFGHIJKLMNOPQRZTUVWXYS. In the second sample case, there are no missing letters. In addition, the given string does not have a substring of length 26 that contains all the letters of the alphabet, so the answer is u2009-u20091. In the third sample case, any string of length 26 that contains all letters of the English alphabet fits as an answer. | 1,300 | false | true | false | false | false | false | false | false | false | false | 6,953 |
580B | Kefa wants to celebrate his first big salary by going to restaurant. However, he needs company. Kefa has _n_ friends, each friend will agree to go to the restaurant if Kefa asks. Each friend is characterized by the amount of money he has and the friendship factor in respect to Kefa. The parrot doesn't want any friend to feel poor compared to somebody else in the company (Kefa doesn't count). A friend feels poor if in the company there is someone who has at least _d_ units of money more than he does. Also, Kefa wants the total friendship factor of the members of the company to be maximum. Help him invite an optimal company! Input The first line of the input contains two space-separated integers, _n_ and _d_ (1u2009≤u2009_n_u2009≤u2009105, ) — the number of Kefa's friends and the minimum difference between the amount of money in order to feel poor, respectively. Next _n_ lines contain the descriptions of Kefa's friends, the (_i_u2009+u20091)-th line contains the description of the _i_-th friend of type _m__i_, _s__i_ (0u2009≤u2009_m__i_,u2009_s__i_u2009≤u2009109) — the amount of money and the friendship factor, respectively. Output Print the maximum total friendship factir that can be reached. Examples Input 4 5 75 5 0 100 150 20 75 1 Input 5 100 0 7 11 32 99 10 46 8 87 54 Note In the first sample test the most profitable strategy is to form a company from only the second friend. At all other variants the total degree of friendship will be worse. In the second sample test we can take all the friends. | 1,500 | false | false | false | false | false | false | false | true | true | false | 7,527 |
463E | Caisa is now at home and his son has a simple task for him. Given a rooted tree with _n_ vertices, numbered from 1 to _n_ (vertex 1 is the root). Each vertex of the tree has a value. You should answer _q_ queries. Each query is one of the following: Format of the query is "1 _v_". Let's write out the sequence of vertices along the path from the root to vertex _v_: _u_1,u2009_u_2,u2009...,u2009_u__k_ (_u_1u2009=u20091;xa0_u__k_u2009=u2009_v_). You need to output such a vertex _u__i_ that _gcd_(_value_xa0_of_xa0_u__i_,u2009_value_xa0_of_xa0_v_)u2009>u20091 and _i_u2009<u2009_k_. If there are several possible vertices _u__i_ pick the one with maximum value of _i_. If there is no such vertex output -1. Format of the query is "2 _v_ _w_". You must change the value of vertex _v_ to _w_. You are given all the queries, help Caisa to solve the problem. Input The first line contains two space-separated integers _n_, _q_ (1u2009≤u2009_n_,u2009_q_u2009≤u2009105). The second line contains _n_ integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20092·106), where _a__i_ represent the value of node _i_. Each of the next _n_u2009-u20091 lines contains two integers _x__i_ and _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_;xa0_x__i_u2009≠u2009_y__i_), denoting the edge of the tree between vertices _x__i_ and _y__i_. Each of the next _q_ lines contains a query in the format that is given above. For each query the following inequalities hold: 1u2009≤u2009_v_u2009≤u2009_n_ and 1u2009≤u2009_w_u2009≤u20092·106. Note that: there are no more than 50 queries that changes the value of a vertex. Output For each query of the first type output the result of the query. Examples Input 4 6 10 8 4 3 1 2 2 3 3 4 1 1 1 2 1 3 1 4 2 1 9 1 4 Note _gcd_(_x_,u2009_y_) is greatest common divisor of two integers _x_ and _y_. | 2,100 | true | false | false | false | false | false | true | false | false | false | 7,985 |
1327C | Petya has a rectangular Board of size $$$n imes m$$$. Initially, $$$k$$$ chips are placed on the board, $$$i$$$-th chip is located in the cell at the intersection of $$$sx_i$$$-th row and $$$sy_i$$$-th column. In one action, Petya can move all the chips to the left, right, down or up by $$$1$$$ cell. If the chip was in the $$$(x, y)$$$ cell, then after the operation: left, its coordinates will be $$$(x, y - 1)$$$; right, its coordinates will be $$$(x, y + 1)$$$; down, its coordinates will be $$$(x + 1, y)$$$; up, its coordinates will be $$$(x - 1, y)$$$. If the chip is located by the wall of the board, and the action chosen by Petya moves it towards the wall, then the chip remains in its current position. Note that several chips can be located in the same cell. For each chip, Petya chose the position which it should visit. Note that it's not necessary for a chip to end up in this position. Since Petya does not have a lot of free time, he is ready to do no more than $$$2nm$$$ actions. You have to find out what actions Petya should do so that each chip visits the position that Petya selected for it at least once. Or determine that it is not possible to do this in $$$2nm$$$ actions. Input The first line contains three integers $$$n, m, k$$$ ($$$1 le n, m, k le 200$$$) — the number of rows and columns of the board and the number of chips, respectively. The next $$$k$$$ lines contains two integers each $$$sx_i, sy_i$$$ ($$$ 1 le sx_i le n, 1 le sy_i le m$$$) — the starting position of the $$$i$$$-th chip. The next $$$k$$$ lines contains two integers each $$$fx_i, fy_i$$$ ($$$ 1 le fx_i le n, 1 le fy_i le m$$$) — the position that the $$$i$$$-chip should visit at least once. Output In the first line print the number of operations so that each chip visits the position that Petya selected for it at least once. In the second line output the sequence of operations. To indicate operations left, right, down, and up, use the characters $$$L, R, D, U$$$ respectively. If the required sequence does not exist, print -1 in the single line. Examples Input 3 3 2 1 2 2 1 3 3 3 2 Input 5 4 3 3 4 3 1 3 3 5 3 1 3 1 4 | 1,600 | false | false | true | false | false | true | false | false | false | false | 4,076 |
1535F | Suppose you are given two strings $$$a$$$ and $$$b$$$. You can apply the following operation any number of times: choose any contiguous substring of $$$a$$$ or $$$b$$$, and sort the characters in it in non-descending order. Let $$$f(a, b)$$$ the minimum number of operations you have to apply in order to make them equal (or $$$f(a, b) = 1337$$$ if it is impossible to make $$$a$$$ and $$$b$$$ equal using these operations). For example: $$$f( ext{ab}, ext{ab}) = 0$$$; $$$f( ext{ba}, ext{ab}) = 1$$$ (in one operation, we can sort the whole first string); $$$f( ext{ebcda}, ext{ecdba}) = 1$$$ (in one operation, we can sort the substring of the second string starting from the $$$2$$$-nd character and ending with the $$$4$$$-th character); $$$f( ext{a}, ext{b}) = 1337$$$. You are given $$$n$$$ strings $$$s_1, s_2, dots, s_k$$$ having equal length. Calculate $$$sum limits_{i = 1}^{n} sumlimits_{j = i + 1}^{n} f(s_i, s_j)$$$. Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the number of strings. Then $$$n$$$ lines follow, each line contains one of the strings $$$s_i$$$, consisting of lowercase Latin letters. $$$s_1 = s_2 = ldots = s_n$$$, and $$$n cdot s_1 le 2 cdot 10^5$$$. All these strings are pairwise distinct. Output Print one integer: $$$sum limits_{i = 1}^{n} sumlimits_{j = i + 1}^{n} f(s_i, s_j)$$$. | 3,000 | false | false | true | false | true | false | true | true | false | false | 2,969 |
734E | Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph. There are _n_ vertices in the tree, each of them is painted black or white. Anton doesn't like multicolored trees, so he wants to change the tree such that all vertices have the same color (black or white). To change the colors Anton can use only operations of one type. We denote it as _paint_(_v_), where _v_ is some vertex of the tree. This operation changes the color of all vertices _u_ such that all vertices on the shortest path from _v_ to _u_ have the same color (including _v_ and _u_). For example, consider the tree and apply operation _paint_(3) to get the following: Anton is interested in the minimum number of operation he needs to perform in order to make the colors of all vertices equal. Input The first line of the input contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000)xa0— the number of vertices in the tree. The second line contains _n_ integers _color__i_ (0u2009≤u2009_color__i_u2009≤u20091)xa0— colors of the vertices. _color__i_u2009=u20090 means that the _i_-th vertex is initially painted white, while _color__i_u2009=u20091 means it's initially painted black. Then follow _n_u2009-u20091 line, each of them contains a pair of integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_,u2009_u__i_u2009≠u2009_v__i_)xa0— indices of vertices connected by the corresponding edge. It's guaranteed that all pairs (_u__i_,u2009_v__i_) are distinct, i.e. there are no multiple edges. Output Print one integerxa0— the minimum number of operations Anton has to apply in order to make all vertices of the tree black or all vertices of the tree white. Examples Input 11 0 0 0 1 1 0 1 0 0 1 1 1 2 1 3 2 4 2 5 5 6 5 7 3 8 3 9 3 10 9 11 Note In the first sample, the tree is the same as on the picture. If we first apply operation _paint_(3) and then apply _paint_(6), the tree will become completely black, so the answer is 2. In the second sample, the tree is already white, so there is no need to apply any operations and the answer is 0. | 2,100 | false | false | false | true | false | false | false | false | false | false | 6,854 |
1834D | Zinaida Viktorovna has $$$n$$$ students in her history class. The homework for today included $$$m$$$ topics, but the students had little time to prepare, so $$$i$$$-th student learned only topics from $$$l_i$$$ to $$$r_i$$$ inclusive. At the beginning of the lesson, each student holds their hand at $$$0$$$. The teacher wants to ask some topics. It goes like this: The teacher asks the topic $$$k$$$. If the student has learned topic $$$k$$$, then he raises his hand by $$$1$$$, otherwise he lower it by $$$1$$$. Each topic Zinaida Viktorovna can ask no more than one time. Find the maximum difference of the heights of the highest and the lowest hand that can be in class after the survey. Note that the student's hand can go below $$$0$$$. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$2 le n le 10^5, 1 le m le 10^9$$$)xa0— the number of students and the number of topics, respectively. Each of the next $$$n$$$ lines of each test case contain two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le m$$$)xa0— the endpoints of the segment of topics that $$$i$$$-th student has learned. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$. Output For each test case, print one integerxa0— the maximum difference of the heights of the highest and the lowest hand that can be in the class after the survey. Example Input 6 4 8 2 6 4 8 2 7 1 5 3 3 1 3 2 3 2 2 3 5 1 5 1 5 1 5 3 5 1 1 3 3 5 5 4 7 1 7 1 3 3 3 4 5 2 4 1 3 2 4 Note In the first test case, Zinaida Viktorovna can ask the topics $$$5, 6, 7, 8$$$. Then the hand of the $$$2$$$-nd student will be at the height of $$$4$$$, and the hand of the $$$4$$$-th will be at the height of $$$-2$$$, that is, the difference will be equal to $$$6$$$. In the second test case, you can ask about the topics $$$1$$$ and $$$3$$$. Then the hand of the $$$1$$$-st student will be at the height of $$$2$$$ and the hand of the $$$3$$$-rd student will be at the height of $$$-2$$$. So the difference will be $$$4$$$. In the third test case, the difference between the highest and lowest hand will be $$$0$$$ for any set of topics asked. In the fifth test case, you can ask all topics. Then the difference between the heights of the $$$1$$$-st and $$$3$$$-rd students' hands will be $$$12$$$. | 1,900 | false | true | true | false | true | false | true | false | true | false | 1,281 |
1553C | Consider a simplified penalty phase at the end of a football match. A penalty phase consists of at most $$$10$$$ kicks, the first team takes the first kick, the second team takes the second kick, then the first team takes the third kick, and so on. The team that scores more goals wins; if both teams score the same number of goals, the game results in a tie (note that it goes against the usual football rules). The penalty phase is stopped if one team has scored more goals than the other team could reach with all of its remaining kicks. For example, if after the $$$7$$$-th kick the first team has scored $$$1$$$ goal, and the second team has scored $$$3$$$ goals, the penalty phase ends — the first team cannot reach $$$3$$$ goals. You know which player will be taking each kick, so you have your predictions for each of the $$$10$$$ kicks. These predictions are represented by a string $$$s$$$ consisting of $$$10$$$ characters. Each character can either be 1, 0, or ?. This string represents your predictions in the following way: if $$$s_i$$$ is 1, then the $$$i$$$-th kick will definitely score a goal; if $$$s_i$$$ is 0, then the $$$i$$$-th kick definitely won't score a goal; if $$$s_i$$$ is ?, then the $$$i$$$-th kick could go either way. Based on your predictions, you have to calculate the minimum possible number of kicks there can be in the penalty phase (that means, the earliest moment when the penalty phase is stopped, considering all possible ways it could go). Note that the referee doesn't take into account any predictions when deciding to stop the penalty phase — you may know that some kick will/won't be scored, but the referee doesn't. Input The first line contains one integer $$$t$$$ ($$$1 le t le 1,000$$$) — the number of test cases. Each test case is represented by one line containing the string $$$s$$$, consisting of exactly $$$10$$$ characters. Each character is either 1, 0, or ?. Output For each test case, print one integer — the minimum possible number of kicks in the penalty phase. Example Input 4 1?0???1001 1111111111 ?????????? 0100000000 Note Consider the example test: In the first test case, consider the situation when the $$$1$$$-st, $$$5$$$-th and $$$7$$$-th kicks score goals, and kicks $$$2$$$, $$$3$$$, $$$4$$$ and $$$6$$$ are unsuccessful. Then the current number of goals for the first team is $$$3$$$, for the second team is $$$0$$$, and the referee sees that the second team can score at most $$$2$$$ goals in the remaining kicks. So the penalty phase can be stopped after the $$$7$$$-th kick. In the second test case, the penalty phase won't be stopped until all $$$10$$$ kicks are finished. In the third test case, if the first team doesn't score any of its three first kicks and the second team scores all of its three first kicks, then after the $$$6$$$-th kick, the first team has scored $$$0$$$ goals and the second team has scored $$$3$$$ goals, and the referee sees that the first team can score at most $$$2$$$ goals in the remaining kicks. So, the penalty phase can be stopped after the $$$6$$$-th kick. In the fourth test case, even though you can predict the whole penalty phase, the referee understands that the phase should be ended only after the $$$9$$$-th kick. | 1,200 | false | true | false | true | false | false | true | false | false | false | 2,873 |
1707D | Kawashiro Nitori is a girl who loves competitive programming. One day she found a rooted tree consisting of $$$n$$$ vertices. The root is vertex $$$1$$$. As an advanced problem setter, she quickly thought of a problem. Kawashiro Nitori has a vertex set $$$U={1,2,ldots,n}$$$. She's going to play a game with the tree and the set. In each operation, she will choose a vertex set $$$T$$$, where $$$T$$$ is a partial virtual tree of $$$U$$$, and change $$$U$$$ into $$$T$$$. A vertex set $$$S_1$$$ is a partial virtual tree of a vertex set $$$S_2$$$, if $$$S_1$$$ is a subset of $$$S_2$$$, $$$S_1 eq S_2$$$, and for all pairs of vertices $$$i$$$ and $$$j$$$ in $$$S_1$$$, $$$operatorname{LCA}(i,j)$$$ is in $$$S_1$$$, where $$$operatorname{LCA}(x,y)$$$ denotes the such that after $$$z$$$ operations the sets $$$U$$$ are different. Since the answer could be very large, you need to find it modulo $$$p$$$. It's guaranteed that $$$p$$$ is a prime number. Input The first line contains two integers $$$n$$$ and $$$p$$$ ($$$2 le n le 2000$$$, $$$10^8 + 7 le p le 10^9+9$$$). It's guaranteed that $$$p$$$ is a prime number. Each of the next $$$n-1$$$ lines contains two integers $$$u_i$$$, $$$v_i$$$ ($$$1 leq u_i, v_i leq n$$$), representing an edge between $$$u_i$$$ and $$$v_i$$$. It is guaranteed that the given edges form a tree. Output The only line contains $$$n-1$$$ integersxa0— the answer modulo $$$p$$$ for $$$k=1,2,ldots,n-1$$$. Note In the first test case, when $$$k=1$$$, the only possible way is: 1. $$${1,2,3,4} o {1}$$$. When $$$k=2$$$, there are $$$6$$$ possible ways: 1. $$${1,2,3,4} o {1,2} o {1}$$$; 2. $$${1,2,3,4} o {1,2,3} o {1}$$$; 3. $$${1,2,3,4} o {1,2,4} o {1}$$$; 4. $$${1,2,3,4} o {1,3} o {1}$$$; 5. $$${1,2,3,4} o {1,3,4} o {1}$$$; 6. $$${1,2,3,4} o {1,4} o {1}$$$. When $$$k=3$$$, there are $$$6$$$ possible ways: 1. $$${1,2,3,4} o {1,2,3} o {1,2} o {1}$$$; 2. $$${1,2,3,4} o {1,2,3} o {1,3} o {1}$$$; 3. $$${1,2,3,4} o {1,2,4} o {1,2} o {1}$$$; 4. $$${1,2,3,4} o {1,2,4} o {1,4} o {1}$$$; 5. $$${1,2,3,4} o {1,3,4} o {1,3} o {1}$$$; 6. $$${1,2,3,4} o {1,3,4} o {1,4} o {1}$$$. | 3,000 | true | false | false | true | false | false | false | false | false | false | 2,031 |
809B | This is an interactive problem. In the output section below you will see the information about flushing the output. On Sunday Leha the hacker took Nura from the house where she lives and went with her to one of the most luxurious restaurants in Vičkopolis. Upon arrival, they left the car in a huge parking lot near the restaurant and hurried inside the building. In the restaurant a polite waiter immediately brought the menu to Leha and Noora, consisting of _n_ dishes. It is interesting that all dishes in the menu are numbered with integers from 1 to _n_. After a little thought, the girl ordered exactly _k_ different dishes from available in the menu. To pass the waiting time while the chefs prepare ordered dishes, the girl invited the hacker to play a game that will help them get to know each other better. The game itself is very simple: Noora wants Leha to guess any two dishes among all ordered. At the same time, she is ready to answer only one type of questions. Leha can say two numbers _x_ and _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_). After that Noora chooses some dish _a_ for the number _x_ such that, at first, _a_ is among the dishes Noora ordered (_x_ can be equal to _a_), and, secondly, the value is the minimum possible. By the same rules the girl chooses dish _b_ for _y_. After that Noora says «TAK» to Leha, if , and «NIE» otherwise. However, the restaurant is preparing quickly, so Leha has enough time to ask no more than 60 questions. After that he should name numbers of any two dishes Noora ordered. Help Leha to solve this problem! Input There are two numbers _n_ and _k_ (2u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009105) in the single line of input denoting the number of dishes in the menu and the number of dishes Noora ordered. Output If you want to provide an answer, output a string of the form 2 _x_ _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_,u2009_x_u2009≠u2009_y_), if you think the dishes _x_ and _y_ was among dishes ordered by Noora. After that, flush the output and terminate your program. Interaction While helping Leha, you can ask queries to Noora no more than 60 times. Each query should be printed in it's own line and have the form 1 _x_ _y_ (1u2009≤u2009_x_,u2009_y_u2009≤u2009_n_). You have to both print the end-of-line character and flush the output. After flushing you should read the answer for this query from input. After each query jury's program will print one line «TAK» or «NIE» (without quotes) in input stream depending on the girl's answer. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; see the documentation for other languages. Hacking For hacking you should write numbers _n_ and _k_ (2u2009≤u2009_k_u2009≤u2009_n_u2009≤u2009105) in the first line and, for describing dishes Noora ordered, _k_ different integers _a_1,u2009_a_2,u2009...,u2009_a__k_ (1u2009≤u2009_a__i_u2009≤u2009_n_), written in ascending order in the second line. Of course, solution you want to hack won't be able to read the numbers of ordered dishes. Example Input 3 2 NIE TAK NIE TAK TAK TAK Output 1 1 2 1 2 1 1 1 3 1 3 1 1 2 3 1 3 2 2 2 3 Note There are three dishes in sample. Noora ordered dished numberes 2 and 3, which Leha should guess. If Noora receive requests for the first dish (_x_u2009=u20091), then she'll choose the second dish (_a_u2009=u20092) as the dish with the minimum value . For the second (_x_u2009=u20092) and the third (_x_u2009=u20093) dishes themselves will be optimal, because in that case . Let Leha asks Noora about the next couple of dishes: _x_u2009=u20091, _y_u2009=u20092, then he'll recieve «NIE» answer, because 1u2009-u20092u2009>u20092u2009-u20092 _x_u2009=u20092, _y_u2009=u20091, then he'll recieve «TAK» answer, because 2u2009-u20092u2009≤u20091u2009-u20092 _x_u2009=u20091, _y_u2009=u20093, then he'll recieve «NIE» answer, because 1u2009-u20092u2009>u20093u2009-u20093 _x_u2009=u20093, _y_u2009=u20091, then he'll recieve «TAK» answer, because 3u2009-u20093u2009≤u20091u2009-u20092 _x_u2009=u20092, _y_u2009=u20093, then he'll recieve «TAK» answer, because 2u2009-u20092u2009≤u20093u2009-u20093 _x_u2009=u20093, _y_u2009=u20092, then he'll recieve «TAK» answer, because 3u2009-u20093u2009≤u20092u2009-u20092 According to the available information, it is possible to say that Nura ordered dishes with numbers 2 and 3. | 2,200 | false | false | false | false | false | false | false | true | false | false | 6,527 |
723E | There are _n_ cities and _m_ two-way roads in Berland, each road connects two cities. It is known that there is no more than one road connecting each pair of cities, and there is no road which connects the city with itself. It is possible that there is no way to get from one city to some other city using only these roads. The road minister decided to make a reform in Berland and to orient all roads in the country, i.e. to make each road one-way. The minister wants to maximize the number of cities, for which the number of roads that begins in the city equals to the number of roads that ends in it. Input The first line contains a positive integer _t_ (1u2009≤u2009_t_u2009≤u2009200)xa0— the number of testsets in the input. Each of the testsets is given in the following way. The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009200, 0u2009≤u2009_m_u2009≤u2009_n_·(_n_u2009-u20091)u2009/u20092)xa0— the number of cities and the number of roads in Berland. The next _m_ lines contain the description of roads in Berland. Each line contains two integers _u_ and _v_ (1u2009≤u2009_u_,u2009_v_u2009≤u2009_n_)xa0— the cities the corresponding road connects. It's guaranteed that there are no self-loops and multiple roads. It is possible that there is no way along roads between a pair of cities. It is guaranteed that the total number of cities in all testset of input data doesn't exceed 200. Pay attention that for hacks, you can only use tests consisting of one testset, so _t_ should be equal to one. Output For each testset print the maximum number of such cities that the number of roads that begins in the city, is equal to the number of roads that ends in it. In the next _m_ lines print oriented roads. First print the number of the city where the road begins and then the number of the city where the road ends. If there are several answers, print any of them. It is allowed to print roads in each test in arbitrary order. Each road should be printed exactly once. Example Input 2 5 5 2 1 4 5 2 3 1 3 3 5 7 2 3 7 4 2 Output 3 1 3 3 5 5 4 3 2 2 1 3 2 4 3 7 | 2,200 | false | true | false | false | false | true | false | false | false | true | 6,916 |
1593G | A sequence of round and square brackets is given. You can change the sequence by performing the following operations: 1. change the direction of a bracket from opening to closing and vice versa without changing the form of the bracket: i.e. you can change '(' to ')' and ')' to '('; you can change '" and "(())()" are correct bracket sequences whereas "(", "[(])" and ")))" are not. Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Then $$$t$$$ test cases follow. For each test case, the first line contains a non-empty string $$$s$$$ containing only round ('(', ')') and square ('[', ']') brackets. The length of the string doesn't exceed $$$10^6$$$. The string contains at least $$$2$$$ characters. The second line contains one integer $$$q$$$ ($$$1 le q le 2 cdot 10^5$$$) — the number of queries. Then $$$q$$$ lines follow, each of them contains two integers $$$l$$$ and $$$r$$$ ($$$1 le l < r le n$$$ where $$$n$$$ is the length of $$$s$$$). It is guaranteed that the substring $$$s[l dots r]$$$ has even length. It is guaranteed that the sum of the lengths of all strings given in all test cases doesn't exceed $$$10^6$$$. The sum of all $$$q$$$ given in all test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case output in a separate line for each query one integer $$$x$$$ ($$$x ge 0$$$) — the minimum cost to pay to make the given substring a correct bracket sequence. Example Input 3 ([))[)()][]] 3 1 12 4 9 3 6 )))))) 2 2 3 1 4 [] 1 1 2 Note Consider the first test case. The first query describes the whole given string, the string can be turned into the following correct bracket sequence: "([()])()[[]]". The forms of the brackets aren't changed so the cost of changing is $$$0$$$. The second query describes the substring ")[)()]". It may be turned into "(()())", the cost is equal to $$$2$$$. The third query describes the substring "))[)". It may be turned into "()()", the cost is equal to $$$1$$$. The substrings of the second test case contain only round brackets. It's possible to prove that any sequence of round brackets having an even length may be turned into a correct bracket sequence for the cost of $$$0$$$ burles. In the third test case, the single query describes the string "[]" that is already a correct bracket sequence. | 2,200 | false | true | false | true | true | true | false | false | false | false | 2,679 |
1354G | This is an interactive problem. Don't forget to flush output after printing queries using cout.flush() or fflush(stdout) in C++ or similar functions in other programming languages. There are $$$n$$$ gift boxes in a row, numbered from $$$1$$$ to $$$n$$$ from left to right. It's known that exactly $$$k$$$ of them contain valuable giftsxa0— other boxes contain just lucky stones. All boxes look the same and differ only in weight. All boxes with stones have the same weight and are strictly heavier than boxes with valuable items. But valuable gifts may be different, so the boxes with valuable items may have different weights. You can ask no more than $$$50$$$ queries (printing an answer doesn't count). By each query you can compare total weights of two non-intersecting subsets of boxes $$$a_1, a_2, dots, a_{k_a}$$$ and $$$b_1, b_2, dots, b_{k_b}$$$. In return you'll get one of four results: FIRST, if subset $$$a_1, a_2, dots, a_{k_a}$$$ is strictly heavier; SECOND, if subset $$$b_1, b_2, dots, b_{k_b}$$$ is strictly heavier; EQUAL, if subsets have equal total weights; WASTED, if the query is incorrect or the limit of queries is exceeded. Using such queries (or, maybe, intuition) find the box with a valuable gift with the minimum index. Input The input consists of several cases. In the beginning, you receive the integer $$$T$$$ ($$$1 le T le 500$$$)xa0— the number of test cases. At the beginning of each test case, you receive two integers $$$n$$$ and $$$k$$$ ($$$2 le n le 1000$$$, $$$1 le k le frac{n}{2}$$$)xa0— the number of boxes in a row and the number of boxes with valuable gifts. It's guaranteed that the order of boxes is fixed beforehand and that the sum of $$$n$$$ in one test doesn't exceed $$$1000$$$. Output For each test case print the minimum index among all boxes with a valuable gift in the following format: "! $$$x$$$" where $$$x$$$ ($$$1 le x le n$$$)xa0— the index of the box. Interaction Print each query in three lines. In the first line print the sizes of subset in the following format: "? $$$k_a$$$ $$$k_b$$$" where $$$k_a$$$ and $$$k_b$$$ ($$$1 le k_a, k_b le n$$$; $$$k_a + k_b le n$$$)xa0— the corresponding sizes. In the second line print $$$k_a$$$ integers $$$a_1, a_2, dots, a_{k_a}$$$ ($$$1 le a_i le n$$$; $$$a_i eq a_j$$$ if $$$i eq j$$$)xa0— indexes of boxes in the first subset. In the third line print $$$k_b$$$ integers $$$b_1, b_2, dots, b_{k_b}$$$ ($$$1 le b_i le n$$$; $$$b_i eq b_j$$$ if $$$i eq j$$$)xa0— indexes of boxes in the second subset. The subsets shouldn't intersect, i. e. $$$a_i eq b_j$$$ for all $$$i$$$ and $$$j$$$. You'll receive one of four responses described above. In the case of WASTED stop your program to avoid getting random verdict instead of Wrong Answer. Example Input 2 2 1 - - - FIRST - 5 2 - - - FIRST - - - SECOND - - - EQUAL - Output - - ? 1 1 1 2 - ! 2 - ? 1 1 1 2 - ? 2 3 4 2 1 3 5 - ? 1 1 4 5 - ! 1 Note Additional separators "–" in the sample are used only to increase the readability of the sample. Don't print any unnecessary symbols or line breaks in your solution when you send it to the system. Hacks are forbidden in this task. | 2,600 | false | false | false | false | false | false | false | true | false | false | 3,932 |
1556F | William is not only interested in trading but also in betting on sports matches. $$$n$$$ teams participate in each match. Each team is characterized by strength $$$a_i$$$. Each two teams $$$i < j$$$ play with each other exactly once. Team $$$i$$$ wins with probability $$$frac{a_i}{a_i + a_j}$$$ and team $$$j$$$ wins with probability $$$frac{a_j}{a_i + a_j}$$$. The team is called a winner if it directly or indirectly defeated all other teams. Team $$$a$$$ defeated (directly or indirectly) team $$$b$$$ if there is a sequence of teams $$$c_1$$$, $$$c_2$$$, ... $$$c_k$$$ such that $$$c_1 = a$$$, $$$c_k = b$$$ and team $$$c_i$$$ defeated team $$$c_{i + 1}$$$ for all $$$i$$$ from $$$1$$$ to $$$k - 1$$$. Note that it is possible that team $$$a$$$ defeated team $$$b$$$ and in the same time team $$$b$$$ defeated team $$$a$$$. William wants you to find the expected value of the number of winners. Input The first line contains a single integer $$$n$$$ ($$$1 leq n leq 14$$$), which is the total number of teams participating in a match. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 leq a_i leq 10^6$$$) xa0— the strengths of teams participating in a match. Output Output a single integer xa0— the expected value of the number of winners of the tournament modulo $$$10^9 + 7$$$. Formally, let $$$M = 10^9+7$$$. It can be demonstrated that the answer can be presented as a irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output a single integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output an integer $$$x$$$ such that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Note To better understand in which situation several winners are possible let's examine the second test: One possible result of the tournament is as follows ($$$a ightarrow b$$$ means that $$$a$$$ defeated $$$b$$$): $$$1 ightarrow 2$$$ $$$2 ightarrow 3$$$ $$$3 ightarrow 1$$$ $$$1 ightarrow 4$$$ $$$1 ightarrow 5$$$ $$$2 ightarrow 4$$$ $$$2 ightarrow 5$$$ $$$3 ightarrow 4$$$ $$$3 ightarrow 5$$$ $$$4 ightarrow 5$$$ Or more clearly in the picture: In this case every team from the set $$${ 1, 2, 3 }$$$ directly or indirectly defeated everyone. I.e.: $$$1$$$st defeated everyone because they can get to everyone else in the following way $$$1 ightarrow 2$$$, $$$1 ightarrow 2 ightarrow 3$$$, $$$1 ightarrow 4$$$, $$$1 ightarrow 5$$$. $$$2$$$nd defeated everyone because they can get to everyone else in the following way $$$2 ightarrow 3$$$, $$$2 ightarrow 3 ightarrow 1$$$, $$$2 ightarrow 4$$$, $$$2 ightarrow 5$$$. $$$3$$$rd defeated everyone because they can get to everyone else in the following way $$$3 ightarrow 1$$$, $$$3 ightarrow 1 ightarrow 2$$$, $$$3 ightarrow 4$$$, $$$3 ightarrow 5$$$. Therefore the total number of winners is $$$3$$$. | 2,500 | true | false | false | true | false | false | false | false | false | true | 2,850 |
1051A | Vasya came up with a password to register for EatForces — a string $$$s$$$. The password in EatForces should be a string, consisting of lowercase and uppercase Latin letters and digits. But since EatForces takes care of the security of its users, user passwords must contain at least one digit, at least one uppercase Latin letter and at least one lowercase Latin letter. For example, the passwords "abaCABA12", "Z7q" and "3R24m" are valid, and the passwords "qwerty", "qwerty12345" and "Password" are not. A substring of string $$$s$$$ is a string $$$x = s_l s_{l + 1} dots s_{l + len - 1} (1 le l le s, 0 le len le s - l + 1)$$$. $$$len$$$ is the length of the substring. Note that the empty string is also considered a substring of $$$s$$$, it has the length $$$0$$$. Vasya's password, however, may come too weak for the security settings of EatForces. He likes his password, so he wants to replace some its substring with another string of the same length in order to satisfy the above conditions. This operation should be performed exactly once, and the chosen string should have the minimal possible length. Note that the length of $$$s$$$ should not change after the replacement of the substring, and the string itself should contain only lowercase and uppercase Latin letters and digits. Input The first line contains a single integer $$$T$$$ ($$$1 le T le 100$$$) — the number of testcases. Each of the next $$$T$$$ lines contains the initial password $$$s~(3 le s le 100)$$$, consisting of lowercase and uppercase Latin letters and digits. Only $$$T = 1$$$ is allowed for hacks. Output For each testcase print a renewed password, which corresponds to given conditions. The length of the replaced substring is calculated as following: write down all the changed positions. If there are none, then the length is $$$0$$$. Otherwise the length is the difference between the first and the last changed position plus one. For example, the length of the changed substring between the passwords "abcdef" $$$ ightarrow$$$ "a7cdEf" is $$$4$$$, because the changed positions are $$$2$$$ and $$$5$$$, thus $$$(5 - 2) + 1 = 4$$$. It is guaranteed that such a password always exists. If there are several suitable passwords — output any of them. Note In the first example Vasya's password lacks a digit, he replaces substring "C" with "4" and gets password "abcD4E". That means, he changed the substring of length 1. In the second example Vasya's password is ok from the beginning, and nothing has to be changed. That is the same as replacing the empty substring with another empty substring (length 0). | 1,200 | false | true | true | false | false | false | false | false | false | false | 5,455 |
15A | Problem - 15A - Codeforces =============== xa0 ") . Then there follow _n_ lines, each of them contains two space-separated integer numbers: _x__i_ _a__i_, where _x__i_ — _x_-coordinate of the centre of the _i_-th house, and _a__i_ — length of its side (u2009-u20091000u2009≤u2009_x__i_u2009≤u20091000, 1u2009≤u2009_a__i_u2009≤u20091000). Output Output the amount of possible positions of the new house. Examples Input 2 2 0 4 6 2 Output 4 Input 2 2 0 4 5 2 Output 3 Input 2 3 0 4 5 2 Output 2 Note It is possible for the _x_-coordinate of the new house to have non-integer value. | 1,200 | false | false | true | false | false | false | false | false | true | false | 9,920 |
1906B | You are given $$$N$$$ buttons (numbered from $$$1$$$ to $$$N$$$) and $$$N$$$ lamps (numbered from $$$1$$$ to $$$N$$$). Each lamp can either be on or off. Initially, lamp $$$i$$$ is on if $$$A_i = 1$$$, and off if $$$A_i = 0$$$. Button $$$i$$$ is connected to lamp $$$i - 1$$$ (if $$$i > 1$$$) and lamp $$$i + 1$$$ (if $$$i < N$$$). In one move, you can press a button $$$i$$$ only if lamp $$$i$$$ is on. When a button is pressed, the state of the lamps connected to this button is toggled. Formally, the lamps will be on if it was off previously, and the lamps will be off if it was on previously. Note that lamp $$$i$$$ is not connected to button $$$i$$$, thus, the state of lamp $$$i$$$ does not change if button $$$i$$$ is pressed. After zero or more moves, you want lamp $$$i$$$ to be on if $$$B_i = 1$$$, and off if $$$B_i = 0$$$. Determine if it is possible to achieve this task. Input This problem has multiple test cases. The first line consists of an integer $$$T$$$ ($$$1 leq T leq 1000$$$), which represents the number of test cases. Each test case consists of three lines. The first line of each test case consists of an integer $$$N$$$ ($$$3 le N le 200,000$$$). The sum of $$$N$$$ over all test cases does not exceed $$$200,000$$$. The second line of each test case consists of a string $$$A$$$ of length $$$N$$$. Each character of $$$A$$$ can either be 0 or 1. The $$$i$$$-th character represents the initial state of lamp $$$i$$$. The third line of each test case consists of a string $$$A$$$ of length $$$N$$$. Each character of $$$B$$$ can either be 0 or 1. The $$$i$$$-th character represents the desired final state of lamp $$$i$$$. Output For each test case, output YES in a single line if the final state of all lamps can be reached after zero or more moves, or NO otherwise. Examples Input 2 4 0101 0100 3 000 010 Input 5 7 0101011 1111010 5 11111 00000 4 1101 1101 6 101010 100100 3 000 000 Note Explanation for the sample input/output #1 For the first test case, by pressing the buttons $$$4, 2, 4, 3, 1, 2$$$ in sequence, the condition of the buttons changes as: $$$0101 ightarrow 0111 ightarrow 1101 ightarrow 1111 ightarrow 1010 ightarrow 1110 ightarrow 0100$$$. For the second test case, you are unable to press any button, hence it is impossible to reach the final state. | 2,600 | false | false | false | false | false | true | false | false | false | false | 876 |
231D | One day Vasya was going home when he saw a box lying on the road. The box can be represented as a rectangular parallelepiped. Vasya needed no time to realize that the box is special, as all its edges are parallel to the coordinate axes, one of its vertices is at point (0,u20090,u20090), and the opposite one is at point (_x_1,u2009_y_1,u2009_z_1). The six faces of the box contain some numbers _a_1,u2009_a_2,u2009...,u2009_a_6, exactly one number right in the center of each face. The numbers are located on the box like that: number _a_1 is written on the face that lies on the ZOX plane; _a_2 is written on the face, parallel to the plane from the previous point; _a_3 is written on the face that lies on the XOY plane; _a_4 is written on the face, parallel to the plane from the previous point; _a_5 is written on the face that lies on the YOZ plane; _a_6 is written on the face, parallel to the plane from the previous point. At the moment Vasya is looking at the box from point (_x_,u2009_y_,u2009_z_). Find the sum of numbers that Vasya sees. Note that all faces of the box are not transparent and Vasya can't see the numbers through the box. The picture contains transparent faces just to make it easier to perceive. You can consider that if Vasya is looking from point, lying on the plane of some face, than he can not see the number that is written on this face. It is enough to see the center of a face to see the corresponding number for Vasya. Also note that Vasya always reads correctly the _a__i_ numbers that he sees, independently of their rotation, angle and other factors (that is, for example, if Vasya sees some _a__i_u2009=u20096, then he can't mistake this number for 9 and so on). Input The fist input line contains three space-separated integers _x_, _y_ and _z_ (_x_,u2009_y_,u2009_z_u2009≤u2009106) — the coordinates of Vasya's position in space. The second line contains three space-separated integers _x_1, _y_1, _z_1 (1u2009≤u2009_x_1,u2009_y_1,u2009_z_1u2009≤u2009106) — the coordinates of the box's vertex that is opposite to the vertex at point (0,u20090,u20090). The third line contains six space-separated integers _a_1,u2009_a_2,u2009...,u2009_a_6 (1u2009≤u2009_a__i_u2009≤u2009106) — the numbers that are written on the box faces. It is guaranteed that point (_x_,u2009_y_,u2009_z_) is located strictly outside the box. Note The first sample corresponds to perspective, depicted on the picture. Vasya sees numbers _a_2 (on the top face that is the darkest), _a_6 (on the right face that is the lightest) and _a_4 (on the left visible face). In the second sample Vasya can only see number _a_4. | 1,600 | false | false | false | false | false | false | true | false | false | false | 8,913 |
1129D | Problem - 1129D - 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 *2900 No tag edit access → Contest materials — the number of elements in the array $$$a$$$ and the restriction from the statement. The following line contains $$$n$$$ space-separated integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 leq a_i leq n$$$) — elements of the array $$$a$$$. Output The first and only line contains the number of ways to divide an array $$$a$$$ modulo $$$998,244,353$$$. Examples Input 3 1 1 1 2 Output 3 Input 5 2 1 1 2 1 3 Output 14 Input 5 5 1 2 3 4 5 Output 16 Note In the first sample, the three possible divisions are as follows. $$$ | 2,900 | false | false | false | true | true | false | false | false | false | false | 5,073 |
1380G | You are creating a level for a video game. The level consists of $$$n$$$ rooms placed in a circle. The rooms are numbered $$$1$$$ through $$$n$$$. Each room contains exactly one exit: completing the $$$j$$$-th room allows you to go the $$$(j+1)$$$-th room (and completing the $$$n$$$-th room allows you to go the $$$1$$$-st room). You are given the description of the multiset of $$$n$$$ chests: the $$$i$$$-th chest has treasure value $$$c_i$$$. Each chest can be of one of two types: regular chestxa0— when a player enters a room with this chest, he grabs the treasure and proceeds to the next room; mimic chestxa0— when a player enters a room with this chest, the chest eats him alive, and he loses. The player starts in a random room with each room having an equal probability of being chosen. The players earnings is equal to the total value of treasure chests he'd collected before he lost. You are allowed to choose the order the chests go into the rooms. For each $$$k$$$ from $$$1$$$ to $$$n$$$ place the chests into the rooms in such a way that: each room contains exactly one chest; exactly $$$k$$$ chests are mimics; the expected value of players earnings is minimum possible. Please note that for each $$$k$$$ the placement is chosen independently. It can be shown that it is in the form of $$$frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are non-negative integers and $$$Q e 0$$$. Report the values of $$$P cdot Q^{-1} pmod {998244353}$$$. Input The first contains a single integer $$$n$$$ ($$$2 le n le 3 cdot 10^5$$$)xa0— the number of rooms and the number of chests. The second line contains $$$n$$$ integers $$$c_1, c_2, dots, c_n$$$ ($$$1 le c_i le 10^6$$$)xa0— the treasure values of each chest. Output Print $$$n$$$ integersxa0— the $$$k$$$xa0-th value should be equal to the minimum possible expected value of players earnings if the chests are placed into the rooms in some order and exactly $$$k$$$ of the chests are mimics. It can be shown that it is in the form of $$$frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are non-negative integers and $$$Q e 0$$$. Report the values of $$$P cdot Q^{-1} pmod {998244353}$$$. Examples Output 499122193 249561095 249561092 873463811 499122178 124780545 623902721 0 Note In the first example the exact values of minimum expected values are: $$$frac 1 2$$$, $$$frac 0 2$$$. In the second example the exact values of minimum expected values are: $$$frac{132} 8$$$, $$$frac{54} 8$$$, $$$frac{30} 8$$$, $$$frac{17} 8$$$, $$$frac{12} 8$$$, $$$frac 7 8$$$, $$$frac 3 8$$$, $$$frac 0 8$$$. | 2,600 | true | true | false | false | false | false | false | false | false | false | 3,765 |
1195B | Each evening after the dinner the SIS's students gather together to play the game of Sport Mafia. For the tournament, Alya puts candies into the box, which will serve as a prize for a winner. To do that, she performs $$$n$$$ actions. The first action performed is to put a single candy into the box. For each of the remaining moves she can choose from two options: the first option, in case the box contains at least one candy, is to take exactly one candy out and eat it. This way the number of candies in the box decreased by $$$1$$$; the second option is to put candies in the box. In this case, Alya will put $$$1$$$ more candy, than she put in the previous time. Thus, if the box is empty, then it can only use the second option. For example, one possible sequence of Alya's actions look as follows: put one candy into the box; put two candies into the box; eat one candy from the box; eat one candy from the box; put three candies into the box; eat one candy from the box; put four candies into the box; eat one candy from the box; put five candies into the box; This way she will perform $$$9$$$ actions, the number of candies at the end will be $$$11$$$, while Alya will eat $$$4$$$ candies in total. You know the total number of actions $$$n$$$ and the number of candies at the end $$$k$$$. You need to find the total number of sweets Alya ate. That is the number of moves of the first option. It's guaranteed, that for the given $$$n$$$ and $$$k$$$ the answer always exists. Please note, that during an action of the first option, Alya takes out and eats exactly one candy. Input The first line contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n le 10^9$$$; $$$0 le k le 10^9$$$)xa0— the total number of moves and the number of candies in the box at the end. It's guaranteed, that for the given $$$n$$$ and $$$k$$$ the answer exists. Output Print a single integerxa0— the number of candies, which Alya ate. Please note, that in this problem there aren't multiple possible answersxa0— the answer is unique for any input data. Note In the first example, Alya has made one move only. According to the statement, the first move is always putting one candy in the box. Hence Alya ate $$$0$$$ candies. In the second example the possible sequence of Alya's actions looks as follows: put $$$1$$$ candy, put $$$2$$$ candies, eat a candy, eat a candy, put $$$3$$$ candies, eat a candy, put $$$4$$$ candies, eat a candy, put $$$5$$$ candies. This way, she will make exactly $$$n=9$$$ actions and in the end the box will contain $$$1+2-1-1+3-1+4-1+5=11$$$ candies. The answer is $$$4$$$, since she ate $$$4$$$ candies in total. | 1,000 | true | false | false | false | false | false | true | true | false | false | 4,728 |
934B | Problem - 934B - 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 implementation *1200 No tag edit access → Contest materials xa0— the desired number of loops. Output Output an integerxa0— if no such _n_ exists, output -1; otherwise output any such _n_. In the latter case, your output should be a positive decimal integer not exceeding 1018. Examples Input 2 Output 462 Input 6 Output 8080 | 1,200 | false | false | true | false | false | true | false | false | false | false | 5,965 |
1827B2 | The only difference between this problem and the easy version is the constraints on $$$t$$$ and $$$n$$$. You are given an array $$$a$$$, consisting of $$$n$$$ distinct integers $$$a_1, a_2, ldots, a_n$$$. Define the beauty of an array $$$p_1, p_2, ldots p_k$$$ as the minimum amount of time needed to sort this array using an arbitrary number of range-sort operations. In each range-sort operation, you will do the following: Choose two integers $$$l$$$ and $$$r$$$ ($$$1 le l < r le k$$$). Sort the subarray $$$p_l, p_{l + 1}, ldots, p_r$$$ in $$$r - l$$$ seconds. Please calculate the sum of beauty over all subarrays of array $$$a$$$. A subarray of an array is defined as a sequence of consecutive elements of the array. Input Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 le t le 10^4$$$). The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 3 cdot 10^5$$$)xa0— the length of the array $$$a$$$. The second line of each test case consists of $$$n$$$ integers $$$a_1,a_2,ldots, a_n$$$ ($$$1le a_ile 10^9$$$). It is guaranteed that all elements of $$$a$$$ are pairwise distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3 cdot 10^5$$$. Output For each test case, output the sum of beauty over all subarrays of array $$$a$$$. Example Input 5 2 6 4 3 3 10 6 4 4 8 7 2 5 9 8 2 4 6 12 2 6 13 3 15 5 10 8 16 9 11 18 Note In the first test case: The subarray $$$[6]$$$ is already sorted, so its beauty is $$$0$$$. The subarray $$$[4]$$$ is already sorted, so its beauty is $$$0$$$. You can sort the subarray $$$[6, 4]$$$ in one operation by choosing $$$l = 1$$$ and $$$r = 2$$$. Its beauty is equal to $$$1$$$. The sum of beauty over all subarrays of the given array is equal to $$$0 + 0 + 1 = 1$$$. In the second test case: The subarray $$$[3]$$$ is already sorted, so its beauty is $$$0$$$. The subarray $$$[10]$$$ is already sorted, so its beauty is $$$0$$$. The subarray $$$[6]$$$ is already sorted, so its beauty is $$$0$$$. The subarray $$$[3, 10]$$$ is already sorted, so its beauty is $$$0$$$. You can sort the subarray $$$[10, 6]$$$ in one operation by choosing $$$l = 1$$$ and $$$r = 2$$$. Its beauty is equal to $$$2 - 1 = 1$$$. You can sort the subarray $$$[3, 10, 6]$$$ in one operation by choosing $$$l = 2$$$ and $$$r = 3$$$. Its beauty is equal to $$$3 - 2 = 1$$$. The sum of beauty over all subarrays of the given array is equal to $$$0 + 0 + 0 + 0 + 1 + 1 = 2$$$. | 2,400 | false | true | false | true | true | false | false | true | false | false | 1,321 |
792C | A positive integer number _n_ is written on a blackboard. It consists of not more than 105 digits. You have to transform it into a beautiful number by erasing some of the digits, and you want to erase as few digits as possible. The number is called beautiful if it consists of at least one digit, doesn't have leading zeroes and is a multiple of 3. For example, 0, 99, 10110 are beautiful numbers, and 00, 03, 122 are not. Write a program which for the given _n_ will find a beautiful number such that _n_ can be transformed into this number by erasing as few digits as possible. You can erase an arbitraty set of digits. For example, they don't have to go one after another in the number _n_. If it's impossible to obtain a beautiful number, print -1. If there are multiple answers, print any of them. Input The first line of input contains _n_ — a positive integer number without leading zeroes (1u2009≤u2009_n_u2009<u200910100000). Output Print one number — any beautiful number obtained by erasing as few as possible digits. If there is no answer, print u2009-u20091. Note In the first example it is enough to erase only the first digit to obtain a multiple of 3. But if we erase the first digit, then we obtain a number with a leading zero. So the minimum number of digits to be erased is two. | 2,000 | true | true | false | true | false | false | false | false | false | false | 6,611 |
875C | You all know that the Library of Bookland is the largest library in the world. There are dozens of thousands of books in the library. Some long and uninteresting story was removed... The alphabet of Bookland is so large that its letters are denoted by positive integers. Each letter can be small or large, the large version of a letter _x_ is denoted by _x_'. BSCII encoding, which is used everywhere in Bookland, is made in that way so that large letters are presented in the order of the numbers they are denoted by, and small letters are presented in the order of the numbers they are denoted by, but all large letters are before all small letters. For example, the following conditions hold: 2u2009<u20093, 2'u2009<u20093', 3'u2009<u20092. A word _x_1,u2009_x_2,u2009...,u2009_x__a_ is not lexicographically greater than _y_1,u2009_y_2,u2009...,u2009_y__b_ if one of the two following conditions holds: _a_u2009≤u2009_b_ and _x_1u2009=u2009_y_1,u2009...,u2009_x__a_u2009=u2009_y__a_, i.e. the first word is the prefix of the second word; there is a position 1u2009≤u2009_j_u2009≤u2009_min_(_a_,u2009_b_), such that _x_1u2009=u2009_y_1,u2009...,u2009_x__j_u2009-u20091u2009=u2009_y__j_u2009-u20091 and _x__j_u2009<u2009_y__j_, i.e. at the first position where the words differ the first word has a smaller letter than the second word has. For example, the word "3' 7 5" is before the word "2 4' 6" in lexicographical order. It is said that sequence of words is in lexicographical order if each word is not lexicographically greater than the next word in the sequence. Denis has a sequence of words consisting of small letters only. He wants to change some letters to large (let's call this process a capitalization) in such a way that the sequence of words is in lexicographical order. However, he soon realized that for some reason he can't change a single letter in a single word. He only can choose a letter and change all of its occurrences in all words to large letters. He can perform this operation any number of times with arbitrary letters of Bookland's alphabet. Help Denis to choose which letters he needs to capitalize (make large) in order to make the sequence of words lexicographically ordered, or determine that it is impossible. Note that some words can be equal. Input The first line contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009100u2009000, 1u2009≤u2009_m_u2009≤u2009100u2009000)xa0— the number of words and the number of letters in Bookland's alphabet, respectively. The letters of Bookland's alphabet are denoted by integers from 1 to _m_. Each of the next _n_ lines contains a description of one word in format _l__i_,u2009_s__i_,u20091,u2009_s__i_,u20092,u2009...,u2009_s__i_,u2009_l__i_ (1u2009≤u2009_l__i_u2009≤u2009100u2009000, 1u2009≤u2009_s__i_,u2009_j_u2009≤u2009_m_), where _l__i_ is the length of the word, and _s__i_,u2009_j_ is the sequence of letters in the word. The words are given in the order Denis has them in the sequence. It is guaranteed that the total length of all words is not greater than 100u2009000. Output In the first line print "Yes" (without quotes), if it is possible to capitalize some set of letters in such a way that the sequence of words becomes lexicographically ordered. Otherwise, print "No" (without quotes). If the required is possible, in the second line print _k_xa0— the number of letters Denis has to capitalize (make large), and in the third line print _k_ distinct integersxa0— these letters. Note that you don't need to minimize the value _k_. You can print the letters in any order. If there are multiple answers, print any of them. Examples Input 4 3 1 2 1 1 3 1 3 2 2 1 1 Input 6 5 2 1 2 2 1 2 3 1 2 3 2 1 5 2 4 4 2 4 4 Input 4 3 4 3 2 2 1 3 1 1 3 3 2 3 3 2 3 1 Note In the first example after Denis makes letters 2 and 3 large, the sequence looks like the following: 2' 1 1 3' 2' 1 1 The condition 2'u2009<u20091 holds, so the first word is not lexicographically larger than the second word. The second word is the prefix of the third word, so the are in lexicographical order. As the first letters of the third and the fourth words are the same, and 3'u2009<u20091, then the third word is not lexicographically larger than the fourth word. In the second example the words are in lexicographical order from the beginning, so Denis can do nothing. In the third example there is no set of letters such that if Denis capitalizes them, the sequence becomes lexicographically ordered. | 2,100 | false | false | true | false | false | false | false | false | false | true | 6,246 |
1949J | This problem has an attachment. You can use it to simulate and visualize the movements of the amoeba. Amoeba Amanda lives inside a rectangular grid of square pixels. Her body occupies some of these pixels. Other pixels may be either free or blocked. Amanda moves across the grid using the so-called amoeboid movement. In each step of such a movement, her body first shrinks by one pixel (one pixel of the body is removed and becomes free), and then grows at a different place (one previously-free pixel is added to the body). To prevent structural damage, Amanda's body always occupies a connected region of pixels, which means that any pair of pixels forming the body can be connected by a sequence of adjacent pixels without ever leaving the body. Two pixels are considered adjacent if they share a common side (each pixel has at most 4 neighbours). The body remains connected even during the movement, including the moment after removing a pixel and before adding another one. Your task is to help Amanda find her way around. Given her initial position and desired final position, suggest a sequence of valid moves leading from the former to the latter. Illustration of sample $$$1$$$: The filled shape is the initial position, the dotted region is the final position. Input The first line contains two integers $$$r$$$ and $$$c$$$ ($$$1le r,c le 50$$$)xa0— the size of the rectangular grid in pixels. The next $$$r$$$ lines contain $$$c$$$ characters each, describing the initial position of Amanda. Each of those characters is either a dot $$$ exttt{.}$$$ denoting a free pixel, an asterisk $$$ exttt{*}$$$ denoting Amanda's body, or an $$$ exttt{X}$$$ denoting a blocked pixel which may never be occupied. The next line is empty. The next $$$r$$$ lines describe the desired final position in the same format as the initial position. It is guaranteed that: The number of pixels forming Amanda's body is the same in both positions, and it is at least 2. The body of Amanda is connected in the initial position. The body of Amanda is connected in the final position. The blocked pixels do not change between the descriptions of the initial and final position, their placement is exactly the same in both positions. Output Print $$$ exttt{YES}$$$ if it is possible for Amanda to go from the initial position to the final one. Otherwise, print $$$ exttt{NO}$$$. If it is possible, on the next line print one integer $$$m$$$ ($$$0le mle 10,000$$$)xa0— the number of moves to execute. The following $$$m$$$ lines must contain four integer coordinates each: $$$i_1$$$, $$$j_1$$$, $$$i_2$$$, $$$j_2$$$ ($$$1le i_1,i_2le r$$$, $$$1le j_1,j_2le c$$$). These four coordinates specify one move, meaning that the pixel at $$$i_1$$$-th row and $$$j_1$$$-th column is first removed from the body. Then, $$$(i_2,j_2)$$$ must designate a different location where one pixel is added. The sequence should consist only of valid moves and after the last move, Amanda's body should occupy the desired final position. If there are multiple solutions, print any of them. Under the assumptions of this problem, it can be proven that if it is possible for Amanda to go from the initial position to the desired final one, then it is possible to do it with at most $$$10,000$$$ moves. Examples Input 5 8 .******. **.X**.. *******. **.X**.. .******. .******. ...X**** .******* ...X**** .******. Output YES 5 3 1 3 8 2 1 2 8 4 1 4 8 2 2 4 7 4 2 2 7 Input 2 5 *.X.. **X.. ..X** ..X*. Note In the first sample, Amanda executes 5 moves to reach the final position, as shown in the figure below. | 2,600 | false | false | true | false | false | false | false | false | false | true | 583 |
2038J | Monocarp is waiting for a bus at the bus stop. Unfortunately, there are many people who want to ride a bus too. You are given a list of events of two types: B $$$b_i$$$xa0— a bus with $$$b_i$$$ free seats arrives at the stop; P $$$p_i$$$xa0— $$$p_i$$$ people arrive at the stop. These events are listed in a chronological order. When a bus arrives, the following happens. All people at the bus stop (except for Monocarp) try to enter the bus. If there are enough free seats for all of them, then they all enter the bus. Otherwise, some people remain at the bus stop (the number of people who enter the bus is equal to the number of free seats). If there is still at least one free seat after all people (except for Monocarp) enter the bus, then Monocarp can decide to enter this bus as well (but he might choose to wait for another bus). For each bus, you have to determine if it is possible for Monocarp to take that bus. Input The first line contains one integer $$$n$$$ $$$(1 le n le 10^3)$$$xa0— the number of events. Then, $$$n$$$ lines follow. The $$$i$$$-th of them contains the description of the $$$i$$$-th event in one of the two following formats: B $$$b_i$$$ ($$$1 le b_i le 10^6$$$)xa0— a bus with $$$b_i$$$ free seats arrives at the stop; P $$$p_i$$$ ($$$1 le p_i le 10^6$$$)xa0— $$$p_i$$$ people arrive at the stop. Additional constraint on the input: there is at least one event of type B. Output For each event of type B, print YES if it is possible for Monocarp to take the corresponding bus, or NO otherwise (case-insensitive). Example Input 10 P 2 P 5 B 8 P 14 B 5 B 9 B 3 P 2 B 1 B 2 Output YES NO NO YES NO YES | 800 | false | true | true | false | false | false | false | false | false | false | 29 |
1284G | It is only a few days until Seollal (Korean Lunar New Year), and Jaehyun has invited his family to his garden. There are kids among the guests. To make the gathering more fun for the kids, Jaehyun is going to run a game of hide-and-seek. The garden can be represented by a $$$n imes m$$$ grid of unit cells. Some (possibly zero) cells are blocked by rocks, and the remaining cells are free. Two cells are neighbors if they share an edge. Each cell has up to 4 neighbors: two in the horizontal direction and two in the vertical direction. Since the garden is represented as a grid, we can classify the cells in the garden as either "black" or "white". The top-left cell is black, and two cells which are neighbors must be different colors. Cell indices are 1-based, so the top-left corner of the garden is cell $$$(1, 1)$$$. Jaehyun wants to turn his garden into a maze by placing some walls between two cells. Walls can only be placed between neighboring cells. If the wall is placed between two neighboring cells $$$a$$$ and $$$b$$$, then the two cells $$$a$$$ and $$$b$$$ are not neighboring from that point. One can walk directly between two neighboring cells if and only if there is no wall directly between them. A maze must have the following property. For each pair of free cells in the maze, there must be exactly one simple path between them. A simple path between cells $$$a$$$ and $$$b$$$ is a sequence of free cells in which the first cell is $$$a$$$, the last cell is $$$b$$$, all cells are distinct, and any two consecutive cells are neighbors which are not directly blocked by a wall. At first, kids will gather in cell $$$(1, 1)$$$, and start the hide-and-seek game. A kid can hide in a cell if and only if that cell is free, it is not $$$(1, 1)$$$, and has exactly one free neighbor. Jaehyun planted roses in the black cells, so it's dangerous if the kids hide there. So Jaehyun wants to create a maze where the kids can only hide in white cells. You are given the map of the garden as input. Your task is to help Jaehyun create a maze. Input Your program will be judged in multiple test cases. The first line contains the number of test cases $$$t$$$. ($$$1 le t le 100$$$). Afterward, $$$t$$$ test cases with the described format will be given. The first line of a test contains two integers $$$n, m$$$ ($$$2 le n, m le 20$$$), the size of the grid. In the next $$$n$$$ line of a test contains a string of length $$$m$$$, consisting of the following characters (without any whitespace): O: A free cell. X: A rock. It is guaranteed that the first cell (cell $$$(1, 1)$$$) is free, and every free cell is reachable from $$$(1, 1)$$$. If $$$t geq 2$$$ is satisfied, then the size of the grid will satisfy $$$n le 10, m le 10$$$. In other words, if any grid with size $$$n > 10$$$ or $$$m > 10$$$ is given as an input, then it will be the only input on the test case ($$$t = 1$$$). Output For each test case, print the following: If there are no possible mazes, print a single line NO. Otherwise, print a single line YES, followed by a grid of size $$$(2n-1) imes (2m-1)$$$ denoting the found maze. The rules for displaying the maze follows. All cells are indexed in 1-base. For all $$$1 le i le n, 1 le j le m$$$, if the cell $$$(i, j)$$$ is free cell, print 'O' in the cell $$$(2i-1, 2j-1)$$$. Otherwise, print 'X' in the cell $$$(2i-1, 2j-1)$$$. For all $$$1 le i le n, 1 le j le m-1$$$, if the neighboring cell $$$(i, j), (i, j+1)$$$ have wall blocking it, print ' ' in the cell $$$(2i-1, 2j)$$$. Otherwise, print any printable character except spaces in the cell $$$(2i-1, 2j)$$$. A printable character has an ASCII code in range $$$[32, 126]$$$: This includes spaces and alphanumeric characters. For all $$$1 le i le n-1, 1 le j le m$$$, if the neighboring cell $$$(i, j), (i+1, j)$$$ have wall blocking it, print 'xa0' in the cell $$$(2i, 2j-1)$$$. Otherwise, print any printable character except spaces in the cell $$$(2i, 2j-1)$$$ For all $$$1 le i le n-1, 1 le j le m-1$$$, print any printable character in the cell $$$(2i, 2j)$$$. Please, be careful about trailing newline characters or spaces. Each row of the grid should contain exactly $$$2m-1$$$ characters, and rows should be separated by a newline character. Trailing spaces must not be omitted in a row. | 3,300 | false | false | false | false | false | false | false | false | false | true | 4,268 |
1380A | Problem - 1380A - 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 data structures *900 No tag edit access → Contest materials xa0— the number of test cases. Next $$$2T$$$ lines contain test casesxa0— two lines per test case. The first line of each test case contains the single integer $$$n$$$ ($$$3 le n le 1000$$$)xa0— the length of the permutation $$$p$$$. The second line contains $$$n$$$ integers $$$p_1, p_2, dots, p_n$$$ ($$$1 le p_i le n$$$; $$$p_i eq p_j$$$ if $$$i eq j$$$)xa0— the permutation $$$p$$$. Output For each test case: if there are such indices $$$i$$$, $$$j$$$ and $$$k$$$, print YES (case insensitive) and the indices themselves; if there are no such indices, print NO (case insensitive). If there are multiple valid triples of indices, print any of them. Example Input 3 4 2 1 4 3 6 4 6 1 2 5 3 5 5 3 1 2 4 Output YES 2 3 4 YES 3 5 6 NO | 900 | false | false | false | false | true | false | true | false | false | false | 3,771 |
1321C | You are given a string $$$s$$$ consisting of lowercase Latin letters. Let the length of $$$s$$$ be $$$s$$$. You may perform several operations on this string. In one operation, you can choose some index $$$i$$$ and remove the $$$i$$$-th character of $$$s$$$ ($$$s_i$$$) if at least one of its adjacent characters is the previous letter in the Latin alphabet for $$$s_i$$$. For example, the previous letter for b is a, the previous letter for s is r, the letter a has no previous letters. Note that after each removal the length of the string decreases by one. So, the index $$$i$$$ should satisfy the condition $$$1 le i le s$$$ during each operation. For the character $$$s_i$$$ adjacent characters are $$$s_{i-1}$$$ and $$$s_{i+1}$$$. The first and the last characters of $$$s$$$ both have only one adjacent character (unless $$$s = 1$$$). Consider the following example. Let $$$s=$$$ bacabcab. 1. During the first move, you can remove the first character $$$s_1=$$$ b because $$$s_2=$$$ a. Then the string becomes $$$s=$$$ acabcab. 2. During the second move, you can remove the fifth character $$$s_5=$$$ c because $$$s_4=$$$ b. Then the string becomes $$$s=$$$ acabab. 3. During the third move, you can remove the sixth character $$$s_6=$$$'b' because $$$s_5=$$$ a. Then the string becomes $$$s=$$$ acaba. 4. During the fourth move, the only character you can remove is $$$s_4=$$$ b, because $$$s_3=$$$ a (or $$$s_5=$$$ a). The string becomes $$$s=$$$ acaa and you cannot do anything with it. Your task is to find the maximum possible number of characters you can remove if you choose the sequence of operations optimally. Input The first line of the input contains one integer $$$s$$$ ($$$1 le s le 100$$$) — the length of $$$s$$$. The second line of the input contains one string $$$s$$$ consisting of $$$s$$$ lowercase Latin letters. Output Print one integer — the maximum possible number of characters you can remove if you choose the sequence of moves optimally. Note The first example is described in the problem statement. Note that the sequence of moves provided in the statement is not the only, but it can be shown that the maximum possible answer to this test is $$$4$$$. In the second example, you can remove all but one character of $$$s$$$. The only possible answer follows. 1. During the first move, remove the third character $$$s_3=$$$ d, $$$s$$$ becomes bca. 2. During the second move, remove the second character $$$s_2=$$$ c, $$$s$$$ becomes ba. 3. And during the third move, remove the first character $$$s_1=$$$ b, $$$s$$$ becomes a. | 1,600 | false | true | false | false | false | true | true | false | false | false | 4,108 |
1814F | There are $$$n$$$ communication towers, numbered from $$$1$$$ to $$$n$$$, and $$$m$$$ bidirectional wires between them. Each tower has a certain set of frequencies that it accepts, the $$$i$$$-th of them accepts frequencies from $$$l_i$$$ to $$$r_i$$$. Let's say that a tower $$$b$$$ is accessible from a tower $$$a$$$, if there exists a frequency $$$x$$$ and a sequence of towers $$$a=v_1, v_2, dots, v_k=b$$$, where consecutive towers in the sequence are directly connected by a wire, and each of them accepts frequency $$$x$$$. Note that accessibility is not transitive, i.u2009e if $$$b$$$ is accessible from $$$a$$$ and $$$c$$$ is accessible from $$$b$$$, then $$$c$$$ may not be accessible from $$$a$$$. Your task is to determine the towers that are accessible from the $$$1$$$-st tower. Input The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 le n le 2 cdot 10^5$$$; $$$0 le m le 4 cdot 10^5$$$)xa0— the number of communication towers and the number of wires, respectively. Then $$$n$$$ lines follows, the $$$i$$$-th of them contains two integers $$$l_i$$$ and $$$r_i$$$ ($$$1 le l_i le r_i le 2 cdot 10^5$$$)xa0— the boundaries of the acceptable frequencies for the $$$i$$$-th tower. Then $$$m$$$ lines follows, the $$$i$$$-th of them contains two integers $$$v_i$$$ and $$$u_i$$$ ($$$1 le v_i, u_i le n$$$; $$$v_i e u_i$$$)xa0— the $$$i$$$-th wire that connects towers $$$v_i$$$ and $$$u_i$$$. There are no two wires connecting the same pair of towers. Output In a single line, print distinct integers from $$$1$$$ to $$$n$$$ in ascending orderxa0— the indices of the communication towers that are accessible from the $$$1$$$-st tower. Examples Input 6 5 3 5 1 2 2 4 2 3 3 3 4 6 1 3 6 1 3 5 3 6 2 3 Input 5 5 1 3 2 3 2 2 3 5 2 4 1 2 2 3 3 4 4 1 4 5 | 2,700 | false | false | false | false | false | false | true | false | false | false | 1,382 |
983D | Arkady has got an infinite plane painted in color $$$0$$$. Then he draws $$$n$$$ rectangles filled with paint with sides parallel to the Cartesian coordinate axes, one after another. The color of the $$$i$$$-th rectangle is $$$i$$$ (rectangles are enumerated from $$$1$$$ to $$$n$$$ in the order he draws them). It is possible that new rectangles cover some of the previous ones completely or partially. Count the number of different colors on the plane after Arkady draws all the rectangles. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 100,000$$$)xa0— the number of rectangles. The $$$i$$$-th of the next $$$n$$$ lines contains $$$4$$$ integers $$$x_1$$$, $$$y_1$$$, $$$x_2$$$ and $$$y_2$$$ ($$$-10^9 le x_1 < x_2 le 10^9$$$, $$$-10^9 le y_1 < y_2 le 10^9$$$)xa0— the coordinates of corners of the $$$i$$$-th rectangle. Output In the single line print the number of different colors in the plane, including color $$$0$$$. Examples Input 5 -1 -1 1 1 -4 0 0 4 0 0 4 4 -4 -4 0 0 0 -4 4 0 Input 4 0 0 4 4 -4 -4 0 0 0 -4 4 0 -2 -4 2 4 Note That's how the plane looks in the first sampleThat's how the plane looks in the second sample $$$0$$$ = white, $$$1$$$ = cyan, $$$2$$$ = blue, $$$3$$$ = purple, $$$4$$$ = yellow, $$$5$$$ = red. | 3,300 | false | false | false | false | true | false | false | false | false | false | 5,783 |
1566A | You are given two positive integers $$$n$$$ and $$$s$$$. Find the maximum possible median of an array of $$$n$$$ non-negative integers (not necessarily distinct), such that the sum of its elements is equal to $$$s$$$. A median of an array of integers of length $$$m$$$ is the number standing on the $$$lceil {frac{m}{2}} ceil$$$-th (rounding up) position in the non-decreasing ordering of its elements. Positions are numbered starting from $$$1$$$. For example, a median of the array $$$[20,40,20,50,50,30]$$$ is the $$$lceil frac{m}{2} ceil$$$-th element of $$$[20,20,30,40,50,50]$$$, so it is $$$30$$$. There exist other definitions of the median, but in this problem we use the described definition. Input The input consists of 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. Each test case contains a single line with two integers $$$n$$$ and $$$s$$$ ($$$1 le n, s le 10^9$$$)xa0— the length of the array and the required sum of the elements. Output For each test case print a single integerxa0— the maximum possible median. Example Input 8 1 5 2 5 3 5 2 1 7 17 4 14 1 1000000000 1000000000 1 Output 5 2 2 0 4 4 1000000000 0 Note Possible arrays for the first three test cases (in each array the median is underlined): In the first test case $$$[underline{5}]$$$ In the second test case $$$[underline{2}, 3]$$$ In the third test case $$$[1, underline{2}, 2]$$$ | 800 | true | true | false | false | false | false | false | true | false | false | 2,813 |
834A | Walking through the streets of Marshmallow City, Slastyona have spotted some merchants selling a kind of useless toy which is very popular nowadaysxa0– caramel spinner! Wanting to join the craze, she has immediately bought the strange contraption. Spinners in Sweetland have the form of V-shaped pieces of caramel. Each spinner can, well, spin around an invisible magic axis. At a specific point in time, a spinner can take 4 positions shown below (each one rotated 90 degrees relative to the previous, with the fourth one followed by the first one): After the spinner was spun, it starts its rotation, which is described by a following algorithm: the spinner maintains its position for a second then majestically switches to the next position in clockwise or counter-clockwise order, depending on the direction the spinner was spun in. Slastyona managed to have spinner rotating for exactly _n_ seconds. Being fascinated by elegance of the process, she completely forgot the direction the spinner was spun in! Lucky for her, she managed to recall the starting position, and wants to deduct the direction given the information she knows. Help her do this. Input There are two characters in the first stringxa0– the starting and the ending position of a spinner. The position is encoded with one of the following characters: v (ASCII code 118, lowercase v), < (ASCII code 60), ^ (ASCII code 94) or > (ASCII code 62) (see the picture above for reference). Characters are separated by a single space. In the second strings, a single number _n_ is given (0u2009≤u2009_n_u2009≤u2009109)xa0– the duration of the rotation. It is guaranteed that the ending position of a spinner is a result of a _n_ second spin in any of the directions, assuming the given starting position. Output Output cw, if the direction is clockwise, ccwxa0– if counter-clockwise, and undefined otherwise. | 900 | false | false | true | false | false | false | false | false | false | false | 6,429 |
1831A | You are given a permutation$$$^dagger$$$ $$$a$$$ of length $$$n$$$. Find any permutation $$$b$$$ of length $$$n$$$ such that $$$a_1+b_1 le a_2+b_2 le a_3+b_3 le ldots le a_n+b_n$$$. It can be proven that a permutation $$$b$$$ that satisfies the condition above always 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 of input contains a single integer $$$t$$$ ($$$1 le t le 2000$$$)xa0— the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 100$$$)xa0— the length of permutations $$$a$$$ and $$$b$$$. The second line of each test case contains $$$n$$$ distinct integers $$$a_1,a_2,ldots,a_n$$$ ($$$1 le a_i le n$$$)xa0— the elements of permutation $$$a$$$. All elements of $$$a$$$ are distinct. Note that there is no bound on the sum of $$$n$$$ over all test cases. Output For each test case, output any permutation $$$b$$$ which satisfies the constraints mentioned in the statement. It can be proven that a permutation $$$b$$$ that satisfies the condition above always exists. Example Input 5 5 1 2 4 5 3 2 1 2 1 1 3 3 2 1 4 1 4 3 2 Output 1 2 4 3 5 2 1 1 1 2 3 1 2 3 4 Note In the first test case $$$a=[1, 2, 4, 5, 3]$$$. Then the permutation $$$b=[1, 2, 4, 3, 5]$$$ satisfies the condition because $$$1 + 1 le 2 + 2 le 4 + 4 le 5 + 3 le 3 + 5$$$. | 800 | false | false | false | false | false | true | false | false | false | false | 1,300 |
313A | Ilya is a very clever lion, he lives in an unusual city ZooVille. In this city all the animals have their rights and obligations. Moreover, they even have their own bank accounts. The state of a bank account is an integer. The state of a bank account can be a negative number. This means that the owner of the account owes the bank money. Ilya the Lion has recently had a birthday, so he got a lot of gifts. One of them (the gift of the main ZooVille bank) is the opportunity to delete the last digit or the digit before last from the state of his bank account no more than once. For example, if the state of Ilya's bank account is -123, then Ilya can delete the last digit and get his account balance equal to -12, also he can remove its digit before last and get the account balance equal to -13. Of course, Ilya is permitted not to use the opportunity to delete a digit from the balance. Ilya is not very good at math, and that's why he asks you to help him maximize his bank account. Find the maximum state of the bank account that can be obtained using the bank's gift. Input The single line contains integer _n_ (10u2009≤u2009_n_u2009≤u2009109) — the state of Ilya's bank account. Output In a single line print an integer — the maximum state of the bank account that Ilya can get. Note In the first test sample Ilya doesn't profit from using the present. In the second test sample you can delete digit 1 and get the state of the account equal to 0. | 900 | false | false | true | false | false | false | false | false | false | false | 8,595 |
1973C | Fox loves permutations! She came up with the following problem and asked Cat to solve it: You are given an even positive integer $$$n$$$ and a permutation$$$^dagger$$$ $$$p$$$ of length $$$n$$$. The score of another permutation $$$q$$$ of length $$$n$$$ is the number of local maximums in the array $$$a$$$ of length $$$n$$$, where $$$a_i = p_i + q_i$$$ for all $$$i$$$ ($$$1 le i le n$$$). In other words, the score of $$$q$$$ is the number of $$$i$$$ such that $$$1 < i < n$$$ (note the strict inequalities), $$$a_{i-1} < a_i$$$, and $$$a_i > a_{i+1}$$$ (once again, note the strict inequalities). Find the permutation $$$q$$$ that achieves the maximum score for given $$$n$$$ and $$$p$$$. If there exist multiple such permutations, you can pick any of them. $$$^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 The first line of input contains an integer $$$t$$$ ($$$1 leq t leq 10^4$$$)xa0— the number of test cases in the input you will have to solve. The first line of each test case contains one even integer $$$n$$$ ($$$4 leq n leq 10^5$$$, $$$n$$$ is even)xa0— the length of the permutation $$$p$$$. The second line of each test case contains the $$$n$$$ integers $$$p_1, p_2, ldots, p_n$$$ ($$$1 leq p_i leq n$$$). It is guaranteed that $$$p$$$ is a permutation of length $$$n$$$. It is guaranteed that the sum of $$$n$$$ across all test cases doesn't exceed $$$10^5$$$. Output For each test case, output one line containing any permutation of length $$$n$$$ (the array $$$q$$$), such that $$$q$$$ maximizes the score under the given constraints. Example Input 4 4 1 2 3 4 4 4 3 1 2 6 6 5 1 4 2 3 8 1 2 4 5 7 6 8 3 Output 2 4 1 3 3 1 4 2 2 5 1 4 3 6 5 4 8 2 7 1 6 3 Note In the first example, $$$a = [3, 6, 4, 7]$$$. The array has just one local maximum (on the second position), so the score of the chosen permutation $$$q$$$ is $$$1$$$. It can be proven that this score is optimal under the constraints. In the last example, the resulting array $$$a = [6, 6, 12, 7, 14, 7, 14, 6]$$$ has $$$3$$$ local maximumsxa0— on the third, fifth and seventh positions. | 1,700 | true | true | true | false | false | true | false | false | true | false | 453 |
163C | Anton came to a chocolate factory. There he found a working conveyor and decided to run on it from the beginning to the end. The conveyor is a looped belt with a total length of 2_l_ meters, of which _l_ meters are located on the surface and are arranged in a straight line. The part of the belt which turns at any moment (the part which emerges from under the floor to the surface and returns from the surface under the floor) is assumed to be negligibly short. The belt is moving uniformly at speed _v_1 meters per second. Anton will be moving on it in the same direction at the constant speed of _v_2 meters per second, so his speed relatively to the floor will be _v_1u2009+u2009_v_2 meters per second. Anton will neither stop nor change the speed or the direction of movement. Here and there there are chocolates stuck to the belt (_n_ chocolates). They move together with the belt, and do not come off it. Anton is keen on the chocolates, but he is more keen to move forward. So he will pick up all the chocolates he will pass by, but nothing more. If a chocolate is at the beginning of the belt at the moment when Anton starts running, he will take it, and if a chocolate is at the end of the belt at the moment when Anton comes off the belt, he will leave it. The figure shows an example with two chocolates. One is located in the position _a_1u2009=u2009_l_u2009-u2009_d_, and is now on the top half of the belt, the second one is in the position _a_2u2009=u20092_l_u2009-u2009_d_, and is now on the bottom half of the belt. You are given the positions of the chocolates relative to the initial start position of the belt 0u2009≤u2009_a_1u2009<u2009_a_2u2009<u2009...u2009<u2009_a__n_u2009<u20092_l_. The positions on the belt from 0 to _l_ correspond to the top, and from _l_ to 2_l_ — to the the bottom half of the belt (see example). All coordinates are given in meters. Anton begins to run along the belt at a random moment of time. This means that all possible positions of the belt at the moment he starts running are equiprobable. For each _i_ from 0 to _n_ calculate the probability that Anton will pick up exactly _i_ chocolates. Input The first line contains space-separated integers _n_, _l_, _v_1 and _v_2 (1u2009≤u2009_n_u2009≤u2009105, 1u2009≤u2009_l_,u2009_v_1,u2009_v_2u2009≤u2009109) — the number of the chocolates, the length of the conveyor's visible part, the conveyor's speed and Anton's speed. The second line contains a sequence of space-separated integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (0u2009≤u2009_a_1u2009<u2009_a_2u2009<u2009...u2009<u2009_a__n_u2009<u20092_l_) — the coordinates of the chocolates. Output Print _n_u2009+u20091 numbers (one per line): the probabilities that Anton picks up exactly _i_ chocolates, for each _i_ from 0 (the first line) to _n_ (the last line). The answer will be considered correct if each number will have absolute or relative error of at most than 10u2009-u20099. Note In the first sample test Anton can pick up a chocolate if by the moment he starts running its coordinate is less than 0.5; but if by the moment the boy starts running the chocolate's coordinate is greater than or equal to 0.5, then Anton won't be able to pick it up. As all positions of the belt are equiprobable, the probability of picking up the chocolate equals , and the probability of not picking it up equals . | 2,100 | false | false | false | false | false | false | false | false | true | false | 9,213 |
1303A | Problem - 1303A - 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 0's from the string. What is the minimum number of 0's that you have to erase? Input The first line contains one integer $$$t$$$ ($$$1 le t le 100$$$) — the number of test cases. Then $$$t$$$ lines follow, each representing a test case. Each line contains one string $$$s$$$ ($$$1 le s le 100$$$); each character of $$$s$$$ is either 0 or 1. Output Print $$$t$$$ integers, where the $$$i$$$-th integer is the answer to the $$$i$$$-th testcase (the minimum number of 0's that you have to erase from $$$s$$$). Example Input 3 010011 0 1111000 Output 2 0 0 Note In the first test case you have to delete the third and forth symbols from string 010011 (it turns into 0111). | 800 | false | false | true | false | false | false | false | false | false | false | 4,178 |
780F | A couple of friends, Axel and Marston are travelling across the country of Bitland. There are _n_ towns in Bitland, with some pairs of towns connected by one-directional roads. Each road in Bitland is either a pedestrian road or a bike road. There can be multiple roads between any pair of towns, and may even be a road from a town to itself. However, no pair of roads shares the starting and the destination towns along with their types simultaneously. The friends are now located in the town 1 and are planning the travel route. Axel enjoys walking, while Marston prefers biking. In order to choose a route diverse and equally interesting for both friends, they have agreed upon the following procedure for choosing the road types during the travel: In the first few steps the route will look as follows: P, PB, PBBP, PBBPBPPB, PBBPBPPBBPPBPBBP, and so on. After that the friends start travelling from the town 1 via Bitlandian roads, choosing the next road according to the next character of their route type each time. If it is impossible to choose the next road, the friends terminate their travel and fly home instead. Help the friends to find the longest possible route that can be travelled along roads of Bitland according to the road types choosing procedure described above. If there is such a route with more than 1018 roads in it, print -1 instead. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009500, 0u2009≤u2009_m_u2009≤u20092_n_2)xa0— the number of towns and roads in Bitland respectively. Next _m_ lines describe the roads. _i_-th of these lines contains three integers _v__i_, _u__i_ and _t__i_ (1u2009≤u2009_v__i_,u2009_u__i_u2009≤u2009_n_, 0u2009≤u2009_t__i_u2009≤u20091), where _v__i_ and _u__i_ denote start and destination towns indices of the _i_-th road, and _t__i_ decribes the type of _i_-th road (0 for a pedestrian road, 1 for a bike road). It is guaranteed that for each pair of distinct indices _i_, _j_ such that 1u2009≤u2009_i_,u2009_j_u2009≤u2009_m_, either _v__i_u2009≠u2009_v__j_, or _u__i_u2009≠u2009_u__j_, or _t__i_u2009≠u2009_t__j_ holds. Output If it is possible to find a route with length strictly greater than 1018, print -1. Otherwise, print the maximum length of a suitable path. Note In the first sample we can obtain a route of length 3 by travelling along the road 1 from town 1 to town 2, and then following the road 2 twice from town 2 to itself. In the second sample we can obtain an arbitrarily long route by travelling the road 1 first, and then choosing road 2 or 3 depending on the necessary type. | 2,400 | false | false | false | true | false | false | false | false | false | true | 6,643 |
1097D | Makoto has a big blackboard with a positive integer $$$n$$$ written on it. He will perform the following action exactly $$$k$$$ times: Suppose the number currently written on the blackboard is $$$v$$$. He will randomly pick one of the divisors of $$$v$$$ (possibly $$$1$$$ and $$$v$$$) and replace $$$v$$$ with this divisor. As Makoto uses his famous random number generator (RNG) and as he always uses $$$58$$$ as his generator seed, each divisor is guaranteed to be chosen with equal probability. He now wonders what is the expected value of the number written on the blackboard after $$$k$$$ steps. It can be shown that this value can be represented as $$$frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are coprime integers and $$$Q otequiv 0 pmod{10^9+7}$$$. Print the value of $$$P cdot Q^{-1}$$$ modulo $$$10^9+7$$$. Input The only line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 leq n leq 10^{15}$$$, $$$1 leq k leq 10^4$$$). Output Print a single integer — the expected value of the number on the blackboard after $$$k$$$ steps as $$$P cdot Q^{-1} pmod{10^9+7}$$$ for $$$P$$$, $$$Q$$$ defined above. Note In the first example, after one step, the number written on the blackboard is $$$1$$$, $$$2$$$, $$$3$$$ or $$$6$$$ — each occurring with equal probability. Hence, the answer is $$$frac{1+2+3+6}{4}=3$$$. In the second example, the answer is equal to $$$1 cdot frac{9}{16}+2 cdot frac{3}{16}+3 cdot frac{3}{16}+6 cdot frac{1}{16}=frac{15}{8}$$$. | 2,200 | true | false | false | true | false | false | false | false | false | false | 5,221 |
472G | There is a simple way to create hard tasks: take one simple problem as the query, and try to find an algorithm that can solve it faster than bruteforce. This kind of tasks usually appears in OI contest, and usually involves data structures. Let's try to create a task, for example, we take the "Hamming distance problem": for two binary strings _s_ and _t_ with the same length, the Hamming distance between them is the number of positions at which the corresponding symbols are different. For example, the Hamming distance between "00111" and "10101" is 2 (the different symbols are marked with bold). We use the Hamming distance problem as a query in the following way: you are given two strings _a_ and _b_ and several queries. Each query will be: what is the Hamming distance between two strings _a__p_1_a__p_1u2009+u20091..._a__p_1u2009+u2009_len_u2009-u20091 and _b__p_2_b__p_2u2009+u20091..._b__p_2u2009+u2009_len_u2009-u20091? Note, that in this problem the strings are zero-based, that is _s_u2009=u2009_s_0_s_1... _s__s_u2009-u20091. Input The first line contains a string _a_ (1u2009≤u2009_a_u2009≤u2009200000). The second line contains a string _b_ (1u2009≤u2009_b_u2009≤u2009200000). Each character of both strings is either "0" or "1". The third line contains an integer _q_ (1u2009≤u2009_q_u2009≤u2009400000) — the number of queries. Each of the following _q_ lines contains three integers: _p_1, _p_2 and _len_ (0u2009≤u2009_p_1u2009≤u2009_a_u2009-u2009_len_;xa00u2009≤u2009_p_2u2009≤u2009_b_u2009-u2009_len_), these numbers denote the parameters of the current query. Output Output _q_ integers — the answers for the queries. Examples Input 101010 11110000 3 0 0 3 2 3 4 5 7 1 Input 10001010101011001010100101010011010 101010100101001010100100101010 5 0 0 12 3 9 7 6 4 15 12 15 10 13 3 20 | 2,800 | false | false | false | false | true | false | false | false | false | false | 7,941 |
180B | Vasya studies divisibility rules at school. Here are some of them: Divisibility by 2. A number is divisible by 2 if and only if its last digit is divisible by 2 or in other words, is even. Divisibility by 3. A number is divisible by 3 if and only if the sum of its digits is divisible by 3. Divisibility by 4. A number is divisible by 4 if and only if its last two digits form a number that is divisible by 4. Divisibility by 5. A number is divisible by 5 if and only if its last digit equals 5 or 0. Divisibility by 6. A number is divisible by 6 if and only if it is divisible by 2 and 3 simultaneously (that is, if the last digit is even and the sum of all digits is divisible by 3). Divisibility by 7. Vasya doesn't know such divisibility rule. Divisibility by 8. A number is divisible by 8 if and only if its last three digits form a number that is divisible by 8. Divisibility by 9. A number is divisible by 9 if and only if the sum of its digits is divisible by 9. Divisibility by 10. A number is divisible by 10 if and only if its last digit is a zero. Divisibility by 11. A number is divisible by 11 if and only if the sum of digits on its odd positions either equals to the sum of digits on the even positions, or they differ in a number that is divisible by 11. Vasya got interested by the fact that some divisibility rules resemble each other. In fact, to check a number's divisibility by 2, 4, 5, 8 and 10 it is enough to check fulfiling some condition for one or several last digits. Vasya calls such rules the 2-type rules. If checking divisibility means finding a sum of digits and checking whether the sum is divisible by the given number, then Vasya calls this rule the 3-type rule (because it works for numbers 3 and 9). If we need to find the difference between the sum of digits on odd and even positions and check whether the difference is divisible by the given divisor, this rule is called the 11-type rule (it works for number 11). In some cases we should divide the divisor into several factors and check whether rules of different types (2-type, 3-type or 11-type) work there. For example, for number 6 we check 2-type and 3-type rules, for number 66 we check all three types. Such mixed divisibility rules are called 6-type rules. And finally, there are some numbers for which no rule works: neither 2-type, nor 3-type, nor 11-type, nor 6-type. The least such number is number 7, so we'll say that in such cases the mysterious 7-type rule works, the one that Vasya hasn't discovered yet. Vasya's dream is finding divisibility rules for all possible numbers. He isn't going to stop on the decimal numbers only. As there are quite many numbers, ha can't do it all by himself. Vasya asked you to write a program that determines the divisibility rule type in the _b_-based notation for the given divisor _d_. Output On the first output line print the type of the rule in the _b_-based notation system, where the divisor is _d_: "2-type", "3-type", "11-type", "6-type" or "7-type". If there are several such types, print the one that goes earlier in the given sequence. If a number belongs to the 2-type, print on the second line the least number of the last _b_-based digits that we will need to use to check the divisibility. Note The divisibility rule for number 3 in binary notation looks as follows: "A number is divisible by 3 if and only if the sum of its digits that occupy the even places differs from the sum of digits that occupy the odd places, in a number that is divisible by 3". That's an 11-type rule. For example, 2110u2009=u2009101012. For it the sum of digits on odd positions equals 1u2009+u20091u2009+u20091u2009=u20093, an on even positions — 0u2009+u20090u2009=u20090. The rule works and the number is divisible by 3. In some notations a number can fit into the 3-type rule and the 11-type rule. In this case the correct answer is "3-type". | 2,300 | true | false | false | false | false | false | false | false | false | false | 9,121 |
125E | The MST (Meaningless State Team) company won another tender for an important state reform in Berland. There are _n_ cities in Berland, some pairs of the cities are connected by roads. Each road has its price. One can move along any road in any direction. The MST team should carry out the repair works on some set of roads such that one can get from any city to any other one moving only along the repaired roads. Moreover, this set should contain exactly _k_ capital roads (that is, the roads that start or finish in the capital). The number of the capital is 1. As the budget has already been approved, the MST Company will profit by finding the set with minimum lengths of roads. Input The first input line contains three integers _n_,u2009_m_,u2009_k_ (1u2009≤u2009_n_u2009≤u20095000;0u2009≤u2009_m_u2009≤u2009105;0u2009≤u2009_k_u2009<u20095000), where _n_ is the number of cities in the country, _m_ is the number of roads in the country, _k_ is the number of capital roads in the required set. Then _m_ lines enumerate the roads in question. Each road is specified by three numbers _a__i_,u2009_b__i_,u2009_w__i_ (1u2009≤u2009_a__i_,u2009_b__i_u2009≤u2009_n_; 1u2009≤u2009_w_u2009≤u2009105), where _a__i_,u2009_b__i_ are the numbers of cities linked by a road and _w__i_ is its length. Between each pair of cities no more than one road exists. There are no roads that start and finish in one city. The capital's number is 1. Output In the first line print the number of roads in the required set. The second line should contain the numbers of roads included in the sought set. If the sought set does not exist, print -1. Examples Input 4 5 2 1 2 1 2 3 1 3 4 1 1 3 3 1 4 2 | 2,400 | false | false | false | false | false | false | false | true | false | true | 9,378 |
1290A | You and your $$$n - 1$$$ friends have found an array of integers $$$a_1, a_2, dots, a_n$$$. You have decided to share it in the following way: All $$$n$$$ of you stand in a line in a particular order. Each minute, the person at the front of the line chooses either the first or the last element of the array, removes it, and keeps it for himself. He then gets out of line, and the next person in line continues the process. You are standing in the $$$m$$$-th position in the line. Before the process starts, you may choose up to $$$k$$$ different people in the line, and persuade them to always take either the first or the last element in the array on their turn (for each person his own choice, not necessarily equal for all people), no matter what the elements themselves are. Once the process starts, you cannot persuade any more people, and you cannot change the choices for the people you already persuaded. Suppose that you're doing your choices optimally. What is the greatest integer $$$x$$$ such that, no matter what are the choices of the friends you didn't choose to control, the element you will take from the array will be greater than or equal to $$$x$$$? Please note that the friends you don't control may do their choice arbitrarily, and they will not necessarily take the biggest element available. Input The input 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 the test cases follows. The first line of each test case contains three space-separated integers $$$n$$$, $$$m$$$ and $$$k$$$ ($$$1 le m le n le 3500$$$, $$$0 le k le n - 1$$$) xa0— the number of elements in the array, your position in line and the number of people whose choices you can fix. The second line of each test case contains $$$n$$$ positive integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 10^9$$$) xa0— elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$3500$$$. Output For each test case, print the largest integer $$$x$$$ such that you can guarantee to obtain at least $$$x$$$. Example Input 4 6 4 2 2 9 2 3 8 5 4 4 1 2 13 60 4 4 1 3 1 2 2 1 2 2 0 1 2 Note In the first test case, an optimal strategy is to force the first person to take the last element and the second person to take the first element. the first person will take the last element ($$$5$$$) because he or she was forced by you to take the last element. After this turn the remaining array will be $$$[2, 9, 2, 3, 8]$$$; the second person will take the first element ($$$2$$$) because he or she was forced by you to take the first element. After this turn the remaining array will be $$$[9, 2, 3, 8]$$$; if the third person will choose to take the first element ($$$9$$$), at your turn the remaining array will be $$$[2, 3, 8]$$$ and you will take $$$8$$$ (the last element); if the third person will choose to take the last element ($$$8$$$), at your turn the remaining array will be $$$[9, 2, 3]$$$ and you will take $$$9$$$ (the first element). Thus, this strategy guarantees to end up with at least $$$8$$$. We can prove that there is no strategy that guarantees to end up with at least $$$9$$$. Hence, the answer is $$$8$$$. In the second test case, an optimal strategy is to force the first person to take the first element. Then, in the worst case, both the second and the third person will take the first element: you will end up with $$$4$$$. | 1,600 | false | false | true | false | true | false | true | false | false | false | 4,246 |
1989A | Monocarp visited a retro arcade club with arcade cabinets. There got curious about the "Catch the Coin" cabinet. The game is pretty simple. The screen represents a coordinate grid such that: the X-axis is directed from left to right; the Y-axis is directed from bottom to top; the center of the screen has coordinates $$$(0, 0)$$$. At the beginning of the game, the character is located in the center, and $$$n$$$ coins appear on the screenxa0— the $$$i$$$-th coin is at coordinates $$$(x_i, y_i)$$$. The coordinates of all coins are different and not equal to $$$(0, 0)$$$. In one second, Monocarp can move the character in one of eight directions. If the character is at coordinates $$$(x, y)$$$, then it can end up at any of the coordinates $$$(x, y + 1)$$$, $$$(x + 1, y + 1)$$$, $$$(x + 1, y)$$$, $$$(x + 1, y - 1)$$$, $$$(x, y - 1)$$$, $$$(x - 1, y - 1)$$$, $$$(x - 1, y)$$$, $$$(x - 1, y + 1)$$$. If the character ends up at the coordinates with a coin, then Monocarp collects that coin. After Monocarp makes a move, all coins fall down by $$$1$$$, that is, they move from $$$(x, y)$$$ to $$$(x, y - 1)$$$. You can assume that the game field is infinite in all directions. Monocarp wants to collect at least one coin, but cannot decide which coin to go for. Help him determine, for each coin, whether he can collect it. Input The first line contains a single integer $$$n$$$ ($$$1 le n le 500$$$)xa0— the number of coins. In the $$$i$$$-th of the next $$$n$$$ lines, two integers $$$x_i$$$ and $$$y_i$$$ ($$$-50 le x_i, y_i le 50$$$) are writtenxa0— the coordinates of the $$$i$$$-th coin. The coordinates of all coins are different. No coin is located at $$$(0, 0)$$$. Note Pay attention to the second coin in the example. Monocarp can first move from $$$(0, 0)$$$ to $$$(-1, -1)$$$. Then the coin falls $$$1$$$ down and ends up at $$$(-2, -2)$$$. Finally, Monocarp moves to $$$(-2, -2)$$$ and collects the coin. | 800 | false | false | true | false | false | false | false | false | false | false | 341 |
1861F | The game of Berland poker is played as follows. There are $$$n+1$$$ people: $$$n$$$ players, numbered from $$$1$$$ to $$$n$$$, and the dealer. The dealer has a deck which contains cards of four different suits (the number of cards of each suit is not necessarily the same); the number of cards in the deck is divisible by $$$n$$$. The dealer gives all cards to the players, so that every player receives the same number of cards, and the whole deck is used. After the cards are dealt, every player chooses one of four suits (independently) and discards all cards from their hand which do not belong to their chosen suit. The winner of the game is the player with the maximum number of cards left in their hand. The number of points the winner receives is $$$x - y$$$, where $$$x$$$ is the number of cards in the winner's hand, and $$$y$$$ is the maximum number of cards among all other players; everyone else receives $$$0$$$ points. Note that it means that if there are multiple players with the maximum number of cards, everyone receives $$$0$$$ points. Since every player wants to maximize their odds to win, they will choose a suit with the maximum number of cards in their hand. Monocarp is the dealer. He has already given some cards to the players; the $$$i$$$-th player received $$$a_{i,j}$$$ cards of suit $$$j$$$. Note that the number of cards in players' hands don't have to be the same at this moment. Monocarp has $$$b_1, b_2, b_3, b_4$$$ cards of suit $$$1, 2, 3, 4$$$ respectively left in his deck. He has to give them to the players so that, after all cards are dealt, every player has the same number of cards. For each player, calculate the maximum number of points they can receive among all ways to deal the remaining cards according to the rules of the game. Input The first line of the input contains one integer $$$n$$$ ($$$2 le n le 5 cdot 10^4$$$) — the number of players. Then $$$n$$$ lines follow. The $$$i$$$-th of them contains four integers $$$a_{i,1}, a_{i,2}, a_{i,3}, a_{i,4}$$$ ($$$0 le a_{i,j} le 10^6$$$), where $$$a_{i,j}$$$ is the number of cards of the $$$j$$$-th suit that the $$$i$$$-th player currently has. The last line contains $$$4$$$ integers $$$b_1, b_2, b_3, b_4$$$ ($$$0 le b_j le 10^6$$$), where $$$b_j$$$ is the number of cards of the $$$j$$$-th suit Monocarp has to deal yet. Additional constraint on the input: it is possible to deal all of the remaining cards so that every player has the same number of cards. | 3,200 | false | true | false | false | false | false | false | true | false | false | 1,114 |
708B | Problem - 708B - 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 implementation math *1900 No tag edit access → Contest materials | 1,900 | true | true | true | false | false | true | false | false | false | false | 6,988 |
593B | The teacher gave Anton a large geometry homework, but he didn't do it (as usual) as he participated in a regular round on Codeforces. In the task he was given a set of _n_ lines defined by the equations _y_u2009=u2009_k__i_·_x_u2009+u2009_b__i_. It was necessary to determine whether there is at least one point of intersection of two of these lines, that lays strictly inside the strip between _x_1u2009<u2009_x_2. In other words, is it true that there are 1u2009≤u2009_i_u2009<u2009_j_u2009≤u2009_n_ and _x_',u2009_y_', such that: _y_'u2009=u2009_k__i_u2009*u2009_x_'u2009+u2009_b__i_, that is, point (_x_',u2009_y_') belongs to the line number _i_; _y_'u2009=u2009_k__j_u2009*u2009_x_'u2009+u2009_b__j_, that is, point (_x_',u2009_y_') belongs to the line number _j_; _x_1u2009<u2009_x_'u2009<u2009_x_2, that is, point (_x_',u2009_y_') lies inside the strip bounded by _x_1u2009<u2009_x_2. You can't leave Anton in trouble, can you? Write a program that solves the given task. Input The first line of the input contains an integer _n_ (2u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of lines in the task given to Anton. The second line contains integers _x_1 and _x_2 (u2009-u20091u2009000u2009000u2009≤u2009_x_1u2009<u2009_x_2u2009≤u20091u2009000u2009000) defining the strip inside which you need to find a point of intersection of at least two lines. The following _n_ lines contain integers _k__i_, _b__i_ (u2009-u20091u2009000u2009000u2009≤u2009_k__i_,u2009_b__i_u2009≤u20091u2009000u2009000)xa0— the descriptions of the lines. It is guaranteed that all lines are pairwise distinct, that is, for any two _i_u2009≠u2009_j_ it is true that either _k__i_u2009≠u2009_k__j_, or _b__i_u2009≠u2009_b__j_. Output Print "Yes" (without quotes), if there is at least one intersection of two distinct lines, located strictly inside the strip. Otherwise print "No" (without quotes). Note In the first sample there are intersections located on the border of the strip, but there are no intersections located strictly inside it. | 1,600 | false | false | false | false | false | false | false | false | true | false | 7,476 |
1718A1 | This is the easy version of this problem. The difference between easy and hard versions is only the constraints on $$$a_i$$$ and on $$$n$$$. You can make hacks only if both versions of the problem are solved. Burenka is the crown princess of Buryatia, and soon she will become the $$$n$$$-th queen of the country. There is an ancient tradition in Buryatiaxa0— before the coronation, the ruler must show their strength to the inhabitants. To determine the strength of the $$$n$$$-th ruler, the inhabitants of the country give them an array of $$$a$$$ of exactly $$$n$$$ numbers, after which the ruler must turn all the elements of the array into zeros in the shortest time. The ruler can do the following two-step operation any number of times: select two indices $$$l$$$ and $$$r$$$, so that $$$1 le l le r le n$$$ and a non-negative integer $$$x$$$, then for all $$$l leq i leq r$$$ assign $$$a_i := a_i oplus x$$$, where $$$oplus$$$ denotes the — the number of test cases. The description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$1 le n le 5000$$$) — the size of the array. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$0 le a_i le 5000$$$)xa0— elements of the array. It is guaranteed that the sum of $$$n$$$ in all tests does not exceed $$$5000$$$. Output For each test case, output a single number xa0— the minimum time that Burenka will need. Example Input 7 4 5 5 5 5 3 1 3 2 2 0 0 3 2 5 7 6 1 2 3 3 2 1 10 27 27 34 32 2 31 23 56 52 4 5 1822 1799 57 23 55 Note In the first test case, Burenka can choose segment $$$l = 1$$$, $$$r = 4$$$, and $$$x=5$$$. so it will fill the array with zeros in $$$2$$$ seconds. In the second test case, Burenka first selects segment $$$l = 1$$$, $$$r = 2$$$, and $$$x = 1$$$, after which $$$a = [0, 2, 2]$$$, and then the segment $$$l = 2$$$, $$$r = 3$$$, and $$$x=2$$$, which fills the array with zeros. In total, Burenka will spend $$$2$$$ seconds. | 1,800 | false | true | false | true | false | false | false | false | false | false | 1,975 |
501D | Let's define the sum of two permutations _p_ and _q_ of numbers 0,u20091,u2009...,u2009(_n_u2009-u20091) as permutation , where _Perm_(_x_) is the _x_-th lexicographically permutation of numbers 0,u20091,u2009...,u2009(_n_u2009-u20091) (counting from zero), and _Ord_(_p_) is the number of permutation _p_ in the lexicographical order. For example, _Perm_(0)u2009=u2009(0,u20091,u2009...,u2009_n_u2009-u20092,u2009_n_u2009-u20091), _Perm_(_n_!u2009-u20091)u2009=u2009(_n_u2009-u20091,u2009_n_u2009-u20092,u2009...,u20091,u20090) Misha has two permutations, _p_ and _q_. Your task is to find their sum. Permutation _a_u2009=u2009(_a_0,u2009_a_1,u2009...,u2009_a__n_u2009-u20091) is called to be lexicographically smaller than permutation _b_u2009=u2009(_b_0,u2009_b_1,u2009...,u2009_b__n_u2009-u20091), if for some _k_ following conditions hold: _a_0u2009=u2009_b_0,u2009_a_1u2009=u2009_b_1,u2009...,u2009_a__k_u2009-u20091u2009=u2009_b__k_u2009-u20091,u2009_a__k_u2009<u2009_b__k_. Input The first line contains an integer _n_ (1u2009≤u2009_n_u2009≤u2009200u2009000). The second line contains _n_ distinct integers from 0 to _n_u2009-u20091, separated by a space, forming permutation _p_. The third line contains _n_ distinct integers from 0 to _n_u2009-u20091, separated by spaces, forming permutation _q_. Output Print _n_ distinct integers from 0 to _n_u2009-u20091, forming the sum of the given permutations. Separate the numbers by spaces. Note Permutations of numbers from 0 to 1 in the lexicographical order: (0,u20091),u2009(1,u20090). In the first sample _Ord_(_p_)u2009=u20090 and _Ord_(_q_)u2009=u20090, so the answer is . In the second sample _Ord_(_p_)u2009=u20090 and _Ord_(_q_)u2009=u20091, so the answer is . Permutations of numbers from 0 to 2 in the lexicographical order: (0,u20091,u20092),u2009(0,u20092,u20091),u2009(1,u20090,u20092),u2009(1,u20092,u20090),u2009(2,u20090,u20091),u2009(2,u20091,u20090). In the third sample _Ord_(_p_)u2009=u20093 and _Ord_(_q_)u2009=u20095, so the answer is . | 2,000 | false | false | false | false | true | false | false | false | false | false | 7,827 |
1951E | You are given a string $$$s$$$ consisting of lowercase Latin characters. You need to partition$$$^dagger$$$ this string into some substrings, such that each substring is not a palindrome$$$^ddagger$$$. $$$^dagger$$$ A partition of a string $$$s$$$ is an ordered sequence of some $$$k$$$ strings $$$t_1, t_2, ldots, t_k$$$, such that $$$t_1 + t_2 + ldots + t_k = s$$$, where $$$+$$$ here represents the concatenation operation. $$$^ddagger$$$ A string $$$s$$$ is considered a palindrome if it reads the same backwards as forwards. For example, $$$mathtt{racecar}$$$, $$$mathtt{abccba}$$$, and $$$mathtt{a}$$$ are palindromes, but $$$mathtt{ab}$$$, $$$mathtt{dokibird}$$$, and $$$mathtt{kurosanji}$$$ are not. Input Each test contains multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 10^4$$$)xa0— the number of test cases. Each test case contains a string $$$s$$$ consisting of lowercase Latin characters ($$$1 le s le 10^6$$$). It is guaranteed that the sum of $$$s$$$ over all test cases does not exceed $$$10^6$$$. Output For each test case, print on one line "YES" if there exists a partition of $$$s$$$ whose parts are not palindromes, or "NO" if there is no such partition. If the answer is "YES", on the second line, print an integer $$$k$$$xa0— the number of parts that $$$s$$$ needs to be partitioned to such that each part is not a palindrome. On the third line, print $$$k$$$ strings $$$t_1, t_2, ldots, t_k$$$ representing such a partition. If there are multiple such partitions, print any of them. Example Input 3 sinktheyacht lllllllll uwuowouwu Output YES 1 sinktheyacht NO YES 3 uw uow ouwu Note In the first test case, since $$$mathtt{sinktheyacht}$$$ is already non-palindrome, the partition $$$[mathtt{sinktheyacht}]$$$ is valid. In the second test case, as any substring of the string $$$s$$$ is palindrome, there are no valid partitions. In the third test case, another valid partition is $$$[mathtt{uw},mathtt{uo}, mathtt{wou}, mathtt{wu}]$$$. | 2,000 | true | true | true | false | false | true | true | false | false | false | 570 |
176D | Paul Erdős's prediction came true. Finally an alien force landed on the Earth. In contrary to our expectation they didn't asked the humans to compute the value of a Ramsey number (maybe they had solved it themselves). They asked another question which seemed as hard as calculating Ramsey numbers. Aliens threatened that if humans don't solve this problem in less than 2 hours they will destroy the Earth. Before telling the problem they introduced the concept of Hyper Strings. A Hyper String is made by concatenation of some base strings. Suppose you are given a list of base strings _b_1,u2009_b_2,u2009...,u2009_b__n_. Now the Hyper String made from indices list _i_1,u2009_i_2,u2009...,u2009_i__m_ is concatenation of base strings _b__i_1,u2009_b__i_2,u2009...,u2009_b__i__m_. A Hyper String can be very large and doing operations on it is very costly for computers. The aliens asked humans to compute the length of the longest common sub-sequence of a Hyper String _t_ with a string _s_. Input The first line of input contains the single integer _n_ (1u2009≤u2009_n_u2009≤u20092000) — the number of base strings. The next _n_ lines contains values of base strings. Each base string is made of lowercase Latin letters. A base string cannot be empty string and the sum of lengths of all _n_ base strings doesn't exceed 106. The next line contains the single integer _m_ (1u2009≤u2009_m_u2009≤u20092000) — the number of base strings in the given Hyper String _t_. The next line contains _m_ space-separated integer numbers _i_1,u2009_i_2,u2009...,u2009_i__m_ (1u2009≤u2009_i__j_u2009≤u2009_n_) — the indices of base strings in the Hyper String _t_. The last line contains a non-empty string _s_. String _s_ is made of lowercase Latin letters and its length is no more than 2000 characters. Output Print the length of longest common sub-sequence of Hyper String _t_ and string _s_. If there is no common sub-sequence print 0. Note The length of string _s_ is the number of characters in it. If the length of string _s_ is marked as _s_, then string _s_ can be represented as _s_u2009=u2009_s_1_s_2... _s__s_. A non-empty string _y_u2009=u2009_s_[_p_1_p_2... _p__y_]u2009=u2009_s__p_1_s__p_2... _s__p__y_ (1u2009≤u2009_p_1u2009<u2009_p_2u2009<u2009...u2009<u2009_p__y_u2009≤u2009_s_) is a subsequence of string _s_. For example, "coders" is a subsequence of "codeforces". | 2,500 | false | false | false | true | false | false | false | false | false | false | 9,156 |
808D | Problem - 808D - Codeforces =============== xa0 ]( "52010") so that the sum of all elements in the first part equals to the sum of elements in the second part. It is not always possible, so Vasya will move some element before dividing the array (Vasya will erase some element and insert it into an arbitrary position). Inserting an element in the same position he was erased from is also considered moving. Can Vasya divide the array after choosing the right element to move and its new position? Input The first line contains single integer _n_ (1u2009≤u2009_n_u2009≤u2009100000) — the size of the array. The second line contains _n_ integers _a_1,u2009_a_2... _a__n_ (1u2009≤u2009_a__i_u2009≤u2009109) — the elements of the array. Output Print YES if Vasya can divide the array after moving one element. Otherwise print NO. Examples Input 3 1 3 2 Output YES Input 5 1 2 3 4 5 Output NO Input 5 2 2 3 4 5 Output YES Note In the first example Vasya can move the second element to the end of the array. In the second example no move can make the division possible. In the third example Vasya can move the fourth element by one position to the left. | 1,900 | false | false | true | false | true | false | false | true | false | false | 6,532 |
1943C | You are given a tree with $$$n$$$ vertices numbered $$$1, 2, ldots, n$$$. Initially, all vertices are colored white. You can perform the following two-step operation: 1. Choose a vertex $$$v$$$ ($$$1 leq v leq n$$$) and a distance $$$d$$$ ($$$0 leq d leq n-1$$$). 2. For all vertices $$$u$$$ ($$$1 leq u leq n$$$) such that $$$ ext{dist}^dagger(u,v)=d$$$, color $$$u$$$ black. Construct a sequence of operations to color all the nodes in the tree black using the minimum possible number of operations. It can be proven that it is always possible to do so using at most $$$n$$$ operations. $$$^dagger$$$ $$$ ext{dist}(x, y)$$$ denotes the number of edges on the (unique) simple path between vertices $$$x$$$ and $$$y$$$ on the tree. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 leq t leq 200$$$)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 le n le 2 cdot 10^3$$$)xa0— the number of vertices of the tree. The following $$$n - 1$$$ lines of each test case describe the edges of the tree. The $$$i$$$-th of these lines contains two integers $$$u_i$$$ and $$$v_i$$$ ($$$1 le u_i, v_i le n$$$, $$$u_i eq v_i$$$), the indices of the vertices connected by the $$$i$$$-th edge. It is guaranteed that the given edges form a tree. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^3$$$. Output For each test case, first output a single integer $$$op$$$xa0$$$(1 le op le n)$$$, the minimum number of operations needed to color all vertices of the tree black. Then, output $$$op$$$ lines, each containing $$$2$$$ integers. The $$$i$$$-th line should contain the values of $$$v$$$ and $$$d$$$ chosen for the $$$i$$$-th operation ($$$1 le v le n$$$, $$$0 le d le n - 1$$$) You must guarantee that at the end of $$$op$$$ operations, all vertices are colored black. If there are multiple solutions, you may output any one of them. Example Input 4 1 2 1 2 4 1 2 1 3 1 4 7 2 7 3 2 6 4 5 7 1 6 6 7 Output 1 1 0 2 1 1 2 1 2 1 1 2 1 3 6 1 7 1 2 1 Note In the first test case, there is only one possible operation, and performing it gives us a valid answer. In the second test case, the first operation colors vertex $$$2$$$ black, and the second operation colors vertex $$$1$$$ black. It can be shown that it is impossible to color both vertices black in one operation, so the minimum number of operations needed is $$$2$$$. Another possible solution is to use the $$$2$$$ operations: $$$(u, r) = (1, 0)$$$ and $$$(u, r) = (2, 0)$$$. In the third test case, the first operation colors vertices $$$2$$$, $$$3$$$ and $$$4$$$ black, and the second operation colors vertex $$$1$$$ black. Again, it can be shown that it is impossible to color all vertices black in $$$1$$$ operation, so the minimum number of operations needed is $$$2$$$. In the fourth test case, the first operation colors vertices $$$4$$$, $$$1$$$ and $$$7$$$ black, the second operation colors vertices $$$2$$$, $$$5$$$ and $$$6$$$ black while the third operation colors vertices $$$3$$$ and $$$7$$$ black. Notice that it is allowed to color vertex $$$7$$$ black twice. Thus, each node was marked at least once, with node $$$7$$$ marked twice. It can be shown that it is impossible to color all vertices black in fewer than $$$3$$$ moves. | 2,300 | false | true | false | false | false | true | false | false | false | false | 621 |
19C | Problem - 19C - Codeforces =============== xa0 — length of the string. The following line contains _n_ space-separated integer numbers from 0 to 109 inclusive — numbers that stand for the letters of the string. It's guaranteed that each letter can be met in the string at most 10 times. Output In the first line output the length of the string's part, left after Bob's deletions. In the second line output all the letters (separated by a space) of the string, left after Bob deleted all the repeats in the described way. Examples Input 6 1 2 3 1 2 3 Output 3 1 2 3 Input 7 4 5 6 5 6 7 7 Output 1 7 | 2,200 | false | true | false | false | false | false | false | false | false | false | 9,898 |
1746E1 | The only difference between this problem and the hard version is the maximum number of questions. This is an interactive problem. There is a hidden integer $$$1 le x le n$$$ which you have to find. In order to find it you can ask at most $$$mathbf{82}$$$ questions. In each question you can choose a non-empty integer set $$$S$$$ and ask if $$$x$$$ belongs to $$$S$$$ or not, after each question, if $$$x$$$ belongs to $$$S$$$, you'll receive "YES", otherwise "NO". But the problem is that not all answers are necessarily true (some of them are joking), it's just guaranteed that for each two consecutive questions, at least one of them is answered correctly. Additionally to the questions, you can make at most $$$2$$$ guesses for the answer $$$x$$$. Each time you make a guess, if you guess $$$x$$$ correctly, you receive ":)" and your program should terminate, otherwise you'll receive ":(". As a part of the joking, we will not fix the value of $$$x$$$ in the beginning. Instead, it can change throughout the interaction as long as all the previous responses are valid as described above. Note that your answer guesses are always answered correctly. If you ask a question before and after a guess, at least one of these two questions is answered correctly, as normal. Input The only line of the input contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$), the maximum possible value of $$$x$$$. Interaction For each question, if you want to ask about a set $$$S$$$, first print the character '?', then print the size of $$$S$$$ and then print the elements of $$$S$$$ one by one. Each element should be an integer between $$$1$$$ and $$$n$$$, the elements must be distinct. After each question, read a string "YES" or "NO", as explained in the statement. You can make at most $$$82$$$ such questions. If you want to guess for $$$x$$$, first print the character '!' and then print your guess. After each guess, read ":)" or ":(". If you guess $$$x$$$ correctly, the answer is ":)" and your program should terminate immediately, otherwise you'll receive ":(". You can make at most $$$2$$$ such guesses. After printing a query do not forget to output end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use: fflush(stdout) or cout.flush() in C++; System.out.flush() in Java; flush(output) in Pascal; stdout.flush() in Python; see documentation for other languages. Hacking is not allowed in this problem. Example Output ? 5 1 2 5 4 3! 6 ? 4 1 2 3 4 ! 5 Note If the answer of the first question were correct, then $$$x$$$ would have been equal to $$$6$$$, but as we can see in the first guess, $$$6$$$ is not the answer. So the answer of the first question is joking. As we know, the answer of at least one of our two questions is correct, since the answer of the first question was joking, the answer of the second question should be correct. So we will understand that $$$x$$$ is not equal to $$$1, 2, 3$$$ or $$$4$$$, and we also knew that $$$x$$$ is not equal to $$$6$$$ either. Hence $$$x$$$ should be equal to $$$5$$$. | 2,500 | false | false | false | false | false | true | false | true | false | false | 1,806 |
1495B | On a weekend, Qingshan suggests that she and her friend Daniel go hiking. Unfortunately, they are busy high school students, so they can only go hiking on scratch paper. A permutation $$$p$$$ is written from left to right on the paper. First Qingshan chooses an integer index $$$x$$$ ($$$1le xle n$$$) and tells it to Daniel. After that, Daniel chooses another integer index $$$y$$$ ($$$1le yle n$$$, $$$y e x$$$). The game progresses turn by turn and as usual, Qingshan moves first. The rules follow: If it is Qingshan's turn, Qingshan must change $$$x$$$ to such an index $$$x'$$$ that $$$1le x'le n$$$, $$$x'-x=1$$$, $$$x' e y$$$, and $$$p_{x'}<p_x$$$ at the same time. If it is Daniel's turn, Daniel must change $$$y$$$ to such an index $$$y'$$$ that $$$1le y'le n$$$, $$$y'-y=1$$$, $$$y' e x$$$, and $$$p_{y'}>p_y$$$ at the same time. The person who can't make her or his move loses, and the other wins. You, as Qingshan's fan, are asked to calculate the number of possible $$$x$$$ to make Qingshan win in the case both players play optimally. Input The first line contains a single integer $$$n$$$ ($$$2le nle 10^5$$$)xa0— the length of the permutation. The second line contains $$$n$$$ distinct integers $$$p_1,p_2,dots,p_n$$$ ($$$1le p_ile n$$$)xa0— the permutation. Output Print the number of possible values of $$$x$$$ that Qingshan can choose to make her win. Note In the first test case, Qingshan can only choose $$$x=3$$$ to win, so the answer is $$$1$$$. In the second test case, if Qingshan will choose $$$x=4$$$, Daniel can choose $$$y=1$$$. In the first turn (Qingshan's) Qingshan chooses $$$x'=3$$$ and changes $$$x$$$ to $$$3$$$. In the second turn (Daniel's) Daniel chooses $$$y'=2$$$ and changes $$$y$$$ to $$$2$$$. Qingshan can't choose $$$x'=2$$$ because $$$y=2$$$ at this time. Then Qingshan loses. | 1,900 | false | true | false | false | false | false | false | false | false | false | 3,195 |
899A | There were _n_ groups of students which came to write a training contest. A group is either one person who can write the contest with anyone else, or two people who want to write the contest in the same team. The coach decided to form teams of exactly three people for this training. Determine the maximum number of teams of three people he can form. It is possible that he can't use all groups to form teams. For groups of two, either both students should write the contest, or both should not. If two students from a group of two will write the contest, they should be in the same team. Input The first line contains single integer _n_ (2u2009≤u2009_n_u2009≤u20092·105) — the number of groups. The second line contains a sequence of integers _a_1,u2009_a_2,u2009...,u2009_a__n_ (1u2009≤u2009_a__i_u2009≤u20092), where _a__i_ is the number of people in group _i_. Output Print the maximum number of teams of three people the coach can form. Note In the first example the coach can form one team. For example, he can take students from the first, second and fourth groups. In the second example he can't make a single team. In the third example the coach can form three teams. For example, he can do this in the following way: The first group (of two people) and the seventh group (of one person), The second group (of two people) and the sixth group (of one person), The third group (of two people) and the fourth group (of one person). | 800 | true | true | false | false | false | true | false | false | false | false | 6,152 |
1965F | You have been asked to organize a very important art conference. The first step is to choose the dates. The conference must last for a certain number of consecutive days. Each day, one lecturer must perform, and the same lecturer cannot perform more than once. You asked $$$n$$$ potential lecturers if they could participate in the conference. Lecturer $$$i$$$ indicated that they could perform on any day from $$$l_i$$$ to $$$r_i$$$ inclusive. A certain segment of days can be chosen as the conference dates if there is a way to assign an available lecturer to each day of the segment, assigning each lecturer to no more than one day. For each $$$k$$$ from $$$1$$$ to $$$n$$$, find how many ways there are to choose a segment of $$$k$$$ consecutive days as the conference dates. Input The first line of input contains one integer $$$n$$$xa0— the number of potential lecturers ($$$1 le n le 2 cdot 10^5$$$). Each of the next $$$n$$$ lines contains two integers $$$l_i$$$ and $$$r_i$$$xa0— the segment of available days for the $$$i$$$th lecturer ($$$1 le l_i le r_i le 2 cdot 10^5$$$). Output Print $$$n$$$ integers, where the $$$k$$$th number denotes the number of ways to select a segment of $$$k$$$ consecutive days as conference dates. Note In the first testcase, a one-day conference can be organized on any of the days from $$$1$$$ to $$$6$$$. A two-day conference can be organized from day $$$2$$$ to day $$$3$$$, as well as from day $$$4$$$ to day $$$5$$$. In the second testcase, five lecturers can perform only from day $$$1$$$ to day $$$3$$$, so it will not be possible to organize a conference longer than three days. | 3,300 | false | false | false | false | true | false | false | false | false | false | 511 |
1187B | The letters shop showcase is a string $$$s$$$, consisting of $$$n$$$ lowercase Latin letters. As the name tells, letters are sold in the shop. Letters are sold one by one from the leftmost to the rightmost. Any customer can only buy some prefix of letters from the string $$$s$$$. There are $$$m$$$ friends, the $$$i$$$-th of them is named $$$t_i$$$. Each of them is planning to estimate the following value: how many letters (the length of the shortest prefix) would s/he need to buy if s/he wanted to construct her/his name of bought letters. The name can be constructed if each letter is presented in the equal or greater amount. For example, for $$$s$$$="arrayhead" and $$$t_i$$$="arya" $$$5$$$ letters have to be bought ("arrayhead"). For example, for $$$s$$$="arrayhead" and $$$t_i$$$="harry" $$$6$$$ letters have to be bought ("arrayhead"). For example, for $$$s$$$="arrayhead" and $$$t_i$$$="ray" $$$5$$$ letters have to be bought ("arrayhead"). For example, for $$$s$$$="arrayhead" and $$$t_i$$$="r" $$$2$$$ letters have to be bought ("arrayhead"). For example, for $$$s$$$="arrayhead" and $$$t_i$$$="areahydra" all $$$9$$$ letters have to be bought ("arrayhead"). It is guaranteed that every friend can construct her/his name using the letters from the string $$$s$$$. Note that the values for friends are independent, friends are only estimating them but not actually buying the letters. Input The first line contains one integer $$$n$$$ ($$$1 le n le 2 cdot 10^5$$$) — the length of showcase string $$$s$$$. The second line contains string $$$s$$$, consisting of exactly $$$n$$$ lowercase Latin letters. The third line contains one integer $$$m$$$ ($$$1 le m le 5 cdot 10^4$$$) — the number of friends. The $$$i$$$-th of the next $$$m$$$ lines contains $$$t_i$$$ ($$$1 le t_i le 2 cdot 10^5$$$) — the name of the $$$i$$$-th friend. It is guaranteed that $$$sum limits_{i=1}^m t_i le 2 cdot 10^5$$$. Output For each friend print the length of the shortest prefix of letters from $$$s$$$ s/he would need to buy to be able to construct her/his name of them. The name can be constructed if each letter is presented in the equal or greater amount. It is guaranteed that every friend can construct her/his name using the letters from the string $$$s$$$. Example Input 9 arrayhead 5 arya harry ray r areahydra | 1,300 | false | false | true | false | false | false | false | true | false | false | 4,765 |
1284F | Donghyun's new social network service (SNS) contains $$$n$$$ users numbered $$$1, 2, ldots, n$$$. Internally, their network is a tree graph, so there are $$$n-1$$$ direct connections between each user. Each user can reach every other users by using some sequence of direct connections. From now on, we will denote this primary network as $$$T_1$$$. To prevent a possible server breakdown, Donghyun created a backup network $$$T_2$$$, which also connects the same $$$n$$$ users via a tree graph. If a system breaks down, exactly one edge $$$e in T_1$$$ becomes unusable. In this case, Donghyun will protect the edge $$$e$$$ by picking another edge $$$f in T_2$$$, and add it to the existing network. This new edge should make the network be connected again. Donghyun wants to assign a replacement edge $$$f in T_2$$$ for as many edges $$$e in T_1$$$ as possible. However, since the backup network $$$T_2$$$ is fragile, $$$f in T_2$$$ can be assigned as the replacement edge for at most one edge in $$$T_1$$$. With this restriction, Donghyun wants to protect as many edges in $$$T_1$$$ as possible. Formally, let $$$E(T)$$$ be an edge set of the tree $$$T$$$. We consider a bipartite graph with two parts $$$E(T_1)$$$ and $$$E(T_2)$$$. For $$$e in E(T_1), f in E(T_2)$$$, there is an edge connecting $$${e, f}$$$ if and only if graph $$$T_1 - {e} + {f}$$$ is a tree. You should find a maximum matching in this bipartite graph. Input The first line contains an integer $$$n$$$ ($$$2 le n le 250,000$$$), the number of users. In the next $$$n-1$$$ lines, two integers $$$a_i$$$, $$$b_i$$$ ($$$1 le a_i, b_i le n$$$) are given. Those two numbers denote the indices of the vertices connected by the corresponding edge in $$$T_1$$$. In the next $$$n-1$$$ lines, two integers $$$c_i$$$, $$$d_i$$$ ($$$1 le c_i, d_i le n$$$) are given. Those two numbers denote the indices of the vertices connected by the corresponding edge in $$$T_2$$$. It is guaranteed that both edge sets form a tree of size $$$n$$$. Output In the first line, print the number $$$m$$$ ($$$0 leq m < n$$$), the maximum number of edges that can be protected. In the next $$$m$$$ lines, print four integers $$$a_i, b_i, c_i, d_i$$$. Those four numbers denote that the edge $$$(a_i, b_i)$$$ in $$$T_1$$$ is will be replaced with an edge $$$(c_i, d_i)$$$ in $$$T_2$$$. All printed edges should belong to their respective network, and they should link to distinct edges in their respective network. If one removes an edge $$$(a_i, b_i)$$$ from $$$T_1$$$ and adds edge $$$(c_i, d_i)$$$ from $$$T_2$$$, the network should remain connected. The order of printing the edges or the order of vertices in each edge does not matter. If there are several solutions, you can print any. Examples Input 4 1 2 2 3 4 3 1 3 2 4 1 4 Output 3 3 2 4 2 2 1 1 3 4 3 1 4 Input 5 1 2 2 4 3 4 4 5 1 2 1 3 1 4 1 5 Output 4 2 1 1 2 3 4 1 3 4 2 1 4 5 4 1 5 Input 9 7 9 2 8 2 1 7 5 4 7 2 4 9 6 3 9 1 8 4 8 2 9 9 5 7 6 1 3 4 6 5 3 Output 8 4 2 9 2 9 7 6 7 5 7 5 9 6 9 4 6 8 2 8 4 3 9 3 5 2 1 1 8 7 4 1 3 | 3,200 | true | false | false | false | true | false | false | false | false | true | 4,269 |
1644A | The knight is standing in front of a long and narrow hallway. A princess is waiting at the end of it. In a hallway there are three doors: a red door, a green door and a blue door. The doors are placed one after another, however, possibly in a different order. To proceed to the next door, the knight must first open the door before. Each door can be only opened with a key of the corresponding color. So three keys: a red key, a green key and a blue keyxa0— are also placed somewhere in the hallway. To open the door, the knight should first pick up the key of its color. The knight has a map of the hallway. It can be transcribed as a string, consisting of six characters: R, G, Bxa0— denoting red, green and blue doors, respectively; r, g, bxa0— denoting red, green and blue keys, respectively. Each of these six characters appears in the string exactly once. The knight is standing at the beginning of the hallwayxa0— on the left on the map. Given a map of the hallway, determine if the knight can open all doors and meet the princess at the end of the hallway. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 720$$$)xa0— the number of testcases. Each testcase consists of a single string. Each character is one of R, G, B (for the doors), r, g, b (for the keys), and each of them appears exactly once. Output For each testcase, print YES if the knight can open all doors. Otherwise, print NO. Example Input 4 rgbBRG RgbrBG bBrRgG rgRGBb Note In the first testcase, the knight first collects all keys, then opens all doors with them. In the second testcase, there is a red door right in front of the knight, but he doesn't have a key for it. In the third testcase, the key to each door is in front of each respective door, so the knight collects the key and uses it immediately three times. In the fourth testcase, the knight can't open the blue door. | 800 | false | false | true | false | false | false | false | false | false | false | 2,401 |
1348D | Phoenix has decided to become a scientist! He is currently investigating the growth of bacteria. Initially, on day $$$1$$$, there is one bacterium with mass $$$1$$$. Every day, some number of bacteria will split (possibly zero or all). When a bacterium of mass $$$m$$$ splits, it becomes two bacteria of mass $$$frac{m}{2}$$$ each. For example, a bacterium of mass $$$3$$$ can split into two bacteria of mass $$$1.5$$$. Also, every night, the mass of every bacteria will increase by one. Phoenix is wondering if it is possible for the total mass of all the bacteria to be exactly $$$n$$$. If it is possible, he is interested in the way to obtain that mass using the minimum possible number of nights. Help him become the best scientist! Input The input consists of multiple test cases. The first line contains an integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first line of each test case contains an integer $$$n$$$ ($$$2 le n le 10^9$$$)xa0— the sum of bacteria masses that Phoenix is interested in. Output For each test case, if there is no way for the bacteria to exactly achieve total mass $$$n$$$, print -1. Otherwise, print two lines. The first line should contain an integer $$$d$$$ xa0— the minimum number of nights needed. The next line should contain $$$d$$$ integers, with the $$$i$$$-th integer representing the number of bacteria that should split on the $$$i$$$-th day. If there are multiple solutions, print any. Example Output 3 1 0 2 3 1 1 2 1 0 Note In the first test case, the following process results in bacteria with total mass $$$9$$$: Day $$$1$$$: The bacterium with mass $$$1$$$ splits. There are now two bacteria with mass $$$0.5$$$ each. Night $$$1$$$: All bacteria's mass increases by one. There are now two bacteria with mass $$$1.5$$$. Day $$$2$$$: None split. Night $$$2$$$: There are now two bacteria with mass $$$2.5$$$. Day $$$3$$$: Both bacteria split. There are now four bacteria with mass $$$1.25$$$. Night $$$3$$$: There are now four bacteria with mass $$$2.25$$$. The total mass is $$$2.25+2.25+2.25+2.25=9$$$. It can be proved that $$$3$$$ is the minimum number of nights needed. There are also other ways to obtain total mass 9 in 3 nights. $$$ $$$ In the second test case, the following process results in bacteria with total mass $$$11$$$: Day $$$1$$$: The bacterium with mass $$$1$$$ splits. There are now two bacteria with mass $$$0.5$$$. Night $$$1$$$: There are now two bacteria with mass $$$1.5$$$. Day $$$2$$$: One bacterium splits. There are now three bacteria with masses $$$0.75$$$, $$$0.75$$$, and $$$1.5$$$. Night $$$2$$$: There are now three bacteria with masses $$$1.75$$$, $$$1.75$$$, and $$$2.5$$$. Day $$$3$$$: The bacteria with mass $$$1.75$$$ and the bacteria with mass $$$2.5$$$ split. There are now five bacteria with masses $$$0.875$$$, $$$0.875$$$, $$$1.25$$$, $$$1.25$$$, and $$$1.75$$$. Night $$$3$$$: There are now five bacteria with masses $$$1.875$$$, $$$1.875$$$, $$$2.25$$$, $$$2.25$$$, and $$$2.75$$$. The total mass is $$$1.875+1.875+2.25+2.25+2.75=11$$$. It can be proved that $$$3$$$ is the minimum number of nights needed. There are also other ways to obtain total mass 11 in 3 nights. $$$ $$$ In the third test case, the bacterium does not split on day $$$1$$$, and then grows to mass $$$2$$$ during night $$$1$$$. | 1,900 | true | true | true | false | false | true | false | true | false | false | 3,967 |
1183E | The only difference between the easy and the hard versions is constraints. A subsequence is a string that can be derived from another string by deleting some or no symbols without changing the order of the remaining symbols. Characters to be deleted are not required to go successively, there can be any gaps between them. For example, for the string "abaca" the following strings are subsequences: "abaca", "aba", "aaa", "a" and "" (empty string). But the following strings are not subsequences: "aabaca", "cb" and "bcaa". You are given a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. In one move you can take any subsequence $$$t$$$ of the given string and add it to the set $$$S$$$. The set $$$S$$$ can't contain duplicates. This move costs $$$n - t$$$, where $$$t$$$ is the length of the added subsequence (i.e. the price equals to the number of the deleted characters). Your task is to find out the minimum possible total cost to obtain a set $$$S$$$ of size $$$k$$$ or report that it is impossible to do so. Input The first line of the input contains two integers $$$n$$$ and $$$k$$$ ($$$1 le n, k le 100$$$) — the length of the string and the size of the set, correspondingly. The second line of the input contains a string $$$s$$$ consisting of $$$n$$$ lowercase Latin letters. Output Print one integer — if it is impossible to obtain the set $$$S$$$ of size $$$k$$$, print -1. Otherwise, print the minimum possible total cost to do it. Note In the first example we can generate $$$S$$$ = { "asdf", "asd", "adf", "asf", "sdf" }. The cost of the first element in $$$S$$$ is $$$0$$$ and the cost of the others is $$$1$$$. So the total cost of $$$S$$$ is $$$4$$$. | 2,000 | false | false | true | true | false | false | false | false | false | true | 4,798 |
1368F | # Lamps on a Circle Input file: standard input Output file: standard output Time limit: 2 seconds Memory limit: 512 megabytes This is an interactive problem. John and his imaginary friend play a game. There are n lamps arranged in a circle. Lamps are numbered 1 through n in clockwise order, that is, lamps i and i + 1 are adjacent for any i = 1 , . . . , n − 1, and also lamps n and 1 are adjacent. Initially all lamps are turned off. John and his friend take turns, with John moving first. On his turn John can choose to terminate the game, or to make a move. To make a move, John can choose any positive number k and turn any k lamps of his choosing on. In response to this move, John’s friend will choose k consecutive lamps and turn all of them off (the lamps in the range that were off before this move stay off). Note that the value of k is the same as John’s number on his last move. For example, if n = 5 and John have just turned three lamps on, John’s friend may choose to turn off lamps 1, 2, 3, or 2, 3, 4, or 3, 4, 5, or 4, 5, 1, or 5, 1, 2.After this, John may choose to terminate or move again, and so on. However, John can not make more than 10 4 moves. John wants to maximize the number of lamps turned on at the end of the game, while his friend wants to minimize this number. Your task is to provide a strategy for John to achieve optimal result. Your program will play interactively for John against the jury’s interactor program playing for John’s friend. Suppose there are n lamps in the game. Let R(n) be the number of turned on lamps at the end of the game if both players act optimally. Your program has to terminate the game with at least R(n) turned on lamps within 10 4 moves. Refer to Interaction section below for interaction details. For technical reasons hacks for this problem are disabled. # Interaction Protocol Initially your program will be fed a single integer n (1 ≤ n ≤ 1000 ) x16 the number of lamps in the game. Then the interactor will wait for your actions. To make a move, print a line starting with an integer k (1 ≤ k ≤ n), followed by k distinct integers l1, . . . , l k (1 ≤ li ≤ n) x16 indices of lamps you want to turn on. The indices may be printed in any order. It is allowed to try to turn on a lamp that is already on (although this will have no effect). If your move was invalid for any reason, or if you have made more than 10 4 moves, the interactor will reply with a line containing a single integer −1. Otherwise, the reply will be a line containing a single integer x (1 ≤ x ≤ n), meaning that the response was to turn off k consecutive lamps starting from x in clockwise order. To terminate the game instead of making a move, print a line containing a single integer 0. The test will be passed if at this point there are at least R(n) lamps turned on (note that neither R(n), nor the verdict received are not communicated to your program in any way). This action does not count towards the number of moves (that is, it is legal to terminate the game after exactly 10 4 moves). To receive the correct verdict, your program should terminate immediately after printing 0, or after receiving −1 as a response. Don’t forget to flush your output after every action. Page 1 of 2 Examples standard input standard output 30412 1 3 0 # Note When n = 3 , any John’s move can be reversed, thus R(3) = 0 , and terminating the game immediately is correct. R(4) = 1 , and one strategy to achieve this result is shown in the second sample case. Blank lines in sample interactions are for clarity and should not be printed. Page 2 of 2 | 2,600 | true | false | true | false | false | false | false | false | false | false | 3,831 |
961A | You are given a following process. There is a platform with $$$n$$$ columns. $$$1 imes 1$$$ squares are appearing one after another in some columns on this platform. If there are no squares in the column, a square will occupy the bottom row. Otherwise a square will appear at the top of the highest square of this column. When all of the $$$n$$$ columns have at least one square in them, the bottom row is being removed. You will receive $$$1$$$ point for this, and all the squares left will fall down one row. You task is to calculate the amount of points you will receive. Input The first line of input contain 2 integer numbers $$$n$$$ and $$$m$$$ ($$$1 le n, m le 1000$$$) — the length of the platform and the number of the squares. The next line contain $$$m$$$ integer numbers $$$c_1, c_2, dots, c_m$$$ ($$$1 le c_i le n$$$) — column in which $$$i$$$-th square will appear. Output Print one integer — the amount of points you will receive. Example Input 3 9 1 1 2 2 2 3 1 2 3 Note In the sample case the answer will be equal to $$$2$$$ because after the appearing of $$$6$$$-th square will be removed one row (counts of the squares on the platform will look like $$$[2~ 3~ 1]$$$, and after removing one row will be $$$[1~ 2~ 0]$$$). After the appearing of $$$9$$$-th square counts will be $$$[2~ 3~ 1]$$$, and after removing one row it will look like $$$[1~ 2~ 0]$$$. So the answer will be equal to $$$2$$$. | 900 | false | false | true | false | false | false | false | false | false | false | 5,863 |
1709F | You are given three integers $$$n$$$, $$$k$$$ and $$$f$$$. Consider all binary strings (i.u2009e. all strings consisting of characters $$$0$$$ and/or $$$1$$$) of length from $$$1$$$ to $$$n$$$. For every such string $$$s$$$, you need to choose an integer $$$c_s$$$ from $$$0$$$ to $$$k$$$. A multiset of binary strings of length exactly $$$n$$$ is considered beautiful if for every binary string $$$s$$$ with length from $$$1$$$ to $$$n$$$, the number of strings in the multiset such that $$$s$$$ is their prefix is not exceeding $$$c_s$$$. For example, let $$$n = 2$$$, $$$c_{0} = 3$$$, $$$c_{00} = 1$$$, $$$c_{01} = 2$$$, $$$c_{1} = 1$$$, $$$c_{10} = 2$$$, and $$$c_{11} = 3$$$. The multiset of strings $$${11, 01, 00, 01}$$$ is beautiful, since: for the string $$$0$$$, there are $$$3$$$ strings in the multiset such that $$$0$$$ is their prefix, and $$$3 le c_0$$$; for the string $$$00$$$, there is one string in the multiset such that $$$00$$$ is its prefix, and $$$1 le c_{00}$$$; for the string $$$01$$$, there are $$$2$$$ strings in the multiset such that $$$01$$$ is their prefix, and $$$2 le c_{01}$$$; for the string $$$1$$$, there is one string in the multiset such that $$$1$$$ is its prefix, and $$$1 le c_1$$$; for the string $$$10$$$, there are $$$0$$$ strings in the multiset such that $$$10$$$ is their prefix, and $$$0 le c_{10}$$$; for the string $$$11$$$, there is one string in the multiset such that $$$11$$$ is its prefix, and $$$1 le c_{11}$$$. Now, for the problem itself. You have to calculate the number of ways to choose the integer $$$c_s$$$ for every binary string $$$s$$$ of length from $$$1$$$ to $$$n$$$ in such a way that the maximum possible size of a beautiful multiset is exactly $$$f$$$. Input The only line of input contains three integers $$$n$$$, $$$k$$$ and $$$f$$$ ($$$1 le n le 15$$$; $$$1 le k, f le 2 cdot 10^5$$$). Output Print one integer — the number of ways to choose the integer $$$c_s$$$ for every binary string $$$s$$$ of length from $$$1$$$ to $$$n$$$ in such a way that the maximum possible size of a beautiful multiset is exactly $$$f$$$. Since it can be huge, print it modulo $$$998244353$$$. | 2,500 | true | false | false | true | false | false | true | false | false | true | 2,021 |
965A | Problem - 965A - Codeforces =============== xa0 xa0— the number of people, the number of airplanes each should make, the number of airplanes that can be made using one sheet and the number of sheets in one pack, respectively. Output Print a single integerxa0— the minimum number of packs they should buy. Examples Input 5 3 2 3 Output 4 Input 5 3 100 1 Output 5 Note In the first sample they have to buy $$$4$$$ packs of paper: there will be $$$12$$$ sheets in total, and giving $$$2$$$ sheets to each person is enough to suit everyone's needs. In the second sample they have to buy a pack for each person as they can't share sheets. | 800 | true | false | false | false | false | false | false | false | false | false | 5,842 |
839C | There are _n_ cities and _n_u2009-u20091 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads. Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities. Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link [ first line contains a single integer _n_ (1u2009≤u2009_n_u2009≤u2009100000)xa0— number of cities. Then _n_u2009-u20091 lines follow. The _i_-th line of these lines contains two integers _u__i_ and _v__i_ (1u2009≤u2009_u__i_,u2009_v__i_u2009≤u2009_n_, _u__i_u2009≠u2009_v__i_)xa0— the cities connected by the _i_-th road. It is guaranteed that one can reach any city from any other by the roads. Output Print a numberxa0— the expected length of their journey. The journey starts in the city 1. Your answer will be considered correct if its absolute or relative error does not exceed 10u2009-u20096. Namely: let's assume that your answer is _a_, and the answer of the jury is _b_. The checker program will consider your answer correct, if . Note In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5. In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2. | 1,500 | false | false | false | true | false | false | false | false | false | true | 6,406 |
1511C | You have a card deck of $$$n$$$ cards, numbered from top to bottom, i.xa0e. the top card has index $$$1$$$ and bottom cardxa0— index $$$n$$$. Each card has its color: the $$$i$$$-th card has color $$$a_i$$$. You should process $$$q$$$ queries. The $$$j$$$-th query is described by integer $$$t_j$$$. For each query you should: find the highest card in the deck with color $$$t_j$$$, i.xa0e. the card with minimum index; print the position of the card you found; take the card and place it on top of the deck. Input The first line contains two integers $$$n$$$ and $$$q$$$ ($$$2 le n le 3 cdot 10^5$$$; $$$1 le q le 3 cdot 10^5$$$)xa0— the number of cards in the deck and the number of queries. The second line contains $$$n$$$ integers $$$a_1, a_2, dots, a_n$$$ ($$$1 le a_i le 50$$$)xa0— the colors of cards. The third line contains $$$q$$$ integers $$$t_1, t_2, dots, t_q$$$ ($$$1 le t_j le 50$$$)xa0— the query colors. It's guaranteed that queries ask only colors that are present in the deck. Output Print $$$q$$$ integersxa0— the answers for each query. Example Input 7 5 2 1 1 4 3 3 1 3 2 1 1 4 Note Description of the sample: 1. the deck is $$$[2, 1, 1, 4, underline{3}, 3, 1]$$$ and the first card with color $$$t_1 = 3$$$ has position $$$5$$$; 2. the deck is $$$[3, underline{2}, 1, 1, 4, 3, 1]$$$ and the first card with color $$$t_2 = 2$$$ has position $$$2$$$; 3. the deck is $$$[2, 3, underline{1}, 1, 4, 3, 1]$$$ and the first card with color $$$t_3 = 1$$$ has position $$$3$$$; 4. the deck is $$$[underline{1}, 2, 3, 1, 4, 3, 1]$$$ and the first card with color $$$t_4 = 1$$$ has position $$$1$$$; 5. the deck is $$$[1, 2, 3, 1, underline{4}, 3, 1]$$$ and the first card with color $$$t_5 = 4$$$ has position $$$5$$$. | 1,100 | false | false | true | false | true | false | true | false | false | false | 3,114 |
1469E | Let's call two strings $$$a$$$ and $$$b$$$ (both of length $$$k$$$) a bit similar if they have the same character in some position, i.xa0e. there exists at least one $$$i in [1, k]$$$ such that $$$a_i = b_i$$$. You are given a binary string $$$s$$$ of length $$$n$$$ (a string of $$$n$$$ characters 0 and/or 1) and an integer $$$k$$$. Let's denote the string $$$s[i..j]$$$ as the substring of $$$s$$$ starting from the $$$i$$$-th character and ending with the $$$j$$$-th character (that is, $$$s[i..j] = s_i s_{i + 1} s_{i + 2} dots s_{j - 1} s_j$$$). Let's call a binary string $$$t$$$ of length $$$k$$$ beautiful if it is a bit similar to all substrings of $$$s$$$ having length exactly $$$k$$$; that is, it is a bit similar to $$$s[1..k], s[2..k+1], dots, s[n-k+1..n]$$$. Your goal is to find the lexicographically smallest string $$$t$$$ that is beautiful, or report that no such string exists. String $$$x$$$ is lexicographically less than string $$$y$$$ if either $$$x$$$ is a prefix of $$$y$$$ (and $$$x e y$$$), or there exists such $$$i$$$ ($$$1 le i le min(x, y)$$$), that $$$x_i < y_i$$$, and for any $$$j$$$ ($$$1 le j < i$$$) $$$x_j = y_j$$$. Input The first line contains one integer $$$q$$$ ($$$1 le q le 10000$$$) — the number of test cases. Each test case consists of two lines. The first line of each test case contains two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n le 10^6$$$). The second line contains the string $$$s$$$, consisting of $$$n$$$ characters (each character is either 0 or 1). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^6$$$. | 2,400 | false | false | false | false | false | false | true | false | false | false | 3,334 |
923B | Alice likes snow a lot! Unfortunately, this year's winter is already over, and she can't expect to have any more of it. Bob has thus bought her a giftxa0— a large snow maker. He plans to make some amount of snow every day. On day _i_ he will make a pile of snow of volume _V__i_ and put it in her garden. Each day, every pile will shrink a little due to melting. More precisely, when the temperature on a given day is _T__i_, each pile will reduce its volume by _T__i_. If this would reduce the volume of a pile to or below zero, it disappears forever. All snow piles are independent of each other. Note that the pile made on day _i_ already loses part of its volume on the same day. In an extreme case, this may mean that there are no piles left at the end of a particular day. You are given the initial pile sizes and the temperature on each day. Determine the total volume of snow melted on each day. Input The first line contains a single integer _N_ (1u2009≤u2009_N_u2009≤u2009105)xa0— the number of days. The second line contains _N_ integers _V_1,u2009_V_2,u2009...,u2009_V__N_ (0u2009≤u2009_V__i_u2009≤u2009109), where _V__i_ is the initial size of a snow pile made on the day _i_. The third line contains _N_ integers _T_1,u2009_T_2,u2009...,u2009_T__N_ (0u2009≤u2009_T__i_u2009≤u2009109), where _T__i_ is the temperature on the day _i_. Output Output a single line with _N_ integers, where the _i_-th integer represents the total volume of snow melted on day _i_. Examples Input 5 30 25 20 15 10 9 10 12 4 13 Note In the first sample, Bob first makes a snow pile of volume 10, which melts to the size of 5 on the same day. On the second day, he makes another pile of size 10. Since it is a bit warmer than the day before, the first pile disappears completely while the second pile shrinks to 3. At the end of the second day, he has only a single pile of size 3. On the third day he makes a smaller pile than usual, but as the temperature dropped too, both piles survive till the end of the day. | 1,600 | false | false | false | false | true | false | false | true | false | false | 6,022 |
180E | Problem - 180E - 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 dp two pointers *1800 No tag edit access → Contest materials . After that, the remaining cubes join together (so that the gaps are closed) and the system counts the score. The number of points you score equals to the length of the maximum sequence of cubes of the same color that follow consecutively. Write a program that determines the maximum possible number of points you can score. Remember, you may delete no more than _k_ any cubes. It is allowed not to delete cubes at all. Input The first line contains three integers _n_, _m_ and _k_ (1u2009≤u2009_n_u2009≤u20092·105,u20091u2009≤u2009_m_u2009≤u2009105,u20090u2009≤u2009_k_u2009<u2009_n_). The second line contains _n_ integers from 1 to _m_ — the numbers of cube colors. The numbers of colors are separated by single spaces. Output Print the maximum possible number of points you can score. Examples Input 10 3 2 1 2 1 1 3 2 1 1 2 2 Output 4 Input 10 2 2 1 2 1 2 1 1 2 1 1 2 Output 5 Input 3 1 2 1 1 1 Output 3 Note In the first sample you should delete the fifth and the sixth cubes. In the second sample you should delete the fourth and the seventh cubes. In the third sample you shouldn't delete any cubes. | 1,800 | false | false | false | true | false | false | false | true | false | false | 9,118 |
1979D | You are given a binary string $$$s$$$ of length $$$n$$$, consisting of zeros and ones. You can perform the following operation exactly once: 1. Choose an integer $$$p$$$ ($$$1 le p le n$$$). 2. Reverse the substring $$$s_1 s_2 ldots s_p$$$. After this step, the string $$$s_1 s_2 ldots s_n$$$ will become $$$s_p s_{p-1} ldots s_1 s_{p+1} s_{p+2} ldots s_n$$$. 3. Then, perform a cyclic shift of the string $$$s$$$ to the left $$$p$$$ times. After this step, the initial string $$$s_1s_2 ldots s_n$$$ will become $$$s_{p+1}s_{p+2} ldots s_n s_p s_{p-1} ldots s_1$$$. For example, if you apply the operation to the string 110001100110 with $$$p=3$$$, after the second step, the string will become 011001100110, and after the third step, it will become 001100110011. A string $$$s$$$ is called $$$k$$$-proper if two conditions are met: $$$s_1=s_2=ldots=s_k$$$; $$$s_{i+k} eq s_i$$$ for any $$$i$$$ ($$$1 le i le n - k$$$). For example, with $$$k=3$$$, the strings 000, 111000111, and 111000 are $$$k$$$-proper, while the strings 000000, 001100, and 1110000 are not. You are given an integer $$$k$$$, which is a divisor of $$$n$$$. Find an integer $$$p$$$ ($$$1 le p le n$$$) such that after performing the operation, the string $$$s$$$ becomes $$$k$$$-proper, or determine that it is impossible. Note that if the string is initially $$$k$$$-proper, you still need to apply exactly one operation to it. Input Each test consists of multiple test cases. The first line contains one 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 two integers $$$n$$$ and $$$k$$$ ($$$1 le k le n$$$, $$$2 le n le 10^5$$$)xa0— the length of the string $$$s$$$ and the value of $$$k$$$. It is guaranteed that $$$k$$$ is a divisor of $$$n$$$. The second line of each test case contains a binary string $$$s$$$ of length $$$n$$$, consisting of the characters 0 and 1. 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 a single integerxa0— the value of $$$p$$$ to make the string $$$k$$$-proper, or $$$-1$$$ if it is impossible. If there are multiple solutions, output any of them. Example Input 7 8 4 11100001 4 2 1110 12 3 111000100011 5 5 00000 6 1 101001 8 4 01110001 12 2 110001100110 Note In the first test case, if you apply the operation with $$$p=3$$$, after the second step of the operation, the string becomes 11100001, and after the third step, it becomes 00001111. This string is $$$4$$$-proper. In the second test case, it can be shown that there is no operation after which the string becomes $$$2$$$-proper. In the third test case, if you apply the operation with $$$p=7$$$, after the second step of the operation, the string becomes 100011100011, and after the third step, it becomes 000111000111. This string is $$$3$$$-proper. In the fourth test case, after the operation with any $$$p$$$, the string becomes $$$5$$$-proper. | 1,800 | false | true | false | true | false | true | true | false | false | false | 413 |
1810G | You're going to generate an array $$$a$$$ with a length of at most $$$n$$$, where each $$$a_{i}$$$ equals either $$$1$$$ or $$$-1$$$. You generate this array in the following way. First, you choose some integer $$$k$$$ ($$$1le k le n$$$), which decides the length of $$$a$$$. Then, for each $$$i$$$ ($$$1le i le k$$$), you set $$$a_{i} = 1$$$ with probability $$$p_{i}$$$, otherwise set $$$a_{i} = -1$$$ (with probability $$$1 - p_{i}$$$). After the array is generated, you calculate $$$s_{i} = a_{1} + a_{2} + a_{3}+ ldots + a_{i}$$$. Specially, $$$s_{0} = 0$$$. Then you let $$$S$$$ equal to $$$displaystyle max_{i=0}^{k}{s_{i}}$$$. That is, $$$S$$$ is the maximum prefix sum of the array $$$a$$$. You are given $$$n+1$$$ integers $$$h_{0} , h_{1}, ldots ,h_{n}$$$. The score of an array $$$a$$$ with maximum prefix sum $$$S$$$ is $$$h_{S}$$$. Now, for each $$$k$$$, you want to know the expected score for an array of length $$$k$$$ modulo $$$10^9+7$$$. Input Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 5000$$$) — the number of test cases. Their description follows. The first line contains an integer $$$n$$$ ($$$1le n le 5000$$$). Then for the following $$$n$$$ lines, each line contains two integers $$$x_{i}$$$ and $$$y_{i}$$$ ($$$0 le x_{i} < 10^9 + 7$$$, $$$1le y_{i} < 10^9 + 7$$$, $$$x_{i} le y_{i}$$$), indicating $$$p_{i} = frac{x_{i}}{y_{i}}$$$. The next line contains $$$n+1$$$ integers $$$h_{0},h_{1}, ldots, h_{n}$$$ ($$$0 le h_{i} < 10^9 + 7$$$). It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$5000$$$. Output For each test case, output $$$n$$$ integers in one single line, the $$$i$$$-th of which denotes the expected score for an array of length $$$i$$$, modulo $$$10^9 + 7$$$. Formally, let $$$M = 10^9 + 7$$$. It can be shown that the answer can be expressed as an irreducible fraction $$$frac{p}{q}$$$, where $$$p$$$ and $$$q$$$ are integers and $$$q ot equiv 0 pmod{M}$$$. Output the integer equal to $$$p cdot q^{-1} bmod M$$$. In other words, output such an integer $$$x$$$ that $$$0 le x < M$$$ and $$$x cdot q equiv p pmod{M}$$$. Example Input 4 2 1 2 1 2 1 2 3 3 1 3 1 4 5 5 1 1 1 1 3 2 5 4 6 0 2 4 3 2 1 5 5 6 5 7 1 6 1 3 4 7 9 0 4 5 2 4 Output 500000005 750000007 1 1 1 200000005 333333339 333333339 500000005 880952391 801587311 781746041 789304620 Note In the first test case, if we choose $$$k=1$$$, there are $$$2$$$ possible arrays with equal probabilities: $$$[1]$$$ and $$$[-1]$$$. The $$$S$$$ values for them are $$$1$$$ and $$$0$$$. So the expected score is $$$frac{1}{2}h_{0} + frac{1}{2}h_{1} = frac{3}{2}$$$. If we choose $$$k=2$$$, there are $$$4$$$ possible arrays with equal probabilities: $$$[1,1]$$$, $$$[1,-1]$$$, $$$[-1,1]$$$, $$$[-1,-1]$$$, and the $$$S$$$ values for them are $$$2,1,0,0$$$. So the expected score is $$$frac{1}{2}h_{0} + frac{1}{4}h_{1} + frac{1}{4}h_{2} = frac{7}{4}$$$. In the second test case, no matter what the $$$S$$$ value is, the score is always $$$1$$$, so the expected score is always $$$1$$$. | 3,200 | false | false | false | true | false | false | false | false | false | false | 1,407 |
638A | The main street of Berland is a straight line with _n_ houses built along it (_n_ is an even number). The houses are located at both sides of the street. The houses with odd numbers are at one side of the street and are numbered from 1 to _n_u2009-u20091 in the order from the beginning of the street to the end (in the picture: from left to right). The houses with even numbers are at the other side of the street and are numbered from 2 to _n_ in the order from the end of the street to its beginning (in the picture: from right to left). The corresponding houses with even and odd numbers are strictly opposite each other, that is, house 1 is opposite house _n_, house 3 is opposite house _n_u2009-u20092, house 5 is opposite house _n_u2009-u20094 and so on. Vasya needs to get to house number _a_ as quickly as possible. He starts driving from the beginning of the street and drives his car to house _a_. To get from the beginning of the street to houses number 1 and _n_, he spends exactly 1 second. He also spends exactly one second to drive the distance between two neighbouring houses. Vasya can park at any side of the road, so the distance between the beginning of the street at the houses that stand opposite one another should be considered the same. Your task is: find the minimum time Vasya needs to reach house _a_. Input The first line of the input contains two integers, _n_ and _a_ (1u2009≤u2009_a_u2009≤u2009_n_u2009≤u2009100u2009000)xa0— the number of houses on the street and the number of the house that Vasya needs to reach, correspondingly. It is guaranteed that number _n_ is even. Output Print a single integer — the minimum time Vasya needs to get from the beginning of the street to house _a_. Note In the first sample there are only four houses on the street, two houses at each side. House 2 will be the last at Vasya's right. The second sample corresponds to picture with _n_u2009=u20098. House 5 is the one before last at Vasya's left. | 1,100 | true | false | false | false | false | true | false | false | false | false | 7,261 |
2024A | Alice has $$$a$$$ coins. She can open a bank deposit called "Profitable", but the minimum amount required to open this deposit is $$$b$$$ coins. There is also a deposit called "Unprofitable", which can be opened with any amount of coins. Alice noticed that if she opens the "Unprofitable" deposit with $$$x$$$ coins, the minimum amount required to open the "Profitable" deposit decreases by $$$2x$$$ coins. However, these coins cannot later be deposited into the "Profitable" deposit. Help Alice determine the maximum number of coins she can deposit into the "Profitable" deposit if she first deposits some amount of coins (possibly $$$0$$$) into the "Unprofitable" deposit. If Alice can never open the "Profitable" deposit, output $$$0$$$. 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 the test cases follows. A single line of each test case contains two integers $$$a$$$ and $$$b$$$ ($$$1 le a, b le 10^9$$$) — the number of coins Alice has and the initial minimum amount required to open the "Profitable" deposit. Output For each test case, output a single integer — the maximum number of coins that Alice can deposit into the "Profitable" deposit. If Alice can never open the "Profitable" deposit, output $$$0$$$. Note In the first test case, $$$a ge b$$$, so Alice can immediately open the "Profitable" deposit with all $$$10$$$ coins. In the second test case, Alice can open the "Unprofitable" deposit with $$$2$$$ coins. Then she will have $$$5$$$ coins left, but the minimum amount required to open the "Profitable" deposit will decrease by $$$4$$$ coins, making it equal to $$$5$$$ coins. Thus, Alice will be able to open the "Profitable" deposit with $$$5$$$ coins. In the third test case, Alice will not be able to open the "Profitable" deposit. | 800 | true | true | false | false | false | false | false | false | false | false | 125 |
1019E | By the year 3018, Summer Informatics School has greatly grown. Hotel «Berendeetronik» has been chosen as a location of the school. The camp consists of $$$n$$$ houses with $$$n-1$$$ pathways between them. It is possible to reach every house from each other using the pathways. Everything had been perfect until the rains started. The weather forecast promises that rains will continue for $$$m$$$ days. A special squad of teachers was able to measure that the $$$i$$$-th pathway, connecting houses $$$u_i$$$ and $$$v_i$$$, before the rain could be passed in $$$b_i$$$ seconds. Unfortunately, the rain erodes the roads, so with every day the time to pass the road will increase by $$$a_i$$$ seconds. In other words, on the $$$t$$$-th (from zero) day after the start of the rain, it will take $$$a_i cdot t + b_i$$$ seconds to pass through this road. Unfortunately, despite all the efforts of teachers, even in the year 3018 not all the students are in their houses by midnight. As by midnight all students have to go to bed, it is important to find the maximal time between all the pairs of houses for each day, so every student would know the time when he has to run to his house. Find all the maximal times of paths between every pairs of houses after $$$t=0$$$, $$$t=1$$$, ..., $$$t=m-1$$$ days. Input In the first line you are given two integers $$$n$$$ and $$$m$$$xa0— the number of houses in the camp and the number of raining days ($$$1 le n le 100,000$$$; $$$1 le m le 1,000,000$$$). In the next $$$n-1$$$ lines you are given the integers $$$u_i$$$, $$$v_i$$$, $$$a_i$$$, $$$b_i$$$xa0— description of pathways ($$$1 le u_i, v_i le n$$$; $$$0 le a_i le 10^5$$$; $$$0 le b_i le 10^9$$$). $$$i$$$-th pathway connects houses $$$u_i$$$ and $$$v_i$$$, and in day $$$t$$$ requires $$$a_i cdot t + b_i$$$ seconds to pass through. It is guaranteed that every two houses are connected by a sequence of pathways. Output Print $$$m$$$ integersxa0— the lengths of the longest path in the camp after a $$$t=0, t=1, ldots, t=m-1$$$ days after the start of the rain. Example Input 5 10 1 2 0 100 1 3 0 100 1 4 10 80 1 5 20 0 Output 200 200 200 210 220 230 260 290 320 350 Note Let's consider the first example. In the first three days ($$$0 le t le 2$$$) the longest path is between 2nd and 3rd houses, and its length is equal to $$$100+100=200$$$ seconds. In the third day ($$$t=2$$$) the road between houses 1 and 4 has length $$$100$$$ and keeps increasing. So, in days $$$t=2, 3, 4, 5$$$ the longest path is between vertices 4 and (1 or 2), and has length $$$180+10t$$$. Notice, that in the day $$$t=2$$$ there are three pathways with length 100, so there are three maximal paths of equal length. In the sixth day ($$$t=5$$$) pathway between first and fifth houses get length 100. So in every day with $$$t=5$$$ and further the longest path is between houses 4 and 5 and has length $$$80+30t$$$. | 3,200 | false | false | false | false | true | false | false | false | false | false | 5,593 |
1771E | Hossam bought a new piece of ground with length $$$n$$$ and width $$$m$$$, he divided it into an $$$n cdot m$$$ grid, each cell being of size $$$1 imes1$$$. Since Hossam's name starts with the letter 'H', he decided to draw the capital letter 'H' by building walls of size $$$1 imes1$$$ on some squares of the ground. Each square $$$1 imes1$$$ on the ground is assigned a quality degree: perfect, medium, or bad. The process of building walls to form up letter 'H' has the following constraints: The letter must consist of one horizontal and two vertical lines. The vertical lines must not be in the same or neighboring columns. The vertical lines must start in the same row and end in the same row (and thus have the same length). The horizontal line should connect the vertical lines, but must not cross them. The horizontal line can be in any row between the vertical lines (not only in the middle), except the top and the bottom one. (With the horizontal line in the top row the letter looks like 'n', and in the bottom row like 'U'.) It is forbidden to build walls in cells of bad quality. You can use at most one square of medium quality. You can use any number of squares of perfect quality. Find the maximum number of walls that can be used to draw the letter 'H'. Check the note for more clarification. Input The first line of the input contains two integer numbers $$$n$$$, $$$m$$$ ($$$1 le n, m le 400$$$). The next $$$n$$$ lines of the input contain $$$m$$$ characters each, describing the grid. The character '.' stands for a perfect square, the character 'm' stands for a medium square, and the character '#' stands for a bad square. Output Print a single integer — the maximum number of walls that form a capital letter 'H'. If it is not possible to draw any letter 'H', print $$$0$$$. Examples Input 7 8 ...#.m.. ..m...m. .#..#.m# ...m..m. m....... ..#.m.mm ......m. Note In the first test case, we can't build the letter 'H'. For the second test case, the figure below represents the grid and some of the valid letters 'H'. Perfect, medium, and bad squares are represented with white, yellow, and black colors respectively. | 2,500 | false | false | true | true | false | false | true | false | false | false | 1,665 |
1698E | You are given a permutation $$$a$$$ of length $$$n$$$. Recall that permutation is an array consisting of $$$n$$$ distinct integers from $$$1$$$ to $$$n$$$ in arbitrary order. You have a strength of $$$s$$$ and perform $$$n$$$ moves on the permutation $$$a$$$. The $$$i$$$-th move consists of the following: Pick two integers $$$x$$$ and $$$y$$$ such that $$$i leq x leq y leq min(i+s,n)$$$, and swap the positions of the integers $$$x$$$ and $$$y$$$ in the permutation $$$a$$$. Note that you can select $$$x=y$$$ in the operation, in which case no swap will occur. You want to turn $$$a$$$ into another permutation $$$b$$$ after $$$n$$$ moves. However, some elements of $$$b$$$ are missing and are replaced with $$$-1$$$ instead. Count the number of ways to replace each $$$-1$$$ in $$$b$$$ with some integer from $$$1$$$ to $$$n$$$ so that $$$b$$$ is a permutation and it is possible to turn $$$a$$$ into $$$b$$$ with a strength of $$$s$$$. Since the answer can be large, output it modulo $$$998,244,353$$$. Input The input consists of multiple test cases. The first line 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 two integers $$$n$$$ and $$$s$$$ ($$$1 leq n leq 2 cdot 10^5$$$; $$$1 leq s leq n$$$)xa0— the size of the permutation and your strength, respectively. The second line of each test case contains $$$n$$$ integers $$$a_1, a_2, ldots, a_n$$$ ($$$1 le a_i le n$$$)xa0— the elements of $$$a$$$. All elements of $$$a$$$ are distinct. The third line of each test case contains $$$n$$$ integers $$$b_1, b_2, ldots, b_n$$$ ($$$1 le b_i le n$$$ or $$$b_i = -1$$$)xa0— the elements of $$$b$$$. All elements of $$$b$$$ that are not equal to $$$-1$$$ are distinct. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$2 cdot 10^5$$$. Note In the first test case, $$$a=[2,1,3]$$$. There are two possible ways to fill out the $$$-1$$$s in $$$b$$$ to make it a permutation: $$$[3,1,2]$$$ or $$$[3,2,1]$$$. We can make $$$a$$$ into $$$[3,1,2]$$$ with a strength of $$$1$$$ as follows: $$$$$$[2,1,3] xrightarrow[x=1,,y=1]{} [2,1,3] xrightarrow[x=2,,y=3]{} [3,1,2] xrightarrow[x=3,,y=3]{} [3,1,2].$$$$$$ It can be proven that it is impossible to make $$$[2,1,3]$$$ into $$$[3,2,1]$$$ with a strength of $$$1$$$. Thus only one permutation $$$b$$$ satisfies the constraints, so the answer is $$$1$$$. In the second test case, $$$a$$$ and $$$b$$$ the same as the previous test case, but we now have a strength of $$$2$$$. We can make $$$a$$$ into $$$[3,2,1]$$$ with a strength of $$$2$$$ as follows: $$$$$$[2,1,3] xrightarrow[x=1,,y=3]{} [2,3,1] xrightarrow[x=2,,y=3]{} [3,2,1] xrightarrow[x=3,,y=3]{} [3,2,1].$$$$$$ We can still make $$$a$$$ into $$$[3,1,2]$$$ using a strength of $$$1$$$ as shown in the previous test case, so the answer is $$$2$$$. In the third test case, there is only one permutation $$$b$$$. It can be shown that it is impossible to turn $$$a$$$ into $$$b$$$, so the answer is $$$0$$$. | 2,300 | false | true | false | false | false | false | true | false | true | false | 2,090 |
1117C | You a captain of a ship. Initially you are standing in a point $$$(x_1, y_1)$$$ (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point $$$(x_2, y_2)$$$. You know the weather forecast — the string $$$s$$$ of length $$$n$$$, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side $$$s_1$$$, the second day — $$$s_2$$$, the $$$n$$$-th day — $$$s_n$$$ and $$$(n+1)$$$-th day — $$$s_1$$$ again and so on. Ship coordinates change the following way: if wind blows the direction U, then the ship moves from $$$(x, y)$$$ to $$$(x, y + 1)$$$; if wind blows the direction D, then the ship moves from $$$(x, y)$$$ to $$$(x, y - 1)$$$; if wind blows the direction L, then the ship moves from $$$(x, y)$$$ to $$$(x - 1, y)$$$; if wind blows the direction R, then the ship moves from $$$(x, y)$$$ to $$$(x + 1, y)$$$. The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction U and the ship moves the direction L, then from point $$$(x, y)$$$ it will move to the point $$$(x - 1, y + 1)$$$, and if it goes the direction U, then it will move to the point $$$(x, y + 2)$$$. You task is to determine the minimal number of days required for the ship to reach the point $$$(x_2, y_2)$$$. Input The first line contains two integers $$$x_1, y_1$$$ ($$$0 le x_1, y_1 le 10^9$$$) — the initial coordinates of the ship. The second line contains two integers $$$x_2, y_2$$$ ($$$0 le x_2, y_2 le 10^9$$$) — the coordinates of the destination point. It is guaranteed that the initial coordinates and destination point coordinates are different. The third line contains a single integer $$$n$$$ ($$$1 le n le 10^5$$$) — the length of the string $$$s$$$. The fourth line contains the string $$$s$$$ itself, consisting only of letters U, D, L and R. Output The only line should contain the minimal number of days required for the ship to reach the point $$$(x_2, y_2)$$$. If it's impossible then print "-1". Note In the first example the ship should perform the following sequence of moves: "RRRRU". Then its coordinates will change accordingly: $$$(0, 0)$$$ $$$ ightarrow$$$ $$$(1, 1)$$$ $$$ ightarrow$$$ $$$(2, 2)$$$ $$$ ightarrow$$$ $$$(3, 3)$$$ $$$ ightarrow$$$ $$$(4, 4)$$$ $$$ ightarrow$$$ $$$(4, 6)$$$. In the second example the ship should perform the following sequence of moves: "DD" (the third day it should stay in place). Then its coordinates will change accordingly: $$$(0, 3)$$$ $$$ ightarrow$$$ $$$(0, 3)$$$ $$$ ightarrow$$$ $$$(0, 1)$$$ $$$ ightarrow$$$ $$$(0, 0)$$$. In the third example the ship can never reach the point $$$(0, 1)$$$. | 1,900 | false | false | false | false | false | false | false | true | false | false | 5,107 |
1666D | Problem - 1666D - 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 *900 No tag edit access → Contest materials ") ") . The following $$$n$$$ lines contain test cases. Each test case consists of two words $$$s$$$ and $$$t$$$ separated by a space. Each word consists of at least one and at most 30 uppercase English letters; $$$s$$$ is the Daisy's initial word for the game; $$$t$$$ is the final word that Daisy would like to get at the end of the game. Output Output $$$n$$$ lines to the outputxa0— a single line for each test case. Output "YES" if it is possible for Daisy to get from the initial word $$$s$$$ to the final word $$$t$$$ by playing the Deletive Editing game. Output "NO" otherwise. Example Input 6 DETERMINED TRME DETERMINED TERM PSEUDOPSEUDOHYPOPARATHYROIDISM PEPA DEINSTITUTIONALIZATION DONATION CONTEST CODE SOLUTION SOLUTION Output YES NO NO YES NO YES | 900 | false | true | false | false | false | false | false | false | false | false | 2,282 |
262A | Problem - 262A - 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 loves the Little Lvov Elephant's lucky numbers. Let us remind you that lucky numbers are positive integers whose decimal representation only contains lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not. Roma's got _n_ positive integers. He wonders, how many of those integers have not more than _k_ lucky digits? Help him, write the program that solves the problem. Input The first line contains two integers _n_, _k_ (1u2009≤u2009_n_,u2009_k_u2009≤u2009100). The second line contains _n_ integers _a__i_ (1u2009≤u2009_a__i_u2009≤u2009109) — the numbers that Roma has. The numbers in the lines are separated by single spaces. Output In a single line print a single integer — the answer to the problem. Examples Input 3 4 1 2 4 Output 3 Input 3 2 447 44 77 Output 2 Note In the first sample all numbers contain at most four lucky digits, so the answer is 3. In the second sample number 447 doesn't fit in, as it contains more than two lucky digits. All other numbers are fine, so the answer is 2. | 800 | false | false | true | false | false | false | false | false | false | false | 8,785 |
448D | Problem - 448D - 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 *1800 No tag edit access → Contest materials . Then he was asked: what number in the table is the _k_-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success? Consider the given multiplication table. If you write out all _n_·_m_ numbers from the table in the non-decreasing order, then the _k_-th number you write out is called the _k_-th largest number. Input The single line contains integers _n_, _m_ and _k_ (1u2009≤u2009_n_,u2009_m_u2009≤u20095·105;xa01u2009≤u2009_k_u2009≤u2009_n_·_m_). Output Print the _k_-th largest number in a _n_u2009×u2009_m_ multiplication table. Examples Input 2 2 2 Output 2 Input 2 3 4 Output 3 Input 1 10 5 Output 5 Note A 2u2009×u20093 multiplication table looks like this: 1 2 3 2 4 6 | 1,800 | false | false | false | false | false | false | true | true | false | false | 8,046 |
1945E | Anton got bored during the hike and wanted to solve something. He asked Kirill if he had any new problems, and of course, Kirill had one. You are given a permutation $$$p$$$ of size $$$n$$$, and a number $$$x$$$ that needs to be found. 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). You decided that you are a cool programmer, so you will use an advanced algorithm for the search — binary search. However, you forgot that for binary search, the array must be sorted. You did not give up and decided to apply this algorithm anyway, and in order to get the correct answer, you can perform the following operation no more than $$$2$$$ times before running the algorithm: choose the indices $$$i$$$, $$$j$$$ ($$$1le i, j le n$$$) and swap the elements at positions $$$i$$$ and $$$j$$$. After that, the binary search is performed. At the beginning of the algorithm, two variables $$$l = 1$$$ and $$$r = n + 1$$$ are declared. Then the following loop is executed: 1. If $$$r - l = 1$$$, end the loop 2. $$$m = lfloor frac{r + l}{2} floor$$$ 3. If $$$p_m le x$$$, assign $$$l = m$$$, otherwise $$$r = m$$$. The goal is to rearrange the numbers in the permutation before the algorithm so that after the algorithm is executed, $$$p_l$$$ is equal to $$$x$$$. It can be shown that $$$2$$$ operations are always sufficient. Input Each test consists of multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 le t le 2cdot 10^4$$$) — the number of test cases. Then follow the descriptions of the test cases. The first line of each test case contains two integers $$$n$$$ and $$$x$$$ ($$$1 le x le n le 2cdot 10^5$$$) — the length of the permutation and the number to be found. The second line contains the permutation $$$p$$$ separated by spaces ($$$1 le p_i le n$$$). It is guaranteed that the sum of the values of $$$n$$$ for all test cases does not exceed $$$2cdot 10^5$$$. Output For each test case, output an integer $$$k$$$ ($$$0 le k le 2$$$) on the first line — the number of operations performed by you. In the next $$$k$$$ lines, output $$$2$$$ integers $$$i$$$, $$$j$$$ ($$$1 le i, j le n$$$) separated by a space, indicating that you are swapping the elements at positions $$$i$$$ and $$$j$$$. Note that you do not need to minimize the number of operations. | 1,700 | false | true | false | false | false | true | false | true | false | false | 609 |
520E | Vasya is sitting on an extremely boring math class. To have fun, he took a piece of paper and wrote out _n_ numbers on a single line. After that, Vasya began to write out different ways to put pluses ("+") in the line between certain digits in the line so that the result was a correct arithmetic expression; formally, no two pluses in such a partition can stand together (between any two adjacent pluses there must be at least one digit), and no plus can stand at the beginning or the end of a line. For example, in the string 100500, ways 100500 (add no pluses), 1+00+500 or 10050+0 are correct, and ways 100++500, +1+0+0+5+0+0 or 100500+ are incorrect. The lesson was long, and Vasya has written all the correct ways to place exactly _k_ pluses in a string of digits. At this point, he got caught having fun by a teacher and he was given the task to calculate the sum of all the resulting arithmetic expressions by the end of the lesson (when calculating the value of an expression the leading zeros should be ignored). As the answer can be large, Vasya is allowed to get only its remainder modulo 109u2009+u20097. Help him! Input The first line contains two integers, _n_ and _k_ (0u2009≤u2009_k_u2009<u2009_n_u2009≤u2009105). The second line contains a string consisting of _n_ digits. Output Print the answer to the problem modulo 109u2009+u20097. Note In the first sample the result equals (1u2009+u200908)u2009+u2009(10u2009+u20098)u2009=u200927. In the second sample the result equals 1u2009+u20090u2009+u20098u2009=u20099. | 2,200 | true | false | false | true | false | false | false | false | false | false | 7,753 |
209C | Vasya went for a walk in the park. The park has _n_ glades, numbered from 1 to _n_. There are _m_ trails between the glades. The trails are numbered from 1 to _m_, where the _i_-th trail connects glades _x__i_ and _y__i_. The numbers of the connected glades may be the same (_x__i_u2009=u2009_y__i_), which means that a trail connects a glade to itself. Also, two glades may have several non-intersecting trails between them. Vasya is on glade 1, he wants to walk on all trails of the park exactly once, so that he can eventually return to glade 1. Unfortunately, Vasya does not know whether this walk is possible or not. Help Vasya, determine whether the walk is possible or not. If such walk is impossible, find the minimum number of trails the authorities need to add to the park in order to make the described walk possible. Vasya can shift from one trail to another one only on glades. He can move on the trails in both directions. If Vasya started going on the trail that connects glades _a_ and _b_, from glade _a_, then he must finish this trail on glade _b_. Input The first line contains two integers _n_ and _m_ (1u2009≤u2009_n_u2009≤u2009106;xa00u2009≤u2009_m_u2009≤u2009106) — the number of glades in the park and the number of trails in the park, respectively. Next _m_ lines specify the trails. The _i_-th line specifies the _i_-th trail as two space-separated numbers, _x__i_, _y__i_ (1u2009≤u2009_x__i_,u2009_y__i_u2009≤u2009_n_) — the numbers of the glades connected by this trail. Output Print the single integer — the answer to the problem. If Vasya's walk is possible without adding extra trails, print 0, otherwise print the minimum number of trails the authorities need to add to the park in order to make Vasya's walk possible. Note In the first test case the described walk is possible without building extra trails. For example, let's first go on the first trail, then on the second one, and finally on the third one. In the second test case the described walk is impossible without adding extra trails. To make the walk possible, it is enough to add one trail, for example, between glades number one and two. | 2,400 | false | true | false | false | false | true | false | false | false | true | 8,994 |
250E | Joe has been hurt on the Internet. Now he is storming around the house, destroying everything in his path. Joe's house has _n_ floors, each floor is a segment of _m_ cells. Each cell either contains nothing (it is an empty cell), or has a brick or a concrete wall (always something one of three). It is believed that each floor is surrounded by a concrete wall on the left and on the right. Now Joe is on the _n_-th floor and in the first cell, counting from left to right. At each moment of time, Joe has the direction of his gaze, to the right or to the left (always one direction of the two). Initially, Joe looks to the right. Joe moves by a particular algorithm. Every second he makes one of the following actions: If the cell directly under Joe is empty, then Joe falls down. That is, he moves to this cell, the gaze direction is preserved. Otherwise consider the next cell in the current direction of the gaze. If the cell is empty, then Joe moves into it, the gaze direction is preserved. If this cell has bricks, then Joe breaks them with his forehead (the cell becomes empty), and changes the direction of his gaze to the opposite. If this cell has a concrete wall, then Joe just changes the direction of his gaze to the opposite (concrete can withstand any number of forehead hits). Joe calms down as soon as he reaches any cell of the first floor. The figure below shows an example Joe's movements around the house. Determine how many seconds Joe will need to calm down. Input The first line contains two integers _n_ and _m_ (2u2009≤u2009_n_u2009≤u2009100, 1u2009≤u2009_m_u2009≤u2009104). Next _n_ lines contain the description of Joe's house. The _i_-th of these lines contains the description of the (_n_u2009-u2009_i_u2009+u20091)-th floor of the house — a line that consists of _m_ characters: "." means an empty cell, "+" means bricks and "#" means a concrete wall. It is guaranteed that the first cell of the _n_-th floor is empty. Output Print a single number — the number of seconds Joe needs to reach the first floor; or else, print word "Never" (without the quotes), if it can never happen. Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier. Examples Input 4 10 ...+.##+.+ +#++..+++# ++.#++++.. .+##.++#.+ | 2,000 | false | false | false | false | false | false | true | false | false | false | 8,832 |
1469D | You have an array $$$a_1, a_2, dots, a_n$$$ where $$$a_i = i$$$. In one step, you can choose two indices $$$x$$$ and $$$y$$$ ($$$x eq y$$$) and set $$$a_x = leftlceil frac{a_x}{a_y} ight ceil$$$ (ceiling function). Your goal is to make array $$$a$$$ consist of $$$n - 1$$$ ones and $$$1$$$ two in no more than $$$n + 5$$$ steps. Note that you don't have to minimize the number of steps. Input The first line contains a single integer $$$t$$$ ($$$1 le t le 1000$$$)xa0— the number of test cases. The first and only line of each test case contains the single integer $$$n$$$ ($$$3 le n le 2 cdot 10^5$$$)xa0— the length of array $$$a$$$. It's guaranteed that the sum of $$$n$$$ over test cases doesn't exceed $$$2 cdot 10^5$$$. Output For each test case, print the sequence of operations that will make $$$a$$$ as $$$n - 1$$$ ones and $$$1$$$ two in the following format: firstly, print one integer $$$m$$$ ($$$m le n + 5$$$)xa0— the number of operations; next print $$$m$$$ pairs of integers $$$x$$$ and $$$y$$$ ($$$1 le x, y le n$$$; $$$x eq y$$$) ($$$x$$$ may be greater or less than $$$y$$$)xa0— the indices of the corresponding operation. It can be proven that for the given constraints it's always possible to find a correct sequence of operations. Example Output 2 3 2 3 2 3 3 4 4 2 4 2 Note In the first test case, you have array $$$a = [1, 2, 3]$$$. For example, you can do the following: 1. choose $$$3$$$, $$$2$$$: $$$a_3 = leftlceil frac{a_3}{a_2} ight ceil = 2$$$ and array $$$a = [1, 2, 2]$$$; 2. choose $$$3$$$, $$$2$$$: $$$a_3 = leftlceil frac{2}{2} ight ceil = 1$$$ and array $$$a = [1, 2, 1]$$$. You've got array with $$$2$$$ ones and $$$1$$$ two in $$$2$$$ steps. In the second test case, $$$a = [1, 2, 3, 4]$$$. For example, you can do the following: 1. choose $$$3$$$, $$$4$$$: $$$a_3 = leftlceil frac{3}{4} ight ceil = 1$$$ and array $$$a = [1, 2, 1, 4]$$$; 2. choose $$$4$$$, $$$2$$$: $$$a_4 = leftlceil frac{4}{2} ight ceil = 2$$$ and array $$$a = [1, 2, 1, 2]$$$; 3. choose $$$4$$$, $$$2$$$: $$$a_4 = leftlceil frac{2}{2} ight ceil = 1$$$ and array $$$a = [1, 2, 1, 1]$$$. | 1,700 | true | false | false | false | false | true | true | false | false | false | 3,335 |
Subsets and Splits