35th ICPC
ICPC 2011
Orlando, United States · 11 problems
- 1Zhejiang University · ArcOfDreamJialin Ouyang, Luyi Mo, Zejun Wu8
- 2University of Michigan at Ann Arbor · VictorsJonathan Plotzke, Mark Gordon, Qifeng Chen8
- 3Tsinghua University · DubheDong Zhou, Zichao Qi7
Problems
11 problemsProblem A · To Add or to Multiply Problem ID: addmul
The Industrial Computer Processor Company offers very fast, special purpose processing units tailored to customer needs. Processors of the -C- family (such as the 1-C-2 and the 5-C-3) have an instruction set with only two different operations:
A add M multiply by
The processor receives an integer, executes a sequence of A and M operations (the program) that modifies the input, and outputs the result. For example, the 1-C-2 processor executing the program AAAM with the input yields the output (the computation is ), while the 5-C-3 processor yields with the same program and input ().
You are an -C- programmer assigned to a top secret project. This means that you have not been told the precise computation your program should perform. But you are given particular values , , , and and the following conditions:
- The input is guaranteed to be a number between and . 2. The output must be some number between and .
Given an -C- processor and the numbers , , , and , your job is to construct the shortest -C- program which, for every input such that , yields some output such that . If there is more than one program of minimum length, choose the one that come first lexicographically, treating each program as a string of As and Ms.
Input
The input contains several test cases. Each test case is given by a line with the six integers , , , , , and as described above (, and ).
The last test case is followed by a line with six zeros.
Output
For each test case, display its case number followed by the best program as described above. Display the word “empty” if the best program uses no operations. Display the word “impossible” if there is no program meeting the specifications.
Display the program as a sequence of space-separated strings, alternating between strings of the form “A” and strings of the form “M”, where . Strings of the former type indicate consecutive A operations, and strings of the latter type indicate consecutive M operations.
Follow the format of the sample output.
ICPC 2011 World Finals Problem A: To Add or to Multiply
Sample Input
1 2 2 3 10 20 1 3 2 3 22 33 3 2 2 3 4 5 5 3 2 3 2 3 0 0 0 0 0 0Sample Output
Case 1: 1A 2M Case 2: 1M 2A 1M Case 3: impossible Case 4: emptyICPC 2011 World Finals Problem A: To Add or to Multiply
Problem B · Affine Mess Problem ID: affine
Tess L. Ation ran into a little problem last week when she demonstrated the beta version of her new drawing software. On the screen she had an elegant demonstration design that illustrated every feature of her program; it had taken her hours to produce it. She was just putting the finishing touches on it as a group of potential investors entered the room to see the demonstration.
The presentation went well. Near the end, Tess clicked on a control panel button and told her audience, “This is the ‘snap to grid’ control. It forces control points, such as vertices, to jump to the nearest grid point. Here, let me show you,” and she placed three bright red dots on the screen. Each one appeared at the grid point nearest to where she clicked. (“Luckily all control points in my demo design were already at integer coordinates. But I will have to remember to delete these three red dots before I save my diagram,” she thought to herself.) “Now I’ll step into the next room and get out of your way so you can discuss the system among yourselves and get a closer look at the screen, but please don’t touch anything, since I haven’t saved that file yet.”
A few minutes later, the group joined Tess. One of the visitors stepped up to Tess and said, “I hope you don’t mind, but I wanted to try it myself. Don’t worry, I just played with the -scale and -scale controls a little bit.” The next person said, “Sorry if this is a problem, but I really wanted to get a feel for the speed of display, so I just played around with the translation tool.” And a third person said, “I couldn’t resist just one tiny test: I rotated the image just so I could see all of the vertices snap to the nearest grid points after the rotation.”
The person who played with the rotation tool remembered going first, but the other two could not recall their order. The three remembered only a few details of the changes. The - and -scaling factors had been (possibly negative) non- zero integers; the center of scaling was the origin . The - and -translation amounts had been integers. Rotation had been specified by a point with integer coordinates on the perimeter of a square of width centered at the origin (hence, and the absolute value of or or both was ). The tool rotated the drawing around the origin such that the positive -axis would pass through afterwards. Snapping took place after this rotation (coordinates with a fractional part of were rounded away from zero).
After they left, Tess looked at her design – it was completely changed! She had not yet implemented the “undo” feature, and she had not saved the diagram prior to giving the demonstration. However, the three identical red dots were still there (transformed to other integer grid locations, of course), and Tess could remember the integer coordinates where she had originally placed them. Obviously, someone else might have altered the drawing without saying anything to her, but she could write a program to see if it was possible to reconstruct the sequence of alterations. Can you too?
Input
The input contains several test cases. Each test case consists of six pairs of integers and ( for ), three pairs per input line. The first three pairs represent the distinct initial locations of the three red dots. The last three pairs represent the distinct final locations of the three dots. The indexing of the pairs in each group of three is not significant: for example, could have been mapped to any of , or .
The last test case is followed by a line with six zeros.
ICPC 2011 World Finals Problem B: Affine Mess
Output
For each test case, display its case number followed by one of the following three messages:
-
“equivalent solutions” to indicate that there are one or more valid transformations, and all of them have the same effect on the whole drawing (no matter what the whole drawing looks like).
-
“inconsistent solutions” to indicate that there are several valid transformations, but in general not all of them map the entire drawing in the same way (some drawing is mapped differently by two valid transforma- tions).
-
“no solution” to indicate that neither of the first two cases occurs.
A valid transformation is a combination of rotation, translation and scaling (or rotation, scaling and translation) which satisfies the restrictions described above and maps the initial set of red dots to the final set (occupying all three final locations).
Follow the format of the sample output.
Sample Input
3 0 4 0 1 4 -2 -4 -1 3 3 -4 0 1 1 1 2 1 1 2 2 2 3 2 1 0 2 0 3 0 3 3 1 1 2 2 1 0 2 0 3 0 3 2 1 1 2 2 2 3 0 6 1 2 2 3 0 6 1 2 0 0 0 0 0 0Sample Output
Case 1: equivalent solutions Case 2: inconsistent solutions Case 3: no solution Case 4: inconsistent solutions Case 5: equivalent solutionsICPC 2011 World Finals Problem B: Affine Mess
-
Problem C · Ancient Messages Problem ID: ancient
In order to understand early civilizations, archaeologists often study texts written in ancient languages. One such language, used in Egypt more than 3000 years ago, is based on characters called hieroglyphs. Figure C.1 shows six hieroglyphs and their names. In this problem, you will write a program to recognize these six characters.

