It's hot, Ali's question was really awesome last night

foreword

Hello everyone, my name is bigsai. I haven't seen you for a long time and I miss you very much.

Last night, a classmate took Ali's written test questions. After the written test, the classmate said that the written test was difficult, and told me one of the questions. I found it to be quite high-quality. Looking at this difficulty, it is the second question , a total of three questions still feel difficult (shivering), I thought I would share it with you.

9df1e5f3966f8d4dcc10ce6340717be7.png

describe

A regular m-gon, and he wants to know the number of acute-angled triangles in the middle of the polygon. (The vertices of the triangle should be on the vertices of the polygon)
The definition of different triangles: two triangles, as long as one point is not in the same position, they are different triangles.

Data range 3-10^7

analyze

When I saw this question yesterday, I thought it was relatively new and had a lot of details, but it took me a long time to figure it out after getting the question, so I will share it with you.

752ce626aaf81e938697e9e5804ede51.png
tossing

The description of the problem is simple, how many isosceles acute triangles are there in a regular polygon with side n.

How should we analyze this problem? Of course, let's draw a few case studies first.

26d80eb08236f62f37ee79c52582392e.png

Looking at it this way, you may feel like there is no pattern.

You may be a little confused about odd numbers: every straight side of an odd number corresponds to a point, so how many isosceles acute triangles have as many sides?

But this is obviously wrong. It is possible to use different vertices as an isosceles acute triangle, which happens to be an equilateral triangle, so there will be repetitions , and then there may be a base that is not exactly an edge of a polygon, but a polygon. Multiple sides form the bottom side (refer to the regular 6-sided type in the above figure).

And from this point of view, the odd-numbered side and the even-numbered side are still a bit different: from a straight point of view, the odd-numbered is point-to-side, and the even-numbered is point-to-point . There are some differences in structure, so it is possible that the odd and even results are a little different.

The above is a ignorant state, let's sort out useful information:

  • Regular polygon, how many edges are how many vertices

  • Regular polygons are axisymmetric, and isosceles acute triangles are axisymmetric.

  • An acute isosceles triangle has two sides and one base, either from the base or from the vertex . Here, the base is obviously not easy to consider, such as the regular 6-sided or even more polygons above, but each vertex is not easy to consider. They are all vertices of polygons, so they must be considered from vertices. Calculate all triangles whose vertices are acute isosceles angles, multiply by the number of vertices (such as regular pentagons) and then subtract duplicates (such as regular 3 and regular 6 polygons) to get the total result .

  • Odd and even numbers are discussed separately.

even case

Let's analyze the even-numbered situation first, without considering the repeated situation (considering too much brain confusion), and put the graph like this:

5b6af70ab7d5557237f85bb943b07693.png

Because it is a regular polygon, it is equivalent to that each vertex is on a circle, which makes it easier to analyze whether it is an acute angle. By analyzing each point in this way , it is easy to see how many acute angles each vertex corresponds to. Positive 6, positive 8 Each vertex corresponds to an acute angle. In fact, some people may have seen the rule, that is , the lines below the right angle can form an acute angle .

For a clearer observation, look at the following diagram for a clearer view:

91d34a8c6c8d914b849ed8a24fa79844.png

In fact, it is to calculate the number of blue lines below the right angle. This intuitive point is half of the number of all horizontal lines (rounded down) . Each horizontal line consists of two points. Let's calculate:

A total of n points, remove the two vertices and the bottom single point, the line composed of n-2 points (n-2)/2. Among them, the bottom half of the right angle is [(n-2)/2]/2=(n-2)/4a, and then multiplied by the number of vertices n, then in the case of even numbers, the number of all isosceles acute triangles is not considered to be repeated:

total=n*((n-2)/4))

odd case

Going back to the analysis of even numbers, the odd number is also very simple. The odd vertices are unchanged and the bases are fixed, that is, to find the number of sides that can form an acute triangle.

27d704d6e95b2f1c6e47220ab392c4dc.png

For the number of horizontal lines, if it is an even number, there is no doubt that the following is an acute angle (there are many vertices above, so the angle is smaller than a right angle), and the line in the middle of an odd number is also an acute angle (if the vertex is removed, it is in the center), so this situation is Half the number of horizontal lines (rounded up) , let's calculate:

A total of n points, one vertex, n-1 points form a line. Then there is (n-1)/2a line in which half of the acute triangles are rounded up and that is (n-1)/2+1, and then multiplied by the number of vertices n, then in the odd case, all the isosceles acute triangles are not considered repeated and the number is:

total=n*(((n-1)/2+1)/2))

repeating triangle

The above considers the case of non-repeating triangles, then how to consider repeated triangles. It is seen above that equilateral triangles and regular hexagons all encounter equilateral triangles, resulting in repeated calculations.

And positive 5 and positive 7 seem to have no repeating equilateral triangles.

Let’s analyze it carefully: the three vertices of an isosceles acute triangle are all on the side of a regular polygon, and in fact, they are also on a circle. If an equilateral triangle is formed, it means that the three vertices can divide the space equally (that is, the vertices, the circle can be divided into three equal parts). Then the number of vertices n must be a multiple of three !

The number of vertices n is a multiple of 3, so how many are repeated?

Just look at each of these equilateral triangles as vertices, there should be n, but each case occurs three times , so only consider equilateral triangles with 1/3 of them as vertices to avoid repetition! So we want to remove (2/3) of n from the total number of times.

Then the total number of times:

total-=(n/3)*2;

specific code

There is a small pit in the code here, n is 10^7, then this order of magnitude will exceed the range of int, you need to use long, but some people use int to calculate such a code:

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();

    long total;
    if(n%2==0)
       total=n*((n-2)/4);
    else
       total=n*(((n-1)/2+1)/2);
    if(n%3==0){
            total-=(n/3)*2;
    }
    System.out.println(total);
}

But it's still wrong. Some people don't know why. The main reason is that the following two sentences are out of bounds:

total=n*((n-2)/4);
total=n*(((n-1)/2+1)/2);

Some people say that I have set total to long, why is it wrong, because the assignment operator calculates the right side first, which is n*((n-2)/4)equivalent to such a process:

int temp=n*((n-2)/4);
long total=temp;

Because the calculated value range is int, the final result is that the int has crossed the bounds, and then the out-of-bounds int result is assigned to the total of the long range.

Similar to this one:

double a=3/2;
 System.out.println(a);

It will output 1.0 instead of 1.5. If you want to calculate correctly, you need to convert the calculated value to double calculation in advance:

double a=(double) 3/2;

The correct code for the same problem is:

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    long total;
    if(n%2==0)
      total=(long)n*((n-2)/4);
    else
      total=(long)n* (((n-1)/2+1)/2);
    if(n%3==0){
      total-=(n/3)*2;
    }
    System.out.println(total);
}

Summarize

This kind of math problems, or problems looking for regularities, may still be encountered occasionally. The more you know, the more experience you have! Currently it is still based on accumulation.

Let's cheer together, and if you need it, you are also welcome to punch and buckle together.

I'm bigsai, I have a data structure and algorithm pdf and a dynamic programming pdf

Welcome to add  "bigsai66", note [pdf] to receive

bac129d2d4bc662d5925dfac606ad1ee.png

It's not easy to be original, I hope you pass by Fairy Yanzu and like it and watch it again

Guess you like

Origin blog.csdn.net/qq_40693171/article/details/123304080