You are on page 1of 12

BSUIR Programming Championship 2013

24 April 2013, Final

Problem A: GCD Matrix.


Time limit: 2 sec.

Calculate determinant of the following matrix A(nn) by modulo 1000000007: d11 d1 [ ], d 1 d where gcd(a, b) is the greatest common divisor of the numbers a and b. In order to simplify your calculations you can use the following fact from number theory: (d) where Input: The input contains several test cases. Each test case is presented on a single line and contains integer number n for which you should calculate determinant A(nn). Constraints: The number n will be betwee 1 a d 1000000 i lusive. The umber of test ases does ot ex eed 1000. Output: For each test case output a single line: determinant of the matrix A. Sample Input 4 3 2 1 Sample Output 4 2 1 1 ( ) is Eulers totie t fu tio .
d

BSUIR Programming Championship 2013


24 April 2013, Final

Problem B: Fraction Partition.


Time limit: 2 sec.

Consider the fraction 1.

where 1 < m < n and m, n integer numbers. You should find ;

integer numbers a1, a2 ak such that

2. 0 < a1 < a2 < < ak; 3. ai is divided by ai-1 for all i such that 1 < i k. Input: The input contains several test cases. Each test case is presented on a single line and contains two integer numbers n and m separated by spaces. Constraints: The numbers n and m will be between 2 and 1000 inclusive. The number of test cases does not exceed 100. Output: For each test case output a single line: the first number in the line is the integer number k and then follow k integer numbers ai. If there are several solutions output any. Sample Input 32 83 Sample Output 226 2 3 24

BSUIR Programming Championship 2013


24 April 2013, Final

Problem C: Linear Combination.


Time limit: 2 sec.

Consider (n+1) triplets of integer numbers (ai, bi, ci). You should check if there is exists a set of nonnegative integer numbers x1, x2 xn, xn+1 such that

1. x1a1 + 2. x1b1 + 3. x1c1 + 4. x1 + x2


Input:

x2a2 + ... + xnan = xn+1an+1; x2b2 + ... + xnbn = xn+1bn+1; x2c2 + ... + xncn = xn+1cn+1; + + xn + xn+1 0.

The input contains several test cases. The first line of each test case contains integer number n. The next (n+1) lines of each test case contain integer numbers ai, bi and ci separated by spaces. Constraints: The integer number n will be between 1 and 100. The integer numbers ai, bi and ci will be between 0 and 16384. The number of test cases does not exceed 100. Output: For each test case output a single line: 1 if the numbers xi exist and 0 otherwise. Sample Input 2 111 222 333 2 121 341 331 Sample Output 1 0

BSUIR Programming Championship 2013


24 April 2013, Final

Problem D: Spreading of Water.


Time limit: 2 sec.

Consider a rectangular box which is placed on a horizontal plane. Assume that the lower left corner of the box is located at position (0, 0), the upper right corner is located at the position (n, m) and the sides of the box are parallel to the X and Y axis. Into the position (x, y) we start pour out a water of total volume v. You should calculate the distance to the farthest point which will be covered by the water after all water will be in the box. You can make two assumptions: 1. the water is spreading in the box evenly i. e. if a point is covered by the water then any other point at the same distance is covered by the water too; 2. you can neglect the thickness of the water layer. Input: The input contains several test cases. Each test case is presented on a single line and contains five integer numbers n, m, x, y and v separated by spaces. Constraints: The integer numbers n and m will be between 2 and 10 inclusive. The integer number x will be between 1 and n 1 inclusive. The integer number y will be between 1 and m 1 inclusive. The integer number v will be between 1 and 100 inclusive. The integer number v does not exceed n m. The number of test cases does not exceed 100. Output: For each test case output a single line: the distance to the farthest point covered by the water. The distance should be rounded to 3 decimal places. Sample Input 64316 23121 Sample Output 1.457 0.564

BSUIR Programming Championship 2013


24 April 2013, Final

Problem E: ATV Tank.


Time limit: 2 sec.

Consider a rectangular field of the width w and the height h. The ATV Tank is currently located at the left bottom corner of the field at position (0, 0) and is going to reach the opposite right top corner at position (w, h). The only issue is that the field is divided into n equals vertical zones which the tank can overcome with the different velocity vi. Knowing the size of the field and the velocity for each of the n zones you should determine the minimum time required to reach point (w, h) from original tanks location. Input: The input contains a single line with integer numbers w, h, n (separated by spaces) and then n integer numbers vi 1 i n) (separated by spaces too). Constraints: The integer numbers w and h will be between 1 and 1000 inclusive. The integer number n will be between 2 and 5 inclusive. The integer numbers vi 1 i n) will be between 1 and 1000 inclusive. Output: Output the minimum time required to overcome the field rounded to 4 decimal places. Sample Input 11211 Sample Output 1.4142

