Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training...

15
Peru ACM ICPC Training Camp Day 4 by Luís Fernando Schultz Xavier da Silveira 2010-08-13 Problems A Krillin B Numbers C Triangular Sums D Shooting Balloons E Two Ends F Almost Shortest Path G Dinner Problem H Last Digits I Wireless Coverage J A Multiplication Game K SAM I AM 1

Transcript of Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training...

Page 1: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

Peru ACM ICPC Training Camp

Day 4

by Luís Fernando Schultz Xavier da Silveira

2010-08-13

Problems

A Krillin

B Numbers

C Triangular Sums

D Shooting Balloons

E Two Ends

F Almost Shortest Path

G Dinner Problem

H Last Digits

I Wireless Coverage

J A Multiplication Game

K SAM I AM

1

Page 2: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

KrillinTreino 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 inferiorstrength 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 Gokuand 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 thatmake a 45 degree angle to the floor. He must also always stay on a grid line and, consequently, always inside the grid or on itsborder (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 assumethat 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 modulo3571.

Sample Input Sample Output12

12

1

Page 3: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

NumbersGoogle 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 last three digits before the decimal point for the number (3 +√

5)n.For example, when n = 5, (3 +

√5)5 = 3935.73982 . . . . The answer is 935.

For n = 2, (3 +√

5)2 = 27.4164079 . . . . The answer is 027.

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 casecontains 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 integerdigits 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 Sample Output252

Case #1: 935Case #2: 027

1

Page 4: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

Greater New York Regional B • Triangular Sums

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 Sample Output 4 3 4 5 10

1 3 45 2 4 105 3 5 210 4 10 2145

Page 5: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

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 andbringing home the cup they can only touch at the event. But some teams also dream, or actu-ally, have nightmares, about losing the competition by a vast amount.

Billy is a pessimistic contestant and is having constant such nightmares. He decided he mustprepare for a losing situation in ICPC. Billy decided to bring a gun to the contest site. If histeam scores no problem during the first four hours of the contest, he will ask to go to the bath-room and will position himself so as to shoot and blow off the largest amount of other team’sballoons he can. Of course, once he shoots, the martials will, with their sticks, quickly escortBilly out of the contest site. He will also probably be disclassified and receive not even a honor-able 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 willmemorize 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 integernumber n, 0 6 n 6 1000, the number of balloons in the room. The next n lines contain eachthree integer numbers x, y and z, − 109 6 x, y, z 6 109, stating there is a balloon in the room inposition (x, y, z) where (x, y) is the position of the projection of the balloon to the floor and z isits 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 willhit a balloon if, and only if, the bullet goes exactly through the balloon. You may also assumethe bullet trajectory is that of a semi-line.

Output

For each test case, output a single line containing a single integer number: the maximumamount of balloons Billy can blow up with a single shot.

Sample Input Sample Output

2 2

1 2 0

1 0 1

1

Page 6: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

2005 East Central Regional Contest 13

Problem H: Two Ends

In the two-player game “Two Ends”, an even number of cards is laid out in a row. On each card, faceup, is written a positive integer. Players take turns removing a card from either end of the row andplacing 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 greedystrategy. However, this is not always optimal, as the following example shows: (The first player wouldwin 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 playeruses 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 withan even integer n followed by n positive integers. A value of n = 0 indicates end of input. You mayassume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers inthe 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 differencebetween 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 48 1 2 3 4 5 6 7 88 2 2 1 5 3 8 7 30

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.

Page 7: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

ACM ICPC2008 – South American Regionals 1

Problem AAlmost 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 ofpoints and route lengths connecting them is an already well known problem, and it’s even partof 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 shortestpaths, most routes that form the shortest path are getting slower because of heavy traffic. Asmost 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 haveaccess to, thus saving him time whenever he has a meeting or any urgent event. He asks youthat the program must answer not the shortest path, but the almost shortest path. He definesthe almost shortest path as the shortest path that goes from a starting point to a destinationpoint such that no route between two consecutive points belongs to any shortest path from thestarting point to the destination.

For example, suppose the figure below represents the map given, with circles representinglocation points, and lines representing direct, one-way routes with lengths indicated. Thestarting point is marked as S and the destination point is marked as D. The bold lines belongto 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 noroute between two consecutive points belongs to any shortest path. Notice that there couldexist more than one possible answer, for instance if the route with length 3 had length 1. Therecould exist no possible answer as well.

S D

1

2

1

1 4

2 2

3 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 respectivelythe number of points in the map and the number of existing one-way routes connecting twopoints directly. Each point is identified by an integer between 0 and N − 1. The second linecontains two integers S and D, separated by a single space, indicating respectively the startingand the destination points (S 6= D; 0 ≤ S, D < N). Each one of the following M lines containsthree 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

Page 8: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

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 fromU to V does not imply there is a route from V to U , and, if such road exists, it can have adifferent length. The end of input is indicated by a line containing only two zeros separated bya 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 notpossible to match the requirements, or an integer representing the length of the almost shortestpath found.

The output must be written to standard output.

Sample input

7 9

0 6

0 1 1

0 2 1

0 3 2

0 4 3

1 5 2

2 6 4

3 6 2

4 6 4

5 6 1

4 6

0 2

0 1 1

1 2 1

1 3 1

3 2 1

2 0 3

3 0 2

6 8

0 1

0 1 1

0 2 2

0 3 3

2 5 3

3 4 2

4 1 1

5 1 1

3 0 1

0 0

Output for the sample input

5

-1

6

Page 9: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

Dinner ProblemSao 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 willprepare the dinner for the whole company. The semester lasts for n days. In sake of fairness they decided that each of thestudents must prepare the dinner at least once during the semester. Now they wonder how many ways are there to plan thesemester – 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 Sample Output2 3 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).

1

Page 10: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

Last DigitsACM 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 basedon repeated exponentiation, but output only the last n digits of the result. Doing this efficiently requires careful thought abouthow 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 toefficiently 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 nextline 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), whichis 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, padthe result with zeroes on the left so that there are exactly n digits.

