{"description": "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. During the first move, you can remove the first character $$$s_1=$$$ b because $$$s_2=$$$ a. Then the string becomes $$$s=$$$ acabcab. During the second move, you can remove the fifth character $$$s_5=$$$ c because $$$s_4=$$$ b. Then the string becomes $$$s=$$$ acabab. During the third move, you can remove the sixth character $$$s_6=$$$'b' because $$$s_5=$$$ a. Then the string becomes $$$s=$$$ acaba. 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_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The only line of the input contains one integer $$$|s|$$$ ($$$1 \\le |s| \\le 100$$$) \u2014 the length of $$$s$$$. The second line of the input contains one string $$$s$$$ consisting of $$$|s|$$$ lowercase Latin letters.", "output_spec": "Print one integer \u2014 the maximum possible number of characters you can remove if you choose the sequence of moves optimally.", "notes": "NoteThe 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. During the first move, remove the third character $$$s_3=$$$ d, $$$s$$$ becomes bca. During the second move, remove the second character $$$s_2=$$$ c, $$$s$$$ becomes ba. And during the third move, remove the first character $$$s_1=$$$ b, $$$s$$$ becomes a. ", "sample_inputs": ["8\nbacabcab", "4\nbcda", "6\nabbbbb"], "sample_outputs": ["4", "3", "5"], "tags": ["brute force", "constructive algorithms", "strings", "greedy"], "src_uid": "9ce37bc2d361f5bb8a0568fb479b8a38", "difficulty": 1600} {"description": "You can not just take the file and send it. When Polycarp trying to send a file in the social network \"Codehorses\", he encountered an unexpected problem. If the name of the file contains three or more \"x\" (lowercase Latin letters \"x\") in a row, the system considers that the file content does not correspond to the social network topic. In this case, the file is not sent and an error message is displayed.Determine the minimum number of characters to remove from the file name so after that the name does not contain \"xxx\" as a substring. Print 0 if the file name does not initially contain a forbidden substring \"xxx\".You can delete characters in arbitrary positions (not necessarily consecutive). If you delete a character, then the length of a string is reduced by $$$1$$$. For example, if you delete the character in the position $$$2$$$ from the string \"exxxii\", then the resulting string is \"exxii\".", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains integer $$$n$$$ $$$(3 \\le n \\le 100)$$$ \u2014 the length of the file name. The second line contains a string of length $$$n$$$ consisting of lowercase Latin letters only \u2014 the file name.", "output_spec": "Print the minimum number of characters to remove from the file name so after that the name does not contain \"xxx\" as a substring. If initially the file name dost not contain a forbidden substring \"xxx\", print 0.", "notes": "NoteIn the first example Polycarp tried to send a file with name contains number $$$33$$$, written in Roman numerals. But he can not just send the file, because it name contains three letters \"x\" in a row. To send the file he needs to remove any one of this letters.", "sample_inputs": ["6\nxxxiii", "5\nxxoxx", "10\nxxxxxxxxxx"], "sample_outputs": ["1", "0", "8"], "tags": ["strings", "greedy"], "src_uid": "8de14db41d0acee116bd5d8079cb2b02", "difficulty": 800} {"description": "Pak Chanek plans to build a garage. He wants the garage to consist of a square and a right triangle that are arranged like the following illustration. Define $$$a$$$ and $$$b$$$ as the lengths of two of the sides in the right triangle as shown in the illustration. An integer $$$x$$$ is suitable if and only if we can construct a garage with assigning positive integer values for the lengths $$$a$$$ and $$$b$$$ ($$$a<b$$$) so that the area of the square at the bottom is exactly $$$x$$$. As a good friend of Pak Chanek, you are asked to help him find the $$$N$$$-th smallest suitable number.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "128 megabytes", "input_spec": "The only line contains a single integer $$$N$$$ ($$$1 \\leq N \\leq 10^9$$$).", "output_spec": "An integer that represents the $$$N$$$-th smallest suitable number.", "notes": "NoteThe $$$3$$$-rd smallest suitable number is $$$7$$$. A square area of $$$7$$$ can be obtained by assigning $$$a=3$$$ and $$$b=4$$$.", "sample_inputs": ["3"], "sample_outputs": ["7"], "tags": ["geometry", "math", "binary search"], "src_uid": "d0a8604b78ba19ab769fd1ec90a72e4e", "difficulty": 1500} {"description": "InputThe input consists of a single string of uppercase letters A-Z. The length of the string is between 1 and 10 characters, inclusive.OutputOutput \"YES\" or \"NO\".ExamplesInput\nHELP\nOutput\nYES\nInput\nAID\nOutput\nNO\nInput\nMARY\nOutput\nNO\nInput\nANNA\nOutput\nYES\nInput\nMUG\nOutput\nYES\nInput\nCUP\nOutput\nNO\nInput\nSUM\nOutput\nYES\nInput\nPRODUCT\nOutput\nNO\n", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The input consists of a single string of uppercase letters A-Z. The length of the string is between 1 and 10 characters, inclusive.", "output_spec": "Output \"YES\" or \"NO\".", "notes": null, "sample_inputs": ["HELP", "AID", "MARY", "ANNA", "MUG", "CUP", "SUM", "PRODUCT"], "sample_outputs": ["YES", "NO", "NO", "YES", "YES", "NO", "YES", "NO"], "tags": ["implementation"], "src_uid": "27e977b41f5b6970a032d13e53db2a6a", "difficulty": 1400} {"description": "A chainword is a special type of crossword. As most of the crosswords do, it has cells that you put the letters in and some sort of hints to what these letters should be.The letter cells in a chainword are put in a single row. We will consider chainwords of length $$$m$$$ in this task.A hint to a chainword is a sequence of segments such that the segments don't intersect with each other and cover all $$$m$$$ letter cells. Each segment contains a description of the word in the corresponding cells.The twist is that there are actually two hints: one sequence is the row above the letter cells and the other sequence is the row below the letter cells. When the sequences are different, they provide a way to resolve the ambiguity in the answers.You are provided with a dictionary of $$$n$$$ words, each word consists of lowercase Latin letters. All words are pairwise distinct.An instance of a chainword is the following triple: a string of $$$m$$$ lowercase Latin letters; the first hint: a sequence of segments such that the letters that correspond to each segment spell a word from the dictionary; the second hint: another sequence of segments such that the letters that correspond to each segment spell a word from the dictionary. Note that the sequences of segments don't necessarily have to be distinct.Two instances of chainwords are considered different if they have different strings, different first hints or different second hints.Count the number of different instances of chainwords. Since the number might be pretty large, output it modulo $$$998\\,244\\,353$$$.", "input_from": "standard input", "output_to": "standard output", "time_limit": "3 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\le n \\le 8$$$, $$$1 \\le m \\le 10^9$$$)\u00a0\u2014 the number of words in the dictionary and the number of letter cells. Each of the next $$$n$$$ lines contains a word\u00a0\u2014 a non-empty string of no more than $$$5$$$ lowercase Latin letters. All words are pairwise distinct. ", "output_spec": "Print a single integer\u00a0\u2014 the number of different instances of chainwords of length $$$m$$$ for the given dictionary modulo $$$998\\,244\\,353$$$.", "notes": "NoteHere are all the instances of the valid chainwords for the first example: The red lines above the letters denote the segments of the first hint, the blue lines below the letters denote the segments of the second hint.In the second example the possible strings are: \"abab\", \"abcd\", \"cdab\" and \"cdcd\". All the hints are segments that cover the first two letters and the last two letters.", "sample_inputs": ["3 5\nababa\nab\na", "2 4\nab\ncd", "5 100\na\naa\naaa\naaaa\naaaaa"], "sample_outputs": ["11", "4", "142528942"], "tags": ["strings", "string suffix structures", "matrices", "data structures", "brute force", "dp"], "src_uid": "711d15e11016d0164fb2b0c3756e4857", "difficulty": 2700} {"description": "Little Johnny Bubbles enjoys spending hours in front of his computer playing video games. His favorite game is Bubble Strike, fast-paced bubble shooting online game for two players.Each game is set in one of the N maps, each having different terrain configuration. First phase of each game decides on which map the game will be played. The game system randomly selects three maps and shows them to the players. Each player must pick one of those three maps to be discarded. The game system then randomly selects one of the maps that were not picked by any of the players and starts the game.Johnny is deeply enthusiastic about the game and wants to spend some time studying maps, thus increasing chances to win games played on those maps. However, he also needs to do his homework, so he does not have time to study all the maps. That is why he asked himself the following question: \"What is the minimum number of maps I have to study, so that the probability to play one of those maps is at least $$$P$$$\"?Can you help Johnny find the answer for this question? You can assume Johnny's opponents do not know him, and they will randomly pick maps.", "input_from": "standard input", "output_to": "standard output", "time_limit": "0.5 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers $$$N$$$ ($$$3$$$ $$$\\leq$$$ $$$N$$$ $$$\\leq$$$ $$$10^{3}$$$) and $$$P$$$ ($$$0$$$ $$$\\leq$$$ $$$P$$$ $$$\\leq$$$ $$$1$$$) \u2013 total number of maps in the game and probability to play map Johnny has studied. $$$P$$$ will have at most four digits after the decimal point.", "output_spec": "Output contains one integer number \u2013 minimum number of maps Johnny has to study.", "notes": null, "sample_inputs": ["7 1.0000"], "sample_outputs": ["6"], "tags": ["combinatorics", "binary search", "ternary search", "probabilities", "math"], "src_uid": "788ed59a964264bd0e755e155a37e14d", "difficulty": 2000} {"description": "You are given an undirected graph consisting of $$$n$$$ vertices and $$$m$$$ edges. Initially there is a single integer written on every vertex: the vertex $$$i$$$ has $$$p_i$$$ written on it. All $$$p_i$$$ are distinct integers from $$$1$$$ to $$$n$$$.You have to process $$$q$$$ queries of two types: $$$1$$$ $$$v$$$ \u2014 among all vertices reachable from the vertex $$$v$$$ using the edges of the graph (including the vertex $$$v$$$ itself), find a vertex $$$u$$$ with the largest number $$$p_u$$$ written on it, print $$$p_u$$$ and replace $$$p_u$$$ with $$$0$$$; $$$2$$$ $$$i$$$ \u2014 delete the $$$i$$$-th edge from the graph. Note that, in a query of the first type, it is possible that all vertices reachable from $$$v$$$ have $$$0$$$ written on them. In this case, $$$u$$$ is not explicitly defined, but since the selection of $$$u$$$ does not affect anything, you can choose any vertex reachable from $$$v$$$ and print its value (which is $$$0$$$). ", "input_from": "standard input", "output_to": "standard output", "time_limit": "1.5 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \\le n \\le 2 \\cdot 10^5$$$; $$$1 \\le m \\le 3 \\cdot 10^5$$$; $$$1 \\le q \\le 5 \\cdot 10^5$$$). The second line contains $$$n$$$ distinct integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_n$$$, where $$$p_i$$$ is the number initially written on vertex $$$i$$$ ($$$1 \\le p_i \\le n$$$). Then $$$m$$$ lines follow, the $$$i$$$-th of them contains two integers $$$a_i$$$ and $$$b_i$$$ ($$$1 \\le a_i, b_i \\le n$$$, $$$a_i \\ne b_i$$$) and means that the $$$i$$$-th edge connects vertices $$$a_i$$$ and $$$b_i$$$. It is guaranteed that the graph does not contain multi-edges. Then $$$q$$$ lines follow, which describe the queries. Each line is given by one of the following formats: $$$1$$$ $$$v$$$ \u2014 denotes a query of the first type with a vertex $$$v$$$ ($$$1 \\le v \\le n$$$). $$$2$$$ $$$i$$$ \u2014 denotes a query of the second type with an edge $$$i$$$ ($$$1 \\le i \\le m$$$). For each query of the second type, it is guaranteed that the corresponding edge is not deleted from the graph yet. ", "output_spec": "For every query of the first type, print the value of $$$p_u$$$ written on the chosen vertex $$$u$$$.", "notes": null, "sample_inputs": ["5 4 6\n1 2 5 4 3\n1 2\n2 3\n1 3\n4 5\n1 1\n2 1\n2 3\n1 1\n1 2\n1 2"], "sample_outputs": ["5\n1\n2\n0"], "tags": ["trees", "data structures", "graphs", "dsu", "implementation"], "src_uid": "ad014bde729222db14f38caa521e4167", "difficulty": 2600} {"description": "Andrey thinks he is truly a successful developer, but in reality he didn't know about the binary search algorithm until recently. After reading some literature Andrey understood that this algorithm allows to quickly find a certain number $$$x$$$ in an array. For an array $$$a$$$ indexed from zero, and an integer $$$x$$$ the pseudocode of the algorithm is as follows: Note that the elements of the array are indexed from zero, and the division is done in integers (rounding down).Andrey read that the algorithm only works if the array is sorted. However, he found this statement untrue, because there certainly exist unsorted arrays for which the algorithm find $$$x$$$!Andrey wants to write a letter to the book authors, but before doing that he must consider the permutations of size $$$n$$$ such that the algorithm finds $$$x$$$ in them. A permutation of size $$$n$$$ is an array consisting of $$$n$$$ distinct integers between $$$1$$$ and $$$n$$$ in arbitrary order.Help Andrey and find the number of permutations of size $$$n$$$ which contain $$$x$$$ at position $$$pos$$$ and for which the given implementation of the binary search algorithm finds $$$x$$$ (returns true). As the result may be extremely large, print the remainder of its division by $$$10^9+7$$$.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The only line of input contains integers $$$n$$$, $$$x$$$ and $$$pos$$$ ($$$1 \\le x \\le n \\le 1000$$$, $$$0 \\le pos \\le n - 1$$$) \u2014 the required length of the permutation, the number to search, and the required position of that number, respectively.", "output_spec": "Print a single number\u00a0\u2014 the remainder of the division of the number of valid permutations by $$$10^9+7$$$.", "notes": "NoteAll possible permutations in the first test case: $$$(2, 3, 1, 4)$$$, $$$(2, 4, 1, 3)$$$, $$$(3, 2, 1, 4)$$$, $$$(3, 4, 1, 2)$$$, $$$(4, 2, 1, 3)$$$, $$$(4, 3, 1, 2)$$$.", "sample_inputs": ["4 1 2", "123 42 24"], "sample_outputs": ["6", "824071958"], "tags": ["combinatorics", "binary search"], "src_uid": "24e2f10463f440affccc2755f4462d8a", "difficulty": 1500} {"description": "Today is the final contest of INOI (Iranian National Olympiad in Informatics). The contest room is a row with $$$n$$$ computers. All computers are numbered with integers from $$$1$$$ to $$$n$$$ from left to right. There are $$$m$$$ participants, numbered with integers from $$$1$$$ to $$$m$$$.We have an array $$$a$$$ of length $$$m$$$ where $$$a_{i}$$$ ($$$1 \\leq a_i \\leq n$$$) is the computer behind which the $$$i$$$-th participant wants to sit.Also, we have another array $$$b$$$ of length $$$m$$$ consisting of characters 'L' and 'R'. $$$b_i$$$ is the side from which the $$$i$$$-th participant enters the room. 'L' means the participant enters from the left of computer $$$1$$$ and goes from left to right, and 'R' means the participant enters from the right of computer $$$n$$$ and goes from right to left.The participants in the order from $$$1$$$ to $$$m$$$ enter the room one by one. The $$$i$$$-th of them enters the contest room in the direction $$$b_i$$$ and goes to sit behind the $$$a_i$$$-th computer. If it is occupied he keeps walking in his direction until he reaches the first unoccupied computer. After that, he sits behind it. If he doesn't find any computer he gets upset and gives up on the contest.The madness of the $$$i$$$-th participant is the distance between his assigned computer ($$$a_i$$$) and the computer he ends up sitting behind. The distance between computers $$$i$$$ and $$$j$$$ is equal to $$$|i - j|$$$.The values in the array $$$a$$$ can be equal. There exist $$$n^m \\cdot 2^m$$$ possible pairs of arrays $$$(a, b)$$$.Consider all pairs of arrays $$$(a, b)$$$ such that no person becomes upset. For each of them let's calculate the sum of participants madnesses. Find the sum of all these values.You will be given some prime modulo $$$p$$$. Find this sum by modulo $$$p$$$.", "input_from": "standard input", "output_to": "standard output", "time_limit": "3 seconds", "memory_limit": "256 megabytes", "input_spec": "The only line contains three integers $$$n$$$, $$$m$$$, $$$p$$$ ($$$1 \\leq m \\leq n \\leq 500, 10^8 \\leq p \\leq 10 ^ 9 + 9$$$). It is guaranteed, that the number $$$p$$$ is prime.", "output_spec": "Print only one integer\u00a0\u2014 the required sum by modulo $$$p$$$.", "notes": "NoteIn the first test, there are three possible arrays $$$a$$$: $$$\\{1\\}$$$, $$$\\{2\\}$$$, and $$$ \\{3\\}$$$ and two possible arrays $$$b$$$: $$$\\{\\mathtt{L}\\}$$$ and $$$\\{\\mathtt{R}\\}$$$. For all six pairs of arrays $$$(a, b)$$$, the only participant will sit behind the computer $$$a_1$$$, so his madness will be $$$0$$$. So the total sum of madnesses will be $$$0$$$.In the second test, all possible pairs of arrays $$$(a, b)$$$, such that no person becomes upset are: $$$(\\{1, 1\\}, \\{\\mathtt{L}, \\mathtt{L}\\})$$$, the sum of madnesses is $$$1$$$; $$$(\\{1, 1\\}, \\{\\mathtt{R}, \\mathtt{L}\\})$$$, the sum of madnesses is $$$1$$$; $$$(\\{2, 2\\}, \\{\\mathtt{R}, \\mathtt{R}\\})$$$, the sum of madnesses is $$$1$$$; $$$(\\{2, 2\\}, \\{\\mathtt{L}, \\mathtt{R}\\})$$$, the sum of madnesses is $$$1$$$; all possible pairs of $$$a \\in \\{\\{1, 2\\}, \\{2, 1\\}\\}$$$ and $$$b \\in \\{\\{\\mathtt{L}, \\mathtt{L}\\}, \\{\\mathtt{R}, \\mathtt{L}\\}, \\{\\mathtt{L}, \\mathtt{R}\\}, \\{\\mathtt{R}, \\mathtt{R}\\}\\}$$$, the sum of madnesses is $$$0$$$. So, the answer is $$$1 + 1 + 1 + 1 + 0 \\ldots = 4$$$.", "sample_inputs": ["3 1 1000000007", "2 2 1000000009", "3 2 998244353", "20 10 1000000009"], "sample_outputs": ["0", "4", "8", "352081045"], "tags": ["fft", "dp", "combinatorics"], "src_uid": "9812de5f2d272511a63ead8765b23190", "difficulty": 3100} {"description": "The map of Bertown can be represented as a set of $$$n$$$ intersections, numbered from $$$1$$$ to $$$n$$$ and connected by $$$m$$$ one-way roads. It is possible to move along the roads from any intersection to any other intersection. The length of some path from one intersection to another is the number of roads that one has to traverse along the path. The shortest path from one intersection $$$v$$$ to another intersection $$$u$$$ is the path that starts in $$$v$$$, ends in $$$u$$$ and has the minimum length among all such paths.Polycarp lives near the intersection $$$s$$$ and works in a building near the intersection $$$t$$$. Every day he gets from $$$s$$$ to $$$t$$$ by car. Today he has chosen the following path to his workplace: $$$p_1$$$, $$$p_2$$$, ..., $$$p_k$$$, where $$$p_1 = s$$$, $$$p_k = t$$$, and all other elements of this sequence are the intermediate intersections, listed in the order Polycarp arrived at them. Polycarp never arrived at the same intersection twice, so all elements of this sequence are pairwise distinct. Note that you know Polycarp's path beforehand (it is fixed), and it is not necessarily one of the shortest paths from $$$s$$$ to $$$t$$$.Polycarp's car has a complex navigation system installed in it. Let's describe how it works. When Polycarp starts his journey at the intersection $$$s$$$, the system chooses some shortest path from $$$s$$$ to $$$t$$$ and shows it to Polycarp. Let's denote the next intersection in the chosen path as $$$v$$$. If Polycarp chooses to drive along the road from $$$s$$$ to $$$v$$$, then the navigator shows him the same shortest path (obviously, starting from $$$v$$$ as soon as he arrives at this intersection). However, if Polycarp chooses to drive to another intersection $$$w$$$ instead, the navigator rebuilds the path: as soon as Polycarp arrives at $$$w$$$, the navigation system chooses some shortest path from $$$w$$$ to $$$t$$$ and shows it to Polycarp. The same process continues until Polycarp arrives at $$$t$$$: if Polycarp moves along the road recommended by the system, it maintains the shortest path it has already built; but if Polycarp chooses some other path, the system rebuilds the path by the same rules.Here is an example. Suppose the map of Bertown looks as follows, and Polycarp drives along the path $$$[1, 2, 3, 4]$$$ ($$$s = 1$$$, $$$t = 4$$$): Check the picture by the link http://tk.codeforces.com/a.png When Polycarp starts at $$$1$$$, the system chooses some shortest path from $$$1$$$ to $$$4$$$. There is only one such path, it is $$$[1, 5, 4]$$$; Polycarp chooses to drive to $$$2$$$, which is not along the path chosen by the system. When Polycarp arrives at $$$2$$$, the navigator rebuilds the path by choosing some shortest path from $$$2$$$ to $$$4$$$, for example, $$$[2, 6, 4]$$$ (note that it could choose $$$[2, 3, 4]$$$); Polycarp chooses to drive to $$$3$$$, which is not along the path chosen by the system. When Polycarp arrives at $$$3$$$, the navigator rebuilds the path by choosing the only shortest path from $$$3$$$ to $$$4$$$, which is $$$[3, 4]$$$; Polycarp arrives at $$$4$$$ along the road chosen by the navigator, so the system does not have to rebuild anything. Overall, we get $$$2$$$ rebuilds in this scenario. Note that if the system chose $$$[2, 3, 4]$$$ instead of $$$[2, 6, 4]$$$ during the second step, there would be only $$$1$$$ rebuild (since Polycarp goes along the path, so the system maintains the path $$$[3, 4]$$$ during the third step).The example shows us that the number of rebuilds can differ even if the map of Bertown and the path chosen by Polycarp stays the same. Given this information (the map and Polycarp's path), can you determine the minimum and the maximum number of rebuilds that could have happened during the journey?", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "512 megabytes", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$2 \\le n \\le m \\le 2 \\cdot 10^5$$$) \u2014 the number of intersections and one-way roads in Bertown, respectively. Then $$$m$$$ lines follow, each describing a road. Each line contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n$$$, $$$u \\ne v$$$) denoting a road from intersection $$$u$$$ to intersection $$$v$$$. All roads in Bertown are pairwise distinct, which means that each ordered pair $$$(u, v)$$$ appears at most once in these $$$m$$$ lines (but if there is a road $$$(u, v)$$$, the road $$$(v, u)$$$ can also appear). The following line contains one integer $$$k$$$ ($$$2 \\le k \\le n$$$) \u2014 the number of intersections in Polycarp's path from home to his workplace. The last line contains $$$k$$$ integers $$$p_1$$$, $$$p_2$$$, ..., $$$p_k$$$ ($$$1 \\le p_i \\le n$$$, all these integers are pairwise distinct) \u2014 the intersections along Polycarp's path in the order he arrived at them. $$$p_1$$$ is the intersection where Polycarp lives ($$$s = p_1$$$), and $$$p_k$$$ is the intersection where Polycarp's workplace is situated ($$$t = p_k$$$). It is guaranteed that for every $$$i \\in [1, k - 1]$$$ the road from $$$p_i$$$ to $$$p_{i + 1}$$$ exists, so the path goes along the roads of Bertown. ", "output_spec": "Print two integers: the minimum and the maximum number of rebuilds that could have happened during the journey.", "notes": null, "sample_inputs": ["6 9\n1 5\n5 4\n1 2\n2 3\n3 4\n4 1\n2 6\n6 4\n4 2\n4\n1 2 3 4", "7 7\n1 2\n2 3\n3 4\n4 5\n5 6\n6 7\n7 1\n7\n1 2 3 4 5 6 7", "8 13\n8 7\n8 6\n7 5\n7 4\n6 5\n6 4\n5 3\n5 2\n4 3\n4 2\n3 1\n2 1\n1 8\n5\n8 7 5 2 1"], "sample_outputs": ["1 2", "0 0", "0 3"], "tags": ["shortest paths", "graphs"], "src_uid": "19a0c05eb2d1559ccfe60e210c6fcd6a", "difficulty": null} {"description": "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 \\not\\equiv 0 \\pmod{10^9+7}$$$. Print the value of $$$P \\cdot Q^{-1}$$$ modulo $$$10^9+7$$$.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "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_spec": "Print a single integer \u2014 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.", "notes": "NoteIn the first example, after one step, the number written on the blackboard is $$$1$$$, $$$2$$$, $$$3$$$ or $$$6$$$ \u2014 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}$$$.", "sample_inputs": ["6 1", "6 2", "60 5"], "sample_outputs": ["3", "875000008", "237178099"], "tags": ["math", "number theory", "probabilities", "dp"], "src_uid": "dc466d9c24b7dcb37c0e99337b4124d2", "difficulty": 2200} {"description": "Let's introduce some definitions that will be needed later.Let $$$prime(x)$$$ be the set of prime divisors of $$$x$$$. For example, $$$prime(140) = \\{ 2, 5, 7 \\}$$$, $$$prime(169) = \\{ 13 \\}$$$.Let $$$g(x, p)$$$ be the maximum possible integer $$$p^k$$$ where $$$k$$$ is an integer such that $$$x$$$ is divisible by $$$p^k$$$. For example: $$$g(45, 3) = 9$$$ ($$$45$$$ is divisible by $$$3^2=9$$$ but not divisible by $$$3^3=27$$$), $$$g(63, 7) = 7$$$ ($$$63$$$ is divisible by $$$7^1=7$$$ but not divisible by $$$7^2=49$$$). Let $$$f(x, y)$$$ be the product of $$$g(y, p)$$$ for all $$$p$$$ in $$$prime(x)$$$. For example: $$$f(30, 70) = g(70, 2) \\cdot g(70, 3) \\cdot g(70, 5) = 2^1 \\cdot 3^0 \\cdot 5^1 = 10$$$, $$$f(525, 63) = g(63, 3) \\cdot g(63, 5) \\cdot g(63, 7) = 3^2 \\cdot 5^0 \\cdot 7^1 = 63$$$. You have integers $$$x$$$ and $$$n$$$. Calculate $$$f(x, 1) \\cdot f(x, 2) \\cdot \\ldots \\cdot f(x, n) \\bmod{(10^{9} + 7)}$$$.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The only line contains integers $$$x$$$ and $$$n$$$ ($$$2 \\le x \\le 10^{9}$$$, $$$1 \\le n \\le 10^{18}$$$)\u00a0\u2014 the numbers used in formula.", "output_spec": "Print the answer.", "notes": "NoteIn the first example, $$$f(10, 1) = g(1, 2) \\cdot g(1, 5) = 1$$$, $$$f(10, 2) = g(2, 2) \\cdot g(2, 5) = 2$$$.In the second example, actual value of formula is approximately $$$1.597 \\cdot 10^{171}$$$. Make sure you print the answer modulo $$$(10^{9} + 7)$$$.In the third example, be careful about overflow issue.", "sample_inputs": ["10 2", "20190929 1605", "947 987654321987654321"], "sample_outputs": ["2", "363165664", "593574252"], "tags": ["math", "number theory"], "src_uid": "04610fbaa746c083dda30e21fa6e1a0c", "difficulty": 1700} {"description": "Anadi has a set of dominoes. Every domino has two parts, and each part contains some dots. For every $$$a$$$ and $$$b$$$ such that $$$1 \\leq a \\leq b \\leq 6$$$, there is exactly one domino with $$$a$$$ dots on one half and $$$b$$$ dots on the other half. The set contains exactly $$$21$$$ dominoes. Here is an exact illustration of his set: Also, Anadi has an undirected graph without self-loops and multiple edges. He wants to choose some dominoes and place them on the edges of this graph. He can use at most one domino of each type. Each edge can fit at most one domino. It's not necessary to place a domino on each edge of the graph.When placing a domino on an edge, he also chooses its direction. In other words, one half of any placed domino must be directed toward one of the endpoints of the edge and the other half must be directed toward the other endpoint. There's a catch: if there are multiple halves of dominoes directed toward the same vertex, each of these halves must contain the same number of dots.How many dominoes at most can Anadi place on the edges of his graph?", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \\leq n \\leq 7$$$, $$$0 \\leq m \\leq \\frac{n\\cdot(n-1)}{2}$$$) \u2014 the number of vertices and the number of edges in the graph. The next $$$m$$$ lines contain two integers each. Integers in the $$$i$$$-th line are $$$a_i$$$ and $$$b_i$$$ ($$$1 \\leq a, b \\leq n$$$, $$$a \\neq b$$$) and denote that there is an edge which connects vertices $$$a_i$$$ and $$$b_i$$$. The graph might be disconnected. It's however guaranteed that the graph doesn't contain any self-loops, and that there is at most one edge between any pair of vertices.", "output_spec": "Output one integer which denotes the maximum number of dominoes which Anadi can place on the edges of the graph.", "notes": "NoteHere is an illustration of Anadi's graph from the first sample test: And here is one of the ways to place a domino on each of its edges: Note that each vertex is faced by the halves of dominoes with the same number of dots. For instance, all halves directed toward vertex $$$1$$$ have three dots.", "sample_inputs": ["4 4\n1 2\n2 3\n3 4\n4 1", "7 0", "3 1\n1 3", "7 21\n1 2\n1 3\n1 4\n1 5\n1 6\n1 7\n2 3\n2 4\n2 5\n2 6\n2 7\n3 4\n3 5\n3 6\n3 7\n4 5\n4 6\n4 7\n5 6\n5 7\n6 7"], "sample_outputs": ["4", "0", "1", "16"], "tags": ["brute force", "graphs"], "src_uid": "11e6559cfb71b8f6ca88242094b17a2b", "difficulty": 1700} {"description": "Melody Pond was stolen from her parents as a newborn baby by Madame Kovarian, to become a weapon of the Silence in their crusade against the Doctor. Madame Kovarian changed Melody's name to River Song, giving her a new identity that allowed her to kill the Eleventh Doctor.Heidi figured out that Madame Kovarian uses a very complicated hashing function in order to change the names of the babies she steals. In order to prevent this from happening to future Doctors, Heidi decided to prepare herself by learning some basic hashing techniques.The first hashing function she designed is as follows.Given two positive integers $$$(x, y)$$$ she defines $$$H(x,y):=x^2+2xy+x+1$$$.Now, Heidi wonders if the function is reversible. That is, given a positive integer $$$r$$$, can you find a pair $$$(x, y)$$$ (of positive integers) such that $$$H(x, y) = r$$$?If multiple such pairs exist, output the one with smallest possible $$$x$$$. If there is no such pair, output \"NO\".", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first and only line contains an integer $$$r$$$ ($$$1 \\le r \\le 10^{12}$$$).", "output_spec": "Output integers $$$x, y$$$ such that $$$H(x,y) = r$$$ and $$$x$$$ is smallest possible, or \"NO\" if no such pair exists.", "notes": null, "sample_inputs": ["19", "16"], "sample_outputs": ["1 8", "NO"], "tags": ["brute force", "math", "number theory"], "src_uid": "3ff1c25a1026c90aeb14d148d7fb96ba", "difficulty": 1200} {"description": "You are given a regular polygon with $$$n$$$ vertices labeled from $$$1$$$ to $$$n$$$ in counter-clockwise order. The triangulation of a given polygon is a set of triangles such that each vertex of each triangle is a vertex of the initial polygon, there is no pair of triangles such that their intersection has non-zero area, and the total area of all triangles is equal to the area of the given polygon. The weight of a triangulation is the sum of weigths of triangles it consists of, where the weight of a triagle is denoted as the product of labels of its vertices.Calculate the minimum weight among all triangulations of the polygon.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains single integer $$$n$$$ ($$$3 \\le n \\le 500$$$) \u2014 the number of vertices in the regular polygon.", "output_spec": "Print one integer \u2014 the minimum weight among all triangulations of the given polygon.", "notes": "NoteAccording to Wiki: polygon triangulation is the decomposition of a polygonal area (simple polygon) $$$P$$$ into a set of triangles, i.\u2009e., finding a set of triangles with pairwise non-intersecting interiors whose union is $$$P$$$.In the first example the polygon is a triangle, so we don't need to cut it further, so the answer is $$$1 \\cdot 2 \\cdot 3 = 6$$$.In the second example the polygon is a rectangle, so it should be divided into two triangles. It's optimal to cut it using diagonal $$$1-3$$$ so answer is $$$1 \\cdot 2 \\cdot 3 + 1 \\cdot 3 \\cdot 4 = 6 + 12 = 18$$$.", "sample_inputs": ["3", "4"], "sample_outputs": ["6", "18"], "tags": ["dp", "greedy", "math"], "src_uid": "1bd29d7a8793c22e81a1f6fd3991307a", "difficulty": 1200} {"description": "Welcome to Codeforces Stock Exchange! We're pretty limited now as we currently allow trading on one stock, Codeforces Ltd. We hope you'll still be able to make profit from the market!In the morning, there are $$$n$$$ opportunities to buy shares. The $$$i$$$-th of them allows to buy as many shares as you want, each at the price of $$$s_i$$$ bourles.In the evening, there are $$$m$$$ opportunities to sell shares. The $$$i$$$-th of them allows to sell as many shares as you want, each at the price of $$$b_i$$$ bourles. You can't sell more shares than you have.It's morning now and you possess $$$r$$$ bourles and no shares.What is the maximum number of bourles you can hold after the evening?", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line of the input contains three integers $$$n, m, r$$$ ($$$1 \\leq n \\leq 30$$$, $$$1 \\leq m \\leq 30$$$, $$$1 \\leq r \\leq 1000$$$) \u2014 the number of ways to buy the shares on the market, the number of ways to sell the shares on the market, and the number of bourles you hold now. The next line contains $$$n$$$ integers $$$s_1, s_2, \\dots, s_n$$$ ($$$1 \\leq s_i \\leq 1000$$$); $$$s_i$$$ indicates the opportunity to buy shares at the price of $$$s_i$$$ bourles. The following line contains $$$m$$$ integers $$$b_1, b_2, \\dots, b_m$$$ ($$$1 \\leq b_i \\leq 1000$$$); $$$b_i$$$ indicates the opportunity to sell shares at the price of $$$b_i$$$ bourles.", "output_spec": "Output a single integer \u2014 the maximum number of bourles you can hold after the evening.", "notes": "NoteIn the first example test, you have $$$11$$$ bourles in the morning. It's optimal to buy $$$5$$$ shares of a stock at the price of $$$2$$$ bourles in the morning, and then to sell all of them at the price of $$$5$$$ bourles in the evening. It's easy to verify that you'll have $$$26$$$ bourles after the evening.In the second example test, it's optimal not to take any action.", "sample_inputs": ["3 4 11\n4 2 5\n4 4 5 4", "2 2 50\n5 7\n4 2"], "sample_outputs": ["26", "50"], "tags": ["greedy", "implementation"], "src_uid": "42f25d492bddc12d3d89d39315d63cb9", "difficulty": 800} {"description": "Getting closer and closer to a mathematician, Serval becomes a university student on math major in Japari University. On the Calculus class, his teacher taught him how to calculate the expected length of a random subsegment of a given segment. Then he left a bonus problem as homework, with the award of a garage kit from IOI. The bonus is to extend this problem to the general case as follows.You are given a segment with length $$$l$$$. We randomly choose $$$n$$$ segments by choosing two points (maybe with non-integer coordinates) from the given segment equiprobably and the interval between the two points forms a segment. You are given the number of random segments $$$n$$$, and another integer $$$k$$$. The $$$2n$$$ endpoints of the chosen segments split the segment into $$$(2n+1)$$$ intervals. Your task is to calculate the expected total length of those intervals that are covered by at least $$$k$$$ segments of the $$$n$$$ random segments.You should find the answer modulo $$$998244353$$$.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "First line contains three space-separated positive integers $$$n$$$, $$$k$$$ and $$$l$$$ ($$$1\\leq k \\leq n \\leq 2000$$$, $$$1\\leq l\\leq 10^9$$$).", "output_spec": "Output one integer\u00a0\u2014 the expected total length of all the intervals covered by at least $$$k$$$ segments of the $$$n$$$ random segments modulo $$$998244353$$$. Formally, let $$$M = 998244353$$$. 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 \\not \\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}$$$.", "notes": "NoteIn the first example, the expected total length is $$$\\int_0^1 \\int_0^1 |x-y| \\,\\mathrm{d}x\\,\\mathrm{d}y = {1\\over 3}$$$, and $$$3^{-1}$$$ modulo $$$998244353$$$ is $$$332748118$$$.", "sample_inputs": ["1 1 1", "6 2 1", "7 5 3", "97 31 9984524"], "sample_outputs": ["332748118", "760234711", "223383352", "267137618"], "tags": ["math", "probabilities", "combinatorics", "dp"], "src_uid": "c9e79e83928d5d034123ebc3b2f5e064", "difficulty": 2600} {"description": "Young Teodor enjoys drawing. His favourite hobby is drawing segments with integer borders inside his huge [1;m] segment. One day Teodor noticed that picture he just drawn has one interesting feature: there doesn't exist an integer point, that belongs each of segments in the picture. Having discovered this fact, Teodor decided to share it with Sasha.Sasha knows that Teodor likes to show off so he never trusts him. Teodor wants to prove that he can be trusted sometimes, so he decided to convince Sasha that there is no such integer point in his picture, which belongs to each segment. However Teodor is lazy person and neither wills to tell Sasha all coordinates of segments' ends nor wills to tell him their amount, so he suggested Sasha to ask him series of questions 'Given the integer point xi, how many segments in Fedya's picture contain that point?', promising to tell correct answers for this questions.Both boys are very busy studying and don't have much time, so they ask you to find out how many questions can Sasha ask Teodor, that having only answers on his questions, Sasha can't be sure that Teodor isn't lying to him. Note that Sasha doesn't know amount of segments in Teodor's picture. Sure, Sasha is smart person and never asks about same point twice.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "First line of input contains two integer numbers: n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009100\u2009000)\u00a0\u2014 amount of segments of Teodor's picture and maximal coordinate of point that Sasha can ask about. ith of next n lines contains two integer numbers li and ri (1\u2009\u2264\u2009li\u2009\u2264\u2009ri\u2009\u2264\u2009m)\u00a0\u2014 left and right ends of ith segment in the picture. Note that that left and right ends of segment can be the same point. It is guaranteed that there is no integer point, that belongs to all segments.", "output_spec": "Single line of output should contain one integer number k \u2013 size of largest set (xi,\u2009cnt(xi)) where all xi are different, 1\u2009\u2264\u2009xi\u2009\u2264\u2009m, and cnt(xi) is amount of segments, containing point with coordinate xi, such that one can't be sure that there doesn't exist point, belonging to all of segments in initial picture, if he knows only this set(and doesn't know n).", "notes": "NoteFirst example shows situation where Sasha can never be sure that Teodor isn't lying to him, because even if one knows cnt(xi) for each point in segment [1;4], he can't distinguish this case from situation Teodor has drawn whole [1;4] segment.In second example Sasha can ask about 5 points e.g. 1,\u20092,\u20093,\u20095,\u20096, still not being sure if Teodor haven't lied to him. But once he knows information about all points in [1;6] segment, Sasha can be sure that Teodor haven't lied to him.", "sample_inputs": ["2 4\n1 2\n3 4", "4 6\n1 3\n2 3\n4 6\n5 6"], "sample_outputs": ["4", "5"], "tags": ["data structures", "dp", "binary search"], "src_uid": "ce8350be138ce2061349d7f9224a5aaf", "difficulty": 1900} {"description": "You are given an integer N. Consider all possible segments on the coordinate axis with endpoints at integer points with coordinates between 0 and N, inclusive; there will be of them.You want to draw these segments in several layers so that in each layer the segments don't overlap (they might touch at the endpoints though). You can not move the segments to a different location on the coordinate axis. Find the minimal number of layers you have to use for the given N.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The only input line contains a single integer N (1\u2009\u2264\u2009N\u2009\u2264\u2009100).", "output_spec": "Output a single integer - the minimal number of layers required to draw the segments for the given N.", "notes": "NoteAs an example, here are the segments and their optimal arrangement into layers for N\u2009=\u20094. ", "sample_inputs": ["2", "3", "4"], "sample_outputs": ["2", "4", "6"], "tags": ["math", "constructive algorithms"], "src_uid": "f8af5dfcf841a7f105ac4c144eb51319", "difficulty": 1300} {"description": "Everybody knows of spaghetti sort. You decided to implement an analog sorting algorithm yourself, but as you survey your pantry you realize you're out of spaghetti! The only type of pasta you have is ravioli, but you are not going to let this stop you...You come up with the following algorithm. For each number in the array ai, build a stack of ai ravioli. The image shows the stack for ai\u2009=\u20094. Arrange the stacks in one row in the order in which the corresponding numbers appear in the input array. Find the tallest one (if there are several stacks of maximal height, use the leftmost one). Remove it and add its height to the end of the output array. Shift the stacks in the row so that there is no gap between them. Repeat the procedure until all stacks have been removed.At first you are very happy with your algorithm, but as you try it on more inputs you realize that it doesn't always produce the right sorted array. Turns out when two stacks of ravioli are next to each other (at any step of the process) and differ in height by two or more, the top ravioli of the taller stack slides down on top of the lower stack.Given an input array, figure out whether the described algorithm will sort it correctly.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line of input contains a single number n (1\u2009\u2264\u2009n\u2009\u2264\u200910) \u2014 the size of the array. The second line of input contains n space-separated integers ai (1\u2009\u2264\u2009ai\u2009\u2264\u2009100) \u2014 the elements of the array.", "output_spec": "Output \"YES\" if the array can be sorted using the described procedure and \"NO\" if it can not.", "notes": "NoteIn the second example the array will change even before the tallest stack is chosen for the first time: ravioli from stack of height 3 will slide on the stack of height 1, and the algorithm will output an array {2,\u20092,\u20092}.", "sample_inputs": ["3\n1 2 3", "3\n3 1 2"], "sample_outputs": ["YES", "NO"], "tags": ["implementation"], "src_uid": "704d0ae50bccaa8bc49319812ae0be45", "difficulty": 1600} {"description": "Given an integer $$$x$$$. Your task is to find out how many positive integers $$$n$$$ ($$$1 \\leq n \\leq x$$$) satisfy $$$$$$n \\cdot a^n \\equiv b \\quad (\\textrm{mod}\\;p),$$$$$$ where $$$a, b, p$$$ are all known constants.", "input_from": "standard input", "output_to": "standard output", "time_limit": "3 seconds", "memory_limit": "256 megabytes", "input_spec": "The only line contains four integers $$$a,b,p,x$$$ ($$$2 \\leq p \\leq 10^6+3$$$, $$$1 \\leq a,b < p$$$, $$$1 \\leq x \\leq 10^{12}$$$). It is guaranteed that $$$p$$$ is a prime.", "output_spec": "Print a single integer: the number of possible answers $$$n$$$.", "notes": "NoteIn the first sample, we can see that $$$n=2$$$ and $$$n=8$$$ are possible answers.", "sample_inputs": ["2 3 5 8", "4 6 7 13", "233 233 10007 1"], "sample_outputs": ["2", "1", "1"], "tags": ["math", "chinese remainder theorem", "number theory"], "src_uid": "4b9f470e5889da29affae6376f6c9f6a", "difficulty": 2100} {"description": "Imp is in a magic forest, where xorangles grow (wut?) A xorangle of order n is such a non-degenerate triangle, that lengths of its sides are integers not exceeding n, and the xor-sum of the lengths is equal to zero. Imp has to count the number of distinct xorangles of order n to get out of the forest. Formally, for a given integer n you have to find the number of such triples (a,\u2009b,\u2009c), that: 1\u2009\u2264\u2009a\u2009\u2264\u2009b\u2009\u2264\u2009c\u2009\u2264\u2009n; , where denotes the bitwise xor of integers x and y. (a,\u2009b,\u2009c) form a non-degenerate (with strictly positive area) triangle. ", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The only line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u20092500).", "output_spec": "Print the number of xorangles of order n.", "notes": "NoteThe only xorangle in the first sample is (3,\u20095,\u20096).", "sample_inputs": ["6", "10"], "sample_outputs": ["1", "2"], "tags": ["brute force"], "src_uid": "838f2e75fdff0f13f002c0dfff0b2e8d", "difficulty": 1300} {"description": "Mahmoud and Ehab play a game called the even-odd game. Ehab chooses his favorite integer n and then they take turns, starting from Mahmoud. In each player's turn, he has to choose an integer a and subtract it from n such that: 1\u2009\u2264\u2009a\u2009\u2264\u2009n. If it's Mahmoud's turn, a has to be even, but if it's Ehab's turn, a has to be odd. If the current player can't choose any number satisfying the conditions, he loses. Can you determine the winner if they both play optimally?", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The only line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009109), the number at the beginning of the game.", "output_spec": "Output \"Mahmoud\" (without quotes) if Mahmoud wins and \"Ehab\" (without quotes) otherwise.", "notes": "NoteIn the first sample, Mahmoud can't choose any integer a initially because there is no positive even integer less than or equal to 1 so Ehab wins.In the second sample, Mahmoud has to choose a\u2009=\u20092 and subtract it from n. It's Ehab's turn and n\u2009=\u20090. There is no positive odd integer less than or equal to 0 so Mahmoud wins.", "sample_inputs": ["1", "2"], "sample_outputs": ["Ehab", "Mahmoud"], "tags": ["games", "math"], "src_uid": "5e74750f44142624e6da41d4b35beb9a", "difficulty": 800} {"description": "Katie, Kuro and Shiro are best friends. They have known each other since kindergarten. That's why they often share everything with each other and work together on some very hard problems.Today is Shiro's birthday. She really loves pizza so she wants to invite her friends to the pizza restaurant near her house to celebrate her birthday, including her best friends Katie and Kuro.She has ordered a very big round pizza, in order to serve her many friends. Exactly $$$n$$$ of Shiro's friends are here. That's why she has to divide the pizza into $$$n + 1$$$ slices (Shiro also needs to eat). She wants the slices to be exactly the same size and shape. If not, some of her friends will get mad and go home early, and the party will be over.Shiro is now hungry. She wants to cut the pizza with minimum of straight cuts. A cut is a straight segment, it might have ends inside or outside the pizza. But she is too lazy to pick up the calculator.As usual, she will ask Katie and Kuro for help. But they haven't come yet. Could you help Shiro with this problem?", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "128 megabytes", "input_spec": "A single line contains one non-negative integer $$$n$$$ ($$$0 \\le n \\leq 10^{18}$$$)\u00a0\u2014 the number of Shiro's friends. The circular pizza has to be sliced into $$$n + 1$$$ pieces.", "output_spec": "A single integer\u00a0\u2014 the number of straight cuts Shiro needs.", "notes": "NoteTo cut the round pizza into quarters one has to make two cuts through the center with angle $$$90^{\\circ}$$$ between them.To cut the round pizza into five equal parts one has to make five cuts.", "sample_inputs": ["3", "4"], "sample_outputs": ["2", "5"], "tags": ["math"], "src_uid": "236177ff30dafe68295b5d33dc501828", "difficulty": 1000} {"description": "Today on Informatics class Nastya learned about GCD and LCM (see links below). Nastya is very intelligent, so she solved all the tasks momentarily and now suggests you to solve one of them as well.We define a pair of integers (a,\u2009b) good, if GCD(a,\u2009b)\u2009=\u2009x and LCM(a,\u2009b)\u2009=\u2009y, where GCD(a,\u2009b) denotes the greatest common divisor of a and b, and LCM(a,\u2009b) denotes the least common multiple of a and b.You are given two integers x and y. You are to find the number of good pairs of integers (a,\u2009b) such that l\u2009\u2264\u2009a,\u2009b\u2009\u2264\u2009r. Note that pairs (a,\u2009b) and (b,\u2009a) are considered different if a\u2009\u2260\u2009b.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The only line contains four integers l,\u2009r,\u2009x,\u2009y (1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009109, 1\u2009\u2264\u2009x\u2009\u2264\u2009y\u2009\u2264\u2009109).", "output_spec": "In the only line print the only integer\u00a0\u2014 the answer for the problem.", "notes": "NoteIn the first example there are two suitable good pairs of integers (a,\u2009b): (1,\u20092) and (2,\u20091).In the second example there are four suitable good pairs of integers (a,\u2009b): (1,\u200912), (12,\u20091), (3,\u20094) and (4,\u20093).In the third example there are good pairs of integers, for example, (3,\u200930), but none of them fits the condition l\u2009\u2264\u2009a,\u2009b\u2009\u2264\u2009r.", "sample_inputs": ["1 2 1 2", "1 12 1 12", "50 100 3 30"], "sample_outputs": ["2", "4", "0"], "tags": ["math", "number theory"], "src_uid": "d37dde5841116352c9b37538631d0b15", "difficulty": 1600} {"description": "Vasya has got a tree consisting of $$$n$$$ vertices. He wants to delete some (possibly zero) edges in this tree such that the maximum matching in the resulting graph is unique. He asks you to calculate the number of ways to choose a set of edges to remove.A matching in the graph is a subset of its edges such that there is no vertex incident to two (or more) edges from the subset. A maximum matching is a matching such that the number of edges in the subset is maximum possible among all matchings in this graph.Since the answer may be large, output it modulo $$$998244353$$$.", "input_from": "standard input", "output_to": "standard output", "time_limit": "3 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains one integer $$$n$$$ ($$$1 \\le n \\le 3 \\cdot 10^5$$$) \u2014 the number of vertices in the tree. Each of the next $$$n \u2212 1$$$ lines contains two integers $$$u$$$ and $$$v$$$ ($$$1 \\le u, v \\le n, u \\neq v$$$) denoting an edge between vertex $$$u$$$ and vertex $$$v$$$. It is guaranteed that these edges form a tree.", "output_spec": "Print one integer \u2014 the number of ways to delete some (possibly empty) subset of edges so that the maximum matching in the resulting graph is unique. Print the answer modulo $$$998244353$$$.", "notes": "NotePossible ways to delete edges in the first example: delete $$$(1, 2)$$$ and $$$(1, 3)$$$. delete $$$(1, 2)$$$ and $$$(1, 4)$$$. delete $$$(1, 3)$$$ and $$$(1, 4)$$$. delete all edges. Possible ways to delete edges in the second example: delete no edges. delete $$$(1, 2)$$$ and $$$(2, 3)$$$. delete $$$(1, 2)$$$ and $$$(3, 4)$$$. delete $$$(2, 3)$$$ and $$$(3, 4)$$$. delete $$$(2, 3)$$$. delete all edges. ", "sample_inputs": ["4\n1 2\n1 3\n1 4", "4\n1 2\n2 3\n3 4", "1"], "sample_outputs": ["4", "6", "1"], "tags": ["dp", "trees"], "src_uid": "a40e78a7144ac2fae1890ac7598990bf", "difficulty": 2400} {"description": "You are given a binary string $$$s$$$.Find the number of distinct cyclical binary strings of length $$$n$$$ which contain $$$s$$$ as a substring.The cyclical string $$$t$$$ contains $$$s$$$ as a substring if there is some cyclical shift of string $$$t$$$, such that $$$s$$$ is a substring of this cyclical shift of $$$t$$$.For example, the cyclical string \"000111\" contains substrings \"001\", \"01110\" and \"10\", but doesn't contain \"0110\" and \"10110\".Two cyclical strings are called different if they differ from each other as strings. For example, two different strings, which differ from each other by a cyclical shift, are still considered different cyclical strings.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains a single integer $$$n$$$ ($$$1 \\le n \\le 40$$$)\u00a0\u2014 the length of the target string $$$t$$$. The next line contains the string $$$s$$$ ($$$1 \\le |s| \\le n$$$)\u00a0\u2014 the string which must be a substring of cyclical string $$$t$$$. String $$$s$$$ contains only characters '0' and '1'.", "output_spec": "Print the only integer\u00a0\u2014 the number of distinct cyclical binary strings $$$t$$$, which contain $$$s$$$ as a substring.", "notes": "NoteIn the first example, there are three cyclical strings, which contain \"0\"\u00a0\u2014 \"00\", \"01\" and \"10\".In the second example, there are only two such strings\u00a0\u2014 \"1010\", \"0101\".", "sample_inputs": ["2\n0", "4\n1010", "20\n10101010101010"], "sample_outputs": ["3", "2", "962"], "tags": ["dp", "strings"], "src_uid": "0034806908c9794086736a2d07fc654c", "difficulty": 2900} {"description": "Masha has three sticks of length $$$a$$$, $$$b$$$ and $$$c$$$ centimeters respectively. In one minute Masha can pick one arbitrary stick and increase its length by one centimeter. She is not allowed to break sticks.What is the minimum number of minutes she needs to spend increasing the stick's length in order to be able to assemble a triangle of positive area. Sticks should be used as triangle's sides (one stick for one side) and their endpoints should be located at triangle's vertices.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The only line contains tree integers $$$a$$$, $$$b$$$ and $$$c$$$ ($$$1 \\leq a, b, c \\leq 100$$$)\u00a0\u2014 the lengths of sticks Masha possesses.", "output_spec": "Print a single integer\u00a0\u2014 the minimum number of minutes that Masha needs to spend in order to be able to make the triangle of positive area from her sticks.", "notes": "NoteIn the first example, Masha can make a triangle from the sticks without increasing the length of any of them.In the second example, Masha can't make a triangle of positive area from the sticks she has at the beginning, but she can spend one minute to increase the length $$$2$$$ centimeter stick by one and after that form a triangle with sides $$$3$$$, $$$3$$$ and $$$5$$$ centimeters.In the third example, Masha can take $$$33$$$ minutes to increase one of the $$$10$$$ centimeters sticks by $$$33$$$ centimeters, and after that take $$$48$$$ minutes to increase another $$$10$$$ centimeters stick by $$$48$$$ centimeters. This way she can form a triangle with lengths $$$43$$$, $$$58$$$ and $$$100$$$ centimeters in $$$81$$$ minutes. One can show that it is impossible to get a valid triangle faster.", "sample_inputs": ["3 4 5", "2 5 3", "100 10 10"], "sample_outputs": ["0", "1", "81"], "tags": ["geometry", "brute force", "math"], "src_uid": "3dc56bc08606a39dd9ca40a43c452f09", "difficulty": 800} {"description": "Hasan loves playing games and has recently discovered a game called TopScore. In this soccer-like game there are $$$p$$$ players doing penalty shoot-outs. Winner is the one who scores the most. In case of ties, one of the top-scorers will be declared as the winner randomly with equal probability.They have just finished the game and now are waiting for the result. But there's a tiny problem! The judges have lost the paper of scores! Fortunately they have calculated sum of the scores before they get lost and also for some of the players they have remembered a lower bound on how much they scored. However, the information about the bounds is private, so Hasan only got to know his bound.According to the available data, he knows that his score is at least $$$r$$$ and sum of the scores is $$$s$$$.Thus the final state of the game can be represented in form of sequence of $$$p$$$ integers $$$a_1, a_2, \\dots, a_p$$$ ($$$0 \\le a_i$$$) \u2014 player's scores. Hasan is player number $$$1$$$, so $$$a_1 \\ge r$$$. Also $$$a_1 + a_2 + \\dots + a_p = s$$$. Two states are considered different if there exists some position $$$i$$$ such that the value of $$$a_i$$$ differs in these states. Once again, Hasan doesn't know the exact scores (he doesn't know his exact score as well). So he considers each of the final states to be equally probable to achieve.Help Hasan find the probability of him winning.It can be shown that it is in the form of $$$\\frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are non-negative integers and $$$Q \\ne 0$$$, $$$P \\le Q$$$. Report the value of $$$P \\cdot Q^{-1} \\pmod {998244353}$$$.", "input_from": "standard input", "output_to": "standard output", "time_limit": "3 seconds", "memory_limit": "256 megabytes", "input_spec": "The only line contains three integers $$$p$$$, $$$s$$$ and $$$r$$$ ($$$1 \\le p \\le 100$$$, $$$0 \\le r \\le s \\le 5000$$$) \u2014 the number of players, the sum of scores of all players and Hasan's score, respectively.", "output_spec": "Print a single integer \u2014 the probability of Hasan winning. It can be shown that it is in the form of $$$\\frac{P}{Q}$$$ where $$$P$$$ and $$$Q$$$ are non-negative integers and $$$Q \\ne 0$$$, $$$P \\le Q$$$. Report the value of $$$P \\cdot Q^{-1} \\pmod {998244353}$$$.", "notes": "NoteIn the first example Hasan can score $$$3$$$, $$$4$$$, $$$5$$$ or $$$6$$$ goals. If he scores $$$4$$$ goals or more than he scores strictly more than his only opponent. If he scores $$$3$$$ then his opponent also scores $$$3$$$ and Hasan has a probability of $$$\\frac 1 2$$$ to win the game. Thus, overall he has the probability of $$$\\frac 7 8$$$ to win.In the second example even Hasan's lower bound on goal implies him scoring more than any of his opponents. Thus, the resulting probability is $$$1$$$.", "sample_inputs": ["2 6 3", "5 20 11", "10 30 10"], "sample_outputs": ["124780545", "1", "85932500"], "tags": ["math", "probabilities", "combinatorics", "dp"], "src_uid": "609195ef4a970c62a8210dafe118580e", "difficulty": 2500} {"description": "Hongcow is learning to spell! One day, his teacher gives him a word that he needs to learn to spell. Being a dutiful student, he immediately learns how to spell the word.Hongcow has decided to try to make new words from this one. He starts by taking the word he just learned how to spell, and moves the last character of the word to the beginning of the word. He calls this a cyclic shift. He can apply cyclic shift many times. For example, consecutively applying cyclic shift operation to the word \"abracadabra\" Hongcow will get words \"aabracadabr\", \"raabracadab\" and so on.Hongcow is now wondering how many distinct words he can generate by doing the cyclic shift arbitrarily many times. The initial string is also counted.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line of input will be a single string s (1\u2009\u2264\u2009|s|\u2009\u2264\u200950), the word Hongcow initially learns how to spell. The string s consists only of lowercase English letters ('a'\u2013'z').", "output_spec": "Output a single integer equal to the number of distinct strings that Hongcow can obtain by applying the cyclic shift arbitrarily many times to the given string.", "notes": "NoteFor the first sample, the strings Hongcow can generate are \"abcd\", \"dabc\", \"cdab\", and \"bcda\".For the second sample, no matter how many times Hongcow does the cyclic shift, Hongcow can only generate \"bbb\".For the third sample, the two strings Hongcow can generate are \"yzyz\" and \"zyzy\".", "sample_inputs": ["abcd", "bbb", "yzyz"], "sample_outputs": ["4", "1", "2"], "tags": ["strings", "implementation"], "src_uid": "8909ac99ed4ab2ee4d681ec864c7831e", "difficulty": 900} {"description": "One day Kefa found n baloons. For convenience, we denote color of i-th baloon as si \u2014 lowercase letter of the Latin alphabet. Also Kefa has k friends. Friend will be upset, If he get two baloons of the same color. Kefa want to give out all baloons to his friends. Help Kefa to find out, can he give out all his baloons, such that no one of his friens will be upset \u2014 print \u00abYES\u00bb, if he can, and \u00abNO\u00bb, otherwise. Note, that Kefa's friend will not upset, if he doesn't get baloons at all.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers n and k (1\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009100) \u2014 the number of baloons and friends. Next line contains string s \u2014 colors of baloons.", "output_spec": "Answer to the task \u2014 \u00abYES\u00bb or \u00abNO\u00bb in a single line. You can choose the case (lower or upper) for each letter arbitrary.", "notes": "NoteIn the first sample Kefa can give 1-st and 3-rd baloon to the first friend, and 2-nd and 4-th to the second.In the second sample Kefa needs to give to all his friends baloons of color a, but one baloon will stay, thats why answer is \u00abNO\u00bb.", "sample_inputs": ["4 2\naabb", "6 3\naacaab"], "sample_outputs": ["YES", "NO"], "tags": ["brute force", "implementation"], "src_uid": "ceb3807aaffef60bcdbcc9a17a1391be", "difficulty": 900} {"description": "The Floral Clock has been standing by the side of Mirror Lake for years. Though unable to keep time, it reminds people of the passage of time and the good old days.On the rim of the Floral Clock are 2n flowers, numbered from 1 to 2n clockwise, each of which has a colour among all n possible ones. For each colour, there are exactly two flowers with it, the distance between which either is less than or equal to 2, or equals n. Additionally, if flowers u and v are of the same colour, then flowers opposite to u and opposite to v should be of the same colour as well \u2014 symmetry is beautiful!Formally, the distance between two flowers is 1 plus the number of flowers on the minor arc (or semicircle) between them. Below is a possible arrangement with n\u2009=\u20096 that cover all possibilities. The beauty of an arrangement is defined to be the product of the lengths of flower segments separated by all opposite flowers of the same colour. In other words, in order to compute the beauty, we remove from the circle all flowers that have the same colour as flowers opposite to them. Then, the beauty is the product of lengths of all remaining segments. Note that we include segments of length 0 in this product. If there are no flowers that have the same colour as flower opposite to them, the beauty equals 0. For instance, the beauty of the above arrangement equals 1\u2009\u00d7\u20093\u2009\u00d7\u20091\u2009\u00d7\u20093\u2009=\u20099 \u2014 the segments are {2}, {4,\u20095,\u20096}, {8} and {10,\u200911,\u200912}.While keeping the constraints satisfied, there may be lots of different arrangements. Find out the sum of beauty over all possible arrangements, modulo 998\u2009244\u2009353. Two arrangements are considered different, if a pair (u,\u2009v) (1\u2009\u2264\u2009u,\u2009v\u2009\u2264\u20092n) exists such that flowers u and v are of the same colour in one of them, but not in the other.", "input_from": "standard input", "output_to": "standard output", "time_limit": "7 seconds", "memory_limit": "256 megabytes", "input_spec": "The first and only line of input contains a lonely positive integer n (3\u2009\u2264\u2009n\u2009\u2264\u200950\u2009000)\u00a0\u2014 the number of colours present on the Floral Clock.", "output_spec": "Output one integer \u2014 the sum of beauty over all possible arrangements of flowers, modulo 998\u2009244\u2009353.", "notes": "NoteWith n\u2009=\u20093, the following six arrangements each have a beauty of 2\u2009\u00d7\u20092\u2009=\u20094. While many others, such as the left one in the figure below, have a beauty of 0. The right one is invalid, since it's asymmetric. ", "sample_inputs": ["3", "4", "7", "15"], "sample_outputs": ["24", "4", "1316", "3436404"], "tags": ["divide and conquer", "combinatorics", "fft", "dp", "math"], "src_uid": "24fd5cd218f65d4ffb7c5b97b725293e", "difficulty": 3400} {"description": "Where do odds begin, and where do they end? Where does hope emerge, and will they ever break?Given an integer sequence a1,\u2009a2,\u2009...,\u2009an of length n. Decide whether it is possible to divide it into an odd number of non-empty subsegments, the each of which has an odd length and begins and ends with odd numbers.A subsegment is a contiguous slice of the whole sequence. For example, {3,\u20094,\u20095} and {1} are subsegments of sequence {1,\u20092,\u20093,\u20094,\u20095,\u20096}, while {1,\u20092,\u20094} and {7} are not.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line of input contains a non-negative integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100) \u2014 the length of the sequence. The second line contains n space-separated non-negative integers a1,\u2009a2,\u2009...,\u2009an (0\u2009\u2264\u2009ai\u2009\u2264\u2009100) \u2014 the elements of the sequence.", "output_spec": "Output \"Yes\" if it's possible to fulfill the requirements, and \"No\" otherwise. You can output each letter in any case (upper or lower).", "notes": "NoteIn the first example, divide the sequence into 1 subsegment: {1,\u20093,\u20095} and the requirements will be met.In the second example, divide the sequence into 3 subsegments: {1,\u20090,\u20091}, {5}, {1}.In the third example, one of the subsegments must start with 4 which is an even number, thus the requirements cannot be met.In the fourth example, the sequence can be divided into 2 subsegments: {3,\u20099,\u20099}, {3}, but this is not a valid solution because 2 is an even number.", "sample_inputs": ["3\n1 3 5", "5\n1 0 1 5 1", "3\n4 3 1", "4\n3 9 9 3"], "sample_outputs": ["Yes", "Yes", "No", "No"], "tags": ["implementation"], "src_uid": "2b8c2deb5d7e49e8e3ededabfd4427db", "difficulty": 1000} {"description": "Nikolay has a lemons, b apples and c pears. He decided to cook a compote. According to the recipe the fruits should be in the ratio 1:\u20092:\u20094. It means that for each lemon in the compote should be exactly 2 apples and exactly 4 pears. You can't crumble up, break up or cut these fruits into pieces. These fruits\u00a0\u2014 lemons, apples and pears\u00a0\u2014 should be put in the compote as whole fruits.Your task is to determine the maximum total number of lemons, apples and pears from which Nikolay can cook the compote. It is possible that Nikolay can't use any fruits, in this case print 0. ", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains the positive integer a (1\u2009\u2264\u2009a\u2009\u2264\u20091000)\u00a0\u2014 the number of lemons Nikolay has. The second line contains the positive integer b (1\u2009\u2264\u2009b\u2009\u2264\u20091000)\u00a0\u2014 the number of apples Nikolay has. The third line contains the positive integer c (1\u2009\u2264\u2009c\u2009\u2264\u20091000)\u00a0\u2014 the number of pears Nikolay has.", "output_spec": "Print the maximum total number of lemons, apples and pears from which Nikolay can cook the compote.", "notes": "NoteIn the first example Nikolay can use 1 lemon, 2 apples and 4 pears, so the answer is 1\u2009+\u20092\u2009+\u20094\u2009=\u20097.In the second example Nikolay can use 3 lemons, 6 apples and 12 pears, so the answer is 3\u2009+\u20096\u2009+\u200912\u2009=\u200921.In the third example Nikolay don't have enough pears to cook any compote, so the answer is 0. ", "sample_inputs": ["2\n5\n7", "4\n7\n13", "2\n3\n2"], "sample_outputs": ["7", "21", "0"], "tags": ["math", "implementation"], "src_uid": "82a4a60eac90765fb62f2a77d2305c01", "difficulty": 800} {"description": "A big company decided to launch a new series of rectangular displays, and decided that the display must have exactly n pixels. Your task is to determine the size of the rectangular display \u2014 the number of lines (rows) of pixels a and the number of columns of pixels b, so that: there are exactly n pixels on the display; the number of rows does not exceed the number of columns, it means a\u2009\u2264\u2009b; the difference b\u2009-\u2009a is as small as possible. ", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains the positive integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009106)\u00a0\u2014 the number of pixels display should have.", "output_spec": "Print two integers\u00a0\u2014 the number of rows and columns on the display. ", "notes": "NoteIn the first example the minimum possible difference equals 2, so on the display should be 2 rows of 4 pixels.In the second example the minimum possible difference equals 0, so on the display should be 8 rows of 8 pixels.In the third example the minimum possible difference equals 4, so on the display should be 1 row of 5 pixels.", "sample_inputs": ["8", "64", "5", "999999"], "sample_outputs": ["2 4", "8 8", "1 5", "999 1001"], "tags": ["brute force", "math"], "src_uid": "f52af273954798a4ae38a1378bfbf77a", "difficulty": 800} {"description": "Pupils decided to go to amusement park. Some of them were with parents. In total, n people came to the park and they all want to get to the most extreme attraction and roll on it exactly once.Tickets for group of x people are sold on the attraction, there should be at least one adult in each group (it is possible that the group consists of one adult). The ticket price for such group is c1\u2009+\u2009c2\u00b7(x\u2009-\u20091)2 (in particular, if the group consists of one person, then the price is c1). All pupils who came to the park and their parents decided to split into groups in such a way that each visitor join exactly one group, and the total price of visiting the most extreme attraction is as low as possible. You are to determine this minimum possible total price. There should be at least one adult in each group. ", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains three integers n, c1 and c2 (1\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000, 1\u2009\u2264\u2009c1,\u2009c2\u2009\u2264\u2009107)\u00a0\u2014 the number of visitors and parameters for determining the ticket prices for a group. The second line contains the string of length n, which consists of zeros and ones. If the i-th symbol of the string is zero, then the i-th visitor is a pupil, otherwise the i-th person is an adult. It is guaranteed that there is at least one adult. It is possible that there are no pupils.", "output_spec": "Print the minimum price of visiting the most extreme attraction for all pupils and their parents. Each of them should roll on the attraction exactly once.", "notes": "NoteIn the first test one group of three people should go to the attraction. Then they have to pay 4\u2009+\u20091\u2009*\u2009(3\u2009-\u20091)2\u2009=\u20098.In the second test it is better to go to the attraction in two groups. The first group should consist of two adults (for example, the first and the second person), the second should consist of one pupil and one adult (the third and the fourth person). Then each group will have a size of two and for each the price of ticket is 7\u2009+\u20092\u2009*\u2009(2\u2009-\u20091)2\u2009=\u20099. Thus, the total price for two groups is 18.", "sample_inputs": ["3 4 1\n011", "4 7 2\n1101"], "sample_outputs": ["8", "18"], "tags": ["ternary search"], "src_uid": "78d013b01497053b8e321fe7b6ce3760", "difficulty": 2100} {"description": "Bomboslav likes to look out of the window in his room and watch lads outside playing famous shell game. The game is played by two persons: operator and player. Operator takes three similar opaque shells and places a ball beneath one of them. Then he shuffles the shells by swapping some pairs and the player has to guess the current position of the ball.Bomboslav noticed that guys are not very inventive, so the operator always swaps the left shell with the middle one during odd moves (first, third, fifth, etc.) and always swaps the middle shell with the right one during even moves (second, fourth, etc.).Let's number shells from 0 to 2 from left to right. Thus the left shell is assigned number 0, the middle shell is 1 and the right shell is 2. Bomboslav has missed the moment when the ball was placed beneath the shell, but he knows that exactly n movements were made by the operator and the ball was under shell x at the end. Now he wonders, what was the initial position of the ball?", "input_from": "standard input", "output_to": "standard output", "time_limit": "0.5 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line of the input contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u20092\u00b7109)\u00a0\u2014 the number of movements made by the operator. The second line contains a single integer x (0\u2009\u2264\u2009x\u2009\u2264\u20092)\u00a0\u2014 the index of the shell where the ball was found after n movements.", "output_spec": "Print one integer from 0 to 2\u00a0\u2014 the index of the shell where the ball was initially placed.", "notes": "NoteIn the first sample, the ball was initially placed beneath the middle shell and the operator completed four movements. During the first move operator swapped the left shell and the middle shell. The ball is now under the left shell. During the second move operator swapped the middle shell and the right one. The ball is still under the left shell. During the third move operator swapped the left shell and the middle shell again. The ball is again in the middle. Finally, the operators swapped the middle shell and the right shell. The ball is now beneath the right shell. ", "sample_inputs": ["4\n2", "1\n1"], "sample_outputs": ["1", "0"], "tags": ["math", "constructive algorithms", "implementation"], "src_uid": "7853e03d520cd71571a6079cdfc4c4b0", "difficulty": 1000} {"description": "Let quasi-palindromic number be such number that adding some leading zeros (possible none) to it produces a palindromic string. String t is called a palindrome, if it reads the same from left to right and from right to left.For example, numbers 131 and 2010200 are quasi-palindromic, they can be transformed to strings \"131\" and \"002010200\", respectively, which are palindromes.You are given some integer number x. Check if it's a quasi-palindromic number.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains one integer number x (1\u2009\u2264\u2009x\u2009\u2264\u2009109). This number is given without any leading zeroes.", "output_spec": "Print \"YES\" if number x is quasi-palindromic. Otherwise, print \"NO\" (without quotes).", "notes": null, "sample_inputs": ["131", "320", "2010200"], "sample_outputs": ["YES", "NO", "YES"], "tags": ["brute force", "implementation"], "src_uid": "d82278932881e3aa997086c909f29051", "difficulty": 900} {"description": "As you may know, MemSQL has American offices in both San Francisco and Seattle. Being a manager in the company, you travel a lot between the two cities, always by plane.You prefer flying from Seattle to San Francisco than in the other direction, because it's warmer in San Francisco. You are so busy that you don't remember the number of flights you have made in either direction. However, for each of the last n days you know whether you were in San Francisco office or in Seattle office. You always fly at nights, so you never were at both offices on the same day. Given this information, determine if you flew more times from Seattle to San Francisco during the last n days, or not.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line of input contains single integer n (2\u2009\u2264\u2009n\u2009\u2264\u2009100)\u00a0\u2014 the number of days. The second line contains a string of length n consisting of only capital 'S' and 'F' letters. If the i-th letter is 'S', then you were in Seattle office on that day. Otherwise you were in San Francisco. The days are given in chronological order, i.e. today is the last day in this sequence.", "output_spec": "Print \"YES\" if you flew more times from Seattle to San Francisco, and \"NO\" otherwise. You can print each letter in any case (upper or lower).", "notes": "NoteIn the first example you were initially at San Francisco, then flew to Seattle, were there for two days and returned to San Francisco. You made one flight in each direction, so the answer is \"NO\".In the second example you just flew from Seattle to San Francisco, so the answer is \"YES\".In the third example you stayed the whole period in San Francisco, so the answer is \"NO\".In the fourth example if you replace 'S' with ones, and 'F' with zeros, you'll get the first few digits of \u03c0 in binary representation. Not very useful information though.", "sample_inputs": ["4\nFSSF", "2\nSF", "10\nFFFFFFFFFF", "10\nSSFFSFFSFF"], "sample_outputs": ["NO", "YES", "NO", "YES"], "tags": ["implementation"], "src_uid": "ab8a2070ea758d118b3c09ee165d9517", "difficulty": 800} {"description": "You are given two lists of non-zero digits.Let's call an integer pretty if its (base 10) representation has at least one digit from the first list and at least one digit from the second list. What is the smallest positive pretty integer?", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20099) \u2014 the lengths of the first and the second lists, respectively. The second line contains n distinct digits a1,\u2009a2,\u2009...,\u2009an (1\u2009\u2264\u2009ai\u2009\u2264\u20099) \u2014 the elements of the first list. The third line contains m distinct digits b1,\u2009b2,\u2009...,\u2009bm (1\u2009\u2264\u2009bi\u2009\u2264\u20099) \u2014 the elements of the second list.", "output_spec": "Print the smallest pretty integer.", "notes": "NoteIn the first example 25, 46, 24567 are pretty, as well as many other integers. The smallest among them is 25. 42 and 24 are not pretty because they don't have digits from the second list.In the second example all integers that have at least one digit different from 9 are pretty. It's obvious that the smallest among them is 1, because it's the smallest positive integer.", "sample_inputs": ["2 3\n4 2\n5 7 6", "8 8\n1 2 3 4 5 6 7 8\n8 7 6 5 4 3 2 1"], "sample_outputs": ["25", "1"], "tags": ["brute force", "implementation"], "src_uid": "3a0c1b6d710fd8f0b6daf420255d76ee", "difficulty": 900} {"description": "Unlucky year in Berland is such a year that its number n can be represented as n\u2009=\u2009xa\u2009+\u2009yb, where a and b are non-negative integer numbers. For example, if x\u2009=\u20092 and y\u2009=\u20093 then the years 4 and 17 are unlucky (4\u2009=\u200920\u2009+\u200931, 17\u2009=\u200923\u2009+\u200932\u2009=\u200924\u2009+\u200930) and year 18 isn't unlucky as there is no such representation for it.Such interval of years that there are no unlucky years in it is called The Golden Age.You should write a program which will find maximum length of The Golden Age which starts no earlier than the year l and ends no later than the year r. If all years in the interval [l,\u2009r] are unlucky then the answer is 0.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains four integer numbers x, y, l and r (2\u2009\u2264\u2009x,\u2009y\u2009\u2264\u20091018, 1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u20091018).", "output_spec": "Print the maximum length of The Golden Age within the interval [l,\u2009r]. If all years in the interval [l,\u2009r] are unlucky then print 0.", "notes": "NoteIn the first example the unlucky years are 2, 3, 4, 5, 7, 9 and 10. So maximum length of The Golden Age is achived in the intervals [1,\u20091], [6,\u20096] and [8,\u20098].In the second example the longest Golden Age is the interval [15,\u200922].", "sample_inputs": ["2 3 1 10", "3 5 10 22", "2 3 3 5"], "sample_outputs": ["1", "8", "0"], "tags": ["brute force", "math"], "src_uid": "68ca8a8730db27ac2230f9fe9b120f5f", "difficulty": 1800} {"description": "Captain Bill the Hummingbird and his crew recieved an interesting challenge offer. Some stranger gave them a map, potion of teleportation and said that only this potion might help them to reach the treasure. Bottle with potion has two values x and y written on it. These values define four moves which can be performed using the potion: Map shows that the position of Captain Bill the Hummingbird is (x1,\u2009y1) and the position of the treasure is (x2,\u2009y2).You task is to tell Captain Bill the Hummingbird whether he should accept this challenge or decline. If it is possible for Captain to reach the treasure using the potion then output \"YES\", otherwise \"NO\" (without quotes).The potion can be used infinite amount of times.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains four integer numbers x1,\u2009y1,\u2009x2,\u2009y2 (\u2009-\u2009105\u2009\u2264\u2009x1,\u2009y1,\u2009x2,\u2009y2\u2009\u2264\u2009105) \u2014 positions of Captain Bill the Hummingbird and treasure respectively. The second line contains two integer numbers x,\u2009y (1\u2009\u2264\u2009x,\u2009y\u2009\u2264\u2009105) \u2014 values on the potion bottle.", "output_spec": "Print \"YES\" if it is possible for Captain to reach the treasure using the potion, otherwise print \"NO\" (without quotes).", "notes": "NoteIn the first example there exists such sequence of moves: \u2014 the first type of move \u2014 the third type of move ", "sample_inputs": ["0 0 0 6\n2 3", "1 1 3 6\n1 5"], "sample_outputs": ["YES", "NO"], "tags": ["math", "number theory", "implementation"], "src_uid": "1c80040104e06c9f24abfcfe654a851f", "difficulty": 1200} {"description": "Okabe needs bananas for one of his experiments for some strange reason. So he decides to go to the forest and cut banana trees.Consider the point (x,\u2009y) in the 2D plane such that x and y are integers and 0\u2009\u2264\u2009x,\u2009y. There is a tree in such a point, and it has x\u2009+\u2009y bananas. There are no trees nor bananas in other points. Now, Okabe draws a line with equation . Okabe can select a single rectangle with axis aligned sides with all points on or under the line and cut all the trees in all points that are inside or on the border of this rectangle and take their bananas. Okabe's rectangle can be degenerate; that is, it can be a line segment or even a point.Help Okabe and find the maximum number of bananas he can get if he chooses the rectangle wisely.Okabe is sure that the answer does not exceed 1018. You can trust him.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line of input contains two space-separated integers m and b (1\u2009\u2264\u2009m\u2009\u2264\u20091000, 1\u2009\u2264\u2009b\u2009\u2264\u200910000).", "output_spec": "Print the maximum number of bananas Okabe can get from the trees he cuts.", "notes": "Note The graph above corresponds to sample test 1. The optimal rectangle is shown in red and has 30 bananas.", "sample_inputs": ["1 5", "2 3"], "sample_outputs": ["30", "25"], "tags": ["brute force", "math"], "src_uid": "9300f1c07dd36e0cf7e6cb7911df4cf2", "difficulty": 1300} {"description": "Holidays have finished. Thanks to the help of the hacker Leha, Noora managed to enter the university of her dreams which is located in a town Pavlopolis. It's well known that universities provide students with dormitory for the period of university studies. Consequently Noora had to leave Vi\u010dkopolis and move to Pavlopolis. Thus Leha was left completely alone in a quiet town Vi\u010dkopolis. He almost even fell into a depression from boredom!Leha came up with a task for himself to relax a little. He chooses two integers A and B and then calculates the greatest common divisor of integers \"A factorial\" and \"B factorial\". Formally the hacker wants to find out GCD(A!,\u2009B!). It's well known that the factorial of an integer x is a product of all positive integers less than or equal to x. Thus x!\u2009=\u20091\u00b72\u00b73\u00b7...\u00b7(x\u2009-\u20091)\u00b7x. For example 4!\u2009=\u20091\u00b72\u00b73\u00b74\u2009=\u200924. Recall that GCD(x,\u2009y) is the largest positive integer q that divides (without a remainder) both x and y.Leha has learned how to solve this task very effective. You are able to cope with it not worse, aren't you?", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first and single line contains two integers A and B (1\u2009\u2264\u2009A,\u2009B\u2009\u2264\u2009109,\u2009min(A,\u2009B)\u2009\u2264\u200912).", "output_spec": "Print a single integer denoting the greatest common divisor of integers A! and B!.", "notes": "NoteConsider the sample.4!\u2009=\u20091\u00b72\u00b73\u00b74\u2009=\u200924. 3!\u2009=\u20091\u00b72\u00b73\u2009=\u20096. The greatest common divisor of integers 24 and 6 is exactly 6.", "sample_inputs": ["4 3"], "sample_outputs": ["6"], "tags": ["math", "number theory", "implementation"], "src_uid": "7bf30ceb24b66d91382e97767f9feeb6", "difficulty": 800} {"description": "One day Petya was solving a very interesting problem. But although he used many optimization techniques, his solution still got Time limit exceeded verdict. Petya conducted a thorough analysis of his program and found out that his function for finding maximum element in an array of n positive integers was too slow. Desperate, Petya decided to use a somewhat unexpected optimization using parameter k, so now his function contains the following code:int fast_max(int n, int a[]) { int ans = 0; int offset = 0; for (int i = 0; i < n; ++i) if (ans < a[i]) { ans = a[i]; offset = 0; } else { offset = offset + 1; if (offset == k) return ans; } return ans;}That way the function iteratively checks array elements, storing the intermediate maximum, and if after k consecutive iterations that maximum has not changed, it is returned as the answer.Now Petya is interested in fault rate of his function. He asked you to find the number of permutations of integers from 1 to n such that the return value of his function on those permutations is not equal to n. Since this number could be very big, output the answer modulo 109\u2009+\u20097.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The only line contains two integers n and k (1\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009106), separated by a space\u00a0\u2014 the length of the permutations and the parameter k.", "output_spec": "Output the answer to the problem modulo 109\u2009+\u20097.", "notes": "NotePermutations from second example: [4,\u20091,\u20092,\u20093,\u20095], [4,\u20091,\u20093,\u20092,\u20095], [4,\u20092,\u20091,\u20093,\u20095], [4,\u20092,\u20093,\u20091,\u20095], [4,\u20093,\u20091,\u20092,\u20095], [4,\u20093,\u20092,\u20091,\u20095].", "sample_inputs": ["5 2", "5 3", "6 3"], "sample_outputs": ["22", "6", "84"], "tags": ["dp", "combinatorics", "math"], "src_uid": "0644605611a2cd10ab3a9f12f18d7ae4", "difficulty": 2400} {"description": "Ivan has a robot which is situated on an infinite grid. Initially the robot is standing in the starting cell (0,\u20090). The robot can process commands. There are four types of commands it can perform: U \u2014 move from the cell (x,\u2009y) to (x,\u2009y\u2009+\u20091); D \u2014 move from (x,\u2009y) to (x,\u2009y\u2009-\u20091); L \u2014 move from (x,\u2009y) to (x\u2009-\u20091,\u2009y); R \u2014 move from (x,\u2009y) to (x\u2009+\u20091,\u2009y). Ivan entered a sequence of n commands, and the robot processed it. After this sequence the robot ended up in the starting cell (0,\u20090), but Ivan doubts that the sequence is such that after performing it correctly the robot ends up in the same cell. He thinks that some commands were ignored by robot. To acknowledge whether the robot is severely bugged, he needs to calculate the maximum possible number of commands that were performed correctly. Help Ivan to do the calculations!", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains one number n \u2014 the length of sequence of commands entered by Ivan (1\u2009\u2264\u2009n\u2009\u2264\u2009100). The second line contains the sequence itself \u2014 a string consisting of n characters. Each character can be U, D, L or R.", "output_spec": "Print the maximum possible number of commands from the sequence the robot could perform to end up in the starting cell.", "notes": null, "sample_inputs": ["4\nLDUR", "5\nRRRUU", "6\nLLRRRR"], "sample_outputs": ["4", "0", "4"], "tags": ["greedy"], "src_uid": "b9fa2bb8001bd064ede531a5281cfd8a", "difficulty": 1000} {"description": "You are given an array a consisting of n integers, and additionally an integer m. You have to choose some sequence of indices b1,\u2009b2,\u2009...,\u2009bk (1\u2009\u2264\u2009b1\u2009<\u2009b2\u2009<\u2009...\u2009<\u2009bk\u2009\u2264\u2009n) in such a way that the value of is maximized. Chosen sequence can be empty.Print the maximum possible value of .", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers n and m (1\u2009\u2264\u2009n\u2009\u2264\u200935, 1\u2009\u2264\u2009m\u2009\u2264\u2009109). The second line contains n integers a1, a2, ..., an (1\u2009\u2264\u2009ai\u2009\u2264\u2009109).", "output_spec": "Print the maximum possible value of .", "notes": "NoteIn the first example you can choose a sequence b\u2009=\u2009{1,\u20092}, so the sum is equal to 7 (and that's 3 after taking it modulo 4).In the second example you can choose a sequence b\u2009=\u2009{3}.", "sample_inputs": ["4 4\n5 2 4 1", "3 20\n199 41 299"], "sample_outputs": ["3", "19"], "tags": ["bitmasks", "meet-in-the-middle", "divide and conquer"], "src_uid": "d3a8a3e69a55936ee33aedd66e5b7f4a", "difficulty": 1800} {"description": "Amr loves Geometry. One day he came up with a very interesting problem.Amr has a circle of radius r and center in point (x,\u2009y). He wants the circle center to be in new position (x',\u2009y').In one step Amr can put a pin to the border of the circle in a certain point, then rotate the circle around that pin by any angle and finally remove the pin.Help Amr to achieve his goal in minimum number of steps.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "Input consists of 5 space-separated integers r, x, y, x' y' (1\u2009\u2264\u2009r\u2009\u2264\u2009105, \u2009-\u2009105\u2009\u2264\u2009x,\u2009y,\u2009x',\u2009y'\u2009\u2264\u2009105), circle radius, coordinates of original center of the circle and coordinates of destination center of the circle respectively.", "output_spec": "Output a single integer \u2014 minimum number of steps required to move the center of the circle to the destination point.", "notes": "NoteIn the first sample test the optimal way is to put a pin at point (0,\u20092) and rotate the circle by 180 degrees counter-clockwise (or clockwise, no matter).", "sample_inputs": ["2 0 0 0 4", "1 1 1 4 4", "4 5 6 5 6"], "sample_outputs": ["1", "3", "0"], "tags": ["geometry", "math"], "src_uid": "698da80c7d24252b57cca4e4f0ca7031", "difficulty": 1400} {"description": "Drazil is playing a math game with Varda.Let's define for positive integer x as a product of factorials of its digits. For example, .First, they choose a decimal number a consisting of n digits that contains at least one digit larger than 1. This number may possibly start with leading zeroes. Then they should find maximum positive number x satisfying following two conditions:1. x doesn't contain neither digit 0 nor digit 1.2. = .Help friends find such number.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u200915) \u2014 the number of digits in a. The second line contains n digits of a. There is at least one digit in a that is larger than 1. Number a may possibly contain leading zeroes.", "output_spec": "Output a maximum possible integer satisfying the conditions above. There should be no zeroes and ones in this number decimal representation.", "notes": "NoteIn the first case, ", "sample_inputs": ["4\n1234", "3\n555"], "sample_outputs": ["33222", "555"], "tags": ["greedy", "math", "sortings", "dp", "implementation"], "src_uid": "60dbfc7a65702ae8bd4a587db1e06398", "difficulty": 1400} {"description": "Let's define the permutation of length n as an array p\u2009=\u2009[p1,\u2009p2,\u2009...,\u2009pn] consisting of n distinct integers from range from 1 to n. We say that this permutation maps value 1 into the value p1, value 2 into the value p2 and so on.Kyota Ootori has just learned about cyclic representation of a permutation. A cycle is a sequence of numbers such that each element of this sequence is being mapped into the next element of this sequence (and the last element of the cycle is being mapped into the first element of the cycle). The cyclic representation is a representation of p as a collection of cycles forming p. For example, permutation p\u2009=\u2009[4,\u20091,\u20096,\u20092,\u20095,\u20093] has a cyclic representation that looks like (142)(36)(5) because 1 is replaced by 4, 4 is replaced by 2, 2 is replaced by 1, 3 and 6 are swapped, and 5 remains in place. Permutation may have several cyclic representations, so Kyoya defines the standard cyclic representation of a permutation as follows. First, reorder the elements within each cycle so the largest element is first. Then, reorder all of the cycles so they are sorted by their first element. For our example above, the standard cyclic representation of [4,\u20091,\u20096,\u20092,\u20095,\u20093] is (421)(5)(63).Now, Kyoya notices that if we drop the parenthesis in the standard cyclic representation, we get another permutation! For instance, [4,\u20091,\u20096,\u20092,\u20095,\u20093] will become [4,\u20092,\u20091,\u20095,\u20096,\u20093].Kyoya notices that some permutations don't change after applying operation described above at all. He wrote all permutations of length n that do not change in a list in lexicographic order. Unfortunately, his friend Tamaki Suoh lost this list. Kyoya wishes to reproduce the list and he needs your help. Given the integers n and k, print the permutation that was k-th on Kyoya's list.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line will contain two integers n, k (1\u2009\u2264\u2009n\u2009\u2264\u200950, 1\u2009\u2264\u2009k\u2009\u2264\u2009min{1018,\u2009l} where l is the length of the Kyoya's list).", "output_spec": "Print n space-separated integers, representing the permutation that is the answer for the question. ", "notes": "NoteThe standard cycle representation is (1)(32)(4), which after removing parenthesis gives us the original permutation. The first permutation on the list would be [1,\u20092,\u20093,\u20094], while the second permutation would be [1,\u20092,\u20094,\u20093].", "sample_inputs": ["4 3", "10 1"], "sample_outputs": ["1 3 2 4", "1 2 3 4 5 6 7 8 9 10"], "tags": ["greedy", "combinatorics", "binary search", "constructive algorithms", "math", "implementation"], "src_uid": "e03c6d3bb8cf9119530668765691a346", "difficulty": 1900} {"description": "You are given three sticks with positive integer lengths of a,\u2009b, and c centimeters. You can increase length of some of them by some positive integer number of centimeters (different sticks can be increased by a different length), but in total by at most l centimeters. In particular, it is allowed not to increase the length of any stick.Determine the number of ways to increase the lengths of some sticks so that you can form from them a non-degenerate (that is, having a positive area) triangle. Two ways are considered different, if the length of some stick is increased by different number of centimeters in them.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The single line contains 4 integers a,\u2009b,\u2009c,\u2009l (1\u2009\u2264\u2009a,\u2009b,\u2009c\u2009\u2264\u20093\u00b7105, 0\u2009\u2264\u2009l\u2009\u2264\u20093\u00b7105).", "output_spec": "Print a single integer \u2014 the number of ways to increase the sizes of the sticks by the total of at most l centimeters, so that you can make a non-degenerate triangle from it.", "notes": "NoteIn the first sample test you can either not increase any stick or increase any two sticks by 1 centimeter.In the second sample test you can increase either the first or the second stick by one centimeter. Note that the triangle made from the initial sticks is degenerate and thus, doesn't meet the conditions.", "sample_inputs": ["1 1 1 2", "1 2 3 1", "10 2 1 7"], "sample_outputs": ["4", "2", "0"], "tags": ["math", "combinatorics", "brute force", "dp", "implementation"], "src_uid": "185ff90a8b0ae0e2b75605f772589410", "difficulty": 2100} {"description": "One day Vasya the Hipster decided to count how many socks he had. It turned out that he had a red socks and b blue socks.According to the latest fashion, hipsters should wear the socks of different colors: a red one on the left foot, a blue one on the right foot.Every day Vasya puts on new socks in the morning and throws them away before going to bed as he doesn't want to wash them.Vasya wonders, what is the maximum number of days when he can dress fashionable and wear different socks, and after that, for how many days he can then wear the same socks until he either runs out of socks or cannot make a single pair from the socks he's got.Can you help him?", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The single line of the input contains two positive integers a and b (1\u2009\u2264\u2009a,\u2009b\u2009\u2264\u2009100) \u2014 the number of red and blue socks that Vasya's got.", "output_spec": "Print two space-separated integers \u2014 the maximum number of days when Vasya can wear different socks and the number of days when he can wear the same socks until he either runs out of socks or cannot make a single pair from the socks he's got. Keep in mind that at the end of the day Vasya throws away the socks that he's been wearing on that day.", "notes": "NoteIn the first sample Vasya can first put on one pair of different socks, after that he has two red socks left to wear on the second day.", "sample_inputs": ["3 1", "2 3", "7 3"], "sample_outputs": ["1 1", "2 0", "3 2"], "tags": ["math", "implementation"], "src_uid": "775766790e91e539c1cfaa5030e5b955", "difficulty": 800} {"description": "A monster is attacking the Cyberland!Master Yang, a braver, is going to beat the monster. Yang and the monster each have 3 attributes: hitpoints (HP), offensive power (ATK) and defensive power (DEF).During the battle, every second the monster's HP decrease by max(0,\u2009ATKY\u2009-\u2009DEFM), while Yang's HP decreases by max(0,\u2009ATKM\u2009-\u2009DEFY), where index Y denotes Master Yang and index M denotes monster. Both decreases happen simultaneously Once monster's HP\u2009\u2264\u20090 and the same time Master Yang's HP\u2009>\u20090, Master Yang wins.Master Yang can buy attributes from the magic shop of Cyberland: h bitcoins per HP, a bitcoins per ATK, and d bitcoins per DEF.Now Master Yang wants to know the minimum number of bitcoins he can spend in order to win.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains three integers HPY,\u2009ATKY,\u2009DEFY, separated by a space, denoting the initial HP, ATK and DEF of Master Yang. The second line contains three integers HPM,\u2009ATKM,\u2009DEFM, separated by a space, denoting the HP, ATK and DEF of the monster. The third line contains three integers h,\u2009a,\u2009d, separated by a space, denoting the price of 1\u00a0HP, 1\u00a0ATK and 1\u00a0DEF. All numbers in input are integer and lie between 1 and 100 inclusively.", "output_spec": "The only output line should contain an integer, denoting the minimum bitcoins Master Yang should spend in order to win.", "notes": "NoteFor the first sample, prices for ATK and DEF are extremely high. Master Yang can buy 99 HP, then he can beat the monster with 1 HP left.For the second sample, Master Yang is strong enough to beat the monster, so he doesn't need to buy anything.", "sample_inputs": ["1 2 1\n1 100 1\n1 100 100", "100 100 100\n1 1 1\n1 1 1"], "sample_outputs": ["99", "0"], "tags": ["brute force", "binary search", "implementation"], "src_uid": "bf8a133154745e64a547de6f31ddc884", "difficulty": 1800} {"description": "Vasya decided to learn to play chess. Classic chess doesn't seem interesting to him, so he plays his own sort of chess.The queen is the piece that captures all squares on its vertical, horizontal and diagonal lines. If the cell is located on the same vertical, horizontal or diagonal line with queen, and the cell contains a piece of the enemy color, the queen is able to move to this square. After that the enemy's piece is removed from the board. The queen cannot move to a cell containing an enemy piece if there is some other piece between it and the queen. There is an n\u2009\u00d7\u2009n chessboard. We'll denote a cell on the intersection of the r-th row and c-th column as (r,\u2009c). The square (1,\u20091) contains the white queen and the square (1,\u2009n) contains the black queen. All other squares contain green pawns that don't belong to anyone.The players move in turns. The player that moves first plays for the white queen, his opponent plays for the black queen.On each move the player has to capture some piece with his queen (that is, move to a square that contains either a green pawn or the enemy queen). The player loses if either he cannot capture any piece during his move or the opponent took his queen during the previous move. Help Vasya determine who wins if both players play with an optimal strategy on the board n\u2009\u00d7\u2009n.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The input contains a single number n (2\u2009\u2264\u2009n\u2009\u2264\u2009109) \u2014 the size of the board.", "output_spec": "On the first line print the answer to problem \u2014 string \"white\" or string \"black\", depending on who wins if the both players play optimally. If the answer is \"white\", then you should also print two integers r and c representing the cell (r,\u2009c), where the first player should make his first move to win. If there are multiple such cells, print the one with the minimum r. If there are still multiple squares, print the one with the minimum c.", "notes": "NoteIn the first sample test the white queen can capture the black queen at the first move, so the white player wins.In the second test from the statement if the white queen captures the green pawn located on the central vertical line, then it will be captured by the black queen during the next move. So the only move for the white player is to capture the green pawn located at (2,\u20091). Similarly, the black queen doesn't have any other options but to capture the green pawn located at (2,\u20093), otherwise if it goes to the middle vertical line, it will be captured by the white queen.During the next move the same thing happens \u2014 neither the white, nor the black queen has other options rather than to capture green pawns situated above them. Thus, the white queen ends up on square (3,\u20091), and the black queen ends up on square (3,\u20093). In this situation the white queen has to capture any of the green pawns located on the middle vertical line, after that it will be captured by the black queen. Thus, the player who plays for the black queen wins.", "sample_inputs": ["2", "3"], "sample_outputs": ["white\n1 2", "black"], "tags": ["games", "math", "constructive algorithms"], "src_uid": "52e07d176aa1d370788f94ee2e61df93", "difficulty": 1700} {"description": "It's tough to be a superhero. And it's twice as tough to resist the supervillain who is cool at math. Suppose that you're an ordinary Batman in an ordinary city of Gotham. Your enemy Joker mined the building of the city administration and you only have several minutes to neutralize the charge. To do that you should enter the cancel code on the bomb control panel.However, that mad man decided to give you a hint. This morning the mayor found a playing card under his pillow. There was a line written on the card:The bomb has a note saying \"J(x)\u2009=\u2009A\", where A is some positive integer. You suspect that the cancel code is some integer x that meets the equation J(x)\u2009=\u2009A. Now in order to decide whether you should neutralize the bomb or run for your life, you've got to count how many distinct positive integers x meet this equation.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The single line of the input contains a single integer A (1\u2009\u2264\u2009A\u2009\u2264\u20091012).", "output_spec": "Print the number of solutions of the equation J(x)\u2009=\u2009A.", "notes": "NoteRecord x|n means that number n divides number x. is defined as the largest positive integer that divides both a and b.In the first sample test the only suitable value of x is 2. Then J(2)\u2009=\u20091\u2009+\u20092.In the second sample test the following values of x match: x\u2009=\u200914, J(14)\u2009=\u20091\u2009+\u20092\u2009+\u20097\u2009+\u200914\u2009=\u200924 x\u2009=\u200915, J(15)\u2009=\u20091\u2009+\u20093\u2009+\u20095\u2009+\u200915\u2009=\u200924 x\u2009=\u200923, J(23)\u2009=\u20091\u2009+\u200923\u2009=\u200924 ", "sample_inputs": ["3", "24"], "sample_outputs": ["1", "3"], "tags": ["math", "number theory", "hashing", "dp", "dfs and similar"], "src_uid": "1f68bd6f8b40e45a5bd360b03a264ef4", "difficulty": 2600} {"description": "Limak is a little polar bear. He has n balls, the i-th ball has size ti.Limak wants to give one ball to each of his three friends. Giving gifts isn't easy\u00a0\u2014 there are two rules Limak must obey to make friends happy: No two friends can get balls of the same size. No two friends can get balls of sizes that differ by more than 2. For example, Limak can choose balls with sizes 4, 5 and 3, or balls with sizes 90, 91 and 92. But he can't choose balls with sizes 5, 5 and 6 (two friends would get balls of the same size), and he can't choose balls with sizes 30, 31 and 33 (because sizes 30 and 33 differ by more than 2).Your task is to check whether Limak can choose three balls that satisfy conditions above.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line of the input contains one integer n (3\u2009\u2264\u2009n\u2009\u2264\u200950)\u00a0\u2014 the number of balls Limak has. The second line contains n integers t1,\u2009t2,\u2009...,\u2009tn (1\u2009\u2264\u2009ti\u2009\u2264\u20091000) where ti denotes the size of the i-th ball.", "output_spec": "Print \"YES\" (without quotes) if Limak can choose three balls of distinct sizes, such that any two of them differ by no more than 2. Otherwise, print \"NO\" (without quotes).", "notes": "NoteIn the first sample, there are 4 balls and Limak is able to choose three of them to satisfy the rules. He must must choose balls with sizes 18, 16 and 17.In the second sample, there is no way to give gifts to three friends without breaking the rules.In the third sample, there is even more than one way to choose balls: Choose balls with sizes 3, 4 and 5. Choose balls with sizes 972, 970, 971. ", "sample_inputs": ["4\n18 55 16 17", "6\n40 41 43 44 44 44", "8\n5 972 3 4 1 4 970 971"], "sample_outputs": ["YES", "NO", "YES"], "tags": ["brute force", "implementation", "sortings"], "src_uid": "d6c876a84c7b92141710be5d76536eab", "difficulty": 900} {"description": "Buses run between the cities A and B, the first one is at 05:00 AM and the last one departs not later than at 11:59 PM. A bus from the city A departs every a minutes and arrives to the city B in a ta minutes, and a bus from the city B departs every b minutes and arrives to the city A in a tb minutes.The driver Simion wants to make his job diverse, so he counts the buses going towards him. Simion doesn't count the buses he meet at the start and finish.You know the time when Simion departed from the city A to the city B. Calculate the number of buses Simion will meet to be sure in his counting.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers a,\u2009ta (1\u2009\u2264\u2009a,\u2009ta\u2009\u2264\u2009120) \u2014 the frequency of the buses from the city A to the city B and the travel time. Both values are given in minutes. The second line contains two integers b,\u2009tb (1\u2009\u2264\u2009b,\u2009tb\u2009\u2264\u2009120) \u2014 the frequency of the buses from the city B to the city A and the travel time. Both values are given in minutes. The last line contains the departure time of Simion from the city A in the format hh:mm. It is guaranteed that there are a bus from the city A at that time. Note that the hours and the minutes are given with exactly two digits.", "output_spec": "Print the only integer z \u2014 the number of buses Simion will meet on the way. Note that you should not count the encounters in cities A and B.", "notes": "NoteIn the first example Simion departs form the city A at 05:20 AM and arrives to the city B at 05:50 AM. He will meet the first 5 buses from the city B that departed in the period [05:00 AM - 05:40 AM]. Also Simion will meet a bus in the city B at 05:50 AM, but he will not count it.Also note that the first encounter will be between 05:26 AM and 05:27 AM (if we suggest that the buses are go with the sustained speed).", "sample_inputs": ["10 30\n10 35\n05:20", "60 120\n24 100\n13:00"], "sample_outputs": ["5", "9"], "tags": ["implementation"], "src_uid": "1c4cf1c3cb464a483511a8a61f8685a7", "difficulty": 1600} {"description": "There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?It's guaranteed that the optimal answer is always integer.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line of the input contains three distinct integers x1, x2 and x3 (1\u2009\u2264\u2009x1,\u2009x2,\u2009x3\u2009\u2264\u2009100)\u00a0\u2014 the coordinates of the houses of the first, the second and the third friends respectively. ", "output_spec": "Print one integer\u00a0\u2014 the minimum total distance the friends need to travel in order to meet together.", "notes": "NoteIn the first sample, friends should meet at the point 4. Thus, the first friend has to travel the distance of 3 (from the point 7 to the point 4), the second friend also has to travel the distance of 3 (from the point 1 to the point 4), while the third friend should not go anywhere because he lives at the point 4.", "sample_inputs": ["7 1 4", "30 20 10"], "sample_outputs": ["6", "20"], "tags": ["math", "implementation", "sortings"], "src_uid": "7bffa6e8d2d21bbb3b7f4aec109b3319", "difficulty": 800} {"description": "A new airplane SuperPuperJet has an infinite number of rows, numbered with positive integers starting with 1 from cockpit to tail. There are six seats in each row, denoted with letters from 'a' to 'f'. Seats 'a', 'b' and 'c' are located to the left of an aisle (if one looks in the direction of the cockpit), while seats 'd', 'e' and 'f' are located to the right. Seats 'a' and 'f' are located near the windows, while seats 'c' and 'd' are located near the aisle. \u00a0It's lunch time and two flight attendants have just started to serve food. They move from the first rows to the tail, always maintaining a distance of two rows from each other because of the food trolley. Thus, at the beginning the first attendant serves row 1 while the second attendant serves row 3. When both rows are done they move one row forward: the first attendant serves row 2 while the second attendant serves row 4. Then they move three rows forward and the first attendant serves row 5 while the second attendant serves row 7. Then they move one row forward again and so on.Flight attendants work with the same speed: it takes exactly 1 second to serve one passenger and 1 second to move one row forward. Each attendant first serves the passengers on the seats to the right of the aisle and then serves passengers on the seats to the left of the aisle (if one looks in the direction of the cockpit). Moreover, they always serve passengers in order from the window to the aisle. Thus, the first passenger to receive food in each row is located in seat 'f', and the last one\u00a0\u2014 in seat 'c'. Assume that all seats are occupied.Vasya has seat s in row n and wants to know how many seconds will pass before he gets his lunch.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The only line of input contains a description of Vasya's seat in the format ns, where n (1\u2009\u2264\u2009n\u2009\u2264\u20091018) is the index of the row and s is the seat in this row, denoted as letter from 'a' to 'f'. The index of the row and the seat are not separated by a space.", "output_spec": "Print one integer\u00a0\u2014 the number of seconds Vasya has to wait until he gets his lunch.", "notes": "NoteIn the first sample, the first flight attendant serves Vasya first, so Vasya gets his lunch after 1 second.In the second sample, the flight attendants will spend 6 seconds to serve everyone in the rows 1 and 3, then they will move one row forward in 1 second. As they first serve seats located to the right of the aisle in order from window to aisle, Vasya has to wait 3 more seconds. The total is 6\u2009+\u20091\u2009+\u20093\u2009=\u200910.", "sample_inputs": ["1f", "2d", "4a", "5e"], "sample_outputs": ["1", "10", "11", "18"], "tags": ["math", "implementation"], "src_uid": "069d0cb9b7c798a81007fb5b63fa0f45", "difficulty": 1200} {"description": "The year 2015 is almost over.Limak is a little polar bear. He has recently learnt about the binary system. He noticed that the passing year has exactly one zero in its representation in the binary system\u00a0\u2014 201510\u2009=\u2009111110111112. Note that he doesn't care about the number of zeros in the decimal representation.Limak chose some interval of years. He is going to count all years from this interval that have exactly one zero in the binary representation. Can you do it faster?Assume that all positive integers are always written without leading zeros.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The only line of the input contains two integers a and b (1\u2009\u2264\u2009a\u2009\u2264\u2009b\u2009\u2264\u20091018)\u00a0\u2014 the first year and the last year in Limak's interval respectively.", "output_spec": "Print one integer\u00a0\u2013 the number of years Limak will count in his chosen interval.", "notes": "NoteIn the first sample Limak's interval contains numbers 510\u2009=\u20091012, 610\u2009=\u20091102, 710\u2009=\u20091112, 810\u2009=\u200910002, 910\u2009=\u200910012 and 1010\u2009=\u200910102. Two of them (1012 and 1102) have the described property.", "sample_inputs": ["5 10", "2015 2015", "100 105", "72057594000000000 72057595000000000"], "sample_outputs": ["2", "1", "0", "26"], "tags": ["brute force", "bitmasks", "implementation"], "src_uid": "581f61b1f50313bf4c75833cefd4d022", "difficulty": 1300} {"description": "Your friend recently gave you some slimes for your birthday. You have n slimes all initially with value 1.You are going to play a game with these slimes. Initially, you put a single slime by itself in a row. Then, you will add the other n\u2009-\u20091 slimes one by one. When you add a slime, you place it at the right of all already placed slimes. Then, while the last two slimes in the row have the same value v, you combine them together to create a slime with value v\u2009+\u20091.You would like to see what the final state of the row is after you've added all n slimes. Please print the values of the slimes in the row from left to right.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line of the input will contain a single integer, n (1\u2009\u2264\u2009n\u2009\u2264\u2009100\u2009000).", "output_spec": "Output a single line with k integers, where k is the number of slimes in the row after you've finished the procedure described in the problem statement. The i-th of these numbers should be the value of the i-th slime from the left.", "notes": "NoteIn the first sample, we only have a single slime with value 1. The final state of the board is just a single slime with value 1.In the second sample, we perform the following steps:Initially we place a single slime in a row by itself. Thus, row is initially 1.Then, we will add another slime. The row is now 1 1. Since two rightmost slimes have the same values, we should replace these slimes with one with value 2. Thus, the final state of the board is 2.In the third sample, after adding the first two slimes, our row is 2. After adding one more slime, the row becomes 2 1.In the last sample, the steps look as follows: 1 2 2 1 3 3 1 3 2 3 2 1 4 ", "sample_inputs": ["1", "2", "3", "8"], "sample_outputs": ["1", "2", "2 1", "4"], "tags": ["implementation"], "src_uid": "757cd804aba01dc4bc108cb0722f68dc", "difficulty": 800} {"description": "You are given an alphabet consisting of n letters, your task is to make a string of the maximum possible length so that the following conditions are satisfied: the i-th letter occurs in the string no more than ai times; the number of occurrences of each letter in the string must be distinct for all the letters that occurred in the string at least once. ", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line of the input contains a single integer n (2\u2009\u2009\u2264\u2009\u2009n\u2009\u2009\u2264\u2009\u200926)\u00a0\u2014 the number of letters in the alphabet. The next line contains n integers ai (1\u2009\u2264\u2009ai\u2009\u2264\u2009109)\u00a0\u2014 i-th of these integers gives the limitation on the number of occurrences of the i-th character in the string.", "output_spec": "Print a single integer \u2014 the maximum length of the string that meets all the requirements.", "notes": "NoteFor convenience let's consider an alphabet consisting of three letters: \"a\", \"b\", \"c\". In the first sample, some of the optimal strings are: \"cccaabbccbb\", \"aabcbcbcbcb\". In the second sample some of the optimal strings are: \"acc\", \"cbc\".", "sample_inputs": ["3\n2 5 5", "3\n1 1 2"], "sample_outputs": ["11", "3"], "tags": ["greedy", "sortings"], "src_uid": "3c4b2d1c9440515bc3002eddd2b89f6f", "difficulty": 1100} {"description": "Famous Brazil city Rio de Janeiro holds a tennis tournament and Ostap Bender doesn't want to miss this event. There will be n players participating, and the tournament will follow knockout rules from the very first game. That means, that if someone loses a game he leaves the tournament immediately.Organizers are still arranging tournament grid (i.e. the order games will happen and who is going to play with whom) but they have already fixed one rule: two players can play against each other only if the number of games one of them has already played differs by no more than one from the number of games the other one has already played. Of course, both players had to win all their games in order to continue participating in the tournament.Tournament hasn't started yet so the audience is a bit bored. Ostap decided to find out what is the maximum number of games the winner of the tournament can take part in (assuming the rule above is used). However, it is unlikely he can deal with this problem without your help.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The only line of the input contains a single integer n (2\u2009\u2264\u2009n\u2009\u2264\u20091018)\u00a0\u2014 the number of players to participate in the tournament.", "output_spec": "Print the maximum number of games in which the winner of the tournament can take part.", "notes": "NoteIn all samples we consider that player number 1 is the winner.In the first sample, there would be only one game so the answer is 1.In the second sample, player 1 can consequently beat players 2 and 3. In the third sample, player 1 can't play with each other player as after he plays with players 2 and 3 he can't play against player 4, as he has 0 games played, while player 1 already played 2. Thus, the answer is 2 and to achieve we make pairs (1,\u20092) and (3,\u20094) and then clash the winners.", "sample_inputs": ["2", "3", "4", "10"], "sample_outputs": ["1", "2", "2", "4"], "tags": ["greedy", "combinatorics", "constructive algorithms", "dp", "math", "dfs and similar"], "src_uid": "3d3432b4f7c6a3b901161fa24b415b14", "difficulty": 1600} {"description": "Calvin the robot lies in an infinite rectangular grid. Calvin's source code contains a list of n commands, each either 'U', 'R', 'D', or 'L'\u00a0\u2014 instructions to move a single square up, right, down, or left, respectively. How many ways can Calvin execute a non-empty contiguous substrings of commands and return to the same square he starts in? Two substrings are considered different if they have different starting or ending indices.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line of the input contains a single positive integer, n (1\u2009\u2264\u2009n\u2009\u2264\u2009200)\u00a0\u2014 the number of commands. The next line contains n characters, each either 'U', 'R', 'D', or 'L'\u00a0\u2014 Calvin's source code.", "output_spec": "Print a single integer\u00a0\u2014 the number of contiguous substrings that Calvin can execute and return to his starting square.", "notes": "NoteIn the first case, the entire source code works, as well as the \"RL\" substring in the second and third characters.Note that, in the third case, the substring \"LR\" appears three times, and is therefore counted three times to the total result.", "sample_inputs": ["6\nURLLDR", "4\nDLUU", "7\nRLRLRLR"], "sample_outputs": ["2", "0", "12"], "tags": ["brute force", "implementation"], "src_uid": "7bd5521531950e2de9a7b0904353184d", "difficulty": 1000} {"description": "Two positive integers a and b have a sum of s and a bitwise XOR of x. How many possible values are there for the ordered pair (a,\u2009b)?", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line of the input contains two integers s and x (2\u2009\u2264\u2009s\u2009\u2264\u20091012, 0\u2009\u2264\u2009x\u2009\u2264\u20091012), the sum and bitwise xor of the pair of positive integers, respectively.", "output_spec": "Print a single integer, the number of solutions to the given conditions. If no solutions exist, print 0.", "notes": "NoteIn the first sample, we have the following solutions: (2,\u20097), (3,\u20096), (6,\u20093), (7,\u20092).In the second sample, the only solutions are (1,\u20092) and (2,\u20091).", "sample_inputs": ["9 5", "3 3", "5 2"], "sample_outputs": ["4", "2", "0"], "tags": ["math", "constructive algorithms", "dp", "implementation"], "src_uid": "18410980789b14c128dd6adfa501aea5", "difficulty": 1700} {"description": "For his computer science class, Jacob builds a model tree with sticks and balls containing n nodes in the shape of a tree. Jacob has spent ai minutes building the i-th ball in the tree.Jacob's teacher will evaluate his model and grade Jacob based on the effort he has put in. However, she does not have enough time to search his whole tree to determine this; Jacob knows that she will examine the first k nodes in a DFS-order traversal of the tree. She will then assign Jacob a grade equal to the minimum ai she finds among those k nodes.Though Jacob does not have enough time to rebuild his model, he can choose the root node that his teacher starts from. Furthermore, he can rearrange the list of neighbors of each node in any order he likes. Help Jacob find the best grade he can get on this assignment.A DFS-order traversal is an ordering of the nodes of a rooted tree, built by a recursive DFS-procedure initially called on the root of the tree. When called on a given node v, the procedure does the following: Print v. Traverse the list of neighbors of the node v in order and iteratively call DFS-procedure on each one. Do not call DFS-procedure on node u if you came to node v directly from u. ", "input_from": "standard input", "output_to": "standard output", "time_limit": "7 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line of the input contains two positive integers, n and k (2\u2009\u2264\u2009n\u2009\u2264\u2009200\u2009000, 1\u2009\u2264\u2009k\u2009\u2264\u2009n)\u00a0\u2014 the number of balls in Jacob's tree and the number of balls the teacher will inspect. The second line contains n integers, ai (1\u2009\u2264\u2009ai\u2009\u2264\u20091\u2009000\u2009000), the time Jacob used to build the i-th ball. Each of the next n\u2009-\u20091 lines contains two integers ui, vi (1\u2009\u2264\u2009ui,\u2009vi\u2009\u2264\u2009n, ui\u2009\u2260\u2009vi) representing a connection in Jacob's tree between balls ui and vi.", "output_spec": "Print a single integer\u00a0\u2014 the maximum grade Jacob can get by picking the right root of the tree and rearranging the list of neighbors.", "notes": "NoteIn the first sample, Jacob can root the tree at node 2 and order 2's neighbors in the order 4, 1, 5 (all other nodes have at most two neighbors). The resulting preorder traversal is 2, 4, 1, 3, 5, and the minimum ai of the first 3 nodes is 3.In the second sample, it is clear that any preorder traversal will contain node 1 as either its first or second node, so Jacob cannot do better than a grade of 1.", "sample_inputs": ["5 3\n3 6 1 4 2\n1 2\n2 4\n2 5\n1 3", "4 2\n1 5 5 5\n1 2\n1 3\n1 4"], "sample_outputs": ["3", "1"], "tags": ["dfs and similar", "graphs", "greedy", "binary search"], "src_uid": "4fb83b890e472f86045981e1743ddaac", "difficulty": 2600} {"description": "The numbers of all offices in the new building of the Tax Office of IT City will have lucky numbers.Lucky number is a number that consists of digits 7 and 8 only. Find the maximum number of offices in the new building of the Tax Office given that a door-plate can hold a number not longer than n digits.", "input_from": "standard input", "output_to": "standard output", "time_limit": "0.5 seconds", "memory_limit": "64 megabytes", "input_spec": "The only line of input contains one integer n (1\u2009\u2264\u2009n\u2009\u2264\u200955) \u2014 the maximum length of a number that a door-plate can hold.", "output_spec": "Output one integer \u2014 the maximum number of offices, than can have unique lucky numbers not longer than n digits.", "notes": null, "sample_inputs": ["2"], "sample_outputs": ["6"], "tags": ["combinatorics", "math"], "src_uid": "f1b43baa14d4c262ba616d892525dfde", "difficulty": 1100} {"description": "Vasya started working in a machine vision company of IT City. Vasya's team creates software and hardware for identification of people by their face.One of the project's know-how is a camera rotating around its optical axis on shooting. People see an eye-catching gadget \u2014 a rotating camera \u2014 come up to it to see it better, look into it. And the camera takes their photo at that time. What could be better for high quality identification?But not everything is so simple. The pictures from camera appear rotated too (on clockwise camera rotation frame the content becomes rotated counter-clockwise). But the identification algorithm can work only with faces that are just slightly deviated from vertical.Vasya was entrusted to correct the situation \u2014 to rotate a captured image so that image would be minimally deviated from vertical. Requirements were severe. Firstly, the picture should be rotated only on angle divisible by 90 degrees to not lose a bit of information about the image. Secondly, the frames from the camera are so huge and FPS is so big that adequate rotation speed is provided by hardware FPGA solution only. And this solution can rotate only by 90 degrees clockwise. Of course, one can apply 90 degrees turn several times but for the sake of performance the number of turns should be minimized.Help Vasya implement the program that by the given rotation angle of the camera can determine the minimum number of 90 degrees clockwise turns necessary to get a picture in which up direction deviation from vertical is minimum.The next figure contains frames taken from an unrotated camera, then from rotated 90 degrees clockwise, then from rotated 90 degrees counter-clockwise. Arrows show direction to \"true up\". The next figure shows 90 degrees clockwise turn by FPGA hardware. ", "input_from": "standard input", "output_to": "standard output", "time_limit": "0.5 seconds", "memory_limit": "64 megabytes", "input_spec": "The only line of the input contains one integer x (\u2009-\u20091018\u2009\u2264\u2009x\u2009\u2264\u20091018) \u2014 camera angle in degrees. Positive value denotes clockwise camera rotation, negative \u2014 counter-clockwise.", "output_spec": "Output one integer \u2014 the minimum required number of 90 degrees clockwise turns.", "notes": "NoteWhen the camera is rotated 60 degrees counter-clockwise (the second example), an image from it is rotated 60 degrees clockwise. One 90 degrees clockwise turn of the image result in 150 degrees clockwise total rotation and deviation from \"true up\" for one turn is 150 degrees. Two 90 degrees clockwise turns of the image result in 240 degrees clockwise total rotation and deviation from \"true up\" for two turns is 120 degrees because 240 degrees clockwise equal to 120 degrees counter-clockwise. Three 90 degrees clockwise turns of the image result in 330 degrees clockwise total rotation and deviation from \"true up\" for three turns is 30 degrees because 330 degrees clockwise equal to 30 degrees counter-clockwise.From 60, 150, 120 and 30 degrees deviations the smallest is 30, and it it achieved in three 90 degrees clockwise turns.", "sample_inputs": ["60", "-60"], "sample_outputs": ["1", "3"], "tags": ["geometry", "math"], "src_uid": "509db9cb6156b692557ba874a09f150e", "difficulty": 1800} {"description": "There are n people, sitting in a line at the table. For each person we know that he always tells either the truth or lies.Little Serge asked them: how many of you always tell the truth? Each of the people at the table knows everything (who is an honest person and who is a liar) about all the people at the table. The honest people are going to say the correct answer, the liars are going to say any integer from 1 to n, which is not the correct answer. Every liar chooses his answer, regardless of the other liars, so two distinct liars may give distinct answer.Serge does not know any information about the people besides their answers to his question. He took a piece of paper and wrote n integers a1,\u2009a2,\u2009...,\u2009an, where ai is the answer of the i-th person in the row. Given this sequence, Serge determined that exactly k people sitting at the table apparently lie.Serge wonders, how many variants of people's answers (sequences of answers a of length n) there are where one can say that exactly k people sitting at the table apparently lie. As there can be rather many described variants of answers, count the remainder of dividing the number of the variants by 777777777.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers n, k, (1\u2009\u2264\u2009k\u2009\u2264\u2009n\u2009\u2264\u200928). It is guaranteed that n \u2014 is the power of number 2.", "output_spec": "Print a single integer \u2014 the answer to the problem modulo 777777777.", "notes": null, "sample_inputs": ["1 1", "2 1"], "sample_outputs": ["0", "2"], "tags": ["dp"], "src_uid": "cfe19131644e5925e32084a581e23286", "difficulty": 2700} {"description": "There have recently been elections in the zoo. Overall there were 7 main political parties: one of them is the Little Elephant Political Party, 6 other parties have less catchy names.Political parties find their number in the ballot highly important. Overall there are m possible numbers: 1,\u20092,\u2009...,\u2009m. Each of these 7 parties is going to be assigned in some way to exactly one number, at that, two distinct parties cannot receive the same number.The Little Elephant Political Party members believe in the lucky digits 4 and 7. They want to evaluate their chances in the elections. For that, they need to find out, how many correct assignments are there, such that the number of lucky digits in the Little Elephant Political Party ballot number is strictly larger than the total number of lucky digits in the ballot numbers of 6 other parties. Help the Little Elephant Political Party, calculate this number. As the answer can be rather large, print the remainder from dividing it by 1000000007 (109\u2009+\u20097).", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "A single line contains a single positive integer m (7\u2009\u2264\u2009m\u2009\u2264\u2009109) \u2014 the number of possible numbers in the ballot.", "output_spec": "In a single line print a single integer \u2014 the answer to the problem modulo 1000000007 (109\u2009+\u20097).", "notes": null, "sample_inputs": ["7", "8"], "sample_outputs": ["0", "1440"], "tags": ["brute force", "math", "combinatorics", "dp"], "src_uid": "656ed7b1b80de84d65a253e5d14d62a9", "difficulty": 1900} {"description": "Dima and Anya love playing different games. Now Dima has imagined a new game that he wants to play with Anya.Dima writes n pairs of integers on a piece of paper (li,\u2009ri) (1\u2009\u2264\u2009li\u2009<\u2009ri\u2009\u2264\u2009p). Then players take turns. On his turn the player can do the following actions: choose the number of the pair i (1\u2009\u2264\u2009i\u2009\u2264\u2009n), such that ri\u2009-\u2009li\u2009>\u20092; replace pair number i by pair or by pair . Notation \u230ax\u230b means rounding down to the closest integer. The player who can't make a move loses.Of course, Dima wants Anya, who will move first, to win. That's why Dima should write out such n pairs of integers (li,\u2009ri) (1\u2009\u2264\u2009li\u2009<\u2009ri\u2009\u2264\u2009p), that if both players play optimally well, the first one wins. Count the number of ways in which Dima can do it. Print the remainder after dividing the answer by number 1000000007\u00a0(109\u2009+\u20097).Two ways are considered distinct, if the ordered sequences of the written pairs are distinct.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers n, p (1\u2009\u2264\u2009n\u2009\u2264\u20091000,\u20091\u2009\u2264\u2009p\u2009\u2264\u2009109). The numbers are separated by a single space.", "output_spec": "In a single line print the remainder after dividing the answer to the problem by number 1000000007\u00a0(109\u2009+\u20097).", "notes": null, "sample_inputs": ["2 2", "4 4", "100 1000"], "sample_outputs": ["0", "520", "269568947"], "tags": ["games", "dp"], "src_uid": "c03b6379e9d186874ac3d97c6968fbd0", "difficulty": 2600} {"description": "Vova, the Ultimate Thule new shaman, wants to build a pipeline. As there are exactly n houses in Ultimate Thule, Vova wants the city to have exactly n pipes, each such pipe should be connected to the water supply. A pipe can be connected to the water supply if there's water flowing out of it. Initially Vova has only one pipe with flowing water. Besides, Vova has several splitters.A splitter is a construction that consists of one input (it can be connected to a water pipe) and x output pipes. When a splitter is connected to a water pipe, water flows from each output pipe. You can assume that the output pipes are ordinary pipes. For example, you can connect water supply to such pipe if there's water flowing out from it. At most one splitter can be connected to any water pipe. The figure shows a 4-output splitter Vova has one splitter of each kind: with 2, 3, 4, ..., k outputs. Help Vova use the minimum number of splitters to build the required pipeline or otherwise state that it's impossible.Vova needs the pipeline to have exactly n pipes with flowing out water. Note that some of those pipes can be the output pipes of the splitters.", "input_from": "standard input", "output_to": "standard output", "time_limit": "0.4 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains two space-separated integers n and k (1\u2009\u2264\u2009n\u2009\u2264\u20091018, 2\u2009\u2264\u2009k\u2009\u2264\u2009109). Please, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use the cin, cout streams or the %I64d specifier.", "output_spec": "Print a single integer \u2014 the minimum number of splitters needed to build the pipeline. If it is impossible to build a pipeline with the given splitters, print -1.", "notes": null, "sample_inputs": ["4 3", "5 5", "8 4"], "sample_outputs": ["2", "1", "-1"], "tags": ["math", "binary search"], "src_uid": "83bcfe32db302fbae18e8a95d89cf411", "difficulty": 1700} {"description": "Special Agent Smart Beaver works in a secret research department of ABBYY. He's been working there for a long time and is satisfied with his job, as it allows him to eat out in the best restaurants and order the most expensive and exotic wood types there. The content special agent has got an important task: to get the latest research by British scientists on the English Language. These developments are encoded and stored in a large safe. The Beaver's teeth are strong enough, so the authorities assured that upon arriving at the place the beaver won't have any problems with opening the safe.And he finishes his aspen sprig and leaves for this important task. Of course, the Beaver arrived at the location without any problems, but alas. He can't open the safe with his strong and big teeth. At this point, the Smart Beaver get a call from the headquarters and learns that opening the safe with the teeth is not necessary, as a reliable source has sent the following information: the safe code consists of digits and has no leading zeroes. There also is a special hint, which can be used to open the safe. The hint is string s with the following structure: if si = \"?\", then the digit that goes i-th in the safe code can be anything (between 0 to 9, inclusively); if si is a digit (between 0 to 9, inclusively), then it means that there is digit si on position i in code; if the string contains letters from \"A\" to \"J\", then all positions with the same letters must contain the same digits and the positions with distinct letters must contain distinct digits. The length of the safe code coincides with the length of the hint. For example, hint \"?JGJ9\" has such matching safe code variants: \"51919\", \"55959\", \"12329\", \"93539\" and so on, and has wrong variants such as: \"56669\", \"00111\", \"03539\" and \"13666\".After receiving such information, the authorities change the plan and ask the special agents to work quietly and gently and not to try to open the safe by mechanical means, and try to find the password using the given hint.At a special agent school the Smart Beaver was the fastest in his platoon finding codes for such safes, but now he is not in that shape: the years take their toll ... Help him to determine the number of possible variants of the code to the safe, matching the given hint. After receiving this information, and knowing his own speed of entering codes, the Smart Beaver will be able to determine whether he will have time for tonight's show \"Beavers are on the trail\" on his favorite TV channel, or he should work for a sleepless night...", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains string s \u2014 the hint to the safe code. String s consists of the following characters: ?, 0-9, A-J. It is guaranteed that the first character of string s doesn't equal to character 0. The input limits for scoring 30 points are (subproblem A1): 1\u2009\u2264\u2009|s|\u2009\u2264\u20095. The input limits for scoring 100 points are (subproblems A1+A2): 1\u2009\u2264\u2009|s|\u2009\u2264\u2009105. Here |s| means the length of string s.", "output_spec": "Print the number of codes that match the given hint.", "notes": null, "sample_inputs": ["AJ", "1?AA"], "sample_outputs": ["81", "100"], "tags": ["greedy"], "src_uid": "d3c10d1b1a17ad018359e2dab80d2b82", "difficulty": 1100} {"description": "Smart Beaver is careful about his appearance and pays special attention to shoes so he has a huge number of pairs of shoes from the most famous brands of the forest. He's trying to handle his shoes carefully so that each pair stood side by side. But by the end of the week because of his very active lifestyle in his dressing room becomes a mess.Smart Beaver from ABBYY is not only the brightest beaver in the area, but he also is the most domestically oriented. For example, on Mondays the Smart Beaver cleans everything in his home.It's Monday morning. Smart Beaver does not want to spend the whole day cleaning, besides, there is much in to do and it\u2019s the gym day, so he wants to clean up as soon as possible. Now the floors are washed, the dust is wiped off \u2014 it\u2019s time to clean up in the dressing room. But as soon as the Smart Beaver entered the dressing room, all plans for the day were suddenly destroyed: chaos reigned there and it seemed impossible to handle, even in a week. Give our hero some hope: tell him what is the minimum number of shoes need to change the position to make the dressing room neat.The dressing room is rectangular and is divided into n\u2009\u00d7\u2009m equal squares, each square contains exactly one shoe. Each pair of shoes has a unique number that is integer from 1 to , more formally, a square with coordinates (i,\u2009j) contains an integer number of the pair which is lying on it. The Smart Beaver believes that the dressing room is neat only when each pair of sneakers lies together. We assume that the pair of sneakers in squares (i1,\u2009j1) and (i2,\u2009j2) lies together if |i1\u2009-\u2009i2|\u2009+\u2009|j1\u2009-\u2009j2|\u2009=\u20091.", "input_from": "standard input", "output_to": "standard output", "time_limit": "4 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains two space-separated integers n and m. They correspond to the dressing room size. Next n lines contain m space-separated integers each. Those numbers describe the dressing room. Each number corresponds to a snicker. It is guaranteed that: n\u00b7m is even. All numbers, corresponding to the numbers of pairs of shoes in the dressing room, will lie between 1 and . Each number from 1 to will occur exactly twice. The input limits for scoring 30 points are (subproblem C1): 2\u2009\u2264\u2009n,\u2009m\u2009\u2264\u20098. The input limits for scoring 100 points are (subproblems C1+C2): 2\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200980. ", "output_spec": "Print exactly one integer \u2014 the minimum number of the sneakers that need to change their location.", "notes": "Note The second sample. ", "sample_inputs": ["2 3\n1 1 2\n2 3 3", "3 4\n1 3 2 6\n2 1 5 6\n4 4 5 3"], "sample_outputs": ["2", "4"], "tags": ["flows"], "src_uid": "1f0e8bbd5bf4fcdea927fbb505a8949b", "difficulty": 2200} {"description": "Iahub got bored, so he invented a game to be played on paper. He writes n integers a1,\u2009a2,\u2009...,\u2009an. Each of those integers can be either 0 or 1. He's allowed to do exactly one move: he chooses two indices i and j (1\u2009\u2264\u2009i\u2009\u2264\u2009j\u2009\u2264\u2009n) and flips all values ak for which their positions are in range [i,\u2009j] (that is i\u2009\u2264\u2009k\u2009\u2264\u2009j). Flip the value of x means to apply operation x\u2009=\u20091 - x.The goal of the game is that after exactly one move to obtain the maximum number of ones. Write a program to solve the little game of Iahub.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line of the input contains an integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009100). In the second line of the input there are n integers: a1,\u2009a2,\u2009...,\u2009an. It is guaranteed that each of those n values is either 0 or 1.", "output_spec": "Print an integer \u2014 the maximal number of 1s that can be obtained after exactly one move. ", "notes": "NoteIn the first case, flip the segment from 2 to 5 (i\u2009=\u20092,\u2009j\u2009=\u20095). That flip changes the sequence, it becomes: [1 1 1 0 1]. So, it contains four ones. There is no way to make the whole sequence equal to [1 1 1 1 1].In the second case, flipping only the second and the third element (i\u2009=\u20092,\u2009j\u2009=\u20093) will turn all numbers into 1.", "sample_inputs": ["5\n1 0 0 1 0", "4\n1 0 0 1"], "sample_outputs": ["4", "4"], "tags": ["brute force", "dp", "implementation"], "src_uid": "9b543e07e805fe1dd8fa869d5d7c8b99", "difficulty": 1200} {"description": "Gerald is very particular to eight point sets. He thinks that any decent eight point set must consist of all pairwise intersections of three distinct integer vertical straight lines and three distinct integer horizontal straight lines, except for the average of these nine points. In other words, there must be three integers x1,\u2009x2,\u2009x3 and three more integers y1,\u2009y2,\u2009y3, such that x1\u2009<\u2009x2\u2009<\u2009x3, y1\u2009<\u2009y2\u2009<\u2009y3 and the eight point set consists of all points (xi,\u2009yj) (1\u2009\u2264\u2009i,\u2009j\u2009\u2264\u20093), except for point (x2,\u2009y2).You have a set of eight points. Find out if Gerald can use this set?", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The input consists of eight lines, the i-th line contains two space-separated integers xi and yi (0\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009106). You do not have any other conditions for these points.", "output_spec": "In a single line print word \"respectable\", if the given set of points corresponds to Gerald's decency rules, and \"ugly\" otherwise.", "notes": null, "sample_inputs": ["0 0\n0 1\n0 2\n1 0\n1 2\n2 0\n2 1\n2 2", "0 0\n1 0\n2 0\n3 0\n4 0\n5 0\n6 0\n7 0", "1 1\n1 2\n1 3\n2 1\n2 2\n2 3\n3 1\n3 2"], "sample_outputs": ["respectable", "ugly", "ugly"], "tags": ["sortings"], "src_uid": "f3c96123334534056f26b96f90886807", "difficulty": 1400} {"description": "Iahub and his friend Floyd have started painting a wall. Iahub is painting the wall red and Floyd is painting it pink. You can consider the wall being made of a very large number of bricks, numbered 1, 2, 3 and so on. Iahub has the following scheme of painting: he skips x\u2009-\u20091 consecutive bricks, then he paints the x-th one. That is, he'll paint bricks x, 2\u00b7x, 3\u00b7x and so on red. Similarly, Floyd skips y\u2009-\u20091 consecutive bricks, then he paints the y-th one. Hence he'll paint bricks y, 2\u00b7y, 3\u00b7y and so on pink.After painting the wall all day, the boys observed that some bricks are painted both red and pink. Iahub has a lucky number a and Floyd has a lucky number b. Boys wonder how many bricks numbered no less than a and no greater than b are painted both red and pink. This is exactly your task: compute and print the answer to the question. ", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The input will have a single line containing four integers in this order: x, y, a, b. (1\u2009\u2264\u2009x,\u2009y\u2009\u2264\u20091000, 1\u2009\u2264\u2009a,\u2009b\u2009\u2264\u20092\u00b7109, a\u2009\u2264\u2009b).", "output_spec": "Output a single integer \u2014 the number of bricks numbered no less than a and no greater than b that are painted both red and pink.", "notes": "NoteLet's look at the bricks from a to b (a\u2009=\u20096,\u2009b\u2009=\u200918). The bricks colored in red are numbered 6, 8, 10, 12, 14, 16, 18. The bricks colored in pink are numbered 6, 9, 12, 15, 18. The bricks colored in both red and pink are numbered with 6, 12 and 18. ", "sample_inputs": ["2 3 6 18"], "sample_outputs": ["3"], "tags": ["math"], "src_uid": "c7aa8a95d5f8832015853cffa1374c48", "difficulty": 1200} {"description": "Mad scientist Mike is busy carrying out experiments in chemistry. Today he will attempt to join three atoms into one molecule.A molecule consists of atoms, with some pairs of atoms connected by atomic bonds. Each atom has a valence number \u2014 the number of bonds the atom must form with other atoms. An atom can form one or multiple bonds with any other atom, but it cannot form a bond with itself. The number of bonds of an atom in the molecule must be equal to its valence number. Mike knows valence numbers of the three atoms. Find a molecule that can be built from these atoms according to the stated rules, or determine that it is impossible.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The single line of the input contains three space-separated integers a, b and c (1\u2009\u2264\u2009a,\u2009b,\u2009c\u2009\u2264\u2009106) \u2014 the valence numbers of the given atoms.", "output_spec": "If such a molecule can be built, print three space-separated integers \u2014 the number of bonds between the 1-st and the 2-nd, the 2-nd and the 3-rd, the 3-rd and the 1-st atoms, correspondingly. If there are multiple solutions, output any of them. If there is no solution, print \"Impossible\" (without the quotes).", "notes": "NoteThe first sample corresponds to the first figure. There are no bonds between atoms 1 and 2 in this case.The second sample corresponds to the second figure. There is one or more bonds between each pair of atoms.The third sample corresponds to the third figure. There is no solution, because an atom cannot form bonds with itself.The configuration in the fourth figure is impossible as each atom must have at least one atomic bond.", "sample_inputs": ["1 1 2", "3 4 5", "4 1 1"], "sample_outputs": ["0 1 1", "1 3 2", "Impossible"], "tags": ["brute force", "math", "graphs"], "src_uid": "b3b986fddc3770fed64b878fa42ab1bc", "difficulty": 1200} {"description": "In mathematics, a subsequence is a sequence that can be derived from another sequence by deleting some elements without changing the order of the remaining elements. For example, the sequence BDF is a subsequence of ABCDEF. A substring of a string is a continuous subsequence of the string. For example, BCD is a substring of ABCDEF.You are given two strings s1, s2 and another string called virus. Your task is to find the longest common subsequence of s1 and s2, such that it doesn't contain virus as a substring.", "input_from": "standard input", "output_to": "standard output", "time_limit": "3 seconds", "memory_limit": "512 megabytes", "input_spec": "The input contains three strings in three separate lines: s1, s2 and virus (1\u2009\u2264\u2009|s1|,\u2009|s2|,\u2009|virus|\u2009\u2264\u2009100). Each string consists only of uppercase English letters.", "output_spec": "Output the longest common subsequence of s1 and s2 without virus as a substring. If there are multiple answers, any of them will be accepted. If there is no valid common subsequence, output 0.", "notes": null, "sample_inputs": ["AJKEQSLOBSROFGZ\nOVGURWZLWVLUXTH\nOZ", "AA\nA\nA"], "sample_outputs": ["ORZ", "0"], "tags": ["dp", "strings"], "src_uid": "391c2abbe862139733fcb997ba1629b8", "difficulty": 2000} {"description": "Jeff loves regular bracket sequences.Today Jeff is going to take a piece of paper and write out the regular bracket sequence, consisting of nm brackets. Let's number all brackets of this sequence from 0 to nm - 1 from left to right. Jeff knows that he is going to spend ai mod n liters of ink on the i-th bracket of the sequence if he paints it opened and bi mod n liters if he paints it closed.You've got sequences a, b and numbers n, m. What minimum amount of ink will Jeff need to paint a regular bracket sequence of length nm?Operation x mod y means taking the remainder after dividing number x by number y.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers n and m (1\u2009\u2264\u2009n\u2009\u2264\u200920;\u00a01\u2009\u2264\u2009m\u2009\u2264\u2009107; m is even). The next line contains n integers: a0, a1, ..., an\u2009-\u20091 (1\u2009\u2264\u2009ai\u2009\u2264\u200910). The next line contains n integers: b0, b1, ..., bn\u2009-\u20091 (1\u2009\u2264\u2009bi\u2009\u2264\u200910). The numbers are separated by spaces.", "output_spec": "In a single line print the answer to the problem \u2014 the minimum required amount of ink in liters.", "notes": "NoteIn the first test the optimal sequence is: ()()()()()(), the required number of ink liters is 12.", "sample_inputs": ["2 6\n1 2\n2 1", "1 10000000\n2\n3"], "sample_outputs": ["12", "25000000"], "tags": ["dp", "matrices"], "src_uid": "f40900973f4ebeb6fdafd75ebe4e9601", "difficulty": 2500} {"description": "Little Petya is learning to play chess. He has already learned how to move a king, a rook and a bishop. Let us remind you the rules of moving chess pieces. A chessboard is 64 square fields organized into an 8\u2009\u00d7\u20098 table. A field is represented by a pair of integers (r,\u2009c) \u2014 the number of the row and the number of the column (in a classical game the columns are traditionally indexed by letters). Each chess piece takes up exactly one field. To make a move is to move a chess piece, the pieces move by the following rules: A rook moves any number of fields horizontally or vertically. A bishop moves any number of fields diagonally. A king moves one field in any direction \u2014 horizontally, vertically or diagonally. The pieces move like that Petya is thinking about the following problem: what minimum number of moves is needed for each of these pieces to move from field (r1,\u2009c1) to field (r2,\u2009c2)? At that, we assume that there are no more pieces besides this one on the board. Help him solve this problem.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The input contains four integers r1,\u2009c1,\u2009r2,\u2009c2 (1\u2009\u2264\u2009r1,\u2009c1,\u2009r2,\u2009c2\u2009\u2264\u20098) \u2014 the coordinates of the starting and the final field. The starting field doesn't coincide with the final one. You can assume that the chessboard rows are numbered from top to bottom 1 through 8, and the columns are numbered from left to right 1 through 8.", "output_spec": "Print three space-separated integers: the minimum number of moves the rook, the bishop and the king (in this order) is needed to move from field (r1,\u2009c1) to field (r2,\u2009c2). If a piece cannot make such a move, print a 0 instead of the corresponding number.", "notes": null, "sample_inputs": ["4 3 1 6", "5 5 5 6"], "sample_outputs": ["2 1 3", "1 0 1"], "tags": ["math", "shortest paths", "graphs"], "src_uid": "7dbf58806db185f0fe70c00b60973f4b", "difficulty": 1100} {"description": "Cucumber boy is fan of Kyubeat, a famous music game.Kyubeat has 16 panels for playing arranged in 4\u2009\u00d7\u20094 table. When a panel lights up, he has to press that panel.Each panel has a timing to press (the preffered time when a player should press it), and Cucumber boy is able to press at most k panels in a time with his one hand. Cucumber boy is trying to press all panels in perfect timing, that is he wants to press each panel exactly in its preffered time. If he cannot press the panels with his two hands in perfect timing, his challenge to press all the panels in perfect timing will fail.You are given one scene of Kyubeat's panel from the music Cucumber boy is trying. Tell him is he able to press all the panels in perfect timing.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains a single integer k (1\u2009\u2264\u2009k\u2009\u2264\u20095) \u2014 the number of panels Cucumber boy can press with his one hand. Next 4 lines contain 4 characters each (digits from 1 to 9, or period) \u2014 table of panels. If a digit i was written on the panel, it means the boy has to press that panel in time i. If period was written on the panel, he doesn't have to press that panel.", "output_spec": "Output \"YES\" (without quotes), if he is able to press all the panels in perfect timing. If not, output \"NO\" (without quotes).", "notes": "NoteIn the third sample boy cannot press all panels in perfect timing. He can press all the panels in timing in time 1, but he cannot press the panels in time 2 in timing with his two hands.", "sample_inputs": ["1\n.135\n1247\n3468\n5789", "5\n..1.\n1111\n..1.\n..1.", "1\n....\n12.1\n.2..\n.2.."], "sample_outputs": ["YES", "YES", "NO"], "tags": ["implementation"], "src_uid": "5fdaf8ee7763cb5815f49c0c38398f16", "difficulty": 900} {"description": "Our bear's forest has a checkered field. The checkered field is an n\u2009\u00d7\u2009n table, the rows are numbered from 1 to n from top to bottom, the columns are numbered from 1 to n from left to right. Let's denote a cell of the field on the intersection of row x and column y by record (x,\u2009y). Each cell of the field contains growing raspberry, at that, the cell (x,\u2009y) of the field contains x\u2009+\u2009y raspberry bushes.The bear came out to walk across the field. At the beginning of the walk his speed is (dx,\u2009dy). Then the bear spends exactly t seconds on the field. Each second the following takes place: Let's suppose that at the current moment the bear is in cell (x,\u2009y). First the bear eats the raspberry from all the bushes he has in the current cell. After the bear eats the raspberry from k bushes, he increases each component of his speed by k. In other words, if before eating the k bushes of raspberry his speed was (dx,\u2009dy), then after eating the berry his speed equals (dx\u2009+\u2009k,\u2009dy\u2009+\u2009k). Let's denote the current speed of the bear (dx,\u2009dy) (it was increased after the previous step). Then the bear moves from cell (x,\u2009y) to cell (((x\u2009+\u2009dx\u2009-\u20091)\u00a0mod\u00a0n)\u2009+\u20091,\u2009((y\u2009+\u2009dy\u2009-\u20091)\u00a0mod\u00a0n)\u2009+\u20091). Then one additional raspberry bush grows in each cell of the field. You task is to predict the bear's actions. Find the cell he ends up in if he starts from cell (sx,\u2009sy). Assume that each bush has infinitely much raspberry and the bear will never eat all of it.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line of the input contains six space-separated integers: n, sx, sy, dx, dy, t (1\u2009\u2264\u2009n\u2009\u2264\u2009109;\u00a01\u2009\u2264\u2009sx,\u2009sy\u2009\u2264\u2009n;\u00a0\u2009-\u2009100\u2009\u2264\u2009dx,\u2009dy\u2009\u2264\u2009100;\u00a00\u2009\u2264\u2009t\u2009\u2264\u20091018).", "output_spec": "Print two integers \u2014 the coordinates of the cell the bear will end up in after t seconds.", "notes": "NoteOperation a\u00a0mod\u00a0b means taking the remainder after dividing a by b. Note that the result of the operation is always non-negative. For example, (\u2009-\u20091)\u00a0mod\u00a03\u2009=\u20092.In the first sample before the first move the speed vector will equal (3,4) and the bear will get to cell (4,1). Before the second move the speed vector will equal (9,10) and he bear will get to cell (3,1). Don't forget that at the second move, the number of berry bushes increased by 1.In the second sample before the first move the speed vector will equal (1,1) and the bear will get to cell (1,1). Before the second move, the speed vector will equal (4,4) and the bear will get to cell (1,1). Don't forget that at the second move, the number of berry bushes increased by 1.", "sample_inputs": ["5 1 2 0 1 2", "1 1 1 -1 -1 2"], "sample_outputs": ["3 1", "1 1"], "tags": ["math", "matrices"], "src_uid": "ee9fa8be2ae05a4e831a4f608c0cc785", "difficulty": 2300} {"description": "You will receive 3 points for solving this problem.Manao is designing the genetic code for a new type of algae to efficiently produce fuel. Specifically, Manao is focusing on a stretch of DNA that encodes one protein. The stretch of DNA is represented by a string containing only the characters 'A', 'T', 'G' and 'C'.Manao has determined that if the stretch of DNA contains a maximal sequence of consecutive identical nucleotides that is of even length, then the protein will be nonfunctional. For example, consider a protein described by DNA string \"GTTAAAG\". It contains four maximal sequences of consecutive identical nucleotides: \"G\", \"TT\", \"AAA\", and \"G\". The protein is nonfunctional because sequence \"TT\" has even length.Manao is trying to obtain a functional protein from the protein he currently has. Manao can insert additional nucleotides into the DNA stretch. Each additional nucleotide is a character from the set {'A', 'T', 'G', 'C'}. Manao wants to determine the minimum number of insertions necessary to make the DNA encode a functional protein.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The input consists of a single line, containing a string s of length n (1\u2009\u2264\u2009n\u2009\u2264\u2009100). Each character of s will be from the set {'A', 'T', 'G', 'C'}. This problem doesn't have subproblems. You will get 3 points for the correct submission.", "output_spec": "The program should print on one line a single integer representing the minimum number of 'A', 'T', 'G', 'C' characters that are required to be inserted into the input string in order to make all runs of identical characters have odd length.", "notes": "NoteIn the first example, it is sufficient to insert a single nucleotide of any type between the two 'T's in the sequence to restore the functionality of the protein.", "sample_inputs": ["GTTAAAG", "AACCAACCAAAAC"], "sample_outputs": ["1", "5"], "tags": ["implementation", "two pointers"], "src_uid": "8b26ca1ca2b28166c3d25dceb1f3d49f", "difficulty": null} {"description": "The employees of the F company have lots of ways to entertain themselves. Today they invited a famous magician who shows a trick with plastic cups and a marble.The point is to trick the spectator's attention. Initially, the spectator stands in front of a line of n plastic cups. Then the magician places a small marble under one cup and shuffles the cups. Then the spectator should guess which cup hides the marble.But the head coder of the F company isn't easy to trick. When he saw the performance, he noticed several important facts: each cup contains a mark \u2014 a number from 1 to n; all marks on the cups are distinct; the magician shuffles the cups in m operations, each operation looks like that: take a cup marked xi, sitting at position yi in the row of cups (the positions are numbered from left to right, starting from 1) and shift it to the very beginning of the cup row (on the first position). When the head coder came home after work he wanted to re-do the trick. Unfortunately, he didn't remember the starting or the final position of the cups. He only remembered which operations the magician performed. Help the coder: given the operations in the order they were made find at least one initial permutation of the cups that can go through the described operations in the given order. Otherwise, state that such permutation doesn't exist.", "input_from": "standard input", "output_to": "standard output", "time_limit": "3 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009106). Each of the next m lines contains a couple of integers. The i-th line contains integers xi, yi (1\u2009\u2264\u2009xi,\u2009yi\u2009\u2264\u2009n) \u2014 the description of the i-th operation of the magician. Note that the operations are given in the order in which the magician made them and the coder wants to make them in the same order.", "output_spec": "If the described permutation doesn't exist (the programmer remembered wrong operations), print -1. Otherwise, print n distinct integers, each from 1 to n: the i-th number should represent the mark on the cup that initially is in the row in position i. If there are multiple correct answers, you should print the lexicographically minimum one.", "notes": null, "sample_inputs": ["2 1\n2 1", "3 2\n1 2\n1 1", "3 3\n1 3\n2 3\n1 3"], "sample_outputs": ["2 1", "2 1 3", "-1"], "tags": ["data structures"], "src_uid": "a2616b1681f30ce4b2a5fdc81cf52b50", "difficulty": 2200} {"description": "Let's assume that set S consists of m distinct intervals [l1,\u2009r1], [l2,\u2009r2], ..., [lm,\u2009rm] (1\u2009\u2264\u2009li\u2009\u2264\u2009ri\u2009\u2264\u2009n; li,\u2009ri are integers).Let's assume that f(S) is the maximum number of intervals that you can choose from the set S, such that every two of them do not intersect. We assume that two intervals, [l1,\u2009r1] and [l2,\u2009r2], intersect if there is an integer x, which meets two inequalities: l1\u2009\u2264\u2009x\u2009\u2264\u2009r1 and l2\u2009\u2264\u2009x\u2009\u2264\u2009r2.Sereja wonders, how many sets S are there, such that f(S)\u2009=\u2009k? Count this number modulo 1000000007 (109\u2009+\u20097).", "input_from": "standard input", "output_to": "standard output", "time_limit": "1.5 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains integers n, k (1\u2009\u2264\u2009n\u2009\u2264\u2009500;\u00a00\u2009\u2264\u2009k\u2009\u2264\u2009500).", "output_spec": "In a single line, print the answer to the problem modulo 1000000007 (109\u2009+\u20097).", "notes": null, "sample_inputs": ["3 1", "3 2", "2 0", "2 2"], "sample_outputs": ["23", "32", "1", "2"], "tags": ["dp"], "src_uid": "111673158df2e37ac6c019bb99225ccb", "difficulty": 2500} {"description": "Iahub is training for the IOI. What is a better way to train than playing a Zuma-like game? There are n balls put in a row. Each ball is colored in one of k colors. Initially the row doesn't contain three or more contiguous balls with the same color. Iahub has a single ball of color x. He can insert his ball at any position in the row (probably, between two other balls). If at any moment there are three or more contiguous balls of the same color in the row, they are destroyed immediately. This rule is applied multiple times, until there are no more sets of 3 or more contiguous balls of the same color. For example, if Iahub has the row of balls [black, black, white, white, black, black] and a white ball, he can insert the ball between two white balls. Thus three white balls are destroyed, and then four black balls become contiguous, so all four balls are destroyed. The row will not contain any ball in the end, so Iahub can destroy all 6 balls.Iahub wants to destroy as many balls as possible. You are given the description of the row of balls, and the color of Iahub's ball. Help Iahub train for the IOI by telling him the maximum number of balls from the row he can destroy.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line of input contains three integers: n (1\u2009\u2264\u2009n\u2009\u2264\u2009100), k (1\u2009\u2264\u2009k\u2009\u2264\u2009100) and x (1\u2009\u2264\u2009x\u2009\u2264\u2009k). The next line contains n space-separated integers c1,\u2009c2,\u2009...,\u2009cn (1\u2009\u2264\u2009ci\u2009\u2264\u2009k). Number ci means that the i-th ball in the row has color ci. It is guaranteed that the initial row of balls will never contain three or more contiguous balls of the same color. ", "output_spec": "Print a single integer \u2014 the maximum number of balls Iahub can destroy.", "notes": null, "sample_inputs": ["6 2 2\n1 1 2 2 1 1", "1 1 1\n1"], "sample_outputs": ["6", "0"], "tags": ["brute force", "two pointers"], "src_uid": "d73d9610e3800817a3109314b1e6f88c", "difficulty": 1400} {"description": "Ann has recently started commuting by subway. We know that a one ride subway ticket costs a rubles. Besides, Ann found out that she can buy a special ticket for m rides (she can buy it several times). It costs b rubles. Ann did the math; she will need to use subway n times. Help Ann, tell her what is the minimum sum of money she will have to spend to make n rides?", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The single line contains four space-separated integers n, m, a, b (1\u2009\u2264\u2009n,\u2009m,\u2009a,\u2009b\u2009\u2264\u20091000) \u2014 the number of rides Ann has planned, the number of rides covered by the m ride ticket, the price of a one ride ticket and the price of an m ride ticket. ", "output_spec": "Print a single integer \u2014 the minimum sum in rubles that Ann will need to spend.", "notes": "NoteIn the first sample one of the optimal solutions is: each time buy a one ride ticket. There are other optimal solutions. For example, buy three m ride tickets.", "sample_inputs": ["6 2 1 2", "5 2 2 3"], "sample_outputs": ["6", "8"], "tags": ["implementation"], "src_uid": "faa343ad6028c5a069857a38fa19bb24", "difficulty": 1200} {"description": "Pasha has a positive integer a without leading zeroes. Today he decided that the number is too small and he should make it larger. Unfortunately, the only operation Pasha can do is to swap two adjacent decimal digits of the integer.Help Pasha count the maximum number he can get if he has the time to make at most k swaps.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The single line contains two integers a and k (1\u2009\u2264\u2009a\u2009\u2264\u20091018;\u00a00\u2009\u2264\u2009k\u2009\u2264\u2009100).", "output_spec": "Print the maximum number that Pasha can get if he makes at most k swaps.", "notes": null, "sample_inputs": ["1990 1", "300 0", "1034 2", "9090000078001234 6"], "sample_outputs": ["9190", "300", "3104", "9907000008001234"], "tags": ["greedy"], "src_uid": "e56f6c343167745821f0b18dcf0d0cde", "difficulty": 1400} {"description": "This winter is so cold in Nvodsk! A group of n friends decided to buy k bottles of a soft drink called \"Take-It-Light\" to warm up a bit. Each bottle has l milliliters of the drink. Also they bought c limes and cut each of them into d slices. After that they found p grams of salt.To make a toast, each friend needs nl milliliters of the drink, a slice of lime and np grams of salt. The friends want to make as many toasts as they can, provided they all drink the same amount. How many toasts can each friend make?", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first and only line contains positive integers n, k, l, c, d, p, nl, np, not exceeding 1000 and no less than 1. The numbers are separated by exactly one space.", "output_spec": "Print a single integer \u2014 the number of toasts each friend can make.", "notes": "NoteA comment to the first sample: Overall the friends have 4\u2009*\u20095\u2009=\u200920 milliliters of the drink, it is enough to make 20\u2009/\u20093\u2009=\u20096 toasts. The limes are enough for 10\u2009*\u20098\u2009=\u200980 toasts and the salt is enough for 100\u2009/\u20091\u2009=\u2009100 toasts. However, there are 3 friends in the group, so the answer is min(6,\u200980,\u2009100)\u2009/\u20093\u2009=\u20092.", "sample_inputs": ["3 4 5 10 8 100 3 1", "5 100 10 1 19 90 4 3", "10 1000 1000 25 23 1 50 1"], "sample_outputs": ["2", "3", "0"], "tags": ["math", "implementation"], "src_uid": "67410b7d36b9d2e6a97ca5c7cff317c1", "difficulty": 800} {"description": "Lavrenty, a baker, is going to make several buns with stuffings and sell them.Lavrenty has n grams of dough as well as m different stuffing types. The stuffing types are numerated from 1 to m. Lavrenty knows that he has ai grams left of the i-th stuffing. It takes exactly bi grams of stuffing i and ci grams of dough to cook a bun with the i-th stuffing. Such bun can be sold for di tugriks.Also he can make buns without stuffings. Each of such buns requires c0 grams of dough and it can be sold for d0 tugriks. So Lavrenty can cook any number of buns with different stuffings or without it unless he runs out of dough and the stuffings. Lavrenty throws away all excess material left after baking.Find the maximum number of tugriks Lavrenty can earn.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains 4 integers n, m, c0 and d0 (1\u2009\u2264\u2009n\u2009\u2264\u20091000, 1\u2009\u2264\u2009m\u2009\u2264\u200910, 1\u2009\u2264\u2009c0,\u2009d0\u2009\u2264\u2009100). Each of the following m lines contains 4 integers. The i-th line contains numbers ai, bi, ci and di (1\u2009\u2264\u2009ai,\u2009bi,\u2009ci,\u2009di\u2009\u2264\u2009100).", "output_spec": "Print the only number \u2014 the maximum number of tugriks Lavrenty can earn.", "notes": "NoteTo get the maximum number of tugriks in the first sample, you need to cook 2 buns with stuffing 1, 4 buns with stuffing 2 and a bun without any stuffing.In the second sample Lavrenty should cook 4 buns without stuffings.", "sample_inputs": ["10 2 2 1\n7 3 2 100\n12 3 1 10", "100 1 25 50\n15 5 20 10"], "sample_outputs": ["241", "200"], "tags": ["dp"], "src_uid": "4e166b8b44427b1227e0f811161d3a6f", "difficulty": 1700} {"description": "Little Petya loves training spiders. Petya has a board n\u2009\u00d7\u2009m in size. Each cell of the board initially has a spider sitting on it. After one second Petya chooses a certain action for each spider, and all of them humbly perform its commands. There are 5 possible commands: to stay idle or to move from current cell to some of the four side-neighboring cells (that is, one command for each of the four possible directions). Petya gives the commands so that no spider leaves the field. It is allowed for spiders to pass through each other when they crawl towards each other in opposite directions. All spiders crawl simultaneously and several spiders may end up in one cell. Petya wants to know the maximum possible number of spider-free cells after one second.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains two space-separated integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u200940,\u2009n\u00b7m\u2009\u2264\u200940) \u2014 the board sizes.", "output_spec": "In the first line print the maximum number of cells without spiders.", "notes": "NoteIn the first sample the only possible answer is:sIn the second sample one of the possible solutions is: rdlruls denotes command \"stay idle\", l, r, d, u denote commands \"crawl left\", \"crawl right\", \"crawl down\", \"crawl up\", correspondingly.", "sample_inputs": ["1 1", "2 3"], "sample_outputs": ["0", "4"], "tags": ["bitmasks", "dp", "dsu"], "src_uid": "097674b4dd696b30e102938f71dd39b9", "difficulty": 2100} {"description": "Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal representation contains only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.One day Petya dreamt of a lexicographically k-th permutation of integers from 1 to n. Determine how many lucky numbers in the permutation are located on the positions whose indexes are also lucky numbers.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers n and k (1\u2009\u2264\u2009n,\u2009k\u2009\u2264\u2009109) \u2014 the number of elements in the permutation and the lexicographical number of the permutation.", "output_spec": "If the k-th permutation of numbers from 1 to n does not exist, print the single number \"-1\" (without the quotes). Otherwise, print the answer to the problem: the number of such indexes i, that i and ai are both lucky numbers.", "notes": "NoteA permutation is an ordered set of n elements, where each integer from 1 to n occurs exactly once. The element of permutation in position with index i is denoted as ai (1\u2009\u2264\u2009i\u2009\u2264\u2009n). Permutation a is lexicographically smaller that permutation b if there is such a i (1\u2009\u2264\u2009i\u2009\u2264\u2009n), that ai\u2009<\u2009bi, and for any j (1\u2009\u2264\u2009j\u2009<\u2009i) aj\u2009=\u2009bj. Let's make a list of all possible permutations of n elements and sort it in the order of lexicographical increasing. Then the lexicographically k-th permutation is the k-th element of this list of permutations.In the first sample the permutation looks like that:1 2 3 4 6 7 5The only suitable position is 4.In the second sample the permutation looks like that:2 1 3 4The only suitable position is 4.", "sample_inputs": ["7 4", "4 7"], "sample_outputs": ["1", "1"], "tags": ["brute force", "combinatorics", "number theory"], "src_uid": "cb2aa02772f95fefd1856960b6ceac4c", "difficulty": 1900} {"description": "Little Petya very much likes strings. Recently he has received a voucher to purchase a string as a gift from his mother. The string can be bought in the local shop. One can consider that the shop has all sorts of strings over the alphabet of fixed size. The size of the alphabet is equal to k. However, the voucher has a string type limitation: specifically, the voucher can be used to purchase string s if the length of string's longest substring that is also its weak subsequence (see the definition given below) equals w.String a with the length of n is considered the weak subsequence of the string s with the length of m, if there exists such a set of indexes 1\u2009\u2264\u2009i1\u2009<\u2009i2\u2009<\u2009...\u2009<\u2009in\u2009\u2264\u2009m, that has the following two properties: ak\u2009=\u2009sik for all k from 1 to n; there exists at least one such k (1\u2009\u2264\u2009k\u2009<\u2009n), for which ik\u2009+\u20091\u2009\u2013\u2009ik\u2009>\u20091. Petya got interested how many different strings are available for him to purchase in the shop. As the number of strings can be very large, please find it modulo 1000000007 (109\u2009+\u20097). If there are infinitely many such strings, print \"-1\".", "input_from": "standard input", "output_to": "standard output", "time_limit": "3 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers k (1\u2009\u2264\u2009k\u2009\u2264\u2009106) and w (2\u2009\u2264\u2009w\u2009\u2264\u2009109) \u2014 the alphabet size and the required length of the maximum substring that also is the weak subsequence, correspondingly.", "output_spec": "Print a single number \u2014 the number of strings Petya can buy using the voucher, modulo 1000000007 (109\u2009+\u20097). If there are infinitely many such strings, print \"-1\" (without the quotes).", "notes": "NoteIn the first sample Petya can buy the following strings: aaa, aab, abab, abb, abba, baa, baab, baba, bba, bbb.", "sample_inputs": ["2 2", "3 5", "2 139"], "sample_outputs": ["10", "1593", "717248223"], "tags": ["combinatorics"], "src_uid": "b715f0fdc83ec539eb3ae2b0371ee130", "difficulty": 3000} {"description": "Life is not easy for the perfectly common variable named Vasya. Wherever it goes, it is either assigned a value, or simply ignored, or is being used!Vasya's life goes in states of a program. In each state, Vasya can either be used (for example, to calculate the value of another variable), or be assigned a value, or ignored. Between some states are directed (oriented) transitions.A path is a sequence of states v1,\u2009v2,\u2009...,\u2009vx, where for any 1\u2009\u2264\u2009i\u2009<\u2009x exists a transition from vi to vi\u2009+\u20091.Vasya's value in state v is interesting to the world, if exists path p1,\u2009p2,\u2009...,\u2009pk such, that pi\u2009=\u2009v for some i (1\u2009\u2264\u2009i\u2009\u2264\u2009k), in state p1 Vasya gets assigned a value, in state pk Vasya is used and there is no state pi (except for p1) where Vasya gets assigned a value.Help Vasya, find the states in which Vasya's value is interesting to the world.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains two space-separated integers n and m (1\u2009\u2264\u2009n,\u2009m\u2009\u2264\u2009105) \u2014 the numbers of states and transitions, correspondingly. The second line contains space-separated n integers f1,\u2009f2,\u2009...,\u2009fn (0\u2009\u2264\u2009fi\u2009\u2264\u20092), fi described actions performed upon Vasya in state i: 0 represents ignoring, 1 \u2014 assigning a value, 2 \u2014 using. Next m lines contain space-separated pairs of integers ai,\u2009bi (1\u2009\u2264\u2009ai,\u2009bi\u2009\u2264\u2009n, ai\u2009\u2260\u2009bi), each pair represents the transition from the state number ai to the state number bi. Between two states can be any number of transitions.", "output_spec": "Print n integers r1,\u2009r2,\u2009...,\u2009rn, separated by spaces or new lines. Number ri should equal 1, if Vasya's value in state i is interesting to the world and otherwise, it should equal 0. The states are numbered from 1 to n in the order, in which they are described in the input.", "notes": "NoteIn the first sample the program states can be used to make the only path in which the value of Vasya interests the world, 1 2 3 4; it includes all the states, so in all of them Vasya's value is interesting to the world.The second sample the only path in which Vasya's value is interesting to the world is , \u2014 1 3; state 2 is not included there.In the third sample we cannot make from the states any path in which the value of Vasya would be interesting to the world, so the value of Vasya is never interesting to the world.", "sample_inputs": ["4 3\n1 0 0 2\n1 2\n2 3\n3 4", "3 1\n1 0 2\n1 3", "3 1\n2 0 1\n1 3"], "sample_outputs": ["1\n1\n1\n1", "1\n0\n1", "0\n0\n0"], "tags": ["graphs"], "src_uid": "87d869a0fd4a510c5e7e310886b86a57", "difficulty": 1700} {"description": "To get money for a new aeonic blaster, ranger Qwerty decided to engage in trade for a while. He wants to buy some number of items (or probably not to buy anything at all) on one of the planets, and then sell the bought items on another planet. Note that this operation is not repeated, that is, the buying and the selling are made only once. To carry out his plan, Qwerty is going to take a bank loan that covers all expenses and to return the loaned money at the end of the operation (the money is returned without the interest). At the same time, Querty wants to get as much profit as possible.The system has n planets in total. On each of them Qwerty can buy or sell items of m types (such as food, medicine, weapons, alcohol, and so on). For each planet i and each type of items j Qwerty knows the following: aij \u2014 the cost of buying an item; bij \u2014 the cost of selling an item; cij \u2014 the number of remaining items.It is not allowed to buy more than cij items of type j on planet i, but it is allowed to sell any number of items of any kind.Knowing that the hold of Qwerty's ship has room for no more than k items, determine the maximum profit which Qwerty can get.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains three space-separated integers n, m and k (2\u2009\u2264\u2009n\u2009\u2264\u200910, 1\u2009\u2264\u2009m,\u2009k\u2009\u2264\u2009100) \u2014 the number of planets, the number of question types and the capacity of Qwerty's ship hold, correspondingly. Then follow n blocks describing each planet. The first line of the i-th block has the planet's name as a string with length from 1 to 10 Latin letters. The first letter of the name is uppercase, the rest are lowercase. Then in the i-th block follow m lines, the j-th of them contains three integers aij, bij and cij (1\u2009\u2264\u2009bij\u2009<\u2009aij\u2009\u2264\u20091000, 0\u2009\u2264\u2009cij\u2009\u2264\u2009100) \u2014 the numbers that describe money operations with the j-th item on the i-th planet. The numbers in the lines are separated by spaces. It is guaranteed that the names of all planets are different.", "output_spec": "Print a single number \u2014 the maximum profit Qwerty can get.", "notes": "NoteIn the first test case you should fly to planet Venus, take a loan on 74 units of money and buy three items of the first type and 7 items of the third type (3\u00b76\u2009+\u20097\u00b78\u2009=\u200974). Then the ranger should fly to planet Earth and sell there all the items he has bought. He gets 3\u00b79\u2009+\u20097\u00b79\u2009=\u200990 units of money for the items, he should give 74 of them for the loan. The resulting profit equals 16 units of money. We cannot get more profit in this case.", "sample_inputs": ["3 3 10\nVenus\n6 5 3\n7 6 5\n8 6 10\nEarth\n10 9 0\n8 6 4\n10 9 3\nMars\n4 3 0\n8 4 12\n7 2 5"], "sample_outputs": ["16"], "tags": ["games", "graph matchings", "greedy"], "src_uid": "7419c4268a9815282fadca6581f28ec1", "difficulty": 1200} {"description": "Several ages ago Berland was a kingdom. The King of Berland adored math. That's why, when he first visited one of his many palaces, he first of all paid attention to the floor in one hall. The floor was tiled with hexagonal tiles.The hall also turned out hexagonal in its shape. The King walked along the perimeter of the hall and concluded that each of the six sides has a, b, c, a, b and c adjacent tiles, correspondingly.To better visualize the situation, look at the picture showing a similar hexagon for a\u2009=\u20092, b\u2009=\u20093 and c\u2009=\u20094. According to the legend, as the King of Berland obtained the values a, b and c, he almost immediately calculated the total number of tiles on the hall floor. Can you do the same?", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains three integers: a, b and c (2\u2009\u2264\u2009a,\u2009b,\u2009c\u2009\u2264\u20091000).", "output_spec": "Print a single number \u2014 the total number of tiles on the hall floor.", "notes": null, "sample_inputs": ["2 3 4"], "sample_outputs": ["18"], "tags": ["math", "implementation"], "src_uid": "8ab25ed4955d978fe20f6872cb94b0da", "difficulty": 1200} {"description": "Polycarpus is an amateur businessman. Recently he was surprised to find out that the market for paper scissors is completely free! Without further ado, Polycarpus decided to start producing and selling such scissors.Polycaprus calculated that the optimal celling price for such scissors would be p bourles. However, he read somewhere that customers are attracted by prices that say something like \"Special Offer! Super price 999 bourles!\". So Polycarpus decided to lower the price a little if it leads to the desired effect.Polycarpus agrees to lower the price by no more than d bourles so that the number of nines at the end of the resulting price is maximum. If there are several ways to do it, he chooses the maximum possible price.Note, Polycarpus counts only the trailing nines in a price.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains two integers p and d (1\u2009\u2264\u2009p\u2009\u2264\u20091018; 0\u2009\u2264\u2009d\u2009<\u2009p) \u2014 the initial price of scissors and the maximum possible price reduction. Please, do not use the %lld specifier to read or write 64-bit integers in \u0421++. It is preferred to use cin, cout streams or the %I64d specifier.", "output_spec": "Print the required price \u2014 the maximum price that ends with the largest number of nines and that is less than p by no more than d. The required number shouldn't have leading zeroes.", "notes": null, "sample_inputs": ["1029 102", "27191 17"], "sample_outputs": ["999", "27189"], "tags": ["implementation"], "src_uid": "c706cfcd4c37fbc1b1631aeeb2c02b6a", "difficulty": 1400} {"description": "The Little Elephant is playing with the Cartesian coordinates' system. Most of all he likes playing with integer points. The Little Elephant defines an integer point as a pair of integers (x;\u00a0y), such that 0\u2009\u2264\u2009x\u2009\u2264\u2009w and 0\u2009\u2264\u2009y\u2009\u2264\u2009h. Thus, the Little Elephant knows only (w\u2009+\u20091)\u00b7(h\u2009+\u20091) distinct integer points.The Little Elephant wants to paint a triangle with vertexes at integer points, the triangle's area must be a positive integer. For that, he needs to find the number of groups of three points that form such triangle. At that, the order of points in a group matters, that is, the group of three points (0;0), (0;2), (2;2) isn't equal to the group (0;2), (0;0), (2;2).Help the Little Elephant to find the number of groups of three integer points that form a nondegenerate triangle with integer area.", "input_from": "standard input", "output_to": "standard output", "time_limit": "3 seconds", "memory_limit": "256 megabytes", "input_spec": "A single line contains two integers w and h (1\u2009\u2264\u2009w,\u2009h\u2009\u2264\u20094000).", "output_spec": "In a single output line print an integer \u2014 the remainder of dividing the answer to the problem by 1000000007 (109\u2009+\u20097).", "notes": null, "sample_inputs": ["2 1", "2 2"], "sample_outputs": ["36", "240"], "tags": ["geometry", "math"], "src_uid": "984788e4b4925c15c9c6f31e42f2f8fa", "difficulty": 2500} {"description": "Polycarpus loves lucky numbers. Everybody knows that lucky numbers are positive integers, whose decimal representation (without leading zeroes) contain only the lucky digits x and y. For example, if x\u2009=\u20094, and y\u2009=\u20097, then numbers 47, 744, 4 are lucky.Let's call a positive integer a undoubtedly lucky, if there are such digits x and y (0\u2009\u2264\u2009x,\u2009y\u2009\u2264\u20099), that the decimal representation of number a (without leading zeroes) contains only digits x and y.Polycarpus has integer n. He wants to know how many positive integers that do not exceed n, are undoubtedly lucky. Help him, count this number.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains a single integer n (1\u2009\u2264\u2009n\u2009\u2264\u2009109) \u2014 Polycarpus's number.", "output_spec": "Print a single integer that says, how many positive integers that do not exceed n are undoubtedly lucky.", "notes": "NoteIn the first test sample all numbers that do not exceed 10 are undoubtedly lucky.In the second sample numbers 102, 103, 104, 105, 106, 107, 108, 109, 120, 123 are not undoubtedly lucky.", "sample_inputs": ["10", "123"], "sample_outputs": ["10", "113"], "tags": ["brute force", "bitmasks", "dfs and similar"], "src_uid": "0f7f10557602c8c2f2eb80762709ffc4", "difficulty": 1600} {"description": "One hot summer day Pete and his friend Billy decided to buy a watermelon. They chose the biggest and the ripest one, in their opinion. After that the watermelon was weighed, and the scales showed w kilos. They rushed home, dying of thirst, and decided to divide the berry, however they faced a hard problem.Pete and Billy are great fans of even numbers, that's why they want to divide the watermelon in such a way that each of the two parts weighs even number of kilos, at the same time it is not obligatory that the parts are equal. The boys are extremely tired and want to start their meal as soon as possible, that's why you should help them and find out, if they can divide the watermelon in the way they want. For sure, each of them should get a part of positive weight.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "64 megabytes", "input_spec": "The first (and the only) input line contains integer number w (1\u2009\u2264\u2009w\u2009\u2264\u2009100) \u2014 the weight of the watermelon bought by the boys.", "output_spec": "Print YES, if the boys can divide the watermelon into two parts, each of them weighing even number of kilos; and NO in the opposite case.", "notes": "NoteFor example, the boys can divide the watermelon into two parts of 2 and 6 kilos respectively (another variant \u2014 two parts of 4 and 4 kilos).", "sample_inputs": ["8"], "sample_outputs": ["YES"], "tags": ["brute force", "math"], "src_uid": "230a3c4d7090401e5fa3c6b9d994cdf2", "difficulty": 800} {"description": "Bob likes to draw camels: with a single hump, two humps, three humps, etc. He draws a camel by connecting points on a coordinate plane. Now he's drawing camels with t humps, representing them as polylines in the plane. Each polyline consists of n vertices with coordinates (x1,\u2009y1), (x2,\u2009y2), ..., (xn,\u2009yn). The first vertex has a coordinate x1\u2009=\u20091, the second \u2014 x2\u2009=\u20092, etc. Coordinates yi might be any, but should satisfy the following conditions: there should be t humps precisely, i.e. such indexes j (2\u2009\u2264\u2009j\u2009\u2264\u2009n\u2009-\u20091), so that yj\u2009-\u20091\u2009<\u2009yj\u2009>\u2009yj\u2009+\u20091, there should be precisely t\u2009-\u20091 such indexes j (2\u2009\u2264\u2009j\u2009\u2264\u2009n\u2009-\u20091), so that yj\u2009-\u20091\u2009>\u2009yj\u2009<\u2009yj\u2009+\u20091, no segment of a polyline should be parallel to the Ox-axis, all yi are integers between 1 and 4. For a series of his drawings of camels with t humps Bob wants to buy a notebook, but he doesn't know how many pages he will need. Output the amount of different polylines that can be drawn to represent camels with t humps for a given number n.", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "64 megabytes", "input_spec": "The first line contains a pair of integers n and t (3\u2009\u2264\u2009n\u2009\u2264\u200920, 1\u2009\u2264\u2009t\u2009\u2264\u200910).", "output_spec": "Output the required amount of camels with t humps.", "notes": "NoteIn the first sample test sequences of y-coordinates for six camels are: 123421, 123431, 123432, 124321, 134321 \u0438 234321 (each digit corresponds to one value of yi).", "sample_inputs": ["6 1", "4 2"], "sample_outputs": ["6", "0"], "tags": ["dp"], "src_uid": "6d67559744583229455c5eafe68f7952", "difficulty": 1900} {"description": "A car moves from point A to point B at speed v meters per second. The action takes place on the X-axis. At the distance d meters from A there are traffic lights. Starting from time 0, for the first g seconds the green light is on, then for the following r seconds the red light is on, then again the green light is on for the g seconds, and so on.The car can be instantly accelerated from 0 to v and vice versa, can instantly slow down from the v to 0. Consider that it passes the traffic lights at the green light instantly. If the car approaches the traffic lights at the moment when the red light has just turned on, it doesn't have time to pass it. But if it approaches the traffic lights at the moment when the green light has just turned on, it can move. The car leaves point A at the time 0.What is the minimum time for the car to get from point A to point B without breaking the traffic rules?", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains integers l, d, v, g, r (1\u2009\u2264\u2009l,\u2009d,\u2009v,\u2009g,\u2009r\u2009\u2264\u20091000,\u2009d\u2009<\u2009l) \u2014 the distance between A and B (in meters), the distance from A to the traffic lights, car's speed, the duration of green light and the duration of red light.", "output_spec": "Output a single number \u2014 the minimum time that the car needs to get from point A to point B. Your output must have relative or absolute error less than 10\u2009-\u20096.", "notes": null, "sample_inputs": ["2 1 3 4 5", "5 4 3 1 1"], "sample_outputs": ["0.66666667", "2.33333333"], "tags": ["implementation"], "src_uid": "e4a4affb439365c843c9f9828d81b42c", "difficulty": 1500} {"description": "Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.", "input_from": "standard input", "output_to": "standard output", "time_limit": "4 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line of the input contains the number of cases t (1\u2009\u2264\u2009t\u2009\u2264\u200910). Each of the next t lines contains two natural numbers li and ri (1\u2009\u2264\u2009li\u2009\u2264\u2009ri\u2009\u2264\u20099\u2009\u00b71018). Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).", "output_spec": "Output should contain t numbers \u2014 answers to the queries, one number per line \u2014 quantities of beautiful numbers in given intervals (from li to ri, inclusively).", "notes": null, "sample_inputs": ["1\n1 9", "1\n12 15"], "sample_outputs": ["9", "2"], "tags": ["dp", "number theory"], "src_uid": "37feadce373f728ba2a560b198ca4bc9", "difficulty": 2500} {"description": "Vasya has recently learned to type and log on to the Internet. He immediately entered a chat room and decided to say hello to everybody. Vasya typed the word s. It is considered that Vasya managed to say hello if several letters can be deleted from the typed word so that it resulted in the word \"hello\". For example, if Vasya types the word \"ahhellllloou\", it will be considered that he said hello, and if he types \"hlelo\", it will be considered that Vasya got misunderstood and he didn't manage to say hello. Determine whether Vasya managed to say hello by the given word s.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first and only line contains the word s, which Vasya typed. This word consisits of small Latin letters, its length is no less that 1 and no more than 100 letters.", "output_spec": "If Vasya managed to say hello, print \"YES\", otherwise print \"NO\".", "notes": null, "sample_inputs": ["ahhellllloou", "hlelo"], "sample_outputs": ["YES", "NO"], "tags": ["strings", "greedy"], "src_uid": "c5d19dc8f2478ee8d9cba8cc2e4cd838", "difficulty": 1000} {"description": "Sometimes one has to spell email addresses over the phone. Then one usually pronounces a dot as dot, an at sign as at. As a result, we get something like vasyaatgmaildotcom. Your task is to transform it into a proper email address (vasya@gmail.com). It is known that a proper email address contains only such symbols as . @ and lower-case Latin letters, doesn't start with and doesn't end with a dot. Also, a proper email address doesn't start with and doesn't end with an at sign. Moreover, an email address contains exactly one such symbol as @, yet may contain any number (possible, zero) of dots. You have to carry out a series of replacements so that the length of the result was as short as possible and it was a proper email address. If the lengths are equal, you should print the lexicographically minimal result. Overall, two variants of replacement are possible: dot can be replaced by a dot, at can be replaced by an at. ", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "The first line contains the email address description. It is guaranteed that that is a proper email address with all the dots replaced by dot an the at signs replaced by at. The line is not empty and its length does not exceed 100 symbols.", "output_spec": "Print the shortest email address, from which the given line could be made by the described above replacements. If there are several solutions to that problem, print the lexicographically minimal one (the lexicographical comparison of the lines are implemented with an operator < in modern programming languages). In the ASCII table the symbols go in this order: . @ ab...z", "notes": null, "sample_inputs": ["vasyaatgmaildotcom", "dotdotdotatdotdotat", "aatt"], "sample_outputs": ["vasya@gmail.com", "dot..@..at", "a@t"], "tags": ["expression parsing", "implementation"], "src_uid": "a11c9679d8e2dca51be17d466202df6e", "difficulty": 1300} {"description": "Fangy collects cookies. Once he decided to take a box and put cookies into it in some way. If we take a square k\u2009\u00d7\u2009k in size, divided into blocks 1\u2009\u00d7\u20091 in size and paint there the main diagonal together with cells, which lie above it, then the painted area will be equal to the area occupied by one cookie k in size. Fangy also has a box with a square base 2n\u2009\u00d7\u20092n, divided into blocks 1\u2009\u00d7\u20091 in size. In a box the cookies should not overlap, and they should not be turned over or rotated. See cookies of sizes 2 and 4 respectively on the figure: To stack the cookies the little walrus uses the following algorithm. He takes out of the repository the largest cookie which can fit in some place in the box and puts it there. Everything could be perfect but alas, in the repository the little walrus has infinitely many cookies of size 2 and larger, and there are no cookies of size 1, therefore, empty cells will remain in the box. Fangy wants to know how many empty cells will be left in the end.", "input_from": "standard input", "output_to": "standard output", "time_limit": "1 second", "memory_limit": "256 megabytes", "input_spec": "The first line contains a single integer n (0\u2009\u2264\u2009n\u2009\u2264\u20091000).", "output_spec": "Print the single number, equal to the number of empty cells in the box. The answer should be printed modulo 106\u2009+\u20093.", "notes": "NoteIf the box possesses the base of 23\u2009\u00d7\u200923 (as in the example), then the cookies will be put there in the following manner: ", "sample_inputs": ["3"], "sample_outputs": ["9"], "tags": ["math"], "src_uid": "1a335a9638523ca0315282a67e18eec7", "difficulty": 1300} {"description": "For each positive integer n consider the integer \u03c8(n) which is obtained from n by replacing every digit a in the decimal notation of n with the digit (9\u2009\u2009-\u2009\u2009a). We say that \u03c8(n) is the reflection of n. For example, reflection of 192 equals 807. Note that leading zeros (if any) should be omitted. So reflection of 9 equals 0, reflection of 91 equals 8.Let us call the weight of the number the product of the number and its reflection. Thus, the weight of the number 10 is equal to 10\u00b789\u2009=\u2009890.Your task is to find the maximum weight of the numbers in the given range [l,\u2009r] (boundaries are included).", "input_from": "standard input", "output_to": "standard output", "time_limit": "2 seconds", "memory_limit": "256 megabytes", "input_spec": "Input contains two space-separated integers l and r (1\u2009\u2264\u2009l\u2009\u2264\u2009r\u2009\u2264\u2009109) \u2014 bounds of the range.", "output_spec": "Output should contain single integer number: maximum value of the product n\u00b7\u03c8(n), where l\u2009\u2264\u2009n\u2009\u2264\u2009r. Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preferred to use cout (also you may use %I64d).", "notes": "NoteIn the third sample weight of 8 equals 8\u00b71\u2009=\u20098, weight of 9 equals 9\u00b70\u2009=\u20090, weight of 10 equals 890.Thus, maximum value of the product is equal to 890.", "sample_inputs": ["3 7", "1 1", "8 10"], "sample_outputs": ["20", "8", "890"], "tags": ["math"], "src_uid": "2c4b2a162563242cb2f43f6209b59d5e", "difficulty": 1600}