X Copa UCI de Programación "Tomás López Jiménez"

X Copa UCI de Programación "Tomás López Jiménez" - March 28, 2015
The 6th ACM-ICPC Caribbean Training Camp at the UCI, Cuba
Document composed by 15 pages (including this cover)
ProblemSetters:
Lic. Fidel Iván Schaposnik Massolo, Professor
Universidad Nacional de La Plata (UNLP), Argentina
Yonny Mondelo Hernández, Professor
José Carlos González Fernández, Professor
Yaniel Alfredo Velázquez Bruceta, Professor
Yosvany Leyva Pizarroza, Professor
Nelson González Peñate, 5th Year Student
Eddy Roberto Morales Pérez, 5th Year Student
José Luis Castrillón Garrido, 5th Year Student
Universidad de las Ciencias Informáticas (UCI), Cuba
Frank Arteaga Salgado
Universidad de las Tunas (ULT), Cuba
Luis Manuel Díaz Barón, Professor
Universidad de Pinar del Río (UPR), Cuba
Jorge Enrique Moreira Broche, Professor
Universidad Central "Marta Abreu" de Las Villas (UCLV), Cuba
Problem A – Adding Up
Description
You are given a integer number N, you must calculate in how many ways can K non-negative
numbers, no greater than N, be added up to sum N.
Input
The first line contain a integer number 1 <= T <= 10000 representing the number of test cases.
The following T lines contain a pair of integer numbers: N and K respectively, 1 <= N, K, <=
250.
Output
For each test case you must print the solution for the given values of N and K, modulated by
1000000007.
Sample Input
6
1
3
2
3
5
5
1
2
3
3
5
1
Sample Output
1
4
6
10
126
1
2
Problem B – BigInteger Problem
Description
Mr. Reni favorite's language is C++, however he feels a bit frustrated because this language has
not implemented natively operations with large integers as Python or Java. He already solved
in Python, their second preferred language, the following problem:
Given integers K and D (2 <= K <= 10, 1 <= D <= 10^9) determine the lowest power of K
having D or more digits in his decimal representation. Since to store the answer you may need
up to 1Gb of memory he just wants the exponent of this power.
In addition, Mr. Reni notes that his algorithm in Python is very inefficient because it consumes
a lot of memory for large values of D. So ask you to resolve the same problem in C++ or any
other.
Input
In the first line appear integers K and D.
Output
Print the smallest exponent E > 0 such that K^E has D or more digits in his decimal
representation.
Sample Input
2 4
Sample Output
10
Hint(s)
2^9 = 512 and 2^10 = 1024.
3
Problem C – Containers at the Harbor
Description
After the last download from a merchant ship there are N containers along a line in harbor,
containers are conveniently numbered between 1 and N being the first container the number 1
and the last the number N. Also, there are K cranes capable of raising the containers to the
trucks in order to move them to the warehouses. Each crane i (1<=i<=K) spends Tij seconds
for processing the container j (1<=j<=N), raising the container to some truck. For security
reasons each crane just can be used to process one consecutive sequence of containers l, r
where 1<=l<=r<=N, and no more than one crane can be operated at same time.
Note that after raising the containers l-th to r-th, the container lth-1 and the rth+1 are not
consecutive.
The workers of the harbor are interested in knowing what is the minimum time for processing
all the containers.
Input
The first line contains N (1 <= N <= 20) and K (1 <= K <= 10). Next follow K lines with N
integers for each line. The j-th number of the i-th line, 0 <= T[i][j] <= 100, is how much time
the i-th crane use to process the j-th container.
Output
Just one line, the minimal time for raising to the trucks the N containers.
Sample Input
3 2
1 4 1
2 2 2
Sample Output
5
Hint(s)
One possibility is assign to the first crane the container 1, and assigns to the other crane the
containers 2, 3. That will have a time cost of 1 + (2 + 2) = 5.
4
Problem D – Divide or Conquer
Description
You are playing a new game over a list of positive integers, on each turn you are allowed:
 to pick any two equal numbers and to replace them with their sum.
 or to pick a single even number X and to replace it with two numbers both equal to X / 2.
