Problem Set
Problem Set
Contest Session
Universidad de Carabobo, Venezuela
This problem set should contain a rules section and nine (09) problems on thirteen (13) numbered pages.
Please inform the Contest Staff immediately if something is missing from your problem set.
Welcome to the Programming Contest and enjoy it!
Rules
• Each team will be provided with a single computer. All teams will have equivalent computing equip-
ment.
• There are nine (09) problems for each team to be completed in five (05) hours.
• All problems require that you read test data from the standard input and write results to the standard
output.
• You may use any of the following programming languages: C, C++ or Java. You may use different
programming languages for different problems and even for different submissions of a problem.
• All problems have a source file name shown below the title of the problem. The extensions you must
use for your source files are: .c for programs written in C, .cpp for those in C++, and .java for those
in Java.
• You may use any of the standard libraries that your chosen programming language provides. You may
not use any other library that requires an extra flag to be passed to the compiler command. If you do
this, judges will probably get a compilation-linking error in your program.
• Output corresponds exactly to the provided sample output format, including (mis)spelling and spacing.
Multiple spaces will not be used in any of the judges’ output, except where explicitly stated.
• Your solution to any problem should be submitted for judging using DOMjudge system (if you are
working in online mode) or PC2 software (if you are working in local mode). Once you have submitted
a solution, it will reach the judges. The time it takes for your problem to be judged will depend on
how busy the judges are. Once your submission has been judged, you will receive a message through
the DOMjudge or PC2 software indicating the judgment. If your solution is accepted the message will
be “Yes” or “Correct”, if it is not then the message will be “No” or “Incorrect”, along with the type of
error the judges encountered.
• Programming style is not considered in this contest. The judges will only test whether the input /
output behavior of your program is correct or not. However, each problem has an unknown execution
time-limit (in seconds). That is, if your program takes more than this time-limit to execute for the
given input, it will be judged as incorrect.
• Contestants may bring any printed materials (books, papers, documentation, source code of programs,
etc.) to the contest area, but no soft copy will be allowed (diskettes, CDs, DVDs, pen-drives, etc.).
1
1 Recognizing h-sequences
Source file name: hseqs.c, [Link] or [Link]
An h-sequence is either a single 0 or it is a 1 followed by two h-sequences. Syntactically, h-sequences may be
defined by the following grammar expressed in BNF (Backus-Naur Form) notation:
2
2 S-Trees
Source file name: strees.c, [Link] or [Link]
A Strange Tree (S-tree) over the variable set Xn = {x1 , . . . , xn } is a complete binary tree representing a
Boolean function f : {0, 1}n → {0, 1}. Each of the S-tree’s nodes has a depth, defined as the number of nodes
in the path from the root to itself minus 1 (so the root has depth 0). The depth of any node in an S-tree is at
most n. The nodes with depth less than n are called non-terminal nodes, each having two children: the right
child and the left child. Each non-terminal node is labeled with some variable xi from the variable set Xn .
All non-terminal nodes with the same depth are labeled with the same variable, and non-terminal nodes with
different depth are labeled with different variables. So, there is a unique variable xi0 corresponding to the
root, a unique variable xi1 corresponding to the nodes with depth 1, and so on. The sequence of the variables
xi0 , xi1 , . . . , xin−1 is called the variable ordering. The nodes having depth n are called terminal nodes. They
have no children and are labeled with either 0 or 1. Note that the variable ordering and the distribution of
0’s and 1’s on terminal nodes are sufficient to completely describe an S-tree.
As stated earlier, each S-tree represents a Boolean function f . If you have an S-tree and values for the
variables x1 , . . . , xn , then it is quite simple to evaluate f (x1 , . . . , xn ): Start at the root and repeat the
following steps: if the node you are at is labeled with a variable xi , then depending on whether the value of
xi is 1 or 0, you go to its right or left child, respectively. Once you reach a terminal node, its label gives the
value of the function.
In the picture, the two S-trees represent the same Boolean function:
x3 x1 x2
In the next line the distribution of 0’s and 1’s over the terminal nodes is given. There will be exactly 2n
characters (each of which can be 0 or 1), followed by the new-line character. The characters are given in the
3
order in which they appear in the S-tree, the first character corresponds to the leftmost terminal node of the
S-tree, the last one to its rightmost terminal node.
The next line contains a single integer m, the number of VVAs, followed by m lines describing them. Each
of the m lines contains exactly n characters (each of which can be 0 or 1), followed by a new-line character.
Regardless of the variable ordering of the S-tree, the first character always describes the value of x1 , the
second character describes the value of x2 , and so on. So, the line
110
4 S-Tree #2
5 0011
6
4
3 See You in XCNGPEKC at 60◦
Source file name: seeyou.c, [Link] or [Link]
A well known network of drug dealers has designed a special encryption method to encode the hour and
place where they will receive their packages. This method uses the position of an analog clock hour hand
(measured in degrees) to represent an hour, a positive integer which represents the number of characters of
the alphabet to count backwards to find each correct character of the word, and an encrypted word. The
Intelligence Department of the police has found a complete list of the next arrival times and places of the
packages, and your job is to write a program capable of decoding the information.
5
4 Led Codes
Source file name: ledcode.c, [Link] or [Link]
Some led systems display characters as a combination of lightlines, much like digital calculators do. Suppose
we have one of such led systems, in which each letter of the standard English alphabet is constructed
combining some of ten possible lightlines, numbered as below:
For instance, the letter A is shown using the lines 1, 2, 3, 4, 5 and 7. With these few lines it is not possible,
of course, to show all the 26 letters with their natural shapes. A complete list of the letters of our system is
appended at the end.
You have to solve the problem of decoding a combination of numbers (representing lightlines) in the form of
the respective letter, to form a whole word or phrase. As input you have a string of letters and/or numbers,
and as output you must show only the letters.
Phrase 1: .....
Phrase 2: .....
where the dots are, of course, the result of the decoding process. The alphabetic letters and blank spaces
must not be decoded at all: only the numbers must be converted to the corresponding led letters.
The output must be written to standard output.
6
4.4 Output for the Sample Input
1 Phrase 1: HELLO WORLD
2 Phrase 2: PROGRAMMING IS COOL
3 Phrase 3: AND MORE IF IN THIS CONTEST
7
5 Counting Graphs
Source file name: counting.c, [Link] or [Link]
A simple graph is an unweighted, undirected graph containing no loops (a loop is an edge that connects a
vertex to itself) or multiple edges. In this problem you have to count the number of simple graphs without
isolated nodes that you can build using n nodes.
8
6 Cable Network
Source file name: cn.c, [Link] or [Link]
A local cable network is planning to expand their services to a neighboring city. For that, a transmission
center is being built and a cable net connecting the new clients has to be installed. Each client has to be
connected to the center in the cable net.
The network organized a survey and has now the addresses of all potential clients. Also it has an estimate
on the cost of installing each cable link through the city.
The network board of directors has decided to offer the following initial subscription package: Each new client
will pay a fixed subscription fee and will get the first three months of service for free. This ensures a potential
initial capital for building the cable net, which is simply the number of potential clients (according to the
survey) multiplied by the subscription fee.
The decision on what links to build in this first phase will be done according to the following. A set of links
induces a loss in the potential initial capital, defined as l + (t × d), where l is the cost of installing the
links in the set, t is the subscription fee and d is the number of potential clients that are not connected to
the center by this set of links. The board of directors decided they will build a set of links that induces a
loss as small as possible in the potential initial capital.
Your job is to write a program to compute the minimum possible loss in the potential initial capital. For
that, you are given the location of each potential client as well as the center location. Also, you are given
the costs of all possible net links and the value of the subscription fee.
5 19 21 5 Cable Net #2
6 40 6 3
7 3 1 7 3
8 5 3 23 8
9 1 19
10 14
11 0 1
9
7 Magic Squares
Source file name: magic.c, [Link] or [Link]
Among the oldest and most popular forms of mathematical recreations are magic squares. A magic square
of order n is an n × n array constructed out of the integers 1, 2, 3, . . . , n2 in such a way that the sum s of the
integers in each row, in each column, and in each of the two diagonals is the same. The number s is called
the magic sum of the magic square. Examples of magic squares of order 3 and 4 are
8 1 6
3 5 7 (1)
4 9 2
16 3 2 13
5 10 11 8
9
(2)
6 7 12
4 15 14 1
with magic sum 15 and 34 respectively. In medieval days there was a certain mysticism associated wirh magic
squares, and they were worn for protection against evils. Benjamin Franklin was a magic square fan, and his
papers contain many interesting examples.
We describe here a method found by de la Loubère in the seventeenth century for constructing magic squares
of order n when n is odd. First a 1 is placed in the middle square of the top row. The successive integers
are then placed in their natural order along a diagonal line which slopes upwards and to the right, with the
following modifications:
1. When the top row is reached, the next integer is put in the bottom row as if it came immediately above
the top row.
2. When the right-hand column is reached, the next integer is put in the left-hand column as if it imme-
diately succeeded the right-hand column.
3. When a square is reached which has already been filled or when the top right-hand square is reached,
the next integer is placed in the square immediately below the last square which was filled.
The magic square of order 3 in (1) was constructed using the de la Loubère’s method, as was this magic
square of order 5:
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22 (3)
10 12 19 21 3
11 18 25 2 9
Your task is to write a program that constructs a magic square of odd order up to 255 using the de la
Loubère’s method described above.
10
7.2 Output Format
The output will consist of one magic square for each positive integer in the input. Each magic square must
be separated by a blank line. The output must be formatted as shown in the example below:
• The numbers are outputted in columns of the same width.
5 17 24 1 8 15
6 23 5 7 14 16
7 4 6 13 20 22
8 10 12 19 21 3
9 11 18 25 2 9
10
11 30 39 48 1 10 19 28
12 38 47 7 9 18 27 29
13 46 6 8 17 26 35 37
14 5 14 16 25 34 36 45
15 13 15 24 33 42 44 4
16 21 23 32 41 43 3 12
17 22 31 40 49 2 11 20
18
11
8 Move the Water
Source file name: water.c, [Link] or [Link]
You have three jars containing water. Each jar has associated a capacity indicating the maximum amount
of water it can contain. Your wish is to have a certain amount of water in each jar. The initial content of
each jar, its capacity, and the desired content, are all non negative integers; in addition, the capacities are
between 1 and 150. You have nothing but the jars to help you in achieving your goal. So the only way to
obtain the desired content in each jar is to make a sequence of movements of water. In a movement of water
you pour a certain amount of water from one jar to another jar. We call the first jar source and the second
jar destination. While moving water two situations can occur: If the current content of the source jar is not
enough to complete the capacity of the destination jar, then all the water contained in the source jar is moved
to the destination jar; otherwise, only the exact amount of water needed to complete the destination jar is
moved from the source jar. Under no circumstances you can use additional water or throw away water. Your
task is to determine the minimum number of movements required to obtain the desired amount of water in
each jar.
12
9 The Red Baron
Source file name: redbaron.c, [Link] or [Link]
Well, this time, is a little hard for you. . . One of your friends nickname is ‘speedracer’, known also as ‘Red
Baron’, because he seems to be flying in his white car. Your fast and furious friend knows that you are an
excellent programmer, and he needs that you do some software for him.
Lately the competence has turned into the streets, it means that you have tons of semaphores and the rules
say that you must respect them.
He measured up all the semaphores lights in the different tracks on the city, and he was wondering if it is
possible that you design a program that let him know at which constant speed he should drive to pass free
through all the semaphores without being stopped.
At a specific time in the night, the staff knows that all the semaphores in the track get synchronized, and
begin in red light at the same time, so that’s the moment when the race begins.
You must consider that he can pass free with yellow and green lights; his plane, oops, his car, takes 11
seconds and 700 meters to reach 300 Km/h (maximum speed of the car without being broken), also you must
consider that at the moment he accelerates by the first time the semaphore is beginning red. In case he got
to stop, he prefers not to run that track. Could you be able to calculate the maximum constant speed he
should drive?
13