Yuanfang is walking on a chain. The chain has n nodes numbered from 1 to n. Every second, he can move from node i to node j with probability:
c(i,j) is an element in a given parameter matrix which is n×m. (1 <= c(i, j) <= 9) Yuanfang wants to know the expectation time for him to walk from node 1 to node n.
There are no more than 10 test cases. In each case, there are two integers n (2 <= n <= 50000), m (1 <= m <= 5), in the first line, meaning that there are n nodes and the parameter matrix is n×m . There are m integers in each of the next n lines which describe the parameter matrix . The input ends with 0 0.
For each case, output the expectation time for Yuanfang to walk from node 1 to node n in one line. The answer should be rounded to 2 digits after decimal point.
3 1 1 1 1 5 2 1 2 2 1 3 2 2 3 1 3 0 0
6.94 8.75
5.
Worm
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
Guy is a flying worm. A gun men are hunting him. Guy can't keep running all the time . At night ,he has to find a place to sleep, and that's very dangerous. In the sky, there are many flying rocks, Guy decides to stay on those rocks at night. The good thing is that the gun man is stupid, he always fire toward a fixed direction. And Guy knows the direction. Guy wants to find a place on a certain rock so that the rock can protect him best from the bullets. The more distance a bullet has to travel inside the rock to reach Guy, the safer is Guy. Please find the safest place for Guy. Guy's world is a 2D plane. A rock can be consider as a polygon. Guy can just stay on the edge of the rock, he can't get inside a rock. Guy stands on a rock ,but he can't hang on a rock. As shown below, Guy can stay on point B,C and D, but he can't stay on point A,E,F and G because if he stay there, he will fall down to the ground when he get asleep.
The direction of the gun fire can be described as a vector (d
x, d
y). The gun man is infinite far away, and he can choose any infinite far away point as the firing position. In other words, the bullet may fly along any straight line which is parallel to the vector (d
x,d
y).
Input
First Line is an integer T <=20, means the number of test cases. In each test cases, the first line is one integer m describing the number of polygons. Then m polygons follow. Each Polygon P starts with an integer n as the number of vertex of P. after that there are n lines, each line is an integer pair (x, y) , meaning a vertex of P, in clockwise or counter-clockwise order. The last line of each test case is two integers d
x, d
y, meaning that vector (d
x,d
y) is the direction of the gun fires. The sum of the vertexes number of all polygons is smaller or equal to 1000. The polygons will not intersect with each other or themselves. One polygon cannot be inside another. There may be extra spaces and empty lines in the input data. For more detail, see the sample input.
Output
Output one line for each test case ---- the distance a bullet has to travel inside the rock to reach Guy when Guy stays on the safest place. If the bullet can hit Guy directly, output “Poor guy” instead. The result must be round to 2 digits after decimal point.
Sample Input
3 1 6 0 0 0 1 -1 1 -1 -1 1 -1 1 0 0 1 1 6 0 0 0 1 -1 1 -1 -1 1 -1 1 0 0 -1 3 4 0 0 0 1 2 1 2 0 4 0 2 0 3 1 3 1 2 4 1 3 1 4 2 4 2 3 0 -1
Sample Output
2.00 Poor guy 1.00
Hint
In the first sample input, Guy could only stand on two segments: {(-1,1)-(0,1)}, and{(0,0)-(1,0)} Notice that, Guy cannot stand on (-1,1),(0,1) and (1,0).
For the firing direction dx = 0, dy = 1, we could see that the safest place is any point on segment (-1,1) - (0,1) (except two ends). And a bullet has two travel distance 2 inside the rock to hit Guy. Please do pay attention to the test case 2 and 3.
6.
Microblog Subscription
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
Jia Baoyu was the best programmer in his big family—the Family of Jia, which is why he was regarded as the most charming gentleman among girls. Recently he was designing a new microblog to target the requirements of sharing information in his family. In his system, a microblog can be regarded as a document contains no more than 2000 words. By asked to other member’s in the Family of Jia, he decided to release a function of “subscription” in his microblog. Let’s describe this function more specifically: A user can submit a query to describe subscription requirement. A query contains a match type, a match distance constraint and a set of not more than 5 English words. After the user submitted the query, he/she will receive all the new microblog that satisfies the query in his/her “Feeds stream”. If a microblog satisfies a query, it should match all the words in the query. And a microblog matches a word X when there is at least a word in microblog that matches the word X. Note that the match constraint is applied independently for each word in the query. There are three types of word matching: exact match, approximate match under a Hamming distance constraint and approximate match under an edit distance. The definitions of these three matches is are follows:
Exact match: two words are exactly the same;
Approximate match under Hamming distance D constraint:two words with the same length have at most D positions at which corresponding letters are different.
Approximate match under edit distance D constraint:one word can be changed into the other word in at most D single-letter edits, including insertion, deletion and substitution. For example, Lin Daiyu submitted a query: edit distance match with distance 1, and the word set “flower poem tear”. Then she will receive the microblog “I wrote a pom full of tears after I saw Daiyu buried the flowers”. In this case, “flower” can be matched with “flowers” in 1 edit distance, “tear” can be matched with “tears” and “poem” can be matched with “pom” (obviously it was a typo). After a user submitted a query, he/she could delete the query in anytime. After that he/she would not receive microblogs that satisfy this query. Now please help Jia Baoyu to accomplish this function.
Input
The first line of input file contains an integer representing the following number of lines. Each of the following line has three formats:
s [id] [type] [dist] [k] [word_1] [word_2] … [word_k] The line that starts with lowercase ‘s’ means adding a query into system. [id] is an integer between 1 to 1000 representing the id of the query. [type] is an integer between 0 to 2 representing the match type (0 means exact match, 1 means hamming distance match and 2 means edit distance match). [dist] is an integer representing the distance constraint (Note that the distance is always 0 for exact match). This distance constraint is at most 2. [k] means the number of following words (0
e [id] The line that starts with lowercase ‘e’ means deleting a query from system. [id] is the id of that query. We guarantee that the id always exist and has not deleted before.
m [id] [k] [word_1] [word_2] … [word_k] The line that starts with lowercase ‘m’ means a microblog. [id] is an integer between 1 to 100 which means the id of the microblog. [k] is an integer meaning the number of words that follows (0
Output
For each microblog in the input file, output all the matched active queries when it published. The “active” means that query has been submitted and not deleted yet. The format is:
[id] [k] [q_1] [q_2] … [q_k] [id] means the id of this microblog. [k] means the number of queries that it satisfies. [q_i] means a query id. Query ids must be sorted increasingly. The order of microblog in output file must be the same as it is displayed in the input file.
Sample Input
6 s 1 1 2 1 bkple m 2 1 apple s 2 0 0 2 apple banana m 1 2 apple banana e 1 m 3 2 apple banana
Sample Output
2 1 1 1 2 1 2 3 1 2
7.
DFS spanning tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
Consider a Depth-First-Search(DFS) spanning tree T of a undirected connected graph G, we define a T-Simple Circle as a path v
1, v
2, ..., v
k (v
1 = v
k) in G that contains at most one edge which not belongs to the DFS spanning tree T. Given a graph G, we process DFS on G starting from vertex 1 and get a DFS spanning tree T, then you should choose some edges of G so that all T-Simple Circles contains at least one edge that you choose. Please minimize the number of edges you choose.
Input
There are at most 100 test cases. For each case, the first line contains two integers n and m denoting the number of vertices and edges. The vertexes are numbered from 1 to n. The following m lines describe the graph. Each line contains two integers x
i and y
i, denoting an edge between vertex x
i and y
i(x
i ≠ y
i). Note that the first n-1 edges of input construct a DFS spanning tree T which is generated by DFS from vertex 1. Input ends with n = 0 and m = 0 (1 <= n <= 2000, 1 <= m <= 20000, 1 <= x
i, y
i <= n)
Output
For each case, output the number of minimal edges that you choose.
Sample Input
4 5 1 2 4 2 3 2 1 3 4 1 10 14 1 2 2 9 1 3 3 4 4 10 3 5 5 7 5 8 3 6 9 1 1 10 1 6 7 3 8 3 0 0
Sample Output
1 3
Hint
Here is the graph G in the first test case:
The solid lines denote the edge which belongs to the DFS spanning tree, and the dotted lines denote the others. For first case, we can choose one edge (1, 2) For second case, we can choose three edges (1, 2), (1, 3), (3, 5)
8.
Coffee shop in Jioufen
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65535/65536 K (Java/Others) Total Submission(s): 0 Accepted Submission(s): 0
Problem Description
Cheer manages a coffee shop in Jioufen. As she is the only assistant in the shop, it is a big problem for her to mix up raw materials into a cup of coffee. To simplify the problem, we can assume that Cheer has n kinds of raw material, and the i-th raw material has been assigned with a value a
i denoting its level of deliciousness. For some unknown reason, Cheer believe that a kind of coffee is perfect if and only if the value of its all kinds of raw materials are co-prime with each other. A kind of perfect coffee also can be made without any of these raw materials, only by water. Now given n kinds of raw material and their value of deliciousness, find out how many distinct kinds of perfect coffee can be made by them. Note that two kinds of coffee are considered distinct if and only if the raw material sets of them are distinct.
Input
There are at most 75 test cases. For each case, The first line contains one integer n indicating the number of kind of raw material. The next line contains n integers separated by space, denoting deliciousness values: a
1, a
2, ..., a
n (1 <= n <= 60, 1 <= a
i <= 2
31-1) The input ends with n = 0
Output
For each test case, print the number of distinct kind of prefect coffee she can made in one line.
Sample Input
Sample Output
3 8 6
Hint
For the first case, there 3 distinct kinds of perfect coffee: none of these raw materials, only with the first kind of raw material, and only with the second kind of raw material. Coffee with both the first kind of raw material and the second is not considered as perfect coffee, because the deliciousness levels of first kind of raw material and the second are not co-prime.
9.
Hululu and Cululu are two pacific ocean countries made up of many islands. These two country has so many years of friendship so they decide to build bridges to connect their islands. Now they want to build the first bridge to connect an island of Hululu and an island of Culuu . Their world can be considered as a matrix made up of three letters 'H','C' and 'O'.Every 'H' stands for an island of Hululu, every 'C' stands for an island of Cululu, and 'O' stands for the ocean. Here is a example:
The coordinate of the up-left corner is (0,0), and the down-right corner is (4, 3). The x-direction is horizontal, and the y-direction is vertical. There may be too many options of selecting two islands. To simplify the problem , they come up with some rules below: 1. The distance between the two islands must be as short as possible. If the two islands' coordinates are (x1,y1) and (x2,y2), the distance is |x1-x2|+|y1-y2|. 2. If there are more than one pair of islands satisfied the rule 1, choose the pair in which the Hululu island has the smallest x coordinate. If there are still more than one options, choose the Hululu island which has the smallest y coordinate. 3.After the Hululu island is decided, if there are multiple options for the Cululu island, also make the choice by rule 2. Please help their people to build the bridge.
There are multiple test cases. In each test case, the first line contains two integers M and N, meaning that the matrix is M×N ( 2<=M,N <= 40). Next M lines stand for the matrix. Each line has N letters. The input ends with M = 0 and N = 0. It's guaranteed that there is a solution.
For each test case, choose two islands, then print their coordinates in a line in following format: x1 y1 x2 y2 x1,y1 is the coordinate of Hululu island, x2 ,y2 is the coordinate of Cululu island.
4 4 CHCH HCHC CCCO COHO 5 4 OHCH HCHC CCCO COHO HCHC 0 0
10.
Shaolin temple is very famous for its Kongfu monks.A lot of young men go to Shaolin temple every year, trying to be a monk there. The master of Shaolin evaluates a young man mainly by his talent on understanding the Buddism scripture, but fighting skill is also taken into account. When a young man passes all the tests and is declared a new monk of Shaolin, there will be a fight , as a part of the welcome party. Every monk has an unique id and a unique fighting grade, which are all integers. The new monk must fight with a old monk whose fighting grade is closest to his fighting grade. If there are two old monks satisfying that condition, the new monk will take the one whose fighting grade is less than his. The master is the first monk in Shaolin, his id is 1,and his fighting grade is 1,000,000,000.He just lost the fighting records. But he still remembers who joined Shaolin earlier, who joined later. Please recover the fighting records for him.
There are several test cases. In each test case: The first line is a integer n (0
Output
A fight can be described as two ids of the monks who make that fight. For each test case, output all fights by the ascending order of happening time. Each fight in a line. For each fight, print the new monk's id first ,then the old monk's id.
Sample Input
Sample Output