Find the maximal integer you can get via the arbitrary number of the above-mentioned
operations.
Input
The first line of the input contains an integer T (1 <= T <= 10) denoting the number of games.
The next T lines, one for each game, contain a list of positive integers separated by a space.
The length of each list will be at most 10^5 and each element on the list will be a positive
integer not greater than 10^9. Each list begins with a integer number representing the amount
of elements in the subsequent list.
Output
For each game find the maximal integer you can get via the arbitrary number of the abovementioned operations.
Sample Input
4
6
3
3
6
2
1
1
2
4
1
6
4
8 1 1 15
1
12
8 1 3 15
Sample Output
16
2
12
15
5
Problem E – Penalty Calculation
Description
In the forthcoming UCI Cup, COJ Development Team fears the possibility of the main
structure of the site to go down. Therefore they have assigned many young programmers (you
among them) to code backup modules, so if anything goes wrong during the contest, the
competitors don’t get upset. The task assigned to you is to calculate the penalty for a specific
problem from a team.
You will be given 1 <= N <= 100, the number of submissions and for each one you will also
know the minute 1 <= Mi <= 300 it was submitted and the judge verdict by its identifier which
can be one of these:
Verdict - Identifier
 Wrong Answer - WA
 Time Limit Exceeded – TLE
 Runtime Error – RTE
 Compilation Error – CE
 Accepted – AC
If a problem is Accepted the penalty raises to the amount of minutes it took to accept and for
each submission before the first Accepted one, the penalty raises by 20 points. If the problem is
not Accepted, the penalty is zero.
Input
In the first line of the input a single integer N (1 <= N <= 100) representing the number of
submissions. The next N lines describe a single submission: with a integer Mi (1 <= Mi <= 300)
representing the minute of the submission and a string V representing the verdict, both values
are space separated. The value of V is one of these: WA, TLE, RTE, CE or AC. You can safely
assume that all Mi are unique.
Output
The total penalty the team will have after all submissions are processed.
Sample Input
4
40
20
30
41
RTE
WA
WA
AC
Sample Output
101
6
Problem F – Shooting Practice
Description
A group of M soldiers is performing the shooting practice corresponding for the month. There
are N targets in the field and the soldiers are placed in different positions on a straight line. The
soldiers have a software that lets them know the trajectory of each shot fired, defining it with
two points in the 2D plane, but the software does not give them all the information they need.
For now they are only interested in you to develop a new functionality; they desire to know if
the projectile goes through at least one target or passes between any two of them. You should
know that a soldier never shoots backwards from the line of fire, because this may create a
slightly unpleasant situation for the chiefs that are behind. The maximum number of targets
that are aligned is always lower than N. Furthermore, there are no two targets placed in the
same location and there are no targets behind the line of fire. Are you ready to solve this little
problem? I hope so!
Input
The first line of input contains two integers 5 <= N <= 10^5, 1 <= M <= 10^5. In each of the
following N lines, two integers Xi, Yi representing the position of the i-th target on the field.
Then M lines follow, each with four integers Xaj, Yaj, Xbj, Ybj, specifying the position of j-th
soldier with the coordinates (Xaj,Yaj) and the coordinates (Xbj,Ybj) for defining the trajectory,
e.g. the projectile is fired from (Xaj,Yaj) and is directed in the direction of (Xbj,Ybj),
continuing its trajectory once it passes through this point until goes through the shooting range
completely without changing its direction. In each line of the input the numbers are separated
by a single space. (-10^7 <= Xi, Yi, Xaj, Yaj, Xbj, Ybj <= 10^7).
Output
The output consists of M lines. In the j-th line will be given the information needed by the j-th
soldier. If the projectile fired by the j-th soldier breaks down at least one objective or passes
between any two, you must outputting "YES"; otherwise, you must to show "NO", both
without quotes.
Sample Input
6 3
0 7
-2 6
0 5
2 5
-2 4
-1 3
-2 1 2 5
1 1 -1 4
4 1 7 3
Sample Output
YES
YES
NO
7
Problem G – Divisor Summation
Description
Given a natural number N (1 < N <= 5*10^4), please find the summation of all divisors of the
factorial of the number N, that are strictly less than N!. Since the answer can be very large print
it modulated by 1000000007.
Input
The first line contains T <= 500, number of test cases. T lines follow each containing the
number N.
Output
Print T lines of output each containing the answer.
Sample Input
3
2
3
4
Sample Output
1
6
36
Hint(s)
Factorial(4) = 24, the number 24 has 6 divisors: 1, 2, 3, 4, 6, 8, 12, and the divisor
summation is: 1 + 2 + 3+ 4 + 6 + 8 + 12 = 36.
8
Problem H – Seven Women per Man
Description
A couple of days before the UCI programming cup, the Caribbean Training Camp were being
held. In this event N male programmers participated as contestants. One of the collateral
activities of the event was the Big Couple of Programmers Party (ICPC in some strange
language). In this wonderful university there are M female programmers, in order to participate
in the party each male programmer should go accompanied by exactly one female programmer
and vice versa. Male programmer i has a prestige value Pi and a subset Mi of the M female
programmers. The value Pi indicates the prestige acquired in the several contests male
programmer i has gotten a good result and the subset Mi indicates all the programmer women
that are willing to go to the party with male programmer i. It is known as a matter of fact that
each male programmer will have at most seven female programmers willing to go with him
from the M female programmers in the university.
The directors of the training camp have defined the prestige of the party as the multiplication of
the prestige of all the male programmers going to the party. The directors of the training camp
are asking you today to compute the maximum prestige of the party, they know this number
may be huge, that's why they have changed your task, compute the number of digits of the
maximum possible prestige of the party.
Input
The first line of input contains two integer N and M, 1 <= N, M <= 5000 indicating the number
of male programmers and female programmers respectively. The following 2*N lines contain
the information for each male programmer. This information comes in two separate lines in the
following way:
 For programmer i the first line contains two integers 2 <= Pi <= 10^9 and 0 <= Mi <= 7
