Problem A. Binary Witch

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:3 sec
Output file: output.txt   Memory limit:32 Mb

Statement

Once upon a time in the silent depths of digital forests there lived a Binary Witch. She was able to forecast weather, telling for any day in the future whether it will be rainy or sunny.

Her magic was based on the following ancient rule: let a1, a2, ..., aN be the sequence of binary digits, where ai=0 indicates that i-th day was rainy, and ai=1 - that it was sunny. To predict the weather in day N + 1, consider the t-postfix aN-t+1, aN-t+2, ..., aN consisting of the last t elements. If that postfix is encountered some- where before the position N - t + 1, i.e. if there is such k ≤ N - t, that ak = aN-t+1, ak+1 = aN-t+2, ..., ak+t-1 = aN then the predicted value will be ak+t. If there is more than one occurrence of t-postfix, then the rightmost one (with maximal k) will be taken. So, to make a prediction, she tried t-postfixes, consequently for t = 13, 12, ..., 1, stopping after the first prediction. If neither postfix was found, she predicted rain ("0"). If prediction for more than one day is needed, it is assumed that all previous days are predicted correctly, so if first predicted value is b, then we make forecast for day N + 2 based on N + 1 values, where aN+1 = b.

Because the witch was burned long ago, your task is to write a program to per- form her arcane job.

Input file format

First line of input file contains two integers N and L, separated by space. Second line contains a string of N characters "0" and " 1".

Output file format

Output file must contain a single string of L characters, which are forecasts for days N+1, N+2, ..., N+L.

Constraints

1 ≤ N ≤ 100000 1 ≤ L ≤ 1000

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
10 7
1101110010
0100100

Problem B. Fool Game

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:3 sec
Output file: output.txt   Memory limit:8 Mb

Statement

The game of fool is played with a small set of cards, which includes nine ranks - 6, 7, 8, 9, 0 (10), J (Jack), Q (Queen), K (King), A (Ace) of the four suits each - h (Hearts), s (Spades), d (Diamonds) and c (Crosses). So, for example a queen of spades is denoted Qs, and a ten of diamonds is 0d. One of the suits is declared a trump. During the game, the card beats another card, if either it has the same suit and higher rank, or it is a trump, while the beaten card is not.

In the game, a move proceeds as following. First player puts one of his cards on the desk, and the second player can either beat it with one of his cards, putting his card over it, or take it, if he has no suitable card. If the card is beaten, first player may flip in any of his remaining cards, which has same rank as any card already on the desk. This card, in turn, may be beaten or taken together with all other cards on the desk, etc. For example, if first player has cards 6s6dQhKd, the second player has 6h7h0sQd and hearts are the trumps, then first player can move with Kd, which is beaten with 6h, then flip in 6s, beaten by 0s, then 6d, beaten by Qd and at last Qh, which can not be beaten with 7h, so the second player has to take it.

Your task is to write a program that, given the trump suit and first and second playerTs cards, determines for the first player such a move as to eventually make the second player take.

Your task is to write a program that, given the trump suit and first and second playerTs cards, determines for the first player such a move as to eventually make the second player take.

If there is more than one such move, the program must find one with smallest rank. If there is several moves with smallest rank, program must choose the card with the first suit in the order mentioned in the first paragraph (i.e. h < s < d < c). In the example above, second player could beat Kd with 7h, thus preventing further flips. On the other hand, move of Qh will be immediately taken.

Input file format

In the first line of input file there is a single character h, s, d, or c, determining a trump suit. On the second line there is a string denoting first playerTs cards, and on the third line S the string with the second playerTs cards. All cards are different, and the players have equal number of cards.

Output file format

Output file must contain a single line with either a card to move or a string "NO", if a program was unable to find it.

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
s
As7h8dJc
Jd8s7c0c
7h

Problem C. Spreadsheet

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:3 sec
Output file: output.txt   Memory limit:8 Mb

Statement

You are to write a program emulating a very simple spreadsheet application. It works with a table with 9 rows, from "1" to "9", and 26 columns, from "A" to "Z". Table cells are referenced by names composed of column and row codes, ex. "B1", "S8".

Each cell contains an expression up to 255 characters long. Expressions use integer constants, cell references, parenthesis, operators "+", "-", "*", and "/" (whole division). For example: 567, E8/2, (3+B3)*(C4O1) are all valid expressions. All opera- tors are whole, all arguments and results are guaranteed to be less than 1000000. Divi- sion by zero yields zero.

