Cedarville University 2013 Programming Contest Problem Set Welcome to the 2013 Cedarville Programming Contest: Cedarville’s 12th annual programming contest! Before you start the contest, please be aware of the following notes: 1. There are ten (10) problems in the packet. These problems are NOT necessarily sorted by difficulty: A. Class Packing B. Modulo Base Arithmetic C. Chemistry Formulas D. Darts E. The King’s Ups and Downs F. Oh, Man ... Will I Pass This Class? G. Shirt Packing H. Conservation I. Dirichlet’s Theorem J. Emergency Room 2. Contestants will work solutions to these problems using either C++ or Java. All work must be done within the PC2 environment, using Notepad, Notepad++, or jEdit as the text editor. Contestants are NOT allowed to program outside the contest environment, i.e., start up the Visual C++ or Netbeans IDEs. 3. All solutions must read its input from a specific file name and write its output to STDOUT. The input filenames are specified for each problem, typically with a “.in” suffix. 4. Solutions to problems submitted for judging are called runs. Each run will be judged. The judges will respond with one of the responses shown in the table below. In the event that more than one response is applicable, the judges may respond with any of the applicable responses. Response Correct Compilation Error Description The run has been judged correct. The program failed to compile on the judges’ machine The program had a run time error. The program failed to complete within a reasonable amount of time. Likely causes would be an infinite loop or an extremely inefficient algorithm. The program generated output that is not correct. The program’s output is not in the correct format. Likely caused by not completing all test sets Run-Time Error Time-Limit Exceeded Incorrect Output Incorrect Output Format Incomplete Output 1 Cedarville University 2013 Programming Contest Problem Set 5. In the event that you feel a problem statement is ambiguous, you may request a clarification. Read the problem carefully before requesting a clarification. If the judges do not believe that you have discovered an ambiguity in the problem, you will receive the response, “The problem statement is not ambiguous, no clarification is necessary.” If you receive this response, you should read the problem description more carefully. If you still feel there is an ambiguity, you will have to be more specific or descriptive of the ambiguity you have found. If the problem statement is ambiguous in specifying the correct output for particular input, please include that input data in the clarification request. 6. You are not allowed to use any electronic form of help during the contest, including data stored on the local computer, data on USB drives, files from the network or internet, calculators, phones, or other programmable devices. You may use any books, notes, or papers that you have brought. You may also use printed copies of any programs that you have brought with you. 7. There should be no communication between teams during the contest. All communication with judges will be via the PC2 environment. 8. Contest judging is based upon 3 components: the number of correct problems submitted, the elapsed time from the beginning of the contest to when problems are correctly submitted, and the number of incorrect submissions for problems for which a correct solution is eventually submitted. Teams are first ranked by # of problems for which a correct solution is submitted. In the event two or more teams solve the same number of problems, penalty minutes will be used as a tiebreaker. The winner will be the team with the least penalty minutes. Penalty minutes are calculated using the following: a. For each solved problem, the number of minutes from the beginning of the contest until the correct solution was submitted. b. For each problem that is eventually solved, a 20 minutes penalty will be accessed for each incorrect solution submitted prior to the correct solution. No penalty will be incurred for incorrect solutions for problems for which a correct solution is never submitted. In the unlikely event there is still a tie after using penalty minutes, the winner will be the team that achieved its score first. 9. Judges decisions will be final. 10. Good luck, and HAVE FUN!!! 2 Cedarville University 2013 Programming Contest Problem Set Problem A Class Packing Input: packing.in Ima Polisci, the principal of the local elementary school, is tired of trying to figure out how many teachers she will need for the school year. She wants you to write a program to figure it out for her. Ima finds out, on the first day of school, the number of students enrolled in each class from kindergarten to 6th grade. She needs to put the students in classes in such a way that she uses the smallest number of teachers while adhering to the following Department of Education rules: Classes containing kindergarten to 2nd grade students must have a size of 20 or less, Classes containing 3rd grade and 4th grade students must have a size of 25 or less, Classes with year 5th and 6th grade students must have a size of 30 or less, A class can only have students from one grade or two consecutive grades; for example, a class combining kindergarten and 1st grade students must have a size of 20 or less, while a class combining 4th and 5th grade students must have a size of 25 or less. A teacher can only be assigned to one class.. Your task is to write a program that reads the enrolment numbers and computes the minimum number of teachers required. Input The input consists of a number of test cases. The description for each test case consists of seven non-negative integers on a line by themselves. The integers represent the number of pupils enrolled from Kindergarten to 6th grade, in that order. All integers have values less than 200, and a single space separates the integers. A line with seven zeroes terminates the input and should not be processed. Output The output consists of a single line, for each test case, which contains a single integer that represents the minimum number of teachers required. 3 Cedarville University 2013 Programming Contest Problem Set Sample Input 20 20 20 20 20 20 20 19 1 0 0 0 0 0 19 0 1 0 0 0 0 19 3 0 0 0 0 0 3 48 77 165 173 165 4 125 141 107 8 68 58 176 0 0 0 0 0 0 0 Sample Output 6 1 2 2 26 30 4 Cedarville University 2013 Programming Contest Problem Set Problem B Modulo Base Arithmetic Input: modulo.in Professor Schumacher loves teaching modular arithmetic. And he really loves working in bases other than base-10. Trying to combine the two ideas, he comes up with modulo base arithmetic. In modulo base arithmetic, you are given a number in a particular base b, and you have to compute the modulus of that number modulo b-1. For example: 782910 mod 9 = 8 377777777777777738 mod 7 = 6 1234567 mod 6 = 3 (Note that 377777777777777738 = 112589990684261910 and 1234567 = 2287510.) Your job is to write a program that reads integer values in various bases and computes the remainder after dividing these values by one less than the input base. Input The first line of input contains a single integer P (1 <= P <= 1000), which specifies the number of data sets that follow. Each data set consists of a single line of input containing two space-separated integers. The first is the integer B (2<= B <=10), denoting a numeric base. The second is an integer number D (0 <= D <=1,000,000,000), in base B representation. Output For each data set, print a single line of output containing the remainder resulting from dividing D by (B - 1). Sample Input 5 10 7829 7 123456 6 432502 8 373 2 10100110 5 Cedarville University 2013 Programming Contest Problem Set Sample Output 8 3 1 6 0 6 Cedarville University 2013 Programming Contest Problem Set Problem C Chemistry Formulas Input: chemistry.in The chemical formula of a molecule M describes its atomic make-up. Chemical formulas obey the following grammar: M := G | M G G := S | S C S := A | '(' M ')' C := T | N E E := D | D E T := '2' | ... | '9' N := '1' | ... | '9' D := '0' | .. | '9' A := U | U L | U L L U := 'A' | .. | 'Z' L := 'a' | .. | 'z' The count C represents a multiplier for the subgroup S that precedes it. For example, H2O has two H (hydrogen) and one O (oxygen) atoms, and (AlC2)3Na4 contains 3 Al (aluminum), 6 C (carbon) and 4 Na (sodium) atoms. Input The input will contain data for one or more test cases. The first line contains a single integer, specifying the number of test cases which follow. For each test case, there will be one line of input, containing a valid chemical formula. Each line will have no more than 100 characters. Output For each test case, output the atomic decomposition of the chemical in the form of a sum as shown in the Sample Output. The atoms should be listed in lexicographical order, and a count of 1 is implied and should not be explicitly written. There should be no blank spaces in the output. 7 Cedarville University 2013 Programming Contest Problem Set Sample Input 2 H2O (AlC2)3Na4 Sample Output 2H+O 3Al+6C+4Na 8 Cedarville University 2013 Programming Contest Problem Set Problem D Darts Input: darts.in Consider a game in which darts are thrown at a board. The board is formed by 10 circles with radii 20, 40, 60, 80, 100, 120, 140, 160, 180, and 200 (measured in millimeters), centered at the origin. Each throw is evaluated depending on where the dart hits the board. If the dart lands on or within the circle of radius 20, 10 points are awarded. Likewise, if the dart lands on or within the circle of radius 40, but outside the circle of radius 20, then 9 points are awarded. Thus, landing on or within the circle of radius 200 (but not on or within any of the other circles) scores 1 point. No points are awarded for a throw that lands outside the largest circle. Your task is to compute the total score of a series of throws. Input The input contains one or more test sets. The first line of the input specifies the number of test sets to follow. Each data set begins with an integer, indicating the number of throws t in the test set. The next t lines contain two integers each, x and y, which the coordinates of where the throw lands. Both x and y will be <= 200. Output For each data set, output a line containing the number of points scored for that test set. Sample Input 1 5 32 -39 71 89 -60 80 0 0 196 89 Sample Output 29 9 Cedarville University 2013 Programming Contest Problem Set Problem E The King’s Ups and Downs Input: updown.in The king has guards of all different heights. Rather than line them up in increasing or decreasing height order, he wants to line them up so each guard is either shorter than the guards next to him or taller than the guards next to him (so the heights go up and down along the line). No two guards will have the exact same height. For example, seven guards of heights 160, 162, 164, 166, 168, 170 and 172 cm. could be arranged as: or perhaps: The king wants to know how many guards he needs so he can have a different up and down order at each changing of the guard for rest of his reign. To be able to do this, he needs to know for a given number of guards, n, how many different up and down orders there are: For example, if there are four guards with heights: 1, 2, 3, 4, then they can be arranged as: 1324, 2143, 3142, 2314, 3412, 4231, 4132, 2413, 3241, 1423 For this problem, you will write a program that takes as input a positive integer n, the number of guards and returns the number of up and down orders for n guards of differing heights. 10 Cedarville University 2013 Programming Contest Problem Set Input The input contains several test sets. Each test set consists of a single integer n (n <= 12), the number of guards. The input is terminated when n = 0; this last line of input should not be processed. Output For each test set there should be one line of output, containing the number of up-down orderings possible for this number of guards. Sample Input 1 3 10 0 Sample Output 1 4 101042 11 Cedarville University 2013 Programming Contest Problem Set Problem F Oh, Man … Will I Pass This Class? Input: final.in One typical question that students ask professors right before the final exam in a course is what score they need on the final exam in order to get a grade that they want. This question always stumps Dr. Shomper. Your task is to write a program that automates the answer to this question. Input The input will consist of one or more test sets. Within each test set, the first integer will be the desired average, d, where 0 <= d <= 100. The next integer will be the number of tests (including the final exam), k, 0 <= k <= 10. Next there will be k integers giving the percentage weights for the tests. You may assume that the sum of the weights is 100. Finally, the scores on the k – 1 tests prior the final are given as input. All test scores including that of the final exam will be no less than 0 and no more than 100. You are to determine if there is a final exam grade that will achieve the desired result. If there is such a grade, the smallest such is the answer for the case. After the last valid case will be a line containing -1. Output For each input case, print the minimum final exam grade required to obtain the desired average. If there is no valid grade that will achieve the desired average, print “Impossible”. Sample Input 90 4 25 85 90 5 15 87 90 6 10 85 -1 25 25 25 87 91 25 15 25 20 62 47 91 10 10 10 10 50 87 91 87 53 12 Cedarville University 2013 Programming Contest Problem Set Sample Output 97 Impossible 100 13 Cedarville University 2013 Programming Contest Problem Set Problem G Shirt Packing Input: shirt.in Two CS majors were packing for a bike tour of America during the summer, and were discussing how many shirts they should take. Note that CS majors are not necessarily known for their hygiene. John said that he was thinking about taking four shirts for a seven-day trip so that he could wear each of three shirts for two days and have a clean shirt for the last day, which sounded like a good idea. Alan said that he might wear each shirt for up to three days, so taking four shirts would allow him to wear one shirt the first three days, one on days four and five, and then have a clean shirt for each of the last two days. They then both came to the conclusion that computing how many consecutive days they could wear clean shirts would make for an interesting programming problem if hygiene is ignored (typical for male CS students). Input Each line of input will have three positive integer values: the length of the trip in number of days, the number of shirts that are packed, and the maximum number of days that each shirt can be worn. You may assume that all values will be <= 1000. After the last input case will be a line of three zeros. Output For each test case, print the case number and the maximum number of consecutive days at the end of the trip that a clean shirt can be worn. If not enough shirts are packed for the trip, print 0 days. Follow the given format exactly, “Case”, a single space, the case number, a colon, a single space, the number of days, a single space, and the word “day” if the number is one or “days” if otherwise. Sample Input 7 7 7 7 0 4 4 2 8 0 2 3 4 2 0 Sample Output Case Case Case Case 1: 2: 3: 4: 1 2 0 7 day days days days 14 Cedarville University 2013 Programming Contest Problem Set Problem H Conservation Input: conservation.in The most famous painting at Cedarville, a portrait of Dr. Gallagher sleeping in his office, needs to be conserved. The work will be conducted in two narrowly specialized laboratories. The conservation process has been divided into several stages. For each of them, we know the laboratory in which it will take place. Transporting the very precious and fragile painting introduces additional risk; therefore, it should be avoided whenever possible. Ideally, all the work in the first laboratory would be done, and then the painting would be moved to the second one. Unfortunately, there are several dependencies between the conservation stages—some of them need to be completed before others may begin. Your task is to find an ordering of conservation stages that minimizes the number of times the painting needs to be moved from one laboratory to the other. The conservation can begin in any of the two laboratories. Input The first line of the input contains the number of test cases. The first line of each test case contains two space-separated integers n and m, which represent the number of conservation stages and the number of dependencies between them, respectively. Both integers will be <= 1000. On the next line of each test case, there are n integers; the ith of them is 1 if the ith conservation stage must take place in the first laboratory, and 2 otherwise. The following m lines contain pairs of integers i, j (1 <= i, j <= n), denoting that the ith stage has to be completed before the jth. You may assume that it is always possible to order the conservation stages so that all the dependencies are satisfied. Output For each test case, output a single line containing the minimal number of times the painting needs to be transported between the laboratories. 15 Cedarville University 2013 Programming Contest Problem Set Sample Input 2 5 1 1 1 2 3 2 3 5 1 1 3 2 6 2 1 2 1 2 3 4 4 5 5 3 2 1 2 1 3 5 4 Sample Output 2 1 16 Cedarville University 2013 Programming Contest Problem Set Problem I Dirichlet’s Theorem Input: dirichlet.in Dirichlet's theorem on arithmetic progressions states that for any two positive integers a and b, if gcd(a,b) = 1 then the arithmetic progression t(n) = a*n + b (n ≥ 0) contains infinitely many prime numbers. Recall that a prime number is a positive integer ≥ 2 that has no divisors other than 1 and itself. For example, if a = 4 and b = 3, then the arithmetic progression is 3, 7, 11, 15, 19, 23, 27, 31, 35, ..., and it can be seen that many prime numbers are contained in the first part of this list. Given arbitrary integers a > 0, b ≥ 0, and U ≥ L ≥ 0, your job is to count how many values of t(n) = a*n+b are prime, where L ≤ n ≤ U. Input The input consists of a number of test cases. The input for each test case is specified by the four integers a, b, L, and U on a line. You may assume that a*U+b ≤ 106 and U - L ≤ 104. A line containing a four 0’s indicates the end of input. Output For each test case, print: Case xxx: yyy where xxx is the case number (starting from 1), and yyy is the number of t(n) between L and U, inclusive, that are prime. Sample Input 4 1 2 0 3 0 7 0 0 2 0 0 8 100 1000 0 Sample Output Case 1: 6 Case 2: 25 Case 3: 3013 17 Cedarville University 2013 Programming Contest Problem Set Problem J Emergency Room Input: emergency.in An emergency room is serviced by a number of doctors. When patients arrive in the emergency room, their arrival time is recorded and they are assigned a number of treatments. Each treatment has a priority level and duration. For any particular patient, the priority levels of successive treatments form a strictly decreasing sequence, such as the numbers 8, 5, 3 in the following example: Treatment 1: priority = 8, duration = 10 units of time Treatment 2: priority = 5, duration = 25 units of time Treatment 3: priority = 3, duration = 15 units of time Each treatment must be performed by a doctor; different treatments for the same person do not need to be performed by the same doctor. Any particular doctor can treat only one patient at any time. All doctors of the facility will open their cubicles in the morning and become available at the same time. Some patients may have already arrived at that time, and additional patients may arrive subsequently. Whenever there is an available doctor, the patient with the highest priority (i.e., priority of the next treatment to be performed) will be selected from the waiting room and assigned to the available doctor. In case of a tie, the person with earliest original arrival time will be chosen. When more than one doctor is available, more than one patient may be admitted to the next treatment at the same time. When, for a particular patient, a treatment has been completed, but there are still remaining treatments pending, the patient will return to the waiting room and wait for his/her next turn. When all of a particular patient's scheduled treatments have been completed, the patient will be released from the facility. Input The input will contain data for several test cases. For each test case, the first line of input will contain three positive integers: the number of doctors at the facility, the number of patients which will be treated, and the clock reading at which the doctors become available in the morning. All times in the input will be expressed in some unspecified unit of time, as a single positive integer up to 1000. The remainder of the input for each test case will provide information about the patients. For each patient, the first line will state the arrival time and the number of treatments t they require. This is followed by t lines, ordered by strictly decreasing order of priority, specifying the priority and duration required for each of the treatments. No two patients have the same arrival time. The patients will be listed in increasing order of arrival times. A line containing three 0’s will mark the end the input. 18 Cedarville University 2013 Programming Contest Problem Set There will be at most 500 doctors and 500 patients in each case. The maximum priority and duration for any treatment is 100. Output After printing the case number, the output will display the time when each patient can be released from the emergency room. Patients will be identified by their arrival times; they will be listed in increasing order of release times. If there are several patients released at the same time, display them in the order of original arrival times. See the sample output for the exact format. Sample Input 1 3 50 10 3 10 5 5 20 4 5 30 3 25 10 8 5 5 5 110 1 20 10 2 3 50 10 3 10 5 5 20 4 5 30 3 25 10 8 5 5 5 110 1 20 10 0 0 0 Sample Output Case 1: Patient Patient Patient Case 2: Patient Patient Patient 30 released at clock = 95 10 released at clock = 100 110 released at clock = 120 30 released at clock = 70 10 released at clock = 80 110 released at clock = 120 19