BSUIR Programming Championship 2013


24 April 2013, Final

Problem F: Alchemistry.
Time limit: 2 sec.

You are one of the best alchemist in the world but you have got a task which you cannot solve for a long time. You own n atomic elements with mass of the ith element equals to mi. Your goal is to make an element with total mass m. Consider the technological process in details. When you merge two elements they come into reaction. New element is created while the old ones disappear. This reaction is very unstable therefore mass of the resulting element equals mi + mj with probability 1/3, mi + mj + 1 with probability 1/3 and mi + mj - 1 with probability 1/3 where mi and mj are the masses of selected elements. You have decided to conduct experiments in the following way: first of all you choose some nonempty subset of the given elements and then merge them in some order. You are ignoring intermediate results and going to analyze only a single element produced after all reactions. At the current stage of research you need a program which can help you to maximize probability of getting element with mass m upon condition that you choose optimal subset of elements and carries out the reactions in the correct order. Input: The first line of the input contains the integer numbers n and m separated by spaces. The second line of the input contains n integer numbers mi 1 i n) (separated by spaces too) denoting the masses of the given atomic elements. Constraints: The integer number n will be between 1 and 33 inclusive. The integer number m will be betwee 1 a d 100000 i lusive. The integer numbers mi 1 i n will be betwee 1 a d 1000 i Output: Output the maximal probability of getting atomic element with mass m from the given elements as an irreducible fraction. Please see the samples below for the details. Sample Input 5 18 10 5 10 6 5 5 41 7 8 9 10 9 Sample Output 1/9 10/81

lusive.

BSUIR Programming Championship 2013


24 April 2013, Final

Problem G: Train Management.


Time limit: 2 sec.

Consider a train T which runs through n cities numbered successively from 1 to n (the starting city has the number 1 and n is the number of the ending city). There are m seats in the train and transporting more passengers then m between any two successive stations is not allowed. You should develop a computer system which allows manage seats in the train T. The computer system receives successive requests and determines whether they may be fulfilled. A request is accepted when in the given section of the railway line there are enough vacant seats in the train. Otherwise it is rejected. Partial accepting of a request is not allowed, e. g. for a part of a route or for fewer passengers. After accepting a request the number of vacant seats in the train is updated. The requests are processed successively in the order of coming. Input: The first line of the input contains three integer numbers n, m and k (separated by spaces) denoting the number of cities on the railway line, the number of seats in the train, and the number of requests, respectively. In the following k lines there are consecutive requests written. The description of each request consists of three integer numbers a, b and c (separated by spaces) denoting the number of origin station, the number of destination station and the requested number of seats, respectively. Constraints: The integer The integer The integer The integer The integer Output: Output for the ith request 1 i k) 1 if it is accepted and 0 otherwise. Sample Input 464 142 132 243 123 Sample Output 1 1 0 0 number n will be between 1 and 100000 inclusive. number m will be between 1 and 100000 inclusive. number k will be between 1 and 100000 inclusive. numbers a and b satisfy 1 a < b n. number c satisfies 1 c m.

BSUIR Programming Championship 2013


24 April 2013, Final

Problem H: Half-Poker.
Time limit: 2 sec.

Consider 26-card deck which consists of 26 cards divided into 2 suits: hearts and diamonds. For each suit there are 13 ranks: 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king and ace, listed from the lowest to the highest. A card is denoted by its rank (2 9 for 2 9, T for 10, J for jack, Q for queen, K for king, and A for ace) followed by its suit (H for hearts and D for diamonds). Cards are partially ordered by their ranks. The suit does not play a role in the cards ordering. A poker hand is a set of five distinct cards. Each hand is said to have a certain ranking. A hand with a higher ranking beats a hand with a lower one. Two hands of the same ranking are compared using a tie-breaking rule specific for their ranking either one of them beats the other or they are tied. The list of poker rankings is given below, from the worst ranking to the best ranking. If a hand satisfies several rankings, only the best one is considered. High Card does not fit into any ranking below. When comparing with another High Card hand, the ranks of the highest cards in the two hands are first compared. If there is a tie, the second highest cards in each hand are compared, and so on. Example: QD, JH, 9D, 7H, 3D. One Pair two cards of the same rank. Pair with higher rank beats the lower pair. In case of a tie, the remaining three cards are used as tie-breakers, compared in the descending order of their ranks (as in High Card). Example: 6D, 6H, QD, 9H, 4D. Two Pairs two pairs of cards of the same rank. When comparing with another Two Pairs hand, the higher pair is first compared then the lower pair, and finally the rank of the fifth remaining card. Example: JH, JD, TH, TD, 8D. Straight five cards in consecutive rank. Two straights are compared using the rank of the highest card. Example: QH, JD, TH, 9D, 8D. Flush five cards of the same suit. When comparing two Flushes, the rank of the highest card is first considered, then the second highest and so on. Example: AH, JH, 8H, 6H, 4H. Straight Flush a hand that is both a Straight and a Flush. Same tie-breaker as for a Straight. Example: TH, 9H, 8H, 7H, 6H. Now consider the following type of poker called Half-Poker. You are going to play HalfPoker with your friend. At the beginning of the game you shuffle the deck and then deal the five cards to yourself and five cards to your friend. Because you own the deck you can change one your card to the topmost card from the deck before showdown, in which the players reveal their cards and evaluate their hands. Given your own poker hand decides which card should be changed to the topmost card from the deck in order to maximize the chances of winning.
8