If the value of the cell referenced by some expression is not defined, it is pre- sumed to be 0 (zero). The situation when two or more cells are mutually dependent on each other is considered a special case of circular reference.

Input file format

First input line contains number of expressions N. Following N lines are in format <Cell reference>=<expression>. All expressions are correct, and each cell is defined by at most one expression.

Output file format

Output file must contain single line with either the value of the cell "A1" or number 1000000 (one million) if the value of A1 cannot be computed due to a circular reference.

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
4
A1=B1+C5
B1=20
C5 =B1 /D7-E1*E1
E1=(3+1)*2
-44

Problem D. PreQueL

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:3 sec
Output file: output.txt   Memory limit:8 Mb

Statement

In some simplistic DBMS named PreQueL, the only column type allowed is CHAR(1) (a single character), and furthermore, its values are restricted to English upper- case letters (eAi to eZi). Table may contain up to 9 columns, numbered from 1 to 9. Tables themselves are named with lower-case English letters (eai to ezi).

The only database query possible first joins all the tables, then selects some rows according to conditions in one of two forms: either <column>=<value> or <column1>=<column2>, for example a2=A or b1=c4. All conditions must hold simultaneously, as if they were connected by eANDi operator.

You must write a PreQueL processor, which, given a tables and a set of conditions, will produce query result, i.e. those rows of a join satisfying all the conditions. Resulting rows must be sorted alphabetically.

Input file format

The first line of input file contains of two integers o number of tables T and number of conditions D.

Starting from the second line there are T tables represented with number of rows RN and number of columns CN in the first line of a table, which is followed by RN lines consisting of exactly CN characters each. D lines with conditions follow the whole set of tables.

Output file format

Output file contains result rows, one row per line. No input query will produce more than 1000 rows.

Constraints

1 ≤ T ≤ 26, 1 ≤ D ≤ 50, 1 ≤ CN ≤ 9, 1 ≤ RN ≤ 1000.

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
2 2
3 2
AX
BX
BY
2 3
ACD
BCC
a1=b1
a2=X
AXACD
BXBCC

Problem E. Hamming Problem

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:3 sec
Output file: output.txt   Memory limit:8 Mb

Statement

For each three prime numbers p1, p2 and p3, letis define Hamming sequence Hi(p1, p2, p3), i = 1,... as containing in increasing order all the natural numbers whose only prime divisors are p1, p2 or p3. For example, H(2, 3, 5) = 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, 16, 18, 20, 24, 25, 27,... So H5(2, 3, 5)=6.

Input file format

In the single line of input file there are space-separated integers p1 p2 p3 i.

Output file format

The output file must contain the single integer - Hi(p1, p2, p3).

Constraints

All numbers in input and output are less than 1018.

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
7 13 19 100
26590291

Problem F. Circular Area

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:3 sec
Output file: output.txt   Memory limit:8 Mb

Statement

Your task is to write a program, which, given two circles, calculates the area of their intersection with the accuracy of two digits after decimal point.

Input file format

In the single line of input file there are space-separated real numbers x1 y1 r1 x2 y2 r2. They represent center coordinates and radii of two circles.

Output file format

The output file must contain single real number — the area.

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
20.0 30.0 15.0 40.0 30.0 30.0
608.37

Problem G. One-move checkmate

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:8 Mb

Statement

The chess endgame "a king and a queen versus a king" is known to be easy victory for the side that has a queen. The position in such an endgame is described by the locations of three figures: the white king, the white queen, and the black king. Locations are written in a usual chess notation, constructed from a letter from "a" to "h", which determine vertical line, and the digit from 1 to 8, which determine horizontal line of the location. Given the position described, you are find a move for the white queen by which the black king will be checkmated, or determine that no such a move exists.

Input file format

The input file contains three two-character locations for the white king, white queen, and black king, in that order, separated by spaces. The input position is a valid chess position for the white's turn, i.e. all figures occupy different spaces, kings are not located in the neighboring squares, and the black king is not under the check.

Output file format

Output file must contain the two-character location - destination square for the white queen's move. If checkmating move does not exist, output file must contain the string "no". If there is more than one checkmating move, you may output any one of them.

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
a3 g2 a1
a2

Problem H. Countryside Highway