Figure C.1: Six hieroglyphs
Input
The input consists of several test cases, each of which describes an image containing one or more hieroglyphs chosen from among those shown in Figure C.1. The image is given in the form of a series of horizontal scan lines consisting of black pixels (represented by 1) and white pixels (represented by 0). In the input data, each scan line is encoded in hexadecimal notation. For example, the sequence of eight pixels 10011100 (one black pixel, followed by two white pixels, and so on) would be represented in hexadecimal notation as 9c. Only digits and lowercase letters a through f are used in the hexadecimal encoding. The first line of each test case contains two integers, and () is the number of scan lines in the image. () is the number of hexadecimal characters in each line. The next lines contain the hexadecimal characters of the image, working from top to bottom. Input images conform to the following rules:
-
The image contains only hieroglyphs shown in Figure C.1.
-
Each image contains at least one valid hieroglyph.
-
Each black pixel in the image is part of a valid hieroglyph.
-
Each hieroglyph consists of a connected set of black pixels and each black pixel has at least one other black pixel on its top, bottom, left, or right side.
-
The hieroglyphs do not touch and no hieroglyph is inside another hieroglyph.
-
Two black pixels that touch diagonally will always have a common touching black pixel.
-
The hieroglyphs may be distorted but each has a shape that is topologically equivalent to one of the symbols in Figure C.11.
The last test case is followed by a line containing two zeros.
1Two figures are topologically equivalent if each can be transformed into the other by stretching without tearing.
ICPC 2011 World Finals Problem C: Ancient Messages
Output
For each test case, display its case number followed by a string containing one character for each hieroglyph recognized in the image, using the following code:
Ankh: A Wedjat: J Djed: D Scarab: S Was: W Akhet: K
In each output string, print the codes in alphabetic order. Follow the format of the sample output.
The sample input contains descriptions of test cases shown in Figures C.2 and C.3. Due to space constraints not all of

