Beruflich Dokumente
Kultur Dokumente
ambitious.in
ambitious.out
4 seconds
512 megabytes
Steven Sleepberg is going to produce the forty seventh sequel to his lm Battleeld Jupiter and is now
planning the main battle scene. The script of the scene says that n empire drones would attack m republic
forts. The attack proceeds as follows: a drone shoots the laser cannon towards one of the forts. The defense
in turn has t power towers, and one pair of towers creates a power shield. The power shield is a segment
connecting the two towers. If the shield intersects the segment which connects the shooting drone with
its target a spectacular explosion takes place.
Steven wants the scene to be as spectacular as possible. So he wants to lm all possible pairs of shoot
and shield that cause explosion. Help him to estimate the length of the scene by counting the number of
possible explosions.
Let us consider drones, forts and power towers as points on a plane. The line of defense coincides with
the line y = 0, so y-coordinates of all forts and towers are less than 0, and y-coordinates of all drones
are greater than 0. You are given coordinates of n drones, m forts and t towers. Find the number of sets
{D, F, T1 , T2 } such that D is drone, F is fort, T1 and T2 are towers, and segments DF and T1 T2 intersect.
No two points coincide, no three points are on the same line.
Input
The input le contains multiple test cases.
Each test case starts with n the number of drones (1 n 1500), n lines follow, each line contains
dxi , dyi coordinates of a drone. Integer m the number of forts (1 m 1500) follows, with m
more lines, each line contains f xi , f yi coordinates of a fort. Finally there goes t the number of towers
(2 t 1500) followed by t lines, each line contains txi , tyi coordinates of a tower.
All drones coordinates satisfy 109 dxi 109 , 0 < dyi 109 . All forts and towers coordinates satisfy
109 f xi , txi 109 , 109 f yi , tyi < 0.
Input is followed by a line containing a single zero.
The total number of drones in the input le is at most 1500. The total number of forts in the input le
is at most 1500. The total number of towers in the input le is at most 1500.
Output
For each test case print one integer: the number of possible pairs of shoot-shield that cause an explosion.
Example
ambitious.in
3
1 12
10 30
30 10
1
10 -10
4
2 -11
9 -1
11 -1
15 -14
0
ambitious.out
7
Page 1 of 15
decomposable.in
decomposable.out
2 seconds
512 megabytes
Deterministic nite automaton (DFA) is an ordered set h, U, S, T, i where is the nite set called input
alphabet, in this problem = {a, b, . . . , z}, U is the nite set of states, S U is the initial state, T U
is the set of terminal states and : U U is the transition function.
The input of the automaton is the string over . Initially the automaton is in state s. Each step the
automaton reads the rst character c of the input string and changes its state to (u, c) where u is the
current state. Then the rst character of the input string is removed and the step repeats. If after its
input string is empty the automaton is in the terminal state, it accepts the initial string , in the other
case it rejects it. The set of all words accepted by an automaton A is denoted as L(A).
One can visualize DFA as a directed graph representing its states as vertices and its transitions as edges
marked with characters. Terminal states are shown as double circled vertices, the initial state is marked
by an arrow. The picture below on the left shows the automaton for a language (ab) of words that
consist of zero or more repeated words ab. The picture below on the right shows the automaton for a
language a ba ba that consist of a and b and contain two b-s. For sake of clarity edges marked by
represent all transitions not explicitly drawn.
a
a
b
b
*
b
*
A set X of words is called a regular language if it is equal to L(A) for some DFA A. The index of a regular
language X denoted as ind(X) is the minimal number of states in a DFA A such that L(A) = X. For
example, the two automatons shown on the picture above are indeed the minimal DFA-s for the described
languages, so ind((ab) ) = 3 and ind(a ba ba ) = 4.
It is well known that if X1 and X2 are two regular languages its intersection X1 X2 is also a
regular language. For example, the intersection of the two languages described above is the language
Y = (ab) (a ba ba ) = {abab} that contains a single word abab. Clearly a single word language is
regular, the automaton for Y is shown on the picture below.
a
b
*
It is easy to see that if W is a single word language W = {w}, and length of w is n, the index of W is
equal to n + 2.
Page 5 of 15
Input
The input le contains multiple test cases.
Each test case consists of a word w on a line on itself, w consists of lowercase letters of the English
alphabet, length of w is between 1 and 50, inclusive.
There are at most 100 tests in one input le.
Output
For each test case rst print YES if the corresponding single-word language is decomposable, or NO if
it is not. If the language is decomposable, the description of two DFA-s must follow. Each DFA description
must start with k the number of states, 1 k n + 1, where n is the length of the input word. Let
states be numbered from 1 to k, the initial state is the state number 1. Then print t the number of
terminal states, 1 t k, followed by t integers from 1 to k terminal states. The following k lines
must contain 26 integers each: for a state u print (u, a), (u, b), . . . , (u, z).
Examples
decomposable.in
abab
a
decomposable.out
YES
3
1
1
2 3
3 1
3 3
4
1
3
2 1
3 2
4 3
4 4
NO
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
4
Page 6 of 15
4
4
4
4
bayes.in
bayes.out
Time limit:
2 seconds
Memory limit:
512 mebibytes
Input le:
Bayes' Law is one of the central theorems of elementary probability theory. It allows to update probability
on the value of
Input
The input le contains multiple test cases.
n+1
The last test case is followed by a zero that should not be processed. The sum of
input le is at most
100 000.
Output
L and R. Your answer may have absolute or relative error of at
P (L R | a f () b) condition and R L minimization. Tests are
6 ) but
designed in such way that there are no segments [L , R ] such that R L < (R L)(1 10
P (L R | a f () b) 106 .
For each test case two real numbers:
most
106
in both
Examples
6
3.0 5.0
0.9
0 2
2 5
5 0
7 2
8 1
13 6
15 0
0
bayes.in
bayes.out
1.0 13.813333333333333
Page 2 of 15
kabbalah.in
kabbalah.out
Time limit:
5 seconds
Memory limit:
512 mebibytes
Input le:
Kai and Kevin are kabbalists. However, they don't like traditional kabbalah with its pentagrams, etc.
They have developed their own version of kabbalah that has its rituals centered around circles.
Now they are preparing a place for their kabbalistic rituals. They are going to have rituals at the backyard
of Kevin's grandmother's house. The backyard has a form of a convex polygon with
vertices. Friends
need to place two circular mats to the yard for their rituals. The mats must t completely into the yard
without overlapping. Of course none of the friends would agree to have a smaller mat, so their radii must
be equal to each other.
To have as powerful rituals as possible, Kai and Kevin would like to have as big mats as possible. Help
them to nd out what maximal radius of the mats could be.
Input
The input le contains multiple test cases.
The rst line of each test case contains
The following
lines describe vertices of the polygon in counterclockwise order, each vertex is described
xi
and
yi (104 xi , yi 104 ).
Output
For each test case rst output one oating point number the maximal possible radius of two nonoverlapping circular mats of the same size that can be put to the backyard. The following two lines must
contain two oating point numbers each coordinates of centers of mats.
Your answer must have absolute or relative error of at most
106 .
Examples
kabbalah.in
4
0 0
10 0
10 10
0 10
0
kabbalah.out
2.9289321881345248
2.9289321881345248 2.9289321881345248
7.0710678118654752 7.0710678118654752
Page 15 of 15
Notes
magic.in
magic.out
Input le:
Output le:
Time limit:
1 second
Memory limit:
256 megabytes
L'Ashyto is a famous magician. He is working in his laboratory on a new set of magic potions to sell on
the upcoming magic fair. He has bought several bottles of each of
be used to create magic potions. Each magic potion must be composed of two dierent magic substances.
To create a potion the magician mixes the contains of two bottles and pronounces the magic spell.
Of course, L'Ashyto would like to create as many potions as possible. But he would like the quality of
their ingredients be as good as possible. L'Ashyto has numbered all substances from 1 to
number 1 being the best, and substance number
n,
substance
the maximal possible number of potions, he would like to create as many potions from substances 1 and
2, as possible. If there are still several variants, he would like to maximize the number of
then
1+4
potions, etc,
1+n
potions,
2+3
1+3
potions,
potions, etc. Help him to nd out how many potions of which
ingredients to create.
Input
The rst line of the input le contains
The second line contains
a1 , a2 , . . . , an the number
9
substance, etc (1 ai 10 ).
integer numbers:
n 100 000).
Output
The rst line of the output le must contain
must create. The following
i, j, cij
the numbers of
substances to create a potion of, and how many such potions to create. For each description there must
be
i < j.
Examples
4
1 1 1 1
magic.in
2
1 2 1
3 4 1
Page 9 of 12
magic.out
restore.in
restore.out
Input le:
Output le:
Time limit:
1 second
Memory limit:
256 megabytes
A permutation
a1 , a2 , . . . , an
a1 , a2 , . . . , an
is said to have a
descent
is said to have a
xed point
at position
A = a1 , a2 , . . . , a2n
of
2n
if
at position
if
ai > ai+1 .
A permutation
ai = i.
2n alternatively xed
if
from 1 to
has exactly
has exactly
for all
xed points.
3, 2, 6, 4, 5, 1
is alternatively xed.
Consider the following transformation of alternatively xed permutation: remove all xed points
and turn the remaining vector to permutation by replacing each remaining number with the count
of remaining numbers not exceeding it. For example, transforming the above permutation yields
3, 2, 6, 4, 5, 1 3, 6, 1 2, 3, 1.
You are given the result of the transformation. Restore the original alternatively xed permutation.
Input
The rst line of the input le contains
(1
n 100 000).
Output
If there is no solution, output
at the rst line of the output le. In the other case output the original
2n
Examples
3
2 3 1
1
1
restore.in
3 2 6 4 5 1
-1
Page 10 of 12
restore.out
tour.in
tour.out
Time limit:
3 seconds
Memory limit:
256 megabytes
Input le:
There are
cities in Flatland, some of them are connected by bidirectional roads. The system of roads is
organized in such way that there is exactly one way to get from any city to any other city by the roads.
Recently the king of Flatland has decided that he would like to travel around the country and visit all the
cities. He would like to start travelling from the capital of the country, travel along the roads and return
to the capital visiting each city exactly once.
The minister of transport of Flatland told the king that it is impossible to do so, because there are no
cycles in the road system of Flatland, in particular there is no way to travel from the capital back to the
capital without visiting a city more than once. But the king insisted that he would like to perform the
tour. So the minister ordered to build new roads in order to make such tour possible.
Help the minister to nd the minimal number of roads to build so that the king could perform his tour.
Input
The rst line of the input le contains
n 100 000).
The following
n1
lines describe roads. Each road is described by two integer numbers the numbers of cities it connects.
The cities are numbered from 1 to
n.
Output
Output one integer number the minimal number of roads that must be built.
Examples
5
1
2
2
1
tour.in
2
3
4
5
tour.out
Page 10 of 11
peaks.in
peaks.out
Time limit:
2 seconds
Memory limit:
256 megabytes
Input le:
a1 , a2 , . . . , an of
< ai > ai+1 are satised
Permutation
ai1
for exactly
dierent indices
is said to have
(we consider
peaks if inequalities
a0 = an+1 = 0
for the
n and k
3, 1, 4, 5, 2
i=1
and
i = 4.
n with exactly k
Input
Input le contains two integer numbers:
and
k (1 n 1015 , 1 k 30).
Output
Output one integer number the number of permutations of numbers from 1 to
modulo 239.
Examples
3 1
10 3
peaks.in
4
131
Page 9 of 11
peaks.out
with exactly
peaks,
square.in
square.out
1 second
64 megabytes
Some sets of n words of length n have a nice property it is possible to arrange them in the n n grid
so that this set of words is read both vertically and horizontally.
An example of such set is { DATE, FIND, IDEA, NEXT }. You can arrange them in the following
way:
F
I
N
D
I
D
E
A
N
E
X
T
D
A
T
E
Note that you can nd each word exactly once as a horizonatal word, and as a vertical word. Such
squares are called word squares, the largest known word square is 10 10.
Another example of the 4 4 word square is:
C
R
A
B
R
A
R
E
A
R
T
S
B
E
S
T
You are given 2n words such that it is possible to construct two dierent n n word squares from them.
You task is to divide the words into two groups of n words and construct the words square from each
group.
You are guaranteed that all words are the English words (however, some of them are really rare ones,
human names, or some extremely special terms).
Input
The rst line of the input le contains n (2 n 10). The following 2n lines contain n-letter word each,
words consist of capital English letters.
Output
Output two n n word squares constructed from the given words. Separate squares by a blank line.
Examples
square.in
4
ARTS
BEST
CRAB
DATE
FIND
IDEA
NEXT
RARE
square.out
FIND
IDEA
NEXT
DATE
CRAB
RARE
ARTS
BEST
Page 8 of 13
Problem H. Subword
Input le:
Output le:
Time limit:
Memory limit:
subword.in
subword.out
1 second
64 megabytes
Input
The rst line of the input le contains . The second line contains . Both words are not empty and
contain at most 2000 characters each.
Output
Output one word the shortest word equivalent to that contains as a subword. If there are several
solutions, print any one.
Examples
subword.in
abAaBBabbA
AaBaba
subword.out
aAaBabaAbA
Page 9 of 13
Problem B. Darts
Input le:
Output le:
Time limit:
Memory limit:
darts.in
darts.out
1 second
64 megabytes
The game of darts is played by two or more players. Players in turn throw three darts into the dartboard.
Initially each player has some number of points, usually 501. Each dart thrown at the dartboard scores
some points. This number is subtracted from the players points. The rst one to reach exactly zero is
the winner.
The dartboard looks as shown on a picture. There are 20 sectors ar5 20 1
ranged around the central circle, called the bull eye. This circle in turn
12
18
consists of the inner bull eye (or simply the bull eye), and outer bull
9
4
eye. Outer bull eyes score is 25, inner bull eye is double outer, which is
50. The outer sectors score the number of points they are marked with.
14
13
There are also two circular areas going around the board. The outer
11
6
circle area doubles the points of the sector, and the inner one triples
them.
8
10
There are additional rules for the last round when the player is to de16
15
crease his score to zero. He can do it by throwing from one to three
7
2
19 3 17
darts. First, the player must decrease it to exactly zero, going below
zero is not allowed. Second, the last dart in a game must be double
that is, it must be thrown into the double part of some segment or into
the bull eye (which is double outer bull eye).
For example, one correct way to nish the game from 50 points is by throwing darts to 18 and D16.
Finishing by playing D20, 10, or 20, T10 is illegal (the last throw in these cases is not double).
Another correct way to win in this case is Bull.
Given the number of points left, determine all the ways to nish the game correctly.
Input
The rst line of the input le contains n the number of points left (1 n 200).
Output
At the rst line of the output le print k the number of ways to correctly nish the game. After
that print k lines, each must contain one correct way to nish the game. Use sectors value to denote it,
preceed it with D for double area, T for triple area. Use 25 to denote the outer bull eye, Bull to
denote the bull eye itself.
Examples
darts.in
5
darts.out
7
1 D1 D1
1 2 D1
1 D2
D1 1 D1
T1 D1
2 1 D1
3 D1
Page 2 of 13