In high school, the teacher gave us a question:
How many parts can a plane be divided into at most four circles?
All of our students began to draw circles, one, two, and three on paper ...... Consider all possible strange locations.
After we tried our best to figure 14 parts, the teacher asked: What about 10 circles?
Now we are all stupid. We have 10 circles. How can we draw them.
But there are also clever students who try to calculate: If a circle is 2, two circles are 4, and three circles are 8, but how can the four circles not be 16 ...... It does not seem feasible.
Finally, the teacher told us that we should learn how to deduce: two circles have at most two intersections, So assume that there are already N circles on the plane, when we draw n + 1 circle, it has at most 2n different intersections with the existing n circles. These points divide the n + 1 circle into 2n arc segments, and because each section of the arc splits its space into two parts, each section of the arc means adding a part. In summary, when n circles exist on the plane, you can add up to 2N parts to draw a circle.
After the teacher explained the inference method to us, I realized that the problem was so simple!
When I got in touch with computers and programming, I always tried to use this mathematical method to think about the problem. -- this is not wrong, however, the most common mistake I make is to ignore the computer's ability to solve problems that we cannot or are difficult to solve through "formulas" and "Inferences": because computers have extremely fast brains.
There is an ACM problem (http://acm.uva.es/p/v1/136.html) which is described as follows:
**************************************** ********************************
If the prime factor of a number contains only 2, 3, and 5, it is called "ugly number ". The following is a sequence of uugly numbers arranged from small to large:
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15 ,...
(According to our habits, here we count 1 as uugly number)
Now, please writeProgramObtain 1,500th ugly numbers.
**************************************** ********************************
I don't know what you thought when you saw this question?
(If you decide to think about it, please do not look down ^_^)
For more information, see the following ......
When there are too many other users, there are too many other users who want to handle these issues.
When I first saw this question, I first thought: I had to calculate the ugly number formula!
However, I have no idea how to push left or right. God, it seems that this question requires a relatively high level of mathematical knowledge! -- I think.
Well, since the formula cannot be pushed out, we have to check each number one by one,
If its prime factor only contains 2, 3, and 5, you can add them to a queue!
However, after careful consideration, it is found that this process is too time-consuming (ACM competition has a limitation on the program execution time, for example, 1 second). It is also a matter of splitting and determining the quality, and it is inevitable to time out at 1500.
So, is there any good way?
The answer is yes. Since it is difficult to "filter" uugly number from a natural number, how can we generate uugly number one by one?
Assume that a number N is ugly number, that is
N = 2 ^ x * 3 ^ y * 5 ^ Z, where x, y, z> = 0.
It is easy to see that 2n, 3n, and 5N are both ugly number.
In this case, we start from 1, multiply by 2, 3, and 5, and add each obtained number to the ugly number queue by size; and constantly perform the multiplication 2, 3, and 5 operations on the next number in the column by ugly number. So we will soon be able to get the sequence of the entire ugly number?
It's easy, isn't it?
(One thing to note here is how to determine the order of these generated uugly numbers. -- That is, when you generate a ugly number and add it to the end of the team, How do you ensure that there is no ugly number between it and the previous ugly number? This problem is left to friends who like research: P)
It seems that,AlgorithmAlthough it has a deep relationship with mathematics, it is sometimes different to use algorithms to solve problems and paper pen + mathematical mind to solve problems. ■