Figure C.3: AAAAA
Sample Input
100 25 0000000000000000000000000 0000000000000000000000000 ...(50 lines omitted)... 00001fe0000000000007c0000 00003fe0000000000007c0000 ...(44 lines omitted)... 0000000000000000000000000 0000000000000000000000000 150 38 00000000000000000000000000000000000000 00000000000000000000000000000000000000 ...(75 lines omitted)... 0000000003fffffffffffffffff00000000000 0000000003fffffffffffffffff00000000000 ...(69 lines omitted)... 00000000000000000000000000000000000000 00000000000000000000000000000000000000 0 0Sample Output
Case 1: AKW Case 2: AAAAAICPC 2011 World Finals Problem C: Ancient Messages
-
Problem D · Chips Challenge Problem ID: chips
A prominent microprocessor company has enlisted your help to lay out some interchangeable components (widgets) on some of their computer chips. Each chip’s design is an square of slots. One slot can hold a single component, and you are to try to fit in as many widgets as possible.
Modern processor designs are complex, of course. You unfortunately have several restrictions:
-
Some of the slots are disabled.
-
Some of the slots are already occupied by other components and cannot be used for widgets.
-
There are sibling memory buses connected to the horizontal and vertical edges of the chip and their bandwidth loads need to match. As such, there must be exactly as many components in the first row as in the first column, exactly as many in the second row as in the second column, and so on. Component counts include both the components already specified on the chip and the added widgets.
-
Similarly, the power supply is connected at the end of each row and column. To avoid hot spots, any given row or column must have no more than of the total components on the chip for a given and .
A specification for a chip is lines of characters, where ‘.’ indicates an open slot, ‘/’ indicates a disabled slot, and ‘C’ indicates a slot already occupied by a component. For example:
CC/.. ./.// ..C.C /.C.. /./C/If no more than of the components may be in any one row or column, the maximum number of widgets that can be added to this chip is . A possible arrangement is below, where ‘W’ indicates a widget added in an open slot.
CC/W. W/W// W.C.C /.CWW /W/C/Input
The input consists of several test cases. Each case starts with a line containing three integers: The size of the chip (), and and (, ) as described above. Each of the following lines contains characters describing the slots, one of ‘.’, ‘/’ or ‘C’, as described above.
The last test case is followed by a line containing three zeros.
ICPC 2011 World Finals Problem D: Chips Challenge
Output
For each test case, display a single line beginning with the case number. If there is a solution, display the maximum number of widgets that can be added to the chip. Display “impossible” if there is no solution.
Follow the format of the sample output.
Sample Input
2 1 1 /. // 2 50 100 /. C/ 2 100 100 ./ C. 5 3 10 CC/.. ./.// ..C.C /.C.. /./C/ 5 2 10 CC/.. ./.// ..C.C /.C.. /./C/ 0 0 0Sample Output
Case 1: 0 Case 2: 1 Case 3: impossible Case 4: 7 Case 5: impossibleICPC 2011 World Finals Problem D: Chips Challenge
-
Problem E · Coffee Central Problem ID: coffee
Is it just a fad or is it here to stay? You’re not sure, but the steadily increasing number of coffee shops that are opening in your hometown has certainly become quite a draw. Apparently, people have become so addicted to coffee that apartments that are close to many coffee shops will actually fetch higher rents.
This has come to the attention of a local real-estate company. They are interested in identifying the most valuable locations in the city in terms of their proximity to large numbers of coffee shops. They have given you a map of the city, marked with the locations of coffee shops. Assuming that the average person is willing to walk only a fixed number of blocks for their morning coffee, you have to find the location from which one can reach the largest number of coffee shops. As you are probably aware, your hometown is built on a square grid layout, with blocks aligned on north-south and east-west axes. Since you have to walk along streets, the distance between intersections and is .
Input
The input contains several test cases. Each test case describes a city. The first line of each test case contains four integers , , , and . These are the dimensions of the city grid (), the number of coffee shops (), and the number of queries (). Each of the next lines contains two integers and (, ); these specify the location of the th coffee shop. There will be at most one coffee shop per intersection. Each of the next lines contains a single integer (), the maximal distance that a person is willing to walk for a cup of coffee.
The last test case is followed by a line containing four zeros.
Output
For each test case in the input, display its case number. Then display one line per query in the test case. Each line displays the maximum number of coffee shops reachable for the given query distance followed by the optimal location. For example, the sample output shows that 3 coffee shops are within query distance 1 of the optimal location , 4 shops are within query distance 2 of optimal location , and 5 shops are within query distance 4 of optimal location . If there are multiple optimal locations, pick the location that is furthest south (minimal positive integer -coordinate). If there is still a tie, pick the location furthest west (minimal positive integer -coordinate).
Follow the format of the sample output.
ICPC 2011 World Finals Problem E: Coffee Central
Sample Input
4 4 5 3 1 1 1 2 3 3 4 4 2 4 1 2 4 0 0 0 0Sample Output
Case 1: 3 (3,4) 4 (2,2) 5 (3,1)ICPC 2011 World Finals Problem E: Coffee Central
Problem F · Machine Works Problem ID: works
You are the director of Arbitrarily Complex Machines (ACM for short), a company producing advanced machinery using even more advanced machinery. The old production machinery has broken down, so you need to buy new production machines for the company. Your goal is to make as much money as possible during the restructuring period. During this period you will be able to buy and sell machines and operate them for profit while ACM owns them. Due to space restrictions, ACM can own at most one machine at a time.
During the restructuring period, there will be several machines for sale. Being an expert in the advanced machines market, you already know the price and the availability day for each machines . Note that if you do not buy machine on day , then somebody else will buy it and it will not be available later. Needless to say, you cannot buy a machine if ACM has less money than the price of the machine.
If you buy a machine on day , then ACM can operate it starting on day . Each day that the machine operates, it produces a profit of dollars for the company.
You may decide to sell a machine to reclaim a part of its purchase price any day after you’ve bought it. Each machine has a resale price for which it may be resold to the market. You cannot operate a machine on the day that you sell it, but you may sell a machine and use the proceeds to buy a new machine on the same day.
Once the restructuring period ends, ACM will sell any machine that it still owns. Your task is to maximize the amount of money that ACM makes during the restructuring.
Input
The input consists of several test cases. Each test case starts with a line containing three positive integers , , and . is the number of machines for sale (), is the number of dollars with which the company begins the restructuring (), and is the number of days that the restructuring lasts ().
Each of the next lines describes a single machine for sale. Each line contains four integers , , and , denoting (respectively) the day on which the machine is for sale, the dollar price for which it may be bought, the dollar price for which it may be resold and the daily profit generated by operating the machine. These numbers satisfy , and .
The last test case is followed by a line containing three zeros.
Output
For each test case, display its case number followed by the largest number of dollars that ACM can have at the end of day .
Follow the format of the sample output.
ICPC 2011 World Finals Problem F: Machine Works
Sample Input
6 10 20 6 12 1 3 1 9 1 2 3 2 1 2 8 20 5 4 4 11 7 4 2 10 9 1 0 0 0Sample Output
Case 1: 44ICPC 2011 World Finals Problem F: Machine Works
Problem G · Magic Sticks Problem ID: magicsticks
Magic was accepted by all ancient peoples as a technique to compel the help of divine powers. In a well-known story, one group of sorcerers threw their walking sticks on the floor where they magically appeared to turn into live serpents. In opposition, another person threw his stick on the floor, where it turned into a serpent which then consumed the sorcerers’ serpents!
The only magic required for this problem is its solution. You are given a magic stick that has several straight segments, with joints between the segments that allow the stick to be folded. Depending on the segment lengths and how they are folded, the segments of the stick can be arranged to produce a number of polygons. You are to determine the maximum area that could be enclosed by the polygons formed by folding the stick, using each segment in at most one polygon. Segments can touch only at their endpoints. For example, the stick shown below on the left has five segments and four joints. It can be folded to produce a polygon as shown on the right.
Input
The input contains several test cases. Each test case describes a magic stick. The first line in each test case contains an integer () which indicates the number of the segments in the magic stick. The next line contains integers () which indicate the lengths of the segments in the order they appear in the stick.
The last test case is followed by a line containing a single zero.
Output
For each case, display its case number followed by the maximum total enclosed area that can be obtained by folding the magic stick at the given points. Answers within an absolute or relative error of will be accepted.
Follow the format of the sample output.
Sample Input
4 1 2 3 4 8 3 4 5 33 3 4 3 5 0Sample Output
Case 1: 4.898979 Case 2: 19.311ICPC 2011 World Finals Problem G: Magic Sticks
Problem H · Mining Your Own Business Problem ID: mining
John Digger is the owner of a large illudium phosdex mine. The mine is made up of a series of tunnels that meet at various large junctions. Unlike some owners, Digger actually cares about the welfare of his workers and has a concern about the layout of the mine. Specifically, he worries that there may a junction which, in case of collapse, will cut off workers in one section of the mine from other workers (illudium phosdex, as you know, is highly unstable). To counter this, he wants to install special escape shafts from the junctions to the surface. He could install one escape shaft at each junction, but Digger doesn’t care about his workers that much. Instead, he wants to install the minimum number of escape shafts so that if any of the junctions collapses, all the workers who survive the junction collapse will have a path to the surface.
Write a program to calculate the minimum number of escape shafts and the total number of ways in which this minimum number of escape shafts can be installed.
Input
The input consists of several test cases. The first line of each case contains a positive integer () indicating the number of mine tunnels. Following this are lines each containing two distinct integers and , where and are junction numbers. Junctions are numbered consecutively starting at 1. Each pair of junctions is joined by at most a single tunnel. Each set of mine tunnels forms one connected unit (that is, you can get from any one junction to any other).
The last test case is followed by a line containing a single zero.
Output
For each test case, display its case number followed by the minimum number of escape shafts needed for the system of mine tunnels and the total number of ways these escape shafts can be installed. You may assume that the result fits in a signed 64-bit integer.
Follow the format of the sample output.
ICPC 2011 World Finals Problem H: Mining Your Own Business
Sample Input
9 1 3 4 1 3 5 1 2 2 6 1 5 6 3 1 6 3 2 6 1 2 1 3 2 4 2 5 3 6 3 7 0Sample Output
Case 1: 2 4 Case 2: 4 1ICPC 2011 World Finals Problem H: Mining Your Own Business
Problem I · Mummy Madness Problem ID: mummy
During an excursion to the desert at the 2011 ACM-ICPC World Finals, you come across an old Egyptian tomb. Unfortunately, opening the tomb turns out to be a bad idea: all of a sudden, what was just a few moments ago an empty desert has now become a desert crawling with grumpy mummies (you would be grumpy too if you were suddenly awakened after a few thousand years of peaceful sleep).2
Faced with this murderous mass of mad mummies, your only chance is to run for it and try to escape before they catch you. The question is: how long will it take before a mummy catches you, assuming neither you nor the mummies ever get tired?
We model the desert as a grid of squares. You and the mummies take turns making moves on the grid. You make the first move. In your turns, you can move to any of the eight squares adjacent to your current location, or you can choose to stand still. In the mummies’ turns, each mummy simply moves to the adjacent square that brings it closest to you (measured by Euclidean distance, assuming that you and all the mummies stand in the centers of their respective squares). It is possible for two mummies to occupy the same square.
A time step consists of your move followed by the mummies’ moves. A mummy catches you if it moves to the square where you are located, or if you move to the square occupied by the mummy. Of course, you try to avoid being caught for as long as possible. After how many time steps will you be caught?

