Noip simulation competition by matrix67 solution report

Source: Internet
Author: User

Problem 1: matrix67 love letter (2)
As we all know, to see whether a number can be divided by two, you only need to check whether its single position can be divided by two. But have you ever wondered why? This is because 10 can be divisible by 2, so a number of 10a + B can be divisible by 2 and only when B can be divisible by 2. You also know that to see if a number can be divided by three, you only need to check whether the sum of the numbers can be divided by three. Why? The answer is more or less similar: Because 10 ^ N-1 is always divisible by 3. 2345 can be written as 2*(999 + 1) + 3*(99 + 1) + 4*(9 + 1) + 5, expand to 2*999 + 3*99 + 4*9 + 2 + 3 + 4 + 5. The items with the number 9 in the front must be divisible by three, so to see whether 2345 can be divisible by three, you only need to check whether 2 + 3 + 4 + 5 can be divisible by three. Of course, this technique can only be used in a 10-digit system, but similar conclusions can be extended to any-digit system.
Note that 36 is an integer multiple of 4, while Zzz... ZZ is always 555 by dividing 7... 55. That is to say, to judge whether a 36-digit number can be fully divided by four depends on its individual digits, while a 36-digit number can be fully divided by 7 and only when the number and the number can be fully divided by 7. If a number can be divisible by 4 and 7 at the same time, the number must be divisible by 28. The problem is converted to the number of consecutive sentences that satisfy the number and are multiples of 7, and the last number is a multiple of 4. In this way, we obtain an O (n) algorithm: using P [I] to represent the number of I Conditions for dividing the remainder of the first several sentences by 7, scan the entire article and constantly update the P array. When the last word in a sentence can be fully divided by four, assume that the prefix ending with this sentence and divide by more than 7 x, then, the value of P [x] is accumulated to the final output result (the numbers with two prefixes are the same as the number divided by seven, the portion with long prefixes must be divided into 7 ).
The above algorithms are my intention, but after the competition I saw a variety of other novel algorithms. For example, someone noticed that 36 ^ n mod 28 is always equal to 8. Using this property, a similar linear algorithm can be constructed. Some people use dynamic planning (or recursion) to solve this problem perfectly. We use F [I, j] to show the number of text fragments ending with sentence I divided by the remainder of 28; when processing the next sentence, we need to scan each different J, and add f [I-1, J] to the corresponding F [I, j. Finally, output the sum of all f [I, 0. This dynamic planning can use a rolling array, so its space is constant like the previous algorithm.
If you have no idea what I'm talking about, you can read articles related to the bitwise System and the same remainder. In addition, I have made a similar question (voj1090) before. You can take a look at it.
In addition, I am very sorry to tell you that the last data of this question is wrong. The first sentence of this data is an empty sentence (thanks to the reminder from AI. Freedom ). Many good comrades who scored 90 points in the first question are probably caused by my mistakes. I am sorry again. If the question mark at the beginning is removed, the output value is 19511110.

Problem 2: birthday gift for mm

We use F [I, j, k] to indicate whether the square with a side length of K meets the requirements. To F [I, j, k] = true, you must first meet F [I-1, J-1, K-2] to true (the gray part meets the requirements ). In addition, we also need to ensure that the two blue areas in the figure are equal, the two green areas are equal, and the four areas must also be symmetric. Because the status of the dynamic planning is already three-way, it is necessary to judge the following conditions in constant time. To this end, we can perform the following six preprocessing before dynamic planning:

Take the two grids (I, j) and (I ', j) of different rows in the same column as the right endpoint, and expand to the left to get the same number of equal regions.
Take the two grids (I, j) AND (I, j ') in different columns in the same row as the bottom, and expand up to get the same number of equal areas.
Centering on a grid (I, j), how many symmetric regions can be obtained by extending to the left and right sides
The number of symmetric regions that can be obtained by extending up or down with the grid (I, j) as the center
With two adjacent grids (I, J-1) and (I, j) as the center, extending to the left and right to obtain the length of the symmetric Area
Centering on two vertically adjacent grids (I-1, J) and (I, j), how long the symmetric area can be obtained by extending up and down

Each of the above preprocessing can be completed in three ways, and the decision of dynamic planning is reduced to a constant level, so the overall complexity is three aspects.

Similarly, this is just the intention of my problem. I have already thought that there should be many other algorithms for this question, but I did not expect that there would be so many. An algorithm that is easy to think of is to enumerate a grid (or an intersection) as the center after performing the same preprocessing as above, and expand it layer by layer. Although this complexity is still three-way, and is essentially the same as the previous algorithm, it is obviously more efficient, because you can stop the innermost loop when you cannot expand outward, continue to enumerate the next center.
It is also the center of the enumerated square. This problem can be solved only by using the last four preprocessing methods. We can find the largest legal square centered on a grid (or intersection) in a linear time. If the maximum side length of a square is K, this is equivalent to finding two squares (k + 1) DIV at the same time. As for how to find the largest square, let's leave it to everyone.
Cockhorse comes up with a very clever algorithm. After preprocessing, it can determine whether any given small square meets the requirements of the subject within the constant time. In F [I, j, k], the bottom edge is represented by the (I, j) In line I and the adjacent K-1 lattice (K in total) on the right, the maximum height of the symmetric left and right rectangle. Then when f [I, j + 1, K-2] is true and the (I, j) lattice is equal to (I, j + k-1) lattice, F [I, j, k] = f [I-1, J, K] + 1 (otherwise f [I, j, k] = 0 ). Similarly, G [I, j, k] is used to represent (J, I)... (J + k-1, I) as the right boundary, so that the maximum width of the rectangle is symmetric up and down. In this way, to determine whether any square meets the requirements of the subject, you only need to check whether its bottom edge and the largest symmetric area on the right can overwrite the square.
Of course, there are many other algorithms for this question. I will not list them here.

Problem 3: Spread of rumors
For a given graph, the point set composed of all vertices in the graph is called U, and a subset S not equal to the complete set is specified, then all one vertex in s another vertex in the U-S is called the "cut edge" of the s point set, because after removing all these edges, the s set will be isolated. This problem requires you to find an edge set E so that no matter what the s set is, the one with the smallest weight in the corresponding cut edge must be in the edge set E. Such an edge set must exist. For example, a set composed of all edges is an edge set that meets the conditions. We hope that you can find as few edges as possible in edge set E.
An incorrect greedy method is to remove the one with the smallest weight in the adjacent edge of each vertex. This algorithm is obviously incorrect. For example, the following is a fresh counterexample:

O ----- o
2 3 1

In, the edge of each vertex has a minimum weight of either 1 or 2, so that the edge is retained, so the s set is the two points on the left (or the two points on the right), and the cut edge is still a lot. It is easy to think that to remove the smallest weight in the cut edge of any s set, we must first ensure that edge set E is connected to all vertices. Otherwise, S is equal to any connected component, edge set E does not contain any cut edge. In this way, edge set E must have at least n-1 edges.
Considering that the optimal solution is at least n-1 edges, The n-1 edges must be connected to all vertices, and the weight of the selected edges must be small. Therefore, the final answer is probably the Minimum Spanning Tree. Now let us assume that we have selected some edges that form several connected components. Consider that all the edges connected to two different connected components (the edges of the two vertices are not in the same connected component) have the smallest weight value. This edge must be selected, otherwise, the s set is one of the connected components, and the question conditions cannot be met. Isn't that the Kruskal algorithm?
We have not proved that the Minimum Spanning Tree meets the condition for any s set. The proof is actually very simple. If the cut edge e with the smallest weight is not in the min spanning tree e, adding cut edge e will form a loop. This loop will start from a point in the s set, go through E to the U-S, and finally have to go back to the s set. Returning to the s set must go through another cut edge e ', while apparently e'> E (because E is the cut edge with the smallest weight, and the question says there is no edge with the same weight), so the edge set E + {e}-{e '} becomes a smaller spanning tree.

Problem 4: robot confession
The fourth question is the simplest question in the simulation competition. It does not require you to construct any algorithms. You only need to simulate the question. Just like last year's noip, the question of pure programming capability is not placed on the first question. This reminds everyone to read all the questions after getting the questions, especially when they see the last question, don't panic, it is very likely that their clothes are stripped away, I found that it was a raw question.
In order to speed up, a wall [I] [J] [k] array is pre-processed, indicating that column J of row I is directed to column K (1 <= k <= 4) whether or not it works. Then enumerate all possible command sequences, simulate the robot's walking process, and see if it can reach the terminal. In the simulation process, you need to use an array hash [I] [J] [k] to indicate that the robot had reached the position of column J of row I after the K Command of the sequence, the hash array is constantly updated during the simulation process. After a command, the robot has not reached the upper right corner, and the corresponding hash value is already true, then the robot's actions will start from here and will never be in the upper right corner.
Although this question does not require any pruning, I would like to explain it again. This topic has a very interesting pruning: The first command in the command sequence can only be U or R, and the last command can only be U or R. Think about why.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.