A. A Simple Test To celebrate the 10th anniversary of ZOJ, we are going to hold a simple test for our students, with a M*N rectangle containing some 1*1 blocks. The students are asked to select two blocks that have a "valid" path connecting each other. Then what does the word "valid" mean? Firstly, from one block, we can only go vertically or horizonally to another block. "valid selection" must conform to following constraints: Both of the positions must have blocks. They should NOT exist blocks on the path. Only two chances of changing direction from vertical to horizonal or horizonal to vertical when advancing. Path should NOT go outside rectangle. For each student, he/she has an initial score of 60. If his/her selection is not valid, his/her score will be substracted by 1. If the selection is valid, his/her score will be added by 2 and the two selected blocks will both be removed. In both situations the test would continue. Notice that when next student come to test, he/she will face a new rectangle having no relationship with the former one. Input There are multiple cases. Each case is the input of one student. The first line describes total row number m and total column number n of the rectangle.(1 ≤ m ≤ 300 and 1 ≤ n ≤ 300) The second line is the number of blocks b(2 ≤ b ≤ m*n) Then following b lines describe row cooridinate and column cooridinate of blocks. If one (or two) of the blocks doesn't exist, student's score will be substracted by 1 without deleting the exist block. After all above, there comes a line that contains the number of the student's inputs. The number is i.It's guaranteed i are non-negative number and will not exceed 1000. Then following i lines are student inputs, containing the row cooridinate and column cooridinate of two chosen blocks. Output For each input in every case , calculate and output the student score step by step. Sample Input 3 5 9 0 0 0 2 0 4 1 0 1 2 1 4 2 0 2 2 2 4 2 0 2 1 2 0 0 2 4 Sample Output 62 64 B. Back to the Past Recently poet Mr. po encountered a serious problem, rumor said some of his early poems are written by others. This brought a lot of trouble to Mr. po, so one day he went to his best friend MasterO for help. MasterO handed over a small wooden box with a silent smile. Mr. po checked the box, a word "YGBH" carved on the side. "The box can take you back to the past," MasterO said, "so you can get any evidence you need. But, before that, you need some patience." There are N tiny dark holes on both sides of the box (2N holes in total). Every day, for each hole, there is a possibility P to successfully absorb the power of moon and then magically sparkle. The possibilities among holes are independent in each day. Once a hole sparkles, it will never turn dark again. The box only works when there are no less than M sparkling holes on each side of the box. The question is that what is the expected number of days before the box is available. Input The input consists of several test cases. For each case there are 3 numbers in a line: N, M, P. 1 ≤ N ≤ 50, 1 ≤ M ≤ N, 0.01 ≤ P ≤ 1. A case with three zeros indicates the end of the input. Output For each test case, output the expected number of days, accurate up to six decimal places. Sample Input 2 1 1 1 1 0.5 0 0 0 Sample Output 1.000000 2.666667 C. Simple Path A path with no repeated vertices of an undirected graph is called a simple path. Given an undirected graph and two verteices S and D, return the number of vertics which don't lie on any simple paths between S and D. Input The input contains multiple test cases. Each case starts with a line of four integers, N(1 < N ≤ 100), M(1 ≤ M ≤ N(N - 1) / 2), S(0 ≤ S < N), D(0 ≤ D < N). N is the number of vertices, M is the number of edges, S andD are two different vertices. Then M lines follow, each line contains two different integers A(0 ≤ A < N) and B(0 ≤ B < N), which represents an edge of the graph. It's ensure that there is at least one simple path between S and D. Output Output the number of such vertics, one line per case. Sample Input 4 3 0 2 0 1 1 2 1 3 4 4 0 2 0 1 1 2 1 3 2 3 Sample Output 1 0 D. Distribution Network The United Federation of Planets has just improved their long range transporter technology which can be used for inter-planetary transportation. Being the largest manufacturer and distributor of genuine food (operated by Ferengis of course) you wish to take advantage of this opportunity the build a distribution network to lower your operation cost. So among all the planets you do business with you wish to choose a subset of them as production base and distribute to the rest of the planets. The cost of distribution is only proportional to the distance between two planets, and not related to the amount of food being transported. That being said, you can transport from production planet A to planet B and then from planet B to planet C to cover both B and C. To set up the network you only need to know which planets to select as production base. You somehow hosted a contest to ask the citizens of those planets to figure out the selection, partially because that's a great advertisement in itself. The problem is that you received tons of submissions and need a way to determine which of them deserve the prizes, and the task soon becomes a nightmare... Could you help to finish the task? Input Input has multiple test cases. First line of each test case has three integers N which is the number of planets, M which is the number of production planets to select, and K which is the number of submissions (2 ≤ N ≤ 500, 2 ≤ M ≤ 10, M ≤ N, 1 ≤ K ≤ 1,000,000). Next N lines each has three integers which is the three dimensional coordinate of each planet in light year (absolute value ≤ 10,000). Next K lines each has M integers which is one submission with each number referring to a planet given above. The index is 1 through N. Output Write a "Yes" or "No" for each submission meaning if the selection of production base is optimal that leads to lowest cost of transportation. Sample Input 5 3 3 0 0 0 1 1 1 2 2 2 4 4 4 5 5 5 1 2 3 1 3 4 1 4 5 Sample Output No Yes Yes E. Equivalent Expression In computer program, there is a kind of expression, called boolean expressions. In this kind of expressions, the variables have only two values, T (TRUE) or F (FALSE) while the result can only be T or F too. All the variables in this kind of expressions are connected with boolean operation (from the highest priority to the lowest priority): ! (not), & (and), | (or) and ^ (xor). The meaning of them are: 1. ! (not). This operation has one variable. !A means to get the logic negative value of A. If A is TRUE, result is FALSE. 2. & (and). This operation has two variables. The result of A&B is TRUE if and only if both A and B are TRUE. 3. | (or). This operation has two variables. The result of A|B is FALSE if and only if both A and B are FALSE. 4. ^ (xor). This operation has two variables. The result of A^B is TRUE if A and B are not equals. If A and B are equals, the result is FALSE. In the expressions, '(' and ')' are also used to get the higher priority inside the brackets. One boolean expression can have many equivalent forms. Two expressions are equivalent only if the results are the same no matter what value the variables are. Now your task is to write a program to check if two boolean expressions are equivalent. Input The expressions are in groups in the input. Each group has two lines and one expression in each line. There are at most 200 characters in each line, at least 2 variables in each expression and no variable named as "TRUE" or "FALSE". There are at most 15 variables for each group of expressions, and each variable is a case sensitive string with at most 10 letters. Please note space can be used at proper place in the expression. An empty line means the end of input. Output For each group, you only need to output TRUE or FALSE in one line indicates if two expressions are equivalent. Sample Input A&(B|C) (B|C)&A A & B B | A Sample Output TRUE FALSE F. Press the Button There are two keyboards for inputting password in a very old cave (supposedly full of treasure), a thief found this place and managed to get the password to enter. The thief used his left index finger to press the button on the left keyboard, and used his right index finger to press the button on the right keyboard. A layer of dust covered the buttons. Each time the thief pressed the button, the dust on his finger and on the button mixed to result in same amount of dust left on finger and button. At first, there is no dust on thief's fingers, and the amount of dust on each button is 1. If we already know how many times are pressed on each keyboard, the question is how many possible passwords are there if the dust on the buttons is properly measured. Assume the left keyboard has 3 buttons: A B C which pressed 2 times, the right keyboard has 2 buttons: X Y which pressed once. If the amount of dust on the buttons is: A:1 B:1/2 C:3/4 X:1/2 Y:1 The possible passwords are: BCX BXC XBC Input The input consists of several test cases. For each case there are 4 integers in the first line: N, M, P, Q. (1 ≤ N, M ≤ 5, 0 ≤ P, Q ≤ 10) N is the number of buttons on left keyboard, M is the number of buttons on right keyboard; P is the times of button pressed on left keyboard, Q is the times of button pressed on right keyboard. The following line of input contains N fractions indicate the amount of dust on the buttons of left keyboard, then a line contains M fractions indicate the amount of dust on the buttons of right keyboard. A test case started with four zeros indicates the end of the input. Input may contain empty lines. Output For each test case, output the number of possible passwords moduled by 10000007. Sample Input 3 2 2 1 1 1/2 3/4 1/2 1 3 3 0 0 1 2 1 1 1 2 0 0 0 0 Sample Output 3 0 G. Marlon's String Long long ago, there was a coder named Marlon. One day he picked two string on the street. A problem suddenly crash his brain... Let Si..j denote the i-th character to the j-th character of string S. Given two strings S and T. Return the amount of tetrad (a,b,c,d) which satisfy Sa..b + Sc..d = T , a≤b and c≤d. The operator + means concate the two strings into one. Input The first line of the data is an integer Tc. Following Tc test cases, each contains two line. The first line is S. The second line is T. The length of S and T are both in range [1,100000]. There are only letters in string S and T. Output For each test cases, output a line for the result. Sample Input 1 aaabbb ab Sample Output 9 H. MOD and MOD_IF ZOJ is 10 years old! For celebrating, ZOj has a new storage system. In this new storage system, data is organized as many records, and each record has several attributes. For example, there can be two records called R1 and R2 with two attributes 'age' and 'height': == age height R1 10 60 R2 30 175 Modification operation in this system is defined as two types: MOD and MOD_IF. MOD simply sets the given attribute with the given value. MOD_IF sets the given attribute with the given value only if the value of the attribute in "if" clause equals to the given value. For example, we have a list of operations: mod age 5 mod age 3 if height 175 When the operations applied to the previous data example, we will get: == age height R1 5 60 R2 3 175 In the system, when an operation list is submitted, the operations in the list will be applied on the first record in the system one by one. Then the second record in the system is chosen, all the operations apply to this record one by one. So on and so force, until all the records in the system are applied. Now we have our question here, can we remove some of the operations in the list while the remaining operations work just the same for any possible data record? Because of some flaws, the operation removal has the following principal. An operation X in the list can be removed if and only if in this situation: after all the previous operations are applied to any possible record, this operation X won't change anything. Input The input consists of several test cases. For each case there are n (1 ≤ n ≤ 100000) lines of operations followed by an "END". "OVER" indicates the end of the whole input. An operation can be either of format "mod ATTR VALUE" or "mod ATTR VALUE if ATTR VALUE", ATTR is a character within {'a','b','c','d','e'}, VALUE is integer in [0, 9]. Note: the two ATTR in mod_if can be same. There may be empty lines in the input. Output For each test case, output each operation, if the operation can be removed, output an "R" at the tail of the operation. Output a line with "END" at the end of each test case. Sample Input mod a 1 mod a 1 mod a 2 END mod a 1 if b 2 mod a 1 if b 2 END OVER Sample Output mod a 1 mod a 1 R mod a 2 END mod a 1 if b 2 mod a 1 if b 2 R END I. A Song of Anniversary Because of the 10th anniversary of ZOJ, Dai decides to write a beautiful song as the present. What's more, to let more people enjoy the anniversary, he is going to make abeatmap for the song in a famous music game - osu!. osu! is a rhythm-cliking music game. When you are playing osu!, some notes will appear on the screen. Each notes has a postion (x,y). The only thing you need to do is to click them with the rhythm. So it's impossible that two notes appear at the same time - Your mouse can't divide into two parts, can it? A beatmap is a file that storaged all information of notes. After several days, Dai finished his work - He made a wonderful beatmap for the special song! However, to make the beatmap being admitted to all people, he need to calculate the difficulty of the beatmap. How to calculate the difficulty? First of all, we need to rearrange the notes by time and divide them into k consecutive parts. For example, if there are some notes appear at time 1, time 2, time 4, time 5, and k is 2, a vaild dividing plan is (1,2), (4,5). Of course, (1), (2,4,5) is also vaild. And we define the distance of neighbour parts is the distance of the last note of previous part and the first note of next part. As below, the distance of (1) and (2,4,5) is the distance of note 1 and 2. Finally, the difficulty of a beatmap is the minimum distance of all distances of neighbour parts. Dai knows that the difficulty is different if he divides the beatmap in different ways. So please tell him what's the maximum difficulty he can get. Input The input contains several test cases. For each test case, the first line is a string "[HitObjects]", which means the beginning of a test case. The second line is a number k (1 ≤ k ≤ the number of notes), which means the beatmap need to cut into k parts. Then followed several lines, each line has the same formula [x],[y],[Time],[NewCombo],[Sound] (0 ≤ x,y ≤ 1000, 0 ≤ Time ≤ 231-1, 0 ≤ NewCombo ≤ 1, 0 ≤ Sound ≤ 100). The number of notes will not exceed 100001 (105+1) and all values in this formula are integers. There's no empty line between the test cases. Process to END_OF_FILE. By the way, you can find informations about NewCombo and Sound here: http://en.wikipedia.org/wiki/Osu!. Output For each test case, output a real number as the description required. The number should rounded to 3 digits after the decimal point. Sample Input [HitObjects] 1 100,100,1,0,2 100,100,2,0,2 100,100,3,0,2 [HitObjects] 2 100,100,1,0,2 100,100,2,0,2 100,100,3,0,2 Sample Output 0.000 0.000 J. -3+1 ZOJ is 10 years old! For celebrating, we are offering the easiest problem in the world to you. Recently we received a long sequence. We can modify the sequence once by the following two steps. 1. Choose any element in the sequence, say x(satisfying x ≥ 3), and subtract 3 from x. 2. Choose any element in the sequence, say x, and add 1 to x. Now, we want to know how many times at most the sequence can be modified. Input The input contains multiple test cases. For each case, the first line contains an integer n(1 ≤ n ≤ 20000). The second line contains n integers describing the sequence. All the numbers in the sequence are non-negative and not greater than 1000000. Output Output number of times at most the sequence can be modified, one line per case. Sample Input 1 10 2 10 11 Sample Output 4 10