Figure I.1: A mummy chase
The figure illustrates what might happen if you are being chased by four mummies. The square labeled H is your initial position, and the squares labeled M are the initial positions of mummies. After four time steps, you are caught by the mummy whose initial position was with respect to your initial position. 2Fortunately, after solving this problem, you woke up safe and sound in a hotel room in Florida. The enraged mummies had just been a dream. Or had they?
ICPC 2011 World Finals Problem I: Mummy Madness
Input
The input consists of several test cases. Each test case begins with an integer giving the number of mummies in the desert. The following lines each contain two integers and , indicating that there is initially a mummy at coordinates of the desert, where and are both bounded by in absolute value. Your starting position is , and no mummy starts at this position.
The last test case is followed by a line containing the number .
Output
For each test case, display its test case number followed by the maximum number of time steps until you are caught (measured as the total number of turns that you get), or the word “never” if you can avoid capture indefinitely.
Follow the format of the sample output.
Sample Input
4 -3 5 3 4 -6 -2 1 -5 1 0 -1 -1Sample Output
Case 1: 4 Case 2: neverICPC 2011 World Finals Problem I: Mummy Madness
Problem J · Pyramids Problem ID: pyramids
It is not too hard to build a pyramid if you have a lot of identical cubes. On a flat foundation you lay, say, cubes in a square. Centered on top of that square you lay a square of cubes. Continuing this way you end up with a single cube, which is the top of the pyramid. The height of such a pyramid equals the length of its base, which in this case is 10. We call this a high pyramid.
If you think that a high pyramid is too steep, you can proceed as follows. On the base square, lay an square, then a square, and so on, ending with a top square (if you start with a base of odd length, you end up with a single cube on top, of course). The height of this pyramid is about half the length of its base. We call this a low pyramid.
Once upon a time (quite a long time ago, actually) there was a pharaoh who inherited a large number of stone cubes from his father. He ordered his architect to use all of these cubes to build a pyramid, not leaving a single one unused. The architect kindly explained that not every number of cubes can form a pyramid. With 10 cubes you can build a low pyramid with base 3. With 5 cubes you can build a high pyramid of base 2. But no pyramid can be built using exactly 7 cubes.
The pharaoh was not amused, but after some thinking he came up with new restrictions.
- All cubes must be used. 2. You may build more than one pyramid, but you must build as few pyramids as possible. 3. All pyramids must be different. 4. Each pyramid must have a height of at least 2. 5. Satisfying the above, the largest of the pyramids must be as large as possible (i.e., containing the most cubes). 6. Satisfying the above, the next-to-largest pyramid must be as large as possible. 7. And so on...
Drawing figures and pictures in the sand, it took the architect quite some time to come up with the best solution.
Write a program that determines how to meet the restrictions of the pharaoh, given the number of cubes.
Input
The input consists of several test cases, each one on a single line. A test case is an integer , where , giving the number of cubes available.
The last test case is followed by a line containing a single zero.
ICPC 2011 World Finals Problem J: Pyramids
Output
For each test case, display its case number followed by the pyramids to be built. The pyramids should be ordered with the largest first. Pyramids are specified by the length of their base followed by an L for low pyramids or an H for high pyramids. If two differenct pyramids have the same number of cubes, list the high pyramid first. Print “impossible” if it is not possible to meet the requirements of the pharaoh.
Follow the format of the sample output.
Sample Input
29 28 0Sample Output
Case 1: 3H 3L 2H Case 2: impossibleICPC 2011 World Finals Problem J: Pyramids
Problem K · Trash Removal Problem ID: trash
Allied Chute Manufacturers is a company that builds trash chutes. A trash chute is a hollow tube installed in buildings so that trash dropped in at the top will fall down and be collected in the basement. Designing trash chutes is actually highly nontrivial. Depending on what kind of trash people are expected to drop into them, the trash chute needs to have an appropriate size. And since the cost of manufacturing a trash chute is proportional to its size, the company always would like to build a chute that is as small as possible. Choosing the right size can be tough though.
We will consider a 2-dimensional simplification of the chute design problem. A trash chute points straight down and has a constant width. Objects that will be dropped into the trash chute are modeled as polygons. Before an object is dropped into the chute it can be rotated so as to provide an optimal fit. Once dropped, it will travel on a straight path downwards and will not rotate in flight. The following figure shows how an object is first rotated so it fits into the trash chute.
Your task is to compute the smallest chute width that will allow a given polygon to pass through.
Input
The input contains several test cases. Each test case starts with a line containing an integer (), the number of points in the polygon that models the trash item.
The next lines then contain pairs of integers and (), giving the coordinates of the polygon vertices in order. All points in one test case are guaranteed to be mutually distinct and the polygon sides will never intersect. (Technically, there is one inevitable exception of two neighboring sides sharing their common vertex. Of course, this is not considered an intersection.)
The last test case is followed by a line containing a single zero.
ICPC 2011 World Finals Problem K: Trash Removal
Output
For each test case, display its case number followed by the width of the smallest trash chute through which it can be dropped. Display the minimum width with exactly two digits to the right of the decimal point, rounding up to the nearest multiple of 1/100. Answers within 1/100 of the correct rounded answer will be accepted.
Follow the format of the sample output.
Sample Input
3 0 0 3 0 0 4 4 0 10 10 0 20 10 10 20 0Sample Output
Case 1: 2.40 Case 2: 14.15ICPC 2011 World Finals Problem K: Trash Removal