Problem A. Bike Roads

ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem A. Bike Roads
Input file:
Output file:
Time limit:
Memory limit:
bike.in
bike.out
2 seconds
64 megabytes
Andrew lives at countryside. The area he lives at has two bike roads, each of which has the form of a
circle with radius r. Roads have no common points.
Andrew’s house is located at one of the roads, and his school is located at the other one. Each day
Andrew rides his bike from home to school and back. He has noticed that riding along the road is easier
than riding by the ground. When riding along the road Andrew’s speed is u, and when riding by the
ground his speed is v. Now Andrew wonders what minimal time he needs to get from his house to school.
Let us introduce the coordinate system so that the center of the bike road where Andrew’s house is
located were (0, 0), and the center of the bike road where his school is located were (0, d). The radius of
each road is r. Andrew’s house is located at a point (x1 , y1 ), and his school is located at a point (x2 , y2 ).
His speed by the road is u, and his speed by the ground is v.
Input
Input file contains
point numbers: d, r, x1 , y1 , x2 , y2 , u and v (1 ≤ r ≤ 100, 2r < d ≤ 100,
√eight floating√
2
2
1 ≤ v < u ≤ 10, x1 + y1 = r, x22 + (y2 − d)2 = r). All equalities are up to 10−9 .
Output
Output one floating point number — the minimal time Andrew needs to get from his house to school.
Your answer must be accurate up to 10−6 .
Example
bike.in
20 5
5 0 5 20
2 1
20 5
-5 0 5 20
2 1
bike.out
16.5757337181
17.2040517249
Page 1 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem B. Cookies
Input file:
Output file:
Time limit:
Memory limit:
cookies.in
cookies.out
2 seconds
64 megabytes
Santa Claus is planning to bring gifts to n children. He has m cookies and is planning to divide them to
n piles. However, as usually problems come unexpected. The child gets unhappy if somebody gets more
cookies than him.
Each child is characterized by his greediness, the greediness of the i-th child is gi . The unhappines of the
i-th child is equal to gi ai where ai is the number of children that get more cookies than him.
Now Santa wants to divide cookies in such a way that the total unhappiness is minimized. Each child
must get at least one cookie. Santa would like to give away all m cookies he has. Help him to do so.
Input
The first line of the input file contains n and m (1 ≤ n ≤ 30, n ≤ m ≤ 5000). The second line contains
n integer numbers g1 , g2 , . . . , gn (1 ≤ gi ≤ 107 ).
Output
Print the minimal possible unhappiness at the first line of the output file. The second line must contain n
integer numbers — the number of cookies the corresponding child must get. If there are several solutions,
output any one.
Example
cookies.in
3
1
4
2
20
2 3
9
1 5 8
cookies.out
2
2 9 9
7
2 1 3 3
Page 2 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem C. Diversion
Input file:
Output file:
Time limit:
Memory limit:
diversion.in
diversion.out
2 seconds
64 megabytes
The kingdom of Farland has n cities connected by m bidirectional roads. Some of the roads are paved
with stone, and others are just country roads. The capital of the kingdom is the city number 1. The
roads are designed in such a way that it is possible to get from any city to any other using only roads
paved with stone, and the number of stone roads is minimal possible. The country roads were designed
in such a way that if any stone road is blocked or destroyed it is still possible to get from any city to any
other by roads.
Let us denote the number of stone roads needed to get from city u to city v as s(u, v). The roads were
created long ago and follow the strange rule: if two cities u and v are connected by a road (no matter,
stone or country), then either s(1, u) + s(u, v) = s(1, v) or s(1, v) + s(v, u) = s(1, u).
The king of Edgeland is planning to attack Farland. He is planning to start his operation by destroying
some roads. Calculations show that the resources he has are enough to destroy one stone road and one
country road. The king would like to destroy such roads that after it there were at least two cities in
Farland not connected by roads any more.
Now he asks his minister of defense to count the number of ways he can organize the diversion. But the
minister can only attack or defend, he cannot count. Help him!
Input
The first line of the input file contains n and m — the number of cities and roads respectively
(3 ≤ n ≤ 20 000, m ≤ 100 000). The following m lines describe roads, each line contains three integer numbers — the numbers of cities connected by the corresponding road, and 1 for a stone road or 0
for a country road. No two cities are connected by more than one road, no road connects a city to itself.
Output
Output one integer number — the number of ways to organize the diversion.
Example
diversion.in
6
1
2
1
3
4
3
5
7
2
3
4
4
5
6
6
diversion.out
4
1
1
0
1
1
0
1
Page 3 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem D. ePig
Input file:
Output file:
Time limit:
Memory limit:
epig.in
epig.out
2 seconds
64 megabytes
Andrew and Ann are developing the new P2P software network ePig. The network is intended to be used
for sharing files. In this problem you will have to simulate the operation of the network when distributing
one large file.
Let there be n clients numbered from 1 to n. Initially the whole file is provided by client 1. All other clients
wish to get this file. The file is split into k chunks numbered from 1 to k. The transfer consists of a series
of rounds. Each round takes one minute and the bandwidth of the connection of each client is enough to
transform one chunk to the client and transform one chunk away from the client (simultaneously). After
a client gets some chunk it starts to provide it to other clients.
Before a round each client decides which chunk it will request. The client will request the chunk that is
provided by the smallest number of clients (except those chunks that it already has). If there are several
such chunks, it selects the one which has the smaller number.
After that the clients make chunk requests. Each client selects the client which has the chunk that it
decided to request, if there are several such clients, the client which provides the smallest number of
chunks is selected. If there are still several possible variants, the client which has the smallest number is
selected.
Each client considers all requests and satisfies one of them. The request satisfied by client X is the one
which comes from the most valued client. The value of the client is the number of chunks it allowed X
to be downloaded from him in the past. If there are several equally valued clients, X gives the chunk to
the one which has the smallest number of chunks available. If there are still several possible variants, the
chunk is provided to the client which has the smallest number.
After the requests that will be satisfied are selected the round begins. The clients whose requests were
rejected do not download anything this round, all other clients download the chunk they requested. After
that the new round starts.
Given n and k, you have to find for each client, what is the number of rounds before it gets the whole
file.
Input
The first line of the input file contains n and k (2 ≤ n ≤ 100, 1 ≤ k ≤ 200).
Output
Output n − 1 numbers — for each client except the first one print the number of rounds before it gets
the whole file.
Example
epig.in
3 2
epig.out
3 3
The file distribution will proceed as follows. At the first round clients 2 and 3 will request chunk 1 from
client 1. Request from client 2 will be satisfied. After that clients 2 and 3 will request chunk 2 from client
1. Client 3 will be satisfied. On the third round client 2 will request chunk 2 from client 3 and client 3
will request chunk 1 from client 2, both requests will be satisfied, and both will have the whole file.
Page 4 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem E. Geometry Problem
Input file:
Output file:
Time limit:
Memory limit:
geometry.in
geometry.out
2 seconds
64 megabytes
Peter is studying in the third grade of elementary school. His teacher of geometry often gives him difficult
home tasks.
At the last lesson the students were studying circles. They learned how to draw circles with compasses.
Peter has completed most of his homework and now he needs to solve the following problem. He is given
two segments. He needs to draw a circle which intersects interior of each segment exactly once.
The circle must intersect the interior of each segment, just touching or passing through the end of the
segment is not satisfactory.
Help Peter to complete his homework.
Input
The input file contains several test cases. Each test case consists of two lines.
The first line of the test case contains four integer numbers x11 , y11 , x12 , y12 — the coordinates of the
ends of the first segment. The second line contains x21 , y21 , x22 , y22 and describes the second segment in
the same way.
Input is followed by two lines each of which contains four zeroes these lines must not be processed.
All coordinates do not exceed 102 by absolute value.
Output
For each test case output three real numbers — the coordinates of the center and the radius of the
circle. All numbers in the output file must not exceed 1010 by their absolute values. The jury makes all
comparisons of real numbers with the precision of 10−4 .
Example
geometry.in
0
1
0
0
0
0
0
0
0
1
0
0
4
4
0
0
geometry.out
0.5 0 2
Page 5 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem F. Hex
Input file:
Output file:
Time limit:
Memory limit:
hex.in
hex.out
2 seconds
64 megabytes
The game of hex is played on a rhombic field of size n × n divided to hexagonal cells. An example of the
field for n = 6 is shown on the picture below.
Two players, one playing for noughts and one for crosses, make moves in turn. A move consists of putting
a player’s piece to one of the free cells. Two cells are considered adjacent if they have a common side.
The goal of noughts is to make a chain of adjacent cells from the left edge of the field to the right edge
of the field, the goal of crosses is to make such chain from the top edge to the bottom edge. A picture
below shows the game won by noughts.
The gameplay of hex strongly depends on so called templates. In this problem we will consider two simple
templates. The two-bridge template is the situation when two player’s pieces can be connected in two
ways in one move. Such pieces can be considered connected. A picture below shows the two-bridge for
crosses. Note that the cells marked by dots must not be occupied. If noughts play to one of these cells,
crosses reply to another cell connecting their pieces. The two cells marked by dots are called bridge cells.
Page 6 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Another simple template is a similar construction at the edge of the board, called template II. A piece in
the second row at the player’s edge, with no pieces in the two cells at the edge adjacent to it, can always
be connected to the edge. A picture below shows template II for noughts.
The game is said to be won up to simple templates by one of the players if there is a sequence p1 , p2 , . . . , pk
of his pieces such that p1 and pk are at his different edges or connected to them by template II, for all
i pieces pi and pi+1 are either adjacent or connected to each other by a two-bridge, and no two-bridges
share a bridge cell. A picture on the left below shows a game won by noughts up to simple templates. A
game on the right is not won by noughts up to simple templates yet, because a cell marked by a dot is
shared by two two-bridges.
Given the position of the field, find out whether the game is won up to simple templates by one of the
players.
Input
The first line of the input file contains two integer numbers: n and m — the size of the field and the
number of pieces on the field (3 ≤ n ≤ 10 000, 1 ≤ m ≤ 30 000). The following m lines describe pieces.
Each piece is described by three integer numbers: x, y and s — the coordinates of the piece and the
player who owns the piece. Coordinates range from 1 to n, s is 0 for noughts and 1 for crosses. The
position need not be a valid game position, so the number of pieces of different players can be different.
No two pieces occupy the same cell. The coordinate system is shown on the picture below, each cell is
marked as x, y.
1,6 2,6 3,6 4,6 5,6 6,6
1,5 2,5 3,5 4,5 5,5 6,5
1,4 2,4 3,4 4,4 5,4 6,4
1,3 2,3 3,3 4,3 5,3 6,3
1,2 2,2 3,2 4,2 5,2 6,2
1,1 2,1 3,1 4,1 5,1 6,1
Page 7 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Output
Output “noughts” if the game is won by noughts up to simple templates, “crosses” if the game is won
by crosses up to simple templates and “none” if the game is not won up to simple templates yet.
Example
hex.in
6
2
4
5
3
4
5
6
2
3
4
6
3
4
4
5
6
4
3
4
5
2
1
8
4
2
3
2
4
5
6
1
hex.out
noughts
0
0
0
1
1
1
none
0
0
0
0
1
1
1
1
Page 8 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem G. Important Roads
Input file:
Output file:
Time limit:
Memory limit:
important.in
important.out
2 seconds
64 megabytes
The city where Georgie lives has n junctions some of which are connected by bidirectional roads.
Every day Georgie drives from his home to work and back. But the roads in the city where Georgie lives
are very bad, so they are very often closed for repair. Georgie noticed that when some roads are closed
he still can get from home to work in the same time as if all roads were available.
But there are such roads that if they are closed for repair the time Georgie needs to get from home to
work increases, and sometimes Georgie even cannot get to work by a car any more. Georgie calls such
roads important.
Help Georgie to find all important roads in the city.
Input
The first line of the input file contains n and m — the number of junctions and roads in the city where
Georgie lives, respectively (2 ≤ n ≤ 20 000, 1 ≤ m ≤ 100 000). Georgie lives at the junction 1 and works
at the junction n.
The following m lines contain information about roads. Each road is specified by the junctions it connects
and the time Georgie needs to drive along it. The time to drive along the road is positive and doesn’t
exceed 100 000. There can be several roads between a pair of junctions, but no road connects a junction
to itself.
It is guaranteed that if all roads are available, Georgie can get from home to work.
Output
Output l — the number of important roads — at the first line of the output file. The second line must
contain l numbers, the numbers of important roads. Roads are numbered from 1 to m as they are given
in the input file.
Example
important.in
6
1
2
2
1
3
2
5
7
2
3
5
3
5
4
6
1
1
3
2
1
1
2
important.out
2
5 7
Page 9 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem H. Irreducible Young Diagrams
Input file:
Output file:
Time limit:
Memory limit:
irreducible.in
irreducible.out
2 seconds
64 megabytes
Young diagram is a well known way to describe a partition of a positive integer number. A partition of a
number n is a representation as a sum of one or several integer numbers n = m1 + m2 + . . . + mk where
m1 ≥ m2 ≥ . . . ≥ mk .
A diagram consists of n boxes arranged in k rows, where k is the number of terms in the partition. A
row representing the number mi contains mi boxes. All rows are left-aligned, and sorted from longest to
shortest.
The diagram on the picture below corresponds to the partition 9 = 5 + 2 + 2.
Let us describe a way to transform the Young diagram. You are allowed to choose two adjacent boxes in
the diagram and remove them. The only restriction is that after the process the remaining diagram must
be a valid Young diagram. Also the resulting diagram must either be empty, or have a left-top corner at
the same position as the diagram that was transformed.
For example, removing the last boxes of the second and the third row from the diagram above, we get
the diagram for the partition 7 = 5 + 1 + 1.
Removing the last two boxes of the first row from the same diagram we get the diagram for the partition
7 = 3 + 2 + 2.
There is one more way to transform the Young diagram in this example: remove the two boxes from the
last row.
The diagram that cannot be transformed in the above way is called irreducible. Clearly, an empty diagram
is irreducible.
Each diagram can be transformed until it is irreducible. Generally there may be several possible ways
of transforming the diagram. You are given a Young diagram. You have to find all possible irreducible
diagrams that the given one can be transformed to.
Input
The first line of the input file contains k — the number of rows in the diagram (1 ≤ k ≤ 100 000). The
second line contains k numbers: m1 , m2 , . . . , mk . The sum n = m1 + m2 + . . . + mk doesn’t exceed 108 .
Page 10 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Output
The first line of the output file must contain one number l — the number of irreducible diagrams the
given diagram can be transformed to. The following l lines must describe these diagrams: each line must
contain t — the number of rows in the corresponding diagram, followed by t numbers — the number of
boxes in corresponding rows.
Example
irreducible.in
3
5 2 2
1
2
irreducible.out
1
1 1
1
0
Page 11 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem I. Nice Sequence
Input file:
Output file:
Time limit:
Memory limit:
nice.in
nice.out
2 seconds
64 megabytes
Let us consider the sequence a1 , a2 , . . . , an of non-negative integer numbers. Denote as ci,j the number
of occurrences of the number i among a1 , a2 , . . . , aj . We call the sequence k-nice if for all i1 < i2 and for
all j the following condition is satisfied: ci1 ,j ≥ ci2 ,j − k.
Given the sequence a1 , a2 , . . . , an and the number k, find its longest prefix that is k-nice.
Input
The first line of the input file contains n and k (1 ≤ n ≤ 200 000, 0 ≤ k ≤ 200 000). The second line
contains n integer numbers ranging from 0 to n.
Output
Output the greatest l such that the sequence a1 , a2 , . . . , al is k-nice.
Example
nice.in
10 1
0 1 1 0 2 2 1 2 2 3
2 0
1 0
nice.out
8
0
Page 12 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem J. Crazy Nim
Input file:
Output file:
Time limit:
Memory limit:
nim.in
nim.out
2 seconds
64 megabytes
Alice and Bob like to play crazy nim. The game proceeds as follows. There are several stones arranged
in 3 piles that have a, b and c stones, respectively. Players make moves in turn, Alice moves first.
Each turn a player can choose any pile and take any number of stones from it. There is one restriction:
it is not allowed to make two piles of equal positive size. The person who takes the last stone wins.
For example, if there are three piles with 1, 3 and 5 stones, the valid moves are:
•
•
•
•
•
•
take
take
take
take
take
take
1
1
3
1
3
5
stone from the first pile;
stone from the second pile;
stones from the second pile;
stone from the third pile;
stones from the third pile;
stones from the third pile.
Given a, b and c, find out who wins the game if both players play optimally.
Input
Input file contains several test cases. Each test case consists of three integer numbers a, b and c on a line
(1 ≤ a, b, c ≤ 109 , a ̸= b, a ̸= c, b ̸= c). The test cases are followed by a line that contains three zeroes.
This line must not be processed.
Output
For each line output who wins the game if both players play optimally. Adhere to the format of sample
output.
Example
nim.in
1 2 3
1 3 5
0 0 0
nim.out
Alice wins the game.
Bob wins the game.
Page 13 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem K. Numbers
Input file:
Output file:
Time limit:
Memory limit:
numbers.in
numbers.out
2 seconds
64 megabytes
Consider numbers from 1 to n.
You have to find the smallest lexicographically number among them which is divisible by k.
Input
Input file contains several test cases. Each test case consists of two integer numbers n and k on a line
(1 ≤ n ≤ 1018 , 1 ≤ k ≤ n). The last test case is followed by a line that contains two zeroes. This line
must not be processed.
Output
For each test case output one integer number — the smallest lexicographically number not exceeding n
which is divisible by k.
Example
numbers.in
2000 17
2000 20
2000 22
0 0
numbers.out
1003
100
1012
Page 14 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem L. Integer Packing
Input file:
Output file:
Time limit:
Memory limit:
packing.in
packing.out
2 seconds
64 megabytes
When transmitting data by network it is important to pack it efficiently since this allows to optimize
bandwidth usage. We will describe a decoding procedure in one efficient way of packing integer numbers.
It uses variable length and has prefix property to ensure correct decoding. Your task will be to implement
the optimal encoding method.
The method is applicable for transmitting integer numbers from −263 to 263 − 1 (long type in Java).
Packed integer occupies from 1 to 9 bytes, depending on its value.
Decoding is performed in the following way. First the leading byte of the number is received. Looking
at its bits from higher to lower, find the first zero. Let q be the number of these leading ones (if the first
byte is 0xff, q = 8). q means the number of bytes to follow. If q = 8, these eight bytes are a standard
big-endian (higher byte first) representation of an integer number.
In the other case the following operation is performed: if the q + 2-nd bit of the first byte is zero (bits are
numbered from 1, higher bits first, if q = 7, the highest bit of the second byte is considered), leading ones
of the first byte are replaced by zeroes, and the number is padded on the left by zero bytes to eight bytes.
In the other case, the q + 1-st bit (it is always zero) is replaced with one, and the number is padded on
the left with 0xff bytes to eight bytes. The result is a standard big-endian representation of an integer
number.
Given several integer number, you must encode each of them so that the length of the encoded number
were minimal possible and it decoded to the original number.
Input
The first line of the input file contains n — the number of test cases (1 ≤ n ≤ 10 000). The following n
lines contain one number between −263 and 263 − 1, inclusive, each.
Output
Output n lines, each line must contain an encoded version of the corresponding number written as the
sequence of hexadecimal digits, higher bytes first.
Example
packing.in
9
0
1
2
3
100
500
1000000
-1
-100
packing.out
00
01
02
03
8064
81f4
cf4240
7f
bf9c
Page 15 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem M. Permutation Reconstruction
Input file:
Output file:
Time limit:
Memory limit:
permutation.in
permutation.out
2 seconds
64 megabytes
The famous Ulam Conjecture claims that if we take a graph G and consider a multiset Gmin of graphs
that are obtained from G by removing one of its vertices and all edges incident to it, then the graph
G can be reconstructed from Gmin . The Ulam conjecture is still not proven and no counterexample is
known.
In this problem we will consider a similar problem for permutations. Consider a permutation
a = ⟨a1 , a2 , . . . an ⟩ of numbers from 1 to n. Let us denote as a/i the permutation of n − 1 numbers
obtained from a by removing a number i and decreasing all numbers greater than i by one.
For example, if a = ⟨1, 3, 5, 2, 6, 4⟩ then a/2 = ⟨1, 2, 4, 5, 3⟩.
You are given a/1, a/2, . . . , a/n in some arbitrary order. You must restore the original permutation a.
Input
The first line of the input file contains n — the order of the initial permutation (5 ≤ n ≤ 300). The
following n lines contain n − 1 numbers each and specify a/i for all i in some order.
Output
Output n integer numbers — the permutation a. It is guaranteed that such permutation exists.
Example
permutation.in
6
1
1
1
2
1
1
permutation.out
1 3 5 2 6 4
3
3
2
4
4
3
5
4
4
1
2
2
2
2
5
5
5
5
4
5
3
3
3
4
In this example the factors are given in the following order: a/6, a/4, a/2, a/1, a/3 and a/5.
Page 16 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem N. Rectangular Polygon
Input file:
Output file:
Time limit:
Memory limit:
polygon.in
polygon.out
2 seconds
64 megabytes
A rectangular polygon is a polygon whose edges are all parallel to the coordinate axes. The polygon
must have a single, non-intersecting boundary. No two adjacent sides must be parallel.
Johnny has several sticks of various lengths. He would like to construct a rectangular polygon. He is
planning to use sticks as horizontal edges of the polygon, and draw vertical edges with a pen.
Now Johnny wonders, how many sticks he can use. Help him, find the maximal number of sticks that
Johnny can use. He will use sticks only as horizontal edges.
Input
The first line of the input file contains n — the number of sticks (1 ≤ n ≤ 100). The second line contains
n integer numbers — the lengths of the sticks he has. The length of each stick doesn’t exceed 200.
Output
Print l — the number of sticks Johnny can use at the first line of the output file. The following 2l lines
must contain the vertices of the rectangular polygon Johnny can construct. Vertices must be listed in
order of traversal. The first two vertices must be the ends of a horizontal edge. If there are several
solution, output any one. Vertex coordinates must not exceed 109 .
If no polygon can be constructed, output l = 0.
Example
polygon.in
4
1 2 3 5
4
1 2 4 8
4
1 1 1 1
polygon.out
3
0
1
1
3
3
0
0
4
0
1
1
2
2
1
1
0
0
0
1
1
2
2
0
0
1
1
-2
-2
-1
-1
In the first example Johnny uses a stick of length 1 for (0, 0)−(1, 0) edge, a stick of length 2 for (1, 1)−(3, 1)
edge and a stick of length 3 for (3, 2) − (0, 2) edge. There is no way to use all four sticks.
Page 17 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem O. Decoding Prefix Codes
Input file:
Output file:
Time limit:
Memory limit:
prefix.in
prefix.out
2 seconds
64 megabytes
A code is a mapping c : Σ → Γ∗ of characters of the given alphabet Σ to words of some another alphabet
Γ. In this problem Σ consists of lowercase letters of the English alphabet, and Γ = {0, 1}.
The code is called prefix-free or simply prefix, if no code word is a prefix of another word. For example,
the code c(‘a’) = 00, c(‘b’) = 01, c(‘c’) = 1 is prefix, but the code c(‘a’) = 0, c(‘b’) = 01 is not.
You are given a text and a string that is obtained from it by encoding it with some prefix code. You
must restore the code that was used to encode the text. If there are several possible variants, output any
one.
Input
The first line of the input file contains the given text. Its length does not exceed 1000. It contains only
letters ‘a’–‘z’ of the English alphabet. There are at most 10 different characters in the given text.
The second line contains the encoded version of the given text. It is guaranteed that it is obtained from
the given text by encoding by some prefix code. No word in the code used has length exceeding 10.
Output
Output the prefix code that could be used to encode the text to get the given encoded version. The
number of lines must be equal to the number of different characters in the given text. Each line must
contain a character followed by a space and the code word for this character.
Example
prefix.in
hello
0100111000
prefix.out
e
h
l
o
001
01
1
000
Page 18 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem P. Hungry Queen
Input file:
Output file:
Time limit:
Memory limit:
queen.in
queen.out
2 seconds
64 megabytes
Consider an infinite chessboard with cells identified by pairs of integer numbers: (x, y). The black queen
is initially located at the cell (0, 0). The queen can move horizontally, vertically or diagonally, but cannot
move downwards. That is, after each turn the y-coordinate of the cell where the queen is after the turn
must be greater or equal to the y-coordinate of the cell where it was before the turn.
There are n white pawns on the board, they are located at cells (xi , yi ), where yi > 0.
The queen wants to take as many pawns as possible. White pawns do not move, and the queen can make
as many consecutive turns as needed. However, each turn the queen must take a pawn.
Find out what is the maximal number of pawns the queen can take, and which pawns it must take to
achieve this number.
Input
The first line of the input file contains n (1 ≤ n ≤ 50 000). The following n lines contain two integer
numbers each — the coordinates (xi , yi ) of the pawns (|xi | ≤ 109 , 0 < yi ≤ 109 ). No two pawns occupy
the same position.
Output
The first line of the output file must contain one integer number k — the number of pawns the queens
can take. The second line must contain k integer numbers — the numbers of the pawns the queen can
take in order she must do it. The pawns are numbered starting from 1 in order they are given in the
input file.
Example
queen.in
4
1 1
4 3
-1 3
4 2
queen.out
3
1 3 2
Page 19 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem Q. Boat Race
Input file:
Output file:
Time limit:
Memory limit:
race.in
race.out
2 seconds
64 megabytes
Jerry is planning to take part in a boat race. The race will take place in a long narrow canal. The canal
runs from east to west, the banks of the canal have a form of a polyline.
Let us introduce the coordinate system in such a way that the western end of the canal has x-coordinate
equal to 0, the eastern end of the canal has x-coordinate equal to l. The polyline describing the southern
bank of the canal has vertices at points (x0 , y0 ), (x1 , y1 ), . . . , (xn , yn ) where 0 = x0 < x1 < . . . < xn = l.
The northern bank has the same form, but is w units to the north, so it is described by a polyline with
coordinates (x0 , y0 + w), (x1 , y1 + w), . . . , (xn , yn + w).
Jerry’s boat can start the race at any point of a start line (a segment (0, y0 ) − (0, y0 + w)) and end the
race at any point of the finish line (a segment (l, yn ) − (l, yn + w)). The boat is so small, that it can be
considered a point. When moving a boat can “touch” the banks of the canal, moving just along them.
To increase his chances of winning, Jerry wants to know what is the shortest path from the start line to
the finish line.
Input
The first line of the input file contains n (1 ≤ n ≤ 100). The following n + 1 lines contain a pair of
integer numbers (xi , yi ) each and describe the southern bank of the canal (0 = x0 < x1 < . . . < xn ≤ 104 ,
|yi | ≤ 104 ). The last line of the input file contains integer number w (1 ≤ w ≤ 104 ).
Output
Output one floating point number — the length of the shortest path through the canal from the start
line to the finish line. Your answer must be accurate up to 10−6 .
Example
race.in
3
0
2
3
5
2
race.out
5.41421356237309505
0
2
-1
0
Page 20 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem R. SETI
Input file:
Output file:
Time limit:
Memory limit:
seti.in
seti.out
2 seconds
64 megabytes
Amateur astronomers Tom and Bob try to find radio broadcasts of extraterrestrial civilizations in the
air. Recently they received some strange signal and represented it as a word consisting of small letters
of the English alphabet. Now they wish to decode the signal. But they do not know what to start with.
They think that the extraterrestrial message consists of words, but they cannot identify them. Tom and
Bob call a subword of the message a potential word if it has at least two non-overlapping occurrences in
the message.
For example, if the message is “abacabacaba”, “abac” is a potential word, but “acaba” is not because
two of its occurrences overlap.
Given a message m help Tom and Bob to find the number of potential words in it.
Input
Input file contains one string that consists of small letters of the English alphabet. The length of the
message doesn’t exceed 10 000.
Output
Output one integer number — the number of potential words in a message.
Example
seti.in
abacabacaba
seti.out
15
Page 21 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem S. Spam Filter
Input file:
Output file:
Time limit:
Memory limit:
spam.in
spam.out
2 seconds
64 megabytes
Most spam filters use so called Bayesian filter to choose which message is spam and which is not. Let us
describe the idea of such filter.
The first stage of using Bayesian filter is its training. During training the filter is shown a number of
messages for each of which it is specified whether it is spam or not. Denote the set of messages that are
spam as Spam and the set of other messages as Good.
Filter parses the messages to words and for each word w it calculates the probability that it is in a spam
message:
|{M |w ∈ M and M ∈ Spam}|
P (w ∈ M |M ∈ Spam) =
,
|Spam|
and the probability that it is not in a spam message
P (w ∈ M |M ∈ Good) =
|{M |w ∈ M and M ∈ Good}|
.
|Good|
After that for each word w in a message to classify the filter uses this information to calculate
P (M ∈ Spam|w ∈ M ) using Bayes’ formula:
P (M ∈ Spam|w ∈ M ) =
P (w ∈ M |M ∈ Spam)P (Spam)
.
P (w ∈ M |M ∈ Spam)P (Spam) + P (w ∈ M |M ∈ Good)P (Good)
Here P (Spam) = |Spam|/(|Spam| + |Good|) and P (Good) = |Good|/(|Spam| + |Good|). Here let 0/0 = 0
(this happens if the word did not occur in training messages).
After that the ratio of words that indicate that M ∈ Spam with probability of at least 1/2 is calculated.
If it reaches some threshold t the message is classified as spam. Note that each word is analyzed only
once even if it occurs several time in a message.
In this problem you will have to implement Bayesian spam filter. You will be given the set of messages
to train and after that the set of messages to classify. For each message to classify you will have to tell
whether it is spam or not.
Input
The first line of the input file contains four integer numbers: s, g, n and t — the number of spam messages
to train on, the number of good messages to train on and the number of messages to classify, and the
threshold in percent (1 ≤ s ≤ 1000, 1 ≤ g ≤ 1000, 1 ≤ n ≤ 1000, 1 ≤ t ≤ 100).
The following s lines contain one message per line that are specified to be spam. A message is the
sequence of one or more words that consist of letters of the English alphabet and are separated and/or
surrounded by spaces, digits and punctuation marks (‘-’, ‘,’, ‘.’, ‘!’, ‘?’). All words are case insensitive.
The length of each message is at most 200 characters. Word length doesn’t exceed 20.
The following g lines contain good messages, one per line, and the last n lines contain messages to be
classified, one per line.
Output
For each message to be classified print “spam” if at least t% words indicate that it is spam, and “good”
in the other case.
Page 22 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Example
spam.in
2 2 2 50
Buy our best computers!
You will find our computers best!
I have completed writing problems for trainings.
I have problems with my computer.
Computers? Solution!
I do not know what to buy. Need help.
spam.out
spam
good
Page 23 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem T. Fun with Squares
Input file:
Output file:
Time limit:
Memory limit:
squares.in
squares.out
2 seconds
64 megabytes
Johnny has n squares with edges parallel to coordinate axes on the plane. Given two squares A and B
let us denote a figure that contains all points of A that do not belong to B as A − B.
−
=
−
=
−
= empty figure
Now Johnny wants to find the number of collections of four distinct squares A, B, C and D such that
A − B and C − D are equal up to a parallel shift. Rotation is not allowed.
Input
The first line of the input file contains n — the number of squares (4 ≤ n ≤ 400). The following n lines
describe squares. Each square is described by three integer numbers: x, y and l — the coordinates of its
left bottom corner and the length of its edge (−109 ≤ x, y ≤ 109 , 1 ≤ l ≤ 109 ).
Output
Output the number of sets of four distinct squares A, B, C and D such that A − B and C − D are
equal up to a parallel shift. Collections that contain the same set of squares but differ by square roles
are considered different.
Example
squares.in
4
0
1
2
1
4
0
1
2
4
squares.out
6
0
1
2
1
3
1
1
3
0
0
1
2
4
3
3
3
3
In the first example the collections are: ⟨0, 1, 3, 2⟩, ⟨1, 0, 2, 3⟩, ⟨1, 3, 2, 0⟩, ⟨2, 0, 1, 3⟩, ⟨2, 3, 1, 0⟩, ⟨3, 2, 0, 1⟩.
Page 24 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem U. Longest Common Subpair
Input file:
Output file:
Time limit:
Memory limit:
subpair.in
subpair.out
2 seconds
64 megabytes
A pair of strings (α, β) is called a subpair of a string γ if γ = γ1 αγ2 βγ3 for some (possibly empty) strings
γ1 , γ2 and γ3 . The length of the pair is the sum of lengths of its strings: |(α, β)| = |α| + |β|.
Given two strings ξ and η find their longest common subpair, that is — such pair (α, β) that it is a
subpair of both ξ and η and its length is greatest possible.
Input
Input file contains two strings ξ and η, one on a line. Both strings contain only small letters of the
English alphabet. Both string are not empty. The length of each string doesn’t exceed 3000.
Output
Output α on the first line of the output file and β on the second line.
Example
subpair.in
abacabadabacaba
acabacadacabaca
ab
bc
subpair.out
acaba
abaca
b
Page 25 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem V. Sum vs Product
Input file:
Output file:
Time limit:
Memory limit:
sump.in
sump.out
2 seconds
64 megabytes
Peter has just learned mathematics. He learned how to add, and how to multiply. The fact that
2 + 2 = 2 × 2 has amazed him greatly. Now he wants find more such examples.
Peters calls a collection of numbers beautiful if the product of the numbers in it is equal to their sum.
For example, the collections {2, 2}, {5}, {1, 2, 3} are beautiful, but {2, 3} is not.
Given n, Peter wants to find the number of beautiful collections with n numbers. Help him!
Input
The first line of the input file contains n (2 ≤ n ≤ 500).
Output
Output one number — the number of the beautiful collections with n numbers.
Example
sump.in
2
5
sump.out
1
3
The collections in the last example are: {1, 1, 1, 2, 5}, {1, 1, 1, 3, 3} and {1, 1, 2, 2, 2}.
Page 26 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem W. Superposition
Input file:
Output file:
Time limit:
Memory limit:
superposition.in
superposition.out
2 seconds
64 megabytes
Tony is writing financial software. One of the components he is writing now will have to deals with risk
and income graphs of various market instruments. Now he needs to plot the graph of a superposition of
two continuous piecewise linear functions.
Tony is given two functions: f (x) and g(x) and needs to find the explicit representation of the function
g(f (x)).
Input
The input file consists of two blocks, one describes f and another describes g.
Each blocks starts with l — the number of intervals of linearity of a function (1 ≤ l ≤ 1000, l ̸= 2).
If l > 2, the following l − 1 lines contain the breaking points of the function. Each line contains two
integer numbers: xi , yi (i from 1 to l − 1, xi < xi+1 ). The function a(x) described by the block is defined
as follows:


y1 , if x < x1 ;





y1 + k1 (x − x1 ), if x1 ≤ x < x2 ;
a(x) = . . .


yl−2 + kl−2 (x − xl−2 ), if xl−2 ≤ x < xl−1 ;




yl−1 , if x ≥ xl−1 .
Here ki = (yi+1 − yi )/(xi+1 − xi ).
If l = 1 the following line contains one integer number y, the function described is a(x) = y.
All coordinates in the input file do not exceed 109 by their absolute values.
Output
Output the function g(f (x)) in the same format as the functions in the input file. The number of intervals
of linearity in the description must be minimal possible. It is guaranteed that the number of intervals of
linearity of the resulting function will not exceed 100 000. Output floating point numbers with absolute
or relative precision of at least 10−8 .
Example
superposition.in
3
0
2
4
0
1
2
0
3
superposition.out
4
0 0
0.666666666667 3
1.333333333333 0
0
3
0
Page 27 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem X. High Speed Trains
Input file:
Output file:
Time limit:
Memory limit:
trains.in
trains.out
2 seconds
64 megabytes
The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was amazed by
high speed trains Shinkansens going all around the country. Therefore he decided to build the system of
high speed trains in Flatland.
Each high speed train line will be bidirectional and connect exactly two different cities of Flatland.
Although there is actually no need of high speed trains in Flatland, the king ordered that there must be
at least one high speed train line from each city of Flatland.
The minister of transportation told the king that there are several train system satisfying his requirements.
The king was amazed by the fact and asked the minister to count the number of possible systems.
Help the minister to calculate the number of train systems.
Input
The input file contains one integer number n (2 ≤ n ≤ 100).
Output
Output one integer number — the number of different train systems that can be arranged in Flatland.
Example
trains.in
4
trains.out
41
Page 28 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem Y. New Year Tree Transportation
Input file:
Output file:
Time limit:
Memory limit:
tree.in
tree.out
2 seconds
64 megabytes
People of Byteland celebrate New Year. Unlike people of most other countries, they do not decorate
fir-trees for the New Year celebration. Instead they decorate binary trees. A binary tree is a rooted tree
such that every node has at most two children.
A nice binary tree with n nodes was prepared to be set up on the main square of Byteland capital. However
first it must be transported from the place where it was grown up to the capital. The transportation will
be arranged by the railroad. But it turned out that the standard railroad car can carry the tree only if
it has at most k nodes.
So it was decided to cut several edges of the tree so that each of the remaining connected parts had at
most k nodes. After the tree is transported to the capital it would be reassembled and set up. Due to
security reasons each car must carry only one tree part.
Of course the department of transportation of Byteland would like to use as few cars as possible to
transport the tree. However, minimizing the number of cars seemed to be too difficult problem. Therefore
the minister of transportation ordered to cut the tree in such a way that the number of cars needed at
least did not exceed ⌈2n/k⌉.
But the people who are transporting the tree couldn’t solve even this problem. Help them! Given a
binary tree find the way to cut some of its edges in such a way that each of the remaining connected
parts had at most k nodes and the number of parts didn’t exceed ⌈2n/k⌉.
Input
The first line of the input file contains n — the number of nodes of the tree, and k — the maximal
capacity of the car (1 ≤ n ≤ 100 000, 1 ≤ k ≤ n). The following n − 1 lines describe the edges of the tree.
Each edge is described by two integer numbers: the parent node and the child node. The given tree is
guaranteed to be a binary tree. Nodes are numbered from 1 to n, root has number 1.
Output
The first line of the output file must contain l — the number of edges that must be cut. The second line
must contain l integer numbers — the edges to be cut. Edges are numbered from 1 to n − 1 as they are
listed in the input file.
If the tree cannot be cut in the described way, output l = −1.
Example
tree.in
5
1
1
5
5
2
2
5
3
4
tree.out
2
2 4
Page 29 of 30
ITMO Summer Camp Scholarship
Online Training Program, April 2015
Problem Z. TV Show
Input file:
Output file:
Time limit:
Memory limit:
tvshow.in
tvshow.out
2 seconds
64 megabytes
Charlie is going to take part in one famous TV Show. The show is a single player game. Initially the
player has 100 dollars. The player is asked n questions, one after another. If the player answers the
question correctly, the sum he has is doubled. If the answer is incorrect, the player gets nothing and
leaves the show.
Before each question the player can choose to leave the show and take away the prize he already has.
Also once in the game the player can buy insurance. Insurance costs c dollars which are subtracted from
the sum the player has before the question. Insurance has the following effect: if the player answers
the question correctly his prize is doubled as usually, if the player answers incorrectly the prize is not
doubled, but the game continues. The player must have more than c dollars to buy insurance.
Charlie’s friend Jerry works on TV so he managed to steal the topics of the questions Charlie will be
asked. Therefore for each question i Charlie knows pi — the probability that he will answer this question
correctly. Now Charlie would like to develop the optimal strategy to maximize his expected prize. Help
him.
Input
The first line of the input file contains two integer numbers n and c (1 ≤ n ≤ 50, 1 ≤ c ≤ 109 ). The
second line contains n integer numbers ranging from 0 to 100 — the probabilities that Charlie will answer
questions correctly, in percent.
Output
Output one real number — the expected prize of Charlie if he follows the optimal strategy. Your answer
must have relative or absolute error within 10−8 .
Example
tvshow.in
2 100
50 50
2 50
50 50
2 50
60 0
tvshow.out
100
112.5
120
The optimal strategy in the second example is to take insurance for the second question. In this case the
expected prize is 1/2 × 0 + 1/2 × (1/2 × 150 + 1/2 × 300).
In the third example it is better to leave the show after the first question, because there is no reason to
try to answer the second one.
Page 30 of 30