Sample Input Sample Output2471010631070

00655360000004195387

1

Page 11: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

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 workin the city of Manhattan. It is widely known that most, if not all, authentication protocols forwireless networks implemented in the industry and distributed in the market suffer from graveflaws which could give a sufficiently smart and determined attacker unauthorized access. Theaircrack-ng is an excellent software suite which gives novice and unknowlegeable users thehability to extract keys (and thus break into) WEP (Wired Equivalent Privacy) networks. Untilvery recently it was common knowledge that WPA (Wi-Fi Protected Access) would be a securealternative to WEP, but many flaws have been found in such system, and Ivan’s work is exactlyabout one of them.

Needless to say, Ivan can break into most wireless networks in the city of Manhattan. Not onlythat, he has many (infinitely many for the purposes of this problem) wireless cards, and since heuses the very flexible operating system Linux, he can use them simultaneously to connect tomultiple access points at once. Being an addict to the Dragon Ball series, Ivan wants to down-load all the new episodes of the new Dragon Ball anime, Dragon Ball Kai. If he can stay at alocation within the reach of several wireless access points, he can increase his download speed byusing 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 Man-hattan whose coverage by wireless access points is maximum. Of course, his program neededurban data about Manhattan, but breaking into the american government database was tooeasy for Ivan to even think about it. He then moved to one such spot and admired his downloadspeed.

You are curious about the download speed Ivan has achieved, so you downloaded the urban dataIvan had used, for he has published it anonimously in the internet. Your only task now iswriting a program to compute what is the number of wireless access points covering one of themost covered block in Manhattan.

Input

Input consists of several test cases. Each test case begins with a line containing two integernumbers m, the size of the grid (1 6 m 6 109), and n, the number of wireless access points inManhattan (0 6 n 6 105). Then n lines follow, each describing an access point and containingthree integers i, j and k. The access point is located in block (i, j), 1 6 i, j 6 m, and its power isk, 0 6 k 6 109.

∗. Problem statement by Luís Fernando Schultz Xavier da Silveira with inspiration of the statement of theproblem “Acesso”, by Ricardo Anido, of the selection of brazilian teams for the Internation Olympiads in Infor-matics (IOI) 2010.

1

Page 12: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

Output

For each test case, print one integer in a line by itself: the number of wireless access points cov-ering 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

Page 13: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

A Multiplication GameUVa 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 startswith p = 1, does his multiplication, then Ollie multiplies the number, then Stan and so on. Before a game starts, they draw aninteger 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.

orOllie wins.

assuming that both of them play perfectly.

Sample Input Sample Output1621734012226

Stan wins.Ollie wins.Stan wins.

1

Page 14: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

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 Output for Sample Input 4 4 3 1 1

2 r1 r3 2 r1 r2

Page 15: Peru ACM ICPC Training Camp - IME-USPschultz/peru-camp/2010-08-13/prova.pdfPeru ACM ICPC Training Camp 2010 by Luís Fernando Schultz Xavier da Silveira Statement Well, many teams

1 4 3 2 4 4 2 1 1 2 2 0 0 0

Problemsetter: Syed Monowar Hossain Special Thanks: Derek Kisman