indicating the prestige of male programmer i and the number of female programmers
that are willing to accompany him to the party. The second line contains Mi female
programmers, 1 <= Mij <= M indicates a female programmer that is willing to go with
male programmer i.
Output
Output an integer value indicating the amount of digits of the maximum prestige the party may
have.
9
Problem H – Seven Women per Man
Sample Input
3
2
1
3
2
4
1
3
1
1
2
2
Sample Output
2
Hint(s)
One of the possible scenarios for having the best possible prestige of the party is when:
 male programmer 2 goes with female programmer 2
 male programmer 3 goes with female programmer 1
 male programmer 1 doesn't go to the party because the only female programmer willing
to go with him to the party is female programmer 1 and she is going with male
programmer 3.
The prestige of the party is equal then to 3*4 = 12, 12 have 2 digits and this is the answer to the
test case.
10
Problem I – The Queue
Description
The zebra of Madagascar (the movie), we all know as Marty, is in queue to buy ice cream.
Each ice cream costs 50 cents but in the queue some of the characters in the zoo have a 1 dollar
coin and the remaining have a 50 cents coin. Each people wants to buy an ice cream and
nobody want the queue stopped by lack of change. The queue never stops when someone goes
to buy an ice cream with a 50 cents coin. The queue stops when someone with a 1 dollar coin
comes to buy in a time when the number of those who have purchased with coin of 50 cents
plus the amount of 50 cents coin that have the seller does not exceed those who have bought
with 1 dollar coins. This means that if S people have bought with 50 cents coins, then there
may be attended a maximum of S people who come with 1 dollar coins plus the amount of 50
cents coins that have the seller before starting the sell.
Marty wants to organize the queue, but as Marty is very distracted he wants to know in how
many ways he can organize the queue so that never it stops by lack of change. For Marty the
order between the people that have the same kind of coins is not important. I.e. if A and B have
50 cents coins and C has a 1 dollar coin, for Marty ABC and BAC is the same, but not CAB or
ACB.
Input
The only line of input contains three integers S, T and Q (1 <= S, T <= 1000; 0 < Q <= 1000),
the number of people with a 50 cents coin, the number of people with an 1 dollar coin and the
amount of 50 cents coins that have the seller before the beginning of the sell.
Output
Print a line with the amount of ways that he can organize the queue so that it never stops by
lack of change mod 10^9+7 (1000000007).
Sample Input
2 1 0
Sample Output
2
Hint(s)
In the sample, the answer is 1 because there are only one way of organize the queue with those
restrictions. First the people with 50 cents coin and second the people with 1 dollar coin. If the
first is who has 1 dollar coin, then there are lack of change and stops the queue.
11
Problem J – Operation Peanut Butter
Description
Anders the cat is always trying to catch Peter the mouse. They both live in the same
neighborhood, which contains M streets that form N intersections, each street has a distance in
meters (1 <= Di <= 10000), and the mouse's speed is 1 meter per second. Anders knows the
location from which Peter starts running S, and also where he ends E. As mice always pick the
shortest route in distance, which is unique, and Anders is a fat, always-drunk, lazy cat, he needs
some help in order to catch his prey.
The cat can spread peanut butter on the streets, which delays Peter while running through it, if
a street is covered with peanut butter, Peter's speed will be 0.5 meters per second. Anders has
enough peanut butter to cover P meters, but he can only cover full streets, it means he can use
the peanut butter from intersection to intersection.
Help Anders to know how much peanut butter he can spend, and how much time will last the
Peter’s run.
Input
In the first line an integer 1 <= T <= 50 denoting the amount of cases. In the second line three
space-separated integers N (1 <= N <= 1000), M (1 <= M <= 10000) and P (1 <= P <= 1000).
The next M lines contain 3 space-separated integers A, B and D (1 <= A <= N, 1 <= B <= N
and 1 <= D <= 100000), describing a street, meaning that exits a street from intersection A to
intersection B with a length of D meters. The last line contains two space separated integers S
and E (1 <= S != E <= N).
Output
For each case print a single line containing two space separated integers denoting the amount
of peanut butter spent by Anders and the time it will take for Peter to get his final location.
Sample Input
1
6
1
1
5
4
2
3
1
1
7
2
4
1
5
3
4
6
5
9
2
1
9
7
3
5
7
Sample Output
8 16
12
Problem K – Fountain
Description
If you ever visited Havana, you should take some time to visit the Light Fountain. It´s a
beautifull place at nights, and perhaps you are one of the fortunate who gets your wish by
throwing a coin into the fountain water. In the World there is several water fountains which use
a system of vessels arranged one above the other as shown in the figures below:
[See online version for pictures]…
For purposes of this problem we will use a similar fountain. You can assume that vessels are
numbered from 1 to n, in the order from the highest to the lowest, and each of them can hold an
amount of water measured in liters. When you spill some liters of water in a vessel i all the
water that overflows from it goes to the vessel i+1, and so on. All the water that overflows
from n-th vessel spills on the floor. All the vessels are empty at the beginning. And we must
point that capacities of the vessels do not necessarily increase from the top vessels to the
bottom ones; it can remain the same or even decrease.
Your task is simulating pouring water into the given vessels, for which two possible actions
can be performed: pouring an amount of liter of water inside a vessel, or query for the amount
of liter of water inside a vessel. When you query a vessel you can assume that all the water
poured up to this point, has already overflown between the vessels.
Input
The first line contain a integer number 1 <= N <= 10^5: the amount of vessels which conform
the fountain. The second line contain N space-separated integer numbers between 1 and 10^9
with the capacities of each vessel from the highest to the lowest vessel. Third line contain a
integer number 1 <= Q <= 10^5: the amount of actions to be performed. Each of the next Q
lines contains the description of one action. Query actions are conformed by two spaceseparated integer numbers: they always start with a number one, followed by the index of the
vessel to be queried. And pouring actions are conformed by three space-separated integer
numbers: they always start with a number two, followed by the index of the vessel in which the
water should be spilled, and finally the amount of liters of water to be poured (integer between
1 and 10^9).
Output
For each query action you must print a single line with the amount of liter of water inside the
queried vessel.
13
Problem K – Fountain
Sample Input
10
45 23 56 7 1 23 99 54 967 3
12
2 3 69
1 4
2 8 71
1 9
2 5 73
1 7
2 4 79
1 9
2 4 915
1 10
2 3 1
1 10
Sample Output
7
17
55
52
0
1
14
Problem L – The 8th Task of Asterix and Obelix
Description
Julius Caesar has given the Gauls 12 tasks. Their best warriors, Asterix and Obelix, were
chosen to fulfill these tasks. So far everything is going well, but as they approach the 8th task
they realize they will need help. The task is simple, just find permit A 38 at "The Place That
Sends You Mad". The problem is that the staff at "The Place That Sends You Mad" is very
unhelpful and will try to finish your patience and lucidity before you get the permit.
Asterix and Obelix have some inside info. At the "The Place That Sends You Mad" are N
functionaries numbered 1 to N, all of which can give you permit A 38, but they won´t give it up
so easily. Each functionary has a list of K other functionaries, every time someone asks a
functionary for permit A 38 they will look up on their list of colleagues and send the asking
person to one they have not sent him yet. If the functionary has already sent the asking person
to each of the functionaries in his list then he has no choice but to give up permit A 38. So the
asking person may end up asking each functionary for permit A 38 several times and still see
no success. It is also known that the distribution of the functionaries in the lists is such that for
every functionary F there are exactly other K functionaries that have F on their list. Asterix and
Obelix start by asking functionary 1 for permit A 38, after that they must only ask the
functionary they have been sent to. Asterix wants to know which is the maximum number of
times they need to ask for permit A 38 before they get it. Can you help him?
Input
Input consists of a single test case starting with numbers N and K in a line (1 <= K < N <= 300).
Then K lines follow, line i from 2..N+1 contains K different integers from 1 to N separated by
spaces meaning the list for functionary i.
Output
First line of output should contain number M, the maximum number of times Asterix and
Obelix may need to ask for permit A 38. Second line of output should containing M numbers
from 1 to N, showing a possible order in which they ask the functionaries for the permit, if
there is more than one such ordering output the lexicographically least.
Sample Input
5
2
5
1
2
3
2
5
3
4
1
4
Sample Output
11
1 2 3 1 5 3 4 2 5 4 1
15