BSUIR Programming Championship 2013


24 April 2013, Final

Input: The input contains a single line with space-separated list of five distinct card descriptions. Each card is described with two characters denoting its rank and suit, respectively. Output: Output the integer number 0 if no change is required. In the other case output a number of the best card. If there are several choices output the card with the lowest number. The cards are numbered from 1 to 5 as they listed in the input. Sample Input Sample Output

5H JD JH 2D 2H

BSUIR Programming Championship 2013


24 April 2013, Final

Problem I: Interesting Trip 2.


Time limit: 2 sec.

Consider a land L with n cities numbered from 1 to n. Some of these cities are connected by bidirectional roads. Moreover each road r has a toll tr. During a trip by the land the toll may be levied or not depending on the urre t poli y of la ds admi istratio . But for a y traveler the tax can be levied no more than two times during a trip. If the traveler has not enough money to pay a tax he will be deported from the land immediately. You are going to the travel by the land L. So you have decided to determine the minimum amount of money required to travel from city A to city B knowing the toll for each road in the land L. Also you know that you can choose any route connecting cities A and B and you know that there is exists at least one path connecting these cities. Input: The first line of the input contains integer numbers n and m denoting the number of cities and roads respectively. The next m lines contain three integer numbers a, b and t (separated by spaces) denoting that the toll for the road connecting cities a and b is t. The last line of the input contains integer numbers A and B separated by spaces. Constraints: The number n will be between 2 and 10000 i lusive. The number m will be betwee 1 a d 100000 i lusive. The numbers ti (1 i m) will be between 1 and 109 inclusive. Output: The output should contain a single integer denoting the minimum amount of money which you should take to the trip. Remember that you can choose any route from A to B depending on the money which you take with yourself. Sample Input 56 1 2 10 134 323 141 452 523 12 Sample Output 5

10

BSUIR Programming Championship 2013


24 April 2013, Final

Problem J: String Generator.


Time limit: 2 sec.

You should find a string S consisting of the capital Latin letters such that: 1. the length of the string S is less than the given integer number n; 2. the number of distinct substrings of the string S is equal to the number n. Input: The first line of the input contains a single integer number n. Constraints: The number n will be between 2 a d 100000 i Output: Output a string S which satisfies specified conditions. Output -1 if such string does not exist. If there are several solutions output any. Sample Input 9 15 Sample Output ABAC BSUIR lusive.

11

BSUIR Programming Championship 2013


24 April 2013, Final

Problem K: String Reconstruction.


Time limit: 2 sec.

Consider an arbitrary string S of the length n. Denote by s(S, i) the ith suffix 1 i n) of the string S i. e. the substring S[in]. Denote by p(S, i) the ith prefix 1 i n) of the string S i. e. the substring S[1 i]. Denote by lcp(S, i, j) the longest common prefix of the ith and jth suffixes of the string S i. e. lcp(i, j) is the maximal integer number k such that p(s(S, i), k) = p(s(S, j), k). Your task is by the given integer matrix A(nn) find a string S consisting of the capital Latin letters such that: 1. the length of the string S is equal to the integer number n; 2. the lcp(S, i, j) is equal to A[i, j] for all 1 i n a d 1 j n. Input: The first line of the input contains a single integer number n. Then the A matrix is given in the following format: the jth integer (1 j n) in the (i+1)th line 1 i n) contains lcp value for ith and jth suffixes of the string S. Constraints: The number n will be between 1 and 1000 inclusive. Output: Output a string S which satisfies lcp matrix given in the input. If there are several solutions output the lexicographically smallest one. It is guaranteed that such string exists. Sample Input 4 4100 1300 0020 0001 Sample Output AABC

12