Download Peru ACM ICPC Training Camp - IME-USP

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Line (geometry) wikipedia , lookup

Addition wikipedia , lookup

Halting problem wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Elementary mathematics wikipedia , lookup

Transcript
Peru ACM ICPC Training Camp
Day 4
by Luís Fernando Schultz Xavier da Silveira
2010-08-13
Problems
A
B
C
D
E
F
G
H
I
J
K
Krillin
Numbers
Triangular Sums
Shooting Balloons
Two Ends
Almost Shortest Path
Dinner Problem
Last Digits
Wireless Coverage
A Multiplication Game
SAM I AM
1
Krillin
Treino para a Maratona de Programao – UFSC
Luı́s Fernando Schultz Xavier da Silveira
Statement
Krillin is Goku’s best friend, but his powers are very different from Goku’s. Being an earthling, Krillin posseses a fairly inferior
strength to that of the sayan Goku, but that does not mean Krillin is weak, not at all.
However, as time passed, the ratio of his power to that of his friends’ went near zero. From decisive factor in various battles,
Krillin became an irrelevant element in the battles against the evil. Inclusively, due to the evil nature of the enemies of Goku
and his friends, they tend to attack the weakest first, and that forced Krillin to invest in his only useful technique left: escaping.
In this problem, you will help Krillin escape from a stronger adversary. Consider the following n × n semi-grid:
Initially, Krillin is close together to his adversary and must escape moving always forward and only in the directions that
make a 45 degree angle to the floor. He must also always stay on a grid line and, consequently, always inside the grid or on its
border (the darker line). Notice Krillin must always stay on the floor or above it.
Your goal will be to tell Krillin how many escape routes he has.
Input
The input contains several test cases, each of which contains a single integer number n, the size of the grid. You may assume
that 0 < n 6 109 .
Output
For each test case, print to the output a single integer in a line by itself: the number of escape routes Krillin can take modulo
3571.
Sample Input
1
2
Sample Output
1
2
Numbers
Google Code Jam 2008 – Round 1A – Problem C
modified by Luı́s Fernando Schultz Xavier da Silveira
Statement
In this problem, you have to find the
three digits before the decimal point for the number (3 +
√ last
5
5)
=
3935.73982 . . . . The answer is 935.
For example, when
n
=
5,
(3
+
√
For n = 2, (3 + 5)2 = 27.4164079 . . . . The answer is 027.
√
5)n .
Input
The first line of input gives the number of cases, T , 1 6 T 6 100. T test cases follow, each on a separate line. Each test case
contains one positive integer n, 2 6 n 6 2000000000.
Output
For each input case, you should
output “Case #X: Y ” where X is the number of the test case and Y is the last three integer
√
digits of the number (3 + 5)n . In case that number has fewer than three integer digits, add leading zeros so that your output
contains exactly three digits.
Sample Input
2
5
2
Sample Output
Case #1: 935
Case #2: 027
B • Triangular Sums
The nth Triangular number, T(n) = 1 + ... + n, is the sum of the first n integers. It is the
number of points in a triangular array with n points on side. For example T(4):
X
X X
X X X
X X X X
Write a program to compute the weighted sum of triangular numbers:
W(n) = SUM[k = 1..n; k*T(k+1)]
Input
The first line of input contains a single integer N, (1 ≤ N ≤ 1000) which is the number of datasets that
follow.
Each dataset consists of a single line of input containing a single integer n, (1 ≤ n ≤300), which is the
number of points on a side of the triangle.
Output
For each dataset, output on a single line the dataset number, (1 through N), a blank, the value of n for
the dataset, a blank, and the weighted sum , W(n), of triangular numbers for n.
Sample Input
4
3
4
5
10
Greater New York Regional
Sample Output
1 3 45
2 4 105
3 5 210
4 10 2145
B • Triangular Sums
Shooting Balloons
Peru ACM ICPC Training Camp – 2010
by Luís Fernando Schultz Xavier da Silveira
Statement
Well, many teams which prepare to the ACM ICPC competition dream about winning and
bringing home the cup they can only touch at the event. But some teams also dream, or actually, have nightmares, about losing the competition by a vast amount.
Billy is a pessimistic contestant and is having constant such nightmares. He decided he must
prepare for a losing situation in ICPC. Billy decided to bring a gun to the contest site. If his
team scores no problem during the first four hours of the contest, he will ask to go to the bathroom and will position himself so as to shoot and blow off the largest amount of other team’s
balloons he can. Of course, once he shoots, the martials will, with their sticks, quickly escort
Billy out of the contest site. He will also probably be disclassified and receive not even a honorable mention.
Your task is to write a program that, given the position of the balloons in the contest room,
returns the largest amount of balloons that can be blown with a single shot. Billy then will
memorize your program so he can reproduce it quickly in the day of the contest.
Input
Input consists of several test cases. Each test case starts with a line containing a single integer
number n, 0 6 n 6 1000, the number of balloons in the room. The next n lines contain each
three integer numbers x, y and z, − 109 6 x, y, z 6 109, stating there is a balloon in the room in
position (x, y, z) where (x, y) is the position of the projection of the balloon to the floor and z is
its height (starting from your desk’s height).
Negative height values mean the balloon has not been filled with helium but instead with air.
You may assume that the balloons are points in the space and so is the bullet, i.e., a shot will
hit a balloon if, and only if, the bullet goes exactly through the balloon. You may also assume
the bullet trajectory is that of a semi-line.
Output
For each test case, output a single line containing a single integer number: the maximum
amount of balloons Billy can blow up with a single shot.
Sample Input Sample Output
2
2
1 2 0
1 0 1
1
2005 East Central Regional Contest
Problem H:
13
Two Ends
In the two-player game “Two Ends”, an even number of cards is laid out in a row. On each card, face
up, is written a positive integer. Players take turns removing a card from either end of the row and
placing the card in their pile. The player whose cards add up to the highest number wins the game.
Now one strategy is to simply pick the card at the end that is the largest — we’ll call this the greedy
strategy. However, this is not always optimal, as the following example shows: (The first player would
win if she would first pick the 3 instead of the 4.)
3 2 10 4
You are to determine exactly how bad the greedy strategy is for different games when the second player
uses it but the first player is free to use any strategy she wishes.
Input
There will be multiple test cases. Each test case will be contained on one line. Each line will start with
an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may
assume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers in
the list does not exceed 1,000,000.
Output
For each test case you should print one line of output of the form:
In game m, the greedy strategy might lose by as many as p points.
where m is the number of the game (starting at game 1) and p is the maximum possible difference
between the first player’s score and second player’s score when the second player uses the greedy strategy.
When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end.
Sample Input
4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0
Sample Output
In game 1, the greedy strategy might lose by as many as 7 points.
In game 2, the greedy strategy might lose by as many as 4 points.
In game 3, the greedy strategy might lose by as many as 5 points.
ACM ICPC2008 – South American Regionals
1
Problem A
Almost Shortest Path
Source file name: almost.c, almost.cpp or almost.java
Finding the shortest path that goes from a starting point to a destination point given a set of
points and route lengths connecting them is an already well known problem, and it’s even part
of our daily lives, as shortest path programs are widely available nowadays.
Most people usually like very much these applications as they make their lives easier. Well,
maybe not that much easier.
Now that almost everyone can have access to GPS navigation devices able to calculate shortest
paths, most routes that form the shortest path are getting slower because of heavy traffic. As
most people try to follow the same path, it’s not worth it anymore to follow these directions.
With this in his mind, your boss asks you to develop a new application that only he will have
access to, thus saving him time whenever he has a meeting or any urgent event. He asks you
that the program must answer not the shortest path, but the almost shortest path. He defines
the almost shortest path as the shortest path that goes from a starting point to a destination
point such that no route between two consecutive points belongs to any shortest path from the
starting point to the destination.
For example, suppose the figure below represents the map given, with circles representing
location points, and lines representing direct, one-way routes with lengths indicated. The
starting point is marked as S and the destination point is marked as D. The bold lines belong
to a shortest path (in this case there are two shortest paths, each with total length 4). Thus,
the almost shortest path would be the one indicated by dashed lines (total length 5), as no
route between two consecutive points belongs to any shortest path. Notice that there could
exist more than one possible answer, for instance if the route with length 3 had length 1. There
could exist no possible answer as well.
2
S
1
1
1
4
2
2
3
D
4
Input
The input contains several test cases. The first line of a test case contains two integers N
(2 ≤ N ≤ 500) and M (1 ≤ M ≤ 104 ), separated by a single space, indicating respectively
the number of points in the map and the number of existing one-way routes connecting two
points directly. Each point is identified by an integer between 0 and N − 1. The second line
contains two integers S and D, separated by a single space, indicating respectively the starting
and the destination points (S 6= D; 0 ≤ S, D < N ). Each one of the following M lines contains
three integers U , V and P (U 6= V ; 0 ≤ U, V < N ; 1 ≤ P ≤ 103 ), separated by single spaces,
indicating the existence of a one-way route from U to V with distance P . There is at most one
ACM ICPC2008 – South American Regionals
2
route from a given point U to a given point V , but notice that the existence of a route from
U to V does not imply there is a route from V to U , and, if such road exists, it can have a
different length. The end of input is indicated by a line containing only two zeros separated by
a single space.
The input must be read from standard input.
Output
For each test case in the input, your program must print a single line, containing -1 if it is not
possible to match the requirements, or an integer representing the length of the almost shortest
path found.
The output must be written to standard output.
Sample input
Output for the sample input
7
0
0
0
0
0
1
2
3
4
5
4
0
0
1
1
3
2
3
6
0
0
0
0
2
3
4
5
3
0
5
-1
6
9
6
1
2
3
4
5
6
6
6
6
6
2
1
2
3
2
0
0
8
1
1
2
3
5
4
1
1
0
0
1
1
2
3
2
4
2
4
1
1
1
1
1
3
2
1
2
3
3
2
1
1
1
Dinner Problem
São Paulo Training Camp 2010
Andrew Stankevich Contest 28, Tuesday, January 19, 2009
by Petr Mitrichev
modified by Luı́s Fernando Schultz Xavier da Silveira
Statement
A group of k students from Cooking University living in the campus decided that each day of the semester one of them will
prepare the dinner for the whole company. The semester lasts for n days. In sake of fairness they decided that each of the
students must prepare the dinner at least once during the semester. Now they wonder how many ways are there to plan the
semester – to decide for each day which student would make a dinner that day. Help them to find that out.
Input
The input file contains several pairs of integer numbers k and n (1 6 k 6 n 6 300).
Output
For each pair (k, n) in the input, output one integer – the number of ways modulo 65537.
Sample Input
23
Sample Output
6
Note
In the example above, there are six ways: (1, 1, 2), (1, 2, 1), (2, 1, 1), (1, 2, 2), (2, 1, 2) and (2, 2, 1).
Last Digits
ACM ICPC – North America – Rocky Mountain – 2005/2006
modified by Luı́s Fernando Schultz Xavier da Silveira
Statement
Exponentiation of one integer by another often produces very large results. In this problem, we will compute a function based
on repeated exponentiation, but output only the last n digits of the result. Doing this efficiently requires careful thought about
how to avoid computing the full answer.
Given integers b, n, and i, we define the function f (x) recursively by f (x) = bf (x−1) if x > 0, and f (0) = 1. Your job is to
efficiently compute the last n decimal digits of f (i).
Input
The input consists of a number of test cases. Each test case starts with the integer b (1 6 b 6 100) called the base. On the next
line is the integer i (1 6 i 6 100) called the iteration count. And finally, the last line contains the number n (1 6 n 6 7), which
is the number of decimal digits to output. The input is terminated when b = 0.
Output
For each test case, print on one line the last n digits of f (i) for the base b specified. If the result has fewer than n digits, pad
the result with zeroes on the left so that there are exactly n digits.
Sample Input
2
4
7
10
10
6
3
10
7
0
Sample Output
0065536
000000
4195387
Wireless Coverage∗
Peru ACM ICPC Training Camp – 2010
Joel Uchoa
Wanderley Guimarães
Luís Fernando Schultz Xavier da Silveira
Statement
Ivan Pontual Costa Carvalho is a famous brazilian hacker. This week, Ivan is presenting a work
in the city of Manhattan. It is widely known that most, if not all, authentication protocols for
wireless networks implemented in the industry and distributed in the market suffer from grave
flaws which could give a sufficiently smart and determined attacker unauthorized access. The
aircrack-ng is an excellent software suite which gives novice and unknowlegeable users the
hability to extract keys (and thus break into) WEP (Wired Equivalent Privacy) networks. Until
very recently it was common knowledge that WPA (Wi-Fi Protected Access) would be a secure
alternative to WEP, but many flaws have been found in such system, and Ivan’s work is exactly
about one of them.
Needless to say, Ivan can break into most wireless networks in the city of Manhattan. Not only
that, he has many (infinitely many for the purposes of this problem) wireless cards, and since he
uses the very flexible operating system Linux, he can use them simultaneously to connect to
multiple access points at once. Being an addict to the Dragon Ball series, Ivan wants to download all the new episodes of the new Dragon Ball anime, Dragon Ball Kai. If he can stay at a
location within the reach of several wireless access points, he can increase his download speed by
using all of them simultaneously.
Manhattan is an m × m grid-like city, with each block in the city being identified by a pair (i, j)
of integers with 1 6 i, j 6 m. An access point in the block (i, j) covers all blocks of the form (i ′,
j ′) such that |i ′ − i| + |j ′ − j | 6 k for a given constant k, the power of the access point.
Ivan quickly opened his laptop and wrote a computer program which computed a spot in Manhattan whose coverage by wireless access points is maximum. Of course, his program needed
urban data about Manhattan, but breaking into the american government database was too
easy for Ivan to even think about it. He then moved to one such spot and admired his download
speed.
You are curious about the download speed Ivan has achieved, so you downloaded the urban data
Ivan had used, for he has published it anonimously in the internet. Your only task now is
writing a program to compute what is the number of wireless access points covering one of the
most covered block in Manhattan.
Input
Input consists of several test cases. Each test case begins with a line containing two integer
numbers m, the size of the grid (1 6 m 6 109), and n, the number of wireless access points in
Manhattan (0 6 n 6 105). Then n lines follow, each describing an access point and containing
three integers i, j and k. The access point is located in block (i, j), 1 6 i, j 6 m, and its power is
k, 0 6 k 6 109.
∗. Problem statement by Luís Fernando Schultz Xavier da Silveira with inspiration of the statement of the
problem “Acesso”, by Ricardo Anido, of the selection of brazilian teams for the Internation Olympiads in Informatics (IOI) 2010.
1
Output
For each test case, print one integer in a line by itself: the number of wireless access points covering a block in Manhattan covered by a maximum amount of wireless access points.
Sample Input Sample Output
5 2
2
1 1 2
3 3 2
2
A Multiplication Game
UVa Online Judge – 847
modified by Luı́s Fernando Schultz Xavier da Silveira
Statement
Stan and Ollie play the game of multiplication by multiplying an integer p by one of the numbers 2 to 9. Stan always starts
with p = 1, does his multiplication, then Ollie multiplies the number, then Stan and so on. Before a game starts, they draw an
integer 1 < n < 4294967295 and the winner is who first reaches p > n.
Input and Output
Each line of input contains one integer number n. For each line of input output one line either
Stan wins.
or
Ollie wins.
assuming that both of them play perfectly.
Sample Input
162
17
34012226
Sample Output
Stan wins.
Ollie wins.
Stan wins.
Problem C
SAM I AM
Input: Standard Input
Output: Standard Output
The world is in great danger!! Mental's forces have returned to Earth to eradicate humankind.
Our last hope to stop this great evil is Sam “Serious” Stone. Equipped with various powerful
weapons, Serious Sam starts his mission to destroy the forces of evil.
After fighting two days and three nights, Sam is now in front of the temple KOPTOS where
Mental's general Ugh Zan III is waiting for him. But this time, he has a serious problem. He is in
shortage of ammo and a lot of enemies crawling inside the temple waiting for him. After
rounding the temple Sam finds that the temple is in rectangle shape and he has the locations of
all enemies in the temple.
All of a sudden he realizes that he can kill the enemies
without entering the temple using the great cannon ball
which spits out a gigantic ball bigger than him killing
anything it runs into and keeps on rolling until it finally
explodes. But the cannonball can only shoot horizontally or
vertically and all the enemies along the path of that cannon
ball will be killed.
Now he wants to save as many cannon balls as possible for
fighting with Mental. So, he wants to know the minimum
number of cannon balls and the positions from which he
can shoot the cannonballs to eliminate all enemies from
outside that temple.
Input
The input file contains several test cases. The first line of each test case contains 3 integers:
length L(0<L<1001), width W(0<W<1001) of the temple and the number of enemies
N(0<N<1000001) inside the temple. After that there are N lines each of which contains 2
integers representing the position of the enemies in that temple. Each test case is followed by a
new line (except the last one). Input is terminated when L=W=N=0. The size of the input file is
around 1.3 MB.
Output
For each test case there will be one line output. First print the minimum number (m) of
cannonballs needed to wipe out the enemies followed by a single space and then m positions
from which he can shoot those cannonballs. For shooting horizontally print “r” followed by the
row number and for vertical shooting print “c” followed by the column number. If there is more
than one solution any one will do.
Sample Input
4 4 3
1 1
Output for Sample Input
2 r1 r3
2 r1 r2
1 4
3 2
4 4 2
1 1
2 2
0 0 0
Problemsetter: Syed Monowar Hossain
Special Thanks: Derek Kisman