Author:unknown
Input file: input.txt   Time limit:15 sec
Output file: output.txt   Memory limit:200 Mb

Statement

Regional government wishes to build a new highway across the region, and it seems that the best route for the highway goes across the Kvadratnaya village.

The village consists of the N x N square estates.

All estates have a size of 100 by 100 meters.

Therefore, in the coordinate system tied to the village, the southwestern corner of the village has coordinates (0, 0), and northeastern corner - coordinates (N*100, N*100).

The highway will cross the village form the west to the east side, cutting through several estates. The government decided to compensate owners of those estates, and to estimate the necessary expenses one should calculate the number of estates crossed by the highway. Given the village size N and the coordinates of the points where the highway crosses eastern and western borders of the village, you must find the number of estates.

You can assume, for the purpose of increasing the financial appeal of the highway project, that the highway is a straight line (i.e. has zero width).

If the highway just touches the edge of an estate, it is still considered a crossing. The highway is a line between points (0, W) and (100*N, E).

Input file format

In the input file, there are three integers, N, W and E, separated by spaces.

Output file format

Output file must contain a single integer - the number of estates crossed by the highway.

Constraints

1 <= N <= 100

0 <= W, E <= 100*N

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
3 150 50
4

Problem I. Market place

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:8 Mb

Statement

A market has a line of N trading posts selling sunflower seeds. Prospective buyers walk along the line, then at some moment stop and buy the seeds to nibble. Quality of the seeds does not differ substantially among the posts, so the only difference is in price and location of posts. Before trying to enter this market as another seed seller, you have conducted a market research to find out how is the number of buyers influenced by these two factors. The research shows that most buyers follow the same pattern. They walk past some posts noticing and remembering the prices, then after passing K posts return to the one with the lowest price encountered, and make a purchase there, then leave the market. If there are several posts with the equal price, they choose the nearest one in the line. For example, let us assume there are five posts with prices 37, 34, 34, 35, 33. If the buyer with K = 4 walks from the left to right, he sees seeds priced at 37, 34, 34, 35. At this moment, he decides he has seen enough, and goes back to the third post and buys seeds there. Although the second post has the same price as the third, it is a longer walk for the buyer to return there. If the same buyer would enter the line from the right end, he would see prices 33, 35, 34, 34, then stop and return to the fifth post. The number of posts passed before the decision (K) is a function of buyer's greed and patience, and is obviously different for different buyers. The research yielded the average percentage BK of buyers for all values of K.

You are to determine the optimal market strategy (i. e. the price and location of the new post which maximizes anticipated average income) under the assumptions that half of the clients walk in the direction from the first post to the Nth and another half - from the Nth post, to the first, and that they behave according to the pattern above.

Input file format

The first line of the input file contains the number of existing posts N. The second line contains N integers pi - the prices for the posts. The third line contains N integers - the values of BK for each K. All numbers in a line are separated by spaces.

Output file format

The output file must contain two integers - L and P. L (1 ≤ L < N) is the number of the existing post after which the new one should be placed (You are not allowed to place your post first or last in the line). The second number P is the optimal price. If there is more than one optimal solution, you should choose the one with the minimal L, and between them - the one with minimal P.

Constraints

2 ≤ N ≤ 100, 1 ≤ pi ≤ 9999, 0 ≤ Bk ≤ 99, B1 + ... + BN = 100

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
5
37 34 34 35 33
10 20 30 30 10
3 33

Problem J. Integer approximation

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:8 Mb

Statement

The FORTH programming language does not support floating-point arithmetic at all. Its author, Chuck Moore, maintains that floating-point calculations are too slow and most of the time can be emulated by integers with proper scaling. For example, to calculate the area of the circle with the radius R he suggests to use formula like R * R * 355 / 113, which is in fact surprisingly accurate. The value of 355 / 113 = 3.14159... is approximating the value of π with the absolute error of only about 10-7. You are to find the best integer approximation of a given floating-point number A within a given integer limit L. That is, to find such two integers N and D (1 ≤ N, D ≤ L) that the value of absolute error |A - N / D| is minimal.

Input file format

The first line of input file contains a floating-point number A with the precision of up to 15 decimal digits. The second line contains the integer limit L.

Output file format

Output file must contain two integers, N and D, separated by space.

Constraints

0.1 ≤ A < 10, 1 ≤ L ≤ 100000

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
3.14159265358979
10000
355 113

Problem K. Multiplication puzzle

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:8 Mb

Statement

The multiplication puzzle is played with a row of cards, each containing a single positive integer. During the move player takes one card out of the row and scores the number of points equal to the product of the number on the card taken and the numbers on the cards on the left and on the right of it. It is not allowed to take out the first and the last card in the row. After the final move, only two cards are left in the row.

The goal is to take cards in such order as to minimize the total number of scored points.

For example, if cards in the row contain numbers 10 1 50 20 5, player might take a card with 1, then 20 and 50, scoring 10*1*50 + 50*20*5 + 10*50*5 = 500+5000+2500 = 8000

If he would take the cards in the opposite order, i.e. 50, then 20, then 1, the score would be 1*50*20 + 1*20*5 + 10*1*5 = 1000+100+50 = 1150.

Input file format

The first line of the input file contains the number of cards N. The second line contains N integers ai, separated by spaces.

Output file format

Output file must contain a single integer - the minimal score.

Constraints

3 ≤ N ≤ 100, 1 ≤ ai ≤ 100

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
6
10 1 50 50 20 5
3650

Problem L. Holey cloth

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:8 Mb

Statement

Several pieces of cloth are laid out on the table without overlapping each other. These pieces contain many holes, some so big that entire other piece of cloth may fit into them. Digital black-and-white image of the tabletop was taken, on which areas covered by cloth are represented with '*'s and not covered areas with '.'s. A single piece of cloth is thus represented with 4-connected area of '*'s - i.e., a group of '.'s located next to each other horizontally or vertically, but not diagonally. For example, on the following image there are three pieces - one without holes, and two with one hole each - first has area of 8, and the second - area of 12.

.***..***
.*.*.**.*
.***.*.**
*...**.*.

Your goal is to find the piece with the most holes in it. The hole is a 4- connected area of '.'s completely surrounded with '*'s. If several pieces have the same number of holes, you must select the one with the smallest area.

Input file format

In the first line of input file there are two numbers W and H, separated by spaces. Next H lines contain W characters each. Characters in these lines are either '*' (ASCII 42) or '.' (ASCII 46).

Output file format

The output file must contain a single integer - the area of the smallest of most holey pieces. If there are no pieces with the holes, the output file must contain zero.

Constraints

1 ≤ W, H ≤ 100

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
9 5
.********
.*......*
.*..**..*
.*......*
.********
22

Problem M. Compacting stickers

Author:Far-Eastern Subregional
Input file: input.txt   Time limit:1 sec
Output file: output.txt   Memory limit:8 Mb

Statement

The text on a sticker consists of words formed from small and capital Latin letters. Words are separated by spaces, line breaks, and/or the following punctuation marks: ",", ".", "!", "?".

There may be any number of spaces and empty lines before and after the words, but there is no more than one punctuation mark after each word. Sticker is printed with monospace font, so every character occupies on the paper a rectangular area of fixed size. Sticker itself is a minimal rectangle enclosing the text plus a margin of one character in width.

Many copies of the sticker are to be printed, and to minimize paper consumption the sticker should be made as small in area as possible. Consider, for example, the sticker with the following text:

Our pink elephants have great size and a small price. Buy our elephants!

Printed in one line, this sticker will have an area of (72 + 2) * (1 + 2) = 222 characters. On the other hand, if printed in four lines, like this:

····················

·Our·pink·elephants·

·have·great·size····

·and·a·small·price.·

·Buy·our·elephants!·

····················

the sticker will only require (18 + 2) * (4 + 2) = 120 characters.

You objective is, given the text of a sticker, to break it into lines so as to achieve the smallest possible area for it. Line break may be inserted after any word or punctuation mark, but not before a punctuation mark. One space must separate each word from the preceding word or punctuation on the same line. You do not have to preserve other spaces or line breaks in the input file.

Input file format

The input file contains one or more lines of the sticker text. Text contains only the following characters: "A" to "Z", "a" to "z", ",", ".", "!", "?", spaces and line ends. The text always contains some non-space characters, and the first of them is always a letter.

Output file format

Output file must contain a single integer number - the area of the smallest sticker.

Constraints

The text length is less or equal than 10000 characters.

Sample tests

No. Input file (input.txt) Output file (output.txt)
1
Our pink
Elephants
have great size and a
small price .
Buy our elephants !
  
120

0.089s 0.006s 31