Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Task A. Alchemist Our hero Stancho is not only a programmer but an alchemist also. He succeeds to find the philosophical stone, with using of which he is transforming some combinations of chemical elements to another combinations of chemical elements. Each of the elements enters in the alchemist reaction with mass 1 gram and each obtained element is also with mass 1 gram (it is clear that to escape the law for conservation of the masses is not a big problem for Stancho). Stancho is starting with 1 gram of lead, then he can include the obtained elements in new reaction, then in new, and so on, trying to obtain as much as possible gold. Write a program that for given list of reactions is finding the maximal quantity of gold that Stancho is able to obtain. Input On the first row of the standard input the number of the test cases will be given. Each test case will start with the number K of different elements that could participate in reactions (2K6). The second line is a list of the elements, separated by spaces (lead and gold are always in the list). Names of the elements are not longer than 10 letters. In the third line the number L of possible reactions will be given (1L100). Each two consecutive of the last 2L lines contain the description of one alchemist reaction: the list of input elements is given on the first, the output elements – on the second. All names in the lists are separated by spaces. Output For each test case the program has to print the maximal quantity of gold that Stancho is able to obtain or the text AS MANY AS NECESSARY if it is possible to obtain any quantity of gold. Sample input 1 5 lead gold sulfur mercury copper 5 lead gold sulfur mercury lead gold copper mercury mercury gold sulfur mercury copper mercury sulfur gold sulfur mercury gold Sample output 3 Task B. Beer In the programming Stancho is not very strong. But in drinking beer – yes, he is. That is why, for his birthday he decided to surprise his friends by proposing to each of them a collection of beers as follows. In the pub where the birthday will be celebrated many different kind of beers are proposed. Stancho has N friends. To each of them Stancho would like to propose the same number of bottles. The collection of each friend has to be different but with the same prize. It is possible that for some kinds of beer more than one bottle is included in the collection. For example, 2 bottles of Tuborg and 1 bottle of Guiness is different collection from 1 bottle of Tuborg and 2 bottles of Guiness. The pub is promising that from each kind of beer there is (theoretically) unlimited quantity of bottles. Write a program which composes a different collection for each of Stancho’s friends. If more than one solution exists then the program has to find one of them. Input On the first row of the standard input the number of the test cases will be given. Each test case will start with the number N of friends (1N100). In the second line the number K of different kinds of beer will be given (1K100). Each of the last K lines will contain the name of the beer (string of length less then or equal 10) and the price of the beer — an integer less then or equal 32767 GBU (generalized beer units). Output For each of the test cases the program has to print N lines, one line for each collection. The description of the collection is a list of couples name – quantity. All names and numbers in the list have to be separated by spaces. If there is no solution, then the file has to contain only one line for this test case with the word NO. Sample input 1 2 3 kamenitza 1 tuborg 2 guiness 3 Sample output guiness 1 kamenitza 2 tuborg 2 kamenitza 1 Task C. Chemical reactions There is one cup with a mixture of chemical compounds. Many chemical reactions can happen with this mixture changing the chemical compounds in the cup. Each possible mixture of chemical compounds has its energy level. All mixtures have different energy levels. All possible mixtures are numbered with numbers from 1 to N. For each mixture there are several possible reactions that convert it to another mixture. In all cases a reaction that converts the current mixture to a mixture with minimal energy takes place. There is only one mixture that has energy 0 and there are no possible reactions for it. Write a program that determines all mixtures that lead to the mixture with energy 0. Input The first line of the standard input contains T – the number of test cases. Each case starts with the number of mixtures – N (N < 10000). Next N lines describe one mixture. Each line starts with two integers – the energy, and Mi – the number of reactions available with this mixture. Next Mi numbers on the line are the numbers of the mixtures that are product of the possible reactions. Output For each test case output “Test case <number of case>:” followed by the numbers of the mixtures that lead to the mixture with energy 0. Sample input 1 6 5 0 9 7 8 6 1 0 2 2 1 1 2 1 6 6 3 4 5 Sample output Test case 1: 1 2 3 Task D. Dog Task Hunter Bob often walks with his dog Ralph. Bob walks with a constant speed and his route is a polygonal line (possibly self-intersecting) whose vertices are specified by N pairs of integers (Xi, Yi) – their Cartesian coordinates. Ralph walks on his own way but always meets his master at the specified N points. The dog starts his journey simultaneously with Bob at the point (X1, Y1) and finishes it also simultaneously with Bob at the point (XN, YN). Ralph can travel at a speed that is up to two times greater than his master's speed. While Bob travels in a straight line from one point to another the cheerful dog seeks trees, bushes, hummocks and all other kinds of interesting places of the local landscape which are specified by M pairs of integers ( X j , Y j ). However, after leaving his master at the point (Xi, Yi) (where 1 i N ) the dog visits at most one interesting place before meeting his master again at the point (Xi+1, Yi+1). Your task is to find the dog's route, which meets the above requirements and allows him to visit the maximal possible number of interesting places. The answer should be presented as a polygonal line that represents Ralph's route. The vertices of this route should be all points (Xi, Yi) and the maximal number of interesting places ( X j , Y j ). The latter should be visited (i.e. listed in the route description) at most once. An example of Bob's route (solid line), a set of interesting places (dots) and one of the best Ralph's routes (dotted line) are presented in the following picture: Y 2 4 1 3 X Input The first line of the standard input contains the number of test cases. Each test case starts with two integers N and M, separated by a space ( 2 N 100 , 0 M 100 ) on a single line. The second line of each test case contains N pairs of integers X1, Y1, ..., XN, YN, separated by spaces, that represent Bob's route. The third line of each test case contains M pairs of integers X 1 , Y1 , ..., X M , YM , separated by spaces, that represent interesting places. All points in the input file are different and their coordinates are integers not greater than 1000 by the absolute value. Output For each test case output two lines to the standard output. The first line for each test case should contain the single integer K – the number of vertices of the best dog's route. The second line should contain K pairs of coordinates X 1 , Y1 , ..., X K , YK , separated by spaces, that represent this route. If there are several such routes, then you may write any of them. Sample input 4 5 1 4 5 7 5 2 -2 4 -4 -2 3 9 1 2 -1 3 8 -3 Sample output 6 1 4 3 9 5 7 5 2 1 2 -2 4 Task E. Exponentials Stancho is building a tower. But it is not an ordinary tower, it is a power tower! Actually he has been challenged by Pancho to prove his mathematics skills. Stancho and Pancho are now playing the following game: Fancho (the master of fair-play) tells Stancho and Pancho an integer N (N ≥ 2). Stancho writes on a paper a sequence of integers a1, a2, …, ak such that they are all greater than 1 and their product is not greater than N. Pancho does the same. The one whose sequence is valid and the power tower a1 a2 a3 ak has greater value wins. In case of a tie, the one whose sequence is lexicographically smaller wins. In case of a tie again, the player whose name begins with 'S' wins. It is not hard to see that Stancho has a winning strategy. Stancho has even proved it in the article "Why Stancho always beats Pancho in the game Power Tower." published by Mineral Water Journal this year. However, Stancho's brain is not as fast as his 32 bit CPU, so he wants to cheat a little... Just a little... And you have to write a program which plays the game for him. Otherwise, you will have 7 years of unhappiness. Input On the standard input, several game descriptions will be given. Each one consists of a single number - the value of N (1 < N ≤ 2000000000). Output For each game description, write to the standard output a line with the winning sequence of numbers separated by space. Sample input 17 130 Sample output 2 2 2 2 2 2 2 2 2 2 2 * Since I’m not sure how readable it will be printed, the power tower is a1^a2^...^ak, where ^ means exponentiation. Task F. Fractions A simple continuous fraction has the form as shown on the right, where the ai’s are integer numbers. It can be denoted as [a1, a2, ..., an]. It is not difficult to show that any rational number p/q, with integers p > q > 0, can be represented in a unique way by a simple continuous fraction with n terms, such that p/q = [a1, a2, ..., an−1, 1], where n and the ai’s are positive natural numbers. Your task is to find and print the simple continuous fraction that corresponds to a given rational number. Input To the standard input a series of cases will be given, each one in a line. A line describing a case contains p and q, two integer numbers separated by a space, with 1019 > p > q > 0. The end of the input is indicated by a line containing “0 0”. Output Cases must be analyzed in the order that are read from the input and the result must be written to the standard output. Output for each case will consist of several lines. The first line indicates the case number, starting at 1, using the format: Case i: replacing i by the corresponding case number. The second line displays the input data in the form p / q. The remaining lines must contain the continuous fraction corresponding to the rational number, p/q , specified in the given input line. The continuous fraction must be printed accordingly to the following rules: Horizontal bars are formed by sequences of dashes ‘-’. The width of each horizontal bar is exactly equal to the width of the denominator under it. Blank characters should be printed using periods ‘.’. The number on a fraction numerator must be printed center justified. That is, the number of spaces at either side must be same, if possible; in other case, one more space must be added at the right side. Sample Input 75 34 65 60 0 0 Sample Output: Case 1: 75 / 34 ..........1...... 2.+.------------............1.... ....4.+.--------..............1.. ........1.+.----................1 ............5.+.................1 Case 2: 65 / 60 ......1... 1.+.-----.........1 ....11.+..........1 Task G. Gancho Stancho is becoming very good in programming. He can write in many programming languages and his programs are always optimal. Because he is very good programmer he always writes his sources on one line. Of course this is not problem for the compilers because usually ‘;’ is used as separator. Stancho is so proud of his idea for writing code on one line that he decided to name this source code – Stancho code. Everything would have been perfect if some parts of the source didn't start to miss. Stancho thinks that Gancho deletes some of his sources. Fortunately he keeps old copies of his sources. However Stancho is interested in understanding what parts of his code were removed. He knows that some substrings of his original code were removed and their number is minimal. Write a program that finds where these substrings are. The program must also print these removed substrings in sorted order. If there are multiple ways to remove substrings with same number of deletions, select this way that has first removed substring with minimum start point, if there are again multiple possibilities choose this one with minimum end point of the first removed substring. If there are again multiple possibilities choose this one with minimum start point of the second removed substring and so on... Input The first line of the standard input contains T – the number of test cases. Each case consists of two lines – the first line is the Stancho code with some parts removed, the second one is the original Stancho code. The first line is up to 1000 characters long. The second one is up to 2000 characters long. All lines may contain ASCII characters with codes between 32 (space) and 127 (including 32 and 127). Output For each test case output “Test case <number of case>:” followed by the number of substrings N. Next 2N numbers must describe each substring – the start and the end position in the string. For additional information see the sample output. Sample input 2 abalen nabaxelenpe int m() int main() Sample output Test case 1: 3 1 1 5 6 10 11 Test case 2: 1 6 8 Task H. Mystic Stancho discovered that for some sequences there is a number of balance and peace, of happiness and joy, of freedom and prosperity – the mystic number. It is a number from the sequence such that the numbers which are smaller than it are exactly as much as the numbers which are greater than it. Since he has many sequences, he needs a program which finds the mystic number of each sequence. Input Several sequences will be given on the standard input. Each of them is described in two lines - the first contains N (0 < N < 100) the number of elements in it. N is always odd number. On the second there are exactly N different numbers separated by spaces. The input ends with a line with number 0. Output For each of the input sequence, write to the standard output a line with its mystic number. Sample input 3 1 2 3 5 5 6 3 4 2 0 Sample output 2 4 Task I. Trinversions Many of you have heard about the research Stancho made about the complexity of the insert sort algorithm. In fact, he discovered that many algorithms depend on the number of inversions from the given sequence (inversion in the sequence A is a pair of indices i, j such that i < j and A[i] > A[j]). He also discovered that this area is so well studied, that it is hard to add something significant to it. That’s why he is trying new and completely different approach. First of all, he will not sort all kinds of sequences any more – he is specializing on sorting digits of a decimal numbers. Second, his algorithms will not be based on the traditional swap primitive (which swaps two values) but a new triswap primitive, which is built in all quantum computers. The triswap instruction works as follows – give 3 values to it which are currently sorted in descending order and it will reorder them, such that they will now be sorted in ascending order. The great optimization comes from the fact that it is actually exchanging only 2 values (the first and last) but is sorting 3 elements! Of course, there is a drawback – if the triplet given to it is not a trinversion, its behavior is undefined, and the whole quantum CPU may collapse, destroying many parallel universes. A trinversion for a sequence A is a triplet of indices i, j, and k such that i < j < k and A[i] > A[j] > A[k]. Stancho has already implemented many triswap based sorting algorithms. However it is very dangerous to test them. Because of this, he wants to see how they will perform on some numbers. He wants you to write a program which counts the trinversions in the sequence of digits in some decimal numbers. Input Several decimal integers will be given on the standard input. Each of them is greater than 0 and less than 101000000. Output For each of the given numbers, write to the standard output a single line with the number of trinversions of its digits. Sample input 12341651 64271241 Sample output 1 13 Task J. Magics Stancho just read something about graphs. As he understands, graphs are some circles with some arrows between them. The number of circles is always N (no other letter can be used) and the number of arcs is exactly M. Each circle has an unique identifier between 0 and N-1, and each arc starts from one circle and ends in another (or the same). Stancho has drawn one beatuful graph on the wall of his room. One of the toys of Stancho – Munio the Magic Toy is a magic toy. Every night after Stancho fall asleep, Munio wakes up, jumps on some circle of the graph, and jumps exactly K1 times to other circles. No more than K-1 times he jumps, and no less! He may jump from one circle to another only if there is an arrow from the first to the second. He remembers the identifiers of the circles he has jumped on, and their sequence he calls a magic path. Munio always succeeds to make a magic path and never makes a magic path he has already made. Input On the standard input, descriptions of the Stancho's graph in several parallel universes will be given. Each description begins with 3 integers – N, M and K (1 ≤ N ≤ 500, 1 ≤ K ≤ 20). Then M lines follow describing each arrow by two numbers between 0 and N-1 – the identifiers of the starting circle and the ending circle. Output For each of the of the parallel universes, write to the standard output a line with one number – how many nights Munio the Magic Toy can jump satisfying the above conditions. Sample input 3 0 1 5 0 0 0 0 1 1 1 2 2 3 2 2 1 2 10 3 1 2 3 4 2 3 4 3 4 4 